1 //===- ValueTrackingTest.cpp - ValueTracking tests ------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Analysis/ValueTracking.h"
10 #include "llvm/Analysis/AssumptionCache.h"
11 #include "llvm/AsmParser/Parser.h"
12 #include "llvm/IR/ConstantRange.h"
13 #include "llvm/IR/Dominators.h"
14 #include "llvm/IR/Function.h"
15 #include "llvm/IR/IRBuilder.h"
16 #include "llvm/IR/InstIterator.h"
17 #include "llvm/IR/Instructions.h"
18 #include "llvm/IR/LLVMContext.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/KnownBits.h"
22 #include "llvm/Support/SourceMgr.h"
23 #include "llvm/Transforms/Utils/Local.h"
24 #include "gtest/gtest.h"
30 static Instruction
*findInstructionByNameOrNull(Function
*F
, StringRef Name
) {
31 for (Instruction
&I
: instructions(F
))
32 if (I
.getName() == Name
)
38 static Instruction
&findInstructionByName(Function
*F
, StringRef Name
) {
39 auto *I
= findInstructionByNameOrNull(F
, Name
);
43 llvm_unreachable("Expected value not found");
46 class ValueTrackingTest
: public testing::Test
{
48 std::unique_ptr
<Module
> parseModule(StringRef Assembly
) {
50 std::unique_ptr
<Module
> M
= parseAssemblyString(Assembly
, Error
, Context
);
53 raw_string_ostream
os(errMsg
);
55 EXPECT_TRUE(M
) << errMsg
;
60 void parseAssembly(StringRef Assembly
) {
61 M
= parseModule(Assembly
);
64 F
= M
->getFunction("test");
65 ASSERT_TRUE(F
) << "Test must have a function @test";
69 A
= findInstructionByNameOrNull(F
, "A");
70 ASSERT_TRUE(A
) << "@test must have an instruction %A";
71 A2
= findInstructionByNameOrNull(F
, "A2");
72 A3
= findInstructionByNameOrNull(F
, "A3");
73 A4
= findInstructionByNameOrNull(F
, "A4");
74 A5
= findInstructionByNameOrNull(F
, "A5");
75 A6
= findInstructionByNameOrNull(F
, "A6");
76 A7
= findInstructionByNameOrNull(F
, "A7");
78 CxtI
= findInstructionByNameOrNull(F
, "CxtI");
79 CxtI2
= findInstructionByNameOrNull(F
, "CxtI2");
80 CxtI3
= findInstructionByNameOrNull(F
, "CxtI3");
84 std::unique_ptr
<Module
> M
;
85 Function
*F
= nullptr;
86 Instruction
*A
= nullptr;
87 // Instructions (optional)
88 Instruction
*A2
= nullptr, *A3
= nullptr, *A4
= nullptr, *A5
= nullptr,
89 *A6
= nullptr, *A7
= nullptr;
91 // Context instructions (optional)
92 Instruction
*CxtI
= nullptr, *CxtI2
= nullptr, *CxtI3
= nullptr;
95 class MatchSelectPatternTest
: public ValueTrackingTest
{
97 void expectPattern(const SelectPatternResult
&P
) {
99 Instruction::CastOps CastOp
;
100 SelectPatternResult R
= matchSelectPattern(A
, LHS
, RHS
, &CastOp
);
101 EXPECT_EQ(P
.Flavor
, R
.Flavor
);
102 EXPECT_EQ(P
.NaNBehavior
, R
.NaNBehavior
);
103 EXPECT_EQ(P
.Ordered
, R
.Ordered
);
107 class ComputeKnownBitsTest
: public ValueTrackingTest
{
109 void expectKnownBits(uint64_t Zero
, uint64_t One
) {
110 auto Known
= computeKnownBits(A
, M
->getDataLayout());
111 ASSERT_FALSE(Known
.hasConflict());
112 EXPECT_EQ(Known
.One
.getZExtValue(), One
);
113 EXPECT_EQ(Known
.Zero
.getZExtValue(), Zero
);
117 class ComputeKnownFPClassTest
: public ValueTrackingTest
{
119 void expectKnownFPClass(unsigned KnownTrue
, std::optional
<bool> SignBitKnown
,
120 Instruction
*TestVal
= nullptr) {
124 KnownFPClass Known
= computeKnownFPClass(TestVal
, M
->getDataLayout());
125 EXPECT_EQ(KnownTrue
, Known
.KnownFPClasses
);
126 EXPECT_EQ(SignBitKnown
, Known
.SignBit
);
131 TEST_F(MatchSelectPatternTest
, SimpleFMin
) {
133 "define float @test(float %a) {\n"
134 " %1 = fcmp ult float %a, 5.0\n"
135 " %A = select i1 %1, float %a, float 5.0\n"
138 expectPattern({SPF_FMINNUM
, SPNB_RETURNS_NAN
, false});
141 TEST_F(MatchSelectPatternTest
, SimpleFMax
) {
143 "define float @test(float %a) {\n"
144 " %1 = fcmp ogt float %a, 5.0\n"
145 " %A = select i1 %1, float %a, float 5.0\n"
148 expectPattern({SPF_FMAXNUM
, SPNB_RETURNS_OTHER
, true});
151 TEST_F(MatchSelectPatternTest
, SwappedFMax
) {
153 "define float @test(float %a) {\n"
154 " %1 = fcmp olt float 5.0, %a\n"
155 " %A = select i1 %1, float %a, float 5.0\n"
158 expectPattern({SPF_FMAXNUM
, SPNB_RETURNS_OTHER
, false});
161 TEST_F(MatchSelectPatternTest
, SwappedFMax2
) {
163 "define float @test(float %a) {\n"
164 " %1 = fcmp olt float %a, 5.0\n"
165 " %A = select i1 %1, float 5.0, float %a\n"
168 expectPattern({SPF_FMAXNUM
, SPNB_RETURNS_NAN
, false});
171 TEST_F(MatchSelectPatternTest
, SwappedFMax3
) {
173 "define float @test(float %a) {\n"
174 " %1 = fcmp ult float %a, 5.0\n"
175 " %A = select i1 %1, float 5.0, float %a\n"
178 expectPattern({SPF_FMAXNUM
, SPNB_RETURNS_OTHER
, true});
181 TEST_F(MatchSelectPatternTest
, FastFMin
) {
183 "define float @test(float %a) {\n"
184 " %1 = fcmp nnan olt float %a, 5.0\n"
185 " %A = select i1 %1, float %a, float 5.0\n"
188 expectPattern({SPF_FMINNUM
, SPNB_RETURNS_ANY
, false});
191 TEST_F(MatchSelectPatternTest
, FMinConstantZero
) {
193 "define float @test(float %a) {\n"
194 " %1 = fcmp ole float %a, 0.0\n"
195 " %A = select i1 %1, float %a, float 0.0\n"
198 // This shouldn't be matched, as %a could be -0.0.
199 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
202 TEST_F(MatchSelectPatternTest
, FMinConstantZeroNsz
) {
204 "define float @test(float %a) {\n"
205 " %1 = fcmp nsz ole float %a, 0.0\n"
206 " %A = select i1 %1, float %a, float 0.0\n"
209 // But this should be, because we've ignored signed zeroes.
210 expectPattern({SPF_FMINNUM
, SPNB_RETURNS_OTHER
, true});
213 TEST_F(MatchSelectPatternTest
, FMinMismatchConstantZero1
) {
215 "define float @test(float %a) {\n"
216 " %1 = fcmp olt float -0.0, %a\n"
217 " %A = select i1 %1, float 0.0, float %a\n"
220 // The sign of zero doesn't matter in fcmp.
221 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
224 TEST_F(MatchSelectPatternTest
, FMinMismatchConstantZero2
) {
226 "define float @test(float %a) {\n"
227 " %1 = fcmp ogt float %a, -0.0\n"
228 " %A = select i1 %1, float 0.0, float %a\n"
231 // The sign of zero doesn't matter in fcmp.
232 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
235 TEST_F(MatchSelectPatternTest
, FMinMismatchConstantZero3
) {
237 "define float @test(float %a) {\n"
238 " %1 = fcmp olt float 0.0, %a\n"
239 " %A = select i1 %1, float -0.0, float %a\n"
242 // The sign of zero doesn't matter in fcmp.
243 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
246 TEST_F(MatchSelectPatternTest
, FMinMismatchConstantZero4
) {
248 "define float @test(float %a) {\n"
249 " %1 = fcmp ogt float %a, 0.0\n"
250 " %A = select i1 %1, float -0.0, float %a\n"
253 // The sign of zero doesn't matter in fcmp.
254 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
257 TEST_F(MatchSelectPatternTest
, FMinMismatchConstantZero5
) {
259 "define float @test(float %a) {\n"
260 " %1 = fcmp ogt float -0.0, %a\n"
261 " %A = select i1 %1, float %a, float 0.0\n"
264 // The sign of zero doesn't matter in fcmp.
265 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
268 TEST_F(MatchSelectPatternTest
, FMinMismatchConstantZero6
) {
270 "define float @test(float %a) {\n"
271 " %1 = fcmp olt float %a, -0.0\n"
272 " %A = select i1 %1, float %a, float 0.0\n"
275 // The sign of zero doesn't matter in fcmp.
276 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
279 TEST_F(MatchSelectPatternTest
, FMinMismatchConstantZero7
) {
281 "define float @test(float %a) {\n"
282 " %1 = fcmp ogt float 0.0, %a\n"
283 " %A = select i1 %1, float %a, float -0.0\n"
286 // The sign of zero doesn't matter in fcmp.
287 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
290 TEST_F(MatchSelectPatternTest
, FMinMismatchConstantZero8
) {
292 "define float @test(float %a) {\n"
293 " %1 = fcmp olt float %a, 0.0\n"
294 " %A = select i1 %1, float %a, float -0.0\n"
297 // The sign of zero doesn't matter in fcmp.
298 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
301 TEST_F(MatchSelectPatternTest
, FMaxMismatchConstantZero1
) {
303 "define float @test(float %a) {\n"
304 " %1 = fcmp ogt float -0.0, %a\n"
305 " %A = select i1 %1, float 0.0, float %a\n"
308 // The sign of zero doesn't matter in fcmp.
309 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
312 TEST_F(MatchSelectPatternTest
, FMaxMismatchConstantZero2
) {
314 "define float @test(float %a) {\n"
315 " %1 = fcmp olt float %a, -0.0\n"
316 " %A = select i1 %1, float 0.0, float %a\n"
319 // The sign of zero doesn't matter in fcmp.
320 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
323 TEST_F(MatchSelectPatternTest
, FMaxMismatchConstantZero3
) {
325 "define float @test(float %a) {\n"
326 " %1 = fcmp ogt float 0.0, %a\n"
327 " %A = select i1 %1, float -0.0, float %a\n"
330 // The sign of zero doesn't matter in fcmp.
331 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
334 TEST_F(MatchSelectPatternTest
, FMaxMismatchConstantZero4
) {
336 "define float @test(float %a) {\n"
337 " %1 = fcmp olt float %a, 0.0\n"
338 " %A = select i1 %1, float -0.0, float %a\n"
341 // The sign of zero doesn't matter in fcmp.
342 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
345 TEST_F(MatchSelectPatternTest
, FMaxMismatchConstantZero5
) {
347 "define float @test(float %a) {\n"
348 " %1 = fcmp olt float -0.0, %a\n"
349 " %A = select i1 %1, float %a, float 0.0\n"
352 // The sign of zero doesn't matter in fcmp.
353 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
356 TEST_F(MatchSelectPatternTest
, FMaxMismatchConstantZero6
) {
358 "define float @test(float %a) {\n"
359 " %1 = fcmp ogt float %a, -0.0\n"
360 " %A = select i1 %1, float %a, float 0.0\n"
363 // The sign of zero doesn't matter in fcmp.
364 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
367 TEST_F(MatchSelectPatternTest
, FMaxMismatchConstantZero7
) {
369 "define float @test(float %a) {\n"
370 " %1 = fcmp olt float 0.0, %a\n"
371 " %A = select i1 %1, float %a, float -0.0\n"
374 // The sign of zero doesn't matter in fcmp.
375 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
378 TEST_F(MatchSelectPatternTest
, FMaxMismatchConstantZero8
) {
380 "define float @test(float %a) {\n"
381 " %1 = fcmp ogt float %a, 0.0\n"
382 " %A = select i1 %1, float %a, float -0.0\n"
385 // The sign of zero doesn't matter in fcmp.
386 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
389 TEST_F(MatchSelectPatternTest
, FMinMismatchConstantZeroVecUndef
) {
391 "define <2 x float> @test(<2 x float> %a) {\n"
392 " %1 = fcmp ogt <2 x float> %a, <float -0.0, float -0.0>\n"
393 " %A = select <2 x i1> %1, <2 x float> <float undef, float 0.0>, <2 x float> %a\n"
394 " ret <2 x float> %A\n"
396 // An undef in a vector constant can not be back-propagated for this analysis.
397 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
400 TEST_F(MatchSelectPatternTest
, FMaxMismatchConstantZeroVecUndef
) {
402 "define <2 x float> @test(<2 x float> %a) {\n"
403 " %1 = fcmp ogt <2 x float> %a, zeroinitializer\n"
404 " %A = select <2 x i1> %1, <2 x float> %a, <2 x float> <float -0.0, float undef>\n"
405 " ret <2 x float> %A\n"
407 // An undef in a vector constant can not be back-propagated for this analysis.
408 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
411 TEST_F(MatchSelectPatternTest
, VectorFMinimum
) {
413 "define <4 x float> @test(<4 x float> %a) {\n"
414 " %1 = fcmp ule <4 x float> %a, \n"
415 " <float 5.0, float 5.0, float 5.0, float 5.0>\n"
416 " %A = select <4 x i1> %1, <4 x float> %a,\n"
417 " <4 x float> <float 5.0, float 5.0, float 5.0, float 5.0>\n"
418 " ret <4 x float> %A\n"
420 // Check that pattern matching works on vectors where each lane has the same
421 // unordered pattern.
422 expectPattern({SPF_FMINNUM
, SPNB_RETURNS_NAN
, false});
425 TEST_F(MatchSelectPatternTest
, VectorFMinOtherOrdered
) {
427 "define <4 x float> @test(<4 x float> %a) {\n"
428 " %1 = fcmp ole <4 x float> %a, \n"
429 " <float 5.0, float 5.0, float 5.0, float 5.0>\n"
430 " %A = select <4 x i1> %1, <4 x float> %a,\n"
431 " <4 x float> <float 5.0, float 5.0, float 5.0, float 5.0>\n"
432 " ret <4 x float> %A\n"
434 // Check that pattern matching works on vectors where each lane has the same
436 expectPattern({SPF_FMINNUM
, SPNB_RETURNS_OTHER
, true});
439 TEST_F(MatchSelectPatternTest
, VectorNotFMinimum
) {
441 "define <4 x float> @test(<4 x float> %a) {\n"
442 " %1 = fcmp ule <4 x float> %a, \n"
443 " <float 5.0, float 0x7ff8000000000000, float 5.0, float 5.0>\n"
444 " %A = select <4 x i1> %1, <4 x float> %a,\n"
445 " <4 x float> <float 5.0, float 0x7ff8000000000000, float 5.0, float "
447 " ret <4 x float> %A\n"
449 // The lane that contains a NaN (0x7ff80...) behaves like a
450 // non-NaN-propagating min and the other lines behave like a NaN-propagating
451 // min, so check that neither is returned.
452 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
455 TEST_F(MatchSelectPatternTest
, VectorNotFMinZero
) {
457 "define <4 x float> @test(<4 x float> %a) {\n"
458 " %1 = fcmp ule <4 x float> %a, \n"
459 " <float 5.0, float -0.0, float 5.0, float 5.0>\n"
460 " %A = select <4 x i1> %1, <4 x float> %a,\n"
461 " <4 x float> <float 5.0, float 0.0, float 5.0, float 5.0>\n"
462 " ret <4 x float> %A\n"
464 // Always selects the second lane of %a if it is positive or negative zero, so
465 // this is stricter than a min.
466 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
469 TEST_F(MatchSelectPatternTest
, DoubleCastU
) {
471 "define i32 @test(i8 %a, i8 %b) {\n"
472 " %1 = icmp ult i8 %a, %b\n"
473 " %2 = zext i8 %a to i32\n"
474 " %3 = zext i8 %b to i32\n"
475 " %A = select i1 %1, i32 %2, i32 %3\n"
478 // We should be able to look through the situation where we cast both operands
480 expectPattern({SPF_UMIN
, SPNB_NA
, false});
483 TEST_F(MatchSelectPatternTest
, DoubleCastS
) {
485 "define i32 @test(i8 %a, i8 %b) {\n"
486 " %1 = icmp slt i8 %a, %b\n"
487 " %2 = sext i8 %a to i32\n"
488 " %3 = sext i8 %b to i32\n"
489 " %A = select i1 %1, i32 %2, i32 %3\n"
492 // We should be able to look through the situation where we cast both operands
494 expectPattern({SPF_SMIN
, SPNB_NA
, false});
497 TEST_F(MatchSelectPatternTest
, DoubleCastBad
) {
499 "define i32 @test(i8 %a, i8 %b) {\n"
500 " %1 = icmp ult i8 %a, %b\n"
501 " %2 = zext i8 %a to i32\n"
502 " %3 = sext i8 %b to i32\n"
503 " %A = select i1 %1, i32 %2, i32 %3\n"
506 // The cast types here aren't the same, so we cannot match an UMIN.
507 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
510 TEST_F(MatchSelectPatternTest
, NotNotSMin
) {
512 "define i8 @test(i8 %a, i8 %b) {\n"
513 " %cmp = icmp sgt i8 %a, %b\n"
514 " %an = xor i8 %a, -1\n"
515 " %bn = xor i8 %b, -1\n"
516 " %A = select i1 %cmp, i8 %an, i8 %bn\n"
519 expectPattern({SPF_SMIN
, SPNB_NA
, false});
522 TEST_F(MatchSelectPatternTest
, NotNotSMinSwap
) {
524 "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n"
525 " %cmp = icmp slt <2 x i8> %a, %b\n"
526 " %an = xor <2 x i8> %a, <i8 -1, i8-1>\n"
527 " %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n"
528 " %A = select <2 x i1> %cmp, <2 x i8> %bn, <2 x i8> %an\n"
531 expectPattern({SPF_SMIN
, SPNB_NA
, false});
534 TEST_F(MatchSelectPatternTest
, NotNotSMax
) {
536 "define i8 @test(i8 %a, i8 %b) {\n"
537 " %cmp = icmp slt i8 %a, %b\n"
538 " %an = xor i8 %a, -1\n"
539 " %bn = xor i8 %b, -1\n"
540 " %A = select i1 %cmp, i8 %an, i8 %bn\n"
543 expectPattern({SPF_SMAX
, SPNB_NA
, false});
546 TEST_F(MatchSelectPatternTest
, NotNotSMaxSwap
) {
548 "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n"
549 " %cmp = icmp sgt <2 x i8> %a, %b\n"
550 " %an = xor <2 x i8> %a, <i8 -1, i8-1>\n"
551 " %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n"
552 " %A = select <2 x i1> %cmp, <2 x i8> %bn, <2 x i8> %an\n"
555 expectPattern({SPF_SMAX
, SPNB_NA
, false});
558 TEST_F(MatchSelectPatternTest
, NotNotUMin
) {
560 "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n"
561 " %cmp = icmp ugt <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> %an, <2 x i8> %bn\n"
567 expectPattern({SPF_UMIN
, SPNB_NA
, false});
570 TEST_F(MatchSelectPatternTest
, NotNotUMinSwap
) {
572 "define i8 @test(i8 %a, i8 %b) {\n"
573 " %cmp = icmp ult i8 %a, %b\n"
574 " %an = xor i8 %a, -1\n"
575 " %bn = xor i8 %b, -1\n"
576 " %A = select i1 %cmp, i8 %bn, i8 %an\n"
579 expectPattern({SPF_UMIN
, SPNB_NA
, false});
582 TEST_F(MatchSelectPatternTest
, NotNotUMax
) {
584 "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n"
585 " %cmp = icmp ult <2 x i8> %a, %b\n"
586 " %an = xor <2 x i8> %a, <i8 -1, i8-1>\n"
587 " %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n"
588 " %A = select <2 x i1> %cmp, <2 x i8> %an, <2 x i8> %bn\n"
591 expectPattern({SPF_UMAX
, SPNB_NA
, false});
594 TEST_F(MatchSelectPatternTest
, NotNotUMaxSwap
) {
596 "define i8 @test(i8 %a, i8 %b) {\n"
597 " %cmp = icmp ugt i8 %a, %b\n"
598 " %an = xor i8 %a, -1\n"
599 " %bn = xor i8 %b, -1\n"
600 " %A = select i1 %cmp, i8 %bn, i8 %an\n"
603 expectPattern({SPF_UMAX
, SPNB_NA
, false});
606 TEST_F(MatchSelectPatternTest
, NotNotEq
) {
608 "define i8 @test(i8 %a, i8 %b) {\n"
609 " %cmp = icmp eq 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"
615 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
618 TEST_F(MatchSelectPatternTest
, NotNotNe
) {
620 "define i8 @test(i8 %a, i8 %b) {\n"
621 " %cmp = icmp ne 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"
627 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
630 TEST(ValueTracking
, GuaranteedToTransferExecutionToSuccessor
) {
632 "declare void @nounwind_readonly(ptr) nounwind readonly "
633 "declare void @nounwind_argmemonly(ptr) nounwind argmemonly "
634 "declare void @nounwind_willreturn(ptr) nounwind willreturn "
635 "declare void @throws_but_readonly(ptr) readonly "
636 "declare void @throws_but_argmemonly(ptr) argmemonly "
637 "declare void @throws_but_willreturn(ptr) willreturn "
639 "declare void @unknown(ptr) "
641 "define void @f(ptr %p) { "
642 " call void @nounwind_readonly(ptr %p) "
643 " call void @nounwind_argmemonly(ptr %p) "
644 " call void @nounwind_willreturn(ptr %p)"
645 " call void @throws_but_readonly(ptr %p) "
646 " call void @throws_but_argmemonly(ptr %p) "
647 " call void @throws_but_willreturn(ptr %p) "
648 " call void @unknown(ptr %p) nounwind readonly "
649 " call void @unknown(ptr %p) nounwind argmemonly "
650 " call void @unknown(ptr %p) nounwind willreturn "
651 " call void @unknown(ptr %p) readonly "
652 " call void @unknown(ptr %p) argmemonly "
653 " call void @unknown(ptr %p) willreturn "
659 auto M
= parseAssemblyString(Assembly
, Error
, Context
);
660 assert(M
&& "Bad assembly?");
662 auto *F
= M
->getFunction("f");
663 assert(F
&& "Bad assembly?");
665 auto &BB
= F
->getEntryBlock();
666 bool ExpectedAnswers
[] = {
667 false, // call void @nounwind_readonly(ptr %p)
668 false, // call void @nounwind_argmemonly(ptr %p)
669 true, // call void @nounwind_willreturn(ptr %p)
670 false, // call void @throws_but_readonly(ptr %p)
671 false, // call void @throws_but_argmemonly(ptr %p)
672 false, // call void @throws_but_willreturn(ptr %p)
673 false, // call void @unknown(ptr %p) nounwind readonly
674 false, // call void @unknown(ptr %p) nounwind argmemonly
675 true, // call void @unknown(ptr %p) nounwind willreturn
676 false, // call void @unknown(ptr %p) readonly
677 false, // call void @unknown(ptr %p) argmemonly
678 false, // call void @unknown(ptr %p) willreturn
684 EXPECT_EQ(isGuaranteedToTransferExecutionToSuccessor(&I
),
685 ExpectedAnswers
[Index
])
686 << "Incorrect answer at instruction " << Index
<< " = " << I
;
691 TEST_F(ValueTrackingTest
, ComputeNumSignBits_PR32045
) {
693 "define i32 @test(i32 %a) {\n"
694 " %A = ashr i32 %a, -1\n"
697 EXPECT_EQ(ComputeNumSignBits(A
, M
->getDataLayout()), 32u);
700 // No guarantees for canonical IR in this analysis, so this just bails out.
701 TEST_F(ValueTrackingTest
, ComputeNumSignBits_Shuffle
) {
703 "define <2 x i32> @test() {\n"
704 " %A = shufflevector <2 x i32> undef, <2 x i32> undef, <2 x i32> <i32 0, i32 0>\n"
705 " ret <2 x i32> %A\n"
707 EXPECT_EQ(ComputeNumSignBits(A
, M
->getDataLayout()), 1u);
710 // No guarantees for canonical IR in this analysis, so a shuffle element that
711 // references an undef value means this can't return any extra information.
712 TEST_F(ValueTrackingTest
, ComputeNumSignBits_Shuffle2
) {
714 "define <2 x i32> @test(<2 x i1> %x) {\n"
715 " %sext = sext <2 x i1> %x to <2 x i32>\n"
716 " %A = shufflevector <2 x i32> %sext, <2 x i32> undef, <2 x i32> <i32 0, i32 2>\n"
717 " ret <2 x i32> %A\n"
719 EXPECT_EQ(ComputeNumSignBits(A
, M
->getDataLayout()), 1u);
722 TEST_F(ValueTrackingTest
, impliesPoisonTest_Identity
) {
723 parseAssembly("define void @test(i32 %x, i32 %y) {\n"
724 " %A = add i32 %x, %y\n"
727 EXPECT_TRUE(impliesPoison(A
, A
));
730 TEST_F(ValueTrackingTest
, impliesPoisonTest_ICmp
) {
731 parseAssembly("define void @test(i32 %x) {\n"
732 " %A2 = icmp eq i32 %x, 0\n"
733 " %A = icmp eq i32 %x, 1\n"
736 EXPECT_TRUE(impliesPoison(A2
, A
));
739 TEST_F(ValueTrackingTest
, impliesPoisonTest_ICmpUnknown
) {
740 parseAssembly("define void @test(i32 %x, i32 %y) {\n"
741 " %A2 = icmp eq i32 %x, %y\n"
742 " %A = icmp eq i32 %x, 1\n"
745 EXPECT_FALSE(impliesPoison(A2
, A
));
748 TEST_F(ValueTrackingTest
, impliesPoisonTest_AddNswOkay
) {
749 parseAssembly("define void @test(i32 %x) {\n"
750 " %A2 = add nsw i32 %x, 1\n"
751 " %A = add i32 %A2, 1\n"
754 EXPECT_TRUE(impliesPoison(A2
, A
));
757 TEST_F(ValueTrackingTest
, impliesPoisonTest_AddNswOkay2
) {
758 parseAssembly("define void @test(i32 %x) {\n"
759 " %A2 = add i32 %x, 1\n"
760 " %A = add nsw i32 %A2, 1\n"
763 EXPECT_TRUE(impliesPoison(A2
, A
));
766 TEST_F(ValueTrackingTest
, impliesPoisonTest_AddNsw
) {
767 parseAssembly("define void @test(i32 %x) {\n"
768 " %A2 = add nsw i32 %x, 1\n"
769 " %A = add i32 %x, 1\n"
772 EXPECT_FALSE(impliesPoison(A2
, A
));
775 TEST_F(ValueTrackingTest
, impliesPoisonTest_Cmp
) {
776 parseAssembly("define void @test(i32 %x, i32 %y, i1 %c) {\n"
777 " %A2 = icmp eq i32 %x, %y\n"
778 " %A0 = icmp ult i32 %x, %y\n"
779 " %A = or i1 %A0, %c\n"
782 EXPECT_TRUE(impliesPoison(A2
, A
));
785 TEST_F(ValueTrackingTest
, impliesPoisonTest_FCmpFMF
) {
786 parseAssembly("define void @test(float %x, float %y, i1 %c) {\n"
787 " %A2 = fcmp nnan oeq float %x, %y\n"
788 " %A0 = fcmp olt float %x, %y\n"
789 " %A = or i1 %A0, %c\n"
792 EXPECT_FALSE(impliesPoison(A2
, A
));
795 TEST_F(ValueTrackingTest
, impliesPoisonTest_AddSubSameOps
) {
796 parseAssembly("define void @test(i32 %x, i32 %y, i1 %c) {\n"
797 " %A2 = add i32 %x, %y\n"
798 " %A = sub i32 %x, %y\n"
801 EXPECT_TRUE(impliesPoison(A2
, A
));
804 TEST_F(ValueTrackingTest
, impliesPoisonTest_MaskCmp
) {
805 parseAssembly("define void @test(i32 %x, i32 %y, i1 %c) {\n"
806 " %M2 = and i32 %x, 7\n"
807 " %A2 = icmp eq i32 %M2, 1\n"
808 " %M = and i32 %x, 15\n"
809 " %A = icmp eq i32 %M, 3\n"
812 EXPECT_TRUE(impliesPoison(A2
, A
));
815 TEST_F(ValueTrackingTest
, ComputeNumSignBits_Shuffle_Pointers
) {
817 "define <2 x ptr> @test(<2 x ptr> %x) {\n"
818 " %A = shufflevector <2 x ptr> zeroinitializer, <2 x ptr> undef, <2 x i32> zeroinitializer\n"
819 " ret <2 x ptr> %A\n"
821 EXPECT_EQ(ComputeNumSignBits(A
, M
->getDataLayout()), 64u);
824 TEST(ValueTracking
, propagatesPoison
) {
825 std::string AsmHead
=
826 "declare i32 @g(i32)\n"
827 "define void @f(i32 %x, i32 %y, i32 %shamt, float %fx, float %fy, "
828 "i1 %cond, ptr %p) {\n";
829 std::string AsmTail
= " ret void\n}";
830 // (propagates poison?, IR instruction)
831 SmallVector
<std::tuple
<bool, std::string
, unsigned>, 32> Data
= {
832 {true, "add i32 %x, %y", 0},
833 {true, "add i32 %x, %y", 1},
834 {true, "add nsw nuw i32 %x, %y", 0},
835 {true, "add nsw nuw i32 %x, %y", 1},
836 {true, "ashr i32 %x, %y", 0},
837 {true, "ashr i32 %x, %y", 1},
838 {true, "lshr exact i32 %x, 31", 0},
839 {true, "lshr exact i32 %x, 31", 1},
840 {true, "fadd float %fx, %fy", 0},
841 {true, "fadd float %fx, %fy", 1},
842 {true, "fsub float %fx, %fy", 0},
843 {true, "fsub float %fx, %fy", 1},
844 {true, "fmul float %fx, %fy", 0},
845 {true, "fmul float %fx, %fy", 1},
846 {true, "fdiv float %fx, %fy", 0},
847 {true, "fdiv float %fx, %fy", 1},
848 {true, "frem float %fx, %fy", 0},
849 {true, "frem float %fx, %fy", 1},
850 {true, "fneg float %fx", 0},
851 {true, "fcmp oeq float %fx, %fy", 0},
852 {true, "fcmp oeq float %fx, %fy", 1},
853 {true, "icmp eq i32 %x, %y", 0},
854 {true, "icmp eq i32 %x, %y", 1},
855 {true, "getelementptr i8, ptr %p, i32 %x", 0},
856 {true, "getelementptr i8, ptr %p, i32 %x", 1},
857 {true, "getelementptr inbounds i8, ptr %p, i32 %x", 0},
858 {true, "getelementptr inbounds i8, ptr %p, i32 %x", 1},
859 {true, "bitcast float %fx to i32", 0},
860 {true, "select i1 %cond, i32 %x, i32 %y", 0},
861 {false, "select i1 %cond, i32 %x, i32 %y", 1},
862 {false, "select i1 %cond, i32 %x, i32 %y", 2},
863 {false, "freeze i32 %x", 0},
864 {true, "udiv i32 %x, %y", 0},
865 {true, "udiv i32 %x, %y", 1},
866 {true, "urem i32 %x, %y", 0},
867 {true, "urem i32 %x, %y", 1},
868 {true, "sdiv exact i32 %x, %y", 0},
869 {true, "sdiv exact i32 %x, %y", 1},
870 {true, "srem i32 %x, %y", 0},
871 {true, "srem i32 %x, %y", 1},
872 {false, "call i32 @g(i32 %x)", 0},
873 {false, "call i32 @g(i32 %x)", 1},
874 {true, "call {i32, i1} @llvm.sadd.with.overflow.i32(i32 %x, i32 %y)", 0},
875 {true, "call {i32, i1} @llvm.ssub.with.overflow.i32(i32 %x, i32 %y)", 0},
876 {true, "call {i32, i1} @llvm.smul.with.overflow.i32(i32 %x, i32 %y)", 0},
877 {true, "call {i32, i1} @llvm.uadd.with.overflow.i32(i32 %x, i32 %y)", 0},
878 {true, "call {i32, i1} @llvm.usub.with.overflow.i32(i32 %x, i32 %y)", 0},
879 {true, "call {i32, i1} @llvm.umul.with.overflow.i32(i32 %x, i32 %y)", 0},
880 {true, "call i32 @llvm.sadd.sat.i32(i32 %x, i32 %y)", 0},
881 {true, "call i32 @llvm.ssub.sat.i32(i32 %x, i32 %y)", 0},
882 {true, "call i32 @llvm.sshl.sat.i32(i32 %x, i32 %y)", 0},
883 {true, "call i32 @llvm.uadd.sat.i32(i32 %x, i32 %y)", 0},
884 {true, "call i32 @llvm.usub.sat.i32(i32 %x, i32 %y)", 0},
885 {true, "call i32 @llvm.ushl.sat.i32(i32 %x, i32 %y)", 0},
886 {true, "call i32 @llvm.ctpop.i32(i32 %x)", 0},
887 {true, "call i32 @llvm.ctlz.i32(i32 %x, i1 true)", 0},
888 {true, "call i32 @llvm.cttz.i32(i32 %x, i1 true)", 0},
889 {true, "call i32 @llvm.abs.i32(i32 %x, i1 true)", 0},
890 {true, "call i32 @llvm.smax.i32(i32 %x, i32 %y)", 0},
891 {true, "call i32 @llvm.smin.i32(i32 %x, i32 %y)", 0},
892 {true, "call i32 @llvm.umax.i32(i32 %x, i32 %y)", 0},
893 {true, "call i32 @llvm.umin.i32(i32 %x, i32 %y)", 0},
894 {true, "call i32 @llvm.bitreverse.i32(i32 %x)", 0},
895 {true, "call i32 @llvm.bswap.i32(i32 %x)", 0},
896 {false, "call i32 @llvm.fshl.i32(i32 %x, i32 %y, i32 %shamt)", 0},
897 {false, "call i32 @llvm.fshl.i32(i32 %x, i32 %y, i32 %shamt)", 1},
898 {false, "call i32 @llvm.fshl.i32(i32 %x, i32 %y, i32 %shamt)", 2},
899 {false, "call i32 @llvm.fshr.i32(i32 %x, i32 %y, i32 %shamt)", 0},
900 {false, "call i32 @llvm.fshr.i32(i32 %x, i32 %y, i32 %shamt)", 1},
901 {false, "call i32 @llvm.fshr.i32(i32 %x, i32 %y, i32 %shamt)", 2},
902 {false, "call float @llvm.sqrt.f32(float %fx)", 0},
903 {false, "call float @llvm.powi.f32.i32(float %fx, i32 %x)", 0},
904 {false, "call float @llvm.sin.f32(float %fx)", 0},
905 {false, "call float @llvm.cos.f32(float %fx)", 0},
906 {false, "call float @llvm.pow.f32(float %fx, float %fy)", 0},
907 {false, "call float @llvm.exp.f32(float %fx)", 0},
908 {false, "call float @llvm.exp2.f32(float %fx)", 0},
909 {false, "call float @llvm.log.f32(float %fx)", 0},
910 {false, "call float @llvm.log10.f32(float %fx)", 0},
911 {false, "call float @llvm.log2.f32(float %fx)", 0},
912 {false, "call float @llvm.fma.f32(float %fx, float %fx, float %fy)", 0},
913 {false, "call float @llvm.fabs.f32(float %fx)", 0},
914 {false, "call float @llvm.minnum.f32(float %fx, float %fy)", 0},
915 {false, "call float @llvm.maxnum.f32(float %fx, float %fy)", 0},
916 {false, "call float @llvm.minimum.f32(float %fx, float %fy)", 0},
917 {false, "call float @llvm.maximum.f32(float %fx, float %fy)", 0},
918 {false, "call float @llvm.copysign.f32(float %fx, float %fy)", 0},
919 {false, "call float @llvm.floor.f32(float %fx)", 0},
920 {false, "call float @llvm.ceil.f32(float %fx)", 0},
921 {false, "call float @llvm.trunc.f32(float %fx)", 0},
922 {false, "call float @llvm.rint.f32(float %fx)", 0},
923 {false, "call float @llvm.nearbyint.f32(float %fx)", 0},
924 {false, "call float @llvm.round.f32(float %fx)", 0},
925 {false, "call float @llvm.roundeven.f32(float %fx)", 0},
926 {false, "call i32 @llvm.lround.f32(float %fx)", 0},
927 {false, "call i64 @llvm.llround.f32(float %fx)", 0},
928 {false, "call i32 @llvm.lrint.f32(float %fx)", 0},
929 {false, "call i64 @llvm.llrint.f32(float %fx)", 0},
930 {false, "call float @llvm.fmuladd.f32(float %fx, float %fx, float %fy)",
933 std::string AssemblyStr
= AsmHead
;
934 for (auto &Itm
: Data
)
935 AssemblyStr
+= std::get
<1>(Itm
) + "\n";
936 AssemblyStr
+= AsmTail
;
940 auto M
= parseAssemblyString(AssemblyStr
, Error
, Context
);
941 assert(M
&& "Bad assembly?");
943 auto *F
= M
->getFunction("f");
944 assert(F
&& "Bad assembly?");
946 auto &BB
= F
->getEntryBlock();
950 if (isa
<ReturnInst
>(&I
))
952 bool ExpectedVal
= std::get
<0>(Data
[Index
]);
953 unsigned OpIdx
= std::get
<2>(Data
[Index
]);
954 EXPECT_EQ(propagatesPoison(I
.getOperandUse(OpIdx
)), ExpectedVal
)
955 << "Incorrect answer at instruction " << Index
<< " = " << I
;
960 TEST_F(ValueTrackingTest
, programUndefinedIfPoison
) {
961 parseAssembly("declare i32 @any_num()"
962 "define void @test(i32 %mask) {\n"
963 " %A = call i32 @any_num()\n"
964 " %B = or i32 %A, %mask\n"
968 // If %A was poison, udiv raises UB regardless of %mask's value
969 EXPECT_EQ(programUndefinedIfPoison(A
), true);
972 TEST_F(ValueTrackingTest
, programUndefinedIfPoisonSelect
) {
973 parseAssembly("declare i32 @any_num()"
974 "define void @test(i1 %Cond) {\n"
975 " %A = call i32 @any_num()\n"
976 " %B = add i32 %A, 1\n"
977 " %C = select i1 %Cond, i32 %A, i32 %B\n"
981 // If A is poison, B is also poison, and therefore C is poison regardless of
982 // the value of %Cond.
983 EXPECT_EQ(programUndefinedIfPoison(A
), true);
986 TEST_F(ValueTrackingTest
, programUndefinedIfUndefOrPoison
) {
987 parseAssembly("declare i32 @any_num()"
988 "define void @test(i32 %mask) {\n"
989 " %A = call i32 @any_num()\n"
990 " %B = or i32 %A, %mask\n"
994 // If %A was undef and %mask was 1, udiv does not raise UB
995 EXPECT_EQ(programUndefinedIfUndefOrPoison(A
), false);
998 TEST_F(ValueTrackingTest
, isGuaranteedNotToBePoison_exploitBranchCond
) {
999 parseAssembly("declare i1 @any_bool()"
1000 "define void @test(i1 %y) {\n"
1001 " %A = call i1 @any_bool()\n"
1002 " %cond = and i1 %A, %y\n"
1003 " br i1 %cond, label %BB1, label %BB2\n"
1009 DominatorTree
DT(*F
);
1010 for (auto &BB
: *F
) {
1011 if (&BB
== &F
->getEntryBlock())
1014 EXPECT_EQ(isGuaranteedNotToBePoison(A
, nullptr, BB
.getTerminator(), &DT
),
1016 << "isGuaranteedNotToBePoison does not hold at " << *BB
.getTerminator();
1020 TEST_F(ValueTrackingTest
, isGuaranteedNotToBePoison_phi
) {
1021 parseAssembly("declare i32 @any_i32(i32)"
1022 "define void @test() {\n"
1026 " %A = phi i32 [0, %ENTRY], [%A.next, %NEXT]\n"
1027 " %A.next = call i32 @any_i32(i32 %A)\n"
1028 " %cond = icmp eq i32 %A.next, 0\n"
1029 " br i1 %cond, label %NEXT, label %EXIT\n"
1035 DominatorTree
DT(*F
);
1036 for (auto &BB
: *F
) {
1037 if (BB
.getName() == "LOOP") {
1038 EXPECT_EQ(isGuaranteedNotToBePoison(A
, nullptr, A
, &DT
), true)
1039 << "isGuaranteedNotToBePoison does not hold";
1044 TEST_F(ValueTrackingTest
, isGuaranteedNotToBeUndefOrPoison
) {
1045 parseAssembly("declare void @f(i32 noundef)"
1046 "define void @test(i32 %x) {\n"
1047 " %A = bitcast i32 %x to i32\n"
1048 " call void @f(i32 noundef %x)\n"
1051 EXPECT_EQ(isGuaranteedNotToBeUndefOrPoison(A
), true);
1052 EXPECT_EQ(isGuaranteedNotToBeUndefOrPoison(UndefValue::get(IntegerType::get(Context
, 8))), false);
1053 EXPECT_EQ(isGuaranteedNotToBeUndefOrPoison(PoisonValue::get(IntegerType::get(Context
, 8))), false);
1054 EXPECT_EQ(isGuaranteedNotToBePoison(UndefValue::get(IntegerType::get(Context
, 8))), true);
1055 EXPECT_EQ(isGuaranteedNotToBePoison(PoisonValue::get(IntegerType::get(Context
, 8))), false);
1057 Type
*Int32Ty
= Type::getInt32Ty(Context
);
1058 Constant
*CU
= UndefValue::get(Int32Ty
);
1059 Constant
*CP
= PoisonValue::get(Int32Ty
);
1060 Constant
*C1
= ConstantInt::get(Int32Ty
, 1);
1061 Constant
*C2
= ConstantInt::get(Int32Ty
, 2);
1064 Constant
*V1
= ConstantVector::get({C1
, C2
});
1065 EXPECT_TRUE(isGuaranteedNotToBeUndefOrPoison(V1
));
1066 EXPECT_TRUE(isGuaranteedNotToBePoison(V1
));
1070 Constant
*V2
= ConstantVector::get({C1
, CU
});
1071 EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(V2
));
1072 EXPECT_TRUE(isGuaranteedNotToBePoison(V2
));
1076 Constant
*V3
= ConstantVector::get({C1
, CP
});
1077 EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(V3
));
1078 EXPECT_FALSE(isGuaranteedNotToBePoison(V3
));
1082 TEST_F(ValueTrackingTest
, isGuaranteedNotToBeUndefOrPoison_assume
) {
1083 parseAssembly("declare i1 @f_i1()\n"
1084 "declare i32 @f_i32()\n"
1085 "declare void @llvm.assume(i1)\n"
1086 "define void @test() {\n"
1087 " %A = call i32 @f_i32()\n"
1088 " %cond = call i1 @f_i1()\n"
1089 " %CxtI = add i32 0, 0\n"
1090 " br i1 %cond, label %BB1, label %EXIT\n"
1092 " %CxtI2 = add i32 0, 0\n"
1093 " %cond2 = call i1 @f_i1()\n"
1094 " call void @llvm.assume(i1 true) [ \"noundef\"(i32 %A) ]\n"
1095 " br i1 %cond2, label %BB2, label %EXIT\n"
1097 " %CxtI3 = add i32 0, 0\n"
1102 AssumptionCache
AC(*F
);
1103 DominatorTree
DT(*F
);
1104 EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(A
, &AC
, CxtI
, &DT
));
1105 EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(A
, &AC
, CxtI2
, &DT
));
1106 EXPECT_TRUE(isGuaranteedNotToBeUndefOrPoison(A
, &AC
, CxtI3
, &DT
));
1109 TEST(ValueTracking
, canCreatePoisonOrUndef
) {
1110 std::string AsmHead
=
1111 "@s = external dso_local global i32, align 1\n"
1112 "declare i32 @g(i32)\n"
1113 "declare {i32, i1} @llvm.sadd.with.overflow.i32(i32 %a, i32 %b)\n"
1114 "declare {i32, i1} @llvm.ssub.with.overflow.i32(i32 %a, i32 %b)\n"
1115 "declare {i32, i1} @llvm.smul.with.overflow.i32(i32 %a, i32 %b)\n"
1116 "declare {i32, i1} @llvm.uadd.with.overflow.i32(i32 %a, i32 %b)\n"
1117 "declare {i32, i1} @llvm.usub.with.overflow.i32(i32 %a, i32 %b)\n"
1118 "declare {i32, i1} @llvm.umul.with.overflow.i32(i32 %a, i32 %b)\n"
1119 "define void @f(i32 %x, i32 %y, float %fx, float %fy, i1 %cond, "
1120 "<4 x i32> %vx, <4 x i32> %vx2, <vscale x 4 x i32> %svx, ptr %p) {\n";
1121 std::string AsmTail
= " ret void\n}";
1122 // (can create poison?, can create undef?, IR instruction)
1123 SmallVector
<std::pair
<std::pair
<bool, bool>, std::string
>, 32> Data
= {
1124 {{false, false}, "add i32 %x, %y"},
1125 {{true, false}, "add nsw nuw i32 %x, %y"},
1126 {{true, false}, "shl i32 %x, %y"},
1127 {{true, false}, "shl <4 x i32> %vx, %vx2"},
1128 {{true, false}, "shl nsw i32 %x, %y"},
1129 {{true, false}, "shl nsw <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"},
1130 {{false, false}, "shl i32 %x, 31"},
1131 {{true, false}, "shl i32 %x, 32"},
1132 {{false, false}, "shl <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"},
1133 {{true, false}, "shl <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 32>"},
1134 {{true, false}, "ashr i32 %x, %y"},
1135 {{true, false}, "ashr exact i32 %x, %y"},
1136 {{false, false}, "ashr i32 %x, 31"},
1137 {{true, false}, "ashr exact i32 %x, 31"},
1138 {{false, false}, "ashr <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"},
1139 {{true, false}, "ashr <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 32>"},
1140 {{true, false}, "ashr exact <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"},
1141 {{true, false}, "lshr i32 %x, %y"},
1142 {{true, false}, "lshr exact i32 %x, 31"},
1143 {{false, false}, "udiv i32 %x, %y"},
1144 {{true, false}, "udiv exact i32 %x, %y"},
1145 {{false, false}, "getelementptr i8, ptr %p, i32 %x"},
1146 {{true, false}, "getelementptr inbounds i8, ptr %p, i32 %x"},
1147 {{true, false}, "fneg nnan float %fx"},
1148 {{false, false}, "fneg float %fx"},
1149 {{false, false}, "fadd float %fx, %fy"},
1150 {{true, false}, "fadd nnan float %fx, %fy"},
1151 {{false, false}, "urem i32 %x, %y"},
1152 {{true, false}, "fptoui float %fx to i32"},
1153 {{true, false}, "fptosi float %fx to i32"},
1154 {{false, false}, "bitcast float %fx to i32"},
1155 {{false, false}, "select i1 %cond, i32 %x, i32 %y"},
1156 {{true, false}, "select nnan i1 %cond, float %fx, float %fy"},
1157 {{true, false}, "extractelement <4 x i32> %vx, i32 %x"},
1158 {{false, false}, "extractelement <4 x i32> %vx, i32 3"},
1159 {{true, false}, "extractelement <vscale x 4 x i32> %svx, i32 4"},
1160 {{true, false}, "insertelement <4 x i32> %vx, i32 %x, i32 %y"},
1161 {{false, false}, "insertelement <4 x i32> %vx, i32 %x, i32 3"},
1162 {{true, false}, "insertelement <vscale x 4 x i32> %svx, i32 %x, i32 4"},
1163 {{false, false}, "freeze i32 %x"},
1165 "shufflevector <4 x i32> %vx, <4 x i32> %vx2, "
1166 "<4 x i32> <i32 0, i32 1, i32 2, i32 3>"},
1168 "shufflevector <4 x i32> %vx, <4 x i32> %vx2, "
1169 "<4 x i32> <i32 0, i32 1, i32 2, i32 poison>"},
1171 "shufflevector <vscale x 4 x i32> %svx, "
1172 "<vscale x 4 x i32> %svx, <vscale x 4 x i32> poison"},
1173 {{true, false}, "call i32 @g(i32 %x)"},
1174 {{false, false}, "call noundef i32 @g(i32 %x)"},
1175 {{true, false}, "fcmp nnan oeq float %fx, %fy"},
1176 {{false, false}, "fcmp oeq float %fx, %fy"},
1177 {{true, false}, "ashr i32 %x, ptrtoint (ptr @s to i32)"},
1179 "call {i32, i1} @llvm.sadd.with.overflow.i32(i32 %x, i32 %y)"},
1181 "call {i32, i1} @llvm.ssub.with.overflow.i32(i32 %x, i32 %y)"},
1183 "call {i32, i1} @llvm.smul.with.overflow.i32(i32 %x, i32 %y)"},
1185 "call {i32, i1} @llvm.uadd.with.overflow.i32(i32 %x, i32 %y)"},
1187 "call {i32, i1} @llvm.usub.with.overflow.i32(i32 %x, i32 %y)"},
1189 "call {i32, i1} @llvm.umul.with.overflow.i32(i32 %x, i32 %y)"}};
1191 std::string AssemblyStr
= AsmHead
;
1192 for (auto &Itm
: Data
)
1193 AssemblyStr
+= Itm
.second
+ "\n";
1194 AssemblyStr
+= AsmTail
;
1196 LLVMContext Context
;
1198 auto M
= parseAssemblyString(AssemblyStr
, Error
, Context
);
1199 assert(M
&& "Bad assembly?");
1201 auto *F
= M
->getFunction("f");
1202 assert(F
&& "Bad assembly?");
1204 auto &BB
= F
->getEntryBlock();
1207 for (auto &I
: BB
) {
1208 if (isa
<ReturnInst
>(&I
))
1210 bool Poison
= Data
[Index
].first
.first
;
1211 bool Undef
= Data
[Index
].first
.second
;
1212 EXPECT_EQ(canCreatePoison(cast
<Operator
>(&I
)), Poison
)
1213 << "Incorrect answer of canCreatePoison at instruction " << Index
1215 EXPECT_EQ(canCreateUndefOrPoison(cast
<Operator
>(&I
)), Undef
|| Poison
)
1216 << "Incorrect answer of canCreateUndef at instruction " << Index
1222 TEST_F(ValueTrackingTest
, computePtrAlignment
) {
1223 parseAssembly("declare i1 @f_i1()\n"
1224 "declare ptr @f_i8p()\n"
1225 "declare void @llvm.assume(i1)\n"
1226 "define void @test() {\n"
1227 " %A = call ptr @f_i8p()\n"
1228 " %cond = call i1 @f_i1()\n"
1229 " %CxtI = add i32 0, 0\n"
1230 " br i1 %cond, label %BB1, label %EXIT\n"
1232 " %CxtI2 = add i32 0, 0\n"
1233 " %cond2 = call i1 @f_i1()\n"
1234 " call void @llvm.assume(i1 true) [ \"align\"(ptr %A, i64 16) ]\n"
1235 " br i1 %cond2, label %BB2, label %EXIT\n"
1237 " %CxtI3 = add i32 0, 0\n"
1242 AssumptionCache
AC(*F
);
1243 DominatorTree
DT(*F
);
1244 const DataLayout
&DL
= M
->getDataLayout();
1245 EXPECT_EQ(getKnownAlignment(A
, DL
, CxtI
, &AC
, &DT
), Align(1));
1246 EXPECT_EQ(getKnownAlignment(A
, DL
, CxtI2
, &AC
, &DT
), Align(1));
1247 EXPECT_EQ(getKnownAlignment(A
, DL
, CxtI3
, &AC
, &DT
), Align(16));
1250 TEST_F(ComputeKnownBitsTest
, ComputeKnownBits
) {
1252 "define i32 @test(i32 %a, i32 %b) {\n"
1253 " %ash = mul i32 %a, 8\n"
1254 " %aad = add i32 %ash, 7\n"
1255 " %aan = and i32 %aad, 4095\n"
1256 " %bsh = shl i32 %b, 4\n"
1257 " %bad = or i32 %bsh, 6\n"
1258 " %ban = and i32 %bad, 4095\n"
1259 " %A = mul i32 %aan, %ban\n"
1262 expectKnownBits(/*zero*/ 4278190085u, /*one*/ 10u);
1265 TEST_F(ComputeKnownBitsTest
, ComputeKnownMulBits
) {
1267 "define i32 @test(i32 %a, i32 %b) {\n"
1268 " %aa = shl i32 %a, 5\n"
1269 " %bb = shl i32 %b, 5\n"
1270 " %aaa = or i32 %aa, 24\n"
1271 " %bbb = or i32 %bb, 28\n"
1272 " %A = mul i32 %aaa, %bbb\n"
1275 expectKnownBits(/*zero*/ 95u, /*one*/ 32u);
1278 TEST_F(ComputeKnownFPClassTest
, SelectPos0
) {
1280 "define float @test(i1 %cond) {\n"
1281 " %A = select i1 %cond, float 0.0, float 0.0"
1284 expectKnownFPClass(fcPosZero
, false);
1287 TEST_F(ComputeKnownFPClassTest
, SelectNeg0
) {
1289 "define float @test(i1 %cond) {\n"
1290 " %A = select i1 %cond, float -0.0, float -0.0"
1293 expectKnownFPClass(fcNegZero
, true);
1296 TEST_F(ComputeKnownFPClassTest
, SelectPosOrNeg0
) {
1298 "define float @test(i1 %cond) {\n"
1299 " %A = select i1 %cond, float 0.0, float -0.0"
1302 expectKnownFPClass(fcZero
, std::nullopt
);
1305 TEST_F(ComputeKnownFPClassTest
, SelectPosInf
) {
1307 "define float @test(i1 %cond) {\n"
1308 " %A = select i1 %cond, float 0x7FF0000000000000, float 0x7FF0000000000000"
1311 expectKnownFPClass(fcPosInf
, false);
1314 TEST_F(ComputeKnownFPClassTest
, SelectNegInf
) {
1316 "define float @test(i1 %cond) {\n"
1317 " %A = select i1 %cond, float 0xFFF0000000000000, float 0xFFF0000000000000"
1320 expectKnownFPClass(fcNegInf
, true);
1323 TEST_F(ComputeKnownFPClassTest
, SelectPosOrNegInf
) {
1325 "define float @test(i1 %cond) {\n"
1326 " %A = select i1 %cond, float 0x7FF0000000000000, float 0xFFF0000000000000"
1329 expectKnownFPClass(fcInf
, std::nullopt
);
1332 TEST_F(ComputeKnownFPClassTest
, SelectNNaN
) {
1334 "define float @test(i1 %cond, float %arg0, float %arg1) {\n"
1335 " %A = select nnan i1 %cond, float %arg0, float %arg1"
1338 expectKnownFPClass(~fcNan
, std::nullopt
);
1341 TEST_F(ComputeKnownFPClassTest
, SelectNInf
) {
1343 "define float @test(i1 %cond, float %arg0, float %arg1) {\n"
1344 " %A = select ninf i1 %cond, float %arg0, float %arg1"
1347 expectKnownFPClass(~fcInf
, std::nullopt
);
1350 TEST_F(ComputeKnownFPClassTest
, SelectNNaNNInf
) {
1352 "define float @test(i1 %cond, float %arg0, float %arg1) {\n"
1353 " %A = select nnan ninf i1 %cond, float %arg0, float %arg1"
1356 expectKnownFPClass(~(fcNan
| fcInf
), std::nullopt
);
1359 TEST_F(ComputeKnownFPClassTest
, SelectNoFPClassArgUnionAll
) {
1361 "define float @test(i1 %cond, float nofpclass(snan ninf nsub pzero pnorm) %arg0, float nofpclass(qnan nnorm nzero psub pinf) %arg1) {\n"
1362 " %A = select i1 %cond, float %arg0, float %arg1"
1365 expectKnownFPClass(fcAllFlags
, std::nullopt
);
1368 TEST_F(ComputeKnownFPClassTest
, SelectNoFPClassArgNoNan
) {
1370 "define float @test(i1 %cond, float nofpclass(nan) %arg0, float nofpclass(nan) %arg1) {\n"
1371 " %A = select i1 %cond, float %arg0, float %arg1"
1374 expectKnownFPClass(~fcNan
, std::nullopt
);
1377 TEST_F(ComputeKnownFPClassTest
, SelectNoFPClassArgNoPInf
) {
1379 "define float @test(i1 %cond, float nofpclass(inf) %arg0, float nofpclass(pinf) %arg1) {\n"
1380 " %A = select i1 %cond, float %arg0, float %arg1"
1383 expectKnownFPClass(~fcPosInf
, std::nullopt
);
1386 TEST_F(ComputeKnownFPClassTest
, SelectNoFPClassArgNoNInf
) {
1388 "define float @test(i1 %cond, float nofpclass(ninf) %arg0, float nofpclass(inf) %arg1) {\n"
1389 " %A = select i1 %cond, float %arg0, float %arg1"
1392 expectKnownFPClass(~fcNegInf
, std::nullopt
);
1395 TEST_F(ComputeKnownFPClassTest
, SelectNoFPClassCallSiteNoNan
) {
1397 "declare float @func()\n"
1398 "define float @test() {\n"
1399 " %A = call nofpclass(nan) float @func()\n"
1402 expectKnownFPClass(~fcNan
, std::nullopt
);
1405 TEST_F(ComputeKnownFPClassTest
, SelectNoFPClassCallSiteNoZeros
) {
1407 "declare float @func()\n"
1408 "define float @test() {\n"
1409 " %A = call nofpclass(zero) float @func()\n"
1412 expectKnownFPClass(~fcZero
, std::nullopt
);
1415 TEST_F(ComputeKnownFPClassTest
, SelectNoFPClassDeclarationNoNan
) {
1417 "declare nofpclass(nan) float @no_nans()\n"
1418 "define float @test() {\n"
1419 " %A = call float @no_nans()\n"
1422 expectKnownFPClass(~fcNan
, std::nullopt
);
1425 // Check nofpclass + ninf works on a callsite
1426 TEST_F(ComputeKnownFPClassTest
, SelectNoFPClassCallSiteNoZerosNInfFlags
) {
1428 "declare float @func()\n"
1429 "define float @test() {\n"
1430 " %A = call ninf nofpclass(zero) float @func()\n"
1433 expectKnownFPClass(~(fcZero
| fcInf
), std::nullopt
);
1436 TEST_F(ComputeKnownFPClassTest
, FNegNInf
) {
1438 "define float @test(float %arg) {\n"
1439 " %A = fneg ninf float %arg"
1442 expectKnownFPClass(~fcInf
, std::nullopt
);
1445 TEST_F(ComputeKnownFPClassTest
, FabsUnknown
) {
1447 "declare float @llvm.fabs.f32(float)"
1448 "define float @test(float %arg) {\n"
1449 " %A = call float @llvm.fabs.f32(float %arg)"
1452 expectKnownFPClass(fcPositive
| fcNan
, false);
1455 TEST_F(ComputeKnownFPClassTest
, FNegFabsUnknown
) {
1457 "declare float @llvm.fabs.f32(float)"
1458 "define float @test(float %arg) {\n"
1459 " %fabs = call float @llvm.fabs.f32(float %arg)"
1460 " %A = fneg float %fabs"
1463 expectKnownFPClass(fcNegative
| fcNan
, true);
1466 TEST_F(ComputeKnownFPClassTest
, NegFabsNInf
) {
1468 "declare float @llvm.fabs.f32(float)"
1469 "define float @test(float %arg) {\n"
1470 " %fabs = call ninf float @llvm.fabs.f32(float %arg)"
1471 " %A = fneg float %fabs"
1474 expectKnownFPClass((fcNegative
& ~fcNegInf
) | fcNan
, true);
1477 TEST_F(ComputeKnownFPClassTest
, FNegFabsNNaN
) {
1479 "declare float @llvm.fabs.f32(float)"
1480 "define float @test(float %arg) {\n"
1481 " %fabs = call nnan float @llvm.fabs.f32(float %arg)"
1482 " %A = fneg float %fabs"
1485 expectKnownFPClass(fcNegative
, true);
1488 TEST_F(ComputeKnownFPClassTest
, CopySignNNanSrc0
) {
1490 "declare float @llvm.fabs.f32(float)\n"
1491 "declare float @llvm.copysign.f32(float, float)\n"
1492 "define float @test(float %arg0, float %arg1) {\n"
1493 " %fabs = call nnan float @llvm.fabs.f32(float %arg0)"
1494 " %A = call float @llvm.copysign.f32(float %fabs, float %arg1)"
1497 expectKnownFPClass(~fcNan
, std::nullopt
);
1500 TEST_F(ComputeKnownFPClassTest
, CopySignNInfSrc0_NegSign
) {
1502 "declare float @llvm.log.f32(float)\n"
1503 "declare float @llvm.copysign.f32(float, float)\n"
1504 "define float @test(float %arg0, float %arg1) {\n"
1505 " %ninf = call ninf float @llvm.log.f32(float %arg0)"
1506 " %A = call float @llvm.copysign.f32(float %ninf, float -1.0)"
1509 expectKnownFPClass(fcNegFinite
| fcNan
, true);
1512 TEST_F(ComputeKnownFPClassTest
, CopySignNInfSrc0_PosSign
) {
1514 "declare float @llvm.sqrt.f32(float)\n"
1515 "declare float @llvm.copysign.f32(float, float)\n"
1516 "define float @test(float %arg0, float %arg1) {\n"
1517 " %ninf = call ninf float @llvm.sqrt.f32(float %arg0)"
1518 " %A = call float @llvm.copysign.f32(float %ninf, float 1.0)"
1521 expectKnownFPClass(fcPosFinite
| fcNan
, false);
1524 TEST_F(ComputeKnownFPClassTest
, UIToFP
) {
1526 "define float @test(i32 %arg0, i16 %arg1) {\n"
1527 " %A = uitofp i32 %arg0 to float"
1528 " %A2 = uitofp i16 %arg1 to half"
1531 expectKnownFPClass(fcPosFinite
& ~fcSubnormal
, false, A
);
1532 expectKnownFPClass(fcPositive
& ~fcSubnormal
, false, A2
);
1535 TEST_F(ComputeKnownFPClassTest
, SIToFP
) {
1537 "define float @test(i32 %arg0, i16 %arg1, i17 %arg2) {\n"
1538 " %A = sitofp i32 %arg0 to float"
1539 " %A2 = sitofp i16 %arg1 to half"
1540 " %A3 = sitofp i17 %arg2 to half"
1543 expectKnownFPClass(fcFinite
& ~fcNegZero
& ~fcSubnormal
, std::nullopt
, A
);
1544 expectKnownFPClass(fcFinite
& ~fcNegZero
& ~fcSubnormal
, std::nullopt
, A2
);
1545 expectKnownFPClass(~(fcNan
| fcNegZero
| fcSubnormal
), std::nullopt
, A3
);
1548 TEST_F(ComputeKnownFPClassTest
, FAdd
) {
1550 "define float @test(float nofpclass(nan inf) %nnan.ninf, float nofpclass(nan) %nnan, float nofpclass(qnan) %no.qnan, float %unknown) {\n"
1551 " %A = fadd float %nnan, %nnan.ninf"
1552 " %A2 = fadd float %nnan.ninf, %nnan"
1553 " %A3 = fadd float %nnan.ninf, %unknown"
1554 " %A4 = fadd float %nnan.ninf, %no.qnan"
1555 " %A5 = fadd float %nnan, %nnan"
1558 expectKnownFPClass(fcFinite
| fcInf
, std::nullopt
, A
);
1559 expectKnownFPClass(fcFinite
| fcInf
, std::nullopt
, A2
);
1560 expectKnownFPClass(fcAllFlags
, std::nullopt
, A3
);
1561 expectKnownFPClass(fcAllFlags
, std::nullopt
, A4
);
1562 expectKnownFPClass(fcAllFlags
, std::nullopt
, A5
);
1565 TEST_F(ComputeKnownFPClassTest
, FSub
) {
1567 "define float @test(float nofpclass(nan inf) %nnan.ninf, float nofpclass(nan) %nnan, float nofpclass(qnan) %no.qnan, float %unknown) {\n"
1568 " %A = fsub float %nnan, %nnan.ninf"
1569 " %A2 = fsub float %nnan.ninf, %nnan"
1570 " %A3 = fsub float %nnan.ninf, %unknown"
1571 " %A4 = fsub float %nnan.ninf, %no.qnan"
1572 " %A5 = fsub float %nnan, %nnan"
1575 expectKnownFPClass(fcFinite
| fcInf
, std::nullopt
, A
);
1576 expectKnownFPClass(fcFinite
| fcInf
, std::nullopt
, A2
);
1577 expectKnownFPClass(fcAllFlags
, std::nullopt
, A3
);
1578 expectKnownFPClass(fcAllFlags
, std::nullopt
, A4
);
1579 expectKnownFPClass(fcAllFlags
, std::nullopt
, A5
);
1582 TEST_F(ComputeKnownFPClassTest
, FMul
) {
1584 "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"
1585 " %A = fmul float %nnan.ninf0, %nnan.ninf1"
1586 " %A2 = fmul float %nnan.ninf0, %nnan"
1587 " %A3 = fmul float %nnan, %nnan.ninf0"
1588 " %A4 = fmul float %nnan.ninf0, %no.qnan"
1589 " %A5 = fmul float %nnan, %nnan"
1592 expectKnownFPClass(fcFinite
| fcInf
, std::nullopt
, A
);
1593 expectKnownFPClass(fcAllFlags
, std::nullopt
, A2
);
1594 expectKnownFPClass(fcAllFlags
, std::nullopt
, A3
);
1595 expectKnownFPClass(fcAllFlags
, std::nullopt
, A4
);
1596 expectKnownFPClass(fcPositive
| fcNan
, std::nullopt
, A5
);
1599 TEST_F(ComputeKnownFPClassTest
, FMulNoZero
) {
1601 "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"
1602 " %A = fmul float %no.zero.nan0, %no.zero.nan1"
1603 " %A2 = fmul float %no.zero, %no.zero"
1604 " %A3 = fmul float %no.poszero.nan, %no.zero.nan0"
1605 " %A4 = fmul float %no.nan, %no.zero"
1606 " %A5 = fmul float %no.zero, %no.inf"
1607 " %A6 = fmul float %no.zero.nan0, %no.nan"
1608 " %A7 = fmul float %no.nan, %no.zero.nan0"
1611 expectKnownFPClass(fcFinite
| fcInf
, std::nullopt
, A
);
1612 expectKnownFPClass(fcPositive
| fcNan
, std::nullopt
, A2
);
1613 expectKnownFPClass(fcAllFlags
, std::nullopt
, A3
);
1614 expectKnownFPClass(fcAllFlags
, std::nullopt
, A4
);
1615 expectKnownFPClass(fcAllFlags
, std::nullopt
, A5
);
1616 expectKnownFPClass(fcAllFlags
, std::nullopt
, A6
);
1617 expectKnownFPClass(fcAllFlags
, std::nullopt
, A7
);
1620 TEST_F(ComputeKnownFPClassTest
, Phi
) {
1622 "define float @test(i1 %cond, float nofpclass(nan inf) %arg0, float nofpclass(nan) %arg1) {\n"
1624 " br i1 %cond, label %bb0, label %bb1\n"
1630 " %A = phi float [ %arg0, %bb0 ], [ %arg1, %bb1 ]\n"
1633 expectKnownFPClass(~fcNan
, std::nullopt
);
1636 TEST_F(ComputeKnownFPClassTest
, PhiKnownSignFalse
) {
1638 "declare float @llvm.fabs.f32(float)"
1639 "define float @test(i1 %cond, float nofpclass(nan) %arg0, float nofpclass(nan) %arg1) {\n"
1641 " br i1 %cond, label %bb0, label %bb1\n"
1643 " %fabs.arg0 = call float @llvm.fabs.f32(float %arg0)\n"
1646 " %fabs.arg1 = call float @llvm.fabs.f32(float %arg1)\n"
1649 " %A = phi float [ %fabs.arg0, %bb0 ], [ %fabs.arg1, %bb1 ]\n"
1652 expectKnownFPClass(fcPositive
, false);
1655 TEST_F(ComputeKnownFPClassTest
, PhiKnownSignTrue
) {
1657 "declare float @llvm.fabs.f32(float)"
1658 "define float @test(i1 %cond, float nofpclass(nan) %arg0, float %arg1) {\n"
1660 " br i1 %cond, label %bb0, label %bb1\n"
1662 " %fabs.arg0 = call float @llvm.fabs.f32(float %arg0)\n"
1663 " %fneg.fabs.arg0 = fneg float %fabs.arg0\n"
1666 " %fabs.arg1 = call float @llvm.fabs.f32(float %arg1)\n"
1667 " %fneg.fabs.arg1 = fneg float %fabs.arg1\n"
1670 " %A = phi float [ %fneg.fabs.arg0, %bb0 ], [ %fneg.fabs.arg1, %bb1 ]\n"
1673 expectKnownFPClass(fcNegative
| fcNan
, true);
1676 TEST_F(ComputeKnownFPClassTest
, UnreachablePhi
) {
1678 "define float @test(float %arg) {\n"
1685 expectKnownFPClass(fcAllFlags
, std::nullopt
);
1688 TEST_F(ComputeKnownFPClassTest
, SelfPhiOnly
) {
1690 "define float @test(float %arg) {\n"
1694 " %A = phi float [ %A, %loop ]\n"
1697 expectKnownFPClass(fcAllFlags
, std::nullopt
);
1700 TEST_F(ComputeKnownFPClassTest
, SelfPhiFirstArg
) {
1702 "define float @test(i1 %cond, float nofpclass(inf) %arg) {\n"
1704 " br i1 %cond, label %loop, label %ret\n"
1706 " %A = phi float [ %arg, %entry ], [ %A, %loop ]\n"
1711 expectKnownFPClass(~fcInf
, std::nullopt
);
1714 TEST_F(ComputeKnownFPClassTest
, SelfPhiSecondArg
) {
1716 "define float @test(i1 %cond, float nofpclass(inf) %arg) {\n"
1718 " br i1 %cond, label %loop, label %ret\n"
1720 " %A = phi float [ %A, %loop ], [ %arg, %entry ]\n"
1725 expectKnownFPClass(~fcInf
, std::nullopt
);
1728 TEST_F(ComputeKnownFPClassTest
, CannotBeOrderedLessThanZero
) {
1729 parseAssembly("define float @test(float %arg) {\n"
1730 " %A = fmul float %arg, %arg"
1734 Type
*FPTy
= Type::getDoubleTy(M
->getContext());
1735 const DataLayout
&DL
= M
->getDataLayout();
1738 computeKnownFPClass(ConstantFP::getZero(FPTy
, /*Negative=*/false), DL
)
1739 .cannotBeOrderedLessThanZero());
1741 computeKnownFPClass(ConstantFP::getZero(FPTy
, /*Negative=*/true), DL
)
1742 .cannotBeOrderedLessThanZero());
1744 EXPECT_TRUE(computeKnownFPClass(ConstantFP::getInfinity(FPTy
, false), DL
)
1745 .cannotBeOrderedLessThanZero());
1746 EXPECT_FALSE(computeKnownFPClass(ConstantFP::getInfinity(FPTy
, true), DL
)
1747 .cannotBeOrderedLessThanZero());
1749 EXPECT_TRUE(computeKnownFPClass(ConstantFP::get(FPTy
, 1.0), DL
)
1750 .cannotBeOrderedLessThanZero());
1751 EXPECT_FALSE(computeKnownFPClass(ConstantFP::get(FPTy
, -1.0), DL
)
1752 .cannotBeOrderedLessThanZero());
1755 computeKnownFPClass(
1756 ConstantFP::get(FPTy
, APFloat::getSmallest(FPTy
->getFltSemantics(),
1757 /*Negative=*/false)),
1759 .cannotBeOrderedLessThanZero());
1761 computeKnownFPClass(
1762 ConstantFP::get(FPTy
, APFloat::getSmallest(FPTy
->getFltSemantics(),
1763 /*Negative=*/true)),
1765 .cannotBeOrderedLessThanZero());
1768 computeKnownFPClass(ConstantFP::getQNaN(FPTy
, /*Negative=*/false), DL
)
1769 .cannotBeOrderedLessThanZero());
1771 computeKnownFPClass(ConstantFP::getQNaN(FPTy
, /*Negative=*/true), DL
)
1772 .cannotBeOrderedLessThanZero());
1774 computeKnownFPClass(ConstantFP::getSNaN(FPTy
, /*Negative=*/false), DL
)
1775 .cannotBeOrderedLessThanZero());
1777 computeKnownFPClass(ConstantFP::getSNaN(FPTy
, /*Negative=*/true), DL
)
1778 .cannotBeOrderedLessThanZero());
1781 TEST_F(ComputeKnownFPClassTest
, FCmpToClassTest_OrdNan
) {
1782 parseAssembly("define i1 @test(double %arg) {\n"
1783 " %A = fcmp ord double %arg, 0x7FF8000000000000"
1784 " %A2 = fcmp uno double %arg, 0x7FF8000000000000"
1785 " %A3 = fcmp oeq double %arg, 0x7FF8000000000000"
1786 " %A4 = fcmp ueq double %arg, 0x7FF8000000000000"
1790 auto [OrdVal
, OrdClass
] = fcmpToClassTest(
1791 CmpInst::FCMP_ORD
, *A
->getFunction(), A
->getOperand(0), A
->getOperand(1));
1792 EXPECT_EQ(A
->getOperand(0), OrdVal
);
1793 EXPECT_EQ(fcNone
, OrdClass
);
1795 auto [UnordVal
, UnordClass
] =
1796 fcmpToClassTest(CmpInst::FCMP_UNO
, *A2
->getFunction(), A2
->getOperand(0),
1798 EXPECT_EQ(A2
->getOperand(0), UnordVal
);
1799 EXPECT_EQ(fcAllFlags
, UnordClass
);
1801 auto [OeqVal
, OeqClass
] =
1802 fcmpToClassTest(CmpInst::FCMP_OEQ
, *A3
->getFunction(), A3
->getOperand(0),
1804 EXPECT_EQ(A3
->getOperand(0), OeqVal
);
1805 EXPECT_EQ(fcNone
, OeqClass
);
1807 auto [UeqVal
, UeqClass
] =
1808 fcmpToClassTest(CmpInst::FCMP_UEQ
, *A3
->getFunction(), A3
->getOperand(0),
1810 EXPECT_EQ(A3
->getOperand(0), UeqVal
);
1811 EXPECT_EQ(fcAllFlags
, UeqClass
);
1814 TEST_F(ComputeKnownFPClassTest
, FCmpToClassTest_NInf
) {
1815 parseAssembly("define i1 @test(double %arg) {\n"
1816 " %A = fcmp olt double %arg, 0xFFF0000000000000"
1817 " %A2 = fcmp uge double %arg, 0xFFF0000000000000"
1818 " %A3 = fcmp ogt double %arg, 0xFFF0000000000000"
1819 " %A4 = fcmp ule double %arg, 0xFFF0000000000000"
1820 " %A5 = fcmp oge double %arg, 0xFFF0000000000000"
1821 " %A6 = fcmp ult double %arg, 0xFFF0000000000000"
1825 auto [OltVal
, OltClass
] = fcmpToClassTest(
1826 CmpInst::FCMP_OLT
, *A
->getFunction(), A
->getOperand(0), A
->getOperand(1));
1827 EXPECT_EQ(A
->getOperand(0), OltVal
);
1828 EXPECT_EQ(fcNone
, OltClass
);
1830 auto [UgeVal
, UgeClass
] =
1831 fcmpToClassTest(CmpInst::FCMP_UGE
, *A2
->getFunction(), A2
->getOperand(0),
1833 EXPECT_EQ(A2
->getOperand(0), UgeVal
);
1834 EXPECT_EQ(fcAllFlags
, UgeClass
);
1836 auto [OgtVal
, OgtClass
] =
1837 fcmpToClassTest(CmpInst::FCMP_OGT
, *A3
->getFunction(), A3
->getOperand(0),
1839 EXPECT_EQ(A3
->getOperand(0), OgtVal
);
1840 EXPECT_EQ(~(fcNegInf
| fcNan
), OgtClass
);
1842 auto [UleVal
, UleClass
] =
1843 fcmpToClassTest(CmpInst::FCMP_ULE
, *A4
->getFunction(), A4
->getOperand(0),
1845 EXPECT_EQ(A4
->getOperand(0), UleVal
);
1846 EXPECT_EQ(fcNegInf
| fcNan
, UleClass
);
1848 auto [OgeVal
, OgeClass
] =
1849 fcmpToClassTest(CmpInst::FCMP_OGE
, *A5
->getFunction(), A5
->getOperand(0),
1851 EXPECT_EQ(A5
->getOperand(0), OgeVal
);
1852 EXPECT_EQ(~fcNan
, OgeClass
);
1854 auto [UltVal
, UltClass
] =
1855 fcmpToClassTest(CmpInst::FCMP_ULT
, *A6
->getFunction(), A6
->getOperand(0),
1857 EXPECT_EQ(A6
->getOperand(0), UltVal
);
1858 EXPECT_EQ(fcNan
, UltClass
);
1861 TEST_F(ComputeKnownFPClassTest
, FCmpToClassTest_FabsNInf
) {
1862 parseAssembly("declare double @llvm.fabs.f64(double)\n"
1863 "define i1 @test(double %arg) {\n"
1864 " %fabs.arg = call double @llvm.fabs.f64(double %arg)\n"
1865 " %A = fcmp olt double %fabs.arg, 0xFFF0000000000000"
1866 " %A2 = fcmp uge double %fabs.arg, 0xFFF0000000000000"
1867 " %A3 = fcmp ogt double %fabs.arg, 0xFFF0000000000000"
1868 " %A4 = fcmp ule double %fabs.arg, 0xFFF0000000000000"
1869 " %A5 = fcmp oge double %fabs.arg, 0xFFF0000000000000"
1870 " %A6 = fcmp ult double %fabs.arg, 0xFFF0000000000000"
1874 Value
*ArgVal
= F
->getArg(0);
1876 auto [OltVal
, OltClass
] = fcmpToClassTest(
1877 CmpInst::FCMP_OLT
, *A
->getFunction(), A
->getOperand(0), A
->getOperand(1));
1878 EXPECT_EQ(ArgVal
, OltVal
);
1879 EXPECT_EQ(fcNone
, OltClass
);
1881 auto [UgeVal
, UgeClass
] =
1882 fcmpToClassTest(CmpInst::FCMP_UGE
, *A2
->getFunction(), A2
->getOperand(0),
1884 EXPECT_EQ(ArgVal
, UgeVal
);
1885 EXPECT_EQ(fcAllFlags
, UgeClass
);
1887 auto [OgtVal
, OgtClass
] =
1888 fcmpToClassTest(CmpInst::FCMP_OGT
, *A3
->getFunction(), A3
->getOperand(0),
1890 EXPECT_EQ(ArgVal
, OgtVal
);
1891 EXPECT_EQ(~fcNan
, OgtClass
);
1893 auto [UleVal
, UleClass
] =
1894 fcmpToClassTest(CmpInst::FCMP_ULE
, *A4
->getFunction(), A4
->getOperand(0),
1896 EXPECT_EQ(ArgVal
, UleVal
);
1897 EXPECT_EQ(fcNan
, UleClass
);
1899 auto [OgeVal
, OgeClass
] =
1900 fcmpToClassTest(CmpInst::FCMP_OGE
, *A5
->getFunction(), A5
->getOperand(0),
1902 EXPECT_EQ(ArgVal
, OgeVal
);
1903 EXPECT_EQ(~fcNan
, OgeClass
);
1905 auto [UltVal
, UltClass
] =
1906 fcmpToClassTest(CmpInst::FCMP_ULT
, *A6
->getFunction(), A6
->getOperand(0),
1908 EXPECT_EQ(ArgVal
, UltVal
);
1909 EXPECT_EQ(fcNan
, UltClass
);
1912 TEST_F(ComputeKnownFPClassTest
, FCmpToClassTest_PInf
) {
1913 parseAssembly("define i1 @test(double %arg) {\n"
1914 " %A = fcmp ogt double %arg, 0x7FF0000000000000"
1915 " %A2 = fcmp ule double %arg, 0x7FF0000000000000"
1916 " %A3 = fcmp ole double %arg, 0x7FF0000000000000"
1917 " %A4 = fcmp ugt double %arg, 0x7FF0000000000000"
1921 auto [OgtVal
, OgtClass
] = fcmpToClassTest(
1922 CmpInst::FCMP_OGT
, *A
->getFunction(), A
->getOperand(0), A
->getOperand(1));
1923 EXPECT_EQ(A
->getOperand(0), OgtVal
);
1924 EXPECT_EQ(fcNone
, OgtClass
);
1926 auto [UleVal
, UleClass
] =
1927 fcmpToClassTest(CmpInst::FCMP_ULE
, *A2
->getFunction(), A2
->getOperand(0),
1929 EXPECT_EQ(A2
->getOperand(0), UleVal
);
1930 EXPECT_EQ(fcAllFlags
, UleClass
);
1932 auto [OleVal
, OleClass
] =
1933 fcmpToClassTest(CmpInst::FCMP_OLE
, *A3
->getFunction(), A3
->getOperand(0),
1935 EXPECT_EQ(A
->getOperand(0), OleVal
);
1936 EXPECT_EQ(~fcNan
, OleClass
);
1938 auto [UgtVal
, UgtClass
] =
1939 fcmpToClassTest(CmpInst::FCMP_UGT
, *A4
->getFunction(), A4
->getOperand(0),
1941 EXPECT_EQ(A4
->getOperand(0), UgtVal
);
1942 EXPECT_EQ(fcNan
, UgtClass
);
1945 TEST_F(ComputeKnownFPClassTest
, SqrtNszSignBit
) {
1947 "declare float @llvm.sqrt.f32(float)\n"
1948 "define float @test(float %arg, float nofpclass(nan) %arg.nnan) {\n"
1949 " %A = call float @llvm.sqrt.f32(float %arg)\n"
1950 " %A2 = call nsz float @llvm.sqrt.f32(float %arg)\n"
1951 " %A3 = call float @llvm.sqrt.f32(float %arg.nnan)\n"
1952 " %A4 = call nsz float @llvm.sqrt.f32(float %arg.nnan)\n"
1956 const FPClassTest SqrtMask
= fcPositive
| fcNegZero
| fcNan
;
1957 const FPClassTest NszSqrtMask
= fcPositive
| fcNan
;
1960 KnownFPClass UseInstrInfo
=
1961 computeKnownFPClass(A
, M
->getDataLayout(), fcAllFlags
, 0, nullptr,
1962 nullptr, nullptr, nullptr, /*UseInstrInfo=*/true);
1963 EXPECT_EQ(SqrtMask
, UseInstrInfo
.KnownFPClasses
);
1964 EXPECT_EQ(std::nullopt
, UseInstrInfo
.SignBit
);
1966 KnownFPClass NoUseInstrInfo
=
1967 computeKnownFPClass(A
, M
->getDataLayout(), fcAllFlags
, 0, nullptr,
1968 nullptr, nullptr, nullptr, /*UseInstrInfo=*/false);
1969 EXPECT_EQ(SqrtMask
, NoUseInstrInfo
.KnownFPClasses
);
1970 EXPECT_EQ(std::nullopt
, NoUseInstrInfo
.SignBit
);
1974 KnownFPClass UseInstrInfoNSZ
=
1975 computeKnownFPClass(A2
, M
->getDataLayout(), fcAllFlags
, 0, nullptr,
1976 nullptr, nullptr, nullptr, /*UseInstrInfo=*/true);
1977 EXPECT_EQ(NszSqrtMask
, UseInstrInfoNSZ
.KnownFPClasses
);
1978 EXPECT_EQ(std::nullopt
, UseInstrInfoNSZ
.SignBit
);
1980 KnownFPClass NoUseInstrInfoNSZ
=
1981 computeKnownFPClass(A2
, M
->getDataLayout(), fcAllFlags
, 0, nullptr,
1982 nullptr, nullptr, nullptr, /*UseInstrInfo=*/false);
1983 EXPECT_EQ(SqrtMask
, NoUseInstrInfoNSZ
.KnownFPClasses
);
1984 EXPECT_EQ(std::nullopt
, NoUseInstrInfoNSZ
.SignBit
);
1988 KnownFPClass UseInstrInfoNoNan
=
1989 computeKnownFPClass(A3
, M
->getDataLayout(), fcAllFlags
, 0, nullptr,
1990 nullptr, nullptr, nullptr, /*UseInstrInfo=*/true);
1991 EXPECT_EQ(fcPositive
| fcNegZero
| fcQNan
,
1992 UseInstrInfoNoNan
.KnownFPClasses
);
1993 EXPECT_EQ(std::nullopt
, UseInstrInfoNoNan
.SignBit
);
1995 KnownFPClass NoUseInstrInfoNoNan
=
1996 computeKnownFPClass(A3
, M
->getDataLayout(), fcAllFlags
, 0, nullptr,
1997 nullptr, nullptr, nullptr, /*UseInstrInfo=*/false);
1998 EXPECT_EQ(fcPositive
| fcNegZero
| fcQNan
,
1999 NoUseInstrInfoNoNan
.KnownFPClasses
);
2000 EXPECT_EQ(std::nullopt
, NoUseInstrInfoNoNan
.SignBit
);
2004 KnownFPClass UseInstrInfoNSZNoNan
=
2005 computeKnownFPClass(A4
, M
->getDataLayout(), fcAllFlags
, 0, nullptr,
2006 nullptr, nullptr, nullptr, /*UseInstrInfo=*/true);
2007 EXPECT_EQ(fcPositive
| fcQNan
, UseInstrInfoNSZNoNan
.KnownFPClasses
);
2008 EXPECT_EQ(std::nullopt
, UseInstrInfoNSZNoNan
.SignBit
);
2010 KnownFPClass NoUseInstrInfoNSZNoNan
=
2011 computeKnownFPClass(A4
, M
->getDataLayout(), fcAllFlags
, 0, nullptr,
2012 nullptr, nullptr, nullptr, /*UseInstrInfo=*/false);
2013 EXPECT_EQ(fcPositive
| fcNegZero
| fcQNan
,
2014 NoUseInstrInfoNSZNoNan
.KnownFPClasses
);
2015 EXPECT_EQ(std::nullopt
, NoUseInstrInfoNSZNoNan
.SignBit
);
2019 TEST_F(ComputeKnownFPClassTest
, Constants
) {
2020 parseAssembly("declare float @func()\n"
2021 "define float @test() {\n"
2022 " %A = call float @func()\n"
2026 Type
*F32
= Type::getFloatTy(Context
);
2027 Type
*V4F32
= FixedVectorType::get(F32
, 4);
2030 KnownFPClass ConstAggZero
= computeKnownFPClass(
2031 ConstantAggregateZero::get(V4F32
), M
->getDataLayout(), fcAllFlags
, 0,
2032 nullptr, nullptr, nullptr, nullptr);
2034 EXPECT_EQ(fcPosZero
, ConstAggZero
.KnownFPClasses
);
2035 ASSERT_TRUE(ConstAggZero
.SignBit
);
2036 EXPECT_FALSE(*ConstAggZero
.SignBit
);
2040 KnownFPClass Undef
=
2041 computeKnownFPClass(UndefValue::get(F32
), M
->getDataLayout(),
2042 fcAllFlags
, 0, nullptr, nullptr, nullptr, nullptr);
2043 EXPECT_EQ(fcAllFlags
, Undef
.KnownFPClasses
);
2044 EXPECT_FALSE(Undef
.SignBit
);
2048 KnownFPClass Poison
=
2049 computeKnownFPClass(PoisonValue::get(F32
), M
->getDataLayout(),
2050 fcAllFlags
, 0, nullptr, nullptr, nullptr, nullptr);
2051 EXPECT_EQ(fcNone
, Poison
.KnownFPClasses
);
2052 ASSERT_TRUE(Poison
.SignBit
);
2053 EXPECT_FALSE(*Poison
.SignBit
);
2057 // Assume the poison element should be 0.
2058 Constant
*ZeroF32
= ConstantFP::getZero(F32
);
2059 Constant
*PoisonF32
= PoisonValue::get(F32
);
2061 KnownFPClass PartiallyPoison
= computeKnownFPClass(
2062 ConstantVector::get({ZeroF32
, PoisonF32
}), M
->getDataLayout(),
2063 fcAllFlags
, 0, nullptr, nullptr, nullptr, nullptr);
2064 EXPECT_EQ(fcPosZero
, PartiallyPoison
.KnownFPClasses
);
2065 ASSERT_TRUE(PartiallyPoison
.SignBit
);
2066 EXPECT_FALSE(*PartiallyPoison
.SignBit
);
2070 // Assume the poison element should be 1.
2071 Constant
*NegZeroF32
= ConstantFP::getZero(F32
, true);
2072 Constant
*PoisonF32
= PoisonValue::get(F32
);
2074 KnownFPClass PartiallyPoison
= computeKnownFPClass(
2075 ConstantVector::get({NegZeroF32
, PoisonF32
}), M
->getDataLayout(),
2076 fcAllFlags
, 0, nullptr, nullptr, nullptr, nullptr);
2077 EXPECT_EQ(fcNegZero
, PartiallyPoison
.KnownFPClasses
);
2078 ASSERT_TRUE(PartiallyPoison
.SignBit
);
2079 EXPECT_TRUE(*PartiallyPoison
.SignBit
);
2083 // Assume the poison element should be 1.
2084 Constant
*NegZeroF32
= ConstantFP::getZero(F32
, true);
2085 Constant
*PoisonF32
= PoisonValue::get(F32
);
2087 KnownFPClass PartiallyPoison
= computeKnownFPClass(
2088 ConstantVector::get({PoisonF32
, NegZeroF32
}), M
->getDataLayout(),
2089 fcAllFlags
, 0, nullptr, nullptr, nullptr, nullptr);
2090 EXPECT_EQ(fcNegZero
, PartiallyPoison
.KnownFPClasses
);
2091 EXPECT_TRUE(PartiallyPoison
.SignBit
);
2095 TEST_F(ValueTrackingTest
, isNonZeroRecurrence
) {
2097 define i1 @test(i8 %n, i8 %r) {
2101 %p = phi i8 [ -1, %entry ], [ %next, %loop ]
2102 %next = add nsw i8 %p, -1
2103 %cmp1 = icmp eq i8 %p, %n
2104 br i1 %cmp1, label %exit, label %loop
2107 %CxtI = icmp eq i8 %A, 0
2111 const DataLayout
&DL
= M
->getDataLayout();
2112 AssumptionCache
AC(*F
);
2113 EXPECT_TRUE(isKnownNonZero(A
, SimplifyQuery(DL
, /*DT=*/nullptr, &AC
, CxtI
)));
2116 TEST_F(ValueTrackingTest
, KnownNonZeroFromDomCond
) {
2119 define void @test(i1 %c) {
2120 %A = call ptr @f_i8()
2121 %B = call ptr @f_i8()
2122 %c1 = icmp ne ptr %A, null
2123 %cond = and i1 %c1, %c
2124 br i1 %cond, label %T, label %Q
2126 %CxtI = add i32 0, 0
2129 %CxtI2 = add i32 0, 0
2133 AssumptionCache
AC(*F
);
2134 DominatorTree
DT(*F
);
2135 const DataLayout
&DL
= M
->getDataLayout();
2136 const SimplifyQuery
SQ(DL
, &DT
, &AC
);
2137 EXPECT_EQ(isKnownNonZero(A
, SQ
.getWithInstruction(CxtI
)), true);
2138 EXPECT_EQ(isKnownNonZero(A
, SQ
.getWithInstruction(CxtI2
)), false);
2141 TEST_F(ValueTrackingTest
, KnownNonZeroFromDomCond2
) {
2144 define void @test(i1 %c) {
2145 %A = call ptr @f_i8()
2146 %B = call ptr @f_i8()
2147 %c1 = icmp ne ptr %A, null
2148 %cond = select i1 %c, i1 %c1, i1 false
2149 br i1 %cond, label %T, label %Q
2151 %CxtI = add i32 0, 0
2154 %CxtI2 = add i32 0, 0
2158 AssumptionCache
AC(*F
);
2159 DominatorTree
DT(*F
);
2160 const DataLayout
&DL
= M
->getDataLayout();
2161 const SimplifyQuery
SQ(DL
, &DT
, &AC
);
2162 EXPECT_EQ(isKnownNonZero(A
, SQ
.getWithInstruction(CxtI
)), true);
2163 EXPECT_EQ(isKnownNonZero(A
, SQ
.getWithInstruction(CxtI2
)), false);
2166 TEST_F(ValueTrackingTest
, IsImpliedConditionAnd
) {
2168 define void @test(i32 %x, i32 %y) {
2169 %c1 = icmp ult i32 %x, 10
2170 %c2 = icmp ult i32 %y, 15
2171 %A = and i1 %c1, %c2
2173 %A2 = icmp ult i32 %x, 20
2174 %A3 = icmp uge i32 %y, 20
2175 %A4 = icmp ult i32 %x, 5
2179 const DataLayout
&DL
= M
->getDataLayout();
2180 EXPECT_EQ(isImpliedCondition(A
, A2
, DL
), true);
2181 EXPECT_EQ(isImpliedCondition(A
, A3
, DL
), false);
2182 EXPECT_EQ(isImpliedCondition(A
, A4
, DL
), std::nullopt
);
2185 TEST_F(ValueTrackingTest
, IsImpliedConditionAnd2
) {
2187 define void @test(i32 %x, i32 %y) {
2188 %c1 = icmp ult i32 %x, 10
2189 %c2 = icmp ult i32 %y, 15
2190 %A = select i1 %c1, i1 %c2, i1 false
2192 %A2 = icmp ult i32 %x, 20
2193 %A3 = icmp uge i32 %y, 20
2194 %A4 = icmp ult i32 %x, 5
2198 const DataLayout
&DL
= M
->getDataLayout();
2199 EXPECT_EQ(isImpliedCondition(A
, A2
, DL
), true);
2200 EXPECT_EQ(isImpliedCondition(A
, A3
, DL
), false);
2201 EXPECT_EQ(isImpliedCondition(A
, A4
, DL
), std::nullopt
);
2204 TEST_F(ValueTrackingTest
, IsImpliedConditionAndVec
) {
2206 define void @test(<2 x i8> %x, <2 x i8> %y) {
2207 %A = icmp ult <2 x i8> %x, %y
2208 %A2 = icmp ule <2 x i8> %x, %y
2212 const DataLayout
&DL
= M
->getDataLayout();
2213 EXPECT_EQ(isImpliedCondition(A
, A2
, DL
), true);
2216 TEST_F(ValueTrackingTest
, IsImpliedConditionOr
) {
2218 define void @test(i32 %x, i32 %y) {
2219 %c1 = icmp ult i32 %x, 10
2220 %c2 = icmp ult i32 %y, 15
2221 %A = or i1 %c1, %c2 ; negated
2222 ; x >= 10 /\ y >= 15
2223 %A2 = icmp ult i32 %x, 5
2224 %A3 = icmp uge i32 %y, 10
2225 %A4 = icmp ult i32 %x, 15
2229 const DataLayout
&DL
= M
->getDataLayout();
2230 EXPECT_EQ(isImpliedCondition(A
, A2
, DL
, false), false);
2231 EXPECT_EQ(isImpliedCondition(A
, A3
, DL
, false), true);
2232 EXPECT_EQ(isImpliedCondition(A
, A4
, DL
, false), std::nullopt
);
2235 TEST_F(ValueTrackingTest
, IsImpliedConditionOr2
) {
2237 define void @test(i32 %x, i32 %y) {
2238 %c1 = icmp ult i32 %x, 10
2239 %c2 = icmp ult i32 %y, 15
2240 %A = select i1 %c1, i1 true, i1 %c2 ; negated
2241 ; x >= 10 /\ y >= 15
2242 %A2 = icmp ult i32 %x, 5
2243 %A3 = icmp uge i32 %y, 10
2244 %A4 = icmp ult i32 %x, 15
2248 const DataLayout
&DL
= M
->getDataLayout();
2249 EXPECT_EQ(isImpliedCondition(A
, A2
, DL
, false), false);
2250 EXPECT_EQ(isImpliedCondition(A
, A3
, DL
, false), true);
2251 EXPECT_EQ(isImpliedCondition(A
, A4
, DL
, false), std::nullopt
);
2254 TEST_F(ComputeKnownBitsTest
, KnownNonZeroShift
) {
2255 // %q is known nonzero without known bits.
2256 // Because %q is nonzero, %A[0] is known to be zero.
2258 "define i8 @test(i8 %p, ptr %pq) {\n"
2259 " %q = load i8, ptr %pq, !range !0\n"
2260 " %A = shl i8 %p, %q\n"
2263 "!0 = !{ i8 1, i8 5 }\n");
2264 expectKnownBits(/*zero*/ 1u, /*one*/ 0u);
2267 TEST_F(ComputeKnownBitsTest
, ComputeKnownFshl
) {
2268 // fshl(....1111....0000, 00..1111........, 6)
2269 // = 11....000000..11
2271 "define i16 @test(i16 %a, i16 %b) {\n"
2272 " %aa = shl i16 %a, 4\n"
2273 " %bb = lshr i16 %b, 2\n"
2274 " %aaa = or i16 %aa, 3840\n"
2275 " %bbb = or i16 %bb, 3840\n"
2276 " %A = call i16 @llvm.fshl.i16(i16 %aaa, i16 %bbb, i16 6)\n"
2279 "declare i16 @llvm.fshl.i16(i16, i16, i16)\n");
2280 expectKnownBits(/*zero*/ 1008u, /*one*/ 49155u);
2283 TEST_F(ComputeKnownBitsTest
, ComputeKnownFshr
) {
2284 // fshr(....1111....0000, 00..1111........, 26)
2285 // = 11....000000..11
2287 "define i16 @test(i16 %a, i16 %b) {\n"
2288 " %aa = shl i16 %a, 4\n"
2289 " %bb = lshr i16 %b, 2\n"
2290 " %aaa = or i16 %aa, 3840\n"
2291 " %bbb = or i16 %bb, 3840\n"
2292 " %A = call i16 @llvm.fshr.i16(i16 %aaa, i16 %bbb, i16 26)\n"
2295 "declare i16 @llvm.fshr.i16(i16, i16, i16)\n");
2296 expectKnownBits(/*zero*/ 1008u, /*one*/ 49155u);
2299 TEST_F(ComputeKnownBitsTest
, ComputeKnownFshlZero
) {
2300 // fshl(....1111....0000, 00..1111........, 0)
2301 // = ....1111....0000
2303 "define i16 @test(i16 %a, i16 %b) {\n"
2304 " %aa = shl i16 %a, 4\n"
2305 " %bb = lshr i16 %b, 2\n"
2306 " %aaa = or i16 %aa, 3840\n"
2307 " %bbb = or i16 %bb, 3840\n"
2308 " %A = call i16 @llvm.fshl.i16(i16 %aaa, i16 %bbb, i16 0)\n"
2311 "declare i16 @llvm.fshl.i16(i16, i16, i16)\n");
2312 expectKnownBits(/*zero*/ 15u, /*one*/ 3840u);
2315 TEST_F(ComputeKnownBitsTest
, ComputeKnownUAddSatLeadingOnes
) {
2316 // uadd.sat(1111...1, ........)
2319 "define i8 @test(i8 %a, i8 %b) {\n"
2320 " %aa = or i8 %a, 241\n"
2321 " %A = call i8 @llvm.uadd.sat.i8(i8 %aa, i8 %b)\n"
2324 "declare i8 @llvm.uadd.sat.i8(i8, i8)\n");
2325 expectKnownBits(/*zero*/ 0u, /*one*/ 240u);
2328 TEST_F(ComputeKnownBitsTest
, ComputeKnownUAddSatOnesPreserved
) {
2329 // uadd.sat(00...011, .1...110)
2332 "define i8 @test(i8 %a, i8 %b) {\n"
2333 " %aa = or i8 %a, 3\n"
2334 " %aaa = and i8 %aa, 59\n"
2335 " %bb = or i8 %b, 70\n"
2336 " %bbb = and i8 %bb, 254\n"
2337 " %A = call i8 @llvm.uadd.sat.i8(i8 %aaa, i8 %bbb)\n"
2340 "declare i8 @llvm.uadd.sat.i8(i8, i8)\n");
2341 expectKnownBits(/*zero*/ 0u, /*one*/ 1u);
2344 TEST_F(ComputeKnownBitsTest
, ComputeKnownUSubSatLHSLeadingZeros
) {
2345 // usub.sat(0000...0, ........)
2348 "define i8 @test(i8 %a, i8 %b) {\n"
2349 " %aa = and i8 %a, 14\n"
2350 " %A = call i8 @llvm.usub.sat.i8(i8 %aa, i8 %b)\n"
2353 "declare i8 @llvm.usub.sat.i8(i8, i8)\n");
2354 expectKnownBits(/*zero*/ 240u, /*one*/ 0u);
2357 TEST_F(ComputeKnownBitsTest
, ComputeKnownUSubSatRHSLeadingOnes
) {
2358 // usub.sat(........, 1111...1)
2361 "define i8 @test(i8 %a, i8 %b) {\n"
2362 " %bb = or i8 %a, 241\n"
2363 " %A = call i8 @llvm.usub.sat.i8(i8 %a, i8 %bb)\n"
2366 "declare i8 @llvm.usub.sat.i8(i8, i8)\n");
2367 expectKnownBits(/*zero*/ 240u, /*one*/ 0u);
2370 TEST_F(ComputeKnownBitsTest
, ComputeKnownUSubSatZerosPreserved
) {
2371 // usub.sat(11...011, .1...110)
2374 "define i8 @test(i8 %a, i8 %b) {\n"
2375 " %aa = or i8 %a, 195\n"
2376 " %aaa = and i8 %aa, 251\n"
2377 " %bb = or i8 %b, 70\n"
2378 " %bbb = and i8 %bb, 254\n"
2379 " %A = call i8 @llvm.usub.sat.i8(i8 %aaa, i8 %bbb)\n"
2382 "declare i8 @llvm.usub.sat.i8(i8, i8)\n");
2383 expectKnownBits(/*zero*/ 2u, /*one*/ 0u);
2386 TEST_F(ComputeKnownBitsTest
, ComputeKnownBitsPtrToIntTrunc
) {
2387 // ptrtoint truncates the pointer type. Make sure we don't crash.
2389 "define void @test(ptr %p) {\n"
2390 " %A = load ptr, ptr %p\n"
2391 " %i = ptrtoint ptr %A to i32\n"
2392 " %m = and i32 %i, 31\n"
2393 " %c = icmp eq i32 %m, 0\n"
2394 " call void @llvm.assume(i1 %c)\n"
2397 "declare void @llvm.assume(i1)\n");
2398 AssumptionCache
AC(*F
);
2399 KnownBits Known
= computeKnownBits(
2400 A
, M
->getDataLayout(), /* Depth */ 0, &AC
, F
->front().getTerminator());
2401 EXPECT_TRUE(Known
.isUnknown());
2404 TEST_F(ComputeKnownBitsTest
, ComputeKnownBitsPtrToIntZext
) {
2405 // ptrtoint zero extends the pointer type. Make sure we don't crash.
2407 "define void @test(ptr %p) {\n"
2408 " %A = load ptr, ptr %p\n"
2409 " %i = ptrtoint ptr %A to i128\n"
2410 " %m = and i128 %i, 31\n"
2411 " %c = icmp eq i128 %m, 0\n"
2412 " call void @llvm.assume(i1 %c)\n"
2415 "declare void @llvm.assume(i1)\n");
2416 AssumptionCache
AC(*F
);
2417 KnownBits Known
= computeKnownBits(
2418 A
, M
->getDataLayout(), /* Depth */ 0, &AC
, F
->front().getTerminator());
2419 EXPECT_TRUE(Known
.isUnknown());
2422 TEST_F(ComputeKnownBitsTest
, ComputeKnownBitsFreeze
) {
2423 parseAssembly("define void @test() {\n"
2424 " %m = call i32 @any_num()\n"
2425 " %A = freeze i32 %m\n"
2426 " %n = and i32 %m, 31\n"
2427 " %c = icmp eq i32 %n, 0\n"
2428 " call void @llvm.assume(i1 %c)\n"
2431 "declare void @llvm.assume(i1)\n"
2432 "declare i32 @any_num()\n");
2433 AssumptionCache
AC(*F
);
2434 KnownBits Known
= computeKnownBits(A
, M
->getDataLayout(), /* Depth */ 0, &AC
,
2435 F
->front().getTerminator());
2436 EXPECT_EQ(Known
.Zero
.getZExtValue(), 31u);
2437 EXPECT_EQ(Known
.One
.getZExtValue(), 0u);
2440 TEST_F(ComputeKnownBitsTest
, ComputeKnownBitsReturnedRangeConflict
) {
2442 "declare i16 @foo(i16 returned)\n"
2444 "define i16 @test() {\n"
2445 " %A = call i16 @foo(i16 4095), !range !{i16 32, i16 33}\n"
2448 // The call returns 32 according to range metadata, but 4095 according to the
2449 // returned arg operand. Given the conflicting information we expect that the
2450 // known bits information simply is cleared.
2451 expectKnownBits(/*zero*/ 0u, /*one*/ 0u);
2454 TEST_F(ComputeKnownBitsTest
, ComputeKnownBitsAddWithRange
) {
2455 parseAssembly("define void @test(ptr %p) {\n"
2456 " %A = load i64, ptr %p, !range !{i64 64, i64 65536}\n"
2457 " %APlus512 = add i64 %A, 512\n"
2458 " %c = icmp ugt i64 %APlus512, 523\n"
2459 " call void @llvm.assume(i1 %c)\n"
2462 "declare void @llvm.assume(i1)\n");
2463 AssumptionCache
AC(*F
);
2464 KnownBits Known
= computeKnownBits(A
, M
->getDataLayout(), /* Depth */ 0, &AC
,
2465 F
->front().getTerminator());
2466 EXPECT_EQ(Known
.Zero
.getZExtValue(), ~(65536llu - 1));
2467 EXPECT_EQ(Known
.One
.getZExtValue(), 0u);
2468 Instruction
&APlus512
= findInstructionByName(F
, "APlus512");
2469 Known
= computeKnownBits(&APlus512
, M
->getDataLayout(), /* Depth */ 0, &AC
,
2470 F
->front().getTerminator());
2471 // We know of one less zero because 512 may have produced a 1 that
2472 // got carried all the way to the first trailing zero.
2473 EXPECT_EQ(Known
.Zero
.getZExtValue(), (~(65536llu - 1)) << 1);
2474 EXPECT_EQ(Known
.One
.getZExtValue(), 0u);
2475 // The known range is not precise given computeKnownBits works
2476 // with the masks of zeros and ones, not the ranges.
2477 EXPECT_EQ(Known
.getMinValue(), 0u);
2478 EXPECT_EQ(Known
.getMaxValue(), 131071);
2481 TEST_F(ComputeKnownBitsTest
, ComputeKnownBitsUnknownVScale
) {
2482 Module
M("", Context
);
2483 IRBuilder
<> Builder(Context
);
2484 Function
*TheFn
= Intrinsic::getOrInsertDeclaration(&M
, Intrinsic::vscale
,
2485 {Builder
.getInt32Ty()});
2486 CallInst
*CI
= Builder
.CreateCall(TheFn
, {}, {}, "");
2488 KnownBits Known
= computeKnownBits(CI
, M
.getDataLayout(), /* Depth */ 0);
2489 // There is no parent function so we cannot look up the vscale_range
2490 // attribute to determine the number of bits.
2491 EXPECT_EQ(Known
.One
.getZExtValue(), 0u);
2492 EXPECT_EQ(Known
.Zero
.getZExtValue(), 0u);
2494 BasicBlock
*BB
= BasicBlock::Create(Context
);
2495 CI
->insertInto(BB
, BB
->end());
2496 Known
= computeKnownBits(CI
, M
.getDataLayout(), /* Depth */ 0);
2497 // There is no parent function so we cannot look up the vscale_range
2498 // attribute to determine the number of bits.
2499 EXPECT_EQ(Known
.One
.getZExtValue(), 0u);
2500 EXPECT_EQ(Known
.Zero
.getZExtValue(), 0u);
2502 CI
->removeFromParent();
2507 // 512 + [32, 64) doesn't produce overlapping bits.
2508 // Make sure we get all the individual bits properly.
2509 TEST_F(ComputeKnownBitsTest
, ComputeKnownBitsAddWithRangeNoOverlap
) {
2510 parseAssembly("define void @test(ptr %p) {\n"
2511 " %A = load i64, ptr %p, !range !{i64 32, i64 64}\n"
2512 " %APlus512 = add i64 %A, 512\n"
2513 " %c = icmp ugt i64 %APlus512, 523\n"
2514 " call void @llvm.assume(i1 %c)\n"
2517 "declare void @llvm.assume(i1)\n");
2518 AssumptionCache
AC(*F
);
2519 KnownBits Known
= computeKnownBits(A
, M
->getDataLayout(), /* Depth */ 0, &AC
,
2520 F
->front().getTerminator());
2521 EXPECT_EQ(Known
.Zero
.getZExtValue(), ~(64llu - 1));
2522 EXPECT_EQ(Known
.One
.getZExtValue(), 32u);
2523 Instruction
&APlus512
= findInstructionByName(F
, "APlus512");
2524 Known
= computeKnownBits(&APlus512
, M
->getDataLayout(), /* Depth */ 0, &AC
,
2525 F
->front().getTerminator());
2526 EXPECT_EQ(Known
.Zero
.getZExtValue(), ~512llu & ~(64llu - 1));
2527 EXPECT_EQ(Known
.One
.getZExtValue(), 512u | 32u);
2528 // The known range is not precise given computeKnownBits works
2529 // with the masks of zeros and ones, not the ranges.
2530 EXPECT_EQ(Known
.getMinValue(), 544);
2531 EXPECT_EQ(Known
.getMaxValue(), 575);
2534 TEST_F(ComputeKnownBitsTest
, ComputeKnownBitsGEPWithRange
) {
2536 "define void @test(ptr %p) {\n"
2537 " %A = load i64, ptr %p, !range !{i64 64, i64 65536}\n"
2538 " %APtr = inttoptr i64 %A to float*"
2539 " %APtrPlus512 = getelementptr float, float* %APtr, i32 128\n"
2540 " %c = icmp ugt float* %APtrPlus512, inttoptr (i32 523 to float*)\n"
2541 " call void @llvm.assume(i1 %c)\n"
2544 "declare void @llvm.assume(i1)\n");
2545 AssumptionCache
AC(*F
);
2546 KnownBits Known
= computeKnownBits(A
, M
->getDataLayout(), /* Depth */ 0, &AC
,
2547 F
->front().getTerminator());
2548 EXPECT_EQ(Known
.Zero
.getZExtValue(), ~(65536llu - 1));
2549 EXPECT_EQ(Known
.One
.getZExtValue(), 0u);
2550 Instruction
&APtrPlus512
= findInstructionByName(F
, "APtrPlus512");
2551 Known
= computeKnownBits(&APtrPlus512
, M
->getDataLayout(), /* Depth */ 0, &AC
,
2552 F
->front().getTerminator());
2553 // We know of one less zero because 512 may have produced a 1 that
2554 // got carried all the way to the first trailing zero.
2555 EXPECT_EQ(Known
.Zero
.getZExtValue(), ~(65536llu - 1) << 1);
2556 EXPECT_EQ(Known
.One
.getZExtValue(), 0u);
2557 // The known range is not precise given computeKnownBits works
2558 // with the masks of zeros and ones, not the ranges.
2559 EXPECT_EQ(Known
.getMinValue(), 0u);
2560 EXPECT_EQ(Known
.getMaxValue(), 131071);
2563 // 4*128 + [32, 64) doesn't produce overlapping bits.
2564 // Make sure we get all the individual bits properly.
2565 // This test is useful to check that we account for the scaling factor
2566 // in the gep. Indeed, gep float, [32,64), 128 is not 128 + [32,64).
2567 TEST_F(ComputeKnownBitsTest
, ComputeKnownBitsGEPWithRangeNoOverlap
) {
2569 "define void @test(ptr %p) {\n"
2570 " %A = load i64, ptr %p, !range !{i64 32, i64 64}\n"
2571 " %APtr = inttoptr i64 %A to float*"
2572 " %APtrPlus512 = getelementptr float, float* %APtr, i32 128\n"
2573 " %c = icmp ugt float* %APtrPlus512, inttoptr (i32 523 to float*)\n"
2574 " call void @llvm.assume(i1 %c)\n"
2577 "declare void @llvm.assume(i1)\n");
2578 AssumptionCache
AC(*F
);
2579 KnownBits Known
= computeKnownBits(A
, M
->getDataLayout(), /* Depth */ 0, &AC
,
2580 F
->front().getTerminator());
2581 EXPECT_EQ(Known
.Zero
.getZExtValue(), ~(64llu - 1));
2582 EXPECT_EQ(Known
.One
.getZExtValue(), 32u);
2583 Instruction
&APtrPlus512
= findInstructionByName(F
, "APtrPlus512");
2584 Known
= computeKnownBits(&APtrPlus512
, M
->getDataLayout(), /* Depth */ 0, &AC
,
2585 F
->front().getTerminator());
2586 EXPECT_EQ(Known
.Zero
.getZExtValue(), ~512llu & ~(64llu - 1));
2587 EXPECT_EQ(Known
.One
.getZExtValue(), 512u | 32u);
2588 // The known range is not precise given computeKnownBits works
2589 // with the masks of zeros and ones, not the ranges.
2590 EXPECT_EQ(Known
.getMinValue(), 544);
2591 EXPECT_EQ(Known
.getMaxValue(), 575);
2594 TEST_F(ComputeKnownBitsTest
, ComputeKnownBitsAbsoluteSymbol
) {
2595 auto M
= parseModule(R
"(
2596 @absolute_0_255 = external global [128 x i32], align 1, !absolute_symbol !0
2597 @absolute_0_256 = external global [128 x i32], align 1, !absolute_symbol !1
2598 @absolute_256_512 = external global [128 x i32], align 1, !absolute_symbol !2
2599 @absolute_0_neg1 = external global [128 x i32], align 1, !absolute_symbol !3
2600 @absolute_neg32_32 = external global [128 x i32], align 1, !absolute_symbol !4
2601 @absolute_neg32_33 = external global [128 x i32], align 1, !absolute_symbol !5
2602 @absolute_neg64_neg32 = external global [128 x i32], align 1, !absolute_symbol !6
2603 @absolute_0_256_align8 = external global [128 x i32], align 8, !absolute_symbol !1
2605 !0 = !{i64 0, i64 255}
2606 !1 = !{i64 0, i64 256}
2607 !2 = !{i64 256, i64 512}
2608 !3 = !{i64 0, i64 -1}
2609 !4 = !{i64 -32, i64 32}
2610 !5 = !{i64 -32, i64 33}
2611 !6 = !{i64 -64, i64 -32}
2614 GlobalValue
*Absolute_0_255
= M
->getNamedValue("absolute_0_255");
2615 GlobalValue
*Absolute_0_256
= M
->getNamedValue("absolute_0_256");
2616 GlobalValue
*Absolute_256_512
= M
->getNamedValue("absolute_256_512");
2617 GlobalValue
*Absolute_0_Neg1
= M
->getNamedValue("absolute_0_neg1");
2618 GlobalValue
*Absolute_Neg32_32
= M
->getNamedValue("absolute_neg32_32");
2619 GlobalValue
*Absolute_Neg32_33
= M
->getNamedValue("absolute_neg32_33");
2620 GlobalValue
*Absolute_Neg64_Neg32
= M
->getNamedValue("absolute_neg64_neg32");
2621 GlobalValue
*Absolute_0_256_Align8
=
2622 M
->getNamedValue("absolute_0_256_align8");
2624 KnownBits Known_0_255
= computeKnownBits(Absolute_0_255
, M
->getDataLayout());
2625 EXPECT_EQ(64u - 8u, Known_0_255
.countMinLeadingZeros());
2626 EXPECT_EQ(0u, Known_0_255
.countMinTrailingZeros());
2627 EXPECT_EQ(0u, Known_0_255
.countMinLeadingOnes());
2628 EXPECT_EQ(0u, Known_0_255
.countMinTrailingOnes());
2630 KnownBits Known_0_256
= computeKnownBits(Absolute_0_256
, M
->getDataLayout());
2631 EXPECT_EQ(64u - 8u, Known_0_256
.countMinLeadingZeros());
2632 EXPECT_EQ(0u, Known_0_256
.countMinTrailingZeros());
2633 EXPECT_EQ(0u, Known_0_256
.countMinLeadingOnes());
2634 EXPECT_EQ(0u, Known_0_256
.countMinTrailingOnes());
2636 KnownBits Known_256_512
=
2637 computeKnownBits(Absolute_256_512
, M
->getDataLayout());
2638 EXPECT_EQ(64u - 8u, Known_0_255
.countMinLeadingZeros());
2639 EXPECT_EQ(0u, Known_0_255
.countMinTrailingZeros());
2640 EXPECT_EQ(0u, Known_0_255
.countMinLeadingOnes());
2641 EXPECT_EQ(0u, Known_0_255
.countMinTrailingOnes());
2643 KnownBits Known_0_Neg1
=
2644 computeKnownBits(Absolute_0_Neg1
, M
->getDataLayout());
2645 EXPECT_EQ(0u, Known_0_Neg1
.countMinLeadingZeros());
2646 EXPECT_EQ(0u, Known_0_Neg1
.countMinTrailingZeros());
2647 EXPECT_EQ(0u, Known_0_Neg1
.countMinLeadingOnes());
2648 EXPECT_EQ(0u, Known_0_Neg1
.countMinTrailingOnes());
2650 KnownBits Known_Neg32_32
=
2651 computeKnownBits(Absolute_Neg32_32
, M
->getDataLayout());
2652 EXPECT_EQ(0u, Known_Neg32_32
.countMinLeadingZeros());
2653 EXPECT_EQ(0u, Known_Neg32_32
.countMinTrailingZeros());
2654 EXPECT_EQ(0u, Known_Neg32_32
.countMinLeadingOnes());
2655 EXPECT_EQ(0u, Known_Neg32_32
.countMinTrailingOnes());
2656 EXPECT_EQ(1u, Known_Neg32_32
.countMinSignBits());
2658 KnownBits Known_Neg32_33
=
2659 computeKnownBits(Absolute_Neg32_33
, M
->getDataLayout());
2660 EXPECT_EQ(0u, Known_Neg32_33
.countMinLeadingZeros());
2661 EXPECT_EQ(0u, Known_Neg32_33
.countMinTrailingZeros());
2662 EXPECT_EQ(0u, Known_Neg32_33
.countMinLeadingOnes());
2663 EXPECT_EQ(0u, Known_Neg32_33
.countMinTrailingOnes());
2664 EXPECT_EQ(1u, Known_Neg32_33
.countMinSignBits());
2666 KnownBits Known_Neg32_Neg32
=
2667 computeKnownBits(Absolute_Neg64_Neg32
, M
->getDataLayout());
2668 EXPECT_EQ(0u, Known_Neg32_Neg32
.countMinLeadingZeros());
2669 EXPECT_EQ(0u, Known_Neg32_Neg32
.countMinTrailingZeros());
2670 EXPECT_EQ(58u, Known_Neg32_Neg32
.countMinLeadingOnes());
2671 EXPECT_EQ(0u, Known_Neg32_Neg32
.countMinTrailingOnes());
2672 EXPECT_EQ(58u, Known_Neg32_Neg32
.countMinSignBits());
2674 KnownBits Known_0_256_Align8
=
2675 computeKnownBits(Absolute_0_256_Align8
, M
->getDataLayout());
2676 EXPECT_EQ(64u - 8u, Known_0_256_Align8
.countMinLeadingZeros());
2677 EXPECT_EQ(3u, Known_0_256_Align8
.countMinTrailingZeros());
2678 EXPECT_EQ(0u, Known_0_256_Align8
.countMinLeadingOnes());
2679 EXPECT_EQ(0u, Known_0_256_Align8
.countMinTrailingOnes());
2682 TEST_F(ValueTrackingTest
, HaveNoCommonBitsSet
) {
2684 // Check for an inverted mask: (X & ~M) op (Y & M).
2685 auto M
= parseModule(R
"(
2686 define i32 @test(i32 %X, i32 %Y, i32 noundef %M) {
2688 %LHS = and i32 %1, %X
2689 %RHS = and i32 %Y, %M
2690 %Ret = add i32 %LHS, %RHS
2694 auto *F
= M
->getFunction("test");
2695 auto *LHS
= findInstructionByNameOrNull(F
, "LHS");
2696 auto *RHS
= findInstructionByNameOrNull(F
, "RHS");
2698 const DataLayout
&DL
= M
->getDataLayout();
2699 EXPECT_TRUE(haveNoCommonBitsSet(LHS
, RHS
, DL
));
2700 EXPECT_TRUE(haveNoCommonBitsSet(RHS
, LHS
, DL
));
2703 // Check for (A & B) and ~(A | B)
2704 auto M
= parseModule(R
"(
2705 define void @test(i32 noundef %A, i32 noundef %B) {
2706 %LHS = and i32 %A, %B
2708 %RHS = xor i32 %or, -1
2710 %LHS2 = and i32 %B, %A
2711 %or2 = or i32 %A, %B
2712 %RHS2 = xor i32 %or2, -1
2717 auto *F
= M
->getFunction("test");
2718 const DataLayout
&DL
= M
->getDataLayout();
2720 auto *LHS
= findInstructionByNameOrNull(F
, "LHS");
2721 auto *RHS
= findInstructionByNameOrNull(F
, "RHS");
2722 EXPECT_TRUE(haveNoCommonBitsSet(LHS
, RHS
, DL
));
2723 EXPECT_TRUE(haveNoCommonBitsSet(RHS
, LHS
, DL
));
2725 auto *LHS2
= findInstructionByNameOrNull(F
, "LHS2");
2726 auto *RHS2
= findInstructionByNameOrNull(F
, "RHS2");
2727 EXPECT_TRUE(haveNoCommonBitsSet(LHS2
, RHS2
, DL
));
2728 EXPECT_TRUE(haveNoCommonBitsSet(RHS2
, LHS2
, DL
));
2731 // Check for (A & B) and ~(A | B) in vector version
2732 auto M
= parseModule(R
"(
2733 define void @test(<2 x i32> noundef %A, <2 x i32> noundef %B) {
2734 %LHS = and <2 x i32> %A, %B
2735 %or = or <2 x i32> %A, %B
2736 %RHS = xor <2 x i32> %or, <i32 -1, i32 -1>
2738 %LHS2 = and <2 x i32> %B, %A
2739 %or2 = or <2 x i32> %A, %B
2740 %RHS2 = xor <2 x i32> %or2, <i32 -1, i32 -1>
2745 auto *F
= M
->getFunction("test");
2746 const DataLayout
&DL
= M
->getDataLayout();
2748 auto *LHS
= findInstructionByNameOrNull(F
, "LHS");
2749 auto *RHS
= findInstructionByNameOrNull(F
, "RHS");
2750 EXPECT_TRUE(haveNoCommonBitsSet(LHS
, RHS
, DL
));
2751 EXPECT_TRUE(haveNoCommonBitsSet(RHS
, LHS
, DL
));
2753 auto *LHS2
= findInstructionByNameOrNull(F
, "LHS2");
2754 auto *RHS2
= findInstructionByNameOrNull(F
, "RHS2");
2755 EXPECT_TRUE(haveNoCommonBitsSet(LHS2
, RHS2
, DL
));
2756 EXPECT_TRUE(haveNoCommonBitsSet(RHS2
, LHS2
, DL
));
2760 class IsBytewiseValueTest
: public ValueTrackingTest
,
2761 public ::testing::WithParamInterface
<
2762 std::pair
<const char *, const char *>> {
2766 const std::pair
<const char *, const char *> IsBytewiseValueTests
[] = {
2777 "i8 zeroinitializer",
2841 "float 0xFFFFFFFFE0000000",
2849 "double 0xF1F1F1F1F1F1F1F1",
2853 "ptr inttoptr (i64 0 to ptr)",
2857 "ptr inttoptr (i64 -1 to ptr)",
2861 "ptr inttoptr (i64 -6148914691236517206 to ptr)",
2865 "ptr inttoptr (i48 -1 to ptr)",
2869 "ptr inttoptr (i96 -1 to ptr)",
2873 "[0 x i8] zeroinitializer",
2881 "[5 x [0 x i8]] zeroinitializer",
2885 "[5 x [0 x i8]] undef",
2889 "[6 x i8] zeroinitializer",
2897 "[5 x i8] [i8 1, i8 1, i8 1, i8 1, i8 1]",
2901 "[5 x i64] [i64 1, i64 1, i64 1, i64 1, i64 1]",
2905 "[5 x i64] [i64 -1, i64 -1, i64 -1, i64 -1, i64 -1]",
2909 "[4 x i8] [i8 1, i8 2, i8 1, i8 1]",
2913 "[4 x i8] [i8 1, i8 undef, i8 1, i8 1]",
2917 "<6 x i8> zeroinitializer",
2925 "<5 x i8> <i8 1, i8 1, i8 1, i8 1, i8 1>",
2929 "<5 x i64> <i64 1, i64 1, i64 1, i64 1, i64 1>",
2933 "<5 x i64> <i64 -1, i64 -1, i64 -1, i64 -1, i64 -1>",
2937 "<4 x i8> <i8 1, i8 1, i8 2, i8 1>",
2941 "<2 x i8> < i8 5, i8 undef >",
2945 "[2 x [2 x i16]] zeroinitializer",
2949 "[2 x [2 x i16]] undef",
2953 "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], "
2954 "[2 x i16] [i16 -21846, i16 -21846]]",
2958 "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], "
2959 "[2 x i16] [i16 -21836, i16 -21846]]",
2963 "{ } zeroinitializer",
2971 "{ {}, {} } zeroinitializer",
2979 "{i8, i64, ptr} zeroinitializer",
2983 "{i8, i64, ptr} undef",
2987 "{i8, i64, ptr} {i8 -86, i64 -6148914691236517206, ptr undef}",
2991 "{i8, i64, ptr} {i8 86, i64 -6148914691236517206, ptr undef}",
2995 INSTANTIATE_TEST_SUITE_P(IsBytewiseValueParamTests
, IsBytewiseValueTest
,
2996 ::testing::ValuesIn(IsBytewiseValueTests
));
2998 TEST_P(IsBytewiseValueTest
, IsBytewiseValue
) {
2999 auto M
= parseModule(std::string("@test = global ") + GetParam().second
);
3000 GlobalVariable
*GV
= dyn_cast
<GlobalVariable
>(M
->getNamedValue("test"));
3001 Value
*Actual
= isBytewiseValue(GV
->getInitializer(), M
->getDataLayout());
3003 raw_string_ostream
S(Buff
);
3006 EXPECT_EQ(GetParam().first
, Buff
);
3009 TEST_F(ValueTrackingTest
, ComputeConstantRange
) {
3016 auto M
= parseModule(R
"(
3017 declare void @llvm.assume(i1)
3019 define i32 @test(i32 %stride) {
3020 %gt = icmp uge i32 %stride, 5
3021 call void @llvm.assume(i1 %gt)
3022 %lt = icmp ult i32 %stride, 10
3023 call void @llvm.assume(i1 %lt)
3024 %stride.plus.one = add nsw nuw i32 %stride, 1
3025 ret i32 %stride.plus.one
3027 Function
*F
= M
->getFunction("test");
3029 AssumptionCache
AC(*F
);
3030 Value
*Stride
= &*F
->arg_begin();
3031 ConstantRange CR1
= computeConstantRange(Stride
, false, true, &AC
, nullptr);
3032 EXPECT_TRUE(CR1
.isFullSet());
3034 Instruction
*I
= &findInstructionByName(F
, "stride.plus.one");
3035 ConstantRange CR2
= computeConstantRange(Stride
, false, true, &AC
, I
);
3036 EXPECT_EQ(5, CR2
.getLower());
3037 EXPECT_EQ(10, CR2
.getUpper());
3046 // stride = [99, 100)
3047 auto M
= parseModule(R
"(
3048 declare void @llvm.assume(i1)
3050 define i32 @test(i32 %stride) {
3051 %gt = icmp uge i32 %stride, 5
3052 call void @llvm.assume(i1 %gt)
3053 %lt = icmp ult i32 %stride, 200
3054 call void @llvm.assume(i1 %lt)
3055 %eq = icmp eq i32 %stride, 99
3056 call void @llvm.assume(i1 %eq)
3057 %stride.plus.one = add nsw nuw i32 %stride, 1
3058 ret i32 %stride.plus.one
3060 Function
*F
= M
->getFunction("test");
3062 AssumptionCache
AC(*F
);
3063 Value
*Stride
= &*F
->arg_begin();
3064 Instruction
*I
= &findInstructionByName(F
, "stride.plus.one");
3065 ConstantRange CR
= computeConstantRange(Stride
, false, true, &AC
, I
);
3066 EXPECT_EQ(99, *CR
.getSingleElement());
3076 // stride = [50, 100)
3077 auto M
= parseModule(R
"(
3078 declare void @llvm.assume(i1)
3080 define i32 @test(i32 %stride, i1 %cond) {
3081 %gt = icmp uge i32 %stride, 5
3082 call void @llvm.assume(i1 %gt)
3083 %gt.2 = icmp uge i32 %stride, 50
3084 call void @llvm.assume(i1 %gt.2)
3085 br i1 %cond, label %bb1, label %bb2
3088 %lt = icmp ult i32 %stride, 200
3089 call void @llvm.assume(i1 %lt)
3090 %lt.2 = icmp ult i32 %stride, 100
3091 call void @llvm.assume(i1 %lt.2)
3092 %stride.plus.one = add nsw nuw i32 %stride, 1
3093 ret i32 %stride.plus.one
3098 Function
*F
= M
->getFunction("test");
3100 AssumptionCache
AC(*F
);
3101 Value
*Stride
= &*F
->arg_begin();
3102 Instruction
*GT2
= &findInstructionByName(F
, "gt.2");
3103 ConstantRange CR
= computeConstantRange(Stride
, false, true, &AC
, GT2
);
3104 EXPECT_EQ(5, CR
.getLower());
3105 EXPECT_EQ(0, CR
.getUpper());
3107 Instruction
*I
= &findInstructionByName(F
, "stride.plus.one");
3108 ConstantRange CR2
= computeConstantRange(Stride
, false, true, &AC
, I
);
3109 EXPECT_EQ(50, CR2
.getLower());
3110 EXPECT_EQ(100, CR2
.getUpper());
3118 // stride = empty range, as the assumptions contradict each other.
3119 auto M
= parseModule(R
"(
3120 declare void @llvm.assume(i1)
3122 define i32 @test(i32 %stride, i1 %cond) {
3123 %gt = icmp ugt i32 %stride, 5
3124 call void @llvm.assume(i1 %gt)
3125 %lt = icmp ult i32 %stride, 5
3126 call void @llvm.assume(i1 %lt)
3127 %stride.plus.one = add nsw nuw i32 %stride, 1
3128 ret i32 %stride.plus.one
3130 Function
*F
= M
->getFunction("test");
3132 AssumptionCache
AC(*F
);
3133 Value
*Stride
= &*F
->arg_begin();
3135 Instruction
*I
= &findInstructionByName(F
, "stride.plus.one");
3136 ConstantRange CR
= computeConstantRange(Stride
, false, true, &AC
, I
);
3137 EXPECT_TRUE(CR
.isEmptySet());
3146 auto M
= parseModule(R
"(
3147 declare void @llvm.assume(i1)
3149 define i32 @test(i32 %x.1, i32 %x.2) {
3150 %gt = icmp uge i32 %x.1, 5
3151 call void @llvm.assume(i1 %gt)
3152 %lt = icmp ult i32 %x.2, %x.1
3153 call void @llvm.assume(i1 %lt)
3154 %stride.plus.one = add nsw nuw i32 %x.1, 1
3155 ret i32 %stride.plus.one
3157 Function
*F
= M
->getFunction("test");
3159 AssumptionCache
AC(*F
);
3160 Value
*X1
= &*(F
->arg_begin());
3161 Value
*X2
= &*std::next(F
->arg_begin());
3163 Instruction
*I
= &findInstructionByName(F
, "stride.plus.one");
3164 ConstantRange CR1
= computeConstantRange(X1
, false, true, &AC
, I
);
3165 ConstantRange CR2
= computeConstantRange(X2
, false, true, &AC
, I
);
3167 EXPECT_EQ(5, CR1
.getLower());
3168 EXPECT_EQ(0, CR1
.getUpper());
3170 EXPECT_EQ(0, CR2
.getLower());
3171 EXPECT_EQ(0xffffffff, CR2
.getUpper());
3173 // Check the depth cutoff results in a conservative result (full set) by
3174 // passing Depth == MaxDepth == 6.
3175 ConstantRange CR3
= computeConstantRange(X2
, false, true, &AC
, I
, nullptr, 6);
3176 EXPECT_TRUE(CR3
.isFullSet());
3181 auto M
= parseModule(R
"(
3182 declare void @llvm.assume(i1)
3184 define i32 @test(i32 %x.1, i32 %x.2) {
3185 %lt = icmp ule i32 %x.2, %x.1
3186 call void @llvm.assume(i1 %lt)
3187 %stride.plus.one = add nsw nuw i32 %x.1, 1
3188 ret i32 %stride.plus.one
3190 Function
*F
= M
->getFunction("test");
3192 AssumptionCache
AC(*F
);
3193 Value
*X2
= &*std::next(F
->arg_begin());
3195 Instruction
*I
= &findInstructionByName(F
, "stride.plus.one");
3196 ConstantRange CR1
= computeConstantRange(X2
, false, true, &AC
, I
);
3197 // If we don't know the value of x.2, we don't know the value of x.1.
3198 EXPECT_TRUE(CR1
.isFullSet());
3202 struct FindAllocaForValueTestParams
{
3204 bool AnyOffsetResult
;
3205 bool ZeroOffsetResult
;
3208 class FindAllocaForValueTest
3209 : public ValueTrackingTest
,
3210 public ::testing::WithParamInterface
<FindAllocaForValueTestParams
> {
3214 const FindAllocaForValueTestParams FindAllocaForValueTests
[] = {
3216 define void @test() {
3218 %r = bitcast ptr %a to ptr
3224 define void @test() {
3226 %r = getelementptr i32, ptr %a, i32 1
3232 define void @test() {
3234 %r = getelementptr i32, ptr %a, i32 0
3240 define void @test(i1 %cond) {
3246 %r = phi ptr [ %a, %entry ], [ %r, %bb1 ]
3247 br i1 %cond, label %bb1, label %exit
3255 define void @test(i1 %cond) {
3257 %r = select i1 %cond, ptr %a, ptr %a
3263 define void @test(i1 %cond) {
3266 %r = select i1 %cond, ptr %a, ptr %b
3272 define void @test(i1 %cond) {
3275 %a32 = bitcast ptr %a to ptr
3279 %x = phi ptr [ %a32, %entry ], [ %x, %bb1 ]
3280 %r = getelementptr i32, ptr %x, i32 1
3281 br i1 %cond, label %bb1, label %exit
3289 define void @test(i1 %cond) {
3292 %a32 = bitcast ptr %a to ptr
3296 %x = phi ptr [ %a32, %entry ], [ %r, %bb1 ]
3297 %r = getelementptr i32, ptr %x, i32 1
3298 br i1 %cond, label %bb1, label %exit
3306 define void @test(i1 %cond, ptr %a) {
3308 %r = bitcast ptr %a to ptr
3314 define void @test(i1 %cond) {
3321 %r = phi ptr [ %a, %entry ], [ %b, %bb1 ]
3322 br i1 %cond, label %bb1, label %exit
3329 declare ptr @retptr(ptr returned)
3330 define void @test(i1 %cond) {
3332 %r = call ptr @retptr(ptr %a)
3337 declare ptr @fun(ptr)
3338 define void @test(i1 %cond) {
3340 %r = call ptr @fun(ptr %a)
3346 TEST_P(FindAllocaForValueTest
, findAllocaForValue
) {
3347 auto M
= parseModule(GetParam().IR
);
3348 Function
*F
= M
->getFunction("test");
3349 Instruction
*I
= &findInstructionByName(F
, "r");
3350 const AllocaInst
*AI
= findAllocaForValue(I
);
3351 EXPECT_EQ(!!AI
, GetParam().AnyOffsetResult
);
3354 TEST_P(FindAllocaForValueTest
, findAllocaForValueZeroOffset
) {
3355 auto M
= parseModule(GetParam().IR
);
3356 Function
*F
= M
->getFunction("test");
3357 Instruction
*I
= &findInstructionByName(F
, "r");
3358 const AllocaInst
*AI
= findAllocaForValue(I
, true);
3359 EXPECT_EQ(!!AI
, GetParam().ZeroOffsetResult
);
3362 INSTANTIATE_TEST_SUITE_P(FindAllocaForValueTest
, FindAllocaForValueTest
,
3363 ::testing::ValuesIn(FindAllocaForValueTests
));