1 //===- llvm/unittest/IR/ConstantsTest.cpp - Constants unit tests ----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/IR/Constants.h"
10 #include "llvm-c/Core.h"
11 #include "llvm/AsmParser/Parser.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"
17 #include "llvm/Support/SourceMgr.h"
18 #include "gtest/gtest.h"
23 TEST(ConstantsTest
, Integer_i1
) {
25 IntegerType
*Int1
= IntegerType::get(Context
, 1);
26 Constant
* One
= ConstantInt::get(Int1
, 1, true);
27 Constant
* Zero
= ConstantInt::get(Int1
, 0);
28 Constant
* NegOne
= ConstantInt::get(Int1
, static_cast<uint64_t>(-1), true);
29 EXPECT_EQ(NegOne
, ConstantInt::getSigned(Int1
, -1));
30 Constant
* Undef
= UndefValue::get(Int1
);
32 // Input: @b = constant i1 add(i1 1 , i1 1)
33 // Output: @b = constant i1 false
34 EXPECT_EQ(Zero
, ConstantExpr::getAdd(One
, One
));
36 // @c = constant i1 add(i1 -1, i1 1)
37 // @c = constant i1 false
38 EXPECT_EQ(Zero
, ConstantExpr::getAdd(NegOne
, One
));
40 // @d = constant i1 add(i1 -1, i1 -1)
41 // @d = constant i1 false
42 EXPECT_EQ(Zero
, ConstantExpr::getAdd(NegOne
, NegOne
));
44 // @e = constant i1 sub(i1 -1, i1 1)
45 // @e = constant i1 false
46 EXPECT_EQ(Zero
, ConstantExpr::getSub(NegOne
, One
));
48 // @f = constant i1 sub(i1 1 , i1 -1)
49 // @f = constant i1 false
50 EXPECT_EQ(Zero
, ConstantExpr::getSub(One
, NegOne
));
52 // @g = constant i1 sub(i1 1 , i1 1)
53 // @g = constant i1 false
54 EXPECT_EQ(Zero
, ConstantExpr::getSub(One
, One
));
56 // @h = constant i1 shl(i1 1 , i1 1) ; undefined
57 // @h = constant i1 undef
58 EXPECT_EQ(Undef
, ConstantExpr::getShl(One
, One
));
60 // @i = constant i1 shl(i1 1 , i1 0)
61 // @i = constant i1 true
62 EXPECT_EQ(One
, ConstantExpr::getShl(One
, Zero
));
64 // @j = constant i1 lshr(i1 1, i1 1) ; undefined
65 // @j = constant i1 undef
66 EXPECT_EQ(Undef
, ConstantExpr::getLShr(One
, One
));
68 // @m = constant i1 ashr(i1 1, i1 1) ; undefined
69 // @m = constant i1 undef
70 EXPECT_EQ(Undef
, ConstantExpr::getAShr(One
, One
));
72 // @n = constant i1 mul(i1 -1, i1 1)
73 // @n = constant i1 true
74 EXPECT_EQ(One
, ConstantExpr::getMul(NegOne
, One
));
76 // @o = constant i1 sdiv(i1 -1, i1 1) ; overflow
77 // @o = constant i1 true
78 EXPECT_EQ(One
, ConstantExpr::getSDiv(NegOne
, One
));
80 // @p = constant i1 sdiv(i1 1 , i1 -1); overflow
81 // @p = constant i1 true
82 EXPECT_EQ(One
, ConstantExpr::getSDiv(One
, NegOne
));
84 // @q = constant i1 udiv(i1 -1, i1 1)
85 // @q = constant i1 true
86 EXPECT_EQ(One
, ConstantExpr::getUDiv(NegOne
, One
));
88 // @r = constant i1 udiv(i1 1, i1 -1)
89 // @r = constant i1 true
90 EXPECT_EQ(One
, ConstantExpr::getUDiv(One
, NegOne
));
92 // @s = constant i1 srem(i1 -1, i1 1) ; overflow
93 // @s = constant i1 false
94 EXPECT_EQ(Zero
, ConstantExpr::getSRem(NegOne
, One
));
96 // @t = constant i1 urem(i1 -1, i1 1)
97 // @t = constant i1 false
98 EXPECT_EQ(Zero
, ConstantExpr::getURem(NegOne
, One
));
100 // @u = constant i1 srem(i1 1, i1 -1) ; overflow
101 // @u = constant i1 false
102 EXPECT_EQ(Zero
, ConstantExpr::getSRem(One
, NegOne
));
105 TEST(ConstantsTest
, IntSigns
) {
107 IntegerType
*Int8Ty
= Type::getInt8Ty(Context
);
108 EXPECT_EQ(100, ConstantInt::get(Int8Ty
, 100, false)->getSExtValue());
109 EXPECT_EQ(100, ConstantInt::get(Int8Ty
, 100, true)->getSExtValue());
110 EXPECT_EQ(100, ConstantInt::getSigned(Int8Ty
, 100)->getSExtValue());
111 EXPECT_EQ(-50, ConstantInt::get(Int8Ty
, 206)->getSExtValue());
112 EXPECT_EQ(-50, ConstantInt::getSigned(Int8Ty
, -50)->getSExtValue());
113 EXPECT_EQ(206U, ConstantInt::getSigned(Int8Ty
, -50)->getZExtValue());
115 // Overflow is handled by truncation.
116 EXPECT_EQ(0x3b, ConstantInt::get(Int8Ty
, 0x13b)->getSExtValue());
119 TEST(ConstantsTest
, FP128Test
) {
121 Type
*FP128Ty
= Type::getFP128Ty(Context
);
123 IntegerType
*Int128Ty
= Type::getIntNTy(Context
, 128);
124 Constant
*Zero128
= Constant::getNullValue(Int128Ty
);
125 Constant
*X
= ConstantExpr::getUIToFP(Zero128
, FP128Ty
);
126 EXPECT_TRUE(isa
<ConstantFP
>(X
));
129 TEST(ConstantsTest
, PointerCast
) {
131 Type
*Int8PtrTy
= Type::getInt8PtrTy(C
);
132 Type
*Int32PtrTy
= Type::getInt32PtrTy(C
);
133 Type
*Int64Ty
= Type::getInt64Ty(C
);
134 VectorType
*Int8PtrVecTy
= VectorType::get(Int8PtrTy
, 4);
135 VectorType
*Int32PtrVecTy
= VectorType::get(Int32PtrTy
, 4);
136 VectorType
*Int64VecTy
= VectorType::get(Int64Ty
, 4);
138 // ptrtoint i8* to i64
139 EXPECT_EQ(Constant::getNullValue(Int64Ty
),
140 ConstantExpr::getPointerCast(
141 Constant::getNullValue(Int8PtrTy
), Int64Ty
));
143 // bitcast i8* to i32*
144 EXPECT_EQ(Constant::getNullValue(Int32PtrTy
),
145 ConstantExpr::getPointerCast(
146 Constant::getNullValue(Int8PtrTy
), Int32PtrTy
));
148 // ptrtoint <4 x i8*> to <4 x i64>
149 EXPECT_EQ(Constant::getNullValue(Int64VecTy
),
150 ConstantExpr::getPointerCast(
151 Constant::getNullValue(Int8PtrVecTy
), Int64VecTy
));
153 // bitcast <4 x i8*> to <4 x i32*>
154 EXPECT_EQ(Constant::getNullValue(Int32PtrVecTy
),
155 ConstantExpr::getPointerCast(
156 Constant::getNullValue(Int8PtrVecTy
), Int32PtrVecTy
));
158 Type
*Int32Ptr1Ty
= Type::getInt32PtrTy(C
, 1);
159 ConstantInt
*K
= ConstantInt::get(Type::getInt64Ty(C
), 1234);
161 // Make sure that addrspacecast of inttoptr is not folded away.
163 ConstantExpr::getAddrSpaceCast(
164 ConstantExpr::getIntToPtr(K
, Int32PtrTy
), Int32Ptr1Ty
));
166 ConstantExpr::getAddrSpaceCast(
167 ConstantExpr::getIntToPtr(K
, Int32Ptr1Ty
), Int32PtrTy
));
169 Constant
*NullInt32Ptr0
= Constant::getNullValue(Int32PtrTy
);
170 Constant
*NullInt32Ptr1
= Constant::getNullValue(Int32Ptr1Ty
);
172 // Make sure that addrspacecast of null is not folded away.
173 EXPECT_NE(Constant::getNullValue(Int32PtrTy
),
174 ConstantExpr::getAddrSpaceCast(NullInt32Ptr0
, Int32Ptr1Ty
));
176 EXPECT_NE(Constant::getNullValue(Int32Ptr1Ty
),
177 ConstantExpr::getAddrSpaceCast(NullInt32Ptr1
, Int32PtrTy
));
180 #define CHECK(x, y) \
183 raw_string_ostream __o(__s); \
184 Instruction *__I = cast<ConstantExpr>(x)->getAsInstruction(); \
186 __I->deleteValue(); \
188 EXPECT_EQ(std::string(" <badref> = " y), __s); \
191 TEST(ConstantsTest
, AsInstructionsTest
) {
193 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
195 Type
*Int64Ty
= Type::getInt64Ty(Context
);
196 Type
*Int32Ty
= Type::getInt32Ty(Context
);
197 Type
*Int16Ty
= Type::getInt16Ty(Context
);
198 Type
*Int1Ty
= Type::getInt1Ty(Context
);
199 Type
*FloatTy
= Type::getFloatTy(Context
);
200 Type
*DoubleTy
= Type::getDoubleTy(Context
);
202 Constant
*Global
= M
->getOrInsertGlobal("dummy",
203 PointerType::getUnqual(Int32Ty
));
204 Constant
*Global2
= M
->getOrInsertGlobal("dummy2",
205 PointerType::getUnqual(Int32Ty
));
207 Constant
*P0
= ConstantExpr::getPtrToInt(Global
, Int32Ty
);
208 Constant
*P1
= ConstantExpr::getUIToFP(P0
, FloatTy
);
209 Constant
*P2
= ConstantExpr::getUIToFP(P0
, DoubleTy
);
210 Constant
*P3
= ConstantExpr::getTrunc(P0
, Int1Ty
);
211 Constant
*P4
= ConstantExpr::getPtrToInt(Global2
, Int32Ty
);
212 Constant
*P5
= ConstantExpr::getUIToFP(P4
, FloatTy
);
213 Constant
*P6
= ConstantExpr::getBitCast(P4
, VectorType::get(Int16Ty
, 2));
215 Constant
*One
= ConstantInt::get(Int32Ty
, 1);
216 Constant
*Two
= ConstantInt::get(Int64Ty
, 2);
217 Constant
*Big
= ConstantInt::get(Context
, APInt
{256, uint64_t(-1), true});
218 Constant
*Elt
= ConstantInt::get(Int16Ty
, 2015);
219 Constant
*Undef16
= UndefValue::get(Int16Ty
);
220 Constant
*Undef64
= UndefValue::get(Int64Ty
);
221 Constant
*UndefV16
= UndefValue::get(P6
->getType());
223 #define P0STR "ptrtoint (i32** @dummy to i32)"
224 #define P1STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to float)"
225 #define P2STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to double)"
226 #define P3STR "ptrtoint (i32** @dummy to i1)"
227 #define P4STR "ptrtoint (i32** @dummy2 to i32)"
228 #define P5STR "uitofp (i32 ptrtoint (i32** @dummy2 to i32) to float)"
229 #define P6STR "bitcast (i32 ptrtoint (i32** @dummy2 to i32) to <2 x i16>)"
231 CHECK(ConstantExpr::getNeg(P0
), "sub i32 0, " P0STR
);
232 CHECK(ConstantExpr::getFNeg(P1
), "fneg float " P1STR
);
233 CHECK(ConstantExpr::getNot(P0
), "xor i32 " P0STR
", -1");
234 CHECK(ConstantExpr::getAdd(P0
, P0
), "add i32 " P0STR
", " P0STR
);
235 CHECK(ConstantExpr::getAdd(P0
, P0
, false, true), "add nsw i32 " P0STR
", "
237 CHECK(ConstantExpr::getAdd(P0
, P0
, true, true), "add nuw nsw i32 " P0STR
", "
239 CHECK(ConstantExpr::getFAdd(P1
, P1
), "fadd float " P1STR
", " P1STR
);
240 CHECK(ConstantExpr::getSub(P0
, P0
), "sub i32 " P0STR
", " P0STR
);
241 CHECK(ConstantExpr::getFSub(P1
, P1
), "fsub float " P1STR
", " P1STR
);
242 CHECK(ConstantExpr::getMul(P0
, P0
), "mul i32 " P0STR
", " P0STR
);
243 CHECK(ConstantExpr::getFMul(P1
, P1
), "fmul float " P1STR
", " P1STR
);
244 CHECK(ConstantExpr::getUDiv(P0
, P0
), "udiv i32 " P0STR
", " P0STR
);
245 CHECK(ConstantExpr::getSDiv(P0
, P0
), "sdiv i32 " P0STR
", " P0STR
);
246 CHECK(ConstantExpr::getFDiv(P1
, P1
), "fdiv float " P1STR
", " P1STR
);
247 CHECK(ConstantExpr::getURem(P0
, P0
), "urem i32 " P0STR
", " P0STR
);
248 CHECK(ConstantExpr::getSRem(P0
, P0
), "srem i32 " P0STR
", " P0STR
);
249 CHECK(ConstantExpr::getFRem(P1
, P1
), "frem float " P1STR
", " P1STR
);
250 CHECK(ConstantExpr::getAnd(P0
, P0
), "and i32 " P0STR
", " P0STR
);
251 CHECK(ConstantExpr::getOr(P0
, P0
), "or i32 " P0STR
", " P0STR
);
252 CHECK(ConstantExpr::getXor(P0
, P0
), "xor i32 " P0STR
", " P0STR
);
253 CHECK(ConstantExpr::getShl(P0
, P0
), "shl i32 " P0STR
", " P0STR
);
254 CHECK(ConstantExpr::getShl(P0
, P0
, true), "shl nuw i32 " P0STR
", " P0STR
);
255 CHECK(ConstantExpr::getShl(P0
, P0
, false, true), "shl nsw i32 " P0STR
", "
257 CHECK(ConstantExpr::getLShr(P0
, P0
, false), "lshr i32 " P0STR
", " P0STR
);
258 CHECK(ConstantExpr::getLShr(P0
, P0
, true), "lshr exact i32 " P0STR
", " P0STR
);
259 CHECK(ConstantExpr::getAShr(P0
, P0
, false), "ashr i32 " P0STR
", " P0STR
);
260 CHECK(ConstantExpr::getAShr(P0
, P0
, true), "ashr exact i32 " P0STR
", " P0STR
);
262 CHECK(ConstantExpr::getSExt(P0
, Int64Ty
), "sext i32 " P0STR
" to i64");
263 CHECK(ConstantExpr::getZExt(P0
, Int64Ty
), "zext i32 " P0STR
" to i64");
264 CHECK(ConstantExpr::getFPTrunc(P2
, FloatTy
), "fptrunc double " P2STR
266 CHECK(ConstantExpr::getFPExtend(P1
, DoubleTy
), "fpext float " P1STR
269 CHECK(ConstantExpr::getExactUDiv(P0
, P0
), "udiv exact i32 " P0STR
", " P0STR
);
271 CHECK(ConstantExpr::getSelect(P3
, P0
, P4
), "select i1 " P3STR
", i32 " P0STR
273 CHECK(ConstantExpr::getICmp(CmpInst::ICMP_EQ
, P0
, P4
), "icmp eq i32 " P0STR
275 CHECK(ConstantExpr::getFCmp(CmpInst::FCMP_ULT
, P1
, P5
), "fcmp ult float "
278 std::vector
<Constant
*> V
;
280 // FIXME: getGetElementPtr() actually creates an inbounds ConstantGEP,
282 //CHECK(ConstantExpr::getGetElementPtr(Global, V, false),
283 // "getelementptr i32*, i32** @dummy, i32 1");
284 CHECK(ConstantExpr::getInBoundsGetElementPtr(PointerType::getUnqual(Int32Ty
),
286 "getelementptr inbounds i32*, i32** @dummy, i32 1");
288 CHECK(ConstantExpr::getExtractElement(P6
, One
), "extractelement <2 x i16> "
291 EXPECT_EQ(Undef16
, ConstantExpr::getExtractElement(P6
, Two
));
292 EXPECT_EQ(Undef16
, ConstantExpr::getExtractElement(P6
, Big
));
293 EXPECT_EQ(Undef16
, ConstantExpr::getExtractElement(P6
, Undef64
));
295 EXPECT_EQ(Elt
, ConstantExpr::getExtractElement(
296 ConstantExpr::getInsertElement(P6
, Elt
, One
), One
));
297 EXPECT_EQ(UndefV16
, ConstantExpr::getInsertElement(P6
, Elt
, Two
));
298 EXPECT_EQ(UndefV16
, ConstantExpr::getInsertElement(P6
, Elt
, Big
));
299 EXPECT_EQ(UndefV16
, ConstantExpr::getInsertElement(P6
, Elt
, Undef64
));
302 #ifdef GTEST_HAS_DEATH_TEST
304 TEST(ConstantsTest
, ReplaceWithConstantTest
) {
306 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
308 Type
*Int32Ty
= Type::getInt32Ty(Context
);
309 Constant
*One
= ConstantInt::get(Int32Ty
, 1);
312 M
->getOrInsertGlobal("dummy", PointerType::getUnqual(Int32Ty
));
313 Constant
*GEP
= ConstantExpr::getGetElementPtr(
314 PointerType::getUnqual(Int32Ty
), Global
, One
);
315 EXPECT_DEATH(Global
->replaceAllUsesWith(GEP
),
316 "this->replaceAllUsesWith\\(expr\\(this\\)\\) is NOT valid!");
324 TEST(ConstantsTest
, ConstantArrayReplaceWithConstant
) {
326 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
328 Type
*IntTy
= Type::getInt8Ty(Context
);
329 ArrayType
*ArrayTy
= ArrayType::get(IntTy
, 2);
330 Constant
*A01Vals
[2] = {ConstantInt::get(IntTy
, 0),
331 ConstantInt::get(IntTy
, 1)};
332 Constant
*A01
= ConstantArray::get(ArrayTy
, A01Vals
);
334 Constant
*Global
= new GlobalVariable(*M
, IntTy
, false,
335 GlobalValue::ExternalLinkage
, nullptr);
336 Constant
*GlobalInt
= ConstantExpr::getPtrToInt(Global
, IntTy
);
337 Constant
*A0GVals
[2] = {ConstantInt::get(IntTy
, 0), GlobalInt
};
338 Constant
*A0G
= ConstantArray::get(ArrayTy
, A0GVals
);
341 GlobalVariable
*RefArray
=
342 new GlobalVariable(*M
, ArrayTy
, false, GlobalValue::ExternalLinkage
, A0G
);
343 ASSERT_EQ(A0G
, RefArray
->getInitializer());
345 GlobalInt
->replaceAllUsesWith(ConstantInt::get(IntTy
, 1));
346 ASSERT_EQ(A01
, RefArray
->getInitializer());
349 TEST(ConstantsTest
, ConstantExprReplaceWithConstant
) {
351 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
353 Type
*IntTy
= Type::getInt8Ty(Context
);
354 Constant
*G1
= new GlobalVariable(*M
, IntTy
, false,
355 GlobalValue::ExternalLinkage
, nullptr);
356 Constant
*G2
= new GlobalVariable(*M
, IntTy
, false,
357 GlobalValue::ExternalLinkage
, nullptr);
360 Constant
*Int1
= ConstantExpr::getPtrToInt(G1
, IntTy
);
361 Constant
*Int2
= ConstantExpr::getPtrToInt(G2
, IntTy
);
362 ASSERT_NE(Int1
, Int2
);
364 GlobalVariable
*Ref
=
365 new GlobalVariable(*M
, IntTy
, false, GlobalValue::ExternalLinkage
, Int1
);
366 ASSERT_EQ(Int1
, Ref
->getInitializer());
368 G1
->replaceAllUsesWith(G2
);
369 ASSERT_EQ(Int2
, Ref
->getInitializer());
372 TEST(ConstantsTest
, GEPReplaceWithConstant
) {
374 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
376 Type
*IntTy
= Type::getInt32Ty(Context
);
377 Type
*PtrTy
= PointerType::get(IntTy
, 0);
378 auto *C1
= ConstantInt::get(IntTy
, 1);
379 auto *Placeholder
= new GlobalVariable(
380 *M
, IntTy
, false, GlobalValue::ExternalWeakLinkage
, nullptr);
381 auto *GEP
= ConstantExpr::getGetElementPtr(IntTy
, Placeholder
, C1
);
382 ASSERT_EQ(GEP
->getOperand(0), Placeholder
);
385 new GlobalVariable(*M
, PtrTy
, false, GlobalValue::ExternalLinkage
, GEP
);
386 ASSERT_EQ(GEP
, Ref
->getInitializer());
388 auto *Global
= new GlobalVariable(*M
, PtrTy
, false,
389 GlobalValue::ExternalLinkage
, nullptr);
390 auto *Alias
= GlobalAlias::create(IntTy
, 0, GlobalValue::ExternalLinkage
,
391 "alias", Global
, M
.get());
392 Placeholder
->replaceAllUsesWith(Alias
);
393 ASSERT_EQ(GEP
, Ref
->getInitializer());
394 ASSERT_EQ(GEP
->getOperand(0), Alias
);
397 TEST(ConstantsTest
, AliasCAPI
) {
400 std::unique_ptr
<Module
> M
=
401 parseAssemblyString("@g = global i32 42", Error
, Context
);
402 GlobalVariable
*G
= M
->getGlobalVariable("g");
403 Type
*I16Ty
= Type::getInt16Ty(Context
);
404 Type
*I16PTy
= PointerType::get(I16Ty
, 0);
405 Constant
*Aliasee
= ConstantExpr::getBitCast(G
, I16PTy
);
406 LLVMValueRef AliasRef
=
407 LLVMAddAlias(wrap(M
.get()), wrap(I16PTy
), wrap(Aliasee
), "a");
408 ASSERT_EQ(unwrap
<GlobalAlias
>(AliasRef
)->getAliasee(), Aliasee
);
411 static std::string
getNameOfType(Type
*T
) {
413 raw_string_ostream
RSOS(S
);
418 TEST(ConstantsTest
, BuildConstantDataArrays
) {
420 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
422 for (Type
*T
: {Type::getInt8Ty(Context
), Type::getInt16Ty(Context
),
423 Type::getInt32Ty(Context
), Type::getInt64Ty(Context
)}) {
424 ArrayType
*ArrayTy
= ArrayType::get(T
, 2);
425 Constant
*Vals
[] = {ConstantInt::get(T
, 0), ConstantInt::get(T
, 1)};
426 Constant
*CDV
= ConstantArray::get(ArrayTy
, Vals
);
427 ASSERT_TRUE(dyn_cast
<ConstantDataArray
>(CDV
) != nullptr)
428 << " T = " << getNameOfType(T
);
431 for (Type
*T
: {Type::getHalfTy(Context
), Type::getFloatTy(Context
),
432 Type::getDoubleTy(Context
)}) {
433 ArrayType
*ArrayTy
= ArrayType::get(T
, 2);
434 Constant
*Vals
[] = {ConstantFP::get(T
, 0), ConstantFP::get(T
, 1)};
435 Constant
*CDV
= ConstantArray::get(ArrayTy
, Vals
);
436 ASSERT_TRUE(dyn_cast
<ConstantDataArray
>(CDV
) != nullptr)
437 << " T = " << getNameOfType(T
);
441 TEST(ConstantsTest
, BuildConstantDataVectors
) {
443 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
445 for (Type
*T
: {Type::getInt8Ty(Context
), Type::getInt16Ty(Context
),
446 Type::getInt32Ty(Context
), Type::getInt64Ty(Context
)}) {
447 Constant
*Vals
[] = {ConstantInt::get(T
, 0), ConstantInt::get(T
, 1)};
448 Constant
*CDV
= ConstantVector::get(Vals
);
449 ASSERT_TRUE(dyn_cast
<ConstantDataVector
>(CDV
) != nullptr)
450 << " T = " << getNameOfType(T
);
453 for (Type
*T
: {Type::getHalfTy(Context
), Type::getFloatTy(Context
),
454 Type::getDoubleTy(Context
)}) {
455 Constant
*Vals
[] = {ConstantFP::get(T
, 0), ConstantFP::get(T
, 1)};
456 Constant
*CDV
= ConstantVector::get(Vals
);
457 ASSERT_TRUE(dyn_cast
<ConstantDataVector
>(CDV
) != nullptr)
458 << " T = " << getNameOfType(T
);
462 TEST(ConstantsTest
, BitcastToGEP
) {
464 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
466 auto *i32
= Type::getInt32Ty(Context
);
467 auto *U
= StructType::create(Context
, "Unsized");
468 Type
*EltTys
[] = {i32
, U
};
469 auto *S
= StructType::create(EltTys
);
471 auto *G
= new GlobalVariable(*M
, S
, false,
472 GlobalValue::ExternalLinkage
, nullptr);
473 auto *PtrTy
= PointerType::get(i32
, 0);
474 auto *C
= ConstantExpr::getBitCast(G
, PtrTy
);
475 ASSERT_EQ(cast
<ConstantExpr
>(C
)->getOpcode(), Instruction::BitCast
);
478 bool foldFuncPtrAndConstToNull(LLVMContext
&Context
, Module
*TheModule
,
479 uint64_t AndValue
, unsigned FunctionAlign
= 0) {
480 Type
*VoidType(Type::getVoidTy(Context
));
481 FunctionType
*FuncType(FunctionType::get(VoidType
, false));
482 Function
*Func(Function::Create(
483 FuncType
, GlobalValue::ExternalLinkage
, "", TheModule
));
485 if (FunctionAlign
) Func
->setAlignment(FunctionAlign
);
487 IntegerType
*ConstantIntType(Type::getInt32Ty(Context
));
488 ConstantInt
*TheConstant(ConstantInt::get(ConstantIntType
, AndValue
));
490 Constant
*TheConstantExpr(
491 ConstantExpr::getPtrToInt(Func
, ConstantIntType
));
494 bool result
= ConstantExpr::get(Instruction::And
, TheConstantExpr
,
495 TheConstant
)->isNullValue();
498 // If the Module exists then it will delete the Function.
505 TEST(ConstantsTest
, FoldFunctionPtrAlignUnknownAnd2
) {
507 Module
TheModule("TestModule", Context
);
508 // When the DataLayout doesn't specify a function pointer alignment we
509 // assume in this case that it is 4 byte aligned. This is a bug but we can't
510 // fix it directly because it causes a code size regression on X86.
511 // FIXME: This test should be changed once existing targets have
512 // appropriate defaults. See associated FIXME in ConstantFoldBinaryInstruction
513 ASSERT_TRUE(foldFuncPtrAndConstToNull(Context
, &TheModule
, 2));
516 TEST(ConstantsTest
, DontFoldFunctionPtrAlignUnknownAnd4
) {
518 Module
TheModule("TestModule", Context
);
519 ASSERT_FALSE(foldFuncPtrAndConstToNull(Context
, &TheModule
, 4));
522 TEST(ConstantsTest
, FoldFunctionPtrAlign4
) {
524 Module
TheModule("TestModule", Context
);
525 const char* AlignmentStrings
[] = { "Fi32", "Fn32" };
527 for (unsigned AndValue
= 1; AndValue
<= 2; ++AndValue
) {
528 for (const char *AlignmentString
: AlignmentStrings
) {
529 TheModule
.setDataLayout(AlignmentString
);
530 ASSERT_TRUE(foldFuncPtrAndConstToNull(Context
, &TheModule
, AndValue
));
535 TEST(ConstantsTest
, DontFoldFunctionPtrAlign1
) {
537 Module
TheModule("TestModule", Context
);
538 const char* AlignmentStrings
[] = { "Fi8", "Fn8" };
540 for (const char* AlignmentString
: AlignmentStrings
) {
541 TheModule
.setDataLayout(AlignmentString
);
542 ASSERT_FALSE(foldFuncPtrAndConstToNull(Context
, &TheModule
, 2));
546 TEST(ConstantsTest
, FoldFunctionAlign4PtrAlignMultiple
) {
548 Module
TheModule("TestModule", Context
);
549 TheModule
.setDataLayout("Fn8");
550 ASSERT_TRUE(foldFuncPtrAndConstToNull(Context
, &TheModule
, 2, 4));
553 TEST(ConstantsTest
, DontFoldFunctionAlign4PtrAlignIndependent
) {
555 Module
TheModule("TestModule", Context
);
556 TheModule
.setDataLayout("Fi8");
557 ASSERT_FALSE(foldFuncPtrAndConstToNull(Context
, &TheModule
, 2, 4));
560 TEST(ConstantsTest
, DontFoldFunctionPtrIfNoModule
) {
562 // Even though the function is explicitly 4 byte aligned, in the absence of a
563 // DataLayout we can't assume that the function pointer is aligned.
564 ASSERT_FALSE(foldFuncPtrAndConstToNull(Context
, nullptr, 2, 4));
567 TEST(ConstantsTest
, FoldGlobalVariablePtr
) {
570 IntegerType
*IntType(Type::getInt32Ty(Context
));
572 std::unique_ptr
<GlobalVariable
> Global(
573 new GlobalVariable(IntType
, true, GlobalValue::ExternalLinkage
));
575 Global
->setAlignment(4);
577 ConstantInt
*TheConstant(ConstantInt::get(IntType
, 2));
579 Constant
*TheConstantExpr(
580 ConstantExpr::getPtrToInt(Global
.get(), IntType
));
582 ASSERT_TRUE(ConstantExpr::get( \
583 Instruction::And
, TheConstantExpr
, TheConstant
)->isNullValue());
586 } // end anonymous namespace
587 } // end namespace llvm