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