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" 22134bec27SJames Molloy #include "gtest/gtest.h" 23134bec27SJames Molloy 24134bec27SJames Molloy using namespace llvm; 25134bec27SJames Molloy 26134bec27SJames Molloy namespace { 27134bec27SJames Molloy 289b3c2a72SJuneyoung Lee static Instruction *findInstructionByNameOrNull(Function *F, StringRef Name) { 298d041811SFlorian Hahn for (Instruction &I : instructions(F)) 308d041811SFlorian Hahn if (I.getName() == Name) 319b3c2a72SJuneyoung Lee return &I; 329b3c2a72SJuneyoung Lee 339b3c2a72SJuneyoung Lee return nullptr; 349b3c2a72SJuneyoung Lee } 359b3c2a72SJuneyoung Lee 369b3c2a72SJuneyoung Lee static Instruction &findInstructionByName(Function *F, StringRef Name) { 379b3c2a72SJuneyoung Lee auto *I = findInstructionByNameOrNull(F, Name); 389b3c2a72SJuneyoung Lee if (I) 399b3c2a72SJuneyoung Lee return *I; 408d041811SFlorian Hahn 418d041811SFlorian Hahn llvm_unreachable("Expected value not found"); 428d041811SFlorian Hahn } 438d041811SFlorian Hahn 44219e5367SNikita Popov class ValueTrackingTest : public testing::Test { 45134bec27SJames Molloy protected: 46329d15b5SVitaly Buka std::unique_ptr<Module> parseModule(StringRef Assembly) { 47134bec27SJames Molloy SMDiagnostic Error; 48329d15b5SVitaly Buka std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context); 49134bec27SJames Molloy 50134bec27SJames Molloy std::string errMsg; 51134bec27SJames Molloy raw_string_ostream os(errMsg); 52134bec27SJames Molloy Error.print("", os); 53329d15b5SVitaly Buka EXPECT_TRUE(M) << os.str(); 54134bec27SJames Molloy 55329d15b5SVitaly Buka return M; 56329d15b5SVitaly Buka } 57329d15b5SVitaly Buka 58329d15b5SVitaly Buka void parseAssembly(StringRef Assembly) { 59329d15b5SVitaly Buka M = parseModule(Assembly); 60329d15b5SVitaly Buka ASSERT_TRUE(M); 61134bec27SJames Molloy 62d86fff6aSNikita Popov F = M->getFunction("test"); 63329d15b5SVitaly Buka ASSERT_TRUE(F) << "Test must have a function @test"; 64329d15b5SVitaly Buka if (!F) 65329d15b5SVitaly Buka return; 66134bec27SJames Molloy 679b3c2a72SJuneyoung Lee A = findInstructionByNameOrNull(F, "A"); 68329d15b5SVitaly Buka ASSERT_TRUE(A) << "@test must have an instruction %A"; 699b3c2a72SJuneyoung Lee 709b3c2a72SJuneyoung Lee CxtI = findInstructionByNameOrNull(F, "CxtI"); 719b3c2a72SJuneyoung Lee CxtI2 = findInstructionByNameOrNull(F, "CxtI2"); 729b3c2a72SJuneyoung Lee CxtI3 = findInstructionByNameOrNull(F, "CxtI3"); 73134bec27SJames Molloy } 74134bec27SJames Molloy 75219e5367SNikita Popov LLVMContext Context; 76219e5367SNikita Popov std::unique_ptr<Module> M; 77d86fff6aSNikita Popov Function *F = nullptr; 78329d15b5SVitaly Buka Instruction *A = nullptr; 799b3c2a72SJuneyoung Lee 809b3c2a72SJuneyoung Lee // Context instructions (optional) 819b3c2a72SJuneyoung Lee Instruction *CxtI = nullptr, *CxtI2 = nullptr, *CxtI3 = nullptr; 82219e5367SNikita Popov }; 83219e5367SNikita Popov 84219e5367SNikita Popov class MatchSelectPatternTest : public ValueTrackingTest { 85219e5367SNikita Popov protected: 86134bec27SJames Molloy void expectPattern(const SelectPatternResult &P) { 87134bec27SJames Molloy Value *LHS, *RHS; 88134bec27SJames Molloy Instruction::CastOps CastOp; 89134bec27SJames Molloy SelectPatternResult R = matchSelectPattern(A, LHS, RHS, &CastOp); 90134bec27SJames Molloy EXPECT_EQ(P.Flavor, R.Flavor); 91134bec27SJames Molloy EXPECT_EQ(P.NaNBehavior, R.NaNBehavior); 92134bec27SJames Molloy EXPECT_EQ(P.Ordered, R.Ordered); 93134bec27SJames Molloy } 94219e5367SNikita Popov }; 95134bec27SJames Molloy 96219e5367SNikita Popov class ComputeKnownBitsTest : public ValueTrackingTest { 97219e5367SNikita Popov protected: 98219e5367SNikita Popov void expectKnownBits(uint64_t Zero, uint64_t One) { 99219e5367SNikita Popov auto Known = computeKnownBits(A, M->getDataLayout()); 100219e5367SNikita Popov ASSERT_FALSE(Known.hasConflict()); 101219e5367SNikita Popov EXPECT_EQ(Known.One.getZExtValue(), One); 102219e5367SNikita Popov EXPECT_EQ(Known.Zero.getZExtValue(), Zero); 103219e5367SNikita Popov } 104134bec27SJames Molloy }; 105134bec27SJames Molloy 106134bec27SJames Molloy } 107134bec27SJames Molloy 108134bec27SJames Molloy TEST_F(MatchSelectPatternTest, SimpleFMin) { 109134bec27SJames Molloy parseAssembly( 110134bec27SJames Molloy "define float @test(float %a) {\n" 111134bec27SJames Molloy " %1 = fcmp ult float %a, 5.0\n" 112134bec27SJames Molloy " %A = select i1 %1, float %a, float 5.0\n" 113134bec27SJames Molloy " ret float %A\n" 114134bec27SJames Molloy "}\n"); 115134bec27SJames Molloy expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false}); 116134bec27SJames Molloy } 117134bec27SJames Molloy 118134bec27SJames Molloy TEST_F(MatchSelectPatternTest, SimpleFMax) { 119134bec27SJames Molloy parseAssembly( 120134bec27SJames Molloy "define float @test(float %a) {\n" 121134bec27SJames Molloy " %1 = fcmp ogt float %a, 5.0\n" 122134bec27SJames Molloy " %A = select i1 %1, float %a, float 5.0\n" 123134bec27SJames Molloy " ret float %A\n" 124134bec27SJames Molloy "}\n"); 125134bec27SJames Molloy expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true}); 126134bec27SJames Molloy } 127134bec27SJames Molloy 128134bec27SJames Molloy TEST_F(MatchSelectPatternTest, SwappedFMax) { 129134bec27SJames Molloy parseAssembly( 130134bec27SJames Molloy "define float @test(float %a) {\n" 131134bec27SJames Molloy " %1 = fcmp olt float 5.0, %a\n" 132134bec27SJames Molloy " %A = select i1 %1, float %a, float 5.0\n" 133134bec27SJames Molloy " ret float %A\n" 134134bec27SJames Molloy "}\n"); 135134bec27SJames Molloy expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, false}); 136134bec27SJames Molloy } 137134bec27SJames Molloy 138134bec27SJames Molloy TEST_F(MatchSelectPatternTest, SwappedFMax2) { 139134bec27SJames Molloy parseAssembly( 140134bec27SJames Molloy "define float @test(float %a) {\n" 141134bec27SJames Molloy " %1 = fcmp olt float %a, 5.0\n" 142134bec27SJames Molloy " %A = select i1 %1, float 5.0, float %a\n" 143134bec27SJames Molloy " ret float %A\n" 144134bec27SJames Molloy "}\n"); 145134bec27SJames Molloy expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, false}); 146134bec27SJames Molloy } 147134bec27SJames Molloy 148134bec27SJames Molloy TEST_F(MatchSelectPatternTest, SwappedFMax3) { 149134bec27SJames Molloy parseAssembly( 150134bec27SJames Molloy "define float @test(float %a) {\n" 151134bec27SJames Molloy " %1 = fcmp ult float %a, 5.0\n" 152134bec27SJames Molloy " %A = select i1 %1, float 5.0, float %a\n" 153134bec27SJames Molloy " ret float %A\n" 154134bec27SJames Molloy "}\n"); 155134bec27SJames Molloy expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true}); 156134bec27SJames Molloy } 157134bec27SJames Molloy 158134bec27SJames Molloy TEST_F(MatchSelectPatternTest, FastFMin) { 159134bec27SJames Molloy parseAssembly( 160134bec27SJames Molloy "define float @test(float %a) {\n" 161134bec27SJames Molloy " %1 = fcmp nnan olt float %a, 5.0\n" 162134bec27SJames Molloy " %A = select i1 %1, float %a, float 5.0\n" 163134bec27SJames Molloy " ret float %A\n" 164134bec27SJames Molloy "}\n"); 165134bec27SJames Molloy expectPattern({SPF_FMINNUM, SPNB_RETURNS_ANY, false}); 166134bec27SJames Molloy } 167134bec27SJames Molloy 168134bec27SJames Molloy TEST_F(MatchSelectPatternTest, FMinConstantZero) { 169134bec27SJames Molloy parseAssembly( 170134bec27SJames Molloy "define float @test(float %a) {\n" 171134bec27SJames Molloy " %1 = fcmp ole float %a, 0.0\n" 172134bec27SJames Molloy " %A = select i1 %1, float %a, float 0.0\n" 173134bec27SJames Molloy " ret float %A\n" 174134bec27SJames Molloy "}\n"); 175134bec27SJames Molloy // This shouldn't be matched, as %a could be -0.0. 176134bec27SJames Molloy expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 177134bec27SJames Molloy } 178134bec27SJames Molloy 179134bec27SJames Molloy TEST_F(MatchSelectPatternTest, FMinConstantZeroNsz) { 180134bec27SJames Molloy parseAssembly( 181134bec27SJames Molloy "define float @test(float %a) {\n" 182134bec27SJames Molloy " %1 = fcmp nsz ole float %a, 0.0\n" 183134bec27SJames Molloy " %A = select i1 %1, float %a, float 0.0\n" 184134bec27SJames Molloy " ret float %A\n" 185134bec27SJames Molloy "}\n"); 186134bec27SJames Molloy // But this should be, because we've ignored signed zeroes. 187134bec27SJames Molloy expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true}); 188134bec27SJames Molloy } 189569cea65SJames Molloy 19003da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero1) { 19103da6e6aSSanjay Patel parseAssembly( 19203da6e6aSSanjay Patel "define float @test(float %a) {\n" 19303da6e6aSSanjay Patel " %1 = fcmp olt float -0.0, %a\n" 19403da6e6aSSanjay Patel " %A = select i1 %1, float 0.0, float %a\n" 19503da6e6aSSanjay Patel " ret float %A\n" 19603da6e6aSSanjay Patel "}\n"); 197e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 198e7c94ef1SSanjay Patel expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, true}); 19903da6e6aSSanjay Patel } 20003da6e6aSSanjay Patel 20103da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero2) { 20203da6e6aSSanjay Patel parseAssembly( 20303da6e6aSSanjay Patel "define float @test(float %a) {\n" 20403da6e6aSSanjay Patel " %1 = fcmp ogt float %a, -0.0\n" 20503da6e6aSSanjay Patel " %A = select i1 %1, float 0.0, float %a\n" 20603da6e6aSSanjay Patel " ret float %A\n" 20703da6e6aSSanjay Patel "}\n"); 208e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 209e7c94ef1SSanjay Patel expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false}); 21003da6e6aSSanjay Patel } 21103da6e6aSSanjay Patel 21203da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero3) { 21303da6e6aSSanjay Patel parseAssembly( 21403da6e6aSSanjay Patel "define float @test(float %a) {\n" 21503da6e6aSSanjay Patel " %1 = fcmp olt float 0.0, %a\n" 21603da6e6aSSanjay Patel " %A = select i1 %1, float -0.0, float %a\n" 21703da6e6aSSanjay Patel " ret float %A\n" 21803da6e6aSSanjay Patel "}\n"); 219e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 220e7c94ef1SSanjay Patel expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, true}); 22103da6e6aSSanjay Patel } 22203da6e6aSSanjay Patel 22303da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero4) { 22403da6e6aSSanjay Patel parseAssembly( 22503da6e6aSSanjay Patel "define float @test(float %a) {\n" 22603da6e6aSSanjay Patel " %1 = fcmp ogt float %a, 0.0\n" 22703da6e6aSSanjay Patel " %A = select i1 %1, float -0.0, float %a\n" 22803da6e6aSSanjay Patel " ret float %A\n" 22903da6e6aSSanjay Patel "}\n"); 230e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 231e7c94ef1SSanjay Patel expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false}); 23203da6e6aSSanjay Patel } 23303da6e6aSSanjay Patel 23403da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero5) { 23503da6e6aSSanjay Patel parseAssembly( 23603da6e6aSSanjay Patel "define float @test(float %a) {\n" 23703da6e6aSSanjay Patel " %1 = fcmp ogt float -0.0, %a\n" 23803da6e6aSSanjay Patel " %A = select i1 %1, float %a, float 0.0\n" 23903da6e6aSSanjay Patel " ret float %A\n" 24003da6e6aSSanjay Patel "}\n"); 241e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 242e7c94ef1SSanjay Patel expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, false}); 24303da6e6aSSanjay Patel } 24403da6e6aSSanjay Patel 24503da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero6) { 24603da6e6aSSanjay Patel parseAssembly( 24703da6e6aSSanjay Patel "define float @test(float %a) {\n" 24803da6e6aSSanjay Patel " %1 = fcmp olt float %a, -0.0\n" 24903da6e6aSSanjay Patel " %A = select i1 %1, float %a, float 0.0\n" 25003da6e6aSSanjay Patel " ret float %A\n" 25103da6e6aSSanjay Patel "}\n"); 252e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 253e7c94ef1SSanjay Patel expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true}); 25403da6e6aSSanjay Patel } 25503da6e6aSSanjay Patel 25603da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero7) { 25703da6e6aSSanjay Patel parseAssembly( 25803da6e6aSSanjay Patel "define float @test(float %a) {\n" 25903da6e6aSSanjay Patel " %1 = fcmp ogt float 0.0, %a\n" 26003da6e6aSSanjay Patel " %A = select i1 %1, float %a, float -0.0\n" 26103da6e6aSSanjay Patel " ret float %A\n" 26203da6e6aSSanjay Patel "}\n"); 263e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 264e7c94ef1SSanjay Patel expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, false}); 26503da6e6aSSanjay Patel } 26603da6e6aSSanjay Patel 26703da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero8) { 26803da6e6aSSanjay Patel parseAssembly( 26903da6e6aSSanjay Patel "define float @test(float %a) {\n" 27003da6e6aSSanjay Patel " %1 = fcmp olt float %a, 0.0\n" 27103da6e6aSSanjay Patel " %A = select i1 %1, float %a, float -0.0\n" 27203da6e6aSSanjay Patel " ret float %A\n" 27303da6e6aSSanjay Patel "}\n"); 274e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 275e7c94ef1SSanjay Patel expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true}); 27603da6e6aSSanjay Patel } 27703da6e6aSSanjay Patel 27803da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero1) { 27903da6e6aSSanjay Patel parseAssembly( 28003da6e6aSSanjay Patel "define float @test(float %a) {\n" 28103da6e6aSSanjay Patel " %1 = fcmp ogt float -0.0, %a\n" 28203da6e6aSSanjay Patel " %A = select i1 %1, float 0.0, float %a\n" 28303da6e6aSSanjay Patel " ret float %A\n" 28403da6e6aSSanjay Patel "}\n"); 285e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 286e7c94ef1SSanjay Patel expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, true}); 28703da6e6aSSanjay Patel } 28803da6e6aSSanjay Patel 28903da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero2) { 29003da6e6aSSanjay Patel parseAssembly( 29103da6e6aSSanjay Patel "define float @test(float %a) {\n" 29203da6e6aSSanjay Patel " %1 = fcmp olt float %a, -0.0\n" 29303da6e6aSSanjay Patel " %A = select i1 %1, float 0.0, float %a\n" 29403da6e6aSSanjay Patel " ret float %A\n" 29503da6e6aSSanjay Patel "}\n"); 296e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 297e7c94ef1SSanjay Patel expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, false}); 29803da6e6aSSanjay Patel } 29903da6e6aSSanjay Patel 30003da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero3) { 30103da6e6aSSanjay Patel parseAssembly( 30203da6e6aSSanjay Patel "define float @test(float %a) {\n" 30303da6e6aSSanjay Patel " %1 = fcmp ogt float 0.0, %a\n" 30403da6e6aSSanjay Patel " %A = select i1 %1, float -0.0, float %a\n" 30503da6e6aSSanjay Patel " ret float %A\n" 30603da6e6aSSanjay Patel "}\n"); 307e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 308e7c94ef1SSanjay Patel expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, true}); 30903da6e6aSSanjay Patel } 31003da6e6aSSanjay Patel 31103da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero4) { 31203da6e6aSSanjay Patel parseAssembly( 31303da6e6aSSanjay Patel "define float @test(float %a) {\n" 31403da6e6aSSanjay Patel " %1 = fcmp olt float %a, 0.0\n" 31503da6e6aSSanjay Patel " %A = select i1 %1, float -0.0, float %a\n" 31603da6e6aSSanjay Patel " ret float %A\n" 31703da6e6aSSanjay Patel "}\n"); 318e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 319e7c94ef1SSanjay Patel expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, false}); 32003da6e6aSSanjay Patel } 32103da6e6aSSanjay Patel 32203da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero5) { 32303da6e6aSSanjay Patel parseAssembly( 32403da6e6aSSanjay Patel "define float @test(float %a) {\n" 32503da6e6aSSanjay Patel " %1 = fcmp olt float -0.0, %a\n" 32603da6e6aSSanjay Patel " %A = select i1 %1, float %a, float 0.0\n" 32703da6e6aSSanjay Patel " ret float %A\n" 32803da6e6aSSanjay Patel "}\n"); 329e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 330e7c94ef1SSanjay Patel expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, false}); 33103da6e6aSSanjay Patel } 33203da6e6aSSanjay Patel 33303da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero6) { 33403da6e6aSSanjay Patel parseAssembly( 33503da6e6aSSanjay Patel "define float @test(float %a) {\n" 33603da6e6aSSanjay Patel " %1 = fcmp ogt float %a, -0.0\n" 33703da6e6aSSanjay Patel " %A = select i1 %1, float %a, float 0.0\n" 33803da6e6aSSanjay Patel " ret float %A\n" 33903da6e6aSSanjay Patel "}\n"); 340e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 341e7c94ef1SSanjay Patel expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true}); 34203da6e6aSSanjay Patel } 34303da6e6aSSanjay Patel 34403da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero7) { 34503da6e6aSSanjay Patel parseAssembly( 34603da6e6aSSanjay Patel "define float @test(float %a) {\n" 34703da6e6aSSanjay Patel " %1 = fcmp olt float 0.0, %a\n" 34803da6e6aSSanjay Patel " %A = select i1 %1, float %a, float -0.0\n" 34903da6e6aSSanjay Patel " ret float %A\n" 35003da6e6aSSanjay Patel "}\n"); 351e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 352e7c94ef1SSanjay Patel expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, false}); 35303da6e6aSSanjay Patel } 35403da6e6aSSanjay Patel 35503da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero8) { 35603da6e6aSSanjay Patel parseAssembly( 35703da6e6aSSanjay Patel "define float @test(float %a) {\n" 35803da6e6aSSanjay Patel " %1 = fcmp ogt float %a, 0.0\n" 35903da6e6aSSanjay Patel " %A = select i1 %1, float %a, float -0.0\n" 36003da6e6aSSanjay Patel " ret float %A\n" 36103da6e6aSSanjay Patel "}\n"); 362e7c94ef1SSanjay Patel // The sign of zero doesn't matter in fcmp. 363e7c94ef1SSanjay Patel expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true}); 364e7c94ef1SSanjay Patel } 365e7c94ef1SSanjay Patel 366e7c94ef1SSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZeroVecUndef) { 367e7c94ef1SSanjay Patel parseAssembly( 368e7c94ef1SSanjay Patel "define <2 x float> @test(<2 x float> %a) {\n" 369e7c94ef1SSanjay Patel " %1 = fcmp ogt <2 x float> %a, <float -0.0, float -0.0>\n" 370e7c94ef1SSanjay Patel " %A = select <2 x i1> %1, <2 x float> <float undef, float 0.0>, <2 x float> %a\n" 371e7c94ef1SSanjay Patel " ret <2 x float> %A\n" 372e7c94ef1SSanjay Patel "}\n"); 373e7c94ef1SSanjay Patel // An undef in a vector constant can not be back-propagated for this analysis. 374e7c94ef1SSanjay Patel expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 375e7c94ef1SSanjay Patel } 376e7c94ef1SSanjay Patel 377e7c94ef1SSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZeroVecUndef) { 378e7c94ef1SSanjay Patel parseAssembly( 379e7c94ef1SSanjay Patel "define <2 x float> @test(<2 x float> %a) {\n" 380e7c94ef1SSanjay Patel " %1 = fcmp ogt <2 x float> %a, zeroinitializer\n" 381e7c94ef1SSanjay Patel " %A = select <2 x i1> %1, <2 x float> %a, <2 x float> <float -0.0, float undef>\n" 382e7c94ef1SSanjay Patel " ret <2 x float> %A\n" 383e7c94ef1SSanjay Patel "}\n"); 384e7c94ef1SSanjay Patel // An undef in a vector constant can not be back-propagated for this analysis. 38503da6e6aSSanjay Patel expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 38603da6e6aSSanjay Patel } 38703da6e6aSSanjay Patel 38830f1d691SThomas Lively TEST_F(MatchSelectPatternTest, VectorFMinimum) { 389d47b5c7bSThomas Lively parseAssembly( 390d47b5c7bSThomas Lively "define <4 x float> @test(<4 x float> %a) {\n" 391d47b5c7bSThomas Lively " %1 = fcmp ule <4 x float> %a, \n" 392d47b5c7bSThomas Lively " <float 5.0, float 5.0, float 5.0, float 5.0>\n" 393d47b5c7bSThomas Lively " %A = select <4 x i1> %1, <4 x float> %a,\n" 394d47b5c7bSThomas Lively " <4 x float> <float 5.0, float 5.0, float 5.0, float 5.0>\n" 395d47b5c7bSThomas Lively " ret <4 x float> %A\n" 396d47b5c7bSThomas Lively "}\n"); 397d47b5c7bSThomas Lively // Check that pattern matching works on vectors where each lane has the same 398d47b5c7bSThomas Lively // unordered pattern. 399d47b5c7bSThomas Lively expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false}); 400d47b5c7bSThomas Lively } 401d47b5c7bSThomas Lively 402d47b5c7bSThomas Lively TEST_F(MatchSelectPatternTest, VectorFMinOtherOrdered) { 403d47b5c7bSThomas Lively parseAssembly( 404d47b5c7bSThomas Lively "define <4 x float> @test(<4 x float> %a) {\n" 405d47b5c7bSThomas Lively " %1 = fcmp ole <4 x float> %a, \n" 406d47b5c7bSThomas Lively " <float 5.0, float 5.0, float 5.0, float 5.0>\n" 407d47b5c7bSThomas Lively " %A = select <4 x i1> %1, <4 x float> %a,\n" 408d47b5c7bSThomas Lively " <4 x float> <float 5.0, float 5.0, float 5.0, float 5.0>\n" 409d47b5c7bSThomas Lively " ret <4 x float> %A\n" 410d47b5c7bSThomas Lively "}\n"); 411d47b5c7bSThomas Lively // Check that pattern matching works on vectors where each lane has the same 412d47b5c7bSThomas Lively // ordered pattern. 413d47b5c7bSThomas Lively expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true}); 414d47b5c7bSThomas Lively } 415d47b5c7bSThomas Lively 41630f1d691SThomas Lively TEST_F(MatchSelectPatternTest, VectorNotFMinimum) { 417d47b5c7bSThomas Lively parseAssembly( 418d47b5c7bSThomas Lively "define <4 x float> @test(<4 x float> %a) {\n" 419d47b5c7bSThomas Lively " %1 = fcmp ule <4 x float> %a, \n" 420d47b5c7bSThomas Lively " <float 5.0, float 0x7ff8000000000000, float 5.0, float 5.0>\n" 421d47b5c7bSThomas Lively " %A = select <4 x i1> %1, <4 x float> %a,\n" 422d47b5c7bSThomas Lively " <4 x float> <float 5.0, float 0x7ff8000000000000, float 5.0, float " 423d47b5c7bSThomas Lively "5.0>\n" 424d47b5c7bSThomas Lively " ret <4 x float> %A\n" 425d47b5c7bSThomas Lively "}\n"); 426d47b5c7bSThomas Lively // The lane that contains a NaN (0x7ff80...) behaves like a 427d47b5c7bSThomas Lively // non-NaN-propagating min and the other lines behave like a NaN-propagating 428d47b5c7bSThomas Lively // min, so check that neither is returned. 429d47b5c7bSThomas Lively expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 430d47b5c7bSThomas Lively } 431d47b5c7bSThomas Lively 432d47b5c7bSThomas Lively TEST_F(MatchSelectPatternTest, VectorNotFMinZero) { 433d47b5c7bSThomas Lively parseAssembly( 434d47b5c7bSThomas Lively "define <4 x float> @test(<4 x float> %a) {\n" 435d47b5c7bSThomas Lively " %1 = fcmp ule <4 x float> %a, \n" 436d47b5c7bSThomas Lively " <float 5.0, float -0.0, float 5.0, float 5.0>\n" 437d47b5c7bSThomas Lively " %A = select <4 x i1> %1, <4 x float> %a,\n" 438d47b5c7bSThomas Lively " <4 x float> <float 5.0, float 0.0, float 5.0, float 5.0>\n" 439d47b5c7bSThomas Lively " ret <4 x float> %A\n" 440d47b5c7bSThomas Lively "}\n"); 441d47b5c7bSThomas Lively // Always selects the second lane of %a if it is positive or negative zero, so 442d47b5c7bSThomas Lively // this is stricter than a min. 443d47b5c7bSThomas Lively expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 444d47b5c7bSThomas Lively } 445d47b5c7bSThomas Lively 446569cea65SJames Molloy TEST_F(MatchSelectPatternTest, DoubleCastU) { 447569cea65SJames Molloy parseAssembly( 448569cea65SJames Molloy "define i32 @test(i8 %a, i8 %b) {\n" 449569cea65SJames Molloy " %1 = icmp ult i8 %a, %b\n" 450569cea65SJames Molloy " %2 = zext i8 %a to i32\n" 451569cea65SJames Molloy " %3 = zext i8 %b to i32\n" 452569cea65SJames Molloy " %A = select i1 %1, i32 %2, i32 %3\n" 453569cea65SJames Molloy " ret i32 %A\n" 454569cea65SJames Molloy "}\n"); 455569cea65SJames Molloy // We should be able to look through the situation where we cast both operands 456569cea65SJames Molloy // to the select. 457569cea65SJames Molloy expectPattern({SPF_UMIN, SPNB_NA, false}); 458569cea65SJames Molloy } 459569cea65SJames Molloy 460569cea65SJames Molloy TEST_F(MatchSelectPatternTest, DoubleCastS) { 461569cea65SJames Molloy parseAssembly( 462569cea65SJames Molloy "define i32 @test(i8 %a, i8 %b) {\n" 463569cea65SJames Molloy " %1 = icmp slt i8 %a, %b\n" 464569cea65SJames Molloy " %2 = sext i8 %a to i32\n" 465569cea65SJames Molloy " %3 = sext i8 %b to i32\n" 466569cea65SJames Molloy " %A = select i1 %1, i32 %2, i32 %3\n" 467569cea65SJames Molloy " ret i32 %A\n" 468569cea65SJames Molloy "}\n"); 469569cea65SJames Molloy // We should be able to look through the situation where we cast both operands 470569cea65SJames Molloy // to the select. 471569cea65SJames Molloy expectPattern({SPF_SMIN, SPNB_NA, false}); 472569cea65SJames Molloy } 473569cea65SJames Molloy 474569cea65SJames Molloy TEST_F(MatchSelectPatternTest, DoubleCastBad) { 475569cea65SJames Molloy parseAssembly( 476569cea65SJames Molloy "define i32 @test(i8 %a, i8 %b) {\n" 477569cea65SJames Molloy " %1 = icmp ult i8 %a, %b\n" 478569cea65SJames Molloy " %2 = zext i8 %a to i32\n" 479569cea65SJames Molloy " %3 = sext i8 %b to i32\n" 480569cea65SJames Molloy " %A = select i1 %1, i32 %2, i32 %3\n" 481569cea65SJames Molloy " ret i32 %A\n" 482569cea65SJames Molloy "}\n"); 483687a8448SJames Molloy // The cast types here aren't the same, so we cannot match an UMIN. 484569cea65SJames Molloy expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 485569cea65SJames Molloy } 4863bb2dbd6SSanjoy Das 4876d343740SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotSMin) { 4886d343740SSanjay Patel parseAssembly( 4896d343740SSanjay Patel "define i8 @test(i8 %a, i8 %b) {\n" 4906d343740SSanjay Patel " %cmp = icmp sgt i8 %a, %b\n" 4916d343740SSanjay Patel " %an = xor i8 %a, -1\n" 4926d343740SSanjay Patel " %bn = xor i8 %b, -1\n" 4936d343740SSanjay Patel " %A = select i1 %cmp, i8 %an, i8 %bn\n" 4946d343740SSanjay Patel " ret i8 %A\n" 4956d343740SSanjay Patel "}\n"); 496867f0c3cSSanjay Patel expectPattern({SPF_SMIN, SPNB_NA, false}); 4976d343740SSanjay Patel } 4986d343740SSanjay Patel 4996d343740SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotSMinSwap) { 5006d343740SSanjay Patel parseAssembly( 501463143f0SSanjay Patel "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n" 502463143f0SSanjay Patel " %cmp = icmp slt <2 x i8> %a, %b\n" 503463143f0SSanjay Patel " %an = xor <2 x i8> %a, <i8 -1, i8-1>\n" 504463143f0SSanjay Patel " %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n" 505463143f0SSanjay Patel " %A = select <2 x i1> %cmp, <2 x i8> %bn, <2 x i8> %an\n" 506463143f0SSanjay Patel " ret <2 x i8> %A\n" 5076d343740SSanjay Patel "}\n"); 508867f0c3cSSanjay Patel expectPattern({SPF_SMIN, SPNB_NA, false}); 5096d343740SSanjay Patel } 5106d343740SSanjay Patel 5116d343740SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotSMax) { 5126d343740SSanjay Patel parseAssembly( 5136d343740SSanjay Patel "define i8 @test(i8 %a, i8 %b) {\n" 5146d343740SSanjay Patel " %cmp = icmp slt i8 %a, %b\n" 5156d343740SSanjay Patel " %an = xor i8 %a, -1\n" 5166d343740SSanjay Patel " %bn = xor i8 %b, -1\n" 5176d343740SSanjay Patel " %A = select i1 %cmp, i8 %an, i8 %bn\n" 5186d343740SSanjay Patel " ret i8 %A\n" 5196d343740SSanjay Patel "}\n"); 520867f0c3cSSanjay Patel expectPattern({SPF_SMAX, SPNB_NA, false}); 5216d343740SSanjay Patel } 5226d343740SSanjay Patel 5236d343740SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotSMaxSwap) { 5246d343740SSanjay Patel parseAssembly( 525463143f0SSanjay Patel "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n" 526463143f0SSanjay Patel " %cmp = icmp sgt <2 x i8> %a, %b\n" 527463143f0SSanjay Patel " %an = xor <2 x i8> %a, <i8 -1, i8-1>\n" 528463143f0SSanjay Patel " %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n" 529463143f0SSanjay Patel " %A = select <2 x i1> %cmp, <2 x i8> %bn, <2 x i8> %an\n" 530463143f0SSanjay Patel " ret <2 x i8> %A\n" 531463143f0SSanjay Patel "}\n"); 532463143f0SSanjay Patel expectPattern({SPF_SMAX, SPNB_NA, false}); 533463143f0SSanjay Patel } 534463143f0SSanjay Patel 535463143f0SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotUMin) { 536463143f0SSanjay Patel parseAssembly( 537463143f0SSanjay Patel "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n" 538463143f0SSanjay Patel " %cmp = icmp ugt <2 x i8> %a, %b\n" 539463143f0SSanjay Patel " %an = xor <2 x i8> %a, <i8 -1, i8-1>\n" 540463143f0SSanjay Patel " %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n" 541463143f0SSanjay Patel " %A = select <2 x i1> %cmp, <2 x i8> %an, <2 x i8> %bn\n" 542463143f0SSanjay Patel " ret <2 x i8> %A\n" 543463143f0SSanjay Patel "}\n"); 544fbb1b43fSSanjay Patel expectPattern({SPF_UMIN, SPNB_NA, false}); 545463143f0SSanjay Patel } 546463143f0SSanjay Patel 547463143f0SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotUMinSwap) { 548463143f0SSanjay Patel parseAssembly( 5496d343740SSanjay Patel "define i8 @test(i8 %a, i8 %b) {\n" 550463143f0SSanjay Patel " %cmp = icmp ult i8 %a, %b\n" 5516d343740SSanjay Patel " %an = xor i8 %a, -1\n" 5526d343740SSanjay Patel " %bn = xor i8 %b, -1\n" 5536d343740SSanjay Patel " %A = select i1 %cmp, i8 %bn, i8 %an\n" 5546d343740SSanjay Patel " ret i8 %A\n" 5556d343740SSanjay Patel "}\n"); 556fbb1b43fSSanjay Patel expectPattern({SPF_UMIN, SPNB_NA, false}); 557463143f0SSanjay Patel } 558463143f0SSanjay Patel 559463143f0SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotUMax) { 560463143f0SSanjay Patel parseAssembly( 561463143f0SSanjay Patel "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n" 562463143f0SSanjay Patel " %cmp = icmp ult <2 x i8> %a, %b\n" 563463143f0SSanjay Patel " %an = xor <2 x i8> %a, <i8 -1, i8-1>\n" 564463143f0SSanjay Patel " %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n" 565463143f0SSanjay Patel " %A = select <2 x i1> %cmp, <2 x i8> %an, <2 x i8> %bn\n" 566463143f0SSanjay Patel " ret <2 x i8> %A\n" 567463143f0SSanjay Patel "}\n"); 568fbb1b43fSSanjay Patel expectPattern({SPF_UMAX, SPNB_NA, false}); 569463143f0SSanjay Patel } 570463143f0SSanjay Patel 571463143f0SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotUMaxSwap) { 572463143f0SSanjay Patel parseAssembly( 573463143f0SSanjay Patel "define i8 @test(i8 %a, i8 %b) {\n" 574463143f0SSanjay Patel " %cmp = icmp ugt i8 %a, %b\n" 575463143f0SSanjay Patel " %an = xor i8 %a, -1\n" 576463143f0SSanjay Patel " %bn = xor i8 %b, -1\n" 577463143f0SSanjay Patel " %A = select i1 %cmp, i8 %bn, i8 %an\n" 578463143f0SSanjay Patel " ret i8 %A\n" 579463143f0SSanjay Patel "}\n"); 580fbb1b43fSSanjay Patel expectPattern({SPF_UMAX, SPNB_NA, false}); 581463143f0SSanjay Patel } 582463143f0SSanjay Patel 583463143f0SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotEq) { 584463143f0SSanjay Patel parseAssembly( 585463143f0SSanjay Patel "define i8 @test(i8 %a, i8 %b) {\n" 586463143f0SSanjay Patel " %cmp = icmp eq i8 %a, %b\n" 587463143f0SSanjay Patel " %an = xor i8 %a, -1\n" 588463143f0SSanjay Patel " %bn = xor i8 %b, -1\n" 589463143f0SSanjay Patel " %A = select i1 %cmp, i8 %bn, i8 %an\n" 590463143f0SSanjay Patel " ret i8 %A\n" 591463143f0SSanjay Patel "}\n"); 592463143f0SSanjay Patel expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 593463143f0SSanjay Patel } 594463143f0SSanjay Patel 595463143f0SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotNe) { 596463143f0SSanjay Patel parseAssembly( 597463143f0SSanjay Patel "define i8 @test(i8 %a, i8 %b) {\n" 598463143f0SSanjay Patel " %cmp = icmp ne i8 %a, %b\n" 599463143f0SSanjay Patel " %an = xor i8 %a, -1\n" 600463143f0SSanjay Patel " %bn = xor i8 %b, -1\n" 601463143f0SSanjay Patel " %A = select i1 %cmp, i8 %bn, i8 %an\n" 602463143f0SSanjay Patel " ret i8 %A\n" 603463143f0SSanjay Patel "}\n"); 604463143f0SSanjay Patel expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 6056d343740SSanjay Patel } 6066d343740SSanjay Patel 6073bb2dbd6SSanjoy Das TEST(ValueTracking, GuaranteedToTransferExecutionToSuccessor) { 6083bb2dbd6SSanjoy Das StringRef Assembly = 6093bb2dbd6SSanjoy Das "declare void @nounwind_readonly(i32*) nounwind readonly " 6103bb2dbd6SSanjoy Das "declare void @nounwind_argmemonly(i32*) nounwind argmemonly " 6113bb2dbd6SSanjoy Das "declare void @throws_but_readonly(i32*) readonly " 6123bb2dbd6SSanjoy Das "declare void @throws_but_argmemonly(i32*) argmemonly " 6136ed459fdSJohannes Doerfert "declare void @nounwind_willreturn(i32*) nounwind willreturn" 6143bb2dbd6SSanjoy Das " " 6153bb2dbd6SSanjoy Das "declare void @unknown(i32*) " 6163bb2dbd6SSanjoy Das " " 6173bb2dbd6SSanjoy Das "define void @f(i32* %p) { " 6183bb2dbd6SSanjoy Das " call void @nounwind_readonly(i32* %p) " 6193bb2dbd6SSanjoy Das " call void @nounwind_argmemonly(i32* %p) " 6203bb2dbd6SSanjoy Das " call void @throws_but_readonly(i32* %p) " 6213bb2dbd6SSanjoy Das " call void @throws_but_argmemonly(i32* %p) " 6223bb2dbd6SSanjoy Das " call void @unknown(i32* %p) nounwind readonly " 6233bb2dbd6SSanjoy Das " call void @unknown(i32* %p) nounwind argmemonly " 6243bb2dbd6SSanjoy Das " call void @unknown(i32* %p) readonly " 6253bb2dbd6SSanjoy Das " call void @unknown(i32* %p) argmemonly " 6266ed459fdSJohannes Doerfert " call void @nounwind_willreturn(i32* %p)" 6273bb2dbd6SSanjoy Das " ret void " 6283bb2dbd6SSanjoy Das "} "; 6293bb2dbd6SSanjoy Das 6303bb2dbd6SSanjoy Das LLVMContext Context; 6313bb2dbd6SSanjoy Das SMDiagnostic Error; 6323bb2dbd6SSanjoy Das auto M = parseAssemblyString(Assembly, Error, Context); 6333bb2dbd6SSanjoy Das assert(M && "Bad assembly?"); 6343bb2dbd6SSanjoy Das 6353bb2dbd6SSanjoy Das auto *F = M->getFunction("f"); 6363bb2dbd6SSanjoy Das assert(F && "Bad assembly?"); 6373bb2dbd6SSanjoy Das 6383bb2dbd6SSanjoy Das auto &BB = F->getEntryBlock(); 6398580cd4eSCraig Topper bool ExpectedAnswers[] = { 6403bb2dbd6SSanjoy Das true, // call void @nounwind_readonly(i32* %p) 6413bb2dbd6SSanjoy Das true, // call void @nounwind_argmemonly(i32* %p) 6423bb2dbd6SSanjoy Das false, // call void @throws_but_readonly(i32* %p) 6433bb2dbd6SSanjoy Das false, // call void @throws_but_argmemonly(i32* %p) 6443bb2dbd6SSanjoy Das true, // call void @unknown(i32* %p) nounwind readonly 6453bb2dbd6SSanjoy Das true, // call void @unknown(i32* %p) nounwind argmemonly 6463bb2dbd6SSanjoy Das false, // call void @unknown(i32* %p) readonly 6473bb2dbd6SSanjoy Das false, // call void @unknown(i32* %p) argmemonly 6486ed459fdSJohannes Doerfert true, // call void @nounwind_willreturn(i32* %p) 6493bb2dbd6SSanjoy Das false, // ret void 6503bb2dbd6SSanjoy Das }; 6513bb2dbd6SSanjoy Das 6523bb2dbd6SSanjoy Das int Index = 0; 6533bb2dbd6SSanjoy Das for (auto &I : BB) { 6543bb2dbd6SSanjoy Das EXPECT_EQ(isGuaranteedToTransferExecutionToSuccessor(&I), 6553bb2dbd6SSanjoy Das ExpectedAnswers[Index]) 6563bb2dbd6SSanjoy Das << "Incorrect answer at instruction " << Index << " = " << I; 6573bb2dbd6SSanjoy Das Index++; 6583bb2dbd6SSanjoy Das } 6593bb2dbd6SSanjoy Das } 66039a684d1SSanjoy Das 661219e5367SNikita Popov TEST_F(ValueTrackingTest, ComputeNumSignBits_PR32045) { 662219e5367SNikita Popov parseAssembly( 663219e5367SNikita Popov "define i32 @test(i32 %a) {\n" 664219e5367SNikita Popov " %A = ashr i32 %a, -1\n" 665219e5367SNikita Popov " ret i32 %A\n" 666219e5367SNikita Popov "}\n"); 667219e5367SNikita Popov EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u); 66839a684d1SSanjoy Das } 66970dbd5fbSSimon Dardis 670a68096c7SSanjay Patel // No guarantees for canonical IR in this analysis, so this just bails out. 671219e5367SNikita Popov TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle) { 672219e5367SNikita Popov parseAssembly( 673219e5367SNikita Popov "define <2 x i32> @test() {\n" 674219e5367SNikita Popov " %A = shufflevector <2 x i32> undef, <2 x i32> undef, <2 x i32> <i32 0, i32 0>\n" 675219e5367SNikita Popov " ret <2 x i32> %A\n" 676219e5367SNikita Popov "}\n"); 677219e5367SNikita Popov EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u); 678a68096c7SSanjay Patel } 679a68096c7SSanjay Patel 68045a148deSSanjay Patel // No guarantees for canonical IR in this analysis, so a shuffle element that 68145a148deSSanjay Patel // references an undef value means this can't return any extra information. 682219e5367SNikita Popov TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle2) { 683219e5367SNikita Popov parseAssembly( 684219e5367SNikita Popov "define <2 x i32> @test(<2 x i1> %x) {\n" 685219e5367SNikita Popov " %sext = sext <2 x i1> %x to <2 x i32>\n" 686219e5367SNikita Popov " %A = shufflevector <2 x i32> %sext, <2 x i32> undef, <2 x i32> <i32 0, i32 2>\n" 687219e5367SNikita Popov " ret <2 x i32> %A\n" 688219e5367SNikita Popov "}\n"); 689219e5367SNikita Popov EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u); 690994543abSJuneyoung Lee } 691994543abSJuneyoung Lee 692e5f602d8SJuneyoung Lee TEST(ValueTracking, propagatesPoison) { 693e5f602d8SJuneyoung Lee std::string AsmHead = "declare i32 @g(i32)\n" 694e5f602d8SJuneyoung Lee "define void @f(i32 %x, i32 %y, float %fx, float %fy, " 695e5f602d8SJuneyoung Lee "i1 %cond, i8* %p) {\n"; 696e5f602d8SJuneyoung Lee std::string AsmTail = " ret void\n}"; 697e5f602d8SJuneyoung Lee // (propagates poison?, IR instruction) 698e5f602d8SJuneyoung Lee SmallVector<std::pair<bool, std::string>, 32> Data = { 699e5f602d8SJuneyoung Lee {true, "add i32 %x, %y"}, 700e5f602d8SJuneyoung Lee {true, "add nsw nuw i32 %x, %y"}, 701e5f602d8SJuneyoung Lee {true, "ashr i32 %x, %y"}, 702e5f602d8SJuneyoung Lee {true, "lshr exact i32 %x, 31"}, 703e5f602d8SJuneyoung Lee {true, "fcmp oeq float %fx, %fy"}, 704e5f602d8SJuneyoung Lee {true, "icmp eq i32 %x, %y"}, 705e5f602d8SJuneyoung Lee {true, "getelementptr i8, i8* %p, i32 %x"}, 706e5f602d8SJuneyoung Lee {true, "getelementptr inbounds i8, i8* %p, i32 %x"}, 707e5f602d8SJuneyoung Lee {true, "bitcast float %fx to i32"}, 708e5f602d8SJuneyoung Lee {false, "select i1 %cond, i32 %x, i32 %y"}, 709e5f602d8SJuneyoung Lee {false, "freeze i32 %x"}, 710e5f602d8SJuneyoung Lee {true, "udiv i32 %x, %y"}, 711e5f602d8SJuneyoung Lee {true, "urem i32 %x, %y"}, 712e5f602d8SJuneyoung Lee {true, "sdiv exact i32 %x, %y"}, 713e5f602d8SJuneyoung Lee {true, "srem i32 %x, %y"}, 714e5f602d8SJuneyoung Lee {false, "call i32 @g(i32 %x)"}}; 715e5f602d8SJuneyoung Lee 716e5f602d8SJuneyoung Lee std::string AssemblyStr = AsmHead; 717e5f602d8SJuneyoung Lee for (auto &Itm : Data) 718e5f602d8SJuneyoung Lee AssemblyStr += Itm.second + "\n"; 719e5f602d8SJuneyoung Lee AssemblyStr += AsmTail; 720e5f602d8SJuneyoung Lee 721e5f602d8SJuneyoung Lee LLVMContext Context; 722e5f602d8SJuneyoung Lee SMDiagnostic Error; 723e5f602d8SJuneyoung Lee auto M = parseAssemblyString(AssemblyStr, Error, Context); 724e5f602d8SJuneyoung Lee assert(M && "Bad assembly?"); 725e5f602d8SJuneyoung Lee 726e5f602d8SJuneyoung Lee auto *F = M->getFunction("f"); 727e5f602d8SJuneyoung Lee assert(F && "Bad assembly?"); 728e5f602d8SJuneyoung Lee 729e5f602d8SJuneyoung Lee auto &BB = F->getEntryBlock(); 730e5f602d8SJuneyoung Lee 731e5f602d8SJuneyoung Lee int Index = 0; 732e5f602d8SJuneyoung Lee for (auto &I : BB) { 733e5f602d8SJuneyoung Lee if (isa<ReturnInst>(&I)) 734e5f602d8SJuneyoung Lee break; 73525ce1e04SJuneyoung Lee EXPECT_EQ(propagatesPoison(cast<Operator>(&I)), Data[Index].first) 736e5f602d8SJuneyoung Lee << "Incorrect answer at instruction " << Index << " = " << I; 737e5f602d8SJuneyoung Lee Index++; 738e5f602d8SJuneyoung Lee } 739e5f602d8SJuneyoung Lee } 740e5f602d8SJuneyoung Lee 74125ce1e04SJuneyoung Lee TEST_F(ValueTrackingTest, programUndefinedIfPoison) { 74225ce1e04SJuneyoung Lee parseAssembly("declare i32 @any_num()" 74325ce1e04SJuneyoung Lee "define void @test(i32 %mask) {\n" 74425ce1e04SJuneyoung Lee " %A = call i32 @any_num()\n" 74525ce1e04SJuneyoung Lee " %B = or i32 %A, %mask\n" 74625ce1e04SJuneyoung Lee " udiv i32 1, %B" 74725ce1e04SJuneyoung Lee " ret void\n" 74825ce1e04SJuneyoung Lee "}\n"); 74925ce1e04SJuneyoung Lee // If %A was poison, udiv raises UB regardless of %mask's value 75025ce1e04SJuneyoung Lee EXPECT_EQ(programUndefinedIfPoison(A), true); 75125ce1e04SJuneyoung Lee } 75225ce1e04SJuneyoung Lee 75325ce1e04SJuneyoung Lee TEST_F(ValueTrackingTest, programUndefinedIfUndefOrPoison) { 75425ce1e04SJuneyoung Lee parseAssembly("declare i32 @any_num()" 75525ce1e04SJuneyoung Lee "define void @test(i32 %mask) {\n" 75625ce1e04SJuneyoung Lee " %A = call i32 @any_num()\n" 75725ce1e04SJuneyoung Lee " %B = or i32 %A, %mask\n" 75825ce1e04SJuneyoung Lee " udiv i32 1, %B" 75925ce1e04SJuneyoung Lee " ret void\n" 76025ce1e04SJuneyoung Lee "}\n"); 76125ce1e04SJuneyoung Lee // If %A was undef and %mask was 1, udiv does not raise UB 76225ce1e04SJuneyoung Lee EXPECT_EQ(programUndefinedIfUndefOrPoison(A), false); 76325ce1e04SJuneyoung Lee } 76425ce1e04SJuneyoung Lee 76525ce1e04SJuneyoung Lee TEST_F(ValueTrackingTest, isGuaranteedNotToBePoison_exploitBranchCond) { 76625ce1e04SJuneyoung Lee parseAssembly("declare i1 @any_bool()" 76725ce1e04SJuneyoung Lee "define void @test(i1 %y) {\n" 76825ce1e04SJuneyoung Lee " %A = call i1 @any_bool()\n" 76925ce1e04SJuneyoung Lee " %cond = and i1 %A, %y\n" 77025ce1e04SJuneyoung Lee " br i1 %cond, label %BB1, label %BB2\n" 77125ce1e04SJuneyoung Lee "BB1:\n" 77225ce1e04SJuneyoung Lee " ret void\n" 77325ce1e04SJuneyoung Lee "BB2:\n" 77425ce1e04SJuneyoung Lee " ret void\n" 77525ce1e04SJuneyoung Lee "}\n"); 77625ce1e04SJuneyoung Lee DominatorTree DT(*F); 77725ce1e04SJuneyoung Lee for (auto &BB : *F) { 77825ce1e04SJuneyoung Lee if (&BB == &F->getEntryBlock()) 77925ce1e04SJuneyoung Lee continue; 78025ce1e04SJuneyoung Lee 7819b3c2a72SJuneyoung Lee EXPECT_EQ(isGuaranteedNotToBePoison(A, nullptr, BB.getTerminator(), &DT), 7829b3c2a72SJuneyoung Lee true) 78325ce1e04SJuneyoung Lee << "isGuaranteedNotToBePoison does not hold at " << *BB.getTerminator(); 78425ce1e04SJuneyoung Lee } 78525ce1e04SJuneyoung Lee } 78625ce1e04SJuneyoung Lee 787ba8911d5SJuneyoung Lee TEST_F(ValueTrackingTest, isGuaranteedNotToBePoison_phi) { 788ba8911d5SJuneyoung Lee parseAssembly("declare i32 @any_i32(i32)" 789ba8911d5SJuneyoung Lee "define void @test() {\n" 790ba8911d5SJuneyoung Lee "ENTRY:\n" 791ba8911d5SJuneyoung Lee " br label %LOOP\n" 792ba8911d5SJuneyoung Lee "LOOP:\n" 793ba8911d5SJuneyoung Lee " %A = phi i32 [0, %ENTRY], [%A.next, %NEXT]\n" 794ba8911d5SJuneyoung Lee " %A.next = call i32 @any_i32(i32 %A)\n" 795ba8911d5SJuneyoung Lee " %cond = icmp eq i32 %A.next, 0\n" 796ba8911d5SJuneyoung Lee " br i1 %cond, label %NEXT, label %EXIT\n" 797ba8911d5SJuneyoung Lee "NEXT:\n" 798ba8911d5SJuneyoung Lee " br label %LOOP\n" 799ba8911d5SJuneyoung Lee "EXIT:\n" 800ba8911d5SJuneyoung Lee " ret void\n" 801ba8911d5SJuneyoung Lee "}\n"); 802ba8911d5SJuneyoung Lee DominatorTree DT(*F); 803ba8911d5SJuneyoung Lee for (auto &BB : *F) { 804ba8911d5SJuneyoung Lee if (BB.getName() == "LOOP") { 8059b3c2a72SJuneyoung Lee EXPECT_EQ(isGuaranteedNotToBePoison(A, nullptr, A, &DT), true) 806ba8911d5SJuneyoung Lee << "isGuaranteedNotToBePoison does not hold"; 807ba8911d5SJuneyoung Lee } 808ba8911d5SJuneyoung Lee } 809ba8911d5SJuneyoung Lee } 810ba8911d5SJuneyoung Lee 8111c452200SJuneyoung Lee TEST_F(ValueTrackingTest, isGuaranteedNotToBeUndefOrPoison) { 8121c452200SJuneyoung Lee parseAssembly("declare void @f(i32 noundef)" 8131c452200SJuneyoung Lee "define void @test(i32 %x) {\n" 8141c452200SJuneyoung Lee " %A = bitcast i32 %x to i32\n" 8151c452200SJuneyoung Lee " call void @f(i32 noundef %x)\n" 8161c452200SJuneyoung Lee " ret void\n" 8171c452200SJuneyoung Lee "}\n"); 8181c452200SJuneyoung Lee EXPECT_EQ(isGuaranteedNotToBeUndefOrPoison(A), true); 8191c452200SJuneyoung Lee } 8201c452200SJuneyoung Lee 8219b3c2a72SJuneyoung Lee TEST_F(ValueTrackingTest, isGuaranteedNotToBeUndefOrPoison_assume) { 8229b3c2a72SJuneyoung Lee parseAssembly("declare i1 @f_i1()\n" 8239b3c2a72SJuneyoung Lee "declare i32 @f_i32()\n" 8249b3c2a72SJuneyoung Lee "declare void @llvm.assume(i1)\n" 8259b3c2a72SJuneyoung Lee "define void @test() {\n" 8269b3c2a72SJuneyoung Lee " %A = call i32 @f_i32()\n" 8279b3c2a72SJuneyoung Lee " %cond = call i1 @f_i1()\n" 8289b3c2a72SJuneyoung Lee " %CxtI = add i32 0, 0\n" 8299b3c2a72SJuneyoung Lee " br i1 %cond, label %BB1, label %EXIT\n" 8309b3c2a72SJuneyoung Lee "BB1:\n" 8319b3c2a72SJuneyoung Lee " %CxtI2 = add i32 0, 0\n" 8329b3c2a72SJuneyoung Lee " %cond2 = call i1 @f_i1()\n" 8339b3c2a72SJuneyoung Lee " call void @llvm.assume(i1 true) [ \"noundef\"(i32 %A) ]\n" 8349b3c2a72SJuneyoung Lee " br i1 %cond2, label %BB2, label %EXIT\n" 8359b3c2a72SJuneyoung Lee "BB2:\n" 8369b3c2a72SJuneyoung Lee " %CxtI3 = add i32 0, 0\n" 8379b3c2a72SJuneyoung Lee " ret void\n" 8389b3c2a72SJuneyoung Lee "EXIT:\n" 8399b3c2a72SJuneyoung Lee " ret void\n" 8409b3c2a72SJuneyoung Lee "}"); 8419b3c2a72SJuneyoung Lee AssumptionCache AC(*F); 8429b3c2a72SJuneyoung Lee DominatorTree DT(*F); 8439b3c2a72SJuneyoung Lee EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(A, &AC, CxtI, &DT)); 8449b3c2a72SJuneyoung Lee EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(A, &AC, CxtI2, &DT)); 8459b3c2a72SJuneyoung Lee EXPECT_TRUE(isGuaranteedNotToBeUndefOrPoison(A, &AC, CxtI3, &DT)); 8469b3c2a72SJuneyoung Lee } 8479b3c2a72SJuneyoung Lee 8480a6aee51SJuneyoung Lee TEST(ValueTracking, canCreatePoisonOrUndef) { 849994543abSJuneyoung Lee std::string AsmHead = 850994543abSJuneyoung Lee "declare i32 @g(i32)\n" 851994543abSJuneyoung Lee "define void @f(i32 %x, i32 %y, float %fx, float %fy, i1 %cond, " 852994543abSJuneyoung Lee "<4 x i32> %vx, <4 x i32> %vx2, <vscale x 4 x i32> %svx, i8* %p) {\n"; 853994543abSJuneyoung Lee std::string AsmTail = " ret void\n}"; 8540a6aee51SJuneyoung Lee // (can create poison?, can create undef?, IR instruction) 855ef66e3d0SJuneyoung Lee SmallVector<std::pair<std::pair<bool, bool>, std::string>, 32> Data = { 856ef66e3d0SJuneyoung Lee {{false, false}, "add i32 %x, %y"}, 857ef66e3d0SJuneyoung Lee {{true, false}, "add nsw nuw i32 %x, %y"}, 858ef66e3d0SJuneyoung Lee {{true, false}, "shl i32 %x, %y"}, 859ef66e3d0SJuneyoung Lee {{true, false}, "shl <4 x i32> %vx, %vx2"}, 860ef66e3d0SJuneyoung Lee {{true, false}, "shl nsw i32 %x, %y"}, 861ef66e3d0SJuneyoung Lee {{true, false}, "shl nsw <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"}, 862ef66e3d0SJuneyoung Lee {{false, false}, "shl i32 %x, 31"}, 863ef66e3d0SJuneyoung Lee {{true, false}, "shl i32 %x, 32"}, 864ef66e3d0SJuneyoung Lee {{false, false}, "shl <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"}, 865ef66e3d0SJuneyoung Lee {{true, false}, "shl <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 32>"}, 866ef66e3d0SJuneyoung Lee {{true, false}, "ashr i32 %x, %y"}, 867ef66e3d0SJuneyoung Lee {{true, false}, "ashr exact i32 %x, %y"}, 868ef66e3d0SJuneyoung Lee {{false, false}, "ashr i32 %x, 31"}, 869ef66e3d0SJuneyoung Lee {{true, false}, "ashr exact i32 %x, 31"}, 870ef66e3d0SJuneyoung Lee {{false, false}, "ashr <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"}, 871ef66e3d0SJuneyoung Lee {{true, false}, "ashr <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 32>"}, 872ef66e3d0SJuneyoung Lee {{true, false}, "ashr exact <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"}, 873ef66e3d0SJuneyoung Lee {{true, false}, "lshr i32 %x, %y"}, 874ef66e3d0SJuneyoung Lee {{true, false}, "lshr exact i32 %x, 31"}, 875ef66e3d0SJuneyoung Lee {{false, false}, "udiv i32 %x, %y"}, 876ef66e3d0SJuneyoung Lee {{true, false}, "udiv exact i32 %x, %y"}, 877ef66e3d0SJuneyoung Lee {{false, false}, "getelementptr i8, i8* %p, i32 %x"}, 878ef66e3d0SJuneyoung Lee {{true, false}, "getelementptr inbounds i8, i8* %p, i32 %x"}, 879ef66e3d0SJuneyoung Lee {{true, false}, "fneg nnan float %fx"}, 880ef66e3d0SJuneyoung Lee {{false, false}, "fneg float %fx"}, 881ef66e3d0SJuneyoung Lee {{false, false}, "fadd float %fx, %fy"}, 882ef66e3d0SJuneyoung Lee {{true, false}, "fadd nnan float %fx, %fy"}, 883ef66e3d0SJuneyoung Lee {{false, false}, "urem i32 %x, %y"}, 884ef66e3d0SJuneyoung Lee {{true, false}, "fptoui float %fx to i32"}, 885ef66e3d0SJuneyoung Lee {{true, false}, "fptosi float %fx to i32"}, 886ef66e3d0SJuneyoung Lee {{false, false}, "bitcast float %fx to i32"}, 887ef66e3d0SJuneyoung Lee {{false, false}, "select i1 %cond, i32 %x, i32 %y"}, 888ef66e3d0SJuneyoung Lee {{true, false}, "select nnan i1 %cond, float %fx, float %fy"}, 889ef66e3d0SJuneyoung Lee {{true, false}, "extractelement <4 x i32> %vx, i32 %x"}, 890ef66e3d0SJuneyoung Lee {{false, false}, "extractelement <4 x i32> %vx, i32 3"}, 891ef66e3d0SJuneyoung Lee {{true, false}, "extractelement <vscale x 4 x i32> %svx, i32 4"}, 892ef66e3d0SJuneyoung Lee {{true, false}, "insertelement <4 x i32> %vx, i32 %x, i32 %y"}, 893ef66e3d0SJuneyoung Lee {{false, false}, "insertelement <4 x i32> %vx, i32 %x, i32 3"}, 894ef66e3d0SJuneyoung Lee {{true, false}, "insertelement <vscale x 4 x i32> %svx, i32 %x, i32 4"}, 895ef66e3d0SJuneyoung Lee {{false, false}, "freeze i32 %x"}, 896ef66e3d0SJuneyoung Lee {{false, false}, 8970a6aee51SJuneyoung Lee "shufflevector <4 x i32> %vx, <4 x i32> %vx2, " 8980a6aee51SJuneyoung Lee "<4 x i32> <i32 0, i32 1, i32 2, i32 3>"}, 899ef66e3d0SJuneyoung Lee {{false, true}, 9000a6aee51SJuneyoung Lee "shufflevector <4 x i32> %vx, <4 x i32> %vx2, " 9010a6aee51SJuneyoung Lee "<4 x i32> <i32 0, i32 1, i32 2, i32 undef>"}, 902ef66e3d0SJuneyoung Lee {{false, true}, 9030a6aee51SJuneyoung Lee "shufflevector <vscale x 4 x i32> %svx, " 9040a6aee51SJuneyoung Lee "<vscale x 4 x i32> %svx, <vscale x 4 x i32> undef"}, 905ef66e3d0SJuneyoung Lee {{true, false}, "call i32 @g(i32 %x)"}, 906ef66e3d0SJuneyoung Lee {{false, false}, "call noundef i32 @g(i32 %x)"}, 907ef66e3d0SJuneyoung Lee {{true, false}, "fcmp nnan oeq float %fx, %fy"}, 908ef66e3d0SJuneyoung Lee {{false, false}, "fcmp oeq float %fx, %fy"}}; 909994543abSJuneyoung Lee 910994543abSJuneyoung Lee std::string AssemblyStr = AsmHead; 911994543abSJuneyoung Lee for (auto &Itm : Data) 912ef66e3d0SJuneyoung Lee AssemblyStr += Itm.second + "\n"; 913994543abSJuneyoung Lee AssemblyStr += AsmTail; 914994543abSJuneyoung Lee 915994543abSJuneyoung Lee LLVMContext Context; 916994543abSJuneyoung Lee SMDiagnostic Error; 917994543abSJuneyoung Lee auto M = parseAssemblyString(AssemblyStr, Error, Context); 918994543abSJuneyoung Lee assert(M && "Bad assembly?"); 919994543abSJuneyoung Lee 920994543abSJuneyoung Lee auto *F = M->getFunction("f"); 921994543abSJuneyoung Lee assert(F && "Bad assembly?"); 922994543abSJuneyoung Lee 923994543abSJuneyoung Lee auto &BB = F->getEntryBlock(); 924994543abSJuneyoung Lee 925994543abSJuneyoung Lee int Index = 0; 926994543abSJuneyoung Lee for (auto &I : BB) { 927994543abSJuneyoung Lee if (isa<ReturnInst>(&I)) 928994543abSJuneyoung Lee break; 929ef66e3d0SJuneyoung Lee bool Poison = Data[Index].first.first; 930ef66e3d0SJuneyoung Lee bool Undef = Data[Index].first.second; 9310a6aee51SJuneyoung Lee EXPECT_EQ(canCreatePoison(cast<Operator>(&I)), Poison) 9320a6aee51SJuneyoung Lee << "Incorrect answer of canCreatePoison at instruction " << Index 9330a6aee51SJuneyoung Lee << " = " << I; 9340a6aee51SJuneyoung Lee EXPECT_EQ(canCreateUndefOrPoison(cast<Operator>(&I)), Undef || Poison) 9350a6aee51SJuneyoung Lee << "Incorrect answer of canCreateUndef at instruction " << Index 9360a6aee51SJuneyoung Lee << " = " << I; 937994543abSJuneyoung Lee Index++; 938994543abSJuneyoung Lee } 93945a148deSSanjay Patel } 94045a148deSSanjay Patel 941219e5367SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownBits) { 942219e5367SNikita Popov parseAssembly( 943219e5367SNikita Popov "define i32 @test(i32 %a, i32 %b) {\n" 944219e5367SNikita Popov " %ash = mul i32 %a, 8\n" 945219e5367SNikita Popov " %aad = add i32 %ash, 7\n" 946219e5367SNikita Popov " %aan = and i32 %aad, 4095\n" 947219e5367SNikita Popov " %bsh = shl i32 %b, 4\n" 948219e5367SNikita Popov " %bad = or i32 %bsh, 6\n" 949219e5367SNikita Popov " %ban = and i32 %bad, 4095\n" 950219e5367SNikita Popov " %A = mul i32 %aan, %ban\n" 951219e5367SNikita Popov " ret i32 %A\n" 952219e5367SNikita Popov "}\n"); 953219e5367SNikita Popov expectKnownBits(/*zero*/ 4278190085u, /*one*/ 10u); 95470dbd5fbSSimon Dardis } 95570dbd5fbSSimon Dardis 956219e5367SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownMulBits) { 957219e5367SNikita Popov parseAssembly( 958219e5367SNikita Popov "define i32 @test(i32 %a, i32 %b) {\n" 959219e5367SNikita Popov " %aa = shl i32 %a, 5\n" 960219e5367SNikita Popov " %bb = shl i32 %b, 5\n" 961219e5367SNikita Popov " %aaa = or i32 %aa, 24\n" 962219e5367SNikita Popov " %bbb = or i32 %bb, 28\n" 963219e5367SNikita Popov " %A = mul i32 %aaa, %bbb\n" 964219e5367SNikita Popov " ret i32 %A\n" 965219e5367SNikita Popov "}\n"); 966219e5367SNikita Popov expectKnownBits(/*zero*/ 95u, /*one*/ 32u); 96770dbd5fbSSimon Dardis } 968687b92cdSNikita Popov 9697cf0f856SNikita Popov TEST_F(ComputeKnownBitsTest, KnownNonZeroShift) { 9707cf0f856SNikita Popov // %q is known nonzero without known bits. 9717cf0f856SNikita Popov // Because %q is nonzero, %A[0] is known to be zero. 9727cf0f856SNikita Popov parseAssembly( 9737cf0f856SNikita Popov "define i8 @test(i8 %p, i8* %pq) {\n" 9747cf0f856SNikita Popov " %q = load i8, i8* %pq, !range !0\n" 9757cf0f856SNikita Popov " %A = shl i8 %p, %q\n" 9767cf0f856SNikita Popov " ret i8 %A\n" 9777cf0f856SNikita Popov "}\n" 9787cf0f856SNikita Popov "!0 = !{ i8 1, i8 5 }\n"); 9797cf0f856SNikita Popov expectKnownBits(/*zero*/ 1u, /*one*/ 0u); 9807cf0f856SNikita Popov } 9817cf0f856SNikita Popov 982687b92cdSNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownFshl) { 983687b92cdSNikita Popov // fshl(....1111....0000, 00..1111........, 6) 984687b92cdSNikita Popov // = 11....000000..11 985687b92cdSNikita Popov parseAssembly( 986687b92cdSNikita Popov "define i16 @test(i16 %a, i16 %b) {\n" 987687b92cdSNikita Popov " %aa = shl i16 %a, 4\n" 988687b92cdSNikita Popov " %bb = lshr i16 %b, 2\n" 989687b92cdSNikita Popov " %aaa = or i16 %aa, 3840\n" 990687b92cdSNikita Popov " %bbb = or i16 %bb, 3840\n" 991687b92cdSNikita Popov " %A = call i16 @llvm.fshl.i16(i16 %aaa, i16 %bbb, i16 6)\n" 992687b92cdSNikita Popov " ret i16 %A\n" 993687b92cdSNikita Popov "}\n" 994687b92cdSNikita Popov "declare i16 @llvm.fshl.i16(i16, i16, i16)\n"); 995687b92cdSNikita Popov expectKnownBits(/*zero*/ 1008u, /*one*/ 49155u); 996687b92cdSNikita Popov } 997687b92cdSNikita Popov 998687b92cdSNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownFshr) { 999687b92cdSNikita Popov // fshr(....1111....0000, 00..1111........, 26) 1000687b92cdSNikita Popov // = 11....000000..11 1001687b92cdSNikita Popov parseAssembly( 1002687b92cdSNikita Popov "define i16 @test(i16 %a, i16 %b) {\n" 1003687b92cdSNikita Popov " %aa = shl i16 %a, 4\n" 1004687b92cdSNikita Popov " %bb = lshr i16 %b, 2\n" 1005687b92cdSNikita Popov " %aaa = or i16 %aa, 3840\n" 1006687b92cdSNikita Popov " %bbb = or i16 %bb, 3840\n" 1007687b92cdSNikita Popov " %A = call i16 @llvm.fshr.i16(i16 %aaa, i16 %bbb, i16 26)\n" 1008687b92cdSNikita Popov " ret i16 %A\n" 1009687b92cdSNikita Popov "}\n" 1010687b92cdSNikita Popov "declare i16 @llvm.fshr.i16(i16, i16, i16)\n"); 1011687b92cdSNikita Popov expectKnownBits(/*zero*/ 1008u, /*one*/ 49155u); 1012687b92cdSNikita Popov } 1013687b92cdSNikita Popov 1014687b92cdSNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownFshlZero) { 1015687b92cdSNikita Popov // fshl(....1111....0000, 00..1111........, 0) 1016687b92cdSNikita Popov // = ....1111....0000 1017687b92cdSNikita Popov parseAssembly( 1018687b92cdSNikita Popov "define i16 @test(i16 %a, i16 %b) {\n" 1019687b92cdSNikita Popov " %aa = shl i16 %a, 4\n" 1020687b92cdSNikita Popov " %bb = lshr i16 %b, 2\n" 1021687b92cdSNikita Popov " %aaa = or i16 %aa, 3840\n" 1022687b92cdSNikita Popov " %bbb = or i16 %bb, 3840\n" 1023687b92cdSNikita Popov " %A = call i16 @llvm.fshl.i16(i16 %aaa, i16 %bbb, i16 0)\n" 1024687b92cdSNikita Popov " ret i16 %A\n" 1025687b92cdSNikita Popov "}\n" 1026687b92cdSNikita Popov "declare i16 @llvm.fshl.i16(i16, i16, i16)\n"); 1027687b92cdSNikita Popov expectKnownBits(/*zero*/ 15u, /*one*/ 3840u); 1028687b92cdSNikita Popov } 1029ed3ca927SNikita Popov 1030ed3ca927SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownUAddSatLeadingOnes) { 1031ed3ca927SNikita Popov // uadd.sat(1111...1, ........) 1032ed3ca927SNikita Popov // = 1111.... 1033ed3ca927SNikita Popov parseAssembly( 1034ed3ca927SNikita Popov "define i8 @test(i8 %a, i8 %b) {\n" 1035ed3ca927SNikita Popov " %aa = or i8 %a, 241\n" 1036ed3ca927SNikita Popov " %A = call i8 @llvm.uadd.sat.i8(i8 %aa, i8 %b)\n" 1037ed3ca927SNikita Popov " ret i8 %A\n" 1038ed3ca927SNikita Popov "}\n" 1039ed3ca927SNikita Popov "declare i8 @llvm.uadd.sat.i8(i8, i8)\n"); 1040ed3ca927SNikita Popov expectKnownBits(/*zero*/ 0u, /*one*/ 240u); 1041ed3ca927SNikita Popov } 1042ed3ca927SNikita Popov 1043ed3ca927SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownUAddSatOnesPreserved) { 1044ed3ca927SNikita Popov // uadd.sat(00...011, .1...110) 1045ed3ca927SNikita Popov // = .......1 1046ed3ca927SNikita Popov parseAssembly( 1047ed3ca927SNikita Popov "define i8 @test(i8 %a, i8 %b) {\n" 1048ed3ca927SNikita Popov " %aa = or i8 %a, 3\n" 1049ed3ca927SNikita Popov " %aaa = and i8 %aa, 59\n" 1050ed3ca927SNikita Popov " %bb = or i8 %b, 70\n" 1051ed3ca927SNikita Popov " %bbb = and i8 %bb, 254\n" 1052ed3ca927SNikita Popov " %A = call i8 @llvm.uadd.sat.i8(i8 %aaa, i8 %bbb)\n" 1053ed3ca927SNikita Popov " ret i8 %A\n" 1054ed3ca927SNikita Popov "}\n" 1055ed3ca927SNikita Popov "declare i8 @llvm.uadd.sat.i8(i8, i8)\n"); 1056ed3ca927SNikita Popov expectKnownBits(/*zero*/ 0u, /*one*/ 1u); 1057ed3ca927SNikita Popov } 1058ed3ca927SNikita Popov 1059ed3ca927SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatLHSLeadingZeros) { 1060ed3ca927SNikita Popov // usub.sat(0000...0, ........) 1061ed3ca927SNikita Popov // = 0000.... 1062ed3ca927SNikita Popov parseAssembly( 1063ed3ca927SNikita Popov "define i8 @test(i8 %a, i8 %b) {\n" 1064ed3ca927SNikita Popov " %aa = and i8 %a, 14\n" 1065ed3ca927SNikita Popov " %A = call i8 @llvm.usub.sat.i8(i8 %aa, i8 %b)\n" 1066ed3ca927SNikita Popov " ret i8 %A\n" 1067ed3ca927SNikita Popov "}\n" 1068ed3ca927SNikita Popov "declare i8 @llvm.usub.sat.i8(i8, i8)\n"); 1069ed3ca927SNikita Popov expectKnownBits(/*zero*/ 240u, /*one*/ 0u); 1070ed3ca927SNikita Popov } 1071ed3ca927SNikita Popov 1072ed3ca927SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatRHSLeadingOnes) { 1073ed3ca927SNikita Popov // usub.sat(........, 1111...1) 1074ed3ca927SNikita Popov // = 0000.... 1075ed3ca927SNikita Popov parseAssembly( 1076ed3ca927SNikita Popov "define i8 @test(i8 %a, i8 %b) {\n" 1077ed3ca927SNikita Popov " %bb = or i8 %a, 241\n" 1078ed3ca927SNikita Popov " %A = call i8 @llvm.usub.sat.i8(i8 %a, i8 %bb)\n" 1079ed3ca927SNikita Popov " ret i8 %A\n" 1080ed3ca927SNikita Popov "}\n" 1081ed3ca927SNikita Popov "declare i8 @llvm.usub.sat.i8(i8, i8)\n"); 1082ed3ca927SNikita Popov expectKnownBits(/*zero*/ 240u, /*one*/ 0u); 1083ed3ca927SNikita Popov } 1084ed3ca927SNikita Popov 1085ed3ca927SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatZerosPreserved) { 1086ed3ca927SNikita Popov // usub.sat(11...011, .1...110) 1087ed3ca927SNikita Popov // = ......0. 1088ed3ca927SNikita Popov parseAssembly( 1089ed3ca927SNikita Popov "define i8 @test(i8 %a, i8 %b) {\n" 1090ed3ca927SNikita Popov " %aa = or i8 %a, 195\n" 1091ed3ca927SNikita Popov " %aaa = and i8 %aa, 251\n" 1092ed3ca927SNikita Popov " %bb = or i8 %b, 70\n" 1093ed3ca927SNikita Popov " %bbb = and i8 %bb, 254\n" 1094ed3ca927SNikita Popov " %A = call i8 @llvm.usub.sat.i8(i8 %aaa, i8 %bbb)\n" 1095ed3ca927SNikita Popov " ret i8 %A\n" 1096ed3ca927SNikita Popov "}\n" 1097ed3ca927SNikita Popov "declare i8 @llvm.usub.sat.i8(i8, i8)\n"); 1098ed3ca927SNikita Popov expectKnownBits(/*zero*/ 2u, /*one*/ 0u); 1099ed3ca927SNikita Popov } 1100329d15b5SVitaly Buka 1101d86fff6aSNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownBitsPtrToIntTrunc) { 1102d86fff6aSNikita Popov // ptrtoint truncates the pointer type. 1103d86fff6aSNikita Popov parseAssembly( 1104d86fff6aSNikita Popov "define void @test(i8** %p) {\n" 1105d86fff6aSNikita Popov " %A = load i8*, i8** %p\n" 1106d86fff6aSNikita Popov " %i = ptrtoint i8* %A to i32\n" 1107d86fff6aSNikita Popov " %m = and i32 %i, 31\n" 1108d86fff6aSNikita Popov " %c = icmp eq i32 %m, 0\n" 1109d86fff6aSNikita Popov " call void @llvm.assume(i1 %c)\n" 1110d86fff6aSNikita Popov " ret void\n" 1111d86fff6aSNikita Popov "}\n" 1112d86fff6aSNikita Popov "declare void @llvm.assume(i1)\n"); 1113d86fff6aSNikita Popov AssumptionCache AC(*F); 1114d86fff6aSNikita Popov KnownBits Known = computeKnownBits( 1115d86fff6aSNikita Popov A, M->getDataLayout(), /* Depth */ 0, &AC, F->front().getTerminator()); 1116d86fff6aSNikita Popov EXPECT_EQ(Known.Zero.getZExtValue(), 31u); 1117d86fff6aSNikita Popov EXPECT_EQ(Known.One.getZExtValue(), 0u); 1118d86fff6aSNikita Popov } 1119d86fff6aSNikita Popov 1120d86fff6aSNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownBitsPtrToIntZext) { 1121d86fff6aSNikita Popov // ptrtoint zero extends the pointer type. 1122d86fff6aSNikita Popov parseAssembly( 1123d86fff6aSNikita Popov "define void @test(i8** %p) {\n" 1124d86fff6aSNikita Popov " %A = load i8*, i8** %p\n" 1125d86fff6aSNikita Popov " %i = ptrtoint i8* %A to i128\n" 1126d86fff6aSNikita Popov " %m = and i128 %i, 31\n" 1127d86fff6aSNikita Popov " %c = icmp eq i128 %m, 0\n" 1128d86fff6aSNikita Popov " call void @llvm.assume(i1 %c)\n" 1129d86fff6aSNikita Popov " ret void\n" 1130d86fff6aSNikita Popov "}\n" 1131d86fff6aSNikita Popov "declare void @llvm.assume(i1)\n"); 1132d86fff6aSNikita Popov AssumptionCache AC(*F); 1133d86fff6aSNikita Popov KnownBits Known = computeKnownBits( 1134d86fff6aSNikita Popov A, M->getDataLayout(), /* Depth */ 0, &AC, F->front().getTerminator()); 1135d86fff6aSNikita Popov EXPECT_EQ(Known.Zero.getZExtValue(), 31u); 1136d86fff6aSNikita Popov EXPECT_EQ(Known.One.getZExtValue(), 0u); 1137d86fff6aSNikita Popov } 1138d86fff6aSNikita Popov 1139a6183d0fSJuneyoung Lee TEST_F(ComputeKnownBitsTest, ComputeKnownBitsFreeze) { 1140a6183d0fSJuneyoung Lee parseAssembly("define void @test() {\n" 1141a6183d0fSJuneyoung Lee " %m = call i32 @any_num()\n" 1142a6183d0fSJuneyoung Lee " %A = freeze i32 %m\n" 1143a6183d0fSJuneyoung Lee " %n = and i32 %m, 31\n" 1144a6183d0fSJuneyoung Lee " %c = icmp eq i32 %n, 0\n" 1145a6183d0fSJuneyoung Lee " call void @llvm.assume(i1 %c)\n" 1146a6183d0fSJuneyoung Lee " ret void\n" 1147a6183d0fSJuneyoung Lee "}\n" 1148a6183d0fSJuneyoung Lee "declare void @llvm.assume(i1)\n" 1149a6183d0fSJuneyoung Lee "declare i32 @any_num()\n"); 1150a6183d0fSJuneyoung Lee AssumptionCache AC(*F); 1151a6183d0fSJuneyoung Lee KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1152a6183d0fSJuneyoung Lee F->front().getTerminator()); 1153a6183d0fSJuneyoung Lee EXPECT_EQ(Known.Zero.getZExtValue(), 31u); 1154a6183d0fSJuneyoung Lee EXPECT_EQ(Known.One.getZExtValue(), 0u); 1155a6183d0fSJuneyoung Lee } 1156a6183d0fSJuneyoung Lee 1157f1f31eb2SQuentin Colombet TEST_F(ComputeKnownBitsTest, ComputeKnownBitsAddWithRange) { 1158f1f31eb2SQuentin Colombet parseAssembly("define void @test(i64* %p) {\n" 1159f1f31eb2SQuentin Colombet " %A = load i64, i64* %p, !range !{i64 64, i64 65536}\n" 1160f1f31eb2SQuentin Colombet " %APlus512 = add i64 %A, 512\n" 1161f1f31eb2SQuentin Colombet " %c = icmp ugt i64 %APlus512, 523\n" 1162f1f31eb2SQuentin Colombet " call void @llvm.assume(i1 %c)\n" 1163f1f31eb2SQuentin Colombet " ret void\n" 1164f1f31eb2SQuentin Colombet "}\n" 1165f1f31eb2SQuentin Colombet "declare void @llvm.assume(i1)\n"); 1166f1f31eb2SQuentin Colombet AssumptionCache AC(*F); 1167f1f31eb2SQuentin Colombet KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1168f1f31eb2SQuentin Colombet F->front().getTerminator()); 1169f1f31eb2SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), ~(65536llu - 1)); 1170f1f31eb2SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 0u); 1171f1f31eb2SQuentin Colombet Instruction &APlus512 = findInstructionByName(F, "APlus512"); 1172f1f31eb2SQuentin Colombet Known = computeKnownBits(&APlus512, M->getDataLayout(), /* Depth */ 0, &AC, 1173f1f31eb2SQuentin Colombet F->front().getTerminator()); 1174f1f31eb2SQuentin Colombet // We know of one less zero because 512 may have produced a 1 that 1175f1f31eb2SQuentin Colombet // got carried all the way to the first trailing zero. 1176f1f31eb2SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), (~(65536llu - 1)) << 1); 1177f1f31eb2SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 0u); 1178f1f31eb2SQuentin Colombet // The known range is not precise given computeKnownBits works 1179f1f31eb2SQuentin Colombet // with the masks of zeros and ones, not the ranges. 1180f1f31eb2SQuentin Colombet EXPECT_EQ(Known.getMinValue(), 0u); 1181f1f31eb2SQuentin Colombet EXPECT_EQ(Known.getMaxValue(), 131071); 1182f1f31eb2SQuentin Colombet } 1183f1f31eb2SQuentin Colombet 1184f1f31eb2SQuentin Colombet // 512 + [32, 64) doesn't produce overlapping bits. 1185f1f31eb2SQuentin Colombet // Make sure we get all the individual bits properly. 1186f1f31eb2SQuentin Colombet TEST_F(ComputeKnownBitsTest, ComputeKnownBitsAddWithRangeNoOverlap) { 1187f1f31eb2SQuentin Colombet parseAssembly("define void @test(i64* %p) {\n" 1188f1f31eb2SQuentin Colombet " %A = load i64, i64* %p, !range !{i64 32, i64 64}\n" 1189f1f31eb2SQuentin Colombet " %APlus512 = add i64 %A, 512\n" 1190f1f31eb2SQuentin Colombet " %c = icmp ugt i64 %APlus512, 523\n" 1191f1f31eb2SQuentin Colombet " call void @llvm.assume(i1 %c)\n" 1192f1f31eb2SQuentin Colombet " ret void\n" 1193f1f31eb2SQuentin Colombet "}\n" 1194f1f31eb2SQuentin Colombet "declare void @llvm.assume(i1)\n"); 1195f1f31eb2SQuentin Colombet AssumptionCache AC(*F); 1196f1f31eb2SQuentin Colombet KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1197f1f31eb2SQuentin Colombet F->front().getTerminator()); 1198f1f31eb2SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), ~(64llu - 1)); 1199f1f31eb2SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 32u); 1200f1f31eb2SQuentin Colombet Instruction &APlus512 = findInstructionByName(F, "APlus512"); 1201f1f31eb2SQuentin Colombet Known = computeKnownBits(&APlus512, M->getDataLayout(), /* Depth */ 0, &AC, 1202f1f31eb2SQuentin Colombet F->front().getTerminator()); 1203f1f31eb2SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), ~512llu & ~(64llu - 1)); 1204f1f31eb2SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 512u | 32u); 1205f1f31eb2SQuentin Colombet // The known range is not precise given computeKnownBits works 1206f1f31eb2SQuentin Colombet // with the masks of zeros and ones, not the ranges. 1207f1f31eb2SQuentin Colombet EXPECT_EQ(Known.getMinValue(), 544); 1208f1f31eb2SQuentin Colombet EXPECT_EQ(Known.getMaxValue(), 575); 1209f1f31eb2SQuentin Colombet } 1210f1f31eb2SQuentin Colombet 1211*ee6abef5SQuentin Colombet TEST_F(ComputeKnownBitsTest, ComputeKnownBitsGEPWithRange) { 1212*ee6abef5SQuentin Colombet parseAssembly( 1213*ee6abef5SQuentin Colombet "define void @test(i64* %p) {\n" 1214*ee6abef5SQuentin Colombet " %A = load i64, i64* %p, !range !{i64 64, i64 65536}\n" 1215*ee6abef5SQuentin Colombet " %APtr = inttoptr i64 %A to float*" 1216*ee6abef5SQuentin Colombet " %APtrPlus512 = getelementptr float, float* %APtr, i32 128\n" 1217*ee6abef5SQuentin Colombet " %c = icmp ugt float* %APtrPlus512, inttoptr (i32 523 to float*)\n" 1218*ee6abef5SQuentin Colombet " call void @llvm.assume(i1 %c)\n" 1219*ee6abef5SQuentin Colombet " ret void\n" 1220*ee6abef5SQuentin Colombet "}\n" 1221*ee6abef5SQuentin Colombet "declare void @llvm.assume(i1)\n"); 1222*ee6abef5SQuentin Colombet AssumptionCache AC(*F); 1223*ee6abef5SQuentin Colombet KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1224*ee6abef5SQuentin Colombet F->front().getTerminator()); 1225*ee6abef5SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), ~(65536llu - 1)); 1226*ee6abef5SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 0u); 1227*ee6abef5SQuentin Colombet Instruction &APtrPlus512 = findInstructionByName(F, "APtrPlus512"); 1228*ee6abef5SQuentin Colombet Known = computeKnownBits(&APtrPlus512, M->getDataLayout(), /* Depth */ 0, &AC, 1229*ee6abef5SQuentin Colombet F->front().getTerminator()); 1230*ee6abef5SQuentin Colombet // We know of one less zero because 512 may have produced a 1 that 1231*ee6abef5SQuentin Colombet // got carried all the way to the first trailing zero. 1232*ee6abef5SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), ~(65536llu - 1) << 1); 1233*ee6abef5SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 0u); 1234*ee6abef5SQuentin Colombet // The known range is not precise given computeKnownBits works 1235*ee6abef5SQuentin Colombet // with the masks of zeros and ones, not the ranges. 1236*ee6abef5SQuentin Colombet EXPECT_EQ(Known.getMinValue(), 0u); 1237*ee6abef5SQuentin Colombet EXPECT_EQ(Known.getMaxValue(), 131071); 1238*ee6abef5SQuentin Colombet } 1239*ee6abef5SQuentin Colombet 1240*ee6abef5SQuentin Colombet // 4*128 + [32, 64) doesn't produce overlapping bits. 1241*ee6abef5SQuentin Colombet // Make sure we get all the individual bits properly. 1242*ee6abef5SQuentin Colombet // This test is useful to check that we account for the scaling factor 1243*ee6abef5SQuentin Colombet // in the gep. Indeed, gep float, [32,64), 128 is not 128 + [32,64). 1244*ee6abef5SQuentin Colombet TEST_F(ComputeKnownBitsTest, ComputeKnownBitsGEPWithRangeNoOverlap) { 1245*ee6abef5SQuentin Colombet parseAssembly( 1246*ee6abef5SQuentin Colombet "define void @test(i64* %p) {\n" 1247*ee6abef5SQuentin Colombet " %A = load i64, i64* %p, !range !{i64 32, i64 64}\n" 1248*ee6abef5SQuentin Colombet " %APtr = inttoptr i64 %A to float*" 1249*ee6abef5SQuentin Colombet " %APtrPlus512 = getelementptr float, float* %APtr, i32 128\n" 1250*ee6abef5SQuentin Colombet " %c = icmp ugt float* %APtrPlus512, inttoptr (i32 523 to float*)\n" 1251*ee6abef5SQuentin Colombet " call void @llvm.assume(i1 %c)\n" 1252*ee6abef5SQuentin Colombet " ret void\n" 1253*ee6abef5SQuentin Colombet "}\n" 1254*ee6abef5SQuentin Colombet "declare void @llvm.assume(i1)\n"); 1255*ee6abef5SQuentin Colombet AssumptionCache AC(*F); 1256*ee6abef5SQuentin Colombet KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC, 1257*ee6abef5SQuentin Colombet F->front().getTerminator()); 1258*ee6abef5SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), ~(64llu - 1)); 1259*ee6abef5SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 32u); 1260*ee6abef5SQuentin Colombet Instruction &APtrPlus512 = findInstructionByName(F, "APtrPlus512"); 1261*ee6abef5SQuentin Colombet Known = computeKnownBits(&APtrPlus512, M->getDataLayout(), /* Depth */ 0, &AC, 1262*ee6abef5SQuentin Colombet F->front().getTerminator()); 1263*ee6abef5SQuentin Colombet EXPECT_EQ(Known.Zero.getZExtValue(), ~512llu & ~(64llu - 1)); 1264*ee6abef5SQuentin Colombet EXPECT_EQ(Known.One.getZExtValue(), 512u | 32u); 1265*ee6abef5SQuentin Colombet // The known range is not precise given computeKnownBits works 1266*ee6abef5SQuentin Colombet // with the masks of zeros and ones, not the ranges. 1267*ee6abef5SQuentin Colombet EXPECT_EQ(Known.getMinValue(), 544); 1268*ee6abef5SQuentin Colombet EXPECT_EQ(Known.getMaxValue(), 575); 1269*ee6abef5SQuentin Colombet } 1270*ee6abef5SQuentin Colombet 1271329d15b5SVitaly Buka class IsBytewiseValueTest : public ValueTrackingTest, 1272329d15b5SVitaly Buka public ::testing::WithParamInterface< 1273329d15b5SVitaly Buka std::pair<const char *, const char *>> { 1274329d15b5SVitaly Buka protected: 1275329d15b5SVitaly Buka }; 1276329d15b5SVitaly Buka 1277329d15b5SVitaly Buka const std::pair<const char *, const char *> IsBytewiseValueTests[] = { 1278329d15b5SVitaly Buka { 1279329d15b5SVitaly Buka "i8 0", 1280329d15b5SVitaly Buka "i48* null", 1281329d15b5SVitaly Buka }, 1282329d15b5SVitaly Buka { 1283329d15b5SVitaly Buka "i8 undef", 1284329d15b5SVitaly Buka "i48* undef", 1285329d15b5SVitaly Buka }, 1286329d15b5SVitaly Buka { 1287329d15b5SVitaly Buka "i8 0", 1288329d15b5SVitaly Buka "i8 zeroinitializer", 1289329d15b5SVitaly Buka }, 1290329d15b5SVitaly Buka { 1291329d15b5SVitaly Buka "i8 0", 1292329d15b5SVitaly Buka "i8 0", 1293329d15b5SVitaly Buka }, 1294329d15b5SVitaly Buka { 1295329d15b5SVitaly Buka "i8 -86", 1296329d15b5SVitaly Buka "i8 -86", 1297329d15b5SVitaly Buka }, 1298329d15b5SVitaly Buka { 1299329d15b5SVitaly Buka "i8 -1", 1300329d15b5SVitaly Buka "i8 -1", 1301329d15b5SVitaly Buka }, 1302329d15b5SVitaly Buka { 1303329d15b5SVitaly Buka "i8 undef", 1304329d15b5SVitaly Buka "i16 undef", 1305329d15b5SVitaly Buka }, 1306329d15b5SVitaly Buka { 1307329d15b5SVitaly Buka "i8 0", 1308329d15b5SVitaly Buka "i16 0", 1309329d15b5SVitaly Buka }, 1310329d15b5SVitaly Buka { 1311329d15b5SVitaly Buka "", 1312329d15b5SVitaly Buka "i16 7", 1313329d15b5SVitaly Buka }, 1314329d15b5SVitaly Buka { 1315329d15b5SVitaly Buka "i8 -86", 1316329d15b5SVitaly Buka "i16 -21846", 1317329d15b5SVitaly Buka }, 1318329d15b5SVitaly Buka { 1319329d15b5SVitaly Buka "i8 -1", 1320329d15b5SVitaly Buka "i16 -1", 1321329d15b5SVitaly Buka }, 1322329d15b5SVitaly Buka { 1323329d15b5SVitaly Buka "i8 0", 1324329d15b5SVitaly Buka "i48 0", 1325329d15b5SVitaly Buka }, 1326329d15b5SVitaly Buka { 1327329d15b5SVitaly Buka "i8 -1", 1328329d15b5SVitaly Buka "i48 -1", 1329329d15b5SVitaly Buka }, 1330329d15b5SVitaly Buka { 1331329d15b5SVitaly Buka "i8 0", 1332329d15b5SVitaly Buka "i49 0", 1333329d15b5SVitaly Buka }, 1334329d15b5SVitaly Buka { 1335329d15b5SVitaly Buka "", 1336329d15b5SVitaly Buka "i49 -1", 1337329d15b5SVitaly Buka }, 1338329d15b5SVitaly Buka { 1339329d15b5SVitaly Buka "i8 0", 1340329d15b5SVitaly Buka "half 0xH0000", 1341329d15b5SVitaly Buka }, 1342329d15b5SVitaly Buka { 1343329d15b5SVitaly Buka "i8 -85", 1344329d15b5SVitaly Buka "half 0xHABAB", 1345329d15b5SVitaly Buka }, 1346329d15b5SVitaly Buka { 1347329d15b5SVitaly Buka "i8 0", 1348329d15b5SVitaly Buka "float 0.0", 1349329d15b5SVitaly Buka }, 1350329d15b5SVitaly Buka { 1351329d15b5SVitaly Buka "i8 -1", 1352329d15b5SVitaly Buka "float 0xFFFFFFFFE0000000", 1353329d15b5SVitaly Buka }, 1354329d15b5SVitaly Buka { 1355329d15b5SVitaly Buka "i8 0", 1356329d15b5SVitaly Buka "double 0.0", 1357329d15b5SVitaly Buka }, 1358329d15b5SVitaly Buka { 1359329d15b5SVitaly Buka "i8 -15", 1360329d15b5SVitaly Buka "double 0xF1F1F1F1F1F1F1F1", 1361329d15b5SVitaly Buka }, 1362329d15b5SVitaly Buka { 1363329d15b5SVitaly Buka "i8 undef", 1364329d15b5SVitaly Buka "i16* undef", 1365329d15b5SVitaly Buka }, 1366329d15b5SVitaly Buka { 1367329d15b5SVitaly Buka "i8 0", 1368329d15b5SVitaly Buka "i16* inttoptr (i64 0 to i16*)", 1369329d15b5SVitaly Buka }, 1370329d15b5SVitaly Buka { 1371c559e637SVitaly Buka "i8 -1", 1372329d15b5SVitaly Buka "i16* inttoptr (i64 -1 to i16*)", 1373329d15b5SVitaly Buka }, 1374329d15b5SVitaly Buka { 1375c559e637SVitaly Buka "i8 -86", 1376329d15b5SVitaly Buka "i16* inttoptr (i64 -6148914691236517206 to i16*)", 1377329d15b5SVitaly Buka }, 1378329d15b5SVitaly Buka { 1379329d15b5SVitaly Buka "", 1380329d15b5SVitaly Buka "i16* inttoptr (i48 -1 to i16*)", 1381329d15b5SVitaly Buka }, 1382329d15b5SVitaly Buka { 1383c559e637SVitaly Buka "i8 -1", 1384329d15b5SVitaly Buka "i16* inttoptr (i96 -1 to i16*)", 1385329d15b5SVitaly Buka }, 1386329d15b5SVitaly Buka { 138752096ee9SVitaly Buka "i8 undef", 1388329d15b5SVitaly Buka "[0 x i8] zeroinitializer", 1389329d15b5SVitaly Buka }, 1390329d15b5SVitaly Buka { 1391329d15b5SVitaly Buka "i8 undef", 1392329d15b5SVitaly Buka "[0 x i8] undef", 1393329d15b5SVitaly Buka }, 1394329d15b5SVitaly Buka { 139552096ee9SVitaly Buka "i8 undef", 1396329d15b5SVitaly Buka "[5 x [0 x i8]] zeroinitializer", 1397329d15b5SVitaly Buka }, 1398329d15b5SVitaly Buka { 1399329d15b5SVitaly Buka "i8 undef", 1400329d15b5SVitaly Buka "[5 x [0 x i8]] undef", 1401329d15b5SVitaly Buka }, 1402329d15b5SVitaly Buka { 1403329d15b5SVitaly Buka "i8 0", 1404329d15b5SVitaly Buka "[6 x i8] zeroinitializer", 1405329d15b5SVitaly Buka }, 1406329d15b5SVitaly Buka { 1407329d15b5SVitaly Buka "i8 undef", 1408329d15b5SVitaly Buka "[6 x i8] undef", 1409329d15b5SVitaly Buka }, 1410329d15b5SVitaly Buka { 1411329d15b5SVitaly Buka "i8 1", 1412329d15b5SVitaly Buka "[5 x i8] [i8 1, i8 1, i8 1, i8 1, i8 1]", 1413329d15b5SVitaly Buka }, 1414329d15b5SVitaly Buka { 1415329d15b5SVitaly Buka "", 1416329d15b5SVitaly Buka "[5 x i64] [i64 1, i64 1, i64 1, i64 1, i64 1]", 1417329d15b5SVitaly Buka }, 1418329d15b5SVitaly Buka { 1419329d15b5SVitaly Buka "i8 -1", 1420329d15b5SVitaly Buka "[5 x i64] [i64 -1, i64 -1, i64 -1, i64 -1, i64 -1]", 1421329d15b5SVitaly Buka }, 1422329d15b5SVitaly Buka { 1423329d15b5SVitaly Buka "", 1424329d15b5SVitaly Buka "[4 x i8] [i8 1, i8 2, i8 1, i8 1]", 1425329d15b5SVitaly Buka }, 1426329d15b5SVitaly Buka { 1427329d15b5SVitaly Buka "i8 1", 1428329d15b5SVitaly Buka "[4 x i8] [i8 1, i8 undef, i8 1, i8 1]", 1429329d15b5SVitaly Buka }, 1430329d15b5SVitaly Buka { 1431329d15b5SVitaly Buka "i8 0", 1432329d15b5SVitaly Buka "<6 x i8> zeroinitializer", 1433329d15b5SVitaly Buka }, 1434329d15b5SVitaly Buka { 1435329d15b5SVitaly Buka "i8 undef", 1436329d15b5SVitaly Buka "<6 x i8> undef", 1437329d15b5SVitaly Buka }, 1438329d15b5SVitaly Buka { 1439329d15b5SVitaly Buka "i8 1", 1440329d15b5SVitaly Buka "<5 x i8> <i8 1, i8 1, i8 1, i8 1, i8 1>", 1441329d15b5SVitaly Buka }, 1442329d15b5SVitaly Buka { 1443329d15b5SVitaly Buka "", 1444329d15b5SVitaly Buka "<5 x i64> <i64 1, i64 1, i64 1, i64 1, i64 1>", 1445329d15b5SVitaly Buka }, 1446329d15b5SVitaly Buka { 1447329d15b5SVitaly Buka "i8 -1", 1448329d15b5SVitaly Buka "<5 x i64> <i64 -1, i64 -1, i64 -1, i64 -1, i64 -1>", 1449329d15b5SVitaly Buka }, 1450329d15b5SVitaly Buka { 1451329d15b5SVitaly Buka "", 1452329d15b5SVitaly Buka "<4 x i8> <i8 1, i8 1, i8 2, i8 1>", 1453329d15b5SVitaly Buka }, 1454329d15b5SVitaly Buka { 1455b1bff76eSVitaly Buka "i8 5", 1456329d15b5SVitaly Buka "<2 x i8> < i8 5, i8 undef >", 1457329d15b5SVitaly Buka }, 1458329d15b5SVitaly Buka { 1459329d15b5SVitaly Buka "i8 0", 1460329d15b5SVitaly Buka "[2 x [2 x i16]] zeroinitializer", 1461329d15b5SVitaly Buka }, 1462329d15b5SVitaly Buka { 1463329d15b5SVitaly Buka "i8 undef", 1464329d15b5SVitaly Buka "[2 x [2 x i16]] undef", 1465329d15b5SVitaly Buka }, 1466329d15b5SVitaly Buka { 1467329d15b5SVitaly Buka "i8 -86", 1468329d15b5SVitaly Buka "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], " 1469329d15b5SVitaly Buka "[2 x i16] [i16 -21846, i16 -21846]]", 1470329d15b5SVitaly Buka }, 1471329d15b5SVitaly Buka { 1472329d15b5SVitaly Buka "", 1473329d15b5SVitaly Buka "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], " 1474329d15b5SVitaly Buka "[2 x i16] [i16 -21836, i16 -21846]]", 1475329d15b5SVitaly Buka }, 1476329d15b5SVitaly Buka { 147752096ee9SVitaly Buka "i8 undef", 1478329d15b5SVitaly Buka "{ } zeroinitializer", 1479329d15b5SVitaly Buka }, 1480329d15b5SVitaly Buka { 1481329d15b5SVitaly Buka "i8 undef", 1482329d15b5SVitaly Buka "{ } undef", 1483329d15b5SVitaly Buka }, 1484329d15b5SVitaly Buka { 148552096ee9SVitaly Buka "i8 undef", 1486329d15b5SVitaly Buka "{ {}, {} } zeroinitializer", 1487329d15b5SVitaly Buka }, 1488329d15b5SVitaly Buka { 1489329d15b5SVitaly Buka "i8 undef", 1490329d15b5SVitaly Buka "{ {}, {} } undef", 1491329d15b5SVitaly Buka }, 1492329d15b5SVitaly Buka { 1493329d15b5SVitaly Buka "i8 0", 1494329d15b5SVitaly Buka "{i8, i64, i16*} zeroinitializer", 1495329d15b5SVitaly Buka }, 1496329d15b5SVitaly Buka { 1497329d15b5SVitaly Buka "i8 undef", 1498329d15b5SVitaly Buka "{i8, i64, i16*} undef", 1499329d15b5SVitaly Buka }, 1500329d15b5SVitaly Buka { 1501329d15b5SVitaly Buka "i8 -86", 1502329d15b5SVitaly Buka "{i8, i64, i16*} {i8 -86, i64 -6148914691236517206, i16* undef}", 1503329d15b5SVitaly Buka }, 1504329d15b5SVitaly Buka { 1505329d15b5SVitaly Buka "", 1506329d15b5SVitaly Buka "{i8, i64, i16*} {i8 86, i64 -6148914691236517206, i16* undef}", 1507329d15b5SVitaly Buka }, 1508329d15b5SVitaly Buka }; 1509329d15b5SVitaly Buka 1510329d15b5SVitaly Buka INSTANTIATE_TEST_CASE_P(IsBytewiseValueParamTests, IsBytewiseValueTest, 15117618b2a5SMikael Holmen ::testing::ValuesIn(IsBytewiseValueTests),); 1512329d15b5SVitaly Buka 1513329d15b5SVitaly Buka TEST_P(IsBytewiseValueTest, IsBytewiseValue) { 1514329d15b5SVitaly Buka auto M = parseModule(std::string("@test = global ") + GetParam().second); 1515329d15b5SVitaly Buka GlobalVariable *GV = dyn_cast<GlobalVariable>(M->getNamedValue("test")); 1516329d15b5SVitaly Buka Value *Actual = isBytewiseValue(GV->getInitializer(), M->getDataLayout()); 1517329d15b5SVitaly Buka std::string Buff; 1518329d15b5SVitaly Buka raw_string_ostream S(Buff); 1519329d15b5SVitaly Buka if (Actual) 1520329d15b5SVitaly Buka S << *Actual; 1521329d15b5SVitaly Buka EXPECT_EQ(GetParam().first, S.str()); 1522329d15b5SVitaly Buka } 15238d041811SFlorian Hahn 15248d041811SFlorian Hahn TEST_F(ValueTrackingTest, ComputeConstantRange) { 15258d041811SFlorian Hahn { 15268d041811SFlorian Hahn // Assumptions: 15278d041811SFlorian Hahn // * stride >= 5 15288d041811SFlorian Hahn // * stride < 10 15298d041811SFlorian Hahn // 15308d041811SFlorian Hahn // stride = [5, 10) 15318d041811SFlorian Hahn auto M = parseModule(R"( 15328d041811SFlorian Hahn declare void @llvm.assume(i1) 15338d041811SFlorian Hahn 15348d041811SFlorian Hahn define i32 @test(i32 %stride) { 15358d041811SFlorian Hahn %gt = icmp uge i32 %stride, 5 15368d041811SFlorian Hahn call void @llvm.assume(i1 %gt) 15378d041811SFlorian Hahn %lt = icmp ult i32 %stride, 10 15388d041811SFlorian Hahn call void @llvm.assume(i1 %lt) 15398d041811SFlorian Hahn %stride.plus.one = add nsw nuw i32 %stride, 1 15408d041811SFlorian Hahn ret i32 %stride.plus.one 15418d041811SFlorian Hahn })"); 15428d041811SFlorian Hahn Function *F = M->getFunction("test"); 15438d041811SFlorian Hahn 15448d041811SFlorian Hahn AssumptionCache AC(*F); 15458d041811SFlorian Hahn Value *Stride = &*F->arg_begin(); 15468d041811SFlorian Hahn ConstantRange CR1 = computeConstantRange(Stride, true, &AC, nullptr); 15478d041811SFlorian Hahn EXPECT_TRUE(CR1.isFullSet()); 15488d041811SFlorian Hahn 15498d041811SFlorian Hahn Instruction *I = &findInstructionByName(F, "stride.plus.one"); 15508d041811SFlorian Hahn ConstantRange CR2 = computeConstantRange(Stride, true, &AC, I); 15518d041811SFlorian Hahn EXPECT_EQ(5, CR2.getLower()); 15528d041811SFlorian Hahn EXPECT_EQ(10, CR2.getUpper()); 15538d041811SFlorian Hahn } 15548d041811SFlorian Hahn 15558d041811SFlorian Hahn { 15568d041811SFlorian Hahn // Assumptions: 15578d041811SFlorian Hahn // * stride >= 5 15588d041811SFlorian Hahn // * stride < 200 15598d041811SFlorian Hahn // * stride == 99 15608d041811SFlorian Hahn // 15618d041811SFlorian Hahn // stride = [99, 100) 15628d041811SFlorian Hahn auto M = parseModule(R"( 15638d041811SFlorian Hahn declare void @llvm.assume(i1) 15648d041811SFlorian Hahn 15658d041811SFlorian Hahn define i32 @test(i32 %stride) { 15668d041811SFlorian Hahn %gt = icmp uge i32 %stride, 5 15678d041811SFlorian Hahn call void @llvm.assume(i1 %gt) 15688d041811SFlorian Hahn %lt = icmp ult i32 %stride, 200 15698d041811SFlorian Hahn call void @llvm.assume(i1 %lt) 15708d041811SFlorian Hahn %eq = icmp eq i32 %stride, 99 15718d041811SFlorian Hahn call void @llvm.assume(i1 %eq) 15728d041811SFlorian Hahn %stride.plus.one = add nsw nuw i32 %stride, 1 15738d041811SFlorian Hahn ret i32 %stride.plus.one 15748d041811SFlorian Hahn })"); 15758d041811SFlorian Hahn Function *F = M->getFunction("test"); 15768d041811SFlorian Hahn 15778d041811SFlorian Hahn AssumptionCache AC(*F); 15788d041811SFlorian Hahn Value *Stride = &*F->arg_begin(); 15798d041811SFlorian Hahn Instruction *I = &findInstructionByName(F, "stride.plus.one"); 15808d041811SFlorian Hahn ConstantRange CR = computeConstantRange(Stride, true, &AC, I); 15818d041811SFlorian Hahn EXPECT_EQ(99, *CR.getSingleElement()); 15828d041811SFlorian Hahn } 15838d041811SFlorian Hahn 15848d041811SFlorian Hahn { 15858d041811SFlorian Hahn // Assumptions: 15868d041811SFlorian Hahn // * stride >= 5 15878d041811SFlorian Hahn // * stride >= 50 15888d041811SFlorian Hahn // * stride < 100 15898d041811SFlorian Hahn // * stride < 200 15908d041811SFlorian Hahn // 15918d041811SFlorian Hahn // stride = [50, 100) 15928d041811SFlorian Hahn auto M = parseModule(R"( 15938d041811SFlorian Hahn declare void @llvm.assume(i1) 15948d041811SFlorian Hahn 15958d041811SFlorian Hahn define i32 @test(i32 %stride, i1 %cond) { 15968d041811SFlorian Hahn %gt = icmp uge i32 %stride, 5 15978d041811SFlorian Hahn call void @llvm.assume(i1 %gt) 15988d041811SFlorian Hahn %gt.2 = icmp uge i32 %stride, 50 15998d041811SFlorian Hahn call void @llvm.assume(i1 %gt.2) 16008d041811SFlorian Hahn br i1 %cond, label %bb1, label %bb2 16018d041811SFlorian Hahn 16028d041811SFlorian Hahn bb1: 16038d041811SFlorian Hahn %lt = icmp ult i32 %stride, 200 16048d041811SFlorian Hahn call void @llvm.assume(i1 %lt) 16058d041811SFlorian Hahn %lt.2 = icmp ult i32 %stride, 100 16068d041811SFlorian Hahn call void @llvm.assume(i1 %lt.2) 16078d041811SFlorian Hahn %stride.plus.one = add nsw nuw i32 %stride, 1 16088d041811SFlorian Hahn ret i32 %stride.plus.one 16098d041811SFlorian Hahn 16108d041811SFlorian Hahn bb2: 16118d041811SFlorian Hahn ret i32 0 16128d041811SFlorian Hahn })"); 16138d041811SFlorian Hahn Function *F = M->getFunction("test"); 16148d041811SFlorian Hahn 16158d041811SFlorian Hahn AssumptionCache AC(*F); 16168d041811SFlorian Hahn Value *Stride = &*F->arg_begin(); 16178d041811SFlorian Hahn Instruction *GT2 = &findInstructionByName(F, "gt.2"); 16188d041811SFlorian Hahn ConstantRange CR = computeConstantRange(Stride, true, &AC, GT2); 16198d041811SFlorian Hahn EXPECT_EQ(5, CR.getLower()); 16208d041811SFlorian Hahn EXPECT_EQ(0, CR.getUpper()); 16218d041811SFlorian Hahn 16228d041811SFlorian Hahn Instruction *I = &findInstructionByName(F, "stride.plus.one"); 16238d041811SFlorian Hahn ConstantRange CR2 = computeConstantRange(Stride, true, &AC, I); 16248d041811SFlorian Hahn EXPECT_EQ(50, CR2.getLower()); 16258d041811SFlorian Hahn EXPECT_EQ(100, CR2.getUpper()); 16268d041811SFlorian Hahn } 16278d041811SFlorian Hahn 16288d041811SFlorian Hahn { 16298d041811SFlorian Hahn // Assumptions: 16308d041811SFlorian Hahn // * stride > 5 16318d041811SFlorian Hahn // * stride < 5 16328d041811SFlorian Hahn // 16338d041811SFlorian Hahn // stride = empty range, as the assumptions contradict each other. 16348d041811SFlorian Hahn auto M = parseModule(R"( 16358d041811SFlorian Hahn declare void @llvm.assume(i1) 16368d041811SFlorian Hahn 16378d041811SFlorian Hahn define i32 @test(i32 %stride, i1 %cond) { 16388d041811SFlorian Hahn %gt = icmp ugt i32 %stride, 5 16398d041811SFlorian Hahn call void @llvm.assume(i1 %gt) 16408d041811SFlorian Hahn %lt = icmp ult i32 %stride, 5 16418d041811SFlorian Hahn call void @llvm.assume(i1 %lt) 16428d041811SFlorian Hahn %stride.plus.one = add nsw nuw i32 %stride, 1 16438d041811SFlorian Hahn ret i32 %stride.plus.one 16448d041811SFlorian Hahn })"); 16458d041811SFlorian Hahn Function *F = M->getFunction("test"); 16468d041811SFlorian Hahn 16478d041811SFlorian Hahn AssumptionCache AC(*F); 16488d041811SFlorian Hahn Value *Stride = &*F->arg_begin(); 16498d041811SFlorian Hahn 16508d041811SFlorian Hahn Instruction *I = &findInstructionByName(F, "stride.plus.one"); 16518d041811SFlorian Hahn ConstantRange CR = computeConstantRange(Stride, true, &AC, I); 16528d041811SFlorian Hahn EXPECT_TRUE(CR.isEmptySet()); 16538d041811SFlorian Hahn } 16548d041811SFlorian Hahn 16558d041811SFlorian Hahn { 16568d041811SFlorian Hahn // Assumptions: 16578d041811SFlorian Hahn // * x.1 >= 5 16588d041811SFlorian Hahn // * x.2 < x.1 16598d041811SFlorian Hahn // 16608d041811SFlorian Hahn // stride = [0, 5) 16618d041811SFlorian Hahn auto M = parseModule(R"( 16628d041811SFlorian Hahn declare void @llvm.assume(i1) 16638d041811SFlorian Hahn 16648d041811SFlorian Hahn define i32 @test(i32 %x.1, i32 %x.2) { 16658d041811SFlorian Hahn %gt = icmp uge i32 %x.1, 5 16668d041811SFlorian Hahn call void @llvm.assume(i1 %gt) 16678d041811SFlorian Hahn %lt = icmp ult i32 %x.2, %x.1 16688d041811SFlorian Hahn call void @llvm.assume(i1 %lt) 16698d041811SFlorian Hahn %stride.plus.one = add nsw nuw i32 %x.1, 1 16708d041811SFlorian Hahn ret i32 %stride.plus.one 16718d041811SFlorian Hahn })"); 16728d041811SFlorian Hahn Function *F = M->getFunction("test"); 16738d041811SFlorian Hahn 16748d041811SFlorian Hahn AssumptionCache AC(*F); 16758d041811SFlorian Hahn Value *X2 = &*std::next(F->arg_begin()); 16768d041811SFlorian Hahn 16778d041811SFlorian Hahn Instruction *I = &findInstructionByName(F, "stride.plus.one"); 16788d041811SFlorian Hahn ConstantRange CR1 = computeConstantRange(X2, true, &AC, I); 16798d041811SFlorian Hahn EXPECT_EQ(0, CR1.getLower()); 16808d041811SFlorian Hahn EXPECT_EQ(5, CR1.getUpper()); 16818d041811SFlorian Hahn 16828d041811SFlorian Hahn // Check the depth cutoff results in a conservative result (full set) by 16838d041811SFlorian Hahn // passing Depth == MaxDepth == 6. 16848d041811SFlorian Hahn ConstantRange CR2 = computeConstantRange(X2, true, &AC, I, 6); 16858d041811SFlorian Hahn EXPECT_TRUE(CR2.isFullSet()); 16868d041811SFlorian Hahn } 16878d041811SFlorian Hahn } 16887f1bb326SVitaly Buka 16896ccacb46SVitaly Buka struct FindAllocaForValueTestParams { 16906ccacb46SVitaly Buka const char *IR; 1691a6927c86SVitaly Buka bool AnyOffsetResult; 1692a6927c86SVitaly Buka bool ZeroOffsetResult; 16936ccacb46SVitaly Buka }; 16946ccacb46SVitaly Buka 16957f1bb326SVitaly Buka class FindAllocaForValueTest 16967f1bb326SVitaly Buka : public ValueTrackingTest, 16976ccacb46SVitaly Buka public ::testing::WithParamInterface<FindAllocaForValueTestParams> { 16987f1bb326SVitaly Buka protected: 16997f1bb326SVitaly Buka }; 17007f1bb326SVitaly Buka 17016ccacb46SVitaly Buka const FindAllocaForValueTestParams FindAllocaForValueTests[] = { 17027f1bb326SVitaly Buka {R"( 17037f1bb326SVitaly Buka define void @test() { 17047f1bb326SVitaly Buka %a = alloca i64 17057f1bb326SVitaly Buka %r = bitcast i64* %a to i32* 17067f1bb326SVitaly Buka ret void 17077f1bb326SVitaly Buka })", 1708a6927c86SVitaly Buka true, true}, 17097f1bb326SVitaly Buka 17107f1bb326SVitaly Buka {R"( 17117f1bb326SVitaly Buka define void @test() { 17127f1bb326SVitaly Buka %a = alloca i32 17137f1bb326SVitaly Buka %r = getelementptr i32, i32* %a, i32 1 17147f1bb326SVitaly Buka ret void 17157f1bb326SVitaly Buka })", 1716a6927c86SVitaly Buka true, false}, 1717a6927c86SVitaly Buka 1718a6927c86SVitaly Buka {R"( 1719a6927c86SVitaly Buka define void @test() { 1720a6927c86SVitaly Buka %a = alloca i32 1721a6927c86SVitaly Buka %r = getelementptr i32, i32* %a, i32 0 1722a6927c86SVitaly Buka ret void 1723a6927c86SVitaly Buka })", 1724a6927c86SVitaly Buka true, true}, 17257f1bb326SVitaly Buka 17267f1bb326SVitaly Buka {R"( 17277f1bb326SVitaly Buka define void @test(i1 %cond) { 17287f1bb326SVitaly Buka entry: 17297f1bb326SVitaly Buka %a = alloca i32 17307f1bb326SVitaly Buka br label %bb1 17317f1bb326SVitaly Buka 17327f1bb326SVitaly Buka bb1: 17337f1bb326SVitaly Buka %r = phi i32* [ %a, %entry ], [ %r, %bb1 ] 17347f1bb326SVitaly Buka br i1 %cond, label %bb1, label %exit 17357f1bb326SVitaly Buka 17367f1bb326SVitaly Buka exit: 17377f1bb326SVitaly Buka ret void 17387f1bb326SVitaly Buka })", 1739a6927c86SVitaly Buka true, true}, 17407f1bb326SVitaly Buka 17417f1bb326SVitaly Buka {R"( 17427f1bb326SVitaly Buka define void @test(i1 %cond) { 1743469debe0SVitaly Buka %a = alloca i32 1744469debe0SVitaly Buka %r = select i1 %cond, i32* %a, i32* %a 1745469debe0SVitaly Buka ret void 1746469debe0SVitaly Buka })", 1747a6927c86SVitaly Buka true, true}, 1748469debe0SVitaly Buka 1749469debe0SVitaly Buka {R"( 1750469debe0SVitaly Buka define void @test(i1 %cond) { 1751469debe0SVitaly Buka %a = alloca i32 1752469debe0SVitaly Buka %b = alloca i32 1753469debe0SVitaly Buka %r = select i1 %cond, i32* %a, i32* %b 1754469debe0SVitaly Buka ret void 1755469debe0SVitaly Buka })", 1756a6927c86SVitaly Buka false, false}, 1757469debe0SVitaly Buka 1758469debe0SVitaly Buka {R"( 1759469debe0SVitaly Buka define void @test(i1 %cond) { 17607f1bb326SVitaly Buka entry: 17617f1bb326SVitaly Buka %a = alloca i64 17627f1bb326SVitaly Buka %a32 = bitcast i64* %a to i32* 17637f1bb326SVitaly Buka br label %bb1 17647f1bb326SVitaly Buka 17657f1bb326SVitaly Buka bb1: 17667f1bb326SVitaly Buka %x = phi i32* [ %a32, %entry ], [ %x, %bb1 ] 17677f1bb326SVitaly Buka %r = getelementptr i32, i32* %x, i32 1 17687f1bb326SVitaly Buka br i1 %cond, label %bb1, label %exit 17697f1bb326SVitaly Buka 17707f1bb326SVitaly Buka exit: 17717f1bb326SVitaly Buka ret void 17727f1bb326SVitaly Buka })", 1773a6927c86SVitaly Buka true, false}, 17747f1bb326SVitaly Buka 17757f1bb326SVitaly Buka {R"( 17767f1bb326SVitaly Buka define void @test(i1 %cond) { 17777f1bb326SVitaly Buka entry: 17787f1bb326SVitaly Buka %a = alloca i64 17797f1bb326SVitaly Buka %a32 = bitcast i64* %a to i32* 17807f1bb326SVitaly Buka br label %bb1 17817f1bb326SVitaly Buka 17827f1bb326SVitaly Buka bb1: 17837f1bb326SVitaly Buka %x = phi i32* [ %a32, %entry ], [ %r, %bb1 ] 17847f1bb326SVitaly Buka %r = getelementptr i32, i32* %x, i32 1 17857f1bb326SVitaly Buka br i1 %cond, label %bb1, label %exit 17867f1bb326SVitaly Buka 17877f1bb326SVitaly Buka exit: 17887f1bb326SVitaly Buka ret void 17897f1bb326SVitaly Buka })", 179023524fdeSVitaly Buka true, false}, 17917f1bb326SVitaly Buka 17927f1bb326SVitaly Buka {R"( 17937f1bb326SVitaly Buka define void @test(i1 %cond, i64* %a) { 17947f1bb326SVitaly Buka entry: 17957f1bb326SVitaly Buka %r = bitcast i64* %a to i32* 17967f1bb326SVitaly Buka ret void 17977f1bb326SVitaly Buka })", 1798a6927c86SVitaly Buka false, false}, 17997f1bb326SVitaly Buka 18007f1bb326SVitaly Buka {R"( 18017f1bb326SVitaly Buka define void @test(i1 %cond) { 18027f1bb326SVitaly Buka entry: 18037f1bb326SVitaly Buka %a = alloca i32 18047f1bb326SVitaly Buka %b = alloca i32 18057f1bb326SVitaly Buka br label %bb1 18067f1bb326SVitaly Buka 18077f1bb326SVitaly Buka bb1: 18088191603dSVitaly Buka %r = phi i32* [ %a, %entry ], [ %b, %bb1 ] 18097f1bb326SVitaly Buka br i1 %cond, label %bb1, label %exit 18107f1bb326SVitaly Buka 18117f1bb326SVitaly Buka exit: 18127f1bb326SVitaly Buka ret void 18137f1bb326SVitaly Buka })", 1814a6927c86SVitaly Buka false, false}, 18157f1bb326SVitaly Buka }; 18167f1bb326SVitaly Buka 18177f1bb326SVitaly Buka TEST_P(FindAllocaForValueTest, findAllocaForValue) { 18186ccacb46SVitaly Buka auto M = parseModule(GetParam().IR); 18197f1bb326SVitaly Buka Function *F = M->getFunction("test"); 18207f1bb326SVitaly Buka Instruction *I = &findInstructionByName(F, "r"); 18217f1bb326SVitaly Buka const AllocaInst *AI = findAllocaForValue(I); 1822a6927c86SVitaly Buka EXPECT_EQ(!!AI, GetParam().AnyOffsetResult); 1823a6927c86SVitaly Buka } 1824a6927c86SVitaly Buka 1825a6927c86SVitaly Buka TEST_P(FindAllocaForValueTest, findAllocaForValueZeroOffset) { 1826a6927c86SVitaly Buka auto M = parseModule(GetParam().IR); 1827a6927c86SVitaly Buka Function *F = M->getFunction("test"); 1828a6927c86SVitaly Buka Instruction *I = &findInstructionByName(F, "r"); 1829a6927c86SVitaly Buka const AllocaInst *AI = findAllocaForValue(I, true); 1830a6927c86SVitaly Buka EXPECT_EQ(!!AI, GetParam().ZeroOffsetResult); 18317f1bb326SVitaly Buka } 18327f1bb326SVitaly Buka 18337f1bb326SVitaly Buka INSTANTIATE_TEST_CASE_P(FindAllocaForValueTest, FindAllocaForValueTest, 18347f1bb326SVitaly Buka ::testing::ValuesIn(FindAllocaForValueTests), ); 1835