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(); \
175 EXPECT_EQ(std::string(" <badref> = " y), __s); \
178 TEST(ConstantsTest
, AsInstructionsTest
) {
180 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
182 Type
*Int64Ty
= Type::getInt64Ty(Context
);
183 Type
*Int32Ty
= Type::getInt32Ty(Context
);
184 Type
*Int16Ty
= Type::getInt16Ty(Context
);
187 M
->getOrInsertGlobal("dummy", PointerType::getUnqual(Int32Ty
));
189 M
->getOrInsertGlobal("dummy2", PointerType::getUnqual(Int32Ty
));
191 Constant
*P0
= ConstantExpr::getPtrToInt(Global
, Int32Ty
);
192 Constant
*P4
= ConstantExpr::getPtrToInt(Global2
, Int32Ty
);
193 Constant
*P6
= ConstantExpr::getBitCast(P4
, FixedVectorType::get(Int16Ty
, 2));
195 Constant
*One
= ConstantInt::get(Int32Ty
, 1);
196 Constant
*Two
= ConstantInt::get(Int64Ty
, 2);
197 Constant
*Big
= ConstantInt::get(Context
, APInt
{256, uint64_t(-1), true});
198 Constant
*Elt
= ConstantInt::get(Int16Ty
, 2015);
199 Constant
*Poison16
= PoisonValue::get(Int16Ty
);
200 Constant
*Undef64
= UndefValue::get(Int64Ty
);
201 Constant
*PoisonV16
= PoisonValue::get(P6
->getType());
203 #define P0STR "ptrtoint (ptr @dummy to i32)"
204 #define P3STR "ptrtoint (ptr @dummy to i1)"
205 #define P4STR "ptrtoint (ptr @dummy2 to i32)"
206 #define P6STR "bitcast (i32 ptrtoint (ptr @dummy2 to i32) to <2 x i16>)"
208 CHECK(ConstantExpr::getNeg(P0
), "sub i32 0, " P0STR
);
209 CHECK(ConstantExpr::getNot(P0
), "xor i32 " P0STR
", -1");
210 CHECK(ConstantExpr::getAdd(P0
, P0
), "add i32 " P0STR
", " P0STR
);
211 CHECK(ConstantExpr::getAdd(P0
, P0
, false, true),
212 "add nsw i32 " P0STR
", " P0STR
);
213 CHECK(ConstantExpr::getAdd(P0
, P0
, true, true),
214 "add nuw nsw i32 " P0STR
", " P0STR
);
215 CHECK(ConstantExpr::getSub(P0
, P0
), "sub i32 " P0STR
", " P0STR
);
216 CHECK(ConstantExpr::getMul(P0
, P0
), "mul i32 " P0STR
", " P0STR
);
217 CHECK(ConstantExpr::getXor(P0
, P0
), "xor i32 " P0STR
", " P0STR
);
219 std::vector
<Constant
*> V
;
221 // FIXME: getGetElementPtr() actually creates an inbounds ConstantGEP,
223 // CHECK(ConstantExpr::getGetElementPtr(Global, V, false),
224 // "getelementptr i32*, i32** @dummy, i32 1");
225 CHECK(ConstantExpr::getInBoundsGetElementPtr(PointerType::getUnqual(Int32Ty
),
227 "getelementptr inbounds ptr, ptr @dummy, i32 1");
229 CHECK(ConstantExpr::getExtractElement(P6
, One
),
230 "extractelement <2 x i16> " P6STR
", i32 1");
232 EXPECT_EQ(Poison16
, ConstantExpr::getExtractElement(P6
, Two
));
233 EXPECT_EQ(Poison16
, ConstantExpr::getExtractElement(P6
, Big
));
234 EXPECT_EQ(Poison16
, ConstantExpr::getExtractElement(P6
, Undef64
));
236 EXPECT_EQ(Elt
, ConstantExpr::getExtractElement(
237 ConstantExpr::getInsertElement(P6
, Elt
, One
), One
));
238 EXPECT_EQ(PoisonV16
, ConstantExpr::getInsertElement(P6
, Elt
, Two
));
239 EXPECT_EQ(PoisonV16
, ConstantExpr::getInsertElement(P6
, Elt
, Big
));
240 EXPECT_EQ(PoisonV16
, ConstantExpr::getInsertElement(P6
, Elt
, Undef64
));
243 #ifdef GTEST_HAS_DEATH_TEST
245 TEST(ConstantsTest
, ReplaceWithConstantTest
) {
247 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
249 Type
*Int32Ty
= Type::getInt32Ty(Context
);
250 Constant
*One
= ConstantInt::get(Int32Ty
, 1);
253 M
->getOrInsertGlobal("dummy", PointerType::getUnqual(Int32Ty
));
254 Constant
*GEP
= ConstantExpr::getGetElementPtr(
255 PointerType::getUnqual(Int32Ty
), Global
, One
);
256 EXPECT_DEATH(Global
->replaceAllUsesWith(GEP
),
257 "this->replaceAllUsesWith\\(expr\\(this\\)\\) is NOT valid!");
265 TEST(ConstantsTest
, ConstantArrayReplaceWithConstant
) {
267 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
269 Type
*IntTy
= Type::getInt8Ty(Context
);
270 ArrayType
*ArrayTy
= ArrayType::get(IntTy
, 2);
271 Constant
*A01Vals
[2] = {ConstantInt::get(IntTy
, 0),
272 ConstantInt::get(IntTy
, 1)};
273 Constant
*A01
= ConstantArray::get(ArrayTy
, A01Vals
);
275 Constant
*Global
= new GlobalVariable(*M
, IntTy
, false,
276 GlobalValue::ExternalLinkage
, nullptr);
277 Constant
*GlobalInt
= ConstantExpr::getPtrToInt(Global
, IntTy
);
278 Constant
*A0GVals
[2] = {ConstantInt::get(IntTy
, 0), GlobalInt
};
279 Constant
*A0G
= ConstantArray::get(ArrayTy
, A0GVals
);
282 GlobalVariable
*RefArray
=
283 new GlobalVariable(*M
, ArrayTy
, false, GlobalValue::ExternalLinkage
, A0G
);
284 ASSERT_EQ(A0G
, RefArray
->getInitializer());
286 GlobalInt
->replaceAllUsesWith(ConstantInt::get(IntTy
, 1));
287 ASSERT_EQ(A01
, RefArray
->getInitializer());
290 TEST(ConstantsTest
, ConstantExprReplaceWithConstant
) {
292 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
294 Type
*IntTy
= Type::getInt8Ty(Context
);
295 Constant
*G1
= new GlobalVariable(*M
, IntTy
, false,
296 GlobalValue::ExternalLinkage
, nullptr);
297 Constant
*G2
= new GlobalVariable(*M
, IntTy
, false,
298 GlobalValue::ExternalLinkage
, nullptr);
301 Constant
*Int1
= ConstantExpr::getPtrToInt(G1
, IntTy
);
302 Constant
*Int2
= ConstantExpr::getPtrToInt(G2
, IntTy
);
303 ASSERT_NE(Int1
, Int2
);
305 GlobalVariable
*Ref
=
306 new GlobalVariable(*M
, IntTy
, false, GlobalValue::ExternalLinkage
, Int1
);
307 ASSERT_EQ(Int1
, Ref
->getInitializer());
309 G1
->replaceAllUsesWith(G2
);
310 ASSERT_EQ(Int2
, Ref
->getInitializer());
313 TEST(ConstantsTest
, GEPReplaceWithConstant
) {
315 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
317 Type
*IntTy
= Type::getInt32Ty(Context
);
318 Type
*PtrTy
= PointerType::get(IntTy
, 0);
319 auto *C1
= ConstantInt::get(IntTy
, 1);
320 auto *Placeholder
= new GlobalVariable(
321 *M
, IntTy
, false, GlobalValue::ExternalWeakLinkage
, nullptr);
322 auto *GEP
= ConstantExpr::getGetElementPtr(IntTy
, Placeholder
, C1
);
323 ASSERT_EQ(GEP
->getOperand(0), Placeholder
);
326 new GlobalVariable(*M
, PtrTy
, false, GlobalValue::ExternalLinkage
, GEP
);
327 ASSERT_EQ(GEP
, Ref
->getInitializer());
329 auto *Global
= new GlobalVariable(*M
, IntTy
, false,
330 GlobalValue::ExternalLinkage
, nullptr);
331 auto *Alias
= GlobalAlias::create(IntTy
, 0, GlobalValue::ExternalLinkage
,
332 "alias", Global
, M
.get());
333 Placeholder
->replaceAllUsesWith(Alias
);
334 ASSERT_EQ(GEP
, Ref
->getInitializer());
335 ASSERT_EQ(GEP
->getOperand(0), Alias
);
338 TEST(ConstantsTest
, AliasCAPI
) {
341 std::unique_ptr
<Module
> M
=
342 parseAssemblyString("@g = global i32 42", Error
, Context
);
343 GlobalVariable
*G
= M
->getGlobalVariable("g");
344 Type
*I16Ty
= Type::getInt16Ty(Context
);
345 Type
*I16PTy
= PointerType::get(I16Ty
, 0);
346 Constant
*Aliasee
= ConstantExpr::getBitCast(G
, I16PTy
);
347 LLVMValueRef AliasRef
=
348 LLVMAddAlias2(wrap(M
.get()), wrap(I16Ty
), 0, wrap(Aliasee
), "a");
349 ASSERT_EQ(unwrap
<GlobalAlias
>(AliasRef
)->getAliasee(), Aliasee
);
352 static std::string
getNameOfType(Type
*T
) {
354 raw_string_ostream
RSOS(S
);
359 TEST(ConstantsTest
, BuildConstantDataArrays
) {
362 for (Type
*T
: {Type::getInt8Ty(Context
), Type::getInt16Ty(Context
),
363 Type::getInt32Ty(Context
), Type::getInt64Ty(Context
)}) {
364 ArrayType
*ArrayTy
= ArrayType::get(T
, 2);
365 Constant
*Vals
[] = {ConstantInt::get(T
, 0), ConstantInt::get(T
, 1)};
366 Constant
*CA
= ConstantArray::get(ArrayTy
, Vals
);
367 ASSERT_TRUE(isa
<ConstantDataArray
>(CA
)) << " T = " << getNameOfType(T
);
368 auto *CDA
= cast
<ConstantDataArray
>(CA
);
369 Constant
*CA2
= ConstantDataArray::getRaw(
370 CDA
->getRawDataValues(), CDA
->getNumElements(), CDA
->getElementType());
371 ASSERT_TRUE(CA
== CA2
) << " T = " << getNameOfType(T
);
374 for (Type
*T
: {Type::getHalfTy(Context
), Type::getBFloatTy(Context
),
375 Type::getFloatTy(Context
), Type::getDoubleTy(Context
)}) {
376 ArrayType
*ArrayTy
= ArrayType::get(T
, 2);
377 Constant
*Vals
[] = {ConstantFP::get(T
, 0), ConstantFP::get(T
, 1)};
378 Constant
*CA
= ConstantArray::get(ArrayTy
, Vals
);
379 ASSERT_TRUE(isa
<ConstantDataArray
>(CA
)) << " T = " << getNameOfType(T
);
380 auto *CDA
= cast
<ConstantDataArray
>(CA
);
381 Constant
*CA2
= ConstantDataArray::getRaw(
382 CDA
->getRawDataValues(), CDA
->getNumElements(), CDA
->getElementType());
383 ASSERT_TRUE(CA
== CA2
) << " T = " << getNameOfType(T
);
387 TEST(ConstantsTest
, BuildConstantDataVectors
) {
390 for (Type
*T
: {Type::getInt8Ty(Context
), Type::getInt16Ty(Context
),
391 Type::getInt32Ty(Context
), Type::getInt64Ty(Context
)}) {
392 Constant
*Vals
[] = {ConstantInt::get(T
, 0), ConstantInt::get(T
, 1)};
393 Constant
*CV
= ConstantVector::get(Vals
);
394 ASSERT_TRUE(isa
<ConstantDataVector
>(CV
)) << " T = " << getNameOfType(T
);
395 auto *CDV
= cast
<ConstantDataVector
>(CV
);
396 Constant
*CV2
= ConstantDataVector::getRaw(
397 CDV
->getRawDataValues(), CDV
->getNumElements(), CDV
->getElementType());
398 ASSERT_TRUE(CV
== CV2
) << " T = " << getNameOfType(T
);
401 for (Type
*T
: {Type::getHalfTy(Context
), Type::getBFloatTy(Context
),
402 Type::getFloatTy(Context
), Type::getDoubleTy(Context
)}) {
403 Constant
*Vals
[] = {ConstantFP::get(T
, 0), ConstantFP::get(T
, 1)};
404 Constant
*CV
= ConstantVector::get(Vals
);
405 ASSERT_TRUE(isa
<ConstantDataVector
>(CV
)) << " T = " << getNameOfType(T
);
406 auto *CDV
= cast
<ConstantDataVector
>(CV
);
407 Constant
*CV2
= ConstantDataVector::getRaw(
408 CDV
->getRawDataValues(), CDV
->getNumElements(), CDV
->getElementType());
409 ASSERT_TRUE(CV
== CV2
) << " T = " << getNameOfType(T
);
413 TEST(ConstantsTest
, BitcastToGEP
) {
415 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
417 auto *i32
= Type::getInt32Ty(Context
);
418 auto *U
= StructType::create(Context
, "Unsized");
419 Type
*EltTys
[] = {i32
, U
};
420 auto *S
= StructType::create(EltTys
);
423 new GlobalVariable(*M
, S
, false, GlobalValue::ExternalLinkage
, nullptr);
424 auto *PtrTy
= PointerType::get(i32
, 0);
425 auto *C
= ConstantExpr::getBitCast(G
, PtrTy
);
426 /* With opaque pointers, no cast is necessary. */
430 bool foldFuncPtrAndConstToNull(LLVMContext
&Context
, Module
*TheModule
,
432 MaybeAlign FunctionAlign
= std::nullopt
) {
433 Type
*VoidType(Type::getVoidTy(Context
));
434 FunctionType
*FuncType(FunctionType::get(VoidType
, false));
436 Function::Create(FuncType
, GlobalValue::ExternalLinkage
, "", TheModule
));
439 Func
->setAlignment(*FunctionAlign
);
441 IntegerType
*ConstantIntType(Type::getInt32Ty(Context
));
442 ConstantInt
*TheConstant(ConstantInt::get(ConstantIntType
, AndValue
));
444 Constant
*TheConstantExpr(ConstantExpr::getPtrToInt(Func
, ConstantIntType
));
446 Constant
*C
= ConstantFoldBinaryInstruction(Instruction::And
, TheConstantExpr
,
448 bool Result
= C
&& C
->isNullValue();
451 // If the Module exists then it will delete the Function.
458 TEST(ConstantsTest
, FoldFunctionPtrAlignUnknownAnd2
) {
460 Module
TheModule("TestModule", Context
);
461 // When the DataLayout doesn't specify a function pointer alignment we
462 // assume in this case that it is 4 byte aligned. This is a bug but we can't
463 // fix it directly because it causes a code size regression on X86.
464 // FIXME: This test should be changed once existing targets have
465 // appropriate defaults. See associated FIXME in ConstantFoldBinaryInstruction
466 ASSERT_TRUE(foldFuncPtrAndConstToNull(Context
, &TheModule
, 2));
469 TEST(ConstantsTest
, DontFoldFunctionPtrAlignUnknownAnd4
) {
471 Module
TheModule("TestModule", Context
);
472 ASSERT_FALSE(foldFuncPtrAndConstToNull(Context
, &TheModule
, 4));
475 TEST(ConstantsTest
, FoldFunctionPtrAlign4
) {
477 Module
TheModule("TestModule", Context
);
478 const char *AlignmentStrings
[] = {"Fi32", "Fn32"};
480 for (unsigned AndValue
= 1; AndValue
<= 2; ++AndValue
) {
481 for (const char *AlignmentString
: AlignmentStrings
) {
482 TheModule
.setDataLayout(AlignmentString
);
483 ASSERT_TRUE(foldFuncPtrAndConstToNull(Context
, &TheModule
, AndValue
));
488 TEST(ConstantsTest
, DontFoldFunctionPtrAlign1
) {
490 Module
TheModule("TestModule", Context
);
491 const char *AlignmentStrings
[] = {"Fi8", "Fn8"};
493 for (const char *AlignmentString
: AlignmentStrings
) {
494 TheModule
.setDataLayout(AlignmentString
);
495 ASSERT_FALSE(foldFuncPtrAndConstToNull(Context
, &TheModule
, 2));
499 TEST(ConstantsTest
, FoldFunctionAlign4PtrAlignMultiple
) {
501 Module
TheModule("TestModule", Context
);
502 TheModule
.setDataLayout("Fn8");
503 ASSERT_TRUE(foldFuncPtrAndConstToNull(Context
, &TheModule
, 2, Align(4)));
506 TEST(ConstantsTest
, DontFoldFunctionAlign4PtrAlignIndependent
) {
508 Module
TheModule("TestModule", Context
);
509 TheModule
.setDataLayout("Fi8");
510 ASSERT_FALSE(foldFuncPtrAndConstToNull(Context
, &TheModule
, 2, Align(4)));
513 TEST(ConstantsTest
, DontFoldFunctionPtrIfNoModule
) {
515 // Even though the function is explicitly 4 byte aligned, in the absence of a
516 // DataLayout we can't assume that the function pointer is aligned.
517 ASSERT_FALSE(foldFuncPtrAndConstToNull(Context
, nullptr, 2, Align(4)));
520 TEST(ConstantsTest
, FoldGlobalVariablePtr
) {
523 IntegerType
*IntType(Type::getInt32Ty(Context
));
525 std::unique_ptr
<GlobalVariable
> Global(
526 new GlobalVariable(IntType
, true, GlobalValue::ExternalLinkage
));
528 Global
->setAlignment(Align(4));
530 ConstantInt
*TheConstant(ConstantInt::get(IntType
, 2));
532 Constant
*TheConstantExpr(ConstantExpr::getPtrToInt(Global
.get(), IntType
));
534 ASSERT_TRUE(ConstantFoldBinaryInstruction(Instruction::And
, TheConstantExpr
,
539 // Check that containsUndefOrPoisonElement and containsPoisonElement is working
542 TEST(ConstantsTest
, containsUndefElemTest
) {
545 Type
*Int32Ty
= Type::getInt32Ty(Context
);
546 Constant
*CU
= UndefValue::get(Int32Ty
);
547 Constant
*CP
= PoisonValue::get(Int32Ty
);
548 Constant
*C1
= ConstantInt::get(Int32Ty
, 1);
549 Constant
*C2
= ConstantInt::get(Int32Ty
, 2);
552 Constant
*V1
= ConstantVector::get({C1
, C2
});
553 EXPECT_FALSE(V1
->containsUndefOrPoisonElement());
554 EXPECT_FALSE(V1
->containsPoisonElement());
558 Constant
*V2
= ConstantVector::get({C1
, CU
});
559 EXPECT_TRUE(V2
->containsUndefOrPoisonElement());
560 EXPECT_FALSE(V2
->containsPoisonElement());
564 Constant
*V3
= ConstantVector::get({C1
, CP
});
565 EXPECT_TRUE(V3
->containsUndefOrPoisonElement());
566 EXPECT_TRUE(V3
->containsPoisonElement());
570 Constant
*V4
= ConstantVector::get({CU
, CP
});
571 EXPECT_TRUE(V4
->containsUndefOrPoisonElement());
572 EXPECT_TRUE(V4
->containsPoisonElement());
576 // Check that poison elements in vector constants are matched
577 // correctly for both integer and floating-point types. Just don't
578 // crash on vectors of pointers (could be handled?).
580 TEST(ConstantsTest
, isElementWiseEqual
) {
583 Type
*Int32Ty
= Type::getInt32Ty(Context
);
584 Constant
*CU
= UndefValue::get(Int32Ty
);
585 Constant
*CP
= PoisonValue::get(Int32Ty
);
586 Constant
*C1
= ConstantInt::get(Int32Ty
, 1);
587 Constant
*C2
= ConstantInt::get(Int32Ty
, 2);
589 Constant
*C1211
= ConstantVector::get({C1
, C2
, C1
, C1
});
590 Constant
*C12U1
= ConstantVector::get({C1
, C2
, CU
, C1
});
591 Constant
*C12U2
= ConstantVector::get({C1
, C2
, CU
, C2
});
592 Constant
*C12U21
= ConstantVector::get({C1
, C2
, CU
, C2
, C1
});
593 Constant
*C12P1
= ConstantVector::get({C1
, C2
, CP
, C1
});
594 Constant
*C12P2
= ConstantVector::get({C1
, C2
, CP
, C2
});
595 Constant
*C12P21
= ConstantVector::get({C1
, C2
, CP
, C2
, C1
});
597 EXPECT_FALSE(C1211
->isElementWiseEqual(C12U1
));
598 EXPECT_FALSE(C12U1
->isElementWiseEqual(C1211
));
599 EXPECT_FALSE(C12U2
->isElementWiseEqual(C12U1
));
600 EXPECT_FALSE(C12U1
->isElementWiseEqual(C12U2
));
601 EXPECT_FALSE(C12U21
->isElementWiseEqual(C12U2
));
603 EXPECT_TRUE(C1211
->isElementWiseEqual(C12P1
));
604 EXPECT_TRUE(C12P1
->isElementWiseEqual(C1211
));
605 EXPECT_FALSE(C12P2
->isElementWiseEqual(C12P1
));
606 EXPECT_FALSE(C12P1
->isElementWiseEqual(C12P2
));
607 EXPECT_FALSE(C12P21
->isElementWiseEqual(C12P2
));
609 Type
*FltTy
= Type::getFloatTy(Context
);
610 Constant
*CFU
= UndefValue::get(FltTy
);
611 Constant
*CFP
= PoisonValue::get(FltTy
);
612 Constant
*CF1
= ConstantFP::get(FltTy
, 1.0);
613 Constant
*CF2
= ConstantFP::get(FltTy
, 2.0);
615 Constant
*CF1211
= ConstantVector::get({CF1
, CF2
, CF1
, CF1
});
616 Constant
*CF12U1
= ConstantVector::get({CF1
, CF2
, CFU
, CF1
});
617 Constant
*CF12U2
= ConstantVector::get({CF1
, CF2
, CFU
, CF2
});
618 Constant
*CFUU1U
= ConstantVector::get({CFU
, CFU
, CF1
, CFU
});
619 Constant
*CF12P1
= ConstantVector::get({CF1
, CF2
, CFP
, CF1
});
620 Constant
*CF12P2
= ConstantVector::get({CF1
, CF2
, CFP
, CF2
});
621 Constant
*CFPP1P
= ConstantVector::get({CFP
, CFP
, CF1
, CFP
});
623 EXPECT_FALSE(CF1211
->isElementWiseEqual(CF12U1
));
624 EXPECT_FALSE(CF12U1
->isElementWiseEqual(CF1211
));
625 EXPECT_FALSE(CFUU1U
->isElementWiseEqual(CF12U1
));
626 EXPECT_FALSE(CF12U2
->isElementWiseEqual(CF12U1
));
627 EXPECT_FALSE(CF12U1
->isElementWiseEqual(CF12U2
));
629 EXPECT_TRUE(CF1211
->isElementWiseEqual(CF12P1
));
630 EXPECT_TRUE(CF12P1
->isElementWiseEqual(CF1211
));
631 EXPECT_TRUE(CFPP1P
->isElementWiseEqual(CF12P1
));
632 EXPECT_FALSE(CF12P2
->isElementWiseEqual(CF12P1
));
633 EXPECT_FALSE(CF12P1
->isElementWiseEqual(CF12P2
));
635 PointerType
*PtrTy
= PointerType::get(Context
, 0);
636 Constant
*CPU
= UndefValue::get(PtrTy
);
637 Constant
*CPP
= PoisonValue::get(PtrTy
);
638 Constant
*CP0
= ConstantPointerNull::get(PtrTy
);
640 Constant
*CP0000
= ConstantVector::get({CP0
, CP0
, CP0
, CP0
});
641 Constant
*CP00U0
= ConstantVector::get({CP0
, CP0
, CPU
, CP0
});
642 Constant
*CP00U
= ConstantVector::get({CP0
, CP0
, CPU
});
643 Constant
*CP00P0
= ConstantVector::get({CP0
, CP0
, CPP
, CP0
});
644 Constant
*CP00P
= ConstantVector::get({CP0
, CP0
, CPP
});
646 EXPECT_FALSE(CP0000
->isElementWiseEqual(CP00U0
));
647 EXPECT_FALSE(CP00U0
->isElementWiseEqual(CP0000
));
648 EXPECT_FALSE(CP0000
->isElementWiseEqual(CP00U
));
649 EXPECT_FALSE(CP00U
->isElementWiseEqual(CP00U0
));
650 EXPECT_FALSE(CP0000
->isElementWiseEqual(CP00P0
));
651 EXPECT_FALSE(CP00P0
->isElementWiseEqual(CP0000
));
652 EXPECT_FALSE(CP0000
->isElementWiseEqual(CP00P
));
653 EXPECT_FALSE(CP00P
->isElementWiseEqual(CP00P0
));
656 // Check that vector/aggregate constants correctly store undef and poison
659 TEST(ConstantsTest
, CheckElementWiseUndefPoison
) {
662 Type
*Int32Ty
= Type::getInt32Ty(Context
);
663 StructType
*STy
= StructType::get(Int32Ty
, Int32Ty
);
664 ArrayType
*ATy
= ArrayType::get(Int32Ty
, 2);
665 Constant
*CU
= UndefValue::get(Int32Ty
);
666 Constant
*CP
= PoisonValue::get(Int32Ty
);
669 Constant
*CUU
= ConstantVector::get({CU
, CU
});
670 Constant
*CPP
= ConstantVector::get({CP
, CP
});
671 Constant
*CUP
= ConstantVector::get({CU
, CP
});
672 Constant
*CPU
= ConstantVector::get({CP
, CU
});
673 EXPECT_EQ(CUU
, UndefValue::get(CUU
->getType()));
674 EXPECT_EQ(CPP
, PoisonValue::get(CPP
->getType()));
675 EXPECT_NE(CUP
, UndefValue::get(CUP
->getType()));
676 EXPECT_NE(CPU
, UndefValue::get(CPU
->getType()));
680 Constant
*CUU
= ConstantStruct::get(STy
, {CU
, CU
});
681 Constant
*CPP
= ConstantStruct::get(STy
, {CP
, CP
});
682 Constant
*CUP
= ConstantStruct::get(STy
, {CU
, CP
});
683 Constant
*CPU
= ConstantStruct::get(STy
, {CP
, CU
});
684 EXPECT_EQ(CUU
, UndefValue::get(CUU
->getType()));
685 EXPECT_EQ(CPP
, PoisonValue::get(CPP
->getType()));
686 EXPECT_NE(CUP
, UndefValue::get(CUP
->getType()));
687 EXPECT_NE(CPU
, UndefValue::get(CPU
->getType()));
691 Constant
*CUU
= ConstantArray::get(ATy
, {CU
, CU
});
692 Constant
*CPP
= ConstantArray::get(ATy
, {CP
, CP
});
693 Constant
*CUP
= ConstantArray::get(ATy
, {CU
, CP
});
694 Constant
*CPU
= ConstantArray::get(ATy
, {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 TEST(ConstantsTest
, GetSplatValueRoundTrip
) {
705 Type
*FloatTy
= Type::getFloatTy(Context
);
706 Type
*Int32Ty
= Type::getInt32Ty(Context
);
707 Type
*Int8Ty
= Type::getInt8Ty(Context
);
709 for (unsigned Min
: {1, 2, 8}) {
710 auto ScalableEC
= ElementCount::getScalable(Min
);
711 auto FixedEC
= ElementCount::getFixed(Min
);
713 for (auto EC
: {ScalableEC
, FixedEC
}) {
714 for (auto *Ty
: {FloatTy
, Int32Ty
, Int8Ty
}) {
715 Constant
*Zero
= Constant::getNullValue(Ty
);
716 Constant
*One
= Constant::getAllOnesValue(Ty
);
718 for (auto *C
: {Zero
, One
}) {
719 Constant
*Splat
= ConstantVector::getSplat(EC
, C
);
720 ASSERT_NE(nullptr, Splat
);
722 Constant
*SplatVal
= Splat
->getSplatValue();
723 EXPECT_NE(nullptr, SplatVal
);
724 EXPECT_EQ(SplatVal
, C
);
731 TEST(ConstantsTest
, ComdatUserTracking
) {
733 Module
M("MyModule", Context
);
735 Comdat
*C
= M
.getOrInsertComdat("comdat");
736 const SmallPtrSetImpl
<GlobalObject
*> &Users
= C
->getUsers();
737 EXPECT_TRUE(Users
.size() == 0);
739 Type
*Ty
= Type::getInt8Ty(Context
);
740 GlobalVariable
*GV1
= cast
<GlobalVariable
>(M
.getOrInsertGlobal("gv1", Ty
));
742 EXPECT_TRUE(Users
.size() == 1);
743 EXPECT_TRUE(Users
.contains(GV1
));
745 GlobalVariable
*GV2
= cast
<GlobalVariable
>(M
.getOrInsertGlobal("gv2", Ty
));
747 EXPECT_TRUE(Users
.size() == 2);
748 EXPECT_TRUE(Users
.contains(GV2
));
750 GV1
->eraseFromParent();
751 EXPECT_TRUE(Users
.size() == 1);
752 EXPECT_TRUE(Users
.contains(GV2
));
754 GV2
->eraseFromParent();
755 EXPECT_TRUE(Users
.size() == 0);
758 // Verify that the C API getters for BlockAddress work
759 TEST(ConstantsTest
, BlockAddressCAPITest
) {
760 const char *BlockAddressIR
= R
"(
761 define void @test_block_address_func() {
771 std::unique_ptr
<Module
> M
=
772 parseAssemblyString(BlockAddressIR
, Error
, Context
);
774 EXPECT_TRUE(M
.get() != nullptr);
777 auto *Func
= M
->getFunction("test_block_address_func");
778 EXPECT_TRUE(Func
!= nullptr);
780 // Get the second basic block, since we can't use the entry one
781 const BasicBlock
&BB
= *(++Func
->begin());
782 EXPECT_EQ(BB
.getName(), "block_bb_0");
784 // Construct the C API values
785 LLVMValueRef BlockAddr
= LLVMBlockAddress(wrap(Func
), wrap(&BB
));
786 EXPECT_TRUE(LLVMIsABlockAddress(BlockAddr
));
788 // Get the Function/BasicBlock values back out
789 auto *OutFunc
= unwrap(LLVMGetBlockAddressFunction(BlockAddr
));
790 auto *OutBB
= unwrap(LLVMGetBlockAddressBasicBlock(BlockAddr
));
792 // Verify that they round-tripped properly
793 EXPECT_EQ(Func
, OutFunc
);
794 EXPECT_EQ(&BB
, OutBB
);
797 } // end anonymous namespace
798 } // end namespace llvm