1134bec27SJames Molloy //===- ValueTrackingTest.cpp - ValueTracking tests ------------------------===// 2134bec27SJames Molloy // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6134bec27SJames Molloy // 7134bec27SJames Molloy //===----------------------------------------------------------------------===// 8134bec27SJames Molloy 9134bec27SJames Molloy #include "llvm/Analysis/ValueTracking.h" 10d86fff6aSNikita Popov #include "llvm/Analysis/AssumptionCache.h" 11134bec27SJames Molloy #include "llvm/AsmParser/Parser.h" 128d041811SFlorian Hahn #include "llvm/IR/ConstantRange.h" 1325ce1e04SJuneyoung Lee #include "llvm/IR/Dominators.h" 14134bec27SJames Molloy #include "llvm/IR/Function.h" 15c2634fc6SDavid Sherwood #include "llvm/IR/IRBuilder.h" 16134bec27SJames Molloy #include "llvm/IR/InstIterator.h" 17be04aba6SCraig Topper #include "llvm/IR/Instructions.h" 18134bec27SJames Molloy #include "llvm/IR/LLVMContext.h" 19134bec27SJames Molloy #include "llvm/IR/Module.h" 20134bec27SJames Molloy #include "llvm/Support/ErrorHandling.h" 2170dbd5fbSSimon Dardis #include "llvm/Support/KnownBits.h" 22329d15b5SVitaly Buka #include "llvm/Support/SourceMgr.h" 23565f4e72SAlex Richardson #include "llvm/Transforms/Utils/Local.h" 24134bec27SJames Molloy #include "gtest/gtest.h" 25134bec27SJames Molloy 26134bec27SJames Molloy using namespace llvm; 27134bec27SJames Molloy 28134bec27SJames Molloy namespace { 29134bec27SJames Molloy 309b3c2a72SJuneyoung Lee static Instruction *findInstructionByNameOrNull(Function *F, StringRef Name) { 318d041811SFlorian Hahn for (Instruction &I : instructions(F)) 328d041811SFlorian Hahn if (I.getName() == Name) 339b3c2a72SJuneyoung Lee return &I; 349b3c2a72SJuneyoung Lee 359b3c2a72SJuneyoung Lee return nullptr; 369b3c2a72SJuneyoung Lee } 379b3c2a72SJuneyoung Lee 389b3c2a72SJuneyoung Lee static Instruction &findInstructionByName(Function *F, StringRef Name) { 399b3c2a72SJuneyoung Lee auto *I = findInstructionByNameOrNull(F, Name); 409b3c2a72SJuneyoung Lee if (I) 419b3c2a72SJuneyoung Lee return *I; 428d041811SFlorian Hahn 438d041811SFlorian Hahn llvm_unreachable("Expected value not found"); 448d041811SFlorian Hahn } 458d041811SFlorian Hahn 46219e5367SNikita Popov class ValueTrackingTest : public testing::Test { 47134bec27SJames Molloy protected: 48329d15b5SVitaly Buka std::unique_ptr<Module> parseModule(StringRef Assembly) { 49134bec27SJames Molloy SMDiagnostic Error; 50329d15b5SVitaly Buka std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context); 51134bec27SJames Molloy 52134bec27SJames Molloy std::string errMsg; 53134bec27SJames Molloy raw_string_ostream os(errMsg); 54134bec27SJames Molloy Error.print("", os); 55329d15b5SVitaly Buka EXPECT_TRUE(M) << os.str(); 56134bec27SJames Molloy 57329d15b5SVitaly Buka return M; 58329d15b5SVitaly Buka } 59329d15b5SVitaly Buka 60329d15b5SVitaly Buka void parseAssembly(StringRef Assembly) { 61329d15b5SVitaly Buka M = parseModule(Assembly); 62329d15b5SVitaly Buka ASSERT_TRUE(M); 63134bec27SJames Molloy 64d86fff6aSNikita Popov F = M->getFunction("test"); 65329d15b5SVitaly Buka ASSERT_TRUE(F) << "Test must have a function @test"; 66329d15b5SVitaly Buka if (!F) 67329d15b5SVitaly Buka return; 68134bec27SJames Molloy 699b3c2a72SJuneyoung Lee A = findInstructionByNameOrNull(F, "A"); 70329d15b5SVitaly Buka ASSERT_TRUE(A) << "@test must have an instruction %A"; 71eca40c36SJuneyoung Lee A2 = findInstructionByNameOrNull(F, "A2"); 72eca40c36SJuneyoung Lee A3 = findInstructionByNameOrNull(F, "A3"); 73eca40c36SJuneyoung Lee A4 = findInstructionByNameOrNull(F, "A4"); 749b3c2a72SJuneyoung Lee 759b3c2a72SJuneyoung Lee CxtI = findInstructionByNameOrNull(F, "CxtI"); 769b3c2a72SJuneyoung Lee CxtI2 = findInstructionByNameOrNull(F, "CxtI2"); 779b3c2a72SJuneyoung Lee CxtI3 = findInstructionByNameOrNull(F, "CxtI3"); 78134bec27SJames Molloy } 79134bec27SJames Molloy 80219e5367SNikita Popov LLVMContext Context; 81219e5367SNikita Popov std::unique_ptr<Module> M; 82d86fff6aSNikita Popov Function *F = nullptr; 83329d15b5SVitaly Buka Instruction *A = nullptr; 84eca40c36SJuneyoung Lee // Instructions (optional) 85eca40c36SJuneyoung Lee Instruction *A2 = nullptr, *A3 = nullptr, *A4 = nullptr; 869b3c2a72SJuneyoung Lee 879b3c2a72SJuneyoung Lee // Context instructions (optional) 889b3c2a72SJuneyoung Lee Instruction *CxtI = nullptr, *CxtI2 = nullptr, *CxtI3 = nullptr; 89219e5367SNikita Popov }; 90219e5367SNikita Popov 91219e5367SNikita Popov class MatchSelectPatternTest : public ValueTrackingTest { 92219e5367SNikita Popov protected: 93134bec27SJames Molloy void expectPattern(const SelectPatternResult &P) { 94134bec27SJames Molloy Value *LHS, *RHS; 95134bec27SJames Molloy Instruction::CastOps CastOp; 96134bec27SJames Molloy SelectPatternResult R = matchSelectPattern(A, LHS, RHS, &CastOp); 97134bec27SJames Molloy EXPECT_EQ(P.Flavor, R.Flavor); 98134bec27SJames Molloy EXPECT_EQ(P.NaNBehavior, R.NaNBehavior); 99134bec27SJames Molloy EXPECT_EQ(P.Ordered, R.Ordered); 100134bec27SJames Molloy } 101219e5367SNikita Popov }; 102134bec27SJames Molloy 103219e5367SNikita Popov class ComputeKnownBitsTest : public ValueTrackingTest { 104219e5367SNikita Popov protected: 105219e5367SNikita Popov void expectKnownBits(uint64_t Zero, uint64_t One) { 106219e5367SNikita Popov auto Known = computeKnownBits(A, M->getDataLayout()); 107219e5367SNikita Popov ASSERT_FALSE(Known.hasConflict()); 108219e5367SNikita Popov EXPECT_EQ(Known.One.getZExtValue(), One); 109219e5367SNikita Popov EXPECT_EQ(Known.Zero.getZExtValue(), Zero); 110219e5367SNikita Popov } 111134bec27SJames Molloy }; 112134bec27SJames Molloy 113134bec27SJames Molloy } 114134bec27SJames Molloy 115134bec27SJames Molloy TEST_F(MatchSelectPatternTest, SimpleFMin) { 116134bec27SJames Molloy parseAssembly( 117134bec27SJames Molloy "define float @test(float %a) {\n" 118134bec27SJames Molloy " %1 = fcmp ult float %a, 5.0\n" 119134bec27SJames Molloy " %A = select i1 %1, float %a, float 5.0\n" 120134bec27SJames Molloy " ret float %A\n" 121134bec27SJames Molloy "}\n"); 122134bec27SJames Molloy expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false}); 123134bec27SJames Molloy } 124134bec27SJames Molloy 125134bec27SJames Molloy TEST_F(MatchSelectPatternTest, SimpleFMax) { 126134bec27SJames Molloy parseAssembly( 127134bec27SJames Molloy "define float @test(float %a) {\n" 128134bec27SJames Molloy " %1 = fcmp ogt float %a, 5.0\n" 129134bec27SJames Molloy " %A = select i1 %1, float %a, float 5.0\n" 130134bec27SJames Molloy " ret float %A\n" 131134bec27SJames Molloy "}\n"); 132134bec27SJames Molloy expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true}); 133134bec27SJames Molloy } 134134bec27SJames Molloy 135134bec27SJames Molloy TEST_F(MatchSelectPatternTest, SwappedFMax) { 136134bec27SJames Molloy parseAssembly( 137134bec27SJames Molloy "define float @test(float %a) {\n" 138134bec27SJames Molloy " %1 = fcmp olt float 5.0, %a\n" 139134bec27SJames Molloy " %A = select i1 %1, float %a, float 5.0\n" 140134bec27SJames Molloy " ret float %A\n" 141134bec27SJames Molloy "}\n"); 142134bec27SJames Molloy expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, false}); 143134bec27SJames Molloy } 144134bec27SJames Molloy 145134bec27SJames Molloy TEST_F(MatchSelectPatternTest, SwappedFMax2) { 146134bec27SJames Molloy parseAssembly( 147134bec27SJames Molloy "define float @test(float %a) {\n" 148134bec27SJames Molloy " %1 = fcmp olt float %a, 5.0\n" 149134bec27SJames Molloy " %A = select i1 %1, float 5.0, float %a\n" 150134bec27SJames Molloy " ret float %A\n" 151134bec27SJames Molloy "}\n"); 152134bec27SJames Molloy expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, false}); 153134bec27SJames Molloy } 154134bec27SJames Molloy 155134bec27SJames Molloy TEST_F(MatchSelectPatternTest, SwappedFMax3) { 156134bec27SJames Molloy parseAssembly( 157134bec27SJames Molloy "define float @test(float %a) {\n" 158134bec27SJames Molloy " %1 = fcmp ult float %a, 5.0\n" 159134bec27SJames Molloy " %A = select i1 %1, float 5.0, float %a\n" 160134bec27SJames Molloy " ret float %A\n" 161134bec27SJames Molloy "}\n"); 162134bec27SJames Molloy expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true}); 163134bec27SJames Molloy } 164134bec27SJames Molloy 165134bec27SJames Molloy TEST_F(MatchSelectPatternTest, FastFMin) { 166134bec27SJames Molloy parseAssembly( 167134bec27SJames Molloy "define float @test(float %a) {\n" 168134bec27SJames Molloy " %1 = fcmp nnan olt float %a, 5.0\n" 169134bec27SJames Molloy " %A = select i1 %1, float %a, float 5.0\n" 170134bec27SJames Molloy " ret float %A\n" 171134bec27SJames Molloy "}\n"); 172134bec27SJames Molloy expectPattern({SPF_FMINNUM, SPNB_RETURNS_ANY, false}); 173134bec27SJames Molloy } 174134bec27SJames Molloy 175134bec27SJames Molloy TEST_F(MatchSelectPatternTest, FMinConstantZero) { 176134bec27SJames Molloy parseAssembly( 177134bec27SJames Molloy "define float @test(float %a) {\n" 178134bec27SJames Molloy " %1 = fcmp ole float %a, 0.0\n" 179134bec27SJames Molloy " %A = select i1 %1, float %a, float 0.0\n" 180134bec27SJames Molloy " ret float %A\n" 181134bec27SJames Molloy "}\n"); 182134bec27SJames Molloy // This shouldn't be matched, as %a could be -0.0. 183134bec27SJames Molloy expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 184134bec27SJames Molloy } 185134bec27SJames Molloy 186134bec27SJames Molloy TEST_F(MatchSelectPatternTest, FMinConstantZeroNsz) { 187134bec27SJames Molloy parseAssembly( 188134bec27SJames Molloy "define float @test(float %a) {\n" 189134bec27SJames Molloy " %1 = fcmp nsz ole float %a, 0.0\n" 190134bec27SJames Molloy " %A = select i1 %1, float %a, float 0.0\n" 191134bec27SJames Molloy " ret float %A\n" 192134bec27SJames Molloy "}\n"); 193134bec27SJames Molloy // But this should be, because we've ignored signed zeroes. 194134bec27SJames Molloy expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true}); 195134bec27SJames Molloy } 196569cea65SJames Molloy 19703da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero1) { 19803da6e6aSSanjay Patel parseAssembly( 19903da6e6aSSanjay Patel "define float @test(float %a) {\n" 20003da6e6aSSanjay Patel " %1 = fcmp olt float -0.0, %a\n" 20103da6e6aSSanjay Patel " %A = select i1 %1, float 0.0, float %a\n" 20203da6e6aSSanjay Patel " ret float %A\n" 20303da6e6aSSanjay Patel "}\n"); 204e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 205e7c94ef1SSanjay Patel expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, true}); 20603da6e6aSSanjay Patel } 20703da6e6aSSanjay Patel 20803da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero2) { 20903da6e6aSSanjay Patel parseAssembly( 21003da6e6aSSanjay Patel "define float @test(float %a) {\n" 21103da6e6aSSanjay Patel " %1 = fcmp ogt float %a, -0.0\n" 21203da6e6aSSanjay Patel " %A = select i1 %1, float 0.0, float %a\n" 21303da6e6aSSanjay Patel " ret float %A\n" 21403da6e6aSSanjay Patel "}\n"); 215e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 216e7c94ef1SSanjay Patel expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false}); 21703da6e6aSSanjay Patel } 21803da6e6aSSanjay Patel 21903da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero3) { 22003da6e6aSSanjay Patel parseAssembly( 22103da6e6aSSanjay Patel "define float @test(float %a) {\n" 22203da6e6aSSanjay Patel " %1 = fcmp olt float 0.0, %a\n" 22303da6e6aSSanjay Patel " %A = select i1 %1, float -0.0, float %a\n" 22403da6e6aSSanjay Patel " ret float %A\n" 22503da6e6aSSanjay Patel "}\n"); 226e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 227e7c94ef1SSanjay Patel expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, true}); 22803da6e6aSSanjay Patel } 22903da6e6aSSanjay Patel 23003da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero4) { 23103da6e6aSSanjay Patel parseAssembly( 23203da6e6aSSanjay Patel "define float @test(float %a) {\n" 23303da6e6aSSanjay Patel " %1 = fcmp ogt float %a, 0.0\n" 23403da6e6aSSanjay Patel " %A = select i1 %1, float -0.0, float %a\n" 23503da6e6aSSanjay Patel " ret float %A\n" 23603da6e6aSSanjay Patel "}\n"); 237e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 238e7c94ef1SSanjay Patel expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false}); 23903da6e6aSSanjay Patel } 24003da6e6aSSanjay Patel 24103da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero5) { 24203da6e6aSSanjay Patel parseAssembly( 24303da6e6aSSanjay Patel "define float @test(float %a) {\n" 24403da6e6aSSanjay Patel " %1 = fcmp ogt float -0.0, %a\n" 24503da6e6aSSanjay Patel " %A = select i1 %1, float %a, float 0.0\n" 24603da6e6aSSanjay Patel " ret float %A\n" 24703da6e6aSSanjay Patel "}\n"); 248e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 249e7c94ef1SSanjay Patel expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, false}); 25003da6e6aSSanjay Patel } 25103da6e6aSSanjay Patel 25203da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero6) { 25303da6e6aSSanjay Patel parseAssembly( 25403da6e6aSSanjay Patel "define float @test(float %a) {\n" 25503da6e6aSSanjay Patel " %1 = fcmp olt float %a, -0.0\n" 25603da6e6aSSanjay Patel " %A = select i1 %1, float %a, float 0.0\n" 25703da6e6aSSanjay Patel " ret float %A\n" 25803da6e6aSSanjay Patel "}\n"); 259e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 260e7c94ef1SSanjay Patel expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true}); 26103da6e6aSSanjay Patel } 26203da6e6aSSanjay Patel 26303da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero7) { 26403da6e6aSSanjay Patel parseAssembly( 26503da6e6aSSanjay Patel "define float @test(float %a) {\n" 26603da6e6aSSanjay Patel " %1 = fcmp ogt float 0.0, %a\n" 26703da6e6aSSanjay Patel " %A = select i1 %1, float %a, float -0.0\n" 26803da6e6aSSanjay Patel " ret float %A\n" 26903da6e6aSSanjay Patel "}\n"); 270e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 271e7c94ef1SSanjay Patel expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, false}); 27203da6e6aSSanjay Patel } 27303da6e6aSSanjay Patel 27403da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero8) { 27503da6e6aSSanjay Patel parseAssembly( 27603da6e6aSSanjay Patel "define float @test(float %a) {\n" 27703da6e6aSSanjay Patel " %1 = fcmp olt float %a, 0.0\n" 27803da6e6aSSanjay Patel " %A = select i1 %1, float %a, float -0.0\n" 27903da6e6aSSanjay Patel " ret float %A\n" 28003da6e6aSSanjay Patel "}\n"); 281e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 282e7c94ef1SSanjay Patel expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true}); 28303da6e6aSSanjay Patel } 28403da6e6aSSanjay Patel 28503da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero1) { 28603da6e6aSSanjay Patel parseAssembly( 28703da6e6aSSanjay Patel "define float @test(float %a) {\n" 28803da6e6aSSanjay Patel " %1 = fcmp ogt float -0.0, %a\n" 28903da6e6aSSanjay Patel " %A = select i1 %1, float 0.0, float %a\n" 29003da6e6aSSanjay Patel " ret float %A\n" 29103da6e6aSSanjay Patel "}\n"); 292e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 293e7c94ef1SSanjay Patel expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, true}); 29403da6e6aSSanjay Patel } 29503da6e6aSSanjay Patel 29603da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero2) { 29703da6e6aSSanjay Patel parseAssembly( 29803da6e6aSSanjay Patel "define float @test(float %a) {\n" 29903da6e6aSSanjay Patel " %1 = fcmp olt float %a, -0.0\n" 30003da6e6aSSanjay Patel " %A = select i1 %1, float 0.0, float %a\n" 30103da6e6aSSanjay Patel " ret float %A\n" 30203da6e6aSSanjay Patel "}\n"); 303e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 304e7c94ef1SSanjay Patel expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, false}); 30503da6e6aSSanjay Patel } 30603da6e6aSSanjay Patel 30703da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero3) { 30803da6e6aSSanjay Patel parseAssembly( 30903da6e6aSSanjay Patel "define float @test(float %a) {\n" 31003da6e6aSSanjay Patel " %1 = fcmp ogt float 0.0, %a\n" 31103da6e6aSSanjay Patel " %A = select i1 %1, float -0.0, float %a\n" 31203da6e6aSSanjay Patel " ret float %A\n" 31303da6e6aSSanjay Patel "}\n"); 314e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 315e7c94ef1SSanjay Patel expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, true}); 31603da6e6aSSanjay Patel } 31703da6e6aSSanjay Patel 31803da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero4) { 31903da6e6aSSanjay Patel parseAssembly( 32003da6e6aSSanjay Patel "define float @test(float %a) {\n" 32103da6e6aSSanjay Patel " %1 = fcmp olt float %a, 0.0\n" 32203da6e6aSSanjay Patel " %A = select i1 %1, float -0.0, float %a\n" 32303da6e6aSSanjay Patel " ret float %A\n" 32403da6e6aSSanjay Patel "}\n"); 325e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 326e7c94ef1SSanjay Patel expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, false}); 32703da6e6aSSanjay Patel } 32803da6e6aSSanjay Patel 32903da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero5) { 33003da6e6aSSanjay Patel parseAssembly( 33103da6e6aSSanjay Patel "define float @test(float %a) {\n" 33203da6e6aSSanjay Patel " %1 = fcmp olt float -0.0, %a\n" 33303da6e6aSSanjay Patel " %A = select i1 %1, float %a, float 0.0\n" 33403da6e6aSSanjay Patel " ret float %A\n" 33503da6e6aSSanjay Patel "}\n"); 336e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 337e7c94ef1SSanjay Patel expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, false}); 33803da6e6aSSanjay Patel } 33903da6e6aSSanjay Patel 34003da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero6) { 34103da6e6aSSanjay Patel parseAssembly( 34203da6e6aSSanjay Patel "define float @test(float %a) {\n" 34303da6e6aSSanjay Patel " %1 = fcmp ogt float %a, -0.0\n" 34403da6e6aSSanjay Patel " %A = select i1 %1, float %a, float 0.0\n" 34503da6e6aSSanjay Patel " ret float %A\n" 34603da6e6aSSanjay Patel "}\n"); 347e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 348e7c94ef1SSanjay Patel expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true}); 34903da6e6aSSanjay Patel } 35003da6e6aSSanjay Patel 35103da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero7) { 35203da6e6aSSanjay Patel parseAssembly( 35303da6e6aSSanjay Patel "define float @test(float %a) {\n" 35403da6e6aSSanjay Patel " %1 = fcmp olt float 0.0, %a\n" 35503da6e6aSSanjay Patel " %A = select i1 %1, float %a, float -0.0\n" 35603da6e6aSSanjay Patel " ret float %A\n" 35703da6e6aSSanjay Patel "}\n"); 358e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 359e7c94ef1SSanjay Patel expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, false}); 36003da6e6aSSanjay Patel } 36103da6e6aSSanjay Patel 36203da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero8) { 36303da6e6aSSanjay Patel parseAssembly( 36403da6e6aSSanjay Patel "define float @test(float %a) {\n" 36503da6e6aSSanjay Patel " %1 = fcmp ogt float %a, 0.0\n" 36603da6e6aSSanjay Patel " %A = select i1 %1, float %a, float -0.0\n" 36703da6e6aSSanjay Patel " ret float %A\n" 36803da6e6aSSanjay Patel "}\n"); 369e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 370e7c94ef1SSanjay Patel expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true}); 371e7c94ef1SSanjay Patel } 372e7c94ef1SSanjay Patel 373e7c94ef1SSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZeroVecUndef) { 374e7c94ef1SSanjay Patel parseAssembly( 375e7c94ef1SSanjay Patel "define <2 x float> @test(<2 x float> %a) {\n" 376e7c94ef1SSanjay Patel " %1 = fcmp ogt <2 x float> %a, <float -0.0, float -0.0>\n" 377e7c94ef1SSanjay Patel " %A = select <2 x i1> %1, <2 x float> <float undef, float 0.0>, <2 x float> %a\n" 378e7c94ef1SSanjay Patel " ret <2 x float> %A\n" 379e7c94ef1SSanjay Patel "}\n"); 380e7c94ef1SSanjay Patel // An undef in a vector constant can not be back-propagated for this analysis. 381e7c94ef1SSanjay Patel expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 382e7c94ef1SSanjay Patel } 383e7c94ef1SSanjay Patel 384e7c94ef1SSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZeroVecUndef) { 385e7c94ef1SSanjay Patel parseAssembly( 386e7c94ef1SSanjay Patel "define <2 x float> @test(<2 x float> %a) {\n" 387e7c94ef1SSanjay Patel " %1 = fcmp ogt <2 x float> %a, zeroinitializer\n" 388e7c94ef1SSanjay Patel " %A = select <2 x i1> %1, <2 x float> %a, <2 x float> <float -0.0, float undef>\n" 389e7c94ef1SSanjay Patel " ret <2 x float> %A\n" 390e7c94ef1SSanjay Patel "}\n"); 391e7c94ef1SSanjay Patel // An undef in a vector constant can not be back-propagated for this analysis. 39203da6e6aSSanjay Patel expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 39303da6e6aSSanjay Patel } 39403da6e6aSSanjay Patel 39530f1d691SThomas Lively TEST_F(MatchSelectPatternTest, VectorFMinimum) { 396d47b5c7bSThomas Lively parseAssembly( 397d47b5c7bSThomas Lively "define <4 x float> @test(<4 x float> %a) {\n" 398d47b5c7bSThomas Lively " %1 = fcmp ule <4 x float> %a, \n" 399d47b5c7bSThomas Lively " <float 5.0, float 5.0, float 5.0, float 5.0>\n" 400d47b5c7bSThomas Lively " %A = select <4 x i1> %1, <4 x float> %a,\n" 401d47b5c7bSThomas Lively " <4 x float> <float 5.0, float 5.0, float 5.0, float 5.0>\n" 402d47b5c7bSThomas Lively " ret <4 x float> %A\n" 403d47b5c7bSThomas Lively "}\n"); 404d47b5c7bSThomas Lively // Check that pattern matching works on vectors where each lane has the same 405d47b5c7bSThomas Lively // unordered pattern. 406d47b5c7bSThomas Lively expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false}); 407d47b5c7bSThomas Lively } 408d47b5c7bSThomas Lively 409d47b5c7bSThomas Lively TEST_F(MatchSelectPatternTest, VectorFMinOtherOrdered) { 410d47b5c7bSThomas Lively parseAssembly( 411d47b5c7bSThomas Lively "define <4 x float> @test(<4 x float> %a) {\n" 412d47b5c7bSThomas Lively " %1 = fcmp ole <4 x float> %a, \n" 413d47b5c7bSThomas Lively " <float 5.0, float 5.0, float 5.0, float 5.0>\n" 414d47b5c7bSThomas Lively " %A = select <4 x i1> %1, <4 x float> %a,\n" 415d47b5c7bSThomas Lively " <4 x float> <float 5.0, float 5.0, float 5.0, float 5.0>\n" 416d47b5c7bSThomas Lively " ret <4 x float> %A\n" 417d47b5c7bSThomas Lively "}\n"); 418d47b5c7bSThomas Lively // Check that pattern matching works on vectors where each lane has the same 419d47b5c7bSThomas Lively // ordered pattern. 420d47b5c7bSThomas Lively expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true}); 421d47b5c7bSThomas Lively } 422d47b5c7bSThomas Lively 42330f1d691SThomas Lively TEST_F(MatchSelectPatternTest, VectorNotFMinimum) { 424d47b5c7bSThomas Lively parseAssembly( 425d47b5c7bSThomas Lively "define <4 x float> @test(<4 x float> %a) {\n" 426d47b5c7bSThomas Lively " %1 = fcmp ule <4 x float> %a, \n" 427d47b5c7bSThomas Lively " <float 5.0, float 0x7ff8000000000000, float 5.0, float 5.0>\n" 428d47b5c7bSThomas Lively " %A = select <4 x i1> %1, <4 x float> %a,\n" 429d47b5c7bSThomas Lively " <4 x float> <float 5.0, float 0x7ff8000000000000, float 5.0, float " 430d47b5c7bSThomas Lively "5.0>\n" 431d47b5c7bSThomas Lively " ret <4 x float> %A\n" 432d47b5c7bSThomas Lively "}\n"); 433d47b5c7bSThomas Lively // The lane that contains a NaN (0x7ff80...) behaves like a 434d47b5c7bSThomas Lively // non-NaN-propagating min and the other lines behave like a NaN-propagating 435d47b5c7bSThomas Lively // min, so check that neither is returned. 436d47b5c7bSThomas Lively expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 437d47b5c7bSThomas Lively } 438d47b5c7bSThomas Lively 439d47b5c7bSThomas Lively TEST_F(MatchSelectPatternTest, VectorNotFMinZero) { 440d47b5c7bSThomas Lively parseAssembly( 441d47b5c7bSThomas Lively "define <4 x float> @test(<4 x float> %a) {\n" 442d47b5c7bSThomas Lively " %1 = fcmp ule <4 x float> %a, \n" 443d47b5c7bSThomas Lively " <float 5.0, float -0.0, float 5.0, float 5.0>\n" 444d47b5c7bSThomas Lively " %A = select <4 x i1> %1, <4 x float> %a,\n" 445d47b5c7bSThomas Lively " <4 x float> <float 5.0, float 0.0, float 5.0, float 5.0>\n" 446d47b5c7bSThomas Lively " ret <4 x float> %A\n" 447d47b5c7bSThomas Lively "}\n"); 448d47b5c7bSThomas Lively // Always selects the second lane of %a if it is positive or negative zero, so 449d47b5c7bSThomas Lively // this is stricter than a min. 450d47b5c7bSThomas Lively expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 451d47b5c7bSThomas Lively } 452d47b5c7bSThomas Lively 453569cea65SJames Molloy TEST_F(MatchSelectPatternTest, DoubleCastU) { 454569cea65SJames Molloy parseAssembly( 455569cea65SJames Molloy "define i32 @test(i8 %a, i8 %b) {\n" 456569cea65SJames Molloy " %1 = icmp ult i8 %a, %b\n" 457569cea65SJames Molloy " %2 = zext i8 %a to i32\n" 458569cea65SJames Molloy " %3 = zext i8 %b to i32\n" 459569cea65SJames Molloy " %A = select i1 %1, i32 %2, i32 %3\n" 460569cea65SJames Molloy " ret i32 %A\n" 461569cea65SJames Molloy "}\n"); 462569cea65SJames Molloy // We should be able to look through the situation where we cast both operands 463569cea65SJames Molloy // to the select. 464569cea65SJames Molloy expectPattern({SPF_UMIN, SPNB_NA, false}); 465569cea65SJames Molloy } 466569cea65SJames Molloy 467569cea65SJames Molloy TEST_F(MatchSelectPatternTest, DoubleCastS) { 468569cea65SJames Molloy parseAssembly( 469569cea65SJames Molloy "define i32 @test(i8 %a, i8 %b) {\n" 470569cea65SJames Molloy " %1 = icmp slt i8 %a, %b\n" 471569cea65SJames Molloy " %2 = sext i8 %a to i32\n" 472569cea65SJames Molloy " %3 = sext i8 %b to i32\n" 473569cea65SJames Molloy " %A = select i1 %1, i32 %2, i32 %3\n" 474569cea65SJames Molloy " ret i32 %A\n" 475569cea65SJames Molloy "}\n"); 476569cea65SJames Molloy // We should be able to look through the situation where we cast both operands 477569cea65SJames Molloy // to the select. 478569cea65SJames Molloy expectPattern({SPF_SMIN, SPNB_NA, false}); 479569cea65SJames Molloy } 480569cea65SJames Molloy 481569cea65SJames Molloy TEST_F(MatchSelectPatternTest, DoubleCastBad) { 482569cea65SJames Molloy parseAssembly( 483569cea65SJames Molloy "define i32 @test(i8 %a, i8 %b) {\n" 484569cea65SJames Molloy " %1 = icmp ult i8 %a, %b\n" 485569cea65SJames Molloy " %2 = zext i8 %a to i32\n" 486569cea65SJames Molloy " %3 = sext i8 %b to i32\n" 487569cea65SJames Molloy " %A = select i1 %1, i32 %2, i32 %3\n" 488569cea65SJames Molloy " ret i32 %A\n" 489569cea65SJames Molloy "}\n"); 490687a8448SJames Molloy // The cast types here aren't the same, so we cannot match an UMIN. 491569cea65SJames Molloy expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 492569cea65SJames Molloy } 4933bb2dbd6SSanjoy Das 4946d343740SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotSMin) { 4956d343740SSanjay Patel parseAssembly( 4966d343740SSanjay Patel "define i8 @test(i8 %a, i8 %b) {\n" 4976d343740SSanjay Patel " %cmp = icmp sgt i8 %a, %b\n" 4986d343740SSanjay Patel " %an = xor i8 %a, -1\n" 4996d343740SSanjay Patel " %bn = xor i8 %b, -1\n" 5006d343740SSanjay Patel " %A = select i1 %cmp, i8 %an, i8 %bn\n" 5016d343740SSanjay Patel " ret i8 %A\n" 5026d343740SSanjay Patel "}\n"); 503867f0c3cSSanjay Patel expectPattern({SPF_SMIN, SPNB_NA, false}); 5046d343740SSanjay Patel } 5056d343740SSanjay Patel 5066d343740SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotSMinSwap) { 5076d343740SSanjay Patel parseAssembly( 508463143f0SSanjay Patel "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n" 509463143f0SSanjay Patel " %cmp = icmp slt <2 x i8> %a, %b\n" 510463143f0SSanjay Patel " %an = xor <2 x i8> %a, <i8 -1, i8-1>\n" 511463143f0SSanjay Patel " %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n" 512463143f0SSanjay Patel " %A = select <2 x i1> %cmp, <2 x i8> %bn, <2 x i8> %an\n" 513463143f0SSanjay Patel " ret <2 x i8> %A\n" 5146d343740SSanjay Patel "}\n"); 515867f0c3cSSanjay Patel expectPattern({SPF_SMIN, SPNB_NA, false}); 5166d343740SSanjay Patel } 5176d343740SSanjay Patel 5186d343740SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotSMax) { 5196d343740SSanjay Patel parseAssembly( 5206d343740SSanjay Patel "define i8 @test(i8 %a, i8 %b) {\n" 5216d343740SSanjay Patel " %cmp = icmp slt i8 %a, %b\n" 5226d343740SSanjay Patel " %an = xor i8 %a, -1\n" 5236d343740SSanjay Patel " %bn = xor i8 %b, -1\n" 5246d343740SSanjay Patel " %A = select i1 %cmp, i8 %an, i8 %bn\n" 5256d343740SSanjay Patel " ret i8 %A\n" 5266d343740SSanjay Patel "}\n"); 527867f0c3cSSanjay Patel expectPattern({SPF_SMAX, SPNB_NA, false}); 5286d343740SSanjay Patel } 5296d343740SSanjay Patel 5306d343740SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotSMaxSwap) { 5316d343740SSanjay Patel parseAssembly( 532463143f0SSanjay Patel "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n" 533463143f0SSanjay Patel " %cmp = icmp sgt <2 x i8> %a, %b\n" 534463143f0SSanjay Patel " %an = xor <2 x i8> %a, <i8 -1, i8-1>\n" 535463143f0SSanjay Patel " %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n" 536463143f0SSanjay Patel " %A = select <2 x i1> %cmp, <2 x i8> %bn, <2 x i8> %an\n" 537463143f0SSanjay Patel " ret <2 x i8> %A\n" 538463143f0SSanjay Patel "}\n"); 539463143f0SSanjay Patel expectPattern({SPF_SMAX, SPNB_NA, false}); 540463143f0SSanjay Patel } 541463143f0SSanjay Patel 542463143f0SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotUMin) { 543463143f0SSanjay Patel parseAssembly( 544463143f0SSanjay Patel "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n" 545463143f0SSanjay Patel " %cmp = icmp ugt <2 x i8> %a, %b\n" 546463143f0SSanjay Patel " %an = xor <2 x i8> %a, <i8 -1, i8-1>\n" 547463143f0SSanjay Patel " %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n" 548463143f0SSanjay Patel " %A = select <2 x i1> %cmp, <2 x i8> %an, <2 x i8> %bn\n" 549463143f0SSanjay Patel " ret <2 x i8> %A\n" 550463143f0SSanjay Patel "}\n"); 551fbb1b43fSSanjay Patel expectPattern({SPF_UMIN, SPNB_NA, false}); 552463143f0SSanjay Patel } 553463143f0SSanjay Patel 554463143f0SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotUMinSwap) { 555463143f0SSanjay Patel parseAssembly( 5566d343740SSanjay Patel "define i8 @test(i8 %a, i8 %b) {\n" 557463143f0SSanjay Patel " %cmp = icmp ult i8 %a, %b\n" 5586d343740SSanjay Patel " %an = xor i8 %a, -1\n" 5596d343740SSanjay Patel " %bn = xor i8 %b, -1\n" 5606d343740SSanjay Patel " %A = select i1 %cmp, i8 %bn, i8 %an\n" 5616d343740SSanjay Patel " ret i8 %A\n" 5626d343740SSanjay Patel "}\n"); 563fbb1b43fSSanjay Patel expectPattern({SPF_UMIN, SPNB_NA, false}); 564463143f0SSanjay Patel } 565463143f0SSanjay Patel 566463143f0SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotUMax) { 567463143f0SSanjay Patel parseAssembly( 568463143f0SSanjay Patel "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n" 569463143f0SSanjay Patel " %cmp = icmp ult <2 x i8> %a, %b\n" 570463143f0SSanjay Patel " %an = xor <2 x i8> %a, <i8 -1, i8-1>\n" 571463143f0SSanjay Patel " %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n" 572463143f0SSanjay Patel " %A = select <2 x i1> %cmp, <2 x i8> %an, <2 x i8> %bn\n" 573463143f0SSanjay Patel " ret <2 x i8> %A\n" 574463143f0SSanjay Patel "}\n"); 575fbb1b43fSSanjay Patel expectPattern({SPF_UMAX, SPNB_NA, false}); 576463143f0SSanjay Patel } 577463143f0SSanjay Patel 578463143f0SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotUMaxSwap) { 579463143f0SSanjay Patel parseAssembly( 580463143f0SSanjay Patel "define i8 @test(i8 %a, i8 %b) {\n" 581463143f0SSanjay Patel " %cmp = icmp ugt i8 %a, %b\n" 582463143f0SSanjay Patel " %an = xor i8 %a, -1\n" 583463143f0SSanjay Patel " %bn = xor i8 %b, -1\n" 584463143f0SSanjay Patel " %A = select i1 %cmp, i8 %bn, i8 %an\n" 585463143f0SSanjay Patel " ret i8 %A\n" 586463143f0SSanjay Patel "}\n"); 587fbb1b43fSSanjay Patel expectPattern({SPF_UMAX, SPNB_NA, false}); 588463143f0SSanjay Patel } 589463143f0SSanjay Patel 590463143f0SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotEq) { 591463143f0SSanjay Patel parseAssembly( 592463143f0SSanjay Patel "define i8 @test(i8 %a, i8 %b) {\n" 593463143f0SSanjay Patel " %cmp = icmp eq i8 %a, %b\n" 594463143f0SSanjay Patel " %an = xor i8 %a, -1\n" 595463143f0SSanjay Patel " %bn = xor i8 %b, -1\n" 596463143f0SSanjay Patel " %A = select i1 %cmp, i8 %bn, i8 %an\n" 597463143f0SSanjay Patel " ret i8 %A\n" 598463143f0SSanjay Patel "}\n"); 599463143f0SSanjay Patel expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 600463143f0SSanjay Patel } 601463143f0SSanjay Patel 602463143f0SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotNe) { 603463143f0SSanjay Patel parseAssembly( 604463143f0SSanjay Patel "define i8 @test(i8 %a, i8 %b) {\n" 605463143f0SSanjay Patel " %cmp = icmp ne i8 %a, %b\n" 606463143f0SSanjay Patel " %an = xor i8 %a, -1\n" 607463143f0SSanjay Patel " %bn = xor i8 %b, -1\n" 608463143f0SSanjay Patel " %A = select i1 %cmp, i8 %bn, i8 %an\n" 609463143f0SSanjay Patel " ret i8 %A\n" 610463143f0SSanjay Patel "}\n"); 611463143f0SSanjay Patel expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 6126d343740SSanjay Patel } 6136d343740SSanjay Patel 6143bb2dbd6SSanjoy Das TEST(ValueTracking, GuaranteedToTransferExecutionToSuccessor) { 6153bb2dbd6SSanjoy Das StringRef Assembly = 6163bb2dbd6SSanjoy Das "declare void @nounwind_readonly(i32*) nounwind readonly " 6173bb2dbd6SSanjoy Das "declare void @nounwind_argmemonly(i32*) nounwind argmemonly " 6185d12b976SNikita Popov "declare void @nounwind_willreturn(i32*) nounwind willreturn " 6193bb2dbd6SSanjoy Das "declare void @throws_but_readonly(i32*) readonly " 6203bb2dbd6SSanjoy Das "declare void @throws_but_argmemonly(i32*) argmemonly " 6215d12b976SNikita Popov "declare void @throws_but_willreturn(i32*) willreturn " 6223bb2dbd6SSanjoy Das " " 6233bb2dbd6SSanjoy Das "declare void @unknown(i32*) " 6243bb2dbd6SSanjoy Das " " 6253bb2dbd6SSanjoy Das "define void @f(i32* %p) { " 6263bb2dbd6SSanjoy Das " call void @nounwind_readonly(i32* %p) " 6273bb2dbd6SSanjoy Das " call void @nounwind_argmemonly(i32* %p) " 6285d12b976SNikita Popov " call void @nounwind_willreturn(i32* %p)" 6293bb2dbd6SSanjoy Das " call void @throws_but_readonly(i32* %p) " 6303bb2dbd6SSanjoy Das " call void @throws_but_argmemonly(i32* %p) " 6315d12b976SNikita Popov " call void @throws_but_willreturn(i32* %p) " 6323bb2dbd6SSanjoy Das " call void @unknown(i32* %p) nounwind readonly " 6333bb2dbd6SSanjoy Das " call void @unknown(i32* %p) nounwind argmemonly " 6345d12b976SNikita Popov " call void @unknown(i32* %p) nounwind willreturn " 6353bb2dbd6SSanjoy Das " call void @unknown(i32* %p) readonly " 6363bb2dbd6SSanjoy Das " call void @unknown(i32* %p) argmemonly " 6375d12b976SNikita Popov " call void @unknown(i32* %p) willreturn " 6383bb2dbd6SSanjoy Das " ret void " 6393bb2dbd6SSanjoy Das "} "; 6403bb2dbd6SSanjoy Das 6413bb2dbd6SSanjoy Das LLVMContext Context; 6423bb2dbd6SSanjoy Das SMDiagnostic Error; 6433bb2dbd6SSanjoy Das auto M = parseAssemblyString(Assembly, Error, Context); 6443bb2dbd6SSanjoy Das assert(M && "Bad assembly?"); 6453bb2dbd6SSanjoy Das 6463bb2dbd6SSanjoy Das auto *F = M->getFunction("f"); 6473bb2dbd6SSanjoy Das assert(F && "Bad assembly?"); 6483bb2dbd6SSanjoy Das 6493bb2dbd6SSanjoy Das auto &BB = F->getEntryBlock(); 6508580cd4eSCraig Topper bool ExpectedAnswers[] = { 6515d12b976SNikita Popov false, // call void @nounwind_readonly(i32* %p) 6525d12b976SNikita Popov false, // call void @nounwind_argmemonly(i32* %p) 6535d12b976SNikita Popov true, // call void @nounwind_willreturn(i32* %p) 6543bb2dbd6SSanjoy Das false, // call void @throws_but_readonly(i32* %p) 6553bb2dbd6SSanjoy Das false, // call void @throws_but_argmemonly(i32* %p) 6565d12b976SNikita Popov false, // call void @throws_but_willreturn(i32* %p) 6575d12b976SNikita Popov false, // call void @unknown(i32* %p) nounwind readonly 6585d12b976SNikita Popov false, // call void @unknown(i32* %p) nounwind argmemonly 6595d12b976SNikita Popov true, // call void @unknown(i32* %p) nounwind willreturn 6603bb2dbd6SSanjoy Das false, // call void @unknown(i32* %p) readonly 6613bb2dbd6SSanjoy Das false, // call void @unknown(i32* %p) argmemonly 6625d12b976SNikita Popov false, // call void @unknown(i32* %p) willreturn 6633bb2dbd6SSanjoy Das false, // ret void 6643bb2dbd6SSanjoy Das }; 6653bb2dbd6SSanjoy Das 6663bb2dbd6SSanjoy Das int Index = 0; 6673bb2dbd6SSanjoy Das for (auto &I : BB) { 6683bb2dbd6SSanjoy Das EXPECT_EQ(isGuaranteedToTransferExecutionToSuccessor(&I), 6693bb2dbd6SSanjoy Das ExpectedAnswers[Index]) 6703bb2dbd6SSanjoy Das << "Incorrect answer at instruction " << Index << " = " << I; 6713bb2dbd6SSanjoy Das Index++; 6723bb2dbd6SSanjoy Das } 6733bb2dbd6SSanjoy Das } 67439a684d1SSanjoy Das 675219e5367SNikita Popov TEST_F(ValueTrackingTest, ComputeNumSignBits_PR32045) { 676219e5367SNikita Popov parseAssembly( 677219e5367SNikita Popov "define i32 @test(i32 %a) {\n" 678219e5367SNikita Popov " %A = ashr i32 %a, -1\n" 679219e5367SNikita Popov " ret i32 %A\n" 680219e5367SNikita Popov "}\n"); 681219e5367SNikita Popov EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u); 68239a684d1SSanjoy Das } 68370dbd5fbSSimon Dardis 684a68096c7SSanjay Patel // No guarantees for canonical IR in this analysis, so this just bails out. 685219e5367SNikita Popov TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle) { 686219e5367SNikita Popov parseAssembly( 687219e5367SNikita Popov "define <2 x i32> @test() {\n" 688219e5367SNikita Popov " %A = shufflevector <2 x i32> undef, <2 x i32> undef, <2 x i32> <i32 0, i32 0>\n" 689219e5367SNikita Popov " ret <2 x i32> %A\n" 690219e5367SNikita Popov "}\n"); 691219e5367SNikita Popov EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u); 692a68096c7SSanjay Patel } 693a68096c7SSanjay Patel 69445a148deSSanjay Patel // No guarantees for canonical IR in this analysis, so a shuffle element that 69545a148deSSanjay Patel // references an undef value means this can't return any extra information. 696219e5367SNikita Popov TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle2) { 697219e5367SNikita Popov parseAssembly( 698219e5367SNikita Popov "define <2 x i32> @test(<2 x i1> %x) {\n" 699219e5367SNikita Popov " %sext = sext <2 x i1> %x to <2 x i32>\n" 700219e5367SNikita Popov " %A = shufflevector <2 x i32> %sext, <2 x i32> undef, <2 x i32> <i32 0, i32 2>\n" 701219e5367SNikita Popov " ret <2 x i32> %A\n" 702219e5367SNikita Popov "}\n"); 703219e5367SNikita Popov EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u); 704994543abSJuneyoung Lee } 705994543abSJuneyoung Lee 7060f2c1801SJuneyoung Lee TEST_F(ValueTrackingTest, impliesPoisonTest_Identity) { 7070f2c1801SJuneyoung Lee parseAssembly("define void @test(i32 %x, i32 %y) {\n" 7080f2c1801SJuneyoung Lee " %A = add i32 %x, %y\n" 7090f2c1801SJuneyoung Lee " ret void\n" 7100f2c1801SJuneyoung Lee "}"); 7110f2c1801SJuneyoung Lee EXPECT_TRUE(impliesPoison(A, A)); 7120f2c1801SJuneyoung Lee } 7130f2c1801SJuneyoung Lee 7140f2c1801SJuneyoung Lee TEST_F(ValueTrackingTest, impliesPoisonTest_ICmp) { 7150f2c1801SJuneyoung Lee parseAssembly("define void @test(i32 %x) {\n" 7160f2c1801SJuneyoung Lee " %A2 = icmp eq i32 %x, 0\n" 7170f2c1801SJuneyoung Lee " %A = icmp eq i32 %x, 1\n" 7180f2c1801SJuneyoung Lee " ret void\n" 7190f2c1801SJuneyoung Lee "}"); 7200f2c1801SJuneyoung Lee EXPECT_TRUE(impliesPoison(A2, A)); 7210f2c1801SJuneyoung Lee } 7220f2c1801SJuneyoung Lee 7230f2c1801SJuneyoung Lee TEST_F(ValueTrackingTest, impliesPoisonTest_ICmpUnknown) { 7240f2c1801SJuneyoung Lee parseAssembly("define void @test(i32 %x, i32 %y) {\n" 7250f2c1801SJuneyoung Lee " %A2 = icmp eq i32 %x, %y\n" 7260f2c1801SJuneyoung Lee " %A = icmp eq i32 %x, 1\n" 7270f2c1801SJuneyoung Lee " ret void\n" 7280f2c1801SJuneyoung Lee "}"); 7290f2c1801SJuneyoung Lee EXPECT_FALSE(impliesPoison(A2, A)); 7300f2c1801SJuneyoung Lee } 7310f2c1801SJuneyoung Lee 7320f2c1801SJuneyoung Lee TEST_F(ValueTrackingTest, impliesPoisonTest_AddNswOkay) { 7330f2c1801SJuneyoung Lee parseAssembly("define void @test(i32 %x) {\n" 7340f2c1801SJuneyoung Lee " %A2 = add nsw i32 %x, 1\n" 7350f2c1801SJuneyoung Lee " %A = add i32 %A2, 1\n" 7360f2c1801SJuneyoung Lee " ret void\n" 7370f2c1801SJuneyoung Lee "}"); 7380f2c1801SJuneyoung Lee EXPECT_TRUE(impliesPoison(A2, A)); 7390f2c1801SJuneyoung Lee } 7400f2c1801SJuneyoung Lee 7410f2c1801SJuneyoung Lee TEST_F(ValueTrackingTest, impliesPoisonTest_AddNswOkay2) { 7420f2c1801SJuneyoung Lee parseAssembly("define void @test(i32 %x) {\n" 7430f2c1801SJuneyoung Lee " %A2 = add i32 %x, 1\n" 7440f2c1801SJuneyoung Lee " %A = add nsw i32 %A2, 1\n" 7450f2c1801SJuneyoung Lee " ret void\n" 7460f2c1801SJuneyoung Lee "}"); 7470f2c1801SJuneyoung Lee EXPECT_TRUE(impliesPoison(A2, A)); 7480f2c1801SJuneyoung Lee } 7490f2c1801SJuneyoung Lee 7500f2c1801SJuneyoung Lee TEST_F(ValueTrackingTest, impliesPoisonTest_AddNsw) { 7510f2c1801SJuneyoung Lee parseAssembly("define void @test(i32 %x) {\n" 7520f2c1801SJuneyoung Lee " %A2 = add nsw i32 %x, 1\n" 7530f2c1801SJuneyoung Lee " %A = add i32 %x, 1\n" 7540f2c1801SJuneyoung Lee " ret void\n" 7550f2c1801SJuneyoung Lee "}"); 7560f2c1801SJuneyoung Lee EXPECT_FALSE(impliesPoison(A2, A)); 7570f2c1801SJuneyoung Lee } 7580f2c1801SJuneyoung Lee 759051ec9f5SNikita Popov TEST_F(ValueTrackingTest, impliesPoisonTest_Cmp) { 760051ec9f5SNikita Popov parseAssembly("define void @test(i32 %x, i32 %y, i1 %c) {\n" 761051ec9f5SNikita Popov " %A2 = icmp eq i32 %x, %y\n" 762051ec9f5SNikita Popov " %A0 = icmp ult i32 %x, %y\n" 763051ec9f5SNikita Popov " %A = or i1 %A0, %c\n" 764051ec9f5SNikita Popov " ret void\n" 765051ec9f5SNikita Popov "}"); 766051ec9f5SNikita Popov EXPECT_TRUE(impliesPoison(A2, A)); 767051ec9f5SNikita Popov } 768051ec9f5SNikita Popov 769051ec9f5SNikita Popov TEST_F(ValueTrackingTest, impliesPoisonTest_FCmpFMF) { 770051ec9f5SNikita Popov parseAssembly("define void @test(float %x, float %y, i1 %c) {\n" 771051ec9f5SNikita Popov " %A2 = fcmp nnan oeq float %x, %y\n" 772051ec9f5SNikita Popov " %A0 = fcmp olt float %x, %y\n" 773051ec9f5SNikita Popov " %A = or i1 %A0, %c\n" 774051ec9f5SNikita Popov " ret void\n" 775051ec9f5SNikita Popov "}"); 776051ec9f5SNikita Popov EXPECT_FALSE(impliesPoison(A2, A)); 777051ec9f5SNikita Popov } 778051ec9f5SNikita Popov 779051ec9f5SNikita Popov TEST_F(ValueTrackingTest, impliesPoisonTest_AddSubSameOps) { 780051ec9f5SNikita Popov parseAssembly("define void @test(i32 %x, i32 %y, i1 %c) {\n" 781051ec9f5SNikita Popov " %A2 = add i32 %x, %y\n" 782051ec9f5SNikita Popov " %A = sub i32 %x, %y\n" 783051ec9f5SNikita Popov " ret void\n" 784051ec9f5SNikita Popov "}"); 785051ec9f5SNikita Popov EXPECT_TRUE(impliesPoison(A2, A)); 786051ec9f5SNikita Popov } 787051ec9f5SNikita Popov 788051ec9f5SNikita Popov TEST_F(ValueTrackingTest, impliesPoisonTest_MaskCmp) { 789051ec9f5SNikita Popov parseAssembly("define void @test(i32 %x, i32 %y, i1 %c) {\n" 790051ec9f5SNikita Popov " %M2 = and i32 %x, 7\n" 791051ec9f5SNikita Popov " %A2 = icmp eq i32 %M2, 1\n" 792051ec9f5SNikita Popov " %M = and i32 %x, 15\n" 793051ec9f5SNikita Popov " %A = icmp eq i32 %M, 3\n" 794051ec9f5SNikita Popov " ret void\n" 795051ec9f5SNikita Popov "}"); 796051ec9f5SNikita Popov EXPECT_TRUE(impliesPoison(A2, A)); 797051ec9f5SNikita Popov } 798051ec9f5SNikita Popov 7998351f9b5SNikita Popov TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle_Pointers) { 8008351f9b5SNikita Popov parseAssembly( 8018351f9b5SNikita Popov "define <2 x i32*> @test(<2 x i32*> %x) {\n" 8028351f9b5SNikita Popov " %A = shufflevector <2 x i32*> zeroinitializer, <2 x i32*> undef, <2 x i32> zeroinitializer\n" 8038351f9b5SNikita Popov " ret <2 x i32*> %A\n" 8048351f9b5SNikita Popov "}\n"); 8058351f9b5SNikita Popov EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 64u); 8068351f9b5SNikita Popov } 8078351f9b5SNikita Popov 808e5f602d8SJuneyoung Lee TEST(ValueTracking, propagatesPoison) { 809df0b97daSJuneyoung Lee std::string AsmHead = 810df0b97daSJuneyoung Lee "declare i32 @g(i32)\n" 811df0b97daSJuneyoung Lee "declare {i32, i1} @llvm.sadd.with.overflow.i32(i32 %a, i32 %b)\n" 812df0b97daSJuneyoung Lee "declare {i32, i1} @llvm.ssub.with.overflow.i32(i32 %a, i32 %b)\n" 813df0b97daSJuneyoung Lee "declare {i32, i1} @llvm.smul.with.overflow.i32(i32 %a, i32 %b)\n" 814df0b97daSJuneyoung Lee "declare {i32, i1} @llvm.uadd.with.overflow.i32(i32 %a, i32 %b)\n" 815df0b97daSJuneyoung Lee "declare {i32, i1} @llvm.usub.with.overflow.i32(i32 %a, i32 %b)\n" 816df0b97daSJuneyoung Lee "declare {i32, i1} @llvm.umul.with.overflow.i32(i32 %a, i32 %b)\n" 817a993bb08SSanjay Patel "declare float @llvm.sqrt.f32(float)\n" 8184c7f820bSBjorn Pettersson "declare float @llvm.powi.f32.i32(float, i32)\n" 819a993bb08SSanjay Patel "declare float @llvm.sin.f32(float)\n" 820a993bb08SSanjay Patel "declare float @llvm.cos.f32(float)\n" 821a993bb08SSanjay Patel "declare float @llvm.pow.f32(float, float)\n" 822a993bb08SSanjay Patel "declare float @llvm.exp.f32(float)\n" 823a993bb08SSanjay Patel "declare float @llvm.exp2.f32(float)\n" 824a993bb08SSanjay Patel "declare float @llvm.log.f32(float)\n" 825a993bb08SSanjay Patel "declare float @llvm.log10.f32(float)\n" 826a993bb08SSanjay Patel "declare float @llvm.log2.f32(float)\n" 827a993bb08SSanjay Patel "declare float @llvm.fma.f32(float, float, float)\n" 828a993bb08SSanjay Patel "declare float @llvm.fabs.f32(float)\n" 829a993bb08SSanjay Patel "declare float @llvm.minnum.f32(float, float)\n" 830a993bb08SSanjay Patel "declare float @llvm.maxnum.f32(float, float)\n" 831a993bb08SSanjay Patel "declare float @llvm.minimum.f32(float, float)\n" 832a993bb08SSanjay Patel "declare float @llvm.maximum.f32(float, float)\n" 833a993bb08SSanjay Patel "declare float @llvm.copysign.f32(float, float)\n" 834a993bb08SSanjay Patel "declare float @llvm.floor.f32(float)\n" 835a993bb08SSanjay Patel "declare float @llvm.ceil.f32(float)\n" 836a993bb08SSanjay Patel "declare float @llvm.trunc.f32(float)\n" 837a993bb08SSanjay Patel "declare float @llvm.rint.f32(float)\n" 838a993bb08SSanjay Patel "declare float @llvm.nearbyint.f32(float)\n" 839a993bb08SSanjay Patel "declare float @llvm.round.f32(float)\n" 840a993bb08SSanjay Patel "declare float @llvm.roundeven.f32(float)\n" 841a993bb08SSanjay Patel "declare i32 @llvm.lround.f32(float)\n" 842a993bb08SSanjay Patel "declare i64 @llvm.llround.f32(float)\n" 843a993bb08SSanjay Patel "declare i32 @llvm.lrint.f32(float)\n" 844a993bb08SSanjay Patel "declare i64 @llvm.llrint.f32(float)\n" 845a993bb08SSanjay Patel "declare float @llvm.fmuladd.f32(float, float, float)\n" 846e5f602d8SJuneyoung Lee "define void @f(i32 %x, i32 %y, float %fx, float %fy, " 847e5f602d8SJuneyoung Lee "i1 %cond, i8* %p) {\n"; 848e5f602d8SJuneyoung Lee std::string AsmTail = " ret void\n}"; 849e5f602d8SJuneyoung Lee // (propagates poison?, IR instruction) 850e5f602d8SJuneyoung Lee SmallVector<std::pair<bool, std::string>, 32> Data = { 851e5f602d8SJuneyoung Lee {true, "add i32 %x, %y"}, 852e5f602d8SJuneyoung Lee {true, "add nsw nuw i32 %x, %y"}, 853e5f602d8SJuneyoung Lee {true, "ashr i32 %x, %y"}, 854e5f602d8SJuneyoung Lee {true, "lshr exact i32 %x, 31"}, 855572e506bSSanjay Patel {true, "fadd float %fx, %fy"}, 856572e506bSSanjay Patel {true, "fsub float %fx, %fy"}, 857572e506bSSanjay Patel {true, "fmul float %fx, %fy"}, 858572e506bSSanjay Patel {true, "fdiv float %fx, %fy"}, 859572e506bSSanjay Patel {true, "frem float %fx, %fy"}, 860572e506bSSanjay Patel {true, "fneg float %fx"}, 861e5f602d8SJuneyoung Lee {true, "fcmp oeq float %fx, %fy"}, 862e5f602d8SJuneyoung Lee {true, "icmp eq i32 %x, %y"}, 863e5f602d8SJuneyoung Lee {true, "getelementptr i8, i8* %p, i32 %x"}, 864e5f602d8SJuneyoung Lee {true, "getelementptr inbounds i8, i8* %p, i32 %x"}, 865e5f602d8SJuneyoung Lee {true, "bitcast float %fx to i32"}, 866e5f602d8SJuneyoung Lee {false, "select i1 %cond, i32 %x, i32 %y"}, 867e5f602d8SJuneyoung Lee {false, "freeze i32 %x"}, 868e5f602d8SJuneyoung Lee {true, "udiv i32 %x, %y"}, 869e5f602d8SJuneyoung Lee {true, "urem i32 %x, %y"}, 870e5f602d8SJuneyoung Lee {true, "sdiv exact i32 %x, %y"}, 871e5f602d8SJuneyoung Lee {true, "srem i32 %x, %y"}, 872df0b97daSJuneyoung Lee {false, "call i32 @g(i32 %x)"}, 873df0b97daSJuneyoung Lee {true, "call {i32, i1} @llvm.sadd.with.overflow.i32(i32 %x, i32 %y)"}, 874df0b97daSJuneyoung Lee {true, "call {i32, i1} @llvm.ssub.with.overflow.i32(i32 %x, i32 %y)"}, 875df0b97daSJuneyoung Lee {true, "call {i32, i1} @llvm.smul.with.overflow.i32(i32 %x, i32 %y)"}, 876df0b97daSJuneyoung Lee {true, "call {i32, i1} @llvm.uadd.with.overflow.i32(i32 %x, i32 %y)"}, 877df0b97daSJuneyoung Lee {true, "call {i32, i1} @llvm.usub.with.overflow.i32(i32 %x, i32 %y)"}, 878a993bb08SSanjay Patel {true, "call {i32, i1} @llvm.umul.with.overflow.i32(i32 %x, i32 %y)"}, 879a993bb08SSanjay Patel {false, "call float @llvm.sqrt.f32(float %fx)"}, 8804c7f820bSBjorn Pettersson {false, "call float @llvm.powi.f32.i32(float %fx, i32 %x)"}, 881a993bb08SSanjay Patel {false, "call float @llvm.sin.f32(float %fx)"}, 882a993bb08SSanjay Patel {false, "call float @llvm.cos.f32(float %fx)"}, 883a993bb08SSanjay Patel {false, "call float @llvm.pow.f32(float %fx, float %fy)"}, 884a993bb08SSanjay Patel {false, "call float @llvm.exp.f32(float %fx)"}, 885a993bb08SSanjay Patel {false, "call float @llvm.exp2.f32(float %fx)"}, 886a993bb08SSanjay Patel {false, "call float @llvm.log.f32(float %fx)"}, 887a993bb08SSanjay Patel {false, "call float @llvm.log10.f32(float %fx)"}, 888a993bb08SSanjay Patel {false, "call float @llvm.log2.f32(float %fx)"}, 889a993bb08SSanjay Patel {false, "call float @llvm.fma.f32(float %fx, float %fx, float %fy)"}, 890a993bb08SSanjay Patel {false, "call float @llvm.fabs.f32(float %fx)"}, 891a993bb08SSanjay Patel {false, "call float @llvm.minnum.f32(float %fx, float %fy)"}, 892a993bb08SSanjay Patel {false, "call float @llvm.maxnum.f32(float %fx, float %fy)"}, 893a993bb08SSanjay Patel {false, "call float @llvm.minimum.f32(float %fx, float %fy)"}, 894a993bb08SSanjay Patel {false, "call float @llvm.maximum.f32(float %fx, float %fy)"}, 895a993bb08SSanjay Patel {false, "call float @llvm.copysign.f32(float %fx, float %fy)"}, 896a993bb08SSanjay Patel {false, "call float @llvm.floor.f32(float %fx)"}, 897a993bb08SSanjay Patel {false, "call float @llvm.ceil.f32(float %fx)"}, 898a993bb08SSanjay Patel {false, "call float @llvm.trunc.f32(float %fx)"}, 899a993bb08SSanjay Patel {false, "call float @llvm.rint.f32(float %fx)"}, 900a993bb08SSanjay Patel {false, "call float @llvm.nearbyint.f32(float %fx)"}, 901a993bb08SSanjay Patel {false, "call float @llvm.round.f32(float %fx)"}, 902a993bb08SSanjay Patel {false, "call float @llvm.roundeven.f32(float %fx)"}, 903a993bb08SSanjay Patel {false, "call i32 @llvm.lround.f32(float %fx)"}, 904a993bb08SSanjay Patel {false, "call i64 @llvm.llround.f32(float %fx)"}, 905a993bb08SSanjay Patel {false, "call i32 @llvm.lrint.f32(float %fx)"}, 906a993bb08SSanjay Patel {false, "call i64 @llvm.llrint.f32(float %fx)"}, 907a993bb08SSanjay Patel {false, "call float @llvm.fmuladd.f32(float %fx, float %fx, float %fy)"}}; 908e5f602d8SJuneyoung Lee 909e5f602d8SJuneyoung Lee std::string AssemblyStr = AsmHead; 910e5f602d8SJuneyoung Lee for (auto &Itm : Data) 911e5f602d8SJuneyoung Lee AssemblyStr += Itm.second + "\n"; 912e5f602d8SJuneyoung Lee AssemblyStr += AsmTail; 913e5f602d8SJuneyoung Lee 914e5f602d8SJuneyoung Lee LLVMContext Context; 915e5f602d8SJuneyoung Lee SMDiagnostic Error; 916e5f602d8SJuneyoung Lee auto M = parseAssemblyString(AssemblyStr, Error, Context); 917e5f602d8SJuneyoung Lee assert(M && "Bad assembly?"); 918e5f602d8SJuneyoung Lee 919e5f602d8SJuneyoung Lee auto *F = M->getFunction("f"); 920e5f602d8SJuneyoung Lee assert(F && "Bad assembly?"); 921e5f602d8SJuneyoung Lee 922e5f602d8SJuneyoung Lee auto &BB = F->getEntryBlock(); 923e5f602d8SJuneyoung Lee 924e5f602d8SJuneyoung Lee int Index = 0; 925e5f602d8SJuneyoung Lee for (auto &I : BB) { 926e5f602d8SJuneyoung Lee if (isa<ReturnInst>(&I)) 927e5f602d8SJuneyoung Lee break; 92825ce1e04SJuneyoung Lee EXPECT_EQ(propagatesPoison(cast<Operator>(&I)), Data[Index].first) 929e5f602d8SJuneyoung Lee << "Incorrect answer at instruction " << Index << " = " << I; 930e5f602d8SJuneyoung Lee Index++; 931e5f602d8SJuneyoung Lee } 932e5f602d8SJuneyoung Lee } 933e5f602d8SJuneyoung Lee 93425ce1e04SJuneyoung Lee TEST_F(ValueTrackingTest, programUndefinedIfPoison) { 93525ce1e04SJuneyoung Lee parseAssembly("declare i32 @any_num()" 93625ce1e04SJuneyoung Lee "define void @test(i32 %mask) {\n" 93725ce1e04SJuneyoung Lee " %A = call i32 @any_num()\n" 93825ce1e04SJuneyoung Lee " %B = or i32 %A, %mask\n" 93925ce1e04SJuneyoung Lee " udiv i32 1, %B" 94025ce1e04SJuneyoung Lee " ret void\n" 94125ce1e04SJuneyoung Lee "}\n"); 94225ce1e04SJuneyoung Lee // If %A was poison, udiv raises UB regardless of %mask's value 94325ce1e04SJuneyoung Lee EXPECT_EQ(programUndefinedIfPoison(A), true); 94425ce1e04SJuneyoung Lee } 94525ce1e04SJuneyoung Lee 94625ce1e04SJuneyoung Lee TEST_F(ValueTrackingTest, programUndefinedIfUndefOrPoison) { 94725ce1e04SJuneyoung Lee parseAssembly("declare i32 @any_num()" 94825ce1e04SJuneyoung Lee "define void @test(i32 %mask) {\n" 94925ce1e04SJuneyoung Lee " %A = call i32 @any_num()\n" 95025ce1e04SJuneyoung Lee " %B = or i32 %A, %mask\n" 95125ce1e04SJuneyoung Lee " udiv i32 1, %B" 95225ce1e04SJuneyoung Lee " ret void\n" 95325ce1e04SJuneyoung Lee "}\n"); 95425ce1e04SJuneyoung Lee // If %A was undef and %mask was 1, udiv does not raise UB 95525ce1e04SJuneyoung Lee EXPECT_EQ(programUndefinedIfUndefOrPoison(A), false); 95625ce1e04SJuneyoung Lee } 95725ce1e04SJuneyoung Lee 95825ce1e04SJuneyoung Lee TEST_F(ValueTrackingTest, isGuaranteedNotToBePoison_exploitBranchCond) { 95925ce1e04SJuneyoung Lee parseAssembly("declare i1 @any_bool()" 96025ce1e04SJuneyoung Lee "define void @test(i1 %y) {\n" 96125ce1e04SJuneyoung Lee " %A = call i1 @any_bool()\n" 96225ce1e04SJuneyoung Lee " %cond = and i1 %A, %y\n" 96325ce1e04SJuneyoung Lee " br i1 %cond, label %BB1, label %BB2\n" 96425ce1e04SJuneyoung Lee "BB1:\n" 96525ce1e04SJuneyoung Lee " ret void\n" 96625ce1e04SJuneyoung Lee "BB2:\n" 96725ce1e04SJuneyoung Lee " ret void\n" 96825ce1e04SJuneyoung Lee "}\n"); 96925ce1e04SJuneyoung Lee DominatorTree DT(*F); 97025ce1e04SJuneyoung Lee for (auto &BB : *F) { 97125ce1e04SJuneyoung Lee if (&BB == &F->getEntryBlock()) 97225ce1e04SJuneyoung Lee continue; 97325ce1e04SJuneyoung Lee 9749b3c2a72SJuneyoung Lee EXPECT_EQ(isGuaranteedNotToBePoison(A, nullptr, BB.getTerminator(), &DT), 9759b3c2a72SJuneyoung Lee true) 97625ce1e04SJuneyoung Lee << "isGuaranteedNotToBePoison does not hold at " << *BB.getTerminator(); 97725ce1e04SJuneyoung Lee } 97825ce1e04SJuneyoung Lee } 97925ce1e04SJuneyoung Lee 980ba8911d5SJuneyoung Lee TEST_F(ValueTrackingTest, isGuaranteedNotToBePoison_phi) { 981ba8911d5SJuneyoung Lee parseAssembly("declare i32 @any_i32(i32)" 982ba8911d5SJuneyoung Lee "define void @test() {\n" 983ba8911d5SJuneyoung Lee "ENTRY:\n" 984ba8911d5SJuneyoung Lee " br label %LOOP\n" 985ba8911d5SJuneyoung Lee "LOOP:\n" 986ba8911d5SJuneyoung Lee " %A = phi i32 [0, %ENTRY], [%A.next, %NEXT]\n" 987ba8911d5SJuneyoung Lee " %A.next = call i32 @any_i32(i32 %A)\n" 988ba8911d5SJuneyoung Lee " %cond = icmp eq i32 %A.next, 0\n" 989ba8911d5SJuneyoung Lee " br i1 %cond, label %NEXT, label %EXIT\n" 990ba8911d5SJuneyoung Lee "NEXT:\n" 991ba8911d5SJuneyoung Lee " br label %LOOP\n" 992ba8911d5SJuneyoung Lee "EXIT:\n" 993ba8911d5SJuneyoung Lee " ret void\n" 994ba8911d5SJuneyoung Lee "}\n"); 995ba8911d5SJuneyoung Lee DominatorTree DT(*F); 996ba8911d5SJuneyoung Lee for (auto &BB : *F) { 997ba8911d5SJuneyoung Lee if (BB.getName() == "LOOP") { 9989b3c2a72SJuneyoung Lee EXPECT_EQ(isGuaranteedNotToBePoison(A, nullptr, A, &DT), true) 999ba8911d5SJuneyoung Lee << "isGuaranteedNotToBePoison does not hold"; 1000ba8911d5SJuneyoung Lee } 1001ba8911d5SJuneyoung Lee } 1002ba8911d5SJuneyoung Lee } 1003ba8911d5SJuneyoung Lee 10041c452200SJuneyoung Lee TEST_F(ValueTrackingTest, isGuaranteedNotToBeUndefOrPoison) { 10051c452200SJuneyoung Lee parseAssembly("declare void @f(i32 noundef)" 10061c452200SJuneyoung Lee "define void @test(i32 %x) {\n" 10071c452200SJuneyoung Lee " %A = bitcast i32 %x to i32\n" 10081c452200SJuneyoung Lee " call void @f(i32 noundef %x)\n" 10091c452200SJuneyoung Lee " ret void\n" 10101c452200SJuneyoung Lee "}\n"); 10111c452200SJuneyoung Lee EXPECT_EQ(isGuaranteedNotToBeUndefOrPoison(A), true); 1012abbef2fdSJuneyoung Lee EXPECT_EQ(isGuaranteedNotToBeUndefOrPoison(UndefValue::get(IntegerType::get(Context, 8))), false); 1013abbef2fdSJuneyoung Lee EXPECT_EQ(isGuaranteedNotToBeUndefOrPoison(PoisonValue::get(IntegerType::get(Context, 8))), false); 1014abbef2fdSJuneyoung Lee EXPECT_EQ(isGuaranteedNotToBePoison(UndefValue::get(IntegerType::get(Context, 8))), true); 1015abbef2fdSJuneyoung Lee EXPECT_EQ(isGuaranteedNotToBePoison(PoisonValue::get(IntegerType::get(Context, 8))), false); 101629f8628dSJuneyoung Lee 101729f8628dSJuneyoung Lee Type *Int32Ty = Type::getInt32Ty(Context); 101829f8628dSJuneyoung Lee Constant *CU = UndefValue::get(Int32Ty); 101929f8628dSJuneyoung Lee Constant *CP = PoisonValue::get(Int32Ty); 102029f8628dSJuneyoung Lee Constant *C1 = ConstantInt::get(Int32Ty, 1); 102129f8628dSJuneyoung Lee Constant *C2 = ConstantInt::get(Int32Ty, 2); 102229f8628dSJuneyoung Lee 102329f8628dSJuneyoung Lee { 102429f8628dSJuneyoung Lee Constant *V1 = ConstantVector::get({C1, C2}); 102529f8628dSJuneyoung Lee EXPECT_TRUE(isGuaranteedNotToBeUndefOrPoison(V1)); 102629f8628dSJuneyoung Lee EXPECT_TRUE(isGuaranteedNotToBePoison(V1)); 102729f8628dSJuneyoung Lee } 102829f8628dSJuneyoung Lee 102929f8628dSJuneyoung Lee { 103029f8628dSJuneyoung Lee Constant *V2 = ConstantVector::get({C1, CU}); 103129f8628dSJuneyoung Lee EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(V2)); 103229f8628dSJuneyoung Lee EXPECT_TRUE(isGuaranteedNotToBePoison(V2)); 103329f8628dSJuneyoung Lee } 103429f8628dSJuneyoung Lee 103529f8628dSJuneyoung Lee { 103629f8628dSJuneyoung Lee Constant *V3 = ConstantVector::get({C1, CP}); 103729f8628dSJuneyoung Lee EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(V3)); 103829f8628dSJuneyoung Lee EXPECT_FALSE(isGuaranteedNotToBePoison(V3)); 103929f8628dSJuneyoung Lee } 10401c452200SJuneyoung Lee } 10411c452200SJuneyoung Lee 10429b3c2a72SJuneyoung Lee TEST_F(ValueTrackingTest, isGuaranteedNotToBeUndefOrPoison_assume) { 10439b3c2a72SJuneyoung Lee parseAssembly("declare i1 @f_i1()\n" 10449b3c2a72SJuneyoung Lee "declare i32 @f_i32()\n" 10459b3c2a72SJuneyoung Lee "declare void @llvm.assume(i1)\n" 10469b3c2a72SJuneyoung Lee "define void @test() {\n" 10479b3c2a72SJuneyoung Lee " %A = call i32 @f_i32()\n" 10489b3c2a72SJuneyoung Lee " %cond = call i1 @f_i1()\n" 10499b3c2a72SJuneyoung Lee " %CxtI = add i32 0, 0\n" 10509b3c2a72SJuneyoung Lee " br i1 %cond, label %BB1, label %EXIT\n" 10519b3c2a72SJuneyoung Lee "BB1:\n" 10529b3c2a72SJuneyoung Lee " %CxtI2 = add i32 0, 0\n" 10539b3c2a72SJuneyoung Lee " %cond2 = call i1 @f_i1()\n" 10549b3c2a72SJuneyoung Lee " call void @llvm.assume(i1 true) [ \"noundef\"(i32 %A) ]\n" 10559b3c2a72SJuneyoung Lee " br i1 %cond2, label %BB2, label %EXIT\n" 10569b3c2a72SJuneyoung Lee "BB2:\n" 10579b3c2a72SJuneyoung Lee " %CxtI3 = add i32 0, 0\n" 10589b3c2a72SJuneyoung Lee " ret void\n" 10599b3c2a72SJuneyoung Lee "EXIT:\n" 10609b3c2a72SJuneyoung Lee " ret void\n" 10619b3c2a72SJuneyoung Lee "}"); 10629b3c2a72SJuneyoung Lee AssumptionCache AC(*F); 10639b3c2a72SJuneyoung Lee DominatorTree DT(*F); 10649b3c2a72SJuneyoung Lee EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(A, &AC, CxtI, &DT)); 10659b3c2a72SJuneyoung Lee EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(A, &AC, CxtI2, &DT)); 10669b3c2a72SJuneyoung Lee EXPECT_TRUE(isGuaranteedNotToBeUndefOrPoison(A, &AC, CxtI3, &DT)); 10679b3c2a72SJuneyoung Lee } 10689b3c2a72SJuneyoung Lee 10690a6aee51SJuneyoung Lee TEST(ValueTracking, canCreatePoisonOrUndef) { 1070994543abSJuneyoung Lee std::string AsmHead = 1071f8cece18SMarkus Lavin "@s = external dso_local global i32, align 1\n" 1072994543abSJuneyoung Lee "declare i32 @g(i32)\n" 1073df0b97daSJuneyoung Lee "declare {i32, i1} @llvm.sadd.with.overflow.i32(i32 %a, i32 %b)\n" 1074df0b97daSJuneyoung Lee "declare {i32, i1} @llvm.ssub.with.overflow.i32(i32 %a, i32 %b)\n" 1075df0b97daSJuneyoung Lee "declare {i32, i1} @llvm.smul.with.overflow.i32(i32 %a, i32 %b)\n" 1076df0b97daSJuneyoung Lee "declare {i32, i1} @llvm.uadd.with.overflow.i32(i32 %a, i32 %b)\n" 1077df0b97daSJuneyoung Lee "declare {i32, i1} @llvm.usub.with.overflow.i32(i32 %a, i32 %b)\n" 1078df0b97daSJuneyoung Lee "declare {i32, i1} @llvm.umul.with.overflow.i32(i32 %a, i32 %b)\n" 1079994543abSJuneyoung Lee "define void @f(i32 %x, i32 %y, float %fx, float %fy, i1 %cond, " 1080994543abSJuneyoung Lee "<4 x i32> %vx, <4 x i32> %vx2, <vscale x 4 x i32> %svx, i8* %p) {\n"; 1081994543abSJuneyoung Lee std::string AsmTail = " ret void\n}"; 10820a6aee51SJuneyoung Lee // (can create poison?, can create undef?, IR instruction) 1083ef66e3d0SJuneyoung Lee SmallVector<std::pair<std::pair<bool, bool>, std::string>, 32> Data = { 1084ef66e3d0SJuneyoung Lee {{false, false}, "add i32 %x, %y"}, 1085ef66e3d0SJuneyoung Lee {{true, false}, "add nsw nuw i32 %x, %y"}, 1086ef66e3d0SJuneyoung Lee {{true, false}, "shl i32 %x, %y"}, 1087ef66e3d0SJuneyoung Lee {{true, false}, "shl <4 x i32> %vx, %vx2"}, 1088ef66e3d0SJuneyoung Lee {{true, false}, "shl nsw i32 %x, %y"}, 1089ef66e3d0SJuneyoung Lee {{true, false}, "shl nsw <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"}, 1090ef66e3d0SJuneyoung Lee {{false, false}, "shl i32 %x, 31"}, 1091ef66e3d0SJuneyoung Lee {{true, false}, "shl i32 %x, 32"}, 1092ef66e3d0SJuneyoung Lee {{false, false}, "shl <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"}, 1093ef66e3d0SJuneyoung Lee {{true, false}, "shl <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 32>"}, 1094ef66e3d0SJuneyoung Lee {{true, false}, "ashr i32 %x, %y"}, 1095ef66e3d0SJuneyoung Lee {{true, false}, "ashr exact i32 %x, %y"}, 1096ef66e3d0SJuneyoung Lee {{false, false}, "ashr i32 %x, 31"}, 1097ef66e3d0SJuneyoung Lee {{true, false}, "ashr exact i32 %x, 31"}, 1098ef66e3d0SJuneyoung Lee {{false, false}, "ashr <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"}, 1099ef66e3d0SJuneyoung Lee {{true, false}, "ashr <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 32>"}, 1100ef66e3d0SJuneyoung Lee {{true, false}, "ashr exact <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"}, 1101ef66e3d0SJuneyoung Lee {{true, false}, "lshr i32 %x, %y"}, 1102ef66e3d0SJuneyoung Lee {{true, false}, "lshr exact i32 %x, 31"}, 1103ef66e3d0SJuneyoung Lee {{false, false}, "udiv i32 %x, %y"}, 1104ef66e3d0SJuneyoung Lee {{true, false}, "udiv exact i32 %x, %y"}, 1105ef66e3d0SJuneyoung Lee {{false, false}, "getelementptr i8, i8* %p, i32 %x"}, 1106ef66e3d0SJuneyoung Lee {{true, false}, "getelementptr inbounds i8, i8* %p, i32 %x"}, 1107ef66e3d0SJuneyoung Lee {{true, false}, "fneg nnan float %fx"}, 1108ef66e3d0SJuneyoung Lee {{false, false}, "fneg float %fx"}, 1109ef66e3d0SJuneyoung Lee {{false, false}, "fadd float %fx, %fy"}, 1110ef66e3d0SJuneyoung Lee {{true, false}, "fadd nnan float %fx, %fy"}, 1111ef66e3d0SJuneyoung Lee {{false, false}, "urem i32 %x, %y"}, 1112ef66e3d0SJuneyoung Lee {{true, false}, "fptoui float %fx to i32"}, 1113ef66e3d0SJuneyoung Lee {{true, false}, "fptosi float %fx to i32"}, 1114ef66e3d0SJuneyoung Lee {{false, false}, "bitcast float %fx to i32"}, 1115ef66e3d0SJuneyoung Lee {{false, false}, "select i1 %cond, i32 %x, i32 %y"}, 1116ef66e3d0SJuneyoung Lee {{true, false}, "select nnan i1 %cond, float %fx, float %fy"}, 1117ef66e3d0SJuneyoung Lee {{true, false}, "extractelement <4 x i32> %vx, i32 %x"}, 1118ef66e3d0SJuneyoung Lee {{false, false}, "extractelement <4 x i32> %vx, i32 3"}, 1119ef66e3d0SJuneyoung Lee {{true, false}, "extractelement <vscale x 4 x i32> %svx, i32 4"}, 1120ef66e3d0SJuneyoung Lee {{true, false}, "insertelement <4 x i32> %vx, i32 %x, i32 %y"}, 1121ef66e3d0SJuneyoung Lee {{false, false}, "insertelement <4 x i32> %vx, i32 %x, i32 3"}, 1122ef66e3d0SJuneyoung Lee {{true, false}, "insertelement <vscale x 4 x i32> %svx, i32 %x, i32 4"}, 1123ef66e3d0SJuneyoung Lee {{false, false}, "freeze i32 %x"}, 1124ef66e3d0SJuneyoung Lee {{false, false}, 11250a6aee51SJuneyoung Lee "shufflevector <4 x i32> %vx, <4 x i32> %vx2, " 11260a6aee51SJuneyoung Lee "<4 x i32> <i32 0, i32 1, i32 2, i32 3>"}, 1127ef66e3d0SJuneyoung Lee {{false, true}, 11280a6aee51SJuneyoung Lee "shufflevector <4 x i32> %vx, <4 x i32> %vx2, " 11290a6aee51SJuneyoung Lee "<4 x i32> <i32 0, i32 1, i32 2, i32 undef>"}, 1130ef66e3d0SJuneyoung Lee {{false, true}, 11310a6aee51SJuneyoung Lee "shufflevector <vscale x 4 x i32> %svx, " 11320a6aee51SJuneyoung Lee "<vscale x 4 x i32> %svx, <vscale x 4 x i32> undef"}, 1133ef66e3d0SJuneyoung Lee {{true, false}, "call i32 @g(i32 %x)"}, 1134ef66e3d0SJuneyoung Lee {{false, false}, "call noundef i32 @g(i32 %x)"}, 1135ef66e3d0SJuneyoung Lee {{true, false}, "fcmp nnan oeq float %fx, %fy"}, 1136f8cece18SMarkus Lavin {{false, false}, "fcmp oeq float %fx, %fy"}, 1137f8cece18SMarkus Lavin {{true, false}, 1138f8cece18SMarkus Lavin "ashr <4 x i32> %vx, select (i1 icmp sgt (i32 ptrtoint (i32* @s to " 1139f8cece18SMarkus Lavin "i32), i32 1), <4 x i32> zeroinitializer, <4 x i32> <i32 0, i32 1, i32 " 1140df0b97daSJuneyoung Lee "2, i32 3>)"}, 1141df0b97daSJuneyoung Lee {{false, false}, 1142df0b97daSJuneyoung Lee "call {i32, i1} @llvm.sadd.with.overflow.i32(i32 %x, i32 %y)"}, 1143df0b97daSJuneyoung Lee {{false, false}, 1144df0b97daSJuneyoung Lee "call {i32, i1} @llvm.ssub.with.overflow.i32(i32 %x, i32 %y)"}, 1145df0b97daSJuneyoung Lee {{false, false}, 1146df0b97daSJuneyoung Lee "call {i32, i1} @llvm.smul.with.overflow.i32(i32 %x, i32 %y)"}, 1147df0b97daSJuneyoung Lee {{false, false}, 1148df0b97daSJuneyoung Lee "call {i32, i1} @llvm.uadd.with.overflow.i32(i32 %x, i32 %y)"}, 1149df0b97daSJuneyoung Lee {{false, false}, 1150df0b97daSJuneyoung Lee "call {i32, i1} @llvm.usub.with.overflow.i32(i32 %x, i32 %y)"}, 1151df0b97daSJuneyoung Lee {{false, false}, 1152df0b97daSJuneyoung Lee "call {i32, i1} @llvm.umul.with.overflow.i32(i32 %x, i32 %y)"}}; 1153994543abSJuneyoung Lee 1154994543abSJuneyoung Lee std::string AssemblyStr = AsmHead; 1155994543abSJuneyoung Lee for (auto &Itm : Data) 1156ef66e3d0SJuneyoung Lee AssemblyStr += Itm.second + "\n"; 1157994543abSJuneyoung Lee AssemblyStr += AsmTail; 1158994543abSJuneyoung Lee 1159994543abSJuneyoung Lee LLVMContext Context; 1160994543abSJuneyoung Lee SMDiagnostic Error; 1161994543abSJuneyoung Lee auto M = parseAssemblyString(AssemblyStr, Error, Context); 1162994543abSJuneyoung Lee assert(M && "Bad assembly?"); 1163994543abSJuneyoung Lee 1164994543abSJuneyoung Lee auto *F = M->getFunction("f"); 1165994543abSJuneyoung Lee assert(F && "Bad assembly?"); 1166994543abSJuneyoung Lee 1167994543abSJuneyoung Lee auto &BB = F->getEntryBlock(); 1168994543abSJuneyoung Lee 1169994543abSJuneyoung Lee int Index = 0; 1170994543abSJuneyoung Lee for (auto &I : BB) { 1171994543abSJuneyoung Lee if (isa<ReturnInst>(&I)) 1172994543abSJuneyoung Lee break; 1173ef66e3d0SJuneyoung Lee bool Poison = Data[Index].first.first; 1174ef66e3d0SJuneyoung Lee bool Undef = Data[Index].first.second; 11750a6aee51SJuneyoung Lee EXPECT_EQ(canCreatePoison(cast<Operator>(&I)), Poison) 11760a6aee51SJuneyoung Lee << "Incorrect answer of canCreatePoison at instruction " << Index 11770a6aee51SJuneyoung Lee << " = " << I; 11780a6aee51SJuneyoung Lee EXPECT_EQ(canCreateUndefOrPoison(cast<Operator>(&I)), Undef || Poison) 11790a6aee51SJuneyoung Lee << "Incorrect answer of canCreateUndef at instruction " << Index 11800a6aee51SJuneyoung Lee << " = " << I; 1181994543abSJuneyoung Lee Index++; 1182994543abSJuneyoung Lee } 118345a148deSSanjay Patel } 118445a148deSSanjay Patel 1185565f4e72SAlex Richardson TEST_F(ValueTrackingTest, computePtrAlignment) { 1186565f4e72SAlex Richardson parseAssembly("declare i1 @f_i1()\n" 1187565f4e72SAlex Richardson "declare i8* @f_i8p()\n" 1188565f4e72SAlex Richardson "declare void @llvm.assume(i1)\n" 1189565f4e72SAlex Richardson "define void @test() {\n" 1190565f4e72SAlex Richardson " %A = call i8* @f_i8p()\n" 1191565f4e72SAlex Richardson " %cond = call i1 @f_i1()\n" 1192565f4e72SAlex Richardson " %CxtI = add i32 0, 0\n" 1193565f4e72SAlex Richardson " br i1 %cond, label %BB1, label %EXIT\n" 1194565f4e72SAlex Richardson "BB1:\n" 1195565f4e72SAlex Richardson " %CxtI2 = add i32 0, 0\n" 1196565f4e72SAlex Richardson " %cond2 = call i1 @f_i1()\n" 1197565f4e72SAlex Richardson " call void @llvm.assume(i1 true) [ \"align\"(i8* %A, i64 16) ]\n" 1198565f4e72SAlex Richardson " br i1 %cond2, label %BB2, label %EXIT\n" 1199565f4e72SAlex Richardson "BB2:\n" 1200565f4e72SAlex Richardson " %CxtI3 = add i32 0, 0\n" 1201565f4e72SAlex Richardson " ret void\n" 1202565f4e72SAlex Richardson "EXIT:\n" 1203565f4e72SAlex Richardson " ret void\n" 1204565f4e72SAlex Richardson "}"); 1205565f4e72SAlex Richardson AssumptionCache AC(*F); 1206565f4e72SAlex Richardson DominatorTree DT(*F); 12074ad59f9aSSimon Pilgrim const DataLayout &DL = M->getDataLayout(); 1208565f4e72SAlex Richardson EXPECT_EQ(getKnownAlignment(A, DL, CxtI, &AC, &DT), Align(1)); 1209565f4e72SAlex Richardson EXPECT_EQ(getKnownAlignment(A, DL, CxtI2, &AC, &DT), Align(1)); 1210565f4e72SAlex Richardson EXPECT_EQ(getKnownAlignment(A, DL, CxtI3, &AC, &DT), Align(16)); 1211565f4e72SAlex Richardson } 1212565f4e72SAlex Richardson 1213219e5367SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownBits) { 1214219e5367SNikita Popov parseAssembly( 1215219e5367SNikita Popov "define i32 @test(i32 %a, i32 %b) {\n" 1216219e5367SNikita Popov " %ash = mul i32 %a, 8\n" 1217219e5367SNikita Popov " %aad = add i32 %ash, 7\n" 1218219e5367SNikita Popov " %aan = and i32 %aad, 4095\n" 1219219e5367SNikita Popov " %bsh = shl i32 %b, 4\n" 1220219e5367SNikita Popov " %bad = or i32 %bsh, 6\n" 1221219e5367SNikita Popov " %ban = and i32 %bad, 4095\n" 1222219e5367SNikita Popov " %A = mul i32 %aan, %ban\n" 1223219e5367SNikita Popov " ret i32 %A\n" 1224219e5367SNikita Popov "}\n"); 1225219e5367SNikita Popov expectKnownBits(/*zero*/ 4278190085u, /*one*/ 10u); 122670dbd5fbSSimon Dardis } 122770dbd5fbSSimon Dardis 1228219e5367SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownMulBits) { 1229219e5367SNikita Popov parseAssembly( 1230219e5367SNikita Popov "define i32 @test(i32 %a, i32 %b) {\n" 1231219e5367SNikita Popov " %aa = shl i32 %a, 5\n" 1232219e5367SNikita Popov " %bb = shl i32 %b, 5\n" 1233219e5367SNikita Popov " %aaa = or i32 %aa, 24\n" 1234219e5367SNikita Popov " %bbb = or i32 %bb, 28\n" 1235219e5367SNikita Popov " %A = mul i32 %aaa, %bbb\n" 1236219e5367SNikita Popov " ret i32 %A\n" 1237219e5367SNikita Popov "}\n"); 1238219e5367SNikita Popov expectKnownBits(/*zero*/ 95u, /*one*/ 32u); 123970dbd5fbSSimon Dardis } 1240687b92cdSNikita Popov 1241989445f4SSanjay Patel TEST_F(ValueTrackingTest, isNonZeroRecurrence) { 1242989445f4SSanjay Patel parseAssembly(R"( 1243989445f4SSanjay Patel define i1 @test(i8 %n, i8 %r) { 1244989445f4SSanjay Patel entry: 1245989445f4SSanjay Patel br label %loop 1246989445f4SSanjay Patel loop: 1247989445f4SSanjay Patel %p = phi i8 [ -1, %entry ], [ %next, %loop ] 1248989445f4SSanjay Patel %next = add nsw i8 %p, -1 1249989445f4SSanjay Patel %cmp1 = icmp eq i8 %p, %n 1250989445f4SSanjay Patel br i1 %cmp1, label %exit, label %loop 1251989445f4SSanjay Patel exit: 1252989445f4SSanjay Patel %A = or i8 %p, %r 1253989445f4SSanjay Patel %CxtI = icmp eq i8 %A, 0 1254989445f4SSanjay Patel ret i1 %CxtI 1255989445f4SSanjay Patel } 1256989445f4SSanjay Patel )"); 12574ad59f9aSSimon Pilgrim const DataLayout &DL = M->getDataLayout(); 1258989445f4SSanjay Patel AssumptionCache AC(*F); 12595ae5d25eSSanjay Patel EXPECT_TRUE(isKnownNonZero(A, DL, 0, &AC, CxtI)); 1260989445f4SSanjay Patel } 1261989445f4SSanjay Patel 1262eca40c36SJuneyoung Lee TEST_F(ValueTrackingTest, KnownNonZeroFromDomCond) { 1263eca40c36SJuneyoung Lee parseAssembly(R"( 1264eca40c36SJuneyoung Lee declare i8* @f_i8() 1265eca40c36SJuneyoung Lee define void @test(i1 %c) { 1266eca40c36SJuneyoung Lee %A = call i8* @f_i8() 1267eca40c36SJuneyoung Lee %B = call i8* @f_i8() 1268eca40c36SJuneyoung Lee %c1 = icmp ne i8* %A, null 1269eca40c36SJuneyoung Lee %cond = and i1 %c1, %c 1270eca40c36SJuneyoung Lee br i1 %cond, label %T, label %Q 1271eca40c36SJuneyoung Lee T: 1272eca40c36SJuneyoung Lee %CxtI = add i32 0, 0 1273eca40c36SJuneyoung Lee ret void 1274eca40c36SJuneyoung Lee Q: 1275eca40c36SJuneyoung Lee %CxtI2 = add i32 0, 0 1276eca40c36SJuneyoung Lee ret void 1277eca40c36SJuneyoung Lee } 1278eca40c36SJuneyoung Lee )"); 1279eca40c36SJuneyoung Lee AssumptionCache AC(*F); 1280eca40c36SJuneyoung Lee DominatorTree DT(*F); 12814ad59f9aSSimon Pilgrim const DataLayout &DL = M->getDataLayout(); 1282eca40c36SJuneyoung Lee EXPECT_EQ(isKnownNonZero(A, DL, 0, &AC, CxtI, &DT), true); 1283eca40c36SJuneyoung Lee EXPECT_EQ(isKnownNonZero(A, DL, 0, &AC, CxtI2, &DT), false); 1284eca40c36SJuneyoung Lee } 1285eca40c36SJuneyoung Lee 1286860199dfSJuneyoung Lee TEST_F(ValueTrackingTest, KnownNonZeroFromDomCond2) { 1287860199dfSJuneyoung Lee parseAssembly(R"( 1288860199dfSJuneyoung Lee declare i8* @f_i8() 1289860199dfSJuneyoung Lee define void @test(i1 %c) { 1290860199dfSJuneyoung Lee %A = call i8* @f_i8() 1291860199dfSJuneyoung Lee %B = call i8* @f_i8() 1292860199dfSJuneyoung Lee %c1 = icmp ne i8* %A, null 1293860199dfSJuneyoung Lee %cond = select i1 %c, i1 %c1, i1 false 1294860199dfSJuneyoung Lee br i1 %cond, label %T, label %Q 1295860199dfSJuneyoung Lee T: 1296860199dfSJuneyoung Lee %CxtI = add i32 0, 0 1297860199dfSJuneyoung Lee ret void 1298860199dfSJuneyoung Lee Q: 1299860199dfSJuneyoung Lee %CxtI2 = add i32 0, 0 1300860199dfSJuneyoung Lee ret void 1301860199dfSJuneyoung Lee } 1302860199dfSJuneyoung Lee )"); 1303860199dfSJuneyoung Lee AssumptionCache AC(*F); 1304860199dfSJuneyoung Lee DominatorTree DT(*F); 13054ad59f9aSSimon Pilgrim const DataLayout &DL = M->getDataLayout(); 1306860199dfSJuneyoung Lee EXPECT_EQ(isKnownNonZero(A, DL, 0, &AC, CxtI, &DT), true); 1307860199dfSJuneyoung Lee EXPECT_EQ(isKnownNonZero(A, DL, 0, &AC, CxtI2, &DT), false); 1308860199dfSJuneyoung Lee } 1309860199dfSJuneyoung Lee 1310eca40c36SJuneyoung Lee TEST_F(ValueTrackingTest, IsImpliedConditionAnd) { 1311eca40c36SJuneyoung Lee parseAssembly(R"( 1312eca40c36SJuneyoung Lee define void @test(i32 %x, i32 %y) { 1313eca40c36SJuneyoung Lee %c1 = icmp ult i32 %x, 10 1314eca40c36SJuneyoung Lee %c2 = icmp ult i32 %y, 15 1315eca40c36SJuneyoung Lee %A = and i1 %c1, %c2 1316eca40c36SJuneyoung Lee ; x < 10 /\ y < 15 1317eca40c36SJuneyoung Lee %A2 = icmp ult i32 %x, 20 1318eca40c36SJuneyoung Lee %A3 = icmp uge i32 %y, 20 1319eca40c36SJuneyoung Lee %A4 = icmp ult i32 %x, 5 1320eca40c36SJuneyoung Lee ret void 1321eca40c36SJuneyoung Lee } 1322eca40c36SJuneyoung Lee )"); 13234ad59f9aSSimon Pilgrim const DataLayout &DL = M->getDataLayout(); 1324eca40c36SJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A2, DL), true); 1325eca40c36SJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A3, DL), false); 1326eca40c36SJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A4, DL), None); 1327eca40c36SJuneyoung Lee } 1328eca40c36SJuneyoung Lee 1329860199dfSJuneyoung Lee TEST_F(ValueTrackingTest, IsImpliedConditionAnd2) { 1330860199dfSJuneyoung Lee parseAssembly(R"( 1331860199dfSJuneyoung Lee define void @test(i32 %x, i32 %y) { 1332860199dfSJuneyoung Lee %c1 = icmp ult i32 %x, 10 1333860199dfSJuneyoung Lee %c2 = icmp ult i32 %y, 15 1334860199dfSJuneyoung Lee %A = select i1 %c1, i1 %c2, i1 false 1335860199dfSJuneyoung Lee ; x < 10 /\ y < 15 1336860199dfSJuneyoung Lee %A2 = icmp ult i32 %x, 20 1337860199dfSJuneyoung Lee %A3 = icmp uge i32 %y, 20 1338860199dfSJuneyoung Lee %A4 = icmp ult i32 %x, 5 1339860199dfSJuneyoung Lee ret void 1340860199dfSJuneyoung Lee } 1341860199dfSJuneyoung Lee )"); 13424ad59f9aSSimon Pilgrim const DataLayout &DL = M->getDataLayout(); 1343860199dfSJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A2, DL), true); 1344860199dfSJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A3, DL), false); 1345860199dfSJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A4, DL), None); 1346860199dfSJuneyoung Lee } 1347860199dfSJuneyoung Lee 1348*4da47beeSSanjay Patel TEST_F(ValueTrackingTest, IsImpliedConditionAndVec) { 1349*4da47beeSSanjay Patel parseAssembly(R"( 1350*4da47beeSSanjay Patel define void @test(<2 x i8> %x, <2 x i8> %y) { 1351*4da47beeSSanjay Patel %A = icmp ult <2 x i8> %x, %y 1352*4da47beeSSanjay Patel %A2 = icmp ule <2 x i8> %x, %y 1353*4da47beeSSanjay Patel ret void 1354*4da47beeSSanjay Patel } 1355*4da47beeSSanjay Patel )"); 1356*4da47beeSSanjay Patel const DataLayout &DL = M->getDataLayout(); 1357*4da47beeSSanjay Patel EXPECT_EQ(isImpliedCondition(A, A2, DL), None); 1358*4da47beeSSanjay Patel } 1359*4da47beeSSanjay Patel 1360eca40c36SJuneyoung Lee TEST_F(ValueTrackingTest, IsImpliedConditionOr) { 1361eca40c36SJuneyoung Lee parseAssembly(R"( 1362eca40c36SJuneyoung Lee define void @test(i32 %x, i32 %y) { 1363eca40c36SJuneyoung Lee %c1 = icmp ult i32 %x, 10 1364eca40c36SJuneyoung Lee %c2 = icmp ult i32 %y, 15 1365eca40c36SJuneyoung Lee %A = or i1 %c1, %c2 ; negated 1366eca40c36SJuneyoung Lee ; x >= 10 /\ y >= 15 1367eca40c36SJuneyoung Lee %A2 = icmp ult i32 %x, 5 1368eca40c36SJuneyoung Lee %A3 = icmp uge i32 %y, 10 1369eca40c36SJuneyoung Lee %A4 = icmp ult i32 %x, 15 1370eca40c36SJuneyoung Lee ret void 1371eca40c36SJuneyoung Lee } 1372eca40c36SJuneyoung Lee )"); 13734ad59f9aSSimon Pilgrim const DataLayout &DL = M->getDataLayout(); 1374eca40c36SJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A2, DL, false), false); 1375eca40c36SJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A3, DL, false), true); 1376eca40c36SJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A4, DL, false), None); 1377eca40c36SJuneyoung Lee } 1378eca40c36SJuneyoung Lee 1379860199dfSJuneyoung Lee TEST_F(ValueTrackingTest, IsImpliedConditionOr2) { 1380860199dfSJuneyoung Lee parseAssembly(R"( 1381860199dfSJuneyoung Lee define void @test(i32 %x, i32 %y) { 1382860199dfSJuneyoung Lee %c1 = icmp ult i32 %x, 10 1383860199dfSJuneyoung Lee %c2 = icmp ult i32 %y, 15 1384860199dfSJuneyoung Lee %A = select i1 %c1, i1 true, i1 %c2 ; negated 1385860199dfSJuneyoung Lee ; x >= 10 /\ y >= 15 1386860199dfSJuneyoung Lee %A2 = icmp ult i32 %x, 5 1387860199dfSJuneyoung Lee %A3 = icmp uge i32 %y, 10 1388860199dfSJuneyoung Lee %A4 = icmp ult i32 %x, 15 1389860199dfSJuneyoung Lee ret void 1390860199dfSJuneyoung Lee } 1391860199dfSJuneyoung Lee )"); 13924ad59f9aSSimon Pilgrim const DataLayout &DL = M->getDataLayout(); 1393860199dfSJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A2, DL, false), false); 1394860199dfSJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A3, DL, false), true); 1395860199dfSJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A4, DL, false), None); 1396860199dfSJuneyoung Lee } 1397860199dfSJuneyoung Lee 13987cf0f856SNikita Popov TEST_F(ComputeKnownBitsTest, KnownNonZeroShift) { 13997cf0f856SNikita Popov // %q is known nonzero without known bits. 14007cf0f856SNikita Popov // Because %q is nonzero, %A[0] is known to be zero. 14017cf0f856SNikita Popov parseAssembly( 14027cf0f856SNikita Popov "define i8 @test(i8 %p, i8* %pq) {\n" 14037cf0f856SNikita Popov " %q = load i8, i8* %pq, !range !0\n" 14047cf0f856SNikita Popov " %A = shl i8 %p, %q\n" 14057cf0f856SNikita Popov " ret i8 %A\n" 14067cf0f856SNikita Popov "}\n" 14077cf0f856SNikita Popov "!0 = !{ i8 1, i8 5 }\n"); 14087cf0f856SNikita Popov expectKnownBits(/*zero*/ 1u, /*one*/ 0u); 14097cf0f856SNikita Popov } 14107cf0f856SNikita Popov 1411687b92cdSNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownFshl) { 1412687b92cdSNikita Popov // fshl(....1111....0000, 00..1111........, 6) 1413687b92cdSNikita Popov // = 11....000000..11 1414687b92cdSNikita Popov parseAssembly( 1415687b92cdSNikita Popov "define i16 @test(i16 %a, i16 %b) {\n" 1416687b92cdSNikita Popov " %aa = shl i16 %a, 4\n" 1417687b92cdSNikita Popov " %bb = lshr i16 %b, 2\n" 1418687b92cdSNikita Popov " %aaa = or i16 %aa, 3840\n" 1419687b92cdSNikita Popov " %bbb = or i16 %bb, 3840\n" 1420687b92cdSNikita Popov " %A = call i16 @llvm.fshl.i16(i16 %aaa, i16 %bbb, i16 6)\n" 1421687b92cdSNikita Popov " ret i16 %A\n" 1422687b92cdSNikita Popov "}\n" 1423687b92cdSNikita Popov "declare i16 @llvm.fshl.i16(i16, i16, i16)\n"); 1424687b92cdSNikita Popov expectKnownBits(/*zero*/ 1008u, /*one*/ 49155u); 1425687b92cdSNikita Popov } 1426687b92cdSNikita Popov 1427687b92cdSNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownFshr) { 1428687b92cdSNikita Popov // fshr(....1111....0000, 00..1111........, 26) 1429687b92cdSNikita Popov // = 11....000000..11 1430687b92cdSNikita Popov parseAssembly( 1431687b92cdSNikita Popov "define i16 @test(i16 %a, i16 %b) {\n" 1432687b92cdSNikita Popov " %aa = shl i16 %a, 4\n" 1433687b92cdSNikita Popov " %bb = lshr i16 %b, 2\n" 1434687b92cdSNikita Popov " %aaa = or i16 %aa, 3840\n" 1435687b92cdSNikita Popov " %bbb = or i16 %bb, 3840\n" 1436687b92cdSNikita Popov " %A = call i16 @llvm.fshr.i16(i16 %aaa, i16 %bbb, i16 26)\n" 1437687b92cdSNikita Popov " ret i16 %A\n" 1438687b92cdSNikita Popov "}\n" 1439687b92cdSNikita Popov "declare i16 @llvm.fshr.i16(i16, i16, i16)\n"); 1440687b92cdSNikita Popov expectKnownBits(/*zero*/ 1008u, /*one*/ 49155u); 1441687b92cdSNikita Popov } 1442687b92cdSNikita Popov 1443687b92cdSNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownFshlZero) { 1444687b92cdSNikita Popov // fshl(....1111....0000, 00..1111........, 0) 1445687b92cdSNikita Popov // = ....1111....0000 1446687b92cdSNikita Popov parseAssembly( 1447687b92cdSNikita Popov "define i16 @test(i16 %a, i16 %b) {\n" 1448687b92cdSNikita Popov " %aa = shl i16 %a, 4\n" 1449687b92cdSNikita Popov " %bb = lshr i16 %b, 2\n" 1450687b92cdSNikita Popov " %aaa = or i16 %aa, 3840\n" 1451687b92cdSNikita Popov " %bbb = or i16 %bb, 3840\n" 1452687b92cdSNikita Popov " %A = call i16 @llvm.fshl.i16(i16 %aaa, i16 %bbb, i16 0)\n" 1453687b92cdSNikita Popov " ret i16 %A\n" 1454687b92cdSNikita Popov "}\n" 1455687b92cdSNikita Popov "declare i16 @llvm.fshl.i16(i16, i16, i16)\n"); 1456687b92cdSNikita Popov expectKnownBits(/*zero*/ 15u, /*one*/ 3840u); 1457687b92cdSNikita Popov } 1458ed3ca927SNikita Popov 1459ed3ca927SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownUAddSatLeadingOnes) { 1460ed3ca927SNikita Popov // uadd.sat(1111...1, ........) 1461ed3ca927SNikita Popov // = 1111.... 1462ed3ca927SNikita Popov parseAssembly( 1463ed3ca927SNikita Popov "define i8 @test(i8 %a, i8 %b) {\n" 1464ed3ca927SNikita Popov " %aa = or i8 %a, 241\n" 1465ed3ca927SNikita Popov " %A = call i8 @llvm.uadd.sat.i8(i8 %aa, i8 %b)\n" 1466ed3ca927SNikita Popov " ret i8 %A\n" 1467ed3ca927SNikita Popov "}\n" 1468ed3ca927SNikita Popov "declare i8 @llvm.uadd.sat.i8(i8, i8)\n"); 1469ed3ca927SNikita Popov expectKnownBits(/*zero*/ 0u, /*one*/ 240u); 1470ed3ca927SNikita Popov } 1471ed3ca927SNikita Popov 1472ed3ca927SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownUAddSatOnesPreserved) { 1473ed3ca927SNikita Popov // uadd.sat(00...011, .1...110) 1474ed3ca927SNikita Popov // = .......1 1475ed3ca927SNikita Popov parseAssembly( 1476ed3ca927SNikita Popov "define i8 @test(i8 %a, i8 %b) {\n" 1477ed3ca927SNikita Popov " %aa = or i8 %a, 3\n" 1478ed3ca927SNikita Popov " %aaa = and i8 %aa, 59\n" 1479ed3ca927SNikita Popov " %bb = or i8 %b, 70\n" 1480ed3ca927SNikita Popov " %bbb = and i8 %bb, 254\n" 1481ed3ca927SNikita Popov " %A = call i8 @llvm.uadd.sat.i8(i8 %aaa, i8 %bbb)\n" 1482ed3ca927SNikita Popov " ret i8 %A\n" 1483ed3ca927SNikita Popov "}\n" 1484ed3ca927SNikita Popov "declare i8 @llvm.uadd.sat.i8(i8, i8)\n"); 1485ed3ca927SNikita Popov expectKnownBits(/*zero*/ 0u, /*one*/ 1u); 1486ed3ca927SNikita Popov } 1487ed3ca927SNikita Popov 1488ed3ca927SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatLHSLeadingZeros) { 1489ed3ca927SNikita Popov // usub.sat(0000...0, ........) 1490ed3ca927SNikita Popov // = 0000.... 1491ed3ca927SNikita Popov parseAssembly( 1492ed3ca927SNikita Popov "define i8 @test(i8 %a, i8 %b) {\n" 1493ed3ca927SNikita Popov " %aa = and i8 %a, 14\n" 1494ed3ca927SNikita Popov " %A = call i8 @llvm.usub.sat.i8(i8 %aa, i8 %b)\n" 1495ed3ca927SNikita Popov " ret i8 %A\n" 1496ed3ca927SNikita Popov "}\n" 1497ed3ca927SNikita Popov "declare i8 @llvm.usub.sat.i8(i8, i8)\n"); 1498ed3ca927SNikita Popov expectKnownBits(/*zero*/ 240u, /*one*/ 0u); 1499ed3ca927SNikita Popov } 1500ed3ca927SNikita Popov 1501ed3ca927SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatRHSLeadingOnes) { 1502ed3ca927SNikita Popov // usub.sat(........, 1111...1) 1503ed3ca927SNikita Popov // = 0000.... 1504ed3ca927SNikita Popov parseAssembly( 1505ed3ca927SNikita Popov "define i8 @test(i8 %a, i8 %b) {\n" 1506ed3ca927SNikita Popov " %bb = or i8 %a, 241\n" 1507ed3ca927SNikita Popov " %A = call i8 @llvm.usub.sat.i8(i8 %a, i8 %bb)\n" 1508ed3ca927SNikita Popov " ret i8 %A\n" 1509ed3ca927SNikita Popov "}\n" 1510ed3ca927SNikita Popov "declare i8 @llvm.usub.sat.i8(i8, i8)\n"); 1511ed3ca927SNikita Popov expectKnownBits(/*zero*/ 240u, /*one*/ 0u); 1512ed3ca927SNikita Popov } 1513ed3ca927SNikita Popov 1514ed3ca927SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatZerosPreserved) { 1515ed3ca927SNikita Popov // usub.sat(11...011, .1...110) 1516ed3ca927SNikita Popov // = ......0. 1517ed3ca927SNikita Popov parseAssembly( 1518ed3ca927SNikita Popov "define i8 @test(i8 %a, i8 %b) {\n" 1519ed3ca927SNikita Popov " %aa = or i8 %a, 195\n" 1520ed3ca927SNikita Popov " %aaa = and i8 %aa, 251\n" 1521ed3ca927SNikita Popov " %bb = or i8 %b, 70\n" 1522ed3ca927SNikita Popov " %bbb = and i8 %bb, 254\n" 1523ed3ca927SNikita Popov " %A = call i8 @llvm.usub.sat.i8(i8 %aaa, i8 %bbb)\n" 1524ed3ca927SNikita Popov " ret i8 %A\n" 1525ed3ca927SNikita Popov "}\n" 1526ed3ca927SNikita Popov "declare i8 @llvm.usub.sat.i8(i8, i8)\n"); 1527ed3ca927SNikita Popov expectKnownBits(/*zero*/ 2u, /*one*/ 0u); 1528ed3ca927SNikita Popov } 1529329d15b5SVitaly Buka 1530d86fff6aSNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownBitsPtrToIntTrunc) { 1531d86fff6aSNikita Popov // ptrtoint truncates the pointer type. 1532d86fff6aSNikita Popov parseAssembly( 1533d86fff6aSNikita Popov "define void @test(i8** %p) {\n" 1534d86fff6aSNikita Popov " %A = load i8*, i8** %p\n" 1535d86fff6aSNikita Popov " %i = ptrtoint i8* %A to i32\n" 1536d86fff6aSNikita Popov " %m = and i32 %i, 31\n" 1537d86fff6aSNikita Popov " %c = icmp eq i32 %m, 0\n" 1538d86fff6aSNikita Popov " call void @llvm.assume(i1 %c)\n" 1539d86fff6aSNikita Popov " ret void\n" 1540d86fff6aSNikita Popov "}\n" 1541d86fff6aSNikita Popov "declare void @llvm.assume(i1)\n"); 1542d86fff6aSNikita Popov AssumptionCache AC(*F); 1543d86fff6aSNikita Popov KnownBits Known = computeKnownBits( 1544d86fff6aSNikita Popov A, M->getDataLayout(), /* Depth */ 0, &AC, F->front().getTerminator()); 1545d86fff6aSNikita Popov EXPECT_EQ(Known.Zero.getZExtValue(), 31u); 1546d86fff6aSNikita Popov EXPECT_EQ(Known.One.getZExtValue(), 0u); 1547d86fff6aSNikita Popov } 1548d86fff6aSNikita Popov 1549d86fff6aSNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownBitsPtrToIntZext) { 1550d86fff6aSNikita Popov // ptrtoint zero extends the pointer type. 1551d86fff6aSNikita Popov parseAssembly( 1552d86fff6aSNikita Popov "define void @test(i8** %p) {\n" 1553d86fff6aSNikita Popov " %A = load i8*, i8** %p\n" 1554d86fff6aSNikita Popov " %i = ptrtoint i8* %A to i128\n" 1555d86fff6aSNikita Popov " %m = and i128 %i, 31\n" 1556d86fff6aSNikita Popov " %c = icmp eq i128 %m, 0\n" 1557d86fff6aSNikita Popov " call void @llvm.assume(i1 %c)\n" 1558d86fff6aSNikita Popov " ret void\n" 1559d86fff6aSNikita Popov "}\n" 1560d86fff6aSNikita Popov "declare void @llvm.assume(i1)\n"); 1561d86fff6aSNikita Popov AssumptionCache AC(*F); 1562d86fff6aSNikita Popov KnownBits Known = computeKnownBits( 1563d86fff6aSNikita Popov A, M->getDataLayout(), /* Depth */ 0, &AC, F->front().getTerminator()); 1564d86fff6aSNikita Popov EXPECT_EQ(Known.Zero.getZExtValue(), 31u); 1565d86fff6aSNikita Popov EXPECT_EQ(Known.One.getZExtValue(), 0u); 1566d86fff6aSNikita Popov } 1567d86fff6aSNikita Popov 1568a6183d0fSJuneyoung Lee TEST_F(ComputeKnownBitsTest, ComputeKnownBitsFreeze) { 1569a6183d0fSJuneyoung Lee parseAssembly("define void @test() {\n" 1570a6183d0fSJuneyoung Lee " %m = call i32 @any_num()\n" 1571a6183d0fSJuneyoung Lee " %A = freeze i32 %m\n" 1572a6183d0fSJuneyoung Lee " %n = and i32 %m, 31\n" 1573a6183d0fSJuneyoung Lee " %c = icmp eq i32 %n, 0\n" 1574a6183d0fSJuneyoung Lee " call void @llvm.assume(i1 %c)\n" 1575a6183d0fSJuneyoung Lee " ret void\n" 1576a6183d0fSJuneyoung Lee "}\n" 1577a6183d0fSJuneyoung Lee "declare void @llvm.assume(i1)\n" 1578a6183d0fSJuneyoung Lee "declare i32 @any_num()\n"); 1579a6183d0fSJuneyoung Lee AssumptionCache AC(*F); 1580a6183d0fSJuneyoung Lee KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1581a6183d0fSJuneyoung Lee F->front().getTerminator()); 1582a6183d0fSJuneyoung Lee EXPECT_EQ(Known.Zero.getZExtValue(), 31u); 1583a6183d0fSJuneyoung Lee EXPECT_EQ(Known.One.getZExtValue(), 0u); 1584a6183d0fSJuneyoung Lee } 1585a6183d0fSJuneyoung Lee 1586f1f31eb2SQuentin Colombet TEST_F(ComputeKnownBitsTest, ComputeKnownBitsAddWithRange) { 1587f1f31eb2SQuentin Colombet parseAssembly("define void @test(i64* %p) {\n" 1588f1f31eb2SQuentin Colombet " %A = load i64, i64* %p, !range !{i64 64, i64 65536}\n" 1589f1f31eb2SQuentin Colombet " %APlus512 = add i64 %A, 512\n" 1590f1f31eb2SQuentin Colombet " %c = icmp ugt i64 %APlus512, 523\n" 1591f1f31eb2SQuentin Colombet " call void @llvm.assume(i1 %c)\n" 1592f1f31eb2SQuentin Colombet " ret void\n" 1593f1f31eb2SQuentin Colombet "}\n" 1594f1f31eb2SQuentin Colombet "declare void @llvm.assume(i1)\n"); 1595f1f31eb2SQuentin Colombet AssumptionCache AC(*F); 1596f1f31eb2SQuentin Colombet KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1597f1f31eb2SQuentin Colombet F->front().getTerminator()); 1598f1f31eb2SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), ~(65536llu - 1)); 1599f1f31eb2SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 0u); 1600f1f31eb2SQuentin Colombet Instruction &APlus512 = findInstructionByName(F, "APlus512"); 1601f1f31eb2SQuentin Colombet Known = computeKnownBits(&APlus512, M->getDataLayout(), /* Depth */ 0, &AC, 1602f1f31eb2SQuentin Colombet F->front().getTerminator()); 1603f1f31eb2SQuentin Colombet // We know of one less zero because 512 may have produced a 1 that 1604f1f31eb2SQuentin Colombet // got carried all the way to the first trailing zero. 1605f1f31eb2SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), (~(65536llu - 1)) << 1); 1606f1f31eb2SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 0u); 1607f1f31eb2SQuentin Colombet // The known range is not precise given computeKnownBits works 1608f1f31eb2SQuentin Colombet // with the masks of zeros and ones, not the ranges. 1609f1f31eb2SQuentin Colombet EXPECT_EQ(Known.getMinValue(), 0u); 1610f1f31eb2SQuentin Colombet EXPECT_EQ(Known.getMaxValue(), 131071); 1611f1f31eb2SQuentin Colombet } 1612f1f31eb2SQuentin Colombet 1613c2634fc6SDavid Sherwood TEST_F(ComputeKnownBitsTest, ComputeKnownBitsUnknownVScale) { 1614c2634fc6SDavid Sherwood Module M("", Context); 1615c2634fc6SDavid Sherwood IRBuilder<> Builder(Context); 1616c2634fc6SDavid Sherwood Function *TheFn = 1617c2634fc6SDavid Sherwood Intrinsic::getDeclaration(&M, Intrinsic::vscale, {Builder.getInt32Ty()}); 1618c2634fc6SDavid Sherwood CallInst *CI = Builder.CreateCall(TheFn, {}, {}, ""); 1619c2634fc6SDavid Sherwood 1620c2634fc6SDavid Sherwood KnownBits Known = computeKnownBits(CI, M.getDataLayout(), /* Depth */ 0); 1621c2634fc6SDavid Sherwood // There is no parent function so we cannot look up the vscale_range 1622c2634fc6SDavid Sherwood // attribute to determine the number of bits. 1623c2634fc6SDavid Sherwood EXPECT_EQ(Known.One.getZExtValue(), 0u); 1624c2634fc6SDavid Sherwood EXPECT_EQ(Known.Zero.getZExtValue(), 0u); 16258e4f7b74SDavid Sherwood 16268e4f7b74SDavid Sherwood BasicBlock *BB = BasicBlock::Create(Context); 16278e4f7b74SDavid Sherwood BB->getInstList().push_back(CI); 16288e4f7b74SDavid Sherwood Known = computeKnownBits(CI, M.getDataLayout(), /* Depth */ 0); 16298e4f7b74SDavid Sherwood // There is no parent function so we cannot look up the vscale_range 16308e4f7b74SDavid Sherwood // attribute to determine the number of bits. 16318e4f7b74SDavid Sherwood EXPECT_EQ(Known.One.getZExtValue(), 0u); 16328e4f7b74SDavid Sherwood EXPECT_EQ(Known.Zero.getZExtValue(), 0u); 16338e4f7b74SDavid Sherwood 16348e4f7b74SDavid Sherwood CI->removeFromParent(); 16358e4f7b74SDavid Sherwood delete CI; 16368e4f7b74SDavid Sherwood delete BB; 1637c2634fc6SDavid Sherwood } 1638c2634fc6SDavid Sherwood 1639f1f31eb2SQuentin Colombet // 512 + [32, 64) doesn't produce overlapping bits. 1640f1f31eb2SQuentin Colombet // Make sure we get all the individual bits properly. 1641f1f31eb2SQuentin Colombet TEST_F(ComputeKnownBitsTest, ComputeKnownBitsAddWithRangeNoOverlap) { 1642f1f31eb2SQuentin Colombet parseAssembly("define void @test(i64* %p) {\n" 1643f1f31eb2SQuentin Colombet " %A = load i64, i64* %p, !range !{i64 32, i64 64}\n" 1644f1f31eb2SQuentin Colombet " %APlus512 = add i64 %A, 512\n" 1645f1f31eb2SQuentin Colombet " %c = icmp ugt i64 %APlus512, 523\n" 1646f1f31eb2SQuentin Colombet " call void @llvm.assume(i1 %c)\n" 1647f1f31eb2SQuentin Colombet " ret void\n" 1648f1f31eb2SQuentin Colombet "}\n" 1649f1f31eb2SQuentin Colombet "declare void @llvm.assume(i1)\n"); 1650f1f31eb2SQuentin Colombet AssumptionCache AC(*F); 1651f1f31eb2SQuentin Colombet KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1652f1f31eb2SQuentin Colombet F->front().getTerminator()); 1653f1f31eb2SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), ~(64llu - 1)); 1654f1f31eb2SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 32u); 1655f1f31eb2SQuentin Colombet Instruction &APlus512 = findInstructionByName(F, "APlus512"); 1656f1f31eb2SQuentin Colombet Known = computeKnownBits(&APlus512, M->getDataLayout(), /* Depth */ 0, &AC, 1657f1f31eb2SQuentin Colombet F->front().getTerminator()); 1658f1f31eb2SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), ~512llu & ~(64llu - 1)); 1659f1f31eb2SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 512u | 32u); 1660f1f31eb2SQuentin Colombet // The known range is not precise given computeKnownBits works 1661f1f31eb2SQuentin Colombet // with the masks of zeros and ones, not the ranges. 1662f1f31eb2SQuentin Colombet EXPECT_EQ(Known.getMinValue(), 544); 1663f1f31eb2SQuentin Colombet EXPECT_EQ(Known.getMaxValue(), 575); 1664f1f31eb2SQuentin Colombet } 1665f1f31eb2SQuentin Colombet 1666ee6abef5SQuentin Colombet TEST_F(ComputeKnownBitsTest, ComputeKnownBitsGEPWithRange) { 1667ee6abef5SQuentin Colombet parseAssembly( 1668ee6abef5SQuentin Colombet "define void @test(i64* %p) {\n" 1669ee6abef5SQuentin Colombet " %A = load i64, i64* %p, !range !{i64 64, i64 65536}\n" 1670ee6abef5SQuentin Colombet " %APtr = inttoptr i64 %A to float*" 1671ee6abef5SQuentin Colombet " %APtrPlus512 = getelementptr float, float* %APtr, i32 128\n" 1672ee6abef5SQuentin Colombet " %c = icmp ugt float* %APtrPlus512, inttoptr (i32 523 to float*)\n" 1673ee6abef5SQuentin Colombet " call void @llvm.assume(i1 %c)\n" 1674ee6abef5SQuentin Colombet " ret void\n" 1675ee6abef5SQuentin Colombet "}\n" 1676ee6abef5SQuentin Colombet "declare void @llvm.assume(i1)\n"); 1677ee6abef5SQuentin Colombet AssumptionCache AC(*F); 1678ee6abef5SQuentin Colombet KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1679ee6abef5SQuentin Colombet F->front().getTerminator()); 1680ee6abef5SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), ~(65536llu - 1)); 1681ee6abef5SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 0u); 1682ee6abef5SQuentin Colombet Instruction &APtrPlus512 = findInstructionByName(F, "APtrPlus512"); 1683ee6abef5SQuentin Colombet Known = computeKnownBits(&APtrPlus512, M->getDataLayout(), /* Depth */ 0, &AC, 1684ee6abef5SQuentin Colombet F->front().getTerminator()); 1685ee6abef5SQuentin Colombet // We know of one less zero because 512 may have produced a 1 that 1686ee6abef5SQuentin Colombet // got carried all the way to the first trailing zero. 1687ee6abef5SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), ~(65536llu - 1) << 1); 1688ee6abef5SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 0u); 1689ee6abef5SQuentin Colombet // The known range is not precise given computeKnownBits works 1690ee6abef5SQuentin Colombet // with the masks of zeros and ones, not the ranges. 1691ee6abef5SQuentin Colombet EXPECT_EQ(Known.getMinValue(), 0u); 1692ee6abef5SQuentin Colombet EXPECT_EQ(Known.getMaxValue(), 131071); 1693ee6abef5SQuentin Colombet } 1694ee6abef5SQuentin Colombet 1695ee6abef5SQuentin Colombet // 4*128 + [32, 64) doesn't produce overlapping bits. 1696ee6abef5SQuentin Colombet // Make sure we get all the individual bits properly. 1697ee6abef5SQuentin Colombet // This test is useful to check that we account for the scaling factor 1698ee6abef5SQuentin Colombet // in the gep. Indeed, gep float, [32,64), 128 is not 128 + [32,64). 1699ee6abef5SQuentin Colombet TEST_F(ComputeKnownBitsTest, ComputeKnownBitsGEPWithRangeNoOverlap) { 1700ee6abef5SQuentin Colombet parseAssembly( 1701ee6abef5SQuentin Colombet "define void @test(i64* %p) {\n" 1702ee6abef5SQuentin Colombet " %A = load i64, i64* %p, !range !{i64 32, i64 64}\n" 1703ee6abef5SQuentin Colombet " %APtr = inttoptr i64 %A to float*" 1704ee6abef5SQuentin Colombet " %APtrPlus512 = getelementptr float, float* %APtr, i32 128\n" 1705ee6abef5SQuentin Colombet " %c = icmp ugt float* %APtrPlus512, inttoptr (i32 523 to float*)\n" 1706ee6abef5SQuentin Colombet " call void @llvm.assume(i1 %c)\n" 1707ee6abef5SQuentin Colombet " ret void\n" 1708ee6abef5SQuentin Colombet "}\n" 1709ee6abef5SQuentin Colombet "declare void @llvm.assume(i1)\n"); 1710ee6abef5SQuentin Colombet AssumptionCache AC(*F); 1711ee6abef5SQuentin Colombet KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1712ee6abef5SQuentin Colombet F->front().getTerminator()); 1713ee6abef5SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), ~(64llu - 1)); 1714ee6abef5SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 32u); 1715ee6abef5SQuentin Colombet Instruction &APtrPlus512 = findInstructionByName(F, "APtrPlus512"); 1716ee6abef5SQuentin Colombet Known = computeKnownBits(&APtrPlus512, M->getDataLayout(), /* Depth */ 0, &AC, 1717ee6abef5SQuentin Colombet F->front().getTerminator()); 1718ee6abef5SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), ~512llu & ~(64llu - 1)); 1719ee6abef5SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 512u | 32u); 1720ee6abef5SQuentin Colombet // The known range is not precise given computeKnownBits works 1721ee6abef5SQuentin Colombet // with the masks of zeros and ones, not the ranges. 1722ee6abef5SQuentin Colombet EXPECT_EQ(Known.getMinValue(), 544); 1723ee6abef5SQuentin Colombet EXPECT_EQ(Known.getMaxValue(), 575); 1724ee6abef5SQuentin Colombet } 1725ee6abef5SQuentin Colombet 17264ee240b8SChuanqi Xu TEST_F(ValueTrackingTest, HaveNoCommonBitsSet) { 17274ee240b8SChuanqi Xu { 17284ee240b8SChuanqi Xu // Check for an inverted mask: (X & ~M) op (Y & M). 17294ee240b8SChuanqi Xu auto M = parseModule(R"( 17304ee240b8SChuanqi Xu define i32 @test(i32 %X, i32 %Y, i32 %M) { 17314ee240b8SChuanqi Xu %1 = xor i32 %M, -1 17324ee240b8SChuanqi Xu %LHS = and i32 %1, %X 17334ee240b8SChuanqi Xu %RHS = and i32 %Y, %M 17344ee240b8SChuanqi Xu %Ret = add i32 %LHS, %RHS 17354ee240b8SChuanqi Xu ret i32 %Ret 17364ee240b8SChuanqi Xu })"); 17374ee240b8SChuanqi Xu 1738e59d6dc0SChuanqi Xu auto *F = M->getFunction("test"); 17394ee240b8SChuanqi Xu auto *LHS = findInstructionByNameOrNull(F, "LHS"); 17404ee240b8SChuanqi Xu auto *RHS = findInstructionByNameOrNull(F, "RHS"); 17414ee240b8SChuanqi Xu 17424ee240b8SChuanqi Xu const DataLayout &DL = M->getDataLayout(); 17434ee240b8SChuanqi Xu EXPECT_TRUE(haveNoCommonBitsSet(LHS, RHS, DL)); 17444ee240b8SChuanqi Xu EXPECT_TRUE(haveNoCommonBitsSet(RHS, LHS, DL)); 17454ee240b8SChuanqi Xu } 1746a2609be0SChuanqi Xu { 1747a2609be0SChuanqi Xu // Check for (A & B) and ~(A | B) 1748a2609be0SChuanqi Xu auto M = parseModule(R"( 1749a2609be0SChuanqi Xu define void @test(i32 %A, i32 %B) { 1750a2609be0SChuanqi Xu %LHS = and i32 %A, %B 1751a2609be0SChuanqi Xu %or = or i32 %A, %B 1752a2609be0SChuanqi Xu %RHS = xor i32 %or, -1 1753a2609be0SChuanqi Xu 1754a2609be0SChuanqi Xu %LHS2 = and i32 %B, %A 1755a2609be0SChuanqi Xu %or2 = or i32 %A, %B 1756a2609be0SChuanqi Xu %RHS2 = xor i32 %or2, -1 1757a2609be0SChuanqi Xu 1758a2609be0SChuanqi Xu ret void 1759a2609be0SChuanqi Xu })"); 1760a2609be0SChuanqi Xu 1761a2609be0SChuanqi Xu auto *F = M->getFunction("test"); 1762a2609be0SChuanqi Xu const DataLayout &DL = M->getDataLayout(); 1763a2609be0SChuanqi Xu 1764a2609be0SChuanqi Xu auto *LHS = findInstructionByNameOrNull(F, "LHS"); 1765a2609be0SChuanqi Xu auto *RHS = findInstructionByNameOrNull(F, "RHS"); 1766a2609be0SChuanqi Xu EXPECT_TRUE(haveNoCommonBitsSet(LHS, RHS, DL)); 1767a2609be0SChuanqi Xu EXPECT_TRUE(haveNoCommonBitsSet(RHS, LHS, DL)); 1768a2609be0SChuanqi Xu 1769a2609be0SChuanqi Xu auto *LHS2 = findInstructionByNameOrNull(F, "LHS2"); 1770a2609be0SChuanqi Xu auto *RHS2 = findInstructionByNameOrNull(F, "RHS2"); 1771a2609be0SChuanqi Xu EXPECT_TRUE(haveNoCommonBitsSet(LHS2, RHS2, DL)); 1772a2609be0SChuanqi Xu EXPECT_TRUE(haveNoCommonBitsSet(RHS2, LHS2, DL)); 1773a2609be0SChuanqi Xu } 1774a2609be0SChuanqi Xu { 1775a2609be0SChuanqi Xu // Check for (A & B) and ~(A | B) in vector version 1776a2609be0SChuanqi Xu auto M = parseModule(R"( 1777a2609be0SChuanqi Xu define void @test(<2 x i32> %A, <2 x i32> %B) { 1778a2609be0SChuanqi Xu %LHS = and <2 x i32> %A, %B 1779a2609be0SChuanqi Xu %or = or <2 x i32> %A, %B 1780a2609be0SChuanqi Xu %RHS = xor <2 x i32> %or, <i32 -1, i32 -1> 1781a2609be0SChuanqi Xu 1782a2609be0SChuanqi Xu %LHS2 = and <2 x i32> %B, %A 1783a2609be0SChuanqi Xu %or2 = or <2 x i32> %A, %B 1784a2609be0SChuanqi Xu %RHS2 = xor <2 x i32> %or2, <i32 -1, i32 -1> 1785a2609be0SChuanqi Xu 1786a2609be0SChuanqi Xu ret void 1787a2609be0SChuanqi Xu })"); 1788a2609be0SChuanqi Xu 1789a2609be0SChuanqi Xu auto *F = M->getFunction("test"); 1790a2609be0SChuanqi Xu const DataLayout &DL = M->getDataLayout(); 1791a2609be0SChuanqi Xu 1792a2609be0SChuanqi Xu auto *LHS = findInstructionByNameOrNull(F, "LHS"); 1793a2609be0SChuanqi Xu auto *RHS = findInstructionByNameOrNull(F, "RHS"); 1794a2609be0SChuanqi Xu EXPECT_TRUE(haveNoCommonBitsSet(LHS, RHS, DL)); 1795a2609be0SChuanqi Xu EXPECT_TRUE(haveNoCommonBitsSet(RHS, LHS, DL)); 1796a2609be0SChuanqi Xu 1797a2609be0SChuanqi Xu auto *LHS2 = findInstructionByNameOrNull(F, "LHS2"); 1798a2609be0SChuanqi Xu auto *RHS2 = findInstructionByNameOrNull(F, "RHS2"); 1799a2609be0SChuanqi Xu EXPECT_TRUE(haveNoCommonBitsSet(LHS2, RHS2, DL)); 1800a2609be0SChuanqi Xu EXPECT_TRUE(haveNoCommonBitsSet(RHS2, LHS2, DL)); 1801a2609be0SChuanqi Xu } 18024ee240b8SChuanqi Xu } 18034ee240b8SChuanqi Xu 1804329d15b5SVitaly Buka class IsBytewiseValueTest : public ValueTrackingTest, 1805329d15b5SVitaly Buka public ::testing::WithParamInterface< 1806329d15b5SVitaly Buka std::pair<const char *, const char *>> { 1807329d15b5SVitaly Buka protected: 1808329d15b5SVitaly Buka }; 1809329d15b5SVitaly Buka 1810329d15b5SVitaly Buka const std::pair<const char *, const char *> IsBytewiseValueTests[] = { 1811329d15b5SVitaly Buka { 1812329d15b5SVitaly Buka "i8 0", 1813329d15b5SVitaly Buka "i48* null", 1814329d15b5SVitaly Buka }, 1815329d15b5SVitaly Buka { 1816329d15b5SVitaly Buka "i8 undef", 1817329d15b5SVitaly Buka "i48* undef", 1818329d15b5SVitaly Buka }, 1819329d15b5SVitaly Buka { 1820329d15b5SVitaly Buka "i8 0", 1821329d15b5SVitaly Buka "i8 zeroinitializer", 1822329d15b5SVitaly Buka }, 1823329d15b5SVitaly Buka { 1824329d15b5SVitaly Buka "i8 0", 1825329d15b5SVitaly Buka "i8 0", 1826329d15b5SVitaly Buka }, 1827329d15b5SVitaly Buka { 1828329d15b5SVitaly Buka "i8 -86", 1829329d15b5SVitaly Buka "i8 -86", 1830329d15b5SVitaly Buka }, 1831329d15b5SVitaly Buka { 1832329d15b5SVitaly Buka "i8 -1", 1833329d15b5SVitaly Buka "i8 -1", 1834329d15b5SVitaly Buka }, 1835329d15b5SVitaly Buka { 1836329d15b5SVitaly Buka "i8 undef", 1837329d15b5SVitaly Buka "i16 undef", 1838329d15b5SVitaly Buka }, 1839329d15b5SVitaly Buka { 1840329d15b5SVitaly Buka "i8 0", 1841329d15b5SVitaly Buka "i16 0", 1842329d15b5SVitaly Buka }, 1843329d15b5SVitaly Buka { 1844329d15b5SVitaly Buka "", 1845329d15b5SVitaly Buka "i16 7", 1846329d15b5SVitaly Buka }, 1847329d15b5SVitaly Buka { 1848329d15b5SVitaly Buka "i8 -86", 1849329d15b5SVitaly Buka "i16 -21846", 1850329d15b5SVitaly Buka }, 1851329d15b5SVitaly Buka { 1852329d15b5SVitaly Buka "i8 -1", 1853329d15b5SVitaly Buka "i16 -1", 1854329d15b5SVitaly Buka }, 1855329d15b5SVitaly Buka { 1856329d15b5SVitaly Buka "i8 0", 1857329d15b5SVitaly Buka "i48 0", 1858329d15b5SVitaly Buka }, 1859329d15b5SVitaly Buka { 1860329d15b5SVitaly Buka "i8 -1", 1861329d15b5SVitaly Buka "i48 -1", 1862329d15b5SVitaly Buka }, 1863329d15b5SVitaly Buka { 1864329d15b5SVitaly Buka "i8 0", 1865329d15b5SVitaly Buka "i49 0", 1866329d15b5SVitaly Buka }, 1867329d15b5SVitaly Buka { 1868329d15b5SVitaly Buka "", 1869329d15b5SVitaly Buka "i49 -1", 1870329d15b5SVitaly Buka }, 1871329d15b5SVitaly Buka { 1872329d15b5SVitaly Buka "i8 0", 1873329d15b5SVitaly Buka "half 0xH0000", 1874329d15b5SVitaly Buka }, 1875329d15b5SVitaly Buka { 1876329d15b5SVitaly Buka "i8 -85", 1877329d15b5SVitaly Buka "half 0xHABAB", 1878329d15b5SVitaly Buka }, 1879329d15b5SVitaly Buka { 1880329d15b5SVitaly Buka "i8 0", 1881329d15b5SVitaly Buka "float 0.0", 1882329d15b5SVitaly Buka }, 1883329d15b5SVitaly Buka { 1884329d15b5SVitaly Buka "i8 -1", 1885329d15b5SVitaly Buka "float 0xFFFFFFFFE0000000", 1886329d15b5SVitaly Buka }, 1887329d15b5SVitaly Buka { 1888329d15b5SVitaly Buka "i8 0", 1889329d15b5SVitaly Buka "double 0.0", 1890329d15b5SVitaly Buka }, 1891329d15b5SVitaly Buka { 1892329d15b5SVitaly Buka "i8 -15", 1893329d15b5SVitaly Buka "double 0xF1F1F1F1F1F1F1F1", 1894329d15b5SVitaly Buka }, 1895329d15b5SVitaly Buka { 1896329d15b5SVitaly Buka "i8 undef", 1897329d15b5SVitaly Buka "i16* undef", 1898329d15b5SVitaly Buka }, 1899329d15b5SVitaly Buka { 1900329d15b5SVitaly Buka "i8 0", 1901329d15b5SVitaly Buka "i16* inttoptr (i64 0 to i16*)", 1902329d15b5SVitaly Buka }, 1903329d15b5SVitaly Buka { 1904c559e637SVitaly Buka "i8 -1", 1905329d15b5SVitaly Buka "i16* inttoptr (i64 -1 to i16*)", 1906329d15b5SVitaly Buka }, 1907329d15b5SVitaly Buka { 1908c559e637SVitaly Buka "i8 -86", 1909329d15b5SVitaly Buka "i16* inttoptr (i64 -6148914691236517206 to i16*)", 1910329d15b5SVitaly Buka }, 1911329d15b5SVitaly Buka { 1912329d15b5SVitaly Buka "", 1913329d15b5SVitaly Buka "i16* inttoptr (i48 -1 to i16*)", 1914329d15b5SVitaly Buka }, 1915329d15b5SVitaly Buka { 1916c559e637SVitaly Buka "i8 -1", 1917329d15b5SVitaly Buka "i16* inttoptr (i96 -1 to i16*)", 1918329d15b5SVitaly Buka }, 1919329d15b5SVitaly Buka { 192052096ee9SVitaly Buka "i8 undef", 1921329d15b5SVitaly Buka "[0 x i8] zeroinitializer", 1922329d15b5SVitaly Buka }, 1923329d15b5SVitaly Buka { 1924329d15b5SVitaly Buka "i8 undef", 1925329d15b5SVitaly Buka "[0 x i8] undef", 1926329d15b5SVitaly Buka }, 1927329d15b5SVitaly Buka { 192852096ee9SVitaly Buka "i8 undef", 1929329d15b5SVitaly Buka "[5 x [0 x i8]] zeroinitializer", 1930329d15b5SVitaly Buka }, 1931329d15b5SVitaly Buka { 1932329d15b5SVitaly Buka "i8 undef", 1933329d15b5SVitaly Buka "[5 x [0 x i8]] undef", 1934329d15b5SVitaly Buka }, 1935329d15b5SVitaly Buka { 1936329d15b5SVitaly Buka "i8 0", 1937329d15b5SVitaly Buka "[6 x i8] zeroinitializer", 1938329d15b5SVitaly Buka }, 1939329d15b5SVitaly Buka { 1940329d15b5SVitaly Buka "i8 undef", 1941329d15b5SVitaly Buka "[6 x i8] undef", 1942329d15b5SVitaly Buka }, 1943329d15b5SVitaly Buka { 1944329d15b5SVitaly Buka "i8 1", 1945329d15b5SVitaly Buka "[5 x i8] [i8 1, i8 1, i8 1, i8 1, i8 1]", 1946329d15b5SVitaly Buka }, 1947329d15b5SVitaly Buka { 1948329d15b5SVitaly Buka "", 1949329d15b5SVitaly Buka "[5 x i64] [i64 1, i64 1, i64 1, i64 1, i64 1]", 1950329d15b5SVitaly Buka }, 1951329d15b5SVitaly Buka { 1952329d15b5SVitaly Buka "i8 -1", 1953329d15b5SVitaly Buka "[5 x i64] [i64 -1, i64 -1, i64 -1, i64 -1, i64 -1]", 1954329d15b5SVitaly Buka }, 1955329d15b5SVitaly Buka { 1956329d15b5SVitaly Buka "", 1957329d15b5SVitaly Buka "[4 x i8] [i8 1, i8 2, i8 1, i8 1]", 1958329d15b5SVitaly Buka }, 1959329d15b5SVitaly Buka { 1960329d15b5SVitaly Buka "i8 1", 1961329d15b5SVitaly Buka "[4 x i8] [i8 1, i8 undef, i8 1, i8 1]", 1962329d15b5SVitaly Buka }, 1963329d15b5SVitaly Buka { 1964329d15b5SVitaly Buka "i8 0", 1965329d15b5SVitaly Buka "<6 x i8> zeroinitializer", 1966329d15b5SVitaly Buka }, 1967329d15b5SVitaly Buka { 1968329d15b5SVitaly Buka "i8 undef", 1969329d15b5SVitaly Buka "<6 x i8> undef", 1970329d15b5SVitaly Buka }, 1971329d15b5SVitaly Buka { 1972329d15b5SVitaly Buka "i8 1", 1973329d15b5SVitaly Buka "<5 x i8> <i8 1, i8 1, i8 1, i8 1, i8 1>", 1974329d15b5SVitaly Buka }, 1975329d15b5SVitaly Buka { 1976329d15b5SVitaly Buka "", 1977329d15b5SVitaly Buka "<5 x i64> <i64 1, i64 1, i64 1, i64 1, i64 1>", 1978329d15b5SVitaly Buka }, 1979329d15b5SVitaly Buka { 1980329d15b5SVitaly Buka "i8 -1", 1981329d15b5SVitaly Buka "<5 x i64> <i64 -1, i64 -1, i64 -1, i64 -1, i64 -1>", 1982329d15b5SVitaly Buka }, 1983329d15b5SVitaly Buka { 1984329d15b5SVitaly Buka "", 1985329d15b5SVitaly Buka "<4 x i8> <i8 1, i8 1, i8 2, i8 1>", 1986329d15b5SVitaly Buka }, 1987329d15b5SVitaly Buka { 1988b1bff76eSVitaly Buka "i8 5", 1989329d15b5SVitaly Buka "<2 x i8> < i8 5, i8 undef >", 1990329d15b5SVitaly Buka }, 1991329d15b5SVitaly Buka { 1992329d15b5SVitaly Buka "i8 0", 1993329d15b5SVitaly Buka "[2 x [2 x i16]] zeroinitializer", 1994329d15b5SVitaly Buka }, 1995329d15b5SVitaly Buka { 1996329d15b5SVitaly Buka "i8 undef", 1997329d15b5SVitaly Buka "[2 x [2 x i16]] undef", 1998329d15b5SVitaly Buka }, 1999329d15b5SVitaly Buka { 2000329d15b5SVitaly Buka "i8 -86", 2001329d15b5SVitaly Buka "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], " 2002329d15b5SVitaly Buka "[2 x i16] [i16 -21846, i16 -21846]]", 2003329d15b5SVitaly Buka }, 2004329d15b5SVitaly Buka { 2005329d15b5SVitaly Buka "", 2006329d15b5SVitaly Buka "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], " 2007329d15b5SVitaly Buka "[2 x i16] [i16 -21836, i16 -21846]]", 2008329d15b5SVitaly Buka }, 2009329d15b5SVitaly Buka { 201052096ee9SVitaly Buka "i8 undef", 2011329d15b5SVitaly Buka "{ } zeroinitializer", 2012329d15b5SVitaly Buka }, 2013329d15b5SVitaly Buka { 2014329d15b5SVitaly Buka "i8 undef", 2015329d15b5SVitaly Buka "{ } undef", 2016329d15b5SVitaly Buka }, 2017329d15b5SVitaly Buka { 201852096ee9SVitaly Buka "i8 undef", 2019329d15b5SVitaly Buka "{ {}, {} } zeroinitializer", 2020329d15b5SVitaly Buka }, 2021329d15b5SVitaly Buka { 2022329d15b5SVitaly Buka "i8 undef", 2023329d15b5SVitaly Buka "{ {}, {} } undef", 2024329d15b5SVitaly Buka }, 2025329d15b5SVitaly Buka { 2026329d15b5SVitaly Buka "i8 0", 2027329d15b5SVitaly Buka "{i8, i64, i16*} zeroinitializer", 2028329d15b5SVitaly Buka }, 2029329d15b5SVitaly Buka { 2030329d15b5SVitaly Buka "i8 undef", 2031329d15b5SVitaly Buka "{i8, i64, i16*} undef", 2032329d15b5SVitaly Buka }, 2033329d15b5SVitaly Buka { 2034329d15b5SVitaly Buka "i8 -86", 2035329d15b5SVitaly Buka "{i8, i64, i16*} {i8 -86, i64 -6148914691236517206, i16* undef}", 2036329d15b5SVitaly Buka }, 2037329d15b5SVitaly Buka { 2038329d15b5SVitaly Buka "", 2039329d15b5SVitaly Buka "{i8, i64, i16*} {i8 86, i64 -6148914691236517206, i16* undef}", 2040329d15b5SVitaly Buka }, 2041329d15b5SVitaly Buka }; 2042329d15b5SVitaly Buka 2043d4d80a29SBenjamin Kramer INSTANTIATE_TEST_SUITE_P(IsBytewiseValueParamTests, IsBytewiseValueTest, 2044d4d80a29SBenjamin Kramer ::testing::ValuesIn(IsBytewiseValueTests)); 2045329d15b5SVitaly Buka 2046329d15b5SVitaly Buka TEST_P(IsBytewiseValueTest, IsBytewiseValue) { 2047329d15b5SVitaly Buka auto M = parseModule(std::string("@test = global ") + GetParam().second); 2048329d15b5SVitaly Buka GlobalVariable *GV = dyn_cast<GlobalVariable>(M->getNamedValue("test")); 2049329d15b5SVitaly Buka Value *Actual = isBytewiseValue(GV->getInitializer(), M->getDataLayout()); 2050329d15b5SVitaly Buka std::string Buff; 2051329d15b5SVitaly Buka raw_string_ostream S(Buff); 2052329d15b5SVitaly Buka if (Actual) 2053329d15b5SVitaly Buka S << *Actual; 2054329d15b5SVitaly Buka EXPECT_EQ(GetParam().first, S.str()); 2055329d15b5SVitaly Buka } 20568d041811SFlorian Hahn 20578d041811SFlorian Hahn TEST_F(ValueTrackingTest, ComputeConstantRange) { 20588d041811SFlorian Hahn { 20598d041811SFlorian Hahn // Assumptions: 20608d041811SFlorian Hahn // * stride >= 5 20618d041811SFlorian Hahn // * stride < 10 20628d041811SFlorian Hahn // 20638d041811SFlorian Hahn // stride = [5, 10) 20648d041811SFlorian Hahn auto M = parseModule(R"( 20658d041811SFlorian Hahn declare void @llvm.assume(i1) 20668d041811SFlorian Hahn 20678d041811SFlorian Hahn define i32 @test(i32 %stride) { 20688d041811SFlorian Hahn %gt = icmp uge i32 %stride, 5 20698d041811SFlorian Hahn call void @llvm.assume(i1 %gt) 20708d041811SFlorian Hahn %lt = icmp ult i32 %stride, 10 20718d041811SFlorian Hahn call void @llvm.assume(i1 %lt) 20728d041811SFlorian Hahn %stride.plus.one = add nsw nuw i32 %stride, 1 20738d041811SFlorian Hahn ret i32 %stride.plus.one 20748d041811SFlorian Hahn })"); 20758d041811SFlorian Hahn Function *F = M->getFunction("test"); 20768d041811SFlorian Hahn 20778d041811SFlorian Hahn AssumptionCache AC(*F); 20788d041811SFlorian Hahn Value *Stride = &*F->arg_begin(); 20790edf9995SSanjay Patel ConstantRange CR1 = computeConstantRange(Stride, false, true, &AC, nullptr); 20808d041811SFlorian Hahn EXPECT_TRUE(CR1.isFullSet()); 20818d041811SFlorian Hahn 20828d041811SFlorian Hahn Instruction *I = &findInstructionByName(F, "stride.plus.one"); 20830edf9995SSanjay Patel ConstantRange CR2 = computeConstantRange(Stride, false, true, &AC, I); 20848d041811SFlorian Hahn EXPECT_EQ(5, CR2.getLower()); 20858d041811SFlorian Hahn EXPECT_EQ(10, CR2.getUpper()); 20868d041811SFlorian Hahn } 20878d041811SFlorian Hahn 20888d041811SFlorian Hahn { 20898d041811SFlorian Hahn // Assumptions: 20908d041811SFlorian Hahn // * stride >= 5 20918d041811SFlorian Hahn // * stride < 200 20928d041811SFlorian Hahn // * stride == 99 20938d041811SFlorian Hahn // 20948d041811SFlorian Hahn // stride = [99, 100) 20958d041811SFlorian Hahn auto M = parseModule(R"( 20968d041811SFlorian Hahn declare void @llvm.assume(i1) 20978d041811SFlorian Hahn 20988d041811SFlorian Hahn define i32 @test(i32 %stride) { 20998d041811SFlorian Hahn %gt = icmp uge i32 %stride, 5 21008d041811SFlorian Hahn call void @llvm.assume(i1 %gt) 21018d041811SFlorian Hahn %lt = icmp ult i32 %stride, 200 21028d041811SFlorian Hahn call void @llvm.assume(i1 %lt) 21038d041811SFlorian Hahn %eq = icmp eq i32 %stride, 99 21048d041811SFlorian Hahn call void @llvm.assume(i1 %eq) 21058d041811SFlorian Hahn %stride.plus.one = add nsw nuw i32 %stride, 1 21068d041811SFlorian Hahn ret i32 %stride.plus.one 21078d041811SFlorian Hahn })"); 21088d041811SFlorian Hahn Function *F = M->getFunction("test"); 21098d041811SFlorian Hahn 21108d041811SFlorian Hahn AssumptionCache AC(*F); 21118d041811SFlorian Hahn Value *Stride = &*F->arg_begin(); 21128d041811SFlorian Hahn Instruction *I = &findInstructionByName(F, "stride.plus.one"); 21130edf9995SSanjay Patel ConstantRange CR = computeConstantRange(Stride, false, true, &AC, I); 21148d041811SFlorian Hahn EXPECT_EQ(99, *CR.getSingleElement()); 21158d041811SFlorian Hahn } 21168d041811SFlorian Hahn 21178d041811SFlorian Hahn { 21188d041811SFlorian Hahn // Assumptions: 21198d041811SFlorian Hahn // * stride >= 5 21208d041811SFlorian Hahn // * stride >= 50 21218d041811SFlorian Hahn // * stride < 100 21228d041811SFlorian Hahn // * stride < 200 21238d041811SFlorian Hahn // 21248d041811SFlorian Hahn // stride = [50, 100) 21258d041811SFlorian Hahn auto M = parseModule(R"( 21268d041811SFlorian Hahn declare void @llvm.assume(i1) 21278d041811SFlorian Hahn 21288d041811SFlorian Hahn define i32 @test(i32 %stride, i1 %cond) { 21298d041811SFlorian Hahn %gt = icmp uge i32 %stride, 5 21308d041811SFlorian Hahn call void @llvm.assume(i1 %gt) 21318d041811SFlorian Hahn %gt.2 = icmp uge i32 %stride, 50 21328d041811SFlorian Hahn call void @llvm.assume(i1 %gt.2) 21338d041811SFlorian Hahn br i1 %cond, label %bb1, label %bb2 21348d041811SFlorian Hahn 21358d041811SFlorian Hahn bb1: 21368d041811SFlorian Hahn %lt = icmp ult i32 %stride, 200 21378d041811SFlorian Hahn call void @llvm.assume(i1 %lt) 21388d041811SFlorian Hahn %lt.2 = icmp ult i32 %stride, 100 21398d041811SFlorian Hahn call void @llvm.assume(i1 %lt.2) 21408d041811SFlorian Hahn %stride.plus.one = add nsw nuw i32 %stride, 1 21418d041811SFlorian Hahn ret i32 %stride.plus.one 21428d041811SFlorian Hahn 21438d041811SFlorian Hahn bb2: 21448d041811SFlorian Hahn ret i32 0 21458d041811SFlorian Hahn })"); 21468d041811SFlorian Hahn Function *F = M->getFunction("test"); 21478d041811SFlorian Hahn 21488d041811SFlorian Hahn AssumptionCache AC(*F); 21498d041811SFlorian Hahn Value *Stride = &*F->arg_begin(); 21508d041811SFlorian Hahn Instruction *GT2 = &findInstructionByName(F, "gt.2"); 21510edf9995SSanjay Patel ConstantRange CR = computeConstantRange(Stride, false, true, &AC, GT2); 21528d041811SFlorian Hahn EXPECT_EQ(5, CR.getLower()); 21538d041811SFlorian Hahn EXPECT_EQ(0, CR.getUpper()); 21548d041811SFlorian Hahn 21558d041811SFlorian Hahn Instruction *I = &findInstructionByName(F, "stride.plus.one"); 21560edf9995SSanjay Patel ConstantRange CR2 = computeConstantRange(Stride, false, true, &AC, I); 21578d041811SFlorian Hahn EXPECT_EQ(50, CR2.getLower()); 21588d041811SFlorian Hahn EXPECT_EQ(100, CR2.getUpper()); 21598d041811SFlorian Hahn } 21608d041811SFlorian Hahn 21618d041811SFlorian Hahn { 21628d041811SFlorian Hahn // Assumptions: 21638d041811SFlorian Hahn // * stride > 5 21648d041811SFlorian Hahn // * stride < 5 21658d041811SFlorian Hahn // 21668d041811SFlorian Hahn // stride = empty range, as the assumptions contradict each other. 21678d041811SFlorian Hahn auto M = parseModule(R"( 21688d041811SFlorian Hahn declare void @llvm.assume(i1) 21698d041811SFlorian Hahn 21708d041811SFlorian Hahn define i32 @test(i32 %stride, i1 %cond) { 21718d041811SFlorian Hahn %gt = icmp ugt i32 %stride, 5 21728d041811SFlorian Hahn call void @llvm.assume(i1 %gt) 21738d041811SFlorian Hahn %lt = icmp ult i32 %stride, 5 21748d041811SFlorian Hahn call void @llvm.assume(i1 %lt) 21758d041811SFlorian Hahn %stride.plus.one = add nsw nuw i32 %stride, 1 21768d041811SFlorian Hahn ret i32 %stride.plus.one 21778d041811SFlorian Hahn })"); 21788d041811SFlorian Hahn Function *F = M->getFunction("test"); 21798d041811SFlorian Hahn 21808d041811SFlorian Hahn AssumptionCache AC(*F); 21818d041811SFlorian Hahn Value *Stride = &*F->arg_begin(); 21828d041811SFlorian Hahn 21838d041811SFlorian Hahn Instruction *I = &findInstructionByName(F, "stride.plus.one"); 21840edf9995SSanjay Patel ConstantRange CR = computeConstantRange(Stride, false, true, &AC, I); 21858d041811SFlorian Hahn EXPECT_TRUE(CR.isEmptySet()); 21868d041811SFlorian Hahn } 21878d041811SFlorian Hahn 21888d041811SFlorian Hahn { 21898d041811SFlorian Hahn // Assumptions: 21908d041811SFlorian Hahn // * x.1 >= 5 21918d041811SFlorian Hahn // * x.2 < x.1 21928d041811SFlorian Hahn // 2193b58eda39SChang-Sun Lin, Jr // stride = [0, -1) 21948d041811SFlorian Hahn auto M = parseModule(R"( 21958d041811SFlorian Hahn declare void @llvm.assume(i1) 21968d041811SFlorian Hahn 21978d041811SFlorian Hahn define i32 @test(i32 %x.1, i32 %x.2) { 21988d041811SFlorian Hahn %gt = icmp uge i32 %x.1, 5 21998d041811SFlorian Hahn call void @llvm.assume(i1 %gt) 22008d041811SFlorian Hahn %lt = icmp ult i32 %x.2, %x.1 22018d041811SFlorian Hahn call void @llvm.assume(i1 %lt) 22028d041811SFlorian Hahn %stride.plus.one = add nsw nuw i32 %x.1, 1 22038d041811SFlorian Hahn ret i32 %stride.plus.one 22048d041811SFlorian Hahn })"); 22058d041811SFlorian Hahn Function *F = M->getFunction("test"); 22068d041811SFlorian Hahn 22078d041811SFlorian Hahn AssumptionCache AC(*F); 2208b58eda39SChang-Sun Lin, Jr Value *X1 = &*(F->arg_begin()); 2209b58eda39SChang-Sun Lin, Jr Value *X2 = &*std::next(F->arg_begin()); 2210b58eda39SChang-Sun Lin, Jr 2211b58eda39SChang-Sun Lin, Jr Instruction *I = &findInstructionByName(F, "stride.plus.one"); 22120edf9995SSanjay Patel ConstantRange CR1 = computeConstantRange(X1, false, true, &AC, I); 22130edf9995SSanjay Patel ConstantRange CR2 = computeConstantRange(X2, false, true, &AC, I); 2214b58eda39SChang-Sun Lin, Jr 2215b58eda39SChang-Sun Lin, Jr EXPECT_EQ(5, CR1.getLower()); 2216b58eda39SChang-Sun Lin, Jr EXPECT_EQ(0, CR1.getUpper()); 2217b58eda39SChang-Sun Lin, Jr 2218b58eda39SChang-Sun Lin, Jr EXPECT_EQ(0, CR2.getLower()); 2219b58eda39SChang-Sun Lin, Jr EXPECT_EQ(0xffffffff, CR2.getUpper()); 2220b58eda39SChang-Sun Lin, Jr 2221b58eda39SChang-Sun Lin, Jr // Check the depth cutoff results in a conservative result (full set) by 2222b58eda39SChang-Sun Lin, Jr // passing Depth == MaxDepth == 6. 22230edf9995SSanjay Patel ConstantRange CR3 = computeConstantRange(X2, false, true, &AC, I, nullptr, 6); 2224b58eda39SChang-Sun Lin, Jr EXPECT_TRUE(CR3.isFullSet()); 2225b58eda39SChang-Sun Lin, Jr } 2226b58eda39SChang-Sun Lin, Jr { 2227b58eda39SChang-Sun Lin, Jr // Assumptions: 2228b58eda39SChang-Sun Lin, Jr // * x.2 <= x.1 2229b58eda39SChang-Sun Lin, Jr auto M = parseModule(R"( 2230b58eda39SChang-Sun Lin, Jr declare void @llvm.assume(i1) 2231b58eda39SChang-Sun Lin, Jr 2232b58eda39SChang-Sun Lin, Jr define i32 @test(i32 %x.1, i32 %x.2) { 2233b58eda39SChang-Sun Lin, Jr %lt = icmp ule i32 %x.2, %x.1 2234b58eda39SChang-Sun Lin, Jr call void @llvm.assume(i1 %lt) 2235b58eda39SChang-Sun Lin, Jr %stride.plus.one = add nsw nuw i32 %x.1, 1 2236b58eda39SChang-Sun Lin, Jr ret i32 %stride.plus.one 2237b58eda39SChang-Sun Lin, Jr })"); 2238b58eda39SChang-Sun Lin, Jr Function *F = M->getFunction("test"); 2239b58eda39SChang-Sun Lin, Jr 2240b58eda39SChang-Sun Lin, Jr AssumptionCache AC(*F); 22418d041811SFlorian Hahn Value *X2 = &*std::next(F->arg_begin()); 22428d041811SFlorian Hahn 22438d041811SFlorian Hahn Instruction *I = &findInstructionByName(F, "stride.plus.one"); 22440edf9995SSanjay Patel ConstantRange CR1 = computeConstantRange(X2, false, true, &AC, I); 2245b58eda39SChang-Sun Lin, Jr // If we don't know the value of x.2, we don't know the value of x.1. 2246b58eda39SChang-Sun Lin, Jr EXPECT_TRUE(CR1.isFullSet()); 22478d041811SFlorian Hahn } 22488d041811SFlorian Hahn } 22497f1bb326SVitaly Buka 22506ccacb46SVitaly Buka struct FindAllocaForValueTestParams { 22516ccacb46SVitaly Buka const char *IR; 2252a6927c86SVitaly Buka bool AnyOffsetResult; 2253a6927c86SVitaly Buka bool ZeroOffsetResult; 22546ccacb46SVitaly Buka }; 22556ccacb46SVitaly Buka 22567f1bb326SVitaly Buka class FindAllocaForValueTest 22577f1bb326SVitaly Buka : public ValueTrackingTest, 22586ccacb46SVitaly Buka public ::testing::WithParamInterface<FindAllocaForValueTestParams> { 22597f1bb326SVitaly Buka protected: 22607f1bb326SVitaly Buka }; 22617f1bb326SVitaly Buka 22626ccacb46SVitaly Buka const FindAllocaForValueTestParams FindAllocaForValueTests[] = { 22637f1bb326SVitaly Buka {R"( 22647f1bb326SVitaly Buka define void @test() { 22657f1bb326SVitaly Buka %a = alloca i64 22667f1bb326SVitaly Buka %r = bitcast i64* %a to i32* 22677f1bb326SVitaly Buka ret void 22687f1bb326SVitaly Buka })", 2269a6927c86SVitaly Buka true, true}, 22707f1bb326SVitaly Buka 22717f1bb326SVitaly Buka {R"( 22727f1bb326SVitaly Buka define void @test() { 22737f1bb326SVitaly Buka %a = alloca i32 22747f1bb326SVitaly Buka %r = getelementptr i32, i32* %a, i32 1 22757f1bb326SVitaly Buka ret void 22767f1bb326SVitaly Buka })", 2277a6927c86SVitaly Buka true, false}, 2278a6927c86SVitaly Buka 2279a6927c86SVitaly Buka {R"( 2280a6927c86SVitaly Buka define void @test() { 2281a6927c86SVitaly Buka %a = alloca i32 2282a6927c86SVitaly Buka %r = getelementptr i32, i32* %a, i32 0 2283a6927c86SVitaly Buka ret void 2284a6927c86SVitaly Buka })", 2285a6927c86SVitaly Buka true, true}, 22867f1bb326SVitaly Buka 22877f1bb326SVitaly Buka {R"( 22887f1bb326SVitaly Buka define void @test(i1 %cond) { 22897f1bb326SVitaly Buka entry: 22907f1bb326SVitaly Buka %a = alloca i32 22917f1bb326SVitaly Buka br label %bb1 22927f1bb326SVitaly Buka 22937f1bb326SVitaly Buka bb1: 22947f1bb326SVitaly Buka %r = phi i32* [ %a, %entry ], [ %r, %bb1 ] 22957f1bb326SVitaly Buka br i1 %cond, label %bb1, label %exit 22967f1bb326SVitaly Buka 22977f1bb326SVitaly Buka exit: 22987f1bb326SVitaly Buka ret void 22997f1bb326SVitaly Buka })", 2300a6927c86SVitaly Buka true, true}, 23017f1bb326SVitaly Buka 23027f1bb326SVitaly Buka {R"( 23037f1bb326SVitaly Buka define void @test(i1 %cond) { 2304469debe0SVitaly Buka %a = alloca i32 2305469debe0SVitaly Buka %r = select i1 %cond, i32* %a, i32* %a 2306469debe0SVitaly Buka ret void 2307469debe0SVitaly Buka })", 2308a6927c86SVitaly Buka true, true}, 2309469debe0SVitaly Buka 2310469debe0SVitaly Buka {R"( 2311469debe0SVitaly Buka define void @test(i1 %cond) { 2312469debe0SVitaly Buka %a = alloca i32 2313469debe0SVitaly Buka %b = alloca i32 2314469debe0SVitaly Buka %r = select i1 %cond, i32* %a, i32* %b 2315469debe0SVitaly Buka ret void 2316469debe0SVitaly Buka })", 2317a6927c86SVitaly Buka false, false}, 2318469debe0SVitaly Buka 2319469debe0SVitaly Buka {R"( 2320469debe0SVitaly Buka define void @test(i1 %cond) { 23217f1bb326SVitaly Buka entry: 23227f1bb326SVitaly Buka %a = alloca i64 23237f1bb326SVitaly Buka %a32 = bitcast i64* %a to i32* 23247f1bb326SVitaly Buka br label %bb1 23257f1bb326SVitaly Buka 23267f1bb326SVitaly Buka bb1: 23277f1bb326SVitaly Buka %x = phi i32* [ %a32, %entry ], [ %x, %bb1 ] 23287f1bb326SVitaly Buka %r = getelementptr i32, i32* %x, i32 1 23297f1bb326SVitaly Buka br i1 %cond, label %bb1, label %exit 23307f1bb326SVitaly Buka 23317f1bb326SVitaly Buka exit: 23327f1bb326SVitaly Buka ret void 23337f1bb326SVitaly Buka })", 2334a6927c86SVitaly Buka true, false}, 23357f1bb326SVitaly Buka 23367f1bb326SVitaly Buka {R"( 23377f1bb326SVitaly Buka define void @test(i1 %cond) { 23387f1bb326SVitaly Buka entry: 23397f1bb326SVitaly Buka %a = alloca i64 23407f1bb326SVitaly Buka %a32 = bitcast i64* %a to i32* 23417f1bb326SVitaly Buka br label %bb1 23427f1bb326SVitaly Buka 23437f1bb326SVitaly Buka bb1: 23447f1bb326SVitaly Buka %x = phi i32* [ %a32, %entry ], [ %r, %bb1 ] 23457f1bb326SVitaly Buka %r = getelementptr i32, i32* %x, i32 1 23467f1bb326SVitaly Buka br i1 %cond, label %bb1, label %exit 23477f1bb326SVitaly Buka 23487f1bb326SVitaly Buka exit: 23497f1bb326SVitaly Buka ret void 23507f1bb326SVitaly Buka })", 235123524fdeSVitaly Buka true, false}, 23527f1bb326SVitaly Buka 23537f1bb326SVitaly Buka {R"( 23547f1bb326SVitaly Buka define void @test(i1 %cond, i64* %a) { 23557f1bb326SVitaly Buka entry: 23567f1bb326SVitaly Buka %r = bitcast i64* %a to i32* 23577f1bb326SVitaly Buka ret void 23587f1bb326SVitaly Buka })", 2359a6927c86SVitaly Buka false, false}, 23607f1bb326SVitaly Buka 23617f1bb326SVitaly Buka {R"( 23627f1bb326SVitaly Buka define void @test(i1 %cond) { 23637f1bb326SVitaly Buka entry: 23647f1bb326SVitaly Buka %a = alloca i32 23657f1bb326SVitaly Buka %b = alloca i32 23667f1bb326SVitaly Buka br label %bb1 23677f1bb326SVitaly Buka 23687f1bb326SVitaly Buka bb1: 23698191603dSVitaly Buka %r = phi i32* [ %a, %entry ], [ %b, %bb1 ] 23707f1bb326SVitaly Buka br i1 %cond, label %bb1, label %exit 23717f1bb326SVitaly Buka 23727f1bb326SVitaly Buka exit: 23737f1bb326SVitaly Buka ret void 23747f1bb326SVitaly Buka })", 2375a6927c86SVitaly Buka false, false}, 23760a22510fSFlorian Mayer {R"( 23770a22510fSFlorian Mayer declare i32* @retptr(i32* returned) 23780a22510fSFlorian Mayer define void @test(i1 %cond) { 23790a22510fSFlorian Mayer %a = alloca i32 23800a22510fSFlorian Mayer %r = call i32* @retptr(i32* %a) 23810a22510fSFlorian Mayer ret void 23820a22510fSFlorian Mayer })", 23830a22510fSFlorian Mayer true, true}, 23840a22510fSFlorian Mayer {R"( 23850a22510fSFlorian Mayer declare i32* @fun(i32*) 23860a22510fSFlorian Mayer define void @test(i1 %cond) { 23870a22510fSFlorian Mayer %a = alloca i32 23880a22510fSFlorian Mayer %r = call i32* @fun(i32* %a) 23890a22510fSFlorian Mayer ret void 23900a22510fSFlorian Mayer })", 23910a22510fSFlorian Mayer false, false}, 23927f1bb326SVitaly Buka }; 23937f1bb326SVitaly Buka 23947f1bb326SVitaly Buka TEST_P(FindAllocaForValueTest, findAllocaForValue) { 23956ccacb46SVitaly Buka auto M = parseModule(GetParam().IR); 23967f1bb326SVitaly Buka Function *F = M->getFunction("test"); 23977f1bb326SVitaly Buka Instruction *I = &findInstructionByName(F, "r"); 23987f1bb326SVitaly Buka const AllocaInst *AI = findAllocaForValue(I); 2399a6927c86SVitaly Buka EXPECT_EQ(!!AI, GetParam().AnyOffsetResult); 2400a6927c86SVitaly Buka } 2401a6927c86SVitaly Buka 2402a6927c86SVitaly Buka TEST_P(FindAllocaForValueTest, findAllocaForValueZeroOffset) { 2403a6927c86SVitaly Buka auto M = parseModule(GetParam().IR); 2404a6927c86SVitaly Buka Function *F = M->getFunction("test"); 2405a6927c86SVitaly Buka Instruction *I = &findInstructionByName(F, "r"); 2406a6927c86SVitaly Buka const AllocaInst *AI = findAllocaForValue(I, true); 2407a6927c86SVitaly Buka EXPECT_EQ(!!AI, GetParam().ZeroOffsetResult); 24087f1bb326SVitaly Buka } 24097f1bb326SVitaly Buka 2410d4d80a29SBenjamin Kramer INSTANTIATE_TEST_SUITE_P(FindAllocaForValueTest, FindAllocaForValueTest, 2411d4d80a29SBenjamin Kramer ::testing::ValuesIn(FindAllocaForValueTests)); 2412