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