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