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
, ConstantFoldBinaryInstruction(Instruction::Shl
, One
, One
));
61 // @i = constant i1 shl(i1 1 , i1 0)
62 // @i = constant i1 true
63 EXPECT_EQ(One
, ConstantFoldBinaryInstruction(Instruction::Shl
, One
, Zero
));
65 // @n = constant i1 mul(i1 -1, i1 1)
66 // @n = constant i1 true
67 EXPECT_EQ(One
, ConstantExpr::getMul(NegOne
, One
));
69 // @o = constant i1 sdiv(i1 -1, i1 1) ; overflow
70 // @o = constant i1 true
71 EXPECT_EQ(One
, ConstantFoldBinaryInstruction(Instruction::SDiv
, NegOne
, One
));
73 // @p = constant i1 sdiv(i1 1 , i1 -1); overflow
74 // @p = constant i1 true
75 EXPECT_EQ(One
, ConstantFoldBinaryInstruction(Instruction::SDiv
, One
, NegOne
));
77 // @q = constant i1 udiv(i1 -1, i1 1)
78 // @q = constant i1 true
79 EXPECT_EQ(One
, ConstantFoldBinaryInstruction(Instruction::UDiv
, NegOne
, One
));
81 // @r = constant i1 udiv(i1 1, i1 -1)
82 // @r = constant i1 true
83 EXPECT_EQ(One
, ConstantFoldBinaryInstruction(Instruction::UDiv
, One
, NegOne
));
85 // @s = constant i1 srem(i1 -1, i1 1) ; overflow
86 // @s = constant i1 false
88 ConstantFoldBinaryInstruction(Instruction::SRem
, NegOne
, One
));
90 // @u = constant i1 srem(i1 1, i1 -1) ; overflow
91 // @u = constant i1 false
93 ConstantFoldBinaryInstruction(Instruction::SRem
, One
, NegOne
));
96 TEST(ConstantsTest
, IntSigns
) {
98 IntegerType
*Int8Ty
= Type::getInt8Ty(Context
);
99 EXPECT_EQ(100, ConstantInt::get(Int8Ty
, 100, false)->getSExtValue());
100 EXPECT_EQ(100, ConstantInt::get(Int8Ty
, 100, true)->getSExtValue());
101 EXPECT_EQ(100, ConstantInt::getSigned(Int8Ty
, 100)->getSExtValue());
102 EXPECT_EQ(-50, ConstantInt::get(Int8Ty
, 206)->getSExtValue());
103 EXPECT_EQ(-50, ConstantInt::getSigned(Int8Ty
, -50)->getSExtValue());
104 EXPECT_EQ(206U, ConstantInt::getSigned(Int8Ty
, -50)->getZExtValue());
106 // Overflow is handled by truncation.
107 EXPECT_EQ(0x3b, ConstantInt::get(Int8Ty
, 0x13b)->getSExtValue());
110 TEST(ConstantsTest
, PointerCast
) {
112 Type
*PtrTy
= PointerType::get(C
, 0);
113 Type
*Int64Ty
= Type::getInt64Ty(C
);
114 VectorType
*PtrVecTy
= FixedVectorType::get(PtrTy
, 4);
115 VectorType
*Int64VecTy
= FixedVectorType::get(Int64Ty
, 4);
116 VectorType
*PtrScalableVecTy
= ScalableVectorType::get(PtrTy
, 4);
117 VectorType
*Int64ScalableVecTy
= ScalableVectorType::get(Int64Ty
, 4);
119 // ptrtoint ptr to i64
121 Constant::getNullValue(Int64Ty
),
122 ConstantExpr::getPointerCast(Constant::getNullValue(PtrTy
), Int64Ty
));
124 // bitcast ptr to ptr
125 EXPECT_EQ(Constant::getNullValue(PtrTy
),
126 ConstantExpr::getPointerCast(Constant::getNullValue(PtrTy
), PtrTy
));
128 // ptrtoint <4 x ptr> to <4 x i64>
129 EXPECT_EQ(Constant::getNullValue(Int64VecTy
),
130 ConstantExpr::getPointerCast(Constant::getNullValue(PtrVecTy
),
133 // ptrtoint <vscale x 4 x ptr> to <vscale x 4 x i64>
134 EXPECT_EQ(Constant::getNullValue(Int64ScalableVecTy
),
135 ConstantExpr::getPointerCast(
136 Constant::getNullValue(PtrScalableVecTy
), Int64ScalableVecTy
));
138 // bitcast <4 x ptr> to <4 x ptr>
140 Constant::getNullValue(PtrVecTy
),
141 ConstantExpr::getPointerCast(Constant::getNullValue(PtrVecTy
), PtrVecTy
));
143 // bitcast <vscale x 4 x ptr> to <vscale x 4 x ptr>
144 EXPECT_EQ(Constant::getNullValue(PtrScalableVecTy
),
145 ConstantExpr::getPointerCast(
146 Constant::getNullValue(PtrScalableVecTy
), PtrScalableVecTy
));
148 Type
*Ptr1Ty
= PointerType::get(C
, 1);
149 ConstantInt
*K
= ConstantInt::get(Type::getInt64Ty(C
), 1234);
151 // Make sure that addrspacecast of inttoptr is not folded away.
152 EXPECT_NE(K
, ConstantExpr::getAddrSpaceCast(
153 ConstantExpr::getIntToPtr(K
, PtrTy
), Ptr1Ty
));
154 EXPECT_NE(K
, ConstantExpr::getAddrSpaceCast(
155 ConstantExpr::getIntToPtr(K
, Ptr1Ty
), PtrTy
));
157 Constant
*NullPtr0
= Constant::getNullValue(PtrTy
);
158 Constant
*NullPtr1
= Constant::getNullValue(Ptr1Ty
);
160 // Make sure that addrspacecast of null is not folded away.
161 EXPECT_NE(Constant::getNullValue(PtrTy
),
162 ConstantExpr::getAddrSpaceCast(NullPtr0
, Ptr1Ty
));
164 EXPECT_NE(Constant::getNullValue(Ptr1Ty
),
165 ConstantExpr::getAddrSpaceCast(NullPtr1
, PtrTy
));
168 #define CHECK(x, y) \
171 raw_string_ostream __o(__s); \
172 Instruction *__I = cast<ConstantExpr>(x)->getAsInstruction(); \
174 __I->deleteValue(); \
176 EXPECT_EQ(std::string(" <badref> = " y), __s); \
179 TEST(ConstantsTest
, AsInstructionsTest
) {
181 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
183 Type
*Int64Ty
= Type::getInt64Ty(Context
);
184 Type
*Int32Ty
= Type::getInt32Ty(Context
);
185 Type
*Int16Ty
= Type::getInt16Ty(Context
);
188 M
->getOrInsertGlobal("dummy", PointerType::getUnqual(Int32Ty
));
190 M
->getOrInsertGlobal("dummy2", PointerType::getUnqual(Int32Ty
));
192 Constant
*P0
= ConstantExpr::getPtrToInt(Global
, Int32Ty
);
193 Constant
*P4
= ConstantExpr::getPtrToInt(Global2
, Int32Ty
);
194 Constant
*P6
= ConstantExpr::getBitCast(P4
, FixedVectorType::get(Int16Ty
, 2));
196 Constant
*One
= ConstantInt::get(Int32Ty
, 1);
197 Constant
*Two
= ConstantInt::get(Int64Ty
, 2);
198 Constant
*Big
= ConstantInt::get(Context
, APInt
{256, uint64_t(-1), true});
199 Constant
*Elt
= ConstantInt::get(Int16Ty
, 2015);
200 Constant
*Poison16
= PoisonValue::get(Int16Ty
);
201 Constant
*Undef64
= UndefValue::get(Int64Ty
);
202 Constant
*PoisonV16
= PoisonValue::get(P6
->getType());
204 #define P0STR "ptrtoint (ptr @dummy to i32)"
205 #define P3STR "ptrtoint (ptr @dummy to i1)"
206 #define P4STR "ptrtoint (ptr @dummy2 to i32)"
207 #define P6STR "bitcast (i32 ptrtoint (ptr @dummy2 to i32) to <2 x i16>)"
209 CHECK(ConstantExpr::getNeg(P0
), "sub i32 0, " P0STR
);
210 CHECK(ConstantExpr::getNot(P0
), "xor i32 " P0STR
", -1");
211 CHECK(ConstantExpr::getAdd(P0
, P0
), "add i32 " P0STR
", " P0STR
);
212 CHECK(ConstantExpr::getAdd(P0
, P0
, false, true),
213 "add nsw i32 " P0STR
", " P0STR
);
214 CHECK(ConstantExpr::getAdd(P0
, P0
, true, true),
215 "add nuw nsw i32 " P0STR
", " P0STR
);
216 CHECK(ConstantExpr::getSub(P0
, P0
), "sub i32 " P0STR
", " P0STR
);
217 CHECK(ConstantExpr::getMul(P0
, P0
), "mul i32 " P0STR
", " P0STR
);
218 CHECK(ConstantExpr::getXor(P0
, P0
), "xor i32 " P0STR
", " P0STR
);
220 std::vector
<Constant
*> V
;
222 // FIXME: getGetElementPtr() actually creates an inbounds ConstantGEP,
224 // CHECK(ConstantExpr::getGetElementPtr(Global, V, false),
225 // "getelementptr i32*, i32** @dummy, i32 1");
226 CHECK(ConstantExpr::getInBoundsGetElementPtr(PointerType::getUnqual(Int32Ty
),
228 "getelementptr inbounds ptr, ptr @dummy, i32 1");
230 CHECK(ConstantExpr::getExtractElement(P6
, One
),
231 "extractelement <2 x i16> " P6STR
", i32 1");
233 EXPECT_EQ(Poison16
, ConstantExpr::getExtractElement(P6
, Two
));
234 EXPECT_EQ(Poison16
, ConstantExpr::getExtractElement(P6
, Big
));
235 EXPECT_EQ(Poison16
, ConstantExpr::getExtractElement(P6
, Undef64
));
237 EXPECT_EQ(Elt
, ConstantExpr::getExtractElement(
238 ConstantExpr::getInsertElement(P6
, Elt
, One
), One
));
239 EXPECT_EQ(PoisonV16
, ConstantExpr::getInsertElement(P6
, Elt
, Two
));
240 EXPECT_EQ(PoisonV16
, ConstantExpr::getInsertElement(P6
, Elt
, Big
));
241 EXPECT_EQ(PoisonV16
, ConstantExpr::getInsertElement(P6
, Elt
, Undef64
));
244 #ifdef GTEST_HAS_DEATH_TEST
246 TEST(ConstantsTest
, ReplaceWithConstantTest
) {
248 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
250 Type
*Int32Ty
= Type::getInt32Ty(Context
);
251 Constant
*One
= ConstantInt::get(Int32Ty
, 1);
254 M
->getOrInsertGlobal("dummy", PointerType::getUnqual(Int32Ty
));
255 Constant
*GEP
= ConstantExpr::getGetElementPtr(
256 PointerType::getUnqual(Int32Ty
), Global
, One
);
257 EXPECT_DEATH(Global
->replaceAllUsesWith(GEP
),
258 "this->replaceAllUsesWith\\(expr\\(this\\)\\) is NOT valid!");
266 TEST(ConstantsTest
, ConstantArrayReplaceWithConstant
) {
268 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
270 Type
*IntTy
= Type::getInt8Ty(Context
);
271 ArrayType
*ArrayTy
= ArrayType::get(IntTy
, 2);
272 Constant
*A01Vals
[2] = {ConstantInt::get(IntTy
, 0),
273 ConstantInt::get(IntTy
, 1)};
274 Constant
*A01
= ConstantArray::get(ArrayTy
, A01Vals
);
276 Constant
*Global
= new GlobalVariable(*M
, IntTy
, false,
277 GlobalValue::ExternalLinkage
, nullptr);
278 Constant
*GlobalInt
= ConstantExpr::getPtrToInt(Global
, IntTy
);
279 Constant
*A0GVals
[2] = {ConstantInt::get(IntTy
, 0), GlobalInt
};
280 Constant
*A0G
= ConstantArray::get(ArrayTy
, A0GVals
);
283 GlobalVariable
*RefArray
=
284 new GlobalVariable(*M
, ArrayTy
, false, GlobalValue::ExternalLinkage
, A0G
);
285 ASSERT_EQ(A0G
, RefArray
->getInitializer());
287 GlobalInt
->replaceAllUsesWith(ConstantInt::get(IntTy
, 1));
288 ASSERT_EQ(A01
, RefArray
->getInitializer());
291 TEST(ConstantsTest
, ConstantExprReplaceWithConstant
) {
293 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
295 Type
*IntTy
= Type::getInt8Ty(Context
);
296 Constant
*G1
= new GlobalVariable(*M
, IntTy
, false,
297 GlobalValue::ExternalLinkage
, nullptr);
298 Constant
*G2
= new GlobalVariable(*M
, IntTy
, false,
299 GlobalValue::ExternalLinkage
, nullptr);
302 Constant
*Int1
= ConstantExpr::getPtrToInt(G1
, IntTy
);
303 Constant
*Int2
= ConstantExpr::getPtrToInt(G2
, IntTy
);
304 ASSERT_NE(Int1
, Int2
);
306 GlobalVariable
*Ref
=
307 new GlobalVariable(*M
, IntTy
, false, GlobalValue::ExternalLinkage
, Int1
);
308 ASSERT_EQ(Int1
, Ref
->getInitializer());
310 G1
->replaceAllUsesWith(G2
);
311 ASSERT_EQ(Int2
, Ref
->getInitializer());
314 TEST(ConstantsTest
, GEPReplaceWithConstant
) {
316 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
318 Type
*IntTy
= Type::getInt32Ty(Context
);
319 Type
*PtrTy
= PointerType::get(IntTy
, 0);
320 auto *C1
= ConstantInt::get(IntTy
, 1);
321 auto *Placeholder
= new GlobalVariable(
322 *M
, IntTy
, false, GlobalValue::ExternalWeakLinkage
, nullptr);
323 auto *GEP
= ConstantExpr::getGetElementPtr(IntTy
, Placeholder
, C1
);
324 ASSERT_EQ(GEP
->getOperand(0), Placeholder
);
327 new GlobalVariable(*M
, PtrTy
, false, GlobalValue::ExternalLinkage
, GEP
);
328 ASSERT_EQ(GEP
, Ref
->getInitializer());
330 auto *Global
= new GlobalVariable(*M
, IntTy
, false,
331 GlobalValue::ExternalLinkage
, nullptr);
332 auto *Alias
= GlobalAlias::create(IntTy
, 0, GlobalValue::ExternalLinkage
,
333 "alias", Global
, M
.get());
334 Placeholder
->replaceAllUsesWith(Alias
);
335 ASSERT_EQ(GEP
, Ref
->getInitializer());
336 ASSERT_EQ(GEP
->getOperand(0), Alias
);
339 TEST(ConstantsTest
, AliasCAPI
) {
342 std::unique_ptr
<Module
> M
=
343 parseAssemblyString("@g = global i32 42", Error
, Context
);
344 GlobalVariable
*G
= M
->getGlobalVariable("g");
345 Type
*I16Ty
= Type::getInt16Ty(Context
);
346 Type
*I16PTy
= PointerType::get(I16Ty
, 0);
347 Constant
*Aliasee
= ConstantExpr::getBitCast(G
, I16PTy
);
348 LLVMValueRef AliasRef
=
349 LLVMAddAlias2(wrap(M
.get()), wrap(I16Ty
), 0, wrap(Aliasee
), "a");
350 ASSERT_EQ(unwrap
<GlobalAlias
>(AliasRef
)->getAliasee(), Aliasee
);
353 static std::string
getNameOfType(Type
*T
) {
355 raw_string_ostream
RSOS(S
);
360 TEST(ConstantsTest
, BuildConstantDataArrays
) {
363 for (Type
*T
: {Type::getInt8Ty(Context
), Type::getInt16Ty(Context
),
364 Type::getInt32Ty(Context
), Type::getInt64Ty(Context
)}) {
365 ArrayType
*ArrayTy
= ArrayType::get(T
, 2);
366 Constant
*Vals
[] = {ConstantInt::get(T
, 0), ConstantInt::get(T
, 1)};
367 Constant
*CA
= ConstantArray::get(ArrayTy
, Vals
);
368 ASSERT_TRUE(isa
<ConstantDataArray
>(CA
)) << " T = " << getNameOfType(T
);
369 auto *CDA
= cast
<ConstantDataArray
>(CA
);
370 Constant
*CA2
= ConstantDataArray::getRaw(
371 CDA
->getRawDataValues(), CDA
->getNumElements(), CDA
->getElementType());
372 ASSERT_TRUE(CA
== CA2
) << " T = " << getNameOfType(T
);
375 for (Type
*T
: {Type::getHalfTy(Context
), Type::getBFloatTy(Context
),
376 Type::getFloatTy(Context
), Type::getDoubleTy(Context
)}) {
377 ArrayType
*ArrayTy
= ArrayType::get(T
, 2);
378 Constant
*Vals
[] = {ConstantFP::get(T
, 0), ConstantFP::get(T
, 1)};
379 Constant
*CA
= ConstantArray::get(ArrayTy
, Vals
);
380 ASSERT_TRUE(isa
<ConstantDataArray
>(CA
)) << " T = " << getNameOfType(T
);
381 auto *CDA
= cast
<ConstantDataArray
>(CA
);
382 Constant
*CA2
= ConstantDataArray::getRaw(
383 CDA
->getRawDataValues(), CDA
->getNumElements(), CDA
->getElementType());
384 ASSERT_TRUE(CA
== CA2
) << " T = " << getNameOfType(T
);
388 TEST(ConstantsTest
, BuildConstantDataVectors
) {
391 for (Type
*T
: {Type::getInt8Ty(Context
), Type::getInt16Ty(Context
),
392 Type::getInt32Ty(Context
), Type::getInt64Ty(Context
)}) {
393 Constant
*Vals
[] = {ConstantInt::get(T
, 0), ConstantInt::get(T
, 1)};
394 Constant
*CV
= ConstantVector::get(Vals
);
395 ASSERT_TRUE(isa
<ConstantDataVector
>(CV
)) << " T = " << getNameOfType(T
);
396 auto *CDV
= cast
<ConstantDataVector
>(CV
);
397 Constant
*CV2
= ConstantDataVector::getRaw(
398 CDV
->getRawDataValues(), CDV
->getNumElements(), CDV
->getElementType());
399 ASSERT_TRUE(CV
== CV2
) << " T = " << getNameOfType(T
);
402 for (Type
*T
: {Type::getHalfTy(Context
), Type::getBFloatTy(Context
),
403 Type::getFloatTy(Context
), Type::getDoubleTy(Context
)}) {
404 Constant
*Vals
[] = {ConstantFP::get(T
, 0), ConstantFP::get(T
, 1)};
405 Constant
*CV
= ConstantVector::get(Vals
);
406 ASSERT_TRUE(isa
<ConstantDataVector
>(CV
)) << " T = " << getNameOfType(T
);
407 auto *CDV
= cast
<ConstantDataVector
>(CV
);
408 Constant
*CV2
= ConstantDataVector::getRaw(
409 CDV
->getRawDataValues(), CDV
->getNumElements(), CDV
->getElementType());
410 ASSERT_TRUE(CV
== CV2
) << " T = " << getNameOfType(T
);
414 TEST(ConstantsTest
, BitcastToGEP
) {
416 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
418 auto *i32
= Type::getInt32Ty(Context
);
419 auto *U
= StructType::create(Context
, "Unsized");
420 Type
*EltTys
[] = {i32
, U
};
421 auto *S
= StructType::create(EltTys
);
424 new GlobalVariable(*M
, S
, false, GlobalValue::ExternalLinkage
, nullptr);
425 auto *PtrTy
= PointerType::get(i32
, 0);
426 auto *C
= ConstantExpr::getBitCast(G
, PtrTy
);
427 /* With opaque pointers, no cast is necessary. */
431 bool foldFuncPtrAndConstToNull(LLVMContext
&Context
, Module
*TheModule
,
433 MaybeAlign FunctionAlign
= std::nullopt
) {
434 Type
*VoidType(Type::getVoidTy(Context
));
435 FunctionType
*FuncType(FunctionType::get(VoidType
, false));
437 Function::Create(FuncType
, GlobalValue::ExternalLinkage
, "", TheModule
));
440 Func
->setAlignment(*FunctionAlign
);
442 IntegerType
*ConstantIntType(Type::getInt32Ty(Context
));
443 ConstantInt
*TheConstant(ConstantInt::get(ConstantIntType
, AndValue
));
445 Constant
*TheConstantExpr(ConstantExpr::getPtrToInt(Func
, ConstantIntType
));
447 Constant
*C
= ConstantFoldBinaryInstruction(Instruction::And
, TheConstantExpr
,
449 bool Result
= C
&& C
->isNullValue();
452 // If the Module exists then it will delete the Function.
459 TEST(ConstantsTest
, FoldFunctionPtrAlignUnknownAnd2
) {
461 Module
TheModule("TestModule", Context
);
462 // When the DataLayout doesn't specify a function pointer alignment we
463 // assume in this case that it is 4 byte aligned. This is a bug but we can't
464 // fix it directly because it causes a code size regression on X86.
465 // FIXME: This test should be changed once existing targets have
466 // appropriate defaults. See associated FIXME in ConstantFoldBinaryInstruction
467 ASSERT_TRUE(foldFuncPtrAndConstToNull(Context
, &TheModule
, 2));
470 TEST(ConstantsTest
, DontFoldFunctionPtrAlignUnknownAnd4
) {
472 Module
TheModule("TestModule", Context
);
473 ASSERT_FALSE(foldFuncPtrAndConstToNull(Context
, &TheModule
, 4));
476 TEST(ConstantsTest
, FoldFunctionPtrAlign4
) {
478 Module
TheModule("TestModule", Context
);
479 const char *AlignmentStrings
[] = {"Fi32", "Fn32"};
481 for (unsigned AndValue
= 1; AndValue
<= 2; ++AndValue
) {
482 for (const char *AlignmentString
: AlignmentStrings
) {
483 TheModule
.setDataLayout(AlignmentString
);
484 ASSERT_TRUE(foldFuncPtrAndConstToNull(Context
, &TheModule
, AndValue
));
489 TEST(ConstantsTest
, DontFoldFunctionPtrAlign1
) {
491 Module
TheModule("TestModule", Context
);
492 const char *AlignmentStrings
[] = {"Fi8", "Fn8"};
494 for (const char *AlignmentString
: AlignmentStrings
) {
495 TheModule
.setDataLayout(AlignmentString
);
496 ASSERT_FALSE(foldFuncPtrAndConstToNull(Context
, &TheModule
, 2));
500 TEST(ConstantsTest
, FoldFunctionAlign4PtrAlignMultiple
) {
502 Module
TheModule("TestModule", Context
);
503 TheModule
.setDataLayout("Fn8");
504 ASSERT_TRUE(foldFuncPtrAndConstToNull(Context
, &TheModule
, 2, Align(4)));
507 TEST(ConstantsTest
, DontFoldFunctionAlign4PtrAlignIndependent
) {
509 Module
TheModule("TestModule", Context
);
510 TheModule
.setDataLayout("Fi8");
511 ASSERT_FALSE(foldFuncPtrAndConstToNull(Context
, &TheModule
, 2, Align(4)));
514 TEST(ConstantsTest
, DontFoldFunctionPtrIfNoModule
) {
516 // Even though the function is explicitly 4 byte aligned, in the absence of a
517 // DataLayout we can't assume that the function pointer is aligned.
518 ASSERT_FALSE(foldFuncPtrAndConstToNull(Context
, nullptr, 2, Align(4)));
521 TEST(ConstantsTest
, FoldGlobalVariablePtr
) {
524 IntegerType
*IntType(Type::getInt32Ty(Context
));
526 std::unique_ptr
<GlobalVariable
> Global(
527 new GlobalVariable(IntType
, true, GlobalValue::ExternalLinkage
));
529 Global
->setAlignment(Align(4));
531 ConstantInt
*TheConstant(ConstantInt::get(IntType
, 2));
533 Constant
*TheConstantExpr(ConstantExpr::getPtrToInt(Global
.get(), IntType
));
535 ASSERT_TRUE(ConstantFoldBinaryInstruction(Instruction::And
, TheConstantExpr
,
540 // Check that containsUndefOrPoisonElement and containsPoisonElement is working
543 TEST(ConstantsTest
, containsUndefElemTest
) {
546 Type
*Int32Ty
= Type::getInt32Ty(Context
);
547 Constant
*CU
= UndefValue::get(Int32Ty
);
548 Constant
*CP
= PoisonValue::get(Int32Ty
);
549 Constant
*C1
= ConstantInt::get(Int32Ty
, 1);
550 Constant
*C2
= ConstantInt::get(Int32Ty
, 2);
553 Constant
*V1
= ConstantVector::get({C1
, C2
});
554 EXPECT_FALSE(V1
->containsUndefOrPoisonElement());
555 EXPECT_FALSE(V1
->containsPoisonElement());
559 Constant
*V2
= ConstantVector::get({C1
, CU
});
560 EXPECT_TRUE(V2
->containsUndefOrPoisonElement());
561 EXPECT_FALSE(V2
->containsPoisonElement());
565 Constant
*V3
= ConstantVector::get({C1
, CP
});
566 EXPECT_TRUE(V3
->containsUndefOrPoisonElement());
567 EXPECT_TRUE(V3
->containsPoisonElement());
571 Constant
*V4
= ConstantVector::get({CU
, CP
});
572 EXPECT_TRUE(V4
->containsUndefOrPoisonElement());
573 EXPECT_TRUE(V4
->containsPoisonElement());
577 // Check that poison elements in vector constants are matched
578 // correctly for both integer and floating-point types. Just don't
579 // crash on vectors of pointers (could be handled?).
581 TEST(ConstantsTest
, isElementWiseEqual
) {
584 Type
*Int32Ty
= Type::getInt32Ty(Context
);
585 Constant
*CU
= UndefValue::get(Int32Ty
);
586 Constant
*CP
= PoisonValue::get(Int32Ty
);
587 Constant
*C1
= ConstantInt::get(Int32Ty
, 1);
588 Constant
*C2
= ConstantInt::get(Int32Ty
, 2);
590 Constant
*C1211
= ConstantVector::get({C1
, C2
, C1
, C1
});
591 Constant
*C12U1
= ConstantVector::get({C1
, C2
, CU
, C1
});
592 Constant
*C12U2
= ConstantVector::get({C1
, C2
, CU
, C2
});
593 Constant
*C12U21
= ConstantVector::get({C1
, C2
, CU
, C2
, C1
});
594 Constant
*C12P1
= ConstantVector::get({C1
, C2
, CP
, C1
});
595 Constant
*C12P2
= ConstantVector::get({C1
, C2
, CP
, C2
});
596 Constant
*C12P21
= ConstantVector::get({C1
, C2
, CP
, C2
, C1
});
598 EXPECT_FALSE(C1211
->isElementWiseEqual(C12U1
));
599 EXPECT_FALSE(C12U1
->isElementWiseEqual(C1211
));
600 EXPECT_FALSE(C12U2
->isElementWiseEqual(C12U1
));
601 EXPECT_FALSE(C12U1
->isElementWiseEqual(C12U2
));
602 EXPECT_FALSE(C12U21
->isElementWiseEqual(C12U2
));
604 EXPECT_TRUE(C1211
->isElementWiseEqual(C12P1
));
605 EXPECT_TRUE(C12P1
->isElementWiseEqual(C1211
));
606 EXPECT_FALSE(C12P2
->isElementWiseEqual(C12P1
));
607 EXPECT_FALSE(C12P1
->isElementWiseEqual(C12P2
));
608 EXPECT_FALSE(C12P21
->isElementWiseEqual(C12P2
));
610 Type
*FltTy
= Type::getFloatTy(Context
);
611 Constant
*CFU
= UndefValue::get(FltTy
);
612 Constant
*CFP
= PoisonValue::get(FltTy
);
613 Constant
*CF1
= ConstantFP::get(FltTy
, 1.0);
614 Constant
*CF2
= ConstantFP::get(FltTy
, 2.0);
616 Constant
*CF1211
= ConstantVector::get({CF1
, CF2
, CF1
, CF1
});
617 Constant
*CF12U1
= ConstantVector::get({CF1
, CF2
, CFU
, CF1
});
618 Constant
*CF12U2
= ConstantVector::get({CF1
, CF2
, CFU
, CF2
});
619 Constant
*CFUU1U
= ConstantVector::get({CFU
, CFU
, CF1
, CFU
});
620 Constant
*CF12P1
= ConstantVector::get({CF1
, CF2
, CFP
, CF1
});
621 Constant
*CF12P2
= ConstantVector::get({CF1
, CF2
, CFP
, CF2
});
622 Constant
*CFPP1P
= ConstantVector::get({CFP
, CFP
, CF1
, CFP
});
624 EXPECT_FALSE(CF1211
->isElementWiseEqual(CF12U1
));
625 EXPECT_FALSE(CF12U1
->isElementWiseEqual(CF1211
));
626 EXPECT_FALSE(CFUU1U
->isElementWiseEqual(CF12U1
));
627 EXPECT_FALSE(CF12U2
->isElementWiseEqual(CF12U1
));
628 EXPECT_FALSE(CF12U1
->isElementWiseEqual(CF12U2
));
630 EXPECT_TRUE(CF1211
->isElementWiseEqual(CF12P1
));
631 EXPECT_TRUE(CF12P1
->isElementWiseEqual(CF1211
));
632 EXPECT_TRUE(CFPP1P
->isElementWiseEqual(CF12P1
));
633 EXPECT_FALSE(CF12P2
->isElementWiseEqual(CF12P1
));
634 EXPECT_FALSE(CF12P1
->isElementWiseEqual(CF12P2
));
636 PointerType
*PtrTy
= PointerType::get(Context
, 0);
637 Constant
*CPU
= UndefValue::get(PtrTy
);
638 Constant
*CPP
= PoisonValue::get(PtrTy
);
639 Constant
*CP0
= ConstantPointerNull::get(PtrTy
);
641 Constant
*CP0000
= ConstantVector::get({CP0
, CP0
, CP0
, CP0
});
642 Constant
*CP00U0
= ConstantVector::get({CP0
, CP0
, CPU
, CP0
});
643 Constant
*CP00U
= ConstantVector::get({CP0
, CP0
, CPU
});
644 Constant
*CP00P0
= ConstantVector::get({CP0
, CP0
, CPP
, CP0
});
645 Constant
*CP00P
= ConstantVector::get({CP0
, CP0
, CPP
});
647 EXPECT_FALSE(CP0000
->isElementWiseEqual(CP00U0
));
648 EXPECT_FALSE(CP00U0
->isElementWiseEqual(CP0000
));
649 EXPECT_FALSE(CP0000
->isElementWiseEqual(CP00U
));
650 EXPECT_FALSE(CP00U
->isElementWiseEqual(CP00U0
));
651 EXPECT_FALSE(CP0000
->isElementWiseEqual(CP00P0
));
652 EXPECT_FALSE(CP00P0
->isElementWiseEqual(CP0000
));
653 EXPECT_FALSE(CP0000
->isElementWiseEqual(CP00P
));
654 EXPECT_FALSE(CP00P
->isElementWiseEqual(CP00P0
));
657 // Check that vector/aggregate constants correctly store undef and poison
660 TEST(ConstantsTest
, CheckElementWiseUndefPoison
) {
663 Type
*Int32Ty
= Type::getInt32Ty(Context
);
664 StructType
*STy
= StructType::get(Int32Ty
, Int32Ty
);
665 ArrayType
*ATy
= ArrayType::get(Int32Ty
, 2);
666 Constant
*CU
= UndefValue::get(Int32Ty
);
667 Constant
*CP
= PoisonValue::get(Int32Ty
);
670 Constant
*CUU
= ConstantVector::get({CU
, CU
});
671 Constant
*CPP
= ConstantVector::get({CP
, CP
});
672 Constant
*CUP
= ConstantVector::get({CU
, CP
});
673 Constant
*CPU
= ConstantVector::get({CP
, CU
});
674 EXPECT_EQ(CUU
, UndefValue::get(CUU
->getType()));
675 EXPECT_EQ(CPP
, PoisonValue::get(CPP
->getType()));
676 EXPECT_NE(CUP
, UndefValue::get(CUP
->getType()));
677 EXPECT_NE(CPU
, UndefValue::get(CPU
->getType()));
681 Constant
*CUU
= ConstantStruct::get(STy
, {CU
, CU
});
682 Constant
*CPP
= ConstantStruct::get(STy
, {CP
, CP
});
683 Constant
*CUP
= ConstantStruct::get(STy
, {CU
, CP
});
684 Constant
*CPU
= ConstantStruct::get(STy
, {CP
, CU
});
685 EXPECT_EQ(CUU
, UndefValue::get(CUU
->getType()));
686 EXPECT_EQ(CPP
, PoisonValue::get(CPP
->getType()));
687 EXPECT_NE(CUP
, UndefValue::get(CUP
->getType()));
688 EXPECT_NE(CPU
, UndefValue::get(CPU
->getType()));
692 Constant
*CUU
= ConstantArray::get(ATy
, {CU
, CU
});
693 Constant
*CPP
= ConstantArray::get(ATy
, {CP
, CP
});
694 Constant
*CUP
= ConstantArray::get(ATy
, {CU
, CP
});
695 Constant
*CPU
= ConstantArray::get(ATy
, {CP
, CU
});
696 EXPECT_EQ(CUU
, UndefValue::get(CUU
->getType()));
697 EXPECT_EQ(CPP
, PoisonValue::get(CPP
->getType()));
698 EXPECT_NE(CUP
, UndefValue::get(CUP
->getType()));
699 EXPECT_NE(CPU
, UndefValue::get(CPU
->getType()));
703 TEST(ConstantsTest
, GetSplatValueRoundTrip
) {
706 Type
*FloatTy
= Type::getFloatTy(Context
);
707 Type
*Int32Ty
= Type::getInt32Ty(Context
);
708 Type
*Int8Ty
= Type::getInt8Ty(Context
);
710 for (unsigned Min
: {1, 2, 8}) {
711 auto ScalableEC
= ElementCount::getScalable(Min
);
712 auto FixedEC
= ElementCount::getFixed(Min
);
714 for (auto EC
: {ScalableEC
, FixedEC
}) {
715 for (auto *Ty
: {FloatTy
, Int32Ty
, Int8Ty
}) {
716 Constant
*Zero
= Constant::getNullValue(Ty
);
717 Constant
*One
= Constant::getAllOnesValue(Ty
);
719 for (auto *C
: {Zero
, One
}) {
720 Constant
*Splat
= ConstantVector::getSplat(EC
, C
);
721 ASSERT_NE(nullptr, Splat
);
723 Constant
*SplatVal
= Splat
->getSplatValue();
724 EXPECT_NE(nullptr, SplatVal
);
725 EXPECT_EQ(SplatVal
, C
);
732 TEST(ConstantsTest
, ComdatUserTracking
) {
734 Module
M("MyModule", Context
);
736 Comdat
*C
= M
.getOrInsertComdat("comdat");
737 const SmallPtrSetImpl
<GlobalObject
*> &Users
= C
->getUsers();
738 EXPECT_TRUE(Users
.size() == 0);
740 Type
*Ty
= Type::getInt8Ty(Context
);
741 GlobalVariable
*GV1
= cast
<GlobalVariable
>(M
.getOrInsertGlobal("gv1", Ty
));
743 EXPECT_TRUE(Users
.size() == 1);
744 EXPECT_TRUE(Users
.contains(GV1
));
746 GlobalVariable
*GV2
= cast
<GlobalVariable
>(M
.getOrInsertGlobal("gv2", Ty
));
748 EXPECT_TRUE(Users
.size() == 2);
749 EXPECT_TRUE(Users
.contains(GV2
));
751 GV1
->eraseFromParent();
752 EXPECT_TRUE(Users
.size() == 1);
753 EXPECT_TRUE(Users
.contains(GV2
));
755 GV2
->eraseFromParent();
756 EXPECT_TRUE(Users
.size() == 0);
759 // Verify that the C API getters for BlockAddress work
760 TEST(ConstantsTest
, BlockAddressCAPITest
) {
761 const char *BlockAddressIR
= R
"(
762 define void @test_block_address_func() {
772 std::unique_ptr
<Module
> M
=
773 parseAssemblyString(BlockAddressIR
, Error
, Context
);
775 EXPECT_TRUE(M
.get() != nullptr);
778 auto *Func
= M
->getFunction("test_block_address_func");
779 EXPECT_TRUE(Func
!= nullptr);
781 // Get the second basic block, since we can't use the entry one
782 const BasicBlock
&BB
= *(++Func
->begin());
783 EXPECT_EQ(BB
.getName(), "block_bb_0");
785 // Construct the C API values
786 LLVMValueRef BlockAddr
= LLVMBlockAddress(wrap(Func
), wrap(&BB
));
787 EXPECT_TRUE(LLVMIsABlockAddress(BlockAddr
));
789 // Get the Function/BasicBlock values back out
790 auto *OutFunc
= unwrap(LLVMGetBlockAddressFunction(BlockAddr
));
791 auto *OutBB
= unwrap(LLVMGetBlockAddressBasicBlock(BlockAddr
));
793 // Verify that they round-tripped properly
794 EXPECT_EQ(Func
, OutFunc
);
795 EXPECT_EQ(&BB
, OutBB
);
798 } // end anonymous namespace
799 } // end namespace llvm