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