1 //===- ValueTrackingTest.cpp - ValueTracking tests ------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/Analysis/ValueTracking.h" 10 #include "llvm/Analysis/AssumptionCache.h" 11 #include "llvm/AsmParser/Parser.h" 12 #include "llvm/IR/ConstantRange.h" 13 #include "llvm/IR/Dominators.h" 14 #include "llvm/IR/Function.h" 15 #include "llvm/IR/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" 25 26 using namespace llvm; 27 28 namespace { 29 30 static Instruction *findInstructionByNameOrNull(Function *F, StringRef Name) { 31 for (Instruction &I : instructions(F)) 32 if (I.getName() == Name) 33 return &I; 34 35 return nullptr; 36 } 37 38 static Instruction &findInstructionByName(Function *F, StringRef Name) { 39 auto *I = findInstructionByNameOrNull(F, Name); 40 if (I) 41 return *I; 42 43 llvm_unreachable("Expected value not found"); 44 } 45 46 class ValueTrackingTest : public testing::Test { 47 protected: 48 std::unique_ptr<Module> parseModule(StringRef Assembly) { 49 SMDiagnostic Error; 50 std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context); 51 52 std::string errMsg; 53 raw_string_ostream os(errMsg); 54 Error.print("", os); 55 EXPECT_TRUE(M) << os.str(); 56 57 return M; 58 } 59 60 void parseAssembly(StringRef Assembly) { 61 M = parseModule(Assembly); 62 ASSERT_TRUE(M); 63 64 F = M->getFunction("test"); 65 ASSERT_TRUE(F) << "Test must have a function @test"; 66 if (!F) 67 return; 68 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"); 77 78 CxtI = findInstructionByNameOrNull(F, "CxtI"); 79 CxtI2 = findInstructionByNameOrNull(F, "CxtI2"); 80 CxtI3 = findInstructionByNameOrNull(F, "CxtI3"); 81 } 82 83 LLVMContext Context; 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; 90 91 // Context instructions (optional) 92 Instruction *CxtI = nullptr, *CxtI2 = nullptr, *CxtI3 = nullptr; 93 }; 94 95 class MatchSelectPatternTest : public ValueTrackingTest { 96 protected: 97 void expectPattern(const SelectPatternResult &P) { 98 Value *LHS, *RHS; 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); 104 } 105 }; 106 107 class ComputeKnownBitsTest : public ValueTrackingTest { 108 protected: 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); 114 } 115 }; 116 117 class ComputeKnownFPClassTest : public ValueTrackingTest { 118 protected: 119 void expectKnownFPClass(unsigned KnownTrue, std::optional<bool> SignBitKnown, 120 Instruction *TestVal = nullptr) { 121 if (!TestVal) 122 TestVal = A; 123 124 KnownFPClass Known = computeKnownFPClass(TestVal, M->getDataLayout()); 125 EXPECT_EQ(KnownTrue, Known.KnownFPClasses); 126 EXPECT_EQ(SignBitKnown, Known.SignBit); 127 } 128 }; 129 } 130 131 TEST_F(MatchSelectPatternTest, SimpleFMin) { 132 parseAssembly( 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" 136 " ret float %A\n" 137 "}\n"); 138 expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false}); 139 } 140 141 TEST_F(MatchSelectPatternTest, SimpleFMax) { 142 parseAssembly( 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" 146 " ret float %A\n" 147 "}\n"); 148 expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true}); 149 } 150 151 TEST_F(MatchSelectPatternTest, SwappedFMax) { 152 parseAssembly( 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" 156 " ret float %A\n" 157 "}\n"); 158 expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, false}); 159 } 160 161 TEST_F(MatchSelectPatternTest, SwappedFMax2) { 162 parseAssembly( 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" 166 " ret float %A\n" 167 "}\n"); 168 expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, false}); 169 } 170 171 TEST_F(MatchSelectPatternTest, SwappedFMax3) { 172 parseAssembly( 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" 176 " ret float %A\n" 177 "}\n"); 178 expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true}); 179 } 180 181 TEST_F(MatchSelectPatternTest, FastFMin) { 182 parseAssembly( 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" 186 " ret float %A\n" 187 "}\n"); 188 expectPattern({SPF_FMINNUM, SPNB_RETURNS_ANY, false}); 189 } 190 191 TEST_F(MatchSelectPatternTest, FMinConstantZero) { 192 parseAssembly( 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" 196 " ret float %A\n" 197 "}\n"); 198 // This shouldn't be matched, as %a could be -0.0. 199 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 200 } 201 202 TEST_F(MatchSelectPatternTest, FMinConstantZeroNsz) { 203 parseAssembly( 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" 207 " ret float %A\n" 208 "}\n"); 209 // But this should be, because we've ignored signed zeroes. 210 expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true}); 211 } 212 213 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero1) { 214 parseAssembly( 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" 218 " ret float %A\n" 219 "}\n"); 220 // The sign of zero doesn't matter in fcmp. 221 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 222 } 223 224 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero2) { 225 parseAssembly( 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" 229 " ret float %A\n" 230 "}\n"); 231 // The sign of zero doesn't matter in fcmp. 232 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 233 } 234 235 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero3) { 236 parseAssembly( 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" 240 " ret float %A\n" 241 "}\n"); 242 // The sign of zero doesn't matter in fcmp. 243 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 244 } 245 246 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero4) { 247 parseAssembly( 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" 251 " ret float %A\n" 252 "}\n"); 253 // The sign of zero doesn't matter in fcmp. 254 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 255 } 256 257 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero5) { 258 parseAssembly( 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" 262 " ret float %A\n" 263 "}\n"); 264 // The sign of zero doesn't matter in fcmp. 265 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 266 } 267 268 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero6) { 269 parseAssembly( 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" 273 " ret float %A\n" 274 "}\n"); 275 // The sign of zero doesn't matter in fcmp. 276 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 277 } 278 279 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero7) { 280 parseAssembly( 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" 284 " ret float %A\n" 285 "}\n"); 286 // The sign of zero doesn't matter in fcmp. 287 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 288 } 289 290 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero8) { 291 parseAssembly( 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" 295 " ret float %A\n" 296 "}\n"); 297 // The sign of zero doesn't matter in fcmp. 298 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 299 } 300 301 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero1) { 302 parseAssembly( 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" 306 " ret float %A\n" 307 "}\n"); 308 // The sign of zero doesn't matter in fcmp. 309 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 310 } 311 312 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero2) { 313 parseAssembly( 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" 317 " ret float %A\n" 318 "}\n"); 319 // The sign of zero doesn't matter in fcmp. 320 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 321 } 322 323 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero3) { 324 parseAssembly( 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" 328 " ret float %A\n" 329 "}\n"); 330 // The sign of zero doesn't matter in fcmp. 331 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 332 } 333 334 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero4) { 335 parseAssembly( 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" 339 " ret float %A\n" 340 "}\n"); 341 // The sign of zero doesn't matter in fcmp. 342 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 343 } 344 345 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero5) { 346 parseAssembly( 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" 350 " ret float %A\n" 351 "}\n"); 352 // The sign of zero doesn't matter in fcmp. 353 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 354 } 355 356 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero6) { 357 parseAssembly( 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" 361 " ret float %A\n" 362 "}\n"); 363 // The sign of zero doesn't matter in fcmp. 364 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 365 } 366 367 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero7) { 368 parseAssembly( 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" 372 " ret float %A\n" 373 "}\n"); 374 // The sign of zero doesn't matter in fcmp. 375 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 376 } 377 378 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero8) { 379 parseAssembly( 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" 383 " ret float %A\n" 384 "}\n"); 385 // The sign of zero doesn't matter in fcmp. 386 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 387 } 388 389 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZeroVecUndef) { 390 parseAssembly( 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" 395 "}\n"); 396 // An undef in a vector constant can not be back-propagated for this analysis. 397 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 398 } 399 400 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZeroVecUndef) { 401 parseAssembly( 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" 406 "}\n"); 407 // An undef in a vector constant can not be back-propagated for this analysis. 408 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 409 } 410 411 TEST_F(MatchSelectPatternTest, VectorFMinimum) { 412 parseAssembly( 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" 419 "}\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}); 423 } 424 425 TEST_F(MatchSelectPatternTest, VectorFMinOtherOrdered) { 426 parseAssembly( 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" 433 "}\n"); 434 // Check that pattern matching works on vectors where each lane has the same 435 // ordered pattern. 436 expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true}); 437 } 438 439 TEST_F(MatchSelectPatternTest, VectorNotFMinimum) { 440 parseAssembly( 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 " 446 "5.0>\n" 447 " ret <4 x float> %A\n" 448 "}\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}); 453 } 454 455 TEST_F(MatchSelectPatternTest, VectorNotFMinZero) { 456 parseAssembly( 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" 463 "}\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}); 467 } 468 469 TEST_F(MatchSelectPatternTest, DoubleCastU) { 470 parseAssembly( 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" 476 " ret i32 %A\n" 477 "}\n"); 478 // We should be able to look through the situation where we cast both operands 479 // to the select. 480 expectPattern({SPF_UMIN, SPNB_NA, false}); 481 } 482 483 TEST_F(MatchSelectPatternTest, DoubleCastS) { 484 parseAssembly( 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" 490 " ret i32 %A\n" 491 "}\n"); 492 // We should be able to look through the situation where we cast both operands 493 // to the select. 494 expectPattern({SPF_SMIN, SPNB_NA, false}); 495 } 496 497 TEST_F(MatchSelectPatternTest, DoubleCastBad) { 498 parseAssembly( 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" 504 " ret i32 %A\n" 505 "}\n"); 506 // The cast types here aren't the same, so we cannot match an UMIN. 507 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 508 } 509 510 TEST_F(MatchSelectPatternTest, NotNotSMin) { 511 parseAssembly( 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" 517 " ret i8 %A\n" 518 "}\n"); 519 expectPattern({SPF_SMIN, SPNB_NA, false}); 520 } 521 522 TEST_F(MatchSelectPatternTest, NotNotSMinSwap) { 523 parseAssembly( 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" 529 " ret <2 x i8> %A\n" 530 "}\n"); 531 expectPattern({SPF_SMIN, SPNB_NA, false}); 532 } 533 534 TEST_F(MatchSelectPatternTest, NotNotSMax) { 535 parseAssembly( 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" 541 " ret i8 %A\n" 542 "}\n"); 543 expectPattern({SPF_SMAX, SPNB_NA, false}); 544 } 545 546 TEST_F(MatchSelectPatternTest, NotNotSMaxSwap) { 547 parseAssembly( 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" 553 " ret <2 x i8> %A\n" 554 "}\n"); 555 expectPattern({SPF_SMAX, SPNB_NA, false}); 556 } 557 558 TEST_F(MatchSelectPatternTest, NotNotUMin) { 559 parseAssembly( 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" 565 " ret <2 x i8> %A\n" 566 "}\n"); 567 expectPattern({SPF_UMIN, SPNB_NA, false}); 568 } 569 570 TEST_F(MatchSelectPatternTest, NotNotUMinSwap) { 571 parseAssembly( 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" 577 " ret i8 %A\n" 578 "}\n"); 579 expectPattern({SPF_UMIN, SPNB_NA, false}); 580 } 581 582 TEST_F(MatchSelectPatternTest, NotNotUMax) { 583 parseAssembly( 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" 589 " ret <2 x i8> %A\n" 590 "}\n"); 591 expectPattern({SPF_UMAX, SPNB_NA, false}); 592 } 593 594 TEST_F(MatchSelectPatternTest, NotNotUMaxSwap) { 595 parseAssembly( 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" 601 " ret i8 %A\n" 602 "}\n"); 603 expectPattern({SPF_UMAX, SPNB_NA, false}); 604 } 605 606 TEST_F(MatchSelectPatternTest, NotNotEq) { 607 parseAssembly( 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" 613 " ret i8 %A\n" 614 "}\n"); 615 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 616 } 617 618 TEST_F(MatchSelectPatternTest, NotNotNe) { 619 parseAssembly( 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" 625 " ret i8 %A\n" 626 "}\n"); 627 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 628 } 629 630 TEST(ValueTracking, GuaranteedToTransferExecutionToSuccessor) { 631 StringRef Assembly = 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 " 638 " " 639 "declare void @unknown(ptr) " 640 " " 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 " 654 " ret void " 655 "} "; 656 657 LLVMContext Context; 658 SMDiagnostic Error; 659 auto M = parseAssemblyString(Assembly, Error, Context); 660 assert(M && "Bad assembly?"); 661 662 auto *F = M->getFunction("f"); 663 assert(F && "Bad assembly?"); 664 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 679 false, // ret void 680 }; 681 682 int Index = 0; 683 for (auto &I : BB) { 684 EXPECT_EQ(isGuaranteedToTransferExecutionToSuccessor(&I), 685 ExpectedAnswers[Index]) 686 << "Incorrect answer at instruction " << Index << " = " << I; 687 Index++; 688 } 689 } 690 691 TEST_F(ValueTrackingTest, ComputeNumSignBits_PR32045) { 692 parseAssembly( 693 "define i32 @test(i32 %a) {\n" 694 " %A = ashr i32 %a, -1\n" 695 " ret i32 %A\n" 696 "}\n"); 697 EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u); 698 } 699 700 // No guarantees for canonical IR in this analysis, so this just bails out. 701 TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle) { 702 parseAssembly( 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" 706 "}\n"); 707 EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u); 708 } 709 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) { 713 parseAssembly( 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" 718 "}\n"); 719 EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u); 720 } 721 722 TEST_F(ValueTrackingTest, impliesPoisonTest_Identity) { 723 parseAssembly("define void @test(i32 %x, i32 %y) {\n" 724 " %A = add i32 %x, %y\n" 725 " ret void\n" 726 "}"); 727 EXPECT_TRUE(impliesPoison(A, A)); 728 } 729 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" 734 " ret void\n" 735 "}"); 736 EXPECT_TRUE(impliesPoison(A2, A)); 737 } 738 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" 743 " ret void\n" 744 "}"); 745 EXPECT_FALSE(impliesPoison(A2, A)); 746 } 747 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" 752 " ret void\n" 753 "}"); 754 EXPECT_TRUE(impliesPoison(A2, A)); 755 } 756 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" 761 " ret void\n" 762 "}"); 763 EXPECT_TRUE(impliesPoison(A2, A)); 764 } 765 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" 770 " ret void\n" 771 "}"); 772 EXPECT_FALSE(impliesPoison(A2, A)); 773 } 774 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" 780 " ret void\n" 781 "}"); 782 EXPECT_TRUE(impliesPoison(A2, A)); 783 } 784 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" 790 " ret void\n" 791 "}"); 792 EXPECT_FALSE(impliesPoison(A2, A)); 793 } 794 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" 799 " ret void\n" 800 "}"); 801 EXPECT_TRUE(impliesPoison(A2, A)); 802 } 803 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" 810 " ret void\n" 811 "}"); 812 EXPECT_TRUE(impliesPoison(A2, A)); 813 } 814 815 TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle_Pointers) { 816 parseAssembly( 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" 820 "}\n"); 821 EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 64u); 822 } 823 824 TEST(ValueTracking, propagatesPoison) { 825 std::string AsmHead = 826 "declare i32 @g(i32)\n" 827 "declare {i32, i1} @llvm.sadd.with.overflow.i32(i32 %a, i32 %b)\n" 828 "declare {i32, i1} @llvm.ssub.with.overflow.i32(i32 %a, i32 %b)\n" 829 "declare {i32, i1} @llvm.smul.with.overflow.i32(i32 %a, i32 %b)\n" 830 "declare {i32, i1} @llvm.uadd.with.overflow.i32(i32 %a, i32 %b)\n" 831 "declare {i32, i1} @llvm.usub.with.overflow.i32(i32 %a, i32 %b)\n" 832 "declare {i32, i1} @llvm.umul.with.overflow.i32(i32 %a, i32 %b)\n" 833 "declare float @llvm.sqrt.f32(float)\n" 834 "declare float @llvm.powi.f32.i32(float, i32)\n" 835 "declare float @llvm.sin.f32(float)\n" 836 "declare float @llvm.cos.f32(float)\n" 837 "declare float @llvm.pow.f32(float, float)\n" 838 "declare float @llvm.exp.f32(float)\n" 839 "declare float @llvm.exp2.f32(float)\n" 840 "declare float @llvm.log.f32(float)\n" 841 "declare float @llvm.log10.f32(float)\n" 842 "declare float @llvm.log2.f32(float)\n" 843 "declare float @llvm.fma.f32(float, float, float)\n" 844 "declare float @llvm.fabs.f32(float)\n" 845 "declare float @llvm.minnum.f32(float, float)\n" 846 "declare float @llvm.maxnum.f32(float, float)\n" 847 "declare float @llvm.minimum.f32(float, float)\n" 848 "declare float @llvm.maximum.f32(float, float)\n" 849 "declare float @llvm.copysign.f32(float, float)\n" 850 "declare float @llvm.floor.f32(float)\n" 851 "declare float @llvm.ceil.f32(float)\n" 852 "declare float @llvm.trunc.f32(float)\n" 853 "declare float @llvm.rint.f32(float)\n" 854 "declare float @llvm.nearbyint.f32(float)\n" 855 "declare float @llvm.round.f32(float)\n" 856 "declare float @llvm.roundeven.f32(float)\n" 857 "declare i32 @llvm.lround.f32(float)\n" 858 "declare i64 @llvm.llround.f32(float)\n" 859 "declare i32 @llvm.lrint.f32(float)\n" 860 "declare i64 @llvm.llrint.f32(float)\n" 861 "declare float @llvm.fmuladd.f32(float, float, float)\n" 862 "define void @f(i32 %x, i32 %y, float %fx, float %fy, " 863 "i1 %cond, ptr %p) {\n"; 864 std::string AsmTail = " ret void\n}"; 865 // (propagates poison?, IR instruction) 866 SmallVector<std::tuple<bool, std::string, unsigned>, 32> Data = { 867 {true, "add i32 %x, %y", 0}, 868 {true, "add i32 %x, %y", 1}, 869 {true, "add nsw nuw i32 %x, %y", 0}, 870 {true, "add nsw nuw i32 %x, %y", 1}, 871 {true, "ashr i32 %x, %y", 0}, 872 {true, "ashr i32 %x, %y", 1}, 873 {true, "lshr exact i32 %x, 31", 0}, 874 {true, "lshr exact i32 %x, 31", 1}, 875 {true, "fadd float %fx, %fy", 0}, 876 {true, "fadd float %fx, %fy", 1}, 877 {true, "fsub float %fx, %fy", 0}, 878 {true, "fsub float %fx, %fy", 1}, 879 {true, "fmul float %fx, %fy", 0}, 880 {true, "fmul float %fx, %fy", 1}, 881 {true, "fdiv float %fx, %fy", 0}, 882 {true, "fdiv float %fx, %fy", 1}, 883 {true, "frem float %fx, %fy", 0}, 884 {true, "frem float %fx, %fy", 1}, 885 {true, "fneg float %fx", 0}, 886 {true, "fcmp oeq float %fx, %fy", 0}, 887 {true, "fcmp oeq float %fx, %fy", 1}, 888 {true, "icmp eq i32 %x, %y", 0}, 889 {true, "icmp eq i32 %x, %y", 1}, 890 {true, "getelementptr i8, ptr %p, i32 %x", 0}, 891 {true, "getelementptr i8, ptr %p, i32 %x", 1}, 892 {true, "getelementptr inbounds i8, ptr %p, i32 %x", 0}, 893 {true, "getelementptr inbounds i8, ptr %p, i32 %x", 1}, 894 {true, "bitcast float %fx to i32", 0}, 895 {true, "select i1 %cond, i32 %x, i32 %y", 0}, 896 {false, "select i1 %cond, i32 %x, i32 %y", 1}, 897 {false, "select i1 %cond, i32 %x, i32 %y", 2}, 898 {false, "freeze i32 %x", 0}, 899 {true, "udiv i32 %x, %y", 0}, 900 {true, "udiv i32 %x, %y", 1}, 901 {true, "urem i32 %x, %y", 0}, 902 {true, "urem i32 %x, %y", 1}, 903 {true, "sdiv exact i32 %x, %y", 0}, 904 {true, "sdiv exact i32 %x, %y", 1}, 905 {true, "srem i32 %x, %y", 0}, 906 {true, "srem i32 %x, %y", 1}, 907 {false, "call i32 @g(i32 %x)", 0}, 908 {false, "call i32 @g(i32 %x)", 1}, 909 {true, "call {i32, i1} @llvm.sadd.with.overflow.i32(i32 %x, i32 %y)", 0}, 910 {true, "call {i32, i1} @llvm.ssub.with.overflow.i32(i32 %x, i32 %y)", 0}, 911 {true, "call {i32, i1} @llvm.smul.with.overflow.i32(i32 %x, i32 %y)", 0}, 912 {true, "call {i32, i1} @llvm.uadd.with.overflow.i32(i32 %x, i32 %y)", 0}, 913 {true, "call {i32, i1} @llvm.usub.with.overflow.i32(i32 %x, i32 %y)", 0}, 914 {true, "call {i32, i1} @llvm.umul.with.overflow.i32(i32 %x, i32 %y)", 0}, 915 {false, "call float @llvm.sqrt.f32(float %fx)", 0}, 916 {false, "call float @llvm.powi.f32.i32(float %fx, i32 %x)", 0}, 917 {false, "call float @llvm.sin.f32(float %fx)", 0}, 918 {false, "call float @llvm.cos.f32(float %fx)", 0}, 919 {false, "call float @llvm.pow.f32(float %fx, float %fy)", 0}, 920 {false, "call float @llvm.exp.f32(float %fx)", 0}, 921 {false, "call float @llvm.exp2.f32(float %fx)", 0}, 922 {false, "call float @llvm.log.f32(float %fx)", 0}, 923 {false, "call float @llvm.log10.f32(float %fx)", 0}, 924 {false, "call float @llvm.log2.f32(float %fx)", 0}, 925 {false, "call float @llvm.fma.f32(float %fx, float %fx, float %fy)", 0}, 926 {false, "call float @llvm.fabs.f32(float %fx)", 0}, 927 {false, "call float @llvm.minnum.f32(float %fx, float %fy)", 0}, 928 {false, "call float @llvm.maxnum.f32(float %fx, float %fy)", 0}, 929 {false, "call float @llvm.minimum.f32(float %fx, float %fy)", 0}, 930 {false, "call float @llvm.maximum.f32(float %fx, float %fy)", 0}, 931 {false, "call float @llvm.copysign.f32(float %fx, float %fy)", 0}, 932 {false, "call float @llvm.floor.f32(float %fx)", 0}, 933 {false, "call float @llvm.ceil.f32(float %fx)", 0}, 934 {false, "call float @llvm.trunc.f32(float %fx)", 0}, 935 {false, "call float @llvm.rint.f32(float %fx)", 0}, 936 {false, "call float @llvm.nearbyint.f32(float %fx)", 0}, 937 {false, "call float @llvm.round.f32(float %fx)", 0}, 938 {false, "call float @llvm.roundeven.f32(float %fx)", 0}, 939 {false, "call i32 @llvm.lround.f32(float %fx)", 0}, 940 {false, "call i64 @llvm.llround.f32(float %fx)", 0}, 941 {false, "call i32 @llvm.lrint.f32(float %fx)", 0}, 942 {false, "call i64 @llvm.llrint.f32(float %fx)", 0}, 943 {false, "call float @llvm.fmuladd.f32(float %fx, float %fx, float %fy)", 944 0}}; 945 946 std::string AssemblyStr = AsmHead; 947 for (auto &Itm : Data) 948 AssemblyStr += std::get<1>(Itm) + "\n"; 949 AssemblyStr += AsmTail; 950 951 LLVMContext Context; 952 SMDiagnostic Error; 953 auto M = parseAssemblyString(AssemblyStr, Error, Context); 954 assert(M && "Bad assembly?"); 955 956 auto *F = M->getFunction("f"); 957 assert(F && "Bad assembly?"); 958 959 auto &BB = F->getEntryBlock(); 960 961 int Index = 0; 962 for (auto &I : BB) { 963 if (isa<ReturnInst>(&I)) 964 break; 965 bool ExpectedVal = std::get<0>(Data[Index]); 966 unsigned OpIdx = std::get<2>(Data[Index]); 967 EXPECT_EQ(propagatesPoison(I.getOperandUse(OpIdx)), ExpectedVal) 968 << "Incorrect answer at instruction " << Index << " = " << I; 969 Index++; 970 } 971 } 972 973 TEST_F(ValueTrackingTest, programUndefinedIfPoison) { 974 parseAssembly("declare i32 @any_num()" 975 "define void @test(i32 %mask) {\n" 976 " %A = call i32 @any_num()\n" 977 " %B = or i32 %A, %mask\n" 978 " udiv i32 1, %B" 979 " ret void\n" 980 "}\n"); 981 // If %A was poison, udiv raises UB regardless of %mask's value 982 EXPECT_EQ(programUndefinedIfPoison(A), true); 983 } 984 985 TEST_F(ValueTrackingTest, programUndefinedIfUndefOrPoison) { 986 parseAssembly("declare i32 @any_num()" 987 "define void @test(i32 %mask) {\n" 988 " %A = call i32 @any_num()\n" 989 " %B = or i32 %A, %mask\n" 990 " udiv i32 1, %B" 991 " ret void\n" 992 "}\n"); 993 // If %A was undef and %mask was 1, udiv does not raise UB 994 EXPECT_EQ(programUndefinedIfUndefOrPoison(A), false); 995 } 996 997 TEST_F(ValueTrackingTest, isGuaranteedNotToBePoison_exploitBranchCond) { 998 parseAssembly("declare i1 @any_bool()" 999 "define void @test(i1 %y) {\n" 1000 " %A = call i1 @any_bool()\n" 1001 " %cond = and i1 %A, %y\n" 1002 " br i1 %cond, label %BB1, label %BB2\n" 1003 "BB1:\n" 1004 " ret void\n" 1005 "BB2:\n" 1006 " ret void\n" 1007 "}\n"); 1008 DominatorTree DT(*F); 1009 for (auto &BB : *F) { 1010 if (&BB == &F->getEntryBlock()) 1011 continue; 1012 1013 EXPECT_EQ(isGuaranteedNotToBePoison(A, nullptr, BB.getTerminator(), &DT), 1014 true) 1015 << "isGuaranteedNotToBePoison does not hold at " << *BB.getTerminator(); 1016 } 1017 } 1018 1019 TEST_F(ValueTrackingTest, isGuaranteedNotToBePoison_phi) { 1020 parseAssembly("declare i32 @any_i32(i32)" 1021 "define void @test() {\n" 1022 "ENTRY:\n" 1023 " br label %LOOP\n" 1024 "LOOP:\n" 1025 " %A = phi i32 [0, %ENTRY], [%A.next, %NEXT]\n" 1026 " %A.next = call i32 @any_i32(i32 %A)\n" 1027 " %cond = icmp eq i32 %A.next, 0\n" 1028 " br i1 %cond, label %NEXT, label %EXIT\n" 1029 "NEXT:\n" 1030 " br label %LOOP\n" 1031 "EXIT:\n" 1032 " ret void\n" 1033 "}\n"); 1034 DominatorTree DT(*F); 1035 for (auto &BB : *F) { 1036 if (BB.getName() == "LOOP") { 1037 EXPECT_EQ(isGuaranteedNotToBePoison(A, nullptr, A, &DT), true) 1038 << "isGuaranteedNotToBePoison does not hold"; 1039 } 1040 } 1041 } 1042 1043 TEST_F(ValueTrackingTest, isGuaranteedNotToBeUndefOrPoison) { 1044 parseAssembly("declare void @f(i32 noundef)" 1045 "define void @test(i32 %x) {\n" 1046 " %A = bitcast i32 %x to i32\n" 1047 " call void @f(i32 noundef %x)\n" 1048 " ret void\n" 1049 "}\n"); 1050 EXPECT_EQ(isGuaranteedNotToBeUndefOrPoison(A), true); 1051 EXPECT_EQ(isGuaranteedNotToBeUndefOrPoison(UndefValue::get(IntegerType::get(Context, 8))), false); 1052 EXPECT_EQ(isGuaranteedNotToBeUndefOrPoison(PoisonValue::get(IntegerType::get(Context, 8))), false); 1053 EXPECT_EQ(isGuaranteedNotToBePoison(UndefValue::get(IntegerType::get(Context, 8))), true); 1054 EXPECT_EQ(isGuaranteedNotToBePoison(PoisonValue::get(IntegerType::get(Context, 8))), false); 1055 1056 Type *Int32Ty = Type::getInt32Ty(Context); 1057 Constant *CU = UndefValue::get(Int32Ty); 1058 Constant *CP = PoisonValue::get(Int32Ty); 1059 Constant *C1 = ConstantInt::get(Int32Ty, 1); 1060 Constant *C2 = ConstantInt::get(Int32Ty, 2); 1061 1062 { 1063 Constant *V1 = ConstantVector::get({C1, C2}); 1064 EXPECT_TRUE(isGuaranteedNotToBeUndefOrPoison(V1)); 1065 EXPECT_TRUE(isGuaranteedNotToBePoison(V1)); 1066 } 1067 1068 { 1069 Constant *V2 = ConstantVector::get({C1, CU}); 1070 EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(V2)); 1071 EXPECT_TRUE(isGuaranteedNotToBePoison(V2)); 1072 } 1073 1074 { 1075 Constant *V3 = ConstantVector::get({C1, CP}); 1076 EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(V3)); 1077 EXPECT_FALSE(isGuaranteedNotToBePoison(V3)); 1078 } 1079 } 1080 1081 TEST_F(ValueTrackingTest, isGuaranteedNotToBeUndefOrPoison_assume) { 1082 parseAssembly("declare i1 @f_i1()\n" 1083 "declare i32 @f_i32()\n" 1084 "declare void @llvm.assume(i1)\n" 1085 "define void @test() {\n" 1086 " %A = call i32 @f_i32()\n" 1087 " %cond = call i1 @f_i1()\n" 1088 " %CxtI = add i32 0, 0\n" 1089 " br i1 %cond, label %BB1, label %EXIT\n" 1090 "BB1:\n" 1091 " %CxtI2 = add i32 0, 0\n" 1092 " %cond2 = call i1 @f_i1()\n" 1093 " call void @llvm.assume(i1 true) [ \"noundef\"(i32 %A) ]\n" 1094 " br i1 %cond2, label %BB2, label %EXIT\n" 1095 "BB2:\n" 1096 " %CxtI3 = add i32 0, 0\n" 1097 " ret void\n" 1098 "EXIT:\n" 1099 " ret void\n" 1100 "}"); 1101 AssumptionCache AC(*F); 1102 DominatorTree DT(*F); 1103 EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(A, &AC, CxtI, &DT)); 1104 EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(A, &AC, CxtI2, &DT)); 1105 EXPECT_TRUE(isGuaranteedNotToBeUndefOrPoison(A, &AC, CxtI3, &DT)); 1106 } 1107 1108 TEST(ValueTracking, canCreatePoisonOrUndef) { 1109 std::string AsmHead = 1110 "@s = external dso_local global i32, align 1\n" 1111 "declare i32 @g(i32)\n" 1112 "declare {i32, i1} @llvm.sadd.with.overflow.i32(i32 %a, i32 %b)\n" 1113 "declare {i32, i1} @llvm.ssub.with.overflow.i32(i32 %a, i32 %b)\n" 1114 "declare {i32, i1} @llvm.smul.with.overflow.i32(i32 %a, i32 %b)\n" 1115 "declare {i32, i1} @llvm.uadd.with.overflow.i32(i32 %a, i32 %b)\n" 1116 "declare {i32, i1} @llvm.usub.with.overflow.i32(i32 %a, i32 %b)\n" 1117 "declare {i32, i1} @llvm.umul.with.overflow.i32(i32 %a, i32 %b)\n" 1118 "define void @f(i32 %x, i32 %y, float %fx, float %fy, i1 %cond, " 1119 "<4 x i32> %vx, <4 x i32> %vx2, <vscale x 4 x i32> %svx, ptr %p) {\n"; 1120 std::string AsmTail = " ret void\n}"; 1121 // (can create poison?, can create undef?, IR instruction) 1122 SmallVector<std::pair<std::pair<bool, bool>, std::string>, 32> Data = { 1123 {{false, false}, "add i32 %x, %y"}, 1124 {{true, false}, "add nsw nuw i32 %x, %y"}, 1125 {{true, false}, "shl i32 %x, %y"}, 1126 {{true, false}, "shl <4 x i32> %vx, %vx2"}, 1127 {{true, false}, "shl nsw i32 %x, %y"}, 1128 {{true, false}, "shl nsw <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"}, 1129 {{false, false}, "shl i32 %x, 31"}, 1130 {{true, false}, "shl i32 %x, 32"}, 1131 {{false, false}, "shl <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"}, 1132 {{true, false}, "shl <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 32>"}, 1133 {{true, false}, "ashr i32 %x, %y"}, 1134 {{true, false}, "ashr exact i32 %x, %y"}, 1135 {{false, false}, "ashr i32 %x, 31"}, 1136 {{true, false}, "ashr exact i32 %x, 31"}, 1137 {{false, false}, "ashr <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"}, 1138 {{true, false}, "ashr <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 32>"}, 1139 {{true, false}, "ashr exact <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"}, 1140 {{true, false}, "lshr i32 %x, %y"}, 1141 {{true, false}, "lshr exact i32 %x, 31"}, 1142 {{false, false}, "udiv i32 %x, %y"}, 1143 {{true, false}, "udiv exact i32 %x, %y"}, 1144 {{false, false}, "getelementptr i8, ptr %p, i32 %x"}, 1145 {{true, false}, "getelementptr inbounds i8, ptr %p, i32 %x"}, 1146 {{true, false}, "fneg nnan float %fx"}, 1147 {{false, false}, "fneg float %fx"}, 1148 {{false, false}, "fadd float %fx, %fy"}, 1149 {{true, false}, "fadd nnan float %fx, %fy"}, 1150 {{false, false}, "urem i32 %x, %y"}, 1151 {{true, false}, "fptoui float %fx to i32"}, 1152 {{true, false}, "fptosi float %fx to i32"}, 1153 {{false, false}, "bitcast float %fx to i32"}, 1154 {{false, false}, "select i1 %cond, i32 %x, i32 %y"}, 1155 {{true, false}, "select nnan i1 %cond, float %fx, float %fy"}, 1156 {{true, false}, "extractelement <4 x i32> %vx, i32 %x"}, 1157 {{false, false}, "extractelement <4 x i32> %vx, i32 3"}, 1158 {{true, false}, "extractelement <vscale x 4 x i32> %svx, i32 4"}, 1159 {{true, false}, "insertelement <4 x i32> %vx, i32 %x, i32 %y"}, 1160 {{false, false}, "insertelement <4 x i32> %vx, i32 %x, i32 3"}, 1161 {{true, false}, "insertelement <vscale x 4 x i32> %svx, i32 %x, i32 4"}, 1162 {{false, false}, "freeze i32 %x"}, 1163 {{false, false}, 1164 "shufflevector <4 x i32> %vx, <4 x i32> %vx2, " 1165 "<4 x i32> <i32 0, i32 1, i32 2, i32 3>"}, 1166 {{false, true}, 1167 "shufflevector <4 x i32> %vx, <4 x i32> %vx2, " 1168 "<4 x i32> <i32 0, i32 1, i32 2, i32 undef>"}, 1169 {{false, true}, 1170 "shufflevector <vscale x 4 x i32> %svx, " 1171 "<vscale x 4 x i32> %svx, <vscale x 4 x i32> undef"}, 1172 {{true, false}, "call i32 @g(i32 %x)"}, 1173 {{false, false}, "call noundef i32 @g(i32 %x)"}, 1174 {{true, false}, "fcmp nnan oeq float %fx, %fy"}, 1175 {{false, false}, "fcmp oeq float %fx, %fy"}, 1176 {{true, false}, "ashr i32 %x, ptrtoint (ptr @s to i32)"}, 1177 {{false, false}, 1178 "call {i32, i1} @llvm.sadd.with.overflow.i32(i32 %x, i32 %y)"}, 1179 {{false, false}, 1180 "call {i32, i1} @llvm.ssub.with.overflow.i32(i32 %x, i32 %y)"}, 1181 {{false, false}, 1182 "call {i32, i1} @llvm.smul.with.overflow.i32(i32 %x, i32 %y)"}, 1183 {{false, false}, 1184 "call {i32, i1} @llvm.uadd.with.overflow.i32(i32 %x, i32 %y)"}, 1185 {{false, false}, 1186 "call {i32, i1} @llvm.usub.with.overflow.i32(i32 %x, i32 %y)"}, 1187 {{false, false}, 1188 "call {i32, i1} @llvm.umul.with.overflow.i32(i32 %x, i32 %y)"}}; 1189 1190 std::string AssemblyStr = AsmHead; 1191 for (auto &Itm : Data) 1192 AssemblyStr += Itm.second + "\n"; 1193 AssemblyStr += AsmTail; 1194 1195 LLVMContext Context; 1196 SMDiagnostic Error; 1197 auto M = parseAssemblyString(AssemblyStr, Error, Context); 1198 assert(M && "Bad assembly?"); 1199 1200 auto *F = M->getFunction("f"); 1201 assert(F && "Bad assembly?"); 1202 1203 auto &BB = F->getEntryBlock(); 1204 1205 int Index = 0; 1206 for (auto &I : BB) { 1207 if (isa<ReturnInst>(&I)) 1208 break; 1209 bool Poison = Data[Index].first.first; 1210 bool Undef = Data[Index].first.second; 1211 EXPECT_EQ(canCreatePoison(cast<Operator>(&I)), Poison) 1212 << "Incorrect answer of canCreatePoison at instruction " << Index 1213 << " = " << I; 1214 EXPECT_EQ(canCreateUndefOrPoison(cast<Operator>(&I)), Undef || Poison) 1215 << "Incorrect answer of canCreateUndef at instruction " << Index 1216 << " = " << I; 1217 Index++; 1218 } 1219 } 1220 1221 TEST_F(ValueTrackingTest, computePtrAlignment) { 1222 parseAssembly("declare i1 @f_i1()\n" 1223 "declare ptr @f_i8p()\n" 1224 "declare void @llvm.assume(i1)\n" 1225 "define void @test() {\n" 1226 " %A = call ptr @f_i8p()\n" 1227 " %cond = call i1 @f_i1()\n" 1228 " %CxtI = add i32 0, 0\n" 1229 " br i1 %cond, label %BB1, label %EXIT\n" 1230 "BB1:\n" 1231 " %CxtI2 = add i32 0, 0\n" 1232 " %cond2 = call i1 @f_i1()\n" 1233 " call void @llvm.assume(i1 true) [ \"align\"(ptr %A, i64 16) ]\n" 1234 " br i1 %cond2, label %BB2, label %EXIT\n" 1235 "BB2:\n" 1236 " %CxtI3 = add i32 0, 0\n" 1237 " ret void\n" 1238 "EXIT:\n" 1239 " ret void\n" 1240 "}"); 1241 AssumptionCache AC(*F); 1242 DominatorTree DT(*F); 1243 const DataLayout &DL = M->getDataLayout(); 1244 EXPECT_EQ(getKnownAlignment(A, DL, CxtI, &AC, &DT), Align(1)); 1245 EXPECT_EQ(getKnownAlignment(A, DL, CxtI2, &AC, &DT), Align(1)); 1246 EXPECT_EQ(getKnownAlignment(A, DL, CxtI3, &AC, &DT), Align(16)); 1247 } 1248 1249 TEST_F(ComputeKnownBitsTest, ComputeKnownBits) { 1250 parseAssembly( 1251 "define i32 @test(i32 %a, i32 %b) {\n" 1252 " %ash = mul i32 %a, 8\n" 1253 " %aad = add i32 %ash, 7\n" 1254 " %aan = and i32 %aad, 4095\n" 1255 " %bsh = shl i32 %b, 4\n" 1256 " %bad = or i32 %bsh, 6\n" 1257 " %ban = and i32 %bad, 4095\n" 1258 " %A = mul i32 %aan, %ban\n" 1259 " ret i32 %A\n" 1260 "}\n"); 1261 expectKnownBits(/*zero*/ 4278190085u, /*one*/ 10u); 1262 } 1263 1264 TEST_F(ComputeKnownBitsTest, ComputeKnownMulBits) { 1265 parseAssembly( 1266 "define i32 @test(i32 %a, i32 %b) {\n" 1267 " %aa = shl i32 %a, 5\n" 1268 " %bb = shl i32 %b, 5\n" 1269 " %aaa = or i32 %aa, 24\n" 1270 " %bbb = or i32 %bb, 28\n" 1271 " %A = mul i32 %aaa, %bbb\n" 1272 " ret i32 %A\n" 1273 "}\n"); 1274 expectKnownBits(/*zero*/ 95u, /*one*/ 32u); 1275 } 1276 1277 TEST_F(ComputeKnownFPClassTest, SelectPos0) { 1278 parseAssembly( 1279 "define float @test(i1 %cond) {\n" 1280 " %A = select i1 %cond, float 0.0, float 0.0" 1281 " ret float %A\n" 1282 "}\n"); 1283 expectKnownFPClass(fcPosZero, false); 1284 } 1285 1286 TEST_F(ComputeKnownFPClassTest, SelectNeg0) { 1287 parseAssembly( 1288 "define float @test(i1 %cond) {\n" 1289 " %A = select i1 %cond, float -0.0, float -0.0" 1290 " ret float %A\n" 1291 "}\n"); 1292 expectKnownFPClass(fcNegZero, true); 1293 } 1294 1295 TEST_F(ComputeKnownFPClassTest, SelectPosOrNeg0) { 1296 parseAssembly( 1297 "define float @test(i1 %cond) {\n" 1298 " %A = select i1 %cond, float 0.0, float -0.0" 1299 " ret float %A\n" 1300 "}\n"); 1301 expectKnownFPClass(fcZero, std::nullopt); 1302 } 1303 1304 TEST_F(ComputeKnownFPClassTest, SelectPosInf) { 1305 parseAssembly( 1306 "define float @test(i1 %cond) {\n" 1307 " %A = select i1 %cond, float 0x7FF0000000000000, float 0x7FF0000000000000" 1308 " ret float %A\n" 1309 "}\n"); 1310 expectKnownFPClass(fcPosInf, false); 1311 } 1312 1313 TEST_F(ComputeKnownFPClassTest, SelectNegInf) { 1314 parseAssembly( 1315 "define float @test(i1 %cond) {\n" 1316 " %A = select i1 %cond, float 0xFFF0000000000000, float 0xFFF0000000000000" 1317 " ret float %A\n" 1318 "}\n"); 1319 expectKnownFPClass(fcNegInf, true); 1320 } 1321 1322 TEST_F(ComputeKnownFPClassTest, SelectPosOrNegInf) { 1323 parseAssembly( 1324 "define float @test(i1 %cond) {\n" 1325 " %A = select i1 %cond, float 0x7FF0000000000000, float 0xFFF0000000000000" 1326 " ret float %A\n" 1327 "}\n"); 1328 expectKnownFPClass(fcInf, std::nullopt); 1329 } 1330 1331 TEST_F(ComputeKnownFPClassTest, SelectNNaN) { 1332 parseAssembly( 1333 "define float @test(i1 %cond, float %arg0, float %arg1) {\n" 1334 " %A = select nnan i1 %cond, float %arg0, float %arg1" 1335 " ret float %A\n" 1336 "}\n"); 1337 expectKnownFPClass(~fcNan, std::nullopt); 1338 } 1339 1340 TEST_F(ComputeKnownFPClassTest, SelectNInf) { 1341 parseAssembly( 1342 "define float @test(i1 %cond, float %arg0, float %arg1) {\n" 1343 " %A = select ninf i1 %cond, float %arg0, float %arg1" 1344 " ret float %A\n" 1345 "}\n"); 1346 expectKnownFPClass(~fcInf, std::nullopt); 1347 } 1348 1349 TEST_F(ComputeKnownFPClassTest, SelectNNaNNInf) { 1350 parseAssembly( 1351 "define float @test(i1 %cond, float %arg0, float %arg1) {\n" 1352 " %A = select nnan ninf i1 %cond, float %arg0, float %arg1" 1353 " ret float %A\n" 1354 "}\n"); 1355 expectKnownFPClass(~(fcNan | fcInf), std::nullopt); 1356 } 1357 1358 TEST_F(ComputeKnownFPClassTest, SelectNoFPClassArgUnionAll) { 1359 parseAssembly( 1360 "define float @test(i1 %cond, float nofpclass(snan ninf nsub pzero pnorm) %arg0, float nofpclass(qnan nnorm nzero psub pinf) %arg1) {\n" 1361 " %A = select i1 %cond, float %arg0, float %arg1" 1362 " ret float %A\n" 1363 "}\n"); 1364 expectKnownFPClass(fcAllFlags, std::nullopt); 1365 } 1366 1367 TEST_F(ComputeKnownFPClassTest, SelectNoFPClassArgNoNan) { 1368 parseAssembly( 1369 "define float @test(i1 %cond, float nofpclass(nan) %arg0, float nofpclass(nan) %arg1) {\n" 1370 " %A = select i1 %cond, float %arg0, float %arg1" 1371 " ret float %A\n" 1372 "}\n"); 1373 expectKnownFPClass(~fcNan, std::nullopt); 1374 } 1375 1376 TEST_F(ComputeKnownFPClassTest, SelectNoFPClassArgNoPInf) { 1377 parseAssembly( 1378 "define float @test(i1 %cond, float nofpclass(inf) %arg0, float nofpclass(pinf) %arg1) {\n" 1379 " %A = select i1 %cond, float %arg0, float %arg1" 1380 " ret float %A\n" 1381 "}\n"); 1382 expectKnownFPClass(~fcPosInf, std::nullopt); 1383 } 1384 1385 TEST_F(ComputeKnownFPClassTest, SelectNoFPClassArgNoNInf) { 1386 parseAssembly( 1387 "define float @test(i1 %cond, float nofpclass(ninf) %arg0, float nofpclass(inf) %arg1) {\n" 1388 " %A = select i1 %cond, float %arg0, float %arg1" 1389 " ret float %A\n" 1390 "}\n"); 1391 expectKnownFPClass(~fcNegInf, std::nullopt); 1392 } 1393 1394 TEST_F(ComputeKnownFPClassTest, SelectNoFPClassCallSiteNoNan) { 1395 parseAssembly( 1396 "declare float @func()\n" 1397 "define float @test() {\n" 1398 " %A = call nofpclass(nan) float @func()\n" 1399 " ret float %A\n" 1400 "}\n"); 1401 expectKnownFPClass(~fcNan, std::nullopt); 1402 } 1403 1404 TEST_F(ComputeKnownFPClassTest, SelectNoFPClassCallSiteNoZeros) { 1405 parseAssembly( 1406 "declare float @func()\n" 1407 "define float @test() {\n" 1408 " %A = call nofpclass(zero) float @func()\n" 1409 " ret float %A\n" 1410 "}\n"); 1411 expectKnownFPClass(~fcZero, std::nullopt); 1412 } 1413 1414 TEST_F(ComputeKnownFPClassTest, SelectNoFPClassDeclarationNoNan) { 1415 parseAssembly( 1416 "declare nofpclass(nan) float @no_nans()\n" 1417 "define float @test() {\n" 1418 " %A = call float @no_nans()\n" 1419 " ret float %A\n" 1420 "}\n"); 1421 expectKnownFPClass(~fcNan, std::nullopt); 1422 } 1423 1424 // Check nofpclass + ninf works on a callsite 1425 TEST_F(ComputeKnownFPClassTest, SelectNoFPClassCallSiteNoZerosNInfFlags) { 1426 parseAssembly( 1427 "declare float @func()\n" 1428 "define float @test() {\n" 1429 " %A = call ninf nofpclass(zero) float @func()\n" 1430 " ret float %A\n" 1431 "}\n"); 1432 expectKnownFPClass(~(fcZero | fcInf), std::nullopt); 1433 } 1434 1435 TEST_F(ComputeKnownFPClassTest, FNegNInf) { 1436 parseAssembly( 1437 "define float @test(float %arg) {\n" 1438 " %A = fneg ninf float %arg" 1439 " ret float %A\n" 1440 "}\n"); 1441 expectKnownFPClass(~fcInf, std::nullopt); 1442 } 1443 1444 TEST_F(ComputeKnownFPClassTest, FabsUnknown) { 1445 parseAssembly( 1446 "declare float @llvm.fabs.f32(float)" 1447 "define float @test(float %arg) {\n" 1448 " %A = call float @llvm.fabs.f32(float %arg)" 1449 " ret float %A\n" 1450 "}\n"); 1451 expectKnownFPClass(fcPositive | fcNan, false); 1452 } 1453 1454 TEST_F(ComputeKnownFPClassTest, FNegFabsUnknown) { 1455 parseAssembly( 1456 "declare float @llvm.fabs.f32(float)" 1457 "define float @test(float %arg) {\n" 1458 " %fabs = call float @llvm.fabs.f32(float %arg)" 1459 " %A = fneg float %fabs" 1460 " ret float %A\n" 1461 "}\n"); 1462 expectKnownFPClass(fcNegative | fcNan, true); 1463 } 1464 1465 TEST_F(ComputeKnownFPClassTest, NegFabsNInf) { 1466 parseAssembly( 1467 "declare float @llvm.fabs.f32(float)" 1468 "define float @test(float %arg) {\n" 1469 " %fabs = call ninf float @llvm.fabs.f32(float %arg)" 1470 " %A = fneg float %fabs" 1471 " ret float %A\n" 1472 "}\n"); 1473 expectKnownFPClass((fcNegative & ~fcNegInf) | fcNan, true); 1474 } 1475 1476 TEST_F(ComputeKnownFPClassTest, FNegFabsNNaN) { 1477 parseAssembly( 1478 "declare float @llvm.fabs.f32(float)" 1479 "define float @test(float %arg) {\n" 1480 " %fabs = call nnan float @llvm.fabs.f32(float %arg)" 1481 " %A = fneg float %fabs" 1482 " ret float %A\n" 1483 "}\n"); 1484 expectKnownFPClass(fcNegative, true); 1485 } 1486 1487 TEST_F(ComputeKnownFPClassTest, CopySignNNanSrc0) { 1488 parseAssembly( 1489 "declare float @llvm.fabs.f32(float)\n" 1490 "declare float @llvm.copysign.f32(float, float)\n" 1491 "define float @test(float %arg0, float %arg1) {\n" 1492 " %fabs = call nnan float @llvm.fabs.f32(float %arg0)" 1493 " %A = call float @llvm.copysign.f32(float %fabs, float %arg1)" 1494 " ret float %A\n" 1495 "}\n"); 1496 expectKnownFPClass(fcPositive, std::nullopt); 1497 } 1498 1499 TEST_F(ComputeKnownFPClassTest, CopySignNInfSrc0_NegSign) { 1500 parseAssembly( 1501 "declare float @llvm.sqrt.f32(float)\n" 1502 "declare float @llvm.copysign.f32(float, float)\n" 1503 "define float @test(float %arg0, float %arg1) {\n" 1504 " %ninf = call ninf float @llvm.sqrt.f32(float %arg0)" 1505 " %A = call float @llvm.copysign.f32(float %ninf, float -1.0)" 1506 " ret float %A\n" 1507 "}\n"); 1508 expectKnownFPClass(fcNegFinite, true); 1509 } 1510 1511 TEST_F(ComputeKnownFPClassTest, CopySignNInfSrc0_PosSign) { 1512 parseAssembly( 1513 "declare float @llvm.sqrt.f32(float)\n" 1514 "declare float @llvm.copysign.f32(float, float)\n" 1515 "define float @test(float %arg0, float %arg1) {\n" 1516 " %ninf = call ninf float @llvm.sqrt.f32(float %arg0)" 1517 " %A = call float @llvm.copysign.f32(float %ninf, float 1.0)" 1518 " ret float %A\n" 1519 "}\n"); 1520 expectKnownFPClass(fcPosFinite, false); 1521 } 1522 1523 TEST_F(ComputeKnownFPClassTest, UIToFP) { 1524 parseAssembly( 1525 "define float @test(i32 %arg0, i16 %arg1) {\n" 1526 " %A = uitofp i32 %arg0 to float" 1527 " %A2 = uitofp i16 %arg1 to half" 1528 " ret float %A\n" 1529 "}\n"); 1530 expectKnownFPClass(fcPosFinite & ~fcSubnormal, false, A); 1531 expectKnownFPClass(fcPositive & ~fcSubnormal, false, A2); 1532 } 1533 1534 TEST_F(ComputeKnownFPClassTest, SIToFP) { 1535 parseAssembly( 1536 "define float @test(i32 %arg0, i16 %arg1, i17 %arg2) {\n" 1537 " %A = sitofp i32 %arg0 to float" 1538 " %A2 = sitofp i16 %arg1 to half" 1539 " %A3 = sitofp i17 %arg2 to half" 1540 " ret float %A\n" 1541 "}\n"); 1542 expectKnownFPClass(fcFinite & ~fcNegZero & ~fcSubnormal, std::nullopt, A); 1543 expectKnownFPClass(fcFinite & ~fcNegZero & ~fcSubnormal, std::nullopt, A2); 1544 expectKnownFPClass(~(fcNan | fcNegZero | fcSubnormal), std::nullopt, A3); 1545 } 1546 1547 TEST_F(ComputeKnownFPClassTest, FAdd) { 1548 parseAssembly( 1549 "define float @test(float nofpclass(nan inf) %nnan.ninf, float nofpclass(nan) %nnan, float nofpclass(qnan) %no.qnan, float %unknown) {\n" 1550 " %A = fadd float %nnan, %nnan.ninf" 1551 " %A2 = fadd float %nnan.ninf, %nnan" 1552 " %A3 = fadd float %nnan.ninf, %unknown" 1553 " %A4 = fadd float %nnan.ninf, %no.qnan" 1554 " %A5 = fadd float %nnan, %nnan" 1555 " ret float %A\n" 1556 "}\n"); 1557 expectKnownFPClass(fcFinite | fcInf, std::nullopt, A); 1558 expectKnownFPClass(fcFinite | fcInf, std::nullopt, A2); 1559 expectKnownFPClass(fcAllFlags, std::nullopt, A3); 1560 expectKnownFPClass(fcAllFlags, std::nullopt, A4); 1561 expectKnownFPClass(fcAllFlags, std::nullopt, A5); 1562 } 1563 1564 TEST_F(ComputeKnownFPClassTest, FSub) { 1565 parseAssembly( 1566 "define float @test(float nofpclass(nan inf) %nnan.ninf, float nofpclass(nan) %nnan, float nofpclass(qnan) %no.qnan, float %unknown) {\n" 1567 " %A = fsub float %nnan, %nnan.ninf" 1568 " %A2 = fsub float %nnan.ninf, %nnan" 1569 " %A3 = fsub float %nnan.ninf, %unknown" 1570 " %A4 = fsub float %nnan.ninf, %no.qnan" 1571 " %A5 = fsub float %nnan, %nnan" 1572 " ret float %A\n" 1573 "}\n"); 1574 expectKnownFPClass(fcFinite | fcInf, std::nullopt, A); 1575 expectKnownFPClass(fcFinite | fcInf, std::nullopt, A2); 1576 expectKnownFPClass(fcAllFlags, std::nullopt, A3); 1577 expectKnownFPClass(fcAllFlags, std::nullopt, A4); 1578 expectKnownFPClass(fcAllFlags, std::nullopt, A5); 1579 } 1580 1581 TEST_F(ComputeKnownFPClassTest, FMul) { 1582 parseAssembly( 1583 "define float @test(float nofpclass(nan inf) %nnan.ninf, float nofpclass(nan) %nnan, float nofpclass(qnan) %no.qnan, float %unknown) {\n" 1584 " %A = fmul float %nnan.ninf, %nnan.ninf" 1585 " %A2 = fmul float %nnan.ninf, %nnan" 1586 " %A3 = fmul float %nnan, %nnan.ninf" 1587 " %A4 = fmul float %nnan.ninf, %no.qnan" 1588 " %A5 = fmul float %nnan, %nnan" 1589 " ret float %A\n" 1590 "}\n"); 1591 expectKnownFPClass(fcFinite | fcInf, std::nullopt, A); 1592 expectKnownFPClass(fcAllFlags, std::nullopt, A2); 1593 expectKnownFPClass(fcAllFlags, std::nullopt, A3); 1594 expectKnownFPClass(fcAllFlags, std::nullopt, A4); 1595 expectKnownFPClass(fcAllFlags, std::nullopt, A5); 1596 } 1597 1598 TEST_F(ComputeKnownFPClassTest, FMulNoZero) { 1599 parseAssembly( 1600 "define float @test(float nofpclass(zero) %no.zero, float nofpclass(zero nan) %no.zero.nan, 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" 1601 " %A = fmul float %no.zero.nan, %no.zero.nan" 1602 " %A2 = fmul float %no.zero, %no.zero" 1603 " %A3 = fmul float %no.poszero.nan, %no.zero.nan" 1604 " %A4 = fmul float %no.nan, %no.zero" 1605 " %A5 = fmul float %no.zero, %no.inf" 1606 " %A6 = fmul float %no.zero.nan, %no.nan" 1607 " %A7 = fmul float %no.nan, %no.zero.nan" 1608 " ret float %A\n" 1609 "}\n"); 1610 expectKnownFPClass(fcFinite | fcInf, std::nullopt, A); 1611 expectKnownFPClass(fcAllFlags, std::nullopt, A2); 1612 expectKnownFPClass(fcAllFlags, std::nullopt, A3); 1613 expectKnownFPClass(fcAllFlags, std::nullopt, A4); 1614 expectKnownFPClass(fcAllFlags, std::nullopt, A5); 1615 expectKnownFPClass(fcAllFlags, std::nullopt, A6); 1616 expectKnownFPClass(fcAllFlags, std::nullopt, A7); 1617 } 1618 1619 TEST_F(ComputeKnownFPClassTest, CannotBeOrderedLessThanZero) { 1620 parseAssembly("define float @test(float %arg) {\n" 1621 " %A = fmul float %arg, %arg" 1622 " ret float %A\n" 1623 "}\n"); 1624 1625 Type *FPTy = Type::getDoubleTy(M->getContext()); 1626 const DataLayout &DL = M->getDataLayout(); 1627 1628 EXPECT_TRUE( 1629 computeKnownFPClass(ConstantFP::getZero(FPTy, /*Negative=*/false), DL) 1630 .cannotBeOrderedLessThanZero()); 1631 EXPECT_TRUE( 1632 computeKnownFPClass(ConstantFP::getZero(FPTy, /*Negative=*/true), DL) 1633 .cannotBeOrderedLessThanZero()); 1634 1635 EXPECT_TRUE(computeKnownFPClass(ConstantFP::getInfinity(FPTy, false), DL) 1636 .cannotBeOrderedLessThanZero()); 1637 EXPECT_FALSE(computeKnownFPClass(ConstantFP::getInfinity(FPTy, true), DL) 1638 .cannotBeOrderedLessThanZero()); 1639 1640 EXPECT_TRUE(computeKnownFPClass(ConstantFP::get(FPTy, 1.0), DL) 1641 .cannotBeOrderedLessThanZero()); 1642 EXPECT_FALSE(computeKnownFPClass(ConstantFP::get(FPTy, -1.0), DL) 1643 .cannotBeOrderedLessThanZero()); 1644 1645 EXPECT_TRUE( 1646 computeKnownFPClass( 1647 ConstantFP::get(FPTy, APFloat::getSmallest(FPTy->getFltSemantics(), 1648 /*Negative=*/false)), 1649 DL) 1650 .cannotBeOrderedLessThanZero()); 1651 EXPECT_FALSE( 1652 computeKnownFPClass( 1653 ConstantFP::get(FPTy, APFloat::getSmallest(FPTy->getFltSemantics(), 1654 /*Negative=*/true)), 1655 DL) 1656 .cannotBeOrderedLessThanZero()); 1657 1658 EXPECT_TRUE( 1659 computeKnownFPClass(ConstantFP::getQNaN(FPTy, /*Negative=*/false), DL) 1660 .cannotBeOrderedLessThanZero()); 1661 EXPECT_TRUE( 1662 computeKnownFPClass(ConstantFP::getQNaN(FPTy, /*Negative=*/true), DL) 1663 .cannotBeOrderedLessThanZero()); 1664 EXPECT_TRUE( 1665 computeKnownFPClass(ConstantFP::getSNaN(FPTy, /*Negative=*/false), DL) 1666 .cannotBeOrderedLessThanZero()); 1667 EXPECT_TRUE( 1668 computeKnownFPClass(ConstantFP::getSNaN(FPTy, /*Negative=*/true), DL) 1669 .cannotBeOrderedLessThanZero()); 1670 } 1671 1672 TEST_F(ValueTrackingTest, isNonZeroRecurrence) { 1673 parseAssembly(R"( 1674 define i1 @test(i8 %n, i8 %r) { 1675 entry: 1676 br label %loop 1677 loop: 1678 %p = phi i8 [ -1, %entry ], [ %next, %loop ] 1679 %next = add nsw i8 %p, -1 1680 %cmp1 = icmp eq i8 %p, %n 1681 br i1 %cmp1, label %exit, label %loop 1682 exit: 1683 %A = or i8 %p, %r 1684 %CxtI = icmp eq i8 %A, 0 1685 ret i1 %CxtI 1686 } 1687 )"); 1688 const DataLayout &DL = M->getDataLayout(); 1689 AssumptionCache AC(*F); 1690 EXPECT_TRUE(isKnownNonZero(A, DL, 0, &AC, CxtI)); 1691 } 1692 1693 TEST_F(ValueTrackingTest, KnownNonZeroFromDomCond) { 1694 parseAssembly(R"( 1695 declare ptr @f_i8() 1696 define void @test(i1 %c) { 1697 %A = call ptr @f_i8() 1698 %B = call ptr @f_i8() 1699 %c1 = icmp ne ptr %A, null 1700 %cond = and i1 %c1, %c 1701 br i1 %cond, label %T, label %Q 1702 T: 1703 %CxtI = add i32 0, 0 1704 ret void 1705 Q: 1706 %CxtI2 = add i32 0, 0 1707 ret void 1708 } 1709 )"); 1710 AssumptionCache AC(*F); 1711 DominatorTree DT(*F); 1712 const DataLayout &DL = M->getDataLayout(); 1713 EXPECT_EQ(isKnownNonZero(A, DL, 0, &AC, CxtI, &DT), true); 1714 EXPECT_EQ(isKnownNonZero(A, DL, 0, &AC, CxtI2, &DT), false); 1715 } 1716 1717 TEST_F(ValueTrackingTest, KnownNonZeroFromDomCond2) { 1718 parseAssembly(R"( 1719 declare ptr @f_i8() 1720 define void @test(i1 %c) { 1721 %A = call ptr @f_i8() 1722 %B = call ptr @f_i8() 1723 %c1 = icmp ne ptr %A, null 1724 %cond = select i1 %c, i1 %c1, i1 false 1725 br i1 %cond, label %T, label %Q 1726 T: 1727 %CxtI = add i32 0, 0 1728 ret void 1729 Q: 1730 %CxtI2 = add i32 0, 0 1731 ret void 1732 } 1733 )"); 1734 AssumptionCache AC(*F); 1735 DominatorTree DT(*F); 1736 const DataLayout &DL = M->getDataLayout(); 1737 EXPECT_EQ(isKnownNonZero(A, DL, 0, &AC, CxtI, &DT), true); 1738 EXPECT_EQ(isKnownNonZero(A, DL, 0, &AC, CxtI2, &DT), false); 1739 } 1740 1741 TEST_F(ValueTrackingTest, IsImpliedConditionAnd) { 1742 parseAssembly(R"( 1743 define void @test(i32 %x, i32 %y) { 1744 %c1 = icmp ult i32 %x, 10 1745 %c2 = icmp ult i32 %y, 15 1746 %A = and i1 %c1, %c2 1747 ; x < 10 /\ y < 15 1748 %A2 = icmp ult i32 %x, 20 1749 %A3 = icmp uge i32 %y, 20 1750 %A4 = icmp ult i32 %x, 5 1751 ret void 1752 } 1753 )"); 1754 const DataLayout &DL = M->getDataLayout(); 1755 EXPECT_EQ(isImpliedCondition(A, A2, DL), true); 1756 EXPECT_EQ(isImpliedCondition(A, A3, DL), false); 1757 EXPECT_EQ(isImpliedCondition(A, A4, DL), std::nullopt); 1758 } 1759 1760 TEST_F(ValueTrackingTest, IsImpliedConditionAnd2) { 1761 parseAssembly(R"( 1762 define void @test(i32 %x, i32 %y) { 1763 %c1 = icmp ult i32 %x, 10 1764 %c2 = icmp ult i32 %y, 15 1765 %A = select i1 %c1, i1 %c2, i1 false 1766 ; x < 10 /\ y < 15 1767 %A2 = icmp ult i32 %x, 20 1768 %A3 = icmp uge i32 %y, 20 1769 %A4 = icmp ult i32 %x, 5 1770 ret void 1771 } 1772 )"); 1773 const DataLayout &DL = M->getDataLayout(); 1774 EXPECT_EQ(isImpliedCondition(A, A2, DL), true); 1775 EXPECT_EQ(isImpliedCondition(A, A3, DL), false); 1776 EXPECT_EQ(isImpliedCondition(A, A4, DL), std::nullopt); 1777 } 1778 1779 TEST_F(ValueTrackingTest, IsImpliedConditionAndVec) { 1780 parseAssembly(R"( 1781 define void @test(<2 x i8> %x, <2 x i8> %y) { 1782 %A = icmp ult <2 x i8> %x, %y 1783 %A2 = icmp ule <2 x i8> %x, %y 1784 ret void 1785 } 1786 )"); 1787 const DataLayout &DL = M->getDataLayout(); 1788 EXPECT_EQ(isImpliedCondition(A, A2, DL), true); 1789 } 1790 1791 TEST_F(ValueTrackingTest, IsImpliedConditionOr) { 1792 parseAssembly(R"( 1793 define void @test(i32 %x, i32 %y) { 1794 %c1 = icmp ult i32 %x, 10 1795 %c2 = icmp ult i32 %y, 15 1796 %A = or i1 %c1, %c2 ; negated 1797 ; x >= 10 /\ y >= 15 1798 %A2 = icmp ult i32 %x, 5 1799 %A3 = icmp uge i32 %y, 10 1800 %A4 = icmp ult i32 %x, 15 1801 ret void 1802 } 1803 )"); 1804 const DataLayout &DL = M->getDataLayout(); 1805 EXPECT_EQ(isImpliedCondition(A, A2, DL, false), false); 1806 EXPECT_EQ(isImpliedCondition(A, A3, DL, false), true); 1807 EXPECT_EQ(isImpliedCondition(A, A4, DL, false), std::nullopt); 1808 } 1809 1810 TEST_F(ValueTrackingTest, IsImpliedConditionOr2) { 1811 parseAssembly(R"( 1812 define void @test(i32 %x, i32 %y) { 1813 %c1 = icmp ult i32 %x, 10 1814 %c2 = icmp ult i32 %y, 15 1815 %A = select i1 %c1, i1 true, i1 %c2 ; negated 1816 ; x >= 10 /\ y >= 15 1817 %A2 = icmp ult i32 %x, 5 1818 %A3 = icmp uge i32 %y, 10 1819 %A4 = icmp ult i32 %x, 15 1820 ret void 1821 } 1822 )"); 1823 const DataLayout &DL = M->getDataLayout(); 1824 EXPECT_EQ(isImpliedCondition(A, A2, DL, false), false); 1825 EXPECT_EQ(isImpliedCondition(A, A3, DL, false), true); 1826 EXPECT_EQ(isImpliedCondition(A, A4, DL, false), std::nullopt); 1827 } 1828 1829 TEST_F(ComputeKnownBitsTest, KnownNonZeroShift) { 1830 // %q is known nonzero without known bits. 1831 // Because %q is nonzero, %A[0] is known to be zero. 1832 parseAssembly( 1833 "define i8 @test(i8 %p, ptr %pq) {\n" 1834 " %q = load i8, ptr %pq, !range !0\n" 1835 " %A = shl i8 %p, %q\n" 1836 " ret i8 %A\n" 1837 "}\n" 1838 "!0 = !{ i8 1, i8 5 }\n"); 1839 expectKnownBits(/*zero*/ 1u, /*one*/ 0u); 1840 } 1841 1842 TEST_F(ComputeKnownBitsTest, ComputeKnownFshl) { 1843 // fshl(....1111....0000, 00..1111........, 6) 1844 // = 11....000000..11 1845 parseAssembly( 1846 "define i16 @test(i16 %a, i16 %b) {\n" 1847 " %aa = shl i16 %a, 4\n" 1848 " %bb = lshr i16 %b, 2\n" 1849 " %aaa = or i16 %aa, 3840\n" 1850 " %bbb = or i16 %bb, 3840\n" 1851 " %A = call i16 @llvm.fshl.i16(i16 %aaa, i16 %bbb, i16 6)\n" 1852 " ret i16 %A\n" 1853 "}\n" 1854 "declare i16 @llvm.fshl.i16(i16, i16, i16)\n"); 1855 expectKnownBits(/*zero*/ 1008u, /*one*/ 49155u); 1856 } 1857 1858 TEST_F(ComputeKnownBitsTest, ComputeKnownFshr) { 1859 // fshr(....1111....0000, 00..1111........, 26) 1860 // = 11....000000..11 1861 parseAssembly( 1862 "define i16 @test(i16 %a, i16 %b) {\n" 1863 " %aa = shl i16 %a, 4\n" 1864 " %bb = lshr i16 %b, 2\n" 1865 " %aaa = or i16 %aa, 3840\n" 1866 " %bbb = or i16 %bb, 3840\n" 1867 " %A = call i16 @llvm.fshr.i16(i16 %aaa, i16 %bbb, i16 26)\n" 1868 " ret i16 %A\n" 1869 "}\n" 1870 "declare i16 @llvm.fshr.i16(i16, i16, i16)\n"); 1871 expectKnownBits(/*zero*/ 1008u, /*one*/ 49155u); 1872 } 1873 1874 TEST_F(ComputeKnownBitsTest, ComputeKnownFshlZero) { 1875 // fshl(....1111....0000, 00..1111........, 0) 1876 // = ....1111....0000 1877 parseAssembly( 1878 "define i16 @test(i16 %a, i16 %b) {\n" 1879 " %aa = shl i16 %a, 4\n" 1880 " %bb = lshr i16 %b, 2\n" 1881 " %aaa = or i16 %aa, 3840\n" 1882 " %bbb = or i16 %bb, 3840\n" 1883 " %A = call i16 @llvm.fshl.i16(i16 %aaa, i16 %bbb, i16 0)\n" 1884 " ret i16 %A\n" 1885 "}\n" 1886 "declare i16 @llvm.fshl.i16(i16, i16, i16)\n"); 1887 expectKnownBits(/*zero*/ 15u, /*one*/ 3840u); 1888 } 1889 1890 TEST_F(ComputeKnownBitsTest, ComputeKnownUAddSatLeadingOnes) { 1891 // uadd.sat(1111...1, ........) 1892 // = 1111.... 1893 parseAssembly( 1894 "define i8 @test(i8 %a, i8 %b) {\n" 1895 " %aa = or i8 %a, 241\n" 1896 " %A = call i8 @llvm.uadd.sat.i8(i8 %aa, i8 %b)\n" 1897 " ret i8 %A\n" 1898 "}\n" 1899 "declare i8 @llvm.uadd.sat.i8(i8, i8)\n"); 1900 expectKnownBits(/*zero*/ 0u, /*one*/ 240u); 1901 } 1902 1903 TEST_F(ComputeKnownBitsTest, ComputeKnownUAddSatOnesPreserved) { 1904 // uadd.sat(00...011, .1...110) 1905 // = .......1 1906 parseAssembly( 1907 "define i8 @test(i8 %a, i8 %b) {\n" 1908 " %aa = or i8 %a, 3\n" 1909 " %aaa = and i8 %aa, 59\n" 1910 " %bb = or i8 %b, 70\n" 1911 " %bbb = and i8 %bb, 254\n" 1912 " %A = call i8 @llvm.uadd.sat.i8(i8 %aaa, i8 %bbb)\n" 1913 " ret i8 %A\n" 1914 "}\n" 1915 "declare i8 @llvm.uadd.sat.i8(i8, i8)\n"); 1916 expectKnownBits(/*zero*/ 0u, /*one*/ 1u); 1917 } 1918 1919 TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatLHSLeadingZeros) { 1920 // usub.sat(0000...0, ........) 1921 // = 0000.... 1922 parseAssembly( 1923 "define i8 @test(i8 %a, i8 %b) {\n" 1924 " %aa = and i8 %a, 14\n" 1925 " %A = call i8 @llvm.usub.sat.i8(i8 %aa, i8 %b)\n" 1926 " ret i8 %A\n" 1927 "}\n" 1928 "declare i8 @llvm.usub.sat.i8(i8, i8)\n"); 1929 expectKnownBits(/*zero*/ 240u, /*one*/ 0u); 1930 } 1931 1932 TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatRHSLeadingOnes) { 1933 // usub.sat(........, 1111...1) 1934 // = 0000.... 1935 parseAssembly( 1936 "define i8 @test(i8 %a, i8 %b) {\n" 1937 " %bb = or i8 %a, 241\n" 1938 " %A = call i8 @llvm.usub.sat.i8(i8 %a, i8 %bb)\n" 1939 " ret i8 %A\n" 1940 "}\n" 1941 "declare i8 @llvm.usub.sat.i8(i8, i8)\n"); 1942 expectKnownBits(/*zero*/ 240u, /*one*/ 0u); 1943 } 1944 1945 TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatZerosPreserved) { 1946 // usub.sat(11...011, .1...110) 1947 // = ......0. 1948 parseAssembly( 1949 "define i8 @test(i8 %a, i8 %b) {\n" 1950 " %aa = or i8 %a, 195\n" 1951 " %aaa = and i8 %aa, 251\n" 1952 " %bb = or i8 %b, 70\n" 1953 " %bbb = and i8 %bb, 254\n" 1954 " %A = call i8 @llvm.usub.sat.i8(i8 %aaa, i8 %bbb)\n" 1955 " ret i8 %A\n" 1956 "}\n" 1957 "declare i8 @llvm.usub.sat.i8(i8, i8)\n"); 1958 expectKnownBits(/*zero*/ 2u, /*one*/ 0u); 1959 } 1960 1961 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsPtrToIntTrunc) { 1962 // ptrtoint truncates the pointer type. 1963 parseAssembly( 1964 "define void @test(ptr %p) {\n" 1965 " %A = load ptr, ptr %p\n" 1966 " %i = ptrtoint ptr %A to i32\n" 1967 " %m = and i32 %i, 31\n" 1968 " %c = icmp eq i32 %m, 0\n" 1969 " call void @llvm.assume(i1 %c)\n" 1970 " ret void\n" 1971 "}\n" 1972 "declare void @llvm.assume(i1)\n"); 1973 AssumptionCache AC(*F); 1974 KnownBits Known = computeKnownBits( 1975 A, M->getDataLayout(), /* Depth */ 0, &AC, F->front().getTerminator()); 1976 EXPECT_EQ(Known.Zero.getZExtValue(), 31u); 1977 EXPECT_EQ(Known.One.getZExtValue(), 0u); 1978 } 1979 1980 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsPtrToIntZext) { 1981 // ptrtoint zero extends the pointer type. 1982 parseAssembly( 1983 "define void @test(ptr %p) {\n" 1984 " %A = load ptr, ptr %p\n" 1985 " %i = ptrtoint ptr %A to i128\n" 1986 " %m = and i128 %i, 31\n" 1987 " %c = icmp eq i128 %m, 0\n" 1988 " call void @llvm.assume(i1 %c)\n" 1989 " ret void\n" 1990 "}\n" 1991 "declare void @llvm.assume(i1)\n"); 1992 AssumptionCache AC(*F); 1993 KnownBits Known = computeKnownBits( 1994 A, M->getDataLayout(), /* Depth */ 0, &AC, F->front().getTerminator()); 1995 EXPECT_EQ(Known.Zero.getZExtValue(), 31u); 1996 EXPECT_EQ(Known.One.getZExtValue(), 0u); 1997 } 1998 1999 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsFreeze) { 2000 parseAssembly("define void @test() {\n" 2001 " %m = call i32 @any_num()\n" 2002 " %A = freeze i32 %m\n" 2003 " %n = and i32 %m, 31\n" 2004 " %c = icmp eq i32 %n, 0\n" 2005 " call void @llvm.assume(i1 %c)\n" 2006 " ret void\n" 2007 "}\n" 2008 "declare void @llvm.assume(i1)\n" 2009 "declare i32 @any_num()\n"); 2010 AssumptionCache AC(*F); 2011 KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 2012 F->front().getTerminator()); 2013 EXPECT_EQ(Known.Zero.getZExtValue(), 31u); 2014 EXPECT_EQ(Known.One.getZExtValue(), 0u); 2015 } 2016 2017 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsAddWithRange) { 2018 parseAssembly("define void @test(ptr %p) {\n" 2019 " %A = load i64, ptr %p, !range !{i64 64, i64 65536}\n" 2020 " %APlus512 = add i64 %A, 512\n" 2021 " %c = icmp ugt i64 %APlus512, 523\n" 2022 " call void @llvm.assume(i1 %c)\n" 2023 " ret void\n" 2024 "}\n" 2025 "declare void @llvm.assume(i1)\n"); 2026 AssumptionCache AC(*F); 2027 KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 2028 F->front().getTerminator()); 2029 EXPECT_EQ(Known.Zero.getZExtValue(), ~(65536llu - 1)); 2030 EXPECT_EQ(Known.One.getZExtValue(), 0u); 2031 Instruction &APlus512 = findInstructionByName(F, "APlus512"); 2032 Known = computeKnownBits(&APlus512, M->getDataLayout(), /* Depth */ 0, &AC, 2033 F->front().getTerminator()); 2034 // We know of one less zero because 512 may have produced a 1 that 2035 // got carried all the way to the first trailing zero. 2036 EXPECT_EQ(Known.Zero.getZExtValue(), (~(65536llu - 1)) << 1); 2037 EXPECT_EQ(Known.One.getZExtValue(), 0u); 2038 // The known range is not precise given computeKnownBits works 2039 // with the masks of zeros and ones, not the ranges. 2040 EXPECT_EQ(Known.getMinValue(), 0u); 2041 EXPECT_EQ(Known.getMaxValue(), 131071); 2042 } 2043 2044 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsUnknownVScale) { 2045 Module M("", Context); 2046 IRBuilder<> Builder(Context); 2047 Function *TheFn = 2048 Intrinsic::getDeclaration(&M, Intrinsic::vscale, {Builder.getInt32Ty()}); 2049 CallInst *CI = Builder.CreateCall(TheFn, {}, {}, ""); 2050 2051 KnownBits Known = computeKnownBits(CI, M.getDataLayout(), /* Depth */ 0); 2052 // There is no parent function so we cannot look up the vscale_range 2053 // attribute to determine the number of bits. 2054 EXPECT_EQ(Known.One.getZExtValue(), 0u); 2055 EXPECT_EQ(Known.Zero.getZExtValue(), 0u); 2056 2057 BasicBlock *BB = BasicBlock::Create(Context); 2058 CI->insertInto(BB, BB->end()); 2059 Known = computeKnownBits(CI, M.getDataLayout(), /* Depth */ 0); 2060 // There is no parent function so we cannot look up the vscale_range 2061 // attribute to determine the number of bits. 2062 EXPECT_EQ(Known.One.getZExtValue(), 0u); 2063 EXPECT_EQ(Known.Zero.getZExtValue(), 0u); 2064 2065 CI->removeFromParent(); 2066 delete CI; 2067 delete BB; 2068 } 2069 2070 // 512 + [32, 64) doesn't produce overlapping bits. 2071 // Make sure we get all the individual bits properly. 2072 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsAddWithRangeNoOverlap) { 2073 parseAssembly("define void @test(ptr %p) {\n" 2074 " %A = load i64, ptr %p, !range !{i64 32, i64 64}\n" 2075 " %APlus512 = add i64 %A, 512\n" 2076 " %c = icmp ugt i64 %APlus512, 523\n" 2077 " call void @llvm.assume(i1 %c)\n" 2078 " ret void\n" 2079 "}\n" 2080 "declare void @llvm.assume(i1)\n"); 2081 AssumptionCache AC(*F); 2082 KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 2083 F->front().getTerminator()); 2084 EXPECT_EQ(Known.Zero.getZExtValue(), ~(64llu - 1)); 2085 EXPECT_EQ(Known.One.getZExtValue(), 32u); 2086 Instruction &APlus512 = findInstructionByName(F, "APlus512"); 2087 Known = computeKnownBits(&APlus512, M->getDataLayout(), /* Depth */ 0, &AC, 2088 F->front().getTerminator()); 2089 EXPECT_EQ(Known.Zero.getZExtValue(), ~512llu & ~(64llu - 1)); 2090 EXPECT_EQ(Known.One.getZExtValue(), 512u | 32u); 2091 // The known range is not precise given computeKnownBits works 2092 // with the masks of zeros and ones, not the ranges. 2093 EXPECT_EQ(Known.getMinValue(), 544); 2094 EXPECT_EQ(Known.getMaxValue(), 575); 2095 } 2096 2097 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsGEPWithRange) { 2098 parseAssembly( 2099 "define void @test(ptr %p) {\n" 2100 " %A = load i64, ptr %p, !range !{i64 64, i64 65536}\n" 2101 " %APtr = inttoptr i64 %A to float*" 2102 " %APtrPlus512 = getelementptr float, float* %APtr, i32 128\n" 2103 " %c = icmp ugt float* %APtrPlus512, inttoptr (i32 523 to float*)\n" 2104 " call void @llvm.assume(i1 %c)\n" 2105 " ret void\n" 2106 "}\n" 2107 "declare void @llvm.assume(i1)\n"); 2108 AssumptionCache AC(*F); 2109 KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 2110 F->front().getTerminator()); 2111 EXPECT_EQ(Known.Zero.getZExtValue(), ~(65536llu - 1)); 2112 EXPECT_EQ(Known.One.getZExtValue(), 0u); 2113 Instruction &APtrPlus512 = findInstructionByName(F, "APtrPlus512"); 2114 Known = computeKnownBits(&APtrPlus512, M->getDataLayout(), /* Depth */ 0, &AC, 2115 F->front().getTerminator()); 2116 // We know of one less zero because 512 may have produced a 1 that 2117 // got carried all the way to the first trailing zero. 2118 EXPECT_EQ(Known.Zero.getZExtValue(), ~(65536llu - 1) << 1); 2119 EXPECT_EQ(Known.One.getZExtValue(), 0u); 2120 // The known range is not precise given computeKnownBits works 2121 // with the masks of zeros and ones, not the ranges. 2122 EXPECT_EQ(Known.getMinValue(), 0u); 2123 EXPECT_EQ(Known.getMaxValue(), 131071); 2124 } 2125 2126 // 4*128 + [32, 64) doesn't produce overlapping bits. 2127 // Make sure we get all the individual bits properly. 2128 // This test is useful to check that we account for the scaling factor 2129 // in the gep. Indeed, gep float, [32,64), 128 is not 128 + [32,64). 2130 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsGEPWithRangeNoOverlap) { 2131 parseAssembly( 2132 "define void @test(ptr %p) {\n" 2133 " %A = load i64, ptr %p, !range !{i64 32, i64 64}\n" 2134 " %APtr = inttoptr i64 %A to float*" 2135 " %APtrPlus512 = getelementptr float, float* %APtr, i32 128\n" 2136 " %c = icmp ugt float* %APtrPlus512, inttoptr (i32 523 to float*)\n" 2137 " call void @llvm.assume(i1 %c)\n" 2138 " ret void\n" 2139 "}\n" 2140 "declare void @llvm.assume(i1)\n"); 2141 AssumptionCache AC(*F); 2142 KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 2143 F->front().getTerminator()); 2144 EXPECT_EQ(Known.Zero.getZExtValue(), ~(64llu - 1)); 2145 EXPECT_EQ(Known.One.getZExtValue(), 32u); 2146 Instruction &APtrPlus512 = findInstructionByName(F, "APtrPlus512"); 2147 Known = computeKnownBits(&APtrPlus512, M->getDataLayout(), /* Depth */ 0, &AC, 2148 F->front().getTerminator()); 2149 EXPECT_EQ(Known.Zero.getZExtValue(), ~512llu & ~(64llu - 1)); 2150 EXPECT_EQ(Known.One.getZExtValue(), 512u | 32u); 2151 // The known range is not precise given computeKnownBits works 2152 // with the masks of zeros and ones, not the ranges. 2153 EXPECT_EQ(Known.getMinValue(), 544); 2154 EXPECT_EQ(Known.getMaxValue(), 575); 2155 } 2156 2157 TEST_F(ValueTrackingTest, HaveNoCommonBitsSet) { 2158 { 2159 // Check for an inverted mask: (X & ~M) op (Y & M). 2160 auto M = parseModule(R"( 2161 define i32 @test(i32 %X, i32 %Y, i32 %M) { 2162 %1 = xor i32 %M, -1 2163 %LHS = and i32 %1, %X 2164 %RHS = and i32 %Y, %M 2165 %Ret = add i32 %LHS, %RHS 2166 ret i32 %Ret 2167 })"); 2168 2169 auto *F = M->getFunction("test"); 2170 auto *LHS = findInstructionByNameOrNull(F, "LHS"); 2171 auto *RHS = findInstructionByNameOrNull(F, "RHS"); 2172 2173 const DataLayout &DL = M->getDataLayout(); 2174 EXPECT_TRUE(haveNoCommonBitsSet(LHS, RHS, DL)); 2175 EXPECT_TRUE(haveNoCommonBitsSet(RHS, LHS, DL)); 2176 } 2177 { 2178 // Check for (A & B) and ~(A | B) 2179 auto M = parseModule(R"( 2180 define void @test(i32 %A, i32 %B) { 2181 %LHS = and i32 %A, %B 2182 %or = or i32 %A, %B 2183 %RHS = xor i32 %or, -1 2184 2185 %LHS2 = and i32 %B, %A 2186 %or2 = or i32 %A, %B 2187 %RHS2 = xor i32 %or2, -1 2188 2189 ret void 2190 })"); 2191 2192 auto *F = M->getFunction("test"); 2193 const DataLayout &DL = M->getDataLayout(); 2194 2195 auto *LHS = findInstructionByNameOrNull(F, "LHS"); 2196 auto *RHS = findInstructionByNameOrNull(F, "RHS"); 2197 EXPECT_TRUE(haveNoCommonBitsSet(LHS, RHS, DL)); 2198 EXPECT_TRUE(haveNoCommonBitsSet(RHS, LHS, DL)); 2199 2200 auto *LHS2 = findInstructionByNameOrNull(F, "LHS2"); 2201 auto *RHS2 = findInstructionByNameOrNull(F, "RHS2"); 2202 EXPECT_TRUE(haveNoCommonBitsSet(LHS2, RHS2, DL)); 2203 EXPECT_TRUE(haveNoCommonBitsSet(RHS2, LHS2, DL)); 2204 } 2205 { 2206 // Check for (A & B) and ~(A | B) in vector version 2207 auto M = parseModule(R"( 2208 define void @test(<2 x i32> %A, <2 x i32> %B) { 2209 %LHS = and <2 x i32> %A, %B 2210 %or = or <2 x i32> %A, %B 2211 %RHS = xor <2 x i32> %or, <i32 -1, i32 -1> 2212 2213 %LHS2 = and <2 x i32> %B, %A 2214 %or2 = or <2 x i32> %A, %B 2215 %RHS2 = xor <2 x i32> %or2, <i32 -1, i32 -1> 2216 2217 ret void 2218 })"); 2219 2220 auto *F = M->getFunction("test"); 2221 const DataLayout &DL = M->getDataLayout(); 2222 2223 auto *LHS = findInstructionByNameOrNull(F, "LHS"); 2224 auto *RHS = findInstructionByNameOrNull(F, "RHS"); 2225 EXPECT_TRUE(haveNoCommonBitsSet(LHS, RHS, DL)); 2226 EXPECT_TRUE(haveNoCommonBitsSet(RHS, LHS, DL)); 2227 2228 auto *LHS2 = findInstructionByNameOrNull(F, "LHS2"); 2229 auto *RHS2 = findInstructionByNameOrNull(F, "RHS2"); 2230 EXPECT_TRUE(haveNoCommonBitsSet(LHS2, RHS2, DL)); 2231 EXPECT_TRUE(haveNoCommonBitsSet(RHS2, LHS2, DL)); 2232 } 2233 } 2234 2235 class IsBytewiseValueTest : public ValueTrackingTest, 2236 public ::testing::WithParamInterface< 2237 std::pair<const char *, const char *>> { 2238 protected: 2239 }; 2240 2241 const std::pair<const char *, const char *> IsBytewiseValueTests[] = { 2242 { 2243 "i8 0", 2244 "i48* null", 2245 }, 2246 { 2247 "i8 undef", 2248 "i48* undef", 2249 }, 2250 { 2251 "i8 0", 2252 "i8 zeroinitializer", 2253 }, 2254 { 2255 "i8 0", 2256 "i8 0", 2257 }, 2258 { 2259 "i8 -86", 2260 "i8 -86", 2261 }, 2262 { 2263 "i8 -1", 2264 "i8 -1", 2265 }, 2266 { 2267 "i8 undef", 2268 "i16 undef", 2269 }, 2270 { 2271 "i8 0", 2272 "i16 0", 2273 }, 2274 { 2275 "", 2276 "i16 7", 2277 }, 2278 { 2279 "i8 -86", 2280 "i16 -21846", 2281 }, 2282 { 2283 "i8 -1", 2284 "i16 -1", 2285 }, 2286 { 2287 "i8 0", 2288 "i48 0", 2289 }, 2290 { 2291 "i8 -1", 2292 "i48 -1", 2293 }, 2294 { 2295 "i8 0", 2296 "i49 0", 2297 }, 2298 { 2299 "", 2300 "i49 -1", 2301 }, 2302 { 2303 "i8 0", 2304 "half 0xH0000", 2305 }, 2306 { 2307 "i8 -85", 2308 "half 0xHABAB", 2309 }, 2310 { 2311 "i8 0", 2312 "float 0.0", 2313 }, 2314 { 2315 "i8 -1", 2316 "float 0xFFFFFFFFE0000000", 2317 }, 2318 { 2319 "i8 0", 2320 "double 0.0", 2321 }, 2322 { 2323 "i8 -15", 2324 "double 0xF1F1F1F1F1F1F1F1", 2325 }, 2326 { 2327 "i8 undef", 2328 "i16* undef", 2329 }, 2330 { 2331 "i8 0", 2332 "i16* inttoptr (i64 0 to i16*)", 2333 }, 2334 { 2335 "i8 -1", 2336 "i16* inttoptr (i64 -1 to i16*)", 2337 }, 2338 { 2339 "i8 -86", 2340 "i16* inttoptr (i64 -6148914691236517206 to i16*)", 2341 }, 2342 { 2343 "", 2344 "i16* inttoptr (i48 -1 to i16*)", 2345 }, 2346 { 2347 "i8 -1", 2348 "i16* inttoptr (i96 -1 to i16*)", 2349 }, 2350 { 2351 "i8 undef", 2352 "[0 x i8] zeroinitializer", 2353 }, 2354 { 2355 "i8 undef", 2356 "[0 x i8] undef", 2357 }, 2358 { 2359 "i8 undef", 2360 "[5 x [0 x i8]] zeroinitializer", 2361 }, 2362 { 2363 "i8 undef", 2364 "[5 x [0 x i8]] undef", 2365 }, 2366 { 2367 "i8 0", 2368 "[6 x i8] zeroinitializer", 2369 }, 2370 { 2371 "i8 undef", 2372 "[6 x i8] undef", 2373 }, 2374 { 2375 "i8 1", 2376 "[5 x i8] [i8 1, i8 1, i8 1, i8 1, i8 1]", 2377 }, 2378 { 2379 "", 2380 "[5 x i64] [i64 1, i64 1, i64 1, i64 1, i64 1]", 2381 }, 2382 { 2383 "i8 -1", 2384 "[5 x i64] [i64 -1, i64 -1, i64 -1, i64 -1, i64 -1]", 2385 }, 2386 { 2387 "", 2388 "[4 x i8] [i8 1, i8 2, i8 1, i8 1]", 2389 }, 2390 { 2391 "i8 1", 2392 "[4 x i8] [i8 1, i8 undef, i8 1, i8 1]", 2393 }, 2394 { 2395 "i8 0", 2396 "<6 x i8> zeroinitializer", 2397 }, 2398 { 2399 "i8 undef", 2400 "<6 x i8> undef", 2401 }, 2402 { 2403 "i8 1", 2404 "<5 x i8> <i8 1, i8 1, i8 1, i8 1, i8 1>", 2405 }, 2406 { 2407 "", 2408 "<5 x i64> <i64 1, i64 1, i64 1, i64 1, i64 1>", 2409 }, 2410 { 2411 "i8 -1", 2412 "<5 x i64> <i64 -1, i64 -1, i64 -1, i64 -1, i64 -1>", 2413 }, 2414 { 2415 "", 2416 "<4 x i8> <i8 1, i8 1, i8 2, i8 1>", 2417 }, 2418 { 2419 "i8 5", 2420 "<2 x i8> < i8 5, i8 undef >", 2421 }, 2422 { 2423 "i8 0", 2424 "[2 x [2 x i16]] zeroinitializer", 2425 }, 2426 { 2427 "i8 undef", 2428 "[2 x [2 x i16]] undef", 2429 }, 2430 { 2431 "i8 -86", 2432 "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], " 2433 "[2 x i16] [i16 -21846, i16 -21846]]", 2434 }, 2435 { 2436 "", 2437 "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], " 2438 "[2 x i16] [i16 -21836, i16 -21846]]", 2439 }, 2440 { 2441 "i8 undef", 2442 "{ } zeroinitializer", 2443 }, 2444 { 2445 "i8 undef", 2446 "{ } undef", 2447 }, 2448 { 2449 "i8 undef", 2450 "{ {}, {} } zeroinitializer", 2451 }, 2452 { 2453 "i8 undef", 2454 "{ {}, {} } undef", 2455 }, 2456 { 2457 "i8 0", 2458 "{i8, i64, i16*} zeroinitializer", 2459 }, 2460 { 2461 "i8 undef", 2462 "{i8, i64, i16*} undef", 2463 }, 2464 { 2465 "i8 -86", 2466 "{i8, i64, i16*} {i8 -86, i64 -6148914691236517206, i16* undef}", 2467 }, 2468 { 2469 "", 2470 "{i8, i64, i16*} {i8 86, i64 -6148914691236517206, i16* undef}", 2471 }, 2472 }; 2473 2474 INSTANTIATE_TEST_SUITE_P(IsBytewiseValueParamTests, IsBytewiseValueTest, 2475 ::testing::ValuesIn(IsBytewiseValueTests)); 2476 2477 TEST_P(IsBytewiseValueTest, IsBytewiseValue) { 2478 auto M = parseModule(std::string("@test = global ") + GetParam().second); 2479 GlobalVariable *GV = dyn_cast<GlobalVariable>(M->getNamedValue("test")); 2480 Value *Actual = isBytewiseValue(GV->getInitializer(), M->getDataLayout()); 2481 std::string Buff; 2482 raw_string_ostream S(Buff); 2483 if (Actual) 2484 S << *Actual; 2485 EXPECT_EQ(GetParam().first, S.str()); 2486 } 2487 2488 TEST_F(ValueTrackingTest, ComputeConstantRange) { 2489 { 2490 // Assumptions: 2491 // * stride >= 5 2492 // * stride < 10 2493 // 2494 // stride = [5, 10) 2495 auto M = parseModule(R"( 2496 declare void @llvm.assume(i1) 2497 2498 define i32 @test(i32 %stride) { 2499 %gt = icmp uge i32 %stride, 5 2500 call void @llvm.assume(i1 %gt) 2501 %lt = icmp ult i32 %stride, 10 2502 call void @llvm.assume(i1 %lt) 2503 %stride.plus.one = add nsw nuw i32 %stride, 1 2504 ret i32 %stride.plus.one 2505 })"); 2506 Function *F = M->getFunction("test"); 2507 2508 AssumptionCache AC(*F); 2509 Value *Stride = &*F->arg_begin(); 2510 ConstantRange CR1 = computeConstantRange(Stride, false, true, &AC, nullptr); 2511 EXPECT_TRUE(CR1.isFullSet()); 2512 2513 Instruction *I = &findInstructionByName(F, "stride.plus.one"); 2514 ConstantRange CR2 = computeConstantRange(Stride, false, true, &AC, I); 2515 EXPECT_EQ(5, CR2.getLower()); 2516 EXPECT_EQ(10, CR2.getUpper()); 2517 } 2518 2519 { 2520 // Assumptions: 2521 // * stride >= 5 2522 // * stride < 200 2523 // * stride == 99 2524 // 2525 // stride = [99, 100) 2526 auto M = parseModule(R"( 2527 declare void @llvm.assume(i1) 2528 2529 define i32 @test(i32 %stride) { 2530 %gt = icmp uge i32 %stride, 5 2531 call void @llvm.assume(i1 %gt) 2532 %lt = icmp ult i32 %stride, 200 2533 call void @llvm.assume(i1 %lt) 2534 %eq = icmp eq i32 %stride, 99 2535 call void @llvm.assume(i1 %eq) 2536 %stride.plus.one = add nsw nuw i32 %stride, 1 2537 ret i32 %stride.plus.one 2538 })"); 2539 Function *F = M->getFunction("test"); 2540 2541 AssumptionCache AC(*F); 2542 Value *Stride = &*F->arg_begin(); 2543 Instruction *I = &findInstructionByName(F, "stride.plus.one"); 2544 ConstantRange CR = computeConstantRange(Stride, false, true, &AC, I); 2545 EXPECT_EQ(99, *CR.getSingleElement()); 2546 } 2547 2548 { 2549 // Assumptions: 2550 // * stride >= 5 2551 // * stride >= 50 2552 // * stride < 100 2553 // * stride < 200 2554 // 2555 // stride = [50, 100) 2556 auto M = parseModule(R"( 2557 declare void @llvm.assume(i1) 2558 2559 define i32 @test(i32 %stride, i1 %cond) { 2560 %gt = icmp uge i32 %stride, 5 2561 call void @llvm.assume(i1 %gt) 2562 %gt.2 = icmp uge i32 %stride, 50 2563 call void @llvm.assume(i1 %gt.2) 2564 br i1 %cond, label %bb1, label %bb2 2565 2566 bb1: 2567 %lt = icmp ult i32 %stride, 200 2568 call void @llvm.assume(i1 %lt) 2569 %lt.2 = icmp ult i32 %stride, 100 2570 call void @llvm.assume(i1 %lt.2) 2571 %stride.plus.one = add nsw nuw i32 %stride, 1 2572 ret i32 %stride.plus.one 2573 2574 bb2: 2575 ret i32 0 2576 })"); 2577 Function *F = M->getFunction("test"); 2578 2579 AssumptionCache AC(*F); 2580 Value *Stride = &*F->arg_begin(); 2581 Instruction *GT2 = &findInstructionByName(F, "gt.2"); 2582 ConstantRange CR = computeConstantRange(Stride, false, true, &AC, GT2); 2583 EXPECT_EQ(5, CR.getLower()); 2584 EXPECT_EQ(0, CR.getUpper()); 2585 2586 Instruction *I = &findInstructionByName(F, "stride.plus.one"); 2587 ConstantRange CR2 = computeConstantRange(Stride, false, true, &AC, I); 2588 EXPECT_EQ(50, CR2.getLower()); 2589 EXPECT_EQ(100, CR2.getUpper()); 2590 } 2591 2592 { 2593 // Assumptions: 2594 // * stride > 5 2595 // * stride < 5 2596 // 2597 // stride = empty range, as the assumptions contradict each other. 2598 auto M = parseModule(R"( 2599 declare void @llvm.assume(i1) 2600 2601 define i32 @test(i32 %stride, i1 %cond) { 2602 %gt = icmp ugt i32 %stride, 5 2603 call void @llvm.assume(i1 %gt) 2604 %lt = icmp ult i32 %stride, 5 2605 call void @llvm.assume(i1 %lt) 2606 %stride.plus.one = add nsw nuw i32 %stride, 1 2607 ret i32 %stride.plus.one 2608 })"); 2609 Function *F = M->getFunction("test"); 2610 2611 AssumptionCache AC(*F); 2612 Value *Stride = &*F->arg_begin(); 2613 2614 Instruction *I = &findInstructionByName(F, "stride.plus.one"); 2615 ConstantRange CR = computeConstantRange(Stride, false, true, &AC, I); 2616 EXPECT_TRUE(CR.isEmptySet()); 2617 } 2618 2619 { 2620 // Assumptions: 2621 // * x.1 >= 5 2622 // * x.2 < x.1 2623 // 2624 // stride = [0, -1) 2625 auto M = parseModule(R"( 2626 declare void @llvm.assume(i1) 2627 2628 define i32 @test(i32 %x.1, i32 %x.2) { 2629 %gt = icmp uge i32 %x.1, 5 2630 call void @llvm.assume(i1 %gt) 2631 %lt = icmp ult i32 %x.2, %x.1 2632 call void @llvm.assume(i1 %lt) 2633 %stride.plus.one = add nsw nuw i32 %x.1, 1 2634 ret i32 %stride.plus.one 2635 })"); 2636 Function *F = M->getFunction("test"); 2637 2638 AssumptionCache AC(*F); 2639 Value *X1 = &*(F->arg_begin()); 2640 Value *X2 = &*std::next(F->arg_begin()); 2641 2642 Instruction *I = &findInstructionByName(F, "stride.plus.one"); 2643 ConstantRange CR1 = computeConstantRange(X1, false, true, &AC, I); 2644 ConstantRange CR2 = computeConstantRange(X2, false, true, &AC, I); 2645 2646 EXPECT_EQ(5, CR1.getLower()); 2647 EXPECT_EQ(0, CR1.getUpper()); 2648 2649 EXPECT_EQ(0, CR2.getLower()); 2650 EXPECT_EQ(0xffffffff, CR2.getUpper()); 2651 2652 // Check the depth cutoff results in a conservative result (full set) by 2653 // passing Depth == MaxDepth == 6. 2654 ConstantRange CR3 = computeConstantRange(X2, false, true, &AC, I, nullptr, 6); 2655 EXPECT_TRUE(CR3.isFullSet()); 2656 } 2657 { 2658 // Assumptions: 2659 // * x.2 <= x.1 2660 auto M = parseModule(R"( 2661 declare void @llvm.assume(i1) 2662 2663 define i32 @test(i32 %x.1, i32 %x.2) { 2664 %lt = icmp ule i32 %x.2, %x.1 2665 call void @llvm.assume(i1 %lt) 2666 %stride.plus.one = add nsw nuw i32 %x.1, 1 2667 ret i32 %stride.plus.one 2668 })"); 2669 Function *F = M->getFunction("test"); 2670 2671 AssumptionCache AC(*F); 2672 Value *X2 = &*std::next(F->arg_begin()); 2673 2674 Instruction *I = &findInstructionByName(F, "stride.plus.one"); 2675 ConstantRange CR1 = computeConstantRange(X2, false, true, &AC, I); 2676 // If we don't know the value of x.2, we don't know the value of x.1. 2677 EXPECT_TRUE(CR1.isFullSet()); 2678 } 2679 } 2680 2681 struct FindAllocaForValueTestParams { 2682 const char *IR; 2683 bool AnyOffsetResult; 2684 bool ZeroOffsetResult; 2685 }; 2686 2687 class FindAllocaForValueTest 2688 : public ValueTrackingTest, 2689 public ::testing::WithParamInterface<FindAllocaForValueTestParams> { 2690 protected: 2691 }; 2692 2693 const FindAllocaForValueTestParams FindAllocaForValueTests[] = { 2694 {R"( 2695 define void @test() { 2696 %a = alloca i64 2697 %r = bitcast ptr %a to ptr 2698 ret void 2699 })", 2700 true, true}, 2701 2702 {R"( 2703 define void @test() { 2704 %a = alloca i32 2705 %r = getelementptr i32, ptr %a, i32 1 2706 ret void 2707 })", 2708 true, false}, 2709 2710 {R"( 2711 define void @test() { 2712 %a = alloca i32 2713 %r = getelementptr i32, ptr %a, i32 0 2714 ret void 2715 })", 2716 true, true}, 2717 2718 {R"( 2719 define void @test(i1 %cond) { 2720 entry: 2721 %a = alloca i32 2722 br label %bb1 2723 2724 bb1: 2725 %r = phi ptr [ %a, %entry ], [ %r, %bb1 ] 2726 br i1 %cond, label %bb1, label %exit 2727 2728 exit: 2729 ret void 2730 })", 2731 true, true}, 2732 2733 {R"( 2734 define void @test(i1 %cond) { 2735 %a = alloca i32 2736 %r = select i1 %cond, ptr %a, ptr %a 2737 ret void 2738 })", 2739 true, true}, 2740 2741 {R"( 2742 define void @test(i1 %cond) { 2743 %a = alloca i32 2744 %b = alloca i32 2745 %r = select i1 %cond, ptr %a, ptr %b 2746 ret void 2747 })", 2748 false, false}, 2749 2750 {R"( 2751 define void @test(i1 %cond) { 2752 entry: 2753 %a = alloca i64 2754 %a32 = bitcast ptr %a to ptr 2755 br label %bb1 2756 2757 bb1: 2758 %x = phi ptr [ %a32, %entry ], [ %x, %bb1 ] 2759 %r = getelementptr i32, ptr %x, i32 1 2760 br i1 %cond, label %bb1, label %exit 2761 2762 exit: 2763 ret void 2764 })", 2765 true, false}, 2766 2767 {R"( 2768 define void @test(i1 %cond) { 2769 entry: 2770 %a = alloca i64 2771 %a32 = bitcast ptr %a to ptr 2772 br label %bb1 2773 2774 bb1: 2775 %x = phi ptr [ %a32, %entry ], [ %r, %bb1 ] 2776 %r = getelementptr i32, ptr %x, i32 1 2777 br i1 %cond, label %bb1, label %exit 2778 2779 exit: 2780 ret void 2781 })", 2782 true, false}, 2783 2784 {R"( 2785 define void @test(i1 %cond, ptr %a) { 2786 entry: 2787 %r = bitcast ptr %a to ptr 2788 ret void 2789 })", 2790 false, false}, 2791 2792 {R"( 2793 define void @test(i1 %cond) { 2794 entry: 2795 %a = alloca i32 2796 %b = alloca i32 2797 br label %bb1 2798 2799 bb1: 2800 %r = phi ptr [ %a, %entry ], [ %b, %bb1 ] 2801 br i1 %cond, label %bb1, label %exit 2802 2803 exit: 2804 ret void 2805 })", 2806 false, false}, 2807 {R"( 2808 declare ptr @retptr(ptr returned) 2809 define void @test(i1 %cond) { 2810 %a = alloca i32 2811 %r = call ptr @retptr(ptr %a) 2812 ret void 2813 })", 2814 true, true}, 2815 {R"( 2816 declare ptr @fun(ptr) 2817 define void @test(i1 %cond) { 2818 %a = alloca i32 2819 %r = call ptr @fun(ptr %a) 2820 ret void 2821 })", 2822 false, false}, 2823 }; 2824 2825 TEST_P(FindAllocaForValueTest, findAllocaForValue) { 2826 auto M = parseModule(GetParam().IR); 2827 Function *F = M->getFunction("test"); 2828 Instruction *I = &findInstructionByName(F, "r"); 2829 const AllocaInst *AI = findAllocaForValue(I); 2830 EXPECT_EQ(!!AI, GetParam().AnyOffsetResult); 2831 } 2832 2833 TEST_P(FindAllocaForValueTest, findAllocaForValueZeroOffset) { 2834 auto M = parseModule(GetParam().IR); 2835 Function *F = M->getFunction("test"); 2836 Instruction *I = &findInstructionByName(F, "r"); 2837 const AllocaInst *AI = findAllocaForValue(I, true); 2838 EXPECT_EQ(!!AI, GetParam().ZeroOffsetResult); 2839 } 2840 2841 INSTANTIATE_TEST_SUITE_P(FindAllocaForValueTest, FindAllocaForValueTest, 2842 ::testing::ValuesIn(FindAllocaForValueTests)); 2843