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
*PtrTy
= PointerType::get(C
, 0);
131 Type
*Int64Ty
= Type::getInt64Ty(C
);
132 VectorType
*PtrVecTy
= FixedVectorType::get(PtrTy
, 4);
133 VectorType
*Int64VecTy
= FixedVectorType::get(Int64Ty
, 4);
134 VectorType
*PtrScalableVecTy
= ScalableVectorType::get(PtrTy
, 4);
135 VectorType
*Int64ScalableVecTy
= ScalableVectorType::get(Int64Ty
, 4);
137 // ptrtoint ptr to i64
139 Constant::getNullValue(Int64Ty
),
140 ConstantExpr::getPointerCast(Constant::getNullValue(PtrTy
), Int64Ty
));
142 // bitcast ptr to ptr
143 EXPECT_EQ(Constant::getNullValue(PtrTy
),
144 ConstantExpr::getPointerCast(Constant::getNullValue(PtrTy
), PtrTy
));
146 // ptrtoint <4 x ptr> to <4 x i64>
147 EXPECT_EQ(Constant::getNullValue(Int64VecTy
),
148 ConstantExpr::getPointerCast(Constant::getNullValue(PtrVecTy
),
151 // ptrtoint <vscale x 4 x ptr> to <vscale x 4 x i64>
152 EXPECT_EQ(Constant::getNullValue(Int64ScalableVecTy
),
153 ConstantExpr::getPointerCast(
154 Constant::getNullValue(PtrScalableVecTy
), Int64ScalableVecTy
));
156 // bitcast <4 x ptr> to <4 x ptr>
158 Constant::getNullValue(PtrVecTy
),
159 ConstantExpr::getPointerCast(Constant::getNullValue(PtrVecTy
), PtrVecTy
));
161 // bitcast <vscale x 4 x ptr> to <vscale x 4 x ptr>
162 EXPECT_EQ(Constant::getNullValue(PtrScalableVecTy
),
163 ConstantExpr::getPointerCast(
164 Constant::getNullValue(PtrScalableVecTy
), PtrScalableVecTy
));
166 Type
*Ptr1Ty
= PointerType::get(C
, 1);
167 ConstantInt
*K
= ConstantInt::get(Type::getInt64Ty(C
), 1234);
169 // Make sure that addrspacecast of inttoptr is not folded away.
170 EXPECT_NE(K
, ConstantExpr::getAddrSpaceCast(
171 ConstantExpr::getIntToPtr(K
, PtrTy
), Ptr1Ty
));
172 EXPECT_NE(K
, ConstantExpr::getAddrSpaceCast(
173 ConstantExpr::getIntToPtr(K
, Ptr1Ty
), PtrTy
));
175 Constant
*NullPtr0
= Constant::getNullValue(PtrTy
);
176 Constant
*NullPtr1
= Constant::getNullValue(Ptr1Ty
);
178 // Make sure that addrspacecast of null is not folded away.
179 EXPECT_NE(Constant::getNullValue(PtrTy
),
180 ConstantExpr::getAddrSpaceCast(NullPtr0
, Ptr1Ty
));
182 EXPECT_NE(Constant::getNullValue(Ptr1Ty
),
183 ConstantExpr::getAddrSpaceCast(NullPtr1
, PtrTy
));
186 #define CHECK(x, y) \
189 raw_string_ostream __o(__s); \
190 Instruction *__I = cast<ConstantExpr>(x)->getAsInstruction(); \
192 __I->deleteValue(); \
194 EXPECT_EQ(std::string(" <badref> = " y), __s); \
197 TEST(ConstantsTest
, AsInstructionsTest
) {
199 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
201 Type
*Int64Ty
= Type::getInt64Ty(Context
);
202 Type
*Int32Ty
= Type::getInt32Ty(Context
);
203 Type
*Int16Ty
= Type::getInt16Ty(Context
);
204 Type
*FloatTy
= Type::getFloatTy(Context
);
205 Type
*DoubleTy
= Type::getDoubleTy(Context
);
208 M
->getOrInsertGlobal("dummy", PointerType::getUnqual(Int32Ty
));
210 M
->getOrInsertGlobal("dummy2", PointerType::getUnqual(Int32Ty
));
212 Constant
*P0
= ConstantExpr::getPtrToInt(Global
, Int32Ty
);
213 Constant
*P1
= ConstantExpr::getUIToFP(P0
, FloatTy
);
214 Constant
*P2
= ConstantExpr::getUIToFP(P0
, DoubleTy
);
215 Constant
*P4
= ConstantExpr::getPtrToInt(Global2
, Int32Ty
);
216 Constant
*P5
= ConstantExpr::getUIToFP(P4
, FloatTy
);
217 Constant
*P6
= ConstantExpr::getBitCast(P4
, FixedVectorType::get(Int16Ty
, 2));
219 Constant
*One
= ConstantInt::get(Int32Ty
, 1);
220 Constant
*Two
= ConstantInt::get(Int64Ty
, 2);
221 Constant
*Big
= ConstantInt::get(Context
, APInt
{256, uint64_t(-1), true});
222 Constant
*Elt
= ConstantInt::get(Int16Ty
, 2015);
223 Constant
*Poison16
= PoisonValue::get(Int16Ty
);
224 Constant
*Undef64
= UndefValue::get(Int64Ty
);
225 Constant
*PoisonV16
= PoisonValue::get(P6
->getType());
227 #define P0STR "ptrtoint (ptr @dummy to i32)"
228 #define P1STR "uitofp (i32 ptrtoint (ptr @dummy to i32) to float)"
229 #define P2STR "uitofp (i32 ptrtoint (ptr @dummy to i32) to double)"
230 #define P3STR "ptrtoint (ptr @dummy to i1)"
231 #define P4STR "ptrtoint (ptr @dummy2 to i32)"
232 #define P5STR "uitofp (i32 ptrtoint (ptr @dummy2 to i32) to float)"
233 #define P6STR "bitcast (i32 ptrtoint (ptr @dummy2 to i32) to <2 x i16>)"
235 CHECK(ConstantExpr::getNeg(P0
), "sub i32 0, " P0STR
);
236 CHECK(ConstantExpr::getNot(P0
), "xor i32 " P0STR
", -1");
237 CHECK(ConstantExpr::getAdd(P0
, P0
), "add i32 " P0STR
", " P0STR
);
238 CHECK(ConstantExpr::getAdd(P0
, P0
, false, true),
239 "add nsw i32 " P0STR
", " P0STR
);
240 CHECK(ConstantExpr::getAdd(P0
, P0
, true, true),
241 "add nuw nsw i32 " P0STR
", " P0STR
);
242 CHECK(ConstantExpr::getSub(P0
, P0
), "sub i32 " P0STR
", " P0STR
);
243 CHECK(ConstantExpr::getMul(P0
, P0
), "mul i32 " P0STR
", " P0STR
);
244 CHECK(ConstantExpr::getXor(P0
, P0
), "xor i32 " P0STR
", " P0STR
);
245 CHECK(ConstantExpr::getShl(P0
, P0
), "shl i32 " P0STR
", " P0STR
);
246 CHECK(ConstantExpr::getShl(P0
, P0
, true), "shl nuw i32 " P0STR
", " P0STR
);
247 CHECK(ConstantExpr::getShl(P0
, P0
, false, true),
248 "shl nsw i32 " P0STR
", " P0STR
);
249 CHECK(ConstantExpr::getLShr(P0
, P0
, false), "lshr i32 " P0STR
", " P0STR
);
250 CHECK(ConstantExpr::getLShr(P0
, P0
, true),
251 "lshr exact i32 " P0STR
", " P0STR
);
252 CHECK(ConstantExpr::getAShr(P0
, P0
, false), "ashr i32 " P0STR
", " P0STR
);
253 CHECK(ConstantExpr::getAShr(P0
, P0
, true),
254 "ashr exact i32 " P0STR
", " P0STR
);
256 CHECK(ConstantExpr::getSExt(P0
, Int64Ty
), "sext i32 " P0STR
" to i64");
257 CHECK(ConstantExpr::getZExt(P0
, Int64Ty
), "zext i32 " P0STR
" to i64");
258 CHECK(ConstantExpr::getFPTrunc(P2
, FloatTy
),
259 "fptrunc double " P2STR
" to float");
260 CHECK(ConstantExpr::getFPExtend(P1
, DoubleTy
),
261 "fpext float " P1STR
" to double");
263 CHECK(ConstantExpr::getICmp(CmpInst::ICMP_EQ
, P0
, P4
),
264 "icmp eq i32 " P0STR
", " P4STR
);
265 CHECK(ConstantExpr::getFCmp(CmpInst::FCMP_ULT
, P1
, P5
),
266 "fcmp ult float " P1STR
", " P5STR
);
268 std::vector
<Constant
*> V
;
270 // FIXME: getGetElementPtr() actually creates an inbounds ConstantGEP,
272 // CHECK(ConstantExpr::getGetElementPtr(Global, V, false),
273 // "getelementptr i32*, i32** @dummy, i32 1");
274 CHECK(ConstantExpr::getInBoundsGetElementPtr(PointerType::getUnqual(Int32Ty
),
276 "getelementptr inbounds ptr, ptr @dummy, i32 1");
278 CHECK(ConstantExpr::getExtractElement(P6
, One
),
279 "extractelement <2 x i16> " P6STR
", i32 1");
281 EXPECT_EQ(Poison16
, ConstantExpr::getExtractElement(P6
, Two
));
282 EXPECT_EQ(Poison16
, ConstantExpr::getExtractElement(P6
, Big
));
283 EXPECT_EQ(Poison16
, ConstantExpr::getExtractElement(P6
, Undef64
));
285 EXPECT_EQ(Elt
, ConstantExpr::getExtractElement(
286 ConstantExpr::getInsertElement(P6
, Elt
, One
), One
));
287 EXPECT_EQ(PoisonV16
, ConstantExpr::getInsertElement(P6
, Elt
, Two
));
288 EXPECT_EQ(PoisonV16
, ConstantExpr::getInsertElement(P6
, Elt
, Big
));
289 EXPECT_EQ(PoisonV16
, ConstantExpr::getInsertElement(P6
, Elt
, Undef64
));
292 #ifdef GTEST_HAS_DEATH_TEST
294 TEST(ConstantsTest
, ReplaceWithConstantTest
) {
296 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
298 Type
*Int32Ty
= Type::getInt32Ty(Context
);
299 Constant
*One
= ConstantInt::get(Int32Ty
, 1);
302 M
->getOrInsertGlobal("dummy", PointerType::getUnqual(Int32Ty
));
303 Constant
*GEP
= ConstantExpr::getGetElementPtr(
304 PointerType::getUnqual(Int32Ty
), Global
, One
);
305 EXPECT_DEATH(Global
->replaceAllUsesWith(GEP
),
306 "this->replaceAllUsesWith\\(expr\\(this\\)\\) is NOT valid!");
314 TEST(ConstantsTest
, ConstantArrayReplaceWithConstant
) {
316 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
318 Type
*IntTy
= Type::getInt8Ty(Context
);
319 ArrayType
*ArrayTy
= ArrayType::get(IntTy
, 2);
320 Constant
*A01Vals
[2] = {ConstantInt::get(IntTy
, 0),
321 ConstantInt::get(IntTy
, 1)};
322 Constant
*A01
= ConstantArray::get(ArrayTy
, A01Vals
);
324 Constant
*Global
= new GlobalVariable(*M
, IntTy
, false,
325 GlobalValue::ExternalLinkage
, nullptr);
326 Constant
*GlobalInt
= ConstantExpr::getPtrToInt(Global
, IntTy
);
327 Constant
*A0GVals
[2] = {ConstantInt::get(IntTy
, 0), GlobalInt
};
328 Constant
*A0G
= ConstantArray::get(ArrayTy
, A0GVals
);
331 GlobalVariable
*RefArray
=
332 new GlobalVariable(*M
, ArrayTy
, false, GlobalValue::ExternalLinkage
, A0G
);
333 ASSERT_EQ(A0G
, RefArray
->getInitializer());
335 GlobalInt
->replaceAllUsesWith(ConstantInt::get(IntTy
, 1));
336 ASSERT_EQ(A01
, RefArray
->getInitializer());
339 TEST(ConstantsTest
, ConstantExprReplaceWithConstant
) {
341 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
343 Type
*IntTy
= Type::getInt8Ty(Context
);
344 Constant
*G1
= new GlobalVariable(*M
, IntTy
, false,
345 GlobalValue::ExternalLinkage
, nullptr);
346 Constant
*G2
= new GlobalVariable(*M
, IntTy
, false,
347 GlobalValue::ExternalLinkage
, nullptr);
350 Constant
*Int1
= ConstantExpr::getPtrToInt(G1
, IntTy
);
351 Constant
*Int2
= ConstantExpr::getPtrToInt(G2
, IntTy
);
352 ASSERT_NE(Int1
, Int2
);
354 GlobalVariable
*Ref
=
355 new GlobalVariable(*M
, IntTy
, false, GlobalValue::ExternalLinkage
, Int1
);
356 ASSERT_EQ(Int1
, Ref
->getInitializer());
358 G1
->replaceAllUsesWith(G2
);
359 ASSERT_EQ(Int2
, Ref
->getInitializer());
362 TEST(ConstantsTest
, GEPReplaceWithConstant
) {
364 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
366 Type
*IntTy
= Type::getInt32Ty(Context
);
367 Type
*PtrTy
= PointerType::get(IntTy
, 0);
368 auto *C1
= ConstantInt::get(IntTy
, 1);
369 auto *Placeholder
= new GlobalVariable(
370 *M
, IntTy
, false, GlobalValue::ExternalWeakLinkage
, nullptr);
371 auto *GEP
= ConstantExpr::getGetElementPtr(IntTy
, Placeholder
, C1
);
372 ASSERT_EQ(GEP
->getOperand(0), Placeholder
);
375 new GlobalVariable(*M
, PtrTy
, false, GlobalValue::ExternalLinkage
, GEP
);
376 ASSERT_EQ(GEP
, Ref
->getInitializer());
378 auto *Global
= new GlobalVariable(*M
, IntTy
, false,
379 GlobalValue::ExternalLinkage
, nullptr);
380 auto *Alias
= GlobalAlias::create(IntTy
, 0, GlobalValue::ExternalLinkage
,
381 "alias", Global
, M
.get());
382 Placeholder
->replaceAllUsesWith(Alias
);
383 ASSERT_EQ(GEP
, Ref
->getInitializer());
384 ASSERT_EQ(GEP
->getOperand(0), Alias
);
387 TEST(ConstantsTest
, AliasCAPI
) {
390 std::unique_ptr
<Module
> M
=
391 parseAssemblyString("@g = global i32 42", Error
, Context
);
392 GlobalVariable
*G
= M
->getGlobalVariable("g");
393 Type
*I16Ty
= Type::getInt16Ty(Context
);
394 Type
*I16PTy
= PointerType::get(I16Ty
, 0);
395 Constant
*Aliasee
= ConstantExpr::getBitCast(G
, I16PTy
);
396 LLVMValueRef AliasRef
=
397 LLVMAddAlias2(wrap(M
.get()), wrap(I16Ty
), 0, wrap(Aliasee
), "a");
398 ASSERT_EQ(unwrap
<GlobalAlias
>(AliasRef
)->getAliasee(), Aliasee
);
401 static std::string
getNameOfType(Type
*T
) {
403 raw_string_ostream
RSOS(S
);
408 TEST(ConstantsTest
, BuildConstantDataArrays
) {
411 for (Type
*T
: {Type::getInt8Ty(Context
), Type::getInt16Ty(Context
),
412 Type::getInt32Ty(Context
), Type::getInt64Ty(Context
)}) {
413 ArrayType
*ArrayTy
= ArrayType::get(T
, 2);
414 Constant
*Vals
[] = {ConstantInt::get(T
, 0), ConstantInt::get(T
, 1)};
415 Constant
*CA
= ConstantArray::get(ArrayTy
, Vals
);
416 ASSERT_TRUE(isa
<ConstantDataArray
>(CA
)) << " T = " << getNameOfType(T
);
417 auto *CDA
= cast
<ConstantDataArray
>(CA
);
418 Constant
*CA2
= ConstantDataArray::getRaw(
419 CDA
->getRawDataValues(), CDA
->getNumElements(), CDA
->getElementType());
420 ASSERT_TRUE(CA
== CA2
) << " T = " << getNameOfType(T
);
423 for (Type
*T
: {Type::getHalfTy(Context
), Type::getBFloatTy(Context
),
424 Type::getFloatTy(Context
), Type::getDoubleTy(Context
)}) {
425 ArrayType
*ArrayTy
= ArrayType::get(T
, 2);
426 Constant
*Vals
[] = {ConstantFP::get(T
, 0), ConstantFP::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
);
436 TEST(ConstantsTest
, BuildConstantDataVectors
) {
439 for (Type
*T
: {Type::getInt8Ty(Context
), Type::getInt16Ty(Context
),
440 Type::getInt32Ty(Context
), Type::getInt64Ty(Context
)}) {
441 Constant
*Vals
[] = {ConstantInt::get(T
, 0), ConstantInt::get(T
, 1)};
442 Constant
*CV
= ConstantVector::get(Vals
);
443 ASSERT_TRUE(isa
<ConstantDataVector
>(CV
)) << " T = " << getNameOfType(T
);
444 auto *CDV
= cast
<ConstantDataVector
>(CV
);
445 Constant
*CV2
= ConstantDataVector::getRaw(
446 CDV
->getRawDataValues(), CDV
->getNumElements(), CDV
->getElementType());
447 ASSERT_TRUE(CV
== CV2
) << " T = " << getNameOfType(T
);
450 for (Type
*T
: {Type::getHalfTy(Context
), Type::getBFloatTy(Context
),
451 Type::getFloatTy(Context
), Type::getDoubleTy(Context
)}) {
452 Constant
*Vals
[] = {ConstantFP::get(T
, 0), ConstantFP::get(T
, 1)};
453 Constant
*CV
= ConstantVector::get(Vals
);
454 ASSERT_TRUE(isa
<ConstantDataVector
>(CV
)) << " T = " << getNameOfType(T
);
455 auto *CDV
= cast
<ConstantDataVector
>(CV
);
456 Constant
*CV2
= ConstantDataVector::getRaw(
457 CDV
->getRawDataValues(), CDV
->getNumElements(), CDV
->getElementType());
458 ASSERT_TRUE(CV
== CV2
) << " 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
);
472 new GlobalVariable(*M
, S
, false, GlobalValue::ExternalLinkage
, nullptr);
473 auto *PtrTy
= PointerType::get(i32
, 0);
474 auto *C
= ConstantExpr::getBitCast(G
, PtrTy
);
475 /* With opaque pointers, no cast is necessary. */
479 bool foldFuncPtrAndConstToNull(LLVMContext
&Context
, Module
*TheModule
,
481 MaybeAlign FunctionAlign
= std::nullopt
) {
482 Type
*VoidType(Type::getVoidTy(Context
));
483 FunctionType
*FuncType(FunctionType::get(VoidType
, false));
485 Function::Create(FuncType
, GlobalValue::ExternalLinkage
, "", TheModule
));
488 Func
->setAlignment(*FunctionAlign
);
490 IntegerType
*ConstantIntType(Type::getInt32Ty(Context
));
491 ConstantInt
*TheConstant(ConstantInt::get(ConstantIntType
, AndValue
));
493 Constant
*TheConstantExpr(ConstantExpr::getPtrToInt(Func
, ConstantIntType
));
495 Constant
*C
= ConstantFoldBinaryInstruction(Instruction::And
, TheConstantExpr
,
497 bool Result
= C
&& C
->isNullValue();
500 // If the Module exists then it will delete the Function.
507 TEST(ConstantsTest
, FoldFunctionPtrAlignUnknownAnd2
) {
509 Module
TheModule("TestModule", Context
);
510 // When the DataLayout doesn't specify a function pointer alignment we
511 // assume in this case that it is 4 byte aligned. This is a bug but we can't
512 // fix it directly because it causes a code size regression on X86.
513 // FIXME: This test should be changed once existing targets have
514 // appropriate defaults. See associated FIXME in ConstantFoldBinaryInstruction
515 ASSERT_TRUE(foldFuncPtrAndConstToNull(Context
, &TheModule
, 2));
518 TEST(ConstantsTest
, DontFoldFunctionPtrAlignUnknownAnd4
) {
520 Module
TheModule("TestModule", Context
);
521 ASSERT_FALSE(foldFuncPtrAndConstToNull(Context
, &TheModule
, 4));
524 TEST(ConstantsTest
, FoldFunctionPtrAlign4
) {
526 Module
TheModule("TestModule", Context
);
527 const char *AlignmentStrings
[] = {"Fi32", "Fn32"};
529 for (unsigned AndValue
= 1; AndValue
<= 2; ++AndValue
) {
530 for (const char *AlignmentString
: AlignmentStrings
) {
531 TheModule
.setDataLayout(AlignmentString
);
532 ASSERT_TRUE(foldFuncPtrAndConstToNull(Context
, &TheModule
, AndValue
));
537 TEST(ConstantsTest
, DontFoldFunctionPtrAlign1
) {
539 Module
TheModule("TestModule", Context
);
540 const char *AlignmentStrings
[] = {"Fi8", "Fn8"};
542 for (const char *AlignmentString
: AlignmentStrings
) {
543 TheModule
.setDataLayout(AlignmentString
);
544 ASSERT_FALSE(foldFuncPtrAndConstToNull(Context
, &TheModule
, 2));
548 TEST(ConstantsTest
, FoldFunctionAlign4PtrAlignMultiple
) {
550 Module
TheModule("TestModule", Context
);
551 TheModule
.setDataLayout("Fn8");
552 ASSERT_TRUE(foldFuncPtrAndConstToNull(Context
, &TheModule
, 2, Align(4)));
555 TEST(ConstantsTest
, DontFoldFunctionAlign4PtrAlignIndependent
) {
557 Module
TheModule("TestModule", Context
);
558 TheModule
.setDataLayout("Fi8");
559 ASSERT_FALSE(foldFuncPtrAndConstToNull(Context
, &TheModule
, 2, Align(4)));
562 TEST(ConstantsTest
, DontFoldFunctionPtrIfNoModule
) {
564 // Even though the function is explicitly 4 byte aligned, in the absence of a
565 // DataLayout we can't assume that the function pointer is aligned.
566 ASSERT_FALSE(foldFuncPtrAndConstToNull(Context
, nullptr, 2, Align(4)));
569 TEST(ConstantsTest
, FoldGlobalVariablePtr
) {
572 IntegerType
*IntType(Type::getInt32Ty(Context
));
574 std::unique_ptr
<GlobalVariable
> Global(
575 new GlobalVariable(IntType
, true, GlobalValue::ExternalLinkage
));
577 Global
->setAlignment(Align(4));
579 ConstantInt
*TheConstant(ConstantInt::get(IntType
, 2));
581 Constant
*TheConstantExpr(ConstantExpr::getPtrToInt(Global
.get(), IntType
));
583 ASSERT_TRUE(ConstantFoldBinaryInstruction(Instruction::And
, TheConstantExpr
,
588 // Check that containsUndefOrPoisonElement and containsPoisonElement is working
591 TEST(ConstantsTest
, containsUndefElemTest
) {
594 Type
*Int32Ty
= Type::getInt32Ty(Context
);
595 Constant
*CU
= UndefValue::get(Int32Ty
);
596 Constant
*CP
= PoisonValue::get(Int32Ty
);
597 Constant
*C1
= ConstantInt::get(Int32Ty
, 1);
598 Constant
*C2
= ConstantInt::get(Int32Ty
, 2);
601 Constant
*V1
= ConstantVector::get({C1
, C2
});
602 EXPECT_FALSE(V1
->containsUndefOrPoisonElement());
603 EXPECT_FALSE(V1
->containsPoisonElement());
607 Constant
*V2
= ConstantVector::get({C1
, CU
});
608 EXPECT_TRUE(V2
->containsUndefOrPoisonElement());
609 EXPECT_FALSE(V2
->containsPoisonElement());
613 Constant
*V3
= ConstantVector::get({C1
, CP
});
614 EXPECT_TRUE(V3
->containsUndefOrPoisonElement());
615 EXPECT_TRUE(V3
->containsPoisonElement());
619 Constant
*V4
= ConstantVector::get({CU
, CP
});
620 EXPECT_TRUE(V4
->containsUndefOrPoisonElement());
621 EXPECT_TRUE(V4
->containsPoisonElement());
625 // Check that undefined elements in vector constants are matched
626 // correctly for both integer and floating-point types. Just don't
627 // crash on vectors of pointers (could be handled?).
629 TEST(ConstantsTest
, isElementWiseEqual
) {
632 Type
*Int32Ty
= Type::getInt32Ty(Context
);
633 Constant
*CU
= UndefValue::get(Int32Ty
);
634 Constant
*C1
= ConstantInt::get(Int32Ty
, 1);
635 Constant
*C2
= ConstantInt::get(Int32Ty
, 2);
637 Constant
*C1211
= ConstantVector::get({C1
, C2
, C1
, C1
});
638 Constant
*C12U1
= ConstantVector::get({C1
, C2
, CU
, C1
});
639 Constant
*C12U2
= ConstantVector::get({C1
, C2
, CU
, C2
});
640 Constant
*C12U21
= ConstantVector::get({C1
, C2
, CU
, C2
, C1
});
642 EXPECT_TRUE(C1211
->isElementWiseEqual(C12U1
));
643 EXPECT_TRUE(C12U1
->isElementWiseEqual(C1211
));
644 EXPECT_FALSE(C12U2
->isElementWiseEqual(C12U1
));
645 EXPECT_FALSE(C12U1
->isElementWiseEqual(C12U2
));
646 EXPECT_FALSE(C12U21
->isElementWiseEqual(C12U2
));
648 Type
*FltTy
= Type::getFloatTy(Context
);
649 Constant
*CFU
= UndefValue::get(FltTy
);
650 Constant
*CF1
= ConstantFP::get(FltTy
, 1.0);
651 Constant
*CF2
= ConstantFP::get(FltTy
, 2.0);
653 Constant
*CF1211
= ConstantVector::get({CF1
, CF2
, CF1
, CF1
});
654 Constant
*CF12U1
= ConstantVector::get({CF1
, CF2
, CFU
, CF1
});
655 Constant
*CF12U2
= ConstantVector::get({CF1
, CF2
, CFU
, CF2
});
656 Constant
*CFUU1U
= ConstantVector::get({CFU
, CFU
, CF1
, CFU
});
658 EXPECT_TRUE(CF1211
->isElementWiseEqual(CF12U1
));
659 EXPECT_TRUE(CF12U1
->isElementWiseEqual(CF1211
));
660 EXPECT_TRUE(CFUU1U
->isElementWiseEqual(CF12U1
));
661 EXPECT_FALSE(CF12U2
->isElementWiseEqual(CF12U1
));
662 EXPECT_FALSE(CF12U1
->isElementWiseEqual(CF12U2
));
664 PointerType
*PtrTy
= PointerType::get(Context
, 0);
665 Constant
*CPU
= UndefValue::get(PtrTy
);
666 Constant
*CP0
= ConstantPointerNull::get(PtrTy
);
668 Constant
*CP0000
= ConstantVector::get({CP0
, CP0
, CP0
, CP0
});
669 Constant
*CP00U0
= ConstantVector::get({CP0
, CP0
, CPU
, CP0
});
670 Constant
*CP00U
= ConstantVector::get({CP0
, CP0
, CPU
});
672 EXPECT_FALSE(CP0000
->isElementWiseEqual(CP00U0
));
673 EXPECT_FALSE(CP00U0
->isElementWiseEqual(CP0000
));
674 EXPECT_FALSE(CP0000
->isElementWiseEqual(CP00U
));
675 EXPECT_FALSE(CP00U
->isElementWiseEqual(CP00U0
));
678 // Check that vector/aggregate constants correctly store undef and poison
681 TEST(ConstantsTest
, CheckElementWiseUndefPoison
) {
684 Type
*Int32Ty
= Type::getInt32Ty(Context
);
685 StructType
*STy
= StructType::get(Int32Ty
, Int32Ty
);
686 ArrayType
*ATy
= ArrayType::get(Int32Ty
, 2);
687 Constant
*CU
= UndefValue::get(Int32Ty
);
688 Constant
*CP
= PoisonValue::get(Int32Ty
);
691 Constant
*CUU
= ConstantVector::get({CU
, CU
});
692 Constant
*CPP
= ConstantVector::get({CP
, CP
});
693 Constant
*CUP
= ConstantVector::get({CU
, CP
});
694 Constant
*CPU
= ConstantVector::get({CP
, CU
});
695 EXPECT_EQ(CUU
, UndefValue::get(CUU
->getType()));
696 EXPECT_EQ(CPP
, PoisonValue::get(CPP
->getType()));
697 EXPECT_NE(CUP
, UndefValue::get(CUP
->getType()));
698 EXPECT_NE(CPU
, UndefValue::get(CPU
->getType()));
702 Constant
*CUU
= ConstantStruct::get(STy
, {CU
, CU
});
703 Constant
*CPP
= ConstantStruct::get(STy
, {CP
, CP
});
704 Constant
*CUP
= ConstantStruct::get(STy
, {CU
, CP
});
705 Constant
*CPU
= ConstantStruct::get(STy
, {CP
, CU
});
706 EXPECT_EQ(CUU
, UndefValue::get(CUU
->getType()));
707 EXPECT_EQ(CPP
, PoisonValue::get(CPP
->getType()));
708 EXPECT_NE(CUP
, UndefValue::get(CUP
->getType()));
709 EXPECT_NE(CPU
, UndefValue::get(CPU
->getType()));
713 Constant
*CUU
= ConstantArray::get(ATy
, {CU
, CU
});
714 Constant
*CPP
= ConstantArray::get(ATy
, {CP
, CP
});
715 Constant
*CUP
= ConstantArray::get(ATy
, {CU
, CP
});
716 Constant
*CPU
= ConstantArray::get(ATy
, {CP
, CU
});
717 EXPECT_EQ(CUU
, UndefValue::get(CUU
->getType()));
718 EXPECT_EQ(CPP
, PoisonValue::get(CPP
->getType()));
719 EXPECT_NE(CUP
, UndefValue::get(CUP
->getType()));
720 EXPECT_NE(CPU
, UndefValue::get(CPU
->getType()));
724 TEST(ConstantsTest
, GetSplatValueRoundTrip
) {
727 Type
*FloatTy
= Type::getFloatTy(Context
);
728 Type
*Int32Ty
= Type::getInt32Ty(Context
);
729 Type
*Int8Ty
= Type::getInt8Ty(Context
);
731 for (unsigned Min
: {1, 2, 8}) {
732 auto ScalableEC
= ElementCount::getScalable(Min
);
733 auto FixedEC
= ElementCount::getFixed(Min
);
735 for (auto EC
: {ScalableEC
, FixedEC
}) {
736 for (auto *Ty
: {FloatTy
, Int32Ty
, Int8Ty
}) {
737 Constant
*Zero
= Constant::getNullValue(Ty
);
738 Constant
*One
= Constant::getAllOnesValue(Ty
);
740 for (auto *C
: {Zero
, One
}) {
741 Constant
*Splat
= ConstantVector::getSplat(EC
, C
);
742 ASSERT_NE(nullptr, Splat
);
744 Constant
*SplatVal
= Splat
->getSplatValue();
745 EXPECT_NE(nullptr, SplatVal
);
746 EXPECT_EQ(SplatVal
, C
);
753 TEST(ConstantsTest
, ComdatUserTracking
) {
755 Module
M("MyModule", Context
);
757 Comdat
*C
= M
.getOrInsertComdat("comdat");
758 const SmallPtrSetImpl
<GlobalObject
*> &Users
= C
->getUsers();
759 EXPECT_TRUE(Users
.size() == 0);
761 Type
*Ty
= Type::getInt8Ty(Context
);
762 GlobalVariable
*GV1
= cast
<GlobalVariable
>(M
.getOrInsertGlobal("gv1", Ty
));
764 EXPECT_TRUE(Users
.size() == 1);
765 EXPECT_TRUE(Users
.contains(GV1
));
767 GlobalVariable
*GV2
= cast
<GlobalVariable
>(M
.getOrInsertGlobal("gv2", Ty
));
769 EXPECT_TRUE(Users
.size() == 2);
770 EXPECT_TRUE(Users
.contains(GV2
));
772 GV1
->eraseFromParent();
773 EXPECT_TRUE(Users
.size() == 1);
774 EXPECT_TRUE(Users
.contains(GV2
));
776 GV2
->eraseFromParent();
777 EXPECT_TRUE(Users
.size() == 0);
780 } // end anonymous namespace
781 } // end namespace llvm