xref: /llvm-project/llvm/unittests/IR/ConstantsTest.cpp (revision b6a01caa64aaac2e5db8d7953a81cbe1a139b81f)
1 //===- llvm/unittest/IR/ConstantsTest.cpp - Constants unit tests ----------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
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"
20 
21 namespace llvm {
22 namespace {
23 
24 TEST(ConstantsTest, Integer_i1) {
25   LLVMContext Context;
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);
32 
33   // Input:  @b = constant i1 add(i1 1 , i1 1)
34   // Output: @b = constant i1 false
35   EXPECT_EQ(Zero, ConstantExpr::getAdd(One, One));
36 
37   // @c = constant i1 add(i1 -1, i1 1)
38   // @c = constant i1 false
39   EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, One));
40 
41   // @d = constant i1 add(i1 -1, i1 -1)
42   // @d = constant i1 false
43   EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, NegOne));
44 
45   // @e = constant i1 sub(i1 -1, i1 1)
46   // @e = constant i1 false
47   EXPECT_EQ(Zero, ConstantExpr::getSub(NegOne, One));
48 
49   // @f = constant i1 sub(i1 1 , i1 -1)
50   // @f = constant i1 false
51   EXPECT_EQ(Zero, ConstantExpr::getSub(One, NegOne));
52 
53   // @g = constant i1 sub(i1 1 , i1 1)
54   // @g = constant i1 false
55   EXPECT_EQ(Zero, ConstantExpr::getSub(One, One));
56 
57   // @h = constant i1 shl(i1 1 , i1 1)  ; poison
58   // @h = constant i1 poison
59   EXPECT_EQ(Poison, ConstantExpr::getShl(One, One));
60 
61   // @i = constant i1 shl(i1 1 , i1 0)
62   // @i = constant i1 true
63   EXPECT_EQ(One, ConstantExpr::getShl(One, Zero));
64 
65   // @j = constant i1 lshr(i1 1, i1 1)  ; poison
66   // @j = constant i1 poison
67   EXPECT_EQ(Poison, ConstantExpr::getLShr(One, One));
68 
69   // @m = constant i1 ashr(i1 1, i1 1)  ; poison
70   // @m = constant i1 poison
71   EXPECT_EQ(Poison, ConstantExpr::getAShr(One, One));
72 
73   // @n = constant i1 mul(i1 -1, i1 1)
74   // @n = constant i1 true
75   EXPECT_EQ(One, ConstantExpr::getMul(NegOne, One));
76 
77   // @o = constant i1 sdiv(i1 -1, i1 1) ; overflow
78   // @o = constant i1 true
79   EXPECT_EQ(One, ConstantFoldBinaryInstruction(Instruction::SDiv, NegOne, One));
80 
81   // @p = constant i1 sdiv(i1 1 , i1 -1); overflow
82   // @p = constant i1 true
83   EXPECT_EQ(One, ConstantFoldBinaryInstruction(Instruction::SDiv, One, NegOne));
84 
85   // @q = constant i1 udiv(i1 -1, i1 1)
86   // @q = constant i1 true
87   EXPECT_EQ(One, ConstantFoldBinaryInstruction(Instruction::UDiv, NegOne, One));
88 
89   // @r = constant i1 udiv(i1 1, i1 -1)
90   // @r = constant i1 true
91   EXPECT_EQ(One, ConstantFoldBinaryInstruction(Instruction::UDiv, One, NegOne));
92 
93   // @s = constant i1 srem(i1 -1, i1 1) ; overflow
94   // @s = constant i1 false
95   EXPECT_EQ(Zero,
96             ConstantFoldBinaryInstruction(Instruction::SRem, NegOne, One));
97 
98   // @u = constant i1 srem(i1  1, i1 -1) ; overflow
99   // @u = constant i1 false
100   EXPECT_EQ(Zero,
101             ConstantFoldBinaryInstruction(Instruction::SRem, One, NegOne));
102 }
103 
104 TEST(ConstantsTest, IntSigns) {
105   LLVMContext Context;
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());
113 
114   // Overflow is handled by truncation.
115   EXPECT_EQ(0x3b, ConstantInt::get(Int8Ty, 0x13b)->getSExtValue());
116 }
117 
118 TEST(ConstantsTest, FP128Test) {
119   LLVMContext Context;
120   Type *FP128Ty = Type::getFP128Ty(Context);
121 
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));
126 }
127 
128 TEST(ConstantsTest, PointerCast) {
129   LLVMContext C;
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);
139 
140   // ptrtoint i8* to i64
141   EXPECT_EQ(
142       Constant::getNullValue(Int64Ty),
143       ConstantExpr::getPointerCast(Constant::getNullValue(Int8PtrTy), Int64Ty));
144 
145   // bitcast i8* to i32*
146   EXPECT_EQ(Constant::getNullValue(Int32PtrTy),
147             ConstantExpr::getPointerCast(Constant::getNullValue(Int8PtrTy),
148                                          Int32PtrTy));
149 
150   // ptrtoint <4 x i8*> to <4 x i64>
151   EXPECT_EQ(Constant::getNullValue(Int64VecTy),
152             ConstantExpr::getPointerCast(Constant::getNullValue(Int8PtrVecTy),
153                                          Int64VecTy));
154 
155   // ptrtoint <vscale x 4 x i8*> to <vscale x 4 x i64>
156   EXPECT_EQ(
157       Constant::getNullValue(Int64ScalableVecTy),
158       ConstantExpr::getPointerCast(Constant::getNullValue(Int8PtrScalableVecTy),
159                                    Int64ScalableVecTy));
160 
161   // bitcast <4 x i8*> to <4 x i32*>
162   EXPECT_EQ(Constant::getNullValue(Int32PtrVecTy),
163             ConstantExpr::getPointerCast(Constant::getNullValue(Int8PtrVecTy),
164                                          Int32PtrVecTy));
165 
166   // bitcast <vscale x 4 x i8*> to <vscale x 4 x i32*>
167   EXPECT_EQ(
168       Constant::getNullValue(Int32PtrScalableVecTy),
169       ConstantExpr::getPointerCast(Constant::getNullValue(Int8PtrScalableVecTy),
170                                    Int32PtrScalableVecTy));
171 
172   Type *Int32Ptr1Ty = Type::getInt32PtrTy(C, 1);
173   ConstantInt *K = ConstantInt::get(Type::getInt64Ty(C), 1234);
174 
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));
180 
181   Constant *NullInt32Ptr0 = Constant::getNullValue(Int32PtrTy);
182   Constant *NullInt32Ptr1 = Constant::getNullValue(Int32Ptr1Ty);
183 
184   // Make sure that addrspacecast of null is not folded away.
185   EXPECT_NE(Constant::getNullValue(Int32PtrTy),
186             ConstantExpr::getAddrSpaceCast(NullInt32Ptr0, Int32Ptr1Ty));
187 
188   EXPECT_NE(Constant::getNullValue(Int32Ptr1Ty),
189             ConstantExpr::getAddrSpaceCast(NullInt32Ptr1, Int32PtrTy));
190 }
191 
192 #define CHECK(x, y)                                                            \
193   {                                                                            \
194     std::string __s;                                                           \
195     raw_string_ostream __o(__s);                                               \
196     Instruction *__I = cast<ConstantExpr>(x)->getAsInstruction();              \
197     __I->print(__o);                                                           \
198     __I->deleteValue();                                                        \
199     __o.flush();                                                               \
200     EXPECT_EQ(std::string("  <badref> = " y), __s);                            \
201   }
202 
203 TEST(ConstantsTest, AsInstructionsTest) {
204   LLVMContext Context;
205   std::unique_ptr<Module> M(new Module("MyModule", Context));
206 
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);
213 
214   Constant *Global =
215       M->getOrInsertGlobal("dummy", PointerType::getUnqual(Int32Ty));
216   Constant *Global2 =
217       M->getOrInsertGlobal("dummy2", PointerType::getUnqual(Int32Ty));
218 
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));
226 
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());
234 
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>)"
242 
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);
265 
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");
272 
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);
279 
280   std::vector<Constant *> V;
281   V.push_back(One);
282   // FIXME: getGetElementPtr() actually creates an inbounds ConstantGEP,
283   //        not a normal one!
284   // CHECK(ConstantExpr::getGetElementPtr(Global, V, false),
285   //      "getelementptr i32*, i32** @dummy, i32 1");
286   CHECK(ConstantExpr::getInBoundsGetElementPtr(PointerType::getUnqual(Int32Ty),
287                                                Global, V),
288         "getelementptr inbounds ptr, ptr @dummy, i32 1");
289 
290   CHECK(ConstantExpr::getExtractElement(P6, One),
291         "extractelement <2 x i16> " P6STR ", i32 1");
292 
293   EXPECT_EQ(Poison16, ConstantExpr::getExtractElement(P6, Two));
294   EXPECT_EQ(Poison16, ConstantExpr::getExtractElement(P6, Big));
295   EXPECT_EQ(Poison16, ConstantExpr::getExtractElement(P6, Undef64));
296 
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));
302 }
303 
304 #ifdef GTEST_HAS_DEATH_TEST
305 #ifndef NDEBUG
306 TEST(ConstantsTest, ReplaceWithConstantTest) {
307   LLVMContext Context;
308   std::unique_ptr<Module> M(new Module("MyModule", Context));
309 
310   Type *Int32Ty = Type::getInt32Ty(Context);
311   Constant *One = ConstantInt::get(Int32Ty, 1);
312 
313   Constant *Global =
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!");
319 }
320 
321 #endif
322 #endif
323 
324 #undef CHECK
325 
326 TEST(ConstantsTest, ConstantArrayReplaceWithConstant) {
327   LLVMContext Context;
328   std::unique_ptr<Module> M(new Module("MyModule", Context));
329 
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);
335 
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);
341   ASSERT_NE(A01, A0G);
342 
343   GlobalVariable *RefArray =
344       new GlobalVariable(*M, ArrayTy, false, GlobalValue::ExternalLinkage, A0G);
345   ASSERT_EQ(A0G, RefArray->getInitializer());
346 
347   GlobalInt->replaceAllUsesWith(ConstantInt::get(IntTy, 1));
348   ASSERT_EQ(A01, RefArray->getInitializer());
349 }
350 
351 TEST(ConstantsTest, ConstantExprReplaceWithConstant) {
352   LLVMContext Context;
353   std::unique_ptr<Module> M(new Module("MyModule", Context));
354 
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);
360   ASSERT_NE(G1, G2);
361 
362   Constant *Int1 = ConstantExpr::getPtrToInt(G1, IntTy);
363   Constant *Int2 = ConstantExpr::getPtrToInt(G2, IntTy);
364   ASSERT_NE(Int1, Int2);
365 
366   GlobalVariable *Ref =
367       new GlobalVariable(*M, IntTy, false, GlobalValue::ExternalLinkage, Int1);
368   ASSERT_EQ(Int1, Ref->getInitializer());
369 
370   G1->replaceAllUsesWith(G2);
371   ASSERT_EQ(Int2, Ref->getInitializer());
372 }
373 
374 TEST(ConstantsTest, GEPReplaceWithConstant) {
375   LLVMContext Context;
376   std::unique_ptr<Module> M(new Module("MyModule", Context));
377 
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);
385 
386   auto *Ref =
387       new GlobalVariable(*M, PtrTy, false, GlobalValue::ExternalLinkage, GEP);
388   ASSERT_EQ(GEP, Ref->getInitializer());
389 
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);
397 }
398 
399 TEST(ConstantsTest, AliasCAPI) {
400   LLVMContext Context;
401   SMDiagnostic Error;
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);
411 }
412 
413 static std::string getNameOfType(Type *T) {
414   std::string S;
415   raw_string_ostream RSOS(S);
416   T->print(RSOS);
417   return S;
418 }
419 
420 TEST(ConstantsTest, BuildConstantDataArrays) {
421   LLVMContext Context;
422 
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);
433   }
434 
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);
445   }
446 }
447 
448 TEST(ConstantsTest, BuildConstantDataVectors) {
449   LLVMContext Context;
450 
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);
460   }
461 
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);
471   }
472 }
473 
474 void bitcastToGEPHelper(bool useOpaquePointers) {
475   LLVMContext Context;
476   Context.setOpaquePointers(useOpaquePointers);
477   std::unique_ptr<Module> M(new Module("MyModule", Context));
478 
479   auto *i32 = Type::getInt32Ty(Context);
480   auto *U = StructType::create(Context, "Unsized");
481   Type *EltTys[] = {i32, U};
482   auto *S = StructType::create(EltTys);
483 
484   auto *G =
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);
490   } else {
491     /* With opaque pointers, no cast is necessary. */
492     EXPECT_EQ(C, G);
493   }
494 }
495 
496 TEST(ConstantsTest, BitcastToGEP) {
497   bitcastToGEPHelper(true);
498   bitcastToGEPHelper(false);
499 }
500 
501 bool foldFuncPtrAndConstToNull(LLVMContext &Context, Module *TheModule,
502                                uint64_t AndValue,
503                                MaybeAlign FunctionAlign = std::nullopt) {
504   Type *VoidType(Type::getVoidTy(Context));
505   FunctionType *FuncType(FunctionType::get(VoidType, false));
506   Function *Func(
507       Function::Create(FuncType, GlobalValue::ExternalLinkage, "", TheModule));
508 
509   if (FunctionAlign)
510     Func->setAlignment(*FunctionAlign);
511 
512   IntegerType *ConstantIntType(Type::getInt32Ty(Context));
513   ConstantInt *TheConstant(ConstantInt::get(ConstantIntType, AndValue));
514 
515   Constant *TheConstantExpr(ConstantExpr::getPtrToInt(Func, ConstantIntType));
516 
517   bool Result =
518       ConstantExpr::get(Instruction::And, TheConstantExpr, TheConstant)
519           ->isNullValue();
520 
521   if (!TheModule) {
522     // If the Module exists then it will delete the Function.
523     delete Func;
524   }
525 
526   return Result;
527 }
528 
529 TEST(ConstantsTest, FoldFunctionPtrAlignUnknownAnd2) {
530   LLVMContext Context;
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));
538 }
539 
540 TEST(ConstantsTest, DontFoldFunctionPtrAlignUnknownAnd4) {
541   LLVMContext Context;
542   Module TheModule("TestModule", Context);
543   ASSERT_FALSE(foldFuncPtrAndConstToNull(Context, &TheModule, 4));
544 }
545 
546 TEST(ConstantsTest, FoldFunctionPtrAlign4) {
547   LLVMContext Context;
548   Module TheModule("TestModule", Context);
549   const char *AlignmentStrings[] = {"Fi32", "Fn32"};
550 
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));
555     }
556   }
557 }
558 
559 TEST(ConstantsTest, DontFoldFunctionPtrAlign1) {
560   LLVMContext Context;
561   Module TheModule("TestModule", Context);
562   const char *AlignmentStrings[] = {"Fi8", "Fn8"};
563 
564   for (const char *AlignmentString : AlignmentStrings) {
565     TheModule.setDataLayout(AlignmentString);
566     ASSERT_FALSE(foldFuncPtrAndConstToNull(Context, &TheModule, 2));
567   }
568 }
569 
570 TEST(ConstantsTest, FoldFunctionAlign4PtrAlignMultiple) {
571   LLVMContext Context;
572   Module TheModule("TestModule", Context);
573   TheModule.setDataLayout("Fn8");
574   ASSERT_TRUE(foldFuncPtrAndConstToNull(Context, &TheModule, 2, Align(4)));
575 }
576 
577 TEST(ConstantsTest, DontFoldFunctionAlign4PtrAlignIndependent) {
578   LLVMContext Context;
579   Module TheModule("TestModule", Context);
580   TheModule.setDataLayout("Fi8");
581   ASSERT_FALSE(foldFuncPtrAndConstToNull(Context, &TheModule, 2, Align(4)));
582 }
583 
584 TEST(ConstantsTest, DontFoldFunctionPtrIfNoModule) {
585   LLVMContext Context;
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)));
589 }
590 
591 TEST(ConstantsTest, FoldGlobalVariablePtr) {
592   LLVMContext Context;
593 
594   IntegerType *IntType(Type::getInt32Ty(Context));
595 
596   std::unique_ptr<GlobalVariable> Global(
597       new GlobalVariable(IntType, true, GlobalValue::ExternalLinkage));
598 
599   Global->setAlignment(Align(4));
600 
601   ConstantInt *TheConstant(ConstantInt::get(IntType, 2));
602 
603   Constant *TheConstantExpr(ConstantExpr::getPtrToInt(Global.get(), IntType));
604 
605   ASSERT_TRUE(ConstantExpr::get(Instruction::And, TheConstantExpr, TheConstant)
606                   ->isNullValue());
607 }
608 
609 // Check that containsUndefOrPoisonElement and containsPoisonElement is working
610 // great
611 
612 TEST(ConstantsTest, containsUndefElemTest) {
613   LLVMContext Context;
614 
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);
620 
621   {
622     Constant *V1 = ConstantVector::get({C1, C2});
623     EXPECT_FALSE(V1->containsUndefOrPoisonElement());
624     EXPECT_FALSE(V1->containsPoisonElement());
625   }
626 
627   {
628     Constant *V2 = ConstantVector::get({C1, CU});
629     EXPECT_TRUE(V2->containsUndefOrPoisonElement());
630     EXPECT_FALSE(V2->containsPoisonElement());
631   }
632 
633   {
634     Constant *V3 = ConstantVector::get({C1, CP});
635     EXPECT_TRUE(V3->containsUndefOrPoisonElement());
636     EXPECT_TRUE(V3->containsPoisonElement());
637   }
638 
639   {
640     Constant *V4 = ConstantVector::get({CU, CP});
641     EXPECT_TRUE(V4->containsUndefOrPoisonElement());
642     EXPECT_TRUE(V4->containsPoisonElement());
643   }
644 }
645 
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?).
649 
650 TEST(ConstantsTest, isElementWiseEqual) {
651   LLVMContext Context;
652 
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);
657 
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});
662 
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));
668 
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);
673 
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});
678 
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));
684 
685   PointerType *PtrTy = Type::getInt8PtrTy(Context);
686   Constant *CPU = UndefValue::get(PtrTy);
687   Constant *CP0 = ConstantPointerNull::get(PtrTy);
688 
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});
692 
693   EXPECT_FALSE(CP0000->isElementWiseEqual(CP00U0));
694   EXPECT_FALSE(CP00U0->isElementWiseEqual(CP0000));
695   EXPECT_FALSE(CP0000->isElementWiseEqual(CP00U));
696   EXPECT_FALSE(CP00U->isElementWiseEqual(CP00U0));
697 }
698 
699 // Check that vector/aggregate constants correctly store undef and poison
700 // elements.
701 
702 TEST(ConstantsTest, CheckElementWiseUndefPoison) {
703   LLVMContext Context;
704 
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);
710 
711   {
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()));
720   }
721 
722   {
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()));
731   }
732 
733   {
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()));
742   }
743 }
744 
745 TEST(ConstantsTest, GetSplatValueRoundTrip) {
746   LLVMContext Context;
747 
748   Type *FloatTy = Type::getFloatTy(Context);
749   Type *Int32Ty = Type::getInt32Ty(Context);
750   Type *Int8Ty = Type::getInt8Ty(Context);
751 
752   for (unsigned Min : {1, 2, 8}) {
753     auto ScalableEC = ElementCount::getScalable(Min);
754     auto FixedEC = ElementCount::getFixed(Min);
755 
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);
760 
761         for (auto *C : {Zero, One}) {
762           Constant *Splat = ConstantVector::getSplat(EC, C);
763           ASSERT_NE(nullptr, Splat);
764 
765           Constant *SplatVal = Splat->getSplatValue();
766           EXPECT_NE(nullptr, SplatVal);
767           EXPECT_EQ(SplatVal, C);
768         }
769       }
770     }
771   }
772 }
773 
774 TEST(ConstantsTest, ComdatUserTracking) {
775   LLVMContext Context;
776   Module M("MyModule", Context);
777 
778   Comdat *C = M.getOrInsertComdat("comdat");
779   const SmallPtrSetImpl<GlobalObject *> &Users = C->getUsers();
780   EXPECT_TRUE(Users.size() == 0);
781 
782   Type *Ty = Type::getInt8Ty(Context);
783   GlobalVariable *GV1 = cast<GlobalVariable>(M.getOrInsertGlobal("gv1", Ty));
784   GV1->setComdat(C);
785   EXPECT_TRUE(Users.size() == 1);
786   EXPECT_TRUE(Users.contains(GV1));
787 
788   GlobalVariable *GV2 = cast<GlobalVariable>(M.getOrInsertGlobal("gv2", Ty));
789   GV2->setComdat(C);
790   EXPECT_TRUE(Users.size() == 2);
791   EXPECT_TRUE(Users.contains(GV2));
792 
793   GV1->eraseFromParent();
794   EXPECT_TRUE(Users.size() == 1);
795   EXPECT_TRUE(Users.contains(GV2));
796 
797   GV2->eraseFromParent();
798   EXPECT_TRUE(Users.size() == 0);
799 }
800 
801 } // end anonymous namespace
802 } // end namespace llvm
803