1134bec27SJames Molloy //===- ValueTrackingTest.cpp - ValueTracking tests ------------------------===//
2134bec27SJames Molloy //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6134bec27SJames Molloy //
7134bec27SJames Molloy //===----------------------------------------------------------------------===//
8134bec27SJames Molloy 
9134bec27SJames Molloy #include "llvm/Analysis/ValueTracking.h"
10d86fff6aSNikita Popov #include "llvm/Analysis/AssumptionCache.h"
11134bec27SJames Molloy #include "llvm/AsmParser/Parser.h"
128d041811SFlorian Hahn #include "llvm/IR/ConstantRange.h"
1325ce1e04SJuneyoung Lee #include "llvm/IR/Dominators.h"
14134bec27SJames Molloy #include "llvm/IR/Function.h"
15134bec27SJames Molloy #include "llvm/IR/InstIterator.h"
16be04aba6SCraig Topper #include "llvm/IR/Instructions.h"
17134bec27SJames Molloy #include "llvm/IR/LLVMContext.h"
18134bec27SJames Molloy #include "llvm/IR/Module.h"
19134bec27SJames Molloy #include "llvm/Support/ErrorHandling.h"
2070dbd5fbSSimon Dardis #include "llvm/Support/KnownBits.h"
21329d15b5SVitaly Buka #include "llvm/Support/SourceMgr.h"
22565f4e72SAlex Richardson #include "llvm/Transforms/Utils/Local.h"
23134bec27SJames Molloy #include "gtest/gtest.h"
24134bec27SJames Molloy 
25134bec27SJames Molloy using namespace llvm;
26134bec27SJames Molloy 
27134bec27SJames Molloy namespace {
28134bec27SJames Molloy 
299b3c2a72SJuneyoung Lee static Instruction *findInstructionByNameOrNull(Function *F, StringRef Name) {
308d041811SFlorian Hahn   for (Instruction &I : instructions(F))
318d041811SFlorian Hahn     if (I.getName() == Name)
329b3c2a72SJuneyoung Lee       return &I;
339b3c2a72SJuneyoung Lee 
349b3c2a72SJuneyoung Lee   return nullptr;
359b3c2a72SJuneyoung Lee }
369b3c2a72SJuneyoung Lee 
379b3c2a72SJuneyoung Lee static Instruction &findInstructionByName(Function *F, StringRef Name) {
389b3c2a72SJuneyoung Lee   auto *I = findInstructionByNameOrNull(F, Name);
399b3c2a72SJuneyoung Lee   if (I)
409b3c2a72SJuneyoung Lee     return *I;
418d041811SFlorian Hahn 
428d041811SFlorian Hahn   llvm_unreachable("Expected value not found");
438d041811SFlorian Hahn }
448d041811SFlorian Hahn 
45219e5367SNikita Popov class ValueTrackingTest : public testing::Test {
46134bec27SJames Molloy protected:
47329d15b5SVitaly Buka   std::unique_ptr<Module> parseModule(StringRef Assembly) {
48134bec27SJames Molloy     SMDiagnostic Error;
49329d15b5SVitaly Buka     std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context);
50134bec27SJames Molloy 
51134bec27SJames Molloy     std::string errMsg;
52134bec27SJames Molloy     raw_string_ostream os(errMsg);
53134bec27SJames Molloy     Error.print("", os);
54329d15b5SVitaly Buka     EXPECT_TRUE(M) << os.str();
55134bec27SJames Molloy 
56329d15b5SVitaly Buka     return M;
57329d15b5SVitaly Buka   }
58329d15b5SVitaly Buka 
59329d15b5SVitaly Buka   void parseAssembly(StringRef Assembly) {
60329d15b5SVitaly Buka     M = parseModule(Assembly);
61329d15b5SVitaly Buka     ASSERT_TRUE(M);
62134bec27SJames Molloy 
63d86fff6aSNikita Popov     F = M->getFunction("test");
64329d15b5SVitaly Buka     ASSERT_TRUE(F) << "Test must have a function @test";
65329d15b5SVitaly Buka     if (!F)
66329d15b5SVitaly Buka       return;
67134bec27SJames Molloy 
689b3c2a72SJuneyoung Lee     A = findInstructionByNameOrNull(F, "A");
69329d15b5SVitaly Buka     ASSERT_TRUE(A) << "@test must have an instruction %A";
70eca40c36SJuneyoung Lee     A2 = findInstructionByNameOrNull(F, "A2");
71eca40c36SJuneyoung Lee     A3 = findInstructionByNameOrNull(F, "A3");
72eca40c36SJuneyoung Lee     A4 = findInstructionByNameOrNull(F, "A4");
739b3c2a72SJuneyoung Lee 
749b3c2a72SJuneyoung Lee     CxtI = findInstructionByNameOrNull(F, "CxtI");
759b3c2a72SJuneyoung Lee     CxtI2 = findInstructionByNameOrNull(F, "CxtI2");
769b3c2a72SJuneyoung Lee     CxtI3 = findInstructionByNameOrNull(F, "CxtI3");
77134bec27SJames Molloy   }
78134bec27SJames Molloy 
79219e5367SNikita Popov   LLVMContext Context;
80219e5367SNikita Popov   std::unique_ptr<Module> M;
81d86fff6aSNikita Popov   Function *F = nullptr;
82329d15b5SVitaly Buka   Instruction *A = nullptr;
83eca40c36SJuneyoung Lee   // Instructions (optional)
84eca40c36SJuneyoung Lee   Instruction *A2 = nullptr, *A3 = nullptr, *A4 = nullptr;
859b3c2a72SJuneyoung Lee 
869b3c2a72SJuneyoung Lee   // Context instructions (optional)
879b3c2a72SJuneyoung Lee   Instruction *CxtI = nullptr, *CxtI2 = nullptr, *CxtI3 = nullptr;
88219e5367SNikita Popov };
89219e5367SNikita Popov 
90219e5367SNikita Popov class MatchSelectPatternTest : public ValueTrackingTest {
91219e5367SNikita Popov protected:
92134bec27SJames Molloy   void expectPattern(const SelectPatternResult &P) {
93134bec27SJames Molloy     Value *LHS, *RHS;
94134bec27SJames Molloy     Instruction::CastOps CastOp;
95134bec27SJames Molloy     SelectPatternResult R = matchSelectPattern(A, LHS, RHS, &CastOp);
96134bec27SJames Molloy     EXPECT_EQ(P.Flavor, R.Flavor);
97134bec27SJames Molloy     EXPECT_EQ(P.NaNBehavior, R.NaNBehavior);
98134bec27SJames Molloy     EXPECT_EQ(P.Ordered, R.Ordered);
99134bec27SJames Molloy   }
100219e5367SNikita Popov };
101134bec27SJames Molloy 
102219e5367SNikita Popov class ComputeKnownBitsTest : public ValueTrackingTest {
103219e5367SNikita Popov protected:
104219e5367SNikita Popov   void expectKnownBits(uint64_t Zero, uint64_t One) {
105219e5367SNikita Popov     auto Known = computeKnownBits(A, M->getDataLayout());
106219e5367SNikita Popov     ASSERT_FALSE(Known.hasConflict());
107219e5367SNikita Popov     EXPECT_EQ(Known.One.getZExtValue(), One);
108219e5367SNikita Popov     EXPECT_EQ(Known.Zero.getZExtValue(), Zero);
109219e5367SNikita Popov   }
110134bec27SJames Molloy };
111134bec27SJames Molloy 
112134bec27SJames Molloy }
113134bec27SJames Molloy 
114134bec27SJames Molloy TEST_F(MatchSelectPatternTest, SimpleFMin) {
115134bec27SJames Molloy   parseAssembly(
116134bec27SJames Molloy       "define float @test(float %a) {\n"
117134bec27SJames Molloy       "  %1 = fcmp ult float %a, 5.0\n"
118134bec27SJames Molloy       "  %A = select i1 %1, float %a, float 5.0\n"
119134bec27SJames Molloy       "  ret float %A\n"
120134bec27SJames Molloy       "}\n");
121134bec27SJames Molloy   expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false});
122134bec27SJames Molloy }
123134bec27SJames Molloy 
124134bec27SJames Molloy TEST_F(MatchSelectPatternTest, SimpleFMax) {
125134bec27SJames Molloy   parseAssembly(
126134bec27SJames Molloy       "define float @test(float %a) {\n"
127134bec27SJames Molloy       "  %1 = fcmp ogt float %a, 5.0\n"
128134bec27SJames Molloy       "  %A = select i1 %1, float %a, float 5.0\n"
129134bec27SJames Molloy       "  ret float %A\n"
130134bec27SJames Molloy       "}\n");
131134bec27SJames Molloy   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true});
132134bec27SJames Molloy }
133134bec27SJames Molloy 
134134bec27SJames Molloy TEST_F(MatchSelectPatternTest, SwappedFMax) {
135134bec27SJames Molloy   parseAssembly(
136134bec27SJames Molloy       "define float @test(float %a) {\n"
137134bec27SJames Molloy       "  %1 = fcmp olt float 5.0, %a\n"
138134bec27SJames Molloy       "  %A = select i1 %1, float %a, float 5.0\n"
139134bec27SJames Molloy       "  ret float %A\n"
140134bec27SJames Molloy       "}\n");
141134bec27SJames Molloy   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, false});
142134bec27SJames Molloy }
143134bec27SJames Molloy 
144134bec27SJames Molloy TEST_F(MatchSelectPatternTest, SwappedFMax2) {
145134bec27SJames Molloy   parseAssembly(
146134bec27SJames Molloy       "define float @test(float %a) {\n"
147134bec27SJames Molloy       "  %1 = fcmp olt float %a, 5.0\n"
148134bec27SJames Molloy       "  %A = select i1 %1, float 5.0, float %a\n"
149134bec27SJames Molloy       "  ret float %A\n"
150134bec27SJames Molloy       "}\n");
151134bec27SJames Molloy   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, false});
152134bec27SJames Molloy }
153134bec27SJames Molloy 
154134bec27SJames Molloy TEST_F(MatchSelectPatternTest, SwappedFMax3) {
155134bec27SJames Molloy   parseAssembly(
156134bec27SJames Molloy       "define float @test(float %a) {\n"
157134bec27SJames Molloy       "  %1 = fcmp ult float %a, 5.0\n"
158134bec27SJames Molloy       "  %A = select i1 %1, float 5.0, float %a\n"
159134bec27SJames Molloy       "  ret float %A\n"
160134bec27SJames Molloy       "}\n");
161134bec27SJames Molloy   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true});
162134bec27SJames Molloy }
163134bec27SJames Molloy 
164134bec27SJames Molloy TEST_F(MatchSelectPatternTest, FastFMin) {
165134bec27SJames Molloy   parseAssembly(
166134bec27SJames Molloy       "define float @test(float %a) {\n"
167134bec27SJames Molloy       "  %1 = fcmp nnan olt float %a, 5.0\n"
168134bec27SJames Molloy       "  %A = select i1 %1, float %a, float 5.0\n"
169134bec27SJames Molloy       "  ret float %A\n"
170134bec27SJames Molloy       "}\n");
171134bec27SJames Molloy   expectPattern({SPF_FMINNUM, SPNB_RETURNS_ANY, false});
172134bec27SJames Molloy }
173134bec27SJames Molloy 
174134bec27SJames Molloy TEST_F(MatchSelectPatternTest, FMinConstantZero) {
175134bec27SJames Molloy   parseAssembly(
176134bec27SJames Molloy       "define float @test(float %a) {\n"
177134bec27SJames Molloy       "  %1 = fcmp ole float %a, 0.0\n"
178134bec27SJames Molloy       "  %A = select i1 %1, float %a, float 0.0\n"
179134bec27SJames Molloy       "  ret float %A\n"
180134bec27SJames Molloy       "}\n");
181134bec27SJames Molloy   // This shouldn't be matched, as %a could be -0.0.
182134bec27SJames Molloy   expectPattern({SPF_UNKNOWN, SPNB_NA, false});
183134bec27SJames Molloy }
184134bec27SJames Molloy 
185134bec27SJames Molloy TEST_F(MatchSelectPatternTest, FMinConstantZeroNsz) {
186134bec27SJames Molloy   parseAssembly(
187134bec27SJames Molloy       "define float @test(float %a) {\n"
188134bec27SJames Molloy       "  %1 = fcmp nsz ole float %a, 0.0\n"
189134bec27SJames Molloy       "  %A = select i1 %1, float %a, float 0.0\n"
190134bec27SJames Molloy       "  ret float %A\n"
191134bec27SJames Molloy       "}\n");
192134bec27SJames Molloy   // But this should be, because we've ignored signed zeroes.
193134bec27SJames Molloy   expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true});
194134bec27SJames Molloy }
195569cea65SJames Molloy 
19603da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero1) {
19703da6e6aSSanjay Patel   parseAssembly(
19803da6e6aSSanjay Patel       "define float @test(float %a) {\n"
19903da6e6aSSanjay Patel       "  %1 = fcmp olt float -0.0, %a\n"
20003da6e6aSSanjay Patel       "  %A = select i1 %1, float 0.0, float %a\n"
20103da6e6aSSanjay Patel       "  ret float %A\n"
20203da6e6aSSanjay Patel       "}\n");
203e7c94ef1SSanjay Patel   // The sign of zero doesn't matter in fcmp.
204e7c94ef1SSanjay Patel   expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, true});
20503da6e6aSSanjay Patel }
20603da6e6aSSanjay Patel 
20703da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero2) {
20803da6e6aSSanjay Patel   parseAssembly(
20903da6e6aSSanjay Patel       "define float @test(float %a) {\n"
21003da6e6aSSanjay Patel       "  %1 = fcmp ogt float %a, -0.0\n"
21103da6e6aSSanjay Patel       "  %A = select i1 %1, float 0.0, float %a\n"
21203da6e6aSSanjay Patel       "  ret float %A\n"
21303da6e6aSSanjay Patel       "}\n");
214e7c94ef1SSanjay Patel   // The sign of zero doesn't matter in fcmp.
215e7c94ef1SSanjay Patel   expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false});
21603da6e6aSSanjay Patel }
21703da6e6aSSanjay Patel 
21803da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero3) {
21903da6e6aSSanjay Patel   parseAssembly(
22003da6e6aSSanjay Patel       "define float @test(float %a) {\n"
22103da6e6aSSanjay Patel       "  %1 = fcmp olt float 0.0, %a\n"
22203da6e6aSSanjay Patel       "  %A = select i1 %1, float -0.0, float %a\n"
22303da6e6aSSanjay Patel       "  ret float %A\n"
22403da6e6aSSanjay Patel       "}\n");
225e7c94ef1SSanjay Patel   // The sign of zero doesn't matter in fcmp.
226e7c94ef1SSanjay Patel   expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, true});
22703da6e6aSSanjay Patel }
22803da6e6aSSanjay Patel 
22903da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero4) {
23003da6e6aSSanjay Patel   parseAssembly(
23103da6e6aSSanjay Patel       "define float @test(float %a) {\n"
23203da6e6aSSanjay Patel       "  %1 = fcmp ogt float %a, 0.0\n"
23303da6e6aSSanjay Patel       "  %A = select i1 %1, float -0.0, float %a\n"
23403da6e6aSSanjay Patel       "  ret float %A\n"
23503da6e6aSSanjay Patel       "}\n");
236e7c94ef1SSanjay Patel   // The sign of zero doesn't matter in fcmp.
237e7c94ef1SSanjay Patel   expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false});
23803da6e6aSSanjay Patel }
23903da6e6aSSanjay Patel 
24003da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero5) {
24103da6e6aSSanjay Patel   parseAssembly(
24203da6e6aSSanjay Patel       "define float @test(float %a) {\n"
24303da6e6aSSanjay Patel       "  %1 = fcmp ogt float -0.0, %a\n"
24403da6e6aSSanjay Patel       "  %A = select i1 %1, float %a, float 0.0\n"
24503da6e6aSSanjay Patel       "  ret float %A\n"
24603da6e6aSSanjay Patel       "}\n");
247e7c94ef1SSanjay Patel   // The sign of zero doesn't matter in fcmp.
248e7c94ef1SSanjay Patel   expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, false});
24903da6e6aSSanjay Patel }
25003da6e6aSSanjay Patel 
25103da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero6) {
25203da6e6aSSanjay Patel   parseAssembly(
25303da6e6aSSanjay Patel       "define float @test(float %a) {\n"
25403da6e6aSSanjay Patel       "  %1 = fcmp olt float %a, -0.0\n"
25503da6e6aSSanjay Patel       "  %A = select i1 %1, float %a, float 0.0\n"
25603da6e6aSSanjay Patel       "  ret float %A\n"
25703da6e6aSSanjay Patel       "}\n");
258e7c94ef1SSanjay Patel   // The sign of zero doesn't matter in fcmp.
259e7c94ef1SSanjay Patel   expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true});
26003da6e6aSSanjay Patel }
26103da6e6aSSanjay Patel 
26203da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero7) {
26303da6e6aSSanjay Patel   parseAssembly(
26403da6e6aSSanjay Patel       "define float @test(float %a) {\n"
26503da6e6aSSanjay Patel       "  %1 = fcmp ogt float 0.0, %a\n"
26603da6e6aSSanjay Patel       "  %A = select i1 %1, float %a, float -0.0\n"
26703da6e6aSSanjay Patel       "  ret float %A\n"
26803da6e6aSSanjay Patel       "}\n");
269e7c94ef1SSanjay Patel   // The sign of zero doesn't matter in fcmp.
270e7c94ef1SSanjay Patel   expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, false});
27103da6e6aSSanjay Patel }
27203da6e6aSSanjay Patel 
27303da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero8) {
27403da6e6aSSanjay Patel   parseAssembly(
27503da6e6aSSanjay Patel       "define float @test(float %a) {\n"
27603da6e6aSSanjay Patel       "  %1 = fcmp olt float %a, 0.0\n"
27703da6e6aSSanjay Patel       "  %A = select i1 %1, float %a, float -0.0\n"
27803da6e6aSSanjay Patel       "  ret float %A\n"
27903da6e6aSSanjay Patel       "}\n");
280e7c94ef1SSanjay Patel   // The sign of zero doesn't matter in fcmp.
281e7c94ef1SSanjay Patel   expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true});
28203da6e6aSSanjay Patel }
28303da6e6aSSanjay Patel 
28403da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero1) {
28503da6e6aSSanjay Patel   parseAssembly(
28603da6e6aSSanjay Patel       "define float @test(float %a) {\n"
28703da6e6aSSanjay Patel       "  %1 = fcmp ogt float -0.0, %a\n"
28803da6e6aSSanjay Patel       "  %A = select i1 %1, float 0.0, float %a\n"
28903da6e6aSSanjay Patel       "  ret float %A\n"
29003da6e6aSSanjay Patel       "}\n");
291e7c94ef1SSanjay Patel   // The sign of zero doesn't matter in fcmp.
292e7c94ef1SSanjay Patel   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, true});
29303da6e6aSSanjay Patel }
29403da6e6aSSanjay Patel 
29503da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero2) {
29603da6e6aSSanjay Patel   parseAssembly(
29703da6e6aSSanjay Patel       "define float @test(float %a) {\n"
29803da6e6aSSanjay Patel       "  %1 = fcmp olt float %a, -0.0\n"
29903da6e6aSSanjay Patel       "  %A = select i1 %1, float 0.0, float %a\n"
30003da6e6aSSanjay Patel       "  ret float %A\n"
30103da6e6aSSanjay Patel       "}\n");
302e7c94ef1SSanjay Patel   // The sign of zero doesn't matter in fcmp.
303e7c94ef1SSanjay Patel   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, false});
30403da6e6aSSanjay Patel }
30503da6e6aSSanjay Patel 
30603da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero3) {
30703da6e6aSSanjay Patel   parseAssembly(
30803da6e6aSSanjay Patel       "define float @test(float %a) {\n"
30903da6e6aSSanjay Patel       "  %1 = fcmp ogt float 0.0, %a\n"
31003da6e6aSSanjay Patel       "  %A = select i1 %1, float -0.0, float %a\n"
31103da6e6aSSanjay Patel       "  ret float %A\n"
31203da6e6aSSanjay Patel       "}\n");
313e7c94ef1SSanjay Patel   // The sign of zero doesn't matter in fcmp.
314e7c94ef1SSanjay Patel   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, true});
31503da6e6aSSanjay Patel }
31603da6e6aSSanjay Patel 
31703da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero4) {
31803da6e6aSSanjay Patel   parseAssembly(
31903da6e6aSSanjay Patel       "define float @test(float %a) {\n"
32003da6e6aSSanjay Patel       "  %1 = fcmp olt float %a, 0.0\n"
32103da6e6aSSanjay Patel       "  %A = select i1 %1, float -0.0, float %a\n"
32203da6e6aSSanjay Patel       "  ret float %A\n"
32303da6e6aSSanjay Patel       "}\n");
324e7c94ef1SSanjay Patel   // The sign of zero doesn't matter in fcmp.
325e7c94ef1SSanjay Patel   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, false});
32603da6e6aSSanjay Patel }
32703da6e6aSSanjay Patel 
32803da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero5) {
32903da6e6aSSanjay Patel   parseAssembly(
33003da6e6aSSanjay Patel       "define float @test(float %a) {\n"
33103da6e6aSSanjay Patel       "  %1 = fcmp olt float -0.0, %a\n"
33203da6e6aSSanjay Patel       "  %A = select i1 %1, float %a, float 0.0\n"
33303da6e6aSSanjay Patel       "  ret float %A\n"
33403da6e6aSSanjay Patel       "}\n");
335e7c94ef1SSanjay Patel   // The sign of zero doesn't matter in fcmp.
336e7c94ef1SSanjay Patel   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, false});
33703da6e6aSSanjay Patel }
33803da6e6aSSanjay Patel 
33903da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero6) {
34003da6e6aSSanjay Patel   parseAssembly(
34103da6e6aSSanjay Patel       "define float @test(float %a) {\n"
34203da6e6aSSanjay Patel       "  %1 = fcmp ogt float %a, -0.0\n"
34303da6e6aSSanjay Patel       "  %A = select i1 %1, float %a, float 0.0\n"
34403da6e6aSSanjay Patel       "  ret float %A\n"
34503da6e6aSSanjay Patel       "}\n");
346e7c94ef1SSanjay Patel   // The sign of zero doesn't matter in fcmp.
347e7c94ef1SSanjay Patel   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true});
34803da6e6aSSanjay Patel }
34903da6e6aSSanjay Patel 
35003da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero7) {
35103da6e6aSSanjay Patel   parseAssembly(
35203da6e6aSSanjay Patel       "define float @test(float %a) {\n"
35303da6e6aSSanjay Patel       "  %1 = fcmp olt float 0.0, %a\n"
35403da6e6aSSanjay Patel       "  %A = select i1 %1, float %a, float -0.0\n"
35503da6e6aSSanjay Patel       "  ret float %A\n"
35603da6e6aSSanjay Patel       "}\n");
357e7c94ef1SSanjay Patel   // The sign of zero doesn't matter in fcmp.
358e7c94ef1SSanjay Patel   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, false});
35903da6e6aSSanjay Patel }
36003da6e6aSSanjay Patel 
36103da6e6aSSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero8) {
36203da6e6aSSanjay Patel   parseAssembly(
36303da6e6aSSanjay Patel       "define float @test(float %a) {\n"
36403da6e6aSSanjay Patel       "  %1 = fcmp ogt float %a, 0.0\n"
36503da6e6aSSanjay Patel       "  %A = select i1 %1, float %a, float -0.0\n"
36603da6e6aSSanjay Patel       "  ret float %A\n"
36703da6e6aSSanjay Patel       "}\n");
368e7c94ef1SSanjay Patel   // The sign of zero doesn't matter in fcmp.
369e7c94ef1SSanjay Patel   expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true});
370e7c94ef1SSanjay Patel }
371e7c94ef1SSanjay Patel 
372e7c94ef1SSanjay Patel TEST_F(MatchSelectPatternTest, FMinMismatchConstantZeroVecUndef) {
373e7c94ef1SSanjay Patel   parseAssembly(
374e7c94ef1SSanjay Patel       "define <2 x float> @test(<2 x float> %a) {\n"
375e7c94ef1SSanjay Patel       "  %1 = fcmp ogt <2 x float> %a, <float -0.0, float -0.0>\n"
376e7c94ef1SSanjay Patel       "  %A = select <2 x i1> %1, <2 x float> <float undef, float 0.0>, <2 x float> %a\n"
377e7c94ef1SSanjay Patel       "  ret <2 x float> %A\n"
378e7c94ef1SSanjay Patel       "}\n");
379e7c94ef1SSanjay Patel   // An undef in a vector constant can not be back-propagated for this analysis.
380e7c94ef1SSanjay Patel   expectPattern({SPF_UNKNOWN, SPNB_NA, false});
381e7c94ef1SSanjay Patel }
382e7c94ef1SSanjay Patel 
383e7c94ef1SSanjay Patel TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZeroVecUndef) {
384e7c94ef1SSanjay Patel   parseAssembly(
385e7c94ef1SSanjay Patel       "define <2 x float> @test(<2 x float> %a) {\n"
386e7c94ef1SSanjay Patel       "  %1 = fcmp ogt <2 x float> %a, zeroinitializer\n"
387e7c94ef1SSanjay Patel       "  %A = select <2 x i1> %1, <2 x float> %a, <2 x float> <float -0.0, float undef>\n"
388e7c94ef1SSanjay Patel       "  ret <2 x float> %A\n"
389e7c94ef1SSanjay Patel       "}\n");
390e7c94ef1SSanjay Patel   // An undef in a vector constant can not be back-propagated for this analysis.
39103da6e6aSSanjay Patel   expectPattern({SPF_UNKNOWN, SPNB_NA, false});
39203da6e6aSSanjay Patel }
39303da6e6aSSanjay Patel 
39430f1d691SThomas Lively TEST_F(MatchSelectPatternTest, VectorFMinimum) {
395d47b5c7bSThomas Lively   parseAssembly(
396d47b5c7bSThomas Lively       "define <4 x float> @test(<4 x float> %a) {\n"
397d47b5c7bSThomas Lively       "  %1 = fcmp ule <4 x float> %a, \n"
398d47b5c7bSThomas Lively       "    <float 5.0, float 5.0, float 5.0, float 5.0>\n"
399d47b5c7bSThomas Lively       "  %A = select <4 x i1> %1, <4 x float> %a,\n"
400d47b5c7bSThomas Lively       "     <4 x float> <float 5.0, float 5.0, float 5.0, float 5.0>\n"
401d47b5c7bSThomas Lively       "  ret <4 x float> %A\n"
402d47b5c7bSThomas Lively       "}\n");
403d47b5c7bSThomas Lively   // Check that pattern matching works on vectors where each lane has the same
404d47b5c7bSThomas Lively   // unordered pattern.
405d47b5c7bSThomas Lively   expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false});
406d47b5c7bSThomas Lively }
407d47b5c7bSThomas Lively 
408d47b5c7bSThomas Lively TEST_F(MatchSelectPatternTest, VectorFMinOtherOrdered) {
409d47b5c7bSThomas Lively   parseAssembly(
410d47b5c7bSThomas Lively       "define <4 x float> @test(<4 x float> %a) {\n"
411d47b5c7bSThomas Lively       "  %1 = fcmp ole <4 x float> %a, \n"
412d47b5c7bSThomas Lively       "    <float 5.0, float 5.0, float 5.0, float 5.0>\n"
413d47b5c7bSThomas Lively       "  %A = select <4 x i1> %1, <4 x float> %a,\n"
414d47b5c7bSThomas Lively       "     <4 x float> <float 5.0, float 5.0, float 5.0, float 5.0>\n"
415d47b5c7bSThomas Lively       "  ret <4 x float> %A\n"
416d47b5c7bSThomas Lively       "}\n");
417d47b5c7bSThomas Lively   // Check that pattern matching works on vectors where each lane has the same
418d47b5c7bSThomas Lively   // ordered pattern.
419d47b5c7bSThomas Lively   expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true});
420d47b5c7bSThomas Lively }
421d47b5c7bSThomas Lively 
42230f1d691SThomas Lively TEST_F(MatchSelectPatternTest, VectorNotFMinimum) {
423d47b5c7bSThomas Lively   parseAssembly(
424d47b5c7bSThomas Lively       "define <4 x float> @test(<4 x float> %a) {\n"
425d47b5c7bSThomas Lively       "  %1 = fcmp ule <4 x float> %a, \n"
426d47b5c7bSThomas Lively       "    <float 5.0, float 0x7ff8000000000000, float 5.0, float 5.0>\n"
427d47b5c7bSThomas Lively       "  %A = select <4 x i1> %1, <4 x float> %a,\n"
428d47b5c7bSThomas Lively       "     <4 x float> <float 5.0, float 0x7ff8000000000000, float 5.0, float "
429d47b5c7bSThomas Lively       "5.0>\n"
430d47b5c7bSThomas Lively       "  ret <4 x float> %A\n"
431d47b5c7bSThomas Lively       "}\n");
432d47b5c7bSThomas Lively   // The lane that contains a NaN (0x7ff80...) behaves like a
433d47b5c7bSThomas Lively   // non-NaN-propagating min and the other lines behave like a NaN-propagating
434d47b5c7bSThomas Lively   // min, so check that neither is returned.
435d47b5c7bSThomas Lively   expectPattern({SPF_UNKNOWN, SPNB_NA, false});
436d47b5c7bSThomas Lively }
437d47b5c7bSThomas Lively 
438d47b5c7bSThomas Lively TEST_F(MatchSelectPatternTest, VectorNotFMinZero) {
439d47b5c7bSThomas Lively   parseAssembly(
440d47b5c7bSThomas Lively       "define <4 x float> @test(<4 x float> %a) {\n"
441d47b5c7bSThomas Lively       "  %1 = fcmp ule <4 x float> %a, \n"
442d47b5c7bSThomas Lively       "    <float 5.0, float -0.0, float 5.0, float 5.0>\n"
443d47b5c7bSThomas Lively       "  %A = select <4 x i1> %1, <4 x float> %a,\n"
444d47b5c7bSThomas Lively       "     <4 x float> <float 5.0, float 0.0, float 5.0, float 5.0>\n"
445d47b5c7bSThomas Lively       "  ret <4 x float> %A\n"
446d47b5c7bSThomas Lively       "}\n");
447d47b5c7bSThomas Lively   // Always selects the second lane of %a if it is positive or negative zero, so
448d47b5c7bSThomas Lively   // this is stricter than a min.
449d47b5c7bSThomas Lively   expectPattern({SPF_UNKNOWN, SPNB_NA, false});
450d47b5c7bSThomas Lively }
451d47b5c7bSThomas Lively 
452569cea65SJames Molloy TEST_F(MatchSelectPatternTest, DoubleCastU) {
453569cea65SJames Molloy   parseAssembly(
454569cea65SJames Molloy       "define i32 @test(i8 %a, i8 %b) {\n"
455569cea65SJames Molloy       "  %1 = icmp ult i8 %a, %b\n"
456569cea65SJames Molloy       "  %2 = zext i8 %a to i32\n"
457569cea65SJames Molloy       "  %3 = zext i8 %b to i32\n"
458569cea65SJames Molloy       "  %A = select i1 %1, i32 %2, i32 %3\n"
459569cea65SJames Molloy       "  ret i32 %A\n"
460569cea65SJames Molloy       "}\n");
461569cea65SJames Molloy   // We should be able to look through the situation where we cast both operands
462569cea65SJames Molloy   // to the select.
463569cea65SJames Molloy   expectPattern({SPF_UMIN, SPNB_NA, false});
464569cea65SJames Molloy }
465569cea65SJames Molloy 
466569cea65SJames Molloy TEST_F(MatchSelectPatternTest, DoubleCastS) {
467569cea65SJames Molloy   parseAssembly(
468569cea65SJames Molloy       "define i32 @test(i8 %a, i8 %b) {\n"
469569cea65SJames Molloy       "  %1 = icmp slt i8 %a, %b\n"
470569cea65SJames Molloy       "  %2 = sext i8 %a to i32\n"
471569cea65SJames Molloy       "  %3 = sext i8 %b to i32\n"
472569cea65SJames Molloy       "  %A = select i1 %1, i32 %2, i32 %3\n"
473569cea65SJames Molloy       "  ret i32 %A\n"
474569cea65SJames Molloy       "}\n");
475569cea65SJames Molloy   // We should be able to look through the situation where we cast both operands
476569cea65SJames Molloy   // to the select.
477569cea65SJames Molloy   expectPattern({SPF_SMIN, SPNB_NA, false});
478569cea65SJames Molloy }
479569cea65SJames Molloy 
480569cea65SJames Molloy TEST_F(MatchSelectPatternTest, DoubleCastBad) {
481569cea65SJames Molloy   parseAssembly(
482569cea65SJames Molloy       "define i32 @test(i8 %a, i8 %b) {\n"
483569cea65SJames Molloy       "  %1 = icmp ult i8 %a, %b\n"
484569cea65SJames Molloy       "  %2 = zext i8 %a to i32\n"
485569cea65SJames Molloy       "  %3 = sext i8 %b to i32\n"
486569cea65SJames Molloy       "  %A = select i1 %1, i32 %2, i32 %3\n"
487569cea65SJames Molloy       "  ret i32 %A\n"
488569cea65SJames Molloy       "}\n");
489687a8448SJames Molloy   // The cast types here aren't the same, so we cannot match an UMIN.
490569cea65SJames Molloy   expectPattern({SPF_UNKNOWN, SPNB_NA, false});
491569cea65SJames Molloy }
4923bb2dbd6SSanjoy Das 
4936d343740SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotSMin) {
4946d343740SSanjay Patel   parseAssembly(
4956d343740SSanjay Patel       "define i8 @test(i8 %a, i8 %b) {\n"
4966d343740SSanjay Patel       "  %cmp = icmp sgt i8 %a, %b\n"
4976d343740SSanjay Patel       "  %an = xor i8 %a, -1\n"
4986d343740SSanjay Patel       "  %bn = xor i8 %b, -1\n"
4996d343740SSanjay Patel       "  %A = select i1 %cmp, i8 %an, i8 %bn\n"
5006d343740SSanjay Patel       "  ret i8 %A\n"
5016d343740SSanjay Patel       "}\n");
502867f0c3cSSanjay Patel   expectPattern({SPF_SMIN, SPNB_NA, false});
5036d343740SSanjay Patel }
5046d343740SSanjay Patel 
5056d343740SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotSMinSwap) {
5066d343740SSanjay Patel   parseAssembly(
507463143f0SSanjay Patel       "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n"
508463143f0SSanjay Patel       "  %cmp = icmp slt <2 x i8> %a, %b\n"
509463143f0SSanjay Patel       "  %an = xor <2 x i8> %a, <i8 -1, i8-1>\n"
510463143f0SSanjay Patel       "  %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n"
511463143f0SSanjay Patel       "  %A = select <2 x i1> %cmp, <2 x i8> %bn, <2 x i8> %an\n"
512463143f0SSanjay Patel       "  ret <2 x i8> %A\n"
5136d343740SSanjay Patel       "}\n");
514867f0c3cSSanjay Patel   expectPattern({SPF_SMIN, SPNB_NA, false});
5156d343740SSanjay Patel }
5166d343740SSanjay Patel 
5176d343740SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotSMax) {
5186d343740SSanjay Patel   parseAssembly(
5196d343740SSanjay Patel       "define i8 @test(i8 %a, i8 %b) {\n"
5206d343740SSanjay Patel       "  %cmp = icmp slt i8 %a, %b\n"
5216d343740SSanjay Patel       "  %an = xor i8 %a, -1\n"
5226d343740SSanjay Patel       "  %bn = xor i8 %b, -1\n"
5236d343740SSanjay Patel       "  %A = select i1 %cmp, i8 %an, i8 %bn\n"
5246d343740SSanjay Patel       "  ret i8 %A\n"
5256d343740SSanjay Patel       "}\n");
526867f0c3cSSanjay Patel   expectPattern({SPF_SMAX, SPNB_NA, false});
5276d343740SSanjay Patel }
5286d343740SSanjay Patel 
5296d343740SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotSMaxSwap) {
5306d343740SSanjay Patel   parseAssembly(
531463143f0SSanjay Patel       "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n"
532463143f0SSanjay Patel       "  %cmp = icmp sgt <2 x i8> %a, %b\n"
533463143f0SSanjay Patel       "  %an = xor <2 x i8> %a, <i8 -1, i8-1>\n"
534463143f0SSanjay Patel       "  %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n"
535463143f0SSanjay Patel       "  %A = select <2 x i1> %cmp, <2 x i8> %bn, <2 x i8> %an\n"
536463143f0SSanjay Patel       "  ret <2 x i8> %A\n"
537463143f0SSanjay Patel       "}\n");
538463143f0SSanjay Patel   expectPattern({SPF_SMAX, SPNB_NA, false});
539463143f0SSanjay Patel }
540463143f0SSanjay Patel 
541463143f0SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotUMin) {
542463143f0SSanjay Patel   parseAssembly(
543463143f0SSanjay Patel       "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n"
544463143f0SSanjay Patel       "  %cmp = icmp ugt <2 x i8> %a, %b\n"
545463143f0SSanjay Patel       "  %an = xor <2 x i8> %a, <i8 -1, i8-1>\n"
546463143f0SSanjay Patel       "  %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n"
547463143f0SSanjay Patel       "  %A = select <2 x i1> %cmp, <2 x i8> %an, <2 x i8> %bn\n"
548463143f0SSanjay Patel       "  ret <2 x i8> %A\n"
549463143f0SSanjay Patel       "}\n");
550fbb1b43fSSanjay Patel   expectPattern({SPF_UMIN, SPNB_NA, false});
551463143f0SSanjay Patel }
552463143f0SSanjay Patel 
553463143f0SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotUMinSwap) {
554463143f0SSanjay Patel   parseAssembly(
5556d343740SSanjay Patel       "define i8 @test(i8 %a, i8 %b) {\n"
556463143f0SSanjay Patel       "  %cmp = icmp ult i8 %a, %b\n"
5576d343740SSanjay Patel       "  %an = xor i8 %a, -1\n"
5586d343740SSanjay Patel       "  %bn = xor i8 %b, -1\n"
5596d343740SSanjay Patel       "  %A = select i1 %cmp, i8 %bn, i8 %an\n"
5606d343740SSanjay Patel       "  ret i8 %A\n"
5616d343740SSanjay Patel       "}\n");
562fbb1b43fSSanjay Patel   expectPattern({SPF_UMIN, SPNB_NA, false});
563463143f0SSanjay Patel }
564463143f0SSanjay Patel 
565463143f0SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotUMax) {
566463143f0SSanjay Patel   parseAssembly(
567463143f0SSanjay Patel       "define <2 x i8> @test(<2 x i8> %a, <2 x i8> %b) {\n"
568463143f0SSanjay Patel       "  %cmp = icmp ult <2 x i8> %a, %b\n"
569463143f0SSanjay Patel       "  %an = xor <2 x i8> %a, <i8 -1, i8-1>\n"
570463143f0SSanjay Patel       "  %bn = xor <2 x i8> %b, <i8 -1, i8-1>\n"
571463143f0SSanjay Patel       "  %A = select <2 x i1> %cmp, <2 x i8> %an, <2 x i8> %bn\n"
572463143f0SSanjay Patel       "  ret <2 x i8> %A\n"
573463143f0SSanjay Patel       "}\n");
574fbb1b43fSSanjay Patel   expectPattern({SPF_UMAX, SPNB_NA, false});
575463143f0SSanjay Patel }
576463143f0SSanjay Patel 
577463143f0SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotUMaxSwap) {
578463143f0SSanjay Patel   parseAssembly(
579463143f0SSanjay Patel       "define i8 @test(i8 %a, i8 %b) {\n"
580463143f0SSanjay Patel       "  %cmp = icmp ugt i8 %a, %b\n"
581463143f0SSanjay Patel       "  %an = xor i8 %a, -1\n"
582463143f0SSanjay Patel       "  %bn = xor i8 %b, -1\n"
583463143f0SSanjay Patel       "  %A = select i1 %cmp, i8 %bn, i8 %an\n"
584463143f0SSanjay Patel       "  ret i8 %A\n"
585463143f0SSanjay Patel       "}\n");
586fbb1b43fSSanjay Patel   expectPattern({SPF_UMAX, SPNB_NA, false});
587463143f0SSanjay Patel }
588463143f0SSanjay Patel 
589463143f0SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotEq) {
590463143f0SSanjay Patel   parseAssembly(
591463143f0SSanjay Patel       "define i8 @test(i8 %a, i8 %b) {\n"
592463143f0SSanjay Patel       "  %cmp = icmp eq i8 %a, %b\n"
593463143f0SSanjay Patel       "  %an = xor i8 %a, -1\n"
594463143f0SSanjay Patel       "  %bn = xor i8 %b, -1\n"
595463143f0SSanjay Patel       "  %A = select i1 %cmp, i8 %bn, i8 %an\n"
596463143f0SSanjay Patel       "  ret i8 %A\n"
597463143f0SSanjay Patel       "}\n");
598463143f0SSanjay Patel   expectPattern({SPF_UNKNOWN, SPNB_NA, false});
599463143f0SSanjay Patel }
600463143f0SSanjay Patel 
601463143f0SSanjay Patel TEST_F(MatchSelectPatternTest, NotNotNe) {
602463143f0SSanjay Patel   parseAssembly(
603463143f0SSanjay Patel       "define i8 @test(i8 %a, i8 %b) {\n"
604463143f0SSanjay Patel       "  %cmp = icmp ne i8 %a, %b\n"
605463143f0SSanjay Patel       "  %an = xor i8 %a, -1\n"
606463143f0SSanjay Patel       "  %bn = xor i8 %b, -1\n"
607463143f0SSanjay Patel       "  %A = select i1 %cmp, i8 %bn, i8 %an\n"
608463143f0SSanjay Patel       "  ret i8 %A\n"
609463143f0SSanjay Patel       "}\n");
610463143f0SSanjay Patel   expectPattern({SPF_UNKNOWN, SPNB_NA, false});
6116d343740SSanjay Patel }
6126d343740SSanjay Patel 
6133bb2dbd6SSanjoy Das TEST(ValueTracking, GuaranteedToTransferExecutionToSuccessor) {
6143bb2dbd6SSanjoy Das   StringRef Assembly =
6153bb2dbd6SSanjoy Das       "declare void @nounwind_readonly(i32*) nounwind readonly "
6163bb2dbd6SSanjoy Das       "declare void @nounwind_argmemonly(i32*) nounwind argmemonly "
6173bb2dbd6SSanjoy Das       "declare void @throws_but_readonly(i32*) readonly "
6183bb2dbd6SSanjoy Das       "declare void @throws_but_argmemonly(i32*) argmemonly "
6196ed459fdSJohannes Doerfert       "declare void @nounwind_willreturn(i32*) nounwind willreturn"
6203bb2dbd6SSanjoy Das       " "
6213bb2dbd6SSanjoy Das       "declare void @unknown(i32*) "
6223bb2dbd6SSanjoy Das       " "
6233bb2dbd6SSanjoy Das       "define void @f(i32* %p) { "
6243bb2dbd6SSanjoy Das       "  call void @nounwind_readonly(i32* %p) "
6253bb2dbd6SSanjoy Das       "  call void @nounwind_argmemonly(i32* %p) "
6263bb2dbd6SSanjoy Das       "  call void @throws_but_readonly(i32* %p) "
6273bb2dbd6SSanjoy Das       "  call void @throws_but_argmemonly(i32* %p) "
6283bb2dbd6SSanjoy Das       "  call void @unknown(i32* %p) nounwind readonly "
6293bb2dbd6SSanjoy Das       "  call void @unknown(i32* %p) nounwind argmemonly "
6303bb2dbd6SSanjoy Das       "  call void @unknown(i32* %p) readonly "
6313bb2dbd6SSanjoy Das       "  call void @unknown(i32* %p) argmemonly "
6326ed459fdSJohannes Doerfert       "  call void @nounwind_willreturn(i32* %p)"
6333bb2dbd6SSanjoy Das       "  ret void "
6343bb2dbd6SSanjoy Das       "} ";
6353bb2dbd6SSanjoy Das 
6363bb2dbd6SSanjoy Das   LLVMContext Context;
6373bb2dbd6SSanjoy Das   SMDiagnostic Error;
6383bb2dbd6SSanjoy Das   auto M = parseAssemblyString(Assembly, Error, Context);
6393bb2dbd6SSanjoy Das   assert(M && "Bad assembly?");
6403bb2dbd6SSanjoy Das 
6413bb2dbd6SSanjoy Das   auto *F = M->getFunction("f");
6423bb2dbd6SSanjoy Das   assert(F && "Bad assembly?");
6433bb2dbd6SSanjoy Das 
6443bb2dbd6SSanjoy Das   auto &BB = F->getEntryBlock();
6458580cd4eSCraig Topper   bool ExpectedAnswers[] = {
6463bb2dbd6SSanjoy Das       true,  // call void @nounwind_readonly(i32* %p)
6473bb2dbd6SSanjoy Das       true,  // call void @nounwind_argmemonly(i32* %p)
6483bb2dbd6SSanjoy Das       false, // call void @throws_but_readonly(i32* %p)
6493bb2dbd6SSanjoy Das       false, // call void @throws_but_argmemonly(i32* %p)
6503bb2dbd6SSanjoy Das       true,  // call void @unknown(i32* %p) nounwind readonly
6513bb2dbd6SSanjoy Das       true,  // call void @unknown(i32* %p) nounwind argmemonly
6523bb2dbd6SSanjoy Das       false, // call void @unknown(i32* %p) readonly
6533bb2dbd6SSanjoy Das       false, // call void @unknown(i32* %p) argmemonly
6546ed459fdSJohannes Doerfert       true,  // call void @nounwind_willreturn(i32* %p)
6553bb2dbd6SSanjoy Das       false, // ret void
6563bb2dbd6SSanjoy Das   };
6573bb2dbd6SSanjoy Das 
6583bb2dbd6SSanjoy Das   int Index = 0;
6593bb2dbd6SSanjoy Das   for (auto &I : BB) {
6603bb2dbd6SSanjoy Das     EXPECT_EQ(isGuaranteedToTransferExecutionToSuccessor(&I),
6613bb2dbd6SSanjoy Das               ExpectedAnswers[Index])
6623bb2dbd6SSanjoy Das         << "Incorrect answer at instruction " << Index << " = " << I;
6633bb2dbd6SSanjoy Das     Index++;
6643bb2dbd6SSanjoy Das   }
6653bb2dbd6SSanjoy Das }
66639a684d1SSanjoy Das 
667219e5367SNikita Popov TEST_F(ValueTrackingTest, ComputeNumSignBits_PR32045) {
668219e5367SNikita Popov   parseAssembly(
669219e5367SNikita Popov       "define i32 @test(i32 %a) {\n"
670219e5367SNikita Popov       "  %A = ashr i32 %a, -1\n"
671219e5367SNikita Popov       "  ret i32 %A\n"
672219e5367SNikita Popov       "}\n");
673219e5367SNikita Popov   EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u);
67439a684d1SSanjoy Das }
67570dbd5fbSSimon Dardis 
676a68096c7SSanjay Patel // No guarantees for canonical IR in this analysis, so this just bails out.
677219e5367SNikita Popov TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle) {
678219e5367SNikita Popov   parseAssembly(
679219e5367SNikita Popov       "define <2 x i32> @test() {\n"
680219e5367SNikita Popov       "  %A = shufflevector <2 x i32> undef, <2 x i32> undef, <2 x i32> <i32 0, i32 0>\n"
681219e5367SNikita Popov       "  ret <2 x i32> %A\n"
682219e5367SNikita Popov       "}\n");
683219e5367SNikita Popov   EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u);
684a68096c7SSanjay Patel }
685a68096c7SSanjay Patel 
68645a148deSSanjay Patel // No guarantees for canonical IR in this analysis, so a shuffle element that
68745a148deSSanjay Patel // references an undef value means this can't return any extra information.
688219e5367SNikita Popov TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle2) {
689219e5367SNikita Popov   parseAssembly(
690219e5367SNikita Popov       "define <2 x i32> @test(<2 x i1> %x) {\n"
691219e5367SNikita Popov       "  %sext = sext <2 x i1> %x to <2 x i32>\n"
692219e5367SNikita Popov       "  %A = shufflevector <2 x i32> %sext, <2 x i32> undef, <2 x i32> <i32 0, i32 2>\n"
693219e5367SNikita Popov       "  ret <2 x i32> %A\n"
694219e5367SNikita Popov       "}\n");
695219e5367SNikita Popov   EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u);
696994543abSJuneyoung Lee }
697994543abSJuneyoung Lee 
6988351f9b5SNikita Popov TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle_Pointers) {
6998351f9b5SNikita Popov   parseAssembly(
7008351f9b5SNikita Popov       "define <2 x i32*> @test(<2 x i32*> %x) {\n"
7018351f9b5SNikita Popov       "  %A = shufflevector <2 x i32*> zeroinitializer, <2 x i32*> undef, <2 x i32> zeroinitializer\n"
7028351f9b5SNikita Popov       "  ret <2 x i32*> %A\n"
7038351f9b5SNikita Popov       "}\n");
7048351f9b5SNikita Popov   EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 64u);
7058351f9b5SNikita Popov }
7068351f9b5SNikita Popov 
707e5f602d8SJuneyoung Lee TEST(ValueTracking, propagatesPoison) {
708e5f602d8SJuneyoung Lee   std::string AsmHead = "declare i32 @g(i32)\n"
709e5f602d8SJuneyoung Lee                         "define void @f(i32 %x, i32 %y, float %fx, float %fy, "
710e5f602d8SJuneyoung Lee                         "i1 %cond, i8* %p) {\n";
711e5f602d8SJuneyoung Lee   std::string AsmTail = "  ret void\n}";
712e5f602d8SJuneyoung Lee   // (propagates poison?, IR instruction)
713e5f602d8SJuneyoung Lee   SmallVector<std::pair<bool, std::string>, 32> Data = {
714e5f602d8SJuneyoung Lee       {true, "add i32 %x, %y"},
715e5f602d8SJuneyoung Lee       {true, "add nsw nuw i32 %x, %y"},
716e5f602d8SJuneyoung Lee       {true, "ashr i32 %x, %y"},
717e5f602d8SJuneyoung Lee       {true, "lshr exact i32 %x, 31"},
718e5f602d8SJuneyoung Lee       {true, "fcmp oeq float %fx, %fy"},
719e5f602d8SJuneyoung Lee       {true, "icmp eq i32 %x, %y"},
720e5f602d8SJuneyoung Lee       {true, "getelementptr i8, i8* %p, i32 %x"},
721e5f602d8SJuneyoung Lee       {true, "getelementptr inbounds i8, i8* %p, i32 %x"},
722e5f602d8SJuneyoung Lee       {true, "bitcast float %fx to i32"},
723e5f602d8SJuneyoung Lee       {false, "select i1 %cond, i32 %x, i32 %y"},
724e5f602d8SJuneyoung Lee       {false, "freeze i32 %x"},
725e5f602d8SJuneyoung Lee       {true, "udiv i32 %x, %y"},
726e5f602d8SJuneyoung Lee       {true, "urem i32 %x, %y"},
727e5f602d8SJuneyoung Lee       {true, "sdiv exact i32 %x, %y"},
728e5f602d8SJuneyoung Lee       {true, "srem i32 %x, %y"},
729e5f602d8SJuneyoung Lee       {false, "call i32 @g(i32 %x)"}};
730e5f602d8SJuneyoung Lee 
731e5f602d8SJuneyoung Lee   std::string AssemblyStr = AsmHead;
732e5f602d8SJuneyoung Lee   for (auto &Itm : Data)
733e5f602d8SJuneyoung Lee     AssemblyStr += Itm.second + "\n";
734e5f602d8SJuneyoung Lee   AssemblyStr += AsmTail;
735e5f602d8SJuneyoung Lee 
736e5f602d8SJuneyoung Lee   LLVMContext Context;
737e5f602d8SJuneyoung Lee   SMDiagnostic Error;
738e5f602d8SJuneyoung Lee   auto M = parseAssemblyString(AssemblyStr, Error, Context);
739e5f602d8SJuneyoung Lee   assert(M && "Bad assembly?");
740e5f602d8SJuneyoung Lee 
741e5f602d8SJuneyoung Lee   auto *F = M->getFunction("f");
742e5f602d8SJuneyoung Lee   assert(F && "Bad assembly?");
743e5f602d8SJuneyoung Lee 
744e5f602d8SJuneyoung Lee   auto &BB = F->getEntryBlock();
745e5f602d8SJuneyoung Lee 
746e5f602d8SJuneyoung Lee   int Index = 0;
747e5f602d8SJuneyoung Lee   for (auto &I : BB) {
748e5f602d8SJuneyoung Lee     if (isa<ReturnInst>(&I))
749e5f602d8SJuneyoung Lee       break;
75025ce1e04SJuneyoung Lee     EXPECT_EQ(propagatesPoison(cast<Operator>(&I)), Data[Index].first)
751e5f602d8SJuneyoung Lee         << "Incorrect answer at instruction " << Index << " = " << I;
752e5f602d8SJuneyoung Lee     Index++;
753e5f602d8SJuneyoung Lee   }
754e5f602d8SJuneyoung Lee }
755e5f602d8SJuneyoung Lee 
75625ce1e04SJuneyoung Lee TEST_F(ValueTrackingTest, programUndefinedIfPoison) {
75725ce1e04SJuneyoung Lee   parseAssembly("declare i32 @any_num()"
75825ce1e04SJuneyoung Lee                 "define void @test(i32 %mask) {\n"
75925ce1e04SJuneyoung Lee                 "  %A = call i32 @any_num()\n"
76025ce1e04SJuneyoung Lee                 "  %B = or i32 %A, %mask\n"
76125ce1e04SJuneyoung Lee                 "  udiv i32 1, %B"
76225ce1e04SJuneyoung Lee                 "  ret void\n"
76325ce1e04SJuneyoung Lee                 "}\n");
76425ce1e04SJuneyoung Lee   // If %A was poison, udiv raises UB regardless of %mask's value
76525ce1e04SJuneyoung Lee   EXPECT_EQ(programUndefinedIfPoison(A), true);
76625ce1e04SJuneyoung Lee }
76725ce1e04SJuneyoung Lee 
76825ce1e04SJuneyoung Lee TEST_F(ValueTrackingTest, programUndefinedIfUndefOrPoison) {
76925ce1e04SJuneyoung Lee   parseAssembly("declare i32 @any_num()"
77025ce1e04SJuneyoung Lee                 "define void @test(i32 %mask) {\n"
77125ce1e04SJuneyoung Lee                 "  %A = call i32 @any_num()\n"
77225ce1e04SJuneyoung Lee                 "  %B = or i32 %A, %mask\n"
77325ce1e04SJuneyoung Lee                 "  udiv i32 1, %B"
77425ce1e04SJuneyoung Lee                 "  ret void\n"
77525ce1e04SJuneyoung Lee                 "}\n");
77625ce1e04SJuneyoung Lee   // If %A was undef and %mask was 1, udiv does not raise UB
77725ce1e04SJuneyoung Lee   EXPECT_EQ(programUndefinedIfUndefOrPoison(A), false);
77825ce1e04SJuneyoung Lee }
77925ce1e04SJuneyoung Lee 
78025ce1e04SJuneyoung Lee TEST_F(ValueTrackingTest, isGuaranteedNotToBePoison_exploitBranchCond) {
78125ce1e04SJuneyoung Lee   parseAssembly("declare i1 @any_bool()"
78225ce1e04SJuneyoung Lee                 "define void @test(i1 %y) {\n"
78325ce1e04SJuneyoung Lee                 "  %A = call i1 @any_bool()\n"
78425ce1e04SJuneyoung Lee                 "  %cond = and i1 %A, %y\n"
78525ce1e04SJuneyoung Lee                 "  br i1 %cond, label %BB1, label %BB2\n"
78625ce1e04SJuneyoung Lee                 "BB1:\n"
78725ce1e04SJuneyoung Lee                 "  ret void\n"
78825ce1e04SJuneyoung Lee                 "BB2:\n"
78925ce1e04SJuneyoung Lee                 "  ret void\n"
79025ce1e04SJuneyoung Lee                 "}\n");
79125ce1e04SJuneyoung Lee   DominatorTree DT(*F);
79225ce1e04SJuneyoung Lee   for (auto &BB : *F) {
79325ce1e04SJuneyoung Lee     if (&BB == &F->getEntryBlock())
79425ce1e04SJuneyoung Lee       continue;
79525ce1e04SJuneyoung Lee 
7969b3c2a72SJuneyoung Lee     EXPECT_EQ(isGuaranteedNotToBePoison(A, nullptr, BB.getTerminator(), &DT),
7979b3c2a72SJuneyoung Lee               true)
79825ce1e04SJuneyoung Lee         << "isGuaranteedNotToBePoison does not hold at " << *BB.getTerminator();
79925ce1e04SJuneyoung Lee   }
80025ce1e04SJuneyoung Lee }
80125ce1e04SJuneyoung Lee 
802ba8911d5SJuneyoung Lee TEST_F(ValueTrackingTest, isGuaranteedNotToBePoison_phi) {
803ba8911d5SJuneyoung Lee   parseAssembly("declare i32 @any_i32(i32)"
804ba8911d5SJuneyoung Lee                 "define void @test() {\n"
805ba8911d5SJuneyoung Lee                 "ENTRY:\n"
806ba8911d5SJuneyoung Lee                 "  br label %LOOP\n"
807ba8911d5SJuneyoung Lee                 "LOOP:\n"
808ba8911d5SJuneyoung Lee                 "  %A = phi i32 [0, %ENTRY], [%A.next, %NEXT]\n"
809ba8911d5SJuneyoung Lee                 "  %A.next = call i32 @any_i32(i32 %A)\n"
810ba8911d5SJuneyoung Lee                 "  %cond = icmp eq i32 %A.next, 0\n"
811ba8911d5SJuneyoung Lee                 "  br i1 %cond, label %NEXT, label %EXIT\n"
812ba8911d5SJuneyoung Lee                 "NEXT:\n"
813ba8911d5SJuneyoung Lee                 "  br label %LOOP\n"
814ba8911d5SJuneyoung Lee                 "EXIT:\n"
815ba8911d5SJuneyoung Lee                 "  ret void\n"
816ba8911d5SJuneyoung Lee                 "}\n");
817ba8911d5SJuneyoung Lee   DominatorTree DT(*F);
818ba8911d5SJuneyoung Lee   for (auto &BB : *F) {
819ba8911d5SJuneyoung Lee     if (BB.getName() == "LOOP") {
8209b3c2a72SJuneyoung Lee       EXPECT_EQ(isGuaranteedNotToBePoison(A, nullptr, A, &DT), true)
821ba8911d5SJuneyoung Lee           << "isGuaranteedNotToBePoison does not hold";
822ba8911d5SJuneyoung Lee     }
823ba8911d5SJuneyoung Lee   }
824ba8911d5SJuneyoung Lee }
825ba8911d5SJuneyoung Lee 
8261c452200SJuneyoung Lee TEST_F(ValueTrackingTest, isGuaranteedNotToBeUndefOrPoison) {
8271c452200SJuneyoung Lee   parseAssembly("declare void @f(i32 noundef)"
8281c452200SJuneyoung Lee                 "define void @test(i32 %x) {\n"
8291c452200SJuneyoung Lee                 "  %A = bitcast i32 %x to i32\n"
8301c452200SJuneyoung Lee                 "  call void @f(i32 noundef %x)\n"
8311c452200SJuneyoung Lee                 "  ret void\n"
8321c452200SJuneyoung Lee                 "}\n");
8331c452200SJuneyoung Lee   EXPECT_EQ(isGuaranteedNotToBeUndefOrPoison(A), true);
8341c452200SJuneyoung Lee }
8351c452200SJuneyoung Lee 
8369b3c2a72SJuneyoung Lee TEST_F(ValueTrackingTest, isGuaranteedNotToBeUndefOrPoison_assume) {
8379b3c2a72SJuneyoung Lee   parseAssembly("declare i1 @f_i1()\n"
8389b3c2a72SJuneyoung Lee                 "declare i32 @f_i32()\n"
8399b3c2a72SJuneyoung Lee                 "declare void @llvm.assume(i1)\n"
8409b3c2a72SJuneyoung Lee                 "define void @test() {\n"
8419b3c2a72SJuneyoung Lee                 "  %A = call i32 @f_i32()\n"
8429b3c2a72SJuneyoung Lee                 "  %cond = call i1 @f_i1()\n"
8439b3c2a72SJuneyoung Lee                 "  %CxtI = add i32 0, 0\n"
8449b3c2a72SJuneyoung Lee                 "  br i1 %cond, label %BB1, label %EXIT\n"
8459b3c2a72SJuneyoung Lee                 "BB1:\n"
8469b3c2a72SJuneyoung Lee                 "  %CxtI2 = add i32 0, 0\n"
8479b3c2a72SJuneyoung Lee                 "  %cond2 = call i1 @f_i1()\n"
8489b3c2a72SJuneyoung Lee                 "  call void @llvm.assume(i1 true) [ \"noundef\"(i32 %A) ]\n"
8499b3c2a72SJuneyoung Lee                 "  br i1 %cond2, label %BB2, label %EXIT\n"
8509b3c2a72SJuneyoung Lee                 "BB2:\n"
8519b3c2a72SJuneyoung Lee                 "  %CxtI3 = add i32 0, 0\n"
8529b3c2a72SJuneyoung Lee                 "  ret void\n"
8539b3c2a72SJuneyoung Lee                 "EXIT:\n"
8549b3c2a72SJuneyoung Lee                 "  ret void\n"
8559b3c2a72SJuneyoung Lee                 "}");
8569b3c2a72SJuneyoung Lee   AssumptionCache AC(*F);
8579b3c2a72SJuneyoung Lee   DominatorTree DT(*F);
8589b3c2a72SJuneyoung Lee   EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(A, &AC, CxtI, &DT));
8599b3c2a72SJuneyoung Lee   EXPECT_FALSE(isGuaranteedNotToBeUndefOrPoison(A, &AC, CxtI2, &DT));
8609b3c2a72SJuneyoung Lee   EXPECT_TRUE(isGuaranteedNotToBeUndefOrPoison(A, &AC, CxtI3, &DT));
8619b3c2a72SJuneyoung Lee }
8629b3c2a72SJuneyoung Lee 
8630a6aee51SJuneyoung Lee TEST(ValueTracking, canCreatePoisonOrUndef) {
864994543abSJuneyoung Lee   std::string AsmHead =
865994543abSJuneyoung Lee       "declare i32 @g(i32)\n"
866994543abSJuneyoung Lee       "define void @f(i32 %x, i32 %y, float %fx, float %fy, i1 %cond, "
867994543abSJuneyoung Lee       "<4 x i32> %vx, <4 x i32> %vx2, <vscale x 4 x i32> %svx, i8* %p) {\n";
868994543abSJuneyoung Lee   std::string AsmTail = "  ret void\n}";
8690a6aee51SJuneyoung Lee   // (can create poison?, can create undef?, IR instruction)
870ef66e3d0SJuneyoung Lee   SmallVector<std::pair<std::pair<bool, bool>, std::string>, 32> Data = {
871ef66e3d0SJuneyoung Lee       {{false, false}, "add i32 %x, %y"},
872ef66e3d0SJuneyoung Lee       {{true, false}, "add nsw nuw i32 %x, %y"},
873ef66e3d0SJuneyoung Lee       {{true, false}, "shl i32 %x, %y"},
874ef66e3d0SJuneyoung Lee       {{true, false}, "shl <4 x i32> %vx, %vx2"},
875ef66e3d0SJuneyoung Lee       {{true, false}, "shl nsw i32 %x, %y"},
876ef66e3d0SJuneyoung Lee       {{true, false}, "shl nsw <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"},
877ef66e3d0SJuneyoung Lee       {{false, false}, "shl i32 %x, 31"},
878ef66e3d0SJuneyoung Lee       {{true, false}, "shl i32 %x, 32"},
879ef66e3d0SJuneyoung Lee       {{false, false}, "shl <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"},
880ef66e3d0SJuneyoung Lee       {{true, false}, "shl <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 32>"},
881ef66e3d0SJuneyoung Lee       {{true, false}, "ashr i32 %x, %y"},
882ef66e3d0SJuneyoung Lee       {{true, false}, "ashr exact i32 %x, %y"},
883ef66e3d0SJuneyoung Lee       {{false, false}, "ashr i32 %x, 31"},
884ef66e3d0SJuneyoung Lee       {{true, false}, "ashr exact i32 %x, 31"},
885ef66e3d0SJuneyoung Lee       {{false, false}, "ashr <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"},
886ef66e3d0SJuneyoung Lee       {{true, false}, "ashr <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 32>"},
887ef66e3d0SJuneyoung Lee       {{true, false}, "ashr exact <4 x i32> %vx, <i32 0, i32 1, i32 2, i32 3>"},
888ef66e3d0SJuneyoung Lee       {{true, false}, "lshr i32 %x, %y"},
889ef66e3d0SJuneyoung Lee       {{true, false}, "lshr exact i32 %x, 31"},
890ef66e3d0SJuneyoung Lee       {{false, false}, "udiv i32 %x, %y"},
891ef66e3d0SJuneyoung Lee       {{true, false}, "udiv exact i32 %x, %y"},
892ef66e3d0SJuneyoung Lee       {{false, false}, "getelementptr i8, i8* %p, i32 %x"},
893ef66e3d0SJuneyoung Lee       {{true, false}, "getelementptr inbounds i8, i8* %p, i32 %x"},
894ef66e3d0SJuneyoung Lee       {{true, false}, "fneg nnan float %fx"},
895ef66e3d0SJuneyoung Lee       {{false, false}, "fneg float %fx"},
896ef66e3d0SJuneyoung Lee       {{false, false}, "fadd float %fx, %fy"},
897ef66e3d0SJuneyoung Lee       {{true, false}, "fadd nnan float %fx, %fy"},
898ef66e3d0SJuneyoung Lee       {{false, false}, "urem i32 %x, %y"},
899ef66e3d0SJuneyoung Lee       {{true, false}, "fptoui float %fx to i32"},
900ef66e3d0SJuneyoung Lee       {{true, false}, "fptosi float %fx to i32"},
901ef66e3d0SJuneyoung Lee       {{false, false}, "bitcast float %fx to i32"},
902ef66e3d0SJuneyoung Lee       {{false, false}, "select i1 %cond, i32 %x, i32 %y"},
903ef66e3d0SJuneyoung Lee       {{true, false}, "select nnan i1 %cond, float %fx, float %fy"},
904ef66e3d0SJuneyoung Lee       {{true, false}, "extractelement <4 x i32> %vx, i32 %x"},
905ef66e3d0SJuneyoung Lee       {{false, false}, "extractelement <4 x i32> %vx, i32 3"},
906ef66e3d0SJuneyoung Lee       {{true, false}, "extractelement <vscale x 4 x i32> %svx, i32 4"},
907ef66e3d0SJuneyoung Lee       {{true, false}, "insertelement <4 x i32> %vx, i32 %x, i32 %y"},
908ef66e3d0SJuneyoung Lee       {{false, false}, "insertelement <4 x i32> %vx, i32 %x, i32 3"},
909ef66e3d0SJuneyoung Lee       {{true, false}, "insertelement <vscale x 4 x i32> %svx, i32 %x, i32 4"},
910ef66e3d0SJuneyoung Lee       {{false, false}, "freeze i32 %x"},
911ef66e3d0SJuneyoung Lee       {{false, false},
9120a6aee51SJuneyoung Lee        "shufflevector <4 x i32> %vx, <4 x i32> %vx2, "
9130a6aee51SJuneyoung Lee        "<4 x i32> <i32 0, i32 1, i32 2, i32 3>"},
914ef66e3d0SJuneyoung Lee       {{false, true},
9150a6aee51SJuneyoung Lee        "shufflevector <4 x i32> %vx, <4 x i32> %vx2, "
9160a6aee51SJuneyoung Lee        "<4 x i32> <i32 0, i32 1, i32 2, i32 undef>"},
917ef66e3d0SJuneyoung Lee       {{false, true},
9180a6aee51SJuneyoung Lee        "shufflevector <vscale x 4 x i32> %svx, "
9190a6aee51SJuneyoung Lee        "<vscale x 4 x i32> %svx, <vscale x 4 x i32> undef"},
920ef66e3d0SJuneyoung Lee       {{true, false}, "call i32 @g(i32 %x)"},
921ef66e3d0SJuneyoung Lee       {{false, false}, "call noundef i32 @g(i32 %x)"},
922ef66e3d0SJuneyoung Lee       {{true, false}, "fcmp nnan oeq float %fx, %fy"},
923ef66e3d0SJuneyoung Lee       {{false, false}, "fcmp oeq float %fx, %fy"}};
924994543abSJuneyoung Lee 
925994543abSJuneyoung Lee   std::string AssemblyStr = AsmHead;
926994543abSJuneyoung Lee   for (auto &Itm : Data)
927ef66e3d0SJuneyoung Lee     AssemblyStr += Itm.second + "\n";
928994543abSJuneyoung Lee   AssemblyStr += AsmTail;
929994543abSJuneyoung Lee 
930994543abSJuneyoung Lee   LLVMContext Context;
931994543abSJuneyoung Lee   SMDiagnostic Error;
932994543abSJuneyoung Lee   auto M = parseAssemblyString(AssemblyStr, Error, Context);
933994543abSJuneyoung Lee   assert(M && "Bad assembly?");
934994543abSJuneyoung Lee 
935994543abSJuneyoung Lee   auto *F = M->getFunction("f");
936994543abSJuneyoung Lee   assert(F && "Bad assembly?");
937994543abSJuneyoung Lee 
938994543abSJuneyoung Lee   auto &BB = F->getEntryBlock();
939994543abSJuneyoung Lee 
940994543abSJuneyoung Lee   int Index = 0;
941994543abSJuneyoung Lee   for (auto &I : BB) {
942994543abSJuneyoung Lee     if (isa<ReturnInst>(&I))
943994543abSJuneyoung Lee       break;
944ef66e3d0SJuneyoung Lee     bool Poison = Data[Index].first.first;
945ef66e3d0SJuneyoung Lee     bool Undef = Data[Index].first.second;
9460a6aee51SJuneyoung Lee     EXPECT_EQ(canCreatePoison(cast<Operator>(&I)), Poison)
9470a6aee51SJuneyoung Lee         << "Incorrect answer of canCreatePoison at instruction " << Index
9480a6aee51SJuneyoung Lee         << " = " << I;
9490a6aee51SJuneyoung Lee     EXPECT_EQ(canCreateUndefOrPoison(cast<Operator>(&I)), Undef || Poison)
9500a6aee51SJuneyoung Lee         << "Incorrect answer of canCreateUndef at instruction " << Index
9510a6aee51SJuneyoung Lee         << " = " << I;
952994543abSJuneyoung Lee     Index++;
953994543abSJuneyoung Lee   }
95445a148deSSanjay Patel }
95545a148deSSanjay Patel 
956565f4e72SAlex Richardson TEST_F(ValueTrackingTest, computePtrAlignment) {
957565f4e72SAlex Richardson   parseAssembly("declare i1 @f_i1()\n"
958565f4e72SAlex Richardson                 "declare i8* @f_i8p()\n"
959565f4e72SAlex Richardson                 "declare void @llvm.assume(i1)\n"
960565f4e72SAlex Richardson                 "define void @test() {\n"
961565f4e72SAlex Richardson                 "  %A = call i8* @f_i8p()\n"
962565f4e72SAlex Richardson                 "  %cond = call i1 @f_i1()\n"
963565f4e72SAlex Richardson                 "  %CxtI = add i32 0, 0\n"
964565f4e72SAlex Richardson                 "  br i1 %cond, label %BB1, label %EXIT\n"
965565f4e72SAlex Richardson                 "BB1:\n"
966565f4e72SAlex Richardson                 "  %CxtI2 = add i32 0, 0\n"
967565f4e72SAlex Richardson                 "  %cond2 = call i1 @f_i1()\n"
968565f4e72SAlex Richardson                 "  call void @llvm.assume(i1 true) [ \"align\"(i8* %A, i64 16) ]\n"
969565f4e72SAlex Richardson                 "  br i1 %cond2, label %BB2, label %EXIT\n"
970565f4e72SAlex Richardson                 "BB2:\n"
971565f4e72SAlex Richardson                 "  %CxtI3 = add i32 0, 0\n"
972565f4e72SAlex Richardson                 "  ret void\n"
973565f4e72SAlex Richardson                 "EXIT:\n"
974565f4e72SAlex Richardson                 "  ret void\n"
975565f4e72SAlex Richardson                 "}");
976565f4e72SAlex Richardson   AssumptionCache AC(*F);
977565f4e72SAlex Richardson   DominatorTree DT(*F);
978565f4e72SAlex Richardson   DataLayout DL = M->getDataLayout();
979565f4e72SAlex Richardson   EXPECT_EQ(getKnownAlignment(A, DL, CxtI, &AC, &DT), Align(1));
980565f4e72SAlex Richardson   EXPECT_EQ(getKnownAlignment(A, DL, CxtI2, &AC, &DT), Align(1));
981565f4e72SAlex Richardson   EXPECT_EQ(getKnownAlignment(A, DL, CxtI3, &AC, &DT), Align(16));
982565f4e72SAlex Richardson }
983565f4e72SAlex Richardson 
984219e5367SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownBits) {
985219e5367SNikita Popov   parseAssembly(
986219e5367SNikita Popov       "define i32 @test(i32 %a, i32 %b) {\n"
987219e5367SNikita Popov       "  %ash = mul i32 %a, 8\n"
988219e5367SNikita Popov       "  %aad = add i32 %ash, 7\n"
989219e5367SNikita Popov       "  %aan = and i32 %aad, 4095\n"
990219e5367SNikita Popov       "  %bsh = shl i32 %b, 4\n"
991219e5367SNikita Popov       "  %bad = or i32 %bsh, 6\n"
992219e5367SNikita Popov       "  %ban = and i32 %bad, 4095\n"
993219e5367SNikita Popov       "  %A = mul i32 %aan, %ban\n"
994219e5367SNikita Popov       "  ret i32 %A\n"
995219e5367SNikita Popov       "}\n");
996219e5367SNikita Popov   expectKnownBits(/*zero*/ 4278190085u, /*one*/ 10u);
99770dbd5fbSSimon Dardis }
99870dbd5fbSSimon Dardis 
999219e5367SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownMulBits) {
1000219e5367SNikita Popov   parseAssembly(
1001219e5367SNikita Popov       "define i32 @test(i32 %a, i32 %b) {\n"
1002219e5367SNikita Popov       "  %aa = shl i32 %a, 5\n"
1003219e5367SNikita Popov       "  %bb = shl i32 %b, 5\n"
1004219e5367SNikita Popov       "  %aaa = or i32 %aa, 24\n"
1005219e5367SNikita Popov       "  %bbb = or i32 %bb, 28\n"
1006219e5367SNikita Popov       "  %A = mul i32 %aaa, %bbb\n"
1007219e5367SNikita Popov       "  ret i32 %A\n"
1008219e5367SNikita Popov       "}\n");
1009219e5367SNikita Popov   expectKnownBits(/*zero*/ 95u, /*one*/ 32u);
101070dbd5fbSSimon Dardis }
1011687b92cdSNikita Popov 
1012eca40c36SJuneyoung Lee TEST_F(ValueTrackingTest, KnownNonZeroFromDomCond) {
1013eca40c36SJuneyoung Lee   parseAssembly(R"(
1014eca40c36SJuneyoung Lee     declare i8* @f_i8()
1015eca40c36SJuneyoung Lee     define void @test(i1 %c) {
1016eca40c36SJuneyoung Lee       %A = call i8* @f_i8()
1017eca40c36SJuneyoung Lee       %B = call i8* @f_i8()
1018eca40c36SJuneyoung Lee       %c1 = icmp ne i8* %A, null
1019eca40c36SJuneyoung Lee       %cond = and i1 %c1, %c
1020eca40c36SJuneyoung Lee       br i1 %cond, label %T, label %Q
1021eca40c36SJuneyoung Lee     T:
1022eca40c36SJuneyoung Lee       %CxtI = add i32 0, 0
1023eca40c36SJuneyoung Lee       ret void
1024eca40c36SJuneyoung Lee     Q:
1025eca40c36SJuneyoung Lee       %CxtI2 = add i32 0, 0
1026eca40c36SJuneyoung Lee       ret void
1027eca40c36SJuneyoung Lee     }
1028eca40c36SJuneyoung Lee   )");
1029eca40c36SJuneyoung Lee   AssumptionCache AC(*F);
1030eca40c36SJuneyoung Lee   DominatorTree DT(*F);
1031eca40c36SJuneyoung Lee   DataLayout DL = M->getDataLayout();
1032eca40c36SJuneyoung Lee   EXPECT_EQ(isKnownNonZero(A, DL, 0, &AC, CxtI, &DT), true);
1033eca40c36SJuneyoung Lee   EXPECT_EQ(isKnownNonZero(A, DL, 0, &AC, CxtI2, &DT), false);
1034eca40c36SJuneyoung Lee }
1035eca40c36SJuneyoung Lee 
1036*860199dfSJuneyoung Lee TEST_F(ValueTrackingTest, KnownNonZeroFromDomCond2) {
1037*860199dfSJuneyoung Lee   parseAssembly(R"(
1038*860199dfSJuneyoung Lee     declare i8* @f_i8()
1039*860199dfSJuneyoung Lee     define void @test(i1 %c) {
1040*860199dfSJuneyoung Lee       %A = call i8* @f_i8()
1041*860199dfSJuneyoung Lee       %B = call i8* @f_i8()
1042*860199dfSJuneyoung Lee       %c1 = icmp ne i8* %A, null
1043*860199dfSJuneyoung Lee       %cond = select i1 %c, i1 %c1, i1 false
1044*860199dfSJuneyoung Lee       br i1 %cond, label %T, label %Q
1045*860199dfSJuneyoung Lee     T:
1046*860199dfSJuneyoung Lee       %CxtI = add i32 0, 0
1047*860199dfSJuneyoung Lee       ret void
1048*860199dfSJuneyoung Lee     Q:
1049*860199dfSJuneyoung Lee       %CxtI2 = add i32 0, 0
1050*860199dfSJuneyoung Lee       ret void
1051*860199dfSJuneyoung Lee     }
1052*860199dfSJuneyoung Lee   )");
1053*860199dfSJuneyoung Lee   AssumptionCache AC(*F);
1054*860199dfSJuneyoung Lee   DominatorTree DT(*F);
1055*860199dfSJuneyoung Lee   DataLayout DL = M->getDataLayout();
1056*860199dfSJuneyoung Lee   EXPECT_EQ(isKnownNonZero(A, DL, 0, &AC, CxtI, &DT), true);
1057*860199dfSJuneyoung Lee   EXPECT_EQ(isKnownNonZero(A, DL, 0, &AC, CxtI2, &DT), false);
1058*860199dfSJuneyoung Lee }
1059*860199dfSJuneyoung Lee 
1060eca40c36SJuneyoung Lee TEST_F(ValueTrackingTest, IsImpliedConditionAnd) {
1061eca40c36SJuneyoung Lee   parseAssembly(R"(
1062eca40c36SJuneyoung Lee     define void @test(i32 %x, i32 %y) {
1063eca40c36SJuneyoung Lee       %c1 = icmp ult i32 %x, 10
1064eca40c36SJuneyoung Lee       %c2 = icmp ult i32 %y, 15
1065eca40c36SJuneyoung Lee       %A = and i1 %c1, %c2
1066eca40c36SJuneyoung Lee       ; x < 10 /\ y < 15
1067eca40c36SJuneyoung Lee       %A2 = icmp ult i32 %x, 20
1068eca40c36SJuneyoung Lee       %A3 = icmp uge i32 %y, 20
1069eca40c36SJuneyoung Lee       %A4 = icmp ult i32 %x, 5
1070eca40c36SJuneyoung Lee       ret void
1071eca40c36SJuneyoung Lee     }
1072eca40c36SJuneyoung Lee   )");
1073eca40c36SJuneyoung Lee   DataLayout DL = M->getDataLayout();
1074eca40c36SJuneyoung Lee   EXPECT_EQ(isImpliedCondition(A, A2, DL), true);
1075eca40c36SJuneyoung Lee   EXPECT_EQ(isImpliedCondition(A, A3, DL), false);
1076eca40c36SJuneyoung Lee   EXPECT_EQ(isImpliedCondition(A, A4, DL), None);
1077eca40c36SJuneyoung Lee }
1078eca40c36SJuneyoung Lee 
1079*860199dfSJuneyoung Lee TEST_F(ValueTrackingTest, IsImpliedConditionAnd2) {
1080*860199dfSJuneyoung Lee   parseAssembly(R"(
1081*860199dfSJuneyoung Lee     define void @test(i32 %x, i32 %y) {
1082*860199dfSJuneyoung Lee       %c1 = icmp ult i32 %x, 10
1083*860199dfSJuneyoung Lee       %c2 = icmp ult i32 %y, 15
1084*860199dfSJuneyoung Lee       %A = select i1 %c1, i1 %c2, i1 false
1085*860199dfSJuneyoung Lee       ; x < 10 /\ y < 15
1086*860199dfSJuneyoung Lee       %A2 = icmp ult i32 %x, 20
1087*860199dfSJuneyoung Lee       %A3 = icmp uge i32 %y, 20
1088*860199dfSJuneyoung Lee       %A4 = icmp ult i32 %x, 5
1089*860199dfSJuneyoung Lee       ret void
1090*860199dfSJuneyoung Lee     }
1091*860199dfSJuneyoung Lee   )");
1092*860199dfSJuneyoung Lee   DataLayout DL = M->getDataLayout();
1093*860199dfSJuneyoung Lee   EXPECT_EQ(isImpliedCondition(A, A2, DL), true);
1094*860199dfSJuneyoung Lee   EXPECT_EQ(isImpliedCondition(A, A3, DL), false);
1095*860199dfSJuneyoung Lee   EXPECT_EQ(isImpliedCondition(A, A4, DL), None);
1096*860199dfSJuneyoung Lee }
1097*860199dfSJuneyoung Lee 
1098eca40c36SJuneyoung Lee TEST_F(ValueTrackingTest, IsImpliedConditionOr) {
1099eca40c36SJuneyoung Lee   parseAssembly(R"(
1100eca40c36SJuneyoung Lee     define void @test(i32 %x, i32 %y) {
1101eca40c36SJuneyoung Lee       %c1 = icmp ult i32 %x, 10
1102eca40c36SJuneyoung Lee       %c2 = icmp ult i32 %y, 15
1103eca40c36SJuneyoung Lee       %A = or i1 %c1, %c2 ; negated
1104eca40c36SJuneyoung Lee       ; x >= 10 /\ y >= 15
1105eca40c36SJuneyoung Lee       %A2 = icmp ult i32 %x, 5
1106eca40c36SJuneyoung Lee       %A3 = icmp uge i32 %y, 10
1107eca40c36SJuneyoung Lee       %A4 = icmp ult i32 %x, 15
1108eca40c36SJuneyoung Lee       ret void
1109eca40c36SJuneyoung Lee     }
1110eca40c36SJuneyoung Lee   )");
1111eca40c36SJuneyoung Lee   DataLayout DL = M->getDataLayout();
1112eca40c36SJuneyoung Lee   EXPECT_EQ(isImpliedCondition(A, A2, DL, false), false);
1113eca40c36SJuneyoung Lee   EXPECT_EQ(isImpliedCondition(A, A3, DL, false), true);
1114eca40c36SJuneyoung Lee   EXPECT_EQ(isImpliedCondition(A, A4, DL, false), None);
1115eca40c36SJuneyoung Lee }
1116eca40c36SJuneyoung Lee 
1117*860199dfSJuneyoung Lee TEST_F(ValueTrackingTest, IsImpliedConditionOr2) {
1118*860199dfSJuneyoung Lee   parseAssembly(R"(
1119*860199dfSJuneyoung Lee     define void @test(i32 %x, i32 %y) {
1120*860199dfSJuneyoung Lee       %c1 = icmp ult i32 %x, 10
1121*860199dfSJuneyoung Lee       %c2 = icmp ult i32 %y, 15
1122*860199dfSJuneyoung Lee       %A = select i1 %c1, i1 true, i1 %c2 ; negated
1123*860199dfSJuneyoung Lee       ; x >= 10 /\ y >= 15
1124*860199dfSJuneyoung Lee       %A2 = icmp ult i32 %x, 5
1125*860199dfSJuneyoung Lee       %A3 = icmp uge i32 %y, 10
1126*860199dfSJuneyoung Lee       %A4 = icmp ult i32 %x, 15
1127*860199dfSJuneyoung Lee       ret void
1128*860199dfSJuneyoung Lee     }
1129*860199dfSJuneyoung Lee   )");
1130*860199dfSJuneyoung Lee   DataLayout DL = M->getDataLayout();
1131*860199dfSJuneyoung Lee   EXPECT_EQ(isImpliedCondition(A, A2, DL, false), false);
1132*860199dfSJuneyoung Lee   EXPECT_EQ(isImpliedCondition(A, A3, DL, false), true);
1133*860199dfSJuneyoung Lee   EXPECT_EQ(isImpliedCondition(A, A4, DL, false), None);
1134*860199dfSJuneyoung Lee }
1135*860199dfSJuneyoung Lee 
11367cf0f856SNikita Popov TEST_F(ComputeKnownBitsTest, KnownNonZeroShift) {
11377cf0f856SNikita Popov   // %q is known nonzero without known bits.
11387cf0f856SNikita Popov   // Because %q is nonzero, %A[0] is known to be zero.
11397cf0f856SNikita Popov   parseAssembly(
11407cf0f856SNikita Popov       "define i8 @test(i8 %p, i8* %pq) {\n"
11417cf0f856SNikita Popov       "  %q = load i8, i8* %pq, !range !0\n"
11427cf0f856SNikita Popov       "  %A = shl i8 %p, %q\n"
11437cf0f856SNikita Popov       "  ret i8 %A\n"
11447cf0f856SNikita Popov       "}\n"
11457cf0f856SNikita Popov       "!0 = !{ i8 1, i8 5 }\n");
11467cf0f856SNikita Popov   expectKnownBits(/*zero*/ 1u, /*one*/ 0u);
11477cf0f856SNikita Popov }
11487cf0f856SNikita Popov 
1149687b92cdSNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownFshl) {
1150687b92cdSNikita Popov   // fshl(....1111....0000, 00..1111........, 6)
1151687b92cdSNikita Popov   // = 11....000000..11
1152687b92cdSNikita Popov   parseAssembly(
1153687b92cdSNikita Popov       "define i16 @test(i16 %a, i16 %b) {\n"
1154687b92cdSNikita Popov       "  %aa = shl i16 %a, 4\n"
1155687b92cdSNikita Popov       "  %bb = lshr i16 %b, 2\n"
1156687b92cdSNikita Popov       "  %aaa = or i16 %aa, 3840\n"
1157687b92cdSNikita Popov       "  %bbb = or i16 %bb, 3840\n"
1158687b92cdSNikita Popov       "  %A = call i16 @llvm.fshl.i16(i16 %aaa, i16 %bbb, i16 6)\n"
1159687b92cdSNikita Popov       "  ret i16 %A\n"
1160687b92cdSNikita Popov       "}\n"
1161687b92cdSNikita Popov       "declare i16 @llvm.fshl.i16(i16, i16, i16)\n");
1162687b92cdSNikita Popov   expectKnownBits(/*zero*/ 1008u, /*one*/ 49155u);
1163687b92cdSNikita Popov }
1164687b92cdSNikita Popov 
1165687b92cdSNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownFshr) {
1166687b92cdSNikita Popov   // fshr(....1111....0000, 00..1111........, 26)
1167687b92cdSNikita Popov   // = 11....000000..11
1168687b92cdSNikita Popov   parseAssembly(
1169687b92cdSNikita Popov       "define i16 @test(i16 %a, i16 %b) {\n"
1170687b92cdSNikita Popov       "  %aa = shl i16 %a, 4\n"
1171687b92cdSNikita Popov       "  %bb = lshr i16 %b, 2\n"
1172687b92cdSNikita Popov       "  %aaa = or i16 %aa, 3840\n"
1173687b92cdSNikita Popov       "  %bbb = or i16 %bb, 3840\n"
1174687b92cdSNikita Popov       "  %A = call i16 @llvm.fshr.i16(i16 %aaa, i16 %bbb, i16 26)\n"
1175687b92cdSNikita Popov       "  ret i16 %A\n"
1176687b92cdSNikita Popov       "}\n"
1177687b92cdSNikita Popov       "declare i16 @llvm.fshr.i16(i16, i16, i16)\n");
1178687b92cdSNikita Popov   expectKnownBits(/*zero*/ 1008u, /*one*/ 49155u);
1179687b92cdSNikita Popov }
1180687b92cdSNikita Popov 
1181687b92cdSNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownFshlZero) {
1182687b92cdSNikita Popov   // fshl(....1111....0000, 00..1111........, 0)
1183687b92cdSNikita Popov   // = ....1111....0000
1184687b92cdSNikita Popov   parseAssembly(
1185687b92cdSNikita Popov       "define i16 @test(i16 %a, i16 %b) {\n"
1186687b92cdSNikita Popov       "  %aa = shl i16 %a, 4\n"
1187687b92cdSNikita Popov       "  %bb = lshr i16 %b, 2\n"
1188687b92cdSNikita Popov       "  %aaa = or i16 %aa, 3840\n"
1189687b92cdSNikita Popov       "  %bbb = or i16 %bb, 3840\n"
1190687b92cdSNikita Popov       "  %A = call i16 @llvm.fshl.i16(i16 %aaa, i16 %bbb, i16 0)\n"
1191687b92cdSNikita Popov       "  ret i16 %A\n"
1192687b92cdSNikita Popov       "}\n"
1193687b92cdSNikita Popov       "declare i16 @llvm.fshl.i16(i16, i16, i16)\n");
1194687b92cdSNikita Popov   expectKnownBits(/*zero*/ 15u, /*one*/ 3840u);
1195687b92cdSNikita Popov }
1196ed3ca927SNikita Popov 
1197ed3ca927SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownUAddSatLeadingOnes) {
1198ed3ca927SNikita Popov   // uadd.sat(1111...1, ........)
1199ed3ca927SNikita Popov   // = 1111....
1200ed3ca927SNikita Popov   parseAssembly(
1201ed3ca927SNikita Popov       "define i8 @test(i8 %a, i8 %b) {\n"
1202ed3ca927SNikita Popov       "  %aa = or i8 %a, 241\n"
1203ed3ca927SNikita Popov       "  %A = call i8 @llvm.uadd.sat.i8(i8 %aa, i8 %b)\n"
1204ed3ca927SNikita Popov       "  ret i8 %A\n"
1205ed3ca927SNikita Popov       "}\n"
1206ed3ca927SNikita Popov       "declare i8 @llvm.uadd.sat.i8(i8, i8)\n");
1207ed3ca927SNikita Popov   expectKnownBits(/*zero*/ 0u, /*one*/ 240u);
1208ed3ca927SNikita Popov }
1209ed3ca927SNikita Popov 
1210ed3ca927SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownUAddSatOnesPreserved) {
1211ed3ca927SNikita Popov   // uadd.sat(00...011, .1...110)
1212ed3ca927SNikita Popov   // = .......1
1213ed3ca927SNikita Popov   parseAssembly(
1214ed3ca927SNikita Popov       "define i8 @test(i8 %a, i8 %b) {\n"
1215ed3ca927SNikita Popov       "  %aa = or i8 %a, 3\n"
1216ed3ca927SNikita Popov       "  %aaa = and i8 %aa, 59\n"
1217ed3ca927SNikita Popov       "  %bb = or i8 %b, 70\n"
1218ed3ca927SNikita Popov       "  %bbb = and i8 %bb, 254\n"
1219ed3ca927SNikita Popov       "  %A = call i8 @llvm.uadd.sat.i8(i8 %aaa, i8 %bbb)\n"
1220ed3ca927SNikita Popov       "  ret i8 %A\n"
1221ed3ca927SNikita Popov       "}\n"
1222ed3ca927SNikita Popov       "declare i8 @llvm.uadd.sat.i8(i8, i8)\n");
1223ed3ca927SNikita Popov   expectKnownBits(/*zero*/ 0u, /*one*/ 1u);
1224ed3ca927SNikita Popov }
1225ed3ca927SNikita Popov 
1226ed3ca927SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatLHSLeadingZeros) {
1227ed3ca927SNikita Popov   // usub.sat(0000...0, ........)
1228ed3ca927SNikita Popov   // = 0000....
1229ed3ca927SNikita Popov   parseAssembly(
1230ed3ca927SNikita Popov       "define i8 @test(i8 %a, i8 %b) {\n"
1231ed3ca927SNikita Popov       "  %aa = and i8 %a, 14\n"
1232ed3ca927SNikita Popov       "  %A = call i8 @llvm.usub.sat.i8(i8 %aa, i8 %b)\n"
1233ed3ca927SNikita Popov       "  ret i8 %A\n"
1234ed3ca927SNikita Popov       "}\n"
1235ed3ca927SNikita Popov       "declare i8 @llvm.usub.sat.i8(i8, i8)\n");
1236ed3ca927SNikita Popov   expectKnownBits(/*zero*/ 240u, /*one*/ 0u);
1237ed3ca927SNikita Popov }
1238ed3ca927SNikita Popov 
1239ed3ca927SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatRHSLeadingOnes) {
1240ed3ca927SNikita Popov   // usub.sat(........, 1111...1)
1241ed3ca927SNikita Popov   // = 0000....
1242ed3ca927SNikita Popov   parseAssembly(
1243ed3ca927SNikita Popov       "define i8 @test(i8 %a, i8 %b) {\n"
1244ed3ca927SNikita Popov       "  %bb = or i8 %a, 241\n"
1245ed3ca927SNikita Popov       "  %A = call i8 @llvm.usub.sat.i8(i8 %a, i8 %bb)\n"
1246ed3ca927SNikita Popov       "  ret i8 %A\n"
1247ed3ca927SNikita Popov       "}\n"
1248ed3ca927SNikita Popov       "declare i8 @llvm.usub.sat.i8(i8, i8)\n");
1249ed3ca927SNikita Popov   expectKnownBits(/*zero*/ 240u, /*one*/ 0u);
1250ed3ca927SNikita Popov }
1251ed3ca927SNikita Popov 
1252ed3ca927SNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownUSubSatZerosPreserved) {
1253ed3ca927SNikita Popov   // usub.sat(11...011, .1...110)
1254ed3ca927SNikita Popov   // = ......0.
1255ed3ca927SNikita Popov   parseAssembly(
1256ed3ca927SNikita Popov       "define i8 @test(i8 %a, i8 %b) {\n"
1257ed3ca927SNikita Popov       "  %aa = or i8 %a, 195\n"
1258ed3ca927SNikita Popov       "  %aaa = and i8 %aa, 251\n"
1259ed3ca927SNikita Popov       "  %bb = or i8 %b, 70\n"
1260ed3ca927SNikita Popov       "  %bbb = and i8 %bb, 254\n"
1261ed3ca927SNikita Popov       "  %A = call i8 @llvm.usub.sat.i8(i8 %aaa, i8 %bbb)\n"
1262ed3ca927SNikita Popov       "  ret i8 %A\n"
1263ed3ca927SNikita Popov       "}\n"
1264ed3ca927SNikita Popov       "declare i8 @llvm.usub.sat.i8(i8, i8)\n");
1265ed3ca927SNikita Popov   expectKnownBits(/*zero*/ 2u, /*one*/ 0u);
1266ed3ca927SNikita Popov }
1267329d15b5SVitaly Buka 
1268d86fff6aSNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownBitsPtrToIntTrunc) {
1269d86fff6aSNikita Popov   // ptrtoint truncates the pointer type.
1270d86fff6aSNikita Popov   parseAssembly(
1271d86fff6aSNikita Popov       "define void @test(i8** %p) {\n"
1272d86fff6aSNikita Popov       "  %A = load i8*, i8** %p\n"
1273d86fff6aSNikita Popov       "  %i = ptrtoint i8* %A to i32\n"
1274d86fff6aSNikita Popov       "  %m = and i32 %i, 31\n"
1275d86fff6aSNikita Popov       "  %c = icmp eq i32 %m, 0\n"
1276d86fff6aSNikita Popov       "  call void @llvm.assume(i1 %c)\n"
1277d86fff6aSNikita Popov       "  ret void\n"
1278d86fff6aSNikita Popov       "}\n"
1279d86fff6aSNikita Popov       "declare void @llvm.assume(i1)\n");
1280d86fff6aSNikita Popov   AssumptionCache AC(*F);
1281d86fff6aSNikita Popov   KnownBits Known = computeKnownBits(
1282d86fff6aSNikita Popov       A, M->getDataLayout(), /* Depth */ 0, &AC, F->front().getTerminator());
1283d86fff6aSNikita Popov   EXPECT_EQ(Known.Zero.getZExtValue(), 31u);
1284d86fff6aSNikita Popov   EXPECT_EQ(Known.One.getZExtValue(), 0u);
1285d86fff6aSNikita Popov }
1286d86fff6aSNikita Popov 
1287d86fff6aSNikita Popov TEST_F(ComputeKnownBitsTest, ComputeKnownBitsPtrToIntZext) {
1288d86fff6aSNikita Popov   // ptrtoint zero extends the pointer type.
1289d86fff6aSNikita Popov   parseAssembly(
1290d86fff6aSNikita Popov       "define void @test(i8** %p) {\n"
1291d86fff6aSNikita Popov       "  %A = load i8*, i8** %p\n"
1292d86fff6aSNikita Popov       "  %i = ptrtoint i8* %A to i128\n"
1293d86fff6aSNikita Popov       "  %m = and i128 %i, 31\n"
1294d86fff6aSNikita Popov       "  %c = icmp eq i128 %m, 0\n"
1295d86fff6aSNikita Popov       "  call void @llvm.assume(i1 %c)\n"
1296d86fff6aSNikita Popov       "  ret void\n"
1297d86fff6aSNikita Popov       "}\n"
1298d86fff6aSNikita Popov       "declare void @llvm.assume(i1)\n");
1299d86fff6aSNikita Popov   AssumptionCache AC(*F);
1300d86fff6aSNikita Popov   KnownBits Known = computeKnownBits(
1301d86fff6aSNikita Popov       A, M->getDataLayout(), /* Depth */ 0, &AC, F->front().getTerminator());
1302d86fff6aSNikita Popov   EXPECT_EQ(Known.Zero.getZExtValue(), 31u);
1303d86fff6aSNikita Popov   EXPECT_EQ(Known.One.getZExtValue(), 0u);
1304d86fff6aSNikita Popov }
1305d86fff6aSNikita Popov 
1306a6183d0fSJuneyoung Lee TEST_F(ComputeKnownBitsTest, ComputeKnownBitsFreeze) {
1307a6183d0fSJuneyoung Lee   parseAssembly("define void @test() {\n"
1308a6183d0fSJuneyoung Lee                 "  %m = call i32 @any_num()\n"
1309a6183d0fSJuneyoung Lee                 "  %A = freeze i32 %m\n"
1310a6183d0fSJuneyoung Lee                 "  %n = and i32 %m, 31\n"
1311a6183d0fSJuneyoung Lee                 "  %c = icmp eq i32 %n, 0\n"
1312a6183d0fSJuneyoung Lee                 "  call void @llvm.assume(i1 %c)\n"
1313a6183d0fSJuneyoung Lee                 "  ret void\n"
1314a6183d0fSJuneyoung Lee                 "}\n"
1315a6183d0fSJuneyoung Lee                 "declare void @llvm.assume(i1)\n"
1316a6183d0fSJuneyoung Lee                 "declare i32 @any_num()\n");
1317a6183d0fSJuneyoung Lee   AssumptionCache AC(*F);
1318a6183d0fSJuneyoung Lee   KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC,
1319a6183d0fSJuneyoung Lee                                      F->front().getTerminator());
1320a6183d0fSJuneyoung Lee   EXPECT_EQ(Known.Zero.getZExtValue(), 31u);
1321a6183d0fSJuneyoung Lee   EXPECT_EQ(Known.One.getZExtValue(), 0u);
1322a6183d0fSJuneyoung Lee }
1323a6183d0fSJuneyoung Lee 
1324f1f31eb2SQuentin Colombet TEST_F(ComputeKnownBitsTest, ComputeKnownBitsAddWithRange) {
1325f1f31eb2SQuentin Colombet   parseAssembly("define void @test(i64* %p) {\n"
1326f1f31eb2SQuentin Colombet                 "  %A = load i64, i64* %p, !range !{i64 64, i64 65536}\n"
1327f1f31eb2SQuentin Colombet                 "  %APlus512 = add i64 %A, 512\n"
1328f1f31eb2SQuentin Colombet                 "  %c = icmp ugt i64 %APlus512, 523\n"
1329f1f31eb2SQuentin Colombet                 "  call void @llvm.assume(i1 %c)\n"
1330f1f31eb2SQuentin Colombet                 "  ret void\n"
1331f1f31eb2SQuentin Colombet                 "}\n"
1332f1f31eb2SQuentin Colombet                 "declare void @llvm.assume(i1)\n");
1333f1f31eb2SQuentin Colombet   AssumptionCache AC(*F);
1334f1f31eb2SQuentin Colombet   KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC,
1335f1f31eb2SQuentin Colombet                                      F->front().getTerminator());
1336f1f31eb2SQuentin Colombet   EXPECT_EQ(Known.Zero.getZExtValue(), ~(65536llu - 1));
1337f1f31eb2SQuentin Colombet   EXPECT_EQ(Known.One.getZExtValue(), 0u);
1338f1f31eb2SQuentin Colombet   Instruction &APlus512 = findInstructionByName(F, "APlus512");
1339f1f31eb2SQuentin Colombet   Known = computeKnownBits(&APlus512, M->getDataLayout(), /* Depth */ 0, &AC,
1340f1f31eb2SQuentin Colombet                            F->front().getTerminator());
1341f1f31eb2SQuentin Colombet   // We know of one less zero because 512 may have produced a 1 that
1342f1f31eb2SQuentin Colombet   // got carried all the way to the first trailing zero.
1343f1f31eb2SQuentin Colombet   EXPECT_EQ(Known.Zero.getZExtValue(), (~(65536llu - 1)) << 1);
1344f1f31eb2SQuentin Colombet   EXPECT_EQ(Known.One.getZExtValue(), 0u);
1345f1f31eb2SQuentin Colombet   // The known range is not precise given computeKnownBits works
1346f1f31eb2SQuentin Colombet   // with the masks of zeros and ones, not the ranges.
1347f1f31eb2SQuentin Colombet   EXPECT_EQ(Known.getMinValue(), 0u);
1348f1f31eb2SQuentin Colombet   EXPECT_EQ(Known.getMaxValue(), 131071);
1349f1f31eb2SQuentin Colombet }
1350f1f31eb2SQuentin Colombet 
1351f1f31eb2SQuentin Colombet // 512 + [32, 64) doesn't produce overlapping bits.
1352f1f31eb2SQuentin Colombet // Make sure we get all the individual bits properly.
1353f1f31eb2SQuentin Colombet TEST_F(ComputeKnownBitsTest, ComputeKnownBitsAddWithRangeNoOverlap) {
1354f1f31eb2SQuentin Colombet   parseAssembly("define void @test(i64* %p) {\n"
1355f1f31eb2SQuentin Colombet                 "  %A = load i64, i64* %p, !range !{i64 32, i64 64}\n"
1356f1f31eb2SQuentin Colombet                 "  %APlus512 = add i64 %A, 512\n"
1357f1f31eb2SQuentin Colombet                 "  %c = icmp ugt i64 %APlus512, 523\n"
1358f1f31eb2SQuentin Colombet                 "  call void @llvm.assume(i1 %c)\n"
1359f1f31eb2SQuentin Colombet                 "  ret void\n"
1360f1f31eb2SQuentin Colombet                 "}\n"
1361f1f31eb2SQuentin Colombet                 "declare void @llvm.assume(i1)\n");
1362f1f31eb2SQuentin Colombet   AssumptionCache AC(*F);
1363f1f31eb2SQuentin Colombet   KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC,
1364f1f31eb2SQuentin Colombet                                      F->front().getTerminator());
1365f1f31eb2SQuentin Colombet   EXPECT_EQ(Known.Zero.getZExtValue(), ~(64llu - 1));
1366f1f31eb2SQuentin Colombet   EXPECT_EQ(Known.One.getZExtValue(), 32u);
1367f1f31eb2SQuentin Colombet   Instruction &APlus512 = findInstructionByName(F, "APlus512");
1368f1f31eb2SQuentin Colombet   Known = computeKnownBits(&APlus512, M->getDataLayout(), /* Depth */ 0, &AC,
1369f1f31eb2SQuentin Colombet                            F->front().getTerminator());
1370f1f31eb2SQuentin Colombet   EXPECT_EQ(Known.Zero.getZExtValue(), ~512llu & ~(64llu - 1));
1371f1f31eb2SQuentin Colombet   EXPECT_EQ(Known.One.getZExtValue(), 512u | 32u);
1372f1f31eb2SQuentin Colombet   // The known range is not precise given computeKnownBits works
1373f1f31eb2SQuentin Colombet   // with the masks of zeros and ones, not the ranges.
1374f1f31eb2SQuentin Colombet   EXPECT_EQ(Known.getMinValue(), 544);
1375f1f31eb2SQuentin Colombet   EXPECT_EQ(Known.getMaxValue(), 575);
1376f1f31eb2SQuentin Colombet }
1377f1f31eb2SQuentin Colombet 
1378ee6abef5SQuentin Colombet TEST_F(ComputeKnownBitsTest, ComputeKnownBitsGEPWithRange) {
1379ee6abef5SQuentin Colombet   parseAssembly(
1380ee6abef5SQuentin Colombet       "define void @test(i64* %p) {\n"
1381ee6abef5SQuentin Colombet       "  %A = load i64, i64* %p, !range !{i64 64, i64 65536}\n"
1382ee6abef5SQuentin Colombet       "  %APtr = inttoptr i64 %A to float*"
1383ee6abef5SQuentin Colombet       "  %APtrPlus512 = getelementptr float, float* %APtr, i32 128\n"
1384ee6abef5SQuentin Colombet       "  %c = icmp ugt float* %APtrPlus512, inttoptr (i32 523 to float*)\n"
1385ee6abef5SQuentin Colombet       "  call void @llvm.assume(i1 %c)\n"
1386ee6abef5SQuentin Colombet       "  ret void\n"
1387ee6abef5SQuentin Colombet       "}\n"
1388ee6abef5SQuentin Colombet       "declare void @llvm.assume(i1)\n");
1389ee6abef5SQuentin Colombet   AssumptionCache AC(*F);
1390ee6abef5SQuentin Colombet   KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC,
1391ee6abef5SQuentin Colombet                                      F->front().getTerminator());
1392ee6abef5SQuentin Colombet   EXPECT_EQ(Known.Zero.getZExtValue(), ~(65536llu - 1));
1393ee6abef5SQuentin Colombet   EXPECT_EQ(Known.One.getZExtValue(), 0u);
1394ee6abef5SQuentin Colombet   Instruction &APtrPlus512 = findInstructionByName(F, "APtrPlus512");
1395ee6abef5SQuentin Colombet   Known = computeKnownBits(&APtrPlus512, M->getDataLayout(), /* Depth */ 0, &AC,
1396ee6abef5SQuentin Colombet                            F->front().getTerminator());
1397ee6abef5SQuentin Colombet   // We know of one less zero because 512 may have produced a 1 that
1398ee6abef5SQuentin Colombet   // got carried all the way to the first trailing zero.
1399ee6abef5SQuentin Colombet   EXPECT_EQ(Known.Zero.getZExtValue(), ~(65536llu - 1) << 1);
1400ee6abef5SQuentin Colombet   EXPECT_EQ(Known.One.getZExtValue(), 0u);
1401ee6abef5SQuentin Colombet   // The known range is not precise given computeKnownBits works
1402ee6abef5SQuentin Colombet   // with the masks of zeros and ones, not the ranges.
1403ee6abef5SQuentin Colombet   EXPECT_EQ(Known.getMinValue(), 0u);
1404ee6abef5SQuentin Colombet   EXPECT_EQ(Known.getMaxValue(), 131071);
1405ee6abef5SQuentin Colombet }
1406ee6abef5SQuentin Colombet 
1407ee6abef5SQuentin Colombet // 4*128 + [32, 64) doesn't produce overlapping bits.
1408ee6abef5SQuentin Colombet // Make sure we get all the individual bits properly.
1409ee6abef5SQuentin Colombet // This test is useful to check that we account for the scaling factor
1410ee6abef5SQuentin Colombet // in the gep. Indeed, gep float, [32,64), 128 is not 128 + [32,64).
1411ee6abef5SQuentin Colombet TEST_F(ComputeKnownBitsTest, ComputeKnownBitsGEPWithRangeNoOverlap) {
1412ee6abef5SQuentin Colombet   parseAssembly(
1413ee6abef5SQuentin Colombet       "define void @test(i64* %p) {\n"
1414ee6abef5SQuentin Colombet       "  %A = load i64, i64* %p, !range !{i64 32, i64 64}\n"
1415ee6abef5SQuentin Colombet       "  %APtr = inttoptr i64 %A to float*"
1416ee6abef5SQuentin Colombet       "  %APtrPlus512 = getelementptr float, float* %APtr, i32 128\n"
1417ee6abef5SQuentin Colombet       "  %c = icmp ugt float* %APtrPlus512, inttoptr (i32 523 to float*)\n"
1418ee6abef5SQuentin Colombet       "  call void @llvm.assume(i1 %c)\n"
1419ee6abef5SQuentin Colombet       "  ret void\n"
1420ee6abef5SQuentin Colombet       "}\n"
1421ee6abef5SQuentin Colombet       "declare void @llvm.assume(i1)\n");
1422ee6abef5SQuentin Colombet   AssumptionCache AC(*F);
1423ee6abef5SQuentin Colombet   KnownBits Known = computeKnownBits(A, M->getDataLayout(), /* Depth */ 0, &AC,
1424ee6abef5SQuentin Colombet                                      F->front().getTerminator());
1425ee6abef5SQuentin Colombet   EXPECT_EQ(Known.Zero.getZExtValue(), ~(64llu - 1));
1426ee6abef5SQuentin Colombet   EXPECT_EQ(Known.One.getZExtValue(), 32u);
1427ee6abef5SQuentin Colombet   Instruction &APtrPlus512 = findInstructionByName(F, "APtrPlus512");
1428ee6abef5SQuentin Colombet   Known = computeKnownBits(&APtrPlus512, M->getDataLayout(), /* Depth */ 0, &AC,
1429ee6abef5SQuentin Colombet                            F->front().getTerminator());
1430ee6abef5SQuentin Colombet   EXPECT_EQ(Known.Zero.getZExtValue(), ~512llu & ~(64llu - 1));
1431ee6abef5SQuentin Colombet   EXPECT_EQ(Known.One.getZExtValue(), 512u | 32u);
1432ee6abef5SQuentin Colombet   // The known range is not precise given computeKnownBits works
1433ee6abef5SQuentin Colombet   // with the masks of zeros and ones, not the ranges.
1434ee6abef5SQuentin Colombet   EXPECT_EQ(Known.getMinValue(), 544);
1435ee6abef5SQuentin Colombet   EXPECT_EQ(Known.getMaxValue(), 575);
1436ee6abef5SQuentin Colombet }
1437ee6abef5SQuentin Colombet 
1438329d15b5SVitaly Buka class IsBytewiseValueTest : public ValueTrackingTest,
1439329d15b5SVitaly Buka                             public ::testing::WithParamInterface<
1440329d15b5SVitaly Buka                                 std::pair<const char *, const char *>> {
1441329d15b5SVitaly Buka protected:
1442329d15b5SVitaly Buka };
1443329d15b5SVitaly Buka 
1444329d15b5SVitaly Buka const std::pair<const char *, const char *> IsBytewiseValueTests[] = {
1445329d15b5SVitaly Buka     {
1446329d15b5SVitaly Buka         "i8 0",
1447329d15b5SVitaly Buka         "i48* null",
1448329d15b5SVitaly Buka     },
1449329d15b5SVitaly Buka     {
1450329d15b5SVitaly Buka         "i8 undef",
1451329d15b5SVitaly Buka         "i48* undef",
1452329d15b5SVitaly Buka     },
1453329d15b5SVitaly Buka     {
1454329d15b5SVitaly Buka         "i8 0",
1455329d15b5SVitaly Buka         "i8 zeroinitializer",
1456329d15b5SVitaly Buka     },
1457329d15b5SVitaly Buka     {
1458329d15b5SVitaly Buka         "i8 0",
1459329d15b5SVitaly Buka         "i8 0",
1460329d15b5SVitaly Buka     },
1461329d15b5SVitaly Buka     {
1462329d15b5SVitaly Buka         "i8 -86",
1463329d15b5SVitaly Buka         "i8 -86",
1464329d15b5SVitaly Buka     },
1465329d15b5SVitaly Buka     {
1466329d15b5SVitaly Buka         "i8 -1",
1467329d15b5SVitaly Buka         "i8 -1",
1468329d15b5SVitaly Buka     },
1469329d15b5SVitaly Buka     {
1470329d15b5SVitaly Buka         "i8 undef",
1471329d15b5SVitaly Buka         "i16 undef",
1472329d15b5SVitaly Buka     },
1473329d15b5SVitaly Buka     {
1474329d15b5SVitaly Buka         "i8 0",
1475329d15b5SVitaly Buka         "i16 0",
1476329d15b5SVitaly Buka     },
1477329d15b5SVitaly Buka     {
1478329d15b5SVitaly Buka         "",
1479329d15b5SVitaly Buka         "i16 7",
1480329d15b5SVitaly Buka     },
1481329d15b5SVitaly Buka     {
1482329d15b5SVitaly Buka         "i8 -86",
1483329d15b5SVitaly Buka         "i16 -21846",
1484329d15b5SVitaly Buka     },
1485329d15b5SVitaly Buka     {
1486329d15b5SVitaly Buka         "i8 -1",
1487329d15b5SVitaly Buka         "i16 -1",
1488329d15b5SVitaly Buka     },
1489329d15b5SVitaly Buka     {
1490329d15b5SVitaly Buka         "i8 0",
1491329d15b5SVitaly Buka         "i48 0",
1492329d15b5SVitaly Buka     },
1493329d15b5SVitaly Buka     {
1494329d15b5SVitaly Buka         "i8 -1",
1495329d15b5SVitaly Buka         "i48 -1",
1496329d15b5SVitaly Buka     },
1497329d15b5SVitaly Buka     {
1498329d15b5SVitaly Buka         "i8 0",
1499329d15b5SVitaly Buka         "i49 0",
1500329d15b5SVitaly Buka     },
1501329d15b5SVitaly Buka     {
1502329d15b5SVitaly Buka         "",
1503329d15b5SVitaly Buka         "i49 -1",
1504329d15b5SVitaly Buka     },
1505329d15b5SVitaly Buka     {
1506329d15b5SVitaly Buka         "i8 0",
1507329d15b5SVitaly Buka         "half 0xH0000",
1508329d15b5SVitaly Buka     },
1509329d15b5SVitaly Buka     {
1510329d15b5SVitaly Buka         "i8 -85",
1511329d15b5SVitaly Buka         "half 0xHABAB",
1512329d15b5SVitaly Buka     },
1513329d15b5SVitaly Buka     {
1514329d15b5SVitaly Buka         "i8 0",
1515329d15b5SVitaly Buka         "float 0.0",
1516329d15b5SVitaly Buka     },
1517329d15b5SVitaly Buka     {
1518329d15b5SVitaly Buka         "i8 -1",
1519329d15b5SVitaly Buka         "float 0xFFFFFFFFE0000000",
1520329d15b5SVitaly Buka     },
1521329d15b5SVitaly Buka     {
1522329d15b5SVitaly Buka         "i8 0",
1523329d15b5SVitaly Buka         "double 0.0",
1524329d15b5SVitaly Buka     },
1525329d15b5SVitaly Buka     {
1526329d15b5SVitaly Buka         "i8 -15",
1527329d15b5SVitaly Buka         "double 0xF1F1F1F1F1F1F1F1",
1528329d15b5SVitaly Buka     },
1529329d15b5SVitaly Buka     {
1530329d15b5SVitaly Buka         "i8 undef",
1531329d15b5SVitaly Buka         "i16* undef",
1532329d15b5SVitaly Buka     },
1533329d15b5SVitaly Buka     {
1534329d15b5SVitaly Buka         "i8 0",
1535329d15b5SVitaly Buka         "i16* inttoptr (i64 0 to i16*)",
1536329d15b5SVitaly Buka     },
1537329d15b5SVitaly Buka     {
1538c559e637SVitaly Buka         "i8 -1",
1539329d15b5SVitaly Buka         "i16* inttoptr (i64 -1 to i16*)",
1540329d15b5SVitaly Buka     },
1541329d15b5SVitaly Buka     {
1542c559e637SVitaly Buka         "i8 -86",
1543329d15b5SVitaly Buka         "i16* inttoptr (i64 -6148914691236517206 to i16*)",
1544329d15b5SVitaly Buka     },
1545329d15b5SVitaly Buka     {
1546329d15b5SVitaly Buka         "",
1547329d15b5SVitaly Buka         "i16* inttoptr (i48 -1 to i16*)",
1548329d15b5SVitaly Buka     },
1549329d15b5SVitaly Buka     {
1550c559e637SVitaly Buka         "i8 -1",
1551329d15b5SVitaly Buka         "i16* inttoptr (i96 -1 to i16*)",
1552329d15b5SVitaly Buka     },
1553329d15b5SVitaly Buka     {
155452096ee9SVitaly Buka         "i8 undef",
1555329d15b5SVitaly Buka         "[0 x i8] zeroinitializer",
1556329d15b5SVitaly Buka     },
1557329d15b5SVitaly Buka     {
1558329d15b5SVitaly Buka         "i8 undef",
1559329d15b5SVitaly Buka         "[0 x i8] undef",
1560329d15b5SVitaly Buka     },
1561329d15b5SVitaly Buka     {
156252096ee9SVitaly Buka         "i8 undef",
1563329d15b5SVitaly Buka         "[5 x [0 x i8]] zeroinitializer",
1564329d15b5SVitaly Buka     },
1565329d15b5SVitaly Buka     {
1566329d15b5SVitaly Buka         "i8 undef",
1567329d15b5SVitaly Buka         "[5 x [0 x i8]] undef",
1568329d15b5SVitaly Buka     },
1569329d15b5SVitaly Buka     {
1570329d15b5SVitaly Buka         "i8 0",
1571329d15b5SVitaly Buka         "[6 x i8] zeroinitializer",
1572329d15b5SVitaly Buka     },
1573329d15b5SVitaly Buka     {
1574329d15b5SVitaly Buka         "i8 undef",
1575329d15b5SVitaly Buka         "[6 x i8] undef",
1576329d15b5SVitaly Buka     },
1577329d15b5SVitaly Buka     {
1578329d15b5SVitaly Buka         "i8 1",
1579329d15b5SVitaly Buka         "[5 x i8] [i8 1, i8 1, i8 1, i8 1, i8 1]",
1580329d15b5SVitaly Buka     },
1581329d15b5SVitaly Buka     {
1582329d15b5SVitaly Buka         "",
1583329d15b5SVitaly Buka         "[5 x i64] [i64 1, i64 1, i64 1, i64 1, i64 1]",
1584329d15b5SVitaly Buka     },
1585329d15b5SVitaly Buka     {
1586329d15b5SVitaly Buka         "i8 -1",
1587329d15b5SVitaly Buka         "[5 x i64] [i64 -1, i64 -1, i64 -1, i64 -1, i64 -1]",
1588329d15b5SVitaly Buka     },
1589329d15b5SVitaly Buka     {
1590329d15b5SVitaly Buka         "",
1591329d15b5SVitaly Buka         "[4 x i8] [i8 1, i8 2, i8 1, i8 1]",
1592329d15b5SVitaly Buka     },
1593329d15b5SVitaly Buka     {
1594329d15b5SVitaly Buka         "i8 1",
1595329d15b5SVitaly Buka         "[4 x i8] [i8 1, i8 undef, i8 1, i8 1]",
1596329d15b5SVitaly Buka     },
1597329d15b5SVitaly Buka     {
1598329d15b5SVitaly Buka         "i8 0",
1599329d15b5SVitaly Buka         "<6 x i8> zeroinitializer",
1600329d15b5SVitaly Buka     },
1601329d15b5SVitaly Buka     {
1602329d15b5SVitaly Buka         "i8 undef",
1603329d15b5SVitaly Buka         "<6 x i8> undef",
1604329d15b5SVitaly Buka     },
1605329d15b5SVitaly Buka     {
1606329d15b5SVitaly Buka         "i8 1",
1607329d15b5SVitaly Buka         "<5 x i8> <i8 1, i8 1, i8 1, i8 1, i8 1>",
1608329d15b5SVitaly Buka     },
1609329d15b5SVitaly Buka     {
1610329d15b5SVitaly Buka         "",
1611329d15b5SVitaly Buka         "<5 x i64> <i64 1, i64 1, i64 1, i64 1, i64 1>",
1612329d15b5SVitaly Buka     },
1613329d15b5SVitaly Buka     {
1614329d15b5SVitaly Buka         "i8 -1",
1615329d15b5SVitaly Buka         "<5 x i64> <i64 -1, i64 -1, i64 -1, i64 -1, i64 -1>",
1616329d15b5SVitaly Buka     },
1617329d15b5SVitaly Buka     {
1618329d15b5SVitaly Buka         "",
1619329d15b5SVitaly Buka         "<4 x i8> <i8 1, i8 1, i8 2, i8 1>",
1620329d15b5SVitaly Buka     },
1621329d15b5SVitaly Buka     {
1622b1bff76eSVitaly Buka         "i8 5",
1623329d15b5SVitaly Buka         "<2 x i8> < i8 5, i8 undef >",
1624329d15b5SVitaly Buka     },
1625329d15b5SVitaly Buka     {
1626329d15b5SVitaly Buka         "i8 0",
1627329d15b5SVitaly Buka         "[2 x [2 x i16]] zeroinitializer",
1628329d15b5SVitaly Buka     },
1629329d15b5SVitaly Buka     {
1630329d15b5SVitaly Buka         "i8 undef",
1631329d15b5SVitaly Buka         "[2 x [2 x i16]] undef",
1632329d15b5SVitaly Buka     },
1633329d15b5SVitaly Buka     {
1634329d15b5SVitaly Buka         "i8 -86",
1635329d15b5SVitaly Buka         "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], "
1636329d15b5SVitaly Buka         "[2 x i16] [i16 -21846, i16 -21846]]",
1637329d15b5SVitaly Buka     },
1638329d15b5SVitaly Buka     {
1639329d15b5SVitaly Buka         "",
1640329d15b5SVitaly Buka         "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], "
1641329d15b5SVitaly Buka         "[2 x i16] [i16 -21836, i16 -21846]]",
1642329d15b5SVitaly Buka     },
1643329d15b5SVitaly Buka     {
164452096ee9SVitaly Buka         "i8 undef",
1645329d15b5SVitaly Buka         "{ } zeroinitializer",
1646329d15b5SVitaly Buka     },
1647329d15b5SVitaly Buka     {
1648329d15b5SVitaly Buka         "i8 undef",
1649329d15b5SVitaly Buka         "{ } undef",
1650329d15b5SVitaly Buka     },
1651329d15b5SVitaly Buka     {
165252096ee9SVitaly Buka         "i8 undef",
1653329d15b5SVitaly Buka         "{ {}, {} } zeroinitializer",
1654329d15b5SVitaly Buka     },
1655329d15b5SVitaly Buka     {
1656329d15b5SVitaly Buka         "i8 undef",
1657329d15b5SVitaly Buka         "{ {}, {} } undef",
1658329d15b5SVitaly Buka     },
1659329d15b5SVitaly Buka     {
1660329d15b5SVitaly Buka         "i8 0",
1661329d15b5SVitaly Buka         "{i8, i64, i16*} zeroinitializer",
1662329d15b5SVitaly Buka     },
1663329d15b5SVitaly Buka     {
1664329d15b5SVitaly Buka         "i8 undef",
1665329d15b5SVitaly Buka         "{i8, i64, i16*} undef",
1666329d15b5SVitaly Buka     },
1667329d15b5SVitaly Buka     {
1668329d15b5SVitaly Buka         "i8 -86",
1669329d15b5SVitaly Buka         "{i8, i64, i16*} {i8 -86, i64 -6148914691236517206, i16* undef}",
1670329d15b5SVitaly Buka     },
1671329d15b5SVitaly Buka     {
1672329d15b5SVitaly Buka         "",
1673329d15b5SVitaly Buka         "{i8, i64, i16*} {i8 86, i64 -6148914691236517206, i16* undef}",
1674329d15b5SVitaly Buka     },
1675329d15b5SVitaly Buka };
1676329d15b5SVitaly Buka 
1677329d15b5SVitaly Buka INSTANTIATE_TEST_CASE_P(IsBytewiseValueParamTests, IsBytewiseValueTest,
16787618b2a5SMikael Holmen                         ::testing::ValuesIn(IsBytewiseValueTests),);
1679329d15b5SVitaly Buka 
1680329d15b5SVitaly Buka TEST_P(IsBytewiseValueTest, IsBytewiseValue) {
1681329d15b5SVitaly Buka   auto M = parseModule(std::string("@test = global ") + GetParam().second);
1682329d15b5SVitaly Buka   GlobalVariable *GV = dyn_cast<GlobalVariable>(M->getNamedValue("test"));
1683329d15b5SVitaly Buka   Value *Actual = isBytewiseValue(GV->getInitializer(), M->getDataLayout());
1684329d15b5SVitaly Buka   std::string Buff;
1685329d15b5SVitaly Buka   raw_string_ostream S(Buff);
1686329d15b5SVitaly Buka   if (Actual)
1687329d15b5SVitaly Buka     S << *Actual;
1688329d15b5SVitaly Buka   EXPECT_EQ(GetParam().first, S.str());
1689329d15b5SVitaly Buka }
16908d041811SFlorian Hahn 
16918d041811SFlorian Hahn TEST_F(ValueTrackingTest, ComputeConstantRange) {
16928d041811SFlorian Hahn   {
16938d041811SFlorian Hahn     // Assumptions:
16948d041811SFlorian Hahn     //  * stride >= 5
16958d041811SFlorian Hahn     //  * stride < 10
16968d041811SFlorian Hahn     //
16978d041811SFlorian Hahn     // stride = [5, 10)
16988d041811SFlorian Hahn     auto M = parseModule(R"(
16998d041811SFlorian Hahn   declare void @llvm.assume(i1)
17008d041811SFlorian Hahn 
17018d041811SFlorian Hahn   define i32 @test(i32 %stride) {
17028d041811SFlorian Hahn     %gt = icmp uge i32 %stride, 5
17038d041811SFlorian Hahn     call void @llvm.assume(i1 %gt)
17048d041811SFlorian Hahn     %lt = icmp ult i32 %stride, 10
17058d041811SFlorian Hahn     call void @llvm.assume(i1 %lt)
17068d041811SFlorian Hahn     %stride.plus.one = add nsw nuw i32 %stride, 1
17078d041811SFlorian Hahn     ret i32 %stride.plus.one
17088d041811SFlorian Hahn   })");
17098d041811SFlorian Hahn     Function *F = M->getFunction("test");
17108d041811SFlorian Hahn 
17118d041811SFlorian Hahn     AssumptionCache AC(*F);
17128d041811SFlorian Hahn     Value *Stride = &*F->arg_begin();
17138d041811SFlorian Hahn     ConstantRange CR1 = computeConstantRange(Stride, true, &AC, nullptr);
17148d041811SFlorian Hahn     EXPECT_TRUE(CR1.isFullSet());
17158d041811SFlorian Hahn 
17168d041811SFlorian Hahn     Instruction *I = &findInstructionByName(F, "stride.plus.one");
17178d041811SFlorian Hahn     ConstantRange CR2 = computeConstantRange(Stride, true, &AC, I);
17188d041811SFlorian Hahn     EXPECT_EQ(5, CR2.getLower());
17198d041811SFlorian Hahn     EXPECT_EQ(10, CR2.getUpper());
17208d041811SFlorian Hahn   }
17218d041811SFlorian Hahn 
17228d041811SFlorian Hahn   {
17238d041811SFlorian Hahn     // Assumptions:
17248d041811SFlorian Hahn     //  * stride >= 5
17258d041811SFlorian Hahn     //  * stride < 200
17268d041811SFlorian Hahn     //  * stride == 99
17278d041811SFlorian Hahn     //
17288d041811SFlorian Hahn     // stride = [99, 100)
17298d041811SFlorian Hahn     auto M = parseModule(R"(
17308d041811SFlorian Hahn   declare void @llvm.assume(i1)
17318d041811SFlorian Hahn 
17328d041811SFlorian Hahn   define i32 @test(i32 %stride) {
17338d041811SFlorian Hahn     %gt = icmp uge i32 %stride, 5
17348d041811SFlorian Hahn     call void @llvm.assume(i1 %gt)
17358d041811SFlorian Hahn     %lt = icmp ult i32 %stride, 200
17368d041811SFlorian Hahn     call void @llvm.assume(i1 %lt)
17378d041811SFlorian Hahn     %eq = icmp eq i32 %stride, 99
17388d041811SFlorian Hahn     call void @llvm.assume(i1 %eq)
17398d041811SFlorian Hahn     %stride.plus.one = add nsw nuw i32 %stride, 1
17408d041811SFlorian Hahn     ret i32 %stride.plus.one
17418d041811SFlorian Hahn   })");
17428d041811SFlorian Hahn     Function *F = M->getFunction("test");
17438d041811SFlorian Hahn 
17448d041811SFlorian Hahn     AssumptionCache AC(*F);
17458d041811SFlorian Hahn     Value *Stride = &*F->arg_begin();
17468d041811SFlorian Hahn     Instruction *I = &findInstructionByName(F, "stride.plus.one");
17478d041811SFlorian Hahn     ConstantRange CR = computeConstantRange(Stride, true, &AC, I);
17488d041811SFlorian Hahn     EXPECT_EQ(99, *CR.getSingleElement());
17498d041811SFlorian Hahn   }
17508d041811SFlorian Hahn 
17518d041811SFlorian Hahn   {
17528d041811SFlorian Hahn     // Assumptions:
17538d041811SFlorian Hahn     //  * stride >= 5
17548d041811SFlorian Hahn     //  * stride >= 50
17558d041811SFlorian Hahn     //  * stride < 100
17568d041811SFlorian Hahn     //  * stride < 200
17578d041811SFlorian Hahn     //
17588d041811SFlorian Hahn     // stride = [50, 100)
17598d041811SFlorian Hahn     auto M = parseModule(R"(
17608d041811SFlorian Hahn   declare void @llvm.assume(i1)
17618d041811SFlorian Hahn 
17628d041811SFlorian Hahn   define i32 @test(i32 %stride, i1 %cond) {
17638d041811SFlorian Hahn     %gt = icmp uge i32 %stride, 5
17648d041811SFlorian Hahn     call void @llvm.assume(i1 %gt)
17658d041811SFlorian Hahn     %gt.2 = icmp uge i32 %stride, 50
17668d041811SFlorian Hahn     call void @llvm.assume(i1 %gt.2)
17678d041811SFlorian Hahn     br i1 %cond, label %bb1, label %bb2
17688d041811SFlorian Hahn 
17698d041811SFlorian Hahn   bb1:
17708d041811SFlorian Hahn     %lt = icmp ult i32 %stride, 200
17718d041811SFlorian Hahn     call void @llvm.assume(i1 %lt)
17728d041811SFlorian Hahn     %lt.2 = icmp ult i32 %stride, 100
17738d041811SFlorian Hahn     call void @llvm.assume(i1 %lt.2)
17748d041811SFlorian Hahn     %stride.plus.one = add nsw nuw i32 %stride, 1
17758d041811SFlorian Hahn     ret i32 %stride.plus.one
17768d041811SFlorian Hahn 
17778d041811SFlorian Hahn   bb2:
17788d041811SFlorian Hahn     ret i32 0
17798d041811SFlorian Hahn   })");
17808d041811SFlorian Hahn     Function *F = M->getFunction("test");
17818d041811SFlorian Hahn 
17828d041811SFlorian Hahn     AssumptionCache AC(*F);
17838d041811SFlorian Hahn     Value *Stride = &*F->arg_begin();
17848d041811SFlorian Hahn     Instruction *GT2 = &findInstructionByName(F, "gt.2");
17858d041811SFlorian Hahn     ConstantRange CR = computeConstantRange(Stride, true, &AC, GT2);
17868d041811SFlorian Hahn     EXPECT_EQ(5, CR.getLower());
17878d041811SFlorian Hahn     EXPECT_EQ(0, CR.getUpper());
17888d041811SFlorian Hahn 
17898d041811SFlorian Hahn     Instruction *I = &findInstructionByName(F, "stride.plus.one");
17908d041811SFlorian Hahn     ConstantRange CR2 = computeConstantRange(Stride, true, &AC, I);
17918d041811SFlorian Hahn     EXPECT_EQ(50, CR2.getLower());
17928d041811SFlorian Hahn     EXPECT_EQ(100, CR2.getUpper());
17938d041811SFlorian Hahn   }
17948d041811SFlorian Hahn 
17958d041811SFlorian Hahn   {
17968d041811SFlorian Hahn     // Assumptions:
17978d041811SFlorian Hahn     //  * stride > 5
17988d041811SFlorian Hahn     //  * stride < 5
17998d041811SFlorian Hahn     //
18008d041811SFlorian Hahn     // stride = empty range, as the assumptions contradict each other.
18018d041811SFlorian Hahn     auto M = parseModule(R"(
18028d041811SFlorian Hahn   declare void @llvm.assume(i1)
18038d041811SFlorian Hahn 
18048d041811SFlorian Hahn   define i32 @test(i32 %stride, i1 %cond) {
18058d041811SFlorian Hahn     %gt = icmp ugt i32 %stride, 5
18068d041811SFlorian Hahn     call void @llvm.assume(i1 %gt)
18078d041811SFlorian Hahn     %lt = icmp ult i32 %stride, 5
18088d041811SFlorian Hahn     call void @llvm.assume(i1 %lt)
18098d041811SFlorian Hahn     %stride.plus.one = add nsw nuw i32 %stride, 1
18108d041811SFlorian Hahn     ret i32 %stride.plus.one
18118d041811SFlorian Hahn   })");
18128d041811SFlorian Hahn     Function *F = M->getFunction("test");
18138d041811SFlorian Hahn 
18148d041811SFlorian Hahn     AssumptionCache AC(*F);
18158d041811SFlorian Hahn     Value *Stride = &*F->arg_begin();
18168d041811SFlorian Hahn 
18178d041811SFlorian Hahn     Instruction *I = &findInstructionByName(F, "stride.plus.one");
18188d041811SFlorian Hahn     ConstantRange CR = computeConstantRange(Stride, true, &AC, I);
18198d041811SFlorian Hahn     EXPECT_TRUE(CR.isEmptySet());
18208d041811SFlorian Hahn   }
18218d041811SFlorian Hahn 
18228d041811SFlorian Hahn   {
18238d041811SFlorian Hahn     // Assumptions:
18248d041811SFlorian Hahn     //  * x.1 >= 5
18258d041811SFlorian Hahn     //  * x.2 < x.1
18268d041811SFlorian Hahn     //
18278d041811SFlorian Hahn     // stride = [0, 5)
18288d041811SFlorian Hahn     auto M = parseModule(R"(
18298d041811SFlorian Hahn   declare void @llvm.assume(i1)
18308d041811SFlorian Hahn 
18318d041811SFlorian Hahn   define i32 @test(i32 %x.1, i32 %x.2) {
18328d041811SFlorian Hahn     %gt = icmp uge i32 %x.1, 5
18338d041811SFlorian Hahn     call void @llvm.assume(i1 %gt)
18348d041811SFlorian Hahn     %lt = icmp ult i32 %x.2, %x.1
18358d041811SFlorian Hahn     call void @llvm.assume(i1 %lt)
18368d041811SFlorian Hahn     %stride.plus.one = add nsw nuw i32 %x.1, 1
18378d041811SFlorian Hahn     ret i32 %stride.plus.one
18388d041811SFlorian Hahn   })");
18398d041811SFlorian Hahn     Function *F = M->getFunction("test");
18408d041811SFlorian Hahn 
18418d041811SFlorian Hahn     AssumptionCache AC(*F);
18428d041811SFlorian Hahn     Value *X2 = &*std::next(F->arg_begin());
18438d041811SFlorian Hahn 
18448d041811SFlorian Hahn     Instruction *I = &findInstructionByName(F, "stride.plus.one");
18458d041811SFlorian Hahn     ConstantRange CR1 = computeConstantRange(X2, true, &AC, I);
18468d041811SFlorian Hahn     EXPECT_EQ(0, CR1.getLower());
18478d041811SFlorian Hahn     EXPECT_EQ(5, CR1.getUpper());
18488d041811SFlorian Hahn 
18498d041811SFlorian Hahn     // Check the depth cutoff results in a conservative result (full set) by
18508d041811SFlorian Hahn     // passing Depth == MaxDepth == 6.
18518d041811SFlorian Hahn     ConstantRange CR2 = computeConstantRange(X2, true, &AC, I, 6);
18528d041811SFlorian Hahn     EXPECT_TRUE(CR2.isFullSet());
18538d041811SFlorian Hahn   }
18548d041811SFlorian Hahn }
18557f1bb326SVitaly Buka 
18566ccacb46SVitaly Buka struct FindAllocaForValueTestParams {
18576ccacb46SVitaly Buka   const char *IR;
1858a6927c86SVitaly Buka   bool AnyOffsetResult;
1859a6927c86SVitaly Buka   bool ZeroOffsetResult;
18606ccacb46SVitaly Buka };
18616ccacb46SVitaly Buka 
18627f1bb326SVitaly Buka class FindAllocaForValueTest
18637f1bb326SVitaly Buka     : public ValueTrackingTest,
18646ccacb46SVitaly Buka       public ::testing::WithParamInterface<FindAllocaForValueTestParams> {
18657f1bb326SVitaly Buka protected:
18667f1bb326SVitaly Buka };
18677f1bb326SVitaly Buka 
18686ccacb46SVitaly Buka const FindAllocaForValueTestParams FindAllocaForValueTests[] = {
18697f1bb326SVitaly Buka     {R"(
18707f1bb326SVitaly Buka       define void @test() {
18717f1bb326SVitaly Buka         %a = alloca i64
18727f1bb326SVitaly Buka         %r = bitcast i64* %a to i32*
18737f1bb326SVitaly Buka         ret void
18747f1bb326SVitaly Buka       })",
1875a6927c86SVitaly Buka      true, true},
18767f1bb326SVitaly Buka 
18777f1bb326SVitaly Buka     {R"(
18787f1bb326SVitaly Buka       define void @test() {
18797f1bb326SVitaly Buka         %a = alloca i32
18807f1bb326SVitaly Buka         %r = getelementptr i32, i32* %a, i32 1
18817f1bb326SVitaly Buka         ret void
18827f1bb326SVitaly Buka       })",
1883a6927c86SVitaly Buka      true, false},
1884a6927c86SVitaly Buka 
1885a6927c86SVitaly Buka     {R"(
1886a6927c86SVitaly Buka       define void @test() {
1887a6927c86SVitaly Buka         %a = alloca i32
1888a6927c86SVitaly Buka         %r = getelementptr i32, i32* %a, i32 0
1889a6927c86SVitaly Buka         ret void
1890a6927c86SVitaly Buka       })",
1891a6927c86SVitaly Buka      true, true},
18927f1bb326SVitaly Buka 
18937f1bb326SVitaly Buka     {R"(
18947f1bb326SVitaly Buka       define void @test(i1 %cond) {
18957f1bb326SVitaly Buka       entry:
18967f1bb326SVitaly Buka         %a = alloca i32
18977f1bb326SVitaly Buka         br label %bb1
18987f1bb326SVitaly Buka 
18997f1bb326SVitaly Buka       bb1:
19007f1bb326SVitaly Buka         %r = phi i32* [ %a, %entry ], [ %r, %bb1 ]
19017f1bb326SVitaly Buka         br i1 %cond, label %bb1, label %exit
19027f1bb326SVitaly Buka 
19037f1bb326SVitaly Buka       exit:
19047f1bb326SVitaly Buka         ret void
19057f1bb326SVitaly Buka       })",
1906a6927c86SVitaly Buka      true, true},
19077f1bb326SVitaly Buka 
19087f1bb326SVitaly Buka     {R"(
19097f1bb326SVitaly Buka       define void @test(i1 %cond) {
1910469debe0SVitaly Buka         %a = alloca i32
1911469debe0SVitaly Buka         %r = select i1 %cond, i32* %a, i32* %a
1912469debe0SVitaly Buka         ret void
1913469debe0SVitaly Buka       })",
1914a6927c86SVitaly Buka      true, true},
1915469debe0SVitaly Buka 
1916469debe0SVitaly Buka     {R"(
1917469debe0SVitaly Buka       define void @test(i1 %cond) {
1918469debe0SVitaly Buka         %a = alloca i32
1919469debe0SVitaly Buka         %b = alloca i32
1920469debe0SVitaly Buka         %r = select i1 %cond, i32* %a, i32* %b
1921469debe0SVitaly Buka         ret void
1922469debe0SVitaly Buka       })",
1923a6927c86SVitaly Buka      false, false},
1924469debe0SVitaly Buka 
1925469debe0SVitaly Buka     {R"(
1926469debe0SVitaly Buka       define void @test(i1 %cond) {
19277f1bb326SVitaly Buka       entry:
19287f1bb326SVitaly Buka         %a = alloca i64
19297f1bb326SVitaly Buka         %a32 = bitcast i64* %a to i32*
19307f1bb326SVitaly Buka         br label %bb1
19317f1bb326SVitaly Buka 
19327f1bb326SVitaly Buka       bb1:
19337f1bb326SVitaly Buka         %x = phi i32* [ %a32, %entry ], [ %x, %bb1 ]
19347f1bb326SVitaly Buka         %r = getelementptr i32, i32* %x, i32 1
19357f1bb326SVitaly Buka         br i1 %cond, label %bb1, label %exit
19367f1bb326SVitaly Buka 
19377f1bb326SVitaly Buka       exit:
19387f1bb326SVitaly Buka         ret void
19397f1bb326SVitaly Buka       })",
1940a6927c86SVitaly Buka      true, false},
19417f1bb326SVitaly Buka 
19427f1bb326SVitaly Buka     {R"(
19437f1bb326SVitaly Buka       define void @test(i1 %cond) {
19447f1bb326SVitaly Buka       entry:
19457f1bb326SVitaly Buka         %a = alloca i64
19467f1bb326SVitaly Buka         %a32 = bitcast i64* %a to i32*
19477f1bb326SVitaly Buka         br label %bb1
19487f1bb326SVitaly Buka 
19497f1bb326SVitaly Buka       bb1:
19507f1bb326SVitaly Buka         %x = phi i32* [ %a32, %entry ], [ %r, %bb1 ]
19517f1bb326SVitaly Buka         %r = getelementptr i32, i32* %x, i32 1
19527f1bb326SVitaly Buka         br i1 %cond, label %bb1, label %exit
19537f1bb326SVitaly Buka 
19547f1bb326SVitaly Buka       exit:
19557f1bb326SVitaly Buka         ret void
19567f1bb326SVitaly Buka       })",
195723524fdeSVitaly Buka      true, false},
19587f1bb326SVitaly Buka 
19597f1bb326SVitaly Buka     {R"(
19607f1bb326SVitaly Buka       define void @test(i1 %cond, i64* %a) {
19617f1bb326SVitaly Buka       entry:
19627f1bb326SVitaly Buka         %r = bitcast i64* %a to i32*
19637f1bb326SVitaly Buka         ret void
19647f1bb326SVitaly Buka       })",
1965a6927c86SVitaly Buka      false, false},
19667f1bb326SVitaly Buka 
19677f1bb326SVitaly Buka     {R"(
19687f1bb326SVitaly Buka       define void @test(i1 %cond) {
19697f1bb326SVitaly Buka       entry:
19707f1bb326SVitaly Buka         %a = alloca i32
19717f1bb326SVitaly Buka         %b = alloca i32
19727f1bb326SVitaly Buka         br label %bb1
19737f1bb326SVitaly Buka 
19747f1bb326SVitaly Buka       bb1:
19758191603dSVitaly Buka         %r = phi i32* [ %a, %entry ], [ %b, %bb1 ]
19767f1bb326SVitaly Buka         br i1 %cond, label %bb1, label %exit
19777f1bb326SVitaly Buka 
19787f1bb326SVitaly Buka       exit:
19797f1bb326SVitaly Buka         ret void
19807f1bb326SVitaly Buka       })",
1981a6927c86SVitaly Buka      false, false},
19827f1bb326SVitaly Buka };
19837f1bb326SVitaly Buka 
19847f1bb326SVitaly Buka TEST_P(FindAllocaForValueTest, findAllocaForValue) {
19856ccacb46SVitaly Buka   auto M = parseModule(GetParam().IR);
19867f1bb326SVitaly Buka   Function *F = M->getFunction("test");
19877f1bb326SVitaly Buka   Instruction *I = &findInstructionByName(F, "r");
19887f1bb326SVitaly Buka   const AllocaInst *AI = findAllocaForValue(I);
1989a6927c86SVitaly Buka   EXPECT_EQ(!!AI, GetParam().AnyOffsetResult);
1990a6927c86SVitaly Buka }
1991a6927c86SVitaly Buka 
1992a6927c86SVitaly Buka TEST_P(FindAllocaForValueTest, findAllocaForValueZeroOffset) {
1993a6927c86SVitaly Buka   auto M = parseModule(GetParam().IR);
1994a6927c86SVitaly Buka   Function *F = M->getFunction("test");
1995a6927c86SVitaly Buka   Instruction *I = &findInstructionByName(F, "r");
1996a6927c86SVitaly Buka   const AllocaInst *AI = findAllocaForValue(I, true);
1997a6927c86SVitaly Buka   EXPECT_EQ(!!AI, GetParam().ZeroOffsetResult);
19987f1bb326SVitaly Buka }
19997f1bb326SVitaly Buka 
20007f1bb326SVitaly Buka INSTANTIATE_TEST_CASE_P(FindAllocaForValueTest, FindAllocaForValueTest,
20017f1bb326SVitaly Buka                         ::testing::ValuesIn(FindAllocaForValueTests), );
2002