blob: 39ae18ca5e0f4d54c0d9a8318b52a58ab4ab26ee [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"
Eric Christophera6b96002015-12-18 01:46:52 +000018#include "llvm-c/Core.h"
Misha Brukmand1d2c502009-03-24 21:36:09 +000019#include "gtest/gtest.h"
20
21namespace llvm {
22namespace {
23
24TEST(ConstantsTest, Integer_i1) {
Mehdi Amini03b42e42016-04-14 21:59:01 +000025 LLVMContext Context;
26 IntegerType *Int1 = IntegerType::get(Context, 1);
Owen Andersonedb4a702009-07-24 23:12:02 +000027 Constant* One = ConstantInt::get(Int1, 1, true);
28 Constant* Zero = ConstantInt::get(Int1, 0);
29 Constant* NegOne = ConstantInt::get(Int1, static_cast<uint64_t>(-1), true);
30 EXPECT_EQ(NegOne, ConstantInt::getSigned(Int1, -1));
Owen Andersonb292b8c2009-07-30 23:03:37 +000031 Constant* Undef = UndefValue::get(Int1);
Misha Brukmand1d2c502009-03-24 21:36:09 +000032
33 // Input: @b = constant i1 add(i1 1 , i1 1)
34 // Output: @b = constant i1 false
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000035 EXPECT_EQ(Zero, ConstantExpr::getAdd(One, One));
Misha Brukmand1d2c502009-03-24 21:36:09 +000036
37 // @c = constant i1 add(i1 -1, i1 1)
38 // @c = constant i1 false
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000039 EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, One));
Misha Brukmand1d2c502009-03-24 21:36:09 +000040
41 // @d = constant i1 add(i1 -1, i1 -1)
42 // @d = constant i1 false
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000043 EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, NegOne));
Misha Brukmand1d2c502009-03-24 21:36:09 +000044
45 // @e = constant i1 sub(i1 -1, i1 1)
46 // @e = constant i1 false
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000047 EXPECT_EQ(Zero, ConstantExpr::getSub(NegOne, One));
Misha Brukmand1d2c502009-03-24 21:36:09 +000048
49 // @f = constant i1 sub(i1 1 , i1 -1)
50 // @f = constant i1 false
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000051 EXPECT_EQ(Zero, ConstantExpr::getSub(One, NegOne));
Misha Brukmand1d2c502009-03-24 21:36:09 +000052
53 // @g = constant i1 sub(i1 1 , i1 1)
54 // @g = constant i1 false
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000055 EXPECT_EQ(Zero, ConstantExpr::getSub(One, One));
Misha Brukmand1d2c502009-03-24 21:36:09 +000056
57 // @h = constant i1 shl(i1 1 , i1 1) ; undefined
58 // @h = constant i1 undef
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000059 EXPECT_EQ(Undef, ConstantExpr::getShl(One, One));
Misha Brukmand1d2c502009-03-24 21:36:09 +000060
61 // @i = constant i1 shl(i1 1 , i1 0)
62 // @i = constant i1 true
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000063 EXPECT_EQ(One, ConstantExpr::getShl(One, Zero));
Misha Brukmand1d2c502009-03-24 21:36:09 +000064
65 // @j = constant i1 lshr(i1 1, i1 1) ; undefined
66 // @j = constant i1 undef
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000067 EXPECT_EQ(Undef, ConstantExpr::getLShr(One, One));
Misha Brukmand1d2c502009-03-24 21:36:09 +000068
69 // @m = constant i1 ashr(i1 1, i1 1) ; undefined
70 // @m = constant i1 undef
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000071 EXPECT_EQ(Undef, ConstantExpr::getAShr(One, One));
Misha Brukmand1d2c502009-03-24 21:36:09 +000072
73 // @n = constant i1 mul(i1 -1, i1 1)
74 // @n = constant i1 true
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000075 EXPECT_EQ(One, ConstantExpr::getMul(NegOne, One));
Misha Brukmand1d2c502009-03-24 21:36:09 +000076
77 // @o = constant i1 sdiv(i1 -1, i1 1) ; overflow
78 // @o = constant i1 true
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000079 EXPECT_EQ(One, ConstantExpr::getSDiv(NegOne, One));
Misha Brukmand1d2c502009-03-24 21:36:09 +000080
81 // @p = constant i1 sdiv(i1 1 , i1 -1); overflow
82 // @p = constant i1 true
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000083 EXPECT_EQ(One, ConstantExpr::getSDiv(One, NegOne));
Misha Brukmand1d2c502009-03-24 21:36:09 +000084
85 // @q = constant i1 udiv(i1 -1, i1 1)
86 // @q = constant i1 true
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000087 EXPECT_EQ(One, ConstantExpr::getUDiv(NegOne, One));
Misha Brukmand1d2c502009-03-24 21:36:09 +000088
89 // @r = constant i1 udiv(i1 1, i1 -1)
90 // @r = constant i1 true
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000091 EXPECT_EQ(One, ConstantExpr::getUDiv(One, NegOne));
Misha Brukmand1d2c502009-03-24 21:36:09 +000092
93 // @s = constant i1 srem(i1 -1, i1 1) ; overflow
94 // @s = constant i1 false
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000095 EXPECT_EQ(Zero, ConstantExpr::getSRem(NegOne, One));
Misha Brukmand1d2c502009-03-24 21:36:09 +000096
97 // @t = constant i1 urem(i1 -1, i1 1)
98 // @t = constant i1 false
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +000099 EXPECT_EQ(Zero, ConstantExpr::getURem(NegOne, One));
Misha Brukmand1d2c502009-03-24 21:36:09 +0000100
101 // @u = constant i1 srem(i1 1, i1 -1) ; overflow
102 // @u = constant i1 false
Benjamin Kramer9a59b2a2009-07-29 19:18:13 +0000103 EXPECT_EQ(Zero, ConstantExpr::getSRem(One, NegOne));
Misha Brukmand1d2c502009-03-24 21:36:09 +0000104}
105
Chris Lattnera776fc72009-04-24 05:30:14 +0000106TEST(ConstantsTest, IntSigns) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000107 LLVMContext Context;
108 IntegerType *Int8Ty = Type::getInt8Ty(Context);
Owen Andersonedb4a702009-07-24 23:12:02 +0000109 EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, false)->getSExtValue());
110 EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, true)->getSExtValue());
111 EXPECT_EQ(100, ConstantInt::getSigned(Int8Ty, 100)->getSExtValue());
112 EXPECT_EQ(-50, ConstantInt::get(Int8Ty, 206)->getSExtValue());
113 EXPECT_EQ(-50, ConstantInt::getSigned(Int8Ty, -50)->getSExtValue());
114 EXPECT_EQ(206U, ConstantInt::getSigned(Int8Ty, -50)->getZExtValue());
Chris Lattnera776fc72009-04-24 05:30:14 +0000115
116 // Overflow is handled by truncation.
Owen Andersonedb4a702009-07-24 23:12:02 +0000117 EXPECT_EQ(0x3b, ConstantInt::get(Int8Ty, 0x13b)->getSExtValue());
Chris Lattnera776fc72009-04-24 05:30:14 +0000118}
119
Chris Lattner1be1fe02010-12-29 01:33:36 +0000120TEST(ConstantsTest, FP128Test) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000121 LLVMContext Context;
122 Type *FP128Ty = Type::getFP128Ty(Context);
Chris Lattner1be1fe02010-12-29 01:33:36 +0000123
Mehdi Amini03b42e42016-04-14 21:59:01 +0000124 IntegerType *Int128Ty = Type::getIntNTy(Context, 128);
Chris Lattner1be1fe02010-12-29 01:33:36 +0000125 Constant *Zero128 = Constant::getNullValue(Int128Ty);
126 Constant *X = ConstantExpr::getUIToFP(Zero128, FP128Ty);
127 EXPECT_TRUE(isa<ConstantFP>(X));
128}
129
Evgeniy Stepanov23382642013-01-16 14:41:46 +0000130TEST(ConstantsTest, PointerCast) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000131 LLVMContext C;
Evgeniy Stepanov23382642013-01-16 14:41:46 +0000132 Type *Int8PtrTy = Type::getInt8PtrTy(C);
133 Type *Int32PtrTy = Type::getInt32PtrTy(C);
134 Type *Int64Ty = Type::getInt64Ty(C);
135 VectorType *Int8PtrVecTy = VectorType::get(Int8PtrTy, 4);
136 VectorType *Int32PtrVecTy = VectorType::get(Int32PtrTy, 4);
137 VectorType *Int64VecTy = VectorType::get(Int64Ty, 4);
138
139 // ptrtoint i8* to i64
140 EXPECT_EQ(Constant::getNullValue(Int64Ty),
141 ConstantExpr::getPointerCast(
142 Constant::getNullValue(Int8PtrTy), Int64Ty));
143
144 // bitcast i8* to i32*
145 EXPECT_EQ(Constant::getNullValue(Int32PtrTy),
146 ConstantExpr::getPointerCast(
147 Constant::getNullValue(Int8PtrTy), Int32PtrTy));
148
149 // ptrtoint <4 x i8*> to <4 x i64>
150 EXPECT_EQ(Constant::getNullValue(Int64VecTy),
151 ConstantExpr::getPointerCast(
152 Constant::getNullValue(Int8PtrVecTy), Int64VecTy));
153
154 // bitcast <4 x i8*> to <4 x i32*>
155 EXPECT_EQ(Constant::getNullValue(Int32PtrVecTy),
156 ConstantExpr::getPointerCast(
157 Constant::getNullValue(Int8PtrVecTy), Int32PtrVecTy));
158}
159
NAKAMURA Takumi0c8f08d2013-01-23 08:30:26 +0000160#define CHECK(x, y) { \
161 std::string __s; \
162 raw_string_ostream __o(__s); \
163 Instruction *__I = cast<ConstantExpr>(x)->getAsInstruction(); \
164 __I->print(__o); \
165 delete __I; \
166 __o.flush(); \
167 EXPECT_EQ(std::string(" <badref> = " y), __s); \
James Molloyce545682012-11-17 17:56:30 +0000168 }
169
170TEST(ConstantsTest, AsInstructionsTest) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000171 LLVMContext Context;
172 std::unique_ptr<Module> M(new Module("MyModule", Context));
James Molloyce545682012-11-17 17:56:30 +0000173
Mehdi Amini03b42e42016-04-14 21:59:01 +0000174 Type *Int64Ty = Type::getInt64Ty(Context);
175 Type *Int32Ty = Type::getInt32Ty(Context);
176 Type *Int16Ty = Type::getInt16Ty(Context);
177 Type *Int1Ty = Type::getInt1Ty(Context);
178 Type *FloatTy = Type::getFloatTy(Context);
179 Type *DoubleTy = Type::getDoubleTy(Context);
James Molloyce545682012-11-17 17:56:30 +0000180
181 Constant *Global = M->getOrInsertGlobal("dummy",
182 PointerType::getUnqual(Int32Ty));
183 Constant *Global2 = M->getOrInsertGlobal("dummy2",
184 PointerType::getUnqual(Int32Ty));
185
186 Constant *P0 = ConstantExpr::getPtrToInt(Global, Int32Ty);
187 Constant *P1 = ConstantExpr::getUIToFP(P0, FloatTy);
188 Constant *P2 = ConstantExpr::getUIToFP(P0, DoubleTy);
189 Constant *P3 = ConstantExpr::getTrunc(P0, Int1Ty);
190 Constant *P4 = ConstantExpr::getPtrToInt(Global2, Int32Ty);
191 Constant *P5 = ConstantExpr::getUIToFP(P4, FloatTy);
192 Constant *P6 = ConstantExpr::getBitCast(P4, VectorType::get(Int16Ty, 2));
193
194 Constant *One = ConstantInt::get(Int32Ty, 1);
Pawel Bylicabce9c2e2015-04-24 07:42:35 +0000195 Constant *Two = ConstantInt::get(Int64Ty, 2);
Mehdi Amini03b42e42016-04-14 21:59:01 +0000196 Constant *Big = ConstantInt::get(Context, APInt{256, uint64_t(-1), true});
Pawel Bylicac25918a2015-04-27 09:30:49 +0000197 Constant *Elt = ConstantInt::get(Int16Ty, 2015);
198 Constant *Undef16 = UndefValue::get(Int16Ty);
199 Constant *Undef64 = UndefValue::get(Int64Ty);
200 Constant *UndefV16 = UndefValue::get(P6->getType());
James Molloyce545682012-11-17 17:56:30 +0000201
202 #define P0STR "ptrtoint (i32** @dummy to i32)"
203 #define P1STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to float)"
204 #define P2STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to double)"
205 #define P3STR "ptrtoint (i32** @dummy to i1)"
206 #define P4STR "ptrtoint (i32** @dummy2 to i32)"
207 #define P5STR "uitofp (i32 ptrtoint (i32** @dummy2 to i32) to float)"
208 #define P6STR "bitcast (i32 ptrtoint (i32** @dummy2 to i32) to <2 x i16>)"
209
210 CHECK(ConstantExpr::getNeg(P0), "sub i32 0, " P0STR);
211 CHECK(ConstantExpr::getFNeg(P1), "fsub float -0.000000e+00, " P1STR);
212 CHECK(ConstantExpr::getNot(P0), "xor i32 " P0STR ", -1");
213 CHECK(ConstantExpr::getAdd(P0, P0), "add i32 " P0STR ", " P0STR);
214 CHECK(ConstantExpr::getAdd(P0, P0, false, true), "add nsw i32 " P0STR ", "
215 P0STR);
216 CHECK(ConstantExpr::getAdd(P0, P0, true, true), "add nuw nsw i32 " P0STR ", "
217 P0STR);
218 CHECK(ConstantExpr::getFAdd(P1, P1), "fadd float " P1STR ", " P1STR);
219 CHECK(ConstantExpr::getSub(P0, P0), "sub i32 " P0STR ", " P0STR);
220 CHECK(ConstantExpr::getFSub(P1, P1), "fsub float " P1STR ", " P1STR);
221 CHECK(ConstantExpr::getMul(P0, P0), "mul i32 " P0STR ", " P0STR);
222 CHECK(ConstantExpr::getFMul(P1, P1), "fmul float " P1STR ", " P1STR);
223 CHECK(ConstantExpr::getUDiv(P0, P0), "udiv i32 " P0STR ", " P0STR);
224 CHECK(ConstantExpr::getSDiv(P0, P0), "sdiv i32 " P0STR ", " P0STR);
225 CHECK(ConstantExpr::getFDiv(P1, P1), "fdiv float " P1STR ", " P1STR);
226 CHECK(ConstantExpr::getURem(P0, P0), "urem i32 " P0STR ", " P0STR);
227 CHECK(ConstantExpr::getSRem(P0, P0), "srem i32 " P0STR ", " P0STR);
228 CHECK(ConstantExpr::getFRem(P1, P1), "frem float " P1STR ", " P1STR);
229 CHECK(ConstantExpr::getAnd(P0, P0), "and i32 " P0STR ", " P0STR);
230 CHECK(ConstantExpr::getOr(P0, P0), "or i32 " P0STR ", " P0STR);
231 CHECK(ConstantExpr::getXor(P0, P0), "xor i32 " P0STR ", " P0STR);
232 CHECK(ConstantExpr::getShl(P0, P0), "shl i32 " P0STR ", " P0STR);
233 CHECK(ConstantExpr::getShl(P0, P0, true), "shl nuw i32 " P0STR ", " P0STR);
234 CHECK(ConstantExpr::getShl(P0, P0, false, true), "shl nsw i32 " P0STR ", "
235 P0STR);
236 CHECK(ConstantExpr::getLShr(P0, P0, false), "lshr i32 " P0STR ", " P0STR);
237 CHECK(ConstantExpr::getLShr(P0, P0, true), "lshr exact i32 " P0STR ", " P0STR);
238 CHECK(ConstantExpr::getAShr(P0, P0, false), "ashr i32 " P0STR ", " P0STR);
239 CHECK(ConstantExpr::getAShr(P0, P0, true), "ashr exact i32 " P0STR ", " P0STR);
240
241 CHECK(ConstantExpr::getSExt(P0, Int64Ty), "sext i32 " P0STR " to i64");
242 CHECK(ConstantExpr::getZExt(P0, Int64Ty), "zext i32 " P0STR " to i64");
243 CHECK(ConstantExpr::getFPTrunc(P2, FloatTy), "fptrunc double " P2STR
244 " to float");
245 CHECK(ConstantExpr::getFPExtend(P1, DoubleTy), "fpext float " P1STR
246 " to double");
247
248 CHECK(ConstantExpr::getExactUDiv(P0, P0), "udiv exact i32 " P0STR ", " P0STR);
249
250 CHECK(ConstantExpr::getSelect(P3, P0, P4), "select i1 " P3STR ", i32 " P0STR
251 ", i32 " P4STR);
252 CHECK(ConstantExpr::getICmp(CmpInst::ICMP_EQ, P0, P4), "icmp eq i32 " P0STR
253 ", " P4STR);
254 CHECK(ConstantExpr::getFCmp(CmpInst::FCMP_ULT, P1, P5), "fcmp ult float "
255 P1STR ", " P5STR);
256
257 std::vector<Constant*> V;
258 V.push_back(One);
259 // FIXME: getGetElementPtr() actually creates an inbounds ConstantGEP,
260 // not a normal one!
261 //CHECK(ConstantExpr::getGetElementPtr(Global, V, false),
David Blaikie79e6c742015-02-27 19:29:02 +0000262 // "getelementptr i32*, i32** @dummy, i32 1");
David Blaikie4a2e73b2015-04-02 18:55:32 +0000263 CHECK(ConstantExpr::getInBoundsGetElementPtr(PointerType::getUnqual(Int32Ty),
264 Global, V),
David Blaikie79e6c742015-02-27 19:29:02 +0000265 "getelementptr inbounds i32*, i32** @dummy, i32 1");
James Molloyce545682012-11-17 17:56:30 +0000266
267 CHECK(ConstantExpr::getExtractElement(P6, One), "extractelement <2 x i16> "
268 P6STR ", i32 1");
Pawel Bylicabce9c2e2015-04-24 07:42:35 +0000269
Pawel Bylicac25918a2015-04-27 09:30:49 +0000270 EXPECT_EQ(Undef16, ConstantExpr::getExtractElement(P6, Two));
271 EXPECT_EQ(Undef16, ConstantExpr::getExtractElement(P6, Big));
272 EXPECT_EQ(Undef16, ConstantExpr::getExtractElement(P6, Undef64));
273
274 EXPECT_EQ(Elt, ConstantExpr::getExtractElement(
275 ConstantExpr::getInsertElement(P6, Elt, One), One));
276 EXPECT_EQ(UndefV16, ConstantExpr::getInsertElement(P6, Elt, Two));
277 EXPECT_EQ(UndefV16, ConstantExpr::getInsertElement(P6, Elt, Big));
278 EXPECT_EQ(UndefV16, ConstantExpr::getInsertElement(P6, Elt, Undef64));
James Molloyce545682012-11-17 17:56:30 +0000279}
280
Rafael Espindola7e2b7562014-05-13 01:23:21 +0000281#ifdef GTEST_HAS_DEATH_TEST
282#ifndef NDEBUG
283TEST(ConstantsTest, ReplaceWithConstantTest) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000284 LLVMContext Context;
285 std::unique_ptr<Module> M(new Module("MyModule", Context));
Rafael Espindola7e2b7562014-05-13 01:23:21 +0000286
Mehdi Amini03b42e42016-04-14 21:59:01 +0000287 Type *Int32Ty = Type::getInt32Ty(Context);
Rafael Espindola7e2b7562014-05-13 01:23:21 +0000288 Constant *One = ConstantInt::get(Int32Ty, 1);
289
290 Constant *Global =
291 M->getOrInsertGlobal("dummy", PointerType::getUnqual(Int32Ty));
David Blaikie4a2e73b2015-04-02 18:55:32 +0000292 Constant *GEP = ConstantExpr::getGetElementPtr(
293 PointerType::getUnqual(Int32Ty), Global, One);
Rafael Espindola7e2b7562014-05-13 01:23:21 +0000294 EXPECT_DEATH(Global->replaceAllUsesWith(GEP),
295 "this->replaceAllUsesWith\\(expr\\(this\\)\\) is NOT valid!");
296}
Rafael Espindola6b238632014-05-16 19:35:39 +0000297
Rafael Espindola7e2b7562014-05-13 01:23:21 +0000298#endif
299#endif
300
James Molloyce545682012-11-17 17:56:30 +0000301#undef CHECK
302
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000303TEST(ConstantsTest, ConstantArrayReplaceWithConstant) {
304 LLVMContext Context;
305 std::unique_ptr<Module> M(new Module("MyModule", Context));
306
307 Type *IntTy = Type::getInt8Ty(Context);
308 ArrayType *ArrayTy = ArrayType::get(IntTy, 2);
309 Constant *A01Vals[2] = {ConstantInt::get(IntTy, 0),
310 ConstantInt::get(IntTy, 1)};
311 Constant *A01 = ConstantArray::get(ArrayTy, A01Vals);
312
313 Constant *Global = new GlobalVariable(*M, IntTy, false,
314 GlobalValue::ExternalLinkage, nullptr);
315 Constant *GlobalInt = ConstantExpr::getPtrToInt(Global, IntTy);
316 Constant *A0GVals[2] = {ConstantInt::get(IntTy, 0), GlobalInt};
317 Constant *A0G = ConstantArray::get(ArrayTy, A0GVals);
318 ASSERT_NE(A01, A0G);
319
320 GlobalVariable *RefArray =
321 new GlobalVariable(*M, ArrayTy, false, GlobalValue::ExternalLinkage, A0G);
322 ASSERT_EQ(A0G, RefArray->getInitializer());
323
324 GlobalInt->replaceAllUsesWith(ConstantInt::get(IntTy, 1));
325 ASSERT_EQ(A01, RefArray->getInitializer());
326}
327
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +0000328TEST(ConstantsTest, ConstantExprReplaceWithConstant) {
329 LLVMContext Context;
330 std::unique_ptr<Module> M(new Module("MyModule", Context));
331
332 Type *IntTy = Type::getInt8Ty(Context);
333 Constant *G1 = new GlobalVariable(*M, IntTy, false,
334 GlobalValue::ExternalLinkage, nullptr);
335 Constant *G2 = new GlobalVariable(*M, IntTy, false,
336 GlobalValue::ExternalLinkage, nullptr);
337 ASSERT_NE(G1, G2);
338
339 Constant *Int1 = ConstantExpr::getPtrToInt(G1, IntTy);
340 Constant *Int2 = ConstantExpr::getPtrToInt(G2, IntTy);
341 ASSERT_NE(Int1, Int2);
342
343 GlobalVariable *Ref =
344 new GlobalVariable(*M, IntTy, false, GlobalValue::ExternalLinkage, Int1);
345 ASSERT_EQ(Int1, Ref->getInitializer());
346
347 G1->replaceAllUsesWith(G2);
348 ASSERT_EQ(Int2, Ref->getInitializer());
349}
350
Duncan P. N. Exon Smith35de5b82014-08-19 21:18:21 +0000351TEST(ConstantsTest, GEPReplaceWithConstant) {
352 LLVMContext Context;
353 std::unique_ptr<Module> M(new Module("MyModule", Context));
354
355 Type *IntTy = Type::getInt32Ty(Context);
David Blaikie16a2f3e2015-09-14 18:01:59 +0000356 Type *PtrTy = PointerType::get(IntTy, 0);
Duncan P. N. Exon Smith35de5b82014-08-19 21:18:21 +0000357 auto *C1 = ConstantInt::get(IntTy, 1);
358 auto *Placeholder = new GlobalVariable(
359 *M, IntTy, false, GlobalValue::ExternalWeakLinkage, nullptr);
David Blaikie4a2e73b2015-04-02 18:55:32 +0000360 auto *GEP = ConstantExpr::getGetElementPtr(IntTy, Placeholder, C1);
Duncan P. N. Exon Smith35de5b82014-08-19 21:18:21 +0000361 ASSERT_EQ(GEP->getOperand(0), Placeholder);
362
363 auto *Ref =
364 new GlobalVariable(*M, PtrTy, false, GlobalValue::ExternalLinkage, GEP);
365 ASSERT_EQ(GEP, Ref->getInitializer());
366
367 auto *Global = new GlobalVariable(*M, PtrTy, false,
368 GlobalValue::ExternalLinkage, nullptr);
David Blaikie16a2f3e2015-09-14 18:01:59 +0000369 auto *Alias = GlobalAlias::create(IntTy, 0, GlobalValue::ExternalLinkage,
Duncan P. N. Exon Smith35de5b82014-08-19 21:18:21 +0000370 "alias", Global, M.get());
371 Placeholder->replaceAllUsesWith(Alias);
372 ASSERT_EQ(GEP, Ref->getInitializer());
373 ASSERT_EQ(GEP->getOperand(0), Alias);
374}
375
Rafael Espindola993502e2015-02-23 21:51:06 +0000376TEST(ConstantsTest, AliasCAPI) {
377 LLVMContext Context;
378 SMDiagnostic Error;
379 std::unique_ptr<Module> M =
380 parseAssemblyString("@g = global i32 42", Error, Context);
381 GlobalVariable *G = M->getGlobalVariable("g");
382 Type *I16Ty = Type::getInt16Ty(Context);
383 Type *I16PTy = PointerType::get(I16Ty, 0);
384 Constant *Aliasee = ConstantExpr::getBitCast(G, I16PTy);
385 LLVMValueRef AliasRef =
386 LLVMAddAlias(wrap(M.get()), wrap(I16PTy), wrap(Aliasee), "a");
387 ASSERT_EQ(unwrap<GlobalAlias>(AliasRef)->getAliasee(), Aliasee);
388}
389
Justin Bogner0ebc8602015-12-08 03:01:16 +0000390static std::string getNameOfType(Type *T) {
391 std::string S;
392 raw_string_ostream RSOS(S);
393 T->print(RSOS);
394 return S;
395}
396
Justin Bognerb7389d6712015-12-09 21:21:07 +0000397TEST(ConstantsTest, BuildConstantDataArrays) {
398 LLVMContext Context;
399 std::unique_ptr<Module> M(new Module("MyModule", Context));
400
401 for (Type *T : {Type::getInt8Ty(Context), Type::getInt16Ty(Context),
402 Type::getInt32Ty(Context), Type::getInt64Ty(Context)}) {
403 ArrayType *ArrayTy = ArrayType::get(T, 2);
404 Constant *Vals[] = {ConstantInt::get(T, 0), ConstantInt::get(T, 1)};
405 Constant *CDV = ConstantArray::get(ArrayTy, Vals);
406 ASSERT_TRUE(dyn_cast<ConstantDataArray>(CDV) != nullptr)
407 << " T = " << getNameOfType(T);
408 }
409
410 for (Type *T : {Type::getHalfTy(Context), Type::getFloatTy(Context),
411 Type::getDoubleTy(Context)}) {
412 ArrayType *ArrayTy = ArrayType::get(T, 2);
413 Constant *Vals[] = {ConstantFP::get(T, 0), ConstantFP::get(T, 1)};
414 Constant *CDV = ConstantArray::get(ArrayTy, Vals);
415 ASSERT_TRUE(dyn_cast<ConstantDataArray>(CDV) != nullptr)
416 << " T = " << getNameOfType(T);
417 }
418}
419
Justin Bogner0ebc8602015-12-08 03:01:16 +0000420TEST(ConstantsTest, BuildConstantDataVectors) {
421 LLVMContext Context;
422 std::unique_ptr<Module> M(new Module("MyModule", Context));
423
424 for (Type *T : {Type::getInt8Ty(Context), Type::getInt16Ty(Context),
425 Type::getInt32Ty(Context), Type::getInt64Ty(Context)}) {
426 Constant *Vals[] = {ConstantInt::get(T, 0), ConstantInt::get(T, 1)};
427 Constant *CDV = ConstantVector::get(Vals);
428 ASSERT_TRUE(dyn_cast<ConstantDataVector>(CDV) != nullptr)
429 << " T = " << getNameOfType(T);
430 }
431
432 for (Type *T : {Type::getHalfTy(Context), Type::getFloatTy(Context),
433 Type::getDoubleTy(Context)}) {
434 Constant *Vals[] = {ConstantFP::get(T, 0), ConstantFP::get(T, 1)};
435 Constant *CDV = ConstantVector::get(Vals);
436 ASSERT_TRUE(dyn_cast<ConstantDataVector>(CDV) != nullptr)
437 << " T = " << getNameOfType(T);
438 }
439}
440
David Majnemer59be1d62015-12-14 19:30:32 +0000441TEST(ConstantsTest, BitcastToGEP) {
442 LLVMContext Context;
443 std::unique_ptr<Module> M(new Module("MyModule", Context));
444
445 auto *i32 = Type::getInt32Ty(Context);
446 auto *U = StructType::create(Context, "Unsized");
447 Type *EltTys[] = {i32, U};
448 auto *S = StructType::create(EltTys);
449
450 auto *G = new GlobalVariable(*M, S, false,
451 GlobalValue::ExternalLinkage, nullptr);
452 auto *PtrTy = PointerType::get(i32, 0);
453 auto *C = ConstantExpr::getBitCast(G, PtrTy);
454 ASSERT_EQ(dyn_cast<ConstantExpr>(C)->getOpcode(),
455 Instruction::BitCast);
456}
457
Misha Brukmand1d2c502009-03-24 21:36:09 +0000458} // end anonymous namespace
459} // end namespace llvm