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/ConstantFold.h"
13 #include "llvm/IR/DerivedTypes.h"
14 #include "llvm/IR/InstrTypes.h"
15 #include "llvm/IR/Instruction.h"
16 #include "llvm/IR/LLVMContext.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/Support/SourceMgr.h"
19 #include "gtest/gtest.h"
24 TEST(ConstantsTest
, Integer_i1
) {
26 IntegerType
*Int1
= IntegerType::get(Context
, 1);
27 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));
31 Constant
*Poison
= PoisonValue::get(Int1
);
33 // Input: @b = constant i1 add(i1 1 , i1 1)
34 // Output: @b = constant i1 false
35 EXPECT_EQ(Zero
, ConstantExpr::getAdd(One
, One
));
37 // @c = constant i1 add(i1 -1, i1 1)
38 // @c = constant i1 false
39 EXPECT_EQ(Zero
, ConstantExpr::getAdd(NegOne
, One
));
41 // @d = constant i1 add(i1 -1, i1 -1)
42 // @d = constant i1 false
43 EXPECT_EQ(Zero
, ConstantExpr::getAdd(NegOne
, NegOne
));
45 // @e = constant i1 sub(i1 -1, i1 1)
46 // @e = constant i1 false
47 EXPECT_EQ(Zero
, ConstantExpr::getSub(NegOne
, One
));
49 // @f = constant i1 sub(i1 1 , i1 -1)
50 // @f = constant i1 false
51 EXPECT_EQ(Zero
, ConstantExpr::getSub(One
, NegOne
));
53 // @g = constant i1 sub(i1 1 , i1 1)
54 // @g = constant i1 false
55 EXPECT_EQ(Zero
, ConstantExpr::getSub(One
, One
));
57 // @h = constant i1 shl(i1 1 , i1 1) ; poison
58 // @h = constant i1 poison
59 EXPECT_EQ(Poison
, ConstantExpr::getShl(One
, One
));
61 // @i = constant i1 shl(i1 1 , i1 0)
62 // @i = constant i1 true
63 EXPECT_EQ(One
, ConstantExpr::getShl(One
, Zero
));
65 // @j = constant i1 lshr(i1 1, i1 1) ; poison
66 // @j = constant i1 poison
67 EXPECT_EQ(Poison
, ConstantExpr::getLShr(One
, One
));
69 // @m = constant i1 ashr(i1 1, i1 1) ; poison
70 // @m = constant i1 poison
71 EXPECT_EQ(Poison
, ConstantExpr::getAShr(One
, One
));
73 // @n = constant i1 mul(i1 -1, i1 1)
74 // @n = constant i1 true
75 EXPECT_EQ(One
, ConstantExpr::getMul(NegOne
, One
));
77 // @o = constant i1 sdiv(i1 -1, i1 1) ; overflow
78 // @o = constant i1 true
79 EXPECT_EQ(One
, ConstantFoldBinaryInstruction(Instruction::SDiv
, NegOne
, One
));
81 // @p = constant i1 sdiv(i1 1 , i1 -1); overflow
82 // @p = constant i1 true
83 EXPECT_EQ(One
, ConstantFoldBinaryInstruction(Instruction::SDiv
, One
, NegOne
));
85 // @q = constant i1 udiv(i1 -1, i1 1)
86 // @q = constant i1 true
87 EXPECT_EQ(One
, ConstantFoldBinaryInstruction(Instruction::UDiv
, NegOne
, One
));
89 // @r = constant i1 udiv(i1 1, i1 -1)
90 // @r = constant i1 true
91 EXPECT_EQ(One
, ConstantFoldBinaryInstruction(Instruction::UDiv
, One
, NegOne
));
93 // @s = constant i1 srem(i1 -1, i1 1) ; overflow
94 // @s = constant i1 false
96 ConstantFoldBinaryInstruction(Instruction::SRem
, NegOne
, One
));
98 // @u = constant i1 srem(i1 1, i1 -1) ; overflow
99 // @u = constant i1 false
101 ConstantFoldBinaryInstruction(Instruction::SRem
, One
, NegOne
));
104 TEST(ConstantsTest
, IntSigns
) {
106 IntegerType
*Int8Ty
= Type::getInt8Ty(Context
);
107 EXPECT_EQ(100, ConstantInt::get(Int8Ty
, 100, false)->getSExtValue());
108 EXPECT_EQ(100, ConstantInt::get(Int8Ty
, 100, true)->getSExtValue());
109 EXPECT_EQ(100, ConstantInt::getSigned(Int8Ty
, 100)->getSExtValue());
110 EXPECT_EQ(-50, ConstantInt::get(Int8Ty
, 206)->getSExtValue());
111 EXPECT_EQ(-50, ConstantInt::getSigned(Int8Ty
, -50)->getSExtValue());
112 EXPECT_EQ(206U, ConstantInt::getSigned(Int8Ty
, -50)->getZExtValue());
114 // Overflow is handled by truncation.
115 EXPECT_EQ(0x3b, ConstantInt::get(Int8Ty
, 0x13b)->getSExtValue());
118 TEST(ConstantsTest
, FP128Test
) {
120 Type
*FP128Ty
= Type::getFP128Ty(Context
);
122 IntegerType
*Int128Ty
= Type::getIntNTy(Context
, 128);
123 Constant
*Zero128
= Constant::getNullValue(Int128Ty
);
124 Constant
*X
= ConstantExpr::getUIToFP(Zero128
, FP128Ty
);
125 EXPECT_TRUE(isa
<ConstantFP
>(X
));
128 TEST(ConstantsTest
, PointerCast
) {
130 Type
*Int8PtrTy
= Type::getInt8PtrTy(C
);
131 Type
*Int32PtrTy
= Type::getInt32PtrTy(C
);
132 Type
*Int64Ty
= Type::getInt64Ty(C
);
133 VectorType
*Int8PtrVecTy
= FixedVectorType::get(Int8PtrTy
, 4);
134 VectorType
*Int32PtrVecTy
= FixedVectorType::get(Int32PtrTy
, 4);
135 VectorType
*Int64VecTy
= FixedVectorType::get(Int64Ty
, 4);
136 VectorType
*Int8PtrScalableVecTy
= ScalableVectorType::get(Int8PtrTy
, 4);
137 VectorType
*Int32PtrScalableVecTy
= ScalableVectorType::get(Int32PtrTy
, 4);
138 VectorType
*Int64ScalableVecTy
= ScalableVectorType::get(Int64Ty
, 4);
140 // ptrtoint i8* to i64
142 Constant::getNullValue(Int64Ty
),
143 ConstantExpr::getPointerCast(Constant::getNullValue(Int8PtrTy
), Int64Ty
));
145 // bitcast i8* to i32*
146 EXPECT_EQ(Constant::getNullValue(Int32PtrTy
),
147 ConstantExpr::getPointerCast(Constant::getNullValue(Int8PtrTy
),
150 // ptrtoint <4 x i8*> to <4 x i64>
151 EXPECT_EQ(Constant::getNullValue(Int64VecTy
),
152 ConstantExpr::getPointerCast(Constant::getNullValue(Int8PtrVecTy
),
155 // ptrtoint <vscale x 4 x i8*> to <vscale x 4 x i64>
157 Constant::getNullValue(Int64ScalableVecTy
),
158 ConstantExpr::getPointerCast(Constant::getNullValue(Int8PtrScalableVecTy
),
159 Int64ScalableVecTy
));
161 // bitcast <4 x i8*> to <4 x i32*>
162 EXPECT_EQ(Constant::getNullValue(Int32PtrVecTy
),
163 ConstantExpr::getPointerCast(Constant::getNullValue(Int8PtrVecTy
),
166 // bitcast <vscale x 4 x i8*> to <vscale x 4 x i32*>
168 Constant::getNullValue(Int32PtrScalableVecTy
),
169 ConstantExpr::getPointerCast(Constant::getNullValue(Int8PtrScalableVecTy
),
170 Int32PtrScalableVecTy
));
172 Type
*Int32Ptr1Ty
= Type::getInt32PtrTy(C
, 1);
173 ConstantInt
*K
= ConstantInt::get(Type::getInt64Ty(C
), 1234);
175 // Make sure that addrspacecast of inttoptr is not folded away.
176 EXPECT_NE(K
, ConstantExpr::getAddrSpaceCast(
177 ConstantExpr::getIntToPtr(K
, Int32PtrTy
), Int32Ptr1Ty
));
178 EXPECT_NE(K
, ConstantExpr::getAddrSpaceCast(
179 ConstantExpr::getIntToPtr(K
, Int32Ptr1Ty
), Int32PtrTy
));
181 Constant
*NullInt32Ptr0
= Constant::getNullValue(Int32PtrTy
);
182 Constant
*NullInt32Ptr1
= Constant::getNullValue(Int32Ptr1Ty
);
184 // Make sure that addrspacecast of null is not folded away.
185 EXPECT_NE(Constant::getNullValue(Int32PtrTy
),
186 ConstantExpr::getAddrSpaceCast(NullInt32Ptr0
, Int32Ptr1Ty
));
188 EXPECT_NE(Constant::getNullValue(Int32Ptr1Ty
),
189 ConstantExpr::getAddrSpaceCast(NullInt32Ptr1
, Int32PtrTy
));
192 #define CHECK(x, y) \
195 raw_string_ostream __o(__s); \
196 Instruction *__I = cast<ConstantExpr>(x)->getAsInstruction(); \
198 __I->deleteValue(); \
200 EXPECT_EQ(std::string(" <badref> = " y), __s); \
203 TEST(ConstantsTest
, AsInstructionsTest
) {
205 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
207 Type
*Int64Ty
= Type::getInt64Ty(Context
);
208 Type
*Int32Ty
= Type::getInt32Ty(Context
);
209 Type
*Int16Ty
= Type::getInt16Ty(Context
);
210 Type
*Int1Ty
= Type::getInt1Ty(Context
);
211 Type
*FloatTy
= Type::getFloatTy(Context
);
212 Type
*DoubleTy
= Type::getDoubleTy(Context
);
215 M
->getOrInsertGlobal("dummy", PointerType::getUnqual(Int32Ty
));
217 M
->getOrInsertGlobal("dummy2", PointerType::getUnqual(Int32Ty
));
219 Constant
*P0
= ConstantExpr::getPtrToInt(Global
, Int32Ty
);
220 Constant
*P1
= ConstantExpr::getUIToFP(P0
, FloatTy
);
221 Constant
*P2
= ConstantExpr::getUIToFP(P0
, DoubleTy
);
222 Constant
*P3
= ConstantExpr::getTrunc(P0
, Int1Ty
);
223 Constant
*P4
= ConstantExpr::getPtrToInt(Global2
, Int32Ty
);
224 Constant
*P5
= ConstantExpr::getUIToFP(P4
, FloatTy
);
225 Constant
*P6
= ConstantExpr::getBitCast(P4
, FixedVectorType::get(Int16Ty
, 2));
227 Constant
*One
= ConstantInt::get(Int32Ty
, 1);
228 Constant
*Two
= ConstantInt::get(Int64Ty
, 2);
229 Constant
*Big
= ConstantInt::get(Context
, APInt
{256, uint64_t(-1), true});
230 Constant
*Elt
= ConstantInt::get(Int16Ty
, 2015);
231 Constant
*Poison16
= PoisonValue::get(Int16Ty
);
232 Constant
*Undef64
= UndefValue::get(Int64Ty
);
233 Constant
*PoisonV16
= PoisonValue::get(P6
->getType());
235 #define P0STR "ptrtoint (ptr @dummy to i32)"
236 #define P1STR "uitofp (i32 ptrtoint (ptr @dummy to i32) to float)"
237 #define P2STR "uitofp (i32 ptrtoint (ptr @dummy to i32) to double)"
238 #define P3STR "ptrtoint (ptr @dummy to i1)"
239 #define P4STR "ptrtoint (ptr @dummy2 to i32)"
240 #define P5STR "uitofp (i32 ptrtoint (ptr @dummy2 to i32) to float)"
241 #define P6STR "bitcast (i32 ptrtoint (ptr @dummy2 to i32) to <2 x i16>)"
243 CHECK(ConstantExpr::getNeg(P0
), "sub i32 0, " P0STR
);
244 CHECK(ConstantExpr::getNot(P0
), "xor i32 " P0STR
", -1");
245 CHECK(ConstantExpr::getAdd(P0
, P0
), "add i32 " P0STR
", " P0STR
);
246 CHECK(ConstantExpr::getAdd(P0
, P0
, false, true),
247 "add nsw i32 " P0STR
", " P0STR
);
248 CHECK(ConstantExpr::getAdd(P0
, P0
, true, true),
249 "add nuw nsw i32 " P0STR
", " P0STR
);
250 CHECK(ConstantExpr::getSub(P0
, P0
), "sub i32 " P0STR
", " P0STR
);
251 CHECK(ConstantExpr::getMul(P0
, P0
), "mul i32 " P0STR
", " P0STR
);
252 CHECK(ConstantExpr::getAnd(P0
, P0
), "and i32 " P0STR
", " P0STR
);
253 CHECK(ConstantExpr::getOr(P0
, P0
), "or i32 " P0STR
", " P0STR
);
254 CHECK(ConstantExpr::getXor(P0
, P0
), "xor i32 " P0STR
", " P0STR
);
255 CHECK(ConstantExpr::getShl(P0
, P0
), "shl i32 " P0STR
", " P0STR
);
256 CHECK(ConstantExpr::getShl(P0
, P0
, true), "shl nuw i32 " P0STR
", " P0STR
);
257 CHECK(ConstantExpr::getShl(P0
, P0
, false, true),
258 "shl nsw i32 " P0STR
", " P0STR
);
259 CHECK(ConstantExpr::getLShr(P0
, P0
, false), "lshr i32 " P0STR
", " P0STR
);
260 CHECK(ConstantExpr::getLShr(P0
, P0
, true),
261 "lshr exact i32 " P0STR
", " P0STR
);
262 CHECK(ConstantExpr::getAShr(P0
, P0
, false), "ashr i32 " P0STR
", " P0STR
);
263 CHECK(ConstantExpr::getAShr(P0
, P0
, true),
264 "ashr exact i32 " P0STR
", " P0STR
);
266 CHECK(ConstantExpr::getSExt(P0
, Int64Ty
), "sext i32 " P0STR
" to i64");
267 CHECK(ConstantExpr::getZExt(P0
, Int64Ty
), "zext i32 " P0STR
" to i64");
268 CHECK(ConstantExpr::getFPTrunc(P2
, FloatTy
),
269 "fptrunc double " P2STR
" to float");
270 CHECK(ConstantExpr::getFPExtend(P1
, DoubleTy
),
271 "fpext float " P1STR
" to double");
273 CHECK(ConstantExpr::getSelect(P3
, P0
, P4
),
274 "select i1 " P3STR
", i32 " P0STR
", i32 " P4STR
);
275 CHECK(ConstantExpr::getICmp(CmpInst::ICMP_EQ
, P0
, P4
),
276 "icmp eq i32 " P0STR
", " P4STR
);
277 CHECK(ConstantExpr::getFCmp(CmpInst::FCMP_ULT
, P1
, P5
),
278 "fcmp ult float " P1STR
", " P5STR
);
280 std::vector
<Constant
*> V
;
282 // FIXME: getGetElementPtr() actually creates an inbounds ConstantGEP,
284 // CHECK(ConstantExpr::getGetElementPtr(Global, V, false),
285 // "getelementptr i32*, i32** @dummy, i32 1");
286 CHECK(ConstantExpr::getInBoundsGetElementPtr(PointerType::getUnqual(Int32Ty
),
288 "getelementptr inbounds ptr, ptr @dummy, i32 1");
290 CHECK(ConstantExpr::getExtractElement(P6
, One
),
291 "extractelement <2 x i16> " P6STR
", i32 1");
293 EXPECT_EQ(Poison16
, ConstantExpr::getExtractElement(P6
, Two
));
294 EXPECT_EQ(Poison16
, ConstantExpr::getExtractElement(P6
, Big
));
295 EXPECT_EQ(Poison16
, ConstantExpr::getExtractElement(P6
, Undef64
));
297 EXPECT_EQ(Elt
, ConstantExpr::getExtractElement(
298 ConstantExpr::getInsertElement(P6
, Elt
, One
), One
));
299 EXPECT_EQ(PoisonV16
, ConstantExpr::getInsertElement(P6
, Elt
, Two
));
300 EXPECT_EQ(PoisonV16
, ConstantExpr::getInsertElement(P6
, Elt
, Big
));
301 EXPECT_EQ(PoisonV16
, ConstantExpr::getInsertElement(P6
, Elt
, Undef64
));
304 #ifdef GTEST_HAS_DEATH_TEST
306 TEST(ConstantsTest
, ReplaceWithConstantTest
) {
308 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
310 Type
*Int32Ty
= Type::getInt32Ty(Context
);
311 Constant
*One
= ConstantInt::get(Int32Ty
, 1);
314 M
->getOrInsertGlobal("dummy", PointerType::getUnqual(Int32Ty
));
315 Constant
*GEP
= ConstantExpr::getGetElementPtr(
316 PointerType::getUnqual(Int32Ty
), Global
, One
);
317 EXPECT_DEATH(Global
->replaceAllUsesWith(GEP
),
318 "this->replaceAllUsesWith\\(expr\\(this\\)\\) is NOT valid!");
326 TEST(ConstantsTest
, ConstantArrayReplaceWithConstant
) {
328 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
330 Type
*IntTy
= Type::getInt8Ty(Context
);
331 ArrayType
*ArrayTy
= ArrayType::get(IntTy
, 2);
332 Constant
*A01Vals
[2] = {ConstantInt::get(IntTy
, 0),
333 ConstantInt::get(IntTy
, 1)};
334 Constant
*A01
= ConstantArray::get(ArrayTy
, A01Vals
);
336 Constant
*Global
= new GlobalVariable(*M
, IntTy
, false,
337 GlobalValue::ExternalLinkage
, nullptr);
338 Constant
*GlobalInt
= ConstantExpr::getPtrToInt(Global
, IntTy
);
339 Constant
*A0GVals
[2] = {ConstantInt::get(IntTy
, 0), GlobalInt
};
340 Constant
*A0G
= ConstantArray::get(ArrayTy
, A0GVals
);
343 GlobalVariable
*RefArray
=
344 new GlobalVariable(*M
, ArrayTy
, false, GlobalValue::ExternalLinkage
, A0G
);
345 ASSERT_EQ(A0G
, RefArray
->getInitializer());
347 GlobalInt
->replaceAllUsesWith(ConstantInt::get(IntTy
, 1));
348 ASSERT_EQ(A01
, RefArray
->getInitializer());
351 TEST(ConstantsTest
, ConstantExprReplaceWithConstant
) {
353 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
355 Type
*IntTy
= Type::getInt8Ty(Context
);
356 Constant
*G1
= new GlobalVariable(*M
, IntTy
, false,
357 GlobalValue::ExternalLinkage
, nullptr);
358 Constant
*G2
= new GlobalVariable(*M
, IntTy
, false,
359 GlobalValue::ExternalLinkage
, nullptr);
362 Constant
*Int1
= ConstantExpr::getPtrToInt(G1
, IntTy
);
363 Constant
*Int2
= ConstantExpr::getPtrToInt(G2
, IntTy
);
364 ASSERT_NE(Int1
, Int2
);
366 GlobalVariable
*Ref
=
367 new GlobalVariable(*M
, IntTy
, false, GlobalValue::ExternalLinkage
, Int1
);
368 ASSERT_EQ(Int1
, Ref
->getInitializer());
370 G1
->replaceAllUsesWith(G2
);
371 ASSERT_EQ(Int2
, Ref
->getInitializer());
374 TEST(ConstantsTest
, GEPReplaceWithConstant
) {
376 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
378 Type
*IntTy
= Type::getInt32Ty(Context
);
379 Type
*PtrTy
= PointerType::get(IntTy
, 0);
380 auto *C1
= ConstantInt::get(IntTy
, 1);
381 auto *Placeholder
= new GlobalVariable(
382 *M
, IntTy
, false, GlobalValue::ExternalWeakLinkage
, nullptr);
383 auto *GEP
= ConstantExpr::getGetElementPtr(IntTy
, Placeholder
, C1
);
384 ASSERT_EQ(GEP
->getOperand(0), Placeholder
);
387 new GlobalVariable(*M
, PtrTy
, false, GlobalValue::ExternalLinkage
, GEP
);
388 ASSERT_EQ(GEP
, Ref
->getInitializer());
390 auto *Global
= new GlobalVariable(*M
, IntTy
, false,
391 GlobalValue::ExternalLinkage
, nullptr);
392 auto *Alias
= GlobalAlias::create(IntTy
, 0, GlobalValue::ExternalLinkage
,
393 "alias", Global
, M
.get());
394 Placeholder
->replaceAllUsesWith(Alias
);
395 ASSERT_EQ(GEP
, Ref
->getInitializer());
396 ASSERT_EQ(GEP
->getOperand(0), Alias
);
399 TEST(ConstantsTest
, AliasCAPI
) {
402 std::unique_ptr
<Module
> M
=
403 parseAssemblyString("@g = global i32 42", Error
, Context
);
404 GlobalVariable
*G
= M
->getGlobalVariable("g");
405 Type
*I16Ty
= Type::getInt16Ty(Context
);
406 Type
*I16PTy
= PointerType::get(I16Ty
, 0);
407 Constant
*Aliasee
= ConstantExpr::getBitCast(G
, I16PTy
);
408 LLVMValueRef AliasRef
=
409 LLVMAddAlias2(wrap(M
.get()), wrap(I16Ty
), 0, wrap(Aliasee
), "a");
410 ASSERT_EQ(unwrap
<GlobalAlias
>(AliasRef
)->getAliasee(), Aliasee
);
413 static std::string
getNameOfType(Type
*T
) {
415 raw_string_ostream
RSOS(S
);
420 TEST(ConstantsTest
, BuildConstantDataArrays
) {
423 for (Type
*T
: {Type::getInt8Ty(Context
), Type::getInt16Ty(Context
),
424 Type::getInt32Ty(Context
), Type::getInt64Ty(Context
)}) {
425 ArrayType
*ArrayTy
= ArrayType::get(T
, 2);
426 Constant
*Vals
[] = {ConstantInt::get(T
, 0), ConstantInt::get(T
, 1)};
427 Constant
*CA
= ConstantArray::get(ArrayTy
, Vals
);
428 ASSERT_TRUE(isa
<ConstantDataArray
>(CA
)) << " T = " << getNameOfType(T
);
429 auto *CDA
= cast
<ConstantDataArray
>(CA
);
430 Constant
*CA2
= ConstantDataArray::getRaw(
431 CDA
->getRawDataValues(), CDA
->getNumElements(), CDA
->getElementType());
432 ASSERT_TRUE(CA
== CA2
) << " T = " << getNameOfType(T
);
435 for (Type
*T
: {Type::getHalfTy(Context
), Type::getBFloatTy(Context
),
436 Type::getFloatTy(Context
), Type::getDoubleTy(Context
)}) {
437 ArrayType
*ArrayTy
= ArrayType::get(T
, 2);
438 Constant
*Vals
[] = {ConstantFP::get(T
, 0), ConstantFP::get(T
, 1)};
439 Constant
*CA
= ConstantArray::get(ArrayTy
, Vals
);
440 ASSERT_TRUE(isa
<ConstantDataArray
>(CA
)) << " T = " << getNameOfType(T
);
441 auto *CDA
= cast
<ConstantDataArray
>(CA
);
442 Constant
*CA2
= ConstantDataArray::getRaw(
443 CDA
->getRawDataValues(), CDA
->getNumElements(), CDA
->getElementType());
444 ASSERT_TRUE(CA
== CA2
) << " T = " << getNameOfType(T
);
448 TEST(ConstantsTest
, BuildConstantDataVectors
) {
451 for (Type
*T
: {Type::getInt8Ty(Context
), Type::getInt16Ty(Context
),
452 Type::getInt32Ty(Context
), Type::getInt64Ty(Context
)}) {
453 Constant
*Vals
[] = {ConstantInt::get(T
, 0), ConstantInt::get(T
, 1)};
454 Constant
*CV
= ConstantVector::get(Vals
);
455 ASSERT_TRUE(isa
<ConstantDataVector
>(CV
)) << " T = " << getNameOfType(T
);
456 auto *CDV
= cast
<ConstantDataVector
>(CV
);
457 Constant
*CV2
= ConstantDataVector::getRaw(
458 CDV
->getRawDataValues(), CDV
->getNumElements(), CDV
->getElementType());
459 ASSERT_TRUE(CV
== CV2
) << " T = " << getNameOfType(T
);
462 for (Type
*T
: {Type::getHalfTy(Context
), Type::getBFloatTy(Context
),
463 Type::getFloatTy(Context
), Type::getDoubleTy(Context
)}) {
464 Constant
*Vals
[] = {ConstantFP::get(T
, 0), ConstantFP::get(T
, 1)};
465 Constant
*CV
= ConstantVector::get(Vals
);
466 ASSERT_TRUE(isa
<ConstantDataVector
>(CV
)) << " T = " << getNameOfType(T
);
467 auto *CDV
= cast
<ConstantDataVector
>(CV
);
468 Constant
*CV2
= ConstantDataVector::getRaw(
469 CDV
->getRawDataValues(), CDV
->getNumElements(), CDV
->getElementType());
470 ASSERT_TRUE(CV
== CV2
) << " T = " << getNameOfType(T
);
474 void bitcastToGEPHelper(bool useOpaquePointers
) {
476 Context
.setOpaquePointers(useOpaquePointers
);
477 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
479 auto *i32
= Type::getInt32Ty(Context
);
480 auto *U
= StructType::create(Context
, "Unsized");
481 Type
*EltTys
[] = {i32
, U
};
482 auto *S
= StructType::create(EltTys
);
485 new GlobalVariable(*M
, S
, false, GlobalValue::ExternalLinkage
, nullptr);
486 auto *PtrTy
= PointerType::get(i32
, 0);
487 auto *C
= ConstantExpr::getBitCast(G
, PtrTy
);
488 if (Context
.supportsTypedPointers()) {
489 EXPECT_EQ(cast
<ConstantExpr
>(C
)->getOpcode(), Instruction::BitCast
);
491 /* With opaque pointers, no cast is necessary. */
496 TEST(ConstantsTest
, BitcastToGEP
) {
497 bitcastToGEPHelper(true);
498 bitcastToGEPHelper(false);
501 bool foldFuncPtrAndConstToNull(LLVMContext
&Context
, Module
*TheModule
,
503 MaybeAlign FunctionAlign
= llvm::None
) {
504 Type
*VoidType(Type::getVoidTy(Context
));
505 FunctionType
*FuncType(FunctionType::get(VoidType
, false));
507 Function::Create(FuncType
, GlobalValue::ExternalLinkage
, "", TheModule
));
510 Func
->setAlignment(*FunctionAlign
);
512 IntegerType
*ConstantIntType(Type::getInt32Ty(Context
));
513 ConstantInt
*TheConstant(ConstantInt::get(ConstantIntType
, AndValue
));
515 Constant
*TheConstantExpr(ConstantExpr::getPtrToInt(Func
, ConstantIntType
));
518 ConstantExpr::get(Instruction::And
, TheConstantExpr
, TheConstant
)
522 // If the Module exists then it will delete the Function.
529 TEST(ConstantsTest
, FoldFunctionPtrAlignUnknownAnd2
) {
531 Module
TheModule("TestModule", Context
);
532 // When the DataLayout doesn't specify a function pointer alignment we
533 // assume in this case that it is 4 byte aligned. This is a bug but we can't
534 // fix it directly because it causes a code size regression on X86.
535 // FIXME: This test should be changed once existing targets have
536 // appropriate defaults. See associated FIXME in ConstantFoldBinaryInstruction
537 ASSERT_TRUE(foldFuncPtrAndConstToNull(Context
, &TheModule
, 2));
540 TEST(ConstantsTest
, DontFoldFunctionPtrAlignUnknownAnd4
) {
542 Module
TheModule("TestModule", Context
);
543 ASSERT_FALSE(foldFuncPtrAndConstToNull(Context
, &TheModule
, 4));
546 TEST(ConstantsTest
, FoldFunctionPtrAlign4
) {
548 Module
TheModule("TestModule", Context
);
549 const char *AlignmentStrings
[] = {"Fi32", "Fn32"};
551 for (unsigned AndValue
= 1; AndValue
<= 2; ++AndValue
) {
552 for (const char *AlignmentString
: AlignmentStrings
) {
553 TheModule
.setDataLayout(AlignmentString
);
554 ASSERT_TRUE(foldFuncPtrAndConstToNull(Context
, &TheModule
, AndValue
));
559 TEST(ConstantsTest
, DontFoldFunctionPtrAlign1
) {
561 Module
TheModule("TestModule", Context
);
562 const char *AlignmentStrings
[] = {"Fi8", "Fn8"};
564 for (const char *AlignmentString
: AlignmentStrings
) {
565 TheModule
.setDataLayout(AlignmentString
);
566 ASSERT_FALSE(foldFuncPtrAndConstToNull(Context
, &TheModule
, 2));
570 TEST(ConstantsTest
, FoldFunctionAlign4PtrAlignMultiple
) {
572 Module
TheModule("TestModule", Context
);
573 TheModule
.setDataLayout("Fn8");
574 ASSERT_TRUE(foldFuncPtrAndConstToNull(Context
, &TheModule
, 2, Align(4)));
577 TEST(ConstantsTest
, DontFoldFunctionAlign4PtrAlignIndependent
) {
579 Module
TheModule("TestModule", Context
);
580 TheModule
.setDataLayout("Fi8");
581 ASSERT_FALSE(foldFuncPtrAndConstToNull(Context
, &TheModule
, 2, Align(4)));
584 TEST(ConstantsTest
, DontFoldFunctionPtrIfNoModule
) {
586 // Even though the function is explicitly 4 byte aligned, in the absence of a
587 // DataLayout we can't assume that the function pointer is aligned.
588 ASSERT_FALSE(foldFuncPtrAndConstToNull(Context
, nullptr, 2, Align(4)));
591 TEST(ConstantsTest
, FoldGlobalVariablePtr
) {
594 IntegerType
*IntType(Type::getInt32Ty(Context
));
596 std::unique_ptr
<GlobalVariable
> Global(
597 new GlobalVariable(IntType
, true, GlobalValue::ExternalLinkage
));
599 Global
->setAlignment(Align(4));
601 ConstantInt
*TheConstant(ConstantInt::get(IntType
, 2));
603 Constant
*TheConstantExpr(ConstantExpr::getPtrToInt(Global
.get(), IntType
));
605 ASSERT_TRUE(ConstantExpr::get(Instruction::And
, TheConstantExpr
, TheConstant
)
609 // Check that containsUndefOrPoisonElement and containsPoisonElement is working
612 TEST(ConstantsTest
, containsUndefElemTest
) {
615 Type
*Int32Ty
= Type::getInt32Ty(Context
);
616 Constant
*CU
= UndefValue::get(Int32Ty
);
617 Constant
*CP
= PoisonValue::get(Int32Ty
);
618 Constant
*C1
= ConstantInt::get(Int32Ty
, 1);
619 Constant
*C2
= ConstantInt::get(Int32Ty
, 2);
622 Constant
*V1
= ConstantVector::get({C1
, C2
});
623 EXPECT_FALSE(V1
->containsUndefOrPoisonElement());
624 EXPECT_FALSE(V1
->containsPoisonElement());
628 Constant
*V2
= ConstantVector::get({C1
, CU
});
629 EXPECT_TRUE(V2
->containsUndefOrPoisonElement());
630 EXPECT_FALSE(V2
->containsPoisonElement());
634 Constant
*V3
= ConstantVector::get({C1
, CP
});
635 EXPECT_TRUE(V3
->containsUndefOrPoisonElement());
636 EXPECT_TRUE(V3
->containsPoisonElement());
640 Constant
*V4
= ConstantVector::get({CU
, CP
});
641 EXPECT_TRUE(V4
->containsUndefOrPoisonElement());
642 EXPECT_TRUE(V4
->containsPoisonElement());
646 // Check that undefined elements in vector constants are matched
647 // correctly for both integer and floating-point types. Just don't
648 // crash on vectors of pointers (could be handled?).
650 TEST(ConstantsTest
, isElementWiseEqual
) {
653 Type
*Int32Ty
= Type::getInt32Ty(Context
);
654 Constant
*CU
= UndefValue::get(Int32Ty
);
655 Constant
*C1
= ConstantInt::get(Int32Ty
, 1);
656 Constant
*C2
= ConstantInt::get(Int32Ty
, 2);
658 Constant
*C1211
= ConstantVector::get({C1
, C2
, C1
, C1
});
659 Constant
*C12U1
= ConstantVector::get({C1
, C2
, CU
, C1
});
660 Constant
*C12U2
= ConstantVector::get({C1
, C2
, CU
, C2
});
661 Constant
*C12U21
= ConstantVector::get({C1
, C2
, CU
, C2
, C1
});
663 EXPECT_TRUE(C1211
->isElementWiseEqual(C12U1
));
664 EXPECT_TRUE(C12U1
->isElementWiseEqual(C1211
));
665 EXPECT_FALSE(C12U2
->isElementWiseEqual(C12U1
));
666 EXPECT_FALSE(C12U1
->isElementWiseEqual(C12U2
));
667 EXPECT_FALSE(C12U21
->isElementWiseEqual(C12U2
));
669 Type
*FltTy
= Type::getFloatTy(Context
);
670 Constant
*CFU
= UndefValue::get(FltTy
);
671 Constant
*CF1
= ConstantFP::get(FltTy
, 1.0);
672 Constant
*CF2
= ConstantFP::get(FltTy
, 2.0);
674 Constant
*CF1211
= ConstantVector::get({CF1
, CF2
, CF1
, CF1
});
675 Constant
*CF12U1
= ConstantVector::get({CF1
, CF2
, CFU
, CF1
});
676 Constant
*CF12U2
= ConstantVector::get({CF1
, CF2
, CFU
, CF2
});
677 Constant
*CFUU1U
= ConstantVector::get({CFU
, CFU
, CF1
, CFU
});
679 EXPECT_TRUE(CF1211
->isElementWiseEqual(CF12U1
));
680 EXPECT_TRUE(CF12U1
->isElementWiseEqual(CF1211
));
681 EXPECT_TRUE(CFUU1U
->isElementWiseEqual(CF12U1
));
682 EXPECT_FALSE(CF12U2
->isElementWiseEqual(CF12U1
));
683 EXPECT_FALSE(CF12U1
->isElementWiseEqual(CF12U2
));
685 PointerType
*PtrTy
= Type::getInt8PtrTy(Context
);
686 Constant
*CPU
= UndefValue::get(PtrTy
);
687 Constant
*CP0
= ConstantPointerNull::get(PtrTy
);
689 Constant
*CP0000
= ConstantVector::get({CP0
, CP0
, CP0
, CP0
});
690 Constant
*CP00U0
= ConstantVector::get({CP0
, CP0
, CPU
, CP0
});
691 Constant
*CP00U
= ConstantVector::get({CP0
, CP0
, CPU
});
693 EXPECT_FALSE(CP0000
->isElementWiseEqual(CP00U0
));
694 EXPECT_FALSE(CP00U0
->isElementWiseEqual(CP0000
));
695 EXPECT_FALSE(CP0000
->isElementWiseEqual(CP00U
));
696 EXPECT_FALSE(CP00U
->isElementWiseEqual(CP00U0
));
699 // Check that vector/aggregate constants correctly store undef and poison
702 TEST(ConstantsTest
, CheckElementWiseUndefPoison
) {
705 Type
*Int32Ty
= Type::getInt32Ty(Context
);
706 StructType
*STy
= StructType::get(Int32Ty
, Int32Ty
);
707 ArrayType
*ATy
= ArrayType::get(Int32Ty
, 2);
708 Constant
*CU
= UndefValue::get(Int32Ty
);
709 Constant
*CP
= PoisonValue::get(Int32Ty
);
712 Constant
*CUU
= ConstantVector::get({CU
, CU
});
713 Constant
*CPP
= ConstantVector::get({CP
, CP
});
714 Constant
*CUP
= ConstantVector::get({CU
, CP
});
715 Constant
*CPU
= ConstantVector::get({CP
, CU
});
716 EXPECT_EQ(CUU
, UndefValue::get(CUU
->getType()));
717 EXPECT_EQ(CPP
, PoisonValue::get(CPP
->getType()));
718 EXPECT_NE(CUP
, UndefValue::get(CUP
->getType()));
719 EXPECT_NE(CPU
, UndefValue::get(CPU
->getType()));
723 Constant
*CUU
= ConstantStruct::get(STy
, {CU
, CU
});
724 Constant
*CPP
= ConstantStruct::get(STy
, {CP
, CP
});
725 Constant
*CUP
= ConstantStruct::get(STy
, {CU
, CP
});
726 Constant
*CPU
= ConstantStruct::get(STy
, {CP
, CU
});
727 EXPECT_EQ(CUU
, UndefValue::get(CUU
->getType()));
728 EXPECT_EQ(CPP
, PoisonValue::get(CPP
->getType()));
729 EXPECT_NE(CUP
, UndefValue::get(CUP
->getType()));
730 EXPECT_NE(CPU
, UndefValue::get(CPU
->getType()));
734 Constant
*CUU
= ConstantArray::get(ATy
, {CU
, CU
});
735 Constant
*CPP
= ConstantArray::get(ATy
, {CP
, CP
});
736 Constant
*CUP
= ConstantArray::get(ATy
, {CU
, CP
});
737 Constant
*CPU
= ConstantArray::get(ATy
, {CP
, CU
});
738 EXPECT_EQ(CUU
, UndefValue::get(CUU
->getType()));
739 EXPECT_EQ(CPP
, PoisonValue::get(CPP
->getType()));
740 EXPECT_NE(CUP
, UndefValue::get(CUP
->getType()));
741 EXPECT_NE(CPU
, UndefValue::get(CPU
->getType()));
745 TEST(ConstantsTest
, GetSplatValueRoundTrip
) {
748 Type
*FloatTy
= Type::getFloatTy(Context
);
749 Type
*Int32Ty
= Type::getInt32Ty(Context
);
750 Type
*Int8Ty
= Type::getInt8Ty(Context
);
752 for (unsigned Min
: {1, 2, 8}) {
753 auto ScalableEC
= ElementCount::getScalable(Min
);
754 auto FixedEC
= ElementCount::getFixed(Min
);
756 for (auto EC
: {ScalableEC
, FixedEC
}) {
757 for (auto *Ty
: {FloatTy
, Int32Ty
, Int8Ty
}) {
758 Constant
*Zero
= Constant::getNullValue(Ty
);
759 Constant
*One
= Constant::getAllOnesValue(Ty
);
761 for (auto *C
: {Zero
, One
}) {
762 Constant
*Splat
= ConstantVector::getSplat(EC
, C
);
763 ASSERT_NE(nullptr, Splat
);
765 Constant
*SplatVal
= Splat
->getSplatValue();
766 EXPECT_NE(nullptr, SplatVal
);
767 EXPECT_EQ(SplatVal
, C
);
774 TEST(ConstantsTest
, ComdatUserTracking
) {
776 Module
M("MyModule", Context
);
778 Comdat
*C
= M
.getOrInsertComdat("comdat");
779 const SmallPtrSetImpl
<GlobalObject
*> &Users
= C
->getUsers();
780 EXPECT_TRUE(Users
.size() == 0);
782 Type
*Ty
= Type::getInt8Ty(Context
);
783 GlobalVariable
*GV1
= cast
<GlobalVariable
>(M
.getOrInsertGlobal("gv1", Ty
));
785 EXPECT_TRUE(Users
.size() == 1);
786 EXPECT_TRUE(Users
.contains(GV1
));
788 GlobalVariable
*GV2
= cast
<GlobalVariable
>(M
.getOrInsertGlobal("gv2", Ty
));
790 EXPECT_TRUE(Users
.size() == 2);
791 EXPECT_TRUE(Users
.contains(GV2
));
793 GV1
->eraseFromParent();
794 EXPECT_TRUE(Users
.size() == 1);
795 EXPECT_TRUE(Users
.contains(GV2
));
797 GV2
->eraseFromParent();
798 EXPECT_TRUE(Users
.size() == 0);
801 } // end anonymous namespace
802 } // end namespace llvm