llvm.org GIT mirror llvm / c779e96
Rename the VMCore unittest tree to IR. Somehow was missed when doing the library rename. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@171747 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 6 years ago
30 changed file(s) with 2449 addition(s) and 2449 deletion(s). Raw diff Collapse all Expand all
1111 add_subdirectory(Option)
1212 add_subdirectory(Support)
1313 add_subdirectory(Transforms)
14 add_subdirectory(VMCore)
14 add_subdirectory(IR)
0 set(LLVM_LINK_COMPONENTS
1 asmparser
2 core
3 ipa
4 )
5
6 set(IRSources
7 ConstantsTest.cpp
8 DominatorTreeTest.cpp
9 IRBuilderTest.cpp
10 InstructionsTest.cpp
11 MDBuilderTest.cpp
12 MetadataTest.cpp
13 PassManagerTest.cpp
14 TypeBuilderTest.cpp
15 TypesTest.cpp
16 ValueMapTest.cpp
17 VerifierTest.cpp
18 WaymarkTest.cpp
19 )
20
21 # MSVC9 and 8 cannot compile ValueMapTest.cpp due to their bug.
22 # See issue#331418 in Visual Studio.
23 if(MSVC AND MSVC_VERSION LESS 1600)
24 list(REMOVE_ITEM IRSources ValueMapTest.cpp)
25 endif()
26
27 # HACK: Declare a couple of source files as optionally compiled to satisfy the
28 # missing-file-checker in LLVM's weird CMake build.
29 set(LLVM_OPTIONAL_SOURCES
30 ValueMapTest.cpp
31 )
32
33 add_llvm_unittest(IRTests
34 ${IRSources}
35 )
0 //===- llvm/unittest/IR/ConstantsTest.cpp - Constants unit tests ----------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/IR/Constants.h"
10 #include "llvm/IR/DerivedTypes.h"
11 #include "llvm/IR/InstrTypes.h"
12 #include "llvm/IR/Instruction.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/Module.h"
15 #include "gtest/gtest.h"
16
17 namespace llvm {
18 namespace {
19
20 TEST(ConstantsTest, Integer_i1) {
21 IntegerType* Int1 = IntegerType::get(getGlobalContext(), 1);
22 Constant* One = ConstantInt::get(Int1, 1, true);
23 Constant* Zero = ConstantInt::get(Int1, 0);
24 Constant* NegOne = ConstantInt::get(Int1, static_cast(-1), true);
25 EXPECT_EQ(NegOne, ConstantInt::getSigned(Int1, -1));
26 Constant* Undef = UndefValue::get(Int1);
27
28 // Input: @b = constant i1 add(i1 1 , i1 1)
29 // Output: @b = constant i1 false
30 EXPECT_EQ(Zero, ConstantExpr::getAdd(One, One));
31
32 // @c = constant i1 add(i1 -1, i1 1)
33 // @c = constant i1 false
34 EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, One));
35
36 // @d = constant i1 add(i1 -1, i1 -1)
37 // @d = constant i1 false
38 EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, NegOne));
39
40 // @e = constant i1 sub(i1 -1, i1 1)
41 // @e = constant i1 false
42 EXPECT_EQ(Zero, ConstantExpr::getSub(NegOne, One));
43
44 // @f = constant i1 sub(i1 1 , i1 -1)
45 // @f = constant i1 false
46 EXPECT_EQ(Zero, ConstantExpr::getSub(One, NegOne));
47
48 // @g = constant i1 sub(i1 1 , i1 1)
49 // @g = constant i1 false
50 EXPECT_EQ(Zero, ConstantExpr::getSub(One, One));
51
52 // @h = constant i1 shl(i1 1 , i1 1) ; undefined
53 // @h = constant i1 undef
54 EXPECT_EQ(Undef, ConstantExpr::getShl(One, One));
55
56 // @i = constant i1 shl(i1 1 , i1 0)
57 // @i = constant i1 true
58 EXPECT_EQ(One, ConstantExpr::getShl(One, Zero));
59
60 // @j = constant i1 lshr(i1 1, i1 1) ; undefined
61 // @j = constant i1 undef
62 EXPECT_EQ(Undef, ConstantExpr::getLShr(One, One));
63
64 // @m = constant i1 ashr(i1 1, i1 1) ; undefined
65 // @m = constant i1 undef
66 EXPECT_EQ(Undef, ConstantExpr::getAShr(One, One));
67
68 // @n = constant i1 mul(i1 -1, i1 1)
69 // @n = constant i1 true
70 EXPECT_EQ(One, ConstantExpr::getMul(NegOne, One));
71
72 // @o = constant i1 sdiv(i1 -1, i1 1) ; overflow
73 // @o = constant i1 true
74 EXPECT_EQ(One, ConstantExpr::getSDiv(NegOne, One));
75
76 // @p = constant i1 sdiv(i1 1 , i1 -1); overflow
77 // @p = constant i1 true
78 EXPECT_EQ(One, ConstantExpr::getSDiv(One, NegOne));
79
80 // @q = constant i1 udiv(i1 -1, i1 1)
81 // @q = constant i1 true
82 EXPECT_EQ(One, ConstantExpr::getUDiv(NegOne, One));
83
84 // @r = constant i1 udiv(i1 1, i1 -1)
85 // @r = constant i1 true
86 EXPECT_EQ(One, ConstantExpr::getUDiv(One, NegOne));
87
88 // @s = constant i1 srem(i1 -1, i1 1) ; overflow
89 // @s = constant i1 false
90 EXPECT_EQ(Zero, ConstantExpr::getSRem(NegOne, One));
91
92 // @t = constant i1 urem(i1 -1, i1 1)
93 // @t = constant i1 false
94 EXPECT_EQ(Zero, ConstantExpr::getURem(NegOne, One));
95
96 // @u = constant i1 srem(i1 1, i1 -1) ; overflow
97 // @u = constant i1 false
98 EXPECT_EQ(Zero, ConstantExpr::getSRem(One, NegOne));
99 }
100
101 TEST(ConstantsTest, IntSigns) {
102 IntegerType* Int8Ty = Type::getInt8Ty(getGlobalContext());
103 EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, false)->getSExtValue());
104 EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, true)->getSExtValue());
105 EXPECT_EQ(100, ConstantInt::getSigned(Int8Ty, 100)->getSExtValue());
106 EXPECT_EQ(-50, ConstantInt::get(Int8Ty, 206)->getSExtValue());
107 EXPECT_EQ(-50, ConstantInt::getSigned(Int8Ty, -50)->getSExtValue());
108 EXPECT_EQ(206U, ConstantInt::getSigned(Int8Ty, -50)->getZExtValue());
109
110 // Overflow is handled by truncation.
111 EXPECT_EQ(0x3b, ConstantInt::get(Int8Ty, 0x13b)->getSExtValue());
112 }
113
114 TEST(ConstantsTest, FP128Test) {
115 Type *FP128Ty = Type::getFP128Ty(getGlobalContext());
116
117 IntegerType *Int128Ty = Type::getIntNTy(getGlobalContext(), 128);
118 Constant *Zero128 = Constant::getNullValue(Int128Ty);
119 Constant *X = ConstantExpr::getUIToFP(Zero128, FP128Ty);
120 EXPECT_TRUE(isa(X));
121 }
122
123 #define CHECK(x, y) { \
124 std::string __s; \
125 raw_string_ostream __o(__s); \
126 cast(x)->getAsInstruction()->print(__o); \
127 __o.flush(); \
128 EXPECT_EQ(std::string(" = " y), __s); \
129 }
130
131 TEST(ConstantsTest, AsInstructionsTest) {
132 Module *M = new Module("MyModule", getGlobalContext());
133
134 Type *Int64Ty = Type::getInt64Ty(getGlobalContext());
135 Type *Int32Ty = Type::getInt32Ty(getGlobalContext());
136 Type *Int16Ty = Type::getInt16Ty(getGlobalContext());
137 Type *Int1Ty = Type::getInt1Ty(getGlobalContext());
138 Type *FloatTy = Type::getFloatTy(getGlobalContext());
139 Type *DoubleTy = Type::getDoubleTy(getGlobalContext());
140
141 Constant *Global = M->getOrInsertGlobal("dummy",
142 PointerType::getUnqual(Int32Ty));
143 Constant *Global2 = M->getOrInsertGlobal("dummy2",
144 PointerType::getUnqual(Int32Ty));
145
146 Constant *P0 = ConstantExpr::getPtrToInt(Global, Int32Ty);
147 Constant *P1 = ConstantExpr::getUIToFP(P0, FloatTy);
148 Constant *P2 = ConstantExpr::getUIToFP(P0, DoubleTy);
149 Constant *P3 = ConstantExpr::getTrunc(P0, Int1Ty);
150 Constant *P4 = ConstantExpr::getPtrToInt(Global2, Int32Ty);
151 Constant *P5 = ConstantExpr::getUIToFP(P4, FloatTy);
152 Constant *P6 = ConstantExpr::getBitCast(P4, VectorType::get(Int16Ty, 2));
153
154 Constant *One = ConstantInt::get(Int32Ty, 1);
155
156 #define P0STR "ptrtoint (i32** @dummy to i32)"
157 #define P1STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to float)"
158 #define P2STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to double)"
159 #define P3STR "ptrtoint (i32** @dummy to i1)"
160 #define P4STR "ptrtoint (i32** @dummy2 to i32)"
161 #define P5STR "uitofp (i32 ptrtoint (i32** @dummy2 to i32) to float)"
162 #define P6STR "bitcast (i32 ptrtoint (i32** @dummy2 to i32) to <2 x i16>)"
163
164 CHECK(ConstantExpr::getNeg(P0), "sub i32 0, " P0STR);
165 CHECK(ConstantExpr::getFNeg(P1), "fsub float -0.000000e+00, " P1STR);
166 CHECK(ConstantExpr::getNot(P0), "xor i32 " P0STR ", -1");
167 CHECK(ConstantExpr::getAdd(P0, P0), "add i32 " P0STR ", " P0STR);
168 CHECK(ConstantExpr::getAdd(P0, P0, false, true), "add nsw i32 " P0STR ", "
169 P0STR);
170 CHECK(ConstantExpr::getAdd(P0, P0, true, true), "add nuw nsw i32 " P0STR ", "
171 P0STR);
172 CHECK(ConstantExpr::getFAdd(P1, P1), "fadd float " P1STR ", " P1STR);
173 CHECK(ConstantExpr::getSub(P0, P0), "sub i32 " P0STR ", " P0STR);
174 CHECK(ConstantExpr::getFSub(P1, P1), "fsub float " P1STR ", " P1STR);
175 CHECK(ConstantExpr::getMul(P0, P0), "mul i32 " P0STR ", " P0STR);
176 CHECK(ConstantExpr::getFMul(P1, P1), "fmul float " P1STR ", " P1STR);
177 CHECK(ConstantExpr::getUDiv(P0, P0), "udiv i32 " P0STR ", " P0STR);
178 CHECK(ConstantExpr::getSDiv(P0, P0), "sdiv i32 " P0STR ", " P0STR);
179 CHECK(ConstantExpr::getFDiv(P1, P1), "fdiv float " P1STR ", " P1STR);
180 CHECK(ConstantExpr::getURem(P0, P0), "urem i32 " P0STR ", " P0STR);
181 CHECK(ConstantExpr::getSRem(P0, P0), "srem i32 " P0STR ", " P0STR);
182 CHECK(ConstantExpr::getFRem(P1, P1), "frem float " P1STR ", " P1STR);
183 CHECK(ConstantExpr::getAnd(P0, P0), "and i32 " P0STR ", " P0STR);
184 CHECK(ConstantExpr::getOr(P0, P0), "or i32 " P0STR ", " P0STR);
185 CHECK(ConstantExpr::getXor(P0, P0), "xor i32 " P0STR ", " P0STR);
186 CHECK(ConstantExpr::getShl(P0, P0), "shl i32 " P0STR ", " P0STR);
187 CHECK(ConstantExpr::getShl(P0, P0, true), "shl nuw i32 " P0STR ", " P0STR);
188 CHECK(ConstantExpr::getShl(P0, P0, false, true), "shl nsw i32 " P0STR ", "
189 P0STR);
190 CHECK(ConstantExpr::getLShr(P0, P0, false), "lshr i32 " P0STR ", " P0STR);
191 CHECK(ConstantExpr::getLShr(P0, P0, true), "lshr exact i32 " P0STR ", " P0STR);
192 CHECK(ConstantExpr::getAShr(P0, P0, false), "ashr i32 " P0STR ", " P0STR);
193 CHECK(ConstantExpr::getAShr(P0, P0, true), "ashr exact i32 " P0STR ", " P0STR);
194
195 CHECK(ConstantExpr::getSExt(P0, Int64Ty), "sext i32 " P0STR " to i64");
196 CHECK(ConstantExpr::getZExt(P0, Int64Ty), "zext i32 " P0STR " to i64");
197 CHECK(ConstantExpr::getFPTrunc(P2, FloatTy), "fptrunc double " P2STR
198 " to float");
199 CHECK(ConstantExpr::getFPExtend(P1, DoubleTy), "fpext float " P1STR
200 " to double");
201
202 CHECK(ConstantExpr::getExactUDiv(P0, P0), "udiv exact i32 " P0STR ", " P0STR);
203
204 CHECK(ConstantExpr::getSelect(P3, P0, P4), "select i1 " P3STR ", i32 " P0STR
205 ", i32 " P4STR);
206 CHECK(ConstantExpr::getICmp(CmpInst::ICMP_EQ, P0, P4), "icmp eq i32 " P0STR
207 ", " P4STR);
208 CHECK(ConstantExpr::getFCmp(CmpInst::FCMP_ULT, P1, P5), "fcmp ult float "
209 P1STR ", " P5STR);
210
211 std::vector V;
212 V.push_back(One);
213 // FIXME: getGetElementPtr() actually creates an inbounds ConstantGEP,
214 // not a normal one!
215 //CHECK(ConstantExpr::getGetElementPtr(Global, V, false),
216 // "getelementptr i32** @dummy, i32 1");
217 CHECK(ConstantExpr::getInBoundsGetElementPtr(Global, V),
218 "getelementptr inbounds i32** @dummy, i32 1");
219
220 CHECK(ConstantExpr::getExtractElement(P6, One), "extractelement <2 x i16> "
221 P6STR ", i32 1");
222 }
223
224 #undef CHECK
225
226 } // end anonymous namespace
227 } // end namespace llvm
0 #include "llvm/Analysis/Dominators.h"
1 #include "llvm/Assembly/Parser.h"
2 #include "llvm/IR/Instructions.h"
3 #include "llvm/IR/LLVMContext.h"
4 #include "llvm/IR/Module.h"
5 #include "llvm/PassManager.h"
6 #include "llvm/Support/SourceMgr.h"
7 #include "gtest/gtest.h"
8
9 using namespace llvm;
10
11 namespace llvm {
12 void initializeDPassPass(PassRegistry&);
13
14 namespace {
15 struct DPass : public FunctionPass {
16 static char ID;
17 virtual bool runOnFunction(Function &F) {
18 DominatorTree *DT = &getAnalysis();
19 Function::iterator FI = F.begin();
20
21 BasicBlock *BB0 = FI++;
22 BasicBlock::iterator BBI = BB0->begin();
23 Instruction *Y1 = BBI++;
24 Instruction *Y2 = BBI++;
25 Instruction *Y3 = BBI++;
26
27 BasicBlock *BB1 = FI++;
28 BBI = BB1->begin();
29 Instruction *Y4 = BBI++;
30
31 BasicBlock *BB2 = FI++;
32 BBI = BB2->begin();
33 Instruction *Y5 = BBI++;
34
35 BasicBlock *BB3 = FI++;
36 BBI = BB3->begin();
37 Instruction *Y6 = BBI++;
38 Instruction *Y7 = BBI++;
39
40 BasicBlock *BB4 = FI++;
41 BBI = BB4->begin();
42 Instruction *Y8 = BBI++;
43 Instruction *Y9 = BBI++;
44
45 // Reachability
46 EXPECT_TRUE(DT->isReachableFromEntry(BB0));
47 EXPECT_TRUE(DT->isReachableFromEntry(BB1));
48 EXPECT_TRUE(DT->isReachableFromEntry(BB2));
49 EXPECT_FALSE(DT->isReachableFromEntry(BB3));
50 EXPECT_TRUE(DT->isReachableFromEntry(BB4));
51
52 // BB dominance
53 EXPECT_TRUE(DT->dominates(BB0, BB0));
54 EXPECT_TRUE(DT->dominates(BB0, BB1));
55 EXPECT_TRUE(DT->dominates(BB0, BB2));
56 EXPECT_TRUE(DT->dominates(BB0, BB3));
57 EXPECT_TRUE(DT->dominates(BB0, BB4));
58
59 EXPECT_FALSE(DT->dominates(BB1, BB0));
60 EXPECT_TRUE(DT->dominates(BB1, BB1));
61 EXPECT_FALSE(DT->dominates(BB1, BB2));
62 EXPECT_TRUE(DT->dominates(BB1, BB3));
63 EXPECT_FALSE(DT->dominates(BB1, BB4));
64
65 EXPECT_FALSE(DT->dominates(BB2, BB0));
66 EXPECT_FALSE(DT->dominates(BB2, BB1));
67 EXPECT_TRUE(DT->dominates(BB2, BB2));
68 EXPECT_TRUE(DT->dominates(BB2, BB3));
69 EXPECT_FALSE(DT->dominates(BB2, BB4));
70
71 EXPECT_FALSE(DT->dominates(BB3, BB0));
72 EXPECT_FALSE(DT->dominates(BB3, BB1));
73 EXPECT_FALSE(DT->dominates(BB3, BB2));
74 EXPECT_TRUE(DT->dominates(BB3, BB3));
75 EXPECT_FALSE(DT->dominates(BB3, BB4));
76
77 // BB proper dominance
78 EXPECT_FALSE(DT->properlyDominates(BB0, BB0));
79 EXPECT_TRUE(DT->properlyDominates(BB0, BB1));
80 EXPECT_TRUE(DT->properlyDominates(BB0, BB2));
81 EXPECT_TRUE(DT->properlyDominates(BB0, BB3));
82
83 EXPECT_FALSE(DT->properlyDominates(BB1, BB0));
84 EXPECT_FALSE(DT->properlyDominates(BB1, BB1));
85 EXPECT_FALSE(DT->properlyDominates(BB1, BB2));
86 EXPECT_TRUE(DT->properlyDominates(BB1, BB3));
87
88 EXPECT_FALSE(DT->properlyDominates(BB2, BB0));
89 EXPECT_FALSE(DT->properlyDominates(BB2, BB1));
90 EXPECT_FALSE(DT->properlyDominates(BB2, BB2));
91 EXPECT_TRUE(DT->properlyDominates(BB2, BB3));
92
93 EXPECT_FALSE(DT->properlyDominates(BB3, BB0));
94 EXPECT_FALSE(DT->properlyDominates(BB3, BB1));
95 EXPECT_FALSE(DT->properlyDominates(BB3, BB2));
96 EXPECT_FALSE(DT->properlyDominates(BB3, BB3));
97
98 // Instruction dominance in the same reachable BB
99 EXPECT_FALSE(DT->dominates(Y1, Y1));
100 EXPECT_TRUE(DT->dominates(Y1, Y2));
101 EXPECT_FALSE(DT->dominates(Y2, Y1));
102 EXPECT_FALSE(DT->dominates(Y2, Y2));
103
104 // Instruction dominance in the same unreachable BB
105 EXPECT_TRUE(DT->dominates(Y6, Y6));
106 EXPECT_TRUE(DT->dominates(Y6, Y7));
107 EXPECT_TRUE(DT->dominates(Y7, Y6));
108 EXPECT_TRUE(DT->dominates(Y7, Y7));
109
110 // Invoke
111 EXPECT_TRUE(DT->dominates(Y3, Y4));
112 EXPECT_FALSE(DT->dominates(Y3, Y5));
113
114 // Phi
115 EXPECT_TRUE(DT->dominates(Y2, Y9));
116 EXPECT_FALSE(DT->dominates(Y3, Y9));
117 EXPECT_FALSE(DT->dominates(Y8, Y9));
118
119 // Anything dominates unreachable
120 EXPECT_TRUE(DT->dominates(Y1, Y6));
121 EXPECT_TRUE(DT->dominates(Y3, Y6));
122
123 // Unreachable doesn't dominate reachable
124 EXPECT_FALSE(DT->dominates(Y6, Y1));
125
126 // Instruction, BB dominance
127 EXPECT_FALSE(DT->dominates(Y1, BB0));
128 EXPECT_TRUE(DT->dominates(Y1, BB1));
129 EXPECT_TRUE(DT->dominates(Y1, BB2));
130 EXPECT_TRUE(DT->dominates(Y1, BB3));
131 EXPECT_TRUE(DT->dominates(Y1, BB4));
132
133 EXPECT_FALSE(DT->dominates(Y3, BB0));
134 EXPECT_TRUE(DT->dominates(Y3, BB1));
135 EXPECT_FALSE(DT->dominates(Y3, BB2));
136 EXPECT_TRUE(DT->dominates(Y3, BB3));
137 EXPECT_FALSE(DT->dominates(Y3, BB4));
138
139 EXPECT_TRUE(DT->dominates(Y6, BB3));
140
141 return false;
142 }
143 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
144 AU.addRequired();
145 }
146 DPass() : FunctionPass(ID) {
147 initializeDPassPass(*PassRegistry::getPassRegistry());
148 }
149 };
150 char DPass::ID = 0;
151
152
153 Module* makeLLVMModule(DPass *P) {
154 const char *ModuleStrig =
155 "declare i32 @g()\n" \
156 "define void @f(i32 %x) {\n" \
157 "bb0:\n" \
158 " %y1 = add i32 %x, 1\n" \
159 " %y2 = add i32 %x, 1\n" \
160 " %y3 = invoke i32 @g() to label %bb1 unwind label %bb2\n" \
161 "bb1:\n" \
162 " %y4 = add i32 %x, 1\n" \
163 " br label %bb4\n" \
164 "bb2:\n" \
165 " %y5 = landingpad i32 personality i32 ()* @g\n" \
166 " cleanup\n" \
167 " br label %bb4\n" \
168 "bb3:\n" \
169 " %y6 = add i32 %x, 1\n" \
170 " %y7 = add i32 %x, 1\n" \
171 " ret void\n" \
172 "bb4:\n" \
173 " %y8 = phi i32 [0, %bb2], [%y4, %bb1]\n"
174 " %y9 = phi i32 [0, %bb2], [%y4, %bb1]\n"
175 " ret void\n" \
176 "}\n";
177 LLVMContext &C = getGlobalContext();
178 SMDiagnostic Err;
179 return ParseAssemblyString(ModuleStrig, NULL, Err, C);
180 }
181
182 TEST(DominatorTree, Unreachable) {
183 DPass *P = new DPass();
184 Module *M = makeLLVMModule(P);
185 PassManager Passes;
186 Passes.add(P);
187 Passes.run(*M);
188 }
189 }
190 }
191
192 INITIALIZE_PASS_BEGIN(DPass, "dpass", "dpass", false, false)
193 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
194 INITIALIZE_PASS_END(DPass, "dpass", "dpass", false, false)
0 //===- llvm/unittest/IR/IRBuilderTest.cpp - IRBuilder tests ---------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/IR/IRBuilder.h"
10 #include "llvm/ADT/OwningPtr.h"
11 #include "llvm/IR/BasicBlock.h"
12 #include "llvm/IR/DataLayout.h"
13 #include "llvm/IR/Function.h"
14 #include "llvm/IR/IntrinsicInst.h"
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/IR/MDBuilder.h"
17 #include "llvm/IR/Module.h"
18 #include "gtest/gtest.h"
19
20 using namespace llvm;
21
22 namespace {
23
24 class IRBuilderTest : public testing::Test {
25 protected:
26 virtual void SetUp() {
27 M.reset(new Module("MyModule", getGlobalContext()));
28 FunctionType *FTy = FunctionType::get(Type::getVoidTy(getGlobalContext()),
29 /*isVarArg=*/false);
30 F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
31 BB = BasicBlock::Create(getGlobalContext(), "", F);
32 GV = new GlobalVariable(Type::getFloatTy(getGlobalContext()), true,
33 GlobalValue::ExternalLinkage);
34 }
35
36 virtual void TearDown() {
37 BB = 0;
38 M.reset();
39 }
40
41 OwningPtr M;
42 Function *F;
43 BasicBlock *BB;
44 GlobalVariable *GV;
45 };
46
47 TEST_F(IRBuilderTest, Lifetime) {
48 IRBuilder<> Builder(BB);
49 AllocaInst *Var1 = Builder.CreateAlloca(Builder.getInt8Ty());
50 AllocaInst *Var2 = Builder.CreateAlloca(Builder.getInt32Ty());
51 AllocaInst *Var3 = Builder.CreateAlloca(Builder.getInt8Ty(),
52 Builder.getInt32(123));
53
54 CallInst *Start1 = Builder.CreateLifetimeStart(Var1);
55 CallInst *Start2 = Builder.CreateLifetimeStart(Var2);
56 CallInst *Start3 = Builder.CreateLifetimeStart(Var3, Builder.getInt64(100));
57
58 EXPECT_EQ(Start1->getArgOperand(0), Builder.getInt64(-1));
59 EXPECT_EQ(Start2->getArgOperand(0), Builder.getInt64(-1));
60 EXPECT_EQ(Start3->getArgOperand(0), Builder.getInt64(100));
61
62 EXPECT_EQ(Start1->getArgOperand(1), Var1);
63 EXPECT_NE(Start2->getArgOperand(1), Var2);
64 EXPECT_EQ(Start3->getArgOperand(1), Var3);
65
66 Value *End1 = Builder.CreateLifetimeEnd(Var1);
67 Builder.CreateLifetimeEnd(Var2);
68 Builder.CreateLifetimeEnd(Var3);
69
70 IntrinsicInst *II_Start1 = dyn_cast(Start1);
71 IntrinsicInst *II_End1 = dyn_cast(End1);
72 ASSERT_TRUE(II_Start1 != NULL);
73 EXPECT_EQ(II_Start1->getIntrinsicID(), Intrinsic::lifetime_start);
74 ASSERT_TRUE(II_End1 != NULL);
75 EXPECT_EQ(II_End1->getIntrinsicID(), Intrinsic::lifetime_end);
76 }
77
78 TEST_F(IRBuilderTest, CreateCondBr) {
79 IRBuilder<> Builder(BB);
80 BasicBlock *TBB = BasicBlock::Create(getGlobalContext(), "", F);
81 BasicBlock *FBB = BasicBlock::Create(getGlobalContext(), "", F);
82
83 BranchInst *BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB);
84 TerminatorInst *TI = BB->getTerminator();
85 EXPECT_EQ(BI, TI);
86 EXPECT_EQ(2u, TI->getNumSuccessors());
87 EXPECT_EQ(TBB, TI->getSuccessor(0));
88 EXPECT_EQ(FBB, TI->getSuccessor(1));
89
90 BI->eraseFromParent();
91 MDNode *Weights = MDBuilder(getGlobalContext()).createBranchWeights(42, 13);
92 BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB, Weights);
93 TI = BB->getTerminator();
94 EXPECT_EQ(BI, TI);
95 EXPECT_EQ(2u, TI->getNumSuccessors());
96 EXPECT_EQ(TBB, TI->getSuccessor(0));
97 EXPECT_EQ(FBB, TI->getSuccessor(1));
98 EXPECT_EQ(Weights, TI->getMetadata(LLVMContext::MD_prof));
99 }
100
101 TEST_F(IRBuilderTest, LandingPadName) {
102 IRBuilder<> Builder(BB);
103 LandingPadInst *LP = Builder.CreateLandingPad(Builder.getInt32Ty(),
104 Builder.getInt32(0), 0, "LP");
105 EXPECT_EQ(LP->getName(), "LP");
106 }
107
108 TEST_F(IRBuilderTest, GetIntTy) {
109 IRBuilder<> Builder(BB);
110 IntegerType *Ty1 = Builder.getInt1Ty();
111 EXPECT_EQ(Ty1, IntegerType::get(getGlobalContext(), 1));
112
113 DataLayout* DL = new DataLayout(M.get());
114 IntegerType *IntPtrTy = Builder.getIntPtrTy(DL);
115 unsigned IntPtrBitSize = DL->getPointerSizeInBits(0);
116 EXPECT_EQ(IntPtrTy, IntegerType::get(getGlobalContext(), IntPtrBitSize));
117 }
118
119 TEST_F(IRBuilderTest, FastMathFlags) {
120 IRBuilder<> Builder(BB);
121 Value *F;
122 Instruction *FDiv, *FAdd;
123
124 F = Builder.CreateLoad(GV);
125 F = Builder.CreateFAdd(F, F);
126
127 EXPECT_FALSE(Builder.getFastMathFlags().any());
128 ASSERT_TRUE(isa(F));
129 FAdd = cast(F);
130 EXPECT_FALSE(FAdd->hasNoNaNs());
131
132 FastMathFlags FMF;
133 Builder.SetFastMathFlags(FMF);
134
135 F = Builder.CreateFAdd(F, F);
136 EXPECT_FALSE(Builder.getFastMathFlags().any());
137
138 FMF.setUnsafeAlgebra();
139 Builder.SetFastMathFlags(FMF);
140
141 F = Builder.CreateFAdd(F, F);
142 EXPECT_TRUE(Builder.getFastMathFlags().any());
143 ASSERT_TRUE(isa(F));
144 FAdd = cast(F);
145 EXPECT_TRUE(FAdd->hasNoNaNs());
146
147 F = Builder.CreateFDiv(F, F);
148 EXPECT_TRUE(Builder.getFastMathFlags().any());
149 EXPECT_TRUE(Builder.getFastMathFlags().UnsafeAlgebra);
150 ASSERT_TRUE(isa(F));
151 FDiv = cast(F);
152 EXPECT_TRUE(FDiv->hasAllowReciprocal());
153
154 Builder.clearFastMathFlags();
155
156 F = Builder.CreateFDiv(F, F);
157 ASSERT_TRUE(isa(F));
158 FDiv = cast(F);
159 EXPECT_FALSE(FDiv->hasAllowReciprocal());
160
161 FMF.clear();
162 FMF.setAllowReciprocal();
163 Builder.SetFastMathFlags(FMF);
164
165 F = Builder.CreateFDiv(F, F);
166 EXPECT_TRUE(Builder.getFastMathFlags().any());
167 EXPECT_TRUE(Builder.getFastMathFlags().AllowReciprocal);
168 ASSERT_TRUE(isa(F));
169 FDiv = cast(F);
170 EXPECT_TRUE(FDiv->hasAllowReciprocal());
171
172 Builder.clearFastMathFlags();
173
174 F = Builder.CreateFDiv(F, F);
175 ASSERT_TRUE(isa(F));
176 FDiv = cast(F);
177 EXPECT_FALSE(FDiv->getFastMathFlags().any());
178 FDiv->copyFastMathFlags(FAdd);
179 EXPECT_TRUE(FDiv->hasNoNaNs());
180
181 }
182
183 }
0 //===- llvm/unittest/IR/InstructionsTest.cpp - Instructions unit tests ----===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/IR/Instructions.h"
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/Analysis/ValueTracking.h"
12 #include "llvm/IR/BasicBlock.h"
13 #include "llvm/IR/Constants.h"
14 #include "llvm/IR/DataLayout.h"
15 #include "llvm/IR/DerivedTypes.h"
16 #include "llvm/IR/IRBuilder.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/IR/MDBuilder.h"
19 #include "llvm/IR/Operator.h"
20 #include "gtest/gtest.h"
21
22 namespace llvm {
23 namespace {
24
25 TEST(InstructionsTest, ReturnInst) {
26 LLVMContext &C(getGlobalContext());
27
28 // test for PR6589
29 const ReturnInst* r0 = ReturnInst::Create(C);
30 EXPECT_EQ(r0->getNumOperands(), 0U);
31 EXPECT_EQ(r0->op_begin(), r0->op_end());
32
33 IntegerType* Int1 = IntegerType::get(C, 1);
34 Constant* One = ConstantInt::get(Int1, 1, true);
35 const ReturnInst* r1 = ReturnInst::Create(C, One);
36 EXPECT_EQ(1U, r1->getNumOperands());
37 User::const_op_iterator b(r1->op_begin());
38 EXPECT_NE(r1->op_end(), b);
39 EXPECT_EQ(One, *b);
40 EXPECT_EQ(One, r1->getOperand(0));
41 ++b;
42 EXPECT_EQ(r1->op_end(), b);
43
44 // clean up
45 delete r0;
46 delete r1;
47 }
48
49 TEST(InstructionsTest, BranchInst) {
50 LLVMContext &C(getGlobalContext());
51
52 // Make a BasicBlocks
53 BasicBlock* bb0 = BasicBlock::Create(C);
54 BasicBlock* bb1 = BasicBlock::Create(C);
55
56 // Mandatory BranchInst
57 const BranchInst* b0 = BranchInst::Create(bb0);
58
59 EXPECT_TRUE(b0->isUnconditional());
60 EXPECT_FALSE(b0->isConditional());
61 EXPECT_EQ(1U, b0->getNumSuccessors());
62
63 // check num operands
64 EXPECT_EQ(1U, b0->getNumOperands());
65
66 EXPECT_NE(b0->op_begin(), b0->op_end());
67 EXPECT_EQ(b0->op_end(), llvm::next(b0->op_begin()));
68
69 EXPECT_EQ(b0->op_end(), llvm::next(b0->op_begin()));
70
71 IntegerType* Int1 = IntegerType::get(C, 1);
72 Constant* One = ConstantInt::get(Int1, 1, true);
73
74 // Conditional BranchInst
75 BranchInst* b1 = BranchInst::Create(bb0, bb1, One);
76
77 EXPECT_FALSE(b1->isUnconditional());
78 EXPECT_TRUE(b1->isConditional());
79 EXPECT_EQ(2U, b1->getNumSuccessors());
80
81 // check num operands
82 EXPECT_EQ(3U, b1->getNumOperands());
83
84 User::const_op_iterator b(b1->op_begin());
85
86 // check COND
87 EXPECT_NE(b, b1->op_end());
88 EXPECT_EQ(One, *b);
89 EXPECT_EQ(One, b1->getOperand(0));
90 EXPECT_EQ(One, b1->getCondition());
91 ++b;
92
93 // check ELSE
94 EXPECT_EQ(bb1, *b);
95 EXPECT_EQ(bb1, b1->getOperand(1));
96 EXPECT_EQ(bb1, b1->getSuccessor(1));
97 ++b;
98
99 // check THEN
100 EXPECT_EQ(bb0, *b);
101 EXPECT_EQ(bb0, b1->getOperand(2));
102 EXPECT_EQ(bb0, b1->getSuccessor(0));
103 ++b;
104
105 EXPECT_EQ(b1->op_end(), b);
106
107 // clean up
108 delete b0;
109 delete b1;
110
111 delete bb0;
112 delete bb1;
113 }
114
115 TEST(InstructionsTest, CastInst) {
116 LLVMContext &C(getGlobalContext());
117
118 Type* Int8Ty = Type::getInt8Ty(C);
119 Type* Int64Ty = Type::getInt64Ty(C);
120 Type* V8x8Ty = VectorType::get(Int8Ty, 8);
121 Type* V8x64Ty = VectorType::get(Int64Ty, 8);
122 Type* X86MMXTy = Type::getX86_MMXTy(C);
123
124 const Constant* c8 = Constant::getNullValue(V8x8Ty);
125 const Constant* c64 = Constant::getNullValue(V8x64Ty);
126
127 EXPECT_TRUE(CastInst::isCastable(V8x8Ty, X86MMXTy));
128 EXPECT_TRUE(CastInst::isCastable(X86MMXTy, V8x8Ty));
129 EXPECT_FALSE(CastInst::isCastable(Int64Ty, X86MMXTy));
130 EXPECT_TRUE(CastInst::isCastable(V8x64Ty, V8x8Ty));
131 EXPECT_TRUE(CastInst::isCastable(V8x8Ty, V8x64Ty));
132 EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true));
133 EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true));
134 }
135
136
137
138 TEST(InstructionsTest, VectorGep) {
139 LLVMContext &C(getGlobalContext());
140
141 // Type Definitions
142 PointerType *Ptri8Ty = PointerType::get(IntegerType::get(C, 8), 0);
143 PointerType *Ptri32Ty = PointerType::get(IntegerType::get(C, 8), 0);
144
145 VectorType *V2xi8PTy = VectorType::get(Ptri8Ty, 2);
146 VectorType *V2xi32PTy = VectorType::get(Ptri32Ty, 2);
147
148 // Test different aspects of the vector-of-pointers type
149 // and GEPs which use this type.
150 ConstantInt *Ci32a = ConstantInt::get(C, APInt(32, 1492));
151 ConstantInt *Ci32b = ConstantInt::get(C, APInt(32, 1948));
152 std::vector ConstVa(2, Ci32a);
153 std::vector ConstVb(2, Ci32b);
154 Constant *C2xi32a = ConstantVector::get(ConstVa);
155 Constant *C2xi32b = ConstantVector::get(ConstVb);
156
157 CastInst *PtrVecA = new IntToPtrInst(C2xi32a, V2xi32PTy);
158 CastInst *PtrVecB = new IntToPtrInst(C2xi32b, V2xi32PTy);
159
160 ICmpInst *ICmp0 = new ICmpInst(ICmpInst::ICMP_SGT, PtrVecA, PtrVecB);
161 ICmpInst *ICmp1 = new ICmpInst(ICmpInst::ICMP_ULT, PtrVecA, PtrVecB);
162 EXPECT_NE(ICmp0, ICmp1); // suppress warning.
163
164 GetElementPtrInst *Gep0 = GetElementPtrInst::Create(PtrVecA, C2xi32a);
165 GetElementPtrInst *Gep1 = GetElementPtrInst::Create(PtrVecA, C2xi32b);
166 GetElementPtrInst *Gep2 = GetElementPtrInst::Create(PtrVecB, C2xi32a);
167 GetElementPtrInst *Gep3 = GetElementPtrInst::Create(PtrVecB, C2xi32b);
168
169 CastInst *BTC0 = new BitCastInst(Gep0, V2xi8PTy);
170 CastInst *BTC1 = new BitCastInst(Gep1, V2xi8PTy);
171 CastInst *BTC2 = new BitCastInst(Gep2, V2xi8PTy);
172 CastInst *BTC3 = new BitCastInst(Gep3, V2xi8PTy);
173
174 Value *S0 = BTC0->stripPointerCasts();
175 Value *S1 = BTC1->stripPointerCasts();
176 Value *S2 = BTC2->stripPointerCasts();
177 Value *S3 = BTC3->stripPointerCasts();
178
179 EXPECT_NE(S0, Gep0);
180 EXPECT_NE(S1, Gep1);
181 EXPECT_NE(S2, Gep2);
182 EXPECT_NE(S3, Gep3);
183
184 int64_t Offset;
185 DataLayout TD("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3"
186 "2:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80"
187 ":128:128-n8:16:32:64-S128");
188 // Make sure we don't crash
189 GetPointerBaseWithConstantOffset(Gep0, Offset, TD);
190 GetPointerBaseWithConstantOffset(Gep1, Offset, TD);
191 GetPointerBaseWithConstantOffset(Gep2, Offset, TD);
192 GetPointerBaseWithConstantOffset(Gep3, Offset, TD);
193
194 // Gep of Geps
195 GetElementPtrInst *GepII0 = GetElementPtrInst::Create(Gep0, C2xi32b);
196 GetElementPtrInst *GepII1 = GetElementPtrInst::Create(Gep1, C2xi32a);
197 GetElementPtrInst *GepII2 = GetElementPtrInst::Create(Gep2, C2xi32b);
198 GetElementPtrInst *GepII3 = GetElementPtrInst::Create(Gep3, C2xi32a);
199
200 EXPECT_EQ(GepII0->getNumIndices(), 1u);
201 EXPECT_EQ(GepII1->getNumIndices(), 1u);
202 EXPECT_EQ(GepII2->getNumIndices(), 1u);
203 EXPECT_EQ(GepII3->getNumIndices(), 1u);
204
205 EXPECT_FALSE(GepII0->hasAllZeroIndices());
206 EXPECT_FALSE(GepII1->hasAllZeroIndices());
207 EXPECT_FALSE(GepII2->hasAllZeroIndices());
208 EXPECT_FALSE(GepII3->hasAllZeroIndices());
209
210 delete GepII0;
211 delete GepII1;
212 delete GepII2;
213 delete GepII3;
214
215 delete BTC0;
216 delete BTC1;
217 delete BTC2;
218 delete BTC3;
219
220 delete Gep0;
221 delete Gep1;
222 delete Gep2;
223 delete Gep3;
224
225 delete ICmp0;
226 delete ICmp1;
227 delete PtrVecA;
228 delete PtrVecB;
229 }
230
231 TEST(InstructionsTest, FPMathOperator) {
232 LLVMContext &Context = getGlobalContext();
233 IRBuilder<> Builder(Context);
234 MDBuilder MDHelper(Context);
235 Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0);
236 MDNode *MD1 = MDHelper.createFPMath(1.0);
237 Value *V1 = Builder.CreateFAdd(I, I, "", MD1);
238 EXPECT_TRUE(isa(V1));
239 FPMathOperator *O1 = cast(V1);
240 EXPECT_EQ(O1->getFPAccuracy(), 1.0);
241 delete V1;
242 delete I;
243 }
244
245
246 TEST(InstructionsTest, isEliminableCastPair) {
247 LLVMContext &C(getGlobalContext());
248
249 Type* Int32Ty = Type::getInt32Ty(C);
250 Type* Int64Ty = Type::getInt64Ty(C);
251 Type* Int64PtrTy = Type::getInt64PtrTy(C);
252
253 // Source and destination pointers have same size -> bitcast.
254 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
255 CastInst::IntToPtr,
256 Int64PtrTy, Int64Ty, Int64PtrTy,
257 Int32Ty, 0, Int32Ty),
258 CastInst::BitCast);
259
260 // Source and destination pointers have different sizes -> fail.
261 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
262 CastInst::IntToPtr,
263 Int64PtrTy, Int64Ty, Int64PtrTy,
264 Int32Ty, 0, Int64Ty),
265 0U);
266
267 // Middle pointer big enough -> bitcast.
268 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
269 CastInst::PtrToInt,
270 Int64Ty, Int64PtrTy, Int64Ty,
271 0, Int64Ty, 0),
272 CastInst::BitCast);
273
274 // Middle pointer too small -> fail.
275 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
276 CastInst::PtrToInt,
277 Int64Ty, Int64PtrTy, Int64Ty,
278 0, Int32Ty, 0),
279 0U);
280 }
281
282 } // end anonymous namespace
283 } // end namespace llvm
0 //===- llvm/unittests/MDBuilderTest.cpp - MDBuilder unit tests ------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/IR/MDBuilder.h"
10 #include "llvm/IR/IRBuilder.h"
11 #include "llvm/IR/Operator.h"
12 #include "gtest/gtest.h"
13
14 using namespace llvm;
15
16 namespace {
17
18 class MDBuilderTest : public testing::Test {
19 protected:
20 LLVMContext Context;
21 };
22
23 TEST_F(MDBuilderTest, createString) {
24 MDBuilder MDHelper(Context);
25 MDString *Str0 = MDHelper.createString("");
26 MDString *Str1 = MDHelper.createString("string");
27 EXPECT_EQ(Str0->getString(), StringRef(""));
28 EXPECT_EQ(Str1->getString(), StringRef("string"));
29 }
30 TEST_F(MDBuilderTest, createFPMath) {
31 MDBuilder MDHelper(Context);
32 MDNode *MD0 = MDHelper.createFPMath(0.0);
33 MDNode *MD1 = MDHelper.createFPMath(1.0);
34 EXPECT_EQ(MD0, (MDNode *)0);
35 EXPECT_NE(MD1, (MDNode *)0);
36 EXPECT_EQ(MD1->getNumOperands(), 1U);
37 Value *Op = MD1->getOperand(0);
38 EXPECT_TRUE(isa(Op));
39 EXPECT_TRUE(Op->getType()->isFloatingPointTy());
40 ConstantFP *Val = cast(Op);
41 EXPECT_TRUE(Val->isExactlyValue(1.0));
42 }
43 TEST_F(MDBuilderTest, createRangeMetadata) {
44 MDBuilder MDHelper(Context);
45 APInt A(8, 1), B(8, 2);
46 MDNode *R0 = MDHelper.createRange(A, A);
47 MDNode *R1 = MDHelper.createRange(A, B);
48 EXPECT_EQ(R0, (MDNode *)0);
49 EXPECT_NE(R1, (MDNode *)0);
50 EXPECT_EQ(R1->getNumOperands(), 2U);
51 EXPECT_TRUE(isa(R1->getOperand(0)));
52 EXPECT_TRUE(isa(R1->getOperand(1)));
53 ConstantInt *C0 = cast(R1->getOperand(0));
54 ConstantInt *C1 = cast(R1->getOperand(1));
55 EXPECT_EQ(C0->getValue(), A);
56 EXPECT_EQ(C1->getValue(), B);
57 }
58 TEST_F(MDBuilderTest, createAnonymousTBAARoot) {
59 MDBuilder MDHelper(Context);
60 MDNode *R0 = MDHelper.createAnonymousTBAARoot();
61 MDNode *R1 = MDHelper.createAnonymousTBAARoot();
62 EXPECT_NE(R0, R1);
63 EXPECT_GE(R0->getNumOperands(), 1U);
64 EXPECT_GE(R1->getNumOperands(), 1U);
65 EXPECT_EQ(R0->getOperand(0), R0);
66 EXPECT_EQ(R1->getOperand(0), R1);
67 EXPECT_TRUE(R0->getNumOperands() == 1 || R0->getOperand(1) == 0);
68 EXPECT_TRUE(R1->getNumOperands() == 1 || R1->getOperand(1) == 0);
69 }
70 TEST_F(MDBuilderTest, createTBAARoot) {
71 MDBuilder MDHelper(Context);
72 MDNode *R0 = MDHelper.createTBAARoot("Root");
73 MDNode *R1 = MDHelper.createTBAARoot("Root");
74 EXPECT_EQ(R0, R1);
75 EXPECT_GE(R0->getNumOperands(), 1U);
76 EXPECT_TRUE(isa(R0->getOperand(0)));
77 EXPECT_EQ(cast(R0->getOperand(0))->getString(), "Root");
78 EXPECT_TRUE(R0->getNumOperands() == 1 || R0->getOperand(1) == 0);
79 }
80 TEST_F(MDBuilderTest, createTBAANode) {
81 MDBuilder MDHelper(Context);
82 MDNode *R = MDHelper.createTBAARoot("Root");
83 MDNode *N0 = MDHelper.createTBAANode("Node", R);
84 MDNode *N1 = MDHelper.createTBAANode("edoN", R);
85 MDNode *N2 = MDHelper.createTBAANode("Node", R, true);
86 MDNode *N3 = MDHelper.createTBAANode("Node", R);
87 EXPECT_EQ(N0, N3);
88 EXPECT_NE(N0, N1);
89 EXPECT_NE(N0, N2);
90 EXPECT_GE(N0->getNumOperands(), 2U);
91 EXPECT_GE(N1->getNumOperands(), 2U);
92 EXPECT_GE(N2->getNumOperands(), 3U);
93 EXPECT_TRUE(isa(N0->getOperand(0)));
94 EXPECT_TRUE(isa(N1->getOperand(0)));
95 EXPECT_TRUE(isa(N2->getOperand(0)));
96 EXPECT_EQ(cast(N0->getOperand(0))->getString(), "Node");
97 EXPECT_EQ(cast(N1->getOperand(0))->getString(), "edoN");
98 EXPECT_EQ(cast(N2->getOperand(0))->getString(), "Node");
99 EXPECT_EQ(N0->getOperand(1), R);
100 EXPECT_EQ(N1->getOperand(1), R);
101 EXPECT_EQ(N2->getOperand(1), R);
102 EXPECT_TRUE(isa(N2->getOperand(2)));
103 EXPECT_EQ(cast(N2->getOperand(2))->getZExtValue(), 1U);
104 }
105 }
0 ##===- unittests/IR/Makefile -------------------------------*- Makefile -*-===##
1 #
2 # The LLVM Compiler Infrastructure
3 #
4 # This file is distributed under the University of Illinois Open Source
5 # License. See LICENSE.TXT for details.
6 #
7 ##===----------------------------------------------------------------------===##
8
9 LEVEL = ../..
10 TESTNAME = IR
11 LINK_COMPONENTS := core ipa asmparser
12
13 include $(LEVEL)/Makefile.config
14 include $(LLVM_SRC_ROOT)/unittests/Makefile.unittest
0 //===- llvm/unittest/IR/Metadata.cpp - Metadata unit tests ----------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/IR/Metadata.h"
10 #include "llvm/IR/Constants.h"
11 #include "llvm/IR/Instructions.h"
12 #include "llvm/IR/LLVMContext.h"
13 #include "llvm/IR/Module.h"
14 #include "llvm/IR/Type.h"
15 #include "llvm/Support/ValueHandle.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include "gtest/gtest.h"
18 using namespace llvm;
19
20 namespace {
21
22 class MetadataTest : public testing::Test {
23 protected:
24 LLVMContext Context;
25 };
26 typedef MetadataTest MDStringTest;
27
28 // Test that construction of MDString with different value produces different
29 // MDString objects, even with the same string pointer and nulls in the string.
30 TEST_F(MDStringTest, CreateDifferent) {
31 char x[3] = { 'f', 0, 'A' };
32 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
33 x[2] = 'B';
34 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
35 EXPECT_NE(s1, s2);
36 }
37
38 // Test that creation of MDStrings with the same string contents produces the
39 // same MDString object, even with different pointers.
40 TEST_F(MDStringTest, CreateSame) {
41 char x[4] = { 'a', 'b', 'c', 'X' };
42 char y[4] = { 'a', 'b', 'c', 'Y' };
43
44 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
45 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
46 EXPECT_EQ(s1, s2);
47 }
48
49 // Test that MDString prints out the string we fed it.
50 TEST_F(MDStringTest, PrintingSimple) {
51 char *str = new char[13];
52 strncpy(str, "testing 1 2 3", 13);
53 MDString *s = MDString::get(Context, StringRef(str, 13));
54 strncpy(str, "aaaaaaaaaaaaa", 13);
55 delete[] str;
56
57 std::string Str;
58 raw_string_ostream oss(Str);
59 s->print(oss);
60 EXPECT_STREQ("metadata !\"testing 1 2 3\"", oss.str().c_str());
61 }
62
63 // Test printing of MDString with non-printable characters.
64 TEST_F(MDStringTest, PrintingComplex) {
65 char str[5] = {0, '\n', '"', '\\', (char)-1};
66 MDString *s = MDString::get(Context, StringRef(str+0, 5));
67 std::string Str;
68 raw_string_ostream oss(Str);
69 s->print(oss);
70 EXPECT_STREQ("metadata !\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
71 }
72
73 typedef MetadataTest MDNodeTest;
74
75 // Test the two constructors, and containing other Constants.
76 TEST_F(MDNodeTest, Simple) {
77 char x[3] = { 'a', 'b', 'c' };
78 char y[3] = { '1', '2', '3' };
79
80 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
81 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
82 ConstantInt *CI = ConstantInt::get(getGlobalContext(), APInt(8, 0));
83
84 std::vector V;
85 V.push_back(s1);
86 V.push_back(CI);
87 V.push_back(s2);
88
89 MDNode *n1 = MDNode::get(Context, V);
90 Value *const c1 = n1;
91 MDNode *n2 = MDNode::get(Context, c1);
92 Value *const c2 = n2;
93 MDNode *n3 = MDNode::get(Context, V);
94 MDNode *n4 = MDNode::getIfExists(Context, V);
95 MDNode *n5 = MDNode::getIfExists(Context, c1);
96 MDNode *n6 = MDNode::getIfExists(Context, c2);
97 EXPECT_NE(n1, n2);
98 #ifdef ENABLE_MDNODE_UNIQUING
99 EXPECT_EQ(n1, n3);
100 #else
101 (void) n3;
102 #endif
103 EXPECT_EQ(n4, n1);
104 EXPECT_EQ(n5, n2);
105 EXPECT_EQ(n6, (Value*)0);
106
107 EXPECT_EQ(3u, n1->getNumOperands());
108 EXPECT_EQ(s1, n1->getOperand(0));
109 EXPECT_EQ(CI, n1->getOperand(1));
110 EXPECT_EQ(s2, n1->getOperand(2));
111
112 EXPECT_EQ(1u, n2->getNumOperands());
113 EXPECT_EQ(n1, n2->getOperand(0));
114 }
115
116 TEST_F(MDNodeTest, Delete) {
117 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
118 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
119
120 Value *const V = I;
121 MDNode *n = MDNode::get(Context, V);
122 WeakVH wvh = n;
123
124 EXPECT_EQ(n, wvh);
125
126 delete I;
127 }
128
129 TEST(NamedMDNodeTest, Search) {
130 LLVMContext Context;
131 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 1);
132 Constant *C2 = ConstantInt::get(Type::getInt32Ty(Context), 2);
133
134 Value *const V = C;
135 Value *const V2 = C2;
136 MDNode *n = MDNode::get(Context, V);
137 MDNode *n2 = MDNode::get(Context, V2);
138
139 Module M("MyModule", Context);
140 const char *Name = "llvm.NMD1";
141 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
142 NMD->addOperand(n);
143 NMD->addOperand(n2);
144
145 std::string Str;
146 raw_string_ostream oss(Str);
147 NMD->print(oss);
148 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
149 oss.str().c_str());
150 }
151 }
0 //===- llvm/unittest/IR/PassManager.cpp - PassManager unit tests ----------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/PassManager.h"
10 #include "llvm/ADT/SmallVector.h"
11 #include "llvm/Analysis/CallGraphSCCPass.h"
12 #include "llvm/Analysis/LoopInfo.h"
13 #include "llvm/Analysis/LoopPass.h"
14 #include "llvm/Analysis/Verifier.h"
15 #include "llvm/Assembly/PrintModulePass.h"
16 #include "llvm/IR/BasicBlock.h"
17 #include "llvm/IR/CallingConv.h"
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/DataLayout.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/GlobalVariable.h"
23 #include "llvm/IR/InlineAsm.h"
24 #include "llvm/IR/Instructions.h"
25 #include "llvm/IR/LLVMContext.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/Pass.h"
28 #include "llvm/Support/MathExtras.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include "gtest/gtest.h"
31
32 using namespace llvm;
33
34 namespace llvm {
35 void initializeModuleNDMPass(PassRegistry&);
36 void initializeFPassPass(PassRegistry&);
37 void initializeCGPassPass(PassRegistry&);
38 void initializeLPassPass(PassRegistry&);
39 void initializeBPassPass(PassRegistry&);
40
41 namespace {
42 // ND = no deps
43 // NM = no modifications
44 struct ModuleNDNM: public ModulePass {
45 public:
46 static char run;
47 static char ID;
48 ModuleNDNM() : ModulePass(ID) { }
49 virtual bool runOnModule(Module &M) {
50 run++;
51 return false;
52 }
53 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
54 AU.setPreservesAll();
55 }
56 };
57 char ModuleNDNM::ID=0;
58 char ModuleNDNM::run=0;
59
60 struct ModuleNDM : public ModulePass {
61 public:
62 static char run;
63 static char ID;
64 ModuleNDM() : ModulePass(ID) {}
65 virtual bool runOnModule(Module &M) {
66 run++;
67 return true;
68 }
69 };
70 char ModuleNDM::ID=0;
71 char ModuleNDM::run=0;
72
73 struct ModuleNDM2 : public ModulePass {
74 public:
75 static char run;
76 static char ID;
77 ModuleNDM2() : ModulePass(ID) {}
78 virtual bool runOnModule(Module &M) {
79 run++;
80 return true;
81 }
82 };
83 char ModuleNDM2::ID=0;
84 char ModuleNDM2::run=0;
85
86 struct ModuleDNM : public ModulePass {
87 public:
88 static char run;
89 static char ID;
90 ModuleDNM() : ModulePass(ID) {
91 initializeModuleNDMPass(*PassRegistry::getPassRegistry());
92 }
93 virtual bool runOnModule(Module &M) {
94 EXPECT_TRUE(getAnalysisIfAvailable());
95 run++;
96 return false;
97 }
98 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
99 AU.addRequired();
100 AU.setPreservesAll();
101 }
102 };
103 char ModuleDNM::ID=0;
104 char ModuleDNM::run=0;
105
106 template
107 struct PassTestBase : public P {
108 protected:
109 static int runc;
110 static bool initialized;
111 static bool finalized;
112 int allocated;
113 void run() {
114 EXPECT_TRUE(initialized);
115 EXPECT_FALSE(finalized);
116 EXPECT_EQ(0, allocated);
117 allocated++;
118 runc++;
119 }
120 public:
121 static char ID;
122 static void finishedOK(int run) {
123 EXPECT_GT(runc, 0);
124 EXPECT_TRUE(initialized);
125 EXPECT_TRUE(finalized);
126 EXPECT_EQ(run, runc);
127 }
128 PassTestBase() : P(ID), allocated(0) {
129 initialized = false;
130 finalized = false;
131 runc = 0;
132 }
133
134 virtual void releaseMemory() {
135 EXPECT_GT(runc, 0);
136 EXPECT_GT(allocated, 0);
137 allocated--;
138 }
139 };
140 template char PassTestBase

