Debian Codegraph @

llvm/unittests/Analysis/ValueTrackingTest.cpp

raw
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/ADT/FloatingPointMode.h"
11 #include "llvm/Analysis/AssumptionCache.h"
12 #include "llvm/Analysis/FloatingPointPredicateUtils.h"
13 #include "llvm/AsmParser/Parser.h"
14 #include "llvm/IR/ConstantRange.h"
15 #include "llvm/IR/Dominators.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/IRBuilder.h"
18 #include "llvm/IR/InstIterator.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/IR/IntrinsicInst.h"
21 #include "llvm/IR/LLVMContext.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/KnownBits.h"
25 #include "llvm/Support/KnownFPClass.h"
26 #include "llvm/Support/SourceMgr.h"
27 #include "llvm/Transforms/Utils/Local.h"
28 #include "gtest/gtest.h"
29
30 using namespace llvm;
31
32 namespace {
33
34 static Instruction *findInstructionByNameOrNull(Function *F, StringRef Name) {
35 for (Instruction &I : instructions(F))
36 if (I.getName() == Name)
37 return &I;
38
39 return nullptr;
40 }
41
42 static Instruction &findInstructionByName(Function *F, StringRef Name) {
43 auto *I = findInstructionByNameOrNull(F, Name);
44 if (I)
45 return *I;
46
47 llvm_unreachable("Expected value not found");
48 }
49
50 class ValueTrackingTest : public testing::Test {
51 protected:
52 std::unique_ptr<Module> parseModule(StringRef Assembly) {
53 SMDiagnostic Error;
54 std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context);
55
56 std::string errMsg;
57 raw_string_ostream os(errMsg);
58 Error.print("", os);
59 EXPECT_TRUE(M) << errMsg;
60
61 return M;
62 }
63
64 void parseAssembly(StringRef Assembly) {
65 M = parseModule(Assembly);
66 ASSERT_TRUE(M);
67
68 F = M->getFunction("test");
69 ASSERT_TRUE(F) << "Test must have a function @test";
70 if (!F)
71 return;
72
73 A = findInstructionByNameOrNull(F, "A");
74 ASSERT_TRUE(A) << "@test must have an instruction %A";
75 A2 = findInstructionByNameOrNull(F, "A2");
76 A3 = findInstructionByNameOrNull(F, "A3");
77 A4 = findInstructionByNameOrNull(F, "A4");
78 A5 = findInstructionByNameOrNull(F, "A5");
79 A6 = findInstructionByNameOrNull(F, "A6");
80 A7 = findInstructionByNameOrNull(F, "A7");
81
82 CxtI = findInstructionByNameOrNull(F, "CxtI");
83 CxtI2 = findInstructionByNameOrNull(F, "CxtI2");
84 CxtI3 = findInstructionByNameOrNull(F, "CxtI3");
85 }
86
87 LLVMContext Context;
88 std::unique_ptr<Module> M;
89 Function *F = nullptr;
90 Instruction *A = nullptr;
91 // Instructions (optional)
92 Instruction *A2 = nullptr, *A3 = nullptr, *A4 = nullptr, *A5 = nullptr,
93 *A6 = nullptr, *A7 = nullptr;
94
95 // Context instructions (optional)
96 Instruction *CxtI = nullptr, *CxtI2 = nullptr, *CxtI3 = nullptr;
97 };
98
99 class MatchSelectPatternTest : public ValueTrackingTest {
100 protected:
101 void expectPattern(const SelectPatternResult &P) {
102 Value *LHS, *RHS;
103 Instruction::CastOps CastOp;
104 SelectPatternResult R = matchSelectPattern(A, LHS, RHS, &CastOp);
105 EXPECT_EQ(P.Flavor, R.Flavor);
106 EXPECT_EQ(P.NaNBehavior, R.NaNBehavior);
107 EXPECT_EQ(P.Ordered, R.Ordered);
108 }
109 };
110
111 class ComputeKnownBitsTest : public ValueTrackingTest {
112 protected:
113 void expectKnownBits(uint64_t Zero, uint64_t One) {
114 auto Known = computeKnownBits(A, M->getDataLayout());
115 ASSERT_FALSE(Known.hasConflict());
116 EXPECT_EQ(Known.One.getZExtValue(), One);
117 EXPECT_EQ(Known.Zero.getZExtValue(), Zero);
118 }
119 };
120
121 class ComputeKnownFPClassTest : public ValueTrackingTest {
122 protected:
123 void expectKnownFPClass(unsigned KnownTrue, std::optional<bool> SignBitKnown,
124 Instruction *TestVal = nullptr) {
125 if (!TestVal)
126 TestVal = A;
127
128 KnownFPClass Known = computeKnownFPClass(TestVal, M->getDataLayout());
129 EXPECT_EQ(KnownTrue, Known.KnownFPClasses);
130 EXPECT_EQ(SignBitKnown, Known.SignBit);
131 }
132 };
133 }
134
135 TEST_F(MatchSelectPatternTest, SimpleFMin) {
136 parseAssembly(
137 "define float @test(float %a) {\n"
138 " %1 = fcmp ult float %a, 5.0\n"
139 " %A = select i1 %1, float %a, float 5.0\n"
140 " ret float %A\n"
141 "}\n");
142 expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false});
143 }
144
145 TEST_F(MatchSelectPatternTest, SimpleFMax) {
146 parseAssembly(
147 "define float @test(float %a) {\n"
148 " %1 = fcmp ogt float %a, 5.0\n"
149 " %A = select i1 %1, float %a, float 5.0\n"
150 " ret float %A\n"
151 "}\n");
152 expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true});
153 }
154
155 TEST_F(MatchSelectPatternTest, SwappedFMax) {
156 parseAssembly(
157 "define float @test(float %a) {\n"
158 " %1 = fcmp olt float 5.0, %a\n"
159 " %A = select i1 %1, float %a, float 5.0\n"
160 " ret float %A\n"
161 "}\n");
162 expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, false});
163 }
164
165 TEST_F(MatchSelectPatternTest, SwappedFMax2) {
166 parseAssembly(
167 "define float @test(float %a) {\n"
168 " %1 = fcmp olt float %a, 5.0\n"
169 " %A = select i1 %1, float 5.0, float %a\n"
170 " ret float %A\n"
171 "}\n");
172 expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, false});
173 }
174
175 TEST_F(MatchSelectPatternTest, SwappedFMax3) {
176 parseAssembly(
177 "define float @test(float %a) {\n"
178 " %1 = fcmp ult float %a, 5.0\n"
179 " %A = select i1 %1, float 5.0, float %a\n"
180 " ret float %A\n"
181 "}\n");
182 expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true});
183 }
184
185 TEST_F(MatchSelectPatternTest, FastFMin) {
186 parseAssembly(
187 "define float @test(float %a) {\n"
188 " %1 = fcmp nnan olt float %a, 5.0\n"
189 " %A = select i1 %1, float %a, float 5.0\n"
190 " ret float %A\n"
191 "}\n");
192 expectPattern({SPF_FMINNUM, SPNB_RETURNS_ANY, true});
193 }
194
195 TEST_F(MatchSelectPatternTest, FastFMinUnordered) {
196 parseAssembly("define float @test(float %a) {\n"
197 " %1 = fcmp nnan ult float %a, 5.0\n"
198 " %A = select i1 %1, float %a, float 5.0\n"
199 " ret float %A\n"
200 "}\n");
201 expectPattern({SPF_FMINNUM, SPNB_RETURNS_ANY, false});
202 }
203
204 TEST_F(MatchSelectPatternTest, FMinConstantZero) {
205 parseAssembly(
206 "define float @test(float %a) {\n"
207 " %1 = fcmp ole float %a, 0.0\n"
208 " %A = select i1 %1, float %a, float 0.0\n"
209 " ret float %A\n"
210 "}\n");
211 // This shouldn't be matched, as %a could be -0.0.
212 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
213 }
214
215 TEST_F(MatchSelectPatternTest, FMinConstantZeroNsz) {
216 parseAssembly("define float @test(float %a) {\n"
217 " %1 = fcmp nsz ole float %a, 0.0\n"
218 " %A = select nsz i1 %1, float %a, float 0.0\n"
219 " ret float %A\n"
220 "}\n");
221 // But this should be, because we've ignored signed zeroes.
222 expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true});
223 }
224
225 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero1) {
226 parseAssembly(
227 "define float @test(float %a) {\n"
228 " %1 = fcmp olt float -0.0, %a\n"
229 " %A = select i1 %1, float 0.0, float %a\n"
230 " ret float %A\n"
231 "}\n");
232 // The sign of zero doesn't matter in fcmp.
233 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
234 }
235
236 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero2) {
237 parseAssembly(
238 "define float @test(float %a) {\n"
239 " %1 = fcmp ogt float %a, -0.0\n"
240 " %A = select i1 %1, float 0.0, float %a\n"
241 " ret float %A\n"
242 "}\n");
243 // The sign of zero doesn't matter in fcmp.
244 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
245 }
246
247 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero3) {
248 parseAssembly(
249 "define float @test(float %a) {\n"
250 " %1 = fcmp olt float 0.0, %a\n"
251 " %A = select i1 %1, float -0.0, float %a\n"
252 " ret float %A\n"
253 "}\n");
254 // The sign of zero doesn't matter in fcmp.
255 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
256 }
257
258 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero4) {
259 parseAssembly(
260 "define float @test(float %a) {\n"
261 " %1 = fcmp ogt float %a, 0.0\n"
262 " %A = select i1 %1, float -0.0, float %a\n"
263 " ret float %A\n"
264 "}\n");
265 // The sign of zero doesn't matter in fcmp.
266 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
267 }
268
269 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero5) {
270 parseAssembly(
271 "define float @test(float %a) {\n"
272 " %1 = fcmp ogt float -0.0, %a\n"
273 " %A = select i1 %1, float %a, float 0.0\n"
274 " ret float %A\n"
275 "}\n");
276 // The sign of zero doesn't matter in fcmp.
277 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
278 }
279
280 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero6) {
281 parseAssembly(
282 "define float @test(float %a) {\n"
283 " %1 = fcmp olt float %a, -0.0\n"
284 " %A = select i1 %1, float %a, float 0.0\n"
285 " ret float %A\n"
286 "}\n");
287 // The sign of zero doesn't matter in fcmp.
288 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
289 }
290
291 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero7) {
292 parseAssembly(
293 "define float @test(float %a) {\n"
294 " %1 = fcmp ogt float 0.0, %a\n"
295 " %A = select i1 %1, float %a, float -0.0\n"
296 " ret float %A\n"
297 "}\n");
298 // The sign of zero doesn't matter in fcmp.
299 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
300 }
301
302 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero8) {
303 parseAssembly(
304 "define float @test(float %a) {\n"
305 " %1 = fcmp olt float %a, 0.0\n"
306 " %A = select i1 %1, float %a, float -0.0\n"
307 " ret float %A\n"
308 "}\n");
309 // The sign of zero doesn't matter in fcmp.
310 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
311 }
312
313 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero1) {
314 parseAssembly(
315 "define float @test(float %a) {\n"
316 " %1 = fcmp ogt float -0.0, %a\n"
317 " %A = select i1 %1, float 0.0, float %a\n"
318 " ret float %A\n"
319 "}\n");
320 // The sign of zero doesn't matter in fcmp.
321 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
322 }
323
324 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero2) {
325 parseAssembly(
326 "define float @test(float %a) {\n"
327 " %1 = fcmp olt float %a, -0.0\n"
328 " %A = select i1 %1, float 0.0, float %a\n"
329 " ret float %A\n"
330 "}\n");
331 // The sign of zero doesn't matter in fcmp.
332 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
333 }
334
335 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero3) {
336 parseAssembly(
337 "define float @test(float %a) {\n"
338 " %1 = fcmp ogt float 0.0, %a\n"
339 " %A = select i1 %1, float -0.0, float %a\n"
340 " ret float %A\n"
341 "}\n");
342 // The sign of zero doesn't matter in fcmp.
343 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
344 }
345
346 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero4) {
347 parseAssembly(
348 "define float @test(float %a) {\n"
349 " %1 = fcmp olt float %a, 0.0\n"
350 " %A = select i1 %1, float -0.0, float %a\n"
351 " ret float %A\n"
352 "}\n");
353 // The sign of zero doesn't matter in fcmp.
354 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
355 }
356
357 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero5) {
358 parseAssembly(
359 "define float @test(float %a) {\n"
360 " %1 = fcmp olt float -0.0, %a\n"
361 " %A = select i1 %1, float %a, float 0.0\n"
362 " ret float %A\n"
363 "}\n");
364 // The sign of zero doesn't matter in fcmp.
365 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
366 }
367
368 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero6) {
369 parseAssembly(
370 "define float @test(float %a) {\n"
371 " %1 = fcmp ogt float %a, -0.0\n"
372 " %A = select i1 %1, float %a, float 0.0\n"
373 " ret float %A\n"
374 "}\n");
375 // The sign of zero doesn't matter in fcmp.
376 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
377 }
378
379 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero7) {
380 parseAssembly(
381 "define float @test(float %a) {\n"
382 " %1 = fcmp olt float 0.0, %a\n"
383 " %A = select i1 %1, float %a, float -0.0\n"
384 " ret float %A\n"
385 "}\n");
386 // The sign of zero doesn't matter in fcmp.
387 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
388 }
389
390 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero8) {
391 parseAssembly(
392 "define float @test(float %a) {\n"
393 " %1 = fcmp ogt float %a, 0.0\n"
394 " %A = select i1 %1, float %a, float -0.0\n"
395 " ret float %A\n"
396 "}\n");
397 // The sign of zero doesn't matter in fcmp.
398 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
399 }
400
401 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZeroVecUndef) {
402 parseAssembly(
403 "define <2 x float> @test(<2 x float> %a) {\n"
404 " %1 = fcmp ogt <2 x float> %a, <float -0.0, float -0.0>\n"
405 " %A = select <2 x i1> %1, <2 x float> <float undef, float 0.0>, <2 x float> %a\n"
406 " ret <2 x float> %A\n"
407 "}\n");
408 // An undef in a vector constant can not be back-propagated for this analysis.
409 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
410 }
411
412 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZeroVecUndef) {
413 parseAssembly(
414 "define <2 x float> @test(<2 x float> %a) {\n"
415 " %1 = fcmp ogt <2 x float> %a, zeroinitializer\n"
416 " %A = select <2 x i1> %1, <2 x float> %a, <2 x float> <float -0.0, float undef>\n"
417 " ret <2 x float> %A\n"
418 "}\n");
419 // An undef in a vector constant can not be back-propagated for this analysis.
420 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
421 }
422
423 TEST_F(MatchSelectPatternTest, VectorFMinimum) {
424 parseAssembly(
425 "define <4 x float> @test(<4 x float> %a) {\n"
426 " %1 = fcmp ule <4 x float> %a, \n"
427 " <float 5.0, float 5.0, float 5.0, float 5.0>\n"
428 " %A = select <4 x i1> %1, <4 x float> %a,\n"
429 " <4 x float> <float 5.0, float 5.0, float 5.0, float 5.0>\n"
430 " ret <4 x float> %A\n"
431 "}\n");
432 // Check that pattern matching works on vectors where each lane has the same
433 // unordered pattern.
434 expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false});
435 }
436
437 TEST_F(MatchSelectPatternTest, VectorFMinOtherOrdered) {
438 parseAssembly(
439 "define <4 x float> @test(<4 x float> %a) {\n"
440 " %1 = fcmp ole <4 x float> %a, \n"
441 " <float 5.0, float 5.0, float 5.0, float 5.0>\n"
442 " %A = select <4 x i1> %1, <4 x float> %a,\n"
443 " <4 x float> <float 5.0, float 5.0, float 5.0, float 5.0>\n"
444 " ret <4 x float> %A\n"
445 "}\n");
446 // Check that pattern matching works on vectors where each lane has the same
447 // ordered pattern.
448 expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true});
449 }
450
451 TEST_F(MatchSelectPatternTest, VectorNotFMinimum) {
452 parseAssembly(
453 "define <4 x float> @test(<4 x float> %a) {\n"
454 " %1 = fcmp ule <4 x float> %a, \n"
455 " <float 5.0, float 0x7ff8000000000000, float 5.0, float 5.0>\n"
456 " %A = select <4 x i1> %1, <4 x float> %a,\n"
457 " <4 x float> <float 5.0, float 0x7ff8000000000000, float 5.0, float "
458 "5.0>\n"
459 " ret <4 x float> %A\n"
460 "}\n");
461 // The lane that contains a NaN (0x7ff80...) behaves like a
462 // non-NaN-propagating min and the other lines behave like a NaN-propagating
463 // min, so check that neither is returned.
464 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
465 }
466
467 TEST_F(MatchSelectPatternTest, VectorNotFMinZero) {
468 parseAssembly(
469 "define <4 x float> @test(<4 x float> %a) {\n"
470 " %1 = fcmp ule <4 x float> %a, \n"
471 " <float 5.0, float -0.0, float 5.0, float 5.0>\n"
472 " %A = select <4 x i1> %1, <4 x float> %a,\n"
473 " <4 x float> <float 5.0, float 0.0, float 5.0, float 5.0>\n"
474 " ret <4 x float> %A\n"
475 "}\n");
476 // Always selects the second lane of %a if it is positive or negative zero, so
477 // this is stricter than a min.
478 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
479 }
480
481 TEST_F(MatchSelectPatternTest, DoubleCastU) {
482 parseAssembly(
483 "define i32 @test(i8 %a, i8 %b) {\n"
484 " %1 = icmp ult i8 %a, %b\n"
485 " %2 = zext i8 %a to i32\n"
486 " %3 = zext i8 %b to i32\n"
487 " %A = select i1 %1, i32 %2, i32 %3\n"
488 " ret i32 %A\n"
489 "}\n");
490 // We should be able to look through the situation where we cast both operands
491 // to the select.
492 expectPattern({SPF_UMIN, SPNB_NA, false});
493 }
494
495 TEST_F(MatchSelectPatternTest, DoubleCastS) {
496 parseAssembly(
497 "define i32 @test(i8 %a, i8 %b) {\n"
498 " %1 = icmp slt i8 %a, %b\n"
499 " %2 = sext i8 %a to i32\n"
500 " %3 = sext i8 %b to i32\n"
501 " %A = select i1 %1, i32 %2, i32 %3\n"
502 " ret i32 %A\n"
503 "}\n");
504 // We should be able to look through the situation where we cast both operands
505 // to the select.
506 expectPattern({SPF_SMIN, SPNB_NA, false});
507 }
508
509 TEST_F(MatchSelectPatternTest, DoubleCastBad) {
510 parseAssembly(
511 "define i32 @test(i8 %a, i8 %b) {\n"
512 " %1 = icmp ult i8 %a, %b\n"
513 " %2 = zext i8 %a to i32\n"
514 " %3 = sext i8 %b to i32\n"
515 " %A = select i1 %1, i32 %2, i32 %3\n"
516 " ret i32 %A\n"
517 "}\n");
518 // The cast types here aren't the same, so we cannot match an UMIN.
519 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
520 }
521
522 TEST_F(MatchSelectPatternTest, NotNotSMin) {
523 parseAssembly(
524 "define i8 @test(i8 %a, i8 %b) {\n"
525 " %cmp = icmp sgt i8 %a, %b\n"
526 " %an = xor i8 %a, -1\n"
527 " %bn = xor i8 %b, -1\n"
528 " %A = select i1 %cmp, i8 %an, i8 %bn\n"
529 " ret i8 %A\n"
530 "}\n");
531 expectPattern({SPF_SMIN, SPNB_NA, false});
532 }
533
534 TEST_F(MatchSelectPatternTest, NotNotSMinSwap) {
535 parseAssembly(
536 "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n"
537 " %cmp = icmp slt <2 x i8> %a, %b\n"
538 " %an = xor <2 x i8> %a, <i8 -1, i8-1>\n"
539 " %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n"
540 " %A = select <2 x i1> %cmp, <2 x i8> %bn, <2 x i8> %an\n"
541 " ret <2 x i8> %A\n"
542 "}\n");
543 expectPattern({SPF_SMIN, SPNB_NA, false});
544 }
545
546 TEST_F(MatchSelectPatternTest, NotNotSMax) {
547 parseAssembly(
548 "define i8 @test(i8 %a, i8 %b) {\n"
549 " %cmp = icmp slt i8 %a, %b\n"
550 " %an = xor i8 %a, -1\n"
551 " %bn = xor i8 %b, -1\n"
552 " %A = select i1 %cmp, i8 %an, i8 %bn\n"
553 " ret i8 %A\n"
554 "}\n");
555 expectPattern({SPF_SMAX, SPNB_NA, false});
556 }
557
558 TEST_F(MatchSelectPatternTest, NotNotSMaxSwap) {
559 parseAssembly(
560 "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n"
561 " %cmp = icmp sgt <2 x i8> %a, %b\n"
562 " %an = xor <2 x i8> %a, <i8 -1, i8-1>\n"
563 " %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n"
564 " %A = select <2 x i1> %cmp, <2 x i8> %bn, <2 x i8> %an\n"
565 " ret <2 x i8> %A\n"
566 "}\n");
567 expectPattern({SPF_SMAX, SPNB_NA, false});
568 }
569
570 TEST_F(MatchSelectPatternTest, NotNotUMin) {
571 parseAssembly(
572 "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n"
573 " %cmp = icmp ugt <2 x i8> %a, %b\n"
574 " %an = xor <2 x i8> %a, <i8 -1, i8-1>\n"
575 " %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n"
576 " %A = select <2 x i1> %cmp, <2 x i8> %an, <2 x i8> %bn\n"
577 " ret <2 x i8> %A\n"
578 "}\n");
579 expectPattern({SPF_UMIN, SPNB_NA, false});
580 }
581
582 TEST_F(MatchSelectPatternTest, NotNotUMinSwap) {
583 parseAssembly(
584 "define i8 @test(i8 %a, i8 %b) {\n"
585 " %cmp = icmp ult i8 %a, %b\n"
586 " %an = xor i8 %a, -1\n"
587 " %bn = xor i8 %b, -1\n"
588 " %A = select i1 %cmp, i8 %bn, i8 %an\n"
589 " ret i8 %A\n"
590 "}\n");
591 expectPattern({SPF_UMIN, SPNB_NA, false});
592 }
593
594 TEST_F(MatchSelectPatternTest, NotNotUMax) {
595 parseAssembly(
596 "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n"
597 " %cmp = icmp ult <2 x i8> %a, %b\n"
598 " %an = xor <2 x i8> %a, <i8 -1, i8-1>\n"
599 " %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n"
600 " %A = select <2 x i1> %cmp, <2 x i8> %an, <2 x i8> %bn\n"
601 " ret <2 x i8> %A\n"
602 "}\n");
603 expectPattern({SPF_UMAX, SPNB_NA, false});
604 }
605
606 TEST_F(MatchSelectPatternTest, NotNotUMaxSwap) {
607 parseAssembly(
608 "define i8 @test(i8 %a, i8 %b) {\n"
609 " %cmp = icmp ugt i8 %a, %b\n"
610 " %an = xor i8 %a, -1\n"
611 " %bn = xor i8 %b, -1\n"
612 " %A = select i1 %cmp, i8 %bn, i8 %an\n"
613 " ret i8 %A\n"
614 "}\n");
615 expectPattern({SPF_UMAX, SPNB_NA, false});
616 }
617
618 TEST_F(MatchSelectPatternTest, NotNotEq) {
619 parseAssembly(
620 "define i8 @test(i8 %a, i8 %b) {\n"
621 " %cmp = icmp eq i8 %a, %b\n"
622 " %an = xor i8 %a, -1\n"
623 " %bn = xor i8 %b, -1\n"
624 " %A = select i1 %cmp, i8 %bn, i8 %an\n"
625 " ret i8 %A\n"
626 "}\n");
627 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
628 }
629
630 TEST_F(MatchSelectPatternTest, NotNotNe) {
631 parseAssembly(
632 "define i8 @test(i8 %a, i8 %b) {\n"
633 " %cmp = icmp ne i8 %a, %b\n"
634 " %an = xor i8 %a, -1\n"
635 " %bn = xor i8 %b, -1\n"
636 " %A = select i1 %cmp, i8 %bn, i8 %an\n"
637 " ret i8 %A\n"
638 "}\n");
639 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
640 }
641
642 TEST(ValueTracking, GuaranteedToTransferExecutionToSuccessor) {
643 StringRef Assembly =
644 "declare void @nounwind_readonly(ptr) nounwind readonly "
645 "declare void @nounwind_argmemonly(ptr) nounwind argmemonly "
646 "declare void @nounwind_willreturn(ptr) nounwind willreturn "
647 "declare void @throws_but_readonly(ptr) readonly "
648 "declare void @throws_but_argmemonly(ptr) argmemonly "
649 "declare void @throws_but_willreturn(ptr) willreturn "
650 " "
651 "declare void @unknown(ptr) "
652 " "
653 "define void @f(ptr %p) { "
654 " call void @nounwind_readonly(ptr %p) "
655 " call void @nounwind_argmemonly(ptr %p) "
656 " call void @nounwind_willreturn(ptr %p)"
657 " call void @throws_but_readonly(ptr %p) "
658 " call void @throws_but_argmemonly(ptr %p) "
659 " call void @throws_but_willreturn(ptr %p) "
660 " call void @unknown(ptr %p) nounwind readonly "
661 " call void @unknown(ptr %p) nounwind argmemonly "
662 " call void @unknown(ptr %p) nounwind willreturn "
663 " call void @unknown(ptr %p) readonly "
664 " call void @unknown(ptr %p) argmemonly "
665 " call void @unknown(ptr %p) willreturn "
666 " ret void "
667 "} ";
668
669 LLVMContext Context;
670 SMDiagnostic Error;
671 auto M = parseAssemblyString(Assembly, Error, Context);
672 assert(M && "Bad assembly?");
673
674 auto *F = M->getFunction("f");
675 assert(F && "Bad assembly?");
676
677 auto &BB = F->getEntryBlock();
678 bool ExpectedAnswers[] = {
679 false, // call void @nounwind_readonly(ptr %p)
680 false, // call void @nounwind_argmemonly(ptr %p)
681 true, // call void @nounwind_willreturn(ptr %p)
682 false, // call void @throws_but_readonly(ptr %p)
683 false, // call void @throws_but_argmemonly(ptr %p)
684 false, // call void @throws_but_willreturn(ptr %p)
685 false, // call void @unknown(ptr %p) nounwind readonly
686 false, // call void @unknown(ptr %p) nounwind argmemonly
687 true, // call void @unknown(ptr %p) nounwind willreturn
688 false, // call void @unknown(ptr %p) readonly
689 false, // call void @unknown(ptr %p) argmemonly
690 false, // call void @unknown(ptr %p) willreturn
691 false, // ret void
692 };
693
694 int Index = 0;
695 for (auto &I : BB) {
696 EXPECT_EQ(isGuaranteedToTransferExecutionToSuccessor(&I),
697 ExpectedAnswers[Index])
698 << "Incorrect answer at instruction " << Index << " = " << I;
699 Index++;
700 }
701 }
702
703 TEST_F(ValueTrackingTest, ComputeNumSignBits_PR32045) {
704 parseAssembly(
705 "define i32 @test(i32 %a) {\n"
706 " %A = ashr i32 %a, -1\n"
707 " ret i32 %A\n"
708 "}\n");
709 EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 32u);
710 }
711
712 // No guarantees for canonical IR in this analysis, so this just bails out.
713 TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle) {
714 parseAssembly(
715 "define <2 x i32> @test() {\n"
716 " %A = shufflevector <2 x i32> undef, <2 x i32> undef, <2 x i32> <i32 0, i32 0>\n"
717 " ret <2 x i32> %A\n"
718 "}\n");
719 EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u);
720 }
721
722 // No guarantees for canonical IR in this analysis, so a shuffle element that
723 // references an undef value means this can't return any extra information.
724 TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle2) {
725 parseAssembly(
726 "define <2 x i32> @test(<2 x i1> %x) {\n"
727 " %sext = sext <2 x i1> %x to <2 x i32>\n"
728 " %A = shufflevector <2 x i32> %sext, <2 x i32> undef, <2 x i32> <i32 0, i32 2>\n"
729 " ret <2 x i32> %A\n"
730 "}\n");
731 EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u);
732 }
733
734 TEST_F(ValueTrackingTest, impliesPoisonTest_Identity) {
735 parseAssembly("define void @test(i32 %x, i32 %y) {\n"
736 " %A = add i32 %x, %y\n"
737 " ret void\n"
738 "}");
739 EXPECT_TRUE(impliesPoison(A, A));
740 }
741
742 TEST_F(ValueTrackingTest, impliesPoisonTest_ICmp) {
743 parseAssembly("define void @test(i32 %x) {\n"
744 " %A2 = icmp eq i32 %x, 0\n"
745 " %A = icmp eq i32 %x, 1\n"
746 " ret void\n"
747 "}");
748 EXPECT_TRUE(impliesPoison(A2, A));
749 }
750
751 TEST_F(ValueTrackingTest, impliesPoisonTest_ICmpUnknown) {
752 parseAssembly("define void @test(i32 %x, i32 %y) {\n"
753 " %A2 = icmp eq i32 %x, %y\n"
754 " %A = icmp eq i32 %x, 1\n"
755 " ret void\n"
756 "}");
757 EXPECT_FALSE(impliesPoison(A2, A));
758 }
759
760 TEST_F(ValueTrackingTest, impliesPoisonTest_AddNswOkay) {
761 parseAssembly("define void @test(i32 %x) {\n"
762 " %A2 = add nsw i32 %x, 1\n"
763 " %A = add i32 %A2, 1\n"
764 " ret void\n"
765 "}");
766 EXPECT_TRUE(impliesPoison(A2, A));
767 }
768
769 TEST_F(ValueTrackingTest, impliesPoisonTest_AddNswOkay2) {
770 parseAssembly("define void @test(i32 %x) {\n"
771 " %A2 = add i32 %x, 1\n"
772 " %A = add nsw i32 %A2, 1\n"
773 " ret void\n"
774 "}");
775 EXPECT_TRUE(impliesPoison(A2, A));
776 }
777
778 TEST_F(ValueTrackingTest, impliesPoisonTest_AddNsw) {
779 parseAssembly("define void @test(i32 %x) {\n"
780 " %A2 = add nsw i32 %x, 1\n"
781 " %A = add i32 %x, 1\n"
782 " ret void\n"
783 "}");
784 EXPECT_FALSE(impliesPoison(A2, A));
785 }
786
787 TEST_F(ValueTrackingTest, impliesPoisonTest_Cmp) {
788 parseAssembly("define void @test(i32 %x, i32 %y, i1 %c) {\n"
789 " %A2 = icmp eq i32 %x, %y\n"
790 " %A0 = icmp ult i32 %x, %y\n"
791 " %A = or i1 %A0, %c\n"
792 " ret void\n"
793 "}");
794 EXPECT_TRUE(impliesPoison(A2, A));
795 }
796
797 TEST_F(ValueTrackingTest, impliesPoisonTest_FCmpFMF) {
798 parseAssembly("define void @test(float %x, float %y, i1 %c) {\n"
799 " %A2 = fcmp nnan oeq float %x, %y\n"
800 " %A0 = fcmp olt float %x, %y\n"
801 " %A = or i1 %A0, %c\n"
802 " ret void\n"
803 "}");
804 EXPECT_FALSE(impliesPoison(A2, A));
805 }
806
807 TEST_F(ValueTrackingTest, impliesPoisonTest_AddSubSameOps) {
808 parseAssembly("define void @test(i32 %x, i32 %y, i1 %c) {\n"
809 " %A2 = add i32 %x, %y\n"
810 " %A = sub i32 %x, %y\n"
811 " ret void\n"
812 "}");
813 EXPECT_TRUE(impliesPoison(A2, A));
814 }
815
816 TEST_F(ValueTrackingTest, impliesPoisonTest_MaskCmp) {
817 parseAssembly("define void @test(i32 %x, i32 %y, i1 %c) {\n"
818 " %M2 = and i32 %x, 7\n"
819 " %A2 = icmp eq i32 %M2, 1\n"
820 " %M = and i32 %x, 15\n"
821 " %A = icmp eq i32 %M, 3\n"
822 " ret void\n"
823 "}");
824 EXPECT_TRUE(impliesPoison(A2, A));
825 }
826
827 TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle_Pointers) {
828 parseAssembly(
829 "define <2 x ptr> @test(<2 x ptr> %x) {\n"
830 " %A = shufflevector <2 x ptr> zeroinitializer, <2 x ptr> undef, <2 x i32> zeroinitializer\n"
831 " ret <2 x ptr> %A\n"
832 "}\n");
833 EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 64u);
834 }
835
836 TEST(ValueTracking, propagatesPoison) {
837 std::string AsmHead =
838 "declare i32 @g(i32)\n"
839 "define void @f(i32 %x, i32 %y, i32 %shamt, float %fx, float %fy, "
840 "i1 %cond, ptr %p) {\n";
841 std::string AsmTail = " ret void\n}";
842 // (propagates poison?, IR instruction)
843 SmallVector<std::tuple<bool, std::string, unsigned>, 32> Data = {
844 {true, "add i32 %x, %y", 0},
845 {true, "add i32 %x, %y", 1},
846 {true, "add nsw nuw i32 %x, %y", 0},
847 {true, "add nsw nuw i32 %x, %y", 1},
848 {true, "ashr i32 %x, %y", 0},
849 {true, "ashr i32 %x, %y", 1},
850 {true, "lshr exact i32 %x, 31", 0},
851 {true, "lshr exact i32 %x, 31", 1},
852 {true, "fadd float %fx, %fy", 0},
853 {true, "fadd float %fx, %fy", 1},
854 {true, "fsub float %fx, %fy", 0},
855 {true, "fsub float %fx, %fy", 1},
856 {true, "fmul float %fx, %fy", 0},
857 {true, "fmul float %fx, %fy", 1},
858 {true, "fdiv float %fx, %fy", 0},
859 {true, "fdiv float %fx, %fy", 1},
860 {true, "frem float %fx, %fy", 0},
861 {true, "frem float %fx, %fy", 1},
862 {true, "fneg float %fx", 0},
863 {true, "fcmp oeq float %fx, %fy", 0},
864 {true, "fcmp oeq float %fx, %fy", 1},
865 {true, "icmp eq i32 %x, %y", 0},
866 {true, "icmp eq i32 %x, %y", 1},
867 {true, "getelementptr i8, ptr %p, i32 %x", 0},
868 {true, "getelementptr i8, ptr %p, i32 %x", 1},
869 {true, "getelementptr inbounds i8, ptr %p, i32 %x", 0},
870 {true, "getelementptr inbounds i8, ptr %p, i32 %x", 1},
871 {true, "bitcast float %fx to i32", 0},
872 {true, "select i1 %cond, i32 %x, i32 %y", 0},
873 {false, "select i1 %cond, i32 %x, i32 %y", 1},
874 {false, "select i1 %cond, i32 %x, i32 %y", 2},
875 {false, "freeze i32 %x", 0},
876 {true, "udiv i32 %x, %y", 0},
877 {true, "udiv i32 %x, %y", 1},
878 {true, "urem i32 %x, %y", 0},
879 {true, "urem i32 %x, %y", 1},
880 {true, "sdiv exact i32 %x, %y", 0},
881 {true, "sdiv exact i32 %x, %y", 1},
882 {true, "srem i32 %x, %y", 0},
883 {true, "srem i32 %x, %y", 1},
884 {false, "call i32 @g(i32 %x)", 0},
885 {false, "call i32 @g(i32 %x)", 1},
886 {true, "call {i32, i1} @llvm.sadd.with.overflow.i32(i32 %x, i32 %y)", 0},
887 {true, "call {i32, i1} @llvm.ssub.with.overflow.i32(i32 %x, i32 %y)", 0},
888 {true, "call {i32, i1} @llvm.smul.with.overflow.i32(i32 %x, i32 %y)", 0},
889 {true, "call {i32, i1} @llvm.uadd.with.overflow.i32(i32 %x, i32 %y)", 0},
890 {true, "call {i32, i1} @llvm.usub.with.overflow.i32(i32 %x, i32 %y)", 0},
891 {true, "call {i32, i1} @llvm.umul.with.overflow.i32(i32 %x, i32 %y)", 0},
892 {true, "call i32 @llvm.sadd.sat.i32(i32 %x, i32 %y)", 0},
893 {true, "call i32 @llvm.ssub.sat.i32(i32 %x, i32 %y)", 0},
894 {true, "call i32 @llvm.sshl.sat.i32(i32 %x, i32 %y)", 0},
895 {true, "call i32 @llvm.uadd.sat.i32(i32 %x, i32 %y)", 0},
896 {true, "call i32 @llvm.usub.sat.i32(i32 %x, i32 %y)", 0},
897 {true, "call i32 @llvm.ushl.sat.i32(i32 %x, i32 %y)", 0},
898 {true, "call i32 @llvm.ctpop.i32(i32 %x)", 0},
899 {true, "call i32 @llvm.ctlz.i32(i32 %x, i1 true)", 0},
900 {true, "call i32 @llvm.cttz.i32(i32 %x, i1 true)", 0},
901 {true, "call i32 @llvm.abs.i32(i32 %x, i1 true)", 0},
902 {true, "call i32 @llvm.smax.i32(i32 %x, i32 %y)", 0},
903 {true, "call i32 @llvm.smin.i32(i32 %x, i32 %y)", 0},
904 {true, "call i32 @llvm.umax.i32(i32 %x, i32 %y)", 0},
905 {true, "call i32 @llvm.umin.i32(i32 %x, i32 %y)", 0},
906 {true, "call i32 @llvm.bitreverse.i32(i32 %x)", 0},
907 {true, "call i32 @llvm.bswap.i32(i32 %x)", 0},
908 {false, "call i32 @llvm.fshl.i32(i32 %x, i32 %y, i32 %shamt)", 0},
909 {false, "call i32 @llvm.fshl.i32(i32 %x, i32 %y, i32 %shamt)", 1},
910 {false, "call i32 @llvm.fshl.i32(i32 %x, i32 %y, i32 %shamt)", 2},
911 {false, "call i32 @llvm.fshr.i32(i32 %x, i32 %y, i32 %shamt)", 0},
912 {false, "call i32 @llvm.fshr.i32(i32 %x, i32 %y, i32 %shamt)", 1},
913 {false, "call i32 @llvm.fshr.i32(i32 %x, i32 %y, i32 %shamt)", 2},
914 {true, "call float @llvm.sqrt.f32(float %fx)", 0},
915 {true, "call float @llvm.powi.f32.i32(float %fx, i32 %x)", 0},
916 {false, "call float @llvm.sin.f32(float %fx)", 0},
917 {false, "call float @llvm.cos.f32(float %fx)", 0},
918 {true, "call float @llvm.pow.f32(float %fx, float %fy)", 0},
919 {false, "call float @llvm.exp.f32(float %fx)", 0},
920 {false, "call float @llvm.exp2.f32(float %fx)", 0},
921 {false, "call float @llvm.log.f32(float %fx)", 0},
922 {false, "call float @llvm.log10.f32(float %fx)", 0},
923 {false, "call float @llvm.log2.f32(float %fx)", 0},
924 {false, "call float @llvm.fma.f32(float %fx, float %fx, float %fy)", 0},
925 {false, "call float @llvm.fabs.f32(float %fx)", 0},
926 {false, "call float @llvm.minnum.f32(float %fx, float %fy)", 0},
927 {false, "call float @llvm.maxnum.f32(float %fx, float %fy)", 0},
928 {false, "call float @llvm.minimum.f32(float %fx, float %fy)", 0},
929 {false, "call float @llvm.maximum.f32(float %fx, float %fy)", 0},
930 {false, "call float @llvm.copysign.f32(float %fx, float %fy)", 0},
931 {false, "call float @llvm.floor.f32(float %fx)", 0},
932 {false, "call float @llvm.ceil.f32(float %fx)", 0},
933 {false, "call float @llvm.trunc.f32(float %fx)", 0},
934 {false, "call float @llvm.rint.f32(float %fx)", 0},
935 {false, "call float @llvm.nearbyint.f32(float %fx)", 0},
936 {false, "call float @llvm.round.f32(float %fx)", 0},
937 {false, "call float @llvm.roundeven.f32(float %fx)", 0},
938 {false, "call i32 @llvm.lround.f32(float %fx)", 0},
939 {false, "call i64 @llvm.llround.f32(float %fx)", 0},
940 {false, "call i32 @llvm.lrint.f32(float %fx)", 0},
941 {false, "call i64 @llvm.llrint.f32(float %fx)", 0},
942 {false, "call float @llvm.fmuladd.f32(float %fx, float %fx, float %fy)",
943 0}};
944
945 std::string AssemblyStr = AsmHead;
946 for (auto &Itm : Data)
947 AssemblyStr += std::get<1>(Itm) + "\n";
948 AssemblyStr += AsmTail;
949
950 LLVMContext Context;
951 SMDiagnostic Error;
952 auto M = parseAssemblyString(AssemblyStr, Error, Context);
953 assert(M && "Bad assembly?");
954
955 auto *F = M->getFunction("f");
956 assert(F && "Bad assembly?");
957
958 auto &BB = F->getEntryBlock();
959
960 int Index = 0;
961 for (auto &I : BB) {
962 if (isa<ReturnInst>(&I))
963 break;
964 bool ExpectedVal = std::get<0>(Data[Index]);
965 unsigned OpIdx = std::get<2>(Data[Index]);
966 EXPECT_EQ(propagatesPoison(I.getOperandUse(OpIdx)), ExpectedVal)
967 << "Incorrect answer at instruction " << Index << " = " << I;
968 Index++;
969 }
970 }
971
972 TEST_F(ValueTrackingTest, programUndefinedIfPoison) {
973 parseAssembly("declare i32 @any_num()"
974 "define void @test(i32 %mask) {\n"
975 " %A = call i32 @any_num()\n"
976 " %B = or i32 %A, %mask\n"
977 " udiv i32 1, %B"
978 " ret void\n"
979 "}\n");
980 // If %A was poison, udiv raises UB regardless of %mask's value
981 EXPECT_EQ(programUndefinedIfPoison(A), true);
982 }
983
984 TEST_F(ValueTrackingTest, programUndefinedIfPoisonSelect) {
985 parseAssembly("declare i32 @any_num()"
986 "define void @test(i1 %Cond) {\n"
987 " %A = call i32 @any_num()\n"
988 " %B = add i32 %A, 1\n"
989 " %C = select i1 %Cond, i32 %A, i32 %B\n"
990 " udiv i32 1, %C"
991 " ret void\n"
992 "}\n");
993 // If A is poison, B is also poison, and therefore C is poison regardless of
994 // the value of %Cond.
995 EXPECT_EQ(programUndefinedIfPoison(A), true);
996 }
997
998 TEST_F(ValueTrackingTest, programUndefinedIfUndefOrPoison) {
999 parseAssembly("declare i32 @any_num()"
1000 "define void @test(i32 %mask) {\n"
1001 " %A = call i32 @any_num()\n"
1002 " %B = or i32 %A, %mask\n"
1003 " udiv i32 1, %B"
1004 " ret void\n"
1005 "}\n");
1006 // If %A was undef and %mask was 1, udiv does not raise UB
1007 EXPECT_EQ(programUndefinedIfUndefOrPoison(A), false);
1008 }
1009
1010 TEST_F(ValueTrackingTest, isGuaranteedNotToBePoison_exploitBranchCond) {
1011 parseAssembly("declare i1 @any_bool()"
1012 "define void @test(i1 %y) {\n"
1013 " %A = call i1 @any_bool()\n"
1014 " %cond = and i1 %A, %y\n"
1015 " br i1 %cond, label %BB1, label %BB2\n"
1016 "BB1:\n"
1017 " ret void\n"
1018 "BB2:\n"
1019 " ret void\n"
1020 "}\n");
1021 DominatorTree DT(*F);
1022 for (auto &BB : *F) {
1023 if (&BB == &F->getEntryBlock())
1024 continue;
1025
1026 EXPECT_EQ(isGuaranteedNotToBePoison(A, nullptr, BB.getTerminator(), &DT),
1027 true)
1028 << "isGuaranteedNotToBePoison does not hold at " << *BB.getTerminator();
1029 }
1030 }
1031
1032 TEST_F(ValueTrackingTest, isGuaranteedNotToBePoison_phi) {
1033 parseAssembly("declare i32 @any_i32(i32)"
1034 "define void @test() {\n"
1035 "ENTRY:\n"
1036 " br label %LOOP\n"
1037 "LOOP:\n"
1038 " %A = phi i32 [0, %ENTRY], [%A.next, %NEXT]\n"
1039 " %A.next = call i32 @any_i32(i32 %A)\n"
1040 " %cond = icmp eq i32 %A.next, 0\n"
1041 " br i1 %cond, label %NEXT, label %EXIT\n"
1042 "NEXT:\n"
1043 " br label %LOOP\n"
1044 "EXIT:\n"
1045 " ret void\n"
1046 "}\n");
1047 DominatorTree DT(*F);
1048 for (auto &BB : *F) {
1049 if (BB.getName() == "LOOP") {
1050 EXPECT_EQ(isGuaranteedNotToBePoison(A, nullptr, A, &DT), true)
1051 << "isGuaranteedNotToBePoison does not hold";
1052 }
1053 }
1054 }
1055
1056 TEST_F(ValueTrackingTest, isGuaranteedNotToBeUndefOrPoison) {
1057 parseAssembly("declare void @f(i32 noundef)"
1058 "define void @test(i32 %x) {\n"
1059 " %A = bitcast i32 %x to i32\n"
1060 " call void @f(i32 noundef %x)\n"
1061 " ret void\n"
1062 "}\n");
1063 EXPECT_EQ(isGuaranteedNotToBeUndefOrPoison(A), true);
1064 EXPECT_EQ(isGuaranteedNotToBeUndefOrPoison(UndefValue::get(IntegerType::get(Context, 8))), false);
1065 EXPECT_EQ(isGuaranteedNotToBeUndefOrPoison(PoisonValue::get(IntegerType::get(Context, 8))), false);
1066 EXPECT_EQ(isGuaranteedNotToBePoison(UndefValue::get(IntegerType::get(Context, 8))), true);
1067 EXPECT_EQ(isGuaranteedNotToBePoison(PoisonValue::get(IntegerType::get(Context, 8))), false);
1068
1069 Type *Int32Ty = Type::getInt32Ty(Context);
1070 Constant *CU = UndefValue::get(Int32Ty);
1071 Constant *CP = PoisonValue::get(Int32Ty);
1072 Constant *C1 = ConstantInt::get(Int32Ty, 1);
1073 Constant *C2 = ConstantInt::get(Int32Ty, 2);
1074
1075 {
1076 Constant *V1 = ConstantVector::get({C1, C2});
1077 EXPECT_TRUE(isGuaranteedNotToBeUndefOrPoison(V1));
1078 EXPECT_TRUE(isGuaranteedNotToBePoison(V1));
1079 }
1080
1081 {
1082 Constant *V2 = ConstantVector::get({C1, CU});
1083 EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(V2));
1084 EXPECT_TRUE(isGuaranteedNotToBePoison(V2));
1085 }
1086
1087 {
1088 Constant *V3 = ConstantVector::get({C1, CP});
1089 EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(V3));
1090 EXPECT_FALSE(isGuaranteedNotToBePoison(V3));
1091 }
1092 }
1093
1094 TEST_F(ValueTrackingTest, isGuaranteedNotToBeUndefOrPoison_assume) {
1095 parseAssembly("declare i1 @f_i1()\n"
1096 "declare i32 @f_i32()\n"
1097 "declare void @llvm.assume(i1)\n"
1098 "define void @test() {\n"
1099 " %A = call i32 @f_i32()\n"
1100 " %cond = call i1 @f_i1()\n"
1101 " %CxtI = add i32 0, 0\n"
1102 " br i1 %cond, label %BB1, label %EXIT\n"
1103 "BB1:\n"
1104 " %CxtI2 = add i32 0, 0\n"
1105 " %cond2 = call i1 @f_i1()\n"
1106 " call void @llvm.assume(i1 true) [ \"noundef\"(i32 %A) ]\n"
1107 " br i1 %cond2, label %BB2, label %EXIT\n"
1108 "BB2:\n"
1109 " %CxtI3 = add i32 0, 0\n"
1110 " ret void\n"
1111 "EXIT:\n"
1112 " ret void\n"
1113 "}");
1114 AssumptionCache AC(*F);
1115 DominatorTree DT(*F);
1116 EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(A, &AC, CxtI, &DT));
1117 EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(A, &AC, CxtI2, &DT));
1118 EXPECT_TRUE(isGuaranteedNotToBeUndefOrPoison(A, &AC, CxtI3, &DT));
1119 }
1120
1121 TEST(ValueTracking, canCreatePoisonOrUndef) {
1122 std::string AsmHead =
1123 "@s = external dso_local global i32, align 1\n"
1124 "declare i32 @g(i32)\n"
1125 "declare {i32, i1} @llvm.sadd.with.overflow.i32(i32 %a, i32 %b)\n"
1126 "declare {i32, i1} @llvm.ssub.with.overflow.i32(i32 %a, i32 %b)\n"
1127 "declare {i32, i1} @llvm.smul.with.overflow.i32(i32 %a, i32 %b)\n"
1128 "declare {i32, i1} @llvm.uadd.with.overflow.i32(i32 %a, i32 %b)\n"
1129 "declare {i32, i1} @llvm.usub.with.overflow.i32(i32 %a, i32 %b)\n"
1130 "declare {i32, i1} @llvm.umul.with.overflow.i32(i32 %a, i32 %b)\n"
1131 "define void @f(i32 %x, i32 %y, float %fx, float %fy, i1 %cond, "
1132 "<4 x i32> %vx, <4 x i32> %vx2, <vscale x 4 x i32> %svx, ptr %p) {\n";
1133 std::string AsmTail = " ret void\n}";
1134 // (can create poison?, can create undef?, IR instruction)
1135 SmallVector<std::pair<std::pair<bool, bool>, std::string>, 32> Data = {
1136 {{false, false}, "add i32 %x, %y"},
1137 {{true, false}, "add nsw nuw i32 %x, %y"},
1138 {{true, false}, "shl i32 %x, %y"},
1139 {{true, false}, "shl <4 x i32> %vx, %vx2"},
1140 {{true, false}, "shl nsw i32 %x, %y"},
1141 {{true, false}, "shl nsw <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"},
1142 {{false, false}, "shl i32 %x, 31"},
1143 {{true, false}, "shl i32 %x, 32"},
1144 {{false, false}, "shl <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"},
1145 {{true, false}, "shl <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 32>"},
1146 {{true, false}, "ashr i32 %x, %y"},
1147 {{true, false}, "ashr exact i32 %x, %y"},
1148 {{false, false}, "ashr i32 %x, 31"},
1149 {{true, false}, "ashr exact i32 %x, 31"},
1150 {{false, false}, "ashr <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"},
1151 {{true, false}, "ashr <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 32>"},
1152 {{true, false}, "ashr exact <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"},
1153 {{true, false}, "lshr i32 %x, %y"},
1154 {{true, false}, "lshr exact i32 %x, 31"},
1155 {{false, false}, "udiv i32 %x, %y"},
1156 {{true, false}, "udiv exact i32 %x, %y"},
1157 {{false, false}, "getelementptr i8, ptr %p, i32 %x"},
1158 {{true, false}, "getelementptr inbounds i8, ptr %p, i32 %x"},
1159 {{true, false}, "fneg nnan float %fx"},
1160 {{false, false}, "fneg float %fx"},
1161 {{false, false}, "fadd float %fx, %fy"},
1162 {{true, false}, "fadd nnan float %fx, %fy"},
1163 {{false, false}, "urem i32 %x, %y"},
1164 {{true, false}, "fptoui float %fx to i32"},
1165 {{true, false}, "fptosi float %fx to i32"},
1166 {{false, false}, "bitcast float %fx to i32"},
1167 {{false, false}, "select i1 %cond, i32 %x, i32 %y"},
1168 {{true, false}, "select nnan i1 %cond, float %fx, float %fy"},
1169 {{true, false}, "extractelement <4 x i32> %vx, i32 %x"},
1170 {{false, false}, "extractelement <4 x i32> %vx, i32 3"},
1171 {{true, false}, "extractelement <vscale x 4 x i32> %svx, i32 4"},
1172 {{true, false}, "insertelement <4 x i32> %vx, i32 %x, i32 %y"},
1173 {{false, false}, "insertelement <4 x i32> %vx, i32 %x, i32 3"},
1174 {{true, false}, "insertelement <vscale x 4 x i32> %svx, i32 %x, i32 4"},
1175 {{false, false}, "freeze i32 %x"},
1176 {{false, false},
1177 "shufflevector <4 x i32> %vx, <4 x i32> %vx2, "
1178 "<4 x i32> <i32 0, i32 1, i32 2, i32 3>"},
1179 {{true, false},
1180 "shufflevector <4 x i32> %vx, <4 x i32> %vx2, "
1181 "<4 x i32> <i32 0, i32 1, i32 2, i32 poison>"},
1182 {{true, false},
1183 "shufflevector <vscale x 4 x i32> %svx, "
1184 "<vscale x 4 x i32> %svx, <vscale x 4 x i32> poison"},
1185 {{true, false}, "call i32 @g(i32 %x)"},
1186 {{false, false}, "call noundef i32 @g(i32 %x)"},
1187 {{true, false}, "fcmp nnan oeq float %fx, %fy"},
1188 {{false, false}, "fcmp oeq float %fx, %fy"},
1189 {{true, false}, "ashr i32 %x, ptrtoint (ptr @s to i32)"},
1190 {{false, false},
1191 "call {i32, i1} @llvm.sadd.with.overflow.i32(i32 %x, i32 %y)"},
1192 {{false, false},
1193 "call {i32, i1} @llvm.ssub.with.overflow.i32(i32 %x, i32 %y)"},
1194 {{false, false},
1195 "call {i32, i1} @llvm.smul.with.overflow.i32(i32 %x, i32 %y)"},
1196 {{false, false},
1197 "call {i32, i1} @llvm.uadd.with.overflow.i32(i32 %x, i32 %y)"},
1198 {{false, false},
1199 "call {i32, i1} @llvm.usub.with.overflow.i32(i32 %x, i32 %y)"},
1200 {{false, false},
1201 "call {i32, i1} @llvm.umul.with.overflow.i32(i32 %x, i32 %y)"}};
1202
1203 std::string AssemblyStr = AsmHead;
1204 for (auto &Itm : Data)
1205 AssemblyStr += Itm.second + "\n";
1206 AssemblyStr += AsmTail;
1207
1208 LLVMContext Context;
1209 SMDiagnostic Error;
1210 auto M = parseAssemblyString(AssemblyStr, Error, Context);
1211 assert(M && "Bad assembly?");
1212
1213 auto *F = M->getFunction("f");
1214 assert(F && "Bad assembly?");
1215
1216 auto &BB = F->getEntryBlock();
1217
1218 int Index = 0;
1219 for (auto &I : BB) {
1220 if (isa<ReturnInst>(&I))
1221 break;
1222 bool Poison = Data[Index].first.first;
1223 bool Undef = Data[Index].first.second;
1224 EXPECT_EQ(canCreatePoison(cast<Operator>(&I)), Poison)
1225 << "Incorrect answer of canCreatePoison at instruction " << Index
1226 << " = " << I;
1227 EXPECT_EQ(canCreateUndefOrPoison(cast<Operator>(&I)), Undef || Poison)
1228 << "Incorrect answer of canCreateUndef at instruction " << Index
1229 << " = " << I;
1230 Index++;
1231 }
1232 }
1233
1234 TEST_F(ValueTrackingTest, computePtrAlignment) {
1235 parseAssembly("declare i1 @f_i1()\n"
1236 "declare ptr @f_i8p()\n"
1237 "declare void @llvm.assume(i1)\n"
1238 "define void @test() {\n"
1239 " %A = call ptr @f_i8p()\n"
1240 " %cond = call i1 @f_i1()\n"
1241 " %CxtI = add i32 0, 0\n"
1242 " br i1 %cond, label %BB1, label %EXIT\n"
1243 "BB1:\n"
1244 " %CxtI2 = add i32 0, 0\n"
1245 " %cond2 = call i1 @f_i1()\n"
1246 " call void @llvm.assume(i1 true) [ \"align\"(ptr %A, i64 16) ]\n"
1247 " br i1 %cond2, label %BB2, label %EXIT\n"
1248 "BB2:\n"
1249 " %CxtI3 = add i32 0, 0\n"
1250 " ret void\n"
1251 "EXIT:\n"
1252 " ret void\n"
1253 "}");
1254 AssumptionCache AC(*F);
1255 DominatorTree DT(*F);
1256 const DataLayout &DL = M->getDataLayout();
1257 EXPECT_EQ(getKnownAlignment(A, DL, CxtI, &AC, &DT), Align(1));
1258 EXPECT_EQ(getKnownAlignment(A, DL, CxtI2, &AC, &DT), Align(1));
1259 EXPECT_EQ(getKnownAlignment(A, DL, CxtI3, &AC, &DT), Align(16));
1260 }
1261
1262 TEST_F(ValueTrackingTest, MatchBinaryIntrinsicRecurrenceUMax) {
1263 auto M = parseModule(R"(
1264 define i8 @test(i8 %a, i8 %b) {
1265 entry:
1266 br label %loop
1267 loop:
1268 %iv = phi i8 [ %iv.next, %loop ], [ 0, %entry ]
1269 %umax.acc = phi i8 [ %umax, %loop ], [ %a, %entry ]
1270 %umax = call i8 @llvm.umax.i8(i8 %umax.acc, i8 %b)
1271 %iv.next = add nuw i8 %iv, 1
1272 %cmp = icmp ult i8 %iv.next, 10
1273 br i1 %cmp, label %loop, label %exit
1274 exit:
1275 ret i8 %umax
1276 }
1277 )");
1278
1279 auto *F = M->getFunction("test");
1280 auto *II = &cast<IntrinsicInst>(findInstructionByName(F, "umax"));
1281 auto *UMaxAcc = &cast<PHINode>(findInstructionByName(F, "umax.acc"));
1282 PHINode *PN;
1283 Value *Init, *OtherOp;
1284 EXPECT_TRUE(matchSimpleBinaryIntrinsicRecurrence(II, PN, Init, OtherOp));
1285 EXPECT_EQ(UMaxAcc, PN);
1286 EXPECT_EQ(F->getArg(0), Init);
1287 EXPECT_EQ(F->getArg(1), OtherOp);
1288 }
1289
1290 TEST_F(ValueTrackingTest, MatchBinaryIntrinsicRecurrenceNegativeFSHR) {
1291 auto M = parseModule(R"(
1292 define i8 @test(i8 %a, i8 %b, i8 %c) {
1293 entry:
1294 br label %loop
1295 loop:
1296 %iv = phi i8 [ %iv.next, %loop ], [ 0, %entry ]
1297 %fshr.acc = phi i8 [ %fshr, %loop ], [ %a, %entry ]
1298 %fshr = call i8 @llvm.fshr.i8(i8 %fshr.acc, i8 %b, i8 %c)
1299 %iv.next = add nuw i8 %iv, 1
1300 %cmp = icmp ult i8 %iv.next, 10
1301 br i1 %cmp, label %loop, label %exit
1302 exit:
1303 ret i8 %fshr
1304 }
1305 )");
1306
1307 auto *F = M->getFunction("test");
1308 auto *II = &cast<IntrinsicInst>(findInstructionByName(F, "fshr"));
1309 PHINode *PN;
1310 Value *Init, *OtherOp;
1311 EXPECT_FALSE(matchSimpleBinaryIntrinsicRecurrence(II, PN, Init, OtherOp));
1312 }
1313
1314 TEST_F(ComputeKnownBitsTest, ComputeKnownBits) {
1315 parseAssembly(
1316 "define i32 @test(i32 %a, i32 %b) {\n"
1317 " %ash = mul i32 %a, 8\n"
1318 " %aad = add i32 %ash, 7\n"
1319 " %aan = and i32 %aad, 4095\n"
1320 " %bsh = shl i32 %b, 4\n"
1321 " %bad = or i32 %bsh, 6\n"
1322 " %ban = and i32 %bad, 4095\n"
1323 " %A = mul i32 %aan, %ban\n"
1324 " ret i32 %A\n"
1325 "}\n");
1326 expectKnownBits(/*zero*/ 4278190085u, /*one*/ 10u);
1327 }
1328
1329 TEST_F(ComputeKnownBitsTest, ComputeKnownMulBits) {
1330 parseAssembly(
1331 "define i32 @test(i32 %a, i32 %b) {\n"
1332 " %aa = shl i32 %a, 5\n"
1333 " %bb = shl i32 %b, 5\n"
1334 " %aaa = or i32 %aa, 24\n"
1335 " %bbb = or i32 %bb, 28\n"
1336 " %A = mul i32 %aaa, %bbb\n"
1337 " ret i32 %A\n"
1338 "}\n");
1339 expectKnownBits(/*zero*/ 95u, /*one*/ 32u);
1340 }
1341
1342 TEST_F(ComputeKnownFPClassTest, SelectPos0) {
1343 parseAssembly(
1344 "define float @test(i1 %cond) {\n"
1345 " %A = select i1 %cond, float 0.0, float 0.0"
1346 " ret float %A\n"
1347 "}\n");
1348 expectKnownFPClass(fcPosZero, false);
1349 }
1350
1351 TEST_F(ComputeKnownFPClassTest, SelectNeg0) {
1352 parseAssembly(
1353 "define float @test(i1 %cond) {\n"
1354 " %A = select i1 %cond, float -0.0, float -0.0"
1355 " ret float %A\n"
1356 "}\n");
1357 expectKnownFPClass(fcNegZero, true);
1358 }
1359
1360 TEST_F(ComputeKnownFPClassTest, SelectPosOrNeg0) {
1361 parseAssembly(
1362 "define float @test(i1 %cond) {\n"
1363 " %A = select i1 %cond, float 0.0, float -0.0"
1364 " ret float %A\n"
1365 "}\n");
1366 expectKnownFPClass(fcZero, std::nullopt);
1367 }
1368
1369 TEST_F(ComputeKnownFPClassTest, SelectPosInf) {
1370 parseAssembly(
1371 "define float @test(i1 %cond) {\n"
1372 " %A = select i1 %cond, float 0x7FF0000000000000, float 0x7FF0000000000000"
1373 " ret float %A\n"
1374 "}\n");
1375 expectKnownFPClass(fcPosInf, false);
1376 }
1377
1378 TEST_F(ComputeKnownFPClassTest, SelectNegInf) {
1379 parseAssembly(
1380 "define float @test(i1 %cond) {\n"
1381 " %A = select i1 %cond, float 0xFFF0000000000000, float 0xFFF0000000000000"
1382 " ret float %A\n"
1383 "}\n");
1384 expectKnownFPClass(fcNegInf, true);
1385 }
1386
1387 TEST_F(ComputeKnownFPClassTest, SelectPosOrNegInf) {
1388 parseAssembly(
1389 "define float @test(i1 %cond) {\n"
1390 " %A = select i1 %cond, float 0x7FF0000000000000, float 0xFFF0000000000000"
1391 " ret float %A\n"
1392 "}\n");
1393 expectKnownFPClass(fcInf, std::nullopt);
1394 }
1395
1396 TEST_F(ComputeKnownFPClassTest, SelectNNaN) {
1397 parseAssembly(
1398 "define float @test(i1 %cond, float %arg0, float %arg1) {\n"
1399 " %A = select nnan i1 %cond, float %arg0, float %arg1"
1400 " ret float %A\n"
1401 "}\n");
1402 expectKnownFPClass(~fcNan, std::nullopt);
1403 }
1404
1405 TEST_F(ComputeKnownFPClassTest, SelectNInf) {
1406 parseAssembly(
1407 "define float @test(i1 %cond, float %arg0, float %arg1) {\n"
1408 " %A = select ninf i1 %cond, float %arg0, float %arg1"
1409 " ret float %A\n"
1410 "}\n");
1411 expectKnownFPClass(~fcInf, std::nullopt);
1412 }
1413
1414 TEST_F(ComputeKnownFPClassTest, SelectNNaNNInf) {
1415 parseAssembly(
1416 "define float @test(i1 %cond, float %arg0, float %arg1) {\n"
1417 " %A = select nnan ninf i1 %cond, float %arg0, float %arg1"
1418 " ret float %A\n"
1419 "}\n");
1420 expectKnownFPClass(~(fcNan | fcInf), std::nullopt);
1421 }
1422
1423 TEST_F(ComputeKnownFPClassTest, SelectNoFPClassArgUnionAll) {
1424 parseAssembly(
1425 "define float @test(i1 %cond, float nofpclass(snan ninf nsub pzero pnorm) %arg0, float nofpclass(qnan nnorm nzero psub pinf) %arg1) {\n"
1426 " %A = select i1 %cond, float %arg0, float %arg1"
1427 " ret float %A\n"
1428 "}\n");
1429 expectKnownFPClass(fcAllFlags, std::nullopt);
1430 }
1431
1432 TEST_F(ComputeKnownFPClassTest, SelectNoFPClassArgNoNan) {
1433 parseAssembly(
1434 "define float @test(i1 %cond, float nofpclass(nan) %arg0, float nofpclass(nan) %arg1) {\n"
1435 " %A = select i1 %cond, float %arg0, float %arg1"
1436 " ret float %A\n"
1437 "}\n");
1438 expectKnownFPClass(~fcNan, std::nullopt);
1439 }
1440
1441 TEST_F(ComputeKnownFPClassTest, SelectNoFPClassArgNoPInf) {
1442 parseAssembly(
1443 "define float @test(i1 %cond, float nofpclass(inf) %arg0, float nofpclass(pinf) %arg1) {\n"
1444 " %A = select i1 %cond, float %arg0, float %arg1"
1445 " ret float %A\n"
1446 "}\n");
1447 expectKnownFPClass(~fcPosInf, std::nullopt);
1448 }
1449
1450 TEST_F(ComputeKnownFPClassTest, SelectNoFPClassArgNoNInf) {
1451 parseAssembly(
1452 "define float @test(i1 %cond, float nofpclass(ninf) %arg0, float nofpclass(inf) %arg1) {\n"
1453 " %A = select i1 %cond, float %arg0, float %arg1"
1454 " ret float %A\n"
1455 "}\n");
1456 expectKnownFPClass(~fcNegInf, std::nullopt);
1457 }
1458
1459 TEST_F(ComputeKnownFPClassTest, SelectNoFPClassCallSiteNoNan) {
1460 parseAssembly(
1461 "declare float @func()\n"
1462 "define float @test() {\n"
1463 " %A = call nofpclass(nan) float @func()\n"
1464 " ret float %A\n"
1465 "}\n");
1466 expectKnownFPClass(~fcNan, std::nullopt);
1467 }
1468
1469 TEST_F(ComputeKnownFPClassTest, SelectNoFPClassCallSiteNoZeros) {
1470 parseAssembly(
1471 "declare float @func()\n"
1472 "define float @test() {\n"
1473 " %A = call nofpclass(zero) float @func()\n"
1474 " ret float %A\n"
1475 "}\n");
1476 expectKnownFPClass(~fcZero, std::nullopt);
1477 }
1478
1479 TEST_F(ComputeKnownFPClassTest, SelectNoFPClassDeclarationNoNan) {
1480 parseAssembly(
1481 "declare nofpclass(nan) float @no_nans()\n"
1482 "define float @test() {\n"
1483 " %A = call float @no_nans()\n"
1484 " ret float %A\n"
1485 "}\n");
1486 expectKnownFPClass(~fcNan, std::nullopt);
1487 }
1488
1489 // Check nofpclass + ninf works on a callsite
1490 TEST_F(ComputeKnownFPClassTest, SelectNoFPClassCallSiteNoZerosNInfFlags) {
1491 parseAssembly(
1492 "declare float @func()\n"
1493 "define float @test() {\n"
1494 " %A = call ninf nofpclass(zero) float @func()\n"
1495 " ret float %A\n"
1496 "}\n");
1497 expectKnownFPClass(~(fcZero | fcInf), std::nullopt);
1498 }
1499
1500 TEST_F(ComputeKnownFPClassTest, FNegNInf) {
1501 parseAssembly(
1502 "define float @test(float %arg) {\n"
1503 " %A = fneg ninf float %arg"
1504 " ret float %A\n"
1505 "}\n");
1506 expectKnownFPClass(~fcInf, std::nullopt);
1507 }
1508
1509 TEST_F(ComputeKnownFPClassTest, FabsUnknown) {
1510 parseAssembly(
1511 "declare float @llvm.fabs.f32(float)"
1512 "define float @test(float %arg) {\n"
1513 " %A = call float @llvm.fabs.f32(float %arg)"
1514 " ret float %A\n"
1515 "}\n");
1516 expectKnownFPClass(fcPositive | fcNan, false);
1517 }
1518
1519 TEST_F(ComputeKnownFPClassTest, FNegFabsUnknown) {
1520 parseAssembly(
1521 "declare float @llvm.fabs.f32(float)"
1522 "define float @test(float %arg) {\n"
1523 " %fabs = call float @llvm.fabs.f32(float %arg)"
1524 " %A = fneg float %fabs"
1525 " ret float %A\n"
1526 "}\n");
1527 expectKnownFPClass(fcNegative | fcNan, true);
1528 }
1529
1530 TEST_F(ComputeKnownFPClassTest, NegFabsNInf) {
1531 parseAssembly(
1532 "declare float @llvm.fabs.f32(float)"
1533 "define float @test(float %arg) {\n"
1534 " %fabs = call ninf float @llvm.fabs.f32(float %arg)"
1535 " %A = fneg float %fabs"
1536 " ret float %A\n"
1537 "}\n");
1538 expectKnownFPClass((fcNegative & ~fcNegInf) | fcNan, true);
1539 }
1540
1541 TEST_F(ComputeKnownFPClassTest, FNegFabsNNaN) {
1542 parseAssembly(
1543 "declare float @llvm.fabs.f32(float)"
1544 "define float @test(float %arg) {\n"
1545 " %fabs = call nnan float @llvm.fabs.f32(float %arg)"
1546 " %A = fneg float %fabs"
1547 " ret float %A\n"
1548 "}\n");
1549 expectKnownFPClass(fcNegative, true);
1550 }
1551
1552 TEST_F(ComputeKnownFPClassTest, CopySignNNanSrc0) {
1553 parseAssembly(
1554 "declare float @llvm.fabs.f32(float)\n"
1555 "declare float @llvm.copysign.f32(float, float)\n"
1556 "define float @test(float %arg0, float %arg1) {\n"
1557 " %fabs = call nnan float @llvm.fabs.f32(float %arg0)"
1558 " %A = call float @llvm.copysign.f32(float %fabs, float %arg1)"
1559 " ret float %A\n"
1560 "}\n");
1561 expectKnownFPClass(~fcNan, std::nullopt);
1562 }
1563
1564 TEST_F(ComputeKnownFPClassTest, CopySignNInfSrc0_NegSign) {
1565 parseAssembly(
1566 "declare float @llvm.log.f32(float)\n"
1567 "declare float @llvm.copysign.f32(float, float)\n"
1568 "define float @test(float %arg0, float %arg1) {\n"
1569 " %ninf = call ninf float @llvm.log.f32(float %arg0)"
1570 " %A = call float @llvm.copysign.f32(float %ninf, float -1.0)"
1571 " ret float %A\n"
1572 "}\n");
1573 expectKnownFPClass(fcNegFinite | fcNan, true);
1574 }
1575
1576 TEST_F(ComputeKnownFPClassTest, CopySignNInfSrc0_PosSign) {
1577 parseAssembly(
1578 "declare float @llvm.sqrt.f32(float)\n"
1579 "declare float @llvm.copysign.f32(float, float)\n"
1580 "define float @test(float %arg0, float %arg1) {\n"
1581 " %ninf = call ninf float @llvm.sqrt.f32(float %arg0)"
1582 " %A = call float @llvm.copysign.f32(float %ninf, float 1.0)"
1583 " ret float %A\n"
1584 "}\n");
1585 expectKnownFPClass(fcPosFinite | fcNan, false);
1586 }
1587
1588 TEST_F(ComputeKnownFPClassTest, UIToFP) {
1589 parseAssembly(
1590 "define float @test(i32 %arg0, i16 %arg1) {\n"
1591 " %A = uitofp i32 %arg0 to float"
1592 " %A2 = uitofp i16 %arg1 to half"
1593 " ret float %A\n"
1594 "}\n");
1595 expectKnownFPClass(fcPosFinite & ~fcSubnormal, false, A);
1596 expectKnownFPClass(fcPositive & ~fcSubnormal, false, A2);
1597 }
1598
1599 TEST_F(ComputeKnownFPClassTest, SIToFP) {
1600 parseAssembly(
1601 "define float @test(i32 %arg0, i16 %arg1, i17 %arg2) {\n"
1602 " %A = sitofp i32 %arg0 to float"
1603 " %A2 = sitofp i16 %arg1 to half"
1604 " %A3 = sitofp i17 %arg2 to half"
1605 " ret float %A\n"
1606 "}\n");
1607 expectKnownFPClass(fcFinite & ~fcNegZero & ~fcSubnormal, std::nullopt, A);
1608 expectKnownFPClass(fcFinite & ~fcNegZero & ~fcSubnormal, std::nullopt, A2);
1609 expectKnownFPClass(~(fcNan | fcNegZero | fcSubnormal), std::nullopt, A3);
1610 }
1611
1612 TEST_F(ComputeKnownFPClassTest, FAdd) {
1613 parseAssembly(
1614 "define float @test(float nofpclass(nan inf) %nnan.ninf, float nofpclass(nan) %nnan, float nofpclass(qnan) %no.qnan, float %unknown) {\n"
1615 " %A = fadd float %nnan, %nnan.ninf"
1616 " %A2 = fadd float %nnan.ninf, %nnan"
1617 " %A3 = fadd float %nnan.ninf, %unknown"
1618 " %A4 = fadd float %nnan.ninf, %no.qnan"
1619 " %A5 = fadd float %nnan, %nnan"
1620 " ret float %A\n"
1621 "}\n");
1622 expectKnownFPClass(fcFinite | fcInf, std::nullopt, A);
1623 expectKnownFPClass(fcFinite | fcInf, std::nullopt, A2);
1624 expectKnownFPClass(fcAllFlags, std::nullopt, A3);
1625 expectKnownFPClass(fcAllFlags, std::nullopt, A4);
1626 expectKnownFPClass(fcAllFlags, std::nullopt, A5);
1627 }
1628
1629 TEST_F(ComputeKnownFPClassTest, FSub) {
1630 parseAssembly(
1631 "define float @test(float nofpclass(nan inf) %nnan.ninf, float nofpclass(nan) %nnan, float nofpclass(qnan) %no.qnan, float %unknown) {\n"
1632 " %A = fsub float %nnan, %nnan.ninf"
1633 " %A2 = fsub float %nnan.ninf, %nnan"
1634 " %A3 = fsub float %nnan.ninf, %unknown"
1635 " %A4 = fsub float %nnan.ninf, %no.qnan"
1636 " %A5 = fsub float %nnan, %nnan"
1637 " ret float %A\n"
1638 "}\n");
1639 expectKnownFPClass(fcFinite | fcInf, std::nullopt, A);
1640 expectKnownFPClass(fcFinite | fcInf, std::nullopt, A2);
1641 expectKnownFPClass(fcAllFlags, std::nullopt, A3);
1642 expectKnownFPClass(fcAllFlags, std::nullopt, A4);
1643 expectKnownFPClass(fcAllFlags, std::nullopt, A5);
1644 }
1645
1646 TEST_F(ComputeKnownFPClassTest, FMul) {
1647 parseAssembly(
1648 "define float @test(float nofpclass(nan inf) %nnan.ninf0, float nofpclass(nan inf) %nnan.ninf1, float nofpclass(nan) %nnan, float nofpclass(qnan) %no.qnan, float %unknown) {\n"
1649 " %A = fmul float %nnan.ninf0, %nnan.ninf1"
1650 " %A2 = fmul float %nnan.ninf0, %nnan"
1651 " %A3 = fmul float %nnan, %nnan.ninf0"
1652 " %A4 = fmul float %nnan.ninf0, %no.qnan"
1653 " %A5 = fmul float %nnan, %nnan"
1654 " ret float %A\n"
1655 "}\n");
1656 expectKnownFPClass(fcFinite | fcInf, std::nullopt, A);
1657 expectKnownFPClass(fcAllFlags, std::nullopt, A2);
1658 expectKnownFPClass(fcAllFlags, std::nullopt, A3);
1659 expectKnownFPClass(fcAllFlags, std::nullopt, A4);
1660 expectKnownFPClass(fcPositive | fcNan, std::nullopt, A5);
1661 }
1662
1663 TEST_F(ComputeKnownFPClassTest, FMulNoZero) {
1664 parseAssembly(
1665 "define float @test(float nofpclass(zero) %no.zero, float nofpclass(zero nan) %no.zero.nan0, float nofpclass(zero nan) %no.zero.nan1, float nofpclass(nzero nan) %no.negzero.nan, float nofpclass(pzero nan) %no.poszero.nan, float nofpclass(inf nan) %no.inf.nan, float nofpclass(inf) %no.inf, float nofpclass(nan) %no.nan) {\n"
1666 " %A = fmul float %no.zero.nan0, %no.zero.nan1"
1667 " %A2 = fmul float %no.zero, %no.zero"
1668 " %A3 = fmul float %no.poszero.nan, %no.zero.nan0"
1669 " %A4 = fmul float %no.nan, %no.zero"
1670 " %A5 = fmul float %no.zero, %no.inf"
1671 " %A6 = fmul float %no.zero.nan0, %no.nan"
1672 " %A7 = fmul float %no.nan, %no.zero.nan0"
1673 " ret float %A\n"
1674 "}\n");
1675 expectKnownFPClass(fcFinite | fcInf, std::nullopt, A);
1676 expectKnownFPClass(fcPositive | fcNan, std::nullopt, A2);
1677 expectKnownFPClass(fcAllFlags, std::nullopt, A3);
1678 expectKnownFPClass(fcAllFlags, std::nullopt, A4);
1679 expectKnownFPClass(fcAllFlags, std::nullopt, A5);
1680 expectKnownFPClass(fcAllFlags, std::nullopt, A6);
1681 expectKnownFPClass(fcAllFlags, std::nullopt, A7);
1682 }
1683
1684 TEST_F(ComputeKnownFPClassTest, MinimumNumSignBit) {
1685 parseAssembly(
1686 R"(
1687 define float @test(
1688 float %unknown,
1689 float nofpclass(nan) %nnan,
1690 float nofpclass(nan pinf pnorm psub pzero) %nnan.nopos,
1691 float nofpclass(nan ninf nnorm nsub nzero) %nnan.noneg,
1692 float nofpclass(ninf nnorm nsub nzero) %noneg,
1693 float nofpclass(pinf pnorm psub pzero) %nopos) {
1694 %A = call float @llvm.minimumnum.f32(float %nnan.nopos, float %unknown)
1695 %A2 = call float @llvm.minimumnum.f32(float %unknown, float %nnan.nopos)
1696 %A3 = call float @llvm.minimumnum.f32(float %nnan.noneg, float %unknown)
1697 %A4 = call float @llvm.minimumnum.f32(float %unknown, float %nnan.noneg)
1698 %A5 = call float @llvm.minimumnum.f32(float %nnan.nopos, float %nnan.noneg)
1699 %A6 = call float @llvm.minimumnum.f32(float %nopos, float %nnan.noneg)
1700 %A7 = call float @llvm.minimumnum.f32(float %nnan.nopos, float %noneg)
1701 ret float %A
1702 })");
1703 expectKnownFPClass(fcNegative, true, A);
1704 expectKnownFPClass(fcNegative, true, A2);
1705 expectKnownFPClass(~fcNan, std::nullopt, A3);
1706 expectKnownFPClass(~fcNan, std::nullopt, A4);
1707 expectKnownFPClass(fcNegative, true, A5);
1708 expectKnownFPClass(~fcNan, std::nullopt, A6);
1709 expectKnownFPClass(fcNegative, true, A7);
1710 }
1711
1712 TEST_F(ComputeKnownFPClassTest, MaximumNumSignBit) {
1713 parseAssembly(
1714 R"(
1715 define float @test(
1716 float %unknown,
1717 float nofpclass(nan) %nnan,
1718 float nofpclass(nan pinf pnorm psub pzero) %nnan.nopos,
1719 float nofpclass(nan ninf nnorm nsub nzero) %nnan.noneg,
1720 float nofpclass(ninf nnorm nsub nzero) %noneg,
1721 float nofpclass(pinf pnorm psub pzero) %nopos) {
1722 %A = call float @llvm.maximumnum.f32(float %nnan.noneg, float %unknown)
1723 %A2 = call float @llvm.maximumnum.f32(float %unknown, float %nnan.noneg)
1724 %A3 = call float @llvm.maximumnum.f32(float %nnan.nopos, float %unknown)
1725 %A4 = call float @llvm.maximumnum.f32(float %unknown, float %nnan.nopos)
1726 %A5 = call float @llvm.maximumnum.f32(float %nnan.noneg, float %nnan.nopos)
1727 %A6 = call float @llvm.maximumnum.f32(float %noneg, float %nnan.nopos)
1728 %A7 = call float @llvm.maximumnum.f32(float %nnan.noneg, float %nopos)
1729 ret float %A
1730 })");
1731 expectKnownFPClass(fcPositive, false, A);
1732 expectKnownFPClass(fcPositive, false, A2);
1733 expectKnownFPClass(~fcNan, std::nullopt, A3);
1734 expectKnownFPClass(~fcNan, std::nullopt, A4);
1735 expectKnownFPClass(fcPositive, false, A5);
1736 expectKnownFPClass(~fcNan, std::nullopt, A6);
1737 expectKnownFPClass(fcPositive, false, A7);
1738 }
1739
1740 TEST_F(ComputeKnownFPClassTest, Phi) {
1741 parseAssembly(
1742 "define float @test(i1 %cond, float nofpclass(nan inf) %arg0, float nofpclass(nan) %arg1) {\n"
1743 "entry:\n"
1744 " br i1 %cond, label %bb0, label %bb1\n"
1745 "bb0:\n"
1746 " br label %ret\n"
1747 "bb1:\n"
1748 " br label %ret\n"
1749 "ret:\n"
1750 " %A = phi float [ %arg0, %bb0 ], [ %arg1, %bb1 ]\n"
1751 " ret float %A\n"
1752 "}\n");
1753 expectKnownFPClass(~fcNan, std::nullopt);
1754 }
1755
1756 TEST_F(ComputeKnownFPClassTest, PhiKnownSignFalse) {
1757 parseAssembly(
1758 "declare float @llvm.fabs.f32(float)"
1759 "define float @test(i1 %cond, float nofpclass(nan) %arg0, float nofpclass(nan) %arg1) {\n"
1760 "entry:\n"
1761 " br i1 %cond, label %bb0, label %bb1\n"
1762 "bb0:\n"
1763 " %fabs.arg0 = call float @llvm.fabs.f32(float %arg0)\n"
1764 " br label %ret\n"
1765 "bb1:\n"
1766 " %fabs.arg1 = call float @llvm.fabs.f32(float %arg1)\n"
1767 " br label %ret\n"
1768 "ret:\n"
1769 " %A = phi float [ %fabs.arg0, %bb0 ], [ %fabs.arg1, %bb1 ]\n"
1770 " ret float %A\n"
1771 "}\n");
1772 expectKnownFPClass(fcPositive, false);
1773 }
1774
1775 TEST_F(ComputeKnownFPClassTest, PhiKnownSignTrue) {
1776 parseAssembly(
1777 "declare float @llvm.fabs.f32(float)"
1778 "define float @test(i1 %cond, float nofpclass(nan) %arg0, float %arg1) {\n"
1779 "entry:\n"
1780 " br i1 %cond, label %bb0, label %bb1\n"
1781 "bb0:\n"
1782 " %fabs.arg0 = call float @llvm.fabs.f32(float %arg0)\n"
1783 " %fneg.fabs.arg0 = fneg float %fabs.arg0\n"
1784 " br label %ret\n"
1785 "bb1:\n"
1786 " %fabs.arg1 = call float @llvm.fabs.f32(float %arg1)\n"
1787 " %fneg.fabs.arg1 = fneg float %fabs.arg1\n"
1788 " br label %ret\n"
1789 "ret:\n"
1790 " %A = phi float [ %fneg.fabs.arg0, %bb0 ], [ %fneg.fabs.arg1, %bb1 ]\n"
1791 " ret float %A\n"
1792 "}\n");
1793 expectKnownFPClass(fcNegative | fcNan, true);
1794 }
1795
1796 TEST_F(ComputeKnownFPClassTest, UnreachablePhi) {
1797 parseAssembly(
1798 "define float @test(float %arg) {\n"
1799 "entry:\n"
1800 " ret float 0.0\n"
1801 "unreachable:\n"
1802 " %A = phi float\n"
1803 " ret float %A\n"
1804 "}\n");
1805 expectKnownFPClass(fcAllFlags, std::nullopt);
1806 }
1807
1808 TEST_F(ComputeKnownFPClassTest, SelfPhiOnly) {
1809 parseAssembly(
1810 "define float @test(float %arg) {\n"
1811 "entry:\n"
1812 " ret float 0.0\n"
1813 "loop:\n"
1814 " %A = phi float [ %A, %loop ]\n"
1815 " br label %loop\n"
1816 "}\n");
1817 expectKnownFPClass(fcAllFlags, std::nullopt);
1818 }
1819
1820 TEST_F(ComputeKnownFPClassTest, SelfPhiFirstArg) {
1821 parseAssembly(
1822 "define float @test(i1 %cond, float nofpclass(inf) %arg) {\n"
1823 "entry:\n"
1824 " br i1 %cond, label %loop, label %ret\n"
1825 "loop:\n"
1826 " %A = phi float [ %arg, %entry ], [ %A, %loop ]\n"
1827 " br label %loop\n"
1828 "ret:\n"
1829 " ret float %A"
1830 "}\n");
1831 expectKnownFPClass(~fcInf, std::nullopt);
1832 }
1833
1834 TEST_F(ComputeKnownFPClassTest, SelfPhiSecondArg) {
1835 parseAssembly(
1836 "define float @test(i1 %cond, float nofpclass(inf) %arg) {\n"
1837 "entry:\n"
1838 " br i1 %cond, label %loop, label %ret\n"
1839 "loop:\n"
1840 " %A = phi float [ %A, %loop ], [ %arg, %entry ]\n"
1841 " br label %loop\n"
1842 "ret:\n"
1843 " ret float %A"
1844 "}\n");
1845 expectKnownFPClass(~fcInf, std::nullopt);
1846 }
1847
1848 TEST_F(ComputeKnownFPClassTest, CannotBeOrderedLessThanZero) {
1849 parseAssembly("define float @test(float %arg) {\n"
1850 " %A = fmul float %arg, %arg"
1851 " ret float %A\n"
1852 "}\n");
1853
1854 Type *FPTy = Type::getDoubleTy(M->getContext());
1855 const DataLayout &DL = M->getDataLayout();
1856
1857 EXPECT_TRUE(
1858 computeKnownFPClass(ConstantFP::getZero(FPTy, /*Negative=*/false), DL)
1859 .cannotBeOrderedLessThanZero());
1860 EXPECT_TRUE(
1861 computeKnownFPClass(ConstantFP::getZero(FPTy, /*Negative=*/true), DL)
1862 .cannotBeOrderedLessThanZero());
1863
1864 EXPECT_TRUE(computeKnownFPClass(ConstantFP::getInfinity(FPTy, false), DL)
1865 .cannotBeOrderedLessThanZero());
1866 EXPECT_FALSE(computeKnownFPClass(ConstantFP::getInfinity(FPTy, true), DL)
1867 .cannotBeOrderedLessThanZero());
1868
1869 EXPECT_TRUE(computeKnownFPClass(ConstantFP::get(FPTy, 1.0), DL)
1870 .cannotBeOrderedLessThanZero());
1871 EXPECT_FALSE(computeKnownFPClass(ConstantFP::get(FPTy, -1.0), DL)
1872 .cannotBeOrderedLessThanZero());
1873
1874 EXPECT_TRUE(
1875 computeKnownFPClass(
1876 ConstantFP::get(FPTy, APFloat::getSmallest(FPTy->getFltSemantics(),
1877 /*Negative=*/false)),
1878 DL)
1879 .cannotBeOrderedLessThanZero());
1880 EXPECT_FALSE(
1881 computeKnownFPClass(
1882 ConstantFP::get(FPTy, APFloat::getSmallest(FPTy->getFltSemantics(),
1883 /*Negative=*/true)),
1884 DL)
1885 .cannotBeOrderedLessThanZero());
1886
1887 EXPECT_TRUE(
1888 computeKnownFPClass(ConstantFP::getQNaN(FPTy, /*Negative=*/false), DL)
1889 .cannotBeOrderedLessThanZero());
1890 EXPECT_TRUE(
1891 computeKnownFPClass(ConstantFP::getQNaN(FPTy, /*Negative=*/true), DL)
1892 .cannotBeOrderedLessThanZero());
1893 EXPECT_TRUE(
1894 computeKnownFPClass(ConstantFP::getSNaN(FPTy, /*Negative=*/false), DL)
1895 .cannotBeOrderedLessThanZero());
1896 EXPECT_TRUE(
1897 computeKnownFPClass(ConstantFP::getSNaN(FPTy, /*Negative=*/true), DL)
1898 .cannotBeOrderedLessThanZero());
1899 }
1900
1901 TEST_F(ComputeKnownFPClassTest, FCmpToClassTest_OrdNan) {
1902 parseAssembly("define i1 @test(double %arg) {\n"
1903 " %A = fcmp ord double %arg, 0x7FF8000000000000"
1904 " %A2 = fcmp uno double %arg, 0x7FF8000000000000"
1905 " %A3 = fcmp oeq double %arg, 0x7FF8000000000000"
1906 " %A4 = fcmp ueq double %arg, 0x7FF8000000000000"
1907 " ret i1 %A\n"
1908 "}\n");
1909
1910 auto [OrdVal, OrdClass] = fcmpToClassTest(
1911 CmpInst::FCMP_ORD, *A->getFunction(), A->getOperand(0), A->getOperand(1));
1912 EXPECT_EQ(A->getOperand(0), OrdVal);
1913 EXPECT_EQ(fcNone, OrdClass);
1914
1915 auto [UnordVal, UnordClass] =
1916 fcmpToClassTest(CmpInst::FCMP_UNO, *A2->getFunction(), A2->getOperand(0),
1917 A2->getOperand(1));
1918 EXPECT_EQ(A2->getOperand(0), UnordVal);
1919 EXPECT_EQ(fcAllFlags, UnordClass);
1920
1921 auto [OeqVal, OeqClass] =
1922 fcmpToClassTest(CmpInst::FCMP_OEQ, *A3->getFunction(), A3->getOperand(0),
1923 A3->getOperand(1));
1924 EXPECT_EQ(A3->getOperand(0), OeqVal);
1925 EXPECT_EQ(fcNone, OeqClass);
1926
1927 auto [UeqVal, UeqClass] =
1928 fcmpToClassTest(CmpInst::FCMP_UEQ, *A3->getFunction(), A3->getOperand(0),
1929 A3->getOperand(1));
1930 EXPECT_EQ(A3->getOperand(0), UeqVal);
1931 EXPECT_EQ(fcAllFlags, UeqClass);
1932 }
1933
1934 TEST_F(ComputeKnownFPClassTest, FCmpToClassTest_NInf) {
1935 parseAssembly("define i1 @test(double %arg) {\n"
1936 " %A = fcmp olt double %arg, 0xFFF0000000000000"
1937 " %A2 = fcmp uge double %arg, 0xFFF0000000000000"
1938 " %A3 = fcmp ogt double %arg, 0xFFF0000000000000"
1939 " %A4 = fcmp ule double %arg, 0xFFF0000000000000"
1940 " %A5 = fcmp oge double %arg, 0xFFF0000000000000"
1941 " %A6 = fcmp ult double %arg, 0xFFF0000000000000"
1942 " ret i1 %A\n"
1943 "}\n");
1944
1945 auto [OltVal, OltClass] = fcmpToClassTest(
1946 CmpInst::FCMP_OLT, *A->getFunction(), A->getOperand(0), A->getOperand(1));
1947 EXPECT_EQ(A->getOperand(0), OltVal);
1948 EXPECT_EQ(fcNone, OltClass);
1949
1950 auto [UgeVal, UgeClass] =
1951 fcmpToClassTest(CmpInst::FCMP_UGE, *A2->getFunction(), A2->getOperand(0),
1952 A2->getOperand(1));
1953 EXPECT_EQ(A2->getOperand(0), UgeVal);
1954 EXPECT_EQ(fcAllFlags, UgeClass);
1955
1956 auto [OgtVal, OgtClass] =
1957 fcmpToClassTest(CmpInst::FCMP_OGT, *A3->getFunction(), A3->getOperand(0),
1958 A3->getOperand(1));
1959 EXPECT_EQ(A3->getOperand(0), OgtVal);
1960 EXPECT_EQ(~(fcNegInf | fcNan), OgtClass);
1961
1962 auto [UleVal, UleClass] =
1963 fcmpToClassTest(CmpInst::FCMP_ULE, *A4->getFunction(), A4->getOperand(0),
1964 A4->getOperand(1));
1965 EXPECT_EQ(A4->getOperand(0), UleVal);
1966 EXPECT_EQ(fcNegInf | fcNan, UleClass);
1967
1968 auto [OgeVal, OgeClass] =
1969 fcmpToClassTest(CmpInst::FCMP_OGE, *A5->getFunction(), A5->getOperand(0),
1970 A5->getOperand(1));
1971 EXPECT_EQ(A5->getOperand(0), OgeVal);
1972 EXPECT_EQ(~fcNan, OgeClass);
1973
1974 auto [UltVal, UltClass] =
1975 fcmpToClassTest(CmpInst::FCMP_ULT, *A6->getFunction(), A6->getOperand(0),
1976 A6->getOperand(1));
1977 EXPECT_EQ(A6->getOperand(0), UltVal);
1978 EXPECT_EQ(fcNan, UltClass);
1979 }
1980
1981 TEST_F(ComputeKnownFPClassTest, FCmpToClassTest_FabsNInf) {
1982 parseAssembly("declare double @llvm.fabs.f64(double)\n"
1983 "define i1 @test(double %arg) {\n"
1984 " %fabs.arg = call double @llvm.fabs.f64(double %arg)\n"
1985 " %A = fcmp olt double %fabs.arg, 0xFFF0000000000000"
1986 " %A2 = fcmp uge double %fabs.arg, 0xFFF0000000000000"
1987 " %A3 = fcmp ogt double %fabs.arg, 0xFFF0000000000000"
1988 " %A4 = fcmp ule double %fabs.arg, 0xFFF0000000000000"
1989 " %A5 = fcmp oge double %fabs.arg, 0xFFF0000000000000"
1990 " %A6 = fcmp ult double %fabs.arg, 0xFFF0000000000000"
1991 " ret i1 %A\n"
1992 "}\n");
1993
1994 Value *ArgVal = F->getArg(0);
1995
1996 auto [OltVal, OltClass] = fcmpToClassTest(
1997 CmpInst::FCMP_OLT, *A->getFunction(), A->getOperand(0), A->getOperand(1));
1998 EXPECT_EQ(ArgVal, OltVal);
1999 EXPECT_EQ(fcNone, OltClass);
2000
2001 auto [UgeVal, UgeClass] =
2002 fcmpToClassTest(CmpInst::FCMP_UGE, *A2->getFunction(), A2->getOperand(0),
2003 A2->getOperand(1));
2004 EXPECT_EQ(ArgVal, UgeVal);
2005 EXPECT_EQ(fcAllFlags, UgeClass);
2006
2007 auto [OgtVal, OgtClass] =
2008 fcmpToClassTest(CmpInst::FCMP_OGT, *A3->getFunction(), A3->getOperand(0),
2009 A3->getOperand(1));
2010 EXPECT_EQ(ArgVal, OgtVal);
2011 EXPECT_EQ(~fcNan, OgtClass);
2012
2013 auto [UleVal, UleClass] =
2014 fcmpToClassTest(CmpInst::FCMP_ULE, *A4->getFunction(), A4->getOperand(0),
2015 A4->getOperand(1));
2016 EXPECT_EQ(ArgVal, UleVal);
2017 EXPECT_EQ(fcNan, UleClass);
2018
2019 auto [OgeVal, OgeClass] =
2020 fcmpToClassTest(CmpInst::FCMP_OGE, *A5->getFunction(), A5->getOperand(0),
2021 A5->getOperand(1));
2022 EXPECT_EQ(ArgVal, OgeVal);
2023 EXPECT_EQ(~fcNan, OgeClass);
2024
2025 auto [UltVal, UltClass] =
2026 fcmpToClassTest(CmpInst::FCMP_ULT, *A6->getFunction(), A6->getOperand(0),
2027 A6->getOperand(1));
2028 EXPECT_EQ(ArgVal, UltVal);
2029 EXPECT_EQ(fcNan, UltClass);
2030 }
2031
2032 TEST_F(ComputeKnownFPClassTest, FCmpToClassTest_PInf) {
2033 parseAssembly("define i1 @test(double %arg) {\n"
2034 " %A = fcmp ogt double %arg, 0x7FF0000000000000"
2035 " %A2 = fcmp ule double %arg, 0x7FF0000000000000"
2036 " %A3 = fcmp ole double %arg, 0x7FF0000000000000"
2037 " %A4 = fcmp ugt double %arg, 0x7FF0000000000000"
2038 " ret i1 %A\n"
2039 "}\n");
2040
2041 auto [OgtVal, OgtClass] = fcmpToClassTest(
2042 CmpInst::FCMP_OGT, *A->getFunction(), A->getOperand(0), A->getOperand(1));
2043 EXPECT_EQ(A->getOperand(0), OgtVal);
2044 EXPECT_EQ(fcNone, OgtClass);
2045
2046 auto [UleVal, UleClass] =
2047 fcmpToClassTest(CmpInst::FCMP_ULE, *A2->getFunction(), A2->getOperand(0),
2048 A2->getOperand(1));
2049 EXPECT_EQ(A2->getOperand(0), UleVal);
2050 EXPECT_EQ(fcAllFlags, UleClass);
2051
2052 auto [OleVal, OleClass] =
2053 fcmpToClassTest(CmpInst::FCMP_OLE, *A3->getFunction(), A3->getOperand(0),
2054 A3->getOperand(1));
2055 EXPECT_EQ(A->getOperand(0), OleVal);
2056 EXPECT_EQ(~fcNan, OleClass);
2057
2058 auto [UgtVal, UgtClass] =
2059 fcmpToClassTest(CmpInst::FCMP_UGT, *A4->getFunction(), A4->getOperand(0),
2060 A4->getOperand(1));
2061 EXPECT_EQ(A4->getOperand(0), UgtVal);
2062 EXPECT_EQ(fcNan, UgtClass);
2063 }
2064
2065 TEST_F(ComputeKnownFPClassTest, SqrtNszSignBit) {
2066 parseAssembly(
2067 "declare float @llvm.sqrt.f32(float)\n"
2068 "define float @test(float %arg, float nofpclass(nan) %arg.nnan) {\n"
2069 " %A = call float @llvm.sqrt.f32(float %arg)\n"
2070 " %A2 = call nsz float @llvm.sqrt.f32(float %arg)\n"
2071 " %A3 = call float @llvm.sqrt.f32(float %arg.nnan)\n"
2072 " %A4 = call nsz float @llvm.sqrt.f32(float %arg.nnan)\n"
2073 " ret float %A\n"
2074 "}\n");
2075
2076 const FPClassTest SqrtMask = fcPositive | fcNegZero | fcNan;
2077 const FPClassTest NszSqrtMask = fcPositive | fcNan;
2078
2079 {
2080 KnownFPClass UseInstrInfo =
2081 computeKnownFPClass(A, M->getDataLayout(), fcAllFlags, nullptr, nullptr,
2082 nullptr, nullptr, /*UseInstrInfo=*/true);
2083 EXPECT_EQ(SqrtMask, UseInstrInfo.KnownFPClasses);
2084 EXPECT_EQ(std::nullopt, UseInstrInfo.SignBit);
2085
2086 KnownFPClass NoUseInstrInfo =
2087 computeKnownFPClass(A, M->getDataLayout(), fcAllFlags, nullptr, nullptr,
2088 nullptr, nullptr, /*UseInstrInfo=*/false);
2089 EXPECT_EQ(SqrtMask, NoUseInstrInfo.KnownFPClasses);
2090 EXPECT_EQ(std::nullopt, NoUseInstrInfo.SignBit);
2091 }
2092
2093 {
2094 KnownFPClass UseInstrInfoNSZ =
2095 computeKnownFPClass(A2, M->getDataLayout(), fcAllFlags, nullptr,
2096 nullptr, nullptr, nullptr, /*UseInstrInfo=*/true);
2097 EXPECT_EQ(NszSqrtMask, UseInstrInfoNSZ.KnownFPClasses);
2098 EXPECT_EQ(std::nullopt, UseInstrInfoNSZ.SignBit);
2099
2100 KnownFPClass NoUseInstrInfoNSZ =
2101 computeKnownFPClass(A2, M->getDataLayout(), fcAllFlags, nullptr,
2102 nullptr, nullptr, nullptr, /*UseInstrInfo=*/false);
2103 EXPECT_EQ(SqrtMask, NoUseInstrInfoNSZ.KnownFPClasses);
2104 EXPECT_EQ(std::nullopt, NoUseInstrInfoNSZ.SignBit);
2105 }
2106
2107 {
2108 KnownFPClass UseInstrInfoNoNan =
2109 computeKnownFPClass(A3, M->getDataLayout(), fcAllFlags, nullptr,
2110 nullptr, nullptr, nullptr, /*UseInstrInfo=*/true);
2111 EXPECT_EQ(fcPositive | fcNegZero | fcQNan,
2112 UseInstrInfoNoNan.KnownFPClasses);
2113 EXPECT_EQ(std::nullopt, UseInstrInfoNoNan.SignBit);
2114
2115 KnownFPClass NoUseInstrInfoNoNan =
2116 computeKnownFPClass(A3, M->getDataLayout(), fcAllFlags, nullptr,
2117 nullptr, nullptr, nullptr, /*UseInstrInfo=*/false);
2118 EXPECT_EQ(fcPositive | fcNegZero | fcQNan,
2119 NoUseInstrInfoNoNan.KnownFPClasses);
2120 EXPECT_EQ(std::nullopt, NoUseInstrInfoNoNan.SignBit);
2121 }
2122
2123 {
2124 KnownFPClass UseInstrInfoNSZNoNan =
2125 computeKnownFPClass(A4, M->getDataLayout(), fcAllFlags, nullptr,
2126 nullptr, nullptr, nullptr, /*UseInstrInfo=*/true);
2127 EXPECT_EQ(fcPositive | fcQNan, UseInstrInfoNSZNoNan.KnownFPClasses);
2128 EXPECT_EQ(std::nullopt, UseInstrInfoNSZNoNan.SignBit);
2129
2130 KnownFPClass NoUseInstrInfoNSZNoNan =
2131 computeKnownFPClass(A4, M->getDataLayout(), fcAllFlags, nullptr,
2132 nullptr, nullptr, nullptr, /*UseInstrInfo=*/false);
2133 EXPECT_EQ(fcPositive | fcNegZero | fcQNan,
2134 NoUseInstrInfoNSZNoNan.KnownFPClasses);
2135 EXPECT_EQ(std::nullopt, NoUseInstrInfoNSZNoNan.SignBit);
2136 }
2137 }
2138
2139 TEST_F(ComputeKnownFPClassTest, Constants) {
2140 parseAssembly("declare float @func()\n"
2141 "define float @test() {\n"
2142 " %A = call float @func()\n"
2143 " ret float %A\n"
2144 "}\n");
2145
2146 Type *F32 = Type::getFloatTy(Context);
2147 Type *V4F32 = FixedVectorType::get(F32, 4);
2148
2149 {
2150 KnownFPClass ConstAggZero = computeKnownFPClass(
2151 ConstantAggregateZero::get(V4F32), M->getDataLayout(), fcAllFlags);
2152
2153 EXPECT_EQ(fcPosZero, ConstAggZero.KnownFPClasses);
2154 ASSERT_TRUE(ConstAggZero.SignBit);
2155 EXPECT_FALSE(*ConstAggZero.SignBit);
2156 }
2157
2158 {
2159 KnownFPClass Undef = computeKnownFPClass(UndefValue::get(F32),
2160 M->getDataLayout(), fcAllFlags);
2161 EXPECT_EQ(fcAllFlags, Undef.KnownFPClasses);
2162 EXPECT_FALSE(Undef.SignBit);
2163 }
2164
2165 {
2166 KnownFPClass Poison = computeKnownFPClass(PoisonValue::get(F32),
2167 M->getDataLayout(), fcAllFlags);
2168 EXPECT_EQ(fcNone, Poison.KnownFPClasses);
2169 ASSERT_TRUE(Poison.SignBit);
2170 EXPECT_FALSE(*Poison.SignBit);
2171 }
2172
2173 {
2174 // Assume the poison element should be 0.
2175 Constant *ZeroF32 = ConstantFP::getZero(F32);
2176 Constant *PoisonF32 = PoisonValue::get(F32);
2177
2178 KnownFPClass PartiallyPoison =
2179 computeKnownFPClass(ConstantVector::get({ZeroF32, PoisonF32}),
2180 M->getDataLayout(), fcAllFlags);
2181 EXPECT_EQ(fcPosZero, PartiallyPoison.KnownFPClasses);
2182 ASSERT_TRUE(PartiallyPoison.SignBit);
2183 EXPECT_FALSE(*PartiallyPoison.SignBit);
2184 }
2185
2186 {
2187 // Assume the poison element should be 1.
2188 Constant *NegZeroF32 = ConstantFP::getZero(F32, true);
2189 Constant *PoisonF32 = PoisonValue::get(F32);
2190
2191 KnownFPClass PartiallyPoison =
2192 computeKnownFPClass(ConstantVector::get({NegZeroF32, PoisonF32}),
2193 M->getDataLayout(), fcAllFlags);
2194 EXPECT_EQ(fcNegZero, PartiallyPoison.KnownFPClasses);
2195 ASSERT_TRUE(PartiallyPoison.SignBit);
2196 EXPECT_TRUE(*PartiallyPoison.SignBit);
2197 }
2198
2199 {
2200 // Assume the poison element should be 1.
2201 Constant *NegZeroF32 = ConstantFP::getZero(F32, true);
2202 Constant *PoisonF32 = PoisonValue::get(F32);
2203
2204 KnownFPClass PartiallyPoison =
2205 computeKnownFPClass(ConstantVector::get({PoisonF32, NegZeroF32}),
2206 M->getDataLayout(), fcAllFlags);
2207 EXPECT_EQ(fcNegZero, PartiallyPoison.KnownFPClasses);
2208 EXPECT_TRUE(PartiallyPoison.SignBit);
2209 }
2210 }
2211
2212 TEST_F(ComputeKnownFPClassTest, fcmpImpliesClass_fabs_zero) {
2213 parseAssembly("define float @test(float %x) {\n"
2214 " %A = call float @llvm.fabs.f32(float %x)\n"
2215 " ret float %A\n"
2216 "}\n");
2217 EXPECT_EQ(std::get<1>(fcmpImpliesClass(FCmpInst::FCMP_OEQ, *F, A, fcZero)),
2218 fcZero);
2219 EXPECT_EQ(std::get<1>(fcmpImpliesClass(FCmpInst::FCMP_UEQ, *F, A, fcZero)),
2220 fcZero | fcNan);
2221 EXPECT_EQ(std::get<1>(fcmpImpliesClass(FCmpInst::FCMP_UNE, *F, A, fcZero)),
2222 ~fcZero);
2223 EXPECT_EQ(std::get<1>(fcmpImpliesClass(FCmpInst::FCMP_ONE, *F, A, fcZero)),
2224 ~fcNan & ~fcZero);
2225 EXPECT_EQ(std::get<1>(fcmpImpliesClass(FCmpInst::FCMP_ORD, *F, A, fcZero)),
2226 ~fcNan);
2227 EXPECT_EQ(std::get<1>(fcmpImpliesClass(FCmpInst::FCMP_UNO, *F, A, fcZero)),
2228 fcNan);
2229 EXPECT_EQ(std::get<1>(fcmpImpliesClass(FCmpInst::FCMP_OGT, *F, A, fcZero)),
2230 fcSubnormal | fcNormal | fcInf);
2231 EXPECT_EQ(std::get<1>(fcmpImpliesClass(FCmpInst::FCMP_UGT, *F, A, fcZero)),
2232 fcSubnormal | fcNormal | fcInf | fcNan);
2233 EXPECT_EQ(std::get<1>(fcmpImpliesClass(FCmpInst::FCMP_OGE, *F, A, fcZero)),
2234 ~fcNan);
2235 EXPECT_EQ(std::get<1>(fcmpImpliesClass(FCmpInst::FCMP_UGE, *F, A, fcZero)),
2236 fcAllFlags);
2237 EXPECT_EQ(std::get<1>(fcmpImpliesClass(FCmpInst::FCMP_OLT, *F, A, fcZero)),
2238 fcNone);
2239 EXPECT_EQ(std::get<1>(fcmpImpliesClass(FCmpInst::FCMP_ULT, *F, A, fcZero)),
2240 fcNan);
2241 EXPECT_EQ(std::get<1>(fcmpImpliesClass(FCmpInst::FCMP_OLE, *F, A, fcZero)),
2242 fcZero);
2243 EXPECT_EQ(std::get<1>(fcmpImpliesClass(FCmpInst::FCMP_ULE, *F, A, fcZero)),
2244 fcZero | fcNan);
2245 }
2246
2247 TEST_F(ValueTrackingTest, isNonZeroRecurrence) {
2248 parseAssembly(R"(
2249 define i1 @test(i8 %n, i8 %r) {
2250 entry:
2251 br label %loop
2252 loop:
2253 %p = phi i8 [ -1, %entry ], [ %next, %loop ]
2254 %next = add nsw i8 %p, -1
2255 %cmp1 = icmp eq i8 %p, %n
2256 br i1 %cmp1, label %exit, label %loop
2257 exit:
2258 %A = or i8 %p, %r
2259 %CxtI = icmp eq i8 %A, 0
2260 ret i1 %CxtI
2261 }
2262 )");
2263 const DataLayout &DL = M->getDataLayout();
2264 AssumptionCache AC(*F);
2265 EXPECT_TRUE(isKnownNonZero(A, SimplifyQuery(DL, /*DT=*/nullptr, &AC, CxtI)));
2266 }
2267
2268 TEST_F(ValueTrackingTest, KnownNonZeroFromDomCond) {
2269 parseAssembly(R"(
2270 declare ptr @f_i8()
2271 define void @test(i1 %c) {
2272 %A = call ptr @f_i8()
2273 %B = call ptr @f_i8()
2274 %c1 = icmp ne ptr %A, null
2275 %cond = and i1 %c1, %c
2276 br i1 %cond, label %T, label %Q
2277 T:
2278 %CxtI = add i32 0, 0
2279 ret void
2280 Q:
2281 %CxtI2 = add i32 0, 0
2282 ret void
2283 }
2284 )");
2285 AssumptionCache AC(*F);
2286 DominatorTree DT(*F);
2287 const DataLayout &DL = M->getDataLayout();
2288 const SimplifyQuery SQ(DL, &DT, &AC);
2289 EXPECT_EQ(isKnownNonZero(A, SQ.getWithInstruction(CxtI)), true);
2290 EXPECT_EQ(isKnownNonZero(A, SQ.getWithInstruction(CxtI2)), false);
2291 }
2292
2293 TEST_F(ValueTrackingTest, KnownNonZeroFromDomCond2) {
2294 parseAssembly(R"(
2295 declare ptr @f_i8()
2296 define void @test(i1 %c) {
2297 %A = call ptr @f_i8()
2298 %B = call ptr @f_i8()
2299 %c1 = icmp ne ptr %A, null
2300 %cond = select i1 %c, i1 %c1, i1 false
2301 br i1 %cond, label %T, label %Q
2302 T:
2303 %CxtI = add i32 0, 0
2304 ret void
2305 Q:
2306 %CxtI2 = add i32 0, 0
2307 ret void
2308 }
2309 )");
2310 AssumptionCache AC(*F);
2311 DominatorTree DT(*F);
2312 const DataLayout &DL = M->getDataLayout();
2313 const SimplifyQuery SQ(DL, &DT, &AC);
2314 EXPECT_EQ(isKnownNonZero(A, SQ.getWithInstruction(CxtI)), true);
2315 EXPECT_EQ(isKnownNonZero(A, SQ.getWithInstruction(CxtI2)), false);
2316 }
2317
2318 TEST_F(ValueTrackingTest, IsImpliedConditionAnd) {
2319 parseAssembly(R"(
2320 define void @test(i32 %x, i32 %y) {
2321 %c1 = icmp ult i32 %x, 10
2322 %c2 = icmp ult i32 %y, 15
2323 %A = and i1 %c1, %c2
2324 ; x < 10 /\ y < 15
2325 %A2 = icmp ult i32 %x, 20
2326 %A3 = icmp uge i32 %y, 20
2327 %A4 = icmp ult i32 %x, 5
2328 ret void
2329 }
2330 )");
2331 const DataLayout &DL = M->getDataLayout();
2332 EXPECT_EQ(isImpliedCondition(A, A2, DL), true);
2333 EXPECT_EQ(isImpliedCondition(A, A3, DL), false);
2334 EXPECT_EQ(isImpliedCondition(A, A4, DL), std::nullopt);
2335 }
2336
2337 TEST_F(ValueTrackingTest, IsImpliedConditionAnd2) {
2338 parseAssembly(R"(
2339 define void @test(i32 %x, i32 %y) {
2340 %c1 = icmp ult i32 %x, 10
2341 %c2 = icmp ult i32 %y, 15
2342 %A = select i1 %c1, i1 %c2, i1 false
2343 ; x < 10 /\ y < 15
2344 %A2 = icmp ult i32 %x, 20
2345 %A3 = icmp uge i32 %y, 20
2346 %A4 = icmp ult i32 %x, 5
2347 ret void
2348 }
2349 )");
2350 const DataLayout &DL = M->getDataLayout();
2351 EXPECT_EQ(isImpliedCondition(A, A2, DL), true);
2352 EXPECT_EQ(isImpliedCondition(A, A3, DL), false);
2353 EXPECT_EQ(isImpliedCondition(A, A4, DL), std::nullopt);
2354 }
2355
2356 TEST_F(ValueTrackingTest, IsImpliedConditionAndVec) {
2357 parseAssembly(R"(
2358 define void @test(<2 x i8> %x, <2 x i8> %y) {
2359 %A = icmp ult <2 x i8> %x, %y
2360 %A2 = icmp ule <2 x i8> %x, %y
2361 ret void
2362 }
2363 )");
2364 const DataLayout &DL = M->getDataLayout();
2365 EXPECT_EQ(isImpliedCondition(A, A2, DL), true);
2366 }
2367
2368 TEST_F(ValueTrackingTest, IsImpliedConditionOr) {
2369 parseAssembly(R"(
2370 define void @test(i32 %x, i32 %y) {
2371 %c1 = icmp ult i32 %x, 10
2372 %c2 = icmp ult i32 %y, 15
2373 %A = or i1 %c1, %c2 ; negated
2374 ; x >= 10 /\ y >= 15
2375 %A2 = icmp ult i32 %x, 5
2376 %A3 = icmp uge i32 %y, 10
2377 %A4 = icmp ult i32 %x, 15
2378 ret void
2379 }
2380 )");
2381 const DataLayout &DL = M->getDataLayout();
2382 EXPECT_EQ(isImpliedCondition(A, A2, DL, false), false);
2383 EXPECT_EQ(isImpliedCondition(A, A3, DL, false), true);
2384 EXPECT_EQ(isImpliedCondition(A, A4, DL, false), std::nullopt);
2385 }
2386
2387 TEST_F(ValueTrackingTest, IsImpliedConditionOr2) {
2388 parseAssembly(R"(
2389 define void @test(i32 %x, i32 %y) {
2390 %c1 = icmp ult i32 %x, 10
2391 %c2 = icmp ult i32 %y, 15
2392 %A = select i1 %c1, i1 true, i1 %c2 ; negated
2393 ; x >= 10 /\ y >= 15
2394 %A2 = icmp ult i32 %x, 5
2395 %A3 = icmp uge i32 %y, 10
2396 %A4 = icmp ult i32 %x, 15
2397 ret void
2398 }
2399 )");
2400 const DataLayout &DL = M->getDataLayout();
2401 EXPECT_EQ(isImpliedCondition(A, A2, DL, false), false);
2402 EXPECT_EQ(isImpliedCondition(A, A3, DL, false), true);
2403 EXPECT_EQ(isImpliedCondition(A, A4, DL, false), std::nullopt);
2404 }
2405
2406 TEST_F(ComputeKnownBitsTest, KnownNonZeroShift) {
2407 // %q is known nonzero without known bits.
2408 // Because %q is nonzero, %A[0] is known to be zero.
2409 parseAssembly(
2410 "define i8 @test(i8 %p, ptr %pq) {\n"
2411 " %q = load i8, ptr %pq, !range !0\n"
2412 " %A = shl i8 %p, %q\n"
2413 " ret i8 %A\n"
2414 "}\n"
2415 "!0 = !{ i8 1, i8 5 }\n");
2416 expectKnownBits(/*zero*/ 1u, /*one*/ 0u);
2417 }
2418
2419 TEST_F(ComputeKnownBitsTest, ComputeKnownFshl) {
2420 // fshl(....1111....0000, 00..1111........, 6)
2421 // = 11....000000..11
2422 parseAssembly(
2423 "define i16 @test(i16 %a, i16 %b) {\n"
2424 " %aa = shl i16 %a, 4\n"
2425 " %bb = lshr i16 %b, 2\n"
2426 " %aaa = or i16 %aa, 3840\n"
2427 " %bbb = or i16 %bb, 3840\n"
2428 " %A = call i16 @llvm.fshl.i16(i16 %aaa, i16 %bbb, i16 6)\n"
2429 " ret i16 %A\n"
2430 "}\n"
2431 "declare i16 @llvm.fshl.i16(i16, i16, i16)\n");
2432 expectKnownBits(/*zero*/ 1008u, /*one*/ 49155u);
2433 }
2434
2435 TEST_F(ComputeKnownBitsTest, ComputeKnownFshr) {
2436 // fshr(....1111....0000, 00..1111........, 26)
2437 // = 11....000000..11
2438 parseAssembly(
2439 "define i16 @test(i16 %a, i16 %b) {\n"
2440 " %aa = shl i16 %a, 4\n"
2441 " %bb = lshr i16 %b, 2\n"
2442 " %aaa = or i16 %aa, 3840\n"
2443 " %bbb = or i16 %bb, 3840\n"
2444 " %A = call i16 @llvm.fshr.i16(i16 %aaa, i16 %bbb, i16 26)\n"
2445 " ret i16 %A\n"
2446 "}\n"
2447 "declare i16 @llvm.fshr.i16(i16, i16, i16)\n");
2448 expectKnownBits(/*zero*/ 1008u, /*one*/ 49155u);
2449 }
2450
2451 TEST_F(ComputeKnownBitsTest, ComputeKnownFshlZero) {
2452 // fshl(....1111....0000, 00..1111........, 0)
2453 // = ....1111....0000
2454 parseAssembly(
2455 "define i16 @test(i16 %a, i16 %b) {\n"
2456 " %aa = shl i16 %a, 4\n"
2457 " %bb = lshr i16 %b, 2\n"
2458 " %aaa = or i16 %aa, 3840\n"
2459 " %bbb = or i16 %bb, 3840\n"
2460 " %A = call i16 @llvm.fshl.i16(i16 %aaa, i16 %bbb, i16 0)\n"
2461 " ret i16 %A\n"
2462 "}\n"
2463 "declare i16 @llvm.fshl.i16(i16, i16, i16)\n");
2464 expectKnownBits(/*zero*/ 15u, /*one*/ 3840u);
2465 }
2466
2467 TEST_F(ComputeKnownBitsTest, ComputeKnownUAddSatLeadingOnes) {
2468 // uadd.sat(1111...1, ........)
2469 // = 1111....
2470 parseAssembly(
2471 "define i8 @test(i8 %a, i8 %b) {\n"
2472 " %aa = or i8 %a, 241\n"
2473 " %A = call i8 @llvm.uadd.sat.i8(i8 %aa, i8 %b)\n"
2474 " ret i8 %A\n"
2475 "}\n"
2476 "declare i8 @llvm.uadd.sat.i8(i8, i8)\n");
2477 expectKnownBits(/*zero*/ 0u, /*one*/ 240u);
2478 }
2479
2480 TEST_F(ComputeKnownBitsTest, ComputeKnownUAddSatOnesPreserved) {
2481 // uadd.sat(00...011, .1...110)
2482 // = .......1
2483 parseAssembly(
2484 "define i8 @test(i8 %a, i8 %b) {\n"
2485 " %aa = or i8 %a, 3\n"
2486 " %aaa = and i8 %aa, 59\n"
2487 " %bb = or i8 %b, 70\n"
2488 " %bbb = and i8 %bb, 254\n"
2489 " %A = call i8 @llvm.uadd.sat.i8(i8 %aaa, i8 %bbb)\n"
2490 " ret i8 %A\n"
2491 "}\n"
2492 "declare i8 @llvm.uadd.sat.i8(i8, i8)\n");
2493 expectKnownBits(/*zero*/ 0u, /*one*/ 1u);
2494 }
2495
2496 TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatLHSLeadingZeros) {
2497 // usub.sat(0000...0, ........)
2498 // = 0000....
2499 parseAssembly(
2500 "define i8 @test(i8 %a, i8 %b) {\n"
2501 " %aa = and i8 %a, 14\n"
2502 " %A = call i8 @llvm.usub.sat.i8(i8 %aa, i8 %b)\n"
2503 " ret i8 %A\n"
2504 "}\n"
2505 "declare i8 @llvm.usub.sat.i8(i8, i8)\n");
2506 expectKnownBits(/*zero*/ 240u, /*one*/ 0u);
2507 }
2508
2509 TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatRHSLeadingOnes) {
2510 // usub.sat(........, 1111...1)
2511 // = 0000....
2512 parseAssembly(
2513 "define i8 @test(i8 %a, i8 %b) {\n"
2514 " %bb = or i8 %a, 241\n"
2515 " %A = call i8 @llvm.usub.sat.i8(i8 %a, i8 %bb)\n"
2516 " ret i8 %A\n"
2517 "}\n"
2518 "declare i8 @llvm.usub.sat.i8(i8, i8)\n");
2519 expectKnownBits(/*zero*/ 240u, /*one*/ 0u);
2520 }
2521
2522 TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatZerosPreserved) {
2523 // usub.sat(11...011, .1...110)
2524 // = ......0.
2525 parseAssembly(
2526 "define i8 @test(i8 %a, i8 %b) {\n"
2527 " %aa = or i8 %a, 195\n"
2528 " %aaa = and i8 %aa, 251\n"
2529 " %bb = or i8 %b, 70\n"
2530 " %bbb = and i8 %bb, 254\n"
2531 " %A = call i8 @llvm.usub.sat.i8(i8 %aaa, i8 %bbb)\n"
2532 " ret i8 %A\n"
2533 "}\n"
2534 "declare i8 @llvm.usub.sat.i8(i8, i8)\n");
2535 expectKnownBits(/*zero*/ 2u, /*one*/ 0u);
2536 }
2537
2538 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsPtrToIntTrunc) {
2539 // ptrtoint truncates the pointer type. Make sure we don't crash.
2540 parseAssembly(
2541 "define void @test(ptr %p) {\n"
2542 " %A = load ptr, ptr %p\n"
2543 " %i = ptrtoint ptr %A to i32\n"
2544 " %m = and i32 %i, 31\n"
2545 " %c = icmp eq i32 %m, 0\n"
2546 " call void @llvm.assume(i1 %c)\n"
2547 " ret void\n"
2548 "}\n"
2549 "declare void @llvm.assume(i1)\n");
2550 AssumptionCache AC(*F);
2551 KnownBits Known =
2552 computeKnownBits(A, M->getDataLayout(), &AC, F->front().getTerminator());
2553 EXPECT_TRUE(Known.isUnknown());
2554 }
2555
2556 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsPtrToIntZext) {
2557 // ptrtoint zero extends the pointer type. Make sure we don't crash.
2558 parseAssembly(
2559 "define void @test(ptr %p) {\n"
2560 " %A = load ptr, ptr %p\n"
2561 " %i = ptrtoint ptr %A to i128\n"
2562 " %m = and i128 %i, 31\n"
2563 " %c = icmp eq i128 %m, 0\n"
2564 " call void @llvm.assume(i1 %c)\n"
2565 " ret void\n"
2566 "}\n"
2567 "declare void @llvm.assume(i1)\n");
2568 AssumptionCache AC(*F);
2569 KnownBits Known =
2570 computeKnownBits(A, M->getDataLayout(), &AC, F->front().getTerminator());
2571 EXPECT_TRUE(Known.isUnknown());
2572 }
2573
2574 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsFreeze) {
2575 parseAssembly("define void @test() {\n"
2576 " %m = call i32 @any_num()\n"
2577 " %A = freeze i32 %m\n"
2578 " %n = and i32 %m, 31\n"
2579 " %c = icmp eq i32 %n, 0\n"
2580 " call void @llvm.assume(i1 %c)\n"
2581 " ret void\n"
2582 "}\n"
2583 "declare void @llvm.assume(i1)\n"
2584 "declare i32 @any_num()\n");
2585 AssumptionCache AC(*F);
2586 KnownBits Known =
2587 computeKnownBits(A, M->getDataLayout(), &AC, F->front().getTerminator());
2588 EXPECT_EQ(Known.Zero.getZExtValue(), 31u);
2589 EXPECT_EQ(Known.One.getZExtValue(), 0u);
2590 }
2591
2592 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsReturnedRangeConflict) {
2593 parseAssembly(
2594 "declare i16 @foo(i16 returned)\n"
2595 "\n"
2596 "define i16 @test() {\n"
2597 " %A = call i16 @foo(i16 4095), !range !{i16 32, i16 33}\n"
2598 " ret i16 %A\n"
2599 "}\n");
2600 // The call returns 32 according to range metadata, but 4095 according to the
2601 // returned arg operand. Given the conflicting information we expect that the
2602 // known bits information simply is cleared.
2603 expectKnownBits(/*zero*/ 0u, /*one*/ 0u);
2604 }
2605
2606 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsAddWithRange) {
2607 parseAssembly("define void @test(ptr %p) {\n"
2608 " %A = load i64, ptr %p, !range !{i64 64, i64 65536}\n"
2609 " %APlus512 = add i64 %A, 512\n"
2610 " %c = icmp ugt i64 %APlus512, 523\n"
2611 " call void @llvm.assume(i1 %c)\n"
2612 " ret void\n"
2613 "}\n"
2614 "declare void @llvm.assume(i1)\n");
2615 AssumptionCache AC(*F);
2616 KnownBits Known =
2617 computeKnownBits(A, M->getDataLayout(), &AC, F->front().getTerminator());
2618 EXPECT_EQ(Known.Zero.getZExtValue(), ~(65536llu - 1));
2619 EXPECT_EQ(Known.One.getZExtValue(), 0u);
2620 Instruction &APlus512 = findInstructionByName(F, "APlus512");
2621 Known = computeKnownBits(&APlus512, M->getDataLayout(), &AC,
2622 F->front().getTerminator());
2623 // We know of one less zero because 512 may have produced a 1 that
2624 // got carried all the way to the first trailing zero.
2625 EXPECT_EQ(Known.Zero.getZExtValue(), (~(65536llu - 1)) << 1);
2626 EXPECT_EQ(Known.One.getZExtValue(), 0u);
2627 // The known range is not precise given computeKnownBits works
2628 // with the masks of zeros and ones, not the ranges.
2629 EXPECT_EQ(Known.getMinValue(), 0u);
2630 EXPECT_EQ(Known.getMaxValue(), 131071);
2631 }
2632
2633 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsUnknownVScale) {
2634 Module M("", Context);
2635 IRBuilder<> Builder(Context);
2636 Function *TheFn = Intrinsic::getOrInsertDeclaration(&M, Intrinsic::vscale,
2637 {Builder.getInt32Ty()});
2638 CallInst *CI = Builder.CreateCall(TheFn, {}, {}, "");
2639
2640 KnownBits Known = computeKnownBits(CI, M.getDataLayout());
2641 // There is no parent function so we cannot look up the vscale_range
2642 // attribute to determine the number of bits.
2643 EXPECT_EQ(Known.One.getZExtValue(), 0u);
2644 EXPECT_EQ(Known.Zero.getZExtValue(), 0u);
2645
2646 BasicBlock *BB = BasicBlock::Create(Context);
2647 CI->insertInto(BB, BB->end());
2648 Known = computeKnownBits(CI, M.getDataLayout());
2649 // There is no parent function so we cannot look up the vscale_range
2650 // attribute to determine the number of bits.
2651 EXPECT_EQ(Known.One.getZExtValue(), 0u);
2652 EXPECT_EQ(Known.Zero.getZExtValue(), 0u);
2653
2654 CI->removeFromParent();
2655 delete CI;
2656 delete BB;
2657 }
2658
2659 // 512 + [32, 64) doesn't produce overlapping bits.
2660 // Make sure we get all the individual bits properly.
2661 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsAddWithRangeNoOverlap) {
2662 parseAssembly("define void @test(ptr %p) {\n"
2663 " %A = load i64, ptr %p, !range !{i64 32, i64 64}\n"
2664 " %APlus512 = add i64 %A, 512\n"
2665 " %c = icmp ugt i64 %APlus512, 523\n"
2666 " call void @llvm.assume(i1 %c)\n"
2667 " ret void\n"
2668 "}\n"
2669 "declare void @llvm.assume(i1)\n");
2670 AssumptionCache AC(*F);
2671 KnownBits Known =
2672 computeKnownBits(A, M->getDataLayout(), &AC, F->front().getTerminator());
2673 EXPECT_EQ(Known.Zero.getZExtValue(), ~(64llu - 1));
2674 EXPECT_EQ(Known.One.getZExtValue(), 32u);
2675 Instruction &APlus512 = findInstructionByName(F, "APlus512");
2676 Known = computeKnownBits(&APlus512, M->getDataLayout(), &AC,
2677 F->front().getTerminator());
2678 EXPECT_EQ(Known.Zero.getZExtValue(), ~512llu & ~(64llu - 1));
2679 EXPECT_EQ(Known.One.getZExtValue(), 512u | 32u);
2680 // The known range is not precise given computeKnownBits works
2681 // with the masks of zeros and ones, not the ranges.
2682 EXPECT_EQ(Known.getMinValue(), 544);
2683 EXPECT_EQ(Known.getMaxValue(), 575);
2684 }
2685
2686 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsGEPWithRange) {
2687 parseAssembly(
2688 "define void @test(ptr %p) {\n"
2689 " %A = load i64, ptr %p, !range !{i64 64, i64 65536}\n"
2690 " %APtr = inttoptr i64 %A to float*"
2691 " %APtrPlus512 = getelementptr float, float* %APtr, i32 128\n"
2692 " %c = icmp ugt float* %APtrPlus512, inttoptr (i32 523 to float*)\n"
2693 " call void @llvm.assume(i1 %c)\n"
2694 " ret void\n"
2695 "}\n"
2696 "declare void @llvm.assume(i1)\n");
2697 AssumptionCache AC(*F);
2698 KnownBits Known =
2699 computeKnownBits(A, M->getDataLayout(), &AC, F->front().getTerminator());
2700 EXPECT_EQ(Known.Zero.getZExtValue(), ~(65536llu - 1));
2701 EXPECT_EQ(Known.One.getZExtValue(), 0u);
2702 Instruction &APtrPlus512 = findInstructionByName(F, "APtrPlus512");
2703 Known = computeKnownBits(&APtrPlus512, M->getDataLayout(), &AC,
2704 F->front().getTerminator());
2705 // We know of one less zero because 512 may have produced a 1 that
2706 // got carried all the way to the first trailing zero.
2707 EXPECT_EQ(Known.Zero.getZExtValue(), ~(65536llu - 1) << 1);
2708 EXPECT_EQ(Known.One.getZExtValue(), 0u);
2709 // The known range is not precise given computeKnownBits works
2710 // with the masks of zeros and ones, not the ranges.
2711 EXPECT_EQ(Known.getMinValue(), 0u);
2712 EXPECT_EQ(Known.getMaxValue(), 131071);
2713 }
2714
2715 // 4*128 + [32, 64) doesn't produce overlapping bits.
2716 // Make sure we get all the individual bits properly.
2717 // This test is useful to check that we account for the scaling factor
2718 // in the gep. Indeed, gep float, [32,64), 128 is not 128 + [32,64).
2719 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsGEPWithRangeNoOverlap) {
2720 parseAssembly(
2721 "define void @test(ptr %p) {\n"
2722 " %A = load i64, ptr %p, !range !{i64 32, i64 64}\n"
2723 " %APtr = inttoptr i64 %A to float*"
2724 " %APtrPlus512 = getelementptr float, float* %APtr, i32 128\n"
2725 " %c = icmp ugt float* %APtrPlus512, inttoptr (i32 523 to float*)\n"
2726 " call void @llvm.assume(i1 %c)\n"
2727 " ret void\n"
2728 "}\n"
2729 "declare void @llvm.assume(i1)\n");
2730 AssumptionCache AC(*F);
2731 KnownBits Known =
2732 computeKnownBits(A, M->getDataLayout(), &AC, F->front().getTerminator());
2733 EXPECT_EQ(Known.Zero.getZExtValue(), ~(64llu - 1));
2734 EXPECT_EQ(Known.One.getZExtValue(), 32u);
2735 Instruction &APtrPlus512 = findInstructionByName(F, "APtrPlus512");
2736 Known = computeKnownBits(&APtrPlus512, M->getDataLayout(), &AC,
2737 F->front().getTerminator());
2738 EXPECT_EQ(Known.Zero.getZExtValue(), ~512llu & ~(64llu - 1));
2739 EXPECT_EQ(Known.One.getZExtValue(), 512u | 32u);
2740 // The known range is not precise given computeKnownBits works
2741 // with the masks of zeros and ones, not the ranges.
2742 EXPECT_EQ(Known.getMinValue(), 544);
2743 EXPECT_EQ(Known.getMaxValue(), 575);
2744 }
2745
2746 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsAbsoluteSymbol) {
2747 auto M = parseModule(R"(
2748 @absolute_0_255 = external global [128 x i32], align 1, !absolute_symbol !0
2749 @absolute_0_256 = external global [128 x i32], align 1, !absolute_symbol !1
2750 @absolute_256_512 = external global [128 x i32], align 1, !absolute_symbol !2
2751 @absolute_0_neg1 = external global [128 x i32], align 1, !absolute_symbol !3
2752 @absolute_neg32_32 = external global [128 x i32], align 1, !absolute_symbol !4
2753 @absolute_neg32_33 = external global [128 x i32], align 1, !absolute_symbol !5
2754 @absolute_neg64_neg32 = external global [128 x i32], align 1, !absolute_symbol !6
2755 @absolute_0_256_align8 = external global [128 x i32], align 8, !absolute_symbol !1
2756
2757 !0 = !{i64 0, i64 255}
2758 !1 = !{i64 0, i64 256}
2759 !2 = !{i64 256, i64 512}
2760 !3 = !{i64 0, i64 -1}
2761 !4 = !{i64 -32, i64 32}
2762 !5 = !{i64 -32, i64 33}
2763 !6 = !{i64 -64, i64 -32}
2764 )");
2765
2766 GlobalValue *Absolute_0_255 = M->getNamedValue("absolute_0_255");
2767 GlobalValue *Absolute_0_256 = M->getNamedValue("absolute_0_256");
2768 GlobalValue *Absolute_256_512 = M->getNamedValue("absolute_256_512");
2769 GlobalValue *Absolute_0_Neg1 = M->getNamedValue("absolute_0_neg1");
2770 GlobalValue *Absolute_Neg32_32 = M->getNamedValue("absolute_neg32_32");
2771 GlobalValue *Absolute_Neg32_33 = M->getNamedValue("absolute_neg32_33");
2772 GlobalValue *Absolute_Neg64_Neg32 = M->getNamedValue("absolute_neg64_neg32");
2773 GlobalValue *Absolute_0_256_Align8 =
2774 M->getNamedValue("absolute_0_256_align8");
2775
2776 KnownBits Known_0_255 = computeKnownBits(Absolute_0_255, M->getDataLayout());
2777 EXPECT_EQ(64u - 8u, Known_0_255.countMinLeadingZeros());
2778 EXPECT_EQ(0u, Known_0_255.countMinTrailingZeros());
2779 EXPECT_EQ(0u, Known_0_255.countMinLeadingOnes());
2780 EXPECT_EQ(0u, Known_0_255.countMinTrailingOnes());
2781
2782 KnownBits Known_0_256 = computeKnownBits(Absolute_0_256, M->getDataLayout());
2783 EXPECT_EQ(64u - 8u, Known_0_256.countMinLeadingZeros());
2784 EXPECT_EQ(0u, Known_0_256.countMinTrailingZeros());
2785 EXPECT_EQ(0u, Known_0_256.countMinLeadingOnes());
2786 EXPECT_EQ(0u, Known_0_256.countMinTrailingOnes());
2787
2788 KnownBits Known_256_512 =
2789 computeKnownBits(Absolute_256_512, M->getDataLayout());
2790 EXPECT_EQ(64u - 8u, Known_0_255.countMinLeadingZeros());
2791 EXPECT_EQ(0u, Known_0_255.countMinTrailingZeros());
2792 EXPECT_EQ(0u, Known_0_255.countMinLeadingOnes());
2793 EXPECT_EQ(0u, Known_0_255.countMinTrailingOnes());
2794
2795 KnownBits Known_0_Neg1 =
2796 computeKnownBits(Absolute_0_Neg1, M->getDataLayout());
2797 EXPECT_EQ(0u, Known_0_Neg1.countMinLeadingZeros());
2798 EXPECT_EQ(0u, Known_0_Neg1.countMinTrailingZeros());
2799 EXPECT_EQ(0u, Known_0_Neg1.countMinLeadingOnes());
2800 EXPECT_EQ(0u, Known_0_Neg1.countMinTrailingOnes());
2801
2802 KnownBits Known_Neg32_32 =
2803 computeKnownBits(Absolute_Neg32_32, M->getDataLayout());
2804 EXPECT_EQ(0u, Known_Neg32_32.countMinLeadingZeros());
2805 EXPECT_EQ(0u, Known_Neg32_32.countMinTrailingZeros());
2806 EXPECT_EQ(0u, Known_Neg32_32.countMinLeadingOnes());
2807 EXPECT_EQ(0u, Known_Neg32_32.countMinTrailingOnes());
2808 EXPECT_EQ(1u, Known_Neg32_32.countMinSignBits());
2809
2810 KnownBits Known_Neg32_33 =
2811 computeKnownBits(Absolute_Neg32_33, M->getDataLayout());
2812 EXPECT_EQ(0u, Known_Neg32_33.countMinLeadingZeros());
2813 EXPECT_EQ(0u, Known_Neg32_33.countMinTrailingZeros());
2814 EXPECT_EQ(0u, Known_Neg32_33.countMinLeadingOnes());
2815 EXPECT_EQ(0u, Known_Neg32_33.countMinTrailingOnes());
2816 EXPECT_EQ(1u, Known_Neg32_33.countMinSignBits());
2817
2818 KnownBits Known_Neg32_Neg32 =
2819 computeKnownBits(Absolute_Neg64_Neg32, M->getDataLayout());
2820 EXPECT_EQ(0u, Known_Neg32_Neg32.countMinLeadingZeros());
2821 EXPECT_EQ(0u, Known_Neg32_Neg32.countMinTrailingZeros());
2822 EXPECT_EQ(58u, Known_Neg32_Neg32.countMinLeadingOnes());
2823 EXPECT_EQ(0u, Known_Neg32_Neg32.countMinTrailingOnes());
2824 EXPECT_EQ(58u, Known_Neg32_Neg32.countMinSignBits());
2825
2826 KnownBits Known_0_256_Align8 =
2827 computeKnownBits(Absolute_0_256_Align8, M->getDataLayout());
2828 EXPECT_EQ(64u - 8u, Known_0_256_Align8.countMinLeadingZeros());
2829 EXPECT_EQ(3u, Known_0_256_Align8.countMinTrailingZeros());
2830 EXPECT_EQ(0u, Known_0_256_Align8.countMinLeadingOnes());
2831 EXPECT_EQ(0u, Known_0_256_Align8.countMinTrailingOnes());
2832 }
2833
2834 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsGEPExtendBeforeMul) {
2835 // The index should be extended before multiplying with the scale.
2836 parseAssembly(R"(
2837 target datalayout = "p:16:16:16"
2838
2839 define void @test(i16 %arg) {
2840 %and = and i16 %arg, u0x8000
2841 %base = inttoptr i16 %and to ptr
2842 %A = getelementptr i32, ptr %base, i8 80
2843 ret void
2844 }
2845 )");
2846 KnownBits Known = computeKnownBits(A, M->getDataLayout());
2847 EXPECT_EQ(~320 & 0x7fff, Known.Zero);
2848 EXPECT_EQ(320, Known.One);
2849 }
2850
2851 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsGEPOnlyIndexBits) {
2852 // GEP should only affect the index width.
2853 parseAssembly(R"(
2854 target datalayout = "p:16:16:16:8"
2855
2856 define void @test(i16 %arg) {
2857 %and = and i16 %arg, u0x8000
2858 %or = or i16 %and, u0x00ff
2859 %base = inttoptr i16 %or to ptr
2860 %A = getelementptr i8, ptr %base, i8 1
2861 ret void
2862 }
2863 )");
2864 KnownBits Known = computeKnownBits(A, M->getDataLayout());
2865 EXPECT_EQ(0x7fff, Known.Zero);
2866 EXPECT_EQ(0, Known.One);
2867 }
2868
2869 TEST_F(ValueTrackingTest, HaveNoCommonBitsSet) {
2870 {
2871 // Check for an inverted mask: (X & ~M) op (Y & M).
2872 auto M = parseModule(R"(
2873 define i32 @test(i32 %X, i32 %Y, i32 noundef %M) {
2874 %1 = xor i32 %M, -1
2875 %LHS = and i32 %1, %X
2876 %RHS = and i32 %Y, %M
2877 %Ret = add i32 %LHS, %RHS
2878 ret i32 %Ret
2879 })");
2880
2881 auto *F = M->getFunction("test");
2882 auto *LHS = findInstructionByNameOrNull(F, "LHS");
2883 auto *RHS = findInstructionByNameOrNull(F, "RHS");
2884
2885 const DataLayout &DL = M->getDataLayout();
2886 EXPECT_TRUE(haveNoCommonBitsSet(LHS, RHS, DL));
2887 EXPECT_TRUE(haveNoCommonBitsSet(RHS, LHS, DL));
2888 }
2889 {
2890 // Check for (A & B) and ~(A | B)
2891 auto M = parseModule(R"(
2892 define void @test(i32 noundef %A, i32 noundef %B) {
2893 %LHS = and i32 %A, %B
2894 %or = or i32 %A, %B
2895 %RHS = xor i32 %or, -1
2896
2897 %LHS2 = and i32 %B, %A
2898 %or2 = or i32 %A, %B
2899 %RHS2 = xor i32 %or2, -1
2900
2901 ret void
2902 })");
2903
2904 auto *F = M->getFunction("test");
2905 const DataLayout &DL = M->getDataLayout();
2906
2907 auto *LHS = findInstructionByNameOrNull(F, "LHS");
2908 auto *RHS = findInstructionByNameOrNull(F, "RHS");
2909 EXPECT_TRUE(haveNoCommonBitsSet(LHS, RHS, DL));
2910 EXPECT_TRUE(haveNoCommonBitsSet(RHS, LHS, DL));
2911
2912 auto *LHS2 = findInstructionByNameOrNull(F, "LHS2");
2913 auto *RHS2 = findInstructionByNameOrNull(F, "RHS2");
2914 EXPECT_TRUE(haveNoCommonBitsSet(LHS2, RHS2, DL));
2915 EXPECT_TRUE(haveNoCommonBitsSet(RHS2, LHS2, DL));
2916 }
2917 {
2918 // Check for (A & B) and ~(A | B) in vector version
2919 auto M = parseModule(R"(
2920 define void @test(<2 x i32> noundef %A, <2 x i32> noundef %B) {
2921 %LHS = and <2 x i32> %A, %B
2922 %or = or <2 x i32> %A, %B
2923 %RHS = xor <2 x i32> %or, <i32 -1, i32 -1>
2924
2925 %LHS2 = and <2 x i32> %B, %A
2926 %or2 = or <2 x i32> %A, %B
2927 %RHS2 = xor <2 x i32> %or2, <i32 -1, i32 -1>
2928
2929 ret void
2930 })");
2931
2932 auto *F = M->getFunction("test");
2933 const DataLayout &DL = M->getDataLayout();
2934
2935 auto *LHS = findInstructionByNameOrNull(F, "LHS");
2936 auto *RHS = findInstructionByNameOrNull(F, "RHS");
2937 EXPECT_TRUE(haveNoCommonBitsSet(LHS, RHS, DL));
2938 EXPECT_TRUE(haveNoCommonBitsSet(RHS, LHS, DL));
2939
2940 auto *LHS2 = findInstructionByNameOrNull(F, "LHS2");
2941 auto *RHS2 = findInstructionByNameOrNull(F, "RHS2");
2942 EXPECT_TRUE(haveNoCommonBitsSet(LHS2, RHS2, DL));
2943 EXPECT_TRUE(haveNoCommonBitsSet(RHS2, LHS2, DL));
2944 }
2945 }
2946
2947 class IsBytewiseValueTest : public ValueTrackingTest,
2948 public ::testing::WithParamInterface<
2949 std::pair<const char *, const char *>> {
2950 protected:
2951 };
2952
2953 const std::pair<const char *, const char *> IsBytewiseValueTests[] = {
2954 {
2955 "i8 0",
2956 "ptr null",
2957 },
2958 {
2959 "i8 undef",
2960 "ptr undef",
2961 },
2962 {
2963 "i8 0",
2964 "i8 zeroinitializer",
2965 },
2966 {
2967 "i8 0",
2968 "i8 0",
2969 },
2970 {
2971 "i8 -86",
2972 "i8 -86",
2973 },
2974 {
2975 "i8 -1",
2976 "i8 -1",
2977 },
2978 {
2979 "i8 undef",
2980 "i16 undef",
2981 },
2982 {
2983 "i8 0",
2984 "i16 0",
2985 },
2986 {
2987 "",
2988 "i16 7",
2989 },
2990 {
2991 "i8 -86",
2992 "i16 -21846",
2993 },
2994 {
2995 "i8 -1",
2996 "i16 -1",
2997 },
2998 {
2999 "i8 0",
3000 "i48 0",
3001 },
3002 {
3003 "i8 -1",
3004 "i48 -1",
3005 },
3006 {
3007 "i8 0",
3008 "i49 0",
3009 },
3010 {
3011 "",
3012 "i49 -1",
3013 },
3014 {
3015 "i8 0",
3016 "half 0xH0000",
3017 },
3018 {
3019 "i8 -85",
3020 "half 0xHABAB",
3021 },
3022 {
3023 "i8 0",
3024 "float 0.0",
3025 },
3026 {
3027 "i8 -1",
3028 "float 0xFFFFFFFFE0000000",
3029 },
3030 {
3031 "i8 0",
3032 "double 0.0",
3033 },
3034 {
3035 "i8 -15",
3036 "double 0xF1F1F1F1F1F1F1F1",
3037 },
3038 {
3039 "i8 0",
3040 "ptr inttoptr (i64 0 to ptr)",
3041 },
3042 {
3043 "i8 -1",
3044 "ptr inttoptr (i64 -1 to ptr)",
3045 },
3046 {
3047 "i8 -86",
3048 "ptr inttoptr (i64 -6148914691236517206 to ptr)",
3049 },
3050 {
3051 "",
3052 "ptr inttoptr (i48 -1 to ptr)",
3053 },
3054 {
3055 "i8 -1",
3056 "ptr inttoptr (i96 -1 to ptr)",
3057 },
3058 {
3059 "i8 poison",
3060 "[0 x i8] zeroinitializer",
3061 },
3062 {
3063 "i8 undef",
3064 "[0 x i8] undef",
3065 },
3066 {
3067 "i8 poison",
3068 "[5 x [0 x i8]] zeroinitializer",
3069 },
3070 {
3071 "i8 undef",
3072 "[5 x [0 x i8]] undef",
3073 },
3074 {
3075 "i8 0",
3076 "[6 x i8] zeroinitializer",
3077 },
3078 {
3079 "i8 undef",
3080 "[6 x i8] undef",
3081 },
3082 {
3083 "i8 1",
3084 "[5 x i8] [i8 1, i8 1, i8 1, i8 1, i8 1]",
3085 },
3086 {
3087 "",
3088 "[5 x i64] [i64 1, i64 1, i64 1, i64 1, i64 1]",
3089 },
3090 {
3091 "i8 -1",
3092 "[5 x i64] [i64 -1, i64 -1, i64 -1, i64 -1, i64 -1]",
3093 },
3094 {
3095 "",
3096 "[4 x i8] [i8 1, i8 2, i8 1, i8 1]",
3097 },
3098 {
3099 "i8 1",
3100 "[4 x i8] [i8 1, i8 undef, i8 1, i8 1]",
3101 },
3102 {
3103 "i8 0",
3104 "<6 x i8> zeroinitializer",
3105 },
3106 {
3107 "i8 undef",
3108 "<6 x i8> undef",
3109 },
3110 {
3111 "i8 1",
3112 "<5 x i8> <i8 1, i8 1, i8 1, i8 1, i8 1>",
3113 },
3114 {
3115 "",
3116 "<5 x i64> <i64 1, i64 1, i64 1, i64 1, i64 1>",
3117 },
3118 {
3119 "i8 -1",
3120 "<5 x i64> <i64 -1, i64 -1, i64 -1, i64 -1, i64 -1>",
3121 },
3122 {
3123 "",
3124 "<4 x i8> <i8 1, i8 1, i8 2, i8 1>",
3125 },
3126 {
3127 "i8 5",
3128 "<2 x i8> < i8 5, i8 undef >",
3129 },
3130 {
3131 "i8 0",
3132 "[2 x [2 x i16]] zeroinitializer",
3133 },
3134 {
3135 "i8 undef",
3136 "[2 x [2 x i16]] undef",
3137 },
3138 {
3139 "i8 -86",
3140 "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], "
3141 "[2 x i16] [i16 -21846, i16 -21846]]",
3142 },
3143 {
3144 "",
3145 "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], "
3146 "[2 x i16] [i16 -21836, i16 -21846]]",
3147 },
3148 {
3149 "i8 poison",
3150 "{ } zeroinitializer",
3151 },
3152 {
3153 "i8 undef",
3154 "{ } undef",
3155 },
3156 {
3157 "i8 poison",
3158 "{ {}, {} } zeroinitializer",
3159 },
3160 {
3161 "i8 undef",
3162 "{ {}, {} } undef",
3163 },
3164 {
3165 "i8 0",
3166 "{i8, i64, ptr} zeroinitializer",
3167 },
3168 {
3169 "i8 undef",
3170 "{i8, i64, ptr} undef",
3171 },
3172 {
3173 "i8 -86",
3174 "{i8, i64, ptr} {i8 -86, i64 -6148914691236517206, ptr undef}",
3175 },
3176 {
3177 "",
3178 "{i8, i64, ptr} {i8 86, i64 -6148914691236517206, ptr undef}",
3179 },
3180 };
3181
3182 INSTANTIATE_TEST_SUITE_P(IsBytewiseValueParamTests, IsBytewiseValueTest,
3183 ::testing::ValuesIn(IsBytewiseValueTests));
3184
3185 TEST_P(IsBytewiseValueTest, IsBytewiseValue) {
3186 auto M = parseModule(std::string("@test = global ") + GetParam().second);
3187 GlobalVariable *GV = dyn_cast<GlobalVariable>(M->getNamedValue("test"));
3188 Value *Actual = isBytewiseValue(GV->getInitializer(), M->getDataLayout());
3189 std::string Buff;
3190 raw_string_ostream S(Buff);
3191 if (Actual)
3192 S << *Actual;
3193 EXPECT_EQ(GetParam().first, Buff);
3194 }
3195
3196 TEST_F(ValueTrackingTest, ComputeConstantRange) {
3197 {
3198 // Assumptions:
3199 // * stride >= 5
3200 // * stride < 10
3201 //
3202 // stride = [5, 10)
3203 auto M = parseModule(R"(
3204 declare void @llvm.assume(i1)
3205
3206 define i32 @test(i32 %stride) {
3207 %gt = icmp uge i32 %stride, 5
3208 call void @llvm.assume(i1 %gt)
3209 %lt = icmp ult i32 %stride, 10
3210 call void @llvm.assume(i1 %lt)
3211 %stride.plus.one = add nsw nuw i32 %stride, 1
3212 ret i32 %stride.plus.one
3213 })");
3214 Function *F = M->getFunction("test");
3215
3216 AssumptionCache AC(*F);
3217 Value *Stride = &*F->arg_begin();
3218 ConstantRange CR1 = computeConstantRange(Stride, false, true, &AC, nullptr);
3219 EXPECT_TRUE(CR1.isFullSet());
3220
3221 Instruction *I = &findInstructionByName(F, "stride.plus.one");
3222 ConstantRange CR2 = computeConstantRange(Stride, false, true, &AC, I);
3223 EXPECT_EQ(5, CR2.getLower());
3224 EXPECT_EQ(10, CR2.getUpper());
3225 }
3226
3227 {
3228 // Assumptions:
3229 // * stride >= 5
3230 // * stride < 200
3231 // * stride == 99
3232 //
3233 // stride = [99, 100)
3234 auto M = parseModule(R"(
3235 declare void @llvm.assume(i1)
3236
3237 define i32 @test(i32 %stride) {
3238 %gt = icmp uge i32 %stride, 5
3239 call void @llvm.assume(i1 %gt)
3240 %lt = icmp ult i32 %stride, 200
3241 call void @llvm.assume(i1 %lt)
3242 %eq = icmp eq i32 %stride, 99
3243 call void @llvm.assume(i1 %eq)
3244 %stride.plus.one = add nsw nuw i32 %stride, 1
3245 ret i32 %stride.plus.one
3246 })");
3247 Function *F = M->getFunction("test");
3248
3249 AssumptionCache AC(*F);
3250 Value *Stride = &*F->arg_begin();
3251 Instruction *I = &findInstructionByName(F, "stride.plus.one");
3252 ConstantRange CR = computeConstantRange(Stride, false, true, &AC, I);
3253 EXPECT_EQ(99, *CR.getSingleElement());
3254 }
3255
3256 {
3257 // Assumptions:
3258 // * stride >= 5
3259 // * stride >= 50
3260 // * stride < 100
3261 // * stride < 200
3262 //
3263 // stride = [50, 100)
3264 auto M = parseModule(R"(
3265 declare void @llvm.assume(i1)
3266
3267 define i32 @test(i32 %stride, i1 %cond) {
3268 %gt = icmp uge i32 %stride, 5
3269 call void @llvm.assume(i1 %gt)
3270 %gt.2 = icmp uge i32 %stride, 50
3271 call void @llvm.assume(i1 %gt.2)
3272 br i1 %cond, label %bb1, label %bb2
3273
3274 bb1:
3275 %lt = icmp ult i32 %stride, 200
3276 call void @llvm.assume(i1 %lt)
3277 %lt.2 = icmp ult i32 %stride, 100
3278 call void @llvm.assume(i1 %lt.2)
3279 %stride.plus.one = add nsw nuw i32 %stride, 1
3280 ret i32 %stride.plus.one
3281
3282 bb2:
3283 ret i32 0
3284 })");
3285 Function *F = M->getFunction("test");
3286
3287 AssumptionCache AC(*F);
3288 Value *Stride = &*F->arg_begin();
3289 Instruction *GT2 = &findInstructionByName(F, "gt.2");
3290 ConstantRange CR = computeConstantRange(Stride, false, true, &AC, GT2);
3291 EXPECT_EQ(5, CR.getLower());
3292 EXPECT_EQ(0, CR.getUpper());
3293
3294 Instruction *I = &findInstructionByName(F, "stride.plus.one");
3295 ConstantRange CR2 = computeConstantRange(Stride, false, true, &AC, I);
3296 EXPECT_EQ(50, CR2.getLower());
3297 EXPECT_EQ(100, CR2.getUpper());
3298 }
3299
3300 {
3301 // Assumptions:
3302 // * stride > 5
3303 // * stride < 5
3304 //
3305 // stride = empty range, as the assumptions contradict each other.
3306 auto M = parseModule(R"(
3307 declare void @llvm.assume(i1)
3308
3309 define i32 @test(i32 %stride, i1 %cond) {
3310 %gt = icmp ugt i32 %stride, 5
3311 call void @llvm.assume(i1 %gt)
3312 %lt = icmp ult i32 %stride, 5
3313 call void @llvm.assume(i1 %lt)
3314 %stride.plus.one = add nsw nuw i32 %stride, 1
3315 ret i32 %stride.plus.one
3316 })");
3317 Function *F = M->getFunction("test");
3318
3319 AssumptionCache AC(*F);
3320 Value *Stride = &*F->arg_begin();
3321
3322 Instruction *I = &findInstructionByName(F, "stride.plus.one");
3323 ConstantRange CR = computeConstantRange(Stride, false, true, &AC, I);
3324 EXPECT_TRUE(CR.isEmptySet());
3325 }
3326
3327 {
3328 // Assumptions:
3329 // * x.1 >= 5
3330 // * x.2 < x.1
3331 //
3332 // stride = [0, -1)
3333 auto M = parseModule(R"(
3334 declare void @llvm.assume(i1)
3335
3336 define i32 @test(i32 %x.1, i32 %x.2) {
3337 %gt = icmp uge i32 %x.1, 5
3338 call void @llvm.assume(i1 %gt)
3339 %lt = icmp ult i32 %x.2, %x.1
3340 call void @llvm.assume(i1 %lt)
3341 %stride.plus.one = add nsw nuw i32 %x.1, 1
3342 ret i32 %stride.plus.one
3343 })");
3344 Function *F = M->getFunction("test");
3345
3346 AssumptionCache AC(*F);
3347 Value *X1 = &*(F->arg_begin());
3348 Value *X2 = &*std::next(F->arg_begin());
3349
3350 Instruction *I = &findInstructionByName(F, "stride.plus.one");
3351 ConstantRange CR1 = computeConstantRange(X1, false, true, &AC, I);
3352 ConstantRange CR2 = computeConstantRange(X2, false, true, &AC, I);
3353
3354 EXPECT_EQ(5, CR1.getLower());
3355 EXPECT_EQ(0, CR1.getUpper());
3356
3357 EXPECT_EQ(0, CR2.getLower());
3358 EXPECT_EQ(0xffffffff, CR2.getUpper());
3359
3360 // Check the depth cutoff results in a conservative result (full set) by
3361 // passing Depth == MaxDepth == 6.
3362 ConstantRange CR3 = computeConstantRange(X2, false, true, &AC, I, nullptr, 6);
3363 EXPECT_TRUE(CR3.isFullSet());
3364 }
3365 {
3366 // Assumptions:
3367 // * x.2 <= x.1
3368 auto M = parseModule(R"(
3369 declare void @llvm.assume(i1)
3370
3371 define i32 @test(i32 %x.1, i32 %x.2) {
3372 %lt = icmp ule i32 %x.2, %x.1
3373 call void @llvm.assume(i1 %lt)
3374 %stride.plus.one = add nsw nuw i32 %x.1, 1
3375 ret i32 %stride.plus.one
3376 })");
3377 Function *F = M->getFunction("test");
3378
3379 AssumptionCache AC(*F);
3380 Value *X2 = &*std::next(F->arg_begin());
3381
3382 Instruction *I = &findInstructionByName(F, "stride.plus.one");
3383 ConstantRange CR1 = computeConstantRange(X2, false, true, &AC, I);
3384 // If we don't know the value of x.2, we don't know the value of x.1.
3385 EXPECT_TRUE(CR1.isFullSet());
3386 }
3387 }
3388
3389 struct FindAllocaForValueTestParams {
3390 const char *IR;
3391 bool AnyOffsetResult;
3392 bool ZeroOffsetResult;
3393 };
3394
3395 class FindAllocaForValueTest
3396 : public ValueTrackingTest,
3397 public ::testing::WithParamInterface<FindAllocaForValueTestParams> {
3398 protected:
3399 };
3400
3401 const FindAllocaForValueTestParams FindAllocaForValueTests[] = {
3402 {R"(
3403 define void @test() {
3404 %a = alloca i64
3405 %r = bitcast ptr %a to ptr
3406 ret void
3407 })",
3408 true, true},
3409
3410 {R"(
3411 define void @test() {
3412 %a = alloca i32
3413 %r = getelementptr i32, ptr %a, i32 1
3414 ret void
3415 })",
3416 true, false},
3417
3418 {R"(
3419 define void @test() {
3420 %a = alloca i32
3421 %r = getelementptr i32, ptr %a, i32 0
3422 ret void
3423 })",
3424 true, true},
3425
3426 {R"(
3427 define void @test(i1 %cond) {
3428 entry:
3429 %a = alloca i32
3430 br label %bb1
3431
3432 bb1:
3433 %r = phi ptr [ %a, %entry ], [ %r, %bb1 ]
3434 br i1 %cond, label %bb1, label %exit
3435
3436 exit:
3437 ret void
3438 })",
3439 true, true},
3440
3441 {R"(
3442 define void @test(i1 %cond) {
3443 %a = alloca i32
3444 %r = select i1 %cond, ptr %a, ptr %a
3445 ret void
3446 })",
3447 true, true},
3448
3449 {R"(
3450 define void @test(i1 %cond) {
3451 %a = alloca i32
3452 %b = alloca i32
3453 %r = select i1 %cond, ptr %a, ptr %b
3454 ret void
3455 })",
3456 false, false},
3457
3458 {R"(
3459 define void @test(i1 %cond) {
3460 entry:
3461 %a = alloca i64
3462 %a32 = bitcast ptr %a to ptr
3463 br label %bb1
3464
3465 bb1:
3466 %x = phi ptr [ %a32, %entry ], [ %x, %bb1 ]
3467 %r = getelementptr i32, ptr %x, i32 1
3468 br i1 %cond, label %bb1, label %exit
3469
3470 exit:
3471 ret void
3472 })",
3473 true, false},
3474
3475 {R"(
3476 define void @test(i1 %cond) {
3477 entry:
3478 %a = alloca i64
3479 %a32 = bitcast ptr %a to ptr
3480 br label %bb1
3481
3482 bb1:
3483 %x = phi ptr [ %a32, %entry ], [ %r, %bb1 ]
3484 %r = getelementptr i32, ptr %x, i32 1
3485 br i1 %cond, label %bb1, label %exit
3486
3487 exit:
3488 ret void
3489 })",
3490 true, false},
3491
3492 {R"(
3493 define void @test(i1 %cond, ptr %a) {
3494 entry:
3495 %r = bitcast ptr %a to ptr
3496 ret void
3497 })",
3498 false, false},
3499
3500 {R"(
3501 define void @test(i1 %cond) {
3502 entry:
3503 %a = alloca i32
3504 %b = alloca i32
3505 br label %bb1
3506
3507 bb1:
3508 %r = phi ptr [ %a, %entry ], [ %b, %bb1 ]
3509 br i1 %cond, label %bb1, label %exit
3510
3511 exit:
3512 ret void
3513 })",
3514 false, false},
3515 {R"(
3516 declare ptr @retptr(ptr returned)
3517 define void @test(i1 %cond) {
3518 %a = alloca i32
3519 %r = call ptr @retptr(ptr %a)
3520 ret void
3521 })",
3522 true, true},
3523 {R"(
3524 declare ptr @fun(ptr)
3525 define void @test(i1 %cond) {
3526 %a = alloca i32
3527 %r = call ptr @fun(ptr %a)
3528 ret void
3529 })",
3530 false, false},
3531 };
3532
3533 TEST_P(FindAllocaForValueTest, findAllocaForValue) {
3534 auto M = parseModule(GetParam().IR);
3535 Function *F = M->getFunction("test");
3536 Instruction *I = &findInstructionByName(F, "r");
3537 const AllocaInst *AI = findAllocaForValue(I);
3538 EXPECT_EQ(!!AI, GetParam().AnyOffsetResult);
3539 }
3540
3541 TEST_P(FindAllocaForValueTest, findAllocaForValueZeroOffset) {
3542 auto M = parseModule(GetParam().IR);
3543 Function *F = M->getFunction("test");
3544 Instruction *I = &findInstructionByName(F, "r");
3545 const AllocaInst *AI = findAllocaForValue(I, true);
3546 EXPECT_EQ(!!AI, GetParam().ZeroOffsetResult);
3547 }
3548
3549 INSTANTIATE_TEST_SUITE_P(FindAllocaForValueTest, FindAllocaForValueTest,
3550 ::testing::ValuesIn(FindAllocaForValueTests));