1134bec27SJames Molloy //===- ValueTrackingTest.cpp - ValueTracking tests ------------------------===// 2134bec27SJames Molloy // 3134bec27SJames Molloy // The LLVM Compiler Infrastructure 4134bec27SJames Molloy // 5134bec27SJames Molloy // This file is distributed under the University of Illinois Open Source 6134bec27SJames Molloy // License. See LICENSE.TXT for details. 7134bec27SJames Molloy // 8134bec27SJames Molloy //===----------------------------------------------------------------------===// 9134bec27SJames Molloy 10134bec27SJames Molloy #include "llvm/Analysis/ValueTracking.h" 11134bec27SJames Molloy #include "llvm/AsmParser/Parser.h" 12134bec27SJames Molloy #include "llvm/IR/Function.h" 13134bec27SJames Molloy #include "llvm/IR/InstIterator.h" 14134bec27SJames Molloy #include "llvm/IR/LLVMContext.h" 15134bec27SJames Molloy #include "llvm/IR/Module.h" 16134bec27SJames Molloy #include "llvm/Support/ErrorHandling.h" 17134bec27SJames Molloy #include "llvm/Support/SourceMgr.h" 18134bec27SJames Molloy #include "gtest/gtest.h" 19134bec27SJames Molloy 20134bec27SJames Molloy using namespace llvm; 21134bec27SJames Molloy 22134bec27SJames Molloy namespace { 23134bec27SJames Molloy 24134bec27SJames Molloy class MatchSelectPatternTest : public testing::Test { 25134bec27SJames Molloy protected: 26134bec27SJames Molloy void parseAssembly(const char *Assembly) { 27134bec27SJames Molloy SMDiagnostic Error; 28134bec27SJames Molloy M = parseAssemblyString(Assembly, Error, getGlobalContext()); 29134bec27SJames Molloy 30134bec27SJames Molloy std::string errMsg; 31134bec27SJames Molloy raw_string_ostream os(errMsg); 32134bec27SJames Molloy Error.print("", os); 33134bec27SJames Molloy 34134bec27SJames Molloy // A failure here means that the test itself is buggy. 35134bec27SJames Molloy if (!M) 36134bec27SJames Molloy report_fatal_error(os.str()); 37134bec27SJames Molloy 38134bec27SJames Molloy Function *F = M->getFunction("test"); 39134bec27SJames Molloy if (F == nullptr) 40134bec27SJames Molloy report_fatal_error("Test must have a function named @test"); 41134bec27SJames Molloy 42134bec27SJames Molloy A = nullptr; 43134bec27SJames Molloy for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) { 44134bec27SJames Molloy if (I->hasName()) { 45134bec27SJames Molloy if (I->getName() == "A") 46134bec27SJames Molloy A = &*I; 47134bec27SJames Molloy } 48134bec27SJames Molloy } 49134bec27SJames Molloy if (A == nullptr) 50134bec27SJames Molloy report_fatal_error("@test must have an instruction %A"); 51134bec27SJames Molloy } 52134bec27SJames Molloy 53134bec27SJames Molloy void expectPattern(const SelectPatternResult &P) { 54134bec27SJames Molloy Value *LHS, *RHS; 55134bec27SJames Molloy Instruction::CastOps CastOp; 56134bec27SJames Molloy SelectPatternResult R = matchSelectPattern(A, LHS, RHS, &CastOp); 57134bec27SJames Molloy EXPECT_EQ(P.Flavor, R.Flavor); 58134bec27SJames Molloy EXPECT_EQ(P.NaNBehavior, R.NaNBehavior); 59134bec27SJames Molloy EXPECT_EQ(P.Ordered, R.Ordered); 60134bec27SJames Molloy } 61134bec27SJames Molloy 62134bec27SJames Molloy std::unique_ptr<Module> M; 63134bec27SJames Molloy Instruction *A, *B; 64134bec27SJames Molloy }; 65134bec27SJames Molloy 66134bec27SJames Molloy } 67134bec27SJames Molloy 68134bec27SJames Molloy TEST_F(MatchSelectPatternTest, SimpleFMin) { 69134bec27SJames Molloy parseAssembly( 70134bec27SJames Molloy "define float @test(float %a) {\n" 71134bec27SJames Molloy " %1 = fcmp ult float %a, 5.0\n" 72134bec27SJames Molloy " %A = select i1 %1, float %a, float 5.0\n" 73134bec27SJames Molloy " ret float %A\n" 74134bec27SJames Molloy "}\n"); 75134bec27SJames Molloy expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false}); 76134bec27SJames Molloy } 77134bec27SJames Molloy 78134bec27SJames Molloy TEST_F(MatchSelectPatternTest, SimpleFMax) { 79134bec27SJames Molloy parseAssembly( 80134bec27SJames Molloy "define float @test(float %a) {\n" 81134bec27SJames Molloy " %1 = fcmp ogt float %a, 5.0\n" 82134bec27SJames Molloy " %A = select i1 %1, float %a, float 5.0\n" 83134bec27SJames Molloy " ret float %A\n" 84134bec27SJames Molloy "}\n"); 85134bec27SJames Molloy expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true}); 86134bec27SJames Molloy } 87134bec27SJames Molloy 88134bec27SJames Molloy TEST_F(MatchSelectPatternTest, SwappedFMax) { 89134bec27SJames Molloy parseAssembly( 90134bec27SJames Molloy "define float @test(float %a) {\n" 91134bec27SJames Molloy " %1 = fcmp olt float 5.0, %a\n" 92134bec27SJames Molloy " %A = select i1 %1, float %a, float 5.0\n" 93134bec27SJames Molloy " ret float %A\n" 94134bec27SJames Molloy "}\n"); 95134bec27SJames Molloy expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, false}); 96134bec27SJames Molloy } 97134bec27SJames Molloy 98134bec27SJames Molloy TEST_F(MatchSelectPatternTest, SwappedFMax2) { 99134bec27SJames Molloy parseAssembly( 100134bec27SJames Molloy "define float @test(float %a) {\n" 101134bec27SJames Molloy " %1 = fcmp olt float %a, 5.0\n" 102134bec27SJames Molloy " %A = select i1 %1, float 5.0, float %a\n" 103134bec27SJames Molloy " ret float %A\n" 104134bec27SJames Molloy "}\n"); 105134bec27SJames Molloy expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, false}); 106134bec27SJames Molloy } 107134bec27SJames Molloy 108134bec27SJames Molloy TEST_F(MatchSelectPatternTest, SwappedFMax3) { 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 5.0, float %a\n" 113134bec27SJames Molloy " ret float %A\n" 114134bec27SJames Molloy "}\n"); 115134bec27SJames Molloy expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true}); 116134bec27SJames Molloy } 117134bec27SJames Molloy 118134bec27SJames Molloy TEST_F(MatchSelectPatternTest, FastFMin) { 119134bec27SJames Molloy parseAssembly( 120134bec27SJames Molloy "define float @test(float %a) {\n" 121134bec27SJames Molloy " %1 = fcmp nnan olt 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_FMINNUM, SPNB_RETURNS_ANY, false}); 126134bec27SJames Molloy } 127134bec27SJames Molloy 128134bec27SJames Molloy TEST_F(MatchSelectPatternTest, FMinConstantZero) { 129134bec27SJames Molloy parseAssembly( 130134bec27SJames Molloy "define float @test(float %a) {\n" 131134bec27SJames Molloy " %1 = fcmp ole float %a, 0.0\n" 132134bec27SJames Molloy " %A = select i1 %1, float %a, float 0.0\n" 133134bec27SJames Molloy " ret float %A\n" 134134bec27SJames Molloy "}\n"); 135134bec27SJames Molloy // This shouldn't be matched, as %a could be -0.0. 136134bec27SJames Molloy expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 137134bec27SJames Molloy } 138134bec27SJames Molloy 139134bec27SJames Molloy TEST_F(MatchSelectPatternTest, FMinConstantZeroNsz) { 140134bec27SJames Molloy parseAssembly( 141134bec27SJames Molloy "define float @test(float %a) {\n" 142134bec27SJames Molloy " %1 = fcmp nsz ole float %a, 0.0\n" 143134bec27SJames Molloy " %A = select i1 %1, float %a, float 0.0\n" 144134bec27SJames Molloy " ret float %A\n" 145134bec27SJames Molloy "}\n"); 146134bec27SJames Molloy // But this should be, because we've ignored signed zeroes. 147134bec27SJames Molloy expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true}); 148134bec27SJames Molloy } 149*569cea65SJames Molloy 150*569cea65SJames Molloy TEST_F(MatchSelectPatternTest, DoubleCastU) { 151*569cea65SJames Molloy parseAssembly( 152*569cea65SJames Molloy "define i32 @test(i8 %a, i8 %b) {\n" 153*569cea65SJames Molloy " %1 = icmp ult i8 %a, %b\n" 154*569cea65SJames Molloy " %2 = zext i8 %a to i32\n" 155*569cea65SJames Molloy " %3 = zext i8 %b to i32\n" 156*569cea65SJames Molloy " %A = select i1 %1, i32 %2, i32 %3\n" 157*569cea65SJames Molloy " ret i32 %A\n" 158*569cea65SJames Molloy "}\n"); 159*569cea65SJames Molloy // We should be able to look through the situation where we cast both operands 160*569cea65SJames Molloy // to the select. 161*569cea65SJames Molloy expectPattern({SPF_UMIN, SPNB_NA, false}); 162*569cea65SJames Molloy } 163*569cea65SJames Molloy 164*569cea65SJames Molloy TEST_F(MatchSelectPatternTest, DoubleCastS) { 165*569cea65SJames Molloy parseAssembly( 166*569cea65SJames Molloy "define i32 @test(i8 %a, i8 %b) {\n" 167*569cea65SJames Molloy " %1 = icmp slt i8 %a, %b\n" 168*569cea65SJames Molloy " %2 = sext i8 %a to i32\n" 169*569cea65SJames Molloy " %3 = sext i8 %b to i32\n" 170*569cea65SJames Molloy " %A = select i1 %1, i32 %2, i32 %3\n" 171*569cea65SJames Molloy " ret i32 %A\n" 172*569cea65SJames Molloy "}\n"); 173*569cea65SJames Molloy // We should be able to look through the situation where we cast both operands 174*569cea65SJames Molloy // to the select. 175*569cea65SJames Molloy expectPattern({SPF_SMIN, SPNB_NA, false}); 176*569cea65SJames Molloy } 177*569cea65SJames Molloy 178*569cea65SJames Molloy TEST_F(MatchSelectPatternTest, DoubleCastBad) { 179*569cea65SJames Molloy parseAssembly( 180*569cea65SJames Molloy "define i32 @test(i8 %a, i8 %b) {\n" 181*569cea65SJames Molloy " %1 = icmp ult i8 %a, %b\n" 182*569cea65SJames Molloy " %2 = zext i8 %a to i32\n" 183*569cea65SJames Molloy " %3 = sext i8 %b to i32\n" 184*569cea65SJames Molloy " %A = select i1 %1, i32 %2, i32 %3\n" 185*569cea65SJames Molloy " ret i32 %A\n" 186*569cea65SJames Molloy "}\n"); 187*569cea65SJames Molloy // We should be able to look through the situation where we cast both operands 188*569cea65SJames Molloy // to the select. 189*569cea65SJames Molloy expectPattern({SPF_UNKNOWN, SPNB_NA, false}); 190*569cea65SJames Molloy } 191