blob: 0bf98f35b3c8148150635e776bc8793d0d3ced5d [file] [log] [blame]
Chandler Carruth74b6a772013-01-07 15:35:46 +00001//===- llvm/unittest/IR/ConstantsTest.cpp - Constants unit tests ----------===//
Misha Brukmand1d2c502009-03-24 21:36:09 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Rafael Espindola993502e2015-02-23 21:51:06 +000010#include "llvm/AsmParser/Parser.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000011#include "llvm/IR/Constants.h"
12#include "llvm/IR/DerivedTypes.h"
13#include "llvm/IR/InstrTypes.h"
14#include "llvm/IR/Instruction.h"
15#include "llvm/IR/LLVMContext.h"
16#include "llvm/IR/Module.h"
Rafael Espindola993502e2015-02-23 21:51:06 +000017#include "llvm/Support/SourceMgr.h"
Misha Brukmand1d2c502009-03-24 21:36:09 +000018#include "gtest/gtest.h"
19
20namespace llvm {
21namespace {
22
23TEST(ConstantsTest, Integer_i1) {
Chris Lattner229907c2011-07-18 04:54:35 +000024 IntegerType* Int1 = IntegerType::get(getGlobalContext(), 1);
Owen Andersonedb4a702009-07-24 23:12:02 +000025 Constant* One = ConstantInt::get(Int1, 1, true);
26 Constant* Zero = ConstantInt::get(Int1, 0);
27 Constant* NegOne = ConstantInt::get(Int1, static_cast<uint64_t>(-1), true);
28 EXPECT_EQ(NegOne, ConstantInt::getSigned(Int1, -1));
Owen Andersonb292b8c2009-07-30 23:03:37 +000029 Constant* Undef = UndefValue::get(Int1);
Misha Brukmand1d2c502009-03-24 21:36:09 +000030
31 // Input: @b = constant i1 add(i1 1 , i1 1)
32 // Output: @b = constant i1 false
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000033 EXPECT_EQ(Zero, ConstantExpr::getAdd(One, One));
Misha Brukmand1d2c502009-03-24 21:36:09 +000034
35 // @c = constant i1 add(i1 -1, i1 1)
36 // @c = constant i1 false
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000037 EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, One));
Misha Brukmand1d2c502009-03-24 21:36:09 +000038
39 // @d = constant i1 add(i1 -1, i1 -1)
40 // @d = constant i1 false
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000041 EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, NegOne));
Misha Brukmand1d2c502009-03-24 21:36:09 +000042
43 // @e = constant i1 sub(i1 -1, i1 1)
44 // @e = constant i1 false
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000045 EXPECT_EQ(Zero, ConstantExpr::getSub(NegOne, One));
Misha Brukmand1d2c502009-03-24 21:36:09 +000046
47 // @f = constant i1 sub(i1 1 , i1 -1)
48 // @f = constant i1 false
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000049 EXPECT_EQ(Zero, ConstantExpr::getSub(One, NegOne));
Misha Brukmand1d2c502009-03-24 21:36:09 +000050
51 // @g = constant i1 sub(i1 1 , i1 1)
52 // @g = constant i1 false
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000053 EXPECT_EQ(Zero, ConstantExpr::getSub(One, One));
Misha Brukmand1d2c502009-03-24 21:36:09 +000054
55 // @h = constant i1 shl(i1 1 , i1 1) ; undefined
56 // @h = constant i1 undef
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000057 EXPECT_EQ(Undef, ConstantExpr::getShl(One, One));
Misha Brukmand1d2c502009-03-24 21:36:09 +000058
59 // @i = constant i1 shl(i1 1 , i1 0)
60 // @i = constant i1 true
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000061 EXPECT_EQ(One, ConstantExpr::getShl(One, Zero));
Misha Brukmand1d2c502009-03-24 21:36:09 +000062
63 // @j = constant i1 lshr(i1 1, i1 1) ; undefined
64 // @j = constant i1 undef
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000065 EXPECT_EQ(Undef, ConstantExpr::getLShr(One, One));
Misha Brukmand1d2c502009-03-24 21:36:09 +000066
67 // @m = constant i1 ashr(i1 1, i1 1) ; undefined
68 // @m = constant i1 undef
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000069 EXPECT_EQ(Undef, ConstantExpr::getAShr(One, One));
Misha Brukmand1d2c502009-03-24 21:36:09 +000070
71 // @n = constant i1 mul(i1 -1, i1 1)
72 // @n = constant i1 true
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000073 EXPECT_EQ(One, ConstantExpr::getMul(NegOne, One));
Misha Brukmand1d2c502009-03-24 21:36:09 +000074
75 // @o = constant i1 sdiv(i1 -1, i1 1) ; overflow
76 // @o = constant i1 true
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000077 EXPECT_EQ(One, ConstantExpr::getSDiv(NegOne, One));
Misha Brukmand1d2c502009-03-24 21:36:09 +000078
79 // @p = constant i1 sdiv(i1 1 , i1 -1); overflow
80 // @p = constant i1 true
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000081 EXPECT_EQ(One, ConstantExpr::getSDiv(One, NegOne));
Misha Brukmand1d2c502009-03-24 21:36:09 +000082
83 // @q = constant i1 udiv(i1 -1, i1 1)
84 // @q = constant i1 true
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000085 EXPECT_EQ(One, ConstantExpr::getUDiv(NegOne, One));
Misha Brukmand1d2c502009-03-24 21:36:09 +000086
87 // @r = constant i1 udiv(i1 1, i1 -1)
88 // @r = constant i1 true
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000089 EXPECT_EQ(One, ConstantExpr::getUDiv(One, NegOne));
Misha Brukmand1d2c502009-03-24 21:36:09 +000090
91 // @s = constant i1 srem(i1 -1, i1 1) ; overflow
92 // @s = constant i1 false
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000093 EXPECT_EQ(Zero, ConstantExpr::getSRem(NegOne, One));
Misha Brukmand1d2c502009-03-24 21:36:09 +000094
95 // @t = constant i1 urem(i1 -1, i1 1)
96 // @t = constant i1 false
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000097 EXPECT_EQ(Zero, ConstantExpr::getURem(NegOne, One));
Misha Brukmand1d2c502009-03-24 21:36:09 +000098
99 // @u = constant i1 srem(i1 1, i1 -1) ; overflow
100 // @u = constant i1 false
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +0000101 EXPECT_EQ(Zero, ConstantExpr::getSRem(One, NegOne));
Misha Brukmand1d2c502009-03-24 21:36:09 +0000102}
103
Chris Lattnera776fc72009-04-24 05:30:14 +0000104TEST(ConstantsTest, IntSigns) {
Chris Lattner229907c2011-07-18 04:54:35 +0000105 IntegerType* Int8Ty = Type::getInt8Ty(getGlobalContext());
Owen Andersonedb4a702009-07-24 23:12:02 +0000106 EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, false)->getSExtValue());
107 EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, true)->getSExtValue());
108 EXPECT_EQ(100, ConstantInt::getSigned(Int8Ty, 100)->getSExtValue());
109 EXPECT_EQ(-50, ConstantInt::get(Int8Ty, 206)->getSExtValue());
110 EXPECT_EQ(-50, ConstantInt::getSigned(Int8Ty, -50)->getSExtValue());
111 EXPECT_EQ(206U, ConstantInt::getSigned(Int8Ty, -50)->getZExtValue());
Chris Lattnera776fc72009-04-24 05:30:14 +0000112
113 // Overflow is handled by truncation.
Owen Andersonedb4a702009-07-24 23:12:02 +0000114 EXPECT_EQ(0x3b, ConstantInt::get(Int8Ty, 0x13b)->getSExtValue());
Chris Lattnera776fc72009-04-24 05:30:14 +0000115}
116
Chris Lattner1be1fe02010-12-29 01:33:36 +0000117TEST(ConstantsTest, FP128Test) {
Chris Lattner229907c2011-07-18 04:54:35 +0000118 Type *FP128Ty = Type::getFP128Ty(getGlobalContext());
Chris Lattner1be1fe02010-12-29 01:33:36 +0000119
Chris Lattner229907c2011-07-18 04:54:35 +0000120 IntegerType *Int128Ty = Type::getIntNTy(getGlobalContext(), 128);
Chris Lattner1be1fe02010-12-29 01:33:36 +0000121 Constant *Zero128 = Constant::getNullValue(Int128Ty);
122 Constant *X = ConstantExpr::getUIToFP(Zero128, FP128Ty);
123 EXPECT_TRUE(isa<ConstantFP>(X));
124}
125
Evgeniy Stepanov23382642013-01-16 14:41:46 +0000126TEST(ConstantsTest, PointerCast) {
127 LLVMContext &C(getGlobalContext());
128 Type *Int8PtrTy = Type::getInt8PtrTy(C);
129 Type *Int32PtrTy = Type::getInt32PtrTy(C);
130 Type *Int64Ty = Type::getInt64Ty(C);
131 VectorType *Int8PtrVecTy = VectorType::get(Int8PtrTy, 4);
132 VectorType *Int32PtrVecTy = VectorType::get(Int32PtrTy, 4);
133 VectorType *Int64VecTy = VectorType::get(Int64Ty, 4);
134
135 // ptrtoint i8* to i64
136 EXPECT_EQ(Constant::getNullValue(Int64Ty),
137 ConstantExpr::getPointerCast(
138 Constant::getNullValue(Int8PtrTy), Int64Ty));
139
140 // bitcast i8* to i32*
141 EXPECT_EQ(Constant::getNullValue(Int32PtrTy),
142 ConstantExpr::getPointerCast(
143 Constant::getNullValue(Int8PtrTy), Int32PtrTy));
144
145 // ptrtoint <4 x i8*> to <4 x i64>
146 EXPECT_EQ(Constant::getNullValue(Int64VecTy),
147 ConstantExpr::getPointerCast(
148 Constant::getNullValue(Int8PtrVecTy), Int64VecTy));
149
150 // bitcast <4 x i8*> to <4 x i32*>
151 EXPECT_EQ(Constant::getNullValue(Int32PtrVecTy),
152 ConstantExpr::getPointerCast(
153 Constant::getNullValue(Int8PtrVecTy), Int32PtrVecTy));
154}
155
NAKAMURA Takumi0c8f08d2013-01-23 08:30:26 +0000156#define CHECK(x, y) { \
157 std::string __s; \
158 raw_string_ostream __o(__s); \
159 Instruction *__I = cast<ConstantExpr>(x)->getAsInstruction(); \
160 __I->print(__o); \
161 delete __I; \
162 __o.flush(); \
163 EXPECT_EQ(std::string(" <badref> = " y), __s); \
James Molloyce545682012-11-17 17:56:30 +0000164 }
165
166TEST(ConstantsTest, AsInstructionsTest) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000167 std::unique_ptr<Module> M(new Module("MyModule", getGlobalContext()));
James Molloyce545682012-11-17 17:56:30 +0000168
169 Type *Int64Ty = Type::getInt64Ty(getGlobalContext());
170 Type *Int32Ty = Type::getInt32Ty(getGlobalContext());
171 Type *Int16Ty = Type::getInt16Ty(getGlobalContext());
172 Type *Int1Ty = Type::getInt1Ty(getGlobalContext());
173 Type *FloatTy = Type::getFloatTy(getGlobalContext());
174 Type *DoubleTy = Type::getDoubleTy(getGlobalContext());
175
176 Constant *Global = M->getOrInsertGlobal("dummy",
177 PointerType::getUnqual(Int32Ty));
178 Constant *Global2 = M->getOrInsertGlobal("dummy2",
179 PointerType::getUnqual(Int32Ty));
180
181 Constant *P0 = ConstantExpr::getPtrToInt(Global, Int32Ty);
182 Constant *P1 = ConstantExpr::getUIToFP(P0, FloatTy);
183 Constant *P2 = ConstantExpr::getUIToFP(P0, DoubleTy);
184 Constant *P3 = ConstantExpr::getTrunc(P0, Int1Ty);
185 Constant *P4 = ConstantExpr::getPtrToInt(Global2, Int32Ty);
186 Constant *P5 = ConstantExpr::getUIToFP(P4, FloatTy);
187 Constant *P6 = ConstantExpr::getBitCast(P4, VectorType::get(Int16Ty, 2));
188
189 Constant *One = ConstantInt::get(Int32Ty, 1);
Pawel Bylicabce9c2e2015-04-24 07:42:35 +0000190 Constant *Two = ConstantInt::get(Int64Ty, 2);
191 Constant *Big = ConstantInt::get(getGlobalContext(),
192 APInt{256, uint64_t(-1), true});
Pawel Bylicac25918a2015-04-27 09:30:49 +0000193 Constant *Elt = ConstantInt::get(Int16Ty, 2015);
194 Constant *Undef16 = UndefValue::get(Int16Ty);
195 Constant *Undef64 = UndefValue::get(Int64Ty);
196 Constant *UndefV16 = UndefValue::get(P6->getType());
James Molloyce545682012-11-17 17:56:30 +0000197
198 #define P0STR "ptrtoint (i32** @dummy to i32)"
199 #define P1STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to float)"
200 #define P2STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to double)"
201 #define P3STR "ptrtoint (i32** @dummy to i1)"
202 #define P4STR "ptrtoint (i32** @dummy2 to i32)"
203 #define P5STR "uitofp (i32 ptrtoint (i32** @dummy2 to i32) to float)"
204 #define P6STR "bitcast (i32 ptrtoint (i32** @dummy2 to i32) to <2 x i16>)"
205
206 CHECK(ConstantExpr::getNeg(P0), "sub i32 0, " P0STR);
207 CHECK(ConstantExpr::getFNeg(P1), "fsub float -0.000000e+00, " P1STR);
208 CHECK(ConstantExpr::getNot(P0), "xor i32 " P0STR ", -1");
209 CHECK(ConstantExpr::getAdd(P0, P0), "add i32 " P0STR ", " P0STR);
210 CHECK(ConstantExpr::getAdd(P0, P0, false, true), "add nsw i32 " P0STR ", "
211 P0STR);
212 CHECK(ConstantExpr::getAdd(P0, P0, true, true), "add nuw nsw i32 " P0STR ", "
213 P0STR);
214 CHECK(ConstantExpr::getFAdd(P1, P1), "fadd float " P1STR ", " P1STR);
215 CHECK(ConstantExpr::getSub(P0, P0), "sub i32 " P0STR ", " P0STR);
216 CHECK(ConstantExpr::getFSub(P1, P1), "fsub float " P1STR ", " P1STR);
217 CHECK(ConstantExpr::getMul(P0, P0), "mul i32 " P0STR ", " P0STR);
218 CHECK(ConstantExpr::getFMul(P1, P1), "fmul float " P1STR ", " P1STR);
219 CHECK(ConstantExpr::getUDiv(P0, P0), "udiv i32 " P0STR ", " P0STR);
220 CHECK(ConstantExpr::getSDiv(P0, P0), "sdiv i32 " P0STR ", " P0STR);
221 CHECK(ConstantExpr::getFDiv(P1, P1), "fdiv float " P1STR ", " P1STR);
222 CHECK(ConstantExpr::getURem(P0, P0), "urem i32 " P0STR ", " P0STR);
223 CHECK(ConstantExpr::getSRem(P0, P0), "srem i32 " P0STR ", " P0STR);
224 CHECK(ConstantExpr::getFRem(P1, P1), "frem float " P1STR ", " P1STR);
225 CHECK(ConstantExpr::getAnd(P0, P0), "and i32 " P0STR ", " P0STR);
226 CHECK(ConstantExpr::getOr(P0, P0), "or i32 " P0STR ", " P0STR);
227 CHECK(ConstantExpr::getXor(P0, P0), "xor i32 " P0STR ", " P0STR);
228 CHECK(ConstantExpr::getShl(P0, P0), "shl i32 " P0STR ", " P0STR);
229 CHECK(ConstantExpr::getShl(P0, P0, true), "shl nuw i32 " P0STR ", " P0STR);
230 CHECK(ConstantExpr::getShl(P0, P0, false, true), "shl nsw i32 " P0STR ", "
231 P0STR);
232 CHECK(ConstantExpr::getLShr(P0, P0, false), "lshr i32 " P0STR ", " P0STR);
233 CHECK(ConstantExpr::getLShr(P0, P0, true), "lshr exact i32 " P0STR ", " P0STR);
234 CHECK(ConstantExpr::getAShr(P0, P0, false), "ashr i32 " P0STR ", " P0STR);
235 CHECK(ConstantExpr::getAShr(P0, P0, true), "ashr exact i32 " P0STR ", " P0STR);
236
237 CHECK(ConstantExpr::getSExt(P0, Int64Ty), "sext i32 " P0STR " to i64");
238 CHECK(ConstantExpr::getZExt(P0, Int64Ty), "zext i32 " P0STR " to i64");
239 CHECK(ConstantExpr::getFPTrunc(P2, FloatTy), "fptrunc double " P2STR
240 " to float");
241 CHECK(ConstantExpr::getFPExtend(P1, DoubleTy), "fpext float " P1STR
242 " to double");
243
244 CHECK(ConstantExpr::getExactUDiv(P0, P0), "udiv exact i32 " P0STR ", " P0STR);
245
246 CHECK(ConstantExpr::getSelect(P3, P0, P4), "select i1 " P3STR ", i32 " P0STR
247 ", i32 " P4STR);
248 CHECK(ConstantExpr::getICmp(CmpInst::ICMP_EQ, P0, P4), "icmp eq i32 " P0STR
249 ", " P4STR);
250 CHECK(ConstantExpr::getFCmp(CmpInst::FCMP_ULT, P1, P5), "fcmp ult float "
251 P1STR ", " P5STR);
252
253 std::vector<Constant*> V;
254 V.push_back(One);
255 // FIXME: getGetElementPtr() actually creates an inbounds ConstantGEP,
256 // not a normal one!
257 //CHECK(ConstantExpr::getGetElementPtr(Global, V, false),
David Blaikie79e6c742015-02-27 19:29:02 +0000258 // "getelementptr i32*, i32** @dummy, i32 1");
David Blaikie4a2e73b2015-04-02 18:55:32 +0000259 CHECK(ConstantExpr::getInBoundsGetElementPtr(PointerType::getUnqual(Int32Ty),
260 Global, V),
David Blaikie79e6c742015-02-27 19:29:02 +0000261 "getelementptr inbounds i32*, i32** @dummy, i32 1");
James Molloyce545682012-11-17 17:56:30 +0000262
263 CHECK(ConstantExpr::getExtractElement(P6, One), "extractelement <2 x i16> "
264 P6STR ", i32 1");
Pawel Bylicabce9c2e2015-04-24 07:42:35 +0000265
Pawel Bylicac25918a2015-04-27 09:30:49 +0000266 EXPECT_EQ(Undef16, ConstantExpr::getExtractElement(P6, Two));
267 EXPECT_EQ(Undef16, ConstantExpr::getExtractElement(P6, Big));
268 EXPECT_EQ(Undef16, ConstantExpr::getExtractElement(P6, Undef64));
269
270 EXPECT_EQ(Elt, ConstantExpr::getExtractElement(
271 ConstantExpr::getInsertElement(P6, Elt, One), One));
272 EXPECT_EQ(UndefV16, ConstantExpr::getInsertElement(P6, Elt, Two));
273 EXPECT_EQ(UndefV16, ConstantExpr::getInsertElement(P6, Elt, Big));
274 EXPECT_EQ(UndefV16, ConstantExpr::getInsertElement(P6, Elt, Undef64));
James Molloyce545682012-11-17 17:56:30 +0000275}
276
Rafael Espindola7e2b7562014-05-13 01:23:21 +0000277#ifdef GTEST_HAS_DEATH_TEST
278#ifndef NDEBUG
279TEST(ConstantsTest, ReplaceWithConstantTest) {
280 std::unique_ptr<Module> M(new Module("MyModule", getGlobalContext()));
281
282 Type *Int32Ty = Type::getInt32Ty(getGlobalContext());
283 Constant *One = ConstantInt::get(Int32Ty, 1);
284
285 Constant *Global =
286 M->getOrInsertGlobal("dummy", PointerType::getUnqual(Int32Ty));
David Blaikie4a2e73b2015-04-02 18:55:32 +0000287 Constant *GEP = ConstantExpr::getGetElementPtr(
288 PointerType::getUnqual(Int32Ty), Global, One);
Rafael Espindola7e2b7562014-05-13 01:23:21 +0000289 EXPECT_DEATH(Global->replaceAllUsesWith(GEP),
290 "this->replaceAllUsesWith\\(expr\\(this\\)\\) is NOT valid!");
291}
Rafael Espindola6b238632014-05-16 19:35:39 +0000292
Rafael Espindola7e2b7562014-05-13 01:23:21 +0000293#endif
294#endif
295
James Molloyce545682012-11-17 17:56:30 +0000296#undef CHECK
297
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000298TEST(ConstantsTest, ConstantArrayReplaceWithConstant) {
299 LLVMContext Context;
300 std::unique_ptr<Module> M(new Module("MyModule", Context));
301
302 Type *IntTy = Type::getInt8Ty(Context);
303 ArrayType *ArrayTy = ArrayType::get(IntTy, 2);
304 Constant *A01Vals[2] = {ConstantInt::get(IntTy, 0),
305 ConstantInt::get(IntTy, 1)};
306 Constant *A01 = ConstantArray::get(ArrayTy, A01Vals);
307
308 Constant *Global = new GlobalVariable(*M, IntTy, false,
309 GlobalValue::ExternalLinkage, nullptr);
310 Constant *GlobalInt = ConstantExpr::getPtrToInt(Global, IntTy);
311 Constant *A0GVals[2] = {ConstantInt::get(IntTy, 0), GlobalInt};
312 Constant *A0G = ConstantArray::get(ArrayTy, A0GVals);
313 ASSERT_NE(A01, A0G);
314
315 GlobalVariable *RefArray =
316 new GlobalVariable(*M, ArrayTy, false, GlobalValue::ExternalLinkage, A0G);
317 ASSERT_EQ(A0G, RefArray->getInitializer());
318
319 GlobalInt->replaceAllUsesWith(ConstantInt::get(IntTy, 1));
320 ASSERT_EQ(A01, RefArray->getInitializer());
321}
322
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +0000323TEST(ConstantsTest, ConstantExprReplaceWithConstant) {
324 LLVMContext Context;
325 std::unique_ptr<Module> M(new Module("MyModule", Context));
326
327 Type *IntTy = Type::getInt8Ty(Context);
328 Constant *G1 = new GlobalVariable(*M, IntTy, false,
329 GlobalValue::ExternalLinkage, nullptr);
330 Constant *G2 = new GlobalVariable(*M, IntTy, false,
331 GlobalValue::ExternalLinkage, nullptr);
332 ASSERT_NE(G1, G2);
333
334 Constant *Int1 = ConstantExpr::getPtrToInt(G1, IntTy);
335 Constant *Int2 = ConstantExpr::getPtrToInt(G2, IntTy);
336 ASSERT_NE(Int1, Int2);
337
338 GlobalVariable *Ref =
339 new GlobalVariable(*M, IntTy, false, GlobalValue::ExternalLinkage, Int1);
340 ASSERT_EQ(Int1, Ref->getInitializer());
341
342 G1->replaceAllUsesWith(G2);
343 ASSERT_EQ(Int2, Ref->getInitializer());
344}
345
Duncan P. N. Exon Smith35de5b82014-08-19 21:18:21 +0000346TEST(ConstantsTest, GEPReplaceWithConstant) {
347 LLVMContext Context;
348 std::unique_ptr<Module> M(new Module("MyModule", Context));
349
350 Type *IntTy = Type::getInt32Ty(Context);
David Blaikie16a2f3e2015-09-14 18:01:59 +0000351 Type *PtrTy = PointerType::get(IntTy, 0);
Duncan P. N. Exon Smith35de5b82014-08-19 21:18:21 +0000352 auto *C1 = ConstantInt::get(IntTy, 1);
353 auto *Placeholder = new GlobalVariable(
354 *M, IntTy, false, GlobalValue::ExternalWeakLinkage, nullptr);
David Blaikie4a2e73b2015-04-02 18:55:32 +0000355 auto *GEP = ConstantExpr::getGetElementPtr(IntTy, Placeholder, C1);
Duncan P. N. Exon Smith35de5b82014-08-19 21:18:21 +0000356 ASSERT_EQ(GEP->getOperand(0), Placeholder);
357
358 auto *Ref =
359 new GlobalVariable(*M, PtrTy, false, GlobalValue::ExternalLinkage, GEP);
360 ASSERT_EQ(GEP, Ref->getInitializer());
361
362 auto *Global = new GlobalVariable(*M, PtrTy, false,
363 GlobalValue::ExternalLinkage, nullptr);
David Blaikie16a2f3e2015-09-14 18:01:59 +0000364 auto *Alias = GlobalAlias::create(IntTy, 0, GlobalValue::ExternalLinkage,
Duncan P. N. Exon Smith35de5b82014-08-19 21:18:21 +0000365 "alias", Global, M.get());
366 Placeholder->replaceAllUsesWith(Alias);
367 ASSERT_EQ(GEP, Ref->getInitializer());
368 ASSERT_EQ(GEP->getOperand(0), Alias);
369}
370
Rafael Espindola993502e2015-02-23 21:51:06 +0000371TEST(ConstantsTest, AliasCAPI) {
372 LLVMContext Context;
373 SMDiagnostic Error;
374 std::unique_ptr<Module> M =
375 parseAssemblyString("@g = global i32 42", Error, Context);
376 GlobalVariable *G = M->getGlobalVariable("g");
377 Type *I16Ty = Type::getInt16Ty(Context);
378 Type *I16PTy = PointerType::get(I16Ty, 0);
379 Constant *Aliasee = ConstantExpr::getBitCast(G, I16PTy);
380 LLVMValueRef AliasRef =
381 LLVMAddAlias(wrap(M.get()), wrap(I16PTy), wrap(Aliasee), "a");
382 ASSERT_EQ(unwrap<GlobalAlias>(AliasRef)->getAliasee(), Aliasee);
383}
384
Justin Bogner0ebc8602015-12-08 03:01:16 +0000385static std::string getNameOfType(Type *T) {
386 std::string S;
387 raw_string_ostream RSOS(S);
388 T->print(RSOS);
389 return S;
390}
391
Justin Bognerb7389d6712015-12-09 21:21:07 +0000392TEST(ConstantsTest, BuildConstantDataArrays) {
393 LLVMContext Context;
394 std::unique_ptr<Module> M(new Module("MyModule", Context));
395
396 for (Type *T : {Type::getInt8Ty(Context), Type::getInt16Ty(Context),
397 Type::getInt32Ty(Context), Type::getInt64Ty(Context)}) {
398 ArrayType *ArrayTy = ArrayType::get(T, 2);
399 Constant *Vals[] = {ConstantInt::get(T, 0), ConstantInt::get(T, 1)};
400 Constant *CDV = ConstantArray::get(ArrayTy, Vals);
401 ASSERT_TRUE(dyn_cast<ConstantDataArray>(CDV) != nullptr)
402 << " T = " << getNameOfType(T);
403 }
404
405 for (Type *T : {Type::getHalfTy(Context), Type::getFloatTy(Context),
406 Type::getDoubleTy(Context)}) {
407 ArrayType *ArrayTy = ArrayType::get(T, 2);
408 Constant *Vals[] = {ConstantFP::get(T, 0), ConstantFP::get(T, 1)};
409 Constant *CDV = ConstantArray::get(ArrayTy, Vals);
410 ASSERT_TRUE(dyn_cast<ConstantDataArray>(CDV) != nullptr)
411 << " T = " << getNameOfType(T);
412 }
413}
414
Justin Bogner0ebc8602015-12-08 03:01:16 +0000415TEST(ConstantsTest, BuildConstantDataVectors) {
416 LLVMContext Context;
417 std::unique_ptr<Module> M(new Module("MyModule", Context));
418
419 for (Type *T : {Type::getInt8Ty(Context), Type::getInt16Ty(Context),
420 Type::getInt32Ty(Context), Type::getInt64Ty(Context)}) {
421 Constant *Vals[] = {ConstantInt::get(T, 0), ConstantInt::get(T, 1)};
422 Constant *CDV = ConstantVector::get(Vals);
423 ASSERT_TRUE(dyn_cast<ConstantDataVector>(CDV) != nullptr)
424 << " T = " << getNameOfType(T);
425 }
426
427 for (Type *T : {Type::getHalfTy(Context), Type::getFloatTy(Context),
428 Type::getDoubleTy(Context)}) {
429 Constant *Vals[] = {ConstantFP::get(T, 0), ConstantFP::get(T, 1)};
430 Constant *CDV = ConstantVector::get(Vals);
431 ASSERT_TRUE(dyn_cast<ConstantDataVector>(CDV) != nullptr)
432 << " T = " << getNameOfType(T);
433 }
434}
435
Misha Brukmand1d2c502009-03-24 21:36:09 +0000436} // end anonymous namespace
437} // end namespace llvm