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/AsmParser/Parser.h"
11 #include "llvm/IR/Function.h"
12 #include "llvm/IR/InstIterator.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/Support/ErrorHandling.h"
16 #include "llvm/Support/KnownBits.h"
17 #include "llvm/Support/SourceMgr.h"
18 #include "gtest/gtest.h"
24 class ValueTrackingTest
: public testing::Test
{
26 std::unique_ptr
<Module
> parseModule(StringRef Assembly
) {
28 std::unique_ptr
<Module
> M
= parseAssemblyString(Assembly
, Error
, Context
);
31 raw_string_ostream
os(errMsg
);
33 EXPECT_TRUE(M
) << os
.str();
38 void parseAssembly(StringRef Assembly
) {
39 M
= parseModule(Assembly
);
42 Function
*F
= M
->getFunction("test");
43 ASSERT_TRUE(F
) << "Test must have a function @test";
48 for (inst_iterator I
= inst_begin(F
), E
= inst_end(F
); I
!= E
; ++I
) {
50 if (I
->getName() == "A")
54 ASSERT_TRUE(A
) << "@test must have an instruction %A";
58 std::unique_ptr
<Module
> M
;
59 Instruction
*A
= nullptr;
62 class MatchSelectPatternTest
: public ValueTrackingTest
{
64 void expectPattern(const SelectPatternResult
&P
) {
66 Instruction::CastOps CastOp
;
67 SelectPatternResult R
= matchSelectPattern(A
, LHS
, RHS
, &CastOp
);
68 EXPECT_EQ(P
.Flavor
, R
.Flavor
);
69 EXPECT_EQ(P
.NaNBehavior
, R
.NaNBehavior
);
70 EXPECT_EQ(P
.Ordered
, R
.Ordered
);
74 class ComputeKnownBitsTest
: public ValueTrackingTest
{
76 void expectKnownBits(uint64_t Zero
, uint64_t One
) {
77 auto Known
= computeKnownBits(A
, M
->getDataLayout());
78 ASSERT_FALSE(Known
.hasConflict());
79 EXPECT_EQ(Known
.One
.getZExtValue(), One
);
80 EXPECT_EQ(Known
.Zero
.getZExtValue(), Zero
);
86 TEST_F(MatchSelectPatternTest
, SimpleFMin
) {
88 "define float @test(float %a) {\n"
89 " %1 = fcmp ult float %a, 5.0\n"
90 " %A = select i1 %1, float %a, float 5.0\n"
93 expectPattern({SPF_FMINNUM
, SPNB_RETURNS_NAN
, false});
96 TEST_F(MatchSelectPatternTest
, SimpleFMax
) {
98 "define float @test(float %a) {\n"
99 " %1 = fcmp ogt float %a, 5.0\n"
100 " %A = select i1 %1, float %a, float 5.0\n"
103 expectPattern({SPF_FMAXNUM
, SPNB_RETURNS_OTHER
, true});
106 TEST_F(MatchSelectPatternTest
, SwappedFMax
) {
108 "define float @test(float %a) {\n"
109 " %1 = fcmp olt float 5.0, %a\n"
110 " %A = select i1 %1, float %a, float 5.0\n"
113 expectPattern({SPF_FMAXNUM
, SPNB_RETURNS_OTHER
, false});
116 TEST_F(MatchSelectPatternTest
, SwappedFMax2
) {
118 "define float @test(float %a) {\n"
119 " %1 = fcmp olt float %a, 5.0\n"
120 " %A = select i1 %1, float 5.0, float %a\n"
123 expectPattern({SPF_FMAXNUM
, SPNB_RETURNS_NAN
, false});
126 TEST_F(MatchSelectPatternTest
, SwappedFMax3
) {
128 "define float @test(float %a) {\n"
129 " %1 = fcmp ult float %a, 5.0\n"
130 " %A = select i1 %1, float 5.0, float %a\n"
133 expectPattern({SPF_FMAXNUM
, SPNB_RETURNS_OTHER
, true});
136 TEST_F(MatchSelectPatternTest
, FastFMin
) {
138 "define float @test(float %a) {\n"
139 " %1 = fcmp nnan olt float %a, 5.0\n"
140 " %A = select i1 %1, float %a, float 5.0\n"
143 expectPattern({SPF_FMINNUM
, SPNB_RETURNS_ANY
, false});
146 TEST_F(MatchSelectPatternTest
, FMinConstantZero
) {
148 "define float @test(float %a) {\n"
149 " %1 = fcmp ole float %a, 0.0\n"
150 " %A = select i1 %1, float %a, float 0.0\n"
153 // This shouldn't be matched, as %a could be -0.0.
154 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
157 TEST_F(MatchSelectPatternTest
, FMinConstantZeroNsz
) {
159 "define float @test(float %a) {\n"
160 " %1 = fcmp nsz ole float %a, 0.0\n"
161 " %A = select i1 %1, float %a, float 0.0\n"
164 // But this should be, because we've ignored signed zeroes.
165 expectPattern({SPF_FMINNUM
, SPNB_RETURNS_OTHER
, true});
168 TEST_F(MatchSelectPatternTest
, FMinMismatchConstantZero1
) {
170 "define float @test(float %a) {\n"
171 " %1 = fcmp olt float -0.0, %a\n"
172 " %A = select i1 %1, float 0.0, float %a\n"
175 // The sign of zero doesn't matter in fcmp.
176 expectPattern({SPF_FMINNUM
, SPNB_RETURNS_NAN
, true});
179 TEST_F(MatchSelectPatternTest
, FMinMismatchConstantZero2
) {
181 "define float @test(float %a) {\n"
182 " %1 = fcmp ogt float %a, -0.0\n"
183 " %A = select i1 %1, float 0.0, float %a\n"
186 // The sign of zero doesn't matter in fcmp.
187 expectPattern({SPF_FMINNUM
, SPNB_RETURNS_NAN
, false});
190 TEST_F(MatchSelectPatternTest
, FMinMismatchConstantZero3
) {
192 "define float @test(float %a) {\n"
193 " %1 = fcmp olt float 0.0, %a\n"
194 " %A = select i1 %1, float -0.0, float %a\n"
197 // The sign of zero doesn't matter in fcmp.
198 expectPattern({SPF_FMINNUM
, SPNB_RETURNS_NAN
, true});
201 TEST_F(MatchSelectPatternTest
, FMinMismatchConstantZero4
) {
203 "define float @test(float %a) {\n"
204 " %1 = fcmp ogt float %a, 0.0\n"
205 " %A = select i1 %1, float -0.0, float %a\n"
208 // The sign of zero doesn't matter in fcmp.
209 expectPattern({SPF_FMINNUM
, SPNB_RETURNS_NAN
, false});
212 TEST_F(MatchSelectPatternTest
, FMinMismatchConstantZero5
) {
214 "define float @test(float %a) {\n"
215 " %1 = fcmp ogt float -0.0, %a\n"
216 " %A = select i1 %1, float %a, float 0.0\n"
219 // The sign of zero doesn't matter in fcmp.
220 expectPattern({SPF_FMINNUM
, SPNB_RETURNS_OTHER
, false});
223 TEST_F(MatchSelectPatternTest
, FMinMismatchConstantZero6
) {
225 "define float @test(float %a) {\n"
226 " %1 = fcmp olt float %a, -0.0\n"
227 " %A = select i1 %1, float %a, float 0.0\n"
230 // The sign of zero doesn't matter in fcmp.
231 expectPattern({SPF_FMINNUM
, SPNB_RETURNS_OTHER
, true});
234 TEST_F(MatchSelectPatternTest
, FMinMismatchConstantZero7
) {
236 "define float @test(float %a) {\n"
237 " %1 = fcmp ogt float 0.0, %a\n"
238 " %A = select i1 %1, float %a, float -0.0\n"
241 // The sign of zero doesn't matter in fcmp.
242 expectPattern({SPF_FMINNUM
, SPNB_RETURNS_OTHER
, false});
245 TEST_F(MatchSelectPatternTest
, FMinMismatchConstantZero8
) {
247 "define float @test(float %a) {\n"
248 " %1 = fcmp olt float %a, 0.0\n"
249 " %A = select i1 %1, float %a, float -0.0\n"
252 // The sign of zero doesn't matter in fcmp.
253 expectPattern({SPF_FMINNUM
, SPNB_RETURNS_OTHER
, true});
256 TEST_F(MatchSelectPatternTest
, FMaxMismatchConstantZero1
) {
258 "define float @test(float %a) {\n"
259 " %1 = fcmp ogt float -0.0, %a\n"
260 " %A = select i1 %1, float 0.0, float %a\n"
263 // The sign of zero doesn't matter in fcmp.
264 expectPattern({SPF_FMAXNUM
, SPNB_RETURNS_NAN
, true});
267 TEST_F(MatchSelectPatternTest
, FMaxMismatchConstantZero2
) {
269 "define float @test(float %a) {\n"
270 " %1 = fcmp olt float %a, -0.0\n"
271 " %A = select i1 %1, float 0.0, float %a\n"
274 // The sign of zero doesn't matter in fcmp.
275 expectPattern({SPF_FMAXNUM
, SPNB_RETURNS_NAN
, false});
278 TEST_F(MatchSelectPatternTest
, FMaxMismatchConstantZero3
) {
280 "define float @test(float %a) {\n"
281 " %1 = fcmp ogt float 0.0, %a\n"
282 " %A = select i1 %1, float -0.0, float %a\n"
285 // The sign of zero doesn't matter in fcmp.
286 expectPattern({SPF_FMAXNUM
, SPNB_RETURNS_NAN
, true});
289 TEST_F(MatchSelectPatternTest
, FMaxMismatchConstantZero4
) {
291 "define float @test(float %a) {\n"
292 " %1 = fcmp olt float %a, 0.0\n"
293 " %A = select i1 %1, float -0.0, float %a\n"
296 // The sign of zero doesn't matter in fcmp.
297 expectPattern({SPF_FMAXNUM
, SPNB_RETURNS_NAN
, false});
300 TEST_F(MatchSelectPatternTest
, FMaxMismatchConstantZero5
) {
302 "define float @test(float %a) {\n"
303 " %1 = fcmp olt float -0.0, %a\n"
304 " %A = select i1 %1, float %a, float 0.0\n"
307 // The sign of zero doesn't matter in fcmp.
308 expectPattern({SPF_FMAXNUM
, SPNB_RETURNS_OTHER
, false});
311 TEST_F(MatchSelectPatternTest
, FMaxMismatchConstantZero6
) {
313 "define float @test(float %a) {\n"
314 " %1 = fcmp ogt float %a, -0.0\n"
315 " %A = select i1 %1, float %a, float 0.0\n"
318 // The sign of zero doesn't matter in fcmp.
319 expectPattern({SPF_FMAXNUM
, SPNB_RETURNS_OTHER
, true});
322 TEST_F(MatchSelectPatternTest
, FMaxMismatchConstantZero7
) {
324 "define float @test(float %a) {\n"
325 " %1 = fcmp olt float 0.0, %a\n"
326 " %A = select i1 %1, float %a, float -0.0\n"
329 // The sign of zero doesn't matter in fcmp.
330 expectPattern({SPF_FMAXNUM
, SPNB_RETURNS_OTHER
, false});
333 TEST_F(MatchSelectPatternTest
, FMaxMismatchConstantZero8
) {
335 "define float @test(float %a) {\n"
336 " %1 = fcmp ogt float %a, 0.0\n"
337 " %A = select i1 %1, float %a, float -0.0\n"
340 // The sign of zero doesn't matter in fcmp.
341 expectPattern({SPF_FMAXNUM
, SPNB_RETURNS_OTHER
, true});
344 TEST_F(MatchSelectPatternTest
, FMinMismatchConstantZeroVecUndef
) {
346 "define <2 x float> @test(<2 x float> %a) {\n"
347 " %1 = fcmp ogt <2 x float> %a, <float -0.0, float -0.0>\n"
348 " %A = select <2 x i1> %1, <2 x float> <float undef, float 0.0>, <2 x float> %a\n"
349 " ret <2 x float> %A\n"
351 // An undef in a vector constant can not be back-propagated for this analysis.
352 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
355 TEST_F(MatchSelectPatternTest
, FMaxMismatchConstantZeroVecUndef
) {
357 "define <2 x float> @test(<2 x float> %a) {\n"
358 " %1 = fcmp ogt <2 x float> %a, zeroinitializer\n"
359 " %A = select <2 x i1> %1, <2 x float> %a, <2 x float> <float -0.0, float undef>\n"
360 " ret <2 x float> %A\n"
362 // An undef in a vector constant can not be back-propagated for this analysis.
363 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
366 TEST_F(MatchSelectPatternTest
, VectorFMinimum
) {
368 "define <4 x float> @test(<4 x float> %a) {\n"
369 " %1 = fcmp ule <4 x float> %a, \n"
370 " <float 5.0, float 5.0, float 5.0, float 5.0>\n"
371 " %A = select <4 x i1> %1, <4 x float> %a,\n"
372 " <4 x float> <float 5.0, float 5.0, float 5.0, float 5.0>\n"
373 " ret <4 x float> %A\n"
375 // Check that pattern matching works on vectors where each lane has the same
376 // unordered pattern.
377 expectPattern({SPF_FMINNUM
, SPNB_RETURNS_NAN
, false});
380 TEST_F(MatchSelectPatternTest
, VectorFMinOtherOrdered
) {
382 "define <4 x float> @test(<4 x float> %a) {\n"
383 " %1 = fcmp ole <4 x float> %a, \n"
384 " <float 5.0, float 5.0, float 5.0, float 5.0>\n"
385 " %A = select <4 x i1> %1, <4 x float> %a,\n"
386 " <4 x float> <float 5.0, float 5.0, float 5.0, float 5.0>\n"
387 " ret <4 x float> %A\n"
389 // Check that pattern matching works on vectors where each lane has the same
391 expectPattern({SPF_FMINNUM
, SPNB_RETURNS_OTHER
, true});
394 TEST_F(MatchSelectPatternTest
, VectorNotFMinimum
) {
396 "define <4 x float> @test(<4 x float> %a) {\n"
397 " %1 = fcmp ule <4 x float> %a, \n"
398 " <float 5.0, float 0x7ff8000000000000, float 5.0, float 5.0>\n"
399 " %A = select <4 x i1> %1, <4 x float> %a,\n"
400 " <4 x float> <float 5.0, float 0x7ff8000000000000, float 5.0, float "
402 " ret <4 x float> %A\n"
404 // The lane that contains a NaN (0x7ff80...) behaves like a
405 // non-NaN-propagating min and the other lines behave like a NaN-propagating
406 // min, so check that neither is returned.
407 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
410 TEST_F(MatchSelectPatternTest
, VectorNotFMinZero
) {
412 "define <4 x float> @test(<4 x float> %a) {\n"
413 " %1 = fcmp ule <4 x float> %a, \n"
414 " <float 5.0, float -0.0, float 5.0, float 5.0>\n"
415 " %A = select <4 x i1> %1, <4 x float> %a,\n"
416 " <4 x float> <float 5.0, float 0.0, float 5.0, float 5.0>\n"
417 " ret <4 x float> %A\n"
419 // Always selects the second lane of %a if it is positive or negative zero, so
420 // this is stricter than a min.
421 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
424 TEST_F(MatchSelectPatternTest
, DoubleCastU
) {
426 "define i32 @test(i8 %a, i8 %b) {\n"
427 " %1 = icmp ult i8 %a, %b\n"
428 " %2 = zext i8 %a to i32\n"
429 " %3 = zext i8 %b to i32\n"
430 " %A = select i1 %1, i32 %2, i32 %3\n"
433 // We should be able to look through the situation where we cast both operands
435 expectPattern({SPF_UMIN
, SPNB_NA
, false});
438 TEST_F(MatchSelectPatternTest
, DoubleCastS
) {
440 "define i32 @test(i8 %a, i8 %b) {\n"
441 " %1 = icmp slt i8 %a, %b\n"
442 " %2 = sext i8 %a to i32\n"
443 " %3 = sext i8 %b to i32\n"
444 " %A = select i1 %1, i32 %2, i32 %3\n"
447 // We should be able to look through the situation where we cast both operands
449 expectPattern({SPF_SMIN
, SPNB_NA
, false});
452 TEST_F(MatchSelectPatternTest
, DoubleCastBad
) {
454 "define i32 @test(i8 %a, i8 %b) {\n"
455 " %1 = icmp ult i8 %a, %b\n"
456 " %2 = zext i8 %a to i32\n"
457 " %3 = sext i8 %b to i32\n"
458 " %A = select i1 %1, i32 %2, i32 %3\n"
461 // The cast types here aren't the same, so we cannot match an UMIN.
462 expectPattern({SPF_UNKNOWN
, SPNB_NA
, false});
465 TEST(ValueTracking
, GuaranteedToTransferExecutionToSuccessor
) {
467 "declare void @nounwind_readonly(i32*) nounwind readonly "
468 "declare void @nounwind_argmemonly(i32*) nounwind argmemonly "
469 "declare void @throws_but_readonly(i32*) readonly "
470 "declare void @throws_but_argmemonly(i32*) argmemonly "
471 "declare void @nounwind_willreturn(i32*) nounwind willreturn"
473 "declare void @unknown(i32*) "
475 "define void @f(i32* %p) { "
476 " call void @nounwind_readonly(i32* %p) "
477 " call void @nounwind_argmemonly(i32* %p) "
478 " call void @throws_but_readonly(i32* %p) "
479 " call void @throws_but_argmemonly(i32* %p) "
480 " call void @unknown(i32* %p) nounwind readonly "
481 " call void @unknown(i32* %p) nounwind argmemonly "
482 " call void @unknown(i32* %p) readonly "
483 " call void @unknown(i32* %p) argmemonly "
484 " call void @nounwind_willreturn(i32* %p)"
490 auto M
= parseAssemblyString(Assembly
, Error
, Context
);
491 assert(M
&& "Bad assembly?");
493 auto *F
= M
->getFunction("f");
494 assert(F
&& "Bad assembly?");
496 auto &BB
= F
->getEntryBlock();
497 bool ExpectedAnswers
[] = {
498 true, // call void @nounwind_readonly(i32* %p)
499 true, // call void @nounwind_argmemonly(i32* %p)
500 false, // call void @throws_but_readonly(i32* %p)
501 false, // call void @throws_but_argmemonly(i32* %p)
502 true, // call void @unknown(i32* %p) nounwind readonly
503 true, // call void @unknown(i32* %p) nounwind argmemonly
504 false, // call void @unknown(i32* %p) readonly
505 false, // call void @unknown(i32* %p) argmemonly
506 true, // call void @nounwind_willreturn(i32* %p)
512 EXPECT_EQ(isGuaranteedToTransferExecutionToSuccessor(&I
),
513 ExpectedAnswers
[Index
])
514 << "Incorrect answer at instruction " << Index
<< " = " << I
;
519 TEST_F(ValueTrackingTest
, ComputeNumSignBits_PR32045
) {
521 "define i32 @test(i32 %a) {\n"
522 " %A = ashr i32 %a, -1\n"
525 EXPECT_EQ(ComputeNumSignBits(A
, M
->getDataLayout()), 1u);
528 // No guarantees for canonical IR in this analysis, so this just bails out.
529 TEST_F(ValueTrackingTest
, ComputeNumSignBits_Shuffle
) {
531 "define <2 x i32> @test() {\n"
532 " %A = shufflevector <2 x i32> undef, <2 x i32> undef, <2 x i32> <i32 0, i32 0>\n"
533 " ret <2 x i32> %A\n"
535 EXPECT_EQ(ComputeNumSignBits(A
, M
->getDataLayout()), 1u);
538 // No guarantees for canonical IR in this analysis, so a shuffle element that
539 // references an undef value means this can't return any extra information.
540 TEST_F(ValueTrackingTest
, ComputeNumSignBits_Shuffle2
) {
542 "define <2 x i32> @test(<2 x i1> %x) {\n"
543 " %sext = sext <2 x i1> %x to <2 x i32>\n"
544 " %A = shufflevector <2 x i32> %sext, <2 x i32> undef, <2 x i32> <i32 0, i32 2>\n"
545 " ret <2 x i32> %A\n"
547 EXPECT_EQ(ComputeNumSignBits(A
, M
->getDataLayout()), 1u);
550 TEST_F(ComputeKnownBitsTest
, ComputeKnownBits
) {
552 "define i32 @test(i32 %a, i32 %b) {\n"
553 " %ash = mul i32 %a, 8\n"
554 " %aad = add i32 %ash, 7\n"
555 " %aan = and i32 %aad, 4095\n"
556 " %bsh = shl i32 %b, 4\n"
557 " %bad = or i32 %bsh, 6\n"
558 " %ban = and i32 %bad, 4095\n"
559 " %A = mul i32 %aan, %ban\n"
562 expectKnownBits(/*zero*/ 4278190085u, /*one*/ 10u);
565 TEST_F(ComputeKnownBitsTest
, ComputeKnownMulBits
) {
567 "define i32 @test(i32 %a, i32 %b) {\n"
568 " %aa = shl i32 %a, 5\n"
569 " %bb = shl i32 %b, 5\n"
570 " %aaa = or i32 %aa, 24\n"
571 " %bbb = or i32 %bb, 28\n"
572 " %A = mul i32 %aaa, %bbb\n"
575 expectKnownBits(/*zero*/ 95u, /*one*/ 32u);
578 TEST_F(ComputeKnownBitsTest
, ComputeKnownFshl
) {
579 // fshl(....1111....0000, 00..1111........, 6)
580 // = 11....000000..11
582 "define i16 @test(i16 %a, i16 %b) {\n"
583 " %aa = shl i16 %a, 4\n"
584 " %bb = lshr i16 %b, 2\n"
585 " %aaa = or i16 %aa, 3840\n"
586 " %bbb = or i16 %bb, 3840\n"
587 " %A = call i16 @llvm.fshl.i16(i16 %aaa, i16 %bbb, i16 6)\n"
590 "declare i16 @llvm.fshl.i16(i16, i16, i16)\n");
591 expectKnownBits(/*zero*/ 1008u, /*one*/ 49155u);
594 TEST_F(ComputeKnownBitsTest
, ComputeKnownFshr
) {
595 // fshr(....1111....0000, 00..1111........, 26)
596 // = 11....000000..11
598 "define i16 @test(i16 %a, i16 %b) {\n"
599 " %aa = shl i16 %a, 4\n"
600 " %bb = lshr i16 %b, 2\n"
601 " %aaa = or i16 %aa, 3840\n"
602 " %bbb = or i16 %bb, 3840\n"
603 " %A = call i16 @llvm.fshr.i16(i16 %aaa, i16 %bbb, i16 26)\n"
606 "declare i16 @llvm.fshr.i16(i16, i16, i16)\n");
607 expectKnownBits(/*zero*/ 1008u, /*one*/ 49155u);
610 TEST_F(ComputeKnownBitsTest
, ComputeKnownFshlZero
) {
611 // fshl(....1111....0000, 00..1111........, 0)
612 // = ....1111....0000
614 "define i16 @test(i16 %a, i16 %b) {\n"
615 " %aa = shl i16 %a, 4\n"
616 " %bb = lshr i16 %b, 2\n"
617 " %aaa = or i16 %aa, 3840\n"
618 " %bbb = or i16 %bb, 3840\n"
619 " %A = call i16 @llvm.fshl.i16(i16 %aaa, i16 %bbb, i16 0)\n"
622 "declare i16 @llvm.fshl.i16(i16, i16, i16)\n");
623 expectKnownBits(/*zero*/ 15u, /*one*/ 3840u);
626 TEST_F(ComputeKnownBitsTest
, ComputeKnownUAddSatLeadingOnes
) {
627 // uadd.sat(1111...1, ........)
630 "define i8 @test(i8 %a, i8 %b) {\n"
631 " %aa = or i8 %a, 241\n"
632 " %A = call i8 @llvm.uadd.sat.i8(i8 %aa, i8 %b)\n"
635 "declare i8 @llvm.uadd.sat.i8(i8, i8)\n");
636 expectKnownBits(/*zero*/ 0u, /*one*/ 240u);
639 TEST_F(ComputeKnownBitsTest
, ComputeKnownUAddSatOnesPreserved
) {
640 // uadd.sat(00...011, .1...110)
643 "define i8 @test(i8 %a, i8 %b) {\n"
644 " %aa = or i8 %a, 3\n"
645 " %aaa = and i8 %aa, 59\n"
646 " %bb = or i8 %b, 70\n"
647 " %bbb = and i8 %bb, 254\n"
648 " %A = call i8 @llvm.uadd.sat.i8(i8 %aaa, i8 %bbb)\n"
651 "declare i8 @llvm.uadd.sat.i8(i8, i8)\n");
652 expectKnownBits(/*zero*/ 0u, /*one*/ 1u);
655 TEST_F(ComputeKnownBitsTest
, ComputeKnownUSubSatLHSLeadingZeros
) {
656 // usub.sat(0000...0, ........)
659 "define i8 @test(i8 %a, i8 %b) {\n"
660 " %aa = and i8 %a, 14\n"
661 " %A = call i8 @llvm.usub.sat.i8(i8 %aa, i8 %b)\n"
664 "declare i8 @llvm.usub.sat.i8(i8, i8)\n");
665 expectKnownBits(/*zero*/ 240u, /*one*/ 0u);
668 TEST_F(ComputeKnownBitsTest
, ComputeKnownUSubSatRHSLeadingOnes
) {
669 // usub.sat(........, 1111...1)
672 "define i8 @test(i8 %a, i8 %b) {\n"
673 " %bb = or i8 %a, 241\n"
674 " %A = call i8 @llvm.usub.sat.i8(i8 %a, i8 %bb)\n"
677 "declare i8 @llvm.usub.sat.i8(i8, i8)\n");
678 expectKnownBits(/*zero*/ 240u, /*one*/ 0u);
681 TEST_F(ComputeKnownBitsTest
, ComputeKnownUSubSatZerosPreserved
) {
682 // usub.sat(11...011, .1...110)
685 "define i8 @test(i8 %a, i8 %b) {\n"
686 " %aa = or i8 %a, 195\n"
687 " %aaa = and i8 %aa, 251\n"
688 " %bb = or i8 %b, 70\n"
689 " %bbb = and i8 %bb, 254\n"
690 " %A = call i8 @llvm.usub.sat.i8(i8 %aaa, i8 %bbb)\n"
693 "declare i8 @llvm.usub.sat.i8(i8, i8)\n");
694 expectKnownBits(/*zero*/ 2u, /*one*/ 0u);
697 class IsBytewiseValueTest
: public ValueTrackingTest
,
698 public ::testing::WithParamInterface
<
699 std::pair
<const char *, const char *>> {
703 const std::pair
<const char *, const char *> IsBytewiseValueTests
[] = {
714 "i8 zeroinitializer",
778 "float 0xFFFFFFFFE0000000",
786 "double 0xF1F1F1F1F1F1F1F1",
794 "i16* inttoptr (i64 0 to i16*)",
798 "i16* inttoptr (i64 -1 to i16*)",
802 "i16* inttoptr (i64 -6148914691236517206 to i16*)",
806 "i16* inttoptr (i48 -1 to i16*)",
810 "i16* inttoptr (i96 -1 to i16*)",
814 "[0 x i8] zeroinitializer",
822 "[5 x [0 x i8]] zeroinitializer",
826 "[5 x [0 x i8]] undef",
830 "[6 x i8] zeroinitializer",
838 "[5 x i8] [i8 1, i8 1, i8 1, i8 1, i8 1]",
842 "[5 x i64] [i64 1, i64 1, i64 1, i64 1, i64 1]",
846 "[5 x i64] [i64 -1, i64 -1, i64 -1, i64 -1, i64 -1]",
850 "[4 x i8] [i8 1, i8 2, i8 1, i8 1]",
854 "[4 x i8] [i8 1, i8 undef, i8 1, i8 1]",
858 "<6 x i8> zeroinitializer",
866 "<5 x i8> <i8 1, i8 1, i8 1, i8 1, i8 1>",
870 "<5 x i64> <i64 1, i64 1, i64 1, i64 1, i64 1>",
874 "<5 x i64> <i64 -1, i64 -1, i64 -1, i64 -1, i64 -1>",
878 "<4 x i8> <i8 1, i8 1, i8 2, i8 1>",
882 "<2 x i8> < i8 5, i8 undef >",
886 "[2 x [2 x i16]] zeroinitializer",
890 "[2 x [2 x i16]] undef",
894 "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], "
895 "[2 x i16] [i16 -21846, i16 -21846]]",
899 "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], "
900 "[2 x i16] [i16 -21836, i16 -21846]]",
904 "{ } zeroinitializer",
912 "{ {}, {} } zeroinitializer",
920 "{i8, i64, i16*} zeroinitializer",
924 "{i8, i64, i16*} undef",
928 "{i8, i64, i16*} {i8 -86, i64 -6148914691236517206, i16* undef}",
932 "{i8, i64, i16*} {i8 86, i64 -6148914691236517206, i16* undef}",
936 INSTANTIATE_TEST_CASE_P(IsBytewiseValueParamTests
, IsBytewiseValueTest
,
937 ::testing::ValuesIn(IsBytewiseValueTests
),);
939 TEST_P(IsBytewiseValueTest
, IsBytewiseValue
) {
940 auto M
= parseModule(std::string("@test = global ") + GetParam().second
);
941 GlobalVariable
*GV
= dyn_cast
<GlobalVariable
>(M
->getNamedValue("test"));
942 Value
*Actual
= isBytewiseValue(GV
->getInitializer(), M
->getDataLayout());
944 raw_string_ostream
S(Buff
);
947 EXPECT_EQ(GetParam().first
, S
.str());