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" 15134bec27SJames Molloy #include "llvm/IR/InstIterator.h" 16be04aba6SCraig Topper #include "llvm/IR/Instructions.h" 17134bec27SJames Molloy #include "llvm/IR/LLVMContext.h" 18134bec27SJames Molloy #include "llvm/IR/Module.h" 19134bec27SJames Molloy #include "llvm/Support/ErrorHandling.h" 2070dbd5fbSSimon Dardis #include "llvm/Support/KnownBits.h" 21329d15b5SVitaly Buka #include "llvm/Support/SourceMgr.h" 22565f4e72SAlex Richardson #include "llvm/Transforms/Utils/Local.h" 23134bec27SJames Molloy #include "gtest/gtest.h" 24134bec27SJames Molloy 25134bec27SJames Molloy using namespace llvm; 26134bec27SJames Molloy 27134bec27SJames Molloy namespace { 28134bec27SJames Molloy 299b3c2a72SJuneyoung Lee static Instruction *findInstructionByNameOrNull(Function *F, StringRef Name) { 308d041811SFlorian Hahn for (Instruction &I : instructions(F)) 318d041811SFlorian Hahn if (I.getName() == Name) 329b3c2a72SJuneyoung Lee return &I; 339b3c2a72SJuneyoung Lee 349b3c2a72SJuneyoung Lee return nullptr; 359b3c2a72SJuneyoung Lee } 369b3c2a72SJuneyoung Lee 379b3c2a72SJuneyoung Lee static Instruction &findInstructionByName(Function *F, StringRef Name) { 389b3c2a72SJuneyoung Lee auto *I = findInstructionByNameOrNull(F, Name); 399b3c2a72SJuneyoung Lee if (I) 409b3c2a72SJuneyoung Lee return *I; 418d041811SFlorian Hahn 428d041811SFlorian Hahn llvm_unreachable("Expected value not found"); 438d041811SFlorian Hahn } 448d041811SFlorian Hahn 45219e5367SNikita Popov class ValueTrackingTest : public testing::Test { 46134bec27SJames Molloy protected: 47329d15b5SVitaly Buka std::unique_ptr<Module> parseModule(StringRef Assembly) { 48134bec27SJames Molloy SMDiagnostic Error; 49329d15b5SVitaly Buka std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context); 50134bec27SJames Molloy 51134bec27SJames Molloy std::string errMsg; 52134bec27SJames Molloy raw_string_ostream os(errMsg); 53134bec27SJames Molloy Error.print("", os); 54329d15b5SVitaly Buka EXPECT_TRUE(M) << os.str(); 55134bec27SJames Molloy 56329d15b5SVitaly Buka return M; 57329d15b5SVitaly Buka } 58329d15b5SVitaly Buka 59329d15b5SVitaly Buka void parseAssembly(StringRef Assembly) { 60329d15b5SVitaly Buka M = parseModule(Assembly); 61329d15b5SVitaly Buka ASSERT_TRUE(M); 62134bec27SJames Molloy 63d86fff6aSNikita Popov F = M->getFunction("test"); 64329d15b5SVitaly Buka ASSERT_TRUE(F) << "Test must have a function @test"; 65329d15b5SVitaly Buka if (!F) 66329d15b5SVitaly Buka return; 67134bec27SJames Molloy 689b3c2a72SJuneyoung Lee A = findInstructionByNameOrNull(F, "A"); 69329d15b5SVitaly Buka ASSERT_TRUE(A) << "@test must have an instruction %A"; 70eca40c36SJuneyoung Lee A2 = findInstructionByNameOrNull(F, "A2"); 71eca40c36SJuneyoung Lee A3 = findInstructionByNameOrNull(F, "A3"); 72eca40c36SJuneyoung Lee A4 = findInstructionByNameOrNull(F, "A4"); 739b3c2a72SJuneyoung Lee 749b3c2a72SJuneyoung Lee CxtI = findInstructionByNameOrNull(F, "CxtI"); 759b3c2a72SJuneyoung Lee CxtI2 = findInstructionByNameOrNull(F, "CxtI2"); 769b3c2a72SJuneyoung Lee CxtI3 = findInstructionByNameOrNull(F, "CxtI3"); 77134bec27SJames Molloy } 78134bec27SJames Molloy 79219e5367SNikita Popov LLVMContext Context; 80219e5367SNikita Popov std::unique_ptr<Module> M; 81d86fff6aSNikita Popov Function *F = nullptr; 82329d15b5SVitaly Buka Instruction *A = nullptr; 83eca40c36SJuneyoung Lee // Instructions (optional) 84eca40c36SJuneyoung Lee Instruction *A2 = nullptr, *A3 = nullptr, *A4 = nullptr; 859b3c2a72SJuneyoung Lee 869b3c2a72SJuneyoung Lee // Context instructions (optional) 879b3c2a72SJuneyoung Lee Instruction *CxtI = nullptr, *CxtI2 = nullptr, *CxtI3 = nullptr; 88219e5367SNikita Popov }; 89219e5367SNikita Popov 90219e5367SNikita Popov class MatchSelectPatternTest : public ValueTrackingTest { 91219e5367SNikita Popov protected: 92134bec27SJames Molloy void expectPattern(const SelectPatternResult &P) { 93134bec27SJames Molloy Value *LHS, *RHS; 94134bec27SJames Molloy Instruction::CastOps CastOp; 95134bec27SJames Molloy SelectPatternResult R = matchSelectPattern(A, LHS, RHS, &CastOp); 96134bec27SJames Molloy EXPECT_EQ(P.Flavor, R.Flavor); 97134bec27SJames Molloy EXPECT_EQ(P.NaNBehavior, R.NaNBehavior); 98134bec27SJames Molloy EXPECT_EQ(P.Ordered, R.Ordered); 99134bec27SJames Molloy } 100219e5367SNikita Popov }; 101134bec27SJames Molloy 102219e5367SNikita Popov class ComputeKnownBitsTest : public ValueTrackingTest { 103219e5367SNikita Popov protected: 104219e5367SNikita Popov void expectKnownBits(uint64_t Zero, uint64_t One) { 105219e5367SNikita Popov auto Known = computeKnownBits(A, M->getDataLayout()); 106219e5367SNikita Popov ASSERT_FALSE(Known.hasConflict()); 107219e5367SNikita Popov EXPECT_EQ(Known.One.getZExtValue(), One); 108219e5367SNikita Popov EXPECT_EQ(Known.Zero.getZExtValue(), Zero); 109219e5367SNikita Popov } 110134bec27SJames Molloy }; 111134bec27SJames Molloy 112134bec27SJames Molloy } 113134bec27SJames Molloy 114134bec27SJames Molloy TEST_F(MatchSelectPatternTest, SimpleFMin) { 115134bec27SJames Molloy parseAssembly( 116134bec27SJames Molloy "define float @test(float %a) {\n" 117134bec27SJames Molloy " %1 = fcmp ult float %a, 5.0\n" 118134bec27SJames Molloy " %A = select i1 %1, float %a, float 5.0\n" 119134bec27SJames Molloy " ret float %A\n" 120134bec27SJames Molloy "}\n"); 121134bec27SJames Molloy expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false}); 122134bec27SJames Molloy } 123134bec27SJames Molloy 124134bec27SJames Molloy TEST_F(MatchSelectPatternTest, SimpleFMax) { 125134bec27SJames Molloy parseAssembly( 126134bec27SJames Molloy "define float @test(float %a) {\n" 127134bec27SJames Molloy " %1 = fcmp ogt float %a, 5.0\n" 128134bec27SJames Molloy " %A = select i1 %1, float %a, float 5.0\n" 129134bec27SJames Molloy " ret float %A\n" 130134bec27SJames Molloy "}\n"); 131134bec27SJames Molloy expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true}); 132134bec27SJames Molloy } 133134bec27SJames Molloy 134134bec27SJames Molloy TEST_F(MatchSelectPatternTest, SwappedFMax) { 135134bec27SJames Molloy parseAssembly( 136134bec27SJames Molloy "define float @test(float %a) {\n" 137134bec27SJames Molloy " %1 = fcmp olt float 5.0, %a\n" 138134bec27SJames Molloy " %A = select i1 %1, float %a, float 5.0\n" 139134bec27SJames Molloy " ret float %A\n" 140134bec27SJames Molloy "}\n"); 141134bec27SJames Molloy expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, false}); 142134bec27SJames Molloy } 143134bec27SJames Molloy 144134bec27SJames Molloy TEST_F(MatchSelectPatternTest, SwappedFMax2) { 145134bec27SJames Molloy parseAssembly( 146134bec27SJames Molloy "define float @test(float %a) {\n" 147134bec27SJames Molloy " %1 = fcmp olt float %a, 5.0\n" 148134bec27SJames Molloy " %A = select i1 %1, float 5.0, float %a\n" 149134bec27SJames Molloy " ret float %A\n" 150134bec27SJames Molloy "}\n"); 151134bec27SJames Molloy expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, false}); 152134bec27SJames Molloy } 153134bec27SJames Molloy 154134bec27SJames Molloy TEST_F(MatchSelectPatternTest, SwappedFMax3) { 155134bec27SJames Molloy parseAssembly( 156134bec27SJames Molloy "define float @test(float %a) {\n" 157134bec27SJames Molloy " %1 = fcmp ult float %a, 5.0\n" 158134bec27SJames Molloy " %A = select i1 %1, float 5.0, float %a\n" 159134bec27SJames Molloy " ret float %A\n" 160134bec27SJames Molloy "}\n"); 161134bec27SJames Molloy expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true}); 162134bec27SJames Molloy } 163134bec27SJames Molloy 164134bec27SJames Molloy TEST_F(MatchSelectPatternTest, FastFMin) { 165134bec27SJames Molloy parseAssembly( 166134bec27SJames Molloy "define float @test(float %a) {\n" 167134bec27SJames Molloy " %1 = fcmp nnan olt float %a, 5.0\n" 168134bec27SJames Molloy " %A = select i1 %1, float %a, float 5.0\n" 169134bec27SJames Molloy " ret float %A\n" 170134bec27SJames Molloy "}\n"); 171134bec27SJames Molloy expectPattern({SPF_FMINNUM, SPNB_RETURNS_ANY, false}); 172134bec27SJames Molloy } 173134bec27SJames Molloy 174134bec27SJames Molloy TEST_F(MatchSelectPatternTest, FMinConstantZero) { 175134bec27SJames Molloy parseAssembly( 176134bec27SJames Molloy "define float @test(float %a) {\n" 177134bec27SJames Molloy " %1 = fcmp ole float %a, 0.0\n" 178134bec27SJames Molloy " %A = select i1 %1, float %a, float 0.0\n" 179134bec27SJames Molloy " ret float %A\n" 180134bec27SJames Molloy "}\n"); 181134bec27SJames Molloy // This shouldn't be matched, as %a could be -0.0. 182134bec27SJames Molloy expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 183134bec27SJames Molloy } 184134bec27SJames Molloy 185134bec27SJames Molloy TEST_F(MatchSelectPatternTest, FMinConstantZeroNsz) { 186134bec27SJames Molloy parseAssembly( 187134bec27SJames Molloy "define float @test(float %a) {\n" 188134bec27SJames Molloy " %1 = fcmp nsz ole float %a, 0.0\n" 189134bec27SJames Molloy " %A = select i1 %1, float %a, float 0.0\n" 190134bec27SJames Molloy " ret float %A\n" 191134bec27SJames Molloy "}\n"); 192134bec27SJames Molloy // But this should be, because we've ignored signed zeroes. 193134bec27SJames Molloy expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true}); 194134bec27SJames Molloy } 195569cea65SJames Molloy 19603da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero1) { 19703da6e6aSSanjay Patel parseAssembly( 19803da6e6aSSanjay Patel "define float @test(float %a) {\n" 19903da6e6aSSanjay Patel " %1 = fcmp olt float -0.0, %a\n" 20003da6e6aSSanjay Patel " %A = select i1 %1, float 0.0, float %a\n" 20103da6e6aSSanjay Patel " ret float %A\n" 20203da6e6aSSanjay Patel "}\n"); 203e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 204e7c94ef1SSanjay Patel expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, true}); 20503da6e6aSSanjay Patel } 20603da6e6aSSanjay Patel 20703da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero2) { 20803da6e6aSSanjay Patel parseAssembly( 20903da6e6aSSanjay Patel "define float @test(float %a) {\n" 21003da6e6aSSanjay Patel " %1 = fcmp ogt float %a, -0.0\n" 21103da6e6aSSanjay Patel " %A = select i1 %1, float 0.0, float %a\n" 21203da6e6aSSanjay Patel " ret float %A\n" 21303da6e6aSSanjay Patel "}\n"); 214e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 215e7c94ef1SSanjay Patel expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false}); 21603da6e6aSSanjay Patel } 21703da6e6aSSanjay Patel 21803da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero3) { 21903da6e6aSSanjay Patel parseAssembly( 22003da6e6aSSanjay Patel "define float @test(float %a) {\n" 22103da6e6aSSanjay Patel " %1 = fcmp olt float 0.0, %a\n" 22203da6e6aSSanjay Patel " %A = select i1 %1, float -0.0, float %a\n" 22303da6e6aSSanjay Patel " ret float %A\n" 22403da6e6aSSanjay Patel "}\n"); 225e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 226e7c94ef1SSanjay Patel expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, true}); 22703da6e6aSSanjay Patel } 22803da6e6aSSanjay Patel 22903da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero4) { 23003da6e6aSSanjay Patel parseAssembly( 23103da6e6aSSanjay Patel "define float @test(float %a) {\n" 23203da6e6aSSanjay Patel " %1 = fcmp ogt float %a, 0.0\n" 23303da6e6aSSanjay Patel " %A = select i1 %1, float -0.0, float %a\n" 23403da6e6aSSanjay Patel " ret float %A\n" 23503da6e6aSSanjay Patel "}\n"); 236e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 237e7c94ef1SSanjay Patel expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false}); 23803da6e6aSSanjay Patel } 23903da6e6aSSanjay Patel 24003da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero5) { 24103da6e6aSSanjay Patel parseAssembly( 24203da6e6aSSanjay Patel "define float @test(float %a) {\n" 24303da6e6aSSanjay Patel " %1 = fcmp ogt float -0.0, %a\n" 24403da6e6aSSanjay Patel " %A = select i1 %1, float %a, float 0.0\n" 24503da6e6aSSanjay Patel " ret float %A\n" 24603da6e6aSSanjay Patel "}\n"); 247e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 248e7c94ef1SSanjay Patel expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, false}); 24903da6e6aSSanjay Patel } 25003da6e6aSSanjay Patel 25103da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero6) { 25203da6e6aSSanjay Patel parseAssembly( 25303da6e6aSSanjay Patel "define float @test(float %a) {\n" 25403da6e6aSSanjay Patel " %1 = fcmp olt float %a, -0.0\n" 25503da6e6aSSanjay Patel " %A = select i1 %1, float %a, float 0.0\n" 25603da6e6aSSanjay Patel " ret float %A\n" 25703da6e6aSSanjay Patel "}\n"); 258e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 259e7c94ef1SSanjay Patel expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true}); 26003da6e6aSSanjay Patel } 26103da6e6aSSanjay Patel 26203da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero7) { 26303da6e6aSSanjay Patel parseAssembly( 26403da6e6aSSanjay Patel "define float @test(float %a) {\n" 26503da6e6aSSanjay Patel " %1 = fcmp ogt float 0.0, %a\n" 26603da6e6aSSanjay Patel " %A = select i1 %1, float %a, float -0.0\n" 26703da6e6aSSanjay Patel " ret float %A\n" 26803da6e6aSSanjay Patel "}\n"); 269e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 270e7c94ef1SSanjay Patel expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, false}); 27103da6e6aSSanjay Patel } 27203da6e6aSSanjay Patel 27303da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero8) { 27403da6e6aSSanjay Patel parseAssembly( 27503da6e6aSSanjay Patel "define float @test(float %a) {\n" 27603da6e6aSSanjay Patel " %1 = fcmp olt float %a, 0.0\n" 27703da6e6aSSanjay Patel " %A = select i1 %1, float %a, float -0.0\n" 27803da6e6aSSanjay Patel " ret float %A\n" 27903da6e6aSSanjay Patel "}\n"); 280e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 281e7c94ef1SSanjay Patel expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true}); 28203da6e6aSSanjay Patel } 28303da6e6aSSanjay Patel 28403da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero1) { 28503da6e6aSSanjay Patel parseAssembly( 28603da6e6aSSanjay Patel "define float @test(float %a) {\n" 28703da6e6aSSanjay Patel " %1 = fcmp ogt float -0.0, %a\n" 28803da6e6aSSanjay Patel " %A = select i1 %1, float 0.0, float %a\n" 28903da6e6aSSanjay Patel " ret float %A\n" 29003da6e6aSSanjay Patel "}\n"); 291e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 292e7c94ef1SSanjay Patel expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, true}); 29303da6e6aSSanjay Patel } 29403da6e6aSSanjay Patel 29503da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero2) { 29603da6e6aSSanjay Patel parseAssembly( 29703da6e6aSSanjay Patel "define float @test(float %a) {\n" 29803da6e6aSSanjay Patel " %1 = fcmp olt float %a, -0.0\n" 29903da6e6aSSanjay Patel " %A = select i1 %1, float 0.0, float %a\n" 30003da6e6aSSanjay Patel " ret float %A\n" 30103da6e6aSSanjay Patel "}\n"); 302e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 303e7c94ef1SSanjay Patel expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, false}); 30403da6e6aSSanjay Patel } 30503da6e6aSSanjay Patel 30603da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero3) { 30703da6e6aSSanjay Patel parseAssembly( 30803da6e6aSSanjay Patel "define float @test(float %a) {\n" 30903da6e6aSSanjay Patel " %1 = fcmp ogt float 0.0, %a\n" 31003da6e6aSSanjay Patel " %A = select i1 %1, float -0.0, float %a\n" 31103da6e6aSSanjay Patel " ret float %A\n" 31203da6e6aSSanjay Patel "}\n"); 313e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 314e7c94ef1SSanjay Patel expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, true}); 31503da6e6aSSanjay Patel } 31603da6e6aSSanjay Patel 31703da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero4) { 31803da6e6aSSanjay Patel parseAssembly( 31903da6e6aSSanjay Patel "define float @test(float %a) {\n" 32003da6e6aSSanjay Patel " %1 = fcmp olt float %a, 0.0\n" 32103da6e6aSSanjay Patel " %A = select i1 %1, float -0.0, float %a\n" 32203da6e6aSSanjay Patel " ret float %A\n" 32303da6e6aSSanjay Patel "}\n"); 324e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 325e7c94ef1SSanjay Patel expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, false}); 32603da6e6aSSanjay Patel } 32703da6e6aSSanjay Patel 32803da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero5) { 32903da6e6aSSanjay Patel parseAssembly( 33003da6e6aSSanjay Patel "define float @test(float %a) {\n" 33103da6e6aSSanjay Patel " %1 = fcmp olt float -0.0, %a\n" 33203da6e6aSSanjay Patel " %A = select i1 %1, float %a, float 0.0\n" 33303da6e6aSSanjay Patel " ret float %A\n" 33403da6e6aSSanjay Patel "}\n"); 335e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 336e7c94ef1SSanjay Patel expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, false}); 33703da6e6aSSanjay Patel } 33803da6e6aSSanjay Patel 33903da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero6) { 34003da6e6aSSanjay Patel parseAssembly( 34103da6e6aSSanjay Patel "define float @test(float %a) {\n" 34203da6e6aSSanjay Patel " %1 = fcmp ogt float %a, -0.0\n" 34303da6e6aSSanjay Patel " %A = select i1 %1, float %a, float 0.0\n" 34403da6e6aSSanjay Patel " ret float %A\n" 34503da6e6aSSanjay Patel "}\n"); 346e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 347e7c94ef1SSanjay Patel expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true}); 34803da6e6aSSanjay Patel } 34903da6e6aSSanjay Patel 35003da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero7) { 35103da6e6aSSanjay Patel parseAssembly( 35203da6e6aSSanjay Patel "define float @test(float %a) {\n" 35303da6e6aSSanjay Patel " %1 = fcmp olt float 0.0, %a\n" 35403da6e6aSSanjay Patel " %A = select i1 %1, float %a, float -0.0\n" 35503da6e6aSSanjay Patel " ret float %A\n" 35603da6e6aSSanjay Patel "}\n"); 357e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 358e7c94ef1SSanjay Patel expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, false}); 35903da6e6aSSanjay Patel } 36003da6e6aSSanjay Patel 36103da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero8) { 36203da6e6aSSanjay Patel parseAssembly( 36303da6e6aSSanjay Patel "define float @test(float %a) {\n" 36403da6e6aSSanjay Patel " %1 = fcmp ogt float %a, 0.0\n" 36503da6e6aSSanjay Patel " %A = select i1 %1, float %a, float -0.0\n" 36603da6e6aSSanjay Patel " ret float %A\n" 36703da6e6aSSanjay Patel "}\n"); 368e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 369e7c94ef1SSanjay Patel expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true}); 370e7c94ef1SSanjay Patel } 371e7c94ef1SSanjay Patel 372e7c94ef1SSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZeroVecUndef) { 373e7c94ef1SSanjay Patel parseAssembly( 374e7c94ef1SSanjay Patel "define <2 x float> @test(<2 x float> %a) {\n" 375e7c94ef1SSanjay Patel " %1 = fcmp ogt <2 x float> %a, <float -0.0, float -0.0>\n" 376e7c94ef1SSanjay Patel " %A = select <2 x i1> %1, <2 x float> <float undef, float 0.0>, <2 x float> %a\n" 377e7c94ef1SSanjay Patel " ret <2 x float> %A\n" 378e7c94ef1SSanjay Patel "}\n"); 379e7c94ef1SSanjay Patel // An undef in a vector constant can not be back-propagated for this analysis. 380e7c94ef1SSanjay Patel expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 381e7c94ef1SSanjay Patel } 382e7c94ef1SSanjay Patel 383e7c94ef1SSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZeroVecUndef) { 384e7c94ef1SSanjay Patel parseAssembly( 385e7c94ef1SSanjay Patel "define <2 x float> @test(<2 x float> %a) {\n" 386e7c94ef1SSanjay Patel " %1 = fcmp ogt <2 x float> %a, zeroinitializer\n" 387e7c94ef1SSanjay Patel " %A = select <2 x i1> %1, <2 x float> %a, <2 x float> <float -0.0, float undef>\n" 388e7c94ef1SSanjay Patel " ret <2 x float> %A\n" 389e7c94ef1SSanjay Patel "}\n"); 390e7c94ef1SSanjay Patel // An undef in a vector constant can not be back-propagated for this analysis. 39103da6e6aSSanjay Patel expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 39203da6e6aSSanjay Patel } 39303da6e6aSSanjay Patel 39430f1d691SThomas Lively TEST_F(MatchSelectPatternTest, VectorFMinimum) { 395d47b5c7bSThomas Lively parseAssembly( 396d47b5c7bSThomas Lively "define <4 x float> @test(<4 x float> %a) {\n" 397d47b5c7bSThomas Lively " %1 = fcmp ule <4 x float> %a, \n" 398d47b5c7bSThomas Lively " <float 5.0, float 5.0, float 5.0, float 5.0>\n" 399d47b5c7bSThomas Lively " %A = select <4 x i1> %1, <4 x float> %a,\n" 400d47b5c7bSThomas Lively " <4 x float> <float 5.0, float 5.0, float 5.0, float 5.0>\n" 401d47b5c7bSThomas Lively " ret <4 x float> %A\n" 402d47b5c7bSThomas Lively "}\n"); 403d47b5c7bSThomas Lively // Check that pattern matching works on vectors where each lane has the same 404d47b5c7bSThomas Lively // unordered pattern. 405d47b5c7bSThomas Lively expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false}); 406d47b5c7bSThomas Lively } 407d47b5c7bSThomas Lively 408d47b5c7bSThomas Lively TEST_F(MatchSelectPatternTest, VectorFMinOtherOrdered) { 409d47b5c7bSThomas Lively parseAssembly( 410d47b5c7bSThomas Lively "define <4 x float> @test(<4 x float> %a) {\n" 411d47b5c7bSThomas Lively " %1 = fcmp ole <4 x float> %a, \n" 412d47b5c7bSThomas Lively " <float 5.0, float 5.0, float 5.0, float 5.0>\n" 413d47b5c7bSThomas Lively " %A = select <4 x i1> %1, <4 x float> %a,\n" 414d47b5c7bSThomas Lively " <4 x float> <float 5.0, float 5.0, float 5.0, float 5.0>\n" 415d47b5c7bSThomas Lively " ret <4 x float> %A\n" 416d47b5c7bSThomas Lively "}\n"); 417d47b5c7bSThomas Lively // Check that pattern matching works on vectors where each lane has the same 418d47b5c7bSThomas Lively // ordered pattern. 419d47b5c7bSThomas Lively expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true}); 420d47b5c7bSThomas Lively } 421d47b5c7bSThomas Lively 42230f1d691SThomas Lively TEST_F(MatchSelectPatternTest, VectorNotFMinimum) { 423d47b5c7bSThomas Lively parseAssembly( 424d47b5c7bSThomas Lively "define <4 x float> @test(<4 x float> %a) {\n" 425d47b5c7bSThomas Lively " %1 = fcmp ule <4 x float> %a, \n" 426d47b5c7bSThomas Lively " <float 5.0, float 0x7ff8000000000000, float 5.0, float 5.0>\n" 427d47b5c7bSThomas Lively " %A = select <4 x i1> %1, <4 x float> %a,\n" 428d47b5c7bSThomas Lively " <4 x float> <float 5.0, float 0x7ff8000000000000, float 5.0, float " 429d47b5c7bSThomas Lively "5.0>\n" 430d47b5c7bSThomas Lively " ret <4 x float> %A\n" 431d47b5c7bSThomas Lively "}\n"); 432d47b5c7bSThomas Lively // The lane that contains a NaN (0x7ff80...) behaves like a 433d47b5c7bSThomas Lively // non-NaN-propagating min and the other lines behave like a NaN-propagating 434d47b5c7bSThomas Lively // min, so check that neither is returned. 435d47b5c7bSThomas Lively expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 436d47b5c7bSThomas Lively } 437d47b5c7bSThomas Lively 438d47b5c7bSThomas Lively TEST_F(MatchSelectPatternTest, VectorNotFMinZero) { 439d47b5c7bSThomas Lively parseAssembly( 440d47b5c7bSThomas Lively "define <4 x float> @test(<4 x float> %a) {\n" 441d47b5c7bSThomas Lively " %1 = fcmp ule <4 x float> %a, \n" 442d47b5c7bSThomas Lively " <float 5.0, float -0.0, float 5.0, float 5.0>\n" 443d47b5c7bSThomas Lively " %A = select <4 x i1> %1, <4 x float> %a,\n" 444d47b5c7bSThomas Lively " <4 x float> <float 5.0, float 0.0, float 5.0, float 5.0>\n" 445d47b5c7bSThomas Lively " ret <4 x float> %A\n" 446d47b5c7bSThomas Lively "}\n"); 447d47b5c7bSThomas Lively // Always selects the second lane of %a if it is positive or negative zero, so 448d47b5c7bSThomas Lively // this is stricter than a min. 449d47b5c7bSThomas Lively expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 450d47b5c7bSThomas Lively } 451d47b5c7bSThomas Lively 452569cea65SJames Molloy TEST_F(MatchSelectPatternTest, DoubleCastU) { 453569cea65SJames Molloy parseAssembly( 454569cea65SJames Molloy "define i32 @test(i8 %a, i8 %b) {\n" 455569cea65SJames Molloy " %1 = icmp ult i8 %a, %b\n" 456569cea65SJames Molloy " %2 = zext i8 %a to i32\n" 457569cea65SJames Molloy " %3 = zext i8 %b to i32\n" 458569cea65SJames Molloy " %A = select i1 %1, i32 %2, i32 %3\n" 459569cea65SJames Molloy " ret i32 %A\n" 460569cea65SJames Molloy "}\n"); 461569cea65SJames Molloy // We should be able to look through the situation where we cast both operands 462569cea65SJames Molloy // to the select. 463569cea65SJames Molloy expectPattern({SPF_UMIN, SPNB_NA, false}); 464569cea65SJames Molloy } 465569cea65SJames Molloy 466569cea65SJames Molloy TEST_F(MatchSelectPatternTest, DoubleCastS) { 467569cea65SJames Molloy parseAssembly( 468569cea65SJames Molloy "define i32 @test(i8 %a, i8 %b) {\n" 469569cea65SJames Molloy " %1 = icmp slt i8 %a, %b\n" 470569cea65SJames Molloy " %2 = sext i8 %a to i32\n" 471569cea65SJames Molloy " %3 = sext i8 %b to i32\n" 472569cea65SJames Molloy " %A = select i1 %1, i32 %2, i32 %3\n" 473569cea65SJames Molloy " ret i32 %A\n" 474569cea65SJames Molloy "}\n"); 475569cea65SJames Molloy // We should be able to look through the situation where we cast both operands 476569cea65SJames Molloy // to the select. 477569cea65SJames Molloy expectPattern({SPF_SMIN, SPNB_NA, false}); 478569cea65SJames Molloy } 479569cea65SJames Molloy 480569cea65SJames Molloy TEST_F(MatchSelectPatternTest, DoubleCastBad) { 481569cea65SJames Molloy parseAssembly( 482569cea65SJames Molloy "define i32 @test(i8 %a, i8 %b) {\n" 483569cea65SJames Molloy " %1 = icmp ult i8 %a, %b\n" 484569cea65SJames Molloy " %2 = zext i8 %a to i32\n" 485569cea65SJames Molloy " %3 = sext i8 %b to i32\n" 486569cea65SJames Molloy " %A = select i1 %1, i32 %2, i32 %3\n" 487569cea65SJames Molloy " ret i32 %A\n" 488569cea65SJames Molloy "}\n"); 489687a8448SJames Molloy // The cast types here aren't the same, so we cannot match an UMIN. 490569cea65SJames Molloy expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 491569cea65SJames Molloy } 4923bb2dbd6SSanjoy Das 4936d343740SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotSMin) { 4946d343740SSanjay Patel parseAssembly( 4956d343740SSanjay Patel "define i8 @test(i8 %a, i8 %b) {\n" 4966d343740SSanjay Patel " %cmp = icmp sgt i8 %a, %b\n" 4976d343740SSanjay Patel " %an = xor i8 %a, -1\n" 4986d343740SSanjay Patel " %bn = xor i8 %b, -1\n" 4996d343740SSanjay Patel " %A = select i1 %cmp, i8 %an, i8 %bn\n" 5006d343740SSanjay Patel " ret i8 %A\n" 5016d343740SSanjay Patel "}\n"); 502867f0c3cSSanjay Patel expectPattern({SPF_SMIN, SPNB_NA, false}); 5036d343740SSanjay Patel } 5046d343740SSanjay Patel 5056d343740SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotSMinSwap) { 5066d343740SSanjay Patel parseAssembly( 507463143f0SSanjay Patel "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n" 508463143f0SSanjay Patel " %cmp = icmp slt <2 x i8> %a, %b\n" 509463143f0SSanjay Patel " %an = xor <2 x i8> %a, <i8 -1, i8-1>\n" 510463143f0SSanjay Patel " %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n" 511463143f0SSanjay Patel " %A = select <2 x i1> %cmp, <2 x i8> %bn, <2 x i8> %an\n" 512463143f0SSanjay Patel " ret <2 x i8> %A\n" 5136d343740SSanjay Patel "}\n"); 514867f0c3cSSanjay Patel expectPattern({SPF_SMIN, SPNB_NA, false}); 5156d343740SSanjay Patel } 5166d343740SSanjay Patel 5176d343740SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotSMax) { 5186d343740SSanjay Patel parseAssembly( 5196d343740SSanjay Patel "define i8 @test(i8 %a, i8 %b) {\n" 5206d343740SSanjay Patel " %cmp = icmp slt i8 %a, %b\n" 5216d343740SSanjay Patel " %an = xor i8 %a, -1\n" 5226d343740SSanjay Patel " %bn = xor i8 %b, -1\n" 5236d343740SSanjay Patel " %A = select i1 %cmp, i8 %an, i8 %bn\n" 5246d343740SSanjay Patel " ret i8 %A\n" 5256d343740SSanjay Patel "}\n"); 526867f0c3cSSanjay Patel expectPattern({SPF_SMAX, SPNB_NA, false}); 5276d343740SSanjay Patel } 5286d343740SSanjay Patel 5296d343740SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotSMaxSwap) { 5306d343740SSanjay Patel parseAssembly( 531463143f0SSanjay Patel "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n" 532463143f0SSanjay Patel " %cmp = icmp sgt <2 x i8> %a, %b\n" 533463143f0SSanjay Patel " %an = xor <2 x i8> %a, <i8 -1, i8-1>\n" 534463143f0SSanjay Patel " %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n" 535463143f0SSanjay Patel " %A = select <2 x i1> %cmp, <2 x i8> %bn, <2 x i8> %an\n" 536463143f0SSanjay Patel " ret <2 x i8> %A\n" 537463143f0SSanjay Patel "}\n"); 538463143f0SSanjay Patel expectPattern({SPF_SMAX, SPNB_NA, false}); 539463143f0SSanjay Patel } 540463143f0SSanjay Patel 541463143f0SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotUMin) { 542463143f0SSanjay Patel parseAssembly( 543463143f0SSanjay Patel "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n" 544463143f0SSanjay Patel " %cmp = icmp ugt <2 x i8> %a, %b\n" 545463143f0SSanjay Patel " %an = xor <2 x i8> %a, <i8 -1, i8-1>\n" 546463143f0SSanjay Patel " %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n" 547463143f0SSanjay Patel " %A = select <2 x i1> %cmp, <2 x i8> %an, <2 x i8> %bn\n" 548463143f0SSanjay Patel " ret <2 x i8> %A\n" 549463143f0SSanjay Patel "}\n"); 550fbb1b43fSSanjay Patel expectPattern({SPF_UMIN, SPNB_NA, false}); 551463143f0SSanjay Patel } 552463143f0SSanjay Patel 553463143f0SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotUMinSwap) { 554463143f0SSanjay Patel parseAssembly( 5556d343740SSanjay Patel "define i8 @test(i8 %a, i8 %b) {\n" 556463143f0SSanjay Patel " %cmp = icmp ult i8 %a, %b\n" 5576d343740SSanjay Patel " %an = xor i8 %a, -1\n" 5586d343740SSanjay Patel " %bn = xor i8 %b, -1\n" 5596d343740SSanjay Patel " %A = select i1 %cmp, i8 %bn, i8 %an\n" 5606d343740SSanjay Patel " ret i8 %A\n" 5616d343740SSanjay Patel "}\n"); 562fbb1b43fSSanjay Patel expectPattern({SPF_UMIN, SPNB_NA, false}); 563463143f0SSanjay Patel } 564463143f0SSanjay Patel 565463143f0SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotUMax) { 566463143f0SSanjay Patel parseAssembly( 567463143f0SSanjay Patel "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n" 568463143f0SSanjay Patel " %cmp = icmp ult <2 x i8> %a, %b\n" 569463143f0SSanjay Patel " %an = xor <2 x i8> %a, <i8 -1, i8-1>\n" 570463143f0SSanjay Patel " %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n" 571463143f0SSanjay Patel " %A = select <2 x i1> %cmp, <2 x i8> %an, <2 x i8> %bn\n" 572463143f0SSanjay Patel " ret <2 x i8> %A\n" 573463143f0SSanjay Patel "}\n"); 574fbb1b43fSSanjay Patel expectPattern({SPF_UMAX, SPNB_NA, false}); 575463143f0SSanjay Patel } 576463143f0SSanjay Patel 577463143f0SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotUMaxSwap) { 578463143f0SSanjay Patel parseAssembly( 579463143f0SSanjay Patel "define i8 @test(i8 %a, i8 %b) {\n" 580463143f0SSanjay Patel " %cmp = icmp ugt i8 %a, %b\n" 581463143f0SSanjay Patel " %an = xor i8 %a, -1\n" 582463143f0SSanjay Patel " %bn = xor i8 %b, -1\n" 583463143f0SSanjay Patel " %A = select i1 %cmp, i8 %bn, i8 %an\n" 584463143f0SSanjay Patel " ret i8 %A\n" 585463143f0SSanjay Patel "}\n"); 586fbb1b43fSSanjay Patel expectPattern({SPF_UMAX, SPNB_NA, false}); 587463143f0SSanjay Patel } 588463143f0SSanjay Patel 589463143f0SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotEq) { 590463143f0SSanjay Patel parseAssembly( 591463143f0SSanjay Patel "define i8 @test(i8 %a, i8 %b) {\n" 592463143f0SSanjay Patel " %cmp = icmp eq i8 %a, %b\n" 593463143f0SSanjay Patel " %an = xor i8 %a, -1\n" 594463143f0SSanjay Patel " %bn = xor i8 %b, -1\n" 595463143f0SSanjay Patel " %A = select i1 %cmp, i8 %bn, i8 %an\n" 596463143f0SSanjay Patel " ret i8 %A\n" 597463143f0SSanjay Patel "}\n"); 598463143f0SSanjay Patel expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 599463143f0SSanjay Patel } 600463143f0SSanjay Patel 601463143f0SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotNe) { 602463143f0SSanjay Patel parseAssembly( 603463143f0SSanjay Patel "define i8 @test(i8 %a, i8 %b) {\n" 604463143f0SSanjay Patel " %cmp = icmp ne i8 %a, %b\n" 605463143f0SSanjay Patel " %an = xor i8 %a, -1\n" 606463143f0SSanjay Patel " %bn = xor i8 %b, -1\n" 607463143f0SSanjay Patel " %A = select i1 %cmp, i8 %bn, i8 %an\n" 608463143f0SSanjay Patel " ret i8 %A\n" 609463143f0SSanjay Patel "}\n"); 610463143f0SSanjay Patel expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 6116d343740SSanjay Patel } 6126d343740SSanjay Patel 6133bb2dbd6SSanjoy Das TEST(ValueTracking, GuaranteedToTransferExecutionToSuccessor) { 6143bb2dbd6SSanjoy Das StringRef Assembly = 6153bb2dbd6SSanjoy Das "declare void @nounwind_readonly(i32*) nounwind readonly " 6163bb2dbd6SSanjoy Das "declare void @nounwind_argmemonly(i32*) nounwind argmemonly " 6175d12b976SNikita Popov "declare void @nounwind_willreturn(i32*) nounwind willreturn " 6183bb2dbd6SSanjoy Das "declare void @throws_but_readonly(i32*) readonly " 6193bb2dbd6SSanjoy Das "declare void @throws_but_argmemonly(i32*) argmemonly " 6205d12b976SNikita Popov "declare void @throws_but_willreturn(i32*) willreturn " 6213bb2dbd6SSanjoy Das " " 6223bb2dbd6SSanjoy Das "declare void @unknown(i32*) " 6233bb2dbd6SSanjoy Das " " 6243bb2dbd6SSanjoy Das "define void @f(i32* %p) { " 6253bb2dbd6SSanjoy Das " call void @nounwind_readonly(i32* %p) " 6263bb2dbd6SSanjoy Das " call void @nounwind_argmemonly(i32* %p) " 6275d12b976SNikita Popov " call void @nounwind_willreturn(i32* %p)" 6283bb2dbd6SSanjoy Das " call void @throws_but_readonly(i32* %p) " 6293bb2dbd6SSanjoy Das " call void @throws_but_argmemonly(i32* %p) " 6305d12b976SNikita Popov " call void @throws_but_willreturn(i32* %p) " 6313bb2dbd6SSanjoy Das " call void @unknown(i32* %p) nounwind readonly " 6323bb2dbd6SSanjoy Das " call void @unknown(i32* %p) nounwind argmemonly " 6335d12b976SNikita Popov " call void @unknown(i32* %p) nounwind willreturn " 6343bb2dbd6SSanjoy Das " call void @unknown(i32* %p) readonly " 6353bb2dbd6SSanjoy Das " call void @unknown(i32* %p) argmemonly " 6365d12b976SNikita Popov " call void @unknown(i32* %p) willreturn " 6373bb2dbd6SSanjoy Das " ret void " 6383bb2dbd6SSanjoy Das "} "; 6393bb2dbd6SSanjoy Das 6403bb2dbd6SSanjoy Das LLVMContext Context; 6413bb2dbd6SSanjoy Das SMDiagnostic Error; 6423bb2dbd6SSanjoy Das auto M = parseAssemblyString(Assembly, Error, Context); 6433bb2dbd6SSanjoy Das assert(M && "Bad assembly?"); 6443bb2dbd6SSanjoy Das 6453bb2dbd6SSanjoy Das auto *F = M->getFunction("f"); 6463bb2dbd6SSanjoy Das assert(F && "Bad assembly?"); 6473bb2dbd6SSanjoy Das 6483bb2dbd6SSanjoy Das auto &BB = F->getEntryBlock(); 6498580cd4eSCraig Topper bool ExpectedAnswers[] = { 6505d12b976SNikita Popov false, // call void @nounwind_readonly(i32* %p) 6515d12b976SNikita Popov false, // call void @nounwind_argmemonly(i32* %p) 6525d12b976SNikita Popov true, // call void @nounwind_willreturn(i32* %p) 6533bb2dbd6SSanjoy Das false, // call void @throws_but_readonly(i32* %p) 6543bb2dbd6SSanjoy Das false, // call void @throws_but_argmemonly(i32* %p) 6555d12b976SNikita Popov false, // call void @throws_but_willreturn(i32* %p) 6565d12b976SNikita Popov false, // call void @unknown(i32* %p) nounwind readonly 6575d12b976SNikita Popov false, // call void @unknown(i32* %p) nounwind argmemonly 6585d12b976SNikita Popov true, // call void @unknown(i32* %p) nounwind willreturn 6593bb2dbd6SSanjoy Das false, // call void @unknown(i32* %p) readonly 6603bb2dbd6SSanjoy Das false, // call void @unknown(i32* %p) argmemonly 6615d12b976SNikita Popov false, // call void @unknown(i32* %p) willreturn 6623bb2dbd6SSanjoy Das false, // ret void 6633bb2dbd6SSanjoy Das }; 6643bb2dbd6SSanjoy Das 6653bb2dbd6SSanjoy Das int Index = 0; 6663bb2dbd6SSanjoy Das for (auto &I : BB) { 6673bb2dbd6SSanjoy Das EXPECT_EQ(isGuaranteedToTransferExecutionToSuccessor(&I), 6683bb2dbd6SSanjoy Das ExpectedAnswers[Index]) 6693bb2dbd6SSanjoy Das << "Incorrect answer at instruction " << Index << " = " << I; 6703bb2dbd6SSanjoy Das Index++; 6713bb2dbd6SSanjoy Das } 6723bb2dbd6SSanjoy Das } 67339a684d1SSanjoy Das 674219e5367SNikita Popov TEST_F(ValueTrackingTest, ComputeNumSignBits_PR32045) { 675219e5367SNikita Popov parseAssembly( 676219e5367SNikita Popov "define i32 @test(i32 %a) {\n" 677219e5367SNikita Popov " %A = ashr i32 %a, -1\n" 678219e5367SNikita Popov " ret i32 %A\n" 679219e5367SNikita Popov "}\n"); 680219e5367SNikita Popov EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u); 68139a684d1SSanjoy Das } 68270dbd5fbSSimon Dardis 683a68096c7SSanjay Patel // No guarantees for canonical IR in this analysis, so this just bails out. 684219e5367SNikita Popov TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle) { 685219e5367SNikita Popov parseAssembly( 686219e5367SNikita Popov "define <2 x i32> @test() {\n" 687219e5367SNikita Popov " %A = shufflevector <2 x i32> undef, <2 x i32> undef, <2 x i32> <i32 0, i32 0>\n" 688219e5367SNikita Popov " ret <2 x i32> %A\n" 689219e5367SNikita Popov "}\n"); 690219e5367SNikita Popov EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u); 691a68096c7SSanjay Patel } 692a68096c7SSanjay Patel 69345a148deSSanjay Patel // No guarantees for canonical IR in this analysis, so a shuffle element that 69445a148deSSanjay Patel // references an undef value means this can't return any extra information. 695219e5367SNikita Popov TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle2) { 696219e5367SNikita Popov parseAssembly( 697219e5367SNikita Popov "define <2 x i32> @test(<2 x i1> %x) {\n" 698219e5367SNikita Popov " %sext = sext <2 x i1> %x to <2 x i32>\n" 699219e5367SNikita Popov " %A = shufflevector <2 x i32> %sext, <2 x i32> undef, <2 x i32> <i32 0, i32 2>\n" 700219e5367SNikita Popov " ret <2 x i32> %A\n" 701219e5367SNikita Popov "}\n"); 702219e5367SNikita Popov EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u); 703994543abSJuneyoung Lee } 704994543abSJuneyoung Lee 7050f2c1801SJuneyoung Lee TEST_F(ValueTrackingTest, impliesPoisonTest_Identity) { 7060f2c1801SJuneyoung Lee parseAssembly("define void @test(i32 %x, i32 %y) {\n" 7070f2c1801SJuneyoung Lee " %A = add i32 %x, %y\n" 7080f2c1801SJuneyoung Lee " ret void\n" 7090f2c1801SJuneyoung Lee "}"); 7100f2c1801SJuneyoung Lee EXPECT_TRUE(impliesPoison(A, A)); 7110f2c1801SJuneyoung Lee } 7120f2c1801SJuneyoung Lee 7130f2c1801SJuneyoung Lee TEST_F(ValueTrackingTest, impliesPoisonTest_ICmp) { 7140f2c1801SJuneyoung Lee parseAssembly("define void @test(i32 %x) {\n" 7150f2c1801SJuneyoung Lee " %A2 = icmp eq i32 %x, 0\n" 7160f2c1801SJuneyoung Lee " %A = icmp eq i32 %x, 1\n" 7170f2c1801SJuneyoung Lee " ret void\n" 7180f2c1801SJuneyoung Lee "}"); 7190f2c1801SJuneyoung Lee EXPECT_TRUE(impliesPoison(A2, A)); 7200f2c1801SJuneyoung Lee } 7210f2c1801SJuneyoung Lee 7220f2c1801SJuneyoung Lee TEST_F(ValueTrackingTest, impliesPoisonTest_ICmpUnknown) { 7230f2c1801SJuneyoung Lee parseAssembly("define void @test(i32 %x, i32 %y) {\n" 7240f2c1801SJuneyoung Lee " %A2 = icmp eq i32 %x, %y\n" 7250f2c1801SJuneyoung Lee " %A = icmp eq i32 %x, 1\n" 7260f2c1801SJuneyoung Lee " ret void\n" 7270f2c1801SJuneyoung Lee "}"); 7280f2c1801SJuneyoung Lee EXPECT_FALSE(impliesPoison(A2, A)); 7290f2c1801SJuneyoung Lee } 7300f2c1801SJuneyoung Lee 7310f2c1801SJuneyoung Lee TEST_F(ValueTrackingTest, impliesPoisonTest_AddNswOkay) { 7320f2c1801SJuneyoung Lee parseAssembly("define void @test(i32 %x) {\n" 7330f2c1801SJuneyoung Lee " %A2 = add nsw i32 %x, 1\n" 7340f2c1801SJuneyoung Lee " %A = add i32 %A2, 1\n" 7350f2c1801SJuneyoung Lee " ret void\n" 7360f2c1801SJuneyoung Lee "}"); 7370f2c1801SJuneyoung Lee EXPECT_TRUE(impliesPoison(A2, A)); 7380f2c1801SJuneyoung Lee } 7390f2c1801SJuneyoung Lee 7400f2c1801SJuneyoung Lee TEST_F(ValueTrackingTest, impliesPoisonTest_AddNswOkay2) { 7410f2c1801SJuneyoung Lee parseAssembly("define void @test(i32 %x) {\n" 7420f2c1801SJuneyoung Lee " %A2 = add i32 %x, 1\n" 7430f2c1801SJuneyoung Lee " %A = add nsw i32 %A2, 1\n" 7440f2c1801SJuneyoung Lee " ret void\n" 7450f2c1801SJuneyoung Lee "}"); 7460f2c1801SJuneyoung Lee EXPECT_TRUE(impliesPoison(A2, A)); 7470f2c1801SJuneyoung Lee } 7480f2c1801SJuneyoung Lee 7490f2c1801SJuneyoung Lee TEST_F(ValueTrackingTest, impliesPoisonTest_AddNsw) { 7500f2c1801SJuneyoung Lee parseAssembly("define void @test(i32 %x) {\n" 7510f2c1801SJuneyoung Lee " %A2 = add nsw i32 %x, 1\n" 7520f2c1801SJuneyoung Lee " %A = add i32 %x, 1\n" 7530f2c1801SJuneyoung Lee " ret void\n" 7540f2c1801SJuneyoung Lee "}"); 7550f2c1801SJuneyoung Lee EXPECT_FALSE(impliesPoison(A2, A)); 7560f2c1801SJuneyoung Lee } 7570f2c1801SJuneyoung Lee 758051ec9f5SNikita Popov TEST_F(ValueTrackingTest, impliesPoisonTest_Cmp) { 759051ec9f5SNikita Popov parseAssembly("define void @test(i32 %x, i32 %y, i1 %c) {\n" 760051ec9f5SNikita Popov " %A2 = icmp eq i32 %x, %y\n" 761051ec9f5SNikita Popov " %A0 = icmp ult i32 %x, %y\n" 762051ec9f5SNikita Popov " %A = or i1 %A0, %c\n" 763051ec9f5SNikita Popov " ret void\n" 764051ec9f5SNikita Popov "}"); 765051ec9f5SNikita Popov EXPECT_TRUE(impliesPoison(A2, A)); 766051ec9f5SNikita Popov } 767051ec9f5SNikita Popov 768051ec9f5SNikita Popov TEST_F(ValueTrackingTest, impliesPoisonTest_FCmpFMF) { 769051ec9f5SNikita Popov parseAssembly("define void @test(float %x, float %y, i1 %c) {\n" 770051ec9f5SNikita Popov " %A2 = fcmp nnan oeq float %x, %y\n" 771051ec9f5SNikita Popov " %A0 = fcmp olt float %x, %y\n" 772051ec9f5SNikita Popov " %A = or i1 %A0, %c\n" 773051ec9f5SNikita Popov " ret void\n" 774051ec9f5SNikita Popov "}"); 775051ec9f5SNikita Popov EXPECT_FALSE(impliesPoison(A2, A)); 776051ec9f5SNikita Popov } 777051ec9f5SNikita Popov 778051ec9f5SNikita Popov TEST_F(ValueTrackingTest, impliesPoisonTest_AddSubSameOps) { 779051ec9f5SNikita Popov parseAssembly("define void @test(i32 %x, i32 %y, i1 %c) {\n" 780051ec9f5SNikita Popov " %A2 = add i32 %x, %y\n" 781051ec9f5SNikita Popov " %A = sub i32 %x, %y\n" 782051ec9f5SNikita Popov " ret void\n" 783051ec9f5SNikita Popov "}"); 784051ec9f5SNikita Popov EXPECT_TRUE(impliesPoison(A2, A)); 785051ec9f5SNikita Popov } 786051ec9f5SNikita Popov 787051ec9f5SNikita Popov TEST_F(ValueTrackingTest, impliesPoisonTest_MaskCmp) { 788051ec9f5SNikita Popov parseAssembly("define void @test(i32 %x, i32 %y, i1 %c) {\n" 789051ec9f5SNikita Popov " %M2 = and i32 %x, 7\n" 790051ec9f5SNikita Popov " %A2 = icmp eq i32 %M2, 1\n" 791051ec9f5SNikita Popov " %M = and i32 %x, 15\n" 792051ec9f5SNikita Popov " %A = icmp eq i32 %M, 3\n" 793051ec9f5SNikita Popov " ret void\n" 794051ec9f5SNikita Popov "}"); 795051ec9f5SNikita Popov EXPECT_TRUE(impliesPoison(A2, A)); 796051ec9f5SNikita Popov } 797051ec9f5SNikita Popov 7988351f9b5SNikita Popov TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle_Pointers) { 7998351f9b5SNikita Popov parseAssembly( 8008351f9b5SNikita Popov "define <2 x i32*> @test(<2 x i32*> %x) {\n" 8018351f9b5SNikita Popov " %A = shufflevector <2 x i32*> zeroinitializer, <2 x i32*> undef, <2 x i32> zeroinitializer\n" 8028351f9b5SNikita Popov " ret <2 x i32*> %A\n" 8038351f9b5SNikita Popov "}\n"); 8048351f9b5SNikita Popov EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 64u); 8058351f9b5SNikita Popov } 8068351f9b5SNikita Popov 807e5f602d8SJuneyoung Lee TEST(ValueTracking, propagatesPoison) { 808df0b97daSJuneyoung Lee std::string AsmHead = 809df0b97daSJuneyoung Lee "declare i32 @g(i32)\n" 810df0b97daSJuneyoung Lee "declare {i32, i1} @llvm.sadd.with.overflow.i32(i32 %a, i32 %b)\n" 811df0b97daSJuneyoung Lee "declare {i32, i1} @llvm.ssub.with.overflow.i32(i32 %a, i32 %b)\n" 812df0b97daSJuneyoung Lee "declare {i32, i1} @llvm.smul.with.overflow.i32(i32 %a, i32 %b)\n" 813df0b97daSJuneyoung Lee "declare {i32, i1} @llvm.uadd.with.overflow.i32(i32 %a, i32 %b)\n" 814df0b97daSJuneyoung Lee "declare {i32, i1} @llvm.usub.with.overflow.i32(i32 %a, i32 %b)\n" 815df0b97daSJuneyoung Lee "declare {i32, i1} @llvm.umul.with.overflow.i32(i32 %a, i32 %b)\n" 816e5f602d8SJuneyoung Lee "define void @f(i32 %x, i32 %y, float %fx, float %fy, " 817e5f602d8SJuneyoung Lee "i1 %cond, i8* %p) {\n"; 818e5f602d8SJuneyoung Lee std::string AsmTail = " ret void\n}"; 819e5f602d8SJuneyoung Lee // (propagates poison?, IR instruction) 820e5f602d8SJuneyoung Lee SmallVector<std::pair<bool, std::string>, 32> Data = { 821e5f602d8SJuneyoung Lee {true, "add i32 %x, %y"}, 822e5f602d8SJuneyoung Lee {true, "add nsw nuw i32 %x, %y"}, 823e5f602d8SJuneyoung Lee {true, "ashr i32 %x, %y"}, 824e5f602d8SJuneyoung Lee {true, "lshr exact i32 %x, 31"}, 825*572e506bSSanjay Patel {true, "fadd float %fx, %fy"}, 826*572e506bSSanjay Patel {true, "fsub float %fx, %fy"}, 827*572e506bSSanjay Patel {true, "fmul float %fx, %fy"}, 828*572e506bSSanjay Patel {true, "fdiv float %fx, %fy"}, 829*572e506bSSanjay Patel {true, "frem float %fx, %fy"}, 830*572e506bSSanjay Patel {true, "fneg float %fx"}, 831e5f602d8SJuneyoung Lee {true, "fcmp oeq float %fx, %fy"}, 832e5f602d8SJuneyoung Lee {true, "icmp eq i32 %x, %y"}, 833e5f602d8SJuneyoung Lee {true, "getelementptr i8, i8* %p, i32 %x"}, 834e5f602d8SJuneyoung Lee {true, "getelementptr inbounds i8, i8* %p, i32 %x"}, 835e5f602d8SJuneyoung Lee {true, "bitcast float %fx to i32"}, 836e5f602d8SJuneyoung Lee {false, "select i1 %cond, i32 %x, i32 %y"}, 837e5f602d8SJuneyoung Lee {false, "freeze i32 %x"}, 838e5f602d8SJuneyoung Lee {true, "udiv i32 %x, %y"}, 839e5f602d8SJuneyoung Lee {true, "urem i32 %x, %y"}, 840e5f602d8SJuneyoung Lee {true, "sdiv exact i32 %x, %y"}, 841e5f602d8SJuneyoung Lee {true, "srem i32 %x, %y"}, 842df0b97daSJuneyoung Lee {false, "call i32 @g(i32 %x)"}, 843df0b97daSJuneyoung Lee {true, "call {i32, i1} @llvm.sadd.with.overflow.i32(i32 %x, i32 %y)"}, 844df0b97daSJuneyoung Lee {true, "call {i32, i1} @llvm.ssub.with.overflow.i32(i32 %x, i32 %y)"}, 845df0b97daSJuneyoung Lee {true, "call {i32, i1} @llvm.smul.with.overflow.i32(i32 %x, i32 %y)"}, 846df0b97daSJuneyoung Lee {true, "call {i32, i1} @llvm.uadd.with.overflow.i32(i32 %x, i32 %y)"}, 847df0b97daSJuneyoung Lee {true, "call {i32, i1} @llvm.usub.with.overflow.i32(i32 %x, i32 %y)"}, 848df0b97daSJuneyoung Lee {true, "call {i32, i1} @llvm.umul.with.overflow.i32(i32 %x, i32 %y)"}}; 849e5f602d8SJuneyoung Lee 850e5f602d8SJuneyoung Lee std::string AssemblyStr = AsmHead; 851e5f602d8SJuneyoung Lee for (auto &Itm : Data) 852e5f602d8SJuneyoung Lee AssemblyStr += Itm.second + "\n"; 853e5f602d8SJuneyoung Lee AssemblyStr += AsmTail; 854e5f602d8SJuneyoung Lee 855e5f602d8SJuneyoung Lee LLVMContext Context; 856e5f602d8SJuneyoung Lee SMDiagnostic Error; 857e5f602d8SJuneyoung Lee auto M = parseAssemblyString(AssemblyStr, Error, Context); 858e5f602d8SJuneyoung Lee assert(M && "Bad assembly?"); 859e5f602d8SJuneyoung Lee 860e5f602d8SJuneyoung Lee auto *F = M->getFunction("f"); 861e5f602d8SJuneyoung Lee assert(F && "Bad assembly?"); 862e5f602d8SJuneyoung Lee 863e5f602d8SJuneyoung Lee auto &BB = F->getEntryBlock(); 864e5f602d8SJuneyoung Lee 865e5f602d8SJuneyoung Lee int Index = 0; 866e5f602d8SJuneyoung Lee for (auto &I : BB) { 867e5f602d8SJuneyoung Lee if (isa<ReturnInst>(&I)) 868e5f602d8SJuneyoung Lee break; 86925ce1e04SJuneyoung Lee EXPECT_EQ(propagatesPoison(cast<Operator>(&I)), Data[Index].first) 870e5f602d8SJuneyoung Lee << "Incorrect answer at instruction " << Index << " = " << I; 871e5f602d8SJuneyoung Lee Index++; 872e5f602d8SJuneyoung Lee } 873e5f602d8SJuneyoung Lee } 874e5f602d8SJuneyoung Lee 87525ce1e04SJuneyoung Lee TEST_F(ValueTrackingTest, programUndefinedIfPoison) { 87625ce1e04SJuneyoung Lee parseAssembly("declare i32 @any_num()" 87725ce1e04SJuneyoung Lee "define void @test(i32 %mask) {\n" 87825ce1e04SJuneyoung Lee " %A = call i32 @any_num()\n" 87925ce1e04SJuneyoung Lee " %B = or i32 %A, %mask\n" 88025ce1e04SJuneyoung Lee " udiv i32 1, %B" 88125ce1e04SJuneyoung Lee " ret void\n" 88225ce1e04SJuneyoung Lee "}\n"); 88325ce1e04SJuneyoung Lee // If %A was poison, udiv raises UB regardless of %mask's value 88425ce1e04SJuneyoung Lee EXPECT_EQ(programUndefinedIfPoison(A), true); 88525ce1e04SJuneyoung Lee } 88625ce1e04SJuneyoung Lee 88725ce1e04SJuneyoung Lee TEST_F(ValueTrackingTest, programUndefinedIfUndefOrPoison) { 88825ce1e04SJuneyoung Lee parseAssembly("declare i32 @any_num()" 88925ce1e04SJuneyoung Lee "define void @test(i32 %mask) {\n" 89025ce1e04SJuneyoung Lee " %A = call i32 @any_num()\n" 89125ce1e04SJuneyoung Lee " %B = or i32 %A, %mask\n" 89225ce1e04SJuneyoung Lee " udiv i32 1, %B" 89325ce1e04SJuneyoung Lee " ret void\n" 89425ce1e04SJuneyoung Lee "}\n"); 89525ce1e04SJuneyoung Lee // If %A was undef and %mask was 1, udiv does not raise UB 89625ce1e04SJuneyoung Lee EXPECT_EQ(programUndefinedIfUndefOrPoison(A), false); 89725ce1e04SJuneyoung Lee } 89825ce1e04SJuneyoung Lee 89925ce1e04SJuneyoung Lee TEST_F(ValueTrackingTest, isGuaranteedNotToBePoison_exploitBranchCond) { 90025ce1e04SJuneyoung Lee parseAssembly("declare i1 @any_bool()" 90125ce1e04SJuneyoung Lee "define void @test(i1 %y) {\n" 90225ce1e04SJuneyoung Lee " %A = call i1 @any_bool()\n" 90325ce1e04SJuneyoung Lee " %cond = and i1 %A, %y\n" 90425ce1e04SJuneyoung Lee " br i1 %cond, label %BB1, label %BB2\n" 90525ce1e04SJuneyoung Lee "BB1:\n" 90625ce1e04SJuneyoung Lee " ret void\n" 90725ce1e04SJuneyoung Lee "BB2:\n" 90825ce1e04SJuneyoung Lee " ret void\n" 90925ce1e04SJuneyoung Lee "}\n"); 91025ce1e04SJuneyoung Lee DominatorTree DT(*F); 91125ce1e04SJuneyoung Lee for (auto &BB : *F) { 91225ce1e04SJuneyoung Lee if (&BB == &F->getEntryBlock()) 91325ce1e04SJuneyoung Lee continue; 91425ce1e04SJuneyoung Lee 9159b3c2a72SJuneyoung Lee EXPECT_EQ(isGuaranteedNotToBePoison(A, nullptr, BB.getTerminator(), &DT), 9169b3c2a72SJuneyoung Lee true) 91725ce1e04SJuneyoung Lee << "isGuaranteedNotToBePoison does not hold at " << *BB.getTerminator(); 91825ce1e04SJuneyoung Lee } 91925ce1e04SJuneyoung Lee } 92025ce1e04SJuneyoung Lee 921ba8911d5SJuneyoung Lee TEST_F(ValueTrackingTest, isGuaranteedNotToBePoison_phi) { 922ba8911d5SJuneyoung Lee parseAssembly("declare i32 @any_i32(i32)" 923ba8911d5SJuneyoung Lee "define void @test() {\n" 924ba8911d5SJuneyoung Lee "ENTRY:\n" 925ba8911d5SJuneyoung Lee " br label %LOOP\n" 926ba8911d5SJuneyoung Lee "LOOP:\n" 927ba8911d5SJuneyoung Lee " %A = phi i32 [0, %ENTRY], [%A.next, %NEXT]\n" 928ba8911d5SJuneyoung Lee " %A.next = call i32 @any_i32(i32 %A)\n" 929ba8911d5SJuneyoung Lee " %cond = icmp eq i32 %A.next, 0\n" 930ba8911d5SJuneyoung Lee " br i1 %cond, label %NEXT, label %EXIT\n" 931ba8911d5SJuneyoung Lee "NEXT:\n" 932ba8911d5SJuneyoung Lee " br label %LOOP\n" 933ba8911d5SJuneyoung Lee "EXIT:\n" 934ba8911d5SJuneyoung Lee " ret void\n" 935ba8911d5SJuneyoung Lee "}\n"); 936ba8911d5SJuneyoung Lee DominatorTree DT(*F); 937ba8911d5SJuneyoung Lee for (auto &BB : *F) { 938ba8911d5SJuneyoung Lee if (BB.getName() == "LOOP") { 9399b3c2a72SJuneyoung Lee EXPECT_EQ(isGuaranteedNotToBePoison(A, nullptr, A, &DT), true) 940ba8911d5SJuneyoung Lee << "isGuaranteedNotToBePoison does not hold"; 941ba8911d5SJuneyoung Lee } 942ba8911d5SJuneyoung Lee } 943ba8911d5SJuneyoung Lee } 944ba8911d5SJuneyoung Lee 9451c452200SJuneyoung Lee TEST_F(ValueTrackingTest, isGuaranteedNotToBeUndefOrPoison) { 9461c452200SJuneyoung Lee parseAssembly("declare void @f(i32 noundef)" 9471c452200SJuneyoung Lee "define void @test(i32 %x) {\n" 9481c452200SJuneyoung Lee " %A = bitcast i32 %x to i32\n" 9491c452200SJuneyoung Lee " call void @f(i32 noundef %x)\n" 9501c452200SJuneyoung Lee " ret void\n" 9511c452200SJuneyoung Lee "}\n"); 9521c452200SJuneyoung Lee EXPECT_EQ(isGuaranteedNotToBeUndefOrPoison(A), true); 953abbef2fdSJuneyoung Lee EXPECT_EQ(isGuaranteedNotToBeUndefOrPoison(UndefValue::get(IntegerType::get(Context, 8))), false); 954abbef2fdSJuneyoung Lee EXPECT_EQ(isGuaranteedNotToBeUndefOrPoison(PoisonValue::get(IntegerType::get(Context, 8))), false); 955abbef2fdSJuneyoung Lee EXPECT_EQ(isGuaranteedNotToBePoison(UndefValue::get(IntegerType::get(Context, 8))), true); 956abbef2fdSJuneyoung Lee EXPECT_EQ(isGuaranteedNotToBePoison(PoisonValue::get(IntegerType::get(Context, 8))), false); 95729f8628dSJuneyoung Lee 95829f8628dSJuneyoung Lee Type *Int32Ty = Type::getInt32Ty(Context); 95929f8628dSJuneyoung Lee Constant *CU = UndefValue::get(Int32Ty); 96029f8628dSJuneyoung Lee Constant *CP = PoisonValue::get(Int32Ty); 96129f8628dSJuneyoung Lee Constant *C1 = ConstantInt::get(Int32Ty, 1); 96229f8628dSJuneyoung Lee Constant *C2 = ConstantInt::get(Int32Ty, 2); 96329f8628dSJuneyoung Lee 96429f8628dSJuneyoung Lee { 96529f8628dSJuneyoung Lee Constant *V1 = ConstantVector::get({C1, C2}); 96629f8628dSJuneyoung Lee EXPECT_TRUE(isGuaranteedNotToBeUndefOrPoison(V1)); 96729f8628dSJuneyoung Lee EXPECT_TRUE(isGuaranteedNotToBePoison(V1)); 96829f8628dSJuneyoung Lee } 96929f8628dSJuneyoung Lee 97029f8628dSJuneyoung Lee { 97129f8628dSJuneyoung Lee Constant *V2 = ConstantVector::get({C1, CU}); 97229f8628dSJuneyoung Lee EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(V2)); 97329f8628dSJuneyoung Lee EXPECT_TRUE(isGuaranteedNotToBePoison(V2)); 97429f8628dSJuneyoung Lee } 97529f8628dSJuneyoung Lee 97629f8628dSJuneyoung Lee { 97729f8628dSJuneyoung Lee Constant *V3 = ConstantVector::get({C1, CP}); 97829f8628dSJuneyoung Lee EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(V3)); 97929f8628dSJuneyoung Lee EXPECT_FALSE(isGuaranteedNotToBePoison(V3)); 98029f8628dSJuneyoung Lee } 9811c452200SJuneyoung Lee } 9821c452200SJuneyoung Lee 9839b3c2a72SJuneyoung Lee TEST_F(ValueTrackingTest, isGuaranteedNotToBeUndefOrPoison_assume) { 9849b3c2a72SJuneyoung Lee parseAssembly("declare i1 @f_i1()\n" 9859b3c2a72SJuneyoung Lee "declare i32 @f_i32()\n" 9869b3c2a72SJuneyoung Lee "declare void @llvm.assume(i1)\n" 9879b3c2a72SJuneyoung Lee "define void @test() {\n" 9889b3c2a72SJuneyoung Lee " %A = call i32 @f_i32()\n" 9899b3c2a72SJuneyoung Lee " %cond = call i1 @f_i1()\n" 9909b3c2a72SJuneyoung Lee " %CxtI = add i32 0, 0\n" 9919b3c2a72SJuneyoung Lee " br i1 %cond, label %BB1, label %EXIT\n" 9929b3c2a72SJuneyoung Lee "BB1:\n" 9939b3c2a72SJuneyoung Lee " %CxtI2 = add i32 0, 0\n" 9949b3c2a72SJuneyoung Lee " %cond2 = call i1 @f_i1()\n" 9959b3c2a72SJuneyoung Lee " call void @llvm.assume(i1 true) [ \"noundef\"(i32 %A) ]\n" 9969b3c2a72SJuneyoung Lee " br i1 %cond2, label %BB2, label %EXIT\n" 9979b3c2a72SJuneyoung Lee "BB2:\n" 9989b3c2a72SJuneyoung Lee " %CxtI3 = add i32 0, 0\n" 9999b3c2a72SJuneyoung Lee " ret void\n" 10009b3c2a72SJuneyoung Lee "EXIT:\n" 10019b3c2a72SJuneyoung Lee " ret void\n" 10029b3c2a72SJuneyoung Lee "}"); 10039b3c2a72SJuneyoung Lee AssumptionCache AC(*F); 10049b3c2a72SJuneyoung Lee DominatorTree DT(*F); 10059b3c2a72SJuneyoung Lee EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(A, &AC, CxtI, &DT)); 10069b3c2a72SJuneyoung Lee EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(A, &AC, CxtI2, &DT)); 10079b3c2a72SJuneyoung Lee EXPECT_TRUE(isGuaranteedNotToBeUndefOrPoison(A, &AC, CxtI3, &DT)); 10089b3c2a72SJuneyoung Lee } 10099b3c2a72SJuneyoung Lee 10100a6aee51SJuneyoung Lee TEST(ValueTracking, canCreatePoisonOrUndef) { 1011994543abSJuneyoung Lee std::string AsmHead = 1012f8cece18SMarkus Lavin "@s = external dso_local global i32, align 1\n" 1013994543abSJuneyoung Lee "declare i32 @g(i32)\n" 1014df0b97daSJuneyoung Lee "declare {i32, i1} @llvm.sadd.with.overflow.i32(i32 %a, i32 %b)\n" 1015df0b97daSJuneyoung Lee "declare {i32, i1} @llvm.ssub.with.overflow.i32(i32 %a, i32 %b)\n" 1016df0b97daSJuneyoung Lee "declare {i32, i1} @llvm.smul.with.overflow.i32(i32 %a, i32 %b)\n" 1017df0b97daSJuneyoung Lee "declare {i32, i1} @llvm.uadd.with.overflow.i32(i32 %a, i32 %b)\n" 1018df0b97daSJuneyoung Lee "declare {i32, i1} @llvm.usub.with.overflow.i32(i32 %a, i32 %b)\n" 1019df0b97daSJuneyoung Lee "declare {i32, i1} @llvm.umul.with.overflow.i32(i32 %a, i32 %b)\n" 1020994543abSJuneyoung Lee "define void @f(i32 %x, i32 %y, float %fx, float %fy, i1 %cond, " 1021994543abSJuneyoung Lee "<4 x i32> %vx, <4 x i32> %vx2, <vscale x 4 x i32> %svx, i8* %p) {\n"; 1022994543abSJuneyoung Lee std::string AsmTail = " ret void\n}"; 10230a6aee51SJuneyoung Lee // (can create poison?, can create undef?, IR instruction) 1024ef66e3d0SJuneyoung Lee SmallVector<std::pair<std::pair<bool, bool>, std::string>, 32> Data = { 1025ef66e3d0SJuneyoung Lee {{false, false}, "add i32 %x, %y"}, 1026ef66e3d0SJuneyoung Lee {{true, false}, "add nsw nuw i32 %x, %y"}, 1027ef66e3d0SJuneyoung Lee {{true, false}, "shl i32 %x, %y"}, 1028ef66e3d0SJuneyoung Lee {{true, false}, "shl <4 x i32> %vx, %vx2"}, 1029ef66e3d0SJuneyoung Lee {{true, false}, "shl nsw i32 %x, %y"}, 1030ef66e3d0SJuneyoung Lee {{true, false}, "shl nsw <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"}, 1031ef66e3d0SJuneyoung Lee {{false, false}, "shl i32 %x, 31"}, 1032ef66e3d0SJuneyoung Lee {{true, false}, "shl i32 %x, 32"}, 1033ef66e3d0SJuneyoung Lee {{false, false}, "shl <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"}, 1034ef66e3d0SJuneyoung Lee {{true, false}, "shl <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 32>"}, 1035ef66e3d0SJuneyoung Lee {{true, false}, "ashr i32 %x, %y"}, 1036ef66e3d0SJuneyoung Lee {{true, false}, "ashr exact i32 %x, %y"}, 1037ef66e3d0SJuneyoung Lee {{false, false}, "ashr i32 %x, 31"}, 1038ef66e3d0SJuneyoung Lee {{true, false}, "ashr exact i32 %x, 31"}, 1039ef66e3d0SJuneyoung Lee {{false, false}, "ashr <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"}, 1040ef66e3d0SJuneyoung Lee {{true, false}, "ashr <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 32>"}, 1041ef66e3d0SJuneyoung Lee {{true, false}, "ashr exact <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"}, 1042ef66e3d0SJuneyoung Lee {{true, false}, "lshr i32 %x, %y"}, 1043ef66e3d0SJuneyoung Lee {{true, false}, "lshr exact i32 %x, 31"}, 1044ef66e3d0SJuneyoung Lee {{false, false}, "udiv i32 %x, %y"}, 1045ef66e3d0SJuneyoung Lee {{true, false}, "udiv exact i32 %x, %y"}, 1046ef66e3d0SJuneyoung Lee {{false, false}, "getelementptr i8, i8* %p, i32 %x"}, 1047ef66e3d0SJuneyoung Lee {{true, false}, "getelementptr inbounds i8, i8* %p, i32 %x"}, 1048ef66e3d0SJuneyoung Lee {{true, false}, "fneg nnan float %fx"}, 1049ef66e3d0SJuneyoung Lee {{false, false}, "fneg float %fx"}, 1050ef66e3d0SJuneyoung Lee {{false, false}, "fadd float %fx, %fy"}, 1051ef66e3d0SJuneyoung Lee {{true, false}, "fadd nnan float %fx, %fy"}, 1052ef66e3d0SJuneyoung Lee {{false, false}, "urem i32 %x, %y"}, 1053ef66e3d0SJuneyoung Lee {{true, false}, "fptoui float %fx to i32"}, 1054ef66e3d0SJuneyoung Lee {{true, false}, "fptosi float %fx to i32"}, 1055ef66e3d0SJuneyoung Lee {{false, false}, "bitcast float %fx to i32"}, 1056ef66e3d0SJuneyoung Lee {{false, false}, "select i1 %cond, i32 %x, i32 %y"}, 1057ef66e3d0SJuneyoung Lee {{true, false}, "select nnan i1 %cond, float %fx, float %fy"}, 1058ef66e3d0SJuneyoung Lee {{true, false}, "extractelement <4 x i32> %vx, i32 %x"}, 1059ef66e3d0SJuneyoung Lee {{false, false}, "extractelement <4 x i32> %vx, i32 3"}, 1060ef66e3d0SJuneyoung Lee {{true, false}, "extractelement <vscale x 4 x i32> %svx, i32 4"}, 1061ef66e3d0SJuneyoung Lee {{true, false}, "insertelement <4 x i32> %vx, i32 %x, i32 %y"}, 1062ef66e3d0SJuneyoung Lee {{false, false}, "insertelement <4 x i32> %vx, i32 %x, i32 3"}, 1063ef66e3d0SJuneyoung Lee {{true, false}, "insertelement <vscale x 4 x i32> %svx, i32 %x, i32 4"}, 1064ef66e3d0SJuneyoung Lee {{false, false}, "freeze i32 %x"}, 1065ef66e3d0SJuneyoung Lee {{false, false}, 10660a6aee51SJuneyoung Lee "shufflevector <4 x i32> %vx, <4 x i32> %vx2, " 10670a6aee51SJuneyoung Lee "<4 x i32> <i32 0, i32 1, i32 2, i32 3>"}, 1068ef66e3d0SJuneyoung Lee {{false, true}, 10690a6aee51SJuneyoung Lee "shufflevector <4 x i32> %vx, <4 x i32> %vx2, " 10700a6aee51SJuneyoung Lee "<4 x i32> <i32 0, i32 1, i32 2, i32 undef>"}, 1071ef66e3d0SJuneyoung Lee {{false, true}, 10720a6aee51SJuneyoung Lee "shufflevector <vscale x 4 x i32> %svx, " 10730a6aee51SJuneyoung Lee "<vscale x 4 x i32> %svx, <vscale x 4 x i32> undef"}, 1074ef66e3d0SJuneyoung Lee {{true, false}, "call i32 @g(i32 %x)"}, 1075ef66e3d0SJuneyoung Lee {{false, false}, "call noundef i32 @g(i32 %x)"}, 1076ef66e3d0SJuneyoung Lee {{true, false}, "fcmp nnan oeq float %fx, %fy"}, 1077f8cece18SMarkus Lavin {{false, false}, "fcmp oeq float %fx, %fy"}, 1078f8cece18SMarkus Lavin {{true, false}, 1079f8cece18SMarkus Lavin "ashr <4 x i32> %vx, select (i1 icmp sgt (i32 ptrtoint (i32* @s to " 1080f8cece18SMarkus Lavin "i32), i32 1), <4 x i32> zeroinitializer, <4 x i32> <i32 0, i32 1, i32 " 1081df0b97daSJuneyoung Lee "2, i32 3>)"}, 1082df0b97daSJuneyoung Lee {{false, false}, 1083df0b97daSJuneyoung Lee "call {i32, i1} @llvm.sadd.with.overflow.i32(i32 %x, i32 %y)"}, 1084df0b97daSJuneyoung Lee {{false, false}, 1085df0b97daSJuneyoung Lee "call {i32, i1} @llvm.ssub.with.overflow.i32(i32 %x, i32 %y)"}, 1086df0b97daSJuneyoung Lee {{false, false}, 1087df0b97daSJuneyoung Lee "call {i32, i1} @llvm.smul.with.overflow.i32(i32 %x, i32 %y)"}, 1088df0b97daSJuneyoung Lee {{false, false}, 1089df0b97daSJuneyoung Lee "call {i32, i1} @llvm.uadd.with.overflow.i32(i32 %x, i32 %y)"}, 1090df0b97daSJuneyoung Lee {{false, false}, 1091df0b97daSJuneyoung Lee "call {i32, i1} @llvm.usub.with.overflow.i32(i32 %x, i32 %y)"}, 1092df0b97daSJuneyoung Lee {{false, false}, 1093df0b97daSJuneyoung Lee "call {i32, i1} @llvm.umul.with.overflow.i32(i32 %x, i32 %y)"}}; 1094994543abSJuneyoung Lee 1095994543abSJuneyoung Lee std::string AssemblyStr = AsmHead; 1096994543abSJuneyoung Lee for (auto &Itm : Data) 1097ef66e3d0SJuneyoung Lee AssemblyStr += Itm.second + "\n"; 1098994543abSJuneyoung Lee AssemblyStr += AsmTail; 1099994543abSJuneyoung Lee 1100994543abSJuneyoung Lee LLVMContext Context; 1101994543abSJuneyoung Lee SMDiagnostic Error; 1102994543abSJuneyoung Lee auto M = parseAssemblyString(AssemblyStr, Error, Context); 1103994543abSJuneyoung Lee assert(M && "Bad assembly?"); 1104994543abSJuneyoung Lee 1105994543abSJuneyoung Lee auto *F = M->getFunction("f"); 1106994543abSJuneyoung Lee assert(F && "Bad assembly?"); 1107994543abSJuneyoung Lee 1108994543abSJuneyoung Lee auto &BB = F->getEntryBlock(); 1109994543abSJuneyoung Lee 1110994543abSJuneyoung Lee int Index = 0; 1111994543abSJuneyoung Lee for (auto &I : BB) { 1112994543abSJuneyoung Lee if (isa<ReturnInst>(&I)) 1113994543abSJuneyoung Lee break; 1114ef66e3d0SJuneyoung Lee bool Poison = Data[Index].first.first; 1115ef66e3d0SJuneyoung Lee bool Undef = Data[Index].first.second; 11160a6aee51SJuneyoung Lee EXPECT_EQ(canCreatePoison(cast<Operator>(&I)), Poison) 11170a6aee51SJuneyoung Lee << "Incorrect answer of canCreatePoison at instruction " << Index 11180a6aee51SJuneyoung Lee << " = " << I; 11190a6aee51SJuneyoung Lee EXPECT_EQ(canCreateUndefOrPoison(cast<Operator>(&I)), Undef || Poison) 11200a6aee51SJuneyoung Lee << "Incorrect answer of canCreateUndef at instruction " << Index 11210a6aee51SJuneyoung Lee << " = " << I; 1122994543abSJuneyoung Lee Index++; 1123994543abSJuneyoung Lee } 112445a148deSSanjay Patel } 112545a148deSSanjay Patel 1126565f4e72SAlex Richardson TEST_F(ValueTrackingTest, computePtrAlignment) { 1127565f4e72SAlex Richardson parseAssembly("declare i1 @f_i1()\n" 1128565f4e72SAlex Richardson "declare i8* @f_i8p()\n" 1129565f4e72SAlex Richardson "declare void @llvm.assume(i1)\n" 1130565f4e72SAlex Richardson "define void @test() {\n" 1131565f4e72SAlex Richardson " %A = call i8* @f_i8p()\n" 1132565f4e72SAlex Richardson " %cond = call i1 @f_i1()\n" 1133565f4e72SAlex Richardson " %CxtI = add i32 0, 0\n" 1134565f4e72SAlex Richardson " br i1 %cond, label %BB1, label %EXIT\n" 1135565f4e72SAlex Richardson "BB1:\n" 1136565f4e72SAlex Richardson " %CxtI2 = add i32 0, 0\n" 1137565f4e72SAlex Richardson " %cond2 = call i1 @f_i1()\n" 1138565f4e72SAlex Richardson " call void @llvm.assume(i1 true) [ \"align\"(i8* %A, i64 16) ]\n" 1139565f4e72SAlex Richardson " br i1 %cond2, label %BB2, label %EXIT\n" 1140565f4e72SAlex Richardson "BB2:\n" 1141565f4e72SAlex Richardson " %CxtI3 = add i32 0, 0\n" 1142565f4e72SAlex Richardson " ret void\n" 1143565f4e72SAlex Richardson "EXIT:\n" 1144565f4e72SAlex Richardson " ret void\n" 1145565f4e72SAlex Richardson "}"); 1146565f4e72SAlex Richardson AssumptionCache AC(*F); 1147565f4e72SAlex Richardson DominatorTree DT(*F); 11484ad59f9aSSimon Pilgrim const DataLayout &DL = M->getDataLayout(); 1149565f4e72SAlex Richardson EXPECT_EQ(getKnownAlignment(A, DL, CxtI, &AC, &DT), Align(1)); 1150565f4e72SAlex Richardson EXPECT_EQ(getKnownAlignment(A, DL, CxtI2, &AC, &DT), Align(1)); 1151565f4e72SAlex Richardson EXPECT_EQ(getKnownAlignment(A, DL, CxtI3, &AC, &DT), Align(16)); 1152565f4e72SAlex Richardson } 1153565f4e72SAlex Richardson 1154219e5367SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownBits) { 1155219e5367SNikita Popov parseAssembly( 1156219e5367SNikita Popov "define i32 @test(i32 %a, i32 %b) {\n" 1157219e5367SNikita Popov " %ash = mul i32 %a, 8\n" 1158219e5367SNikita Popov " %aad = add i32 %ash, 7\n" 1159219e5367SNikita Popov " %aan = and i32 %aad, 4095\n" 1160219e5367SNikita Popov " %bsh = shl i32 %b, 4\n" 1161219e5367SNikita Popov " %bad = or i32 %bsh, 6\n" 1162219e5367SNikita Popov " %ban = and i32 %bad, 4095\n" 1163219e5367SNikita Popov " %A = mul i32 %aan, %ban\n" 1164219e5367SNikita Popov " ret i32 %A\n" 1165219e5367SNikita Popov "}\n"); 1166219e5367SNikita Popov expectKnownBits(/*zero*/ 4278190085u, /*one*/ 10u); 116770dbd5fbSSimon Dardis } 116870dbd5fbSSimon Dardis 1169219e5367SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownMulBits) { 1170219e5367SNikita Popov parseAssembly( 1171219e5367SNikita Popov "define i32 @test(i32 %a, i32 %b) {\n" 1172219e5367SNikita Popov " %aa = shl i32 %a, 5\n" 1173219e5367SNikita Popov " %bb = shl i32 %b, 5\n" 1174219e5367SNikita Popov " %aaa = or i32 %aa, 24\n" 1175219e5367SNikita Popov " %bbb = or i32 %bb, 28\n" 1176219e5367SNikita Popov " %A = mul i32 %aaa, %bbb\n" 1177219e5367SNikita Popov " ret i32 %A\n" 1178219e5367SNikita Popov "}\n"); 1179219e5367SNikita Popov expectKnownBits(/*zero*/ 95u, /*one*/ 32u); 118070dbd5fbSSimon Dardis } 1181687b92cdSNikita Popov 1182989445f4SSanjay Patel TEST_F(ValueTrackingTest, isNonZeroRecurrence) { 1183989445f4SSanjay Patel parseAssembly(R"( 1184989445f4SSanjay Patel define i1 @test(i8 %n, i8 %r) { 1185989445f4SSanjay Patel entry: 1186989445f4SSanjay Patel br label %loop 1187989445f4SSanjay Patel loop: 1188989445f4SSanjay Patel %p = phi i8 [ -1, %entry ], [ %next, %loop ] 1189989445f4SSanjay Patel %next = add nsw i8 %p, -1 1190989445f4SSanjay Patel %cmp1 = icmp eq i8 %p, %n 1191989445f4SSanjay Patel br i1 %cmp1, label %exit, label %loop 1192989445f4SSanjay Patel exit: 1193989445f4SSanjay Patel %A = or i8 %p, %r 1194989445f4SSanjay Patel %CxtI = icmp eq i8 %A, 0 1195989445f4SSanjay Patel ret i1 %CxtI 1196989445f4SSanjay Patel } 1197989445f4SSanjay Patel )"); 11984ad59f9aSSimon Pilgrim const DataLayout &DL = M->getDataLayout(); 1199989445f4SSanjay Patel AssumptionCache AC(*F); 12005ae5d25eSSanjay Patel EXPECT_TRUE(isKnownNonZero(A, DL, 0, &AC, CxtI)); 1201989445f4SSanjay Patel } 1202989445f4SSanjay Patel 1203eca40c36SJuneyoung Lee TEST_F(ValueTrackingTest, KnownNonZeroFromDomCond) { 1204eca40c36SJuneyoung Lee parseAssembly(R"( 1205eca40c36SJuneyoung Lee declare i8* @f_i8() 1206eca40c36SJuneyoung Lee define void @test(i1 %c) { 1207eca40c36SJuneyoung Lee %A = call i8* @f_i8() 1208eca40c36SJuneyoung Lee %B = call i8* @f_i8() 1209eca40c36SJuneyoung Lee %c1 = icmp ne i8* %A, null 1210eca40c36SJuneyoung Lee %cond = and i1 %c1, %c 1211eca40c36SJuneyoung Lee br i1 %cond, label %T, label %Q 1212eca40c36SJuneyoung Lee T: 1213eca40c36SJuneyoung Lee %CxtI = add i32 0, 0 1214eca40c36SJuneyoung Lee ret void 1215eca40c36SJuneyoung Lee Q: 1216eca40c36SJuneyoung Lee %CxtI2 = add i32 0, 0 1217eca40c36SJuneyoung Lee ret void 1218eca40c36SJuneyoung Lee } 1219eca40c36SJuneyoung Lee )"); 1220eca40c36SJuneyoung Lee AssumptionCache AC(*F); 1221eca40c36SJuneyoung Lee DominatorTree DT(*F); 12224ad59f9aSSimon Pilgrim const DataLayout &DL = M->getDataLayout(); 1223eca40c36SJuneyoung Lee EXPECT_EQ(isKnownNonZero(A, DL, 0, &AC, CxtI, &DT), true); 1224eca40c36SJuneyoung Lee EXPECT_EQ(isKnownNonZero(A, DL, 0, &AC, CxtI2, &DT), false); 1225eca40c36SJuneyoung Lee } 1226eca40c36SJuneyoung Lee 1227860199dfSJuneyoung Lee TEST_F(ValueTrackingTest, KnownNonZeroFromDomCond2) { 1228860199dfSJuneyoung Lee parseAssembly(R"( 1229860199dfSJuneyoung Lee declare i8* @f_i8() 1230860199dfSJuneyoung Lee define void @test(i1 %c) { 1231860199dfSJuneyoung Lee %A = call i8* @f_i8() 1232860199dfSJuneyoung Lee %B = call i8* @f_i8() 1233860199dfSJuneyoung Lee %c1 = icmp ne i8* %A, null 1234860199dfSJuneyoung Lee %cond = select i1 %c, i1 %c1, i1 false 1235860199dfSJuneyoung Lee br i1 %cond, label %T, label %Q 1236860199dfSJuneyoung Lee T: 1237860199dfSJuneyoung Lee %CxtI = add i32 0, 0 1238860199dfSJuneyoung Lee ret void 1239860199dfSJuneyoung Lee Q: 1240860199dfSJuneyoung Lee %CxtI2 = add i32 0, 0 1241860199dfSJuneyoung Lee ret void 1242860199dfSJuneyoung Lee } 1243860199dfSJuneyoung Lee )"); 1244860199dfSJuneyoung Lee AssumptionCache AC(*F); 1245860199dfSJuneyoung Lee DominatorTree DT(*F); 12464ad59f9aSSimon Pilgrim const DataLayout &DL = M->getDataLayout(); 1247860199dfSJuneyoung Lee EXPECT_EQ(isKnownNonZero(A, DL, 0, &AC, CxtI, &DT), true); 1248860199dfSJuneyoung Lee EXPECT_EQ(isKnownNonZero(A, DL, 0, &AC, CxtI2, &DT), false); 1249860199dfSJuneyoung Lee } 1250860199dfSJuneyoung Lee 1251eca40c36SJuneyoung Lee TEST_F(ValueTrackingTest, IsImpliedConditionAnd) { 1252eca40c36SJuneyoung Lee parseAssembly(R"( 1253eca40c36SJuneyoung Lee define void @test(i32 %x, i32 %y) { 1254eca40c36SJuneyoung Lee %c1 = icmp ult i32 %x, 10 1255eca40c36SJuneyoung Lee %c2 = icmp ult i32 %y, 15 1256eca40c36SJuneyoung Lee %A = and i1 %c1, %c2 1257eca40c36SJuneyoung Lee ; x < 10 /\ y < 15 1258eca40c36SJuneyoung Lee %A2 = icmp ult i32 %x, 20 1259eca40c36SJuneyoung Lee %A3 = icmp uge i32 %y, 20 1260eca40c36SJuneyoung Lee %A4 = icmp ult i32 %x, 5 1261eca40c36SJuneyoung Lee ret void 1262eca40c36SJuneyoung Lee } 1263eca40c36SJuneyoung Lee )"); 12644ad59f9aSSimon Pilgrim const DataLayout &DL = M->getDataLayout(); 1265eca40c36SJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A2, DL), true); 1266eca40c36SJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A3, DL), false); 1267eca40c36SJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A4, DL), None); 1268eca40c36SJuneyoung Lee } 1269eca40c36SJuneyoung Lee 1270860199dfSJuneyoung Lee TEST_F(ValueTrackingTest, IsImpliedConditionAnd2) { 1271860199dfSJuneyoung Lee parseAssembly(R"( 1272860199dfSJuneyoung Lee define void @test(i32 %x, i32 %y) { 1273860199dfSJuneyoung Lee %c1 = icmp ult i32 %x, 10 1274860199dfSJuneyoung Lee %c2 = icmp ult i32 %y, 15 1275860199dfSJuneyoung Lee %A = select i1 %c1, i1 %c2, i1 false 1276860199dfSJuneyoung Lee ; x < 10 /\ y < 15 1277860199dfSJuneyoung Lee %A2 = icmp ult i32 %x, 20 1278860199dfSJuneyoung Lee %A3 = icmp uge i32 %y, 20 1279860199dfSJuneyoung Lee %A4 = icmp ult i32 %x, 5 1280860199dfSJuneyoung Lee ret void 1281860199dfSJuneyoung Lee } 1282860199dfSJuneyoung Lee )"); 12834ad59f9aSSimon Pilgrim const DataLayout &DL = M->getDataLayout(); 1284860199dfSJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A2, DL), true); 1285860199dfSJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A3, DL), false); 1286860199dfSJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A4, DL), None); 1287860199dfSJuneyoung Lee } 1288860199dfSJuneyoung Lee 1289eca40c36SJuneyoung Lee TEST_F(ValueTrackingTest, IsImpliedConditionOr) { 1290eca40c36SJuneyoung Lee parseAssembly(R"( 1291eca40c36SJuneyoung Lee define void @test(i32 %x, i32 %y) { 1292eca40c36SJuneyoung Lee %c1 = icmp ult i32 %x, 10 1293eca40c36SJuneyoung Lee %c2 = icmp ult i32 %y, 15 1294eca40c36SJuneyoung Lee %A = or i1 %c1, %c2 ; negated 1295eca40c36SJuneyoung Lee ; x >= 10 /\ y >= 15 1296eca40c36SJuneyoung Lee %A2 = icmp ult i32 %x, 5 1297eca40c36SJuneyoung Lee %A3 = icmp uge i32 %y, 10 1298eca40c36SJuneyoung Lee %A4 = icmp ult i32 %x, 15 1299eca40c36SJuneyoung Lee ret void 1300eca40c36SJuneyoung Lee } 1301eca40c36SJuneyoung Lee )"); 13024ad59f9aSSimon Pilgrim const DataLayout &DL = M->getDataLayout(); 1303eca40c36SJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A2, DL, false), false); 1304eca40c36SJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A3, DL, false), true); 1305eca40c36SJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A4, DL, false), None); 1306eca40c36SJuneyoung Lee } 1307eca40c36SJuneyoung Lee 1308860199dfSJuneyoung Lee TEST_F(ValueTrackingTest, IsImpliedConditionOr2) { 1309860199dfSJuneyoung Lee parseAssembly(R"( 1310860199dfSJuneyoung Lee define void @test(i32 %x, i32 %y) { 1311860199dfSJuneyoung Lee %c1 = icmp ult i32 %x, 10 1312860199dfSJuneyoung Lee %c2 = icmp ult i32 %y, 15 1313860199dfSJuneyoung Lee %A = select i1 %c1, i1 true, i1 %c2 ; negated 1314860199dfSJuneyoung Lee ; x >= 10 /\ y >= 15 1315860199dfSJuneyoung Lee %A2 = icmp ult i32 %x, 5 1316860199dfSJuneyoung Lee %A3 = icmp uge i32 %y, 10 1317860199dfSJuneyoung Lee %A4 = icmp ult i32 %x, 15 1318860199dfSJuneyoung Lee ret void 1319860199dfSJuneyoung Lee } 1320860199dfSJuneyoung Lee )"); 13214ad59f9aSSimon Pilgrim const DataLayout &DL = M->getDataLayout(); 1322860199dfSJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A2, DL, false), false); 1323860199dfSJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A3, DL, false), true); 1324860199dfSJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A4, DL, false), None); 1325860199dfSJuneyoung Lee } 1326860199dfSJuneyoung Lee 13277cf0f856SNikita Popov TEST_F(ComputeKnownBitsTest, KnownNonZeroShift) { 13287cf0f856SNikita Popov // %q is known nonzero without known bits. 13297cf0f856SNikita Popov // Because %q is nonzero, %A[0] is known to be zero. 13307cf0f856SNikita Popov parseAssembly( 13317cf0f856SNikita Popov "define i8 @test(i8 %p, i8* %pq) {\n" 13327cf0f856SNikita Popov " %q = load i8, i8* %pq, !range !0\n" 13337cf0f856SNikita Popov " %A = shl i8 %p, %q\n" 13347cf0f856SNikita Popov " ret i8 %A\n" 13357cf0f856SNikita Popov "}\n" 13367cf0f856SNikita Popov "!0 = !{ i8 1, i8 5 }\n"); 13377cf0f856SNikita Popov expectKnownBits(/*zero*/ 1u, /*one*/ 0u); 13387cf0f856SNikita Popov } 13397cf0f856SNikita Popov 1340687b92cdSNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownFshl) { 1341687b92cdSNikita Popov // fshl(....1111....0000, 00..1111........, 6) 1342687b92cdSNikita Popov // = 11....000000..11 1343687b92cdSNikita Popov parseAssembly( 1344687b92cdSNikita Popov "define i16 @test(i16 %a, i16 %b) {\n" 1345687b92cdSNikita Popov " %aa = shl i16 %a, 4\n" 1346687b92cdSNikita Popov " %bb = lshr i16 %b, 2\n" 1347687b92cdSNikita Popov " %aaa = or i16 %aa, 3840\n" 1348687b92cdSNikita Popov " %bbb = or i16 %bb, 3840\n" 1349687b92cdSNikita Popov " %A = call i16 @llvm.fshl.i16(i16 %aaa, i16 %bbb, i16 6)\n" 1350687b92cdSNikita Popov " ret i16 %A\n" 1351687b92cdSNikita Popov "}\n" 1352687b92cdSNikita Popov "declare i16 @llvm.fshl.i16(i16, i16, i16)\n"); 1353687b92cdSNikita Popov expectKnownBits(/*zero*/ 1008u, /*one*/ 49155u); 1354687b92cdSNikita Popov } 1355687b92cdSNikita Popov 1356687b92cdSNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownFshr) { 1357687b92cdSNikita Popov // fshr(....1111....0000, 00..1111........, 26) 1358687b92cdSNikita Popov // = 11....000000..11 1359687b92cdSNikita Popov parseAssembly( 1360687b92cdSNikita Popov "define i16 @test(i16 %a, i16 %b) {\n" 1361687b92cdSNikita Popov " %aa = shl i16 %a, 4\n" 1362687b92cdSNikita Popov " %bb = lshr i16 %b, 2\n" 1363687b92cdSNikita Popov " %aaa = or i16 %aa, 3840\n" 1364687b92cdSNikita Popov " %bbb = or i16 %bb, 3840\n" 1365687b92cdSNikita Popov " %A = call i16 @llvm.fshr.i16(i16 %aaa, i16 %bbb, i16 26)\n" 1366687b92cdSNikita Popov " ret i16 %A\n" 1367687b92cdSNikita Popov "}\n" 1368687b92cdSNikita Popov "declare i16 @llvm.fshr.i16(i16, i16, i16)\n"); 1369687b92cdSNikita Popov expectKnownBits(/*zero*/ 1008u, /*one*/ 49155u); 1370687b92cdSNikita Popov } 1371687b92cdSNikita Popov 1372687b92cdSNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownFshlZero) { 1373687b92cdSNikita Popov // fshl(....1111....0000, 00..1111........, 0) 1374687b92cdSNikita Popov // = ....1111....0000 1375687b92cdSNikita Popov parseAssembly( 1376687b92cdSNikita Popov "define i16 @test(i16 %a, i16 %b) {\n" 1377687b92cdSNikita Popov " %aa = shl i16 %a, 4\n" 1378687b92cdSNikita Popov " %bb = lshr i16 %b, 2\n" 1379687b92cdSNikita Popov " %aaa = or i16 %aa, 3840\n" 1380687b92cdSNikita Popov " %bbb = or i16 %bb, 3840\n" 1381687b92cdSNikita Popov " %A = call i16 @llvm.fshl.i16(i16 %aaa, i16 %bbb, i16 0)\n" 1382687b92cdSNikita Popov " ret i16 %A\n" 1383687b92cdSNikita Popov "}\n" 1384687b92cdSNikita Popov "declare i16 @llvm.fshl.i16(i16, i16, i16)\n"); 1385687b92cdSNikita Popov expectKnownBits(/*zero*/ 15u, /*one*/ 3840u); 1386687b92cdSNikita Popov } 1387ed3ca927SNikita Popov 1388ed3ca927SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownUAddSatLeadingOnes) { 1389ed3ca927SNikita Popov // uadd.sat(1111...1, ........) 1390ed3ca927SNikita Popov // = 1111.... 1391ed3ca927SNikita Popov parseAssembly( 1392ed3ca927SNikita Popov "define i8 @test(i8 %a, i8 %b) {\n" 1393ed3ca927SNikita Popov " %aa = or i8 %a, 241\n" 1394ed3ca927SNikita Popov " %A = call i8 @llvm.uadd.sat.i8(i8 %aa, i8 %b)\n" 1395ed3ca927SNikita Popov " ret i8 %A\n" 1396ed3ca927SNikita Popov "}\n" 1397ed3ca927SNikita Popov "declare i8 @llvm.uadd.sat.i8(i8, i8)\n"); 1398ed3ca927SNikita Popov expectKnownBits(/*zero*/ 0u, /*one*/ 240u); 1399ed3ca927SNikita Popov } 1400ed3ca927SNikita Popov 1401ed3ca927SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownUAddSatOnesPreserved) { 1402ed3ca927SNikita Popov // uadd.sat(00...011, .1...110) 1403ed3ca927SNikita Popov // = .......1 1404ed3ca927SNikita Popov parseAssembly( 1405ed3ca927SNikita Popov "define i8 @test(i8 %a, i8 %b) {\n" 1406ed3ca927SNikita Popov " %aa = or i8 %a, 3\n" 1407ed3ca927SNikita Popov " %aaa = and i8 %aa, 59\n" 1408ed3ca927SNikita Popov " %bb = or i8 %b, 70\n" 1409ed3ca927SNikita Popov " %bbb = and i8 %bb, 254\n" 1410ed3ca927SNikita Popov " %A = call i8 @llvm.uadd.sat.i8(i8 %aaa, i8 %bbb)\n" 1411ed3ca927SNikita Popov " ret i8 %A\n" 1412ed3ca927SNikita Popov "}\n" 1413ed3ca927SNikita Popov "declare i8 @llvm.uadd.sat.i8(i8, i8)\n"); 1414ed3ca927SNikita Popov expectKnownBits(/*zero*/ 0u, /*one*/ 1u); 1415ed3ca927SNikita Popov } 1416ed3ca927SNikita Popov 1417ed3ca927SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatLHSLeadingZeros) { 1418ed3ca927SNikita Popov // usub.sat(0000...0, ........) 1419ed3ca927SNikita Popov // = 0000.... 1420ed3ca927SNikita Popov parseAssembly( 1421ed3ca927SNikita Popov "define i8 @test(i8 %a, i8 %b) {\n" 1422ed3ca927SNikita Popov " %aa = and i8 %a, 14\n" 1423ed3ca927SNikita Popov " %A = call i8 @llvm.usub.sat.i8(i8 %aa, i8 %b)\n" 1424ed3ca927SNikita Popov " ret i8 %A\n" 1425ed3ca927SNikita Popov "}\n" 1426ed3ca927SNikita Popov "declare i8 @llvm.usub.sat.i8(i8, i8)\n"); 1427ed3ca927SNikita Popov expectKnownBits(/*zero*/ 240u, /*one*/ 0u); 1428ed3ca927SNikita Popov } 1429ed3ca927SNikita Popov 1430ed3ca927SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatRHSLeadingOnes) { 1431ed3ca927SNikita Popov // usub.sat(........, 1111...1) 1432ed3ca927SNikita Popov // = 0000.... 1433ed3ca927SNikita Popov parseAssembly( 1434ed3ca927SNikita Popov "define i8 @test(i8 %a, i8 %b) {\n" 1435ed3ca927SNikita Popov " %bb = or i8 %a, 241\n" 1436ed3ca927SNikita Popov " %A = call i8 @llvm.usub.sat.i8(i8 %a, i8 %bb)\n" 1437ed3ca927SNikita Popov " ret i8 %A\n" 1438ed3ca927SNikita Popov "}\n" 1439ed3ca927SNikita Popov "declare i8 @llvm.usub.sat.i8(i8, i8)\n"); 1440ed3ca927SNikita Popov expectKnownBits(/*zero*/ 240u, /*one*/ 0u); 1441ed3ca927SNikita Popov } 1442ed3ca927SNikita Popov 1443ed3ca927SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatZerosPreserved) { 1444ed3ca927SNikita Popov // usub.sat(11...011, .1...110) 1445ed3ca927SNikita Popov // = ......0. 1446ed3ca927SNikita Popov parseAssembly( 1447ed3ca927SNikita Popov "define i8 @test(i8 %a, i8 %b) {\n" 1448ed3ca927SNikita Popov " %aa = or i8 %a, 195\n" 1449ed3ca927SNikita Popov " %aaa = and i8 %aa, 251\n" 1450ed3ca927SNikita Popov " %bb = or i8 %b, 70\n" 1451ed3ca927SNikita Popov " %bbb = and i8 %bb, 254\n" 1452ed3ca927SNikita Popov " %A = call i8 @llvm.usub.sat.i8(i8 %aaa, i8 %bbb)\n" 1453ed3ca927SNikita Popov " ret i8 %A\n" 1454ed3ca927SNikita Popov "}\n" 1455ed3ca927SNikita Popov "declare i8 @llvm.usub.sat.i8(i8, i8)\n"); 1456ed3ca927SNikita Popov expectKnownBits(/*zero*/ 2u, /*one*/ 0u); 1457ed3ca927SNikita Popov } 1458329d15b5SVitaly Buka 1459d86fff6aSNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownBitsPtrToIntTrunc) { 1460d86fff6aSNikita Popov // ptrtoint truncates the pointer type. 1461d86fff6aSNikita Popov parseAssembly( 1462d86fff6aSNikita Popov "define void @test(i8** %p) {\n" 1463d86fff6aSNikita Popov " %A = load i8*, i8** %p\n" 1464d86fff6aSNikita Popov " %i = ptrtoint i8* %A to i32\n" 1465d86fff6aSNikita Popov " %m = and i32 %i, 31\n" 1466d86fff6aSNikita Popov " %c = icmp eq i32 %m, 0\n" 1467d86fff6aSNikita Popov " call void @llvm.assume(i1 %c)\n" 1468d86fff6aSNikita Popov " ret void\n" 1469d86fff6aSNikita Popov "}\n" 1470d86fff6aSNikita Popov "declare void @llvm.assume(i1)\n"); 1471d86fff6aSNikita Popov AssumptionCache AC(*F); 1472d86fff6aSNikita Popov KnownBits Known = computeKnownBits( 1473d86fff6aSNikita Popov A, M->getDataLayout(), /* Depth */ 0, &AC, F->front().getTerminator()); 1474d86fff6aSNikita Popov EXPECT_EQ(Known.Zero.getZExtValue(), 31u); 1475d86fff6aSNikita Popov EXPECT_EQ(Known.One.getZExtValue(), 0u); 1476d86fff6aSNikita Popov } 1477d86fff6aSNikita Popov 1478d86fff6aSNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownBitsPtrToIntZext) { 1479d86fff6aSNikita Popov // ptrtoint zero extends the pointer type. 1480d86fff6aSNikita Popov parseAssembly( 1481d86fff6aSNikita Popov "define void @test(i8** %p) {\n" 1482d86fff6aSNikita Popov " %A = load i8*, i8** %p\n" 1483d86fff6aSNikita Popov " %i = ptrtoint i8* %A to i128\n" 1484d86fff6aSNikita Popov " %m = and i128 %i, 31\n" 1485d86fff6aSNikita Popov " %c = icmp eq i128 %m, 0\n" 1486d86fff6aSNikita Popov " call void @llvm.assume(i1 %c)\n" 1487d86fff6aSNikita Popov " ret void\n" 1488d86fff6aSNikita Popov "}\n" 1489d86fff6aSNikita Popov "declare void @llvm.assume(i1)\n"); 1490d86fff6aSNikita Popov AssumptionCache AC(*F); 1491d86fff6aSNikita Popov KnownBits Known = computeKnownBits( 1492d86fff6aSNikita Popov A, M->getDataLayout(), /* Depth */ 0, &AC, F->front().getTerminator()); 1493d86fff6aSNikita Popov EXPECT_EQ(Known.Zero.getZExtValue(), 31u); 1494d86fff6aSNikita Popov EXPECT_EQ(Known.One.getZExtValue(), 0u); 1495d86fff6aSNikita Popov } 1496d86fff6aSNikita Popov 1497a6183d0fSJuneyoung Lee TEST_F(ComputeKnownBitsTest, ComputeKnownBitsFreeze) { 1498a6183d0fSJuneyoung Lee parseAssembly("define void @test() {\n" 1499a6183d0fSJuneyoung Lee " %m = call i32 @any_num()\n" 1500a6183d0fSJuneyoung Lee " %A = freeze i32 %m\n" 1501a6183d0fSJuneyoung Lee " %n = and i32 %m, 31\n" 1502a6183d0fSJuneyoung Lee " %c = icmp eq i32 %n, 0\n" 1503a6183d0fSJuneyoung Lee " call void @llvm.assume(i1 %c)\n" 1504a6183d0fSJuneyoung Lee " ret void\n" 1505a6183d0fSJuneyoung Lee "}\n" 1506a6183d0fSJuneyoung Lee "declare void @llvm.assume(i1)\n" 1507a6183d0fSJuneyoung Lee "declare i32 @any_num()\n"); 1508a6183d0fSJuneyoung Lee AssumptionCache AC(*F); 1509a6183d0fSJuneyoung Lee KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1510a6183d0fSJuneyoung Lee F->front().getTerminator()); 1511a6183d0fSJuneyoung Lee EXPECT_EQ(Known.Zero.getZExtValue(), 31u); 1512a6183d0fSJuneyoung Lee EXPECT_EQ(Known.One.getZExtValue(), 0u); 1513a6183d0fSJuneyoung Lee } 1514a6183d0fSJuneyoung Lee 1515f1f31eb2SQuentin Colombet TEST_F(ComputeKnownBitsTest, ComputeKnownBitsAddWithRange) { 1516f1f31eb2SQuentin Colombet parseAssembly("define void @test(i64* %p) {\n" 1517f1f31eb2SQuentin Colombet " %A = load i64, i64* %p, !range !{i64 64, i64 65536}\n" 1518f1f31eb2SQuentin Colombet " %APlus512 = add i64 %A, 512\n" 1519f1f31eb2SQuentin Colombet " %c = icmp ugt i64 %APlus512, 523\n" 1520f1f31eb2SQuentin Colombet " call void @llvm.assume(i1 %c)\n" 1521f1f31eb2SQuentin Colombet " ret void\n" 1522f1f31eb2SQuentin Colombet "}\n" 1523f1f31eb2SQuentin Colombet "declare void @llvm.assume(i1)\n"); 1524f1f31eb2SQuentin Colombet AssumptionCache AC(*F); 1525f1f31eb2SQuentin Colombet KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1526f1f31eb2SQuentin Colombet F->front().getTerminator()); 1527f1f31eb2SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), ~(65536llu - 1)); 1528f1f31eb2SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 0u); 1529f1f31eb2SQuentin Colombet Instruction &APlus512 = findInstructionByName(F, "APlus512"); 1530f1f31eb2SQuentin Colombet Known = computeKnownBits(&APlus512, M->getDataLayout(), /* Depth */ 0, &AC, 1531f1f31eb2SQuentin Colombet F->front().getTerminator()); 1532f1f31eb2SQuentin Colombet // We know of one less zero because 512 may have produced a 1 that 1533f1f31eb2SQuentin Colombet // got carried all the way to the first trailing zero. 1534f1f31eb2SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), (~(65536llu - 1)) << 1); 1535f1f31eb2SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 0u); 1536f1f31eb2SQuentin Colombet // The known range is not precise given computeKnownBits works 1537f1f31eb2SQuentin Colombet // with the masks of zeros and ones, not the ranges. 1538f1f31eb2SQuentin Colombet EXPECT_EQ(Known.getMinValue(), 0u); 1539f1f31eb2SQuentin Colombet EXPECT_EQ(Known.getMaxValue(), 131071); 1540f1f31eb2SQuentin Colombet } 1541f1f31eb2SQuentin Colombet 1542f1f31eb2SQuentin Colombet // 512 + [32, 64) doesn't produce overlapping bits. 1543f1f31eb2SQuentin Colombet // Make sure we get all the individual bits properly. 1544f1f31eb2SQuentin Colombet TEST_F(ComputeKnownBitsTest, ComputeKnownBitsAddWithRangeNoOverlap) { 1545f1f31eb2SQuentin Colombet parseAssembly("define void @test(i64* %p) {\n" 1546f1f31eb2SQuentin Colombet " %A = load i64, i64* %p, !range !{i64 32, i64 64}\n" 1547f1f31eb2SQuentin Colombet " %APlus512 = add i64 %A, 512\n" 1548f1f31eb2SQuentin Colombet " %c = icmp ugt i64 %APlus512, 523\n" 1549f1f31eb2SQuentin Colombet " call void @llvm.assume(i1 %c)\n" 1550f1f31eb2SQuentin Colombet " ret void\n" 1551f1f31eb2SQuentin Colombet "}\n" 1552f1f31eb2SQuentin Colombet "declare void @llvm.assume(i1)\n"); 1553f1f31eb2SQuentin Colombet AssumptionCache AC(*F); 1554f1f31eb2SQuentin Colombet KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1555f1f31eb2SQuentin Colombet F->front().getTerminator()); 1556f1f31eb2SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), ~(64llu - 1)); 1557f1f31eb2SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 32u); 1558f1f31eb2SQuentin Colombet Instruction &APlus512 = findInstructionByName(F, "APlus512"); 1559f1f31eb2SQuentin Colombet Known = computeKnownBits(&APlus512, M->getDataLayout(), /* Depth */ 0, &AC, 1560f1f31eb2SQuentin Colombet F->front().getTerminator()); 1561f1f31eb2SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), ~512llu & ~(64llu - 1)); 1562f1f31eb2SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 512u | 32u); 1563f1f31eb2SQuentin Colombet // The known range is not precise given computeKnownBits works 1564f1f31eb2SQuentin Colombet // with the masks of zeros and ones, not the ranges. 1565f1f31eb2SQuentin Colombet EXPECT_EQ(Known.getMinValue(), 544); 1566f1f31eb2SQuentin Colombet EXPECT_EQ(Known.getMaxValue(), 575); 1567f1f31eb2SQuentin Colombet } 1568f1f31eb2SQuentin Colombet 1569ee6abef5SQuentin Colombet TEST_F(ComputeKnownBitsTest, ComputeKnownBitsGEPWithRange) { 1570ee6abef5SQuentin Colombet parseAssembly( 1571ee6abef5SQuentin Colombet "define void @test(i64* %p) {\n" 1572ee6abef5SQuentin Colombet " %A = load i64, i64* %p, !range !{i64 64, i64 65536}\n" 1573ee6abef5SQuentin Colombet " %APtr = inttoptr i64 %A to float*" 1574ee6abef5SQuentin Colombet " %APtrPlus512 = getelementptr float, float* %APtr, i32 128\n" 1575ee6abef5SQuentin Colombet " %c = icmp ugt float* %APtrPlus512, inttoptr (i32 523 to float*)\n" 1576ee6abef5SQuentin Colombet " call void @llvm.assume(i1 %c)\n" 1577ee6abef5SQuentin Colombet " ret void\n" 1578ee6abef5SQuentin Colombet "}\n" 1579ee6abef5SQuentin Colombet "declare void @llvm.assume(i1)\n"); 1580ee6abef5SQuentin Colombet AssumptionCache AC(*F); 1581ee6abef5SQuentin Colombet KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1582ee6abef5SQuentin Colombet F->front().getTerminator()); 1583ee6abef5SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), ~(65536llu - 1)); 1584ee6abef5SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 0u); 1585ee6abef5SQuentin Colombet Instruction &APtrPlus512 = findInstructionByName(F, "APtrPlus512"); 1586ee6abef5SQuentin Colombet Known = computeKnownBits(&APtrPlus512, M->getDataLayout(), /* Depth */ 0, &AC, 1587ee6abef5SQuentin Colombet F->front().getTerminator()); 1588ee6abef5SQuentin Colombet // We know of one less zero because 512 may have produced a 1 that 1589ee6abef5SQuentin Colombet // got carried all the way to the first trailing zero. 1590ee6abef5SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), ~(65536llu - 1) << 1); 1591ee6abef5SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 0u); 1592ee6abef5SQuentin Colombet // The known range is not precise given computeKnownBits works 1593ee6abef5SQuentin Colombet // with the masks of zeros and ones, not the ranges. 1594ee6abef5SQuentin Colombet EXPECT_EQ(Known.getMinValue(), 0u); 1595ee6abef5SQuentin Colombet EXPECT_EQ(Known.getMaxValue(), 131071); 1596ee6abef5SQuentin Colombet } 1597ee6abef5SQuentin Colombet 1598ee6abef5SQuentin Colombet // 4*128 + [32, 64) doesn't produce overlapping bits. 1599ee6abef5SQuentin Colombet // Make sure we get all the individual bits properly. 1600ee6abef5SQuentin Colombet // This test is useful to check that we account for the scaling factor 1601ee6abef5SQuentin Colombet // in the gep. Indeed, gep float, [32,64), 128 is not 128 + [32,64). 1602ee6abef5SQuentin Colombet TEST_F(ComputeKnownBitsTest, ComputeKnownBitsGEPWithRangeNoOverlap) { 1603ee6abef5SQuentin Colombet parseAssembly( 1604ee6abef5SQuentin Colombet "define void @test(i64* %p) {\n" 1605ee6abef5SQuentin Colombet " %A = load i64, i64* %p, !range !{i64 32, i64 64}\n" 1606ee6abef5SQuentin Colombet " %APtr = inttoptr i64 %A to float*" 1607ee6abef5SQuentin Colombet " %APtrPlus512 = getelementptr float, float* %APtr, i32 128\n" 1608ee6abef5SQuentin Colombet " %c = icmp ugt float* %APtrPlus512, inttoptr (i32 523 to float*)\n" 1609ee6abef5SQuentin Colombet " call void @llvm.assume(i1 %c)\n" 1610ee6abef5SQuentin Colombet " ret void\n" 1611ee6abef5SQuentin Colombet "}\n" 1612ee6abef5SQuentin Colombet "declare void @llvm.assume(i1)\n"); 1613ee6abef5SQuentin Colombet AssumptionCache AC(*F); 1614ee6abef5SQuentin Colombet KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1615ee6abef5SQuentin Colombet F->front().getTerminator()); 1616ee6abef5SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), ~(64llu - 1)); 1617ee6abef5SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 32u); 1618ee6abef5SQuentin Colombet Instruction &APtrPlus512 = findInstructionByName(F, "APtrPlus512"); 1619ee6abef5SQuentin Colombet Known = computeKnownBits(&APtrPlus512, M->getDataLayout(), /* Depth */ 0, &AC, 1620ee6abef5SQuentin Colombet F->front().getTerminator()); 1621ee6abef5SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), ~512llu & ~(64llu - 1)); 1622ee6abef5SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 512u | 32u); 1623ee6abef5SQuentin Colombet // The known range is not precise given computeKnownBits works 1624ee6abef5SQuentin Colombet // with the masks of zeros and ones, not the ranges. 1625ee6abef5SQuentin Colombet EXPECT_EQ(Known.getMinValue(), 544); 1626ee6abef5SQuentin Colombet EXPECT_EQ(Known.getMaxValue(), 575); 1627ee6abef5SQuentin Colombet } 1628ee6abef5SQuentin Colombet 1629329d15b5SVitaly Buka class IsBytewiseValueTest : public ValueTrackingTest, 1630329d15b5SVitaly Buka public ::testing::WithParamInterface< 1631329d15b5SVitaly Buka std::pair<const char *, const char *>> { 1632329d15b5SVitaly Buka protected: 1633329d15b5SVitaly Buka }; 1634329d15b5SVitaly Buka 1635329d15b5SVitaly Buka const std::pair<const char *, const char *> IsBytewiseValueTests[] = { 1636329d15b5SVitaly Buka { 1637329d15b5SVitaly Buka "i8 0", 1638329d15b5SVitaly Buka "i48* null", 1639329d15b5SVitaly Buka }, 1640329d15b5SVitaly Buka { 1641329d15b5SVitaly Buka "i8 undef", 1642329d15b5SVitaly Buka "i48* undef", 1643329d15b5SVitaly Buka }, 1644329d15b5SVitaly Buka { 1645329d15b5SVitaly Buka "i8 0", 1646329d15b5SVitaly Buka "i8 zeroinitializer", 1647329d15b5SVitaly Buka }, 1648329d15b5SVitaly Buka { 1649329d15b5SVitaly Buka "i8 0", 1650329d15b5SVitaly Buka "i8 0", 1651329d15b5SVitaly Buka }, 1652329d15b5SVitaly Buka { 1653329d15b5SVitaly Buka "i8 -86", 1654329d15b5SVitaly Buka "i8 -86", 1655329d15b5SVitaly Buka }, 1656329d15b5SVitaly Buka { 1657329d15b5SVitaly Buka "i8 -1", 1658329d15b5SVitaly Buka "i8 -1", 1659329d15b5SVitaly Buka }, 1660329d15b5SVitaly Buka { 1661329d15b5SVitaly Buka "i8 undef", 1662329d15b5SVitaly Buka "i16 undef", 1663329d15b5SVitaly Buka }, 1664329d15b5SVitaly Buka { 1665329d15b5SVitaly Buka "i8 0", 1666329d15b5SVitaly Buka "i16 0", 1667329d15b5SVitaly Buka }, 1668329d15b5SVitaly Buka { 1669329d15b5SVitaly Buka "", 1670329d15b5SVitaly Buka "i16 7", 1671329d15b5SVitaly Buka }, 1672329d15b5SVitaly Buka { 1673329d15b5SVitaly Buka "i8 -86", 1674329d15b5SVitaly Buka "i16 -21846", 1675329d15b5SVitaly Buka }, 1676329d15b5SVitaly Buka { 1677329d15b5SVitaly Buka "i8 -1", 1678329d15b5SVitaly Buka "i16 -1", 1679329d15b5SVitaly Buka }, 1680329d15b5SVitaly Buka { 1681329d15b5SVitaly Buka "i8 0", 1682329d15b5SVitaly Buka "i48 0", 1683329d15b5SVitaly Buka }, 1684329d15b5SVitaly Buka { 1685329d15b5SVitaly Buka "i8 -1", 1686329d15b5SVitaly Buka "i48 -1", 1687329d15b5SVitaly Buka }, 1688329d15b5SVitaly Buka { 1689329d15b5SVitaly Buka "i8 0", 1690329d15b5SVitaly Buka "i49 0", 1691329d15b5SVitaly Buka }, 1692329d15b5SVitaly Buka { 1693329d15b5SVitaly Buka "", 1694329d15b5SVitaly Buka "i49 -1", 1695329d15b5SVitaly Buka }, 1696329d15b5SVitaly Buka { 1697329d15b5SVitaly Buka "i8 0", 1698329d15b5SVitaly Buka "half 0xH0000", 1699329d15b5SVitaly Buka }, 1700329d15b5SVitaly Buka { 1701329d15b5SVitaly Buka "i8 -85", 1702329d15b5SVitaly Buka "half 0xHABAB", 1703329d15b5SVitaly Buka }, 1704329d15b5SVitaly Buka { 1705329d15b5SVitaly Buka "i8 0", 1706329d15b5SVitaly Buka "float 0.0", 1707329d15b5SVitaly Buka }, 1708329d15b5SVitaly Buka { 1709329d15b5SVitaly Buka "i8 -1", 1710329d15b5SVitaly Buka "float 0xFFFFFFFFE0000000", 1711329d15b5SVitaly Buka }, 1712329d15b5SVitaly Buka { 1713329d15b5SVitaly Buka "i8 0", 1714329d15b5SVitaly Buka "double 0.0", 1715329d15b5SVitaly Buka }, 1716329d15b5SVitaly Buka { 1717329d15b5SVitaly Buka "i8 -15", 1718329d15b5SVitaly Buka "double 0xF1F1F1F1F1F1F1F1", 1719329d15b5SVitaly Buka }, 1720329d15b5SVitaly Buka { 1721329d15b5SVitaly Buka "i8 undef", 1722329d15b5SVitaly Buka "i16* undef", 1723329d15b5SVitaly Buka }, 1724329d15b5SVitaly Buka { 1725329d15b5SVitaly Buka "i8 0", 1726329d15b5SVitaly Buka "i16* inttoptr (i64 0 to i16*)", 1727329d15b5SVitaly Buka }, 1728329d15b5SVitaly Buka { 1729c559e637SVitaly Buka "i8 -1", 1730329d15b5SVitaly Buka "i16* inttoptr (i64 -1 to i16*)", 1731329d15b5SVitaly Buka }, 1732329d15b5SVitaly Buka { 1733c559e637SVitaly Buka "i8 -86", 1734329d15b5SVitaly Buka "i16* inttoptr (i64 -6148914691236517206 to i16*)", 1735329d15b5SVitaly Buka }, 1736329d15b5SVitaly Buka { 1737329d15b5SVitaly Buka "", 1738329d15b5SVitaly Buka "i16* inttoptr (i48 -1 to i16*)", 1739329d15b5SVitaly Buka }, 1740329d15b5SVitaly Buka { 1741c559e637SVitaly Buka "i8 -1", 1742329d15b5SVitaly Buka "i16* inttoptr (i96 -1 to i16*)", 1743329d15b5SVitaly Buka }, 1744329d15b5SVitaly Buka { 174552096ee9SVitaly Buka "i8 undef", 1746329d15b5SVitaly Buka "[0 x i8] zeroinitializer", 1747329d15b5SVitaly Buka }, 1748329d15b5SVitaly Buka { 1749329d15b5SVitaly Buka "i8 undef", 1750329d15b5SVitaly Buka "[0 x i8] undef", 1751329d15b5SVitaly Buka }, 1752329d15b5SVitaly Buka { 175352096ee9SVitaly Buka "i8 undef", 1754329d15b5SVitaly Buka "[5 x [0 x i8]] zeroinitializer", 1755329d15b5SVitaly Buka }, 1756329d15b5SVitaly Buka { 1757329d15b5SVitaly Buka "i8 undef", 1758329d15b5SVitaly Buka "[5 x [0 x i8]] undef", 1759329d15b5SVitaly Buka }, 1760329d15b5SVitaly Buka { 1761329d15b5SVitaly Buka "i8 0", 1762329d15b5SVitaly Buka "[6 x i8] zeroinitializer", 1763329d15b5SVitaly Buka }, 1764329d15b5SVitaly Buka { 1765329d15b5SVitaly Buka "i8 undef", 1766329d15b5SVitaly Buka "[6 x i8] undef", 1767329d15b5SVitaly Buka }, 1768329d15b5SVitaly Buka { 1769329d15b5SVitaly Buka "i8 1", 1770329d15b5SVitaly Buka "[5 x i8] [i8 1, i8 1, i8 1, i8 1, i8 1]", 1771329d15b5SVitaly Buka }, 1772329d15b5SVitaly Buka { 1773329d15b5SVitaly Buka "", 1774329d15b5SVitaly Buka "[5 x i64] [i64 1, i64 1, i64 1, i64 1, i64 1]", 1775329d15b5SVitaly Buka }, 1776329d15b5SVitaly Buka { 1777329d15b5SVitaly Buka "i8 -1", 1778329d15b5SVitaly Buka "[5 x i64] [i64 -1, i64 -1, i64 -1, i64 -1, i64 -1]", 1779329d15b5SVitaly Buka }, 1780329d15b5SVitaly Buka { 1781329d15b5SVitaly Buka "", 1782329d15b5SVitaly Buka "[4 x i8] [i8 1, i8 2, i8 1, i8 1]", 1783329d15b5SVitaly Buka }, 1784329d15b5SVitaly Buka { 1785329d15b5SVitaly Buka "i8 1", 1786329d15b5SVitaly Buka "[4 x i8] [i8 1, i8 undef, i8 1, i8 1]", 1787329d15b5SVitaly Buka }, 1788329d15b5SVitaly Buka { 1789329d15b5SVitaly Buka "i8 0", 1790329d15b5SVitaly Buka "<6 x i8> zeroinitializer", 1791329d15b5SVitaly Buka }, 1792329d15b5SVitaly Buka { 1793329d15b5SVitaly Buka "i8 undef", 1794329d15b5SVitaly Buka "<6 x i8> undef", 1795329d15b5SVitaly Buka }, 1796329d15b5SVitaly Buka { 1797329d15b5SVitaly Buka "i8 1", 1798329d15b5SVitaly Buka "<5 x i8> <i8 1, i8 1, i8 1, i8 1, i8 1>", 1799329d15b5SVitaly Buka }, 1800329d15b5SVitaly Buka { 1801329d15b5SVitaly Buka "", 1802329d15b5SVitaly Buka "<5 x i64> <i64 1, i64 1, i64 1, i64 1, i64 1>", 1803329d15b5SVitaly Buka }, 1804329d15b5SVitaly Buka { 1805329d15b5SVitaly Buka "i8 -1", 1806329d15b5SVitaly Buka "<5 x i64> <i64 -1, i64 -1, i64 -1, i64 -1, i64 -1>", 1807329d15b5SVitaly Buka }, 1808329d15b5SVitaly Buka { 1809329d15b5SVitaly Buka "", 1810329d15b5SVitaly Buka "<4 x i8> <i8 1, i8 1, i8 2, i8 1>", 1811329d15b5SVitaly Buka }, 1812329d15b5SVitaly Buka { 1813b1bff76eSVitaly Buka "i8 5", 1814329d15b5SVitaly Buka "<2 x i8> < i8 5, i8 undef >", 1815329d15b5SVitaly Buka }, 1816329d15b5SVitaly Buka { 1817329d15b5SVitaly Buka "i8 0", 1818329d15b5SVitaly Buka "[2 x [2 x i16]] zeroinitializer", 1819329d15b5SVitaly Buka }, 1820329d15b5SVitaly Buka { 1821329d15b5SVitaly Buka "i8 undef", 1822329d15b5SVitaly Buka "[2 x [2 x i16]] undef", 1823329d15b5SVitaly Buka }, 1824329d15b5SVitaly Buka { 1825329d15b5SVitaly Buka "i8 -86", 1826329d15b5SVitaly Buka "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], " 1827329d15b5SVitaly Buka "[2 x i16] [i16 -21846, i16 -21846]]", 1828329d15b5SVitaly Buka }, 1829329d15b5SVitaly Buka { 1830329d15b5SVitaly Buka "", 1831329d15b5SVitaly Buka "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], " 1832329d15b5SVitaly Buka "[2 x i16] [i16 -21836, i16 -21846]]", 1833329d15b5SVitaly Buka }, 1834329d15b5SVitaly Buka { 183552096ee9SVitaly Buka "i8 undef", 1836329d15b5SVitaly Buka "{ } zeroinitializer", 1837329d15b5SVitaly Buka }, 1838329d15b5SVitaly Buka { 1839329d15b5SVitaly Buka "i8 undef", 1840329d15b5SVitaly Buka "{ } undef", 1841329d15b5SVitaly Buka }, 1842329d15b5SVitaly Buka { 184352096ee9SVitaly Buka "i8 undef", 1844329d15b5SVitaly Buka "{ {}, {} } zeroinitializer", 1845329d15b5SVitaly Buka }, 1846329d15b5SVitaly Buka { 1847329d15b5SVitaly Buka "i8 undef", 1848329d15b5SVitaly Buka "{ {}, {} } undef", 1849329d15b5SVitaly Buka }, 1850329d15b5SVitaly Buka { 1851329d15b5SVitaly Buka "i8 0", 1852329d15b5SVitaly Buka "{i8, i64, i16*} zeroinitializer", 1853329d15b5SVitaly Buka }, 1854329d15b5SVitaly Buka { 1855329d15b5SVitaly Buka "i8 undef", 1856329d15b5SVitaly Buka "{i8, i64, i16*} undef", 1857329d15b5SVitaly Buka }, 1858329d15b5SVitaly Buka { 1859329d15b5SVitaly Buka "i8 -86", 1860329d15b5SVitaly Buka "{i8, i64, i16*} {i8 -86, i64 -6148914691236517206, i16* undef}", 1861329d15b5SVitaly Buka }, 1862329d15b5SVitaly Buka { 1863329d15b5SVitaly Buka "", 1864329d15b5SVitaly Buka "{i8, i64, i16*} {i8 86, i64 -6148914691236517206, i16* undef}", 1865329d15b5SVitaly Buka }, 1866329d15b5SVitaly Buka }; 1867329d15b5SVitaly Buka 1868d4d80a29SBenjamin Kramer INSTANTIATE_TEST_SUITE_P(IsBytewiseValueParamTests, IsBytewiseValueTest, 1869d4d80a29SBenjamin Kramer ::testing::ValuesIn(IsBytewiseValueTests)); 1870329d15b5SVitaly Buka 1871329d15b5SVitaly Buka TEST_P(IsBytewiseValueTest, IsBytewiseValue) { 1872329d15b5SVitaly Buka auto M = parseModule(std::string("@test = global ") + GetParam().second); 1873329d15b5SVitaly Buka GlobalVariable *GV = dyn_cast<GlobalVariable>(M->getNamedValue("test")); 1874329d15b5SVitaly Buka Value *Actual = isBytewiseValue(GV->getInitializer(), M->getDataLayout()); 1875329d15b5SVitaly Buka std::string Buff; 1876329d15b5SVitaly Buka raw_string_ostream S(Buff); 1877329d15b5SVitaly Buka if (Actual) 1878329d15b5SVitaly Buka S << *Actual; 1879329d15b5SVitaly Buka EXPECT_EQ(GetParam().first, S.str()); 1880329d15b5SVitaly Buka } 18818d041811SFlorian Hahn 18828d041811SFlorian Hahn TEST_F(ValueTrackingTest, ComputeConstantRange) { 18838d041811SFlorian Hahn { 18848d041811SFlorian Hahn // Assumptions: 18858d041811SFlorian Hahn // * stride >= 5 18868d041811SFlorian Hahn // * stride < 10 18878d041811SFlorian Hahn // 18888d041811SFlorian Hahn // stride = [5, 10) 18898d041811SFlorian Hahn auto M = parseModule(R"( 18908d041811SFlorian Hahn declare void @llvm.assume(i1) 18918d041811SFlorian Hahn 18928d041811SFlorian Hahn define i32 @test(i32 %stride) { 18938d041811SFlorian Hahn %gt = icmp uge i32 %stride, 5 18948d041811SFlorian Hahn call void @llvm.assume(i1 %gt) 18958d041811SFlorian Hahn %lt = icmp ult i32 %stride, 10 18968d041811SFlorian Hahn call void @llvm.assume(i1 %lt) 18978d041811SFlorian Hahn %stride.plus.one = add nsw nuw i32 %stride, 1 18988d041811SFlorian Hahn ret i32 %stride.plus.one 18998d041811SFlorian Hahn })"); 19008d041811SFlorian Hahn Function *F = M->getFunction("test"); 19018d041811SFlorian Hahn 19028d041811SFlorian Hahn AssumptionCache AC(*F); 19038d041811SFlorian Hahn Value *Stride = &*F->arg_begin(); 19048d041811SFlorian Hahn ConstantRange CR1 = computeConstantRange(Stride, true, &AC, nullptr); 19058d041811SFlorian Hahn EXPECT_TRUE(CR1.isFullSet()); 19068d041811SFlorian Hahn 19078d041811SFlorian Hahn Instruction *I = &findInstructionByName(F, "stride.plus.one"); 19088d041811SFlorian Hahn ConstantRange CR2 = computeConstantRange(Stride, true, &AC, I); 19098d041811SFlorian Hahn EXPECT_EQ(5, CR2.getLower()); 19108d041811SFlorian Hahn EXPECT_EQ(10, CR2.getUpper()); 19118d041811SFlorian Hahn } 19128d041811SFlorian Hahn 19138d041811SFlorian Hahn { 19148d041811SFlorian Hahn // Assumptions: 19158d041811SFlorian Hahn // * stride >= 5 19168d041811SFlorian Hahn // * stride < 200 19178d041811SFlorian Hahn // * stride == 99 19188d041811SFlorian Hahn // 19198d041811SFlorian Hahn // stride = [99, 100) 19208d041811SFlorian Hahn auto M = parseModule(R"( 19218d041811SFlorian Hahn declare void @llvm.assume(i1) 19228d041811SFlorian Hahn 19238d041811SFlorian Hahn define i32 @test(i32 %stride) { 19248d041811SFlorian Hahn %gt = icmp uge i32 %stride, 5 19258d041811SFlorian Hahn call void @llvm.assume(i1 %gt) 19268d041811SFlorian Hahn %lt = icmp ult i32 %stride, 200 19278d041811SFlorian Hahn call void @llvm.assume(i1 %lt) 19288d041811SFlorian Hahn %eq = icmp eq i32 %stride, 99 19298d041811SFlorian Hahn call void @llvm.assume(i1 %eq) 19308d041811SFlorian Hahn %stride.plus.one = add nsw nuw i32 %stride, 1 19318d041811SFlorian Hahn ret i32 %stride.plus.one 19328d041811SFlorian Hahn })"); 19338d041811SFlorian Hahn Function *F = M->getFunction("test"); 19348d041811SFlorian Hahn 19358d041811SFlorian Hahn AssumptionCache AC(*F); 19368d041811SFlorian Hahn Value *Stride = &*F->arg_begin(); 19378d041811SFlorian Hahn Instruction *I = &findInstructionByName(F, "stride.plus.one"); 19388d041811SFlorian Hahn ConstantRange CR = computeConstantRange(Stride, true, &AC, I); 19398d041811SFlorian Hahn EXPECT_EQ(99, *CR.getSingleElement()); 19408d041811SFlorian Hahn } 19418d041811SFlorian Hahn 19428d041811SFlorian Hahn { 19438d041811SFlorian Hahn // Assumptions: 19448d041811SFlorian Hahn // * stride >= 5 19458d041811SFlorian Hahn // * stride >= 50 19468d041811SFlorian Hahn // * stride < 100 19478d041811SFlorian Hahn // * stride < 200 19488d041811SFlorian Hahn // 19498d041811SFlorian Hahn // stride = [50, 100) 19508d041811SFlorian Hahn auto M = parseModule(R"( 19518d041811SFlorian Hahn declare void @llvm.assume(i1) 19528d041811SFlorian Hahn 19538d041811SFlorian Hahn define i32 @test(i32 %stride, i1 %cond) { 19548d041811SFlorian Hahn %gt = icmp uge i32 %stride, 5 19558d041811SFlorian Hahn call void @llvm.assume(i1 %gt) 19568d041811SFlorian Hahn %gt.2 = icmp uge i32 %stride, 50 19578d041811SFlorian Hahn call void @llvm.assume(i1 %gt.2) 19588d041811SFlorian Hahn br i1 %cond, label %bb1, label %bb2 19598d041811SFlorian Hahn 19608d041811SFlorian Hahn bb1: 19618d041811SFlorian Hahn %lt = icmp ult i32 %stride, 200 19628d041811SFlorian Hahn call void @llvm.assume(i1 %lt) 19638d041811SFlorian Hahn %lt.2 = icmp ult i32 %stride, 100 19648d041811SFlorian Hahn call void @llvm.assume(i1 %lt.2) 19658d041811SFlorian Hahn %stride.plus.one = add nsw nuw i32 %stride, 1 19668d041811SFlorian Hahn ret i32 %stride.plus.one 19678d041811SFlorian Hahn 19688d041811SFlorian Hahn bb2: 19698d041811SFlorian Hahn ret i32 0 19708d041811SFlorian Hahn })"); 19718d041811SFlorian Hahn Function *F = M->getFunction("test"); 19728d041811SFlorian Hahn 19738d041811SFlorian Hahn AssumptionCache AC(*F); 19748d041811SFlorian Hahn Value *Stride = &*F->arg_begin(); 19758d041811SFlorian Hahn Instruction *GT2 = &findInstructionByName(F, "gt.2"); 19768d041811SFlorian Hahn ConstantRange CR = computeConstantRange(Stride, true, &AC, GT2); 19778d041811SFlorian Hahn EXPECT_EQ(5, CR.getLower()); 19788d041811SFlorian Hahn EXPECT_EQ(0, CR.getUpper()); 19798d041811SFlorian Hahn 19808d041811SFlorian Hahn Instruction *I = &findInstructionByName(F, "stride.plus.one"); 19818d041811SFlorian Hahn ConstantRange CR2 = computeConstantRange(Stride, true, &AC, I); 19828d041811SFlorian Hahn EXPECT_EQ(50, CR2.getLower()); 19838d041811SFlorian Hahn EXPECT_EQ(100, CR2.getUpper()); 19848d041811SFlorian Hahn } 19858d041811SFlorian Hahn 19868d041811SFlorian Hahn { 19878d041811SFlorian Hahn // Assumptions: 19888d041811SFlorian Hahn // * stride > 5 19898d041811SFlorian Hahn // * stride < 5 19908d041811SFlorian Hahn // 19918d041811SFlorian Hahn // stride = empty range, as the assumptions contradict each other. 19928d041811SFlorian Hahn auto M = parseModule(R"( 19938d041811SFlorian Hahn declare void @llvm.assume(i1) 19948d041811SFlorian Hahn 19958d041811SFlorian Hahn define i32 @test(i32 %stride, i1 %cond) { 19968d041811SFlorian Hahn %gt = icmp ugt i32 %stride, 5 19978d041811SFlorian Hahn call void @llvm.assume(i1 %gt) 19988d041811SFlorian Hahn %lt = icmp ult i32 %stride, 5 19998d041811SFlorian Hahn call void @llvm.assume(i1 %lt) 20008d041811SFlorian Hahn %stride.plus.one = add nsw nuw i32 %stride, 1 20018d041811SFlorian Hahn ret i32 %stride.plus.one 20028d041811SFlorian Hahn })"); 20038d041811SFlorian Hahn Function *F = M->getFunction("test"); 20048d041811SFlorian Hahn 20058d041811SFlorian Hahn AssumptionCache AC(*F); 20068d041811SFlorian Hahn Value *Stride = &*F->arg_begin(); 20078d041811SFlorian Hahn 20088d041811SFlorian Hahn Instruction *I = &findInstructionByName(F, "stride.plus.one"); 20098d041811SFlorian Hahn ConstantRange CR = computeConstantRange(Stride, true, &AC, I); 20108d041811SFlorian Hahn EXPECT_TRUE(CR.isEmptySet()); 20118d041811SFlorian Hahn } 20128d041811SFlorian Hahn 20138d041811SFlorian Hahn { 20148d041811SFlorian Hahn // Assumptions: 20158d041811SFlorian Hahn // * x.1 >= 5 20168d041811SFlorian Hahn // * x.2 < x.1 20178d041811SFlorian Hahn // 20188d041811SFlorian Hahn // stride = [0, 5) 20198d041811SFlorian Hahn auto M = parseModule(R"( 20208d041811SFlorian Hahn declare void @llvm.assume(i1) 20218d041811SFlorian Hahn 20228d041811SFlorian Hahn define i32 @test(i32 %x.1, i32 %x.2) { 20238d041811SFlorian Hahn %gt = icmp uge i32 %x.1, 5 20248d041811SFlorian Hahn call void @llvm.assume(i1 %gt) 20258d041811SFlorian Hahn %lt = icmp ult i32 %x.2, %x.1 20268d041811SFlorian Hahn call void @llvm.assume(i1 %lt) 20278d041811SFlorian Hahn %stride.plus.one = add nsw nuw i32 %x.1, 1 20288d041811SFlorian Hahn ret i32 %stride.plus.one 20298d041811SFlorian Hahn })"); 20308d041811SFlorian Hahn Function *F = M->getFunction("test"); 20318d041811SFlorian Hahn 20328d041811SFlorian Hahn AssumptionCache AC(*F); 20338d041811SFlorian Hahn Value *X2 = &*std::next(F->arg_begin()); 20348d041811SFlorian Hahn 20358d041811SFlorian Hahn Instruction *I = &findInstructionByName(F, "stride.plus.one"); 20368d041811SFlorian Hahn ConstantRange CR1 = computeConstantRange(X2, true, &AC, I); 20378d041811SFlorian Hahn EXPECT_EQ(0, CR1.getLower()); 20388d041811SFlorian Hahn EXPECT_EQ(5, CR1.getUpper()); 20398d041811SFlorian Hahn 20408d041811SFlorian Hahn // Check the depth cutoff results in a conservative result (full set) by 20418d041811SFlorian Hahn // passing Depth == MaxDepth == 6. 20428d041811SFlorian Hahn ConstantRange CR2 = computeConstantRange(X2, true, &AC, I, 6); 20438d041811SFlorian Hahn EXPECT_TRUE(CR2.isFullSet()); 20448d041811SFlorian Hahn } 20458d041811SFlorian Hahn } 20467f1bb326SVitaly Buka 20476ccacb46SVitaly Buka struct FindAllocaForValueTestParams { 20486ccacb46SVitaly Buka const char *IR; 2049a6927c86SVitaly Buka bool AnyOffsetResult; 2050a6927c86SVitaly Buka bool ZeroOffsetResult; 20516ccacb46SVitaly Buka }; 20526ccacb46SVitaly Buka 20537f1bb326SVitaly Buka class FindAllocaForValueTest 20547f1bb326SVitaly Buka : public ValueTrackingTest, 20556ccacb46SVitaly Buka public ::testing::WithParamInterface<FindAllocaForValueTestParams> { 20567f1bb326SVitaly Buka protected: 20577f1bb326SVitaly Buka }; 20587f1bb326SVitaly Buka 20596ccacb46SVitaly Buka const FindAllocaForValueTestParams FindAllocaForValueTests[] = { 20607f1bb326SVitaly Buka {R"( 20617f1bb326SVitaly Buka define void @test() { 20627f1bb326SVitaly Buka %a = alloca i64 20637f1bb326SVitaly Buka %r = bitcast i64* %a to i32* 20647f1bb326SVitaly Buka ret void 20657f1bb326SVitaly Buka })", 2066a6927c86SVitaly Buka true, true}, 20677f1bb326SVitaly Buka 20687f1bb326SVitaly Buka {R"( 20697f1bb326SVitaly Buka define void @test() { 20707f1bb326SVitaly Buka %a = alloca i32 20717f1bb326SVitaly Buka %r = getelementptr i32, i32* %a, i32 1 20727f1bb326SVitaly Buka ret void 20737f1bb326SVitaly Buka })", 2074a6927c86SVitaly Buka true, false}, 2075a6927c86SVitaly Buka 2076a6927c86SVitaly Buka {R"( 2077a6927c86SVitaly Buka define void @test() { 2078a6927c86SVitaly Buka %a = alloca i32 2079a6927c86SVitaly Buka %r = getelementptr i32, i32* %a, i32 0 2080a6927c86SVitaly Buka ret void 2081a6927c86SVitaly Buka })", 2082a6927c86SVitaly Buka true, true}, 20837f1bb326SVitaly Buka 20847f1bb326SVitaly Buka {R"( 20857f1bb326SVitaly Buka define void @test(i1 %cond) { 20867f1bb326SVitaly Buka entry: 20877f1bb326SVitaly Buka %a = alloca i32 20887f1bb326SVitaly Buka br label %bb1 20897f1bb326SVitaly Buka 20907f1bb326SVitaly Buka bb1: 20917f1bb326SVitaly Buka %r = phi i32* [ %a, %entry ], [ %r, %bb1 ] 20927f1bb326SVitaly Buka br i1 %cond, label %bb1, label %exit 20937f1bb326SVitaly Buka 20947f1bb326SVitaly Buka exit: 20957f1bb326SVitaly Buka ret void 20967f1bb326SVitaly Buka })", 2097a6927c86SVitaly Buka true, true}, 20987f1bb326SVitaly Buka 20997f1bb326SVitaly Buka {R"( 21007f1bb326SVitaly Buka define void @test(i1 %cond) { 2101469debe0SVitaly Buka %a = alloca i32 2102469debe0SVitaly Buka %r = select i1 %cond, i32* %a, i32* %a 2103469debe0SVitaly Buka ret void 2104469debe0SVitaly Buka })", 2105a6927c86SVitaly Buka true, true}, 2106469debe0SVitaly Buka 2107469debe0SVitaly Buka {R"( 2108469debe0SVitaly Buka define void @test(i1 %cond) { 2109469debe0SVitaly Buka %a = alloca i32 2110469debe0SVitaly Buka %b = alloca i32 2111469debe0SVitaly Buka %r = select i1 %cond, i32* %a, i32* %b 2112469debe0SVitaly Buka ret void 2113469debe0SVitaly Buka })", 2114a6927c86SVitaly Buka false, false}, 2115469debe0SVitaly Buka 2116469debe0SVitaly Buka {R"( 2117469debe0SVitaly Buka define void @test(i1 %cond) { 21187f1bb326SVitaly Buka entry: 21197f1bb326SVitaly Buka %a = alloca i64 21207f1bb326SVitaly Buka %a32 = bitcast i64* %a to i32* 21217f1bb326SVitaly Buka br label %bb1 21227f1bb326SVitaly Buka 21237f1bb326SVitaly Buka bb1: 21247f1bb326SVitaly Buka %x = phi i32* [ %a32, %entry ], [ %x, %bb1 ] 21257f1bb326SVitaly Buka %r = getelementptr i32, i32* %x, i32 1 21267f1bb326SVitaly Buka br i1 %cond, label %bb1, label %exit 21277f1bb326SVitaly Buka 21287f1bb326SVitaly Buka exit: 21297f1bb326SVitaly Buka ret void 21307f1bb326SVitaly Buka })", 2131a6927c86SVitaly Buka true, false}, 21327f1bb326SVitaly Buka 21337f1bb326SVitaly Buka {R"( 21347f1bb326SVitaly Buka define void @test(i1 %cond) { 21357f1bb326SVitaly Buka entry: 21367f1bb326SVitaly Buka %a = alloca i64 21377f1bb326SVitaly Buka %a32 = bitcast i64* %a to i32* 21387f1bb326SVitaly Buka br label %bb1 21397f1bb326SVitaly Buka 21407f1bb326SVitaly Buka bb1: 21417f1bb326SVitaly Buka %x = phi i32* [ %a32, %entry ], [ %r, %bb1 ] 21427f1bb326SVitaly Buka %r = getelementptr i32, i32* %x, i32 1 21437f1bb326SVitaly Buka br i1 %cond, label %bb1, label %exit 21447f1bb326SVitaly Buka 21457f1bb326SVitaly Buka exit: 21467f1bb326SVitaly Buka ret void 21477f1bb326SVitaly Buka })", 214823524fdeSVitaly Buka true, false}, 21497f1bb326SVitaly Buka 21507f1bb326SVitaly Buka {R"( 21517f1bb326SVitaly Buka define void @test(i1 %cond, i64* %a) { 21527f1bb326SVitaly Buka entry: 21537f1bb326SVitaly Buka %r = bitcast i64* %a to i32* 21547f1bb326SVitaly Buka ret void 21557f1bb326SVitaly Buka })", 2156a6927c86SVitaly Buka false, false}, 21577f1bb326SVitaly Buka 21587f1bb326SVitaly Buka {R"( 21597f1bb326SVitaly Buka define void @test(i1 %cond) { 21607f1bb326SVitaly Buka entry: 21617f1bb326SVitaly Buka %a = alloca i32 21627f1bb326SVitaly Buka %b = alloca i32 21637f1bb326SVitaly Buka br label %bb1 21647f1bb326SVitaly Buka 21657f1bb326SVitaly Buka bb1: 21668191603dSVitaly Buka %r = phi i32* [ %a, %entry ], [ %b, %bb1 ] 21677f1bb326SVitaly Buka br i1 %cond, label %bb1, label %exit 21687f1bb326SVitaly Buka 21697f1bb326SVitaly Buka exit: 21707f1bb326SVitaly Buka ret void 21717f1bb326SVitaly Buka })", 2172a6927c86SVitaly Buka false, false}, 21737f1bb326SVitaly Buka }; 21747f1bb326SVitaly Buka 21757f1bb326SVitaly Buka TEST_P(FindAllocaForValueTest, findAllocaForValue) { 21766ccacb46SVitaly Buka auto M = parseModule(GetParam().IR); 21777f1bb326SVitaly Buka Function *F = M->getFunction("test"); 21787f1bb326SVitaly Buka Instruction *I = &findInstructionByName(F, "r"); 21797f1bb326SVitaly Buka const AllocaInst *AI = findAllocaForValue(I); 2180a6927c86SVitaly Buka EXPECT_EQ(!!AI, GetParam().AnyOffsetResult); 2181a6927c86SVitaly Buka } 2182a6927c86SVitaly Buka 2183a6927c86SVitaly Buka TEST_P(FindAllocaForValueTest, findAllocaForValueZeroOffset) { 2184a6927c86SVitaly Buka auto M = parseModule(GetParam().IR); 2185a6927c86SVitaly Buka Function *F = M->getFunction("test"); 2186a6927c86SVitaly Buka Instruction *I = &findInstructionByName(F, "r"); 2187a6927c86SVitaly Buka const AllocaInst *AI = findAllocaForValue(I, true); 2188a6927c86SVitaly Buka EXPECT_EQ(!!AI, GetParam().ZeroOffsetResult); 21897f1bb326SVitaly Buka } 21907f1bb326SVitaly Buka 2191d4d80a29SBenjamin Kramer INSTANTIATE_TEST_SUITE_P(FindAllocaForValueTest, FindAllocaForValueTest, 2192d4d80a29SBenjamin Kramer ::testing::ValuesIn(FindAllocaForValueTests)); 2193