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/InstIterator.h" 16 #include "llvm/IR/Instructions.h" 17 #include "llvm/IR/LLVMContext.h" 18 #include "llvm/IR/Module.h" 19 #include "llvm/Support/ErrorHandling.h" 20 #include "llvm/Support/KnownBits.h" 21 #include "llvm/Support/SourceMgr.h" 22 #include "llvm/Transforms/Utils/Local.h" 23 #include "gtest/gtest.h" 24 25 using namespace llvm; 26 27 namespace { 28 29 static Instruction *findInstructionByNameOrNull(Function *F, StringRef Name) { 30 for (Instruction &I : instructions(F)) 31 if (I.getName() == Name) 32 return &I; 33 34 return nullptr; 35 } 36 37 static Instruction &findInstructionByName(Function *F, StringRef Name) { 38 auto *I = findInstructionByNameOrNull(F, Name); 39 if (I) 40 return *I; 41 42 llvm_unreachable("Expected value not found"); 43 } 44 45 class ValueTrackingTest : public testing::Test { 46 protected: 47 std::unique_ptr<Module> parseModule(StringRef Assembly) { 48 SMDiagnostic Error; 49 std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context); 50 51 std::string errMsg; 52 raw_string_ostream os(errMsg); 53 Error.print("", os); 54 EXPECT_TRUE(M) << os.str(); 55 56 return M; 57 } 58 59 void parseAssembly(StringRef Assembly) { 60 M = parseModule(Assembly); 61 ASSERT_TRUE(M); 62 63 F = M->getFunction("test"); 64 ASSERT_TRUE(F) << "Test must have a function @test"; 65 if (!F) 66 return; 67 68 A = findInstructionByNameOrNull(F, "A"); 69 ASSERT_TRUE(A) << "@test must have an instruction %A"; 70 A2 = findInstructionByNameOrNull(F, "A2"); 71 A3 = findInstructionByNameOrNull(F, "A3"); 72 A4 = findInstructionByNameOrNull(F, "A4"); 73 74 CxtI = findInstructionByNameOrNull(F, "CxtI"); 75 CxtI2 = findInstructionByNameOrNull(F, "CxtI2"); 76 CxtI3 = findInstructionByNameOrNull(F, "CxtI3"); 77 } 78 79 LLVMContext Context; 80 std::unique_ptr<Module> M; 81 Function *F = nullptr; 82 Instruction *A = nullptr; 83 // Instructions (optional) 84 Instruction *A2 = nullptr, *A3 = nullptr, *A4 = nullptr; 85 86 // Context instructions (optional) 87 Instruction *CxtI = nullptr, *CxtI2 = nullptr, *CxtI3 = nullptr; 88 }; 89 90 class MatchSelectPatternTest : public ValueTrackingTest { 91 protected: 92 void expectPattern(const SelectPatternResult &P) { 93 Value *LHS, *RHS; 94 Instruction::CastOps CastOp; 95 SelectPatternResult R = matchSelectPattern(A, LHS, RHS, &CastOp); 96 EXPECT_EQ(P.Flavor, R.Flavor); 97 EXPECT_EQ(P.NaNBehavior, R.NaNBehavior); 98 EXPECT_EQ(P.Ordered, R.Ordered); 99 } 100 }; 101 102 class ComputeKnownBitsTest : public ValueTrackingTest { 103 protected: 104 void expectKnownBits(uint64_t Zero, uint64_t One) { 105 auto Known = computeKnownBits(A, M->getDataLayout()); 106 ASSERT_FALSE(Known.hasConflict()); 107 EXPECT_EQ(Known.One.getZExtValue(), One); 108 EXPECT_EQ(Known.Zero.getZExtValue(), Zero); 109 } 110 }; 111 112 } 113 114 TEST_F(MatchSelectPatternTest, SimpleFMin) { 115 parseAssembly( 116 "define float @test(float %a) {\n" 117 " %1 = fcmp ult float %a, 5.0\n" 118 " %A = select i1 %1, float %a, float 5.0\n" 119 " ret float %A\n" 120 "}\n"); 121 expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false}); 122 } 123 124 TEST_F(MatchSelectPatternTest, SimpleFMax) { 125 parseAssembly( 126 "define float @test(float %a) {\n" 127 " %1 = fcmp ogt float %a, 5.0\n" 128 " %A = select i1 %1, float %a, float 5.0\n" 129 " ret float %A\n" 130 "}\n"); 131 expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true}); 132 } 133 134 TEST_F(MatchSelectPatternTest, SwappedFMax) { 135 parseAssembly( 136 "define float @test(float %a) {\n" 137 " %1 = fcmp olt float 5.0, %a\n" 138 " %A = select i1 %1, float %a, float 5.0\n" 139 " ret float %A\n" 140 "}\n"); 141 expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, false}); 142 } 143 144 TEST_F(MatchSelectPatternTest, SwappedFMax2) { 145 parseAssembly( 146 "define float @test(float %a) {\n" 147 " %1 = fcmp olt float %a, 5.0\n" 148 " %A = select i1 %1, float 5.0, float %a\n" 149 " ret float %A\n" 150 "}\n"); 151 expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, false}); 152 } 153 154 TEST_F(MatchSelectPatternTest, SwappedFMax3) { 155 parseAssembly( 156 "define float @test(float %a) {\n" 157 " %1 = fcmp ult float %a, 5.0\n" 158 " %A = select i1 %1, float 5.0, float %a\n" 159 " ret float %A\n" 160 "}\n"); 161 expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true}); 162 } 163 164 TEST_F(MatchSelectPatternTest, FastFMin) { 165 parseAssembly( 166 "define float @test(float %a) {\n" 167 " %1 = fcmp nnan olt float %a, 5.0\n" 168 " %A = select i1 %1, float %a, float 5.0\n" 169 " ret float %A\n" 170 "}\n"); 171 expectPattern({SPF_FMINNUM, SPNB_RETURNS_ANY, false}); 172 } 173 174 TEST_F(MatchSelectPatternTest, FMinConstantZero) { 175 parseAssembly( 176 "define float @test(float %a) {\n" 177 " %1 = fcmp ole float %a, 0.0\n" 178 " %A = select i1 %1, float %a, float 0.0\n" 179 " ret float %A\n" 180 "}\n"); 181 // This shouldn't be matched, as %a could be -0.0. 182 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 183 } 184 185 TEST_F(MatchSelectPatternTest, FMinConstantZeroNsz) { 186 parseAssembly( 187 "define float @test(float %a) {\n" 188 " %1 = fcmp nsz ole float %a, 0.0\n" 189 " %A = select i1 %1, float %a, float 0.0\n" 190 " ret float %A\n" 191 "}\n"); 192 // But this should be, because we've ignored signed zeroes. 193 expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true}); 194 } 195 196 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero1) { 197 parseAssembly( 198 "define float @test(float %a) {\n" 199 " %1 = fcmp olt float -0.0, %a\n" 200 " %A = select i1 %1, float 0.0, float %a\n" 201 " ret float %A\n" 202 "}\n"); 203 // The sign of zero doesn't matter in fcmp. 204 expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, true}); 205 } 206 207 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero2) { 208 parseAssembly( 209 "define float @test(float %a) {\n" 210 " %1 = fcmp ogt float %a, -0.0\n" 211 " %A = select i1 %1, float 0.0, float %a\n" 212 " ret float %A\n" 213 "}\n"); 214 // The sign of zero doesn't matter in fcmp. 215 expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false}); 216 } 217 218 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero3) { 219 parseAssembly( 220 "define float @test(float %a) {\n" 221 " %1 = fcmp olt float 0.0, %a\n" 222 " %A = select i1 %1, float -0.0, float %a\n" 223 " ret float %A\n" 224 "}\n"); 225 // The sign of zero doesn't matter in fcmp. 226 expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, true}); 227 } 228 229 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero4) { 230 parseAssembly( 231 "define float @test(float %a) {\n" 232 " %1 = fcmp ogt float %a, 0.0\n" 233 " %A = select i1 %1, float -0.0, float %a\n" 234 " ret float %A\n" 235 "}\n"); 236 // The sign of zero doesn't matter in fcmp. 237 expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false}); 238 } 239 240 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero5) { 241 parseAssembly( 242 "define float @test(float %a) {\n" 243 " %1 = fcmp ogt float -0.0, %a\n" 244 " %A = select i1 %1, float %a, float 0.0\n" 245 " ret float %A\n" 246 "}\n"); 247 // The sign of zero doesn't matter in fcmp. 248 expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, false}); 249 } 250 251 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero6) { 252 parseAssembly( 253 "define float @test(float %a) {\n" 254 " %1 = fcmp olt float %a, -0.0\n" 255 " %A = select i1 %1, float %a, float 0.0\n" 256 " ret float %A\n" 257 "}\n"); 258 // The sign of zero doesn't matter in fcmp. 259 expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true}); 260 } 261 262 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero7) { 263 parseAssembly( 264 "define float @test(float %a) {\n" 265 " %1 = fcmp ogt float 0.0, %a\n" 266 " %A = select i1 %1, float %a, float -0.0\n" 267 " ret float %A\n" 268 "}\n"); 269 // The sign of zero doesn't matter in fcmp. 270 expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, false}); 271 } 272 273 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero8) { 274 parseAssembly( 275 "define float @test(float %a) {\n" 276 " %1 = fcmp olt float %a, 0.0\n" 277 " %A = select i1 %1, float %a, float -0.0\n" 278 " ret float %A\n" 279 "}\n"); 280 // The sign of zero doesn't matter in fcmp. 281 expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true}); 282 } 283 284 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero1) { 285 parseAssembly( 286 "define float @test(float %a) {\n" 287 " %1 = fcmp ogt float -0.0, %a\n" 288 " %A = select i1 %1, float 0.0, float %a\n" 289 " ret float %A\n" 290 "}\n"); 291 // The sign of zero doesn't matter in fcmp. 292 expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, true}); 293 } 294 295 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero2) { 296 parseAssembly( 297 "define float @test(float %a) {\n" 298 " %1 = fcmp olt float %a, -0.0\n" 299 " %A = select i1 %1, float 0.0, float %a\n" 300 " ret float %A\n" 301 "}\n"); 302 // The sign of zero doesn't matter in fcmp. 303 expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, false}); 304 } 305 306 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero3) { 307 parseAssembly( 308 "define float @test(float %a) {\n" 309 " %1 = fcmp ogt float 0.0, %a\n" 310 " %A = select i1 %1, float -0.0, float %a\n" 311 " ret float %A\n" 312 "}\n"); 313 // The sign of zero doesn't matter in fcmp. 314 expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, true}); 315 } 316 317 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero4) { 318 parseAssembly( 319 "define float @test(float %a) {\n" 320 " %1 = fcmp olt float %a, 0.0\n" 321 " %A = select i1 %1, float -0.0, float %a\n" 322 " ret float %A\n" 323 "}\n"); 324 // The sign of zero doesn't matter in fcmp. 325 expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, false}); 326 } 327 328 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero5) { 329 parseAssembly( 330 "define float @test(float %a) {\n" 331 " %1 = fcmp olt float -0.0, %a\n" 332 " %A = select i1 %1, float %a, float 0.0\n" 333 " ret float %A\n" 334 "}\n"); 335 // The sign of zero doesn't matter in fcmp. 336 expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, false}); 337 } 338 339 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero6) { 340 parseAssembly( 341 "define float @test(float %a) {\n" 342 " %1 = fcmp ogt float %a, -0.0\n" 343 " %A = select i1 %1, float %a, float 0.0\n" 344 " ret float %A\n" 345 "}\n"); 346 // The sign of zero doesn't matter in fcmp. 347 expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true}); 348 } 349 350 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero7) { 351 parseAssembly( 352 "define float @test(float %a) {\n" 353 " %1 = fcmp olt float 0.0, %a\n" 354 " %A = select i1 %1, float %a, float -0.0\n" 355 " ret float %A\n" 356 "}\n"); 357 // The sign of zero doesn't matter in fcmp. 358 expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, false}); 359 } 360 361 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero8) { 362 parseAssembly( 363 "define float @test(float %a) {\n" 364 " %1 = fcmp ogt float %a, 0.0\n" 365 " %A = select i1 %1, float %a, float -0.0\n" 366 " ret float %A\n" 367 "}\n"); 368 // The sign of zero doesn't matter in fcmp. 369 expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true}); 370 } 371 372 TEST_F(MatchSelectPatternTest, FMinMismatchConstantZeroVecUndef) { 373 parseAssembly( 374 "define <2 x float> @test(<2 x float> %a) {\n" 375 " %1 = fcmp ogt <2 x float> %a, <float -0.0, float -0.0>\n" 376 " %A = select <2 x i1> %1, <2 x float> <float undef, float 0.0>, <2 x float> %a\n" 377 " ret <2 x float> %A\n" 378 "}\n"); 379 // An undef in a vector constant can not be back-propagated for this analysis. 380 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 381 } 382 383 TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZeroVecUndef) { 384 parseAssembly( 385 "define <2 x float> @test(<2 x float> %a) {\n" 386 " %1 = fcmp ogt <2 x float> %a, zeroinitializer\n" 387 " %A = select <2 x i1> %1, <2 x float> %a, <2 x float> <float -0.0, float undef>\n" 388 " ret <2 x float> %A\n" 389 "}\n"); 390 // An undef in a vector constant can not be back-propagated for this analysis. 391 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 392 } 393 394 TEST_F(MatchSelectPatternTest, VectorFMinimum) { 395 parseAssembly( 396 "define <4 x float> @test(<4 x float> %a) {\n" 397 " %1 = fcmp ule <4 x float> %a, \n" 398 " <float 5.0, float 5.0, float 5.0, float 5.0>\n" 399 " %A = select <4 x i1> %1, <4 x float> %a,\n" 400 " <4 x float> <float 5.0, float 5.0, float 5.0, float 5.0>\n" 401 " ret <4 x float> %A\n" 402 "}\n"); 403 // Check that pattern matching works on vectors where each lane has the same 404 // unordered pattern. 405 expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false}); 406 } 407 408 TEST_F(MatchSelectPatternTest, VectorFMinOtherOrdered) { 409 parseAssembly( 410 "define <4 x float> @test(<4 x float> %a) {\n" 411 " %1 = fcmp ole <4 x float> %a, \n" 412 " <float 5.0, float 5.0, float 5.0, float 5.0>\n" 413 " %A = select <4 x i1> %1, <4 x float> %a,\n" 414 " <4 x float> <float 5.0, float 5.0, float 5.0, float 5.0>\n" 415 " ret <4 x float> %A\n" 416 "}\n"); 417 // Check that pattern matching works on vectors where each lane has the same 418 // ordered pattern. 419 expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true}); 420 } 421 422 TEST_F(MatchSelectPatternTest, VectorNotFMinimum) { 423 parseAssembly( 424 "define <4 x float> @test(<4 x float> %a) {\n" 425 " %1 = fcmp ule <4 x float> %a, \n" 426 " <float 5.0, float 0x7ff8000000000000, float 5.0, float 5.0>\n" 427 " %A = select <4 x i1> %1, <4 x float> %a,\n" 428 " <4 x float> <float 5.0, float 0x7ff8000000000000, float 5.0, float " 429 "5.0>\n" 430 " ret <4 x float> %A\n" 431 "}\n"); 432 // The lane that contains a NaN (0x7ff80...) behaves like a 433 // non-NaN-propagating min and the other lines behave like a NaN-propagating 434 // min, so check that neither is returned. 435 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 436 } 437 438 TEST_F(MatchSelectPatternTest, VectorNotFMinZero) { 439 parseAssembly( 440 "define <4 x float> @test(<4 x float> %a) {\n" 441 " %1 = fcmp ule <4 x float> %a, \n" 442 " <float 5.0, float -0.0, float 5.0, float 5.0>\n" 443 " %A = select <4 x i1> %1, <4 x float> %a,\n" 444 " <4 x float> <float 5.0, float 0.0, float 5.0, float 5.0>\n" 445 " ret <4 x float> %A\n" 446 "}\n"); 447 // Always selects the second lane of %a if it is positive or negative zero, so 448 // this is stricter than a min. 449 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 450 } 451 452 TEST_F(MatchSelectPatternTest, DoubleCastU) { 453 parseAssembly( 454 "define i32 @test(i8 %a, i8 %b) {\n" 455 " %1 = icmp ult i8 %a, %b\n" 456 " %2 = zext i8 %a to i32\n" 457 " %3 = zext i8 %b to i32\n" 458 " %A = select i1 %1, i32 %2, i32 %3\n" 459 " ret i32 %A\n" 460 "}\n"); 461 // We should be able to look through the situation where we cast both operands 462 // to the select. 463 expectPattern({SPF_UMIN, SPNB_NA, false}); 464 } 465 466 TEST_F(MatchSelectPatternTest, DoubleCastS) { 467 parseAssembly( 468 "define i32 @test(i8 %a, i8 %b) {\n" 469 " %1 = icmp slt i8 %a, %b\n" 470 " %2 = sext i8 %a to i32\n" 471 " %3 = sext i8 %b to i32\n" 472 " %A = select i1 %1, i32 %2, i32 %3\n" 473 " ret i32 %A\n" 474 "}\n"); 475 // We should be able to look through the situation where we cast both operands 476 // to the select. 477 expectPattern({SPF_SMIN, SPNB_NA, false}); 478 } 479 480 TEST_F(MatchSelectPatternTest, DoubleCastBad) { 481 parseAssembly( 482 "define i32 @test(i8 %a, i8 %b) {\n" 483 " %1 = icmp ult i8 %a, %b\n" 484 " %2 = zext i8 %a to i32\n" 485 " %3 = sext i8 %b to i32\n" 486 " %A = select i1 %1, i32 %2, i32 %3\n" 487 " ret i32 %A\n" 488 "}\n"); 489 // The cast types here aren't the same, so we cannot match an UMIN. 490 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 491 } 492 493 TEST_F(MatchSelectPatternTest, NotNotSMin) { 494 parseAssembly( 495 "define i8 @test(i8 %a, i8 %b) {\n" 496 " %cmp = icmp sgt i8 %a, %b\n" 497 " %an = xor i8 %a, -1\n" 498 " %bn = xor i8 %b, -1\n" 499 " %A = select i1 %cmp, i8 %an, i8 %bn\n" 500 " ret i8 %A\n" 501 "}\n"); 502 expectPattern({SPF_SMIN, SPNB_NA, false}); 503 } 504 505 TEST_F(MatchSelectPatternTest, NotNotSMinSwap) { 506 parseAssembly( 507 "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n" 508 " %cmp = icmp slt <2 x i8> %a, %b\n" 509 " %an = xor <2 x i8> %a, <i8 -1, i8-1>\n" 510 " %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n" 511 " %A = select <2 x i1> %cmp, <2 x i8> %bn, <2 x i8> %an\n" 512 " ret <2 x i8> %A\n" 513 "}\n"); 514 expectPattern({SPF_SMIN, SPNB_NA, false}); 515 } 516 517 TEST_F(MatchSelectPatternTest, NotNotSMax) { 518 parseAssembly( 519 "define i8 @test(i8 %a, i8 %b) {\n" 520 " %cmp = icmp slt i8 %a, %b\n" 521 " %an = xor i8 %a, -1\n" 522 " %bn = xor i8 %b, -1\n" 523 " %A = select i1 %cmp, i8 %an, i8 %bn\n" 524 " ret i8 %A\n" 525 "}\n"); 526 expectPattern({SPF_SMAX, SPNB_NA, false}); 527 } 528 529 TEST_F(MatchSelectPatternTest, NotNotSMaxSwap) { 530 parseAssembly( 531 "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n" 532 " %cmp = icmp sgt <2 x i8> %a, %b\n" 533 " %an = xor <2 x i8> %a, <i8 -1, i8-1>\n" 534 " %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n" 535 " %A = select <2 x i1> %cmp, <2 x i8> %bn, <2 x i8> %an\n" 536 " ret <2 x i8> %A\n" 537 "}\n"); 538 expectPattern({SPF_SMAX, SPNB_NA, false}); 539 } 540 541 TEST_F(MatchSelectPatternTest, NotNotUMin) { 542 parseAssembly( 543 "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n" 544 " %cmp = icmp ugt <2 x i8> %a, %b\n" 545 " %an = xor <2 x i8> %a, <i8 -1, i8-1>\n" 546 " %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n" 547 " %A = select <2 x i1> %cmp, <2 x i8> %an, <2 x i8> %bn\n" 548 " ret <2 x i8> %A\n" 549 "}\n"); 550 expectPattern({SPF_UMIN, SPNB_NA, false}); 551 } 552 553 TEST_F(MatchSelectPatternTest, NotNotUMinSwap) { 554 parseAssembly( 555 "define i8 @test(i8 %a, i8 %b) {\n" 556 " %cmp = icmp ult i8 %a, %b\n" 557 " %an = xor i8 %a, -1\n" 558 " %bn = xor i8 %b, -1\n" 559 " %A = select i1 %cmp, i8 %bn, i8 %an\n" 560 " ret i8 %A\n" 561 "}\n"); 562 expectPattern({SPF_UMIN, SPNB_NA, false}); 563 } 564 565 TEST_F(MatchSelectPatternTest, NotNotUMax) { 566 parseAssembly( 567 "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n" 568 " %cmp = icmp ult <2 x i8> %a, %b\n" 569 " %an = xor <2 x i8> %a, <i8 -1, i8-1>\n" 570 " %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n" 571 " %A = select <2 x i1> %cmp, <2 x i8> %an, <2 x i8> %bn\n" 572 " ret <2 x i8> %A\n" 573 "}\n"); 574 expectPattern({SPF_UMAX, SPNB_NA, false}); 575 } 576 577 TEST_F(MatchSelectPatternTest, NotNotUMaxSwap) { 578 parseAssembly( 579 "define i8 @test(i8 %a, i8 %b) {\n" 580 " %cmp = icmp ugt i8 %a, %b\n" 581 " %an = xor i8 %a, -1\n" 582 " %bn = xor i8 %b, -1\n" 583 " %A = select i1 %cmp, i8 %bn, i8 %an\n" 584 " ret i8 %A\n" 585 "}\n"); 586 expectPattern({SPF_UMAX, SPNB_NA, false}); 587 } 588 589 TEST_F(MatchSelectPatternTest, NotNotEq) { 590 parseAssembly( 591 "define i8 @test(i8 %a, i8 %b) {\n" 592 " %cmp = icmp eq i8 %a, %b\n" 593 " %an = xor i8 %a, -1\n" 594 " %bn = xor i8 %b, -1\n" 595 " %A = select i1 %cmp, i8 %bn, i8 %an\n" 596 " ret i8 %A\n" 597 "}\n"); 598 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 599 } 600 601 TEST_F(MatchSelectPatternTest, NotNotNe) { 602 parseAssembly( 603 "define i8 @test(i8 %a, i8 %b) {\n" 604 " %cmp = icmp ne i8 %a, %b\n" 605 " %an = xor i8 %a, -1\n" 606 " %bn = xor i8 %b, -1\n" 607 " %A = select i1 %cmp, i8 %bn, i8 %an\n" 608 " ret i8 %A\n" 609 "}\n"); 610 expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 611 } 612 613 TEST(ValueTracking, GuaranteedToTransferExecutionToSuccessor) { 614 StringRef Assembly = 615 "declare void @nounwind_readonly(i32*) nounwind readonly " 616 "declare void @nounwind_argmemonly(i32*) nounwind argmemonly " 617 "declare void @throws_but_readonly(i32*) readonly " 618 "declare void @throws_but_argmemonly(i32*) argmemonly " 619 "declare void @nounwind_willreturn(i32*) nounwind willreturn" 620 " " 621 "declare void @unknown(i32*) " 622 " " 623 "define void @f(i32* %p) { " 624 " call void @nounwind_readonly(i32* %p) " 625 " call void @nounwind_argmemonly(i32* %p) " 626 " call void @throws_but_readonly(i32* %p) " 627 " call void @throws_but_argmemonly(i32* %p) " 628 " call void @unknown(i32* %p) nounwind readonly " 629 " call void @unknown(i32* %p) nounwind argmemonly " 630 " call void @unknown(i32* %p) readonly " 631 " call void @unknown(i32* %p) argmemonly " 632 " call void @nounwind_willreturn(i32* %p)" 633 " ret void " 634 "} "; 635 636 LLVMContext Context; 637 SMDiagnostic Error; 638 auto M = parseAssemblyString(Assembly, Error, Context); 639 assert(M && "Bad assembly?"); 640 641 auto *F = M->getFunction("f"); 642 assert(F && "Bad assembly?"); 643 644 auto &BB = F->getEntryBlock(); 645 bool ExpectedAnswers[] = { 646 true, // call void @nounwind_readonly(i32* %p) 647 true, // call void @nounwind_argmemonly(i32* %p) 648 false, // call void @throws_but_readonly(i32* %p) 649 false, // call void @throws_but_argmemonly(i32* %p) 650 true, // call void @unknown(i32* %p) nounwind readonly 651 true, // call void @unknown(i32* %p) nounwind argmemonly 652 false, // call void @unknown(i32* %p) readonly 653 false, // call void @unknown(i32* %p) argmemonly 654 true, // call void @nounwind_willreturn(i32* %p) 655 false, // ret void 656 }; 657 658 int Index = 0; 659 for (auto &I : BB) { 660 EXPECT_EQ(isGuaranteedToTransferExecutionToSuccessor(&I), 661 ExpectedAnswers[Index]) 662 << "Incorrect answer at instruction " << Index << " = " << I; 663 Index++; 664 } 665 } 666 667 TEST_F(ValueTrackingTest, ComputeNumSignBits_PR32045) { 668 parseAssembly( 669 "define i32 @test(i32 %a) {\n" 670 " %A = ashr i32 %a, -1\n" 671 " ret i32 %A\n" 672 "}\n"); 673 EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u); 674 } 675 676 // No guarantees for canonical IR in this analysis, so this just bails out. 677 TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle) { 678 parseAssembly( 679 "define <2 x i32> @test() {\n" 680 " %A = shufflevector <2 x i32> undef, <2 x i32> undef, <2 x i32> <i32 0, i32 0>\n" 681 " ret <2 x i32> %A\n" 682 "}\n"); 683 EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u); 684 } 685 686 // No guarantees for canonical IR in this analysis, so a shuffle element that 687 // references an undef value means this can't return any extra information. 688 TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle2) { 689 parseAssembly( 690 "define <2 x i32> @test(<2 x i1> %x) {\n" 691 " %sext = sext <2 x i1> %x to <2 x i32>\n" 692 " %A = shufflevector <2 x i32> %sext, <2 x i32> undef, <2 x i32> <i32 0, i32 2>\n" 693 " ret <2 x i32> %A\n" 694 "}\n"); 695 EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u); 696 } 697 698 TEST_F(ValueTrackingTest, impliesPoisonTest_Identity) { 699 parseAssembly("define void @test(i32 %x, i32 %y) {\n" 700 " %A = add i32 %x, %y\n" 701 " ret void\n" 702 "}"); 703 EXPECT_TRUE(impliesPoison(A, A)); 704 } 705 706 TEST_F(ValueTrackingTest, impliesPoisonTest_ICmp) { 707 parseAssembly("define void @test(i32 %x) {\n" 708 " %A2 = icmp eq i32 %x, 0\n" 709 " %A = icmp eq i32 %x, 1\n" 710 " ret void\n" 711 "}"); 712 EXPECT_TRUE(impliesPoison(A2, A)); 713 } 714 715 TEST_F(ValueTrackingTest, impliesPoisonTest_ICmpUnknown) { 716 parseAssembly("define void @test(i32 %x, i32 %y) {\n" 717 " %A2 = icmp eq i32 %x, %y\n" 718 " %A = icmp eq i32 %x, 1\n" 719 " ret void\n" 720 "}"); 721 EXPECT_FALSE(impliesPoison(A2, A)); 722 } 723 724 TEST_F(ValueTrackingTest, impliesPoisonTest_AddNswOkay) { 725 parseAssembly("define void @test(i32 %x) {\n" 726 " %A2 = add nsw i32 %x, 1\n" 727 " %A = add i32 %A2, 1\n" 728 " ret void\n" 729 "}"); 730 EXPECT_TRUE(impliesPoison(A2, A)); 731 } 732 733 TEST_F(ValueTrackingTest, impliesPoisonTest_AddNswOkay2) { 734 parseAssembly("define void @test(i32 %x) {\n" 735 " %A2 = add i32 %x, 1\n" 736 " %A = add nsw i32 %A2, 1\n" 737 " ret void\n" 738 "}"); 739 EXPECT_TRUE(impliesPoison(A2, A)); 740 } 741 742 TEST_F(ValueTrackingTest, impliesPoisonTest_AddNsw) { 743 parseAssembly("define void @test(i32 %x) {\n" 744 " %A2 = add nsw i32 %x, 1\n" 745 " %A = add i32 %x, 1\n" 746 " ret void\n" 747 "}"); 748 EXPECT_FALSE(impliesPoison(A2, A)); 749 } 750 751 TEST_F(ValueTrackingTest, impliesPoisonTest_Cmp) { 752 parseAssembly("define void @test(i32 %x, i32 %y, i1 %c) {\n" 753 " %A2 = icmp eq i32 %x, %y\n" 754 " %A0 = icmp ult i32 %x, %y\n" 755 " %A = or i1 %A0, %c\n" 756 " ret void\n" 757 "}"); 758 EXPECT_TRUE(impliesPoison(A2, A)); 759 } 760 761 TEST_F(ValueTrackingTest, impliesPoisonTest_FCmpFMF) { 762 parseAssembly("define void @test(float %x, float %y, i1 %c) {\n" 763 " %A2 = fcmp nnan oeq float %x, %y\n" 764 " %A0 = fcmp olt float %x, %y\n" 765 " %A = or i1 %A0, %c\n" 766 " ret void\n" 767 "}"); 768 EXPECT_FALSE(impliesPoison(A2, A)); 769 } 770 771 TEST_F(ValueTrackingTest, impliesPoisonTest_AddSubSameOps) { 772 parseAssembly("define void @test(i32 %x, i32 %y, i1 %c) {\n" 773 " %A2 = add i32 %x, %y\n" 774 " %A = sub i32 %x, %y\n" 775 " ret void\n" 776 "}"); 777 EXPECT_TRUE(impliesPoison(A2, A)); 778 } 779 780 TEST_F(ValueTrackingTest, impliesPoisonTest_MaskCmp) { 781 parseAssembly("define void @test(i32 %x, i32 %y, i1 %c) {\n" 782 " %M2 = and i32 %x, 7\n" 783 " %A2 = icmp eq i32 %M2, 1\n" 784 " %M = and i32 %x, 15\n" 785 " %A = icmp eq i32 %M, 3\n" 786 " ret void\n" 787 "}"); 788 EXPECT_TRUE(impliesPoison(A2, A)); 789 } 790 791 TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle_Pointers) { 792 parseAssembly( 793 "define <2 x i32*> @test(<2 x i32*> %x) {\n" 794 " %A = shufflevector <2 x i32*> zeroinitializer, <2 x i32*> undef, <2 x i32> zeroinitializer\n" 795 " ret <2 x i32*> %A\n" 796 "}\n"); 797 EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 64u); 798 } 799 800 TEST(ValueTracking, propagatesPoison) { 801 std::string AsmHead = "declare i32 @g(i32)\n" 802 "define void @f(i32 %x, i32 %y, float %fx, float %fy, " 803 "i1 %cond, i8* %p) {\n"; 804 std::string AsmTail = " ret void\n}"; 805 // (propagates poison?, IR instruction) 806 SmallVector<std::pair<bool, std::string>, 32> Data = { 807 {true, "add i32 %x, %y"}, 808 {true, "add nsw nuw i32 %x, %y"}, 809 {true, "ashr i32 %x, %y"}, 810 {true, "lshr exact i32 %x, 31"}, 811 {true, "fcmp oeq float %fx, %fy"}, 812 {true, "icmp eq i32 %x, %y"}, 813 {true, "getelementptr i8, i8* %p, i32 %x"}, 814 {true, "getelementptr inbounds i8, i8* %p, i32 %x"}, 815 {true, "bitcast float %fx to i32"}, 816 {false, "select i1 %cond, i32 %x, i32 %y"}, 817 {false, "freeze i32 %x"}, 818 {true, "udiv i32 %x, %y"}, 819 {true, "urem i32 %x, %y"}, 820 {true, "sdiv exact i32 %x, %y"}, 821 {true, "srem i32 %x, %y"}, 822 {false, "call i32 @g(i32 %x)"}}; 823 824 std::string AssemblyStr = AsmHead; 825 for (auto &Itm : Data) 826 AssemblyStr += Itm.second + "\n"; 827 AssemblyStr += AsmTail; 828 829 LLVMContext Context; 830 SMDiagnostic Error; 831 auto M = parseAssemblyString(AssemblyStr, Error, Context); 832 assert(M && "Bad assembly?"); 833 834 auto *F = M->getFunction("f"); 835 assert(F && "Bad assembly?"); 836 837 auto &BB = F->getEntryBlock(); 838 839 int Index = 0; 840 for (auto &I : BB) { 841 if (isa<ReturnInst>(&I)) 842 break; 843 EXPECT_EQ(propagatesPoison(cast<Operator>(&I)), Data[Index].first) 844 << "Incorrect answer at instruction " << Index << " = " << I; 845 Index++; 846 } 847 } 848 849 TEST_F(ValueTrackingTest, programUndefinedIfPoison) { 850 parseAssembly("declare i32 @any_num()" 851 "define void @test(i32 %mask) {\n" 852 " %A = call i32 @any_num()\n" 853 " %B = or i32 %A, %mask\n" 854 " udiv i32 1, %B" 855 " ret void\n" 856 "}\n"); 857 // If %A was poison, udiv raises UB regardless of %mask's value 858 EXPECT_EQ(programUndefinedIfPoison(A), true); 859 } 860 861 TEST_F(ValueTrackingTest, programUndefinedIfUndefOrPoison) { 862 parseAssembly("declare i32 @any_num()" 863 "define void @test(i32 %mask) {\n" 864 " %A = call i32 @any_num()\n" 865 " %B = or i32 %A, %mask\n" 866 " udiv i32 1, %B" 867 " ret void\n" 868 "}\n"); 869 // If %A was undef and %mask was 1, udiv does not raise UB 870 EXPECT_EQ(programUndefinedIfUndefOrPoison(A), false); 871 } 872 873 TEST_F(ValueTrackingTest, isGuaranteedNotToBePoison_exploitBranchCond) { 874 parseAssembly("declare i1 @any_bool()" 875 "define void @test(i1 %y) {\n" 876 " %A = call i1 @any_bool()\n" 877 " %cond = and i1 %A, %y\n" 878 " br i1 %cond, label %BB1, label %BB2\n" 879 "BB1:\n" 880 " ret void\n" 881 "BB2:\n" 882 " ret void\n" 883 "}\n"); 884 DominatorTree DT(*F); 885 for (auto &BB : *F) { 886 if (&BB == &F->getEntryBlock()) 887 continue; 888 889 EXPECT_EQ(isGuaranteedNotToBePoison(A, nullptr, BB.getTerminator(), &DT), 890 true) 891 << "isGuaranteedNotToBePoison does not hold at " << *BB.getTerminator(); 892 } 893 } 894 895 TEST_F(ValueTrackingTest, isGuaranteedNotToBePoison_phi) { 896 parseAssembly("declare i32 @any_i32(i32)" 897 "define void @test() {\n" 898 "ENTRY:\n" 899 " br label %LOOP\n" 900 "LOOP:\n" 901 " %A = phi i32 [0, %ENTRY], [%A.next, %NEXT]\n" 902 " %A.next = call i32 @any_i32(i32 %A)\n" 903 " %cond = icmp eq i32 %A.next, 0\n" 904 " br i1 %cond, label %NEXT, label %EXIT\n" 905 "NEXT:\n" 906 " br label %LOOP\n" 907 "EXIT:\n" 908 " ret void\n" 909 "}\n"); 910 DominatorTree DT(*F); 911 for (auto &BB : *F) { 912 if (BB.getName() == "LOOP") { 913 EXPECT_EQ(isGuaranteedNotToBePoison(A, nullptr, A, &DT), true) 914 << "isGuaranteedNotToBePoison does not hold"; 915 } 916 } 917 } 918 919 TEST_F(ValueTrackingTest, isGuaranteedNotToBeUndefOrPoison) { 920 parseAssembly("declare void @f(i32 noundef)" 921 "define void @test(i32 %x) {\n" 922 " %A = bitcast i32 %x to i32\n" 923 " call void @f(i32 noundef %x)\n" 924 " ret void\n" 925 "}\n"); 926 EXPECT_EQ(isGuaranteedNotToBeUndefOrPoison(A), true); 927 EXPECT_EQ(isGuaranteedNotToBeUndefOrPoison(UndefValue::get(IntegerType::get(Context, 8))), false); 928 EXPECT_EQ(isGuaranteedNotToBeUndefOrPoison(PoisonValue::get(IntegerType::get(Context, 8))), false); 929 EXPECT_EQ(isGuaranteedNotToBePoison(UndefValue::get(IntegerType::get(Context, 8))), true); 930 EXPECT_EQ(isGuaranteedNotToBePoison(PoisonValue::get(IntegerType::get(Context, 8))), false); 931 932 Type *Int32Ty = Type::getInt32Ty(Context); 933 Constant *CU = UndefValue::get(Int32Ty); 934 Constant *CP = PoisonValue::get(Int32Ty); 935 Constant *C1 = ConstantInt::get(Int32Ty, 1); 936 Constant *C2 = ConstantInt::get(Int32Ty, 2); 937 938 { 939 Constant *V1 = ConstantVector::get({C1, C2}); 940 EXPECT_TRUE(isGuaranteedNotToBeUndefOrPoison(V1)); 941 EXPECT_TRUE(isGuaranteedNotToBePoison(V1)); 942 } 943 944 { 945 Constant *V2 = ConstantVector::get({C1, CU}); 946 EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(V2)); 947 EXPECT_TRUE(isGuaranteedNotToBePoison(V2)); 948 } 949 950 { 951 Constant *V3 = ConstantVector::get({C1, CP}); 952 EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(V3)); 953 EXPECT_FALSE(isGuaranteedNotToBePoison(V3)); 954 } 955 } 956 957 TEST_F(ValueTrackingTest, isGuaranteedNotToBeUndefOrPoison_assume) { 958 parseAssembly("declare i1 @f_i1()\n" 959 "declare i32 @f_i32()\n" 960 "declare void @llvm.assume(i1)\n" 961 "define void @test() {\n" 962 " %A = call i32 @f_i32()\n" 963 " %cond = call i1 @f_i1()\n" 964 " %CxtI = add i32 0, 0\n" 965 " br i1 %cond, label %BB1, label %EXIT\n" 966 "BB1:\n" 967 " %CxtI2 = add i32 0, 0\n" 968 " %cond2 = call i1 @f_i1()\n" 969 " call void @llvm.assume(i1 true) [ \"noundef\"(i32 %A) ]\n" 970 " br i1 %cond2, label %BB2, label %EXIT\n" 971 "BB2:\n" 972 " %CxtI3 = add i32 0, 0\n" 973 " ret void\n" 974 "EXIT:\n" 975 " ret void\n" 976 "}"); 977 AssumptionCache AC(*F); 978 DominatorTree DT(*F); 979 EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(A, &AC, CxtI, &DT)); 980 EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(A, &AC, CxtI2, &DT)); 981 EXPECT_TRUE(isGuaranteedNotToBeUndefOrPoison(A, &AC, CxtI3, &DT)); 982 } 983 984 TEST(ValueTracking, canCreatePoisonOrUndef) { 985 std::string AsmHead = 986 "@s = external dso_local global i32, align 1\n" 987 "declare i32 @g(i32)\n" 988 "define void @f(i32 %x, i32 %y, float %fx, float %fy, i1 %cond, " 989 "<4 x i32> %vx, <4 x i32> %vx2, <vscale x 4 x i32> %svx, i8* %p) {\n"; 990 std::string AsmTail = " ret void\n}"; 991 // (can create poison?, can create undef?, IR instruction) 992 SmallVector<std::pair<std::pair<bool, bool>, std::string>, 32> Data = { 993 {{false, false}, "add i32 %x, %y"}, 994 {{true, false}, "add nsw nuw i32 %x, %y"}, 995 {{true, false}, "shl i32 %x, %y"}, 996 {{true, false}, "shl <4 x i32> %vx, %vx2"}, 997 {{true, false}, "shl nsw i32 %x, %y"}, 998 {{true, false}, "shl nsw <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"}, 999 {{false, false}, "shl i32 %x, 31"}, 1000 {{true, false}, "shl i32 %x, 32"}, 1001 {{false, false}, "shl <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"}, 1002 {{true, false}, "shl <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 32>"}, 1003 {{true, false}, "ashr i32 %x, %y"}, 1004 {{true, false}, "ashr exact i32 %x, %y"}, 1005 {{false, false}, "ashr i32 %x, 31"}, 1006 {{true, false}, "ashr exact i32 %x, 31"}, 1007 {{false, false}, "ashr <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"}, 1008 {{true, false}, "ashr <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 32>"}, 1009 {{true, false}, "ashr exact <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"}, 1010 {{true, false}, "lshr i32 %x, %y"}, 1011 {{true, false}, "lshr exact i32 %x, 31"}, 1012 {{false, false}, "udiv i32 %x, %y"}, 1013 {{true, false}, "udiv exact i32 %x, %y"}, 1014 {{false, false}, "getelementptr i8, i8* %p, i32 %x"}, 1015 {{true, false}, "getelementptr inbounds i8, i8* %p, i32 %x"}, 1016 {{true, false}, "fneg nnan float %fx"}, 1017 {{false, false}, "fneg float %fx"}, 1018 {{false, false}, "fadd float %fx, %fy"}, 1019 {{true, false}, "fadd nnan float %fx, %fy"}, 1020 {{false, false}, "urem i32 %x, %y"}, 1021 {{true, false}, "fptoui float %fx to i32"}, 1022 {{true, false}, "fptosi float %fx to i32"}, 1023 {{false, false}, "bitcast float %fx to i32"}, 1024 {{false, false}, "select i1 %cond, i32 %x, i32 %y"}, 1025 {{true, false}, "select nnan i1 %cond, float %fx, float %fy"}, 1026 {{true, false}, "extractelement <4 x i32> %vx, i32 %x"}, 1027 {{false, false}, "extractelement <4 x i32> %vx, i32 3"}, 1028 {{true, false}, "extractelement <vscale x 4 x i32> %svx, i32 4"}, 1029 {{true, false}, "insertelement <4 x i32> %vx, i32 %x, i32 %y"}, 1030 {{false, false}, "insertelement <4 x i32> %vx, i32 %x, i32 3"}, 1031 {{true, false}, "insertelement <vscale x 4 x i32> %svx, i32 %x, i32 4"}, 1032 {{false, false}, "freeze i32 %x"}, 1033 {{false, false}, 1034 "shufflevector <4 x i32> %vx, <4 x i32> %vx2, " 1035 "<4 x i32> <i32 0, i32 1, i32 2, i32 3>"}, 1036 {{false, true}, 1037 "shufflevector <4 x i32> %vx, <4 x i32> %vx2, " 1038 "<4 x i32> <i32 0, i32 1, i32 2, i32 undef>"}, 1039 {{false, true}, 1040 "shufflevector <vscale x 4 x i32> %svx, " 1041 "<vscale x 4 x i32> %svx, <vscale x 4 x i32> undef"}, 1042 {{true, false}, "call i32 @g(i32 %x)"}, 1043 {{false, false}, "call noundef i32 @g(i32 %x)"}, 1044 {{true, false}, "fcmp nnan oeq float %fx, %fy"}, 1045 {{false, false}, "fcmp oeq float %fx, %fy"}, 1046 {{true, false}, 1047 "ashr <4 x i32> %vx, select (i1 icmp sgt (i32 ptrtoint (i32* @s to " 1048 "i32), i32 1), <4 x i32> zeroinitializer, <4 x i32> <i32 0, i32 1, i32 " 1049 "2, i32 3>)"}}; 1050 1051 std::string AssemblyStr = AsmHead; 1052 for (auto &Itm : Data) 1053 AssemblyStr += Itm.second + "\n"; 1054 AssemblyStr += AsmTail; 1055 1056 LLVMContext Context; 1057 SMDiagnostic Error; 1058 auto M = parseAssemblyString(AssemblyStr, Error, Context); 1059 assert(M && "Bad assembly?"); 1060 1061 auto *F = M->getFunction("f"); 1062 assert(F && "Bad assembly?"); 1063 1064 auto &BB = F->getEntryBlock(); 1065 1066 int Index = 0; 1067 for (auto &I : BB) { 1068 if (isa<ReturnInst>(&I)) 1069 break; 1070 bool Poison = Data[Index].first.first; 1071 bool Undef = Data[Index].first.second; 1072 EXPECT_EQ(canCreatePoison(cast<Operator>(&I)), Poison) 1073 << "Incorrect answer of canCreatePoison at instruction " << Index 1074 << " = " << I; 1075 EXPECT_EQ(canCreateUndefOrPoison(cast<Operator>(&I)), Undef || Poison) 1076 << "Incorrect answer of canCreateUndef at instruction " << Index 1077 << " = " << I; 1078 Index++; 1079 } 1080 } 1081 1082 TEST_F(ValueTrackingTest, computePtrAlignment) { 1083 parseAssembly("declare i1 @f_i1()\n" 1084 "declare i8* @f_i8p()\n" 1085 "declare void @llvm.assume(i1)\n" 1086 "define void @test() {\n" 1087 " %A = call i8* @f_i8p()\n" 1088 " %cond = call i1 @f_i1()\n" 1089 " %CxtI = add i32 0, 0\n" 1090 " br i1 %cond, label %BB1, label %EXIT\n" 1091 "BB1:\n" 1092 " %CxtI2 = add i32 0, 0\n" 1093 " %cond2 = call i1 @f_i1()\n" 1094 " call void @llvm.assume(i1 true) [ \"align\"(i8* %A, i64 16) ]\n" 1095 " br i1 %cond2, label %BB2, label %EXIT\n" 1096 "BB2:\n" 1097 " %CxtI3 = add i32 0, 0\n" 1098 " ret void\n" 1099 "EXIT:\n" 1100 " ret void\n" 1101 "}"); 1102 AssumptionCache AC(*F); 1103 DominatorTree DT(*F); 1104 DataLayout DL = M->getDataLayout(); 1105 EXPECT_EQ(getKnownAlignment(A, DL, CxtI, &AC, &DT), Align(1)); 1106 EXPECT_EQ(getKnownAlignment(A, DL, CxtI2, &AC, &DT), Align(1)); 1107 EXPECT_EQ(getKnownAlignment(A, DL, CxtI3, &AC, &DT), Align(16)); 1108 } 1109 1110 TEST_F(ComputeKnownBitsTest, ComputeKnownBits) { 1111 parseAssembly( 1112 "define i32 @test(i32 %a, i32 %b) {\n" 1113 " %ash = mul i32 %a, 8\n" 1114 " %aad = add i32 %ash, 7\n" 1115 " %aan = and i32 %aad, 4095\n" 1116 " %bsh = shl i32 %b, 4\n" 1117 " %bad = or i32 %bsh, 6\n" 1118 " %ban = and i32 %bad, 4095\n" 1119 " %A = mul i32 %aan, %ban\n" 1120 " ret i32 %A\n" 1121 "}\n"); 1122 expectKnownBits(/*zero*/ 4278190085u, /*one*/ 10u); 1123 } 1124 1125 TEST_F(ComputeKnownBitsTest, ComputeKnownMulBits) { 1126 parseAssembly( 1127 "define i32 @test(i32 %a, i32 %b) {\n" 1128 " %aa = shl i32 %a, 5\n" 1129 " %bb = shl i32 %b, 5\n" 1130 " %aaa = or i32 %aa, 24\n" 1131 " %bbb = or i32 %bb, 28\n" 1132 " %A = mul i32 %aaa, %bbb\n" 1133 " ret i32 %A\n" 1134 "}\n"); 1135 expectKnownBits(/*zero*/ 95u, /*one*/ 32u); 1136 } 1137 1138 TEST_F(ValueTrackingTest, KnownNonZeroFromDomCond) { 1139 parseAssembly(R"( 1140 declare i8* @f_i8() 1141 define void @test(i1 %c) { 1142 %A = call i8* @f_i8() 1143 %B = call i8* @f_i8() 1144 %c1 = icmp ne i8* %A, null 1145 %cond = and i1 %c1, %c 1146 br i1 %cond, label %T, label %Q 1147 T: 1148 %CxtI = add i32 0, 0 1149 ret void 1150 Q: 1151 %CxtI2 = add i32 0, 0 1152 ret void 1153 } 1154 )"); 1155 AssumptionCache AC(*F); 1156 DominatorTree DT(*F); 1157 DataLayout DL = M->getDataLayout(); 1158 EXPECT_EQ(isKnownNonZero(A, DL, 0, &AC, CxtI, &DT), true); 1159 EXPECT_EQ(isKnownNonZero(A, DL, 0, &AC, CxtI2, &DT), false); 1160 } 1161 1162 TEST_F(ValueTrackingTest, KnownNonZeroFromDomCond2) { 1163 parseAssembly(R"( 1164 declare i8* @f_i8() 1165 define void @test(i1 %c) { 1166 %A = call i8* @f_i8() 1167 %B = call i8* @f_i8() 1168 %c1 = icmp ne i8* %A, null 1169 %cond = select i1 %c, i1 %c1, i1 false 1170 br i1 %cond, label %T, label %Q 1171 T: 1172 %CxtI = add i32 0, 0 1173 ret void 1174 Q: 1175 %CxtI2 = add i32 0, 0 1176 ret void 1177 } 1178 )"); 1179 AssumptionCache AC(*F); 1180 DominatorTree DT(*F); 1181 DataLayout DL = M->getDataLayout(); 1182 EXPECT_EQ(isKnownNonZero(A, DL, 0, &AC, CxtI, &DT), true); 1183 EXPECT_EQ(isKnownNonZero(A, DL, 0, &AC, CxtI2, &DT), false); 1184 } 1185 1186 TEST_F(ValueTrackingTest, IsImpliedConditionAnd) { 1187 parseAssembly(R"( 1188 define void @test(i32 %x, i32 %y) { 1189 %c1 = icmp ult i32 %x, 10 1190 %c2 = icmp ult i32 %y, 15 1191 %A = and i1 %c1, %c2 1192 ; x < 10 /\ y < 15 1193 %A2 = icmp ult i32 %x, 20 1194 %A3 = icmp uge i32 %y, 20 1195 %A4 = icmp ult i32 %x, 5 1196 ret void 1197 } 1198 )"); 1199 DataLayout DL = M->getDataLayout(); 1200 EXPECT_EQ(isImpliedCondition(A, A2, DL), true); 1201 EXPECT_EQ(isImpliedCondition(A, A3, DL), false); 1202 EXPECT_EQ(isImpliedCondition(A, A4, DL), None); 1203 } 1204 1205 TEST_F(ValueTrackingTest, IsImpliedConditionAnd2) { 1206 parseAssembly(R"( 1207 define void @test(i32 %x, i32 %y) { 1208 %c1 = icmp ult i32 %x, 10 1209 %c2 = icmp ult i32 %y, 15 1210 %A = select i1 %c1, i1 %c2, i1 false 1211 ; x < 10 /\ y < 15 1212 %A2 = icmp ult i32 %x, 20 1213 %A3 = icmp uge i32 %y, 20 1214 %A4 = icmp ult i32 %x, 5 1215 ret void 1216 } 1217 )"); 1218 DataLayout DL = M->getDataLayout(); 1219 EXPECT_EQ(isImpliedCondition(A, A2, DL), true); 1220 EXPECT_EQ(isImpliedCondition(A, A3, DL), false); 1221 EXPECT_EQ(isImpliedCondition(A, A4, DL), None); 1222 } 1223 1224 TEST_F(ValueTrackingTest, IsImpliedConditionOr) { 1225 parseAssembly(R"( 1226 define void @test(i32 %x, i32 %y) { 1227 %c1 = icmp ult i32 %x, 10 1228 %c2 = icmp ult i32 %y, 15 1229 %A = or i1 %c1, %c2 ; negated 1230 ; x >= 10 /\ y >= 15 1231 %A2 = icmp ult i32 %x, 5 1232 %A3 = icmp uge i32 %y, 10 1233 %A4 = icmp ult i32 %x, 15 1234 ret void 1235 } 1236 )"); 1237 DataLayout DL = M->getDataLayout(); 1238 EXPECT_EQ(isImpliedCondition(A, A2, DL, false), false); 1239 EXPECT_EQ(isImpliedCondition(A, A3, DL, false), true); 1240 EXPECT_EQ(isImpliedCondition(A, A4, DL, false), None); 1241 } 1242 1243 TEST_F(ValueTrackingTest, IsImpliedConditionOr2) { 1244 parseAssembly(R"( 1245 define void @test(i32 %x, i32 %y) { 1246 %c1 = icmp ult i32 %x, 10 1247 %c2 = icmp ult i32 %y, 15 1248 %A = select i1 %c1, i1 true, i1 %c2 ; negated 1249 ; x >= 10 /\ y >= 15 1250 %A2 = icmp ult i32 %x, 5 1251 %A3 = icmp uge i32 %y, 10 1252 %A4 = icmp ult i32 %x, 15 1253 ret void 1254 } 1255 )"); 1256 DataLayout DL = M->getDataLayout(); 1257 EXPECT_EQ(isImpliedCondition(A, A2, DL, false), false); 1258 EXPECT_EQ(isImpliedCondition(A, A3, DL, false), true); 1259 EXPECT_EQ(isImpliedCondition(A, A4, DL, false), None); 1260 } 1261 1262 TEST_F(ComputeKnownBitsTest, KnownNonZeroShift) { 1263 // %q is known nonzero without known bits. 1264 // Because %q is nonzero, %A[0] is known to be zero. 1265 parseAssembly( 1266 "define i8 @test(i8 %p, i8* %pq) {\n" 1267 " %q = load i8, i8* %pq, !range !0\n" 1268 " %A = shl i8 %p, %q\n" 1269 " ret i8 %A\n" 1270 "}\n" 1271 "!0 = !{ i8 1, i8 5 }\n"); 1272 expectKnownBits(/*zero*/ 1u, /*one*/ 0u); 1273 } 1274 1275 TEST_F(ComputeKnownBitsTest, ComputeKnownFshl) { 1276 // fshl(....1111....0000, 00..1111........, 6) 1277 // = 11....000000..11 1278 parseAssembly( 1279 "define i16 @test(i16 %a, i16 %b) {\n" 1280 " %aa = shl i16 %a, 4\n" 1281 " %bb = lshr i16 %b, 2\n" 1282 " %aaa = or i16 %aa, 3840\n" 1283 " %bbb = or i16 %bb, 3840\n" 1284 " %A = call i16 @llvm.fshl.i16(i16 %aaa, i16 %bbb, i16 6)\n" 1285 " ret i16 %A\n" 1286 "}\n" 1287 "declare i16 @llvm.fshl.i16(i16, i16, i16)\n"); 1288 expectKnownBits(/*zero*/ 1008u, /*one*/ 49155u); 1289 } 1290 1291 TEST_F(ComputeKnownBitsTest, ComputeKnownFshr) { 1292 // fshr(....1111....0000, 00..1111........, 26) 1293 // = 11....000000..11 1294 parseAssembly( 1295 "define i16 @test(i16 %a, i16 %b) {\n" 1296 " %aa = shl i16 %a, 4\n" 1297 " %bb = lshr i16 %b, 2\n" 1298 " %aaa = or i16 %aa, 3840\n" 1299 " %bbb = or i16 %bb, 3840\n" 1300 " %A = call i16 @llvm.fshr.i16(i16 %aaa, i16 %bbb, i16 26)\n" 1301 " ret i16 %A\n" 1302 "}\n" 1303 "declare i16 @llvm.fshr.i16(i16, i16, i16)\n"); 1304 expectKnownBits(/*zero*/ 1008u, /*one*/ 49155u); 1305 } 1306 1307 TEST_F(ComputeKnownBitsTest, ComputeKnownFshlZero) { 1308 // fshl(....1111....0000, 00..1111........, 0) 1309 // = ....1111....0000 1310 parseAssembly( 1311 "define i16 @test(i16 %a, i16 %b) {\n" 1312 " %aa = shl i16 %a, 4\n" 1313 " %bb = lshr i16 %b, 2\n" 1314 " %aaa = or i16 %aa, 3840\n" 1315 " %bbb = or i16 %bb, 3840\n" 1316 " %A = call i16 @llvm.fshl.i16(i16 %aaa, i16 %bbb, i16 0)\n" 1317 " ret i16 %A\n" 1318 "}\n" 1319 "declare i16 @llvm.fshl.i16(i16, i16, i16)\n"); 1320 expectKnownBits(/*zero*/ 15u, /*one*/ 3840u); 1321 } 1322 1323 TEST_F(ComputeKnownBitsTest, ComputeKnownUAddSatLeadingOnes) { 1324 // uadd.sat(1111...1, ........) 1325 // = 1111.... 1326 parseAssembly( 1327 "define i8 @test(i8 %a, i8 %b) {\n" 1328 " %aa = or i8 %a, 241\n" 1329 " %A = call i8 @llvm.uadd.sat.i8(i8 %aa, i8 %b)\n" 1330 " ret i8 %A\n" 1331 "}\n" 1332 "declare i8 @llvm.uadd.sat.i8(i8, i8)\n"); 1333 expectKnownBits(/*zero*/ 0u, /*one*/ 240u); 1334 } 1335 1336 TEST_F(ComputeKnownBitsTest, ComputeKnownUAddSatOnesPreserved) { 1337 // uadd.sat(00...011, .1...110) 1338 // = .......1 1339 parseAssembly( 1340 "define i8 @test(i8 %a, i8 %b) {\n" 1341 " %aa = or i8 %a, 3\n" 1342 " %aaa = and i8 %aa, 59\n" 1343 " %bb = or i8 %b, 70\n" 1344 " %bbb = and i8 %bb, 254\n" 1345 " %A = call i8 @llvm.uadd.sat.i8(i8 %aaa, i8 %bbb)\n" 1346 " ret i8 %A\n" 1347 "}\n" 1348 "declare i8 @llvm.uadd.sat.i8(i8, i8)\n"); 1349 expectKnownBits(/*zero*/ 0u, /*one*/ 1u); 1350 } 1351 1352 TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatLHSLeadingZeros) { 1353 // usub.sat(0000...0, ........) 1354 // = 0000.... 1355 parseAssembly( 1356 "define i8 @test(i8 %a, i8 %b) {\n" 1357 " %aa = and i8 %a, 14\n" 1358 " %A = call i8 @llvm.usub.sat.i8(i8 %aa, i8 %b)\n" 1359 " ret i8 %A\n" 1360 "}\n" 1361 "declare i8 @llvm.usub.sat.i8(i8, i8)\n"); 1362 expectKnownBits(/*zero*/ 240u, /*one*/ 0u); 1363 } 1364 1365 TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatRHSLeadingOnes) { 1366 // usub.sat(........, 1111...1) 1367 // = 0000.... 1368 parseAssembly( 1369 "define i8 @test(i8 %a, i8 %b) {\n" 1370 " %bb = or i8 %a, 241\n" 1371 " %A = call i8 @llvm.usub.sat.i8(i8 %a, i8 %bb)\n" 1372 " ret i8 %A\n" 1373 "}\n" 1374 "declare i8 @llvm.usub.sat.i8(i8, i8)\n"); 1375 expectKnownBits(/*zero*/ 240u, /*one*/ 0u); 1376 } 1377 1378 TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatZerosPreserved) { 1379 // usub.sat(11...011, .1...110) 1380 // = ......0. 1381 parseAssembly( 1382 "define i8 @test(i8 %a, i8 %b) {\n" 1383 " %aa = or i8 %a, 195\n" 1384 " %aaa = and i8 %aa, 251\n" 1385 " %bb = or i8 %b, 70\n" 1386 " %bbb = and i8 %bb, 254\n" 1387 " %A = call i8 @llvm.usub.sat.i8(i8 %aaa, i8 %bbb)\n" 1388 " ret i8 %A\n" 1389 "}\n" 1390 "declare i8 @llvm.usub.sat.i8(i8, i8)\n"); 1391 expectKnownBits(/*zero*/ 2u, /*one*/ 0u); 1392 } 1393 1394 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsPtrToIntTrunc) { 1395 // ptrtoint truncates the pointer type. 1396 parseAssembly( 1397 "define void @test(i8** %p) {\n" 1398 " %A = load i8*, i8** %p\n" 1399 " %i = ptrtoint i8* %A to i32\n" 1400 " %m = and i32 %i, 31\n" 1401 " %c = icmp eq i32 %m, 0\n" 1402 " call void @llvm.assume(i1 %c)\n" 1403 " ret void\n" 1404 "}\n" 1405 "declare void @llvm.assume(i1)\n"); 1406 AssumptionCache AC(*F); 1407 KnownBits Known = computeKnownBits( 1408 A, M->getDataLayout(), /* Depth */ 0, &AC, F->front().getTerminator()); 1409 EXPECT_EQ(Known.Zero.getZExtValue(), 31u); 1410 EXPECT_EQ(Known.One.getZExtValue(), 0u); 1411 } 1412 1413 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsPtrToIntZext) { 1414 // ptrtoint zero extends the pointer type. 1415 parseAssembly( 1416 "define void @test(i8** %p) {\n" 1417 " %A = load i8*, i8** %p\n" 1418 " %i = ptrtoint i8* %A to i128\n" 1419 " %m = and i128 %i, 31\n" 1420 " %c = icmp eq i128 %m, 0\n" 1421 " call void @llvm.assume(i1 %c)\n" 1422 " ret void\n" 1423 "}\n" 1424 "declare void @llvm.assume(i1)\n"); 1425 AssumptionCache AC(*F); 1426 KnownBits Known = computeKnownBits( 1427 A, M->getDataLayout(), /* Depth */ 0, &AC, F->front().getTerminator()); 1428 EXPECT_EQ(Known.Zero.getZExtValue(), 31u); 1429 EXPECT_EQ(Known.One.getZExtValue(), 0u); 1430 } 1431 1432 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsFreeze) { 1433 parseAssembly("define void @test() {\n" 1434 " %m = call i32 @any_num()\n" 1435 " %A = freeze i32 %m\n" 1436 " %n = and i32 %m, 31\n" 1437 " %c = icmp eq i32 %n, 0\n" 1438 " call void @llvm.assume(i1 %c)\n" 1439 " ret void\n" 1440 "}\n" 1441 "declare void @llvm.assume(i1)\n" 1442 "declare i32 @any_num()\n"); 1443 AssumptionCache AC(*F); 1444 KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1445 F->front().getTerminator()); 1446 EXPECT_EQ(Known.Zero.getZExtValue(), 31u); 1447 EXPECT_EQ(Known.One.getZExtValue(), 0u); 1448 } 1449 1450 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsAddWithRange) { 1451 parseAssembly("define void @test(i64* %p) {\n" 1452 " %A = load i64, i64* %p, !range !{i64 64, i64 65536}\n" 1453 " %APlus512 = add i64 %A, 512\n" 1454 " %c = icmp ugt i64 %APlus512, 523\n" 1455 " call void @llvm.assume(i1 %c)\n" 1456 " ret void\n" 1457 "}\n" 1458 "declare void @llvm.assume(i1)\n"); 1459 AssumptionCache AC(*F); 1460 KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1461 F->front().getTerminator()); 1462 EXPECT_EQ(Known.Zero.getZExtValue(), ~(65536llu - 1)); 1463 EXPECT_EQ(Known.One.getZExtValue(), 0u); 1464 Instruction &APlus512 = findInstructionByName(F, "APlus512"); 1465 Known = computeKnownBits(&APlus512, M->getDataLayout(), /* Depth */ 0, &AC, 1466 F->front().getTerminator()); 1467 // We know of one less zero because 512 may have produced a 1 that 1468 // got carried all the way to the first trailing zero. 1469 EXPECT_EQ(Known.Zero.getZExtValue(), (~(65536llu - 1)) << 1); 1470 EXPECT_EQ(Known.One.getZExtValue(), 0u); 1471 // The known range is not precise given computeKnownBits works 1472 // with the masks of zeros and ones, not the ranges. 1473 EXPECT_EQ(Known.getMinValue(), 0u); 1474 EXPECT_EQ(Known.getMaxValue(), 131071); 1475 } 1476 1477 // 512 + [32, 64) doesn't produce overlapping bits. 1478 // Make sure we get all the individual bits properly. 1479 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsAddWithRangeNoOverlap) { 1480 parseAssembly("define void @test(i64* %p) {\n" 1481 " %A = load i64, i64* %p, !range !{i64 32, i64 64}\n" 1482 " %APlus512 = add i64 %A, 512\n" 1483 " %c = icmp ugt i64 %APlus512, 523\n" 1484 " call void @llvm.assume(i1 %c)\n" 1485 " ret void\n" 1486 "}\n" 1487 "declare void @llvm.assume(i1)\n"); 1488 AssumptionCache AC(*F); 1489 KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1490 F->front().getTerminator()); 1491 EXPECT_EQ(Known.Zero.getZExtValue(), ~(64llu - 1)); 1492 EXPECT_EQ(Known.One.getZExtValue(), 32u); 1493 Instruction &APlus512 = findInstructionByName(F, "APlus512"); 1494 Known = computeKnownBits(&APlus512, M->getDataLayout(), /* Depth */ 0, &AC, 1495 F->front().getTerminator()); 1496 EXPECT_EQ(Known.Zero.getZExtValue(), ~512llu & ~(64llu - 1)); 1497 EXPECT_EQ(Known.One.getZExtValue(), 512u | 32u); 1498 // The known range is not precise given computeKnownBits works 1499 // with the masks of zeros and ones, not the ranges. 1500 EXPECT_EQ(Known.getMinValue(), 544); 1501 EXPECT_EQ(Known.getMaxValue(), 575); 1502 } 1503 1504 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsGEPWithRange) { 1505 parseAssembly( 1506 "define void @test(i64* %p) {\n" 1507 " %A = load i64, i64* %p, !range !{i64 64, i64 65536}\n" 1508 " %APtr = inttoptr i64 %A to float*" 1509 " %APtrPlus512 = getelementptr float, float* %APtr, i32 128\n" 1510 " %c = icmp ugt float* %APtrPlus512, inttoptr (i32 523 to float*)\n" 1511 " call void @llvm.assume(i1 %c)\n" 1512 " ret void\n" 1513 "}\n" 1514 "declare void @llvm.assume(i1)\n"); 1515 AssumptionCache AC(*F); 1516 KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1517 F->front().getTerminator()); 1518 EXPECT_EQ(Known.Zero.getZExtValue(), ~(65536llu - 1)); 1519 EXPECT_EQ(Known.One.getZExtValue(), 0u); 1520 Instruction &APtrPlus512 = findInstructionByName(F, "APtrPlus512"); 1521 Known = computeKnownBits(&APtrPlus512, M->getDataLayout(), /* Depth */ 0, &AC, 1522 F->front().getTerminator()); 1523 // We know of one less zero because 512 may have produced a 1 that 1524 // got carried all the way to the first trailing zero. 1525 EXPECT_EQ(Known.Zero.getZExtValue(), ~(65536llu - 1) << 1); 1526 EXPECT_EQ(Known.One.getZExtValue(), 0u); 1527 // The known range is not precise given computeKnownBits works 1528 // with the masks of zeros and ones, not the ranges. 1529 EXPECT_EQ(Known.getMinValue(), 0u); 1530 EXPECT_EQ(Known.getMaxValue(), 131071); 1531 } 1532 1533 // 4*128 + [32, 64) doesn't produce overlapping bits. 1534 // Make sure we get all the individual bits properly. 1535 // This test is useful to check that we account for the scaling factor 1536 // in the gep. Indeed, gep float, [32,64), 128 is not 128 + [32,64). 1537 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsGEPWithRangeNoOverlap) { 1538 parseAssembly( 1539 "define void @test(i64* %p) {\n" 1540 " %A = load i64, i64* %p, !range !{i64 32, i64 64}\n" 1541 " %APtr = inttoptr i64 %A to float*" 1542 " %APtrPlus512 = getelementptr float, float* %APtr, i32 128\n" 1543 " %c = icmp ugt float* %APtrPlus512, inttoptr (i32 523 to float*)\n" 1544 " call void @llvm.assume(i1 %c)\n" 1545 " ret void\n" 1546 "}\n" 1547 "declare void @llvm.assume(i1)\n"); 1548 AssumptionCache AC(*F); 1549 KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1550 F->front().getTerminator()); 1551 EXPECT_EQ(Known.Zero.getZExtValue(), ~(64llu - 1)); 1552 EXPECT_EQ(Known.One.getZExtValue(), 32u); 1553 Instruction &APtrPlus512 = findInstructionByName(F, "APtrPlus512"); 1554 Known = computeKnownBits(&APtrPlus512, M->getDataLayout(), /* Depth */ 0, &AC, 1555 F->front().getTerminator()); 1556 EXPECT_EQ(Known.Zero.getZExtValue(), ~512llu & ~(64llu - 1)); 1557 EXPECT_EQ(Known.One.getZExtValue(), 512u | 32u); 1558 // The known range is not precise given computeKnownBits works 1559 // with the masks of zeros and ones, not the ranges. 1560 EXPECT_EQ(Known.getMinValue(), 544); 1561 EXPECT_EQ(Known.getMaxValue(), 575); 1562 } 1563 1564 class IsBytewiseValueTest : public ValueTrackingTest, 1565 public ::testing::WithParamInterface< 1566 std::pair<const char *, const char *>> { 1567 protected: 1568 }; 1569 1570 const std::pair<const char *, const char *> IsBytewiseValueTests[] = { 1571 { 1572 "i8 0", 1573 "i48* null", 1574 }, 1575 { 1576 "i8 undef", 1577 "i48* undef", 1578 }, 1579 { 1580 "i8 0", 1581 "i8 zeroinitializer", 1582 }, 1583 { 1584 "i8 0", 1585 "i8 0", 1586 }, 1587 { 1588 "i8 -86", 1589 "i8 -86", 1590 }, 1591 { 1592 "i8 -1", 1593 "i8 -1", 1594 }, 1595 { 1596 "i8 undef", 1597 "i16 undef", 1598 }, 1599 { 1600 "i8 0", 1601 "i16 0", 1602 }, 1603 { 1604 "", 1605 "i16 7", 1606 }, 1607 { 1608 "i8 -86", 1609 "i16 -21846", 1610 }, 1611 { 1612 "i8 -1", 1613 "i16 -1", 1614 }, 1615 { 1616 "i8 0", 1617 "i48 0", 1618 }, 1619 { 1620 "i8 -1", 1621 "i48 -1", 1622 }, 1623 { 1624 "i8 0", 1625 "i49 0", 1626 }, 1627 { 1628 "", 1629 "i49 -1", 1630 }, 1631 { 1632 "i8 0", 1633 "half 0xH0000", 1634 }, 1635 { 1636 "i8 -85", 1637 "half 0xHABAB", 1638 }, 1639 { 1640 "i8 0", 1641 "float 0.0", 1642 }, 1643 { 1644 "i8 -1", 1645 "float 0xFFFFFFFFE0000000", 1646 }, 1647 { 1648 "i8 0", 1649 "double 0.0", 1650 }, 1651 { 1652 "i8 -15", 1653 "double 0xF1F1F1F1F1F1F1F1", 1654 }, 1655 { 1656 "i8 undef", 1657 "i16* undef", 1658 }, 1659 { 1660 "i8 0", 1661 "i16* inttoptr (i64 0 to i16*)", 1662 }, 1663 { 1664 "i8 -1", 1665 "i16* inttoptr (i64 -1 to i16*)", 1666 }, 1667 { 1668 "i8 -86", 1669 "i16* inttoptr (i64 -6148914691236517206 to i16*)", 1670 }, 1671 { 1672 "", 1673 "i16* inttoptr (i48 -1 to i16*)", 1674 }, 1675 { 1676 "i8 -1", 1677 "i16* inttoptr (i96 -1 to i16*)", 1678 }, 1679 { 1680 "i8 undef", 1681 "[0 x i8] zeroinitializer", 1682 }, 1683 { 1684 "i8 undef", 1685 "[0 x i8] undef", 1686 }, 1687 { 1688 "i8 undef", 1689 "[5 x [0 x i8]] zeroinitializer", 1690 }, 1691 { 1692 "i8 undef", 1693 "[5 x [0 x i8]] undef", 1694 }, 1695 { 1696 "i8 0", 1697 "[6 x i8] zeroinitializer", 1698 }, 1699 { 1700 "i8 undef", 1701 "[6 x i8] undef", 1702 }, 1703 { 1704 "i8 1", 1705 "[5 x i8] [i8 1, i8 1, i8 1, i8 1, i8 1]", 1706 }, 1707 { 1708 "", 1709 "[5 x i64] [i64 1, i64 1, i64 1, i64 1, i64 1]", 1710 }, 1711 { 1712 "i8 -1", 1713 "[5 x i64] [i64 -1, i64 -1, i64 -1, i64 -1, i64 -1]", 1714 }, 1715 { 1716 "", 1717 "[4 x i8] [i8 1, i8 2, i8 1, i8 1]", 1718 }, 1719 { 1720 "i8 1", 1721 "[4 x i8] [i8 1, i8 undef, i8 1, i8 1]", 1722 }, 1723 { 1724 "i8 0", 1725 "<6 x i8> zeroinitializer", 1726 }, 1727 { 1728 "i8 undef", 1729 "<6 x i8> undef", 1730 }, 1731 { 1732 "i8 1", 1733 "<5 x i8> <i8 1, i8 1, i8 1, i8 1, i8 1>", 1734 }, 1735 { 1736 "", 1737 "<5 x i64> <i64 1, i64 1, i64 1, i64 1, i64 1>", 1738 }, 1739 { 1740 "i8 -1", 1741 "<5 x i64> <i64 -1, i64 -1, i64 -1, i64 -1, i64 -1>", 1742 }, 1743 { 1744 "", 1745 "<4 x i8> <i8 1, i8 1, i8 2, i8 1>", 1746 }, 1747 { 1748 "i8 5", 1749 "<2 x i8> < i8 5, i8 undef >", 1750 }, 1751 { 1752 "i8 0", 1753 "[2 x [2 x i16]] zeroinitializer", 1754 }, 1755 { 1756 "i8 undef", 1757 "[2 x [2 x i16]] undef", 1758 }, 1759 { 1760 "i8 -86", 1761 "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], " 1762 "[2 x i16] [i16 -21846, i16 -21846]]", 1763 }, 1764 { 1765 "", 1766 "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], " 1767 "[2 x i16] [i16 -21836, i16 -21846]]", 1768 }, 1769 { 1770 "i8 undef", 1771 "{ } zeroinitializer", 1772 }, 1773 { 1774 "i8 undef", 1775 "{ } undef", 1776 }, 1777 { 1778 "i8 undef", 1779 "{ {}, {} } zeroinitializer", 1780 }, 1781 { 1782 "i8 undef", 1783 "{ {}, {} } undef", 1784 }, 1785 { 1786 "i8 0", 1787 "{i8, i64, i16*} zeroinitializer", 1788 }, 1789 { 1790 "i8 undef", 1791 "{i8, i64, i16*} undef", 1792 }, 1793 { 1794 "i8 -86", 1795 "{i8, i64, i16*} {i8 -86, i64 -6148914691236517206, i16* undef}", 1796 }, 1797 { 1798 "", 1799 "{i8, i64, i16*} {i8 86, i64 -6148914691236517206, i16* undef}", 1800 }, 1801 }; 1802 1803 INSTANTIATE_TEST_CASE_P(IsBytewiseValueParamTests, IsBytewiseValueTest, 1804 ::testing::ValuesIn(IsBytewiseValueTests),); 1805 1806 TEST_P(IsBytewiseValueTest, IsBytewiseValue) { 1807 auto M = parseModule(std::string("@test = global ") + GetParam().second); 1808 GlobalVariable *GV = dyn_cast<GlobalVariable>(M->getNamedValue("test")); 1809 Value *Actual = isBytewiseValue(GV->getInitializer(), M->getDataLayout()); 1810 std::string Buff; 1811 raw_string_ostream S(Buff); 1812 if (Actual) 1813 S << *Actual; 1814 EXPECT_EQ(GetParam().first, S.str()); 1815 } 1816 1817 TEST_F(ValueTrackingTest, ComputeConstantRange) { 1818 { 1819 // Assumptions: 1820 // * stride >= 5 1821 // * stride < 10 1822 // 1823 // stride = [5, 10) 1824 auto M = parseModule(R"( 1825 declare void @llvm.assume(i1) 1826 1827 define i32 @test(i32 %stride) { 1828 %gt = icmp uge i32 %stride, 5 1829 call void @llvm.assume(i1 %gt) 1830 %lt = icmp ult i32 %stride, 10 1831 call void @llvm.assume(i1 %lt) 1832 %stride.plus.one = add nsw nuw i32 %stride, 1 1833 ret i32 %stride.plus.one 1834 })"); 1835 Function *F = M->getFunction("test"); 1836 1837 AssumptionCache AC(*F); 1838 Value *Stride = &*F->arg_begin(); 1839 ConstantRange CR1 = computeConstantRange(Stride, true, &AC, nullptr); 1840 EXPECT_TRUE(CR1.isFullSet()); 1841 1842 Instruction *I = &findInstructionByName(F, "stride.plus.one"); 1843 ConstantRange CR2 = computeConstantRange(Stride, true, &AC, I); 1844 EXPECT_EQ(5, CR2.getLower()); 1845 EXPECT_EQ(10, CR2.getUpper()); 1846 } 1847 1848 { 1849 // Assumptions: 1850 // * stride >= 5 1851 // * stride < 200 1852 // * stride == 99 1853 // 1854 // stride = [99, 100) 1855 auto M = parseModule(R"( 1856 declare void @llvm.assume(i1) 1857 1858 define i32 @test(i32 %stride) { 1859 %gt = icmp uge i32 %stride, 5 1860 call void @llvm.assume(i1 %gt) 1861 %lt = icmp ult i32 %stride, 200 1862 call void @llvm.assume(i1 %lt) 1863 %eq = icmp eq i32 %stride, 99 1864 call void @llvm.assume(i1 %eq) 1865 %stride.plus.one = add nsw nuw i32 %stride, 1 1866 ret i32 %stride.plus.one 1867 })"); 1868 Function *F = M->getFunction("test"); 1869 1870 AssumptionCache AC(*F); 1871 Value *Stride = &*F->arg_begin(); 1872 Instruction *I = &findInstructionByName(F, "stride.plus.one"); 1873 ConstantRange CR = computeConstantRange(Stride, true, &AC, I); 1874 EXPECT_EQ(99, *CR.getSingleElement()); 1875 } 1876 1877 { 1878 // Assumptions: 1879 // * stride >= 5 1880 // * stride >= 50 1881 // * stride < 100 1882 // * stride < 200 1883 // 1884 // stride = [50, 100) 1885 auto M = parseModule(R"( 1886 declare void @llvm.assume(i1) 1887 1888 define i32 @test(i32 %stride, i1 %cond) { 1889 %gt = icmp uge i32 %stride, 5 1890 call void @llvm.assume(i1 %gt) 1891 %gt.2 = icmp uge i32 %stride, 50 1892 call void @llvm.assume(i1 %gt.2) 1893 br i1 %cond, label %bb1, label %bb2 1894 1895 bb1: 1896 %lt = icmp ult i32 %stride, 200 1897 call void @llvm.assume(i1 %lt) 1898 %lt.2 = icmp ult i32 %stride, 100 1899 call void @llvm.assume(i1 %lt.2) 1900 %stride.plus.one = add nsw nuw i32 %stride, 1 1901 ret i32 %stride.plus.one 1902 1903 bb2: 1904 ret i32 0 1905 })"); 1906 Function *F = M->getFunction("test"); 1907 1908 AssumptionCache AC(*F); 1909 Value *Stride = &*F->arg_begin(); 1910 Instruction *GT2 = &findInstructionByName(F, "gt.2"); 1911 ConstantRange CR = computeConstantRange(Stride, true, &AC, GT2); 1912 EXPECT_EQ(5, CR.getLower()); 1913 EXPECT_EQ(0, CR.getUpper()); 1914 1915 Instruction *I = &findInstructionByName(F, "stride.plus.one"); 1916 ConstantRange CR2 = computeConstantRange(Stride, true, &AC, I); 1917 EXPECT_EQ(50, CR2.getLower()); 1918 EXPECT_EQ(100, CR2.getUpper()); 1919 } 1920 1921 { 1922 // Assumptions: 1923 // * stride > 5 1924 // * stride < 5 1925 // 1926 // stride = empty range, as the assumptions contradict each other. 1927 auto M = parseModule(R"( 1928 declare void @llvm.assume(i1) 1929 1930 define i32 @test(i32 %stride, i1 %cond) { 1931 %gt = icmp ugt i32 %stride, 5 1932 call void @llvm.assume(i1 %gt) 1933 %lt = icmp ult i32 %stride, 5 1934 call void @llvm.assume(i1 %lt) 1935 %stride.plus.one = add nsw nuw i32 %stride, 1 1936 ret i32 %stride.plus.one 1937 })"); 1938 Function *F = M->getFunction("test"); 1939 1940 AssumptionCache AC(*F); 1941 Value *Stride = &*F->arg_begin(); 1942 1943 Instruction *I = &findInstructionByName(F, "stride.plus.one"); 1944 ConstantRange CR = computeConstantRange(Stride, true, &AC, I); 1945 EXPECT_TRUE(CR.isEmptySet()); 1946 } 1947 1948 { 1949 // Assumptions: 1950 // * x.1 >= 5 1951 // * x.2 < x.1 1952 // 1953 // stride = [0, 5) 1954 auto M = parseModule(R"( 1955 declare void @llvm.assume(i1) 1956 1957 define i32 @test(i32 %x.1, i32 %x.2) { 1958 %gt = icmp uge i32 %x.1, 5 1959 call void @llvm.assume(i1 %gt) 1960 %lt = icmp ult i32 %x.2, %x.1 1961 call void @llvm.assume(i1 %lt) 1962 %stride.plus.one = add nsw nuw i32 %x.1, 1 1963 ret i32 %stride.plus.one 1964 })"); 1965 Function *F = M->getFunction("test"); 1966 1967 AssumptionCache AC(*F); 1968 Value *X2 = &*std::next(F->arg_begin()); 1969 1970 Instruction *I = &findInstructionByName(F, "stride.plus.one"); 1971 ConstantRange CR1 = computeConstantRange(X2, true, &AC, I); 1972 EXPECT_EQ(0, CR1.getLower()); 1973 EXPECT_EQ(5, CR1.getUpper()); 1974 1975 // Check the depth cutoff results in a conservative result (full set) by 1976 // passing Depth == MaxDepth == 6. 1977 ConstantRange CR2 = computeConstantRange(X2, true, &AC, I, 6); 1978 EXPECT_TRUE(CR2.isFullSet()); 1979 } 1980 } 1981 1982 struct FindAllocaForValueTestParams { 1983 const char *IR; 1984 bool AnyOffsetResult; 1985 bool ZeroOffsetResult; 1986 }; 1987 1988 class FindAllocaForValueTest 1989 : public ValueTrackingTest, 1990 public ::testing::WithParamInterface<FindAllocaForValueTestParams> { 1991 protected: 1992 }; 1993 1994 const FindAllocaForValueTestParams FindAllocaForValueTests[] = { 1995 {R"( 1996 define void @test() { 1997 %a = alloca i64 1998 %r = bitcast i64* %a to i32* 1999 ret void 2000 })", 2001 true, true}, 2002 2003 {R"( 2004 define void @test() { 2005 %a = alloca i32 2006 %r = getelementptr i32, i32* %a, i32 1 2007 ret void 2008 })", 2009 true, false}, 2010 2011 {R"( 2012 define void @test() { 2013 %a = alloca i32 2014 %r = getelementptr i32, i32* %a, i32 0 2015 ret void 2016 })", 2017 true, true}, 2018 2019 {R"( 2020 define void @test(i1 %cond) { 2021 entry: 2022 %a = alloca i32 2023 br label %bb1 2024 2025 bb1: 2026 %r = phi i32* [ %a, %entry ], [ %r, %bb1 ] 2027 br i1 %cond, label %bb1, label %exit 2028 2029 exit: 2030 ret void 2031 })", 2032 true, true}, 2033 2034 {R"( 2035 define void @test(i1 %cond) { 2036 %a = alloca i32 2037 %r = select i1 %cond, i32* %a, i32* %a 2038 ret void 2039 })", 2040 true, true}, 2041 2042 {R"( 2043 define void @test(i1 %cond) { 2044 %a = alloca i32 2045 %b = alloca i32 2046 %r = select i1 %cond, i32* %a, i32* %b 2047 ret void 2048 })", 2049 false, false}, 2050 2051 {R"( 2052 define void @test(i1 %cond) { 2053 entry: 2054 %a = alloca i64 2055 %a32 = bitcast i64* %a to i32* 2056 br label %bb1 2057 2058 bb1: 2059 %x = phi i32* [ %a32, %entry ], [ %x, %bb1 ] 2060 %r = getelementptr i32, i32* %x, i32 1 2061 br i1 %cond, label %bb1, label %exit 2062 2063 exit: 2064 ret void 2065 })", 2066 true, false}, 2067 2068 {R"( 2069 define void @test(i1 %cond) { 2070 entry: 2071 %a = alloca i64 2072 %a32 = bitcast i64* %a to i32* 2073 br label %bb1 2074 2075 bb1: 2076 %x = phi i32* [ %a32, %entry ], [ %r, %bb1 ] 2077 %r = getelementptr i32, i32* %x, i32 1 2078 br i1 %cond, label %bb1, label %exit 2079 2080 exit: 2081 ret void 2082 })", 2083 true, false}, 2084 2085 {R"( 2086 define void @test(i1 %cond, i64* %a) { 2087 entry: 2088 %r = bitcast i64* %a to i32* 2089 ret void 2090 })", 2091 false, false}, 2092 2093 {R"( 2094 define void @test(i1 %cond) { 2095 entry: 2096 %a = alloca i32 2097 %b = alloca i32 2098 br label %bb1 2099 2100 bb1: 2101 %r = phi i32* [ %a, %entry ], [ %b, %bb1 ] 2102 br i1 %cond, label %bb1, label %exit 2103 2104 exit: 2105 ret void 2106 })", 2107 false, false}, 2108 }; 2109 2110 TEST_P(FindAllocaForValueTest, findAllocaForValue) { 2111 auto M = parseModule(GetParam().IR); 2112 Function *F = M->getFunction("test"); 2113 Instruction *I = &findInstructionByName(F, "r"); 2114 const AllocaInst *AI = findAllocaForValue(I); 2115 EXPECT_EQ(!!AI, GetParam().AnyOffsetResult); 2116 } 2117 2118 TEST_P(FindAllocaForValueTest, findAllocaForValueZeroOffset) { 2119 auto M = parseModule(GetParam().IR); 2120 Function *F = M->getFunction("test"); 2121 Instruction *I = &findInstructionByName(F, "r"); 2122 const AllocaInst *AI = findAllocaForValue(I, true); 2123 EXPECT_EQ(!!AI, GetParam().ZeroOffsetResult); 2124 } 2125 2126 INSTANTIATE_TEST_CASE_P(FindAllocaForValueTest, FindAllocaForValueTest, 2127 ::testing::ValuesIn(FindAllocaForValueTests), ); 2128