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 @nounwind_willreturn(i32*) nounwind willreturn " 618 "declare void @throws_but_readonly(i32*) readonly " 619 "declare void @throws_but_argmemonly(i32*) argmemonly " 620 "declare void @throws_but_willreturn(i32*) willreturn " 621 " " 622 "declare void @unknown(i32*) " 623 " " 624 "define void @f(i32* %p) { " 625 " call void @nounwind_readonly(i32* %p) " 626 " call void @nounwind_argmemonly(i32* %p) " 627 " call void @nounwind_willreturn(i32* %p)" 628 " call void @throws_but_readonly(i32* %p) " 629 " call void @throws_but_argmemonly(i32* %p) " 630 " call void @throws_but_willreturn(i32* %p) " 631 " call void @unknown(i32* %p) nounwind readonly " 632 " call void @unknown(i32* %p) nounwind argmemonly " 633 " call void @unknown(i32* %p) nounwind willreturn " 634 " call void @unknown(i32* %p) readonly " 635 " call void @unknown(i32* %p) argmemonly " 636 " call void @unknown(i32* %p) willreturn " 637 " ret void " 638 "} "; 639 640 LLVMContext Context; 641 SMDiagnostic Error; 642 auto M = parseAssemblyString(Assembly, Error, Context); 643 assert(M && "Bad assembly?"); 644 645 auto *F = M->getFunction("f"); 646 assert(F && "Bad assembly?"); 647 648 auto &BB = F->getEntryBlock(); 649 bool ExpectedAnswers[] = { 650 false, // call void @nounwind_readonly(i32* %p) 651 false, // call void @nounwind_argmemonly(i32* %p) 652 true, // call void @nounwind_willreturn(i32* %p) 653 false, // call void @throws_but_readonly(i32* %p) 654 false, // call void @throws_but_argmemonly(i32* %p) 655 false, // call void @throws_but_willreturn(i32* %p) 656 false, // call void @unknown(i32* %p) nounwind readonly 657 false, // call void @unknown(i32* %p) nounwind argmemonly 658 true, // call void @unknown(i32* %p) nounwind willreturn 659 false, // call void @unknown(i32* %p) readonly 660 false, // call void @unknown(i32* %p) argmemonly 661 false, // call void @unknown(i32* %p) willreturn 662 false, // ret void 663 }; 664 665 int Index = 0; 666 for (auto &I : BB) { 667 EXPECT_EQ(isGuaranteedToTransferExecutionToSuccessor(&I), 668 ExpectedAnswers[Index]) 669 << "Incorrect answer at instruction " << Index << " = " << I; 670 Index++; 671 } 672 } 673 674 TEST_F(ValueTrackingTest, ComputeNumSignBits_PR32045) { 675 parseAssembly( 676 "define i32 @test(i32 %a) {\n" 677 " %A = ashr i32 %a, -1\n" 678 " ret i32 %A\n" 679 "}\n"); 680 EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u); 681 } 682 683 // No guarantees for canonical IR in this analysis, so this just bails out. 684 TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle) { 685 parseAssembly( 686 "define <2 x i32> @test() {\n" 687 " %A = shufflevector <2 x i32> undef, <2 x i32> undef, <2 x i32> <i32 0, i32 0>\n" 688 " ret <2 x i32> %A\n" 689 "}\n"); 690 EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u); 691 } 692 693 // No guarantees for canonical IR in this analysis, so a shuffle element that 694 // references an undef value means this can't return any extra information. 695 TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle2) { 696 parseAssembly( 697 "define <2 x i32> @test(<2 x i1> %x) {\n" 698 " %sext = sext <2 x i1> %x to <2 x i32>\n" 699 " %A = shufflevector <2 x i32> %sext, <2 x i32> undef, <2 x i32> <i32 0, i32 2>\n" 700 " ret <2 x i32> %A\n" 701 "}\n"); 702 EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u); 703 } 704 705 TEST_F(ValueTrackingTest, impliesPoisonTest_Identity) { 706 parseAssembly("define void @test(i32 %x, i32 %y) {\n" 707 " %A = add i32 %x, %y\n" 708 " ret void\n" 709 "}"); 710 EXPECT_TRUE(impliesPoison(A, A)); 711 } 712 713 TEST_F(ValueTrackingTest, impliesPoisonTest_ICmp) { 714 parseAssembly("define void @test(i32 %x) {\n" 715 " %A2 = icmp eq i32 %x, 0\n" 716 " %A = icmp eq i32 %x, 1\n" 717 " ret void\n" 718 "}"); 719 EXPECT_TRUE(impliesPoison(A2, A)); 720 } 721 722 TEST_F(ValueTrackingTest, impliesPoisonTest_ICmpUnknown) { 723 parseAssembly("define void @test(i32 %x, i32 %y) {\n" 724 " %A2 = icmp eq i32 %x, %y\n" 725 " %A = icmp eq i32 %x, 1\n" 726 " ret void\n" 727 "}"); 728 EXPECT_FALSE(impliesPoison(A2, A)); 729 } 730 731 TEST_F(ValueTrackingTest, impliesPoisonTest_AddNswOkay) { 732 parseAssembly("define void @test(i32 %x) {\n" 733 " %A2 = add nsw i32 %x, 1\n" 734 " %A = add i32 %A2, 1\n" 735 " ret void\n" 736 "}"); 737 EXPECT_TRUE(impliesPoison(A2, A)); 738 } 739 740 TEST_F(ValueTrackingTest, impliesPoisonTest_AddNswOkay2) { 741 parseAssembly("define void @test(i32 %x) {\n" 742 " %A2 = add i32 %x, 1\n" 743 " %A = add nsw i32 %A2, 1\n" 744 " ret void\n" 745 "}"); 746 EXPECT_TRUE(impliesPoison(A2, A)); 747 } 748 749 TEST_F(ValueTrackingTest, impliesPoisonTest_AddNsw) { 750 parseAssembly("define void @test(i32 %x) {\n" 751 " %A2 = add nsw i32 %x, 1\n" 752 " %A = add i32 %x, 1\n" 753 " ret void\n" 754 "}"); 755 EXPECT_FALSE(impliesPoison(A2, A)); 756 } 757 758 TEST_F(ValueTrackingTest, impliesPoisonTest_Cmp) { 759 parseAssembly("define void @test(i32 %x, i32 %y, i1 %c) {\n" 760 " %A2 = icmp eq i32 %x, %y\n" 761 " %A0 = icmp ult i32 %x, %y\n" 762 " %A = or i1 %A0, %c\n" 763 " ret void\n" 764 "}"); 765 EXPECT_TRUE(impliesPoison(A2, A)); 766 } 767 768 TEST_F(ValueTrackingTest, impliesPoisonTest_FCmpFMF) { 769 parseAssembly("define void @test(float %x, float %y, i1 %c) {\n" 770 " %A2 = fcmp nnan oeq float %x, %y\n" 771 " %A0 = fcmp olt float %x, %y\n" 772 " %A = or i1 %A0, %c\n" 773 " ret void\n" 774 "}"); 775 EXPECT_FALSE(impliesPoison(A2, A)); 776 } 777 778 TEST_F(ValueTrackingTest, impliesPoisonTest_AddSubSameOps) { 779 parseAssembly("define void @test(i32 %x, i32 %y, i1 %c) {\n" 780 " %A2 = add i32 %x, %y\n" 781 " %A = sub i32 %x, %y\n" 782 " ret void\n" 783 "}"); 784 EXPECT_TRUE(impliesPoison(A2, A)); 785 } 786 787 TEST_F(ValueTrackingTest, impliesPoisonTest_MaskCmp) { 788 parseAssembly("define void @test(i32 %x, i32 %y, i1 %c) {\n" 789 " %M2 = and i32 %x, 7\n" 790 " %A2 = icmp eq i32 %M2, 1\n" 791 " %M = and i32 %x, 15\n" 792 " %A = icmp eq i32 %M, 3\n" 793 " ret void\n" 794 "}"); 795 EXPECT_TRUE(impliesPoison(A2, A)); 796 } 797 798 TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle_Pointers) { 799 parseAssembly( 800 "define <2 x i32*> @test(<2 x i32*> %x) {\n" 801 " %A = shufflevector <2 x i32*> zeroinitializer, <2 x i32*> undef, <2 x i32> zeroinitializer\n" 802 " ret <2 x i32*> %A\n" 803 "}\n"); 804 EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 64u); 805 } 806 807 TEST(ValueTracking, propagatesPoison) { 808 std::string AsmHead = "declare i32 @g(i32)\n" 809 "define void @f(i32 %x, i32 %y, float %fx, float %fy, " 810 "i1 %cond, i8* %p) {\n"; 811 std::string AsmTail = " ret void\n}"; 812 // (propagates poison?, IR instruction) 813 SmallVector<std::pair<bool, std::string>, 32> Data = { 814 {true, "add i32 %x, %y"}, 815 {true, "add nsw nuw i32 %x, %y"}, 816 {true, "ashr i32 %x, %y"}, 817 {true, "lshr exact i32 %x, 31"}, 818 {true, "fcmp oeq float %fx, %fy"}, 819 {true, "icmp eq i32 %x, %y"}, 820 {true, "getelementptr i8, i8* %p, i32 %x"}, 821 {true, "getelementptr inbounds i8, i8* %p, i32 %x"}, 822 {true, "bitcast float %fx to i32"}, 823 {false, "select i1 %cond, i32 %x, i32 %y"}, 824 {false, "freeze i32 %x"}, 825 {true, "udiv i32 %x, %y"}, 826 {true, "urem i32 %x, %y"}, 827 {true, "sdiv exact i32 %x, %y"}, 828 {true, "srem i32 %x, %y"}, 829 {false, "call i32 @g(i32 %x)"}}; 830 831 std::string AssemblyStr = AsmHead; 832 for (auto &Itm : Data) 833 AssemblyStr += Itm.second + "\n"; 834 AssemblyStr += AsmTail; 835 836 LLVMContext Context; 837 SMDiagnostic Error; 838 auto M = parseAssemblyString(AssemblyStr, Error, Context); 839 assert(M && "Bad assembly?"); 840 841 auto *F = M->getFunction("f"); 842 assert(F && "Bad assembly?"); 843 844 auto &BB = F->getEntryBlock(); 845 846 int Index = 0; 847 for (auto &I : BB) { 848 if (isa<ReturnInst>(&I)) 849 break; 850 EXPECT_EQ(propagatesPoison(cast<Operator>(&I)), Data[Index].first) 851 << "Incorrect answer at instruction " << Index << " = " << I; 852 Index++; 853 } 854 } 855 856 TEST_F(ValueTrackingTest, programUndefinedIfPoison) { 857 parseAssembly("declare i32 @any_num()" 858 "define void @test(i32 %mask) {\n" 859 " %A = call i32 @any_num()\n" 860 " %B = or i32 %A, %mask\n" 861 " udiv i32 1, %B" 862 " ret void\n" 863 "}\n"); 864 // If %A was poison, udiv raises UB regardless of %mask's value 865 EXPECT_EQ(programUndefinedIfPoison(A), true); 866 } 867 868 TEST_F(ValueTrackingTest, programUndefinedIfUndefOrPoison) { 869 parseAssembly("declare i32 @any_num()" 870 "define void @test(i32 %mask) {\n" 871 " %A = call i32 @any_num()\n" 872 " %B = or i32 %A, %mask\n" 873 " udiv i32 1, %B" 874 " ret void\n" 875 "}\n"); 876 // If %A was undef and %mask was 1, udiv does not raise UB 877 EXPECT_EQ(programUndefinedIfUndefOrPoison(A), false); 878 } 879 880 TEST_F(ValueTrackingTest, isGuaranteedNotToBePoison_exploitBranchCond) { 881 parseAssembly("declare i1 @any_bool()" 882 "define void @test(i1 %y) {\n" 883 " %A = call i1 @any_bool()\n" 884 " %cond = and i1 %A, %y\n" 885 " br i1 %cond, label %BB1, label %BB2\n" 886 "BB1:\n" 887 " ret void\n" 888 "BB2:\n" 889 " ret void\n" 890 "}\n"); 891 DominatorTree DT(*F); 892 for (auto &BB : *F) { 893 if (&BB == &F->getEntryBlock()) 894 continue; 895 896 EXPECT_EQ(isGuaranteedNotToBePoison(A, nullptr, BB.getTerminator(), &DT), 897 true) 898 << "isGuaranteedNotToBePoison does not hold at " << *BB.getTerminator(); 899 } 900 } 901 902 TEST_F(ValueTrackingTest, isGuaranteedNotToBePoison_phi) { 903 parseAssembly("declare i32 @any_i32(i32)" 904 "define void @test() {\n" 905 "ENTRY:\n" 906 " br label %LOOP\n" 907 "LOOP:\n" 908 " %A = phi i32 [0, %ENTRY], [%A.next, %NEXT]\n" 909 " %A.next = call i32 @any_i32(i32 %A)\n" 910 " %cond = icmp eq i32 %A.next, 0\n" 911 " br i1 %cond, label %NEXT, label %EXIT\n" 912 "NEXT:\n" 913 " br label %LOOP\n" 914 "EXIT:\n" 915 " ret void\n" 916 "}\n"); 917 DominatorTree DT(*F); 918 for (auto &BB : *F) { 919 if (BB.getName() == "LOOP") { 920 EXPECT_EQ(isGuaranteedNotToBePoison(A, nullptr, A, &DT), true) 921 << "isGuaranteedNotToBePoison does not hold"; 922 } 923 } 924 } 925 926 TEST_F(ValueTrackingTest, isGuaranteedNotToBeUndefOrPoison) { 927 parseAssembly("declare void @f(i32 noundef)" 928 "define void @test(i32 %x) {\n" 929 " %A = bitcast i32 %x to i32\n" 930 " call void @f(i32 noundef %x)\n" 931 " ret void\n" 932 "}\n"); 933 EXPECT_EQ(isGuaranteedNotToBeUndefOrPoison(A), true); 934 EXPECT_EQ(isGuaranteedNotToBeUndefOrPoison(UndefValue::get(IntegerType::get(Context, 8))), false); 935 EXPECT_EQ(isGuaranteedNotToBeUndefOrPoison(PoisonValue::get(IntegerType::get(Context, 8))), false); 936 EXPECT_EQ(isGuaranteedNotToBePoison(UndefValue::get(IntegerType::get(Context, 8))), true); 937 EXPECT_EQ(isGuaranteedNotToBePoison(PoisonValue::get(IntegerType::get(Context, 8))), false); 938 939 Type *Int32Ty = Type::getInt32Ty(Context); 940 Constant *CU = UndefValue::get(Int32Ty); 941 Constant *CP = PoisonValue::get(Int32Ty); 942 Constant *C1 = ConstantInt::get(Int32Ty, 1); 943 Constant *C2 = ConstantInt::get(Int32Ty, 2); 944 945 { 946 Constant *V1 = ConstantVector::get({C1, C2}); 947 EXPECT_TRUE(isGuaranteedNotToBeUndefOrPoison(V1)); 948 EXPECT_TRUE(isGuaranteedNotToBePoison(V1)); 949 } 950 951 { 952 Constant *V2 = ConstantVector::get({C1, CU}); 953 EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(V2)); 954 EXPECT_TRUE(isGuaranteedNotToBePoison(V2)); 955 } 956 957 { 958 Constant *V3 = ConstantVector::get({C1, CP}); 959 EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(V3)); 960 EXPECT_FALSE(isGuaranteedNotToBePoison(V3)); 961 } 962 } 963 964 TEST_F(ValueTrackingTest, isGuaranteedNotToBeUndefOrPoison_assume) { 965 parseAssembly("declare i1 @f_i1()\n" 966 "declare i32 @f_i32()\n" 967 "declare void @llvm.assume(i1)\n" 968 "define void @test() {\n" 969 " %A = call i32 @f_i32()\n" 970 " %cond = call i1 @f_i1()\n" 971 " %CxtI = add i32 0, 0\n" 972 " br i1 %cond, label %BB1, label %EXIT\n" 973 "BB1:\n" 974 " %CxtI2 = add i32 0, 0\n" 975 " %cond2 = call i1 @f_i1()\n" 976 " call void @llvm.assume(i1 true) [ \"noundef\"(i32 %A) ]\n" 977 " br i1 %cond2, label %BB2, label %EXIT\n" 978 "BB2:\n" 979 " %CxtI3 = add i32 0, 0\n" 980 " ret void\n" 981 "EXIT:\n" 982 " ret void\n" 983 "}"); 984 AssumptionCache AC(*F); 985 DominatorTree DT(*F); 986 EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(A, &AC, CxtI, &DT)); 987 EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(A, &AC, CxtI2, &DT)); 988 EXPECT_TRUE(isGuaranteedNotToBeUndefOrPoison(A, &AC, CxtI3, &DT)); 989 } 990 991 TEST(ValueTracking, canCreatePoisonOrUndef) { 992 std::string AsmHead = 993 "@s = external dso_local global i32, align 1\n" 994 "declare i32 @g(i32)\n" 995 "define void @f(i32 %x, i32 %y, float %fx, float %fy, i1 %cond, " 996 "<4 x i32> %vx, <4 x i32> %vx2, <vscale x 4 x i32> %svx, i8* %p) {\n"; 997 std::string AsmTail = " ret void\n}"; 998 // (can create poison?, can create undef?, IR instruction) 999 SmallVector<std::pair<std::pair<bool, bool>, std::string>, 32> Data = { 1000 {{false, false}, "add i32 %x, %y"}, 1001 {{true, false}, "add nsw nuw i32 %x, %y"}, 1002 {{true, false}, "shl i32 %x, %y"}, 1003 {{true, false}, "shl <4 x i32> %vx, %vx2"}, 1004 {{true, false}, "shl nsw i32 %x, %y"}, 1005 {{true, false}, "shl nsw <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"}, 1006 {{false, false}, "shl i32 %x, 31"}, 1007 {{true, false}, "shl i32 %x, 32"}, 1008 {{false, false}, "shl <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"}, 1009 {{true, false}, "shl <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 32>"}, 1010 {{true, false}, "ashr i32 %x, %y"}, 1011 {{true, false}, "ashr exact i32 %x, %y"}, 1012 {{false, false}, "ashr i32 %x, 31"}, 1013 {{true, false}, "ashr exact i32 %x, 31"}, 1014 {{false, false}, "ashr <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"}, 1015 {{true, false}, "ashr <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 32>"}, 1016 {{true, false}, "ashr exact <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"}, 1017 {{true, false}, "lshr i32 %x, %y"}, 1018 {{true, false}, "lshr exact i32 %x, 31"}, 1019 {{false, false}, "udiv i32 %x, %y"}, 1020 {{true, false}, "udiv exact i32 %x, %y"}, 1021 {{false, false}, "getelementptr i8, i8* %p, i32 %x"}, 1022 {{true, false}, "getelementptr inbounds i8, i8* %p, i32 %x"}, 1023 {{true, false}, "fneg nnan float %fx"}, 1024 {{false, false}, "fneg float %fx"}, 1025 {{false, false}, "fadd float %fx, %fy"}, 1026 {{true, false}, "fadd nnan float %fx, %fy"}, 1027 {{false, false}, "urem i32 %x, %y"}, 1028 {{true, false}, "fptoui float %fx to i32"}, 1029 {{true, false}, "fptosi float %fx to i32"}, 1030 {{false, false}, "bitcast float %fx to i32"}, 1031 {{false, false}, "select i1 %cond, i32 %x, i32 %y"}, 1032 {{true, false}, "select nnan i1 %cond, float %fx, float %fy"}, 1033 {{true, false}, "extractelement <4 x i32> %vx, i32 %x"}, 1034 {{false, false}, "extractelement <4 x i32> %vx, i32 3"}, 1035 {{true, false}, "extractelement <vscale x 4 x i32> %svx, i32 4"}, 1036 {{true, false}, "insertelement <4 x i32> %vx, i32 %x, i32 %y"}, 1037 {{false, false}, "insertelement <4 x i32> %vx, i32 %x, i32 3"}, 1038 {{true, false}, "insertelement <vscale x 4 x i32> %svx, i32 %x, i32 4"}, 1039 {{false, false}, "freeze i32 %x"}, 1040 {{false, false}, 1041 "shufflevector <4 x i32> %vx, <4 x i32> %vx2, " 1042 "<4 x i32> <i32 0, i32 1, i32 2, i32 3>"}, 1043 {{false, true}, 1044 "shufflevector <4 x i32> %vx, <4 x i32> %vx2, " 1045 "<4 x i32> <i32 0, i32 1, i32 2, i32 undef>"}, 1046 {{false, true}, 1047 "shufflevector <vscale x 4 x i32> %svx, " 1048 "<vscale x 4 x i32> %svx, <vscale x 4 x i32> undef"}, 1049 {{true, false}, "call i32 @g(i32 %x)"}, 1050 {{false, false}, "call noundef i32 @g(i32 %x)"}, 1051 {{true, false}, "fcmp nnan oeq float %fx, %fy"}, 1052 {{false, false}, "fcmp oeq float %fx, %fy"}, 1053 {{true, false}, 1054 "ashr <4 x i32> %vx, select (i1 icmp sgt (i32 ptrtoint (i32* @s to " 1055 "i32), i32 1), <4 x i32> zeroinitializer, <4 x i32> <i32 0, i32 1, i32 " 1056 "2, i32 3>)"}}; 1057 1058 std::string AssemblyStr = AsmHead; 1059 for (auto &Itm : Data) 1060 AssemblyStr += Itm.second + "\n"; 1061 AssemblyStr += AsmTail; 1062 1063 LLVMContext Context; 1064 SMDiagnostic Error; 1065 auto M = parseAssemblyString(AssemblyStr, Error, Context); 1066 assert(M && "Bad assembly?"); 1067 1068 auto *F = M->getFunction("f"); 1069 assert(F && "Bad assembly?"); 1070 1071 auto &BB = F->getEntryBlock(); 1072 1073 int Index = 0; 1074 for (auto &I : BB) { 1075 if (isa<ReturnInst>(&I)) 1076 break; 1077 bool Poison = Data[Index].first.first; 1078 bool Undef = Data[Index].first.second; 1079 EXPECT_EQ(canCreatePoison(cast<Operator>(&I)), Poison) 1080 << "Incorrect answer of canCreatePoison at instruction " << Index 1081 << " = " << I; 1082 EXPECT_EQ(canCreateUndefOrPoison(cast<Operator>(&I)), Undef || Poison) 1083 << "Incorrect answer of canCreateUndef at instruction " << Index 1084 << " = " << I; 1085 Index++; 1086 } 1087 } 1088 1089 TEST_F(ValueTrackingTest, computePtrAlignment) { 1090 parseAssembly("declare i1 @f_i1()\n" 1091 "declare i8* @f_i8p()\n" 1092 "declare void @llvm.assume(i1)\n" 1093 "define void @test() {\n" 1094 " %A = call i8* @f_i8p()\n" 1095 " %cond = call i1 @f_i1()\n" 1096 " %CxtI = add i32 0, 0\n" 1097 " br i1 %cond, label %BB1, label %EXIT\n" 1098 "BB1:\n" 1099 " %CxtI2 = add i32 0, 0\n" 1100 " %cond2 = call i1 @f_i1()\n" 1101 " call void @llvm.assume(i1 true) [ \"align\"(i8* %A, i64 16) ]\n" 1102 " br i1 %cond2, label %BB2, label %EXIT\n" 1103 "BB2:\n" 1104 " %CxtI3 = add i32 0, 0\n" 1105 " ret void\n" 1106 "EXIT:\n" 1107 " ret void\n" 1108 "}"); 1109 AssumptionCache AC(*F); 1110 DominatorTree DT(*F); 1111 DataLayout DL = M->getDataLayout(); 1112 EXPECT_EQ(getKnownAlignment(A, DL, CxtI, &AC, &DT), Align(1)); 1113 EXPECT_EQ(getKnownAlignment(A, DL, CxtI2, &AC, &DT), Align(1)); 1114 EXPECT_EQ(getKnownAlignment(A, DL, CxtI3, &AC, &DT), Align(16)); 1115 } 1116 1117 TEST_F(ComputeKnownBitsTest, ComputeKnownBits) { 1118 parseAssembly( 1119 "define i32 @test(i32 %a, i32 %b) {\n" 1120 " %ash = mul i32 %a, 8\n" 1121 " %aad = add i32 %ash, 7\n" 1122 " %aan = and i32 %aad, 4095\n" 1123 " %bsh = shl i32 %b, 4\n" 1124 " %bad = or i32 %bsh, 6\n" 1125 " %ban = and i32 %bad, 4095\n" 1126 " %A = mul i32 %aan, %ban\n" 1127 " ret i32 %A\n" 1128 "}\n"); 1129 expectKnownBits(/*zero*/ 4278190085u, /*one*/ 10u); 1130 } 1131 1132 TEST_F(ComputeKnownBitsTest, ComputeKnownMulBits) { 1133 parseAssembly( 1134 "define i32 @test(i32 %a, i32 %b) {\n" 1135 " %aa = shl i32 %a, 5\n" 1136 " %bb = shl i32 %b, 5\n" 1137 " %aaa = or i32 %aa, 24\n" 1138 " %bbb = or i32 %bb, 28\n" 1139 " %A = mul i32 %aaa, %bbb\n" 1140 " ret i32 %A\n" 1141 "}\n"); 1142 expectKnownBits(/*zero*/ 95u, /*one*/ 32u); 1143 } 1144 1145 TEST_F(ValueTrackingTest, KnownNonZeroFromDomCond) { 1146 parseAssembly(R"( 1147 declare i8* @f_i8() 1148 define void @test(i1 %c) { 1149 %A = call i8* @f_i8() 1150 %B = call i8* @f_i8() 1151 %c1 = icmp ne i8* %A, null 1152 %cond = and i1 %c1, %c 1153 br i1 %cond, label %T, label %Q 1154 T: 1155 %CxtI = add i32 0, 0 1156 ret void 1157 Q: 1158 %CxtI2 = add i32 0, 0 1159 ret void 1160 } 1161 )"); 1162 AssumptionCache AC(*F); 1163 DominatorTree DT(*F); 1164 DataLayout DL = M->getDataLayout(); 1165 EXPECT_EQ(isKnownNonZero(A, DL, 0, &AC, CxtI, &DT), true); 1166 EXPECT_EQ(isKnownNonZero(A, DL, 0, &AC, CxtI2, &DT), false); 1167 } 1168 1169 TEST_F(ValueTrackingTest, KnownNonZeroFromDomCond2) { 1170 parseAssembly(R"( 1171 declare i8* @f_i8() 1172 define void @test(i1 %c) { 1173 %A = call i8* @f_i8() 1174 %B = call i8* @f_i8() 1175 %c1 = icmp ne i8* %A, null 1176 %cond = select i1 %c, i1 %c1, i1 false 1177 br i1 %cond, label %T, label %Q 1178 T: 1179 %CxtI = add i32 0, 0 1180 ret void 1181 Q: 1182 %CxtI2 = add i32 0, 0 1183 ret void 1184 } 1185 )"); 1186 AssumptionCache AC(*F); 1187 DominatorTree DT(*F); 1188 DataLayout DL = M->getDataLayout(); 1189 EXPECT_EQ(isKnownNonZero(A, DL, 0, &AC, CxtI, &DT), true); 1190 EXPECT_EQ(isKnownNonZero(A, DL, 0, &AC, CxtI2, &DT), false); 1191 } 1192 1193 TEST_F(ValueTrackingTest, IsImpliedConditionAnd) { 1194 parseAssembly(R"( 1195 define void @test(i32 %x, i32 %y) { 1196 %c1 = icmp ult i32 %x, 10 1197 %c2 = icmp ult i32 %y, 15 1198 %A = and i1 %c1, %c2 1199 ; x < 10 /\ y < 15 1200 %A2 = icmp ult i32 %x, 20 1201 %A3 = icmp uge i32 %y, 20 1202 %A4 = icmp ult i32 %x, 5 1203 ret void 1204 } 1205 )"); 1206 DataLayout DL = M->getDataLayout(); 1207 EXPECT_EQ(isImpliedCondition(A, A2, DL), true); 1208 EXPECT_EQ(isImpliedCondition(A, A3, DL), false); 1209 EXPECT_EQ(isImpliedCondition(A, A4, DL), None); 1210 } 1211 1212 TEST_F(ValueTrackingTest, IsImpliedConditionAnd2) { 1213 parseAssembly(R"( 1214 define void @test(i32 %x, i32 %y) { 1215 %c1 = icmp ult i32 %x, 10 1216 %c2 = icmp ult i32 %y, 15 1217 %A = select i1 %c1, i1 %c2, i1 false 1218 ; x < 10 /\ y < 15 1219 %A2 = icmp ult i32 %x, 20 1220 %A3 = icmp uge i32 %y, 20 1221 %A4 = icmp ult i32 %x, 5 1222 ret void 1223 } 1224 )"); 1225 DataLayout DL = M->getDataLayout(); 1226 EXPECT_EQ(isImpliedCondition(A, A2, DL), true); 1227 EXPECT_EQ(isImpliedCondition(A, A3, DL), false); 1228 EXPECT_EQ(isImpliedCondition(A, A4, DL), None); 1229 } 1230 1231 TEST_F(ValueTrackingTest, IsImpliedConditionOr) { 1232 parseAssembly(R"( 1233 define void @test(i32 %x, i32 %y) { 1234 %c1 = icmp ult i32 %x, 10 1235 %c2 = icmp ult i32 %y, 15 1236 %A = or i1 %c1, %c2 ; negated 1237 ; x >= 10 /\ y >= 15 1238 %A2 = icmp ult i32 %x, 5 1239 %A3 = icmp uge i32 %y, 10 1240 %A4 = icmp ult i32 %x, 15 1241 ret void 1242 } 1243 )"); 1244 DataLayout DL = M->getDataLayout(); 1245 EXPECT_EQ(isImpliedCondition(A, A2, DL, false), false); 1246 EXPECT_EQ(isImpliedCondition(A, A3, DL, false), true); 1247 EXPECT_EQ(isImpliedCondition(A, A4, DL, false), None); 1248 } 1249 1250 TEST_F(ValueTrackingTest, IsImpliedConditionOr2) { 1251 parseAssembly(R"( 1252 define void @test(i32 %x, i32 %y) { 1253 %c1 = icmp ult i32 %x, 10 1254 %c2 = icmp ult i32 %y, 15 1255 %A = select i1 %c1, i1 true, i1 %c2 ; negated 1256 ; x >= 10 /\ y >= 15 1257 %A2 = icmp ult i32 %x, 5 1258 %A3 = icmp uge i32 %y, 10 1259 %A4 = icmp ult i32 %x, 15 1260 ret void 1261 } 1262 )"); 1263 DataLayout DL = M->getDataLayout(); 1264 EXPECT_EQ(isImpliedCondition(A, A2, DL, false), false); 1265 EXPECT_EQ(isImpliedCondition(A, A3, DL, false), true); 1266 EXPECT_EQ(isImpliedCondition(A, A4, DL, false), None); 1267 } 1268 1269 TEST_F(ComputeKnownBitsTest, KnownNonZeroShift) { 1270 // %q is known nonzero without known bits. 1271 // Because %q is nonzero, %A[0] is known to be zero. 1272 parseAssembly( 1273 "define i8 @test(i8 %p, i8* %pq) {\n" 1274 " %q = load i8, i8* %pq, !range !0\n" 1275 " %A = shl i8 %p, %q\n" 1276 " ret i8 %A\n" 1277 "}\n" 1278 "!0 = !{ i8 1, i8 5 }\n"); 1279 expectKnownBits(/*zero*/ 1u, /*one*/ 0u); 1280 } 1281 1282 TEST_F(ComputeKnownBitsTest, ComputeKnownFshl) { 1283 // fshl(....1111....0000, 00..1111........, 6) 1284 // = 11....000000..11 1285 parseAssembly( 1286 "define i16 @test(i16 %a, i16 %b) {\n" 1287 " %aa = shl i16 %a, 4\n" 1288 " %bb = lshr i16 %b, 2\n" 1289 " %aaa = or i16 %aa, 3840\n" 1290 " %bbb = or i16 %bb, 3840\n" 1291 " %A = call i16 @llvm.fshl.i16(i16 %aaa, i16 %bbb, i16 6)\n" 1292 " ret i16 %A\n" 1293 "}\n" 1294 "declare i16 @llvm.fshl.i16(i16, i16, i16)\n"); 1295 expectKnownBits(/*zero*/ 1008u, /*one*/ 49155u); 1296 } 1297 1298 TEST_F(ComputeKnownBitsTest, ComputeKnownFshr) { 1299 // fshr(....1111....0000, 00..1111........, 26) 1300 // = 11....000000..11 1301 parseAssembly( 1302 "define i16 @test(i16 %a, i16 %b) {\n" 1303 " %aa = shl i16 %a, 4\n" 1304 " %bb = lshr i16 %b, 2\n" 1305 " %aaa = or i16 %aa, 3840\n" 1306 " %bbb = or i16 %bb, 3840\n" 1307 " %A = call i16 @llvm.fshr.i16(i16 %aaa, i16 %bbb, i16 26)\n" 1308 " ret i16 %A\n" 1309 "}\n" 1310 "declare i16 @llvm.fshr.i16(i16, i16, i16)\n"); 1311 expectKnownBits(/*zero*/ 1008u, /*one*/ 49155u); 1312 } 1313 1314 TEST_F(ComputeKnownBitsTest, ComputeKnownFshlZero) { 1315 // fshl(....1111....0000, 00..1111........, 0) 1316 // = ....1111....0000 1317 parseAssembly( 1318 "define i16 @test(i16 %a, i16 %b) {\n" 1319 " %aa = shl i16 %a, 4\n" 1320 " %bb = lshr i16 %b, 2\n" 1321 " %aaa = or i16 %aa, 3840\n" 1322 " %bbb = or i16 %bb, 3840\n" 1323 " %A = call i16 @llvm.fshl.i16(i16 %aaa, i16 %bbb, i16 0)\n" 1324 " ret i16 %A\n" 1325 "}\n" 1326 "declare i16 @llvm.fshl.i16(i16, i16, i16)\n"); 1327 expectKnownBits(/*zero*/ 15u, /*one*/ 3840u); 1328 } 1329 1330 TEST_F(ComputeKnownBitsTest, ComputeKnownUAddSatLeadingOnes) { 1331 // uadd.sat(1111...1, ........) 1332 // = 1111.... 1333 parseAssembly( 1334 "define i8 @test(i8 %a, i8 %b) {\n" 1335 " %aa = or i8 %a, 241\n" 1336 " %A = call i8 @llvm.uadd.sat.i8(i8 %aa, i8 %b)\n" 1337 " ret i8 %A\n" 1338 "}\n" 1339 "declare i8 @llvm.uadd.sat.i8(i8, i8)\n"); 1340 expectKnownBits(/*zero*/ 0u, /*one*/ 240u); 1341 } 1342 1343 TEST_F(ComputeKnownBitsTest, ComputeKnownUAddSatOnesPreserved) { 1344 // uadd.sat(00...011, .1...110) 1345 // = .......1 1346 parseAssembly( 1347 "define i8 @test(i8 %a, i8 %b) {\n" 1348 " %aa = or i8 %a, 3\n" 1349 " %aaa = and i8 %aa, 59\n" 1350 " %bb = or i8 %b, 70\n" 1351 " %bbb = and i8 %bb, 254\n" 1352 " %A = call i8 @llvm.uadd.sat.i8(i8 %aaa, i8 %bbb)\n" 1353 " ret i8 %A\n" 1354 "}\n" 1355 "declare i8 @llvm.uadd.sat.i8(i8, i8)\n"); 1356 expectKnownBits(/*zero*/ 0u, /*one*/ 1u); 1357 } 1358 1359 TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatLHSLeadingZeros) { 1360 // usub.sat(0000...0, ........) 1361 // = 0000.... 1362 parseAssembly( 1363 "define i8 @test(i8 %a, i8 %b) {\n" 1364 " %aa = and i8 %a, 14\n" 1365 " %A = call i8 @llvm.usub.sat.i8(i8 %aa, i8 %b)\n" 1366 " ret i8 %A\n" 1367 "}\n" 1368 "declare i8 @llvm.usub.sat.i8(i8, i8)\n"); 1369 expectKnownBits(/*zero*/ 240u, /*one*/ 0u); 1370 } 1371 1372 TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatRHSLeadingOnes) { 1373 // usub.sat(........, 1111...1) 1374 // = 0000.... 1375 parseAssembly( 1376 "define i8 @test(i8 %a, i8 %b) {\n" 1377 " %bb = or i8 %a, 241\n" 1378 " %A = call i8 @llvm.usub.sat.i8(i8 %a, i8 %bb)\n" 1379 " ret i8 %A\n" 1380 "}\n" 1381 "declare i8 @llvm.usub.sat.i8(i8, i8)\n"); 1382 expectKnownBits(/*zero*/ 240u, /*one*/ 0u); 1383 } 1384 1385 TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatZerosPreserved) { 1386 // usub.sat(11...011, .1...110) 1387 // = ......0. 1388 parseAssembly( 1389 "define i8 @test(i8 %a, i8 %b) {\n" 1390 " %aa = or i8 %a, 195\n" 1391 " %aaa = and i8 %aa, 251\n" 1392 " %bb = or i8 %b, 70\n" 1393 " %bbb = and i8 %bb, 254\n" 1394 " %A = call i8 @llvm.usub.sat.i8(i8 %aaa, i8 %bbb)\n" 1395 " ret i8 %A\n" 1396 "}\n" 1397 "declare i8 @llvm.usub.sat.i8(i8, i8)\n"); 1398 expectKnownBits(/*zero*/ 2u, /*one*/ 0u); 1399 } 1400 1401 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsPtrToIntTrunc) { 1402 // ptrtoint truncates the pointer type. 1403 parseAssembly( 1404 "define void @test(i8** %p) {\n" 1405 " %A = load i8*, i8** %p\n" 1406 " %i = ptrtoint i8* %A to i32\n" 1407 " %m = and i32 %i, 31\n" 1408 " %c = icmp eq i32 %m, 0\n" 1409 " call void @llvm.assume(i1 %c)\n" 1410 " ret void\n" 1411 "}\n" 1412 "declare void @llvm.assume(i1)\n"); 1413 AssumptionCache AC(*F); 1414 KnownBits Known = computeKnownBits( 1415 A, M->getDataLayout(), /* Depth */ 0, &AC, F->front().getTerminator()); 1416 EXPECT_EQ(Known.Zero.getZExtValue(), 31u); 1417 EXPECT_EQ(Known.One.getZExtValue(), 0u); 1418 } 1419 1420 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsPtrToIntZext) { 1421 // ptrtoint zero extends the pointer type. 1422 parseAssembly( 1423 "define void @test(i8** %p) {\n" 1424 " %A = load i8*, i8** %p\n" 1425 " %i = ptrtoint i8* %A to i128\n" 1426 " %m = and i128 %i, 31\n" 1427 " %c = icmp eq i128 %m, 0\n" 1428 " call void @llvm.assume(i1 %c)\n" 1429 " ret void\n" 1430 "}\n" 1431 "declare void @llvm.assume(i1)\n"); 1432 AssumptionCache AC(*F); 1433 KnownBits Known = computeKnownBits( 1434 A, M->getDataLayout(), /* Depth */ 0, &AC, F->front().getTerminator()); 1435 EXPECT_EQ(Known.Zero.getZExtValue(), 31u); 1436 EXPECT_EQ(Known.One.getZExtValue(), 0u); 1437 } 1438 1439 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsFreeze) { 1440 parseAssembly("define void @test() {\n" 1441 " %m = call i32 @any_num()\n" 1442 " %A = freeze i32 %m\n" 1443 " %n = and i32 %m, 31\n" 1444 " %c = icmp eq i32 %n, 0\n" 1445 " call void @llvm.assume(i1 %c)\n" 1446 " ret void\n" 1447 "}\n" 1448 "declare void @llvm.assume(i1)\n" 1449 "declare i32 @any_num()\n"); 1450 AssumptionCache AC(*F); 1451 KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1452 F->front().getTerminator()); 1453 EXPECT_EQ(Known.Zero.getZExtValue(), 31u); 1454 EXPECT_EQ(Known.One.getZExtValue(), 0u); 1455 } 1456 1457 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsAddWithRange) { 1458 parseAssembly("define void @test(i64* %p) {\n" 1459 " %A = load i64, i64* %p, !range !{i64 64, i64 65536}\n" 1460 " %APlus512 = add i64 %A, 512\n" 1461 " %c = icmp ugt i64 %APlus512, 523\n" 1462 " call void @llvm.assume(i1 %c)\n" 1463 " ret void\n" 1464 "}\n" 1465 "declare void @llvm.assume(i1)\n"); 1466 AssumptionCache AC(*F); 1467 KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1468 F->front().getTerminator()); 1469 EXPECT_EQ(Known.Zero.getZExtValue(), ~(65536llu - 1)); 1470 EXPECT_EQ(Known.One.getZExtValue(), 0u); 1471 Instruction &APlus512 = findInstructionByName(F, "APlus512"); 1472 Known = computeKnownBits(&APlus512, M->getDataLayout(), /* Depth */ 0, &AC, 1473 F->front().getTerminator()); 1474 // We know of one less zero because 512 may have produced a 1 that 1475 // got carried all the way to the first trailing zero. 1476 EXPECT_EQ(Known.Zero.getZExtValue(), (~(65536llu - 1)) << 1); 1477 EXPECT_EQ(Known.One.getZExtValue(), 0u); 1478 // The known range is not precise given computeKnownBits works 1479 // with the masks of zeros and ones, not the ranges. 1480 EXPECT_EQ(Known.getMinValue(), 0u); 1481 EXPECT_EQ(Known.getMaxValue(), 131071); 1482 } 1483 1484 // 512 + [32, 64) doesn't produce overlapping bits. 1485 // Make sure we get all the individual bits properly. 1486 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsAddWithRangeNoOverlap) { 1487 parseAssembly("define void @test(i64* %p) {\n" 1488 " %A = load i64, i64* %p, !range !{i64 32, i64 64}\n" 1489 " %APlus512 = add i64 %A, 512\n" 1490 " %c = icmp ugt i64 %APlus512, 523\n" 1491 " call void @llvm.assume(i1 %c)\n" 1492 " ret void\n" 1493 "}\n" 1494 "declare void @llvm.assume(i1)\n"); 1495 AssumptionCache AC(*F); 1496 KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1497 F->front().getTerminator()); 1498 EXPECT_EQ(Known.Zero.getZExtValue(), ~(64llu - 1)); 1499 EXPECT_EQ(Known.One.getZExtValue(), 32u); 1500 Instruction &APlus512 = findInstructionByName(F, "APlus512"); 1501 Known = computeKnownBits(&APlus512, M->getDataLayout(), /* Depth */ 0, &AC, 1502 F->front().getTerminator()); 1503 EXPECT_EQ(Known.Zero.getZExtValue(), ~512llu & ~(64llu - 1)); 1504 EXPECT_EQ(Known.One.getZExtValue(), 512u | 32u); 1505 // The known range is not precise given computeKnownBits works 1506 // with the masks of zeros and ones, not the ranges. 1507 EXPECT_EQ(Known.getMinValue(), 544); 1508 EXPECT_EQ(Known.getMaxValue(), 575); 1509 } 1510 1511 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsGEPWithRange) { 1512 parseAssembly( 1513 "define void @test(i64* %p) {\n" 1514 " %A = load i64, i64* %p, !range !{i64 64, i64 65536}\n" 1515 " %APtr = inttoptr i64 %A to float*" 1516 " %APtrPlus512 = getelementptr float, float* %APtr, i32 128\n" 1517 " %c = icmp ugt float* %APtrPlus512, inttoptr (i32 523 to float*)\n" 1518 " call void @llvm.assume(i1 %c)\n" 1519 " ret void\n" 1520 "}\n" 1521 "declare void @llvm.assume(i1)\n"); 1522 AssumptionCache AC(*F); 1523 KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1524 F->front().getTerminator()); 1525 EXPECT_EQ(Known.Zero.getZExtValue(), ~(65536llu - 1)); 1526 EXPECT_EQ(Known.One.getZExtValue(), 0u); 1527 Instruction &APtrPlus512 = findInstructionByName(F, "APtrPlus512"); 1528 Known = computeKnownBits(&APtrPlus512, M->getDataLayout(), /* Depth */ 0, &AC, 1529 F->front().getTerminator()); 1530 // We know of one less zero because 512 may have produced a 1 that 1531 // got carried all the way to the first trailing zero. 1532 EXPECT_EQ(Known.Zero.getZExtValue(), ~(65536llu - 1) << 1); 1533 EXPECT_EQ(Known.One.getZExtValue(), 0u); 1534 // The known range is not precise given computeKnownBits works 1535 // with the masks of zeros and ones, not the ranges. 1536 EXPECT_EQ(Known.getMinValue(), 0u); 1537 EXPECT_EQ(Known.getMaxValue(), 131071); 1538 } 1539 1540 // 4*128 + [32, 64) doesn't produce overlapping bits. 1541 // Make sure we get all the individual bits properly. 1542 // This test is useful to check that we account for the scaling factor 1543 // in the gep. Indeed, gep float, [32,64), 128 is not 128 + [32,64). 1544 TEST_F(ComputeKnownBitsTest, ComputeKnownBitsGEPWithRangeNoOverlap) { 1545 parseAssembly( 1546 "define void @test(i64* %p) {\n" 1547 " %A = load i64, i64* %p, !range !{i64 32, i64 64}\n" 1548 " %APtr = inttoptr i64 %A to float*" 1549 " %APtrPlus512 = getelementptr float, float* %APtr, i32 128\n" 1550 " %c = icmp ugt float* %APtrPlus512, inttoptr (i32 523 to float*)\n" 1551 " call void @llvm.assume(i1 %c)\n" 1552 " ret void\n" 1553 "}\n" 1554 "declare void @llvm.assume(i1)\n"); 1555 AssumptionCache AC(*F); 1556 KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1557 F->front().getTerminator()); 1558 EXPECT_EQ(Known.Zero.getZExtValue(), ~(64llu - 1)); 1559 EXPECT_EQ(Known.One.getZExtValue(), 32u); 1560 Instruction &APtrPlus512 = findInstructionByName(F, "APtrPlus512"); 1561 Known = computeKnownBits(&APtrPlus512, M->getDataLayout(), /* Depth */ 0, &AC, 1562 F->front().getTerminator()); 1563 EXPECT_EQ(Known.Zero.getZExtValue(), ~512llu & ~(64llu - 1)); 1564 EXPECT_EQ(Known.One.getZExtValue(), 512u | 32u); 1565 // The known range is not precise given computeKnownBits works 1566 // with the masks of zeros and ones, not the ranges. 1567 EXPECT_EQ(Known.getMinValue(), 544); 1568 EXPECT_EQ(Known.getMaxValue(), 575); 1569 } 1570 1571 class IsBytewiseValueTest : public ValueTrackingTest, 1572 public ::testing::WithParamInterface< 1573 std::pair<const char *, const char *>> { 1574 protected: 1575 }; 1576 1577 const std::pair<const char *, const char *> IsBytewiseValueTests[] = { 1578 { 1579 "i8 0", 1580 "i48* null", 1581 }, 1582 { 1583 "i8 undef", 1584 "i48* undef", 1585 }, 1586 { 1587 "i8 0", 1588 "i8 zeroinitializer", 1589 }, 1590 { 1591 "i8 0", 1592 "i8 0", 1593 }, 1594 { 1595 "i8 -86", 1596 "i8 -86", 1597 }, 1598 { 1599 "i8 -1", 1600 "i8 -1", 1601 }, 1602 { 1603 "i8 undef", 1604 "i16 undef", 1605 }, 1606 { 1607 "i8 0", 1608 "i16 0", 1609 }, 1610 { 1611 "", 1612 "i16 7", 1613 }, 1614 { 1615 "i8 -86", 1616 "i16 -21846", 1617 }, 1618 { 1619 "i8 -1", 1620 "i16 -1", 1621 }, 1622 { 1623 "i8 0", 1624 "i48 0", 1625 }, 1626 { 1627 "i8 -1", 1628 "i48 -1", 1629 }, 1630 { 1631 "i8 0", 1632 "i49 0", 1633 }, 1634 { 1635 "", 1636 "i49 -1", 1637 }, 1638 { 1639 "i8 0", 1640 "half 0xH0000", 1641 }, 1642 { 1643 "i8 -85", 1644 "half 0xHABAB", 1645 }, 1646 { 1647 "i8 0", 1648 "float 0.0", 1649 }, 1650 { 1651 "i8 -1", 1652 "float 0xFFFFFFFFE0000000", 1653 }, 1654 { 1655 "i8 0", 1656 "double 0.0", 1657 }, 1658 { 1659 "i8 -15", 1660 "double 0xF1F1F1F1F1F1F1F1", 1661 }, 1662 { 1663 "i8 undef", 1664 "i16* undef", 1665 }, 1666 { 1667 "i8 0", 1668 "i16* inttoptr (i64 0 to i16*)", 1669 }, 1670 { 1671 "i8 -1", 1672 "i16* inttoptr (i64 -1 to i16*)", 1673 }, 1674 { 1675 "i8 -86", 1676 "i16* inttoptr (i64 -6148914691236517206 to i16*)", 1677 }, 1678 { 1679 "", 1680 "i16* inttoptr (i48 -1 to i16*)", 1681 }, 1682 { 1683 "i8 -1", 1684 "i16* inttoptr (i96 -1 to i16*)", 1685 }, 1686 { 1687 "i8 undef", 1688 "[0 x i8] zeroinitializer", 1689 }, 1690 { 1691 "i8 undef", 1692 "[0 x i8] undef", 1693 }, 1694 { 1695 "i8 undef", 1696 "[5 x [0 x i8]] zeroinitializer", 1697 }, 1698 { 1699 "i8 undef", 1700 "[5 x [0 x i8]] undef", 1701 }, 1702 { 1703 "i8 0", 1704 "[6 x i8] zeroinitializer", 1705 }, 1706 { 1707 "i8 undef", 1708 "[6 x i8] undef", 1709 }, 1710 { 1711 "i8 1", 1712 "[5 x i8] [i8 1, i8 1, i8 1, i8 1, i8 1]", 1713 }, 1714 { 1715 "", 1716 "[5 x i64] [i64 1, i64 1, i64 1, i64 1, i64 1]", 1717 }, 1718 { 1719 "i8 -1", 1720 "[5 x i64] [i64 -1, i64 -1, i64 -1, i64 -1, i64 -1]", 1721 }, 1722 { 1723 "", 1724 "[4 x i8] [i8 1, i8 2, i8 1, i8 1]", 1725 }, 1726 { 1727 "i8 1", 1728 "[4 x i8] [i8 1, i8 undef, i8 1, i8 1]", 1729 }, 1730 { 1731 "i8 0", 1732 "<6 x i8> zeroinitializer", 1733 }, 1734 { 1735 "i8 undef", 1736 "<6 x i8> undef", 1737 }, 1738 { 1739 "i8 1", 1740 "<5 x i8> <i8 1, i8 1, i8 1, i8 1, i8 1>", 1741 }, 1742 { 1743 "", 1744 "<5 x i64> <i64 1, i64 1, i64 1, i64 1, i64 1>", 1745 }, 1746 { 1747 "i8 -1", 1748 "<5 x i64> <i64 -1, i64 -1, i64 -1, i64 -1, i64 -1>", 1749 }, 1750 { 1751 "", 1752 "<4 x i8> <i8 1, i8 1, i8 2, i8 1>", 1753 }, 1754 { 1755 "i8 5", 1756 "<2 x i8> < i8 5, i8 undef >", 1757 }, 1758 { 1759 "i8 0", 1760 "[2 x [2 x i16]] zeroinitializer", 1761 }, 1762 { 1763 "i8 undef", 1764 "[2 x [2 x i16]] undef", 1765 }, 1766 { 1767 "i8 -86", 1768 "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], " 1769 "[2 x i16] [i16 -21846, i16 -21846]]", 1770 }, 1771 { 1772 "", 1773 "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], " 1774 "[2 x i16] [i16 -21836, i16 -21846]]", 1775 }, 1776 { 1777 "i8 undef", 1778 "{ } zeroinitializer", 1779 }, 1780 { 1781 "i8 undef", 1782 "{ } undef", 1783 }, 1784 { 1785 "i8 undef", 1786 "{ {}, {} } zeroinitializer", 1787 }, 1788 { 1789 "i8 undef", 1790 "{ {}, {} } undef", 1791 }, 1792 { 1793 "i8 0", 1794 "{i8, i64, i16*} zeroinitializer", 1795 }, 1796 { 1797 "i8 undef", 1798 "{i8, i64, i16*} undef", 1799 }, 1800 { 1801 "i8 -86", 1802 "{i8, i64, i16*} {i8 -86, i64 -6148914691236517206, i16* undef}", 1803 }, 1804 { 1805 "", 1806 "{i8, i64, i16*} {i8 86, i64 -6148914691236517206, i16* undef}", 1807 }, 1808 }; 1809 1810 INSTANTIATE_TEST_CASE_P(IsBytewiseValueParamTests, IsBytewiseValueTest, 1811 ::testing::ValuesIn(IsBytewiseValueTests),); 1812 1813 TEST_P(IsBytewiseValueTest, IsBytewiseValue) { 1814 auto M = parseModule(std::string("@test = global ") + GetParam().second); 1815 GlobalVariable *GV = dyn_cast<GlobalVariable>(M->getNamedValue("test")); 1816 Value *Actual = isBytewiseValue(GV->getInitializer(), M->getDataLayout()); 1817 std::string Buff; 1818 raw_string_ostream S(Buff); 1819 if (Actual) 1820 S << *Actual; 1821 EXPECT_EQ(GetParam().first, S.str()); 1822 } 1823 1824 TEST_F(ValueTrackingTest, ComputeConstantRange) { 1825 { 1826 // Assumptions: 1827 // * stride >= 5 1828 // * stride < 10 1829 // 1830 // stride = [5, 10) 1831 auto M = parseModule(R"( 1832 declare void @llvm.assume(i1) 1833 1834 define i32 @test(i32 %stride) { 1835 %gt = icmp uge i32 %stride, 5 1836 call void @llvm.assume(i1 %gt) 1837 %lt = icmp ult i32 %stride, 10 1838 call void @llvm.assume(i1 %lt) 1839 %stride.plus.one = add nsw nuw i32 %stride, 1 1840 ret i32 %stride.plus.one 1841 })"); 1842 Function *F = M->getFunction("test"); 1843 1844 AssumptionCache AC(*F); 1845 Value *Stride = &*F->arg_begin(); 1846 ConstantRange CR1 = computeConstantRange(Stride, true, &AC, nullptr); 1847 EXPECT_TRUE(CR1.isFullSet()); 1848 1849 Instruction *I = &findInstructionByName(F, "stride.plus.one"); 1850 ConstantRange CR2 = computeConstantRange(Stride, true, &AC, I); 1851 EXPECT_EQ(5, CR2.getLower()); 1852 EXPECT_EQ(10, CR2.getUpper()); 1853 } 1854 1855 { 1856 // Assumptions: 1857 // * stride >= 5 1858 // * stride < 200 1859 // * stride == 99 1860 // 1861 // stride = [99, 100) 1862 auto M = parseModule(R"( 1863 declare void @llvm.assume(i1) 1864 1865 define i32 @test(i32 %stride) { 1866 %gt = icmp uge i32 %stride, 5 1867 call void @llvm.assume(i1 %gt) 1868 %lt = icmp ult i32 %stride, 200 1869 call void @llvm.assume(i1 %lt) 1870 %eq = icmp eq i32 %stride, 99 1871 call void @llvm.assume(i1 %eq) 1872 %stride.plus.one = add nsw nuw i32 %stride, 1 1873 ret i32 %stride.plus.one 1874 })"); 1875 Function *F = M->getFunction("test"); 1876 1877 AssumptionCache AC(*F); 1878 Value *Stride = &*F->arg_begin(); 1879 Instruction *I = &findInstructionByName(F, "stride.plus.one"); 1880 ConstantRange CR = computeConstantRange(Stride, true, &AC, I); 1881 EXPECT_EQ(99, *CR.getSingleElement()); 1882 } 1883 1884 { 1885 // Assumptions: 1886 // * stride >= 5 1887 // * stride >= 50 1888 // * stride < 100 1889 // * stride < 200 1890 // 1891 // stride = [50, 100) 1892 auto M = parseModule(R"( 1893 declare void @llvm.assume(i1) 1894 1895 define i32 @test(i32 %stride, i1 %cond) { 1896 %gt = icmp uge i32 %stride, 5 1897 call void @llvm.assume(i1 %gt) 1898 %gt.2 = icmp uge i32 %stride, 50 1899 call void @llvm.assume(i1 %gt.2) 1900 br i1 %cond, label %bb1, label %bb2 1901 1902 bb1: 1903 %lt = icmp ult i32 %stride, 200 1904 call void @llvm.assume(i1 %lt) 1905 %lt.2 = icmp ult i32 %stride, 100 1906 call void @llvm.assume(i1 %lt.2) 1907 %stride.plus.one = add nsw nuw i32 %stride, 1 1908 ret i32 %stride.plus.one 1909 1910 bb2: 1911 ret i32 0 1912 })"); 1913 Function *F = M->getFunction("test"); 1914 1915 AssumptionCache AC(*F); 1916 Value *Stride = &*F->arg_begin(); 1917 Instruction *GT2 = &findInstructionByName(F, "gt.2"); 1918 ConstantRange CR = computeConstantRange(Stride, true, &AC, GT2); 1919 EXPECT_EQ(5, CR.getLower()); 1920 EXPECT_EQ(0, CR.getUpper()); 1921 1922 Instruction *I = &findInstructionByName(F, "stride.plus.one"); 1923 ConstantRange CR2 = computeConstantRange(Stride, true, &AC, I); 1924 EXPECT_EQ(50, CR2.getLower()); 1925 EXPECT_EQ(100, CR2.getUpper()); 1926 } 1927 1928 { 1929 // Assumptions: 1930 // * stride > 5 1931 // * stride < 5 1932 // 1933 // stride = empty range, as the assumptions contradict each other. 1934 auto M = parseModule(R"( 1935 declare void @llvm.assume(i1) 1936 1937 define i32 @test(i32 %stride, i1 %cond) { 1938 %gt = icmp ugt i32 %stride, 5 1939 call void @llvm.assume(i1 %gt) 1940 %lt = icmp ult i32 %stride, 5 1941 call void @llvm.assume(i1 %lt) 1942 %stride.plus.one = add nsw nuw i32 %stride, 1 1943 ret i32 %stride.plus.one 1944 })"); 1945 Function *F = M->getFunction("test"); 1946 1947 AssumptionCache AC(*F); 1948 Value *Stride = &*F->arg_begin(); 1949 1950 Instruction *I = &findInstructionByName(F, "stride.plus.one"); 1951 ConstantRange CR = computeConstantRange(Stride, true, &AC, I); 1952 EXPECT_TRUE(CR.isEmptySet()); 1953 } 1954 1955 { 1956 // Assumptions: 1957 // * x.1 >= 5 1958 // * x.2 < x.1 1959 // 1960 // stride = [0, 5) 1961 auto M = parseModule(R"( 1962 declare void @llvm.assume(i1) 1963 1964 define i32 @test(i32 %x.1, i32 %x.2) { 1965 %gt = icmp uge i32 %x.1, 5 1966 call void @llvm.assume(i1 %gt) 1967 %lt = icmp ult i32 %x.2, %x.1 1968 call void @llvm.assume(i1 %lt) 1969 %stride.plus.one = add nsw nuw i32 %x.1, 1 1970 ret i32 %stride.plus.one 1971 })"); 1972 Function *F = M->getFunction("test"); 1973 1974 AssumptionCache AC(*F); 1975 Value *X2 = &*std::next(F->arg_begin()); 1976 1977 Instruction *I = &findInstructionByName(F, "stride.plus.one"); 1978 ConstantRange CR1 = computeConstantRange(X2, true, &AC, I); 1979 EXPECT_EQ(0, CR1.getLower()); 1980 EXPECT_EQ(5, CR1.getUpper()); 1981 1982 // Check the depth cutoff results in a conservative result (full set) by 1983 // passing Depth == MaxDepth == 6. 1984 ConstantRange CR2 = computeConstantRange(X2, true, &AC, I, 6); 1985 EXPECT_TRUE(CR2.isFullSet()); 1986 } 1987 } 1988 1989 struct FindAllocaForValueTestParams { 1990 const char *IR; 1991 bool AnyOffsetResult; 1992 bool ZeroOffsetResult; 1993 }; 1994 1995 class FindAllocaForValueTest 1996 : public ValueTrackingTest, 1997 public ::testing::WithParamInterface<FindAllocaForValueTestParams> { 1998 protected: 1999 }; 2000 2001 const FindAllocaForValueTestParams FindAllocaForValueTests[] = { 2002 {R"( 2003 define void @test() { 2004 %a = alloca i64 2005 %r = bitcast i64* %a to i32* 2006 ret void 2007 })", 2008 true, true}, 2009 2010 {R"( 2011 define void @test() { 2012 %a = alloca i32 2013 %r = getelementptr i32, i32* %a, i32 1 2014 ret void 2015 })", 2016 true, false}, 2017 2018 {R"( 2019 define void @test() { 2020 %a = alloca i32 2021 %r = getelementptr i32, i32* %a, i32 0 2022 ret void 2023 })", 2024 true, true}, 2025 2026 {R"( 2027 define void @test(i1 %cond) { 2028 entry: 2029 %a = alloca i32 2030 br label %bb1 2031 2032 bb1: 2033 %r = phi i32* [ %a, %entry ], [ %r, %bb1 ] 2034 br i1 %cond, label %bb1, label %exit 2035 2036 exit: 2037 ret void 2038 })", 2039 true, true}, 2040 2041 {R"( 2042 define void @test(i1 %cond) { 2043 %a = alloca i32 2044 %r = select i1 %cond, i32* %a, i32* %a 2045 ret void 2046 })", 2047 true, true}, 2048 2049 {R"( 2050 define void @test(i1 %cond) { 2051 %a = alloca i32 2052 %b = alloca i32 2053 %r = select i1 %cond, i32* %a, i32* %b 2054 ret void 2055 })", 2056 false, false}, 2057 2058 {R"( 2059 define void @test(i1 %cond) { 2060 entry: 2061 %a = alloca i64 2062 %a32 = bitcast i64* %a to i32* 2063 br label %bb1 2064 2065 bb1: 2066 %x = phi i32* [ %a32, %entry ], [ %x, %bb1 ] 2067 %r = getelementptr i32, i32* %x, i32 1 2068 br i1 %cond, label %bb1, label %exit 2069 2070 exit: 2071 ret void 2072 })", 2073 true, false}, 2074 2075 {R"( 2076 define void @test(i1 %cond) { 2077 entry: 2078 %a = alloca i64 2079 %a32 = bitcast i64* %a to i32* 2080 br label %bb1 2081 2082 bb1: 2083 %x = phi i32* [ %a32, %entry ], [ %r, %bb1 ] 2084 %r = getelementptr i32, i32* %x, i32 1 2085 br i1 %cond, label %bb1, label %exit 2086 2087 exit: 2088 ret void 2089 })", 2090 true, false}, 2091 2092 {R"( 2093 define void @test(i1 %cond, i64* %a) { 2094 entry: 2095 %r = bitcast i64* %a to i32* 2096 ret void 2097 })", 2098 false, false}, 2099 2100 {R"( 2101 define void @test(i1 %cond) { 2102 entry: 2103 %a = alloca i32 2104 %b = alloca i32 2105 br label %bb1 2106 2107 bb1: 2108 %r = phi i32* [ %a, %entry ], [ %b, %bb1 ] 2109 br i1 %cond, label %bb1, label %exit 2110 2111 exit: 2112 ret void 2113 })", 2114 false, false}, 2115 }; 2116 2117 TEST_P(FindAllocaForValueTest, findAllocaForValue) { 2118 auto M = parseModule(GetParam().IR); 2119 Function *F = M->getFunction("test"); 2120 Instruction *I = &findInstructionByName(F, "r"); 2121 const AllocaInst *AI = findAllocaForValue(I); 2122 EXPECT_EQ(!!AI, GetParam().AnyOffsetResult); 2123 } 2124 2125 TEST_P(FindAllocaForValueTest, findAllocaForValueZeroOffset) { 2126 auto M = parseModule(GetParam().IR); 2127 Function *F = M->getFunction("test"); 2128 Instruction *I = &findInstructionByName(F, "r"); 2129 const AllocaInst *AI = findAllocaForValue(I, true); 2130 EXPECT_EQ(!!AI, GetParam().ZeroOffsetResult); 2131 } 2132 2133 INSTANTIATE_TEST_CASE_P(FindAllocaForValueTest, FindAllocaForValueTest, 2134 ::testing::ValuesIn(FindAllocaForValueTests), ); 2135