1 //===- ValueTrackingTest.cpp - ValueTracking tests ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Analysis/ValueTracking.h"
11 #include "llvm/AsmParser/Parser.h"
12 #include "llvm/IR/Function.h"
13 #include "llvm/IR/InstIterator.h"
14 #include "llvm/IR/LLVMContext.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/SourceMgr.h"
18 #include "gtest/gtest.h"
24 class MatchSelectPatternTest : public testing::Test {
26 void parseAssembly(const char *Assembly) {
28 M = parseAssemblyString(Assembly, Error, getGlobalContext());
31 raw_string_ostream os(errMsg);
34 // A failure here means that the test itself is buggy.
36 report_fatal_error(os.str());
38 Function *F = M->getFunction("test");
40 report_fatal_error("Test must have a function named @test");
43 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
45 if (I->getName() == "A")
50 report_fatal_error("@test must have an instruction %A");
53 void expectPattern(const SelectPatternResult &P) {
55 Instruction::CastOps CastOp;
56 SelectPatternResult R = matchSelectPattern(A, LHS, RHS, &CastOp);
57 EXPECT_EQ(P.Flavor, R.Flavor);
58 EXPECT_EQ(P.NaNBehavior, R.NaNBehavior);
59 EXPECT_EQ(P.Ordered, R.Ordered);
62 std::unique_ptr<Module> M;
68 TEST_F(MatchSelectPatternTest, SimpleFMin) {
70 "define float @test(float %a) {\n"
71 " %1 = fcmp ult float %a, 5.0\n"
72 " %A = select i1 %1, float %a, float 5.0\n"
75 expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false});
78 TEST_F(MatchSelectPatternTest, SimpleFMax) {
80 "define float @test(float %a) {\n"
81 " %1 = fcmp ogt float %a, 5.0\n"
82 " %A = select i1 %1, float %a, float 5.0\n"
85 expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true});
88 TEST_F(MatchSelectPatternTest, SwappedFMax) {
90 "define float @test(float %a) {\n"
91 " %1 = fcmp olt float 5.0, %a\n"
92 " %A = select i1 %1, float %a, float 5.0\n"
95 expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, false});
98 TEST_F(MatchSelectPatternTest, SwappedFMax2) {
100 "define float @test(float %a) {\n"
101 " %1 = fcmp olt float %a, 5.0\n"
102 " %A = select i1 %1, float 5.0, float %a\n"
105 expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, false});
108 TEST_F(MatchSelectPatternTest, SwappedFMax3) {
110 "define float @test(float %a) {\n"
111 " %1 = fcmp ult float %a, 5.0\n"
112 " %A = select i1 %1, float 5.0, float %a\n"
115 expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true});
118 TEST_F(MatchSelectPatternTest, FastFMin) {
120 "define float @test(float %a) {\n"
121 " %1 = fcmp nnan olt float %a, 5.0\n"
122 " %A = select i1 %1, float %a, float 5.0\n"
125 expectPattern({SPF_FMINNUM, SPNB_RETURNS_ANY, false});
128 TEST_F(MatchSelectPatternTest, FMinConstantZero) {
130 "define float @test(float %a) {\n"
131 " %1 = fcmp ole float %a, 0.0\n"
132 " %A = select i1 %1, float %a, float 0.0\n"
135 // This shouldn't be matched, as %a could be -0.0.
136 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
139 TEST_F(MatchSelectPatternTest, FMinConstantZeroNsz) {
141 "define float @test(float %a) {\n"
142 " %1 = fcmp nsz ole float %a, 0.0\n"
143 " %A = select i1 %1, float %a, float 0.0\n"
146 // But this should be, because we've ignored signed zeroes.
147 expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true});
150 TEST_F(MatchSelectPatternTest, DoubleCastU) {
152 "define i32 @test(i8 %a, i8 %b) {\n"
153 " %1 = icmp ult i8 %a, %b\n"
154 " %2 = zext i8 %a to i32\n"
155 " %3 = zext i8 %b to i32\n"
156 " %A = select i1 %1, i32 %2, i32 %3\n"
159 // We should be able to look through the situation where we cast both operands
161 expectPattern({SPF_UMIN, SPNB_NA, false});
164 TEST_F(MatchSelectPatternTest, DoubleCastS) {
166 "define i32 @test(i8 %a, i8 %b) {\n"
167 " %1 = icmp slt i8 %a, %b\n"
168 " %2 = sext i8 %a to i32\n"
169 " %3 = sext i8 %b to i32\n"
170 " %A = select i1 %1, i32 %2, i32 %3\n"
173 // We should be able to look through the situation where we cast both operands
175 expectPattern({SPF_SMIN, SPNB_NA, false});
178 TEST_F(MatchSelectPatternTest, DoubleCastBad) {
180 "define i32 @test(i8 %a, i8 %b) {\n"
181 " %1 = icmp ult i8 %a, %b\n"
182 " %2 = zext i8 %a to i32\n"
183 " %3 = sext i8 %b to i32\n"
184 " %A = select i1 %1, i32 %2, i32 %3\n"
187 // The cast types here aren't the same, so we cannot match an UMIN.
188 expectPattern({SPF_UNKNOWN, SPNB_NA, false});