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