1 //===- ValueTrackingTest.cpp - ValueTracking tests ------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/Analysis/ValueTracking.h"
10 #include "llvm/Analysis/AssumptionCache.h"
11 #include "llvm/AsmParser/Parser.h"
12 #include "llvm/IR/ConstantRange.h"
13 #include "llvm/IR/Dominators.h"
14 #include "llvm/IR/Function.h"
15 #include "llvm/IR/InstIterator.h"
16 #include "llvm/IR/Instructions.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/KnownBits.h"
21 #include "llvm/Support/SourceMgr.h"
22 #include "gtest/gtest.h"
23 
24 using namespace llvm;
25 
26 namespace {
27 
28 static Instruction &findInstructionByName(Function *F, StringRef Name) {
29   for (Instruction &I : instructions(F))
30     if (I.getName() == Name)
31       return I;
32 
33   llvm_unreachable("Expected value not found");
34 }
35 
36 class ValueTrackingTest : public testing::Test {
37 protected:
38   std::unique_ptr<Module> parseModule(StringRef Assembly) {
39     SMDiagnostic Error;
40     std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context);
41 
42     std::string errMsg;
43     raw_string_ostream os(errMsg);
44     Error.print("", os);
45     EXPECT_TRUE(M) << os.str();
46 
47     return M;
48   }
49 
50   void parseAssembly(StringRef Assembly) {
51     M = parseModule(Assembly);
52     ASSERT_TRUE(M);
53 
54     F = M->getFunction("test");
55     ASSERT_TRUE(F) << "Test must have a function @test";
56     if (!F)
57       return;
58 
59     A = &findInstructionByName(F, "A");
60     ASSERT_TRUE(A) << "@test must have an instruction %A";
61   }
62 
63   LLVMContext Context;
64   std::unique_ptr<Module> M;
65   Function *F = nullptr;
66   Instruction *A = nullptr;
67 };
68 
69 class MatchSelectPatternTest : public ValueTrackingTest {
70 protected:
71   void expectPattern(const SelectPatternResult &P) {
72     Value *LHS, *RHS;
73     Instruction::CastOps CastOp;
74     SelectPatternResult R = matchSelectPattern(A, LHS, RHS, &CastOp);
75     EXPECT_EQ(P.Flavor, R.Flavor);
76     EXPECT_EQ(P.NaNBehavior, R.NaNBehavior);
77     EXPECT_EQ(P.Ordered, R.Ordered);
78   }
79 };
80 
81 class ComputeKnownBitsTest : public ValueTrackingTest {
82 protected:
83   void expectKnownBits(uint64_t Zero, uint64_t One) {
84     auto Known = computeKnownBits(A, M->getDataLayout());
85     ASSERT_FALSE(Known.hasConflict());
86     EXPECT_EQ(Known.One.getZExtValue(), One);
87     EXPECT_EQ(Known.Zero.getZExtValue(), Zero);
88   }
89 };
90 
91 }
92 
93 TEST_F(MatchSelectPatternTest, SimpleFMin) {
94   parseAssembly(
95       "define float @test(float %a) {\n"
96       "  %1 = fcmp ult float %a, 5.0\n"
97       "  %A = select i1 %1, float %a, float 5.0\n"
98       "  ret float %A\n"
99       "}\n");
100   expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false});
101 }
102 
103 TEST_F(MatchSelectPatternTest, SimpleFMax) {
104   parseAssembly(
105       "define float @test(float %a) {\n"
106       "  %1 = fcmp ogt float %a, 5.0\n"
107       "  %A = select i1 %1, float %a, float 5.0\n"
108       "  ret float %A\n"
109       "}\n");
110   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true});
111 }
112 
113 TEST_F(MatchSelectPatternTest, SwappedFMax) {
114   parseAssembly(
115       "define float @test(float %a) {\n"
116       "  %1 = fcmp olt float 5.0, %a\n"
117       "  %A = select i1 %1, float %a, float 5.0\n"
118       "  ret float %A\n"
119       "}\n");
120   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, false});
121 }
122 
123 TEST_F(MatchSelectPatternTest, SwappedFMax2) {
124   parseAssembly(
125       "define float @test(float %a) {\n"
126       "  %1 = fcmp olt float %a, 5.0\n"
127       "  %A = select i1 %1, float 5.0, float %a\n"
128       "  ret float %A\n"
129       "}\n");
130   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, false});
131 }
132 
133 TEST_F(MatchSelectPatternTest, SwappedFMax3) {
134   parseAssembly(
135       "define float @test(float %a) {\n"
136       "  %1 = fcmp ult float %a, 5.0\n"
137       "  %A = select i1 %1, float 5.0, float %a\n"
138       "  ret float %A\n"
139       "}\n");
140   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true});
141 }
142 
143 TEST_F(MatchSelectPatternTest, FastFMin) {
144   parseAssembly(
145       "define float @test(float %a) {\n"
146       "  %1 = fcmp nnan olt float %a, 5.0\n"
147       "  %A = select i1 %1, float %a, float 5.0\n"
148       "  ret float %A\n"
149       "}\n");
150   expectPattern({SPF_FMINNUM, SPNB_RETURNS_ANY, false});
151 }
152 
153 TEST_F(MatchSelectPatternTest, FMinConstantZero) {
154   parseAssembly(
155       "define float @test(float %a) {\n"
156       "  %1 = fcmp ole float %a, 0.0\n"
157       "  %A = select i1 %1, float %a, float 0.0\n"
158       "  ret float %A\n"
159       "}\n");
160   // This shouldn't be matched, as %a could be -0.0.
161   expectPattern({SPF_UNKNOWN, SPNB_NA, false});
162 }
163 
164 TEST_F(MatchSelectPatternTest, FMinConstantZeroNsz) {
165   parseAssembly(
166       "define float @test(float %a) {\n"
167       "  %1 = fcmp nsz ole float %a, 0.0\n"
168       "  %A = select i1 %1, float %a, float 0.0\n"
169       "  ret float %A\n"
170       "}\n");
171   // But this should be, because we've ignored signed zeroes.
172   expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true});
173 }
174 
175 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero1) {
176   parseAssembly(
177       "define float @test(float %a) {\n"
178       "  %1 = fcmp olt float -0.0, %a\n"
179       "  %A = select i1 %1, float 0.0, float %a\n"
180       "  ret float %A\n"
181       "}\n");
182   // The sign of zero doesn't matter in fcmp.
183   expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, true});
184 }
185 
186 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero2) {
187   parseAssembly(
188       "define float @test(float %a) {\n"
189       "  %1 = fcmp ogt float %a, -0.0\n"
190       "  %A = select i1 %1, float 0.0, float %a\n"
191       "  ret float %A\n"
192       "}\n");
193   // The sign of zero doesn't matter in fcmp.
194   expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false});
195 }
196 
197 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero3) {
198   parseAssembly(
199       "define float @test(float %a) {\n"
200       "  %1 = fcmp olt float 0.0, %a\n"
201       "  %A = select i1 %1, float -0.0, float %a\n"
202       "  ret float %A\n"
203       "}\n");
204   // The sign of zero doesn't matter in fcmp.
205   expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, true});
206 }
207 
208 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero4) {
209   parseAssembly(
210       "define float @test(float %a) {\n"
211       "  %1 = fcmp ogt float %a, 0.0\n"
212       "  %A = select i1 %1, float -0.0, float %a\n"
213       "  ret float %A\n"
214       "}\n");
215   // The sign of zero doesn't matter in fcmp.
216   expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false});
217 }
218 
219 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero5) {
220   parseAssembly(
221       "define float @test(float %a) {\n"
222       "  %1 = fcmp ogt float -0.0, %a\n"
223       "  %A = select i1 %1, float %a, float 0.0\n"
224       "  ret float %A\n"
225       "}\n");
226   // The sign of zero doesn't matter in fcmp.
227   expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, false});
228 }
229 
230 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero6) {
231   parseAssembly(
232       "define float @test(float %a) {\n"
233       "  %1 = fcmp olt float %a, -0.0\n"
234       "  %A = select i1 %1, float %a, float 0.0\n"
235       "  ret float %A\n"
236       "}\n");
237   // The sign of zero doesn't matter in fcmp.
238   expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true});
239 }
240 
241 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero7) {
242   parseAssembly(
243       "define float @test(float %a) {\n"
244       "  %1 = fcmp ogt float 0.0, %a\n"
245       "  %A = select i1 %1, float %a, float -0.0\n"
246       "  ret float %A\n"
247       "}\n");
248   // The sign of zero doesn't matter in fcmp.
249   expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, false});
250 }
251 
252 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero8) {
253   parseAssembly(
254       "define float @test(float %a) {\n"
255       "  %1 = fcmp olt float %a, 0.0\n"
256       "  %A = select i1 %1, float %a, float -0.0\n"
257       "  ret float %A\n"
258       "}\n");
259   // The sign of zero doesn't matter in fcmp.
260   expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true});
261 }
262 
263 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero1) {
264   parseAssembly(
265       "define float @test(float %a) {\n"
266       "  %1 = fcmp ogt float -0.0, %a\n"
267       "  %A = select i1 %1, float 0.0, float %a\n"
268       "  ret float %A\n"
269       "}\n");
270   // The sign of zero doesn't matter in fcmp.
271   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, true});
272 }
273 
274 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero2) {
275   parseAssembly(
276       "define float @test(float %a) {\n"
277       "  %1 = fcmp olt float %a, -0.0\n"
278       "  %A = select i1 %1, float 0.0, float %a\n"
279       "  ret float %A\n"
280       "}\n");
281   // The sign of zero doesn't matter in fcmp.
282   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, false});
283 }
284 
285 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero3) {
286   parseAssembly(
287       "define float @test(float %a) {\n"
288       "  %1 = fcmp ogt float 0.0, %a\n"
289       "  %A = select i1 %1, float -0.0, float %a\n"
290       "  ret float %A\n"
291       "}\n");
292   // The sign of zero doesn't matter in fcmp.
293   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, true});
294 }
295 
296 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero4) {
297   parseAssembly(
298       "define float @test(float %a) {\n"
299       "  %1 = fcmp olt float %a, 0.0\n"
300       "  %A = select i1 %1, float -0.0, float %a\n"
301       "  ret float %A\n"
302       "}\n");
303   // The sign of zero doesn't matter in fcmp.
304   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, false});
305 }
306 
307 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero5) {
308   parseAssembly(
309       "define float @test(float %a) {\n"
310       "  %1 = fcmp olt float -0.0, %a\n"
311       "  %A = select i1 %1, float %a, float 0.0\n"
312       "  ret float %A\n"
313       "}\n");
314   // The sign of zero doesn't matter in fcmp.
315   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, false});
316 }
317 
318 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero6) {
319   parseAssembly(
320       "define float @test(float %a) {\n"
321       "  %1 = fcmp ogt float %a, -0.0\n"
322       "  %A = select i1 %1, float %a, float 0.0\n"
323       "  ret float %A\n"
324       "}\n");
325   // The sign of zero doesn't matter in fcmp.
326   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true});
327 }
328 
329 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero7) {
330   parseAssembly(
331       "define float @test(float %a) {\n"
332       "  %1 = fcmp olt float 0.0, %a\n"
333       "  %A = select i1 %1, float %a, float -0.0\n"
334       "  ret float %A\n"
335       "}\n");
336   // The sign of zero doesn't matter in fcmp.
337   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, false});
338 }
339 
340 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero8) {
341   parseAssembly(
342       "define float @test(float %a) {\n"
343       "  %1 = fcmp ogt float %a, 0.0\n"
344       "  %A = select i1 %1, float %a, float -0.0\n"
345       "  ret float %A\n"
346       "}\n");
347   // The sign of zero doesn't matter in fcmp.
348   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true});
349 }
350 
351 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZeroVecUndef) {
352   parseAssembly(
353       "define <2 x float> @test(<2 x float> %a) {\n"
354       "  %1 = fcmp ogt <2 x float> %a, <float -0.0, float -0.0>\n"
355       "  %A = select <2 x i1> %1, <2 x float> <float undef, float 0.0>, <2 x float> %a\n"
356       "  ret <2 x float> %A\n"
357       "}\n");
358   // An undef in a vector constant can not be back-propagated for this analysis.
359   expectPattern({SPF_UNKNOWN, SPNB_NA, false});
360 }
361 
362 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZeroVecUndef) {
363   parseAssembly(
364       "define <2 x float> @test(<2 x float> %a) {\n"
365       "  %1 = fcmp ogt <2 x float> %a, zeroinitializer\n"
366       "  %A = select <2 x i1> %1, <2 x float> %a, <2 x float> <float -0.0, float undef>\n"
367       "  ret <2 x float> %A\n"
368       "}\n");
369   // An undef in a vector constant can not be back-propagated for this analysis.
370   expectPattern({SPF_UNKNOWN, SPNB_NA, false});
371 }
372 
373 TEST_F(MatchSelectPatternTest, VectorFMinimum) {
374   parseAssembly(
375       "define <4 x float> @test(<4 x float> %a) {\n"
376       "  %1 = fcmp ule <4 x float> %a, \n"
377       "    <float 5.0, float 5.0, float 5.0, float 5.0>\n"
378       "  %A = select <4 x i1> %1, <4 x float> %a,\n"
379       "     <4 x float> <float 5.0, float 5.0, float 5.0, float 5.0>\n"
380       "  ret <4 x float> %A\n"
381       "}\n");
382   // Check that pattern matching works on vectors where each lane has the same
383   // unordered pattern.
384   expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false});
385 }
386 
387 TEST_F(MatchSelectPatternTest, VectorFMinOtherOrdered) {
388   parseAssembly(
389       "define <4 x float> @test(<4 x float> %a) {\n"
390       "  %1 = fcmp ole <4 x float> %a, \n"
391       "    <float 5.0, float 5.0, float 5.0, float 5.0>\n"
392       "  %A = select <4 x i1> %1, <4 x float> %a,\n"
393       "     <4 x float> <float 5.0, float 5.0, float 5.0, float 5.0>\n"
394       "  ret <4 x float> %A\n"
395       "}\n");
396   // Check that pattern matching works on vectors where each lane has the same
397   // ordered pattern.
398   expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true});
399 }
400 
401 TEST_F(MatchSelectPatternTest, VectorNotFMinimum) {
402   parseAssembly(
403       "define <4 x float> @test(<4 x float> %a) {\n"
404       "  %1 = fcmp ule <4 x float> %a, \n"
405       "    <float 5.0, float 0x7ff8000000000000, float 5.0, float 5.0>\n"
406       "  %A = select <4 x i1> %1, <4 x float> %a,\n"
407       "     <4 x float> <float 5.0, float 0x7ff8000000000000, float 5.0, float "
408       "5.0>\n"
409       "  ret <4 x float> %A\n"
410       "}\n");
411   // The lane that contains a NaN (0x7ff80...) behaves like a
412   // non-NaN-propagating min and the other lines behave like a NaN-propagating
413   // min, so check that neither is returned.
414   expectPattern({SPF_UNKNOWN, SPNB_NA, false});
415 }
416 
417 TEST_F(MatchSelectPatternTest, VectorNotFMinZero) {
418   parseAssembly(
419       "define <4 x float> @test(<4 x float> %a) {\n"
420       "  %1 = fcmp ule <4 x float> %a, \n"
421       "    <float 5.0, float -0.0, float 5.0, float 5.0>\n"
422       "  %A = select <4 x i1> %1, <4 x float> %a,\n"
423       "     <4 x float> <float 5.0, float 0.0, float 5.0, float 5.0>\n"
424       "  ret <4 x float> %A\n"
425       "}\n");
426   // Always selects the second lane of %a if it is positive or negative zero, so
427   // this is stricter than a min.
428   expectPattern({SPF_UNKNOWN, SPNB_NA, false});
429 }
430 
431 TEST_F(MatchSelectPatternTest, DoubleCastU) {
432   parseAssembly(
433       "define i32 @test(i8 %a, i8 %b) {\n"
434       "  %1 = icmp ult i8 %a, %b\n"
435       "  %2 = zext i8 %a to i32\n"
436       "  %3 = zext i8 %b to i32\n"
437       "  %A = select i1 %1, i32 %2, i32 %3\n"
438       "  ret i32 %A\n"
439       "}\n");
440   // We should be able to look through the situation where we cast both operands
441   // to the select.
442   expectPattern({SPF_UMIN, SPNB_NA, false});
443 }
444 
445 TEST_F(MatchSelectPatternTest, DoubleCastS) {
446   parseAssembly(
447       "define i32 @test(i8 %a, i8 %b) {\n"
448       "  %1 = icmp slt i8 %a, %b\n"
449       "  %2 = sext i8 %a to i32\n"
450       "  %3 = sext i8 %b to i32\n"
451       "  %A = select i1 %1, i32 %2, i32 %3\n"
452       "  ret i32 %A\n"
453       "}\n");
454   // We should be able to look through the situation where we cast both operands
455   // to the select.
456   expectPattern({SPF_SMIN, SPNB_NA, false});
457 }
458 
459 TEST_F(MatchSelectPatternTest, DoubleCastBad) {
460   parseAssembly(
461       "define i32 @test(i8 %a, i8 %b) {\n"
462       "  %1 = icmp ult i8 %a, %b\n"
463       "  %2 = zext i8 %a to i32\n"
464       "  %3 = sext i8 %b to i32\n"
465       "  %A = select i1 %1, i32 %2, i32 %3\n"
466       "  ret i32 %A\n"
467       "}\n");
468   // The cast types here aren't the same, so we cannot match an UMIN.
469   expectPattern({SPF_UNKNOWN, SPNB_NA, false});
470 }
471 
472 TEST_F(MatchSelectPatternTest, NotNotSMin) {
473   parseAssembly(
474       "define i8 @test(i8 %a, i8 %b) {\n"
475       "  %cmp = icmp sgt i8 %a, %b\n"
476       "  %an = xor i8 %a, -1\n"
477       "  %bn = xor i8 %b, -1\n"
478       "  %A = select i1 %cmp, i8 %an, i8 %bn\n"
479       "  ret i8 %A\n"
480       "}\n");
481   expectPattern({SPF_SMIN, SPNB_NA, false});
482 }
483 
484 TEST_F(MatchSelectPatternTest, NotNotSMinSwap) {
485   parseAssembly(
486       "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n"
487       "  %cmp = icmp slt <2 x i8> %a, %b\n"
488       "  %an = xor <2 x i8> %a, <i8 -1, i8-1>\n"
489       "  %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n"
490       "  %A = select <2 x i1> %cmp, <2 x i8> %bn, <2 x i8> %an\n"
491       "  ret <2 x i8> %A\n"
492       "}\n");
493   expectPattern({SPF_SMIN, SPNB_NA, false});
494 }
495 
496 TEST_F(MatchSelectPatternTest, NotNotSMax) {
497   parseAssembly(
498       "define i8 @test(i8 %a, i8 %b) {\n"
499       "  %cmp = icmp slt i8 %a, %b\n"
500       "  %an = xor i8 %a, -1\n"
501       "  %bn = xor i8 %b, -1\n"
502       "  %A = select i1 %cmp, i8 %an, i8 %bn\n"
503       "  ret i8 %A\n"
504       "}\n");
505   expectPattern({SPF_SMAX, SPNB_NA, false});
506 }
507 
508 TEST_F(MatchSelectPatternTest, NotNotSMaxSwap) {
509   parseAssembly(
510       "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n"
511       "  %cmp = icmp sgt <2 x i8> %a, %b\n"
512       "  %an = xor <2 x i8> %a, <i8 -1, i8-1>\n"
513       "  %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n"
514       "  %A = select <2 x i1> %cmp, <2 x i8> %bn, <2 x i8> %an\n"
515       "  ret <2 x i8> %A\n"
516       "}\n");
517   expectPattern({SPF_SMAX, SPNB_NA, false});
518 }
519 
520 TEST_F(MatchSelectPatternTest, NotNotUMin) {
521   parseAssembly(
522       "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n"
523       "  %cmp = icmp ugt <2 x i8> %a, %b\n"
524       "  %an = xor <2 x i8> %a, <i8 -1, i8-1>\n"
525       "  %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n"
526       "  %A = select <2 x i1> %cmp, <2 x i8> %an, <2 x i8> %bn\n"
527       "  ret <2 x i8> %A\n"
528       "}\n");
529   expectPattern({SPF_UMIN, SPNB_NA, false});
530 }
531 
532 TEST_F(MatchSelectPatternTest, NotNotUMinSwap) {
533   parseAssembly(
534       "define i8 @test(i8 %a, i8 %b) {\n"
535       "  %cmp = icmp ult i8 %a, %b\n"
536       "  %an = xor i8 %a, -1\n"
537       "  %bn = xor i8 %b, -1\n"
538       "  %A = select i1 %cmp, i8 %bn, i8 %an\n"
539       "  ret i8 %A\n"
540       "}\n");
541   expectPattern({SPF_UMIN, SPNB_NA, false});
542 }
543 
544 TEST_F(MatchSelectPatternTest, NotNotUMax) {
545   parseAssembly(
546       "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n"
547       "  %cmp = icmp ult <2 x i8> %a, %b\n"
548       "  %an = xor <2 x i8> %a, <i8 -1, i8-1>\n"
549       "  %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n"
550       "  %A = select <2 x i1> %cmp, <2 x i8> %an, <2 x i8> %bn\n"
551       "  ret <2 x i8> %A\n"
552       "}\n");
553   expectPattern({SPF_UMAX, SPNB_NA, false});
554 }
555 
556 TEST_F(MatchSelectPatternTest, NotNotUMaxSwap) {
557   parseAssembly(
558       "define i8 @test(i8 %a, i8 %b) {\n"
559       "  %cmp = icmp ugt i8 %a, %b\n"
560       "  %an = xor i8 %a, -1\n"
561       "  %bn = xor i8 %b, -1\n"
562       "  %A = select i1 %cmp, i8 %bn, i8 %an\n"
563       "  ret i8 %A\n"
564       "}\n");
565   expectPattern({SPF_UMAX, SPNB_NA, false});
566 }
567 
568 TEST_F(MatchSelectPatternTest, NotNotEq) {
569   parseAssembly(
570       "define i8 @test(i8 %a, i8 %b) {\n"
571       "  %cmp = icmp eq i8 %a, %b\n"
572       "  %an = xor i8 %a, -1\n"
573       "  %bn = xor i8 %b, -1\n"
574       "  %A = select i1 %cmp, i8 %bn, i8 %an\n"
575       "  ret i8 %A\n"
576       "}\n");
577   expectPattern({SPF_UNKNOWN, SPNB_NA, false});
578 }
579 
580 TEST_F(MatchSelectPatternTest, NotNotNe) {
581   parseAssembly(
582       "define i8 @test(i8 %a, i8 %b) {\n"
583       "  %cmp = icmp ne i8 %a, %b\n"
584       "  %an = xor i8 %a, -1\n"
585       "  %bn = xor i8 %b, -1\n"
586       "  %A = select i1 %cmp, i8 %bn, i8 %an\n"
587       "  ret i8 %A\n"
588       "}\n");
589   expectPattern({SPF_UNKNOWN, SPNB_NA, false});
590 }
591 
592 TEST(ValueTracking, GuaranteedToTransferExecutionToSuccessor) {
593   StringRef Assembly =
594       "declare void @nounwind_readonly(i32*) nounwind readonly "
595       "declare void @nounwind_argmemonly(i32*) nounwind argmemonly "
596       "declare void @throws_but_readonly(i32*) readonly "
597       "declare void @throws_but_argmemonly(i32*) argmemonly "
598       "declare void @nounwind_willreturn(i32*) nounwind willreturn"
599       " "
600       "declare void @unknown(i32*) "
601       " "
602       "define void @f(i32* %p) { "
603       "  call void @nounwind_readonly(i32* %p) "
604       "  call void @nounwind_argmemonly(i32* %p) "
605       "  call void @throws_but_readonly(i32* %p) "
606       "  call void @throws_but_argmemonly(i32* %p) "
607       "  call void @unknown(i32* %p) nounwind readonly "
608       "  call void @unknown(i32* %p) nounwind argmemonly "
609       "  call void @unknown(i32* %p) readonly "
610       "  call void @unknown(i32* %p) argmemonly "
611       "  call void @nounwind_willreturn(i32* %p)"
612       "  ret void "
613       "} ";
614 
615   LLVMContext Context;
616   SMDiagnostic Error;
617   auto M = parseAssemblyString(Assembly, Error, Context);
618   assert(M && "Bad assembly?");
619 
620   auto *F = M->getFunction("f");
621   assert(F && "Bad assembly?");
622 
623   auto &BB = F->getEntryBlock();
624   bool ExpectedAnswers[] = {
625       true,  // call void @nounwind_readonly(i32* %p)
626       true,  // call void @nounwind_argmemonly(i32* %p)
627       false, // call void @throws_but_readonly(i32* %p)
628       false, // call void @throws_but_argmemonly(i32* %p)
629       true,  // call void @unknown(i32* %p) nounwind readonly
630       true,  // call void @unknown(i32* %p) nounwind argmemonly
631       false, // call void @unknown(i32* %p) readonly
632       false, // call void @unknown(i32* %p) argmemonly
633       true,  // call void @nounwind_willreturn(i32* %p)
634       false, // ret void
635   };
636 
637   int Index = 0;
638   for (auto &I : BB) {
639     EXPECT_EQ(isGuaranteedToTransferExecutionToSuccessor(&I),
640               ExpectedAnswers[Index])
641         << "Incorrect answer at instruction " << Index << " = " << I;
642     Index++;
643   }
644 }
645 
646 TEST_F(ValueTrackingTest, ComputeNumSignBits_PR32045) {
647   parseAssembly(
648       "define i32 @test(i32 %a) {\n"
649       "  %A = ashr i32 %a, -1\n"
650       "  ret i32 %A\n"
651       "}\n");
652   EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u);
653 }
654 
655 // No guarantees for canonical IR in this analysis, so this just bails out.
656 TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle) {
657   parseAssembly(
658       "define <2 x i32> @test() {\n"
659       "  %A = shufflevector <2 x i32> undef, <2 x i32> undef, <2 x i32> <i32 0, i32 0>\n"
660       "  ret <2 x i32> %A\n"
661       "}\n");
662   EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u);
663 }
664 
665 // No guarantees for canonical IR in this analysis, so a shuffle element that
666 // references an undef value means this can't return any extra information.
667 TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle2) {
668   parseAssembly(
669       "define <2 x i32> @test(<2 x i1> %x) {\n"
670       "  %sext = sext <2 x i1> %x to <2 x i32>\n"
671       "  %A = shufflevector <2 x i32> %sext, <2 x i32> undef, <2 x i32> <i32 0, i32 2>\n"
672       "  ret <2 x i32> %A\n"
673       "}\n");
674   EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u);
675 }
676 
677 TEST(ValueTracking, propagatesPoison) {
678   std::string AsmHead = "declare i32 @g(i32)\n"
679                         "define void @f(i32 %x, i32 %y, float %fx, float %fy, "
680                         "i1 %cond, i8* %p) {\n";
681   std::string AsmTail = "  ret void\n}";
682   // (propagates poison?, IR instruction)
683   SmallVector<std::pair<bool, std::string>, 32> Data = {
684       {true, "add i32 %x, %y"},
685       {true, "add nsw nuw i32 %x, %y"},
686       {true, "ashr i32 %x, %y"},
687       {true, "lshr exact i32 %x, 31"},
688       {true, "fcmp oeq float %fx, %fy"},
689       {true, "icmp eq i32 %x, %y"},
690       {true, "getelementptr i8, i8* %p, i32 %x"},
691       {true, "getelementptr inbounds i8, i8* %p, i32 %x"},
692       {true, "bitcast float %fx to i32"},
693       {false, "select i1 %cond, i32 %x, i32 %y"},
694       {false, "freeze i32 %x"},
695       {true, "udiv i32 %x, %y"},
696       {true, "urem i32 %x, %y"},
697       {true, "sdiv exact i32 %x, %y"},
698       {true, "srem i32 %x, %y"},
699       {false, "call i32 @g(i32 %x)"}};
700 
701   std::string AssemblyStr = AsmHead;
702   for (auto &Itm : Data)
703     AssemblyStr += Itm.second + "\n";
704   AssemblyStr += AsmTail;
705 
706   LLVMContext Context;
707   SMDiagnostic Error;
708   auto M = parseAssemblyString(AssemblyStr, Error, Context);
709   assert(M && "Bad assembly?");
710 
711   auto *F = M->getFunction("f");
712   assert(F && "Bad assembly?");
713 
714   auto &BB = F->getEntryBlock();
715 
716   int Index = 0;
717   for (auto &I : BB) {
718     if (isa<ReturnInst>(&I))
719       break;
720     EXPECT_EQ(propagatesPoison(cast<Operator>(&I)), Data[Index].first)
721         << "Incorrect answer at instruction " << Index << " = " << I;
722     Index++;
723   }
724 }
725 
726 TEST_F(ValueTrackingTest, programUndefinedIfPoison) {
727   parseAssembly("declare i32 @any_num()"
728                 "define void @test(i32 %mask) {\n"
729                 "  %A = call i32 @any_num()\n"
730                 "  %B = or i32 %A, %mask\n"
731                 "  udiv i32 1, %B"
732                 "  ret void\n"
733                 "}\n");
734   // If %A was poison, udiv raises UB regardless of %mask's value
735   EXPECT_EQ(programUndefinedIfPoison(A), true);
736 }
737 
738 TEST_F(ValueTrackingTest, programUndefinedIfUndefOrPoison) {
739   parseAssembly("declare i32 @any_num()"
740                 "define void @test(i32 %mask) {\n"
741                 "  %A = call i32 @any_num()\n"
742                 "  %B = or i32 %A, %mask\n"
743                 "  udiv i32 1, %B"
744                 "  ret void\n"
745                 "}\n");
746   // If %A was undef and %mask was 1, udiv does not raise UB
747   EXPECT_EQ(programUndefinedIfUndefOrPoison(A), false);
748 }
749 
750 TEST_F(ValueTrackingTest, isGuaranteedNotToBePoison_exploitBranchCond) {
751   parseAssembly("declare i1 @any_bool()"
752                 "define void @test(i1 %y) {\n"
753                 "  %A = call i1 @any_bool()\n"
754                 "  %cond = and i1 %A, %y\n"
755                 "  br i1 %cond, label %BB1, label %BB2\n"
756                 "BB1:\n"
757                 "  ret void\n"
758                 "BB2:\n"
759                 "  ret void\n"
760                 "}\n");
761   DominatorTree DT(*F);
762   for (auto &BB : *F) {
763     if (&BB == &F->getEntryBlock())
764       continue;
765 
766     EXPECT_EQ(isGuaranteedNotToBePoison(A, BB.getTerminator(), &DT), true)
767         << "isGuaranteedNotToBePoison does not hold at " << *BB.getTerminator();
768   }
769 }
770 
771 TEST_F(ValueTrackingTest, isGuaranteedNotToBePoison_phi) {
772   parseAssembly("declare i32 @any_i32(i32)"
773                 "define void @test() {\n"
774                 "ENTRY:\n"
775                 "  br label %LOOP\n"
776                 "LOOP:\n"
777                 "  %A = phi i32 [0, %ENTRY], [%A.next, %NEXT]\n"
778                 "  %A.next = call i32 @any_i32(i32 %A)\n"
779                 "  %cond = icmp eq i32 %A.next, 0\n"
780                 "  br i1 %cond, label %NEXT, label %EXIT\n"
781                 "NEXT:\n"
782                 "  br label %LOOP\n"
783                 "EXIT:\n"
784                 "  ret void\n"
785                 "}\n");
786   DominatorTree DT(*F);
787   for (auto &BB : *F) {
788     if (BB.getName() == "LOOP") {
789       EXPECT_EQ(isGuaranteedNotToBePoison(A, A, &DT), true)
790           << "isGuaranteedNotToBePoison does not hold";
791     }
792   }
793 }
794 
795 TEST_F(ValueTrackingTest, isGuaranteedNotToBeUndefOrPoison) {
796   parseAssembly("declare void @f(i32 noundef)"
797                 "define void @test(i32 %x) {\n"
798                 "  %A = bitcast i32 %x to i32\n"
799                 "  call void @f(i32 noundef %x)\n"
800                 "  ret void\n"
801                 "}\n");
802   EXPECT_EQ(isGuaranteedNotToBeUndefOrPoison(A), true);
803 }
804 
805 TEST(ValueTracking, canCreatePoisonOrUndef) {
806   std::string AsmHead =
807       "declare i32 @g(i32)\n"
808       "define void @f(i32 %x, i32 %y, float %fx, float %fy, i1 %cond, "
809       "<4 x i32> %vx, <4 x i32> %vx2, <vscale x 4 x i32> %svx, i8* %p) {\n";
810   std::string AsmTail = "  ret void\n}";
811   // (can create poison?, can create undef?, IR instruction)
812   SmallVector<std::pair<std::pair<bool, bool>, std::string>, 32> Data = {
813       {{false, false}, "add i32 %x, %y"},
814       {{true, false}, "add nsw nuw i32 %x, %y"},
815       {{true, false}, "shl i32 %x, %y"},
816       {{true, false}, "shl <4 x i32> %vx, %vx2"},
817       {{true, false}, "shl nsw i32 %x, %y"},
818       {{true, false}, "shl nsw <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"},
819       {{false, false}, "shl i32 %x, 31"},
820       {{true, false}, "shl i32 %x, 32"},
821       {{false, false}, "shl <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"},
822       {{true, false}, "shl <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 32>"},
823       {{true, false}, "ashr i32 %x, %y"},
824       {{true, false}, "ashr exact i32 %x, %y"},
825       {{false, false}, "ashr i32 %x, 31"},
826       {{true, false}, "ashr exact i32 %x, 31"},
827       {{false, false}, "ashr <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"},
828       {{true, false}, "ashr <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 32>"},
829       {{true, false}, "ashr exact <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"},
830       {{true, false}, "lshr i32 %x, %y"},
831       {{true, false}, "lshr exact i32 %x, 31"},
832       {{false, false}, "udiv i32 %x, %y"},
833       {{true, false}, "udiv exact i32 %x, %y"},
834       {{false, false}, "getelementptr i8, i8* %p, i32 %x"},
835       {{true, false}, "getelementptr inbounds i8, i8* %p, i32 %x"},
836       {{true, false}, "fneg nnan float %fx"},
837       {{false, false}, "fneg float %fx"},
838       {{false, false}, "fadd float %fx, %fy"},
839       {{true, false}, "fadd nnan float %fx, %fy"},
840       {{false, false}, "urem i32 %x, %y"},
841       {{true, false}, "fptoui float %fx to i32"},
842       {{true, false}, "fptosi float %fx to i32"},
843       {{false, false}, "bitcast float %fx to i32"},
844       {{false, false}, "select i1 %cond, i32 %x, i32 %y"},
845       {{true, false}, "select nnan i1 %cond, float %fx, float %fy"},
846       {{true, false}, "extractelement <4 x i32> %vx, i32 %x"},
847       {{false, false}, "extractelement <4 x i32> %vx, i32 3"},
848       {{true, false}, "extractelement <vscale x 4 x i32> %svx, i32 4"},
849       {{true, false}, "insertelement <4 x i32> %vx, i32 %x, i32 %y"},
850       {{false, false}, "insertelement <4 x i32> %vx, i32 %x, i32 3"},
851       {{true, false}, "insertelement <vscale x 4 x i32> %svx, i32 %x, i32 4"},
852       {{false, false}, "freeze i32 %x"},
853       {{false, false},
854        "shufflevector <4 x i32> %vx, <4 x i32> %vx2, "
855        "<4 x i32> <i32 0, i32 1, i32 2, i32 3>"},
856       {{false, true},
857        "shufflevector <4 x i32> %vx, <4 x i32> %vx2, "
858        "<4 x i32> <i32 0, i32 1, i32 2, i32 undef>"},
859       {{false, true},
860        "shufflevector <vscale x 4 x i32> %svx, "
861        "<vscale x 4 x i32> %svx, <vscale x 4 x i32> undef"},
862       {{true, false}, "call i32 @g(i32 %x)"},
863       {{false, false}, "call noundef i32 @g(i32 %x)"},
864       {{true, false}, "fcmp nnan oeq float %fx, %fy"},
865       {{false, false}, "fcmp oeq float %fx, %fy"}};
866 
867   std::string AssemblyStr = AsmHead;
868   for (auto &Itm : Data)
869     AssemblyStr += Itm.second + "\n";
870   AssemblyStr += AsmTail;
871 
872   LLVMContext Context;
873   SMDiagnostic Error;
874   auto M = parseAssemblyString(AssemblyStr, Error, Context);
875   assert(M && "Bad assembly?");
876 
877   auto *F = M->getFunction("f");
878   assert(F && "Bad assembly?");
879 
880   auto &BB = F->getEntryBlock();
881 
882   int Index = 0;
883   for (auto &I : BB) {
884     if (isa<ReturnInst>(&I))
885       break;
886     bool Poison = Data[Index].first.first;
887     bool Undef = Data[Index].first.second;
888     EXPECT_EQ(canCreatePoison(cast<Operator>(&I)), Poison)
889         << "Incorrect answer of canCreatePoison at instruction " << Index
890         << " = " << I;
891     EXPECT_EQ(canCreateUndefOrPoison(cast<Operator>(&I)), Undef || Poison)
892         << "Incorrect answer of canCreateUndef at instruction " << Index
893         << " = " << I;
894     Index++;
895   }
896 }
897 
898 TEST_F(ComputeKnownBitsTest, ComputeKnownBits) {
899   parseAssembly(
900       "define i32 @test(i32 %a, i32 %b) {\n"
901       "  %ash = mul i32 %a, 8\n"
902       "  %aad = add i32 %ash, 7\n"
903       "  %aan = and i32 %aad, 4095\n"
904       "  %bsh = shl i32 %b, 4\n"
905       "  %bad = or i32 %bsh, 6\n"
906       "  %ban = and i32 %bad, 4095\n"
907       "  %A = mul i32 %aan, %ban\n"
908       "  ret i32 %A\n"
909       "}\n");
910   expectKnownBits(/*zero*/ 4278190085u, /*one*/ 10u);
911 }
912 
913 TEST_F(ComputeKnownBitsTest, ComputeKnownMulBits) {
914   parseAssembly(
915       "define i32 @test(i32 %a, i32 %b) {\n"
916       "  %aa = shl i32 %a, 5\n"
917       "  %bb = shl i32 %b, 5\n"
918       "  %aaa = or i32 %aa, 24\n"
919       "  %bbb = or i32 %bb, 28\n"
920       "  %A = mul i32 %aaa, %bbb\n"
921       "  ret i32 %A\n"
922       "}\n");
923   expectKnownBits(/*zero*/ 95u, /*one*/ 32u);
924 }
925 
926 TEST_F(ComputeKnownBitsTest, KnownNonZeroShift) {
927   // %q is known nonzero without known bits.
928   // Because %q is nonzero, %A[0] is known to be zero.
929   parseAssembly(
930       "define i8 @test(i8 %p, i8* %pq) {\n"
931       "  %q = load i8, i8* %pq, !range !0\n"
932       "  %A = shl i8 %p, %q\n"
933       "  ret i8 %A\n"
934       "}\n"
935       "!0 = !{ i8 1, i8 5 }\n");
936   expectKnownBits(/*zero*/ 1u, /*one*/ 0u);
937 }
938 
939 TEST_F(ComputeKnownBitsTest, ComputeKnownFshl) {
940   // fshl(....1111....0000, 00..1111........, 6)
941   // = 11....000000..11
942   parseAssembly(
943       "define i16 @test(i16 %a, i16 %b) {\n"
944       "  %aa = shl i16 %a, 4\n"
945       "  %bb = lshr i16 %b, 2\n"
946       "  %aaa = or i16 %aa, 3840\n"
947       "  %bbb = or i16 %bb, 3840\n"
948       "  %A = call i16 @llvm.fshl.i16(i16 %aaa, i16 %bbb, i16 6)\n"
949       "  ret i16 %A\n"
950       "}\n"
951       "declare i16 @llvm.fshl.i16(i16, i16, i16)\n");
952   expectKnownBits(/*zero*/ 1008u, /*one*/ 49155u);
953 }
954 
955 TEST_F(ComputeKnownBitsTest, ComputeKnownFshr) {
956   // fshr(....1111....0000, 00..1111........, 26)
957   // = 11....000000..11
958   parseAssembly(
959       "define i16 @test(i16 %a, i16 %b) {\n"
960       "  %aa = shl i16 %a, 4\n"
961       "  %bb = lshr i16 %b, 2\n"
962       "  %aaa = or i16 %aa, 3840\n"
963       "  %bbb = or i16 %bb, 3840\n"
964       "  %A = call i16 @llvm.fshr.i16(i16 %aaa, i16 %bbb, i16 26)\n"
965       "  ret i16 %A\n"
966       "}\n"
967       "declare i16 @llvm.fshr.i16(i16, i16, i16)\n");
968   expectKnownBits(/*zero*/ 1008u, /*one*/ 49155u);
969 }
970 
971 TEST_F(ComputeKnownBitsTest, ComputeKnownFshlZero) {
972   // fshl(....1111....0000, 00..1111........, 0)
973   // = ....1111....0000
974   parseAssembly(
975       "define i16 @test(i16 %a, i16 %b) {\n"
976       "  %aa = shl i16 %a, 4\n"
977       "  %bb = lshr i16 %b, 2\n"
978       "  %aaa = or i16 %aa, 3840\n"
979       "  %bbb = or i16 %bb, 3840\n"
980       "  %A = call i16 @llvm.fshl.i16(i16 %aaa, i16 %bbb, i16 0)\n"
981       "  ret i16 %A\n"
982       "}\n"
983       "declare i16 @llvm.fshl.i16(i16, i16, i16)\n");
984   expectKnownBits(/*zero*/ 15u, /*one*/ 3840u);
985 }
986 
987 TEST_F(ComputeKnownBitsTest, ComputeKnownUAddSatLeadingOnes) {
988   // uadd.sat(1111...1, ........)
989   // = 1111....
990   parseAssembly(
991       "define i8 @test(i8 %a, i8 %b) {\n"
992       "  %aa = or i8 %a, 241\n"
993       "  %A = call i8 @llvm.uadd.sat.i8(i8 %aa, i8 %b)\n"
994       "  ret i8 %A\n"
995       "}\n"
996       "declare i8 @llvm.uadd.sat.i8(i8, i8)\n");
997   expectKnownBits(/*zero*/ 0u, /*one*/ 240u);
998 }
999 
1000 TEST_F(ComputeKnownBitsTest, ComputeKnownUAddSatOnesPreserved) {
1001   // uadd.sat(00...011, .1...110)
1002   // = .......1
1003   parseAssembly(
1004       "define i8 @test(i8 %a, i8 %b) {\n"
1005       "  %aa = or i8 %a, 3\n"
1006       "  %aaa = and i8 %aa, 59\n"
1007       "  %bb = or i8 %b, 70\n"
1008       "  %bbb = and i8 %bb, 254\n"
1009       "  %A = call i8 @llvm.uadd.sat.i8(i8 %aaa, i8 %bbb)\n"
1010       "  ret i8 %A\n"
1011       "}\n"
1012       "declare i8 @llvm.uadd.sat.i8(i8, i8)\n");
1013   expectKnownBits(/*zero*/ 0u, /*one*/ 1u);
1014 }
1015 
1016 TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatLHSLeadingZeros) {
1017   // usub.sat(0000...0, ........)
1018   // = 0000....
1019   parseAssembly(
1020       "define i8 @test(i8 %a, i8 %b) {\n"
1021       "  %aa = and i8 %a, 14\n"
1022       "  %A = call i8 @llvm.usub.sat.i8(i8 %aa, i8 %b)\n"
1023       "  ret i8 %A\n"
1024       "}\n"
1025       "declare i8 @llvm.usub.sat.i8(i8, i8)\n");
1026   expectKnownBits(/*zero*/ 240u, /*one*/ 0u);
1027 }
1028 
1029 TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatRHSLeadingOnes) {
1030   // usub.sat(........, 1111...1)
1031   // = 0000....
1032   parseAssembly(
1033       "define i8 @test(i8 %a, i8 %b) {\n"
1034       "  %bb = or i8 %a, 241\n"
1035       "  %A = call i8 @llvm.usub.sat.i8(i8 %a, i8 %bb)\n"
1036       "  ret i8 %A\n"
1037       "}\n"
1038       "declare i8 @llvm.usub.sat.i8(i8, i8)\n");
1039   expectKnownBits(/*zero*/ 240u, /*one*/ 0u);
1040 }
1041 
1042 TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatZerosPreserved) {
1043   // usub.sat(11...011, .1...110)
1044   // = ......0.
1045   parseAssembly(
1046       "define i8 @test(i8 %a, i8 %b) {\n"
1047       "  %aa = or i8 %a, 195\n"
1048       "  %aaa = and i8 %aa, 251\n"
1049       "  %bb = or i8 %b, 70\n"
1050       "  %bbb = and i8 %bb, 254\n"
1051       "  %A = call i8 @llvm.usub.sat.i8(i8 %aaa, i8 %bbb)\n"
1052       "  ret i8 %A\n"
1053       "}\n"
1054       "declare i8 @llvm.usub.sat.i8(i8, i8)\n");
1055   expectKnownBits(/*zero*/ 2u, /*one*/ 0u);
1056 }
1057 
1058 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsPtrToIntTrunc) {
1059   // ptrtoint truncates the pointer type.
1060   parseAssembly(
1061       "define void @test(i8** %p) {\n"
1062       "  %A = load i8*, i8** %p\n"
1063       "  %i = ptrtoint i8* %A to i32\n"
1064       "  %m = and i32 %i, 31\n"
1065       "  %c = icmp eq i32 %m, 0\n"
1066       "  call void @llvm.assume(i1 %c)\n"
1067       "  ret void\n"
1068       "}\n"
1069       "declare void @llvm.assume(i1)\n");
1070   AssumptionCache AC(*F);
1071   KnownBits Known = computeKnownBits(
1072       A, M->getDataLayout(), /* Depth */ 0, &AC, F->front().getTerminator());
1073   EXPECT_EQ(Known.Zero.getZExtValue(), 31u);
1074   EXPECT_EQ(Known.One.getZExtValue(), 0u);
1075 }
1076 
1077 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsPtrToIntZext) {
1078   // ptrtoint zero extends the pointer type.
1079   parseAssembly(
1080       "define void @test(i8** %p) {\n"
1081       "  %A = load i8*, i8** %p\n"
1082       "  %i = ptrtoint i8* %A to i128\n"
1083       "  %m = and i128 %i, 31\n"
1084       "  %c = icmp eq i128 %m, 0\n"
1085       "  call void @llvm.assume(i1 %c)\n"
1086       "  ret void\n"
1087       "}\n"
1088       "declare void @llvm.assume(i1)\n");
1089   AssumptionCache AC(*F);
1090   KnownBits Known = computeKnownBits(
1091       A, M->getDataLayout(), /* Depth */ 0, &AC, F->front().getTerminator());
1092   EXPECT_EQ(Known.Zero.getZExtValue(), 31u);
1093   EXPECT_EQ(Known.One.getZExtValue(), 0u);
1094 }
1095 
1096 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsFreeze) {
1097   parseAssembly("define void @test() {\n"
1098                 "  %m = call i32 @any_num()\n"
1099                 "  %A = freeze i32 %m\n"
1100                 "  %n = and i32 %m, 31\n"
1101                 "  %c = icmp eq i32 %n, 0\n"
1102                 "  call void @llvm.assume(i1 %c)\n"
1103                 "  ret void\n"
1104                 "}\n"
1105                 "declare void @llvm.assume(i1)\n"
1106                 "declare i32 @any_num()\n");
1107   AssumptionCache AC(*F);
1108   KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC,
1109                                      F->front().getTerminator());
1110   EXPECT_EQ(Known.Zero.getZExtValue(), 31u);
1111   EXPECT_EQ(Known.One.getZExtValue(), 0u);
1112 }
1113 
1114 class IsBytewiseValueTest : public ValueTrackingTest,
1115                             public ::testing::WithParamInterface<
1116                                 std::pair<const char *, const char *>> {
1117 protected:
1118 };
1119 
1120 const std::pair<const char *, const char *> IsBytewiseValueTests[] = {
1121     {
1122         "i8 0",
1123         "i48* null",
1124     },
1125     {
1126         "i8 undef",
1127         "i48* undef",
1128     },
1129     {
1130         "i8 0",
1131         "i8 zeroinitializer",
1132     },
1133     {
1134         "i8 0",
1135         "i8 0",
1136     },
1137     {
1138         "i8 -86",
1139         "i8 -86",
1140     },
1141     {
1142         "i8 -1",
1143         "i8 -1",
1144     },
1145     {
1146         "i8 undef",
1147         "i16 undef",
1148     },
1149     {
1150         "i8 0",
1151         "i16 0",
1152     },
1153     {
1154         "",
1155         "i16 7",
1156     },
1157     {
1158         "i8 -86",
1159         "i16 -21846",
1160     },
1161     {
1162         "i8 -1",
1163         "i16 -1",
1164     },
1165     {
1166         "i8 0",
1167         "i48 0",
1168     },
1169     {
1170         "i8 -1",
1171         "i48 -1",
1172     },
1173     {
1174         "i8 0",
1175         "i49 0",
1176     },
1177     {
1178         "",
1179         "i49 -1",
1180     },
1181     {
1182         "i8 0",
1183         "half 0xH0000",
1184     },
1185     {
1186         "i8 -85",
1187         "half 0xHABAB",
1188     },
1189     {
1190         "i8 0",
1191         "float 0.0",
1192     },
1193     {
1194         "i8 -1",
1195         "float 0xFFFFFFFFE0000000",
1196     },
1197     {
1198         "i8 0",
1199         "double 0.0",
1200     },
1201     {
1202         "i8 -15",
1203         "double 0xF1F1F1F1F1F1F1F1",
1204     },
1205     {
1206         "i8 undef",
1207         "i16* undef",
1208     },
1209     {
1210         "i8 0",
1211         "i16* inttoptr (i64 0 to i16*)",
1212     },
1213     {
1214         "i8 -1",
1215         "i16* inttoptr (i64 -1 to i16*)",
1216     },
1217     {
1218         "i8 -86",
1219         "i16* inttoptr (i64 -6148914691236517206 to i16*)",
1220     },
1221     {
1222         "",
1223         "i16* inttoptr (i48 -1 to i16*)",
1224     },
1225     {
1226         "i8 -1",
1227         "i16* inttoptr (i96 -1 to i16*)",
1228     },
1229     {
1230         "i8 undef",
1231         "[0 x i8] zeroinitializer",
1232     },
1233     {
1234         "i8 undef",
1235         "[0 x i8] undef",
1236     },
1237     {
1238         "i8 undef",
1239         "[5 x [0 x i8]] zeroinitializer",
1240     },
1241     {
1242         "i8 undef",
1243         "[5 x [0 x i8]] undef",
1244     },
1245     {
1246         "i8 0",
1247         "[6 x i8] zeroinitializer",
1248     },
1249     {
1250         "i8 undef",
1251         "[6 x i8] undef",
1252     },
1253     {
1254         "i8 1",
1255         "[5 x i8] [i8 1, i8 1, i8 1, i8 1, i8 1]",
1256     },
1257     {
1258         "",
1259         "[5 x i64] [i64 1, i64 1, i64 1, i64 1, i64 1]",
1260     },
1261     {
1262         "i8 -1",
1263         "[5 x i64] [i64 -1, i64 -1, i64 -1, i64 -1, i64 -1]",
1264     },
1265     {
1266         "",
1267         "[4 x i8] [i8 1, i8 2, i8 1, i8 1]",
1268     },
1269     {
1270         "i8 1",
1271         "[4 x i8] [i8 1, i8 undef, i8 1, i8 1]",
1272     },
1273     {
1274         "i8 0",
1275         "<6 x i8> zeroinitializer",
1276     },
1277     {
1278         "i8 undef",
1279         "<6 x i8> undef",
1280     },
1281     {
1282         "i8 1",
1283         "<5 x i8> <i8 1, i8 1, i8 1, i8 1, i8 1>",
1284     },
1285     {
1286         "",
1287         "<5 x i64> <i64 1, i64 1, i64 1, i64 1, i64 1>",
1288     },
1289     {
1290         "i8 -1",
1291         "<5 x i64> <i64 -1, i64 -1, i64 -1, i64 -1, i64 -1>",
1292     },
1293     {
1294         "",
1295         "<4 x i8> <i8 1, i8 1, i8 2, i8 1>",
1296     },
1297     {
1298         "i8 5",
1299         "<2 x i8> < i8 5, i8 undef >",
1300     },
1301     {
1302         "i8 0",
1303         "[2 x [2 x i16]] zeroinitializer",
1304     },
1305     {
1306         "i8 undef",
1307         "[2 x [2 x i16]] undef",
1308     },
1309     {
1310         "i8 -86",
1311         "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], "
1312         "[2 x i16] [i16 -21846, i16 -21846]]",
1313     },
1314     {
1315         "",
1316         "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], "
1317         "[2 x i16] [i16 -21836, i16 -21846]]",
1318     },
1319     {
1320         "i8 undef",
1321         "{ } zeroinitializer",
1322     },
1323     {
1324         "i8 undef",
1325         "{ } undef",
1326     },
1327     {
1328         "i8 undef",
1329         "{ {}, {} } zeroinitializer",
1330     },
1331     {
1332         "i8 undef",
1333         "{ {}, {} } undef",
1334     },
1335     {
1336         "i8 0",
1337         "{i8, i64, i16*} zeroinitializer",
1338     },
1339     {
1340         "i8 undef",
1341         "{i8, i64, i16*} undef",
1342     },
1343     {
1344         "i8 -86",
1345         "{i8, i64, i16*} {i8 -86, i64 -6148914691236517206, i16* undef}",
1346     },
1347     {
1348         "",
1349         "{i8, i64, i16*} {i8 86, i64 -6148914691236517206, i16* undef}",
1350     },
1351 };
1352 
1353 INSTANTIATE_TEST_CASE_P(IsBytewiseValueParamTests, IsBytewiseValueTest,
1354                         ::testing::ValuesIn(IsBytewiseValueTests),);
1355 
1356 TEST_P(IsBytewiseValueTest, IsBytewiseValue) {
1357   auto M = parseModule(std::string("@test = global ") + GetParam().second);
1358   GlobalVariable *GV = dyn_cast<GlobalVariable>(M->getNamedValue("test"));
1359   Value *Actual = isBytewiseValue(GV->getInitializer(), M->getDataLayout());
1360   std::string Buff;
1361   raw_string_ostream S(Buff);
1362   if (Actual)
1363     S << *Actual;
1364   EXPECT_EQ(GetParam().first, S.str());
1365 }
1366 
1367 TEST_F(ValueTrackingTest, ComputeConstantRange) {
1368   {
1369     // Assumptions:
1370     //  * stride >= 5
1371     //  * stride < 10
1372     //
1373     // stride = [5, 10)
1374     auto M = parseModule(R"(
1375   declare void @llvm.assume(i1)
1376 
1377   define i32 @test(i32 %stride) {
1378     %gt = icmp uge i32 %stride, 5
1379     call void @llvm.assume(i1 %gt)
1380     %lt = icmp ult i32 %stride, 10
1381     call void @llvm.assume(i1 %lt)
1382     %stride.plus.one = add nsw nuw i32 %stride, 1
1383     ret i32 %stride.plus.one
1384   })");
1385     Function *F = M->getFunction("test");
1386 
1387     AssumptionCache AC(*F);
1388     Value *Stride = &*F->arg_begin();
1389     ConstantRange CR1 = computeConstantRange(Stride, true, &AC, nullptr);
1390     EXPECT_TRUE(CR1.isFullSet());
1391 
1392     Instruction *I = &findInstructionByName(F, "stride.plus.one");
1393     ConstantRange CR2 = computeConstantRange(Stride, true, &AC, I);
1394     EXPECT_EQ(5, CR2.getLower());
1395     EXPECT_EQ(10, CR2.getUpper());
1396   }
1397 
1398   {
1399     // Assumptions:
1400     //  * stride >= 5
1401     //  * stride < 200
1402     //  * stride == 99
1403     //
1404     // stride = [99, 100)
1405     auto M = parseModule(R"(
1406   declare void @llvm.assume(i1)
1407 
1408   define i32 @test(i32 %stride) {
1409     %gt = icmp uge i32 %stride, 5
1410     call void @llvm.assume(i1 %gt)
1411     %lt = icmp ult i32 %stride, 200
1412     call void @llvm.assume(i1 %lt)
1413     %eq = icmp eq i32 %stride, 99
1414     call void @llvm.assume(i1 %eq)
1415     %stride.plus.one = add nsw nuw i32 %stride, 1
1416     ret i32 %stride.plus.one
1417   })");
1418     Function *F = M->getFunction("test");
1419 
1420     AssumptionCache AC(*F);
1421     Value *Stride = &*F->arg_begin();
1422     Instruction *I = &findInstructionByName(F, "stride.plus.one");
1423     ConstantRange CR = computeConstantRange(Stride, true, &AC, I);
1424     EXPECT_EQ(99, *CR.getSingleElement());
1425   }
1426 
1427   {
1428     // Assumptions:
1429     //  * stride >= 5
1430     //  * stride >= 50
1431     //  * stride < 100
1432     //  * stride < 200
1433     //
1434     // stride = [50, 100)
1435     auto M = parseModule(R"(
1436   declare void @llvm.assume(i1)
1437 
1438   define i32 @test(i32 %stride, i1 %cond) {
1439     %gt = icmp uge i32 %stride, 5
1440     call void @llvm.assume(i1 %gt)
1441     %gt.2 = icmp uge i32 %stride, 50
1442     call void @llvm.assume(i1 %gt.2)
1443     br i1 %cond, label %bb1, label %bb2
1444 
1445   bb1:
1446     %lt = icmp ult i32 %stride, 200
1447     call void @llvm.assume(i1 %lt)
1448     %lt.2 = icmp ult i32 %stride, 100
1449     call void @llvm.assume(i1 %lt.2)
1450     %stride.plus.one = add nsw nuw i32 %stride, 1
1451     ret i32 %stride.plus.one
1452 
1453   bb2:
1454     ret i32 0
1455   })");
1456     Function *F = M->getFunction("test");
1457 
1458     AssumptionCache AC(*F);
1459     Value *Stride = &*F->arg_begin();
1460     Instruction *GT2 = &findInstructionByName(F, "gt.2");
1461     ConstantRange CR = computeConstantRange(Stride, true, &AC, GT2);
1462     EXPECT_EQ(5, CR.getLower());
1463     EXPECT_EQ(0, CR.getUpper());
1464 
1465     Instruction *I = &findInstructionByName(F, "stride.plus.one");
1466     ConstantRange CR2 = computeConstantRange(Stride, true, &AC, I);
1467     EXPECT_EQ(50, CR2.getLower());
1468     EXPECT_EQ(100, CR2.getUpper());
1469   }
1470 
1471   {
1472     // Assumptions:
1473     //  * stride > 5
1474     //  * stride < 5
1475     //
1476     // stride = empty range, as the assumptions contradict each other.
1477     auto M = parseModule(R"(
1478   declare void @llvm.assume(i1)
1479 
1480   define i32 @test(i32 %stride, i1 %cond) {
1481     %gt = icmp ugt i32 %stride, 5
1482     call void @llvm.assume(i1 %gt)
1483     %lt = icmp ult i32 %stride, 5
1484     call void @llvm.assume(i1 %lt)
1485     %stride.plus.one = add nsw nuw i32 %stride, 1
1486     ret i32 %stride.plus.one
1487   })");
1488     Function *F = M->getFunction("test");
1489 
1490     AssumptionCache AC(*F);
1491     Value *Stride = &*F->arg_begin();
1492 
1493     Instruction *I = &findInstructionByName(F, "stride.plus.one");
1494     ConstantRange CR = computeConstantRange(Stride, true, &AC, I);
1495     EXPECT_TRUE(CR.isEmptySet());
1496   }
1497 
1498   {
1499     // Assumptions:
1500     //  * x.1 >= 5
1501     //  * x.2 < x.1
1502     //
1503     // stride = [0, 5)
1504     auto M = parseModule(R"(
1505   declare void @llvm.assume(i1)
1506 
1507   define i32 @test(i32 %x.1, i32 %x.2) {
1508     %gt = icmp uge i32 %x.1, 5
1509     call void @llvm.assume(i1 %gt)
1510     %lt = icmp ult i32 %x.2, %x.1
1511     call void @llvm.assume(i1 %lt)
1512     %stride.plus.one = add nsw nuw i32 %x.1, 1
1513     ret i32 %stride.plus.one
1514   })");
1515     Function *F = M->getFunction("test");
1516 
1517     AssumptionCache AC(*F);
1518     Value *X2 = &*std::next(F->arg_begin());
1519 
1520     Instruction *I = &findInstructionByName(F, "stride.plus.one");
1521     ConstantRange CR1 = computeConstantRange(X2, true, &AC, I);
1522     EXPECT_EQ(0, CR1.getLower());
1523     EXPECT_EQ(5, CR1.getUpper());
1524 
1525     // Check the depth cutoff results in a conservative result (full set) by
1526     // passing Depth == MaxDepth == 6.
1527     ConstantRange CR2 = computeConstantRange(X2, true, &AC, I, 6);
1528     EXPECT_TRUE(CR2.isFullSet());
1529   }
1530 }
1531 
1532 struct FindAllocaForValueTestParams {
1533   const char *IR;
1534   bool AnyOffsetResult;
1535   bool ZeroOffsetResult;
1536 };
1537 
1538 class FindAllocaForValueTest
1539     : public ValueTrackingTest,
1540       public ::testing::WithParamInterface<FindAllocaForValueTestParams> {
1541 protected:
1542 };
1543 
1544 const FindAllocaForValueTestParams FindAllocaForValueTests[] = {
1545     {R"(
1546       define void @test() {
1547         %a = alloca i64
1548         %r = bitcast i64* %a to i32*
1549         ret void
1550       })",
1551      true, true},
1552 
1553     {R"(
1554       define void @test() {
1555         %a = alloca i32
1556         %r = getelementptr i32, i32* %a, i32 1
1557         ret void
1558       })",
1559      true, false},
1560 
1561     {R"(
1562       define void @test() {
1563         %a = alloca i32
1564         %r = getelementptr i32, i32* %a, i32 0
1565         ret void
1566       })",
1567      true, true},
1568 
1569     {R"(
1570       define void @test(i1 %cond) {
1571       entry:
1572         %a = alloca i32
1573         br label %bb1
1574 
1575       bb1:
1576         %r = phi i32* [ %a, %entry ], [ %r, %bb1 ]
1577         br i1 %cond, label %bb1, label %exit
1578 
1579       exit:
1580         ret void
1581       })",
1582      true, true},
1583 
1584     {R"(
1585       define void @test(i1 %cond) {
1586         %a = alloca i32
1587         %r = select i1 %cond, i32* %a, i32* %a
1588         ret void
1589       })",
1590      true, true},
1591 
1592     {R"(
1593       define void @test(i1 %cond) {
1594         %a = alloca i32
1595         %b = alloca i32
1596         %r = select i1 %cond, i32* %a, i32* %b
1597         ret void
1598       })",
1599      false, false},
1600 
1601     {R"(
1602       define void @test(i1 %cond) {
1603       entry:
1604         %a = alloca i64
1605         %a32 = bitcast i64* %a to i32*
1606         br label %bb1
1607 
1608       bb1:
1609         %x = phi i32* [ %a32, %entry ], [ %x, %bb1 ]
1610         %r = getelementptr i32, i32* %x, i32 1
1611         br i1 %cond, label %bb1, label %exit
1612 
1613       exit:
1614         ret void
1615       })",
1616      true, false},
1617 
1618     {R"(
1619       define void @test(i1 %cond) {
1620       entry:
1621         %a = alloca i64
1622         %a32 = bitcast i64* %a to i32*
1623         br label %bb1
1624 
1625       bb1:
1626         %x = phi i32* [ %a32, %entry ], [ %r, %bb1 ]
1627         %r = getelementptr i32, i32* %x, i32 1
1628         br i1 %cond, label %bb1, label %exit
1629 
1630       exit:
1631         ret void
1632       })",
1633      true, false},
1634 
1635     {R"(
1636       define void @test(i1 %cond, i64* %a) {
1637       entry:
1638         %r = bitcast i64* %a to i32*
1639         ret void
1640       })",
1641      false, false},
1642 
1643     {R"(
1644       define void @test(i1 %cond) {
1645       entry:
1646         %a = alloca i32
1647         %b = alloca i32
1648         br label %bb1
1649 
1650       bb1:
1651         %r = phi i32* [ %a, %entry ], [ %b, %bb1 ]
1652         br i1 %cond, label %bb1, label %exit
1653 
1654       exit:
1655         ret void
1656       })",
1657      false, false},
1658 };
1659 
1660 TEST_P(FindAllocaForValueTest, findAllocaForValue) {
1661   auto M = parseModule(GetParam().IR);
1662   Function *F = M->getFunction("test");
1663   Instruction *I = &findInstructionByName(F, "r");
1664   const AllocaInst *AI = findAllocaForValue(I);
1665   EXPECT_EQ(!!AI, GetParam().AnyOffsetResult);
1666 }
1667 
1668 TEST_P(FindAllocaForValueTest, findAllocaForValueZeroOffset) {
1669   auto M = parseModule(GetParam().IR);
1670   Function *F = M->getFunction("test");
1671   Instruction *I = &findInstructionByName(F, "r");
1672   const AllocaInst *AI = findAllocaForValue(I, true);
1673   EXPECT_EQ(!!AI, GetParam().ZeroOffsetResult);
1674 }
1675 
1676 INSTANTIATE_TEST_CASE_P(FindAllocaForValueTest, FindAllocaForValueTest,
1677                         ::testing::ValuesIn(FindAllocaForValueTests), );
1678