::ID;

141 template int PassTestBase

::runc;

142 template bool PassTestBase

::initialized;

143 template bool PassTestBase

::finalized;

144
145 template
146 struct PassTest : public PassTestBase

{

147 public:
148 #ifndef _MSC_VER // MSVC complains that Pass is not base class.
149 using llvm::Pass::doInitialization;
150 using llvm::Pass::doFinalization;
151 #endif
152 virtual bool doInitialization(T &t) {
153 EXPECT_FALSE(PassTestBase

::initialized);

154 PassTestBase

::initialized = true;

155 return false;
156 }
157 virtual bool doFinalization(T &t) {
158 EXPECT_FALSE(PassTestBase

::finalized);

159 PassTestBase

::finalized = true;

160 EXPECT_EQ(0, PassTestBase

::allocated);

161 return false;
162 }
163 };
164
165 struct CGPass : public PassTest {
166 public:
167 CGPass() {
168 initializeCGPassPass(*PassRegistry::getPassRegistry());
169 }
170 virtual bool runOnSCC(CallGraphSCC &SCMM) {
171 EXPECT_TRUE(getAnalysisIfAvailable());
172 run();
173 return false;
174 }
175 };
176
177 struct FPass : public PassTest {
178 public:
179 virtual bool runOnFunction(Function &F) {
180 // FIXME: PR4112
181 // EXPECT_TRUE(getAnalysisIfAvailable());
182 run();
183 return false;
184 }
185 };
186
187 struct LPass : public PassTestBase {
188 private:
189 static int initcount;
190 static int fincount;
191 public:
192 LPass() {
193 initializeLPassPass(*PassRegistry::getPassRegistry());
194 initcount = 0; fincount=0;
195 EXPECT_FALSE(initialized);
196 }
197 static void finishedOK(int run, int finalized) {
198 PassTestBase::finishedOK(run);
199 EXPECT_EQ(run, initcount);
200 EXPECT_EQ(finalized, fincount);
201 }
202 using llvm::Pass::doInitialization;
203 using llvm::Pass::doFinalization;
204 virtual bool doInitialization(Loop* L, LPPassManager &LPM) {
205 initialized = true;
206 initcount++;
207 return false;
208 }
209 virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
210 EXPECT_TRUE(getAnalysisIfAvailable());
211 run();
212 return false;
213 }
214 virtual bool doFinalization() {
215 fincount++;
216 finalized = true;
217 return false;
218 }
219 };
220 int LPass::initcount=0;
221 int LPass::fincount=0;
222
223 struct BPass : public PassTestBase {
224 private:
225 static int inited;
226 static int fin;
227 public:
228 static void finishedOK(int run, int N) {
229 PassTestBase::finishedOK(run);
230 EXPECT_EQ(inited, N);
231 EXPECT_EQ(fin, N);
232 }
233 BPass() {
234 inited = 0;
235 fin = 0;
236 }
237 virtual bool doInitialization(Module &M) {
238 EXPECT_FALSE(initialized);
239 initialized = true;
240 return false;
241 }
242 virtual bool doInitialization(Function &F) {
243 inited++;
244 return false;
245 }
246 virtual bool runOnBasicBlock(BasicBlock &BB) {
247 EXPECT_TRUE(getAnalysisIfAvailable());
248 run();
249 return false;
250 }
251 virtual bool doFinalization(Function &F) {
252 fin++;
253 return false;
254 }
255 virtual bool doFinalization(Module &M) {
256 EXPECT_FALSE(finalized);
257 finalized = true;
258 EXPECT_EQ(0, allocated);
259 return false;
260 }
261 };
262 int BPass::inited=0;
263 int BPass::fin=0;
264
265 struct OnTheFlyTest: public ModulePass {
266 public:
267 static char ID;
268 OnTheFlyTest() : ModulePass(ID) {
269 initializeFPassPass(*PassRegistry::getPassRegistry());
270 }
271 virtual bool runOnModule(Module &M) {
272 EXPECT_TRUE(getAnalysisIfAvailable());
273 for (Module::iterator I=M.begin(),E=M.end(); I != E; ++I) {
274 Function &F = *I;
275 {
276 SCOPED_TRACE("Running on the fly function pass");
277 getAnalysis(F);
278 }
279 }
280 return false;
281 }
282 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
283 AU.addRequired();
284 }
285 };
286 char OnTheFlyTest::ID=0;
287
288 TEST(PassManager, RunOnce) {
289 Module M("test-once", getGlobalContext());
290 struct ModuleNDNM *mNDNM = new ModuleNDNM();
291 struct ModuleDNM *mDNM = new ModuleDNM();
292 struct ModuleNDM *mNDM = new ModuleNDM();
293 struct ModuleNDM2 *mNDM2 = new ModuleNDM2();
294
295 mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0;
296
297 PassManager Passes;
298 Passes.add(new DataLayout(&M));
299 Passes.add(mNDM2);
300 Passes.add(mNDM);
301 Passes.add(mNDNM);
302 Passes.add(mDNM);
303
304 Passes.run(M);
305 // each pass must be run exactly once, since nothing invalidates them
306 EXPECT_EQ(1, mNDM->run);
307 EXPECT_EQ(1, mNDNM->run);
308 EXPECT_EQ(1, mDNM->run);
309 EXPECT_EQ(1, mNDM2->run);
310 }
311
312 TEST(PassManager, ReRun) {
313 Module M("test-rerun", getGlobalContext());
314 struct ModuleNDNM *mNDNM = new ModuleNDNM();
315 struct ModuleDNM *mDNM = new ModuleDNM();
316 struct ModuleNDM *mNDM = new ModuleNDM();
317 struct ModuleNDM2 *mNDM2 = new ModuleNDM2();
318
319 mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0;
320
321 PassManager Passes;
322 Passes.add(new DataLayout(&M));
323 Passes.add(mNDM);
324 Passes.add(mNDNM);
325 Passes.add(mNDM2);// invalidates mNDM needed by mDNM
326 Passes.add(mDNM);
327
328 Passes.run(M);
329 // Some passes must be rerun because a pass that modified the
330 // module/function was run in between
331 EXPECT_EQ(2, mNDM->run);
332 EXPECT_EQ(1, mNDNM->run);
333 EXPECT_EQ(1, mNDM2->run);
334 EXPECT_EQ(1, mDNM->run);
335 }
336
337 Module* makeLLVMModule();
338
339 template
340 void MemoryTestHelper(int run) {
341 OwningPtr M(makeLLVMModule());
342 T *P = new T();
343 PassManager Passes;
344 Passes.add(new DataLayout(M.get()));
345 Passes.add(P);
346 Passes.run(*M);
347 T::finishedOK(run);
348 }
349
350 template
351 void MemoryTestHelper(int run, int N) {
352 Module *M = makeLLVMModule();
353 T *P = new T();
354 PassManager Passes;
355 Passes.add(new DataLayout(M));
356 Passes.add(P);
357 Passes.run(*M);
358 T::finishedOK(run, N);
359 delete M;
360 }
361
362 TEST(PassManager, Memory) {
363 // SCC#1: test1->test2->test3->test1
364 // SCC#2: test4
365 // SCC#3: indirect call node
366 {
367 SCOPED_TRACE("Callgraph pass");
368 MemoryTestHelper(3);
369 }
370
371 {
372 SCOPED_TRACE("Function pass");
373 MemoryTestHelper(4);// 4 functions
374 }
375
376 {
377 SCOPED_TRACE("Loop pass");
378 MemoryTestHelper(2, 1); //2 loops, 1 function
379 }
380 {
381 SCOPED_TRACE("Basic block pass");
382 MemoryTestHelper(7, 4); //9 basic blocks
383 }
384
385 }
386
387 TEST(PassManager, MemoryOnTheFly) {
388 Module *M = makeLLVMModule();
389 {
390 SCOPED_TRACE("Running OnTheFlyTest");
391 struct OnTheFlyTest *O = new OnTheFlyTest();
392 PassManager Passes;
393 Passes.add(new DataLayout(M));
394 Passes.add(O);
395 Passes.run(*M);
396
397 FPass::finishedOK(4);
398 }
399 delete M;
400 }
401
402 Module* makeLLVMModule() {
403 // Module Construction
404 Module* mod = new Module("test-mem", getGlobalContext());
405 mod->setDataLayout("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
406 "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-"
407 "a0:0:64-s0:64:64-f80:128:128");
408 mod->setTargetTriple("x86_64-unknown-linux-gnu");
409
410 // Type Definitions
411 std::vectorFuncTy_0_args;
412 FunctionType* FuncTy_0 = FunctionType::get(
413 /*Result=*/IntegerType::get(getGlobalContext(), 32),
414 /*Params=*/FuncTy_0_args,
415 /*isVarArg=*/false);
416
417 std::vectorFuncTy_2_args;
418 FuncTy_2_args.push_back(IntegerType::get(getGlobalContext(), 1));
419 FunctionType* FuncTy_2 = FunctionType::get(
420 /*Result=*/Type::getVoidTy(getGlobalContext()),
421 /*Params=*/FuncTy_2_args,
422 /*isVarArg=*/false);
423
424
425 // Function Declarations
426
427 Function* func_test1 = Function::Create(
428 /*Type=*/FuncTy_0,
429 /*Linkage=*/GlobalValue::ExternalLinkage,
430 /*Name=*/"test1", mod);
431 func_test1->setCallingConv(CallingConv::C);
432 AttributeSet func_test1_PAL;
433 func_test1->setAttributes(func_test1_PAL);
434
435 Function* func_test2 = Function::Create(
436 /*Type=*/FuncTy_0,
437 /*Linkage=*/GlobalValue::ExternalLinkage,
438 /*Name=*/"test2", mod);
439 func_test2->setCallingConv(CallingConv::C);
440 AttributeSet func_test2_PAL;
441 func_test2->setAttributes(func_test2_PAL);
442
443 Function* func_test3 = Function::Create(
444 /*Type=*/FuncTy_0,
445 /*Linkage=*/GlobalValue::ExternalLinkage,
446 /*Name=*/"test3", mod);
447 func_test3->setCallingConv(CallingConv::C);
448 AttributeSet func_test3_PAL;
449 func_test3->setAttributes(func_test3_PAL);
450
451 Function* func_test4 = Function::Create(
452 /*Type=*/FuncTy_2,
453 /*Linkage=*/GlobalValue::ExternalLinkage,
454 /*Name=*/"test4", mod);
455 func_test4->setCallingConv(CallingConv::C);
456 AttributeSet func_test4_PAL;
457 func_test4->setAttributes(func_test4_PAL);
458
459 // Global Variable Declarations
460
461
462 // Constant Definitions
463
464 // Global Variable Definitions
465
466 // Function Definitions
467
468 // Function: test1 (func_test1)
469 {
470
471 BasicBlock* label_entry = BasicBlock::Create(getGlobalContext(), "entry",func_test1,0);
472
473 // Block entry (label_entry)
474 CallInst* int32_3 = CallInst::Create(func_test2, "", label_entry);
475 int32_3->setCallingConv(CallingConv::C);
476 int32_3->setTailCall(false);AttributeSet int32_3_PAL;
477 int32_3->setAttributes(int32_3_PAL);
478
479 ReturnInst::Create(getGlobalContext(), int32_3, label_entry);
480
481 }
482
483 // Function: test2 (func_test2)
484 {
485
486 BasicBlock* label_entry_5 = BasicBlock::Create(getGlobalContext(), "entry",func_test2,0);
487
488 // Block entry (label_entry_5)
489 CallInst* int32_6 = CallInst::Create(func_test3, "", label_entry_5);
490 int32_6->setCallingConv(CallingConv::C);
491 int32_6->setTailCall(false);AttributeSet int32_6_PAL;
492 int32_6->setAttributes(int32_6_PAL);
493
494 ReturnInst::Create(getGlobalContext(), int32_6, label_entry_5);
495
496 }
497
498 // Function: test3 (func_test3)
499 {
500
501 BasicBlock* label_entry_8 = BasicBlock::Create(getGlobalContext(), "entry",func_test3,0);
502
503 // Block entry (label_entry_8)
504 CallInst* int32_9 = CallInst::Create(func_test1, "", label_entry_8);
505 int32_9->setCallingConv(CallingConv::C);
506 int32_9->setTailCall(false);AttributeSet int32_9_PAL;
507 int32_9->setAttributes(int32_9_PAL);
508
509 ReturnInst::Create(getGlobalContext(), int32_9, label_entry_8);
510
511 }
512
513 // Function: test4 (func_test4)
514 {
515 Function::arg_iterator args = func_test4->arg_begin();
516 Value* int1_f = args++;
517 int1_f->setName("f");
518
519 BasicBlock* label_entry_11 = BasicBlock::Create(getGlobalContext(), "entry",func_test4,0);
520 BasicBlock* label_bb = BasicBlock::Create(getGlobalContext(), "bb",func_test4,0);
521 BasicBlock* label_bb1 = BasicBlock::Create(getGlobalContext(), "bb1",func_test4,0);
522 BasicBlock* label_return = BasicBlock::Create(getGlobalContext(), "return",func_test4,0);
523
524 // Block entry (label_entry_11)
525 BranchInst::Create(label_bb, label_entry_11);
526
527 // Block bb (label_bb)
528 BranchInst::Create(label_bb, label_bb1, int1_f, label_bb);
529
530 // Block bb1 (label_bb1)
531 BranchInst::Create(label_bb1, label_return, int1_f, label_bb1);
532
533 // Block return (label_return)
534 ReturnInst::Create(getGlobalContext(), label_return);
535
536 }
537 return mod;
538 }
539
540 }
541 }
542
543 INITIALIZE_PASS(ModuleNDM, "mndm", "mndm", false, false)
544 INITIALIZE_PASS_BEGIN(CGPass, "cgp","cgp", false, false)
545 INITIALIZE_AG_DEPENDENCY(CallGraph)
546 INITIALIZE_PASS_END(CGPass, "cgp","cgp", false, false)
547 INITIALIZE_PASS(FPass, "fp","fp", false, false)
548 INITIALIZE_PASS_BEGIN(LPass, "lp","lp", false, false)
549 INITIALIZE_PASS_DEPENDENCY(LoopInfo)
550 INITIALIZE_PASS_END(LPass, "lp","lp", false, false)
551 INITIALIZE_PASS(BPass, "bp","bp", false, false)
0 //===- llvm/unittest/TypeBuilderTest.cpp - TypeBuilder tests --------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/IR/TypeBuilder.h"
10 #include "llvm/ADT/ArrayRef.h"
11 #include "llvm/IR/LLVMContext.h"
12 #include "gtest/gtest.h"
13
14 using namespace llvm;
15
16 namespace {
17
18 TEST(TypeBuilderTest, Void) {
19 EXPECT_EQ(Type::getVoidTy(getGlobalContext()), (TypeBuilder::get(getGlobalContext())));
20 EXPECT_EQ(Type::getVoidTy(getGlobalContext()), (TypeBuilder::get(getGlobalContext())));
21 // Special cases for C compatibility:
22 EXPECT_EQ(Type::getInt8PtrTy(getGlobalContext()),
23 (TypeBuilder::get(getGlobalContext())));
24 EXPECT_EQ(Type::getInt8PtrTy(getGlobalContext()),
25 (TypeBuilder::get(getGlobalContext())));
26 EXPECT_EQ(Type::getInt8PtrTy(getGlobalContext()),
27 (TypeBuilder::get(getGlobalContext())));
28 EXPECT_EQ(Type::getInt8PtrTy(getGlobalContext()),
29 (TypeBuilder::get(
30 getGlobalContext())));
31 }
32
33 TEST(TypeBuilderTest, HostIntegers) {
34 EXPECT_EQ(Type::getInt8Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext())));
35 EXPECT_EQ(Type::getInt8Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext())));
36 EXPECT_EQ(Type::getInt16Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext())));
37 EXPECT_EQ(Type::getInt16Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext())));
38 EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext())));
39 EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext())));
40 EXPECT_EQ(Type::getInt64Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext())));
41 EXPECT_EQ(Type::getInt64Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext())));
42
43 EXPECT_EQ(IntegerType::get(getGlobalContext(), sizeof(size_t) * CHAR_BIT),
44 (TypeBuilder::get(getGlobalContext())));
45 EXPECT_EQ(IntegerType::get(getGlobalContext(), sizeof(ptrdiff_t) * CHAR_BIT),
46 (TypeBuilder::get(getGlobalContext())));
47 }
48
49 TEST(TypeBuilderTest, CrossCompilableIntegers) {
50 EXPECT_EQ(IntegerType::get(getGlobalContext(), 1), (TypeBuilder, true>::get(getGlobalContext())));
51 EXPECT_EQ(IntegerType::get(getGlobalContext(), 1), (TypeBuilder, false>::get(getGlobalContext())));
52 EXPECT_EQ(IntegerType::get(getGlobalContext(), 72), (TypeBuilder, true>::get(getGlobalContext())));
53 EXPECT_EQ(IntegerType::get(getGlobalContext(), 72), (TypeBuilder, false>::get(getGlobalContext())));
54 }
55
56 TEST(TypeBuilderTest, Float) {
57 EXPECT_EQ(Type::getFloatTy(getGlobalContext()), (TypeBuilder::get(getGlobalContext())));
58 EXPECT_EQ(Type::getDoubleTy(getGlobalContext()), (TypeBuilder::get(getGlobalContext())));
59 // long double isn't supported yet.
60 EXPECT_EQ(Type::getFloatTy(getGlobalContext()), (TypeBuilder::get(getGlobalContext())));
61 EXPECT_EQ(Type::getFloatTy(getGlobalContext()), (TypeBuilder::get(getGlobalContext())));
62 EXPECT_EQ(Type::getDoubleTy(getGlobalContext()), (TypeBuilder::get(getGlobalContext())));
63 EXPECT_EQ(Type::getDoubleTy(getGlobalContext()), (TypeBuilder::get(getGlobalContext())));
64 EXPECT_EQ(Type::getX86_FP80Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext())));
65 EXPECT_EQ(Type::getX86_FP80Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext())));
66 EXPECT_EQ(Type::getFP128Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext())));
67 EXPECT_EQ(Type::getFP128Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext())));
68 EXPECT_EQ(Type::getPPC_FP128Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext())));
69 EXPECT_EQ(Type::getPPC_FP128Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext())));
70 }
71
72 TEST(TypeBuilderTest, Derived) {
73 EXPECT_EQ(PointerType::getUnqual(Type::getInt8PtrTy(getGlobalContext())),
74 (TypeBuilder::get(getGlobalContext())));
75 EXPECT_EQ(ArrayType::get(Type::getInt8Ty(getGlobalContext()), 7),
76 (TypeBuilder::get(getGlobalContext())));
77 EXPECT_EQ(ArrayType::get(Type::getInt8Ty(getGlobalContext()), 0),
78 (TypeBuilder::get(getGlobalContext())));
79
80 EXPECT_EQ(PointerType::getUnqual(Type::getInt8PtrTy(getGlobalContext())),
81 (TypeBuilder**, false>::get(getGlobalContext())));
82 EXPECT_EQ(ArrayType::get(Type::getInt8Ty(getGlobalContext()), 7),
83 (TypeBuilder[7], false>::get(getGlobalContext())));
84 EXPECT_EQ(ArrayType::get(Type::getInt8Ty(getGlobalContext()), 0),
85 (TypeBuilder[], false>::get(getGlobalContext())));
86
87 EXPECT_EQ(PointerType::getUnqual(Type::getInt8PtrTy(getGlobalContext())),
88 (TypeBuilder**, true>::get(getGlobalContext())));
89 EXPECT_EQ(ArrayType::get(Type::getInt8Ty(getGlobalContext()), 7),
90 (TypeBuilder[7], true>::get(getGlobalContext())));
91 EXPECT_EQ(ArrayType::get(Type::getInt8Ty(getGlobalContext()), 0),
92 (TypeBuilder[], true>::get(getGlobalContext())));
93
94
95 EXPECT_EQ(Type::getInt8Ty(getGlobalContext()),
96 (TypeBuilder::get(getGlobalContext())));
97 EXPECT_EQ(Type::getInt8Ty(getGlobalContext()),
98 (TypeBuilder::get(getGlobalContext())));
99 EXPECT_EQ(Type::getInt8Ty(getGlobalContext()),
100 (TypeBuilder::get(getGlobalContext())));
101
102 EXPECT_EQ(Type::getInt8Ty(getGlobalContext()),
103 (TypeBuilder, false>::get(getGlobalContext())));
104 EXPECT_EQ(Type::getInt8Ty(getGlobalContext()),
105 (TypeBuilder, false>::get(getGlobalContext())));
106 EXPECT_EQ(Type::getInt8Ty(getGlobalContext()),
107 (TypeBuilder, false>::get(getGlobalContext())));
108
109 EXPECT_EQ(Type::getInt8Ty(getGlobalContext()),
110 (TypeBuilder, true>::get(getGlobalContext())));
111 EXPECT_EQ(Type::getInt8Ty(getGlobalContext()),
112 (TypeBuilder, true>::get(getGlobalContext())));
113 EXPECT_EQ(Type::getInt8Ty(getGlobalContext()),
114 (TypeBuilder, true>::get(getGlobalContext())));
115
116 EXPECT_EQ(Type::getInt8PtrTy(getGlobalContext()),
117 (TypeBuilder::get(getGlobalContext())));
118 }
119
120 TEST(TypeBuilderTest, Functions) {
121 std::vector params;
122 EXPECT_EQ(FunctionType::get(Type::getVoidTy(getGlobalContext()), params, false),
123 (TypeBuilder::get(getGlobalContext())));
124 EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, true),
125 (TypeBuilder::get(getGlobalContext())));
126 params.push_back(TypeBuilder::get(getGlobalContext()));
127 EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, false),
128 (TypeBuilder::get(getGlobalContext())));
129 EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, true),
130 (TypeBuilder::get(getGlobalContext())));
131 params.push_back(TypeBuilder::get(getGlobalContext()));
132 EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, false),
133 (TypeBuilder::get(getGlobalContext())));
134 EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, true),
135 (TypeBuilder::get(getGlobalContext())));
136 params.push_back(TypeBuilder::get(getGlobalContext()));
137 EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, false),
138 (TypeBuilder::get(getGlobalContext())));
139 EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, true),
140 (TypeBuilder::get(getGlobalContext())));
141 params.push_back(TypeBuilder::get(getGlobalContext()));
142 EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, false),
143 (TypeBuilder::get(getGlobalContext())));
144 EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, true),
145 (TypeBuilder
146 false>::get(getGlobalContext())));
147 params.push_back(TypeBuilder::get(getGlobalContext()));
148 EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, false),
149 (TypeBuilder
150 false>::get(getGlobalContext())));
151 EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, true),
152 (TypeBuilder
153 false>::get(getGlobalContext())));
154 }
155
156 TEST(TypeBuilderTest, Context) {
157 // We used to cache TypeBuilder results in static local variables. This
158 // produced the same type for different contexts, which of course broke
159 // things.
160 LLVMContext context1;
161 EXPECT_EQ(&context1,
162 &(TypeBuilder, true>::get(context1))->getContext());
163 LLVMContext context2;
164 EXPECT_EQ(&context2,
165 &(TypeBuilder, true>::get(context2))->getContext());
166 }
167
168 struct MyType {
169 int a;
170 int *b;
171 void *array[1];
172 };
173
174 struct MyPortableType {
175 int32_t a;
176 int32_t *b;
177 void *array[1];
178 };
179
180 } // anonymous namespace
181
182 namespace llvm {
183 template class TypeBuilder {
184 public:
185 static StructType *get(LLVMContext &Context) {
186 // Using the static result variable ensures that the type is
187 // only looked up once.
188 std::vector st;
189 st.push_back(TypeBuilder::get(Context));
190 st.push_back(TypeBuilder::get(Context));
191 st.push_back(TypeBuilder::get(Context));
192 static StructType *const result = StructType::get(Context, st);
193 return result;
194 }
195
196 // You may find this a convenient place to put some constants
197 // to help with getelementptr. They don't have any effect on
198 // the operation of TypeBuilder.
199 enum Fields {
200 FIELD_A,
201 FIELD_B,
202 FIELD_ARRAY
203 };
204 };
205
206 template class TypeBuilder {
207 public:
208 static StructType *get(LLVMContext &Context) {
209 // Using the static result variable ensures that the type is
210 // only looked up once.
211 std::vector st;
212 st.push_back(TypeBuilder, cross>::get(Context));
213 st.push_back(TypeBuilder*, cross>::get(Context));
214 st.push_back(TypeBuilder*[], cross>::get(Context));
215 static StructType *const result = StructType::get(Context, st);
216 return result;
217 }
218
219 // You may find this a convenient place to put some constants
220 // to help with getelementptr. They don't have any effect on
221 // the operation of TypeBuilder.
222 enum Fields {
223 FIELD_A,
224 FIELD_B,
225 FIELD_ARRAY
226 };
227 };
228 } // namespace llvm
229 namespace {
230
231 TEST(TypeBuilderTest, Extensions) {
232 EXPECT_EQ(PointerType::getUnqual(StructType::get(
233 TypeBuilder::get(getGlobalContext()),
234 TypeBuilder::get(getGlobalContext()),
235 TypeBuilder::get(getGlobalContext()),
236 (void*)0)),
237 (TypeBuilder::get(getGlobalContext())));
238 EXPECT_EQ(PointerType::getUnqual(StructType::get(
239 TypeBuilder, false>::get(getGlobalContext()),
240 TypeBuilder*, false>::get(getGlobalContext()),
241 TypeBuilder*[], false>::get(getGlobalContext()),
242 (void*)0)),
243 (TypeBuilder::get(getGlobalContext())));
244 EXPECT_EQ(PointerType::getUnqual(StructType::get(
245 TypeBuilder, false>::get(getGlobalContext()),
246 TypeBuilder*, false>::get(getGlobalContext()),
247 TypeBuilder*[], false>::get(getGlobalContext()),
248 (void*)0)),
249 (TypeBuilder::get(getGlobalContext())));
250 }
251
252 } // anonymous namespace
0 //===- llvm/unittest/IR/TypesTest.cpp - Type unit tests -------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/IR/DerivedTypes.h"
10 #include "llvm/IR/LLVMContext.h"
11 #include "gtest/gtest.h"
12 using namespace llvm;
13
14 namespace {
15
16 TEST(TypesTest, StructType) {
17 LLVMContext C;
18
19 // PR13522
20 StructType *Struct = StructType::create(C, "FooBar");
21 EXPECT_EQ("FooBar", Struct->getName());
22 Struct->setName(Struct->getName().substr(0, 3));
23 EXPECT_EQ("Foo", Struct->getName());
24 Struct->setName("");
25 EXPECT_TRUE(Struct->getName().empty());
26 EXPECT_FALSE(Struct->hasName());
27 }
28
29 } // end anonymous namespace
0 //===- llvm/unittest/ADT/ValueMapTest.cpp - ValueMap unit tests -*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/ADT/ValueMap.h"
10 #include "llvm/ADT/OwningPtr.h"
11 #include "llvm/Config/llvm-config.h"
12 #include "llvm/IR/Constants.h"
13 #include "llvm/IR/Instructions.h"
14 #include "llvm/IR/LLVMContext.h"
15 #include "gtest/gtest.h"
16
17 using namespace llvm;
18
19 namespace {
20
21 // Test fixture
22 template
23 class ValueMapTest : public testing::Test {
24 protected:
25 Constant *ConstantV;
26 OwningPtr BitcastV;
27 OwningPtr AddV;
28
29 ValueMapTest() :
30 ConstantV(ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 0)),
31 BitcastV(new BitCastInst(ConstantV, Type::getInt32Ty(getGlobalContext()))),
32 AddV(BinaryOperator::CreateAdd(ConstantV, ConstantV)) {
33 }
34 };
35
36 // Run everything on Value*, a subtype to make sure that casting works as
37 // expected, and a const subtype to make sure we cast const correctly.
38 typedef ::testing::Types KeyTypes;
39 TYPED_TEST_CASE(ValueMapTest, KeyTypes);
40
41 TYPED_TEST(ValueMapTest, Null) {
42 ValueMap VM1;
43 VM1[NULL] = 7;
44 EXPECT_EQ(7, VM1.lookup(NULL));
45 }
46
47 TYPED_TEST(ValueMapTest, FollowsValue) {
48 ValueMap VM;
49 VM[this->BitcastV.get()] = 7;
50 EXPECT_EQ(7, VM.lookup(this->BitcastV.get()));
51 EXPECT_EQ(0, VM.count(this->AddV.get()));
52 this->BitcastV->replaceAllUsesWith(this->AddV.get());
53 EXPECT_EQ(7, VM.lookup(this->AddV.get()));
54 EXPECT_EQ(0, VM.count(this->BitcastV.get()));
55 this->AddV.reset();
56 EXPECT_EQ(0, VM.count(this->AddV.get()));
57 EXPECT_EQ(0, VM.count(this->BitcastV.get()));
58 EXPECT_EQ(0U, VM.size());
59 }
60
61 TYPED_TEST(ValueMapTest, OperationsWork) {
62 ValueMap VM;
63 ValueMap VM2(16); (void)VM2;
64 typename ValueMapConfig::ExtraData Data;
65 ValueMap VM3(Data, 16); (void)VM3;
66 EXPECT_TRUE(VM.empty());
67
68 VM[this->BitcastV.get()] = 7;
69
70 // Find:
71 typename ValueMap::iterator I =
72 VM.find(this->BitcastV.get());
73 ASSERT_TRUE(I != VM.end());
74 EXPECT_EQ(this->BitcastV.get(), I->first);
75 EXPECT_EQ(7, I->second);
76 EXPECT_TRUE(VM.find(this->AddV.get()) == VM.end());
77
78 // Const find:
79 const ValueMap &CVM = VM;
80 typename ValueMap::const_iterator CI =
81 CVM.find(this->BitcastV.get());
82 ASSERT_TRUE(CI != CVM.end());
83 EXPECT_EQ(this->BitcastV.get(), CI->first);
84 EXPECT_EQ(7, CI->second);
85 EXPECT_TRUE(CVM.find(this->AddV.get()) == CVM.end());
86
87 // Insert:
88 std::pair::iterator, bool> InsertResult1 =
89 VM.insert(std::make_pair(this->AddV.get(), 3));
90 EXPECT_EQ(this->AddV.get(), InsertResult1.first->first);
91 EXPECT_EQ(3, InsertResult1.first->second);
92 EXPECT_TRUE(InsertResult1.second);
93 EXPECT_EQ(true, VM.count(this->AddV.get()));
94 std::pair::iterator, bool> InsertResult2 =
95 VM.insert(std::make_pair(this->AddV.get(), 5));
96 EXPECT_EQ(this->AddV.get(), InsertResult2.first->first);
97 EXPECT_EQ(3, InsertResult2.first->second);
98 EXPECT_FALSE(InsertResult2.second);
99
100 // Erase:
101 VM.erase(InsertResult2.first);
102 EXPECT_EQ(0U, VM.count(this->AddV.get()));
103 EXPECT_EQ(1U, VM.count(this->BitcastV.get()));
104 VM.erase(this->BitcastV.get());
105 EXPECT_EQ(0U, VM.count(this->BitcastV.get()));
106 EXPECT_EQ(0U, VM.size());
107
108 // Range insert:
109 SmallVector, 2> Elems;
110 Elems.push_back(std::make_pair(this->AddV.get(), 1));
111 Elems.push_back(std::make_pair(this->BitcastV.get(), 2));
112 VM.insert(Elems.begin(), Elems.end());
113 EXPECT_EQ(1, VM.lookup(this->AddV.get()));
114 EXPECT_EQ(2, VM.lookup(this->BitcastV.get()));
115 }
116
117 template
118 void CompileAssertHasType(VarType) {
119 typedef char assert[is_same::value ? 1 : -1];
120 }
121
122 TYPED_TEST(ValueMapTest, Iteration) {
123 ValueMap VM;
124 VM[this->BitcastV.get()] = 2;
125 VM[this->AddV.get()] = 3;
126 size_t size = 0;
127 for (typename ValueMap::iterator I = VM.begin(), E = VM.end();
128 I != E; ++I) {
129 ++size;
130 std::pair value = *I; (void)value;
131 CompileAssertHasType(I->first);
132 if (I->second == 2) {
133 EXPECT_EQ(this->BitcastV.get(), I->first);
134 I->second = 5;
135 } else if (I->second == 3) {
136 EXPECT_EQ(this->AddV.get(), I->first);
137 I->second = 6;
138 } else {
139 ADD_FAILURE() << "Iterated through an extra value.";
140 }
141 }
142 EXPECT_EQ(2U, size);
143 EXPECT_EQ(5, VM[this->BitcastV.get()]);
144 EXPECT_EQ(6, VM[this->AddV.get()]);
145
146 size = 0;
147 // Cast to const ValueMap to avoid a bug in DenseMap's iterators.
148 const ValueMap& CVM = VM;
149 for (typename ValueMap::const_iterator I = CVM.begin(),
150 E = CVM.end(); I != E; ++I) {
151 ++size;
152 std::pair value = *I; (void)value;
153 CompileAssertHasType(I->first);
154 if (I->second == 5) {
155 EXPECT_EQ(this->BitcastV.get(), I->first);
156 } else if (I->second == 6) {
157 EXPECT_EQ(this->AddV.get(), I->first);
158 } else {
159 ADD_FAILURE() << "Iterated through an extra value.";
160 }
161 }
162 EXPECT_EQ(2U, size);
163 }
164
165 TYPED_TEST(ValueMapTest, DefaultCollisionBehavior) {
166 // By default, we overwrite the old value with the replaced value.
167 ValueMap VM;
168 VM[this->BitcastV.get()] = 7;
169 VM[this->AddV.get()] = 9;
170 this->BitcastV->replaceAllUsesWith(this->AddV.get());
171 EXPECT_EQ(0, VM.count(this->BitcastV.get()));
172 EXPECT_EQ(9, VM.lookup(this->AddV.get()));
173 }
174
175 TYPED_TEST(ValueMapTest, ConfiguredCollisionBehavior) {
176 // TODO: Implement this when someone needs it.
177 }
178
179 template
180 struct LockMutex : ValueMapConfig {
181 struct ExtraData {
182 sys::Mutex *M;
183 bool *CalledRAUW;
184 bool *CalledDeleted;
185 };
186 static void onRAUW(const ExtraData &Data, KeyT Old, KeyT New) {
187 *Data.CalledRAUW = true;
188 EXPECT_FALSE(Data.M->tryacquire()) << "Mutex should already be locked.";
189 }
190 static void onDelete(const ExtraData &Data, KeyT Old) {
191 *Data.CalledDeleted = true;
192 EXPECT_FALSE(Data.M->tryacquire()) << "Mutex should already be locked.";
193 }
194 static sys::Mutex *getMutex(const ExtraData &Data) { return Data.M; }
195 };
196 #if LLVM_ENABLE_THREADS
197 TYPED_TEST(ValueMapTest, LocksMutex) {
198 sys::Mutex M(false); // Not recursive.
199 bool CalledRAUW = false, CalledDeleted = false;
200 typename LockMutex::ExtraData Data =
201 {&M, &CalledRAUW, &CalledDeleted};
202 ValueMap > VM(Data);
203 VM[this->BitcastV.get()] = 7;
204 this->BitcastV->replaceAllUsesWith(this->AddV.get());
205 this->AddV.reset();
206 EXPECT_TRUE(CalledRAUW);
207 EXPECT_TRUE(CalledDeleted);
208 }
209 #endif
210
211 template
212 struct NoFollow : ValueMapConfig {
213 enum { FollowRAUW = false };
214 };
215
216 TYPED_TEST(ValueMapTest, NoFollowRAUW) {
217 ValueMap > VM;
218 VM[this->BitcastV.get()] = 7;
219 EXPECT_EQ(7, VM.lookup(this->BitcastV.get()));
220 EXPECT_EQ(0, VM.count(this->AddV.get()));
221 this->BitcastV->replaceAllUsesWith(this->AddV.get());
222 EXPECT_EQ(7, VM.lookup(this->BitcastV.get()));
223 EXPECT_EQ(0, VM.lookup(this->AddV.get()));
224 this->AddV.reset();
225 EXPECT_EQ(7, VM.lookup(this->BitcastV.get()));
226 EXPECT_EQ(0, VM.lookup(this->AddV.get()));
227 this->BitcastV.reset();
228 EXPECT_EQ(0, VM.lookup(this->BitcastV.get()));
229 EXPECT_EQ(0, VM.lookup(this->AddV.get()));
230 EXPECT_EQ(0U, VM.size());
231 }
232
233 template
234 struct CountOps : ValueMapConfig {
235 struct ExtraData {
236 int *Deletions;
237 int *RAUWs;
238 };
239
240 static void onRAUW(const ExtraData &Data, KeyT Old, KeyT New) {
241 ++*Data.RAUWs;
242 }
243 static void onDelete(const ExtraData &Data, KeyT Old) {
244 ++*Data.Deletions;
245 }
246 };
247
248 TYPED_TEST(ValueMapTest, CallsConfig) {
249 int Deletions = 0, RAUWs = 0;
250 typename CountOps::ExtraData Data = {&Deletions, &RAUWs};
251 ValueMap > VM(Data);
252 VM[this->BitcastV.get()] = 7;
253 this->BitcastV->replaceAllUsesWith(this->AddV.get());
254 EXPECT_EQ(0, Deletions);
255 EXPECT_EQ(1, RAUWs);
256 this->AddV.reset();
257 EXPECT_EQ(1, Deletions);
258 EXPECT_EQ(1, RAUWs);
259 this->BitcastV.reset();
260 EXPECT_EQ(1, Deletions);
261 EXPECT_EQ(1, RAUWs);
262 }
263
264 template
265 struct ModifyingConfig : ValueMapConfig {
266 // We'll put a pointer here back to the ValueMap this key is in, so
267 // that we can modify it (and clobber *this) before the ValueMap
268 // tries to do the same modification. In previous versions of
269 // ValueMap, that exploded.
270 typedef ValueMap > **ExtraData;
271
272 static void onRAUW(ExtraData Map, KeyT Old, KeyT New) {
273 (*Map)->erase(Old);
274 }
275 static void onDelete(ExtraData Map, KeyT Old) {
276 (*Map)->erase(Old);
277 }
278 };
279 TYPED_TEST(ValueMapTest, SurvivesModificationByConfig) {
280 ValueMap > *MapAddress;
281 ValueMap > VM(&MapAddress);
282 MapAddress = &VM;
283 // Now the ModifyingConfig can modify the Map inside a callback.
284 VM[this->BitcastV.get()] = 7;
285 this->BitcastV->replaceAllUsesWith(this->AddV.get());
286 EXPECT_FALSE(VM.count(this->BitcastV.get()));
287 EXPECT_FALSE(VM.count(this->AddV.get()));
288 VM[this->AddV.get()] = 7;
289 this->AddV.reset();
290 EXPECT_FALSE(VM.count(this->AddV.get()));
291 }
292
293 }
0 //===- llvm/unittest/IR/VerifierTest.cpp - Verifier unit tests ------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Analysis/Verifier.h"
10 #include "llvm/ADT/OwningPtr.h"
11 #include "llvm/IR/Constants.h"
12 #include "llvm/IR/DerivedTypes.h"
13 #include "llvm/IR/Function.h"
14 #include "llvm/IR/GlobalAlias.h"
15 #include "llvm/IR/GlobalVariable.h"
16 #include "llvm/IR/Instructions.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/IR/Module.h"
19 #include "gtest/gtest.h"
20
21 namespace llvm {
22 namespace {
23
24 TEST(VerifierTest, Branch_i1) {
25 LLVMContext &C = getGlobalContext();
26 FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), /*isVarArg=*/false);
27 OwningPtr F(Function::Create(FTy, GlobalValue::ExternalLinkage));
28 BasicBlock *Entry = BasicBlock::Create(C, "entry", F.get());
29 BasicBlock *Exit = BasicBlock::Create(C, "exit", F.get());
30 ReturnInst::Create(C, Exit);
31
32 // To avoid triggering an assertion in BranchInst::Create, we first create
33 // a branch with an 'i1' condition ...
34
35 Constant *False = ConstantInt::getFalse(C);
36 BranchInst *BI = BranchInst::Create(Exit, Exit, False, Entry);
37
38 // ... then use setOperand to redirect it to a value of different type.
39
40 Constant *Zero32 = ConstantInt::get(IntegerType::get(C, 32), 0);
41 BI->setOperand(0, Zero32);
42
43 EXPECT_TRUE(verifyFunction(*F, ReturnStatusAction));
44 }
45
46 TEST(VerifierTest, AliasUnnamedAddr) {
47 LLVMContext &C = getGlobalContext();
48 Module M("M", C);
49 Type *Ty = Type::getInt8Ty(C);
50 Constant *Init = Constant::getNullValue(Ty);
51 GlobalVariable *Aliasee = new GlobalVariable(M, Ty, true,
52 GlobalValue::ExternalLinkage,
53 Init, "foo");
54 GlobalAlias *GA = new GlobalAlias(Type::getInt8PtrTy(C),
55 GlobalValue::ExternalLinkage,
56 "bar", Aliasee, &M);
57 GA->setUnnamedAddr(true);
58 std::string Error;
59 EXPECT_TRUE(verifyModule(M, ReturnStatusAction, &Error));
60 EXPECT_TRUE(StringRef(Error).startswith("Alias cannot have unnamed_addr"));
61 }
62 }
63 }
0 //===- llvm/unittest/IR/WaymarkTest.cpp - getUser() unit tests ------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 // we perform white-box tests
10 //
11 #include "llvm/IR/Function.h"
12 #include "llvm/IR/Instructions.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "gtest/gtest.h"
15 #include
16
17 namespace llvm {
18 namespace {
19
20 Constant *char2constant(char c) {
21 return ConstantInt::get(Type::getInt8Ty(getGlobalContext()), c);
22 }
23
24
25 TEST(WaymarkTest, NativeArray) {
26 static uint8_t tail[22] = "s02s33s30y2y0s1x0syxS";
27 Value * values[22];
28 std::transform(tail, tail + 22, values, char2constant);
29 FunctionType *FT = FunctionType::get(Type::getVoidTy(getGlobalContext()), true);
30 Function *F = Function::Create(FT, GlobalValue::ExternalLinkage);
31 const CallInst *A = CallInst::Create(F, makeArrayRef(values));
32 ASSERT_NE(A, (const CallInst*)NULL);
33 ASSERT_EQ(1U + 22, A->getNumOperands());
34 const Use *U = &A->getOperandUse(0);
35 const Use *Ue = &A->getOperandUse(22);
36 for (; U != Ue; ++U)
37 {
38 EXPECT_EQ(A, U->getUser());
39 }
40 }
41
42 TEST(WaymarkTest, TwoBit) {
43 Use* many = (Use*)calloc(sizeof(Use), 8212 + 1);
44 ASSERT_TRUE(many);
45 Use::initTags(many, many + 8212);
46 for (const Use *U = many, *Ue = many + 8212 - 1; U != Ue; ++U)
47 {
48 EXPECT_EQ((User*)(Ue + 1), U->getUser());
49 }
50 }
51
52 } // end anonymous namespace
53 } // end namespace llvm
88
99 LEVEL = ..
1010
11 PARALLEL_DIRS = ADT ExecutionEngine Support Transforms VMCore Analysis Bitcode
11 PARALLEL_DIRS = ADT ExecutionEngine Support Transforms IR Analysis Bitcode
1212
1313 include $(LEVEL)/Makefile.common
1414
+0
-36
unittests/VMCore/CMakeLists.txt less more
None set(LLVM_LINK_COMPONENTS
1 asmparser
2 core
3 ipa
4 )
5
6 set(VMCoreSources
7 ConstantsTest.cpp
8 DominatorTreeTest.cpp
9 IRBuilderTest.cpp
10 InstructionsTest.cpp
11 MDBuilderTest.cpp
12 MetadataTest.cpp
13 PassManagerTest.cpp
14 TypeBuilderTest.cpp
15 TypesTest.cpp
16 ValueMapTest.cpp
17 VerifierTest.cpp
18 WaymarkTest.cpp
19 )
20
21 # MSVC9 and 8 cannot compile ValueMapTest.cpp due to their bug.
22 # See issue#331418 in Visual Studio.
23 if(MSVC AND MSVC_VERSION LESS 1600)
24 list(REMOVE_ITEM VMCoreSources ValueMapTest.cpp)
25 endif()
26
27 # HACK: Declare a couple of source files as optionally compiled to satisfy the
28 # missing-file-checker in LLVM's weird CMake build.
29 set(LLVM_OPTIONAL_SOURCES
30 ValueMapTest.cpp
31 )
32
33 add_llvm_unittest(VMCoreTests
34 ${VMCoreSources}
35 )
+0
-228
unittests/VMCore/ConstantsTest.cpp less more
None //===- llvm/unittest/VMCore/ConstantsTest.cpp - Constants unit tests ------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/IR/Constants.h"
10 #include "llvm/IR/DerivedTypes.h"
11 #include "llvm/IR/InstrTypes.h"
12 #include "llvm/IR/Instruction.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/Module.h"
15 #include "gtest/gtest.h"
16
17 namespace llvm {
18 namespace {
19
20 TEST(ConstantsTest, Integer_i1) {
21 IntegerType* Int1 = IntegerType::get(getGlobalContext(), 1);
22 Constant* One = ConstantInt::get(Int1, 1, true);
23 Constant* Zero = ConstantInt::get(Int1, 0);
24 Constant* NegOne = ConstantInt::get(Int1, static_cast(-1), true);
25 EXPECT_EQ(NegOne, ConstantInt::getSigned(Int1, -1));
26 Constant* Undef = UndefValue::get(Int1);
27
28 // Input: @b = constant i1 add(i1 1 , i1 1)
29 // Output: @b = constant i1 false
30 EXPECT_EQ(Zero, ConstantExpr::getAdd(One, One));
31
32 // @c = constant i1 add(i1 -1, i1 1)
33 // @c = constant i1 false
34 EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, One));
35
36 // @d = constant i1 add(i1 -1, i1 -1)
37 // @d = constant i1 false
38 EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, NegOne));
39
40 // @e = constant i1 sub(i1 -1, i1 1)
41 // @e = constant i1 false
42 EXPECT_EQ(Zero, ConstantExpr::getSub(NegOne, One));
43
44 // @f = constant i1 sub(i1 1 , i1 -1)
45 // @f = constant i1 false
46 EXPECT_EQ(Zero, ConstantExpr::getSub(One, NegOne));
47
48 // @g = constant i1 sub(i1 1 , i1 1)
49 // @g = constant i1 false
50 EXPECT_EQ(Zero, ConstantExpr::getSub(One, One));
51
52 // @h = constant i1 shl(i1 1 , i1 1) ; undefined
53 // @h = constant i1 undef
54 EXPECT_EQ(Undef, ConstantExpr::getShl(One, One));
55
56 // @i = constant i1 shl(i1 1 , i1 0)
57 // @i = constant i1 true
58 EXPECT_EQ(One, ConstantExpr::getShl(One, Zero));
59
60 // @j = constant i1 lshr(i1 1, i1 1) ; undefined
61 // @j = constant i1 undef
62 EXPECT_EQ(Undef, ConstantExpr::getLShr(One, One));
63
64 // @m = constant i1 ashr(i1 1, i1 1) ; undefined
65 // @m = constant i1 undef
66 EXPECT_EQ(Undef, ConstantExpr::getAShr(One, One));
67
68 // @n = constant i1 mul(i1 -1, i1 1)
69 // @n = constant i1 true
70 EXPECT_EQ(One, ConstantExpr::getMul(NegOne, One));
71
72 // @o = constant i1 sdiv(i1 -1, i1 1) ; overflow
73 // @o = constant i1 true
74 EXPECT_EQ(One, ConstantExpr::getSDiv(NegOne, One));
75
76 // @p = constant i1 sdiv(i1 1 , i1 -1); overflow
77 // @p = constant i1 true
78 EXPECT_EQ(One, ConstantExpr::getSDiv(One, NegOne));
79
80 // @q = constant i1 udiv(i1 -1, i1 1)
81 // @q = constant i1 true
82 EXPECT_EQ(One, ConstantExpr::getUDiv(NegOne, One));
83
84 // @r = constant i1 udiv(i1 1, i1 -1)
85 // @r = constant i1 true
86 EXPECT_EQ(One, ConstantExpr::getUDiv(One, NegOne));
87
88 // @s = constant i1 srem(i1 -1, i1 1) ; overflow
89 // @s = constant i1 false
90 EXPECT_EQ(Zero, ConstantExpr::getSRem(NegOne, One));
91
92 // @t = constant i1 urem(i1 -1, i1 1)
93 // @t = constant i1 false
94 EXPECT_EQ(Zero, ConstantExpr::getURem(NegOne, One));
95
96 // @u = constant i1 srem(i1 1, i1 -1) ; overflow
97 // @u = constant i1 false
98 EXPECT_EQ(Zero, ConstantExpr::getSRem(One, NegOne));
99 }
100
101 TEST(ConstantsTest, IntSigns) {
102 IntegerType* Int8Ty = Type::getInt8Ty(getGlobalContext());
103 EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, false)->getSExtValue());
104 EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, true)->getSExtValue());
105 EXPECT_EQ(100, ConstantInt::getSigned(Int8Ty, 100)->getSExtValue());
106 EXPECT_EQ(-50, ConstantInt::get(Int8Ty, 206)->getSExtValue());
107 EXPECT_EQ(-50, ConstantInt::getSigned(Int8Ty, -50)->getSExtValue());
108 EXPECT_EQ(206U, ConstantInt::getSigned(Int8Ty, -50)->getZExtValue());
109
110 // Overflow is handled by truncation.
111 EXPECT_EQ(0x3b, ConstantInt::get(Int8Ty, 0x13b)->getSExtValue());
112 }
113
114 TEST(ConstantsTest, FP128Test) {
115 Type *FP128Ty = Type::getFP128Ty(getGlobalContext());
116
117 IntegerType *Int128Ty = Type::getIntNTy(getGlobalContext(), 128);
118 Constant *Zero128 = Constant::getNullValue(Int128Ty);
119 Constant *X = ConstantExpr::getUIToFP(Zero128, FP128Ty);
120 EXPECT_TRUE(isa(X));
121 }
122
123 #define CHECK(x, y) { \
124 std::string __s; \
125 raw_string_ostream __o(__s); \
126 cast(x)->getAsInstruction()->print(__o); \
127 __o.flush(); \
128 EXPECT_EQ(std::string(" = " y), __s); \
129 }
130
131 TEST(ConstantsTest, AsInstructionsTest) {
132 Module *M = new Module("MyModule", getGlobalContext());
133
134 Type *Int64Ty = Type::getInt64Ty(getGlobalContext());
135 Type *Int32Ty = Type::getInt32Ty(getGlobalContext());
136 Type *Int16Ty = Type::getInt16Ty(getGlobalContext());
137 Type *Int1Ty = Type::getInt1Ty(getGlobalContext());
138 Type *FloatTy = Type::getFloatTy(getGlobalContext());
139 Type *DoubleTy = Type::getDoubleTy(getGlobalContext());
140
141 Constant *Global = M->getOrInsertGlobal("dummy",
142 PointerType::getUnqual(Int32Ty));
143 Constant *Global2 = M->getOrInsertGlobal("dummy2",
144 PointerType::getUnqual(Int32Ty));
145
146 Constant *P0 = ConstantExpr::getPtrToInt(Global, Int32Ty);
147 Constant *P1 = ConstantExpr::getUIToFP(P0, FloatTy);
148 Constant *P2 = ConstantExpr::getUIToFP(P0, DoubleTy);
149 Constant *P3 = ConstantExpr::getTrunc(P0, Int1Ty);
150 Constant *P4 = ConstantExpr::getPtrToInt(Global2, Int32Ty);
151 Constant *P5 = ConstantExpr::getUIToFP(P4, FloatTy);
152 Constant *P6 = ConstantExpr::getBitCast(P4, VectorType::get(Int16Ty, 2));
153
154 Constant *One = ConstantInt::get(Int32Ty, 1);
155
156 #define P0STR "ptrtoint (i32** @dummy to i32)"
157 #define P1STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to float)"
158 #define P2STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to double)"
159 #define P3STR "ptrtoint (i32** @dummy to i1)"
160 #define P4STR "ptrtoint (i32** @dummy2 to i32)"
161 #define P5STR "uitofp (i32 ptrtoint (i32** @dummy2 to i32) to float)"
162 #define P6STR "bitcast (i32 ptrtoint (i32** @dummy2 to i32) to <2 x i16>)"
163
164 CHECK(ConstantExpr::getNeg(P0), "sub i32 0, " P0STR);
165 CHECK(ConstantExpr::getFNeg(P1), "fsub float -0.000000e+00, " P1STR);
166 CHECK(ConstantExpr::getNot(P0), "xor i32 " P0STR ", -1");
167 CHECK(ConstantExpr::getAdd(P0, P0), "add i32 " P0STR ", " P0STR);
168 CHECK(ConstantExpr::getAdd(P0, P0, false, true), "add nsw i32 " P0STR ", "
169 P0STR);
170 CHECK(ConstantExpr::getAdd(P0, P0, true, true), "add nuw nsw i32 " P0STR ", "
171 P0STR);
172 CHECK(ConstantExpr::getFAdd(P1, P1), "fadd float " P1STR ", " P1STR);
173 CHECK(ConstantExpr::getSub(P0, P0), "sub i32 " P0STR ", " P0STR);
174 CHECK(ConstantExpr::getFSub(P1, P1), "fsub float " P1STR ", " P1STR);
175 CHECK(ConstantExpr::getMul(P0, P0), "mul i32 " P0STR ", " P0STR);
176 CHECK(ConstantExpr::getFMul(P1, P1), "fmul float " P1STR ", " P1STR);
177 CHECK(ConstantExpr::getUDiv(P0, P0), "udiv i32 " P0STR ", " P0STR);
178 CHECK(ConstantExpr::getSDiv(P0, P0), "sdiv i32 " P0STR ", " P0STR);
179 CHECK(ConstantExpr::getFDiv(P1, P1), "fdiv float " P1STR ", " P1STR);
180 CHECK(ConstantExpr::getURem(P0, P0), "urem i32 " P0STR ", " P0STR);
181 CHECK(ConstantExpr::getSRem(P0, P0), "srem i32 " P0STR ", " P0STR);
182 CHECK(ConstantExpr::getFRem(P1, P1), "frem float " P1STR ", " P1STR);
183 CHECK(ConstantExpr::getAnd(P0, P0), "and i32 " P0STR ", " P0STR);
184 CHECK(ConstantExpr::getOr(P0, P0), "or i32 " P0STR ", " P0STR);
185 CHECK(ConstantExpr::getXor(P0, P0), "xor i32 " P0STR ", " P0STR);
186 CHECK(ConstantExpr::getShl(P0, P0), "shl i32 " P0STR ", " P0STR);
187 CHECK(ConstantExpr::getShl(P0, P0, true), "shl nuw i32 " P0STR ", " P0STR);
188 CHECK(ConstantExpr::getShl(P0, P0, false, true), "shl nsw i32 " P0STR ", "
189 P0STR);
190 CHECK(ConstantExpr::getLShr(P0, P0, false), "lshr i32 " P0STR ", " P0STR);
191 CHECK(ConstantExpr::getLShr(P0, P0, true), "lshr exact i32 " P0STR ", " P0STR);
192 CHECK(ConstantExpr::getAShr(P0, P0, false), "ashr i32 " P0STR ", " P0STR);
193 CHECK(ConstantExpr::getAShr(P0, P0, true), "ashr exact i32 " P0STR ", " P0STR);
194
195 CHECK(ConstantExpr::getSExt(P0, Int64Ty), "sext i32 " P0STR " to i64");
196 CHECK(ConstantExpr::getZExt(P0, Int64Ty), "zext i32 " P0STR " to i64");
197 CHECK(ConstantExpr::getFPTrunc(P2, FloatTy), "fptrunc double " P2STR
198 " to float");
199 CHECK(ConstantExpr::getFPExtend(P1, DoubleTy), "fpext float " P1STR
200 " to double");
201
202 CHECK(ConstantExpr::getExactUDiv(P0, P0), "udiv exact i32 " P0STR ", " P0STR);
203
204 CHECK(ConstantExpr::getSelect(P3, P0, P4), "select i1 " P3STR ", i32 " P0STR
205 ", i32 " P4STR);
206 CHECK(ConstantExpr::getICmp(CmpInst::ICMP_EQ, P0, P4), "icmp eq i32 " P0STR
207 ", " P4STR);
208 CHECK(ConstantExpr::getFCmp(CmpInst::FCMP_ULT, P1, P5), "fcmp ult float "
209 P1STR ", " P5STR);
210
211 std::vector V;
212 V.push_back(One);
213 // FIXME: getGetElementPtr() actually creates an inbounds ConstantGEP,
214 // not a normal one!
215 //CHECK(ConstantExpr::getGetElementPtr(Global, V, false),
216 // "getelementptr i32** @dummy, i32 1");
217 CHECK(ConstantExpr::getInBoundsGetElementPtr(Global, V),
218 "getelementptr inbounds i32** @dummy, i32 1");
219
220 CHECK(ConstantExpr::getExtractElement(P6, One), "extractelement <2 x i16> "
221 P6STR ", i32 1");
222 }
223
224 #undef CHECK
225
226 } // end anonymous namespace
227 } // end namespace llvm
+0
-195
unittests/VMCore/DominatorTreeTest.cpp less more
None #include "llvm/Analysis/Dominators.h"
1 #include "llvm/Assembly/Parser.h"
2 #include "llvm/IR/Instructions.h"
3 #include "llvm/IR/LLVMContext.h"
4 #include "llvm/IR/Module.h"
5 #include "llvm/PassManager.h"
6 #include "llvm/Support/SourceMgr.h"
7 #include "gtest/gtest.h"
8
9 using namespace llvm;
10
11 namespace llvm {
12 void initializeDPassPass(PassRegistry&);
13
14 namespace {
15 struct DPass : public FunctionPass {
16 static char ID;
17 virtual bool runOnFunction(Function &F) {
18 DominatorTree *DT = &getAnalysis();
19 Function::iterator FI = F.begin();
20
21 BasicBlock *BB0 = FI++;
22 BasicBlock::iterator BBI = BB0->begin();
23 Instruction *Y1 = BBI++;
24 Instruction *Y2 = BBI++;
25 Instruction *Y3 = BBI++;
26
27 BasicBlock *BB1 = FI++;
28 BBI = BB1->begin();
29 Instruction *Y4 = BBI++;
30
31 BasicBlock *BB2 = FI++;
32 BBI = BB2->begin();
33 Instruction *Y5 = BBI++;
34
35 BasicBlock *BB3 = FI++;
36 BBI = BB3->begin();
37 Instruction *Y6 = BBI++;
38 Instruction *Y7 = BBI++;
39
40 BasicBlock *BB4 = FI++;
41 BBI = BB4->begin();
42 Instruction *Y8 = BBI++;
43 Instruction *Y9 = BBI++;
44
45 // Reachability
46 EXPECT_TRUE(DT->isReachableFromEntry(BB0));
47 EXPECT_TRUE(DT->isReachableFromEntry(BB1));
48 EXPECT_TRUE(DT->isReachableFromEntry(BB2));
49 EXPECT_FALSE(DT->isReachableFromEntry(BB3));
50 EXPECT_TRUE(DT->isReachableFromEntry(BB4));
51
52 // BB dominance
53 EXPECT_TRUE(DT->dominates(BB0, BB0));
54 EXPECT_TRUE(DT->dominates(BB0, BB1));
55 EXPECT_TRUE(DT->dominates(BB0, BB2));
56 EXPECT_TRUE(DT->dominates(BB0, BB3));
57 EXPECT_TRUE(DT->dominates(BB0, BB4));
58
59 EXPECT_FALSE(DT->dominates(BB1, BB0));
60 EXPECT_TRUE(DT->dominates(BB1, BB1));
61 EXPECT_FALSE(DT->dominates(BB1, BB2));
62 EXPECT_TRUE(DT->dominates(BB1, BB3));
63 EXPECT_FALSE(DT->dominates(BB1, BB4));
64
65 EXPECT_FALSE(DT->dominates(BB2, BB0));
66 EXPECT_FALSE(DT->dominates(BB2, BB1));
67 EXPECT_TRUE(DT->dominates(BB2, BB2));
68 EXPECT_TRUE(DT->dominates(BB2, BB3));
69 EXPECT_FALSE(DT->dominates(BB2, BB4));
70
71 EXPECT_FALSE(DT->dominates(BB3, BB0));
72 EXPECT_FALSE(DT->dominates(BB3, BB1));
73 EXPECT_FALSE(DT->dominates(BB3, BB2));
74 EXPECT_TRUE(DT->dominates(BB3, BB3));
75 EXPECT_FALSE(DT->dominates(BB3, BB4));
76
77 // BB proper dominance
78 EXPECT_FALSE(DT->properlyDominates(BB0, BB0));
79 EXPECT_TRUE(DT->properlyDominates(BB0, BB1));
80 EXPECT_TRUE(DT->properlyDominates(BB0, BB2));
81 EXPECT_TRUE(DT->properlyDominates(BB0, BB3));
82
83 EXPECT_FALSE(DT->properlyDominates(BB1, BB0));
84 EXPECT_FALSE(DT->properlyDominates(BB1, BB1));
85 EXPECT_FALSE(DT->properlyDominates(BB1, BB2));
86 EXPECT_TRUE(DT->properlyDominates(BB1, BB3));
87
88 EXPECT_FALSE(DT->properlyDominates(BB2, BB0));
89 EXPECT_FALSE(DT->properlyDominates(BB2, BB1));
90 EXPECT_FALSE(DT->properlyDominates(BB2, BB2));
91 EXPECT_TRUE(DT->properlyDominates(BB2, BB3));
92
93 EXPECT_FALSE(DT->properlyDominates(BB3, BB0));
94 EXPECT_FALSE(DT->properlyDominates(BB3, BB1));
95 EXPECT_FALSE(DT->properlyDominates(BB3, BB2));
96 EXPECT_FALSE(DT->properlyDominates(BB3, BB3));
97
98 // Instruction dominance in the same reachable BB
99 EXPECT_FALSE(DT->dominates(Y1, Y1));
100 EXPECT_TRUE(DT->dominates(Y1, Y2));
101 EXPECT_FALSE(DT->dominates(Y2, Y1));
102 EXPECT_FALSE(DT->dominates(Y2, Y2));
103
104 // Instruction dominance in the same unreachable BB
105 EXPECT_TRUE(DT->dominates(Y6, Y6));
106 EXPECT_TRUE(DT->dominates(Y6, Y7));
107 EXPECT_TRUE(DT->dominates(Y7, Y6));
108 EXPECT_TRUE(DT->dominates(Y7, Y7));
109
110 // Invoke
111 EXPECT_TRUE(DT->dominates(Y3, Y4));
112 EXPECT_FALSE(DT->dominates(Y3, Y5));
113
114 // Phi
115 EXPECT_TRUE(DT->dominates(Y2, Y9));
116 EXPECT_FALSE(DT->dominates(Y3, Y9));
117 EXPECT_FALSE(DT->dominates(Y8, Y9));
118
119 // Anything dominates unreachable
120 EXPECT_TRUE(DT->dominates(Y1, Y6));
121 EXPECT_TRUE(DT->dominates(Y3, Y6));
122
123 // Unreachable doesn't dominate reachable
124 EXPECT_FALSE(DT->dominates(Y6, Y1));
125
126 // Instruction, BB dominance
127 EXPECT_FALSE(DT->dominates(Y1, BB0));
128 EXPECT_TRUE(DT->dominates(Y1, BB1));
129 EXPECT_TRUE(DT->dominates(Y1, BB2));
130 EXPECT_TRUE(DT->dominates(Y1, BB3));
131 EXPECT_TRUE(DT->dominates(Y1, BB4));
132
133 EXPECT_FALSE(DT->dominates(Y3, BB0));
134 EXPECT_TRUE(DT->dominates(Y3, BB1));
135 EXPECT_FALSE(DT->dominates(Y3, BB2));
136 EXPECT_TRUE(DT->dominates(Y3, BB3));
137 EXPECT_FALSE(DT->dominates(Y3, BB4));
138
139 EXPECT_TRUE(DT->dominates(Y6, BB3));
140
141 return false;
142 }
143 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
144 AU.addRequired();
145 }
146 DPass() : FunctionPass(ID) {
147 initializeDPassPass(*PassRegistry::getPassRegistry());
148 }
149 };
150 char DPass::ID = 0;
151
152
153 Module* makeLLVMModule(DPass *P) {
154 const char *ModuleStrig =
155 "declare i32 @g()\n" \
156 "define void @f(i32 %x) {\n" \
157 "bb0:\n" \
158 " %y1 = add i32 %x, 1\n" \
159 " %y2 = add i32 %x, 1\n" \
160 " %y3 = invoke i32 @g() to label %bb1 unwind label %bb2\n" \
161 "bb1:\n" \
162 " %y4 = add i32 %x, 1\n" \
163 " br label %bb4\n" \
164 "bb2:\n" \
165 " %y5 = landingpad i32 personality i32 ()* @g\n" \
166 " cleanup\n" \
167 " br label %bb4\n" \
168 "bb3:\n" \
169 " %y6 = add i32 %x, 1\n" \
170 " %y7 = add i32 %x, 1\n" \
171 " ret void\n" \
172 "bb4:\n" \
173 " %y8 = phi i32 [0, %bb2], [%y4, %bb1]\n"
174 " %y9 = phi i32 [0, %bb2], [%y4, %bb1]\n"
175 " ret void\n" \
176 "}\n";
177 LLVMContext &C = getGlobalContext();
178 SMDiagnostic Err;
179 return ParseAssemblyString(ModuleStrig, NULL, Err, C);
180 }
181
182 TEST(DominatorTree, Unreachable) {
183 DPass *P = new DPass();
184 Module *M = makeLLVMModule(P);
185 PassManager Passes;
186 Passes.add(P);
187 Passes.run(*M);
188 }
189 }
190 }
191
192 INITIALIZE_PASS_BEGIN(DPass, "dpass", "dpass", false, false)
193 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
194 INITIALIZE_PASS_END(DPass, "dpass", "dpass", false, false)
+0
-184
unittests/VMCore/IRBuilderTest.cpp less more
None //===- llvm/unittest/VMCore/IRBuilderTest.cpp - IRBuilder tests -----------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/IR/IRBuilder.h"
10 #include "llvm/ADT/OwningPtr.h"
11 #include "llvm/IR/BasicBlock.h"
12 #include "llvm/IR/DataLayout.h"
13 #include "llvm/IR/Function.h"
14 #include "llvm/IR/IntrinsicInst.h"
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/IR/MDBuilder.h"
17 #include "llvm/IR/Module.h"
18 #include "gtest/gtest.h"
19
20 using namespace llvm;
21
22 namespace {
23
24 class IRBuilderTest : public testing::Test {
25 protected:
26 virtual void SetUp() {
27 M.reset(new Module("MyModule", getGlobalContext()));
28 FunctionType *FTy = FunctionType::get(Type::getVoidTy(getGlobalContext()),
29 /*isVarArg=*/false);
30 F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
31 BB = BasicBlock::Create(getGlobalContext(), "", F);
32 GV = new GlobalVariable(Type::getFloatTy(getGlobalContext()), true,
33 GlobalValue::ExternalLinkage);
34 }
35
36 virtual void TearDown() {
37 BB = 0;
38 M.reset();
39 }
40
41 OwningPtr M;
42 Function *F;
43 BasicBlock *BB;
44 GlobalVariable *GV;
45 };
46
47 TEST_F(IRBuilderTest, Lifetime) {
48 IRBuilder<> Builder(BB);
49 AllocaInst *Var1 = Builder.CreateAlloca(Builder.getInt8Ty());
50 AllocaInst *Var2 = Builder.CreateAlloca(Builder.getInt32Ty());
51 AllocaInst *Var3 = Builder.CreateAlloca(Builder.getInt8Ty(),
52 Builder.getInt32(123));
53
54 CallInst *Start1 = Builder.CreateLifetimeStart(Var1);
55 CallInst *Start2 = Builder.CreateLifetimeStart(Var2);
56 CallInst *Start3 = Builder.CreateLifetimeStart(Var3, Builder.getInt64(100));
57
58 EXPECT_EQ(Start1->getArgOperand(0), Builder.getInt64(-1));
59 EXPECT_EQ(Start2->getArgOperand(0), Builder.getInt64(-1));
60 EXPECT_EQ(Start3->getArgOperand(0), Builder.getInt64(100));
61
62 EXPECT_EQ(Start1->getArgOperand(1), Var1);
63 EXPECT_NE(Start2->getArgOperand(1), Var2);
64 EXPECT_EQ(Start3->getArgOperand(1), Var3);
65
66 Value *End1 = Builder.CreateLifetimeEnd(Var1);
67 Builder.CreateLifetimeEnd(Var2);
68 Builder.CreateLifetimeEnd(Var3);
69
70 IntrinsicInst *II_Start1 = dyn_cast(Start1);
71 IntrinsicInst *II_End1 = dyn_cast(End1);
72 ASSERT_TRUE(II_Start1 != NULL);
73 EXPECT_EQ(II_Start1->getIntrinsicID(), Intrinsic::lifetime_start);
74 ASSERT_TRUE(II_End1 != NULL);
75 EXPECT_EQ(II_End1->getIntrinsicID(), Intrinsic::lifetime_end);
76 }
77
78 TEST_F(IRBuilderTest, CreateCondBr) {
79 IRBuilder<> Builder(BB);
80 BasicBlock *TBB = BasicBlock::Create(getGlobalContext(), "", F);
81 BasicBlock *FBB = BasicBlock::Create(getGlobalContext(), "", F);
82
83 BranchInst *BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB);
84 TerminatorInst *TI = BB->getTerminator();
85 EXPECT_EQ(BI, TI);
86 EXPECT_EQ(2u, TI->getNumSuccessors());
87 EXPECT_EQ(TBB, TI->getSuccessor(0));
88 EXPECT_EQ(FBB, TI->getSuccessor(1));
89
90 BI->eraseFromParent();
91 MDNode *Weights = MDBuilder(getGlobalContext()).createBranchWeights(42, 13);
92 BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB, Weights);
93 TI = BB->getTerminator();
94 EXPECT_EQ(BI, TI);
95 EXPECT_EQ(2u, TI->getNumSuccessors());
96 EXPECT_EQ(TBB, TI->getSuccessor(0));
97 EXPECT_EQ(FBB, TI->getSuccessor(1));
98 EXPECT_EQ(Weights, TI->getMetadata(LLVMContext::MD_prof));
99 }
100
101 TEST_F(IRBuilderTest, LandingPadName) {
102 IRBuilder<> Builder(BB);
103 LandingPadInst *LP = Builder.CreateLandingPad(Builder.getInt32Ty(),
104 Builder.getInt32(0), 0, "LP");
105 EXPECT_EQ(LP->getName(), "LP");
106 }
107
108 TEST_F(IRBuilderTest, GetIntTy) {
109 IRBuilder<> Builder(BB);
110 IntegerType *Ty1 = Builder.getInt1Ty();
111 EXPECT_EQ(Ty1, IntegerType::get(getGlobalContext(), 1));
112
113 DataLayout* DL = new DataLayout(M.get());
114 IntegerType *IntPtrTy = Builder.getIntPtrTy(DL);
115 unsigned IntPtrBitSize = DL->getPointerSizeInBits(0);
116 EXPECT_EQ(IntPtrTy, IntegerType::get(getGlobalContext(), IntPtrBitSize));
117 }
118
119 TEST_F(IRBuilderTest, FastMathFlags) {
120 IRBuilder<> Builder(BB);
121 Value *F;
122 Instruction *FDiv, *FAdd;
123
124 F = Builder.CreateLoad(GV);
125 F = Builder.CreateFAdd(F, F);
126
127 EXPECT_FALSE(Builder.getFastMathFlags().any());
128 ASSERT_TRUE(isa(F));
129 FAdd = cast(F);
130 EXPECT_FALSE(FAdd->hasNoNaNs());
131
132 FastMathFlags FMF;
133 Builder.SetFastMathFlags(FMF);
134
135 F = Builder.CreateFAdd(F, F);
136 EXPECT_FALSE(Builder.getFastMathFlags().any());
137
138 FMF.setUnsafeAlgebra();
139 Builder.SetFastMathFlags(FMF);
140
141 F = Builder.CreateFAdd(F, F);
142 EXPECT_TRUE(Builder.getFastMathFlags().any());
143 ASSERT_TRUE(isa(F));
144 FAdd = cast(F);
145 EXPECT_TRUE(FAdd->hasNoNaNs());
146
147 F = Builder.CreateFDiv(F, F);
148 EXPECT_TRUE(Builder.getFastMathFlags().any());
149 EXPECT_TRUE(Builder.getFastMathFlags().UnsafeAlgebra);
150 ASSERT_TRUE(isa(F));
151 FDiv = cast(F);
152 EXPECT_TRUE(FDiv->hasAllowReciprocal());
153
154 Builder.clearFastMathFlags();
155
156 F = Builder.CreateFDiv(F, F);
157 ASSERT_TRUE(isa(F));
158 FDiv = cast(F);
159 EXPECT_FALSE(FDiv->hasAllowReciprocal());
160
161 FMF.clear();
162 FMF.setAllowReciprocal();
163 Builder.SetFastMathFlags(FMF);
164
165 F = Builder.CreateFDiv(F, F);
166 EXPECT_TRUE(Builder.getFastMathFlags().any());
167 EXPECT_TRUE(Builder.getFastMathFlags().AllowReciprocal);
168 ASSERT_TRUE(isa(F));
169 FDiv = cast(F);
170 EXPECT_TRUE(FDiv->hasAllowReciprocal());
171
172 Builder.clearFastMathFlags();
173
174 F = Builder.CreateFDiv(F, F);
175 ASSERT_TRUE(isa(F));
176 FDiv = cast(F);
177 EXPECT_FALSE(FDiv->getFastMathFlags().any());
178 FDiv->copyFastMathFlags(FAdd);
179 EXPECT_TRUE(FDiv->hasNoNaNs());
180
181 }
182
183 }
+0
-284
unittests/VMCore/InstructionsTest.cpp less more
None //===- llvm/unittest/VMCore/InstructionsTest.cpp - Instructions unit tests ===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/IR/Instructions.h"
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/Analysis/ValueTracking.h"
12 #include "llvm/IR/BasicBlock.h"
13 #include "llvm/IR/Constants.h"
14 #include "llvm/IR/DataLayout.h"
15 #include "llvm/IR/DerivedTypes.h"
16 #include "llvm/IR/IRBuilder.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/IR/MDBuilder.h"
19 #include "llvm/IR/Operator.h"
20 #include "gtest/gtest.h"
21
22 namespace llvm {
23 namespace {
24
25 TEST(InstructionsTest, ReturnInst) {
26 LLVMContext &C(getGlobalContext());
27
28 // test for PR6589
29 const ReturnInst* r0 = ReturnInst::Create(C);
30 EXPECT_EQ(r0->getNumOperands(), 0U);
31 EXPECT_EQ(r0->op_begin(), r0->op_end());
32
33 IntegerType* Int1 = IntegerType::get(C, 1);
34 Constant* One = ConstantInt::get(Int1, 1, true);
35 const ReturnInst* r1 = ReturnInst::Create(C, One);
36 EXPECT_EQ(1U, r1->getNumOperands());
37 User::const_op_iterator b(r1->op_begin());
38 EXPECT_NE(r1->op_end(), b);
39 EXPECT_EQ(One, *b);
40 EXPECT_EQ(One, r1->getOperand(0));
41 ++b;
42 EXPECT_EQ(r1->op_end(), b);
43
44 // clean up
45 delete r0;
46 delete r1;
47 }
48
49 TEST(InstructionsTest, BranchInst) {
50 LLVMContext &C(getGlobalContext());
51
52 // Make a BasicBlocks
53 BasicBlock* bb0 = BasicBlock::Create(C);
54 BasicBlock* bb1 = BasicBlock::Create(C);
55
56 // Mandatory BranchInst
57 const BranchInst* b0 = BranchInst::Create(bb0);
58
59 EXPECT_TRUE(b0->isUnconditional());
60 EXPECT_FALSE(b0->isConditional());
61 EXPECT_EQ(1U, b0->getNumSuccessors());
62
63 // check num operands
64 EXPECT_EQ(1U, b0->getNumOperands());
65
66 EXPECT_NE(b0->op_begin(), b0->op_end());
67 EXPECT_EQ(b0->op_end(), llvm::next(b0->op_begin()));
68
69 EXPECT_EQ(b0->op_end(), llvm::next(b0->op_begin()));
70
71 IntegerType* Int1 = IntegerType::get(C, 1);
72 Constant* One = ConstantInt::get(Int1, 1, true);
73
74 // Conditional BranchInst
75 BranchInst* b1 = BranchInst::Create(bb0, bb1, One);
76
77 EXPECT_FALSE(b1->isUnconditional());
78 EXPECT_TRUE(b1->isConditional());
79 EXPECT_EQ(2U, b1->getNumSuccessors());
80
81 // check num operands
82 EXPECT_EQ(3U, b1->getNumOperands());
83
84 User::const_op_iterator b(b1->op_begin());
85
86 // check COND
87 EXPECT_NE(b, b1->op_end());
88 EXPECT_EQ(One, *b);
89 EXPECT_EQ(One, b1->getOperand(0));
90 EXPECT_EQ(One, b1->getCondition());
91 ++b;
92
93 // check ELSE
94 EXPECT_EQ(bb1, *b);
95 EXPECT_EQ(bb1, b1->getOperand(1));
96 EXPECT_EQ(bb1, b1->getSuccessor(1));
97 ++b;
98
99 // check THEN
100 EXPECT_EQ(bb0, *b);
101 EXPECT_EQ(bb0, b1->getOperand(2));
102 EXPECT_EQ(bb0, b1->getSuccessor(0));
103 ++b;
104
105 EXPECT_EQ(b1->op_end(), b);
106
107 // clean up
108 delete b0;
109 delete b1;
110
111 delete bb0;
112 delete bb1;
113 }
114
115 TEST(InstructionsTest, CastInst) {
116 LLVMContext &C(getGlobalContext());
117
118 Type* Int8Ty = Type::getInt8Ty(C);
119 Type* Int64Ty = Type::getInt64Ty(C);
120 Type* V8x8Ty = VectorType::get(Int8Ty, 8);
121 Type* V8x64Ty = VectorType::get(Int64Ty, 8);
122 Type* X86MMXTy = Type::getX86_MMXTy(C);
123
124 const Constant* c8 = Constant::getNullValue(V8x8Ty);
125 const Constant* c64 = Constant::getNullValue(V8x64Ty);
126
127 EXPECT_TRUE(CastInst::isCastable(V8x8Ty, X86MMXTy));
128 EXPECT_TRUE(CastInst::isCastable(X86MMXTy, V8x8Ty));
129 EXPECT_FALSE(CastInst::isCastable(Int64Ty, X86MMXTy));
130 EXPECT_TRUE(CastInst::isCastable(V8x64Ty, V8x8Ty));
131 EXPECT_TRUE(CastInst::isCastable(V8x8Ty, V8x64Ty));
132 EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true));
133 EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true));
134 }
135
136
137
138 TEST(InstructionsTest, VectorGep) {
139 LLVMContext &C(getGlobalContext());
140
141 // Type Definitions
142 PointerType *Ptri8Ty = PointerType::get(IntegerType::get(C, 8), 0);
143 PointerType *Ptri32Ty = PointerType::get(IntegerType::get(C, 8), 0);
144
145 VectorType *V2xi8PTy = VectorType::get(Ptri8Ty, 2);
146 VectorType *V2xi32PTy = VectorType::get(Ptri32Ty, 2);
147
148 // Test different aspects of the vector-of-pointers type
149 // and GEPs which use this type.
150 ConstantInt *Ci32a = ConstantInt::get(C, APInt(32, 1492));
151 ConstantInt *Ci32b = ConstantInt::get(C, APInt(32, 1948));
152 std::vector ConstVa(2, Ci32a);
153 std::vector ConstVb(2, Ci32b);
154 Constant *C2xi32a = ConstantVector::get(ConstVa);
155 Constant *C2xi32b = ConstantVector::get(ConstVb);
156
157 CastInst *PtrVecA = new IntToPtrInst(C2xi32a, V2xi32PTy);
158 CastInst *PtrVecB = new IntToPtrInst(C2xi32b, V2xi32PTy);
159
160 ICmpInst *ICmp0 = new ICmpInst(ICmpInst::ICMP_SGT, PtrVecA, PtrVecB);
161 ICmpInst *ICmp1 = new ICmpInst(ICmpInst::ICMP_ULT, PtrVecA, PtrVecB);
162 EXPECT_NE(ICmp0, ICmp1); // suppress warning.
163
164 GetElementPtrInst *Gep0 = GetElementPtrInst::Create(PtrVecA, C2xi32a);
165 GetElementPtrInst *Gep1 = GetElementPtrInst::Create(PtrVecA, C2xi32b);
166 GetElementPtrInst *Gep2 = GetElementPtrInst::Create(PtrVecB, C2xi32a);
167 GetElementPtrInst *Gep3 = GetElementPtrInst::Create(PtrVecB, C2xi32b);
168
169 CastInst *BTC0 = new BitCastInst(Gep0, V2xi8PTy);
170 CastInst *BTC1 = new BitCastInst(Gep1, V2xi8PTy);
171 CastInst *BTC2 = new BitCastInst(Gep2, V2xi8PTy);
172 CastInst *BTC3 = new BitCastInst(Gep3, V2xi8PTy);
173
174 Value *S0 = BTC0->stripPointerCasts();
175 Value *S1 = BTC1->stripPointerCasts();
176 Value *S2 = BTC2->stripPointerCasts();
177 Value *S3 = BTC3->stripPointerCasts();
178
179 EXPECT_NE(S0, Gep0);
180 EXPECT_NE(S1, Gep1);
181 EXPECT_NE(S2, Gep2);
182 EXPECT_NE(S3, Gep3);
183
184 int64_t Offset;
185 DataLayout TD("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3"
186 "2:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80"
187 ":128:128-n8:16:32:64-S128");
188 // Make sure we don't crash
189 GetPointerBaseWithConstantOffset(Gep0, Offset, TD);
190 GetPointerBaseWithConstantOffset(Gep1, Offset, TD);
191 GetPointerBaseWithConstantOffset(Gep2, Offset, TD);
192 GetPointerBaseWithConstantOffset(Gep3, Offset, TD);
193
194 // Gep of Geps
195 GetElementPtrInst *GepII0 = GetElementPtrInst::Create(Gep0, C2xi32b);
196 GetElementPtrInst *GepII1 = GetElementPtrInst::Create(Gep1, C2xi32a);
197 GetElementPtrInst *GepII2 = GetElementPtrInst::Create(Gep2, C2xi32b);
198 GetElementPtrInst *GepII3 = GetElementPtrInst::Create(Gep3, C2xi32a);
199
200 EXPECT_EQ(GepII0->getNumIndices(), 1u);
201 EXPECT_EQ(GepII1->getNumIndices(), 1u);
202 EXPECT_EQ(GepII2->getNumIndices(), 1u);
203 EXPECT_EQ(GepII3->getNumIndices(), 1u);
204
205 EXPECT_FALSE(GepII0->hasAllZeroIndices());
206 EXPECT_FALSE(GepII1->hasAllZeroIndices());
207 EXPECT_FALSE(GepII2->hasAllZeroIndices());
208 EXPECT_FALSE(GepII3->hasAllZeroIndices());
209
210 delete GepII0;
211 delete GepII1;
212 delete GepII2;
213 delete GepII3;
214
215 delete BTC0;
216 delete BTC1;
217 delete BTC2;
218 delete BTC3;
219
220 delete Gep0;
221 delete Gep1;
222 delete Gep2;
223 delete Gep3;
224
225 delete ICmp0;
226 delete ICmp1;
227 delete PtrVecA;
228 delete PtrVecB;
229 }
230
231 TEST(InstructionsTest, FPMathOperator) {
232 LLVMContext &Context = getGlobalContext();
233 IRBuilder<> Builder(Context);
234 MDBuilder MDHelper(Context);
235 Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0);
236 MDNode *MD1 = MDHelper.createFPMath(1.0);
237 Value *V1 = Builder.CreateFAdd(I, I, "", MD1);
238 EXPECT_TRUE(isa(V1));
239 FPMathOperator *O1 = cast(V1);
240 EXPECT_EQ(O1->getFPAccuracy(), 1.0);
241 delete V1;
242 delete I;
243 }
244
245
246 TEST(InstructionsTest, isEliminableCastPair) {
247 LLVMContext &C(getGlobalContext());
248
249 Type* Int32Ty = Type::getInt32Ty(C);
250 Type* Int64Ty = Type::getInt64Ty(C);
251 Type* Int64PtrTy = Type::getInt64PtrTy(C);
252
253 // Source and destination pointers have same size -> bitcast.
254 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
255 CastInst::IntToPtr,
256 Int64PtrTy, Int64Ty, Int64PtrTy,
257 Int32Ty, 0, Int32Ty),
258 CastInst::BitCast);
259
260 // Source and destination pointers have different sizes -> fail.
261 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
262 CastInst::IntToPtr,
263 Int64PtrTy, Int64Ty, Int64PtrTy,
264 Int32Ty, 0, Int64Ty),
265 0U);
266
267 // Middle pointer big enough -> bitcast.
268 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
269 CastInst::PtrToInt,
270 Int64Ty, Int64PtrTy, Int64Ty,
271 0, Int64Ty, 0),
272 CastInst::BitCast);
273
274 // Middle pointer too small -> fail.
275 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
276 CastInst::PtrToInt,
277 Int64Ty, Int64PtrTy, Int64Ty,
278 0, Int32Ty, 0),
279 0U);
280 }
281
282 } // end anonymous namespace
283 } // end namespace llvm
+0
-106
unittests/VMCore/MDBuilderTest.cpp less more
None //===- llvm/unittests/MDBuilderTest.cpp - MDBuilder unit tests ------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/IR/MDBuilder.h"
10 #include "llvm/IR/IRBuilder.h"
11 #include "llvm/IR/Operator.h"
12 #include "gtest/gtest.h"
13
14 using namespace llvm;
15
16 namespace {
17
18 class MDBuilderTest : public testing::Test {
19 protected:
20 LLVMContext Context;
21 };
22
23 TEST_F(MDBuilderTest, createString) {
24 MDBuilder MDHelper(Context);
25 MDString *Str0 = MDHelper.createString("");
26 MDString *Str1 = MDHelper.createString("string");
27 EXPECT_EQ(Str0->getString(), StringRef(""));
28 EXPECT_EQ(Str1->getString(), StringRef("string"));
29 }
30 TEST_F(MDBuilderTest, createFPMath) {
31 MDBuilder MDHelper(Context);
32 MDNode *MD0 = MDHelper.createFPMath(0.0);
33 MDNode *MD1 = MDHelper.createFPMath(1.0);
34 EXPECT_EQ(MD0, (MDNode *)0);
35 EXPECT_NE(MD1, (MDNode *)0);
36 EXPECT_EQ(MD1->getNumOperands(), 1U);
37 Value *Op = MD1->getOperand(0);
38 EXPECT_TRUE(isa(Op));
39 EXPECT_TRUE(Op->getType()->isFloatingPointTy());
40 ConstantFP *Val = cast(Op);
41 EXPECT_TRUE(Val->isExactlyValue(1.0));
42 }
43 TEST_F(MDBuilderTest, createRangeMetadata) {
44 MDBuilder MDHelper(Context);
45 APInt A(8, 1), B(8, 2);
46 MDNode *R0 = MDHelper.createRange(A, A);
47 MDNode *R1 = MDHelper.createRange(A, B);
48 EXPECT_EQ(R0, (MDNode *)0);
49 EXPECT_NE(R1, (MDNode *)0);
50 EXPECT_EQ(R1->getNumOperands(), 2U);
51 EXPECT_TRUE(isa(R1->getOperand(0)));
52 EXPECT_TRUE(isa(R1->getOperand(1)));
53 ConstantInt *C0 = cast(R1->getOperand(0));
54 ConstantInt *C1 = cast(R1->getOperand(1));
55 EXPECT_EQ(C0->getValue(), A);
56 EXPECT_EQ(C1->getValue(), B);
57 }
58 TEST_F(MDBuilderTest, createAnonymousTBAARoot) {
59 MDBuilder MDHelper(Context);
60 MDNode *R0 = MDHelper.createAnonymousTBAARoot();
61 MDNode *R1 = MDHelper.createAnonymousTBAARoot();
62 EXPECT_NE(R0, R1);
63 EXPECT_GE(R0->getNumOperands(), 1U);
64 EXPECT_GE(R1->getNumOperands(), 1U);
65 EXPECT_EQ(R0->getOperand(0), R0);
66 EXPECT_EQ(R1->getOperand(0), R1);
67 EXPECT_TRUE(R0->getNumOperands() == 1 || R0->getOperand(1) == 0);
68 EXPECT_TRUE(R1->getNumOperands() == 1 || R1->getOperand(1) == 0);
69 }
70 TEST_F(MDBuilderTest, createTBAARoot) {
71 MDBuilder MDHelper(Context);
72 MDNode *R0 = MDHelper.createTBAARoot("Root");
73 MDNode *R1 = MDHelper.createTBAARoot("Root");
74 EXPECT_EQ(R0, R1);
75 EXPECT_GE(R0->getNumOperands(), 1U);
76 EXPECT_TRUE(isa(R0->getOperand(0)));
77 EXPECT_EQ(cast(R0->getOperand(0))->getString(), "Root");
78 EXPECT_TRUE(R0->getNumOperands() == 1 || R0->getOperand(1) == 0);
79 }
80 TEST_F(MDBuilderTest, createTBAANode) {
81 MDBuilder MDHelper(Context);
82 MDNode *R = MDHelper.createTBAARoot("Root");
83 MDNode *N0 = MDHelper.createTBAANode("Node", R);
84 MDNode *N1 = MDHelper.createTBAANode("edoN", R);
85 MDNode *N2 = MDHelper.createTBAANode("Node", R, true);
86 MDNode *N3 = MDHelper.createTBAANode("Node", R);
87 EXPECT_EQ(N0, N3);
88 EXPECT_NE(N0, N1);
89 EXPECT_NE(N0, N2);
90 EXPECT_GE(N0->getNumOperands(), 2U);
91 EXPECT_GE(N1->getNumOperands(), 2U);
92 EXPECT_GE(N2->getNumOperands(), 3U);
93 EXPECT_TRUE(isa(N0->getOperand(0)));
94 EXPECT_TRUE(isa(N1->getOperand(0)));
95 EXPECT_TRUE(isa(N2->getOperand(0)));
96 EXPECT_EQ(cast(N0->getOperand(0))->getString(), "Node");
97 EXPECT_EQ(cast(N1->getOperand(0))->getString(), "edoN");
98 EXPECT_EQ(cast(N2->getOperand(0))->getString(), "Node");
99 EXPECT_EQ(N0->getOperand(1), R);
100 EXPECT_EQ(N1->getOperand(1), R);
101 EXPECT_EQ(N2->getOperand(1), R);
102 EXPECT_TRUE(isa(N2->getOperand(2)));
103 EXPECT_EQ(cast(N2->getOperand(2))->getZExtValue(), 1U);
104 }
105 }
+0
-15
unittests/VMCore/Makefile less more
None ##===- unittests/VMCore/Makefile ---------------------------*- Makefile -*-===##
1 #
2 # The LLVM Compiler Infrastructure
3 #
4 # This file is distributed under the University of Illinois Open Source
5 # License. See LICENSE.TXT for details.
6 #
7 ##===----------------------------------------------------------------------===##
8
9 LEVEL = ../..
10 TESTNAME = VMCore
11 LINK_COMPONENTS := core ipa asmparser
12
13 include $(LEVEL)/Makefile.config
14 include $(LLVM_SRC_ROOT)/unittests/Makefile.unittest
+0
-152
unittests/VMCore/MetadataTest.cpp less more
None //===- llvm/unittest/VMCore/Metadata.cpp - Metadata unit tests ------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/IR/Metadata.h"
10 #include "llvm/IR/Constants.h"
11 #include "llvm/IR/Instructions.h"
12 #include "llvm/IR/LLVMContext.h"
13 #include "llvm/IR/Module.h"
14 #include "llvm/IR/Type.h"
15 #include "llvm/Support/ValueHandle.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include "gtest/gtest.h"
18 using namespace llvm;
19
20 namespace {