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 " 6173bb2dbd6SSanjoy Das "declare void @throws_but_readonly(i32*) readonly " 6183bb2dbd6SSanjoy Das "declare void @throws_but_argmemonly(i32*) argmemonly " 6196ed459fdSJohannes Doerfert "declare void @nounwind_willreturn(i32*) nounwind willreturn" 6203bb2dbd6SSanjoy Das " " 6213bb2dbd6SSanjoy Das "declare void @unknown(i32*) " 6223bb2dbd6SSanjoy Das " " 6233bb2dbd6SSanjoy Das "define void @f(i32* %p) { " 6243bb2dbd6SSanjoy Das " call void @nounwind_readonly(i32* %p) " 6253bb2dbd6SSanjoy Das " call void @nounwind_argmemonly(i32* %p) " 6263bb2dbd6SSanjoy Das " call void @throws_but_readonly(i32* %p) " 6273bb2dbd6SSanjoy Das " call void @throws_but_argmemonly(i32* %p) " 6283bb2dbd6SSanjoy Das " call void @unknown(i32* %p) nounwind readonly " 6293bb2dbd6SSanjoy Das " call void @unknown(i32* %p) nounwind argmemonly " 6303bb2dbd6SSanjoy Das " call void @unknown(i32* %p) readonly " 6313bb2dbd6SSanjoy Das " call void @unknown(i32* %p) argmemonly " 6326ed459fdSJohannes Doerfert " call void @nounwind_willreturn(i32* %p)" 6333bb2dbd6SSanjoy Das " ret void " 6343bb2dbd6SSanjoy Das "} "; 6353bb2dbd6SSanjoy Das 6363bb2dbd6SSanjoy Das LLVMContext Context; 6373bb2dbd6SSanjoy Das SMDiagnostic Error; 6383bb2dbd6SSanjoy Das auto M = parseAssemblyString(Assembly, Error, Context); 6393bb2dbd6SSanjoy Das assert(M && "Bad assembly?"); 6403bb2dbd6SSanjoy Das 6413bb2dbd6SSanjoy Das auto *F = M->getFunction("f"); 6423bb2dbd6SSanjoy Das assert(F && "Bad assembly?"); 6433bb2dbd6SSanjoy Das 6443bb2dbd6SSanjoy Das auto &BB = F->getEntryBlock(); 6458580cd4eSCraig Topper bool ExpectedAnswers[] = { 6463bb2dbd6SSanjoy Das true, // call void @nounwind_readonly(i32* %p) 6473bb2dbd6SSanjoy Das true, // call void @nounwind_argmemonly(i32* %p) 6483bb2dbd6SSanjoy Das false, // call void @throws_but_readonly(i32* %p) 6493bb2dbd6SSanjoy Das false, // call void @throws_but_argmemonly(i32* %p) 6503bb2dbd6SSanjoy Das true, // call void @unknown(i32* %p) nounwind readonly 6513bb2dbd6SSanjoy Das true, // call void @unknown(i32* %p) nounwind argmemonly 6523bb2dbd6SSanjoy Das false, // call void @unknown(i32* %p) readonly 6533bb2dbd6SSanjoy Das false, // call void @unknown(i32* %p) argmemonly 6546ed459fdSJohannes Doerfert true, // call void @nounwind_willreturn(i32* %p) 6553bb2dbd6SSanjoy Das false, // ret void 6563bb2dbd6SSanjoy Das }; 6573bb2dbd6SSanjoy Das 6583bb2dbd6SSanjoy Das int Index = 0; 6593bb2dbd6SSanjoy Das for (auto &I : BB) { 6603bb2dbd6SSanjoy Das EXPECT_EQ(isGuaranteedToTransferExecutionToSuccessor(&I), 6613bb2dbd6SSanjoy Das ExpectedAnswers[Index]) 6623bb2dbd6SSanjoy Das << "Incorrect answer at instruction " << Index << " = " << I; 6633bb2dbd6SSanjoy Das Index++; 6643bb2dbd6SSanjoy Das } 6653bb2dbd6SSanjoy Das } 66639a684d1SSanjoy Das 667219e5367SNikita Popov TEST_F(ValueTrackingTest, ComputeNumSignBits_PR32045) { 668219e5367SNikita Popov parseAssembly( 669219e5367SNikita Popov "define i32 @test(i32 %a) {\n" 670219e5367SNikita Popov " %A = ashr i32 %a, -1\n" 671219e5367SNikita Popov " ret i32 %A\n" 672219e5367SNikita Popov "}\n"); 673219e5367SNikita Popov EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u); 67439a684d1SSanjoy Das } 67570dbd5fbSSimon Dardis 676a68096c7SSanjay Patel // No guarantees for canonical IR in this analysis, so this just bails out. 677219e5367SNikita Popov TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle) { 678219e5367SNikita Popov parseAssembly( 679219e5367SNikita Popov "define <2 x i32> @test() {\n" 680219e5367SNikita Popov " %A = shufflevector <2 x i32> undef, <2 x i32> undef, <2 x i32> <i32 0, i32 0>\n" 681219e5367SNikita Popov " ret <2 x i32> %A\n" 682219e5367SNikita Popov "}\n"); 683219e5367SNikita Popov EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u); 684a68096c7SSanjay Patel } 685a68096c7SSanjay Patel 68645a148deSSanjay Patel // No guarantees for canonical IR in this analysis, so a shuffle element that 68745a148deSSanjay Patel // references an undef value means this can't return any extra information. 688219e5367SNikita Popov TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle2) { 689219e5367SNikita Popov parseAssembly( 690219e5367SNikita Popov "define <2 x i32> @test(<2 x i1> %x) {\n" 691219e5367SNikita Popov " %sext = sext <2 x i1> %x to <2 x i32>\n" 692219e5367SNikita Popov " %A = shufflevector <2 x i32> %sext, <2 x i32> undef, <2 x i32> <i32 0, i32 2>\n" 693219e5367SNikita Popov " ret <2 x i32> %A\n" 694219e5367SNikita Popov "}\n"); 695219e5367SNikita Popov EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u); 696994543abSJuneyoung Lee } 697994543abSJuneyoung Lee 6980f2c1801SJuneyoung Lee TEST_F(ValueTrackingTest, impliesPoisonTest_Identity) { 6990f2c1801SJuneyoung Lee parseAssembly("define void @test(i32 %x, i32 %y) {\n" 7000f2c1801SJuneyoung Lee " %A = add i32 %x, %y\n" 7010f2c1801SJuneyoung Lee " ret void\n" 7020f2c1801SJuneyoung Lee "}"); 7030f2c1801SJuneyoung Lee EXPECT_TRUE(impliesPoison(A, A)); 7040f2c1801SJuneyoung Lee } 7050f2c1801SJuneyoung Lee 7060f2c1801SJuneyoung Lee TEST_F(ValueTrackingTest, impliesPoisonTest_ICmp) { 7070f2c1801SJuneyoung Lee parseAssembly("define void @test(i32 %x) {\n" 7080f2c1801SJuneyoung Lee " %A2 = icmp eq i32 %x, 0\n" 7090f2c1801SJuneyoung Lee " %A = icmp eq i32 %x, 1\n" 7100f2c1801SJuneyoung Lee " ret void\n" 7110f2c1801SJuneyoung Lee "}"); 7120f2c1801SJuneyoung Lee EXPECT_TRUE(impliesPoison(A2, A)); 7130f2c1801SJuneyoung Lee } 7140f2c1801SJuneyoung Lee 7150f2c1801SJuneyoung Lee TEST_F(ValueTrackingTest, impliesPoisonTest_ICmpUnknown) { 7160f2c1801SJuneyoung Lee parseAssembly("define void @test(i32 %x, i32 %y) {\n" 7170f2c1801SJuneyoung Lee " %A2 = icmp eq i32 %x, %y\n" 7180f2c1801SJuneyoung Lee " %A = icmp eq i32 %x, 1\n" 7190f2c1801SJuneyoung Lee " ret void\n" 7200f2c1801SJuneyoung Lee "}"); 7210f2c1801SJuneyoung Lee EXPECT_FALSE(impliesPoison(A2, A)); 7220f2c1801SJuneyoung Lee } 7230f2c1801SJuneyoung Lee 7240f2c1801SJuneyoung Lee TEST_F(ValueTrackingTest, impliesPoisonTest_AddNswOkay) { 7250f2c1801SJuneyoung Lee parseAssembly("define void @test(i32 %x) {\n" 7260f2c1801SJuneyoung Lee " %A2 = add nsw i32 %x, 1\n" 7270f2c1801SJuneyoung Lee " %A = add i32 %A2, 1\n" 7280f2c1801SJuneyoung Lee " ret void\n" 7290f2c1801SJuneyoung Lee "}"); 7300f2c1801SJuneyoung Lee EXPECT_TRUE(impliesPoison(A2, A)); 7310f2c1801SJuneyoung Lee } 7320f2c1801SJuneyoung Lee 7330f2c1801SJuneyoung Lee TEST_F(ValueTrackingTest, impliesPoisonTest_AddNswOkay2) { 7340f2c1801SJuneyoung Lee parseAssembly("define void @test(i32 %x) {\n" 7350f2c1801SJuneyoung Lee " %A2 = add i32 %x, 1\n" 7360f2c1801SJuneyoung Lee " %A = add nsw i32 %A2, 1\n" 7370f2c1801SJuneyoung Lee " ret void\n" 7380f2c1801SJuneyoung Lee "}"); 7390f2c1801SJuneyoung Lee EXPECT_TRUE(impliesPoison(A2, A)); 7400f2c1801SJuneyoung Lee } 7410f2c1801SJuneyoung Lee 7420f2c1801SJuneyoung Lee TEST_F(ValueTrackingTest, impliesPoisonTest_AddNsw) { 7430f2c1801SJuneyoung Lee parseAssembly("define void @test(i32 %x) {\n" 7440f2c1801SJuneyoung Lee " %A2 = add nsw i32 %x, 1\n" 7450f2c1801SJuneyoung Lee " %A = add i32 %x, 1\n" 7460f2c1801SJuneyoung Lee " ret void\n" 7470f2c1801SJuneyoung Lee "}"); 7480f2c1801SJuneyoung Lee EXPECT_FALSE(impliesPoison(A2, A)); 7490f2c1801SJuneyoung Lee } 7500f2c1801SJuneyoung Lee 7518351f9b5SNikita Popov TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle_Pointers) { 7528351f9b5SNikita Popov parseAssembly( 7538351f9b5SNikita Popov "define <2 x i32*> @test(<2 x i32*> %x) {\n" 7548351f9b5SNikita Popov " %A = shufflevector <2 x i32*> zeroinitializer, <2 x i32*> undef, <2 x i32> zeroinitializer\n" 7558351f9b5SNikita Popov " ret <2 x i32*> %A\n" 7568351f9b5SNikita Popov "}\n"); 7578351f9b5SNikita Popov EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 64u); 7588351f9b5SNikita Popov } 7598351f9b5SNikita Popov 760e5f602d8SJuneyoung Lee TEST(ValueTracking, propagatesPoison) { 761e5f602d8SJuneyoung Lee std::string AsmHead = "declare i32 @g(i32)\n" 762e5f602d8SJuneyoung Lee "define void @f(i32 %x, i32 %y, float %fx, float %fy, " 763e5f602d8SJuneyoung Lee "i1 %cond, i8* %p) {\n"; 764e5f602d8SJuneyoung Lee std::string AsmTail = " ret void\n}"; 765e5f602d8SJuneyoung Lee // (propagates poison?, IR instruction) 766e5f602d8SJuneyoung Lee SmallVector<std::pair<bool, std::string>, 32> Data = { 767e5f602d8SJuneyoung Lee {true, "add i32 %x, %y"}, 768e5f602d8SJuneyoung Lee {true, "add nsw nuw i32 %x, %y"}, 769e5f602d8SJuneyoung Lee {true, "ashr i32 %x, %y"}, 770e5f602d8SJuneyoung Lee {true, "lshr exact i32 %x, 31"}, 771e5f602d8SJuneyoung Lee {true, "fcmp oeq float %fx, %fy"}, 772e5f602d8SJuneyoung Lee {true, "icmp eq i32 %x, %y"}, 773e5f602d8SJuneyoung Lee {true, "getelementptr i8, i8* %p, i32 %x"}, 774e5f602d8SJuneyoung Lee {true, "getelementptr inbounds i8, i8* %p, i32 %x"}, 775e5f602d8SJuneyoung Lee {true, "bitcast float %fx to i32"}, 776e5f602d8SJuneyoung Lee {false, "select i1 %cond, i32 %x, i32 %y"}, 777e5f602d8SJuneyoung Lee {false, "freeze i32 %x"}, 778e5f602d8SJuneyoung Lee {true, "udiv i32 %x, %y"}, 779e5f602d8SJuneyoung Lee {true, "urem i32 %x, %y"}, 780e5f602d8SJuneyoung Lee {true, "sdiv exact i32 %x, %y"}, 781e5f602d8SJuneyoung Lee {true, "srem i32 %x, %y"}, 782e5f602d8SJuneyoung Lee {false, "call i32 @g(i32 %x)"}}; 783e5f602d8SJuneyoung Lee 784e5f602d8SJuneyoung Lee std::string AssemblyStr = AsmHead; 785e5f602d8SJuneyoung Lee for (auto &Itm : Data) 786e5f602d8SJuneyoung Lee AssemblyStr += Itm.second + "\n"; 787e5f602d8SJuneyoung Lee AssemblyStr += AsmTail; 788e5f602d8SJuneyoung Lee 789e5f602d8SJuneyoung Lee LLVMContext Context; 790e5f602d8SJuneyoung Lee SMDiagnostic Error; 791e5f602d8SJuneyoung Lee auto M = parseAssemblyString(AssemblyStr, Error, Context); 792e5f602d8SJuneyoung Lee assert(M && "Bad assembly?"); 793e5f602d8SJuneyoung Lee 794e5f602d8SJuneyoung Lee auto *F = M->getFunction("f"); 795e5f602d8SJuneyoung Lee assert(F && "Bad assembly?"); 796e5f602d8SJuneyoung Lee 797e5f602d8SJuneyoung Lee auto &BB = F->getEntryBlock(); 798e5f602d8SJuneyoung Lee 799e5f602d8SJuneyoung Lee int Index = 0; 800e5f602d8SJuneyoung Lee for (auto &I : BB) { 801e5f602d8SJuneyoung Lee if (isa<ReturnInst>(&I)) 802e5f602d8SJuneyoung Lee break; 80325ce1e04SJuneyoung Lee EXPECT_EQ(propagatesPoison(cast<Operator>(&I)), Data[Index].first) 804e5f602d8SJuneyoung Lee << "Incorrect answer at instruction " << Index << " = " << I; 805e5f602d8SJuneyoung Lee Index++; 806e5f602d8SJuneyoung Lee } 807e5f602d8SJuneyoung Lee } 808e5f602d8SJuneyoung Lee 80925ce1e04SJuneyoung Lee TEST_F(ValueTrackingTest, programUndefinedIfPoison) { 81025ce1e04SJuneyoung Lee parseAssembly("declare i32 @any_num()" 81125ce1e04SJuneyoung Lee "define void @test(i32 %mask) {\n" 81225ce1e04SJuneyoung Lee " %A = call i32 @any_num()\n" 81325ce1e04SJuneyoung Lee " %B = or i32 %A, %mask\n" 81425ce1e04SJuneyoung Lee " udiv i32 1, %B" 81525ce1e04SJuneyoung Lee " ret void\n" 81625ce1e04SJuneyoung Lee "}\n"); 81725ce1e04SJuneyoung Lee // If %A was poison, udiv raises UB regardless of %mask's value 81825ce1e04SJuneyoung Lee EXPECT_EQ(programUndefinedIfPoison(A), true); 81925ce1e04SJuneyoung Lee } 82025ce1e04SJuneyoung Lee 82125ce1e04SJuneyoung Lee TEST_F(ValueTrackingTest, programUndefinedIfUndefOrPoison) { 82225ce1e04SJuneyoung Lee parseAssembly("declare i32 @any_num()" 82325ce1e04SJuneyoung Lee "define void @test(i32 %mask) {\n" 82425ce1e04SJuneyoung Lee " %A = call i32 @any_num()\n" 82525ce1e04SJuneyoung Lee " %B = or i32 %A, %mask\n" 82625ce1e04SJuneyoung Lee " udiv i32 1, %B" 82725ce1e04SJuneyoung Lee " ret void\n" 82825ce1e04SJuneyoung Lee "}\n"); 82925ce1e04SJuneyoung Lee // If %A was undef and %mask was 1, udiv does not raise UB 83025ce1e04SJuneyoung Lee EXPECT_EQ(programUndefinedIfUndefOrPoison(A), false); 83125ce1e04SJuneyoung Lee } 83225ce1e04SJuneyoung Lee 83325ce1e04SJuneyoung Lee TEST_F(ValueTrackingTest, isGuaranteedNotToBePoison_exploitBranchCond) { 83425ce1e04SJuneyoung Lee parseAssembly("declare i1 @any_bool()" 83525ce1e04SJuneyoung Lee "define void @test(i1 %y) {\n" 83625ce1e04SJuneyoung Lee " %A = call i1 @any_bool()\n" 83725ce1e04SJuneyoung Lee " %cond = and i1 %A, %y\n" 83825ce1e04SJuneyoung Lee " br i1 %cond, label %BB1, label %BB2\n" 83925ce1e04SJuneyoung Lee "BB1:\n" 84025ce1e04SJuneyoung Lee " ret void\n" 84125ce1e04SJuneyoung Lee "BB2:\n" 84225ce1e04SJuneyoung Lee " ret void\n" 84325ce1e04SJuneyoung Lee "}\n"); 84425ce1e04SJuneyoung Lee DominatorTree DT(*F); 84525ce1e04SJuneyoung Lee for (auto &BB : *F) { 84625ce1e04SJuneyoung Lee if (&BB == &F->getEntryBlock()) 84725ce1e04SJuneyoung Lee continue; 84825ce1e04SJuneyoung Lee 8499b3c2a72SJuneyoung Lee EXPECT_EQ(isGuaranteedNotToBePoison(A, nullptr, BB.getTerminator(), &DT), 8509b3c2a72SJuneyoung Lee true) 85125ce1e04SJuneyoung Lee << "isGuaranteedNotToBePoison does not hold at " << *BB.getTerminator(); 85225ce1e04SJuneyoung Lee } 85325ce1e04SJuneyoung Lee } 85425ce1e04SJuneyoung Lee 855ba8911d5SJuneyoung Lee TEST_F(ValueTrackingTest, isGuaranteedNotToBePoison_phi) { 856ba8911d5SJuneyoung Lee parseAssembly("declare i32 @any_i32(i32)" 857ba8911d5SJuneyoung Lee "define void @test() {\n" 858ba8911d5SJuneyoung Lee "ENTRY:\n" 859ba8911d5SJuneyoung Lee " br label %LOOP\n" 860ba8911d5SJuneyoung Lee "LOOP:\n" 861ba8911d5SJuneyoung Lee " %A = phi i32 [0, %ENTRY], [%A.next, %NEXT]\n" 862ba8911d5SJuneyoung Lee " %A.next = call i32 @any_i32(i32 %A)\n" 863ba8911d5SJuneyoung Lee " %cond = icmp eq i32 %A.next, 0\n" 864ba8911d5SJuneyoung Lee " br i1 %cond, label %NEXT, label %EXIT\n" 865ba8911d5SJuneyoung Lee "NEXT:\n" 866ba8911d5SJuneyoung Lee " br label %LOOP\n" 867ba8911d5SJuneyoung Lee "EXIT:\n" 868ba8911d5SJuneyoung Lee " ret void\n" 869ba8911d5SJuneyoung Lee "}\n"); 870ba8911d5SJuneyoung Lee DominatorTree DT(*F); 871ba8911d5SJuneyoung Lee for (auto &BB : *F) { 872ba8911d5SJuneyoung Lee if (BB.getName() == "LOOP") { 8739b3c2a72SJuneyoung Lee EXPECT_EQ(isGuaranteedNotToBePoison(A, nullptr, A, &DT), true) 874ba8911d5SJuneyoung Lee << "isGuaranteedNotToBePoison does not hold"; 875ba8911d5SJuneyoung Lee } 876ba8911d5SJuneyoung Lee } 877ba8911d5SJuneyoung Lee } 878ba8911d5SJuneyoung Lee 8791c452200SJuneyoung Lee TEST_F(ValueTrackingTest, isGuaranteedNotToBeUndefOrPoison) { 8801c452200SJuneyoung Lee parseAssembly("declare void @f(i32 noundef)" 8811c452200SJuneyoung Lee "define void @test(i32 %x) {\n" 8821c452200SJuneyoung Lee " %A = bitcast i32 %x to i32\n" 8831c452200SJuneyoung Lee " call void @f(i32 noundef %x)\n" 8841c452200SJuneyoung Lee " ret void\n" 8851c452200SJuneyoung Lee "}\n"); 8861c452200SJuneyoung Lee EXPECT_EQ(isGuaranteedNotToBeUndefOrPoison(A), true); 887*abbef2fdSJuneyoung Lee EXPECT_EQ(isGuaranteedNotToBeUndefOrPoison(UndefValue::get(IntegerType::get(Context, 8))), false); 888*abbef2fdSJuneyoung Lee EXPECT_EQ(isGuaranteedNotToBeUndefOrPoison(PoisonValue::get(IntegerType::get(Context, 8))), false); 889*abbef2fdSJuneyoung Lee EXPECT_EQ(isGuaranteedNotToBePoison(UndefValue::get(IntegerType::get(Context, 8))), true); 890*abbef2fdSJuneyoung Lee EXPECT_EQ(isGuaranteedNotToBePoison(PoisonValue::get(IntegerType::get(Context, 8))), false); 8911c452200SJuneyoung Lee } 8921c452200SJuneyoung Lee 8939b3c2a72SJuneyoung Lee TEST_F(ValueTrackingTest, isGuaranteedNotToBeUndefOrPoison_assume) { 8949b3c2a72SJuneyoung Lee parseAssembly("declare i1 @f_i1()\n" 8959b3c2a72SJuneyoung Lee "declare i32 @f_i32()\n" 8969b3c2a72SJuneyoung Lee "declare void @llvm.assume(i1)\n" 8979b3c2a72SJuneyoung Lee "define void @test() {\n" 8989b3c2a72SJuneyoung Lee " %A = call i32 @f_i32()\n" 8999b3c2a72SJuneyoung Lee " %cond = call i1 @f_i1()\n" 9009b3c2a72SJuneyoung Lee " %CxtI = add i32 0, 0\n" 9019b3c2a72SJuneyoung Lee " br i1 %cond, label %BB1, label %EXIT\n" 9029b3c2a72SJuneyoung Lee "BB1:\n" 9039b3c2a72SJuneyoung Lee " %CxtI2 = add i32 0, 0\n" 9049b3c2a72SJuneyoung Lee " %cond2 = call i1 @f_i1()\n" 9059b3c2a72SJuneyoung Lee " call void @llvm.assume(i1 true) [ \"noundef\"(i32 %A) ]\n" 9069b3c2a72SJuneyoung Lee " br i1 %cond2, label %BB2, label %EXIT\n" 9079b3c2a72SJuneyoung Lee "BB2:\n" 9089b3c2a72SJuneyoung Lee " %CxtI3 = add i32 0, 0\n" 9099b3c2a72SJuneyoung Lee " ret void\n" 9109b3c2a72SJuneyoung Lee "EXIT:\n" 9119b3c2a72SJuneyoung Lee " ret void\n" 9129b3c2a72SJuneyoung Lee "}"); 9139b3c2a72SJuneyoung Lee AssumptionCache AC(*F); 9149b3c2a72SJuneyoung Lee DominatorTree DT(*F); 9159b3c2a72SJuneyoung Lee EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(A, &AC, CxtI, &DT)); 9169b3c2a72SJuneyoung Lee EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(A, &AC, CxtI2, &DT)); 9179b3c2a72SJuneyoung Lee EXPECT_TRUE(isGuaranteedNotToBeUndefOrPoison(A, &AC, CxtI3, &DT)); 9189b3c2a72SJuneyoung Lee } 9199b3c2a72SJuneyoung Lee 9200a6aee51SJuneyoung Lee TEST(ValueTracking, canCreatePoisonOrUndef) { 921994543abSJuneyoung Lee std::string AsmHead = 922994543abSJuneyoung Lee "declare i32 @g(i32)\n" 923994543abSJuneyoung Lee "define void @f(i32 %x, i32 %y, float %fx, float %fy, i1 %cond, " 924994543abSJuneyoung Lee "<4 x i32> %vx, <4 x i32> %vx2, <vscale x 4 x i32> %svx, i8* %p) {\n"; 925994543abSJuneyoung Lee std::string AsmTail = " ret void\n}"; 9260a6aee51SJuneyoung Lee // (can create poison?, can create undef?, IR instruction) 927ef66e3d0SJuneyoung Lee SmallVector<std::pair<std::pair<bool, bool>, std::string>, 32> Data = { 928ef66e3d0SJuneyoung Lee {{false, false}, "add i32 %x, %y"}, 929ef66e3d0SJuneyoung Lee {{true, false}, "add nsw nuw i32 %x, %y"}, 930ef66e3d0SJuneyoung Lee {{true, false}, "shl i32 %x, %y"}, 931ef66e3d0SJuneyoung Lee {{true, false}, "shl <4 x i32> %vx, %vx2"}, 932ef66e3d0SJuneyoung Lee {{true, false}, "shl nsw i32 %x, %y"}, 933ef66e3d0SJuneyoung Lee {{true, false}, "shl nsw <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"}, 934ef66e3d0SJuneyoung Lee {{false, false}, "shl i32 %x, 31"}, 935ef66e3d0SJuneyoung Lee {{true, false}, "shl i32 %x, 32"}, 936ef66e3d0SJuneyoung Lee {{false, false}, "shl <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"}, 937ef66e3d0SJuneyoung Lee {{true, false}, "shl <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 32>"}, 938ef66e3d0SJuneyoung Lee {{true, false}, "ashr i32 %x, %y"}, 939ef66e3d0SJuneyoung Lee {{true, false}, "ashr exact i32 %x, %y"}, 940ef66e3d0SJuneyoung Lee {{false, false}, "ashr i32 %x, 31"}, 941ef66e3d0SJuneyoung Lee {{true, false}, "ashr exact i32 %x, 31"}, 942ef66e3d0SJuneyoung Lee {{false, false}, "ashr <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"}, 943ef66e3d0SJuneyoung Lee {{true, false}, "ashr <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 32>"}, 944ef66e3d0SJuneyoung Lee {{true, false}, "ashr exact <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"}, 945ef66e3d0SJuneyoung Lee {{true, false}, "lshr i32 %x, %y"}, 946ef66e3d0SJuneyoung Lee {{true, false}, "lshr exact i32 %x, 31"}, 947ef66e3d0SJuneyoung Lee {{false, false}, "udiv i32 %x, %y"}, 948ef66e3d0SJuneyoung Lee {{true, false}, "udiv exact i32 %x, %y"}, 949ef66e3d0SJuneyoung Lee {{false, false}, "getelementptr i8, i8* %p, i32 %x"}, 950ef66e3d0SJuneyoung Lee {{true, false}, "getelementptr inbounds i8, i8* %p, i32 %x"}, 951ef66e3d0SJuneyoung Lee {{true, false}, "fneg nnan float %fx"}, 952ef66e3d0SJuneyoung Lee {{false, false}, "fneg float %fx"}, 953ef66e3d0SJuneyoung Lee {{false, false}, "fadd float %fx, %fy"}, 954ef66e3d0SJuneyoung Lee {{true, false}, "fadd nnan float %fx, %fy"}, 955ef66e3d0SJuneyoung Lee {{false, false}, "urem i32 %x, %y"}, 956ef66e3d0SJuneyoung Lee {{true, false}, "fptoui float %fx to i32"}, 957ef66e3d0SJuneyoung Lee {{true, false}, "fptosi float %fx to i32"}, 958ef66e3d0SJuneyoung Lee {{false, false}, "bitcast float %fx to i32"}, 959ef66e3d0SJuneyoung Lee {{false, false}, "select i1 %cond, i32 %x, i32 %y"}, 960ef66e3d0SJuneyoung Lee {{true, false}, "select nnan i1 %cond, float %fx, float %fy"}, 961ef66e3d0SJuneyoung Lee {{true, false}, "extractelement <4 x i32> %vx, i32 %x"}, 962ef66e3d0SJuneyoung Lee {{false, false}, "extractelement <4 x i32> %vx, i32 3"}, 963ef66e3d0SJuneyoung Lee {{true, false}, "extractelement <vscale x 4 x i32> %svx, i32 4"}, 964ef66e3d0SJuneyoung Lee {{true, false}, "insertelement <4 x i32> %vx, i32 %x, i32 %y"}, 965ef66e3d0SJuneyoung Lee {{false, false}, "insertelement <4 x i32> %vx, i32 %x, i32 3"}, 966ef66e3d0SJuneyoung Lee {{true, false}, "insertelement <vscale x 4 x i32> %svx, i32 %x, i32 4"}, 967ef66e3d0SJuneyoung Lee {{false, false}, "freeze i32 %x"}, 968ef66e3d0SJuneyoung Lee {{false, false}, 9690a6aee51SJuneyoung Lee "shufflevector <4 x i32> %vx, <4 x i32> %vx2, " 9700a6aee51SJuneyoung Lee "<4 x i32> <i32 0, i32 1, i32 2, i32 3>"}, 971ef66e3d0SJuneyoung Lee {{false, true}, 9720a6aee51SJuneyoung Lee "shufflevector <4 x i32> %vx, <4 x i32> %vx2, " 9730a6aee51SJuneyoung Lee "<4 x i32> <i32 0, i32 1, i32 2, i32 undef>"}, 974ef66e3d0SJuneyoung Lee {{false, true}, 9750a6aee51SJuneyoung Lee "shufflevector <vscale x 4 x i32> %svx, " 9760a6aee51SJuneyoung Lee "<vscale x 4 x i32> %svx, <vscale x 4 x i32> undef"}, 977ef66e3d0SJuneyoung Lee {{true, false}, "call i32 @g(i32 %x)"}, 978ef66e3d0SJuneyoung Lee {{false, false}, "call noundef i32 @g(i32 %x)"}, 979ef66e3d0SJuneyoung Lee {{true, false}, "fcmp nnan oeq float %fx, %fy"}, 980ef66e3d0SJuneyoung Lee {{false, false}, "fcmp oeq float %fx, %fy"}}; 981994543abSJuneyoung Lee 982994543abSJuneyoung Lee std::string AssemblyStr = AsmHead; 983994543abSJuneyoung Lee for (auto &Itm : Data) 984ef66e3d0SJuneyoung Lee AssemblyStr += Itm.second + "\n"; 985994543abSJuneyoung Lee AssemblyStr += AsmTail; 986994543abSJuneyoung Lee 987994543abSJuneyoung Lee LLVMContext Context; 988994543abSJuneyoung Lee SMDiagnostic Error; 989994543abSJuneyoung Lee auto M = parseAssemblyString(AssemblyStr, Error, Context); 990994543abSJuneyoung Lee assert(M && "Bad assembly?"); 991994543abSJuneyoung Lee 992994543abSJuneyoung Lee auto *F = M->getFunction("f"); 993994543abSJuneyoung Lee assert(F && "Bad assembly?"); 994994543abSJuneyoung Lee 995994543abSJuneyoung Lee auto &BB = F->getEntryBlock(); 996994543abSJuneyoung Lee 997994543abSJuneyoung Lee int Index = 0; 998994543abSJuneyoung Lee for (auto &I : BB) { 999994543abSJuneyoung Lee if (isa<ReturnInst>(&I)) 1000994543abSJuneyoung Lee break; 1001ef66e3d0SJuneyoung Lee bool Poison = Data[Index].first.first; 1002ef66e3d0SJuneyoung Lee bool Undef = Data[Index].first.second; 10030a6aee51SJuneyoung Lee EXPECT_EQ(canCreatePoison(cast<Operator>(&I)), Poison) 10040a6aee51SJuneyoung Lee << "Incorrect answer of canCreatePoison at instruction " << Index 10050a6aee51SJuneyoung Lee << " = " << I; 10060a6aee51SJuneyoung Lee EXPECT_EQ(canCreateUndefOrPoison(cast<Operator>(&I)), Undef || Poison) 10070a6aee51SJuneyoung Lee << "Incorrect answer of canCreateUndef at instruction " << Index 10080a6aee51SJuneyoung Lee << " = " << I; 1009994543abSJuneyoung Lee Index++; 1010994543abSJuneyoung Lee } 101145a148deSSanjay Patel } 101245a148deSSanjay Patel 1013565f4e72SAlex Richardson TEST_F(ValueTrackingTest, computePtrAlignment) { 1014565f4e72SAlex Richardson parseAssembly("declare i1 @f_i1()\n" 1015565f4e72SAlex Richardson "declare i8* @f_i8p()\n" 1016565f4e72SAlex Richardson "declare void @llvm.assume(i1)\n" 1017565f4e72SAlex Richardson "define void @test() {\n" 1018565f4e72SAlex Richardson " %A = call i8* @f_i8p()\n" 1019565f4e72SAlex Richardson " %cond = call i1 @f_i1()\n" 1020565f4e72SAlex Richardson " %CxtI = add i32 0, 0\n" 1021565f4e72SAlex Richardson " br i1 %cond, label %BB1, label %EXIT\n" 1022565f4e72SAlex Richardson "BB1:\n" 1023565f4e72SAlex Richardson " %CxtI2 = add i32 0, 0\n" 1024565f4e72SAlex Richardson " %cond2 = call i1 @f_i1()\n" 1025565f4e72SAlex Richardson " call void @llvm.assume(i1 true) [ \"align\"(i8* %A, i64 16) ]\n" 1026565f4e72SAlex Richardson " br i1 %cond2, label %BB2, label %EXIT\n" 1027565f4e72SAlex Richardson "BB2:\n" 1028565f4e72SAlex Richardson " %CxtI3 = add i32 0, 0\n" 1029565f4e72SAlex Richardson " ret void\n" 1030565f4e72SAlex Richardson "EXIT:\n" 1031565f4e72SAlex Richardson " ret void\n" 1032565f4e72SAlex Richardson "}"); 1033565f4e72SAlex Richardson AssumptionCache AC(*F); 1034565f4e72SAlex Richardson DominatorTree DT(*F); 1035565f4e72SAlex Richardson DataLayout DL = M->getDataLayout(); 1036565f4e72SAlex Richardson EXPECT_EQ(getKnownAlignment(A, DL, CxtI, &AC, &DT), Align(1)); 1037565f4e72SAlex Richardson EXPECT_EQ(getKnownAlignment(A, DL, CxtI2, &AC, &DT), Align(1)); 1038565f4e72SAlex Richardson EXPECT_EQ(getKnownAlignment(A, DL, CxtI3, &AC, &DT), Align(16)); 1039565f4e72SAlex Richardson } 1040565f4e72SAlex Richardson 1041219e5367SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownBits) { 1042219e5367SNikita Popov parseAssembly( 1043219e5367SNikita Popov "define i32 @test(i32 %a, i32 %b) {\n" 1044219e5367SNikita Popov " %ash = mul i32 %a, 8\n" 1045219e5367SNikita Popov " %aad = add i32 %ash, 7\n" 1046219e5367SNikita Popov " %aan = and i32 %aad, 4095\n" 1047219e5367SNikita Popov " %bsh = shl i32 %b, 4\n" 1048219e5367SNikita Popov " %bad = or i32 %bsh, 6\n" 1049219e5367SNikita Popov " %ban = and i32 %bad, 4095\n" 1050219e5367SNikita Popov " %A = mul i32 %aan, %ban\n" 1051219e5367SNikita Popov " ret i32 %A\n" 1052219e5367SNikita Popov "}\n"); 1053219e5367SNikita Popov expectKnownBits(/*zero*/ 4278190085u, /*one*/ 10u); 105470dbd5fbSSimon Dardis } 105570dbd5fbSSimon Dardis 1056219e5367SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownMulBits) { 1057219e5367SNikita Popov parseAssembly( 1058219e5367SNikita Popov "define i32 @test(i32 %a, i32 %b) {\n" 1059219e5367SNikita Popov " %aa = shl i32 %a, 5\n" 1060219e5367SNikita Popov " %bb = shl i32 %b, 5\n" 1061219e5367SNikita Popov " %aaa = or i32 %aa, 24\n" 1062219e5367SNikita Popov " %bbb = or i32 %bb, 28\n" 1063219e5367SNikita Popov " %A = mul i32 %aaa, %bbb\n" 1064219e5367SNikita Popov " ret i32 %A\n" 1065219e5367SNikita Popov "}\n"); 1066219e5367SNikita Popov expectKnownBits(/*zero*/ 95u, /*one*/ 32u); 106770dbd5fbSSimon Dardis } 1068687b92cdSNikita Popov 1069eca40c36SJuneyoung Lee TEST_F(ValueTrackingTest, KnownNonZeroFromDomCond) { 1070eca40c36SJuneyoung Lee parseAssembly(R"( 1071eca40c36SJuneyoung Lee declare i8* @f_i8() 1072eca40c36SJuneyoung Lee define void @test(i1 %c) { 1073eca40c36SJuneyoung Lee %A = call i8* @f_i8() 1074eca40c36SJuneyoung Lee %B = call i8* @f_i8() 1075eca40c36SJuneyoung Lee %c1 = icmp ne i8* %A, null 1076eca40c36SJuneyoung Lee %cond = and i1 %c1, %c 1077eca40c36SJuneyoung Lee br i1 %cond, label %T, label %Q 1078eca40c36SJuneyoung Lee T: 1079eca40c36SJuneyoung Lee %CxtI = add i32 0, 0 1080eca40c36SJuneyoung Lee ret void 1081eca40c36SJuneyoung Lee Q: 1082eca40c36SJuneyoung Lee %CxtI2 = add i32 0, 0 1083eca40c36SJuneyoung Lee ret void 1084eca40c36SJuneyoung Lee } 1085eca40c36SJuneyoung Lee )"); 1086eca40c36SJuneyoung Lee AssumptionCache AC(*F); 1087eca40c36SJuneyoung Lee DominatorTree DT(*F); 1088eca40c36SJuneyoung Lee DataLayout DL = M->getDataLayout(); 1089eca40c36SJuneyoung Lee EXPECT_EQ(isKnownNonZero(A, DL, 0, &AC, CxtI, &DT), true); 1090eca40c36SJuneyoung Lee EXPECT_EQ(isKnownNonZero(A, DL, 0, &AC, CxtI2, &DT), false); 1091eca40c36SJuneyoung Lee } 1092eca40c36SJuneyoung Lee 1093860199dfSJuneyoung Lee TEST_F(ValueTrackingTest, KnownNonZeroFromDomCond2) { 1094860199dfSJuneyoung Lee parseAssembly(R"( 1095860199dfSJuneyoung Lee declare i8* @f_i8() 1096860199dfSJuneyoung Lee define void @test(i1 %c) { 1097860199dfSJuneyoung Lee %A = call i8* @f_i8() 1098860199dfSJuneyoung Lee %B = call i8* @f_i8() 1099860199dfSJuneyoung Lee %c1 = icmp ne i8* %A, null 1100860199dfSJuneyoung Lee %cond = select i1 %c, i1 %c1, i1 false 1101860199dfSJuneyoung Lee br i1 %cond, label %T, label %Q 1102860199dfSJuneyoung Lee T: 1103860199dfSJuneyoung Lee %CxtI = add i32 0, 0 1104860199dfSJuneyoung Lee ret void 1105860199dfSJuneyoung Lee Q: 1106860199dfSJuneyoung Lee %CxtI2 = add i32 0, 0 1107860199dfSJuneyoung Lee ret void 1108860199dfSJuneyoung Lee } 1109860199dfSJuneyoung Lee )"); 1110860199dfSJuneyoung Lee AssumptionCache AC(*F); 1111860199dfSJuneyoung Lee DominatorTree DT(*F); 1112860199dfSJuneyoung Lee DataLayout DL = M->getDataLayout(); 1113860199dfSJuneyoung Lee EXPECT_EQ(isKnownNonZero(A, DL, 0, &AC, CxtI, &DT), true); 1114860199dfSJuneyoung Lee EXPECT_EQ(isKnownNonZero(A, DL, 0, &AC, CxtI2, &DT), false); 1115860199dfSJuneyoung Lee } 1116860199dfSJuneyoung Lee 1117eca40c36SJuneyoung Lee TEST_F(ValueTrackingTest, IsImpliedConditionAnd) { 1118eca40c36SJuneyoung Lee parseAssembly(R"( 1119eca40c36SJuneyoung Lee define void @test(i32 %x, i32 %y) { 1120eca40c36SJuneyoung Lee %c1 = icmp ult i32 %x, 10 1121eca40c36SJuneyoung Lee %c2 = icmp ult i32 %y, 15 1122eca40c36SJuneyoung Lee %A = and i1 %c1, %c2 1123eca40c36SJuneyoung Lee ; x < 10 /\ y < 15 1124eca40c36SJuneyoung Lee %A2 = icmp ult i32 %x, 20 1125eca40c36SJuneyoung Lee %A3 = icmp uge i32 %y, 20 1126eca40c36SJuneyoung Lee %A4 = icmp ult i32 %x, 5 1127eca40c36SJuneyoung Lee ret void 1128eca40c36SJuneyoung Lee } 1129eca40c36SJuneyoung Lee )"); 1130eca40c36SJuneyoung Lee DataLayout DL = M->getDataLayout(); 1131eca40c36SJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A2, DL), true); 1132eca40c36SJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A3, DL), false); 1133eca40c36SJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A4, DL), None); 1134eca40c36SJuneyoung Lee } 1135eca40c36SJuneyoung Lee 1136860199dfSJuneyoung Lee TEST_F(ValueTrackingTest, IsImpliedConditionAnd2) { 1137860199dfSJuneyoung Lee parseAssembly(R"( 1138860199dfSJuneyoung Lee define void @test(i32 %x, i32 %y) { 1139860199dfSJuneyoung Lee %c1 = icmp ult i32 %x, 10 1140860199dfSJuneyoung Lee %c2 = icmp ult i32 %y, 15 1141860199dfSJuneyoung Lee %A = select i1 %c1, i1 %c2, i1 false 1142860199dfSJuneyoung Lee ; x < 10 /\ y < 15 1143860199dfSJuneyoung Lee %A2 = icmp ult i32 %x, 20 1144860199dfSJuneyoung Lee %A3 = icmp uge i32 %y, 20 1145860199dfSJuneyoung Lee %A4 = icmp ult i32 %x, 5 1146860199dfSJuneyoung Lee ret void 1147860199dfSJuneyoung Lee } 1148860199dfSJuneyoung Lee )"); 1149860199dfSJuneyoung Lee DataLayout DL = M->getDataLayout(); 1150860199dfSJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A2, DL), true); 1151860199dfSJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A3, DL), false); 1152860199dfSJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A4, DL), None); 1153860199dfSJuneyoung Lee } 1154860199dfSJuneyoung Lee 1155eca40c36SJuneyoung Lee TEST_F(ValueTrackingTest, IsImpliedConditionOr) { 1156eca40c36SJuneyoung Lee parseAssembly(R"( 1157eca40c36SJuneyoung Lee define void @test(i32 %x, i32 %y) { 1158eca40c36SJuneyoung Lee %c1 = icmp ult i32 %x, 10 1159eca40c36SJuneyoung Lee %c2 = icmp ult i32 %y, 15 1160eca40c36SJuneyoung Lee %A = or i1 %c1, %c2 ; negated 1161eca40c36SJuneyoung Lee ; x >= 10 /\ y >= 15 1162eca40c36SJuneyoung Lee %A2 = icmp ult i32 %x, 5 1163eca40c36SJuneyoung Lee %A3 = icmp uge i32 %y, 10 1164eca40c36SJuneyoung Lee %A4 = icmp ult i32 %x, 15 1165eca40c36SJuneyoung Lee ret void 1166eca40c36SJuneyoung Lee } 1167eca40c36SJuneyoung Lee )"); 1168eca40c36SJuneyoung Lee DataLayout DL = M->getDataLayout(); 1169eca40c36SJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A2, DL, false), false); 1170eca40c36SJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A3, DL, false), true); 1171eca40c36SJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A4, DL, false), None); 1172eca40c36SJuneyoung Lee } 1173eca40c36SJuneyoung Lee 1174860199dfSJuneyoung Lee TEST_F(ValueTrackingTest, IsImpliedConditionOr2) { 1175860199dfSJuneyoung Lee parseAssembly(R"( 1176860199dfSJuneyoung Lee define void @test(i32 %x, i32 %y) { 1177860199dfSJuneyoung Lee %c1 = icmp ult i32 %x, 10 1178860199dfSJuneyoung Lee %c2 = icmp ult i32 %y, 15 1179860199dfSJuneyoung Lee %A = select i1 %c1, i1 true, i1 %c2 ; negated 1180860199dfSJuneyoung Lee ; x >= 10 /\ y >= 15 1181860199dfSJuneyoung Lee %A2 = icmp ult i32 %x, 5 1182860199dfSJuneyoung Lee %A3 = icmp uge i32 %y, 10 1183860199dfSJuneyoung Lee %A4 = icmp ult i32 %x, 15 1184860199dfSJuneyoung Lee ret void 1185860199dfSJuneyoung Lee } 1186860199dfSJuneyoung Lee )"); 1187860199dfSJuneyoung Lee DataLayout DL = M->getDataLayout(); 1188860199dfSJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A2, DL, false), false); 1189860199dfSJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A3, DL, false), true); 1190860199dfSJuneyoung Lee EXPECT_EQ(isImpliedCondition(A, A4, DL, false), None); 1191860199dfSJuneyoung Lee } 1192860199dfSJuneyoung Lee 11937cf0f856SNikita Popov TEST_F(ComputeKnownBitsTest, KnownNonZeroShift) { 11947cf0f856SNikita Popov // %q is known nonzero without known bits. 11957cf0f856SNikita Popov // Because %q is nonzero, %A[0] is known to be zero. 11967cf0f856SNikita Popov parseAssembly( 11977cf0f856SNikita Popov "define i8 @test(i8 %p, i8* %pq) {\n" 11987cf0f856SNikita Popov " %q = load i8, i8* %pq, !range !0\n" 11997cf0f856SNikita Popov " %A = shl i8 %p, %q\n" 12007cf0f856SNikita Popov " ret i8 %A\n" 12017cf0f856SNikita Popov "}\n" 12027cf0f856SNikita Popov "!0 = !{ i8 1, i8 5 }\n"); 12037cf0f856SNikita Popov expectKnownBits(/*zero*/ 1u, /*one*/ 0u); 12047cf0f856SNikita Popov } 12057cf0f856SNikita Popov 1206687b92cdSNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownFshl) { 1207687b92cdSNikita Popov // fshl(....1111....0000, 00..1111........, 6) 1208687b92cdSNikita Popov // = 11....000000..11 1209687b92cdSNikita Popov parseAssembly( 1210687b92cdSNikita Popov "define i16 @test(i16 %a, i16 %b) {\n" 1211687b92cdSNikita Popov " %aa = shl i16 %a, 4\n" 1212687b92cdSNikita Popov " %bb = lshr i16 %b, 2\n" 1213687b92cdSNikita Popov " %aaa = or i16 %aa, 3840\n" 1214687b92cdSNikita Popov " %bbb = or i16 %bb, 3840\n" 1215687b92cdSNikita Popov " %A = call i16 @llvm.fshl.i16(i16 %aaa, i16 %bbb, i16 6)\n" 1216687b92cdSNikita Popov " ret i16 %A\n" 1217687b92cdSNikita Popov "}\n" 1218687b92cdSNikita Popov "declare i16 @llvm.fshl.i16(i16, i16, i16)\n"); 1219687b92cdSNikita Popov expectKnownBits(/*zero*/ 1008u, /*one*/ 49155u); 1220687b92cdSNikita Popov } 1221687b92cdSNikita Popov 1222687b92cdSNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownFshr) { 1223687b92cdSNikita Popov // fshr(....1111....0000, 00..1111........, 26) 1224687b92cdSNikita Popov // = 11....000000..11 1225687b92cdSNikita Popov parseAssembly( 1226687b92cdSNikita Popov "define i16 @test(i16 %a, i16 %b) {\n" 1227687b92cdSNikita Popov " %aa = shl i16 %a, 4\n" 1228687b92cdSNikita Popov " %bb = lshr i16 %b, 2\n" 1229687b92cdSNikita Popov " %aaa = or i16 %aa, 3840\n" 1230687b92cdSNikita Popov " %bbb = or i16 %bb, 3840\n" 1231687b92cdSNikita Popov " %A = call i16 @llvm.fshr.i16(i16 %aaa, i16 %bbb, i16 26)\n" 1232687b92cdSNikita Popov " ret i16 %A\n" 1233687b92cdSNikita Popov "}\n" 1234687b92cdSNikita Popov "declare i16 @llvm.fshr.i16(i16, i16, i16)\n"); 1235687b92cdSNikita Popov expectKnownBits(/*zero*/ 1008u, /*one*/ 49155u); 1236687b92cdSNikita Popov } 1237687b92cdSNikita Popov 1238687b92cdSNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownFshlZero) { 1239687b92cdSNikita Popov // fshl(....1111....0000, 00..1111........, 0) 1240687b92cdSNikita Popov // = ....1111....0000 1241687b92cdSNikita Popov parseAssembly( 1242687b92cdSNikita Popov "define i16 @test(i16 %a, i16 %b) {\n" 1243687b92cdSNikita Popov " %aa = shl i16 %a, 4\n" 1244687b92cdSNikita Popov " %bb = lshr i16 %b, 2\n" 1245687b92cdSNikita Popov " %aaa = or i16 %aa, 3840\n" 1246687b92cdSNikita Popov " %bbb = or i16 %bb, 3840\n" 1247687b92cdSNikita Popov " %A = call i16 @llvm.fshl.i16(i16 %aaa, i16 %bbb, i16 0)\n" 1248687b92cdSNikita Popov " ret i16 %A\n" 1249687b92cdSNikita Popov "}\n" 1250687b92cdSNikita Popov "declare i16 @llvm.fshl.i16(i16, i16, i16)\n"); 1251687b92cdSNikita Popov expectKnownBits(/*zero*/ 15u, /*one*/ 3840u); 1252687b92cdSNikita Popov } 1253ed3ca927SNikita Popov 1254ed3ca927SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownUAddSatLeadingOnes) { 1255ed3ca927SNikita Popov // uadd.sat(1111...1, ........) 1256ed3ca927SNikita Popov // = 1111.... 1257ed3ca927SNikita Popov parseAssembly( 1258ed3ca927SNikita Popov "define i8 @test(i8 %a, i8 %b) {\n" 1259ed3ca927SNikita Popov " %aa = or i8 %a, 241\n" 1260ed3ca927SNikita Popov " %A = call i8 @llvm.uadd.sat.i8(i8 %aa, i8 %b)\n" 1261ed3ca927SNikita Popov " ret i8 %A\n" 1262ed3ca927SNikita Popov "}\n" 1263ed3ca927SNikita Popov "declare i8 @llvm.uadd.sat.i8(i8, i8)\n"); 1264ed3ca927SNikita Popov expectKnownBits(/*zero*/ 0u, /*one*/ 240u); 1265ed3ca927SNikita Popov } 1266ed3ca927SNikita Popov 1267ed3ca927SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownUAddSatOnesPreserved) { 1268ed3ca927SNikita Popov // uadd.sat(00...011, .1...110) 1269ed3ca927SNikita Popov // = .......1 1270ed3ca927SNikita Popov parseAssembly( 1271ed3ca927SNikita Popov "define i8 @test(i8 %a, i8 %b) {\n" 1272ed3ca927SNikita Popov " %aa = or i8 %a, 3\n" 1273ed3ca927SNikita Popov " %aaa = and i8 %aa, 59\n" 1274ed3ca927SNikita Popov " %bb = or i8 %b, 70\n" 1275ed3ca927SNikita Popov " %bbb = and i8 %bb, 254\n" 1276ed3ca927SNikita Popov " %A = call i8 @llvm.uadd.sat.i8(i8 %aaa, i8 %bbb)\n" 1277ed3ca927SNikita Popov " ret i8 %A\n" 1278ed3ca927SNikita Popov "}\n" 1279ed3ca927SNikita Popov "declare i8 @llvm.uadd.sat.i8(i8, i8)\n"); 1280ed3ca927SNikita Popov expectKnownBits(/*zero*/ 0u, /*one*/ 1u); 1281ed3ca927SNikita Popov } 1282ed3ca927SNikita Popov 1283ed3ca927SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatLHSLeadingZeros) { 1284ed3ca927SNikita Popov // usub.sat(0000...0, ........) 1285ed3ca927SNikita Popov // = 0000.... 1286ed3ca927SNikita Popov parseAssembly( 1287ed3ca927SNikita Popov "define i8 @test(i8 %a, i8 %b) {\n" 1288ed3ca927SNikita Popov " %aa = and i8 %a, 14\n" 1289ed3ca927SNikita Popov " %A = call i8 @llvm.usub.sat.i8(i8 %aa, i8 %b)\n" 1290ed3ca927SNikita Popov " ret i8 %A\n" 1291ed3ca927SNikita Popov "}\n" 1292ed3ca927SNikita Popov "declare i8 @llvm.usub.sat.i8(i8, i8)\n"); 1293ed3ca927SNikita Popov expectKnownBits(/*zero*/ 240u, /*one*/ 0u); 1294ed3ca927SNikita Popov } 1295ed3ca927SNikita Popov 1296ed3ca927SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatRHSLeadingOnes) { 1297ed3ca927SNikita Popov // usub.sat(........, 1111...1) 1298ed3ca927SNikita Popov // = 0000.... 1299ed3ca927SNikita Popov parseAssembly( 1300ed3ca927SNikita Popov "define i8 @test(i8 %a, i8 %b) {\n" 1301ed3ca927SNikita Popov " %bb = or i8 %a, 241\n" 1302ed3ca927SNikita Popov " %A = call i8 @llvm.usub.sat.i8(i8 %a, i8 %bb)\n" 1303ed3ca927SNikita Popov " ret i8 %A\n" 1304ed3ca927SNikita Popov "}\n" 1305ed3ca927SNikita Popov "declare i8 @llvm.usub.sat.i8(i8, i8)\n"); 1306ed3ca927SNikita Popov expectKnownBits(/*zero*/ 240u, /*one*/ 0u); 1307ed3ca927SNikita Popov } 1308ed3ca927SNikita Popov 1309ed3ca927SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatZerosPreserved) { 1310ed3ca927SNikita Popov // usub.sat(11...011, .1...110) 1311ed3ca927SNikita Popov // = ......0. 1312ed3ca927SNikita Popov parseAssembly( 1313ed3ca927SNikita Popov "define i8 @test(i8 %a, i8 %b) {\n" 1314ed3ca927SNikita Popov " %aa = or i8 %a, 195\n" 1315ed3ca927SNikita Popov " %aaa = and i8 %aa, 251\n" 1316ed3ca927SNikita Popov " %bb = or i8 %b, 70\n" 1317ed3ca927SNikita Popov " %bbb = and i8 %bb, 254\n" 1318ed3ca927SNikita Popov " %A = call i8 @llvm.usub.sat.i8(i8 %aaa, i8 %bbb)\n" 1319ed3ca927SNikita Popov " ret i8 %A\n" 1320ed3ca927SNikita Popov "}\n" 1321ed3ca927SNikita Popov "declare i8 @llvm.usub.sat.i8(i8, i8)\n"); 1322ed3ca927SNikita Popov expectKnownBits(/*zero*/ 2u, /*one*/ 0u); 1323ed3ca927SNikita Popov } 1324329d15b5SVitaly Buka 1325d86fff6aSNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownBitsPtrToIntTrunc) { 1326d86fff6aSNikita Popov // ptrtoint truncates the pointer type. 1327d86fff6aSNikita Popov parseAssembly( 1328d86fff6aSNikita Popov "define void @test(i8** %p) {\n" 1329d86fff6aSNikita Popov " %A = load i8*, i8** %p\n" 1330d86fff6aSNikita Popov " %i = ptrtoint i8* %A to i32\n" 1331d86fff6aSNikita Popov " %m = and i32 %i, 31\n" 1332d86fff6aSNikita Popov " %c = icmp eq i32 %m, 0\n" 1333d86fff6aSNikita Popov " call void @llvm.assume(i1 %c)\n" 1334d86fff6aSNikita Popov " ret void\n" 1335d86fff6aSNikita Popov "}\n" 1336d86fff6aSNikita Popov "declare void @llvm.assume(i1)\n"); 1337d86fff6aSNikita Popov AssumptionCache AC(*F); 1338d86fff6aSNikita Popov KnownBits Known = computeKnownBits( 1339d86fff6aSNikita Popov A, M->getDataLayout(), /* Depth */ 0, &AC, F->front().getTerminator()); 1340d86fff6aSNikita Popov EXPECT_EQ(Known.Zero.getZExtValue(), 31u); 1341d86fff6aSNikita Popov EXPECT_EQ(Known.One.getZExtValue(), 0u); 1342d86fff6aSNikita Popov } 1343d86fff6aSNikita Popov 1344d86fff6aSNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownBitsPtrToIntZext) { 1345d86fff6aSNikita Popov // ptrtoint zero extends the pointer type. 1346d86fff6aSNikita Popov parseAssembly( 1347d86fff6aSNikita Popov "define void @test(i8** %p) {\n" 1348d86fff6aSNikita Popov " %A = load i8*, i8** %p\n" 1349d86fff6aSNikita Popov " %i = ptrtoint i8* %A to i128\n" 1350d86fff6aSNikita Popov " %m = and i128 %i, 31\n" 1351d86fff6aSNikita Popov " %c = icmp eq i128 %m, 0\n" 1352d86fff6aSNikita Popov " call void @llvm.assume(i1 %c)\n" 1353d86fff6aSNikita Popov " ret void\n" 1354d86fff6aSNikita Popov "}\n" 1355d86fff6aSNikita Popov "declare void @llvm.assume(i1)\n"); 1356d86fff6aSNikita Popov AssumptionCache AC(*F); 1357d86fff6aSNikita Popov KnownBits Known = computeKnownBits( 1358d86fff6aSNikita Popov A, M->getDataLayout(), /* Depth */ 0, &AC, F->front().getTerminator()); 1359d86fff6aSNikita Popov EXPECT_EQ(Known.Zero.getZExtValue(), 31u); 1360d86fff6aSNikita Popov EXPECT_EQ(Known.One.getZExtValue(), 0u); 1361d86fff6aSNikita Popov } 1362d86fff6aSNikita Popov 1363a6183d0fSJuneyoung Lee TEST_F(ComputeKnownBitsTest, ComputeKnownBitsFreeze) { 1364a6183d0fSJuneyoung Lee parseAssembly("define void @test() {\n" 1365a6183d0fSJuneyoung Lee " %m = call i32 @any_num()\n" 1366a6183d0fSJuneyoung Lee " %A = freeze i32 %m\n" 1367a6183d0fSJuneyoung Lee " %n = and i32 %m, 31\n" 1368a6183d0fSJuneyoung Lee " %c = icmp eq i32 %n, 0\n" 1369a6183d0fSJuneyoung Lee " call void @llvm.assume(i1 %c)\n" 1370a6183d0fSJuneyoung Lee " ret void\n" 1371a6183d0fSJuneyoung Lee "}\n" 1372a6183d0fSJuneyoung Lee "declare void @llvm.assume(i1)\n" 1373a6183d0fSJuneyoung Lee "declare i32 @any_num()\n"); 1374a6183d0fSJuneyoung Lee AssumptionCache AC(*F); 1375a6183d0fSJuneyoung Lee KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1376a6183d0fSJuneyoung Lee F->front().getTerminator()); 1377a6183d0fSJuneyoung Lee EXPECT_EQ(Known.Zero.getZExtValue(), 31u); 1378a6183d0fSJuneyoung Lee EXPECT_EQ(Known.One.getZExtValue(), 0u); 1379a6183d0fSJuneyoung Lee } 1380a6183d0fSJuneyoung Lee 1381f1f31eb2SQuentin Colombet TEST_F(ComputeKnownBitsTest, ComputeKnownBitsAddWithRange) { 1382f1f31eb2SQuentin Colombet parseAssembly("define void @test(i64* %p) {\n" 1383f1f31eb2SQuentin Colombet " %A = load i64, i64* %p, !range !{i64 64, i64 65536}\n" 1384f1f31eb2SQuentin Colombet " %APlus512 = add i64 %A, 512\n" 1385f1f31eb2SQuentin Colombet " %c = icmp ugt i64 %APlus512, 523\n" 1386f1f31eb2SQuentin Colombet " call void @llvm.assume(i1 %c)\n" 1387f1f31eb2SQuentin Colombet " ret void\n" 1388f1f31eb2SQuentin Colombet "}\n" 1389f1f31eb2SQuentin Colombet "declare void @llvm.assume(i1)\n"); 1390f1f31eb2SQuentin Colombet AssumptionCache AC(*F); 1391f1f31eb2SQuentin Colombet KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1392f1f31eb2SQuentin Colombet F->front().getTerminator()); 1393f1f31eb2SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), ~(65536llu - 1)); 1394f1f31eb2SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 0u); 1395f1f31eb2SQuentin Colombet Instruction &APlus512 = findInstructionByName(F, "APlus512"); 1396f1f31eb2SQuentin Colombet Known = computeKnownBits(&APlus512, M->getDataLayout(), /* Depth */ 0, &AC, 1397f1f31eb2SQuentin Colombet F->front().getTerminator()); 1398f1f31eb2SQuentin Colombet // We know of one less zero because 512 may have produced a 1 that 1399f1f31eb2SQuentin Colombet // got carried all the way to the first trailing zero. 1400f1f31eb2SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), (~(65536llu - 1)) << 1); 1401f1f31eb2SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 0u); 1402f1f31eb2SQuentin Colombet // The known range is not precise given computeKnownBits works 1403f1f31eb2SQuentin Colombet // with the masks of zeros and ones, not the ranges. 1404f1f31eb2SQuentin Colombet EXPECT_EQ(Known.getMinValue(), 0u); 1405f1f31eb2SQuentin Colombet EXPECT_EQ(Known.getMaxValue(), 131071); 1406f1f31eb2SQuentin Colombet } 1407f1f31eb2SQuentin Colombet 1408f1f31eb2SQuentin Colombet // 512 + [32, 64) doesn't produce overlapping bits. 1409f1f31eb2SQuentin Colombet // Make sure we get all the individual bits properly. 1410f1f31eb2SQuentin Colombet TEST_F(ComputeKnownBitsTest, ComputeKnownBitsAddWithRangeNoOverlap) { 1411f1f31eb2SQuentin Colombet parseAssembly("define void @test(i64* %p) {\n" 1412f1f31eb2SQuentin Colombet " %A = load i64, i64* %p, !range !{i64 32, i64 64}\n" 1413f1f31eb2SQuentin Colombet " %APlus512 = add i64 %A, 512\n" 1414f1f31eb2SQuentin Colombet " %c = icmp ugt i64 %APlus512, 523\n" 1415f1f31eb2SQuentin Colombet " call void @llvm.assume(i1 %c)\n" 1416f1f31eb2SQuentin Colombet " ret void\n" 1417f1f31eb2SQuentin Colombet "}\n" 1418f1f31eb2SQuentin Colombet "declare void @llvm.assume(i1)\n"); 1419f1f31eb2SQuentin Colombet AssumptionCache AC(*F); 1420f1f31eb2SQuentin Colombet KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1421f1f31eb2SQuentin Colombet F->front().getTerminator()); 1422f1f31eb2SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), ~(64llu - 1)); 1423f1f31eb2SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 32u); 1424f1f31eb2SQuentin Colombet Instruction &APlus512 = findInstructionByName(F, "APlus512"); 1425f1f31eb2SQuentin Colombet Known = computeKnownBits(&APlus512, M->getDataLayout(), /* Depth */ 0, &AC, 1426f1f31eb2SQuentin Colombet F->front().getTerminator()); 1427f1f31eb2SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), ~512llu & ~(64llu - 1)); 1428f1f31eb2SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 512u | 32u); 1429f1f31eb2SQuentin Colombet // The known range is not precise given computeKnownBits works 1430f1f31eb2SQuentin Colombet // with the masks of zeros and ones, not the ranges. 1431f1f31eb2SQuentin Colombet EXPECT_EQ(Known.getMinValue(), 544); 1432f1f31eb2SQuentin Colombet EXPECT_EQ(Known.getMaxValue(), 575); 1433f1f31eb2SQuentin Colombet } 1434f1f31eb2SQuentin Colombet 1435ee6abef5SQuentin Colombet TEST_F(ComputeKnownBitsTest, ComputeKnownBitsGEPWithRange) { 1436ee6abef5SQuentin Colombet parseAssembly( 1437ee6abef5SQuentin Colombet "define void @test(i64* %p) {\n" 1438ee6abef5SQuentin Colombet " %A = load i64, i64* %p, !range !{i64 64, i64 65536}\n" 1439ee6abef5SQuentin Colombet " %APtr = inttoptr i64 %A to float*" 1440ee6abef5SQuentin Colombet " %APtrPlus512 = getelementptr float, float* %APtr, i32 128\n" 1441ee6abef5SQuentin Colombet " %c = icmp ugt float* %APtrPlus512, inttoptr (i32 523 to float*)\n" 1442ee6abef5SQuentin Colombet " call void @llvm.assume(i1 %c)\n" 1443ee6abef5SQuentin Colombet " ret void\n" 1444ee6abef5SQuentin Colombet "}\n" 1445ee6abef5SQuentin Colombet "declare void @llvm.assume(i1)\n"); 1446ee6abef5SQuentin Colombet AssumptionCache AC(*F); 1447ee6abef5SQuentin Colombet KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1448ee6abef5SQuentin Colombet F->front().getTerminator()); 1449ee6abef5SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), ~(65536llu - 1)); 1450ee6abef5SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 0u); 1451ee6abef5SQuentin Colombet Instruction &APtrPlus512 = findInstructionByName(F, "APtrPlus512"); 1452ee6abef5SQuentin Colombet Known = computeKnownBits(&APtrPlus512, M->getDataLayout(), /* Depth */ 0, &AC, 1453ee6abef5SQuentin Colombet F->front().getTerminator()); 1454ee6abef5SQuentin Colombet // We know of one less zero because 512 may have produced a 1 that 1455ee6abef5SQuentin Colombet // got carried all the way to the first trailing zero. 1456ee6abef5SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), ~(65536llu - 1) << 1); 1457ee6abef5SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 0u); 1458ee6abef5SQuentin Colombet // The known range is not precise given computeKnownBits works 1459ee6abef5SQuentin Colombet // with the masks of zeros and ones, not the ranges. 1460ee6abef5SQuentin Colombet EXPECT_EQ(Known.getMinValue(), 0u); 1461ee6abef5SQuentin Colombet EXPECT_EQ(Known.getMaxValue(), 131071); 1462ee6abef5SQuentin Colombet } 1463ee6abef5SQuentin Colombet 1464ee6abef5SQuentin Colombet // 4*128 + [32, 64) doesn't produce overlapping bits. 1465ee6abef5SQuentin Colombet // Make sure we get all the individual bits properly. 1466ee6abef5SQuentin Colombet // This test is useful to check that we account for the scaling factor 1467ee6abef5SQuentin Colombet // in the gep. Indeed, gep float, [32,64), 128 is not 128 + [32,64). 1468ee6abef5SQuentin Colombet TEST_F(ComputeKnownBitsTest, ComputeKnownBitsGEPWithRangeNoOverlap) { 1469ee6abef5SQuentin Colombet parseAssembly( 1470ee6abef5SQuentin Colombet "define void @test(i64* %p) {\n" 1471ee6abef5SQuentin Colombet " %A = load i64, i64* %p, !range !{i64 32, i64 64}\n" 1472ee6abef5SQuentin Colombet " %APtr = inttoptr i64 %A to float*" 1473ee6abef5SQuentin Colombet " %APtrPlus512 = getelementptr float, float* %APtr, i32 128\n" 1474ee6abef5SQuentin Colombet " %c = icmp ugt float* %APtrPlus512, inttoptr (i32 523 to float*)\n" 1475ee6abef5SQuentin Colombet " call void @llvm.assume(i1 %c)\n" 1476ee6abef5SQuentin Colombet " ret void\n" 1477ee6abef5SQuentin Colombet "}\n" 1478ee6abef5SQuentin Colombet "declare void @llvm.assume(i1)\n"); 1479ee6abef5SQuentin Colombet AssumptionCache AC(*F); 1480ee6abef5SQuentin Colombet KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1481ee6abef5SQuentin Colombet F->front().getTerminator()); 1482ee6abef5SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), ~(64llu - 1)); 1483ee6abef5SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 32u); 1484ee6abef5SQuentin Colombet Instruction &APtrPlus512 = findInstructionByName(F, "APtrPlus512"); 1485ee6abef5SQuentin Colombet Known = computeKnownBits(&APtrPlus512, M->getDataLayout(), /* Depth */ 0, &AC, 1486ee6abef5SQuentin Colombet F->front().getTerminator()); 1487ee6abef5SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), ~512llu & ~(64llu - 1)); 1488ee6abef5SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 512u | 32u); 1489ee6abef5SQuentin Colombet // The known range is not precise given computeKnownBits works 1490ee6abef5SQuentin Colombet // with the masks of zeros and ones, not the ranges. 1491ee6abef5SQuentin Colombet EXPECT_EQ(Known.getMinValue(), 544); 1492ee6abef5SQuentin Colombet EXPECT_EQ(Known.getMaxValue(), 575); 1493ee6abef5SQuentin Colombet } 1494ee6abef5SQuentin Colombet 1495329d15b5SVitaly Buka class IsBytewiseValueTest : public ValueTrackingTest, 1496329d15b5SVitaly Buka public ::testing::WithParamInterface< 1497329d15b5SVitaly Buka std::pair<const char *, const char *>> { 1498329d15b5SVitaly Buka protected: 1499329d15b5SVitaly Buka }; 1500329d15b5SVitaly Buka 1501329d15b5SVitaly Buka const std::pair<const char *, const char *> IsBytewiseValueTests[] = { 1502329d15b5SVitaly Buka { 1503329d15b5SVitaly Buka "i8 0", 1504329d15b5SVitaly Buka "i48* null", 1505329d15b5SVitaly Buka }, 1506329d15b5SVitaly Buka { 1507329d15b5SVitaly Buka "i8 undef", 1508329d15b5SVitaly Buka "i48* undef", 1509329d15b5SVitaly Buka }, 1510329d15b5SVitaly Buka { 1511329d15b5SVitaly Buka "i8 0", 1512329d15b5SVitaly Buka "i8 zeroinitializer", 1513329d15b5SVitaly Buka }, 1514329d15b5SVitaly Buka { 1515329d15b5SVitaly Buka "i8 0", 1516329d15b5SVitaly Buka "i8 0", 1517329d15b5SVitaly Buka }, 1518329d15b5SVitaly Buka { 1519329d15b5SVitaly Buka "i8 -86", 1520329d15b5SVitaly Buka "i8 -86", 1521329d15b5SVitaly Buka }, 1522329d15b5SVitaly Buka { 1523329d15b5SVitaly Buka "i8 -1", 1524329d15b5SVitaly Buka "i8 -1", 1525329d15b5SVitaly Buka }, 1526329d15b5SVitaly Buka { 1527329d15b5SVitaly Buka "i8 undef", 1528329d15b5SVitaly Buka "i16 undef", 1529329d15b5SVitaly Buka }, 1530329d15b5SVitaly Buka { 1531329d15b5SVitaly Buka "i8 0", 1532329d15b5SVitaly Buka "i16 0", 1533329d15b5SVitaly Buka }, 1534329d15b5SVitaly Buka { 1535329d15b5SVitaly Buka "", 1536329d15b5SVitaly Buka "i16 7", 1537329d15b5SVitaly Buka }, 1538329d15b5SVitaly Buka { 1539329d15b5SVitaly Buka "i8 -86", 1540329d15b5SVitaly Buka "i16 -21846", 1541329d15b5SVitaly Buka }, 1542329d15b5SVitaly Buka { 1543329d15b5SVitaly Buka "i8 -1", 1544329d15b5SVitaly Buka "i16 -1", 1545329d15b5SVitaly Buka }, 1546329d15b5SVitaly Buka { 1547329d15b5SVitaly Buka "i8 0", 1548329d15b5SVitaly Buka "i48 0", 1549329d15b5SVitaly Buka }, 1550329d15b5SVitaly Buka { 1551329d15b5SVitaly Buka "i8 -1", 1552329d15b5SVitaly Buka "i48 -1", 1553329d15b5SVitaly Buka }, 1554329d15b5SVitaly Buka { 1555329d15b5SVitaly Buka "i8 0", 1556329d15b5SVitaly Buka "i49 0", 1557329d15b5SVitaly Buka }, 1558329d15b5SVitaly Buka { 1559329d15b5SVitaly Buka "", 1560329d15b5SVitaly Buka "i49 -1", 1561329d15b5SVitaly Buka }, 1562329d15b5SVitaly Buka { 1563329d15b5SVitaly Buka "i8 0", 1564329d15b5SVitaly Buka "half 0xH0000", 1565329d15b5SVitaly Buka }, 1566329d15b5SVitaly Buka { 1567329d15b5SVitaly Buka "i8 -85", 1568329d15b5SVitaly Buka "half 0xHABAB", 1569329d15b5SVitaly Buka }, 1570329d15b5SVitaly Buka { 1571329d15b5SVitaly Buka "i8 0", 1572329d15b5SVitaly Buka "float 0.0", 1573329d15b5SVitaly Buka }, 1574329d15b5SVitaly Buka { 1575329d15b5SVitaly Buka "i8 -1", 1576329d15b5SVitaly Buka "float 0xFFFFFFFFE0000000", 1577329d15b5SVitaly Buka }, 1578329d15b5SVitaly Buka { 1579329d15b5SVitaly Buka "i8 0", 1580329d15b5SVitaly Buka "double 0.0", 1581329d15b5SVitaly Buka }, 1582329d15b5SVitaly Buka { 1583329d15b5SVitaly Buka "i8 -15", 1584329d15b5SVitaly Buka "double 0xF1F1F1F1F1F1F1F1", 1585329d15b5SVitaly Buka }, 1586329d15b5SVitaly Buka { 1587329d15b5SVitaly Buka "i8 undef", 1588329d15b5SVitaly Buka "i16* undef", 1589329d15b5SVitaly Buka }, 1590329d15b5SVitaly Buka { 1591329d15b5SVitaly Buka "i8 0", 1592329d15b5SVitaly Buka "i16* inttoptr (i64 0 to i16*)", 1593329d15b5SVitaly Buka }, 1594329d15b5SVitaly Buka { 1595c559e637SVitaly Buka "i8 -1", 1596329d15b5SVitaly Buka "i16* inttoptr (i64 -1 to i16*)", 1597329d15b5SVitaly Buka }, 1598329d15b5SVitaly Buka { 1599c559e637SVitaly Buka "i8 -86", 1600329d15b5SVitaly Buka "i16* inttoptr (i64 -6148914691236517206 to i16*)", 1601329d15b5SVitaly Buka }, 1602329d15b5SVitaly Buka { 1603329d15b5SVitaly Buka "", 1604329d15b5SVitaly Buka "i16* inttoptr (i48 -1 to i16*)", 1605329d15b5SVitaly Buka }, 1606329d15b5SVitaly Buka { 1607c559e637SVitaly Buka "i8 -1", 1608329d15b5SVitaly Buka "i16* inttoptr (i96 -1 to i16*)", 1609329d15b5SVitaly Buka }, 1610329d15b5SVitaly Buka { 161152096ee9SVitaly Buka "i8 undef", 1612329d15b5SVitaly Buka "[0 x i8] zeroinitializer", 1613329d15b5SVitaly Buka }, 1614329d15b5SVitaly Buka { 1615329d15b5SVitaly Buka "i8 undef", 1616329d15b5SVitaly Buka "[0 x i8] undef", 1617329d15b5SVitaly Buka }, 1618329d15b5SVitaly Buka { 161952096ee9SVitaly Buka "i8 undef", 1620329d15b5SVitaly Buka "[5 x [0 x i8]] zeroinitializer", 1621329d15b5SVitaly Buka }, 1622329d15b5SVitaly Buka { 1623329d15b5SVitaly Buka "i8 undef", 1624329d15b5SVitaly Buka "[5 x [0 x i8]] undef", 1625329d15b5SVitaly Buka }, 1626329d15b5SVitaly Buka { 1627329d15b5SVitaly Buka "i8 0", 1628329d15b5SVitaly Buka "[6 x i8] zeroinitializer", 1629329d15b5SVitaly Buka }, 1630329d15b5SVitaly Buka { 1631329d15b5SVitaly Buka "i8 undef", 1632329d15b5SVitaly Buka "[6 x i8] undef", 1633329d15b5SVitaly Buka }, 1634329d15b5SVitaly Buka { 1635329d15b5SVitaly Buka "i8 1", 1636329d15b5SVitaly Buka "[5 x i8] [i8 1, i8 1, i8 1, i8 1, i8 1]", 1637329d15b5SVitaly Buka }, 1638329d15b5SVitaly Buka { 1639329d15b5SVitaly Buka "", 1640329d15b5SVitaly Buka "[5 x i64] [i64 1, i64 1, i64 1, i64 1, i64 1]", 1641329d15b5SVitaly Buka }, 1642329d15b5SVitaly Buka { 1643329d15b5SVitaly Buka "i8 -1", 1644329d15b5SVitaly Buka "[5 x i64] [i64 -1, i64 -1, i64 -1, i64 -1, i64 -1]", 1645329d15b5SVitaly Buka }, 1646329d15b5SVitaly Buka { 1647329d15b5SVitaly Buka "", 1648329d15b5SVitaly Buka "[4 x i8] [i8 1, i8 2, i8 1, i8 1]", 1649329d15b5SVitaly Buka }, 1650329d15b5SVitaly Buka { 1651329d15b5SVitaly Buka "i8 1", 1652329d15b5SVitaly Buka "[4 x i8] [i8 1, i8 undef, i8 1, i8 1]", 1653329d15b5SVitaly Buka }, 1654329d15b5SVitaly Buka { 1655329d15b5SVitaly Buka "i8 0", 1656329d15b5SVitaly Buka "<6 x i8> zeroinitializer", 1657329d15b5SVitaly Buka }, 1658329d15b5SVitaly Buka { 1659329d15b5SVitaly Buka "i8 undef", 1660329d15b5SVitaly Buka "<6 x i8> undef", 1661329d15b5SVitaly Buka }, 1662329d15b5SVitaly Buka { 1663329d15b5SVitaly Buka "i8 1", 1664329d15b5SVitaly Buka "<5 x i8> <i8 1, i8 1, i8 1, i8 1, i8 1>", 1665329d15b5SVitaly Buka }, 1666329d15b5SVitaly Buka { 1667329d15b5SVitaly Buka "", 1668329d15b5SVitaly Buka "<5 x i64> <i64 1, i64 1, i64 1, i64 1, i64 1>", 1669329d15b5SVitaly Buka }, 1670329d15b5SVitaly Buka { 1671329d15b5SVitaly Buka "i8 -1", 1672329d15b5SVitaly Buka "<5 x i64> <i64 -1, i64 -1, i64 -1, i64 -1, i64 -1>", 1673329d15b5SVitaly Buka }, 1674329d15b5SVitaly Buka { 1675329d15b5SVitaly Buka "", 1676329d15b5SVitaly Buka "<4 x i8> <i8 1, i8 1, i8 2, i8 1>", 1677329d15b5SVitaly Buka }, 1678329d15b5SVitaly Buka { 1679b1bff76eSVitaly Buka "i8 5", 1680329d15b5SVitaly Buka "<2 x i8> < i8 5, i8 undef >", 1681329d15b5SVitaly Buka }, 1682329d15b5SVitaly Buka { 1683329d15b5SVitaly Buka "i8 0", 1684329d15b5SVitaly Buka "[2 x [2 x i16]] zeroinitializer", 1685329d15b5SVitaly Buka }, 1686329d15b5SVitaly Buka { 1687329d15b5SVitaly Buka "i8 undef", 1688329d15b5SVitaly Buka "[2 x [2 x i16]] undef", 1689329d15b5SVitaly Buka }, 1690329d15b5SVitaly Buka { 1691329d15b5SVitaly Buka "i8 -86", 1692329d15b5SVitaly Buka "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], " 1693329d15b5SVitaly Buka "[2 x i16] [i16 -21846, i16 -21846]]", 1694329d15b5SVitaly Buka }, 1695329d15b5SVitaly Buka { 1696329d15b5SVitaly Buka "", 1697329d15b5SVitaly Buka "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], " 1698329d15b5SVitaly Buka "[2 x i16] [i16 -21836, i16 -21846]]", 1699329d15b5SVitaly Buka }, 1700329d15b5SVitaly Buka { 170152096ee9SVitaly Buka "i8 undef", 1702329d15b5SVitaly Buka "{ } zeroinitializer", 1703329d15b5SVitaly Buka }, 1704329d15b5SVitaly Buka { 1705329d15b5SVitaly Buka "i8 undef", 1706329d15b5SVitaly Buka "{ } undef", 1707329d15b5SVitaly Buka }, 1708329d15b5SVitaly Buka { 170952096ee9SVitaly Buka "i8 undef", 1710329d15b5SVitaly Buka "{ {}, {} } zeroinitializer", 1711329d15b5SVitaly Buka }, 1712329d15b5SVitaly Buka { 1713329d15b5SVitaly Buka "i8 undef", 1714329d15b5SVitaly Buka "{ {}, {} } undef", 1715329d15b5SVitaly Buka }, 1716329d15b5SVitaly Buka { 1717329d15b5SVitaly Buka "i8 0", 1718329d15b5SVitaly Buka "{i8, i64, i16*} zeroinitializer", 1719329d15b5SVitaly Buka }, 1720329d15b5SVitaly Buka { 1721329d15b5SVitaly Buka "i8 undef", 1722329d15b5SVitaly Buka "{i8, i64, i16*} undef", 1723329d15b5SVitaly Buka }, 1724329d15b5SVitaly Buka { 1725329d15b5SVitaly Buka "i8 -86", 1726329d15b5SVitaly Buka "{i8, i64, i16*} {i8 -86, i64 -6148914691236517206, i16* undef}", 1727329d15b5SVitaly Buka }, 1728329d15b5SVitaly Buka { 1729329d15b5SVitaly Buka "", 1730329d15b5SVitaly Buka "{i8, i64, i16*} {i8 86, i64 -6148914691236517206, i16* undef}", 1731329d15b5SVitaly Buka }, 1732329d15b5SVitaly Buka }; 1733329d15b5SVitaly Buka 1734329d15b5SVitaly Buka INSTANTIATE_TEST_CASE_P(IsBytewiseValueParamTests, IsBytewiseValueTest, 17357618b2a5SMikael Holmen ::testing::ValuesIn(IsBytewiseValueTests),); 1736329d15b5SVitaly Buka 1737329d15b5SVitaly Buka TEST_P(IsBytewiseValueTest, IsBytewiseValue) { 1738329d15b5SVitaly Buka auto M = parseModule(std::string("@test = global ") + GetParam().second); 1739329d15b5SVitaly Buka GlobalVariable *GV = dyn_cast<GlobalVariable>(M->getNamedValue("test")); 1740329d15b5SVitaly Buka Value *Actual = isBytewiseValue(GV->getInitializer(), M->getDataLayout()); 1741329d15b5SVitaly Buka std::string Buff; 1742329d15b5SVitaly Buka raw_string_ostream S(Buff); 1743329d15b5SVitaly Buka if (Actual) 1744329d15b5SVitaly Buka S << *Actual; 1745329d15b5SVitaly Buka EXPECT_EQ(GetParam().first, S.str()); 1746329d15b5SVitaly Buka } 17478d041811SFlorian Hahn 17488d041811SFlorian Hahn TEST_F(ValueTrackingTest, ComputeConstantRange) { 17498d041811SFlorian Hahn { 17508d041811SFlorian Hahn // Assumptions: 17518d041811SFlorian Hahn // * stride >= 5 17528d041811SFlorian Hahn // * stride < 10 17538d041811SFlorian Hahn // 17548d041811SFlorian Hahn // stride = [5, 10) 17558d041811SFlorian Hahn auto M = parseModule(R"( 17568d041811SFlorian Hahn declare void @llvm.assume(i1) 17578d041811SFlorian Hahn 17588d041811SFlorian Hahn define i32 @test(i32 %stride) { 17598d041811SFlorian Hahn %gt = icmp uge i32 %stride, 5 17608d041811SFlorian Hahn call void @llvm.assume(i1 %gt) 17618d041811SFlorian Hahn %lt = icmp ult i32 %stride, 10 17628d041811SFlorian Hahn call void @llvm.assume(i1 %lt) 17638d041811SFlorian Hahn %stride.plus.one = add nsw nuw i32 %stride, 1 17648d041811SFlorian Hahn ret i32 %stride.plus.one 17658d041811SFlorian Hahn })"); 17668d041811SFlorian Hahn Function *F = M->getFunction("test"); 17678d041811SFlorian Hahn 17688d041811SFlorian Hahn AssumptionCache AC(*F); 17698d041811SFlorian Hahn Value *Stride = &*F->arg_begin(); 17708d041811SFlorian Hahn ConstantRange CR1 = computeConstantRange(Stride, true, &AC, nullptr); 17718d041811SFlorian Hahn EXPECT_TRUE(CR1.isFullSet()); 17728d041811SFlorian Hahn 17738d041811SFlorian Hahn Instruction *I = &findInstructionByName(F, "stride.plus.one"); 17748d041811SFlorian Hahn ConstantRange CR2 = computeConstantRange(Stride, true, &AC, I); 17758d041811SFlorian Hahn EXPECT_EQ(5, CR2.getLower()); 17768d041811SFlorian Hahn EXPECT_EQ(10, CR2.getUpper()); 17778d041811SFlorian Hahn } 17788d041811SFlorian Hahn 17798d041811SFlorian Hahn { 17808d041811SFlorian Hahn // Assumptions: 17818d041811SFlorian Hahn // * stride >= 5 17828d041811SFlorian Hahn // * stride < 200 17838d041811SFlorian Hahn // * stride == 99 17848d041811SFlorian Hahn // 17858d041811SFlorian Hahn // stride = [99, 100) 17868d041811SFlorian Hahn auto M = parseModule(R"( 17878d041811SFlorian Hahn declare void @llvm.assume(i1) 17888d041811SFlorian Hahn 17898d041811SFlorian Hahn define i32 @test(i32 %stride) { 17908d041811SFlorian Hahn %gt = icmp uge i32 %stride, 5 17918d041811SFlorian Hahn call void @llvm.assume(i1 %gt) 17928d041811SFlorian Hahn %lt = icmp ult i32 %stride, 200 17938d041811SFlorian Hahn call void @llvm.assume(i1 %lt) 17948d041811SFlorian Hahn %eq = icmp eq i32 %stride, 99 17958d041811SFlorian Hahn call void @llvm.assume(i1 %eq) 17968d041811SFlorian Hahn %stride.plus.one = add nsw nuw i32 %stride, 1 17978d041811SFlorian Hahn ret i32 %stride.plus.one 17988d041811SFlorian Hahn })"); 17998d041811SFlorian Hahn Function *F = M->getFunction("test"); 18008d041811SFlorian Hahn 18018d041811SFlorian Hahn AssumptionCache AC(*F); 18028d041811SFlorian Hahn Value *Stride = &*F->arg_begin(); 18038d041811SFlorian Hahn Instruction *I = &findInstructionByName(F, "stride.plus.one"); 18048d041811SFlorian Hahn ConstantRange CR = computeConstantRange(Stride, true, &AC, I); 18058d041811SFlorian Hahn EXPECT_EQ(99, *CR.getSingleElement()); 18068d041811SFlorian Hahn } 18078d041811SFlorian Hahn 18088d041811SFlorian Hahn { 18098d041811SFlorian Hahn // Assumptions: 18108d041811SFlorian Hahn // * stride >= 5 18118d041811SFlorian Hahn // * stride >= 50 18128d041811SFlorian Hahn // * stride < 100 18138d041811SFlorian Hahn // * stride < 200 18148d041811SFlorian Hahn // 18158d041811SFlorian Hahn // stride = [50, 100) 18168d041811SFlorian Hahn auto M = parseModule(R"( 18178d041811SFlorian Hahn declare void @llvm.assume(i1) 18188d041811SFlorian Hahn 18198d041811SFlorian Hahn define i32 @test(i32 %stride, i1 %cond) { 18208d041811SFlorian Hahn %gt = icmp uge i32 %stride, 5 18218d041811SFlorian Hahn call void @llvm.assume(i1 %gt) 18228d041811SFlorian Hahn %gt.2 = icmp uge i32 %stride, 50 18238d041811SFlorian Hahn call void @llvm.assume(i1 %gt.2) 18248d041811SFlorian Hahn br i1 %cond, label %bb1, label %bb2 18258d041811SFlorian Hahn 18268d041811SFlorian Hahn bb1: 18278d041811SFlorian Hahn %lt = icmp ult i32 %stride, 200 18288d041811SFlorian Hahn call void @llvm.assume(i1 %lt) 18298d041811SFlorian Hahn %lt.2 = icmp ult i32 %stride, 100 18308d041811SFlorian Hahn call void @llvm.assume(i1 %lt.2) 18318d041811SFlorian Hahn %stride.plus.one = add nsw nuw i32 %stride, 1 18328d041811SFlorian Hahn ret i32 %stride.plus.one 18338d041811SFlorian Hahn 18348d041811SFlorian Hahn bb2: 18358d041811SFlorian Hahn ret i32 0 18368d041811SFlorian Hahn })"); 18378d041811SFlorian Hahn Function *F = M->getFunction("test"); 18388d041811SFlorian Hahn 18398d041811SFlorian Hahn AssumptionCache AC(*F); 18408d041811SFlorian Hahn Value *Stride = &*F->arg_begin(); 18418d041811SFlorian Hahn Instruction *GT2 = &findInstructionByName(F, "gt.2"); 18428d041811SFlorian Hahn ConstantRange CR = computeConstantRange(Stride, true, &AC, GT2); 18438d041811SFlorian Hahn EXPECT_EQ(5, CR.getLower()); 18448d041811SFlorian Hahn EXPECT_EQ(0, CR.getUpper()); 18458d041811SFlorian Hahn 18468d041811SFlorian Hahn Instruction *I = &findInstructionByName(F, "stride.plus.one"); 18478d041811SFlorian Hahn ConstantRange CR2 = computeConstantRange(Stride, true, &AC, I); 18488d041811SFlorian Hahn EXPECT_EQ(50, CR2.getLower()); 18498d041811SFlorian Hahn EXPECT_EQ(100, CR2.getUpper()); 18508d041811SFlorian Hahn } 18518d041811SFlorian Hahn 18528d041811SFlorian Hahn { 18538d041811SFlorian Hahn // Assumptions: 18548d041811SFlorian Hahn // * stride > 5 18558d041811SFlorian Hahn // * stride < 5 18568d041811SFlorian Hahn // 18578d041811SFlorian Hahn // stride = empty range, as the assumptions contradict each other. 18588d041811SFlorian Hahn auto M = parseModule(R"( 18598d041811SFlorian Hahn declare void @llvm.assume(i1) 18608d041811SFlorian Hahn 18618d041811SFlorian Hahn define i32 @test(i32 %stride, i1 %cond) { 18628d041811SFlorian Hahn %gt = icmp ugt i32 %stride, 5 18638d041811SFlorian Hahn call void @llvm.assume(i1 %gt) 18648d041811SFlorian Hahn %lt = icmp ult i32 %stride, 5 18658d041811SFlorian Hahn call void @llvm.assume(i1 %lt) 18668d041811SFlorian Hahn %stride.plus.one = add nsw nuw i32 %stride, 1 18678d041811SFlorian Hahn ret i32 %stride.plus.one 18688d041811SFlorian Hahn })"); 18698d041811SFlorian Hahn Function *F = M->getFunction("test"); 18708d041811SFlorian Hahn 18718d041811SFlorian Hahn AssumptionCache AC(*F); 18728d041811SFlorian Hahn Value *Stride = &*F->arg_begin(); 18738d041811SFlorian Hahn 18748d041811SFlorian Hahn Instruction *I = &findInstructionByName(F, "stride.plus.one"); 18758d041811SFlorian Hahn ConstantRange CR = computeConstantRange(Stride, true, &AC, I); 18768d041811SFlorian Hahn EXPECT_TRUE(CR.isEmptySet()); 18778d041811SFlorian Hahn } 18788d041811SFlorian Hahn 18798d041811SFlorian Hahn { 18808d041811SFlorian Hahn // Assumptions: 18818d041811SFlorian Hahn // * x.1 >= 5 18828d041811SFlorian Hahn // * x.2 < x.1 18838d041811SFlorian Hahn // 18848d041811SFlorian Hahn // stride = [0, 5) 18858d041811SFlorian Hahn auto M = parseModule(R"( 18868d041811SFlorian Hahn declare void @llvm.assume(i1) 18878d041811SFlorian Hahn 18888d041811SFlorian Hahn define i32 @test(i32 %x.1, i32 %x.2) { 18898d041811SFlorian Hahn %gt = icmp uge i32 %x.1, 5 18908d041811SFlorian Hahn call void @llvm.assume(i1 %gt) 18918d041811SFlorian Hahn %lt = icmp ult i32 %x.2, %x.1 18928d041811SFlorian Hahn call void @llvm.assume(i1 %lt) 18938d041811SFlorian Hahn %stride.plus.one = add nsw nuw i32 %x.1, 1 18948d041811SFlorian Hahn ret i32 %stride.plus.one 18958d041811SFlorian Hahn })"); 18968d041811SFlorian Hahn Function *F = M->getFunction("test"); 18978d041811SFlorian Hahn 18988d041811SFlorian Hahn AssumptionCache AC(*F); 18998d041811SFlorian Hahn Value *X2 = &*std::next(F->arg_begin()); 19008d041811SFlorian Hahn 19018d041811SFlorian Hahn Instruction *I = &findInstructionByName(F, "stride.plus.one"); 19028d041811SFlorian Hahn ConstantRange CR1 = computeConstantRange(X2, true, &AC, I); 19038d041811SFlorian Hahn EXPECT_EQ(0, CR1.getLower()); 19048d041811SFlorian Hahn EXPECT_EQ(5, CR1.getUpper()); 19058d041811SFlorian Hahn 19068d041811SFlorian Hahn // Check the depth cutoff results in a conservative result (full set) by 19078d041811SFlorian Hahn // passing Depth == MaxDepth == 6. 19088d041811SFlorian Hahn ConstantRange CR2 = computeConstantRange(X2, true, &AC, I, 6); 19098d041811SFlorian Hahn EXPECT_TRUE(CR2.isFullSet()); 19108d041811SFlorian Hahn } 19118d041811SFlorian Hahn } 19127f1bb326SVitaly Buka 19136ccacb46SVitaly Buka struct FindAllocaForValueTestParams { 19146ccacb46SVitaly Buka const char *IR; 1915a6927c86SVitaly Buka bool AnyOffsetResult; 1916a6927c86SVitaly Buka bool ZeroOffsetResult; 19176ccacb46SVitaly Buka }; 19186ccacb46SVitaly Buka 19197f1bb326SVitaly Buka class FindAllocaForValueTest 19207f1bb326SVitaly Buka : public ValueTrackingTest, 19216ccacb46SVitaly Buka public ::testing::WithParamInterface<FindAllocaForValueTestParams> { 19227f1bb326SVitaly Buka protected: 19237f1bb326SVitaly Buka }; 19247f1bb326SVitaly Buka 19256ccacb46SVitaly Buka const FindAllocaForValueTestParams FindAllocaForValueTests[] = { 19267f1bb326SVitaly Buka {R"( 19277f1bb326SVitaly Buka define void @test() { 19287f1bb326SVitaly Buka %a = alloca i64 19297f1bb326SVitaly Buka %r = bitcast i64* %a to i32* 19307f1bb326SVitaly Buka ret void 19317f1bb326SVitaly Buka })", 1932a6927c86SVitaly Buka true, true}, 19337f1bb326SVitaly Buka 19347f1bb326SVitaly Buka {R"( 19357f1bb326SVitaly Buka define void @test() { 19367f1bb326SVitaly Buka %a = alloca i32 19377f1bb326SVitaly Buka %r = getelementptr i32, i32* %a, i32 1 19387f1bb326SVitaly Buka ret void 19397f1bb326SVitaly Buka })", 1940a6927c86SVitaly Buka true, false}, 1941a6927c86SVitaly Buka 1942a6927c86SVitaly Buka {R"( 1943a6927c86SVitaly Buka define void @test() { 1944a6927c86SVitaly Buka %a = alloca i32 1945a6927c86SVitaly Buka %r = getelementptr i32, i32* %a, i32 0 1946a6927c86SVitaly Buka ret void 1947a6927c86SVitaly Buka })", 1948a6927c86SVitaly Buka true, true}, 19497f1bb326SVitaly Buka 19507f1bb326SVitaly Buka {R"( 19517f1bb326SVitaly Buka define void @test(i1 %cond) { 19527f1bb326SVitaly Buka entry: 19537f1bb326SVitaly Buka %a = alloca i32 19547f1bb326SVitaly Buka br label %bb1 19557f1bb326SVitaly Buka 19567f1bb326SVitaly Buka bb1: 19577f1bb326SVitaly Buka %r = phi i32* [ %a, %entry ], [ %r, %bb1 ] 19587f1bb326SVitaly Buka br i1 %cond, label %bb1, label %exit 19597f1bb326SVitaly Buka 19607f1bb326SVitaly Buka exit: 19617f1bb326SVitaly Buka ret void 19627f1bb326SVitaly Buka })", 1963a6927c86SVitaly Buka true, true}, 19647f1bb326SVitaly Buka 19657f1bb326SVitaly Buka {R"( 19667f1bb326SVitaly Buka define void @test(i1 %cond) { 1967469debe0SVitaly Buka %a = alloca i32 1968469debe0SVitaly Buka %r = select i1 %cond, i32* %a, i32* %a 1969469debe0SVitaly Buka ret void 1970469debe0SVitaly Buka })", 1971a6927c86SVitaly Buka true, true}, 1972469debe0SVitaly Buka 1973469debe0SVitaly Buka {R"( 1974469debe0SVitaly Buka define void @test(i1 %cond) { 1975469debe0SVitaly Buka %a = alloca i32 1976469debe0SVitaly Buka %b = alloca i32 1977469debe0SVitaly Buka %r = select i1 %cond, i32* %a, i32* %b 1978469debe0SVitaly Buka ret void 1979469debe0SVitaly Buka })", 1980a6927c86SVitaly Buka false, false}, 1981469debe0SVitaly Buka 1982469debe0SVitaly Buka {R"( 1983469debe0SVitaly Buka define void @test(i1 %cond) { 19847f1bb326SVitaly Buka entry: 19857f1bb326SVitaly Buka %a = alloca i64 19867f1bb326SVitaly Buka %a32 = bitcast i64* %a to i32* 19877f1bb326SVitaly Buka br label %bb1 19887f1bb326SVitaly Buka 19897f1bb326SVitaly Buka bb1: 19907f1bb326SVitaly Buka %x = phi i32* [ %a32, %entry ], [ %x, %bb1 ] 19917f1bb326SVitaly Buka %r = getelementptr i32, i32* %x, i32 1 19927f1bb326SVitaly Buka br i1 %cond, label %bb1, label %exit 19937f1bb326SVitaly Buka 19947f1bb326SVitaly Buka exit: 19957f1bb326SVitaly Buka ret void 19967f1bb326SVitaly Buka })", 1997a6927c86SVitaly Buka true, false}, 19987f1bb326SVitaly Buka 19997f1bb326SVitaly Buka {R"( 20007f1bb326SVitaly Buka define void @test(i1 %cond) { 20017f1bb326SVitaly Buka entry: 20027f1bb326SVitaly Buka %a = alloca i64 20037f1bb326SVitaly Buka %a32 = bitcast i64* %a to i32* 20047f1bb326SVitaly Buka br label %bb1 20057f1bb326SVitaly Buka 20067f1bb326SVitaly Buka bb1: 20077f1bb326SVitaly Buka %x = phi i32* [ %a32, %entry ], [ %r, %bb1 ] 20087f1bb326SVitaly Buka %r = getelementptr i32, i32* %x, i32 1 20097f1bb326SVitaly Buka br i1 %cond, label %bb1, label %exit 20107f1bb326SVitaly Buka 20117f1bb326SVitaly Buka exit: 20127f1bb326SVitaly Buka ret void 20137f1bb326SVitaly Buka })", 201423524fdeSVitaly Buka true, false}, 20157f1bb326SVitaly Buka 20167f1bb326SVitaly Buka {R"( 20177f1bb326SVitaly Buka define void @test(i1 %cond, i64* %a) { 20187f1bb326SVitaly Buka entry: 20197f1bb326SVitaly Buka %r = bitcast i64* %a to i32* 20207f1bb326SVitaly Buka ret void 20217f1bb326SVitaly Buka })", 2022a6927c86SVitaly Buka false, false}, 20237f1bb326SVitaly Buka 20247f1bb326SVitaly Buka {R"( 20257f1bb326SVitaly Buka define void @test(i1 %cond) { 20267f1bb326SVitaly Buka entry: 20277f1bb326SVitaly Buka %a = alloca i32 20287f1bb326SVitaly Buka %b = alloca i32 20297f1bb326SVitaly Buka br label %bb1 20307f1bb326SVitaly Buka 20317f1bb326SVitaly Buka bb1: 20328191603dSVitaly Buka %r = phi i32* [ %a, %entry ], [ %b, %bb1 ] 20337f1bb326SVitaly Buka br i1 %cond, label %bb1, label %exit 20347f1bb326SVitaly Buka 20357f1bb326SVitaly Buka exit: 20367f1bb326SVitaly Buka ret void 20377f1bb326SVitaly Buka })", 2038a6927c86SVitaly Buka false, false}, 20397f1bb326SVitaly Buka }; 20407f1bb326SVitaly Buka 20417f1bb326SVitaly Buka TEST_P(FindAllocaForValueTest, findAllocaForValue) { 20426ccacb46SVitaly Buka auto M = parseModule(GetParam().IR); 20437f1bb326SVitaly Buka Function *F = M->getFunction("test"); 20447f1bb326SVitaly Buka Instruction *I = &findInstructionByName(F, "r"); 20457f1bb326SVitaly Buka const AllocaInst *AI = findAllocaForValue(I); 2046a6927c86SVitaly Buka EXPECT_EQ(!!AI, GetParam().AnyOffsetResult); 2047a6927c86SVitaly Buka } 2048a6927c86SVitaly Buka 2049a6927c86SVitaly Buka TEST_P(FindAllocaForValueTest, findAllocaForValueZeroOffset) { 2050a6927c86SVitaly Buka auto M = parseModule(GetParam().IR); 2051a6927c86SVitaly Buka Function *F = M->getFunction("test"); 2052a6927c86SVitaly Buka Instruction *I = &findInstructionByName(F, "r"); 2053a6927c86SVitaly Buka const AllocaInst *AI = findAllocaForValue(I, true); 2054a6927c86SVitaly Buka EXPECT_EQ(!!AI, GetParam().ZeroOffsetResult); 20557f1bb326SVitaly Buka } 20567f1bb326SVitaly Buka 20577f1bb326SVitaly Buka INSTANTIATE_TEST_CASE_P(FindAllocaForValueTest, FindAllocaForValueTest, 20587f1bb326SVitaly Buka ::testing::ValuesIn(FindAllocaForValueTests), ); 2059