• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- llvm/unittest/IR/ConstantsTest.cpp - Constants unit tests ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/AsmParser/Parser.h"
11 #include "llvm/IR/Constants.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 "llvm-c/Core.h"
19 #include "gtest/gtest.h"
20 
21 namespace llvm {
22 namespace {
23 
TEST(ConstantsTest,Integer_i1)24 TEST(ConstantsTest, Integer_i1) {
25   IntegerType* Int1 = IntegerType::get(getGlobalContext(), 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 
TEST(ConstantsTest,IntSigns)105 TEST(ConstantsTest, IntSigns) {
106   IntegerType* Int8Ty = Type::getInt8Ty(getGlobalContext());
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 
TEST(ConstantsTest,FP128Test)118 TEST(ConstantsTest, FP128Test) {
119   Type *FP128Ty = Type::getFP128Ty(getGlobalContext());
120 
121   IntegerType *Int128Ty = Type::getIntNTy(getGlobalContext(), 128);
122   Constant *Zero128 = Constant::getNullValue(Int128Ty);
123   Constant *X = ConstantExpr::getUIToFP(Zero128, FP128Ty);
124   EXPECT_TRUE(isa<ConstantFP>(X));
125 }
126 
TEST(ConstantsTest,PointerCast)127 TEST(ConstantsTest, PointerCast) {
128   LLVMContext &C(getGlobalContext());
129   Type *Int8PtrTy = Type::getInt8PtrTy(C);
130   Type *Int32PtrTy = Type::getInt32PtrTy(C);
131   Type *Int64Ty = Type::getInt64Ty(C);
132   VectorType *Int8PtrVecTy = VectorType::get(Int8PtrTy, 4);
133   VectorType *Int32PtrVecTy = VectorType::get(Int32PtrTy, 4);
134   VectorType *Int64VecTy = VectorType::get(Int64Ty, 4);
135 
136   // ptrtoint i8* to i64
137   EXPECT_EQ(Constant::getNullValue(Int64Ty),
138             ConstantExpr::getPointerCast(
139               Constant::getNullValue(Int8PtrTy), Int64Ty));
140 
141   // bitcast i8* to i32*
142   EXPECT_EQ(Constant::getNullValue(Int32PtrTy),
143             ConstantExpr::getPointerCast(
144               Constant::getNullValue(Int8PtrTy), Int32PtrTy));
145 
146   // ptrtoint <4 x i8*> to <4 x i64>
147   EXPECT_EQ(Constant::getNullValue(Int64VecTy),
148             ConstantExpr::getPointerCast(
149               Constant::getNullValue(Int8PtrVecTy), Int64VecTy));
150 
151   // bitcast <4 x i8*> to <4 x i32*>
152   EXPECT_EQ(Constant::getNullValue(Int32PtrVecTy),
153             ConstantExpr::getPointerCast(
154               Constant::getNullValue(Int8PtrVecTy), Int32PtrVecTy));
155 }
156 
157 #define CHECK(x, y) {                                         		\
158     std::string __s;                                            	\
159     raw_string_ostream __o(__s);                                	\
160     Instruction *__I = cast<ConstantExpr>(x)->getAsInstruction();	\
161     __I->print(__o);      						\
162     delete __I; 							\
163     __o.flush();                                                	\
164     EXPECT_EQ(std::string("  <badref> = " y), __s);             	\
165   }
166 
TEST(ConstantsTest,AsInstructionsTest)167 TEST(ConstantsTest, AsInstructionsTest) {
168   std::unique_ptr<Module> M(new Module("MyModule", getGlobalContext()));
169 
170   Type *Int64Ty = Type::getInt64Ty(getGlobalContext());
171   Type *Int32Ty = Type::getInt32Ty(getGlobalContext());
172   Type *Int16Ty = Type::getInt16Ty(getGlobalContext());
173   Type *Int1Ty = Type::getInt1Ty(getGlobalContext());
174   Type *FloatTy = Type::getFloatTy(getGlobalContext());
175   Type *DoubleTy = Type::getDoubleTy(getGlobalContext());
176 
177   Constant *Global = M->getOrInsertGlobal("dummy",
178                                          PointerType::getUnqual(Int32Ty));
179   Constant *Global2 = M->getOrInsertGlobal("dummy2",
180                                          PointerType::getUnqual(Int32Ty));
181 
182   Constant *P0 = ConstantExpr::getPtrToInt(Global, Int32Ty);
183   Constant *P1 = ConstantExpr::getUIToFP(P0, FloatTy);
184   Constant *P2 = ConstantExpr::getUIToFP(P0, DoubleTy);
185   Constant *P3 = ConstantExpr::getTrunc(P0, Int1Ty);
186   Constant *P4 = ConstantExpr::getPtrToInt(Global2, Int32Ty);
187   Constant *P5 = ConstantExpr::getUIToFP(P4, FloatTy);
188   Constant *P6 = ConstantExpr::getBitCast(P4, VectorType::get(Int16Ty, 2));
189 
190   Constant *One = ConstantInt::get(Int32Ty, 1);
191   Constant *Two = ConstantInt::get(Int64Ty, 2);
192   Constant *Big = ConstantInt::get(getGlobalContext(),
193                                    APInt{256, uint64_t(-1), true});
194   Constant *Elt = ConstantInt::get(Int16Ty, 2015);
195   Constant *Undef16  = UndefValue::get(Int16Ty);
196   Constant *Undef64  = UndefValue::get(Int64Ty);
197   Constant *UndefV16 = UndefValue::get(P6->getType());
198 
199   #define P0STR "ptrtoint (i32** @dummy to i32)"
200   #define P1STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to float)"
201   #define P2STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to double)"
202   #define P3STR "ptrtoint (i32** @dummy to i1)"
203   #define P4STR "ptrtoint (i32** @dummy2 to i32)"
204   #define P5STR "uitofp (i32 ptrtoint (i32** @dummy2 to i32) to float)"
205   #define P6STR "bitcast (i32 ptrtoint (i32** @dummy2 to i32) to <2 x i16>)"
206 
207   CHECK(ConstantExpr::getNeg(P0), "sub i32 0, " P0STR);
208   CHECK(ConstantExpr::getFNeg(P1), "fsub float -0.000000e+00, " P1STR);
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), "add nsw i32 " P0STR ", "
212         P0STR);
213   CHECK(ConstantExpr::getAdd(P0, P0, true, true), "add nuw nsw i32 " P0STR ", "
214         P0STR);
215   CHECK(ConstantExpr::getFAdd(P1, P1), "fadd float " P1STR ", " P1STR);
216   CHECK(ConstantExpr::getSub(P0, P0), "sub i32 " P0STR ", " P0STR);
217   CHECK(ConstantExpr::getFSub(P1, P1), "fsub float " P1STR ", " P1STR);
218   CHECK(ConstantExpr::getMul(P0, P0), "mul i32 " P0STR ", " P0STR);
219   CHECK(ConstantExpr::getFMul(P1, P1), "fmul float " P1STR ", " P1STR);
220   CHECK(ConstantExpr::getUDiv(P0, P0), "udiv i32 " P0STR ", " P0STR);
221   CHECK(ConstantExpr::getSDiv(P0, P0), "sdiv i32 " P0STR ", " P0STR);
222   CHECK(ConstantExpr::getFDiv(P1, P1), "fdiv float " P1STR ", " P1STR);
223   CHECK(ConstantExpr::getURem(P0, P0), "urem i32 " P0STR ", " P0STR);
224   CHECK(ConstantExpr::getSRem(P0, P0), "srem i32 " P0STR ", " P0STR);
225   CHECK(ConstantExpr::getFRem(P1, P1), "frem float " P1STR ", " P1STR);
226   CHECK(ConstantExpr::getAnd(P0, P0), "and i32 " P0STR ", " P0STR);
227   CHECK(ConstantExpr::getOr(P0, P0), "or i32 " P0STR ", " P0STR);
228   CHECK(ConstantExpr::getXor(P0, P0), "xor i32 " P0STR ", " P0STR);
229   CHECK(ConstantExpr::getShl(P0, P0), "shl i32 " P0STR ", " P0STR);
230   CHECK(ConstantExpr::getShl(P0, P0, true), "shl nuw i32 " P0STR ", " P0STR);
231   CHECK(ConstantExpr::getShl(P0, P0, false, true), "shl nsw i32 " P0STR ", "
232         P0STR);
233   CHECK(ConstantExpr::getLShr(P0, P0, false), "lshr i32 " P0STR ", " P0STR);
234   CHECK(ConstantExpr::getLShr(P0, P0, true), "lshr exact i32 " P0STR ", " P0STR);
235   CHECK(ConstantExpr::getAShr(P0, P0, false), "ashr i32 " P0STR ", " P0STR);
236   CHECK(ConstantExpr::getAShr(P0, P0, true), "ashr exact i32 " P0STR ", " P0STR);
237 
238   CHECK(ConstantExpr::getSExt(P0, Int64Ty), "sext i32 " P0STR " to i64");
239   CHECK(ConstantExpr::getZExt(P0, Int64Ty), "zext i32 " P0STR " to i64");
240   CHECK(ConstantExpr::getFPTrunc(P2, FloatTy), "fptrunc double " P2STR
241         " to float");
242   CHECK(ConstantExpr::getFPExtend(P1, DoubleTy), "fpext float " P1STR
243         " to double");
244 
245   CHECK(ConstantExpr::getExactUDiv(P0, P0), "udiv exact i32 " P0STR ", " P0STR);
246 
247   CHECK(ConstantExpr::getSelect(P3, P0, P4), "select i1 " P3STR ", i32 " P0STR
248         ", i32 " P4STR);
249   CHECK(ConstantExpr::getICmp(CmpInst::ICMP_EQ, P0, P4), "icmp eq i32 " P0STR
250         ", " P4STR);
251   CHECK(ConstantExpr::getFCmp(CmpInst::FCMP_ULT, P1, P5), "fcmp ult float "
252         P1STR ", " P5STR);
253 
254   std::vector<Constant*> V;
255   V.push_back(One);
256   // FIXME: getGetElementPtr() actually creates an inbounds ConstantGEP,
257   //        not a normal one!
258   //CHECK(ConstantExpr::getGetElementPtr(Global, V, false),
259   //      "getelementptr i32*, i32** @dummy, i32 1");
260   CHECK(ConstantExpr::getInBoundsGetElementPtr(PointerType::getUnqual(Int32Ty),
261                                                Global, V),
262         "getelementptr inbounds i32*, i32** @dummy, i32 1");
263 
264   CHECK(ConstantExpr::getExtractElement(P6, One), "extractelement <2 x i16> "
265         P6STR ", i32 1");
266 
267   EXPECT_EQ(Undef16, ConstantExpr::getExtractElement(P6, Two));
268   EXPECT_EQ(Undef16, ConstantExpr::getExtractElement(P6, Big));
269   EXPECT_EQ(Undef16, ConstantExpr::getExtractElement(P6, Undef64));
270 
271   EXPECT_EQ(Elt, ConstantExpr::getExtractElement(
272                  ConstantExpr::getInsertElement(P6, Elt, One), One));
273   EXPECT_EQ(UndefV16, ConstantExpr::getInsertElement(P6, Elt, Two));
274   EXPECT_EQ(UndefV16, ConstantExpr::getInsertElement(P6, Elt, Big));
275   EXPECT_EQ(UndefV16, ConstantExpr::getInsertElement(P6, Elt, Undef64));
276 }
277 
278 #ifdef GTEST_HAS_DEATH_TEST
279 #ifndef NDEBUG
TEST(ConstantsTest,ReplaceWithConstantTest)280 TEST(ConstantsTest, ReplaceWithConstantTest) {
281   std::unique_ptr<Module> M(new Module("MyModule", getGlobalContext()));
282 
283   Type *Int32Ty = Type::getInt32Ty(getGlobalContext());
284   Constant *One = ConstantInt::get(Int32Ty, 1);
285 
286   Constant *Global =
287       M->getOrInsertGlobal("dummy", PointerType::getUnqual(Int32Ty));
288   Constant *GEP = ConstantExpr::getGetElementPtr(
289       PointerType::getUnqual(Int32Ty), Global, One);
290   EXPECT_DEATH(Global->replaceAllUsesWith(GEP),
291                "this->replaceAllUsesWith\\(expr\\(this\\)\\) is NOT valid!");
292 }
293 
294 #endif
295 #endif
296 
297 #undef CHECK
298 
TEST(ConstantsTest,ConstantArrayReplaceWithConstant)299 TEST(ConstantsTest, ConstantArrayReplaceWithConstant) {
300   LLVMContext Context;
301   std::unique_ptr<Module> M(new Module("MyModule", Context));
302 
303   Type *IntTy = Type::getInt8Ty(Context);
304   ArrayType *ArrayTy = ArrayType::get(IntTy, 2);
305   Constant *A01Vals[2] = {ConstantInt::get(IntTy, 0),
306                           ConstantInt::get(IntTy, 1)};
307   Constant *A01 = ConstantArray::get(ArrayTy, A01Vals);
308 
309   Constant *Global = new GlobalVariable(*M, IntTy, false,
310                                         GlobalValue::ExternalLinkage, nullptr);
311   Constant *GlobalInt = ConstantExpr::getPtrToInt(Global, IntTy);
312   Constant *A0GVals[2] = {ConstantInt::get(IntTy, 0), GlobalInt};
313   Constant *A0G = ConstantArray::get(ArrayTy, A0GVals);
314   ASSERT_NE(A01, A0G);
315 
316   GlobalVariable *RefArray =
317       new GlobalVariable(*M, ArrayTy, false, GlobalValue::ExternalLinkage, A0G);
318   ASSERT_EQ(A0G, RefArray->getInitializer());
319 
320   GlobalInt->replaceAllUsesWith(ConstantInt::get(IntTy, 1));
321   ASSERT_EQ(A01, RefArray->getInitializer());
322 }
323 
TEST(ConstantsTest,ConstantExprReplaceWithConstant)324 TEST(ConstantsTest, ConstantExprReplaceWithConstant) {
325   LLVMContext Context;
326   std::unique_ptr<Module> M(new Module("MyModule", Context));
327 
328   Type *IntTy = Type::getInt8Ty(Context);
329   Constant *G1 = new GlobalVariable(*M, IntTy, false,
330                                     GlobalValue::ExternalLinkage, nullptr);
331   Constant *G2 = new GlobalVariable(*M, IntTy, false,
332                                     GlobalValue::ExternalLinkage, nullptr);
333   ASSERT_NE(G1, G2);
334 
335   Constant *Int1 = ConstantExpr::getPtrToInt(G1, IntTy);
336   Constant *Int2 = ConstantExpr::getPtrToInt(G2, IntTy);
337   ASSERT_NE(Int1, Int2);
338 
339   GlobalVariable *Ref =
340       new GlobalVariable(*M, IntTy, false, GlobalValue::ExternalLinkage, Int1);
341   ASSERT_EQ(Int1, Ref->getInitializer());
342 
343   G1->replaceAllUsesWith(G2);
344   ASSERT_EQ(Int2, Ref->getInitializer());
345 }
346 
TEST(ConstantsTest,GEPReplaceWithConstant)347 TEST(ConstantsTest, GEPReplaceWithConstant) {
348   LLVMContext Context;
349   std::unique_ptr<Module> M(new Module("MyModule", Context));
350 
351   Type *IntTy = Type::getInt32Ty(Context);
352   Type *PtrTy = PointerType::get(IntTy, 0);
353   auto *C1 = ConstantInt::get(IntTy, 1);
354   auto *Placeholder = new GlobalVariable(
355       *M, IntTy, false, GlobalValue::ExternalWeakLinkage, nullptr);
356   auto *GEP = ConstantExpr::getGetElementPtr(IntTy, Placeholder, C1);
357   ASSERT_EQ(GEP->getOperand(0), Placeholder);
358 
359   auto *Ref =
360       new GlobalVariable(*M, PtrTy, false, GlobalValue::ExternalLinkage, GEP);
361   ASSERT_EQ(GEP, Ref->getInitializer());
362 
363   auto *Global = new GlobalVariable(*M, PtrTy, false,
364                                     GlobalValue::ExternalLinkage, nullptr);
365   auto *Alias = GlobalAlias::create(IntTy, 0, GlobalValue::ExternalLinkage,
366                                     "alias", Global, M.get());
367   Placeholder->replaceAllUsesWith(Alias);
368   ASSERT_EQ(GEP, Ref->getInitializer());
369   ASSERT_EQ(GEP->getOperand(0), Alias);
370 }
371 
TEST(ConstantsTest,AliasCAPI)372 TEST(ConstantsTest, AliasCAPI) {
373   LLVMContext Context;
374   SMDiagnostic Error;
375   std::unique_ptr<Module> M =
376       parseAssemblyString("@g = global i32 42", Error, Context);
377   GlobalVariable *G = M->getGlobalVariable("g");
378   Type *I16Ty = Type::getInt16Ty(Context);
379   Type *I16PTy = PointerType::get(I16Ty, 0);
380   Constant *Aliasee = ConstantExpr::getBitCast(G, I16PTy);
381   LLVMValueRef AliasRef =
382       LLVMAddAlias(wrap(M.get()), wrap(I16PTy), wrap(Aliasee), "a");
383   ASSERT_EQ(unwrap<GlobalAlias>(AliasRef)->getAliasee(), Aliasee);
384 }
385 
getNameOfType(Type * T)386 static std::string getNameOfType(Type *T) {
387   std::string S;
388   raw_string_ostream RSOS(S);
389   T->print(RSOS);
390   return S;
391 }
392 
TEST(ConstantsTest,BuildConstantDataArrays)393 TEST(ConstantsTest, BuildConstantDataArrays) {
394   LLVMContext Context;
395   std::unique_ptr<Module> M(new Module("MyModule", Context));
396 
397   for (Type *T : {Type::getInt8Ty(Context), Type::getInt16Ty(Context),
398                   Type::getInt32Ty(Context), Type::getInt64Ty(Context)}) {
399     ArrayType *ArrayTy = ArrayType::get(T, 2);
400     Constant *Vals[] = {ConstantInt::get(T, 0), ConstantInt::get(T, 1)};
401     Constant *CDV = ConstantArray::get(ArrayTy, Vals);
402     ASSERT_TRUE(dyn_cast<ConstantDataArray>(CDV) != nullptr)
403         << " T = " << getNameOfType(T);
404   }
405 
406   for (Type *T : {Type::getHalfTy(Context), Type::getFloatTy(Context),
407                   Type::getDoubleTy(Context)}) {
408     ArrayType *ArrayTy = ArrayType::get(T, 2);
409     Constant *Vals[] = {ConstantFP::get(T, 0), ConstantFP::get(T, 1)};
410     Constant *CDV = ConstantArray::get(ArrayTy, Vals);
411     ASSERT_TRUE(dyn_cast<ConstantDataArray>(CDV) != nullptr)
412         << " T = " << getNameOfType(T);
413   }
414 }
415 
TEST(ConstantsTest,BuildConstantDataVectors)416 TEST(ConstantsTest, BuildConstantDataVectors) {
417   LLVMContext Context;
418   std::unique_ptr<Module> M(new Module("MyModule", Context));
419 
420   for (Type *T : {Type::getInt8Ty(Context), Type::getInt16Ty(Context),
421                   Type::getInt32Ty(Context), Type::getInt64Ty(Context)}) {
422     Constant *Vals[] = {ConstantInt::get(T, 0), ConstantInt::get(T, 1)};
423     Constant *CDV = ConstantVector::get(Vals);
424     ASSERT_TRUE(dyn_cast<ConstantDataVector>(CDV) != nullptr)
425         << " T = " << getNameOfType(T);
426   }
427 
428   for (Type *T : {Type::getHalfTy(Context), Type::getFloatTy(Context),
429                   Type::getDoubleTy(Context)}) {
430     Constant *Vals[] = {ConstantFP::get(T, 0), ConstantFP::get(T, 1)};
431     Constant *CDV = ConstantVector::get(Vals);
432     ASSERT_TRUE(dyn_cast<ConstantDataVector>(CDV) != nullptr)
433         << " T = " << getNameOfType(T);
434   }
435 }
436 
TEST(ConstantsTest,BitcastToGEP)437 TEST(ConstantsTest, BitcastToGEP) {
438   LLVMContext Context;
439   std::unique_ptr<Module> M(new Module("MyModule", Context));
440 
441   auto *i32 = Type::getInt32Ty(Context);
442   auto *U = StructType::create(Context, "Unsized");
443   Type *EltTys[] = {i32, U};
444   auto *S = StructType::create(EltTys);
445 
446   auto *G = new GlobalVariable(*M, S, false,
447                                GlobalValue::ExternalLinkage, nullptr);
448   auto *PtrTy = PointerType::get(i32, 0);
449   auto *C = ConstantExpr::getBitCast(G, PtrTy);
450   ASSERT_EQ(dyn_cast<ConstantExpr>(C)->getOpcode(),
451             Instruction::BitCast);
452 }
453 
454 }  // end anonymous namespace
455 }  // end namespace llvm
456