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/Function.h"
13 #include "llvm/IR/InstIterator.h"
14 #include "llvm/IR/Instructions.h"
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/KnownBits.h"
19 #include "llvm/Support/SourceMgr.h"
20 #include "gtest/gtest.h"
21 
22 using namespace llvm;
23 
24 namespace {
25 
26 class ValueTrackingTest : public testing::Test {
27 protected:
28   std::unique_ptr<Module> parseModule(StringRef Assembly) {
29     SMDiagnostic Error;
30     std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context);
31 
32     std::string errMsg;
33     raw_string_ostream os(errMsg);
34     Error.print("", os);
35     EXPECT_TRUE(M) << os.str();
36 
37     return M;
38   }
39 
40   void parseAssembly(StringRef Assembly) {
41     M = parseModule(Assembly);
42     ASSERT_TRUE(M);
43 
44     F = M->getFunction("test");
45     ASSERT_TRUE(F) << "Test must have a function @test";
46     if (!F)
47       return;
48 
49     A = nullptr;
50     for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
51       if (I->hasName()) {
52         if (I->getName() == "A")
53           A = &*I;
54       }
55     }
56     ASSERT_TRUE(A) << "@test must have an instruction %A";
57   }
58 
59   LLVMContext Context;
60   std::unique_ptr<Module> M;
61   Function *F = nullptr;
62   Instruction *A = nullptr;
63 };
64 
65 class MatchSelectPatternTest : public ValueTrackingTest {
66 protected:
67   void expectPattern(const SelectPatternResult &P) {
68     Value *LHS, *RHS;
69     Instruction::CastOps CastOp;
70     SelectPatternResult R = matchSelectPattern(A, LHS, RHS, &CastOp);
71     EXPECT_EQ(P.Flavor, R.Flavor);
72     EXPECT_EQ(P.NaNBehavior, R.NaNBehavior);
73     EXPECT_EQ(P.Ordered, R.Ordered);
74   }
75 };
76 
77 class ComputeKnownBitsTest : public ValueTrackingTest {
78 protected:
79   void expectKnownBits(uint64_t Zero, uint64_t One) {
80     auto Known = computeKnownBits(A, M->getDataLayout());
81     ASSERT_FALSE(Known.hasConflict());
82     EXPECT_EQ(Known.One.getZExtValue(), One);
83     EXPECT_EQ(Known.Zero.getZExtValue(), Zero);
84   }
85 };
86 
87 }
88 
89 TEST_F(MatchSelectPatternTest, SimpleFMin) {
90   parseAssembly(
91       "define float @test(float %a) {\n"
92       "  %1 = fcmp ult float %a, 5.0\n"
93       "  %A = select i1 %1, float %a, float 5.0\n"
94       "  ret float %A\n"
95       "}\n");
96   expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false});
97 }
98 
99 TEST_F(MatchSelectPatternTest, SimpleFMax) {
100   parseAssembly(
101       "define float @test(float %a) {\n"
102       "  %1 = fcmp ogt float %a, 5.0\n"
103       "  %A = select i1 %1, float %a, float 5.0\n"
104       "  ret float %A\n"
105       "}\n");
106   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true});
107 }
108 
109 TEST_F(MatchSelectPatternTest, SwappedFMax) {
110   parseAssembly(
111       "define float @test(float %a) {\n"
112       "  %1 = fcmp olt float 5.0, %a\n"
113       "  %A = select i1 %1, float %a, float 5.0\n"
114       "  ret float %A\n"
115       "}\n");
116   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, false});
117 }
118 
119 TEST_F(MatchSelectPatternTest, SwappedFMax2) {
120   parseAssembly(
121       "define float @test(float %a) {\n"
122       "  %1 = fcmp olt float %a, 5.0\n"
123       "  %A = select i1 %1, float 5.0, float %a\n"
124       "  ret float %A\n"
125       "}\n");
126   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, false});
127 }
128 
129 TEST_F(MatchSelectPatternTest, SwappedFMax3) {
130   parseAssembly(
131       "define float @test(float %a) {\n"
132       "  %1 = fcmp ult float %a, 5.0\n"
133       "  %A = select i1 %1, float 5.0, float %a\n"
134       "  ret float %A\n"
135       "}\n");
136   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true});
137 }
138 
139 TEST_F(MatchSelectPatternTest, FastFMin) {
140   parseAssembly(
141       "define float @test(float %a) {\n"
142       "  %1 = fcmp nnan olt float %a, 5.0\n"
143       "  %A = select i1 %1, float %a, float 5.0\n"
144       "  ret float %A\n"
145       "}\n");
146   expectPattern({SPF_FMINNUM, SPNB_RETURNS_ANY, false});
147 }
148 
149 TEST_F(MatchSelectPatternTest, FMinConstantZero) {
150   parseAssembly(
151       "define float @test(float %a) {\n"
152       "  %1 = fcmp ole float %a, 0.0\n"
153       "  %A = select i1 %1, float %a, float 0.0\n"
154       "  ret float %A\n"
155       "}\n");
156   // This shouldn't be matched, as %a could be -0.0.
157   expectPattern({SPF_UNKNOWN, SPNB_NA, false});
158 }
159 
160 TEST_F(MatchSelectPatternTest, FMinConstantZeroNsz) {
161   parseAssembly(
162       "define float @test(float %a) {\n"
163       "  %1 = fcmp nsz ole float %a, 0.0\n"
164       "  %A = select i1 %1, float %a, float 0.0\n"
165       "  ret float %A\n"
166       "}\n");
167   // But this should be, because we've ignored signed zeroes.
168   expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true});
169 }
170 
171 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero1) {
172   parseAssembly(
173       "define float @test(float %a) {\n"
174       "  %1 = fcmp olt float -0.0, %a\n"
175       "  %A = select i1 %1, float 0.0, float %a\n"
176       "  ret float %A\n"
177       "}\n");
178   // The sign of zero doesn't matter in fcmp.
179   expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, true});
180 }
181 
182 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero2) {
183   parseAssembly(
184       "define float @test(float %a) {\n"
185       "  %1 = fcmp ogt float %a, -0.0\n"
186       "  %A = select i1 %1, float 0.0, float %a\n"
187       "  ret float %A\n"
188       "}\n");
189   // The sign of zero doesn't matter in fcmp.
190   expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false});
191 }
192 
193 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero3) {
194   parseAssembly(
195       "define float @test(float %a) {\n"
196       "  %1 = fcmp olt float 0.0, %a\n"
197       "  %A = select i1 %1, float -0.0, float %a\n"
198       "  ret float %A\n"
199       "}\n");
200   // The sign of zero doesn't matter in fcmp.
201   expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, true});
202 }
203 
204 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero4) {
205   parseAssembly(
206       "define float @test(float %a) {\n"
207       "  %1 = fcmp ogt float %a, 0.0\n"
208       "  %A = select i1 %1, float -0.0, float %a\n"
209       "  ret float %A\n"
210       "}\n");
211   // The sign of zero doesn't matter in fcmp.
212   expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false});
213 }
214 
215 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero5) {
216   parseAssembly(
217       "define float @test(float %a) {\n"
218       "  %1 = fcmp ogt float -0.0, %a\n"
219       "  %A = select i1 %1, float %a, float 0.0\n"
220       "  ret float %A\n"
221       "}\n");
222   // The sign of zero doesn't matter in fcmp.
223   expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, false});
224 }
225 
226 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero6) {
227   parseAssembly(
228       "define float @test(float %a) {\n"
229       "  %1 = fcmp olt float %a, -0.0\n"
230       "  %A = select i1 %1, float %a, float 0.0\n"
231       "  ret float %A\n"
232       "}\n");
233   // The sign of zero doesn't matter in fcmp.
234   expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true});
235 }
236 
237 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero7) {
238   parseAssembly(
239       "define float @test(float %a) {\n"
240       "  %1 = fcmp ogt float 0.0, %a\n"
241       "  %A = select i1 %1, float %a, float -0.0\n"
242       "  ret float %A\n"
243       "}\n");
244   // The sign of zero doesn't matter in fcmp.
245   expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, false});
246 }
247 
248 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero8) {
249   parseAssembly(
250       "define float @test(float %a) {\n"
251       "  %1 = fcmp olt float %a, 0.0\n"
252       "  %A = select i1 %1, float %a, float -0.0\n"
253       "  ret float %A\n"
254       "}\n");
255   // The sign of zero doesn't matter in fcmp.
256   expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true});
257 }
258 
259 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero1) {
260   parseAssembly(
261       "define float @test(float %a) {\n"
262       "  %1 = fcmp ogt float -0.0, %a\n"
263       "  %A = select i1 %1, float 0.0, float %a\n"
264       "  ret float %A\n"
265       "}\n");
266   // The sign of zero doesn't matter in fcmp.
267   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, true});
268 }
269 
270 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero2) {
271   parseAssembly(
272       "define float @test(float %a) {\n"
273       "  %1 = fcmp olt float %a, -0.0\n"
274       "  %A = select i1 %1, float 0.0, float %a\n"
275       "  ret float %A\n"
276       "}\n");
277   // The sign of zero doesn't matter in fcmp.
278   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, false});
279 }
280 
281 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero3) {
282   parseAssembly(
283       "define float @test(float %a) {\n"
284       "  %1 = fcmp ogt float 0.0, %a\n"
285       "  %A = select i1 %1, float -0.0, float %a\n"
286       "  ret float %A\n"
287       "}\n");
288   // The sign of zero doesn't matter in fcmp.
289   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, true});
290 }
291 
292 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero4) {
293   parseAssembly(
294       "define float @test(float %a) {\n"
295       "  %1 = fcmp olt float %a, 0.0\n"
296       "  %A = select i1 %1, float -0.0, float %a\n"
297       "  ret float %A\n"
298       "}\n");
299   // The sign of zero doesn't matter in fcmp.
300   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, false});
301 }
302 
303 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero5) {
304   parseAssembly(
305       "define float @test(float %a) {\n"
306       "  %1 = fcmp olt float -0.0, %a\n"
307       "  %A = select i1 %1, float %a, float 0.0\n"
308       "  ret float %A\n"
309       "}\n");
310   // The sign of zero doesn't matter in fcmp.
311   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, false});
312 }
313 
314 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero6) {
315   parseAssembly(
316       "define float @test(float %a) {\n"
317       "  %1 = fcmp ogt float %a, -0.0\n"
318       "  %A = select i1 %1, float %a, float 0.0\n"
319       "  ret float %A\n"
320       "}\n");
321   // The sign of zero doesn't matter in fcmp.
322   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true});
323 }
324 
325 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero7) {
326   parseAssembly(
327       "define float @test(float %a) {\n"
328       "  %1 = fcmp olt float 0.0, %a\n"
329       "  %A = select i1 %1, float %a, float -0.0\n"
330       "  ret float %A\n"
331       "}\n");
332   // The sign of zero doesn't matter in fcmp.
333   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, false});
334 }
335 
336 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero8) {
337   parseAssembly(
338       "define float @test(float %a) {\n"
339       "  %1 = fcmp ogt float %a, 0.0\n"
340       "  %A = select i1 %1, float %a, float -0.0\n"
341       "  ret float %A\n"
342       "}\n");
343   // The sign of zero doesn't matter in fcmp.
344   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true});
345 }
346 
347 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZeroVecUndef) {
348   parseAssembly(
349       "define <2 x float> @test(<2 x float> %a) {\n"
350       "  %1 = fcmp ogt <2 x float> %a, <float -0.0, float -0.0>\n"
351       "  %A = select <2 x i1> %1, <2 x float> <float undef, float 0.0>, <2 x float> %a\n"
352       "  ret <2 x float> %A\n"
353       "}\n");
354   // An undef in a vector constant can not be back-propagated for this analysis.
355   expectPattern({SPF_UNKNOWN, SPNB_NA, false});
356 }
357 
358 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZeroVecUndef) {
359   parseAssembly(
360       "define <2 x float> @test(<2 x float> %a) {\n"
361       "  %1 = fcmp ogt <2 x float> %a, zeroinitializer\n"
362       "  %A = select <2 x i1> %1, <2 x float> %a, <2 x float> <float -0.0, float undef>\n"
363       "  ret <2 x float> %A\n"
364       "}\n");
365   // An undef in a vector constant can not be back-propagated for this analysis.
366   expectPattern({SPF_UNKNOWN, SPNB_NA, false});
367 }
368 
369 TEST_F(MatchSelectPatternTest, VectorFMinimum) {
370   parseAssembly(
371       "define <4 x float> @test(<4 x float> %a) {\n"
372       "  %1 = fcmp ule <4 x float> %a, \n"
373       "    <float 5.0, float 5.0, float 5.0, float 5.0>\n"
374       "  %A = select <4 x i1> %1, <4 x float> %a,\n"
375       "     <4 x float> <float 5.0, float 5.0, float 5.0, float 5.0>\n"
376       "  ret <4 x float> %A\n"
377       "}\n");
378   // Check that pattern matching works on vectors where each lane has the same
379   // unordered pattern.
380   expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false});
381 }
382 
383 TEST_F(MatchSelectPatternTest, VectorFMinOtherOrdered) {
384   parseAssembly(
385       "define <4 x float> @test(<4 x float> %a) {\n"
386       "  %1 = fcmp ole <4 x float> %a, \n"
387       "    <float 5.0, float 5.0, float 5.0, float 5.0>\n"
388       "  %A = select <4 x i1> %1, <4 x float> %a,\n"
389       "     <4 x float> <float 5.0, float 5.0, float 5.0, float 5.0>\n"
390       "  ret <4 x float> %A\n"
391       "}\n");
392   // Check that pattern matching works on vectors where each lane has the same
393   // ordered pattern.
394   expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true});
395 }
396 
397 TEST_F(MatchSelectPatternTest, VectorNotFMinimum) {
398   parseAssembly(
399       "define <4 x float> @test(<4 x float> %a) {\n"
400       "  %1 = fcmp ule <4 x float> %a, \n"
401       "    <float 5.0, float 0x7ff8000000000000, float 5.0, float 5.0>\n"
402       "  %A = select <4 x i1> %1, <4 x float> %a,\n"
403       "     <4 x float> <float 5.0, float 0x7ff8000000000000, float 5.0, float "
404       "5.0>\n"
405       "  ret <4 x float> %A\n"
406       "}\n");
407   // The lane that contains a NaN (0x7ff80...) behaves like a
408   // non-NaN-propagating min and the other lines behave like a NaN-propagating
409   // min, so check that neither is returned.
410   expectPattern({SPF_UNKNOWN, SPNB_NA, false});
411 }
412 
413 TEST_F(MatchSelectPatternTest, VectorNotFMinZero) {
414   parseAssembly(
415       "define <4 x float> @test(<4 x float> %a) {\n"
416       "  %1 = fcmp ule <4 x float> %a, \n"
417       "    <float 5.0, float -0.0, float 5.0, float 5.0>\n"
418       "  %A = select <4 x i1> %1, <4 x float> %a,\n"
419       "     <4 x float> <float 5.0, float 0.0, float 5.0, float 5.0>\n"
420       "  ret <4 x float> %A\n"
421       "}\n");
422   // Always selects the second lane of %a if it is positive or negative zero, so
423   // this is stricter than a min.
424   expectPattern({SPF_UNKNOWN, SPNB_NA, false});
425 }
426 
427 TEST_F(MatchSelectPatternTest, DoubleCastU) {
428   parseAssembly(
429       "define i32 @test(i8 %a, i8 %b) {\n"
430       "  %1 = icmp ult i8 %a, %b\n"
431       "  %2 = zext i8 %a to i32\n"
432       "  %3 = zext i8 %b to i32\n"
433       "  %A = select i1 %1, i32 %2, i32 %3\n"
434       "  ret i32 %A\n"
435       "}\n");
436   // We should be able to look through the situation where we cast both operands
437   // to the select.
438   expectPattern({SPF_UMIN, SPNB_NA, false});
439 }
440 
441 TEST_F(MatchSelectPatternTest, DoubleCastS) {
442   parseAssembly(
443       "define i32 @test(i8 %a, i8 %b) {\n"
444       "  %1 = icmp slt i8 %a, %b\n"
445       "  %2 = sext i8 %a to i32\n"
446       "  %3 = sext i8 %b to i32\n"
447       "  %A = select i1 %1, i32 %2, i32 %3\n"
448       "  ret i32 %A\n"
449       "}\n");
450   // We should be able to look through the situation where we cast both operands
451   // to the select.
452   expectPattern({SPF_SMIN, SPNB_NA, false});
453 }
454 
455 TEST_F(MatchSelectPatternTest, DoubleCastBad) {
456   parseAssembly(
457       "define i32 @test(i8 %a, i8 %b) {\n"
458       "  %1 = icmp ult i8 %a, %b\n"
459       "  %2 = zext i8 %a to i32\n"
460       "  %3 = sext i8 %b to i32\n"
461       "  %A = select i1 %1, i32 %2, i32 %3\n"
462       "  ret i32 %A\n"
463       "}\n");
464   // The cast types here aren't the same, so we cannot match an UMIN.
465   expectPattern({SPF_UNKNOWN, SPNB_NA, false});
466 }
467 
468 TEST_F(MatchSelectPatternTest, NotNotSMin) {
469   parseAssembly(
470       "define i8 @test(i8 %a, i8 %b) {\n"
471       "  %cmp = icmp sgt i8 %a, %b\n"
472       "  %an = xor i8 %a, -1\n"
473       "  %bn = xor i8 %b, -1\n"
474       "  %A = select i1 %cmp, i8 %an, i8 %bn\n"
475       "  ret i8 %A\n"
476       "}\n");
477   expectPattern({SPF_SMIN, SPNB_NA, false});
478 }
479 
480 TEST_F(MatchSelectPatternTest, NotNotSMinSwap) {
481   parseAssembly(
482       "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n"
483       "  %cmp = icmp slt <2 x i8> %a, %b\n"
484       "  %an = xor <2 x i8> %a, <i8 -1, i8-1>\n"
485       "  %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n"
486       "  %A = select <2 x i1> %cmp, <2 x i8> %bn, <2 x i8> %an\n"
487       "  ret <2 x i8> %A\n"
488       "}\n");
489   expectPattern({SPF_SMIN, SPNB_NA, false});
490 }
491 
492 TEST_F(MatchSelectPatternTest, NotNotSMax) {
493   parseAssembly(
494       "define i8 @test(i8 %a, i8 %b) {\n"
495       "  %cmp = icmp slt i8 %a, %b\n"
496       "  %an = xor i8 %a, -1\n"
497       "  %bn = xor i8 %b, -1\n"
498       "  %A = select i1 %cmp, i8 %an, i8 %bn\n"
499       "  ret i8 %A\n"
500       "}\n");
501   expectPattern({SPF_SMAX, SPNB_NA, false});
502 }
503 
504 TEST_F(MatchSelectPatternTest, NotNotSMaxSwap) {
505   parseAssembly(
506       "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n"
507       "  %cmp = icmp sgt <2 x i8> %a, %b\n"
508       "  %an = xor <2 x i8> %a, <i8 -1, i8-1>\n"
509       "  %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n"
510       "  %A = select <2 x i1> %cmp, <2 x i8> %bn, <2 x i8> %an\n"
511       "  ret <2 x i8> %A\n"
512       "}\n");
513   expectPattern({SPF_SMAX, SPNB_NA, false});
514 }
515 
516 TEST_F(MatchSelectPatternTest, NotNotUMin) {
517   parseAssembly(
518       "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n"
519       "  %cmp = icmp ugt <2 x i8> %a, %b\n"
520       "  %an = xor <2 x i8> %a, <i8 -1, i8-1>\n"
521       "  %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n"
522       "  %A = select <2 x i1> %cmp, <2 x i8> %an, <2 x i8> %bn\n"
523       "  ret <2 x i8> %A\n"
524       "}\n");
525   expectPattern({SPF_UMIN, SPNB_NA, false});
526 }
527 
528 TEST_F(MatchSelectPatternTest, NotNotUMinSwap) {
529   parseAssembly(
530       "define i8 @test(i8 %a, i8 %b) {\n"
531       "  %cmp = icmp ult i8 %a, %b\n"
532       "  %an = xor i8 %a, -1\n"
533       "  %bn = xor i8 %b, -1\n"
534       "  %A = select i1 %cmp, i8 %bn, i8 %an\n"
535       "  ret i8 %A\n"
536       "}\n");
537   expectPattern({SPF_UMIN, SPNB_NA, false});
538 }
539 
540 TEST_F(MatchSelectPatternTest, NotNotUMax) {
541   parseAssembly(
542       "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n"
543       "  %cmp = icmp ult <2 x i8> %a, %b\n"
544       "  %an = xor <2 x i8> %a, <i8 -1, i8-1>\n"
545       "  %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n"
546       "  %A = select <2 x i1> %cmp, <2 x i8> %an, <2 x i8> %bn\n"
547       "  ret <2 x i8> %A\n"
548       "}\n");
549   expectPattern({SPF_UMAX, SPNB_NA, false});
550 }
551 
552 TEST_F(MatchSelectPatternTest, NotNotUMaxSwap) {
553   parseAssembly(
554       "define i8 @test(i8 %a, i8 %b) {\n"
555       "  %cmp = icmp ugt i8 %a, %b\n"
556       "  %an = xor i8 %a, -1\n"
557       "  %bn = xor i8 %b, -1\n"
558       "  %A = select i1 %cmp, i8 %bn, i8 %an\n"
559       "  ret i8 %A\n"
560       "}\n");
561   expectPattern({SPF_UMAX, SPNB_NA, false});
562 }
563 
564 TEST_F(MatchSelectPatternTest, NotNotEq) {
565   parseAssembly(
566       "define i8 @test(i8 %a, i8 %b) {\n"
567       "  %cmp = icmp eq i8 %a, %b\n"
568       "  %an = xor i8 %a, -1\n"
569       "  %bn = xor i8 %b, -1\n"
570       "  %A = select i1 %cmp, i8 %bn, i8 %an\n"
571       "  ret i8 %A\n"
572       "}\n");
573   expectPattern({SPF_UNKNOWN, SPNB_NA, false});
574 }
575 
576 TEST_F(MatchSelectPatternTest, NotNotNe) {
577   parseAssembly(
578       "define i8 @test(i8 %a, i8 %b) {\n"
579       "  %cmp = icmp ne i8 %a, %b\n"
580       "  %an = xor i8 %a, -1\n"
581       "  %bn = xor i8 %b, -1\n"
582       "  %A = select i1 %cmp, i8 %bn, i8 %an\n"
583       "  ret i8 %A\n"
584       "}\n");
585   expectPattern({SPF_UNKNOWN, SPNB_NA, false});
586 }
587 
588 TEST(ValueTracking, GuaranteedToTransferExecutionToSuccessor) {
589   StringRef Assembly =
590       "declare void @nounwind_readonly(i32*) nounwind readonly "
591       "declare void @nounwind_argmemonly(i32*) nounwind argmemonly "
592       "declare void @throws_but_readonly(i32*) readonly "
593       "declare void @throws_but_argmemonly(i32*) argmemonly "
594       "declare void @nounwind_willreturn(i32*) nounwind willreturn"
595       " "
596       "declare void @unknown(i32*) "
597       " "
598       "define void @f(i32* %p) { "
599       "  call void @nounwind_readonly(i32* %p) "
600       "  call void @nounwind_argmemonly(i32* %p) "
601       "  call void @throws_but_readonly(i32* %p) "
602       "  call void @throws_but_argmemonly(i32* %p) "
603       "  call void @unknown(i32* %p) nounwind readonly "
604       "  call void @unknown(i32* %p) nounwind argmemonly "
605       "  call void @unknown(i32* %p) readonly "
606       "  call void @unknown(i32* %p) argmemonly "
607       "  call void @nounwind_willreturn(i32* %p)"
608       "  ret void "
609       "} ";
610 
611   LLVMContext Context;
612   SMDiagnostic Error;
613   auto M = parseAssemblyString(Assembly, Error, Context);
614   assert(M && "Bad assembly?");
615 
616   auto *F = M->getFunction("f");
617   assert(F && "Bad assembly?");
618 
619   auto &BB = F->getEntryBlock();
620   bool ExpectedAnswers[] = {
621       true,  // call void @nounwind_readonly(i32* %p)
622       true,  // call void @nounwind_argmemonly(i32* %p)
623       false, // call void @throws_but_readonly(i32* %p)
624       false, // call void @throws_but_argmemonly(i32* %p)
625       true,  // call void @unknown(i32* %p) nounwind readonly
626       true,  // call void @unknown(i32* %p) nounwind argmemonly
627       false, // call void @unknown(i32* %p) readonly
628       false, // call void @unknown(i32* %p) argmemonly
629       true,  // call void @nounwind_willreturn(i32* %p)
630       false, // ret void
631   };
632 
633   int Index = 0;
634   for (auto &I : BB) {
635     EXPECT_EQ(isGuaranteedToTransferExecutionToSuccessor(&I),
636               ExpectedAnswers[Index])
637         << "Incorrect answer at instruction " << Index << " = " << I;
638     Index++;
639   }
640 }
641 
642 TEST_F(ValueTrackingTest, ComputeNumSignBits_PR32045) {
643   parseAssembly(
644       "define i32 @test(i32 %a) {\n"
645       "  %A = ashr i32 %a, -1\n"
646       "  ret i32 %A\n"
647       "}\n");
648   EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u);
649 }
650 
651 // No guarantees for canonical IR in this analysis, so this just bails out.
652 TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle) {
653   parseAssembly(
654       "define <2 x i32> @test() {\n"
655       "  %A = shufflevector <2 x i32> undef, <2 x i32> undef, <2 x i32> <i32 0, i32 0>\n"
656       "  ret <2 x i32> %A\n"
657       "}\n");
658   EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u);
659 }
660 
661 // No guarantees for canonical IR in this analysis, so a shuffle element that
662 // references an undef value means this can't return any extra information.
663 TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle2) {
664   parseAssembly(
665       "define <2 x i32> @test(<2 x i1> %x) {\n"
666       "  %sext = sext <2 x i1> %x to <2 x i32>\n"
667       "  %A = shufflevector <2 x i32> %sext, <2 x i32> undef, <2 x i32> <i32 0, i32 2>\n"
668       "  ret <2 x i32> %A\n"
669       "}\n");
670   EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u);
671 }
672 
673 TEST(ValueTracking, propagatesPoison) {
674   std::string AsmHead = "declare i32 @g(i32)\n"
675                         "define void @f(i32 %x, i32 %y, float %fx, float %fy, "
676                         "i1 %cond, i8* %p) {\n";
677   std::string AsmTail = "  ret void\n}";
678   // (propagates poison?, IR instruction)
679   SmallVector<std::pair<bool, std::string>, 32> Data = {
680       {true, "add i32 %x, %y"},
681       {true, "add nsw nuw i32 %x, %y"},
682       {true, "ashr i32 %x, %y"},
683       {true, "lshr exact i32 %x, 31"},
684       {true, "fcmp oeq float %fx, %fy"},
685       {true, "icmp eq i32 %x, %y"},
686       {true, "getelementptr i8, i8* %p, i32 %x"},
687       {true, "getelementptr inbounds i8, i8* %p, i32 %x"},
688       {true, "bitcast float %fx to i32"},
689       {false, "select i1 %cond, i32 %x, i32 %y"},
690       {false, "freeze i32 %x"},
691       {true, "udiv i32 %x, %y"},
692       {true, "urem i32 %x, %y"},
693       {true, "sdiv exact i32 %x, %y"},
694       {true, "srem i32 %x, %y"},
695       {false, "call i32 @g(i32 %x)"}};
696 
697   std::string AssemblyStr = AsmHead;
698   for (auto &Itm : Data)
699     AssemblyStr += Itm.second + "\n";
700   AssemblyStr += AsmTail;
701 
702   LLVMContext Context;
703   SMDiagnostic Error;
704   auto M = parseAssemblyString(AssemblyStr, Error, Context);
705   assert(M && "Bad assembly?");
706 
707   auto *F = M->getFunction("f");
708   assert(F && "Bad assembly?");
709 
710   auto &BB = F->getEntryBlock();
711 
712   int Index = 0;
713   for (auto &I : BB) {
714     if (isa<ReturnInst>(&I))
715       break;
716     EXPECT_EQ(propagatesPoison(&I), Data[Index].first)
717         << "Incorrect answer at instruction " << Index << " = " << I;
718     Index++;
719   }
720 }
721 
722 TEST(ValueTracking, canCreatePoison) {
723   std::string AsmHead =
724       "declare i32 @g(i32)\n"
725       "define void @f(i32 %x, i32 %y, float %fx, float %fy, i1 %cond, "
726       "<4 x i32> %vx, <4 x i32> %vx2, <vscale x 4 x i32> %svx, i8* %p) {\n";
727   std::string AsmTail = "  ret void\n}";
728   // (can create poison?, IR instruction)
729   SmallVector<std::pair<bool, std::string>, 32> Data = {
730       {false, "add i32 %x, %y"},
731       {true, "add nsw nuw i32 %x, %y"},
732       {true, "shl i32 %x, %y"},
733       {true, "shl <4 x i32> %vx, %vx2"},
734       {true, "shl nsw i32 %x, %y"},
735       {true, "shl nsw <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"},
736       {false, "shl i32 %x, 31"},
737       {true, "shl i32 %x, 32"},
738       {false, "shl <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"},
739       {true, "shl <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 32>"},
740       {true, "ashr i32 %x, %y"},
741       {true, "ashr exact i32 %x, %y"},
742       {false, "ashr i32 %x, 31"},
743       {true, "ashr exact i32 %x, 31"},
744       {false, "ashr <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"},
745       {true, "ashr <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 32>"},
746       {true, "ashr exact <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"},
747       {true, "lshr i32 %x, %y"},
748       {true, "lshr exact i32 %x, 31"},
749       {false, "udiv i32 %x, %y"},
750       {true, "udiv exact i32 %x, %y"},
751       {false, "getelementptr i8, i8* %p, i32 %x"},
752       {true, "getelementptr inbounds i8, i8* %p, i32 %x"},
753       {true, "fneg nnan float %fx"},
754       {false, "fneg float %fx"},
755       {false, "fadd float %fx, %fy"},
756       {true, "fadd nnan float %fx, %fy"},
757       {false, "urem i32 %x, %y"},
758       {true, "fptoui float %fx to i32"},
759       {true, "fptosi float %fx to i32"},
760       {false, "bitcast float %fx to i32"},
761       {false, "select i1 %cond, i32 %x, i32 %y"},
762       {true, "select nnan i1 %cond, float %fx, float %fy"},
763       {true, "extractelement <4 x i32> %vx, i32 %x"},
764       {false, "extractelement <4 x i32> %vx, i32 3"},
765       {true, "extractelement <vscale x 4 x i32> %svx, i32 4"},
766       {true, "insertelement <4 x i32> %vx, i32 %x, i32 %y"},
767       {false, "insertelement <4 x i32> %vx, i32 %x, i32 3"},
768       {true, "insertelement <vscale x 4 x i32> %svx, i32 %x, i32 4"},
769       {false, "freeze i32 %x"},
770       {true, "call i32 @g(i32 %x)"},
771       {true, "fcmp nnan oeq float %fx, %fy"},
772       {false, "fcmp oeq float %fx, %fy"}};
773 
774   std::string AssemblyStr = AsmHead;
775   for (auto &Itm : Data)
776     AssemblyStr += Itm.second + "\n";
777   AssemblyStr += AsmTail;
778 
779   LLVMContext Context;
780   SMDiagnostic Error;
781   auto M = parseAssemblyString(AssemblyStr, Error, Context);
782   assert(M && "Bad assembly?");
783 
784   auto *F = M->getFunction("f");
785   assert(F && "Bad assembly?");
786 
787   auto &BB = F->getEntryBlock();
788 
789   int Index = 0;
790   for (auto &I : BB) {
791     if (isa<ReturnInst>(&I))
792       break;
793     EXPECT_EQ(canCreatePoison(&I), Data[Index].first)
794         << "Incorrect answer at instruction " << Index << " = " << I;
795     Index++;
796   }
797 }
798 
799 TEST_F(ComputeKnownBitsTest, ComputeKnownBits) {
800   parseAssembly(
801       "define i32 @test(i32 %a, i32 %b) {\n"
802       "  %ash = mul i32 %a, 8\n"
803       "  %aad = add i32 %ash, 7\n"
804       "  %aan = and i32 %aad, 4095\n"
805       "  %bsh = shl i32 %b, 4\n"
806       "  %bad = or i32 %bsh, 6\n"
807       "  %ban = and i32 %bad, 4095\n"
808       "  %A = mul i32 %aan, %ban\n"
809       "  ret i32 %A\n"
810       "}\n");
811   expectKnownBits(/*zero*/ 4278190085u, /*one*/ 10u);
812 }
813 
814 TEST_F(ComputeKnownBitsTest, ComputeKnownMulBits) {
815   parseAssembly(
816       "define i32 @test(i32 %a, i32 %b) {\n"
817       "  %aa = shl i32 %a, 5\n"
818       "  %bb = shl i32 %b, 5\n"
819       "  %aaa = or i32 %aa, 24\n"
820       "  %bbb = or i32 %bb, 28\n"
821       "  %A = mul i32 %aaa, %bbb\n"
822       "  ret i32 %A\n"
823       "}\n");
824   expectKnownBits(/*zero*/ 95u, /*one*/ 32u);
825 }
826 
827 TEST_F(ComputeKnownBitsTest, KnownNonZeroShift) {
828   // %q is known nonzero without known bits.
829   // Because %q is nonzero, %A[0] is known to be zero.
830   parseAssembly(
831       "define i8 @test(i8 %p, i8* %pq) {\n"
832       "  %q = load i8, i8* %pq, !range !0\n"
833       "  %A = shl i8 %p, %q\n"
834       "  ret i8 %A\n"
835       "}\n"
836       "!0 = !{ i8 1, i8 5 }\n");
837   expectKnownBits(/*zero*/ 1u, /*one*/ 0u);
838 }
839 
840 TEST_F(ComputeKnownBitsTest, ComputeKnownFshl) {
841   // fshl(....1111....0000, 00..1111........, 6)
842   // = 11....000000..11
843   parseAssembly(
844       "define i16 @test(i16 %a, i16 %b) {\n"
845       "  %aa = shl i16 %a, 4\n"
846       "  %bb = lshr i16 %b, 2\n"
847       "  %aaa = or i16 %aa, 3840\n"
848       "  %bbb = or i16 %bb, 3840\n"
849       "  %A = call i16 @llvm.fshl.i16(i16 %aaa, i16 %bbb, i16 6)\n"
850       "  ret i16 %A\n"
851       "}\n"
852       "declare i16 @llvm.fshl.i16(i16, i16, i16)\n");
853   expectKnownBits(/*zero*/ 1008u, /*one*/ 49155u);
854 }
855 
856 TEST_F(ComputeKnownBitsTest, ComputeKnownFshr) {
857   // fshr(....1111....0000, 00..1111........, 26)
858   // = 11....000000..11
859   parseAssembly(
860       "define i16 @test(i16 %a, i16 %b) {\n"
861       "  %aa = shl i16 %a, 4\n"
862       "  %bb = lshr i16 %b, 2\n"
863       "  %aaa = or i16 %aa, 3840\n"
864       "  %bbb = or i16 %bb, 3840\n"
865       "  %A = call i16 @llvm.fshr.i16(i16 %aaa, i16 %bbb, i16 26)\n"
866       "  ret i16 %A\n"
867       "}\n"
868       "declare i16 @llvm.fshr.i16(i16, i16, i16)\n");
869   expectKnownBits(/*zero*/ 1008u, /*one*/ 49155u);
870 }
871 
872 TEST_F(ComputeKnownBitsTest, ComputeKnownFshlZero) {
873   // fshl(....1111....0000, 00..1111........, 0)
874   // = ....1111....0000
875   parseAssembly(
876       "define i16 @test(i16 %a, i16 %b) {\n"
877       "  %aa = shl i16 %a, 4\n"
878       "  %bb = lshr i16 %b, 2\n"
879       "  %aaa = or i16 %aa, 3840\n"
880       "  %bbb = or i16 %bb, 3840\n"
881       "  %A = call i16 @llvm.fshl.i16(i16 %aaa, i16 %bbb, i16 0)\n"
882       "  ret i16 %A\n"
883       "}\n"
884       "declare i16 @llvm.fshl.i16(i16, i16, i16)\n");
885   expectKnownBits(/*zero*/ 15u, /*one*/ 3840u);
886 }
887 
888 TEST_F(ComputeKnownBitsTest, ComputeKnownUAddSatLeadingOnes) {
889   // uadd.sat(1111...1, ........)
890   // = 1111....
891   parseAssembly(
892       "define i8 @test(i8 %a, i8 %b) {\n"
893       "  %aa = or i8 %a, 241\n"
894       "  %A = call i8 @llvm.uadd.sat.i8(i8 %aa, i8 %b)\n"
895       "  ret i8 %A\n"
896       "}\n"
897       "declare i8 @llvm.uadd.sat.i8(i8, i8)\n");
898   expectKnownBits(/*zero*/ 0u, /*one*/ 240u);
899 }
900 
901 TEST_F(ComputeKnownBitsTest, ComputeKnownUAddSatOnesPreserved) {
902   // uadd.sat(00...011, .1...110)
903   // = .......1
904   parseAssembly(
905       "define i8 @test(i8 %a, i8 %b) {\n"
906       "  %aa = or i8 %a, 3\n"
907       "  %aaa = and i8 %aa, 59\n"
908       "  %bb = or i8 %b, 70\n"
909       "  %bbb = and i8 %bb, 254\n"
910       "  %A = call i8 @llvm.uadd.sat.i8(i8 %aaa, i8 %bbb)\n"
911       "  ret i8 %A\n"
912       "}\n"
913       "declare i8 @llvm.uadd.sat.i8(i8, i8)\n");
914   expectKnownBits(/*zero*/ 0u, /*one*/ 1u);
915 }
916 
917 TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatLHSLeadingZeros) {
918   // usub.sat(0000...0, ........)
919   // = 0000....
920   parseAssembly(
921       "define i8 @test(i8 %a, i8 %b) {\n"
922       "  %aa = and i8 %a, 14\n"
923       "  %A = call i8 @llvm.usub.sat.i8(i8 %aa, i8 %b)\n"
924       "  ret i8 %A\n"
925       "}\n"
926       "declare i8 @llvm.usub.sat.i8(i8, i8)\n");
927   expectKnownBits(/*zero*/ 240u, /*one*/ 0u);
928 }
929 
930 TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatRHSLeadingOnes) {
931   // usub.sat(........, 1111...1)
932   // = 0000....
933   parseAssembly(
934       "define i8 @test(i8 %a, i8 %b) {\n"
935       "  %bb = or i8 %a, 241\n"
936       "  %A = call i8 @llvm.usub.sat.i8(i8 %a, i8 %bb)\n"
937       "  ret i8 %A\n"
938       "}\n"
939       "declare i8 @llvm.usub.sat.i8(i8, i8)\n");
940   expectKnownBits(/*zero*/ 240u, /*one*/ 0u);
941 }
942 
943 TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatZerosPreserved) {
944   // usub.sat(11...011, .1...110)
945   // = ......0.
946   parseAssembly(
947       "define i8 @test(i8 %a, i8 %b) {\n"
948       "  %aa = or i8 %a, 195\n"
949       "  %aaa = and i8 %aa, 251\n"
950       "  %bb = or i8 %b, 70\n"
951       "  %bbb = and i8 %bb, 254\n"
952       "  %A = call i8 @llvm.usub.sat.i8(i8 %aaa, i8 %bbb)\n"
953       "  ret i8 %A\n"
954       "}\n"
955       "declare i8 @llvm.usub.sat.i8(i8, i8)\n");
956   expectKnownBits(/*zero*/ 2u, /*one*/ 0u);
957 }
958 
959 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsPtrToIntTrunc) {
960   // ptrtoint truncates the pointer type.
961   parseAssembly(
962       "define void @test(i8** %p) {\n"
963       "  %A = load i8*, i8** %p\n"
964       "  %i = ptrtoint i8* %A to i32\n"
965       "  %m = and i32 %i, 31\n"
966       "  %c = icmp eq i32 %m, 0\n"
967       "  call void @llvm.assume(i1 %c)\n"
968       "  ret void\n"
969       "}\n"
970       "declare void @llvm.assume(i1)\n");
971   AssumptionCache AC(*F);
972   KnownBits Known = computeKnownBits(
973       A, M->getDataLayout(), /* Depth */ 0, &AC, F->front().getTerminator());
974   EXPECT_EQ(Known.Zero.getZExtValue(), 31u);
975   EXPECT_EQ(Known.One.getZExtValue(), 0u);
976 }
977 
978 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsPtrToIntZext) {
979   // ptrtoint zero extends the pointer type.
980   parseAssembly(
981       "define void @test(i8** %p) {\n"
982       "  %A = load i8*, i8** %p\n"
983       "  %i = ptrtoint i8* %A to i128\n"
984       "  %m = and i128 %i, 31\n"
985       "  %c = icmp eq i128 %m, 0\n"
986       "  call void @llvm.assume(i1 %c)\n"
987       "  ret void\n"
988       "}\n"
989       "declare void @llvm.assume(i1)\n");
990   AssumptionCache AC(*F);
991   KnownBits Known = computeKnownBits(
992       A, M->getDataLayout(), /* Depth */ 0, &AC, F->front().getTerminator());
993   EXPECT_EQ(Known.Zero.getZExtValue(), 31u);
994   EXPECT_EQ(Known.One.getZExtValue(), 0u);
995 }
996 
997 class IsBytewiseValueTest : public ValueTrackingTest,
998                             public ::testing::WithParamInterface<
999                                 std::pair<const char *, const char *>> {
1000 protected:
1001 };
1002 
1003 const std::pair<const char *, const char *> IsBytewiseValueTests[] = {
1004     {
1005         "i8 0",
1006         "i48* null",
1007     },
1008     {
1009         "i8 undef",
1010         "i48* undef",
1011     },
1012     {
1013         "i8 0",
1014         "i8 zeroinitializer",
1015     },
1016     {
1017         "i8 0",
1018         "i8 0",
1019     },
1020     {
1021         "i8 -86",
1022         "i8 -86",
1023     },
1024     {
1025         "i8 -1",
1026         "i8 -1",
1027     },
1028     {
1029         "i8 undef",
1030         "i16 undef",
1031     },
1032     {
1033         "i8 0",
1034         "i16 0",
1035     },
1036     {
1037         "",
1038         "i16 7",
1039     },
1040     {
1041         "i8 -86",
1042         "i16 -21846",
1043     },
1044     {
1045         "i8 -1",
1046         "i16 -1",
1047     },
1048     {
1049         "i8 0",
1050         "i48 0",
1051     },
1052     {
1053         "i8 -1",
1054         "i48 -1",
1055     },
1056     {
1057         "i8 0",
1058         "i49 0",
1059     },
1060     {
1061         "",
1062         "i49 -1",
1063     },
1064     {
1065         "i8 0",
1066         "half 0xH0000",
1067     },
1068     {
1069         "i8 -85",
1070         "half 0xHABAB",
1071     },
1072     {
1073         "i8 0",
1074         "float 0.0",
1075     },
1076     {
1077         "i8 -1",
1078         "float 0xFFFFFFFFE0000000",
1079     },
1080     {
1081         "i8 0",
1082         "double 0.0",
1083     },
1084     {
1085         "i8 -15",
1086         "double 0xF1F1F1F1F1F1F1F1",
1087     },
1088     {
1089         "i8 undef",
1090         "i16* undef",
1091     },
1092     {
1093         "i8 0",
1094         "i16* inttoptr (i64 0 to i16*)",
1095     },
1096     {
1097         "i8 -1",
1098         "i16* inttoptr (i64 -1 to i16*)",
1099     },
1100     {
1101         "i8 -86",
1102         "i16* inttoptr (i64 -6148914691236517206 to i16*)",
1103     },
1104     {
1105         "",
1106         "i16* inttoptr (i48 -1 to i16*)",
1107     },
1108     {
1109         "i8 -1",
1110         "i16* inttoptr (i96 -1 to i16*)",
1111     },
1112     {
1113         "i8 undef",
1114         "[0 x i8] zeroinitializer",
1115     },
1116     {
1117         "i8 undef",
1118         "[0 x i8] undef",
1119     },
1120     {
1121         "i8 undef",
1122         "[5 x [0 x i8]] zeroinitializer",
1123     },
1124     {
1125         "i8 undef",
1126         "[5 x [0 x i8]] undef",
1127     },
1128     {
1129         "i8 0",
1130         "[6 x i8] zeroinitializer",
1131     },
1132     {
1133         "i8 undef",
1134         "[6 x i8] undef",
1135     },
1136     {
1137         "i8 1",
1138         "[5 x i8] [i8 1, i8 1, i8 1, i8 1, i8 1]",
1139     },
1140     {
1141         "",
1142         "[5 x i64] [i64 1, i64 1, i64 1, i64 1, i64 1]",
1143     },
1144     {
1145         "i8 -1",
1146         "[5 x i64] [i64 -1, i64 -1, i64 -1, i64 -1, i64 -1]",
1147     },
1148     {
1149         "",
1150         "[4 x i8] [i8 1, i8 2, i8 1, i8 1]",
1151     },
1152     {
1153         "i8 1",
1154         "[4 x i8] [i8 1, i8 undef, i8 1, i8 1]",
1155     },
1156     {
1157         "i8 0",
1158         "<6 x i8> zeroinitializer",
1159     },
1160     {
1161         "i8 undef",
1162         "<6 x i8> undef",
1163     },
1164     {
1165         "i8 1",
1166         "<5 x i8> <i8 1, i8 1, i8 1, i8 1, i8 1>",
1167     },
1168     {
1169         "",
1170         "<5 x i64> <i64 1, i64 1, i64 1, i64 1, i64 1>",
1171     },
1172     {
1173         "i8 -1",
1174         "<5 x i64> <i64 -1, i64 -1, i64 -1, i64 -1, i64 -1>",
1175     },
1176     {
1177         "",
1178         "<4 x i8> <i8 1, i8 1, i8 2, i8 1>",
1179     },
1180     {
1181         "i8 5",
1182         "<2 x i8> < i8 5, i8 undef >",
1183     },
1184     {
1185         "i8 0",
1186         "[2 x [2 x i16]] zeroinitializer",
1187     },
1188     {
1189         "i8 undef",
1190         "[2 x [2 x i16]] undef",
1191     },
1192     {
1193         "i8 -86",
1194         "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], "
1195         "[2 x i16] [i16 -21846, i16 -21846]]",
1196     },
1197     {
1198         "",
1199         "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], "
1200         "[2 x i16] [i16 -21836, i16 -21846]]",
1201     },
1202     {
1203         "i8 undef",
1204         "{ } zeroinitializer",
1205     },
1206     {
1207         "i8 undef",
1208         "{ } undef",
1209     },
1210     {
1211         "i8 undef",
1212         "{ {}, {} } zeroinitializer",
1213     },
1214     {
1215         "i8 undef",
1216         "{ {}, {} } undef",
1217     },
1218     {
1219         "i8 0",
1220         "{i8, i64, i16*} zeroinitializer",
1221     },
1222     {
1223         "i8 undef",
1224         "{i8, i64, i16*} undef",
1225     },
1226     {
1227         "i8 -86",
1228         "{i8, i64, i16*} {i8 -86, i64 -6148914691236517206, i16* undef}",
1229     },
1230     {
1231         "",
1232         "{i8, i64, i16*} {i8 86, i64 -6148914691236517206, i16* undef}",
1233     },
1234 };
1235 
1236 INSTANTIATE_TEST_CASE_P(IsBytewiseValueParamTests, IsBytewiseValueTest,
1237                         ::testing::ValuesIn(IsBytewiseValueTests),);
1238 
1239 TEST_P(IsBytewiseValueTest, IsBytewiseValue) {
1240   auto M = parseModule(std::string("@test = global ") + GetParam().second);
1241   GlobalVariable *GV = dyn_cast<GlobalVariable>(M->getNamedValue("test"));
1242   Value *Actual = isBytewiseValue(GV->getInitializer(), M->getDataLayout());
1243   std::string Buff;
1244   raw_string_ostream S(Buff);
1245   if (Actual)
1246     S << *Actual;
1247   EXPECT_EQ(GetParam().first, S.str());
1248 }
1249