xref: /llvm-project/llvm/unittests/IR/ConstantsTest.cpp (revision fe16aa7d65512161bd7ea3b85f6aa726cc36ae12)
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/DerivedTypes.h"
13 #include "llvm/IR/InstrTypes.h"
14 #include "llvm/IR/Instruction.h"
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/Support/SourceMgr.h"
18 #include "gtest/gtest.h"
19 
20 namespace llvm {
21 namespace {
22 
23 TEST(ConstantsTest, Integer_i1) {
24   LLVMContext Context;
25   IntegerType *Int1 = IntegerType::get(Context, 1);
26   Constant *One = ConstantInt::get(Int1, 1, true);
27   Constant *Zero = ConstantInt::get(Int1, 0);
28   Constant *NegOne = ConstantInt::get(Int1, static_cast<uint64_t>(-1), true);
29   EXPECT_EQ(NegOne, ConstantInt::getSigned(Int1, -1));
30   Constant *Undef = UndefValue::get(Int1);
31 
32   // Input:  @b = constant i1 add(i1 1 , i1 1)
33   // Output: @b = constant i1 false
34   EXPECT_EQ(Zero, ConstantExpr::getAdd(One, One));
35 
36   // @c = constant i1 add(i1 -1, i1 1)
37   // @c = constant i1 false
38   EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, One));
39 
40   // @d = constant i1 add(i1 -1, i1 -1)
41   // @d = constant i1 false
42   EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, NegOne));
43 
44   // @e = constant i1 sub(i1 -1, i1 1)
45   // @e = constant i1 false
46   EXPECT_EQ(Zero, ConstantExpr::getSub(NegOne, One));
47 
48   // @f = constant i1 sub(i1 1 , i1 -1)
49   // @f = constant i1 false
50   EXPECT_EQ(Zero, ConstantExpr::getSub(One, NegOne));
51 
52   // @g = constant i1 sub(i1 1 , i1 1)
53   // @g = constant i1 false
54   EXPECT_EQ(Zero, ConstantExpr::getSub(One, One));
55 
56   // @h = constant i1 shl(i1 1 , i1 1)  ; undefined
57   // @h = constant i1 undef
58   EXPECT_EQ(Undef, ConstantExpr::getShl(One, One));
59 
60   // @i = constant i1 shl(i1 1 , i1 0)
61   // @i = constant i1 true
62   EXPECT_EQ(One, ConstantExpr::getShl(One, Zero));
63 
64   // @j = constant i1 lshr(i1 1, i1 1)  ; undefined
65   // @j = constant i1 undef
66   EXPECT_EQ(Undef, ConstantExpr::getLShr(One, One));
67 
68   // @m = constant i1 ashr(i1 1, i1 1)  ; undefined
69   // @m = constant i1 undef
70   EXPECT_EQ(Undef, ConstantExpr::getAShr(One, One));
71 
72   // @n = constant i1 mul(i1 -1, i1 1)
73   // @n = constant i1 true
74   EXPECT_EQ(One, ConstantExpr::getMul(NegOne, One));
75 
76   // @o = constant i1 sdiv(i1 -1, i1 1) ; overflow
77   // @o = constant i1 true
78   EXPECT_EQ(One, ConstantExpr::getSDiv(NegOne, One));
79 
80   // @p = constant i1 sdiv(i1 1 , i1 -1); overflow
81   // @p = constant i1 true
82   EXPECT_EQ(One, ConstantExpr::getSDiv(One, NegOne));
83 
84   // @q = constant i1 udiv(i1 -1, i1 1)
85   // @q = constant i1 true
86   EXPECT_EQ(One, ConstantExpr::getUDiv(NegOne, One));
87 
88   // @r = constant i1 udiv(i1 1, i1 -1)
89   // @r = constant i1 true
90   EXPECT_EQ(One, ConstantExpr::getUDiv(One, NegOne));
91 
92   // @s = constant i1 srem(i1 -1, i1 1) ; overflow
93   // @s = constant i1 false
94   EXPECT_EQ(Zero, ConstantExpr::getSRem(NegOne, One));
95 
96   // @t = constant i1 urem(i1 -1, i1 1)
97   // @t = constant i1 false
98   EXPECT_EQ(Zero, ConstantExpr::getURem(NegOne, One));
99 
100   // @u = constant i1 srem(i1  1, i1 -1) ; overflow
101   // @u = constant i1 false
102   EXPECT_EQ(Zero, ConstantExpr::getSRem(One, NegOne));
103 }
104 
105 TEST(ConstantsTest, IntSigns) {
106   LLVMContext Context;
107   IntegerType *Int8Ty = Type::getInt8Ty(Context);
108   EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, false)->getSExtValue());
109   EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, true)->getSExtValue());
110   EXPECT_EQ(100, ConstantInt::getSigned(Int8Ty, 100)->getSExtValue());
111   EXPECT_EQ(-50, ConstantInt::get(Int8Ty, 206)->getSExtValue());
112   EXPECT_EQ(-50, ConstantInt::getSigned(Int8Ty, -50)->getSExtValue());
113   EXPECT_EQ(206U, ConstantInt::getSigned(Int8Ty, -50)->getZExtValue());
114 
115   // Overflow is handled by truncation.
116   EXPECT_EQ(0x3b, ConstantInt::get(Int8Ty, 0x13b)->getSExtValue());
117 }
118 
119 TEST(ConstantsTest, FP128Test) {
120   LLVMContext Context;
121   Type *FP128Ty = Type::getFP128Ty(Context);
122 
123   IntegerType *Int128Ty = Type::getIntNTy(Context, 128);
124   Constant *Zero128 = Constant::getNullValue(Int128Ty);
125   Constant *X = ConstantExpr::getUIToFP(Zero128, FP128Ty);
126   EXPECT_TRUE(isa<ConstantFP>(X));
127 }
128 
129 TEST(ConstantsTest, PointerCast) {
130   LLVMContext C;
131   Type *Int8PtrTy = Type::getInt8PtrTy(C);
132   Type *Int32PtrTy = Type::getInt32PtrTy(C);
133   Type *Int64Ty = Type::getInt64Ty(C);
134   VectorType *Int8PtrVecTy = FixedVectorType::get(Int8PtrTy, 4);
135   VectorType *Int32PtrVecTy = FixedVectorType::get(Int32PtrTy, 4);
136   VectorType *Int64VecTy = FixedVectorType::get(Int64Ty, 4);
137 
138   // ptrtoint i8* to i64
139   EXPECT_EQ(
140       Constant::getNullValue(Int64Ty),
141       ConstantExpr::getPointerCast(Constant::getNullValue(Int8PtrTy), Int64Ty));
142 
143   // bitcast i8* to i32*
144   EXPECT_EQ(Constant::getNullValue(Int32PtrTy),
145             ConstantExpr::getPointerCast(Constant::getNullValue(Int8PtrTy),
146                                          Int32PtrTy));
147 
148   // ptrtoint <4 x i8*> to <4 x i64>
149   EXPECT_EQ(Constant::getNullValue(Int64VecTy),
150             ConstantExpr::getPointerCast(Constant::getNullValue(Int8PtrVecTy),
151                                          Int64VecTy));
152 
153   // bitcast <4 x i8*> to <4 x i32*>
154   EXPECT_EQ(Constant::getNullValue(Int32PtrVecTy),
155             ConstantExpr::getPointerCast(Constant::getNullValue(Int8PtrVecTy),
156                                          Int32PtrVecTy));
157 
158   Type *Int32Ptr1Ty = Type::getInt32PtrTy(C, 1);
159   ConstantInt *K = ConstantInt::get(Type::getInt64Ty(C), 1234);
160 
161   // Make sure that addrspacecast of inttoptr is not folded away.
162   EXPECT_NE(K, ConstantExpr::getAddrSpaceCast(
163                    ConstantExpr::getIntToPtr(K, Int32PtrTy), Int32Ptr1Ty));
164   EXPECT_NE(K, ConstantExpr::getAddrSpaceCast(
165                    ConstantExpr::getIntToPtr(K, Int32Ptr1Ty), Int32PtrTy));
166 
167   Constant *NullInt32Ptr0 = Constant::getNullValue(Int32PtrTy);
168   Constant *NullInt32Ptr1 = Constant::getNullValue(Int32Ptr1Ty);
169 
170   // Make sure that addrspacecast of null is not folded away.
171   EXPECT_NE(Constant::getNullValue(Int32PtrTy),
172             ConstantExpr::getAddrSpaceCast(NullInt32Ptr0, Int32Ptr1Ty));
173 
174   EXPECT_NE(Constant::getNullValue(Int32Ptr1Ty),
175             ConstantExpr::getAddrSpaceCast(NullInt32Ptr1, Int32PtrTy));
176 }
177 
178 #define CHECK(x, y)                                                            \
179   {                                                                            \
180     std::string __s;                                                           \
181     raw_string_ostream __o(__s);                                               \
182     Instruction *__I = cast<ConstantExpr>(x)->getAsInstruction();              \
183     __I->print(__o);                                                           \
184     __I->deleteValue();                                                        \
185     __o.flush();                                                               \
186     EXPECT_EQ(std::string("  <badref> = " y), __s);                            \
187   }
188 
189 TEST(ConstantsTest, AsInstructionsTest) {
190   LLVMContext Context;
191   std::unique_ptr<Module> M(new Module("MyModule", Context));
192 
193   Type *Int64Ty = Type::getInt64Ty(Context);
194   Type *Int32Ty = Type::getInt32Ty(Context);
195   Type *Int16Ty = Type::getInt16Ty(Context);
196   Type *Int1Ty = Type::getInt1Ty(Context);
197   Type *FloatTy = Type::getFloatTy(Context);
198   Type *DoubleTy = Type::getDoubleTy(Context);
199 
200   Constant *Global =
201       M->getOrInsertGlobal("dummy", PointerType::getUnqual(Int32Ty));
202   Constant *Global2 =
203       M->getOrInsertGlobal("dummy2", PointerType::getUnqual(Int32Ty));
204 
205   Constant *P0 = ConstantExpr::getPtrToInt(Global, Int32Ty);
206   Constant *P1 = ConstantExpr::getUIToFP(P0, FloatTy);
207   Constant *P2 = ConstantExpr::getUIToFP(P0, DoubleTy);
208   Constant *P3 = ConstantExpr::getTrunc(P0, Int1Ty);
209   Constant *P4 = ConstantExpr::getPtrToInt(Global2, Int32Ty);
210   Constant *P5 = ConstantExpr::getUIToFP(P4, FloatTy);
211   Constant *P6 = ConstantExpr::getBitCast(P4, FixedVectorType::get(Int16Ty, 2));
212 
213   Constant *One = ConstantInt::get(Int32Ty, 1);
214   Constant *Two = ConstantInt::get(Int64Ty, 2);
215   Constant *Big = ConstantInt::get(Context, APInt{256, uint64_t(-1), true});
216   Constant *Elt = ConstantInt::get(Int16Ty, 2015);
217   Constant *Poison16 = PoisonValue::get(Int16Ty);
218   Constant *Undef64 = UndefValue::get(Int64Ty);
219   Constant *UndefV16 = UndefValue::get(P6->getType());
220   Constant *PoisonV16 = PoisonValue::get(P6->getType());
221 
222 #define P0STR "ptrtoint (i32** @dummy to i32)"
223 #define P1STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to float)"
224 #define P2STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to double)"
225 #define P3STR "ptrtoint (i32** @dummy to i1)"
226 #define P4STR "ptrtoint (i32** @dummy2 to i32)"
227 #define P5STR "uitofp (i32 ptrtoint (i32** @dummy2 to i32) to float)"
228 #define P6STR "bitcast (i32 ptrtoint (i32** @dummy2 to i32) to <2 x i16>)"
229 
230   CHECK(ConstantExpr::getNeg(P0), "sub i32 0, " P0STR);
231   CHECK(ConstantExpr::getFNeg(P1), "fneg float " P1STR);
232   CHECK(ConstantExpr::getNot(P0), "xor i32 " P0STR ", -1");
233   CHECK(ConstantExpr::getAdd(P0, P0), "add i32 " P0STR ", " P0STR);
234   CHECK(ConstantExpr::getAdd(P0, P0, false, true),
235         "add nsw i32 " P0STR ", " P0STR);
236   CHECK(ConstantExpr::getAdd(P0, P0, true, true),
237         "add nuw nsw i32 " P0STR ", " P0STR);
238   CHECK(ConstantExpr::getFAdd(P1, P1), "fadd float " P1STR ", " P1STR);
239   CHECK(ConstantExpr::getSub(P0, P0), "sub i32 " P0STR ", " P0STR);
240   CHECK(ConstantExpr::getFSub(P1, P1), "fsub float " P1STR ", " P1STR);
241   CHECK(ConstantExpr::getMul(P0, P0), "mul i32 " P0STR ", " P0STR);
242   CHECK(ConstantExpr::getFMul(P1, P1), "fmul float " P1STR ", " P1STR);
243   CHECK(ConstantExpr::getUDiv(P0, P0), "udiv i32 " P0STR ", " P0STR);
244   CHECK(ConstantExpr::getSDiv(P0, P0), "sdiv i32 " P0STR ", " P0STR);
245   CHECK(ConstantExpr::getFDiv(P1, P1), "fdiv float " P1STR ", " P1STR);
246   CHECK(ConstantExpr::getURem(P0, P0), "urem i32 " P0STR ", " P0STR);
247   CHECK(ConstantExpr::getSRem(P0, P0), "srem i32 " P0STR ", " P0STR);
248   CHECK(ConstantExpr::getFRem(P1, P1), "frem float " P1STR ", " P1STR);
249   CHECK(ConstantExpr::getAnd(P0, P0), "and i32 " P0STR ", " P0STR);
250   CHECK(ConstantExpr::getOr(P0, P0), "or i32 " P0STR ", " P0STR);
251   CHECK(ConstantExpr::getXor(P0, P0), "xor i32 " P0STR ", " P0STR);
252   CHECK(ConstantExpr::getShl(P0, P0), "shl i32 " P0STR ", " P0STR);
253   CHECK(ConstantExpr::getShl(P0, P0, true), "shl nuw i32 " P0STR ", " P0STR);
254   CHECK(ConstantExpr::getShl(P0, P0, false, true),
255         "shl nsw i32 " P0STR ", " P0STR);
256   CHECK(ConstantExpr::getLShr(P0, P0, false), "lshr i32 " P0STR ", " P0STR);
257   CHECK(ConstantExpr::getLShr(P0, P0, true),
258         "lshr exact i32 " P0STR ", " P0STR);
259   CHECK(ConstantExpr::getAShr(P0, P0, false), "ashr i32 " P0STR ", " P0STR);
260   CHECK(ConstantExpr::getAShr(P0, P0, true),
261         "ashr exact i32 " P0STR ", " P0STR);
262 
263   CHECK(ConstantExpr::getSExt(P0, Int64Ty), "sext i32 " P0STR " to i64");
264   CHECK(ConstantExpr::getZExt(P0, Int64Ty), "zext i32 " P0STR " to i64");
265   CHECK(ConstantExpr::getFPTrunc(P2, FloatTy),
266         "fptrunc double " P2STR " to float");
267   CHECK(ConstantExpr::getFPExtend(P1, DoubleTy),
268         "fpext float " P1STR " to double");
269 
270   CHECK(ConstantExpr::getExactUDiv(P0, P0), "udiv exact i32 " P0STR ", " P0STR);
271 
272   CHECK(ConstantExpr::getSelect(P3, P0, P4),
273         "select i1 " P3STR ", i32 " P0STR ", i32 " P4STR);
274   CHECK(ConstantExpr::getICmp(CmpInst::ICMP_EQ, P0, P4),
275         "icmp eq i32 " P0STR ", " P4STR);
276   CHECK(ConstantExpr::getFCmp(CmpInst::FCMP_ULT, P1, P5),
277         "fcmp ult float " P1STR ", " P5STR);
278 
279   std::vector<Constant *> V;
280   V.push_back(One);
281   // FIXME: getGetElementPtr() actually creates an inbounds ConstantGEP,
282   //        not a normal one!
283   // CHECK(ConstantExpr::getGetElementPtr(Global, V, false),
284   //      "getelementptr i32*, i32** @dummy, i32 1");
285   CHECK(ConstantExpr::getInBoundsGetElementPtr(PointerType::getUnqual(Int32Ty),
286                                                Global, V),
287         "getelementptr inbounds i32*, i32** @dummy, i32 1");
288 
289   CHECK(ConstantExpr::getExtractElement(P6, One),
290         "extractelement <2 x i16> " P6STR ", i32 1");
291 
292   EXPECT_EQ(Poison16, ConstantExpr::getExtractElement(P6, Two));
293   EXPECT_EQ(Poison16, ConstantExpr::getExtractElement(P6, Big));
294   EXPECT_EQ(Poison16, ConstantExpr::getExtractElement(P6, Undef64));
295 
296   EXPECT_EQ(Elt, ConstantExpr::getExtractElement(
297                      ConstantExpr::getInsertElement(P6, Elt, One), One));
298   EXPECT_EQ(UndefV16, ConstantExpr::getInsertElement(P6, Elt, Two));
299   EXPECT_EQ(UndefV16, ConstantExpr::getInsertElement(P6, Elt, Big));
300   EXPECT_EQ(PoisonV16, ConstantExpr::getInsertElement(P6, Elt, Undef64));
301 }
302 
303 #ifdef GTEST_HAS_DEATH_TEST
304 #ifndef NDEBUG
305 TEST(ConstantsTest, ReplaceWithConstantTest) {
306   LLVMContext Context;
307   std::unique_ptr<Module> M(new Module("MyModule", Context));
308 
309   Type *Int32Ty = Type::getInt32Ty(Context);
310   Constant *One = ConstantInt::get(Int32Ty, 1);
311 
312   Constant *Global =
313       M->getOrInsertGlobal("dummy", PointerType::getUnqual(Int32Ty));
314   Constant *GEP = ConstantExpr::getGetElementPtr(
315       PointerType::getUnqual(Int32Ty), Global, One);
316   EXPECT_DEATH(Global->replaceAllUsesWith(GEP),
317                "this->replaceAllUsesWith\\(expr\\(this\\)\\) is NOT valid!");
318 }
319 
320 #endif
321 #endif
322 
323 #undef CHECK
324 
325 TEST(ConstantsTest, ConstantArrayReplaceWithConstant) {
326   LLVMContext Context;
327   std::unique_ptr<Module> M(new Module("MyModule", Context));
328 
329   Type *IntTy = Type::getInt8Ty(Context);
330   ArrayType *ArrayTy = ArrayType::get(IntTy, 2);
331   Constant *A01Vals[2] = {ConstantInt::get(IntTy, 0),
332                           ConstantInt::get(IntTy, 1)};
333   Constant *A01 = ConstantArray::get(ArrayTy, A01Vals);
334 
335   Constant *Global = new GlobalVariable(*M, IntTy, false,
336                                         GlobalValue::ExternalLinkage, nullptr);
337   Constant *GlobalInt = ConstantExpr::getPtrToInt(Global, IntTy);
338   Constant *A0GVals[2] = {ConstantInt::get(IntTy, 0), GlobalInt};
339   Constant *A0G = ConstantArray::get(ArrayTy, A0GVals);
340   ASSERT_NE(A01, A0G);
341 
342   GlobalVariable *RefArray =
343       new GlobalVariable(*M, ArrayTy, false, GlobalValue::ExternalLinkage, A0G);
344   ASSERT_EQ(A0G, RefArray->getInitializer());
345 
346   GlobalInt->replaceAllUsesWith(ConstantInt::get(IntTy, 1));
347   ASSERT_EQ(A01, RefArray->getInitializer());
348 }
349 
350 TEST(ConstantsTest, ConstantExprReplaceWithConstant) {
351   LLVMContext Context;
352   std::unique_ptr<Module> M(new Module("MyModule", Context));
353 
354   Type *IntTy = Type::getInt8Ty(Context);
355   Constant *G1 = new GlobalVariable(*M, IntTy, false,
356                                     GlobalValue::ExternalLinkage, nullptr);
357   Constant *G2 = new GlobalVariable(*M, IntTy, false,
358                                     GlobalValue::ExternalLinkage, nullptr);
359   ASSERT_NE(G1, G2);
360 
361   Constant *Int1 = ConstantExpr::getPtrToInt(G1, IntTy);
362   Constant *Int2 = ConstantExpr::getPtrToInt(G2, IntTy);
363   ASSERT_NE(Int1, Int2);
364 
365   GlobalVariable *Ref =
366       new GlobalVariable(*M, IntTy, false, GlobalValue::ExternalLinkage, Int1);
367   ASSERT_EQ(Int1, Ref->getInitializer());
368 
369   G1->replaceAllUsesWith(G2);
370   ASSERT_EQ(Int2, Ref->getInitializer());
371 }
372 
373 TEST(ConstantsTest, GEPReplaceWithConstant) {
374   LLVMContext Context;
375   std::unique_ptr<Module> M(new Module("MyModule", Context));
376 
377   Type *IntTy = Type::getInt32Ty(Context);
378   Type *PtrTy = PointerType::get(IntTy, 0);
379   auto *C1 = ConstantInt::get(IntTy, 1);
380   auto *Placeholder = new GlobalVariable(
381       *M, IntTy, false, GlobalValue::ExternalWeakLinkage, nullptr);
382   auto *GEP = ConstantExpr::getGetElementPtr(IntTy, Placeholder, C1);
383   ASSERT_EQ(GEP->getOperand(0), Placeholder);
384 
385   auto *Ref =
386       new GlobalVariable(*M, PtrTy, false, GlobalValue::ExternalLinkage, GEP);
387   ASSERT_EQ(GEP, Ref->getInitializer());
388 
389   auto *Global = new GlobalVariable(*M, PtrTy, false,
390                                     GlobalValue::ExternalLinkage, nullptr);
391   auto *Alias = GlobalAlias::create(IntTy, 0, GlobalValue::ExternalLinkage,
392                                     "alias", Global, M.get());
393   Placeholder->replaceAllUsesWith(Alias);
394   ASSERT_EQ(GEP, Ref->getInitializer());
395   ASSERT_EQ(GEP->getOperand(0), Alias);
396 }
397 
398 TEST(ConstantsTest, AliasCAPI) {
399   LLVMContext Context;
400   SMDiagnostic Error;
401   std::unique_ptr<Module> M =
402       parseAssemblyString("@g = global i32 42", Error, Context);
403   GlobalVariable *G = M->getGlobalVariable("g");
404   Type *I16Ty = Type::getInt16Ty(Context);
405   Type *I16PTy = PointerType::get(I16Ty, 0);
406   Constant *Aliasee = ConstantExpr::getBitCast(G, I16PTy);
407   LLVMValueRef AliasRef =
408       LLVMAddAlias(wrap(M.get()), wrap(I16PTy), wrap(Aliasee), "a");
409   ASSERT_EQ(unwrap<GlobalAlias>(AliasRef)->getAliasee(), Aliasee);
410 }
411 
412 static std::string getNameOfType(Type *T) {
413   std::string S;
414   raw_string_ostream RSOS(S);
415   T->print(RSOS);
416   return S;
417 }
418 
419 TEST(ConstantsTest, BuildConstantDataArrays) {
420   LLVMContext Context;
421 
422   for (Type *T : {Type::getInt8Ty(Context), Type::getInt16Ty(Context),
423                   Type::getInt32Ty(Context), Type::getInt64Ty(Context)}) {
424     ArrayType *ArrayTy = ArrayType::get(T, 2);
425     Constant *Vals[] = {ConstantInt::get(T, 0), ConstantInt::get(T, 1)};
426     Constant *CA = ConstantArray::get(ArrayTy, Vals);
427     ASSERT_TRUE(isa<ConstantDataArray>(CA)) << " T = " << getNameOfType(T);
428     auto *CDA = cast<ConstantDataArray>(CA);
429     Constant *CA2 = ConstantDataArray::getRaw(
430         CDA->getRawDataValues(), CDA->getNumElements(), CDA->getElementType());
431     ASSERT_TRUE(CA == CA2) << " T = " << getNameOfType(T);
432   }
433 
434   for (Type *T : {Type::getHalfTy(Context), Type::getBFloatTy(Context),
435                   Type::getFloatTy(Context), Type::getDoubleTy(Context)}) {
436     ArrayType *ArrayTy = ArrayType::get(T, 2);
437     Constant *Vals[] = {ConstantFP::get(T, 0), ConstantFP::get(T, 1)};
438     Constant *CA = ConstantArray::get(ArrayTy, Vals);
439     ASSERT_TRUE(isa<ConstantDataArray>(CA)) << " T = " << getNameOfType(T);
440     auto *CDA = cast<ConstantDataArray>(CA);
441     Constant *CA2 = ConstantDataArray::getRaw(
442         CDA->getRawDataValues(), CDA->getNumElements(), CDA->getElementType());
443     ASSERT_TRUE(CA == CA2) << " T = " << getNameOfType(T);
444   }
445 }
446 
447 TEST(ConstantsTest, BuildConstantDataVectors) {
448   LLVMContext Context;
449 
450   for (Type *T : {Type::getInt8Ty(Context), Type::getInt16Ty(Context),
451                   Type::getInt32Ty(Context), Type::getInt64Ty(Context)}) {
452     Constant *Vals[] = {ConstantInt::get(T, 0), ConstantInt::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);
459   }
460 
461   for (Type *T : {Type::getHalfTy(Context), Type::getBFloatTy(Context),
462                   Type::getFloatTy(Context), Type::getDoubleTy(Context)}) {
463     Constant *Vals[] = {ConstantFP::get(T, 0), ConstantFP::get(T, 1)};
464     Constant *CV = ConstantVector::get(Vals);
465     ASSERT_TRUE(isa<ConstantDataVector>(CV)) << " T = " << getNameOfType(T);
466     auto *CDV = cast<ConstantDataVector>(CV);
467     Constant *CV2 = ConstantDataVector::getRaw(
468         CDV->getRawDataValues(), CDV->getNumElements(), CDV->getElementType());
469     ASSERT_TRUE(CV == CV2) << " T = " << getNameOfType(T);
470   }
471 }
472 
473 TEST(ConstantsTest, BitcastToGEP) {
474   LLVMContext Context;
475   std::unique_ptr<Module> M(new Module("MyModule", Context));
476 
477   auto *i32 = Type::getInt32Ty(Context);
478   auto *U = StructType::create(Context, "Unsized");
479   Type *EltTys[] = {i32, U};
480   auto *S = StructType::create(EltTys);
481 
482   auto *G =
483       new GlobalVariable(*M, S, false, GlobalValue::ExternalLinkage, nullptr);
484   auto *PtrTy = PointerType::get(i32, 0);
485   auto *C = ConstantExpr::getBitCast(G, PtrTy);
486   ASSERT_EQ(cast<ConstantExpr>(C)->getOpcode(), Instruction::BitCast);
487 }
488 
489 bool foldFuncPtrAndConstToNull(LLVMContext &Context, Module *TheModule,
490                                uint64_t AndValue,
491                                MaybeAlign FunctionAlign = llvm::None) {
492   Type *VoidType(Type::getVoidTy(Context));
493   FunctionType *FuncType(FunctionType::get(VoidType, false));
494   Function *Func(
495       Function::Create(FuncType, GlobalValue::ExternalLinkage, "", TheModule));
496 
497   if (FunctionAlign)
498     Func->setAlignment(*FunctionAlign);
499 
500   IntegerType *ConstantIntType(Type::getInt32Ty(Context));
501   ConstantInt *TheConstant(ConstantInt::get(ConstantIntType, AndValue));
502 
503   Constant *TheConstantExpr(ConstantExpr::getPtrToInt(Func, ConstantIntType));
504 
505   bool Result =
506       ConstantExpr::get(Instruction::And, TheConstantExpr, TheConstant)
507           ->isNullValue();
508 
509   if (!TheModule) {
510     // If the Module exists then it will delete the Function.
511     delete Func;
512   }
513 
514   return Result;
515 }
516 
517 TEST(ConstantsTest, FoldFunctionPtrAlignUnknownAnd2) {
518   LLVMContext Context;
519   Module TheModule("TestModule", Context);
520   // When the DataLayout doesn't specify a function pointer alignment we
521   // assume in this case that it is 4 byte aligned. This is a bug but we can't
522   // fix it directly because it causes a code size regression on X86.
523   // FIXME: This test should be changed once existing targets have
524   // appropriate defaults. See associated FIXME in ConstantFoldBinaryInstruction
525   ASSERT_TRUE(foldFuncPtrAndConstToNull(Context, &TheModule, 2));
526 }
527 
528 TEST(ConstantsTest, DontFoldFunctionPtrAlignUnknownAnd4) {
529   LLVMContext Context;
530   Module TheModule("TestModule", Context);
531   ASSERT_FALSE(foldFuncPtrAndConstToNull(Context, &TheModule, 4));
532 }
533 
534 TEST(ConstantsTest, FoldFunctionPtrAlign4) {
535   LLVMContext Context;
536   Module TheModule("TestModule", Context);
537   const char *AlignmentStrings[] = {"Fi32", "Fn32"};
538 
539   for (unsigned AndValue = 1; AndValue <= 2; ++AndValue) {
540     for (const char *AlignmentString : AlignmentStrings) {
541       TheModule.setDataLayout(AlignmentString);
542       ASSERT_TRUE(foldFuncPtrAndConstToNull(Context, &TheModule, AndValue));
543     }
544   }
545 }
546 
547 TEST(ConstantsTest, DontFoldFunctionPtrAlign1) {
548   LLVMContext Context;
549   Module TheModule("TestModule", Context);
550   const char *AlignmentStrings[] = {"Fi8", "Fn8"};
551 
552   for (const char *AlignmentString : AlignmentStrings) {
553     TheModule.setDataLayout(AlignmentString);
554     ASSERT_FALSE(foldFuncPtrAndConstToNull(Context, &TheModule, 2));
555   }
556 }
557 
558 TEST(ConstantsTest, FoldFunctionAlign4PtrAlignMultiple) {
559   LLVMContext Context;
560   Module TheModule("TestModule", Context);
561   TheModule.setDataLayout("Fn8");
562   ASSERT_TRUE(foldFuncPtrAndConstToNull(Context, &TheModule, 2, Align(4)));
563 }
564 
565 TEST(ConstantsTest, DontFoldFunctionAlign4PtrAlignIndependent) {
566   LLVMContext Context;
567   Module TheModule("TestModule", Context);
568   TheModule.setDataLayout("Fi8");
569   ASSERT_FALSE(foldFuncPtrAndConstToNull(Context, &TheModule, 2, Align(4)));
570 }
571 
572 TEST(ConstantsTest, DontFoldFunctionPtrIfNoModule) {
573   LLVMContext Context;
574   // Even though the function is explicitly 4 byte aligned, in the absence of a
575   // DataLayout we can't assume that the function pointer is aligned.
576   ASSERT_FALSE(foldFuncPtrAndConstToNull(Context, nullptr, 2, Align(4)));
577 }
578 
579 TEST(ConstantsTest, FoldGlobalVariablePtr) {
580   LLVMContext Context;
581 
582   IntegerType *IntType(Type::getInt32Ty(Context));
583 
584   std::unique_ptr<GlobalVariable> Global(
585       new GlobalVariable(IntType, true, GlobalValue::ExternalLinkage));
586 
587   Global->setAlignment(Align(4));
588 
589   ConstantInt *TheConstant(ConstantInt::get(IntType, 2));
590 
591   Constant *TheConstantExpr(ConstantExpr::getPtrToInt(Global.get(), IntType));
592 
593   ASSERT_TRUE(ConstantExpr::get(Instruction::And, TheConstantExpr, TheConstant)
594                   ->isNullValue());
595 }
596 
597 // Check that containsUndefOrPoisonElement and containsPoisonElement is working
598 // great
599 
600 TEST(ConstantsTest, containsUndefElemTest) {
601   LLVMContext Context;
602 
603   Type *Int32Ty = Type::getInt32Ty(Context);
604   Constant *CU = UndefValue::get(Int32Ty);
605   Constant *CP = PoisonValue::get(Int32Ty);
606   Constant *C1 = ConstantInt::get(Int32Ty, 1);
607   Constant *C2 = ConstantInt::get(Int32Ty, 2);
608 
609   {
610     Constant *V1 = ConstantVector::get({C1, C2});
611     EXPECT_FALSE(V1->containsUndefOrPoisonElement());
612     EXPECT_FALSE(V1->containsPoisonElement());
613   }
614 
615   {
616     Constant *V2 = ConstantVector::get({C1, CU});
617     EXPECT_TRUE(V2->containsUndefOrPoisonElement());
618     EXPECT_FALSE(V2->containsPoisonElement());
619   }
620 
621   {
622     Constant *V3 = ConstantVector::get({C1, CP});
623     EXPECT_TRUE(V3->containsUndefOrPoisonElement());
624     EXPECT_TRUE(V3->containsPoisonElement());
625   }
626 
627   {
628     Constant *V4 = ConstantVector::get({CU, CP});
629     EXPECT_TRUE(V4->containsUndefOrPoisonElement());
630     EXPECT_TRUE(V4->containsPoisonElement());
631   }
632 }
633 
634 // Check that undefined elements in vector constants are matched
635 // correctly for both integer and floating-point types. Just don't
636 // crash on vectors of pointers (could be handled?).
637 
638 TEST(ConstantsTest, isElementWiseEqual) {
639   LLVMContext Context;
640 
641   Type *Int32Ty = Type::getInt32Ty(Context);
642   Constant *CU = UndefValue::get(Int32Ty);
643   Constant *C1 = ConstantInt::get(Int32Ty, 1);
644   Constant *C2 = ConstantInt::get(Int32Ty, 2);
645 
646   Constant *C1211 = ConstantVector::get({C1, C2, C1, C1});
647   Constant *C12U1 = ConstantVector::get({C1, C2, CU, C1});
648   Constant *C12U2 = ConstantVector::get({C1, C2, CU, C2});
649   Constant *C12U21 = ConstantVector::get({C1, C2, CU, C2, C1});
650 
651   EXPECT_TRUE(C1211->isElementWiseEqual(C12U1));
652   EXPECT_TRUE(C12U1->isElementWiseEqual(C1211));
653   EXPECT_FALSE(C12U2->isElementWiseEqual(C12U1));
654   EXPECT_FALSE(C12U1->isElementWiseEqual(C12U2));
655   EXPECT_FALSE(C12U21->isElementWiseEqual(C12U2));
656 
657   Type *FltTy = Type::getFloatTy(Context);
658   Constant *CFU = UndefValue::get(FltTy);
659   Constant *CF1 = ConstantFP::get(FltTy, 1.0);
660   Constant *CF2 = ConstantFP::get(FltTy, 2.0);
661 
662   Constant *CF1211 = ConstantVector::get({CF1, CF2, CF1, CF1});
663   Constant *CF12U1 = ConstantVector::get({CF1, CF2, CFU, CF1});
664   Constant *CF12U2 = ConstantVector::get({CF1, CF2, CFU, CF2});
665   Constant *CFUU1U = ConstantVector::get({CFU, CFU, CF1, CFU});
666 
667   EXPECT_TRUE(CF1211->isElementWiseEqual(CF12U1));
668   EXPECT_TRUE(CF12U1->isElementWiseEqual(CF1211));
669   EXPECT_TRUE(CFUU1U->isElementWiseEqual(CF12U1));
670   EXPECT_FALSE(CF12U2->isElementWiseEqual(CF12U1));
671   EXPECT_FALSE(CF12U1->isElementWiseEqual(CF12U2));
672 
673   PointerType *PtrTy = Type::getInt8PtrTy(Context);
674   Constant *CPU = UndefValue::get(PtrTy);
675   Constant *CP0 = ConstantPointerNull::get(PtrTy);
676 
677   Constant *CP0000 = ConstantVector::get({CP0, CP0, CP0, CP0});
678   Constant *CP00U0 = ConstantVector::get({CP0, CP0, CPU, CP0});
679   Constant *CP00U = ConstantVector::get({CP0, CP0, CPU});
680 
681   EXPECT_FALSE(CP0000->isElementWiseEqual(CP00U0));
682   EXPECT_FALSE(CP00U0->isElementWiseEqual(CP0000));
683   EXPECT_FALSE(CP0000->isElementWiseEqual(CP00U));
684   EXPECT_FALSE(CP00U->isElementWiseEqual(CP00U0));
685 }
686 
687 // Check that vector/aggregate constants correctly store undef and poison
688 // elements.
689 
690 TEST(ConstantsTest, CheckElementWiseUndefPoison) {
691   LLVMContext Context;
692 
693   Type *Int32Ty = Type::getInt32Ty(Context);
694   StructType *STy = StructType::get(Int32Ty, Int32Ty);
695   ArrayType *ATy = ArrayType::get(Int32Ty, 2);
696   Constant *CU = UndefValue::get(Int32Ty);
697   Constant *CP = PoisonValue::get(Int32Ty);
698 
699   {
700     Constant *CUU = ConstantVector::get({CU, CU});
701     Constant *CPP = ConstantVector::get({CP, CP});
702     Constant *CUP = ConstantVector::get({CU, CP});
703     Constant *CPU = ConstantVector::get({CP, CU});
704     EXPECT_EQ(CUU, UndefValue::get(CUU->getType()));
705     EXPECT_EQ(CPP, PoisonValue::get(CPP->getType()));
706     EXPECT_NE(CUP, UndefValue::get(CUP->getType()));
707     EXPECT_NE(CPU, UndefValue::get(CPU->getType()));
708   }
709 
710   {
711     Constant *CUU = ConstantStruct::get(STy, {CU, CU});
712     Constant *CPP = ConstantStruct::get(STy, {CP, CP});
713     Constant *CUP = ConstantStruct::get(STy, {CU, CP});
714     Constant *CPU = ConstantStruct::get(STy, {CP, CU});
715     EXPECT_EQ(CUU, UndefValue::get(CUU->getType()));
716     EXPECT_EQ(CPP, PoisonValue::get(CPP->getType()));
717     EXPECT_NE(CUP, UndefValue::get(CUP->getType()));
718     EXPECT_NE(CPU, UndefValue::get(CPU->getType()));
719   }
720 
721   {
722     Constant *CUU = ConstantArray::get(ATy, {CU, CU});
723     Constant *CPP = ConstantArray::get(ATy, {CP, CP});
724     Constant *CUP = ConstantArray::get(ATy, {CU, CP});
725     Constant *CPU = ConstantArray::get(ATy, {CP, CU});
726     EXPECT_EQ(CUU, UndefValue::get(CUU->getType()));
727     EXPECT_EQ(CPP, PoisonValue::get(CPP->getType()));
728     EXPECT_NE(CUP, UndefValue::get(CUP->getType()));
729     EXPECT_NE(CPU, UndefValue::get(CPU->getType()));
730   }
731 }
732 
733 TEST(ConstantsTest, GetSplatValueRoundTrip) {
734   LLVMContext Context;
735 
736   Type *FloatTy = Type::getFloatTy(Context);
737   Type *Int32Ty = Type::getInt32Ty(Context);
738   Type *Int8Ty = Type::getInt8Ty(Context);
739 
740   for (unsigned Min : {1, 2, 8}) {
741     auto ScalableEC = ElementCount::getScalable(Min);
742     auto FixedEC = ElementCount::getFixed(Min);
743 
744     for (auto EC : {ScalableEC, FixedEC}) {
745       for (auto *Ty : {FloatTy, Int32Ty, Int8Ty}) {
746         Constant *Zero = Constant::getNullValue(Ty);
747         Constant *One = Constant::getAllOnesValue(Ty);
748 
749         for (auto *C : {Zero, One}) {
750           Constant *Splat = ConstantVector::getSplat(EC, C);
751           ASSERT_NE(nullptr, Splat);
752 
753           Constant *SplatVal = Splat->getSplatValue();
754           EXPECT_NE(nullptr, SplatVal);
755           EXPECT_EQ(SplatVal, C);
756         }
757       }
758     }
759   }
760 }
761 
762 } // end anonymous namespace
763 } // end namespace llvm
764