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, isGuaranteedNotToBeUndefOrPoison) {
772   parseAssembly("declare void @f(i32 noundef)"
773                 "define void @test(i32 %x) {\n"
774                 "  %A = bitcast i32 %x to i32\n"
775                 "  call void @f(i32 noundef %x)\n"
776                 "  ret void\n"
777                 "}\n");
778   EXPECT_EQ(isGuaranteedNotToBeUndefOrPoison(A), true);
779 }
780 
781 TEST(ValueTracking, canCreatePoisonOrUndef) {
782   std::string AsmHead =
783       "declare i32 @g(i32)\n"
784       "define void @f(i32 %x, i32 %y, float %fx, float %fy, i1 %cond, "
785       "<4 x i32> %vx, <4 x i32> %vx2, <vscale x 4 x i32> %svx, i8* %p) {\n";
786   std::string AsmTail = "  ret void\n}";
787   // (can create poison?, can create undef?, IR instruction)
788   SmallVector<std::pair<std::pair<bool, bool>, std::string>, 32> Data = {
789       {{false, false}, "add i32 %x, %y"},
790       {{true, false}, "add nsw nuw i32 %x, %y"},
791       {{true, false}, "shl i32 %x, %y"},
792       {{true, false}, "shl <4 x i32> %vx, %vx2"},
793       {{true, false}, "shl nsw i32 %x, %y"},
794       {{true, false}, "shl nsw <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"},
795       {{false, false}, "shl i32 %x, 31"},
796       {{true, false}, "shl i32 %x, 32"},
797       {{false, false}, "shl <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"},
798       {{true, false}, "shl <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 32>"},
799       {{true, false}, "ashr i32 %x, %y"},
800       {{true, false}, "ashr exact i32 %x, %y"},
801       {{false, false}, "ashr i32 %x, 31"},
802       {{true, false}, "ashr exact i32 %x, 31"},
803       {{false, false}, "ashr <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"},
804       {{true, false}, "ashr <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 32>"},
805       {{true, false}, "ashr exact <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"},
806       {{true, false}, "lshr i32 %x, %y"},
807       {{true, false}, "lshr exact i32 %x, 31"},
808       {{false, false}, "udiv i32 %x, %y"},
809       {{true, false}, "udiv exact i32 %x, %y"},
810       {{false, false}, "getelementptr i8, i8* %p, i32 %x"},
811       {{true, false}, "getelementptr inbounds i8, i8* %p, i32 %x"},
812       {{true, false}, "fneg nnan float %fx"},
813       {{false, false}, "fneg float %fx"},
814       {{false, false}, "fadd float %fx, %fy"},
815       {{true, false}, "fadd nnan float %fx, %fy"},
816       {{false, false}, "urem i32 %x, %y"},
817       {{true, false}, "fptoui float %fx to i32"},
818       {{true, false}, "fptosi float %fx to i32"},
819       {{false, false}, "bitcast float %fx to i32"},
820       {{false, false}, "select i1 %cond, i32 %x, i32 %y"},
821       {{true, false}, "select nnan i1 %cond, float %fx, float %fy"},
822       {{true, false}, "extractelement <4 x i32> %vx, i32 %x"},
823       {{false, false}, "extractelement <4 x i32> %vx, i32 3"},
824       {{true, false}, "extractelement <vscale x 4 x i32> %svx, i32 4"},
825       {{true, false}, "insertelement <4 x i32> %vx, i32 %x, i32 %y"},
826       {{false, false}, "insertelement <4 x i32> %vx, i32 %x, i32 3"},
827       {{true, false}, "insertelement <vscale x 4 x i32> %svx, i32 %x, i32 4"},
828       {{false, false}, "freeze i32 %x"},
829       {{false, false},
830        "shufflevector <4 x i32> %vx, <4 x i32> %vx2, "
831        "<4 x i32> <i32 0, i32 1, i32 2, i32 3>"},
832       {{false, true},
833        "shufflevector <4 x i32> %vx, <4 x i32> %vx2, "
834        "<4 x i32> <i32 0, i32 1, i32 2, i32 undef>"},
835       {{false, true},
836        "shufflevector <vscale x 4 x i32> %svx, "
837        "<vscale x 4 x i32> %svx, <vscale x 4 x i32> undef"},
838       {{true, false}, "call i32 @g(i32 %x)"},
839       {{false, false}, "call noundef i32 @g(i32 %x)"},
840       {{true, false}, "fcmp nnan oeq float %fx, %fy"},
841       {{false, false}, "fcmp oeq float %fx, %fy"}};
842 
843   std::string AssemblyStr = AsmHead;
844   for (auto &Itm : Data)
845     AssemblyStr += Itm.second + "\n";
846   AssemblyStr += AsmTail;
847 
848   LLVMContext Context;
849   SMDiagnostic Error;
850   auto M = parseAssemblyString(AssemblyStr, Error, Context);
851   assert(M && "Bad assembly?");
852 
853   auto *F = M->getFunction("f");
854   assert(F && "Bad assembly?");
855 
856   auto &BB = F->getEntryBlock();
857 
858   int Index = 0;
859   for (auto &I : BB) {
860     if (isa<ReturnInst>(&I))
861       break;
862     bool Poison = Data[Index].first.first;
863     bool Undef = Data[Index].first.second;
864     EXPECT_EQ(canCreatePoison(cast<Operator>(&I)), Poison)
865         << "Incorrect answer of canCreatePoison at instruction " << Index
866         << " = " << I;
867     EXPECT_EQ(canCreateUndefOrPoison(cast<Operator>(&I)), Undef || Poison)
868         << "Incorrect answer of canCreateUndef at instruction " << Index
869         << " = " << I;
870     Index++;
871   }
872 }
873 
874 TEST_F(ComputeKnownBitsTest, ComputeKnownBits) {
875   parseAssembly(
876       "define i32 @test(i32 %a, i32 %b) {\n"
877       "  %ash = mul i32 %a, 8\n"
878       "  %aad = add i32 %ash, 7\n"
879       "  %aan = and i32 %aad, 4095\n"
880       "  %bsh = shl i32 %b, 4\n"
881       "  %bad = or i32 %bsh, 6\n"
882       "  %ban = and i32 %bad, 4095\n"
883       "  %A = mul i32 %aan, %ban\n"
884       "  ret i32 %A\n"
885       "}\n");
886   expectKnownBits(/*zero*/ 4278190085u, /*one*/ 10u);
887 }
888 
889 TEST_F(ComputeKnownBitsTest, ComputeKnownMulBits) {
890   parseAssembly(
891       "define i32 @test(i32 %a, i32 %b) {\n"
892       "  %aa = shl i32 %a, 5\n"
893       "  %bb = shl i32 %b, 5\n"
894       "  %aaa = or i32 %aa, 24\n"
895       "  %bbb = or i32 %bb, 28\n"
896       "  %A = mul i32 %aaa, %bbb\n"
897       "  ret i32 %A\n"
898       "}\n");
899   expectKnownBits(/*zero*/ 95u, /*one*/ 32u);
900 }
901 
902 TEST_F(ComputeKnownBitsTest, KnownNonZeroShift) {
903   // %q is known nonzero without known bits.
904   // Because %q is nonzero, %A[0] is known to be zero.
905   parseAssembly(
906       "define i8 @test(i8 %p, i8* %pq) {\n"
907       "  %q = load i8, i8* %pq, !range !0\n"
908       "  %A = shl i8 %p, %q\n"
909       "  ret i8 %A\n"
910       "}\n"
911       "!0 = !{ i8 1, i8 5 }\n");
912   expectKnownBits(/*zero*/ 1u, /*one*/ 0u);
913 }
914 
915 TEST_F(ComputeKnownBitsTest, ComputeKnownFshl) {
916   // fshl(....1111....0000, 00..1111........, 6)
917   // = 11....000000..11
918   parseAssembly(
919       "define i16 @test(i16 %a, i16 %b) {\n"
920       "  %aa = shl i16 %a, 4\n"
921       "  %bb = lshr i16 %b, 2\n"
922       "  %aaa = or i16 %aa, 3840\n"
923       "  %bbb = or i16 %bb, 3840\n"
924       "  %A = call i16 @llvm.fshl.i16(i16 %aaa, i16 %bbb, i16 6)\n"
925       "  ret i16 %A\n"
926       "}\n"
927       "declare i16 @llvm.fshl.i16(i16, i16, i16)\n");
928   expectKnownBits(/*zero*/ 1008u, /*one*/ 49155u);
929 }
930 
931 TEST_F(ComputeKnownBitsTest, ComputeKnownFshr) {
932   // fshr(....1111....0000, 00..1111........, 26)
933   // = 11....000000..11
934   parseAssembly(
935       "define i16 @test(i16 %a, i16 %b) {\n"
936       "  %aa = shl i16 %a, 4\n"
937       "  %bb = lshr i16 %b, 2\n"
938       "  %aaa = or i16 %aa, 3840\n"
939       "  %bbb = or i16 %bb, 3840\n"
940       "  %A = call i16 @llvm.fshr.i16(i16 %aaa, i16 %bbb, i16 26)\n"
941       "  ret i16 %A\n"
942       "}\n"
943       "declare i16 @llvm.fshr.i16(i16, i16, i16)\n");
944   expectKnownBits(/*zero*/ 1008u, /*one*/ 49155u);
945 }
946 
947 TEST_F(ComputeKnownBitsTest, ComputeKnownFshlZero) {
948   // fshl(....1111....0000, 00..1111........, 0)
949   // = ....1111....0000
950   parseAssembly(
951       "define i16 @test(i16 %a, i16 %b) {\n"
952       "  %aa = shl i16 %a, 4\n"
953       "  %bb = lshr i16 %b, 2\n"
954       "  %aaa = or i16 %aa, 3840\n"
955       "  %bbb = or i16 %bb, 3840\n"
956       "  %A = call i16 @llvm.fshl.i16(i16 %aaa, i16 %bbb, i16 0)\n"
957       "  ret i16 %A\n"
958       "}\n"
959       "declare i16 @llvm.fshl.i16(i16, i16, i16)\n");
960   expectKnownBits(/*zero*/ 15u, /*one*/ 3840u);
961 }
962 
963 TEST_F(ComputeKnownBitsTest, ComputeKnownUAddSatLeadingOnes) {
964   // uadd.sat(1111...1, ........)
965   // = 1111....
966   parseAssembly(
967       "define i8 @test(i8 %a, i8 %b) {\n"
968       "  %aa = or i8 %a, 241\n"
969       "  %A = call i8 @llvm.uadd.sat.i8(i8 %aa, i8 %b)\n"
970       "  ret i8 %A\n"
971       "}\n"
972       "declare i8 @llvm.uadd.sat.i8(i8, i8)\n");
973   expectKnownBits(/*zero*/ 0u, /*one*/ 240u);
974 }
975 
976 TEST_F(ComputeKnownBitsTest, ComputeKnownUAddSatOnesPreserved) {
977   // uadd.sat(00...011, .1...110)
978   // = .......1
979   parseAssembly(
980       "define i8 @test(i8 %a, i8 %b) {\n"
981       "  %aa = or i8 %a, 3\n"
982       "  %aaa = and i8 %aa, 59\n"
983       "  %bb = or i8 %b, 70\n"
984       "  %bbb = and i8 %bb, 254\n"
985       "  %A = call i8 @llvm.uadd.sat.i8(i8 %aaa, i8 %bbb)\n"
986       "  ret i8 %A\n"
987       "}\n"
988       "declare i8 @llvm.uadd.sat.i8(i8, i8)\n");
989   expectKnownBits(/*zero*/ 0u, /*one*/ 1u);
990 }
991 
992 TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatLHSLeadingZeros) {
993   // usub.sat(0000...0, ........)
994   // = 0000....
995   parseAssembly(
996       "define i8 @test(i8 %a, i8 %b) {\n"
997       "  %aa = and i8 %a, 14\n"
998       "  %A = call i8 @llvm.usub.sat.i8(i8 %aa, i8 %b)\n"
999       "  ret i8 %A\n"
1000       "}\n"
1001       "declare i8 @llvm.usub.sat.i8(i8, i8)\n");
1002   expectKnownBits(/*zero*/ 240u, /*one*/ 0u);
1003 }
1004 
1005 TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatRHSLeadingOnes) {
1006   // usub.sat(........, 1111...1)
1007   // = 0000....
1008   parseAssembly(
1009       "define i8 @test(i8 %a, i8 %b) {\n"
1010       "  %bb = or i8 %a, 241\n"
1011       "  %A = call i8 @llvm.usub.sat.i8(i8 %a, i8 %bb)\n"
1012       "  ret i8 %A\n"
1013       "}\n"
1014       "declare i8 @llvm.usub.sat.i8(i8, i8)\n");
1015   expectKnownBits(/*zero*/ 240u, /*one*/ 0u);
1016 }
1017 
1018 TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatZerosPreserved) {
1019   // usub.sat(11...011, .1...110)
1020   // = ......0.
1021   parseAssembly(
1022       "define i8 @test(i8 %a, i8 %b) {\n"
1023       "  %aa = or i8 %a, 195\n"
1024       "  %aaa = and i8 %aa, 251\n"
1025       "  %bb = or i8 %b, 70\n"
1026       "  %bbb = and i8 %bb, 254\n"
1027       "  %A = call i8 @llvm.usub.sat.i8(i8 %aaa, i8 %bbb)\n"
1028       "  ret i8 %A\n"
1029       "}\n"
1030       "declare i8 @llvm.usub.sat.i8(i8, i8)\n");
1031   expectKnownBits(/*zero*/ 2u, /*one*/ 0u);
1032 }
1033 
1034 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsPtrToIntTrunc) {
1035   // ptrtoint truncates the pointer type.
1036   parseAssembly(
1037       "define void @test(i8** %p) {\n"
1038       "  %A = load i8*, i8** %p\n"
1039       "  %i = ptrtoint i8* %A to i32\n"
1040       "  %m = and i32 %i, 31\n"
1041       "  %c = icmp eq i32 %m, 0\n"
1042       "  call void @llvm.assume(i1 %c)\n"
1043       "  ret void\n"
1044       "}\n"
1045       "declare void @llvm.assume(i1)\n");
1046   AssumptionCache AC(*F);
1047   KnownBits Known = computeKnownBits(
1048       A, M->getDataLayout(), /* Depth */ 0, &AC, F->front().getTerminator());
1049   EXPECT_EQ(Known.Zero.getZExtValue(), 31u);
1050   EXPECT_EQ(Known.One.getZExtValue(), 0u);
1051 }
1052 
1053 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsPtrToIntZext) {
1054   // ptrtoint zero extends the pointer type.
1055   parseAssembly(
1056       "define void @test(i8** %p) {\n"
1057       "  %A = load i8*, i8** %p\n"
1058       "  %i = ptrtoint i8* %A to i128\n"
1059       "  %m = and i128 %i, 31\n"
1060       "  %c = icmp eq i128 %m, 0\n"
1061       "  call void @llvm.assume(i1 %c)\n"
1062       "  ret void\n"
1063       "}\n"
1064       "declare void @llvm.assume(i1)\n");
1065   AssumptionCache AC(*F);
1066   KnownBits Known = computeKnownBits(
1067       A, M->getDataLayout(), /* Depth */ 0, &AC, F->front().getTerminator());
1068   EXPECT_EQ(Known.Zero.getZExtValue(), 31u);
1069   EXPECT_EQ(Known.One.getZExtValue(), 0u);
1070 }
1071 
1072 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsFreeze) {
1073   parseAssembly("define void @test() {\n"
1074                 "  %m = call i32 @any_num()\n"
1075                 "  %A = freeze i32 %m\n"
1076                 "  %n = and i32 %m, 31\n"
1077                 "  %c = icmp eq i32 %n, 0\n"
1078                 "  call void @llvm.assume(i1 %c)\n"
1079                 "  ret void\n"
1080                 "}\n"
1081                 "declare void @llvm.assume(i1)\n"
1082                 "declare i32 @any_num()\n");
1083   AssumptionCache AC(*F);
1084   KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC,
1085                                      F->front().getTerminator());
1086   EXPECT_EQ(Known.Zero.getZExtValue(), 31u);
1087   EXPECT_EQ(Known.One.getZExtValue(), 0u);
1088 }
1089 
1090 class IsBytewiseValueTest : public ValueTrackingTest,
1091                             public ::testing::WithParamInterface<
1092                                 std::pair<const char *, const char *>> {
1093 protected:
1094 };
1095 
1096 const std::pair<const char *, const char *> IsBytewiseValueTests[] = {
1097     {
1098         "i8 0",
1099         "i48* null",
1100     },
1101     {
1102         "i8 undef",
1103         "i48* undef",
1104     },
1105     {
1106         "i8 0",
1107         "i8 zeroinitializer",
1108     },
1109     {
1110         "i8 0",
1111         "i8 0",
1112     },
1113     {
1114         "i8 -86",
1115         "i8 -86",
1116     },
1117     {
1118         "i8 -1",
1119         "i8 -1",
1120     },
1121     {
1122         "i8 undef",
1123         "i16 undef",
1124     },
1125     {
1126         "i8 0",
1127         "i16 0",
1128     },
1129     {
1130         "",
1131         "i16 7",
1132     },
1133     {
1134         "i8 -86",
1135         "i16 -21846",
1136     },
1137     {
1138         "i8 -1",
1139         "i16 -1",
1140     },
1141     {
1142         "i8 0",
1143         "i48 0",
1144     },
1145     {
1146         "i8 -1",
1147         "i48 -1",
1148     },
1149     {
1150         "i8 0",
1151         "i49 0",
1152     },
1153     {
1154         "",
1155         "i49 -1",
1156     },
1157     {
1158         "i8 0",
1159         "half 0xH0000",
1160     },
1161     {
1162         "i8 -85",
1163         "half 0xHABAB",
1164     },
1165     {
1166         "i8 0",
1167         "float 0.0",
1168     },
1169     {
1170         "i8 -1",
1171         "float 0xFFFFFFFFE0000000",
1172     },
1173     {
1174         "i8 0",
1175         "double 0.0",
1176     },
1177     {
1178         "i8 -15",
1179         "double 0xF1F1F1F1F1F1F1F1",
1180     },
1181     {
1182         "i8 undef",
1183         "i16* undef",
1184     },
1185     {
1186         "i8 0",
1187         "i16* inttoptr (i64 0 to i16*)",
1188     },
1189     {
1190         "i8 -1",
1191         "i16* inttoptr (i64 -1 to i16*)",
1192     },
1193     {
1194         "i8 -86",
1195         "i16* inttoptr (i64 -6148914691236517206 to i16*)",
1196     },
1197     {
1198         "",
1199         "i16* inttoptr (i48 -1 to i16*)",
1200     },
1201     {
1202         "i8 -1",
1203         "i16* inttoptr (i96 -1 to i16*)",
1204     },
1205     {
1206         "i8 undef",
1207         "[0 x i8] zeroinitializer",
1208     },
1209     {
1210         "i8 undef",
1211         "[0 x i8] undef",
1212     },
1213     {
1214         "i8 undef",
1215         "[5 x [0 x i8]] zeroinitializer",
1216     },
1217     {
1218         "i8 undef",
1219         "[5 x [0 x i8]] undef",
1220     },
1221     {
1222         "i8 0",
1223         "[6 x i8] zeroinitializer",
1224     },
1225     {
1226         "i8 undef",
1227         "[6 x i8] undef",
1228     },
1229     {
1230         "i8 1",
1231         "[5 x i8] [i8 1, i8 1, i8 1, i8 1, i8 1]",
1232     },
1233     {
1234         "",
1235         "[5 x i64] [i64 1, i64 1, i64 1, i64 1, i64 1]",
1236     },
1237     {
1238         "i8 -1",
1239         "[5 x i64] [i64 -1, i64 -1, i64 -1, i64 -1, i64 -1]",
1240     },
1241     {
1242         "",
1243         "[4 x i8] [i8 1, i8 2, i8 1, i8 1]",
1244     },
1245     {
1246         "i8 1",
1247         "[4 x i8] [i8 1, i8 undef, i8 1, i8 1]",
1248     },
1249     {
1250         "i8 0",
1251         "<6 x i8> zeroinitializer",
1252     },
1253     {
1254         "i8 undef",
1255         "<6 x i8> undef",
1256     },
1257     {
1258         "i8 1",
1259         "<5 x i8> <i8 1, i8 1, i8 1, i8 1, i8 1>",
1260     },
1261     {
1262         "",
1263         "<5 x i64> <i64 1, i64 1, i64 1, i64 1, i64 1>",
1264     },
1265     {
1266         "i8 -1",
1267         "<5 x i64> <i64 -1, i64 -1, i64 -1, i64 -1, i64 -1>",
1268     },
1269     {
1270         "",
1271         "<4 x i8> <i8 1, i8 1, i8 2, i8 1>",
1272     },
1273     {
1274         "i8 5",
1275         "<2 x i8> < i8 5, i8 undef >",
1276     },
1277     {
1278         "i8 0",
1279         "[2 x [2 x i16]] zeroinitializer",
1280     },
1281     {
1282         "i8 undef",
1283         "[2 x [2 x i16]] undef",
1284     },
1285     {
1286         "i8 -86",
1287         "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], "
1288         "[2 x i16] [i16 -21846, i16 -21846]]",
1289     },
1290     {
1291         "",
1292         "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], "
1293         "[2 x i16] [i16 -21836, i16 -21846]]",
1294     },
1295     {
1296         "i8 undef",
1297         "{ } zeroinitializer",
1298     },
1299     {
1300         "i8 undef",
1301         "{ } undef",
1302     },
1303     {
1304         "i8 undef",
1305         "{ {}, {} } zeroinitializer",
1306     },
1307     {
1308         "i8 undef",
1309         "{ {}, {} } undef",
1310     },
1311     {
1312         "i8 0",
1313         "{i8, i64, i16*} zeroinitializer",
1314     },
1315     {
1316         "i8 undef",
1317         "{i8, i64, i16*} undef",
1318     },
1319     {
1320         "i8 -86",
1321         "{i8, i64, i16*} {i8 -86, i64 -6148914691236517206, i16* undef}",
1322     },
1323     {
1324         "",
1325         "{i8, i64, i16*} {i8 86, i64 -6148914691236517206, i16* undef}",
1326     },
1327 };
1328 
1329 INSTANTIATE_TEST_CASE_P(IsBytewiseValueParamTests, IsBytewiseValueTest,
1330                         ::testing::ValuesIn(IsBytewiseValueTests),);
1331 
1332 TEST_P(IsBytewiseValueTest, IsBytewiseValue) {
1333   auto M = parseModule(std::string("@test = global ") + GetParam().second);
1334   GlobalVariable *GV = dyn_cast<GlobalVariable>(M->getNamedValue("test"));
1335   Value *Actual = isBytewiseValue(GV->getInitializer(), M->getDataLayout());
1336   std::string Buff;
1337   raw_string_ostream S(Buff);
1338   if (Actual)
1339     S << *Actual;
1340   EXPECT_EQ(GetParam().first, S.str());
1341 }
1342 
1343 TEST_F(ValueTrackingTest, ComputeConstantRange) {
1344   {
1345     // Assumptions:
1346     //  * stride >= 5
1347     //  * stride < 10
1348     //
1349     // stride = [5, 10)
1350     auto M = parseModule(R"(
1351   declare void @llvm.assume(i1)
1352 
1353   define i32 @test(i32 %stride) {
1354     %gt = icmp uge i32 %stride, 5
1355     call void @llvm.assume(i1 %gt)
1356     %lt = icmp ult i32 %stride, 10
1357     call void @llvm.assume(i1 %lt)
1358     %stride.plus.one = add nsw nuw i32 %stride, 1
1359     ret i32 %stride.plus.one
1360   })");
1361     Function *F = M->getFunction("test");
1362 
1363     AssumptionCache AC(*F);
1364     Value *Stride = &*F->arg_begin();
1365     ConstantRange CR1 = computeConstantRange(Stride, true, &AC, nullptr);
1366     EXPECT_TRUE(CR1.isFullSet());
1367 
1368     Instruction *I = &findInstructionByName(F, "stride.plus.one");
1369     ConstantRange CR2 = computeConstantRange(Stride, true, &AC, I);
1370     EXPECT_EQ(5, CR2.getLower());
1371     EXPECT_EQ(10, CR2.getUpper());
1372   }
1373 
1374   {
1375     // Assumptions:
1376     //  * stride >= 5
1377     //  * stride < 200
1378     //  * stride == 99
1379     //
1380     // stride = [99, 100)
1381     auto M = parseModule(R"(
1382   declare void @llvm.assume(i1)
1383 
1384   define i32 @test(i32 %stride) {
1385     %gt = icmp uge i32 %stride, 5
1386     call void @llvm.assume(i1 %gt)
1387     %lt = icmp ult i32 %stride, 200
1388     call void @llvm.assume(i1 %lt)
1389     %eq = icmp eq i32 %stride, 99
1390     call void @llvm.assume(i1 %eq)
1391     %stride.plus.one = add nsw nuw i32 %stride, 1
1392     ret i32 %stride.plus.one
1393   })");
1394     Function *F = M->getFunction("test");
1395 
1396     AssumptionCache AC(*F);
1397     Value *Stride = &*F->arg_begin();
1398     Instruction *I = &findInstructionByName(F, "stride.plus.one");
1399     ConstantRange CR = computeConstantRange(Stride, true, &AC, I);
1400     EXPECT_EQ(99, *CR.getSingleElement());
1401   }
1402 
1403   {
1404     // Assumptions:
1405     //  * stride >= 5
1406     //  * stride >= 50
1407     //  * stride < 100
1408     //  * stride < 200
1409     //
1410     // stride = [50, 100)
1411     auto M = parseModule(R"(
1412   declare void @llvm.assume(i1)
1413 
1414   define i32 @test(i32 %stride, i1 %cond) {
1415     %gt = icmp uge i32 %stride, 5
1416     call void @llvm.assume(i1 %gt)
1417     %gt.2 = icmp uge i32 %stride, 50
1418     call void @llvm.assume(i1 %gt.2)
1419     br i1 %cond, label %bb1, label %bb2
1420 
1421   bb1:
1422     %lt = icmp ult i32 %stride, 200
1423     call void @llvm.assume(i1 %lt)
1424     %lt.2 = icmp ult i32 %stride, 100
1425     call void @llvm.assume(i1 %lt.2)
1426     %stride.plus.one = add nsw nuw i32 %stride, 1
1427     ret i32 %stride.plus.one
1428 
1429   bb2:
1430     ret i32 0
1431   })");
1432     Function *F = M->getFunction("test");
1433 
1434     AssumptionCache AC(*F);
1435     Value *Stride = &*F->arg_begin();
1436     Instruction *GT2 = &findInstructionByName(F, "gt.2");
1437     ConstantRange CR = computeConstantRange(Stride, true, &AC, GT2);
1438     EXPECT_EQ(5, CR.getLower());
1439     EXPECT_EQ(0, CR.getUpper());
1440 
1441     Instruction *I = &findInstructionByName(F, "stride.plus.one");
1442     ConstantRange CR2 = computeConstantRange(Stride, true, &AC, I);
1443     EXPECT_EQ(50, CR2.getLower());
1444     EXPECT_EQ(100, CR2.getUpper());
1445   }
1446 
1447   {
1448     // Assumptions:
1449     //  * stride > 5
1450     //  * stride < 5
1451     //
1452     // stride = empty range, as the assumptions contradict each other.
1453     auto M = parseModule(R"(
1454   declare void @llvm.assume(i1)
1455 
1456   define i32 @test(i32 %stride, i1 %cond) {
1457     %gt = icmp ugt i32 %stride, 5
1458     call void @llvm.assume(i1 %gt)
1459     %lt = icmp ult i32 %stride, 5
1460     call void @llvm.assume(i1 %lt)
1461     %stride.plus.one = add nsw nuw i32 %stride, 1
1462     ret i32 %stride.plus.one
1463   })");
1464     Function *F = M->getFunction("test");
1465 
1466     AssumptionCache AC(*F);
1467     Value *Stride = &*F->arg_begin();
1468 
1469     Instruction *I = &findInstructionByName(F, "stride.plus.one");
1470     ConstantRange CR = computeConstantRange(Stride, true, &AC, I);
1471     EXPECT_TRUE(CR.isEmptySet());
1472   }
1473 
1474   {
1475     // Assumptions:
1476     //  * x.1 >= 5
1477     //  * x.2 < x.1
1478     //
1479     // stride = [0, 5)
1480     auto M = parseModule(R"(
1481   declare void @llvm.assume(i1)
1482 
1483   define i32 @test(i32 %x.1, i32 %x.2) {
1484     %gt = icmp uge i32 %x.1, 5
1485     call void @llvm.assume(i1 %gt)
1486     %lt = icmp ult i32 %x.2, %x.1
1487     call void @llvm.assume(i1 %lt)
1488     %stride.plus.one = add nsw nuw i32 %x.1, 1
1489     ret i32 %stride.plus.one
1490   })");
1491     Function *F = M->getFunction("test");
1492 
1493     AssumptionCache AC(*F);
1494     Value *X2 = &*std::next(F->arg_begin());
1495 
1496     Instruction *I = &findInstructionByName(F, "stride.plus.one");
1497     ConstantRange CR1 = computeConstantRange(X2, true, &AC, I);
1498     EXPECT_EQ(0, CR1.getLower());
1499     EXPECT_EQ(5, CR1.getUpper());
1500 
1501     // Check the depth cutoff results in a conservative result (full set) by
1502     // passing Depth == MaxDepth == 6.
1503     ConstantRange CR2 = computeConstantRange(X2, true, &AC, I, 6);
1504     EXPECT_TRUE(CR2.isFullSet());
1505   }
1506 }
1507 
1508 struct FindAllocaForValueTestParams {
1509   const char *IR;
1510   bool AnyOffsetResult;
1511   bool ZeroOffsetResult;
1512 };
1513 
1514 class FindAllocaForValueTest
1515     : public ValueTrackingTest,
1516       public ::testing::WithParamInterface<FindAllocaForValueTestParams> {
1517 protected:
1518 };
1519 
1520 const FindAllocaForValueTestParams FindAllocaForValueTests[] = {
1521     {R"(
1522       define void @test() {
1523         %a = alloca i64
1524         %r = bitcast i64* %a to i32*
1525         ret void
1526       })",
1527      true, true},
1528 
1529     {R"(
1530       define void @test() {
1531         %a = alloca i32
1532         %r = getelementptr i32, i32* %a, i32 1
1533         ret void
1534       })",
1535      true, false},
1536 
1537     {R"(
1538       define void @test() {
1539         %a = alloca i32
1540         %r = getelementptr i32, i32* %a, i32 0
1541         ret void
1542       })",
1543      true, true},
1544 
1545     {R"(
1546       define void @test(i1 %cond) {
1547       entry:
1548         %a = alloca i32
1549         br label %bb1
1550 
1551       bb1:
1552         %r = phi i32* [ %a, %entry ], [ %r, %bb1 ]
1553         br i1 %cond, label %bb1, label %exit
1554 
1555       exit:
1556         ret void
1557       })",
1558      true, true},
1559 
1560     {R"(
1561       define void @test(i1 %cond) {
1562         %a = alloca i32
1563         %r = select i1 %cond, i32* %a, i32* %a
1564         ret void
1565       })",
1566      true, true},
1567 
1568     {R"(
1569       define void @test(i1 %cond) {
1570         %a = alloca i32
1571         %b = alloca i32
1572         %r = select i1 %cond, i32* %a, i32* %b
1573         ret void
1574       })",
1575      false, false},
1576 
1577     {R"(
1578       define void @test(i1 %cond) {
1579       entry:
1580         %a = alloca i64
1581         %a32 = bitcast i64* %a to i32*
1582         br label %bb1
1583 
1584       bb1:
1585         %x = phi i32* [ %a32, %entry ], [ %x, %bb1 ]
1586         %r = getelementptr i32, i32* %x, i32 1
1587         br i1 %cond, label %bb1, label %exit
1588 
1589       exit:
1590         ret void
1591       })",
1592      true, false},
1593 
1594     {R"(
1595       define void @test(i1 %cond) {
1596       entry:
1597         %a = alloca i64
1598         %a32 = bitcast i64* %a to i32*
1599         br label %bb1
1600 
1601       bb1:
1602         %x = phi i32* [ %a32, %entry ], [ %r, %bb1 ]
1603         %r = getelementptr i32, i32* %x, i32 1
1604         br i1 %cond, label %bb1, label %exit
1605 
1606       exit:
1607         ret void
1608       })",
1609      true, false},
1610 
1611     {R"(
1612       define void @test(i1 %cond, i64* %a) {
1613       entry:
1614         %r = bitcast i64* %a to i32*
1615         ret void
1616       })",
1617      false, false},
1618 
1619     {R"(
1620       define void @test(i1 %cond) {
1621       entry:
1622         %a = alloca i32
1623         %b = alloca i32
1624         br label %bb1
1625 
1626       bb1:
1627         %r = phi i32* [ %a, %entry ], [ %b, %bb1 ]
1628         br i1 %cond, label %bb1, label %exit
1629 
1630       exit:
1631         ret void
1632       })",
1633      false, false},
1634 };
1635 
1636 TEST_P(FindAllocaForValueTest, findAllocaForValue) {
1637   auto M = parseModule(GetParam().IR);
1638   Function *F = M->getFunction("test");
1639   Instruction *I = &findInstructionByName(F, "r");
1640   const AllocaInst *AI = findAllocaForValue(I);
1641   EXPECT_EQ(!!AI, GetParam().AnyOffsetResult);
1642 }
1643 
1644 TEST_P(FindAllocaForValueTest, findAllocaForValueZeroOffset) {
1645   auto M = parseModule(GetParam().IR);
1646   Function *F = M->getFunction("test");
1647   Instruction *I = &findInstructionByName(F, "r");
1648   const AllocaInst *AI = findAllocaForValue(I, true);
1649   EXPECT_EQ(!!AI, GetParam().ZeroOffsetResult);
1650 }
1651 
1652 INSTANTIATE_TEST_CASE_P(FindAllocaForValueTest, FindAllocaForValueTest,
1653                         ::testing::ValuesIn(FindAllocaForValueTests), );
1654