1 //===--- CGExprConstant.cpp - Emit LLVM Code from Constant Expressions ----===//
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 // This contains code to emit Constant Expr nodes as LLVM code.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CodeGenFunction.h"
15 #include "CodeGenModule.h"
16 #include "CGObjCRuntime.h"
17 #include "clang/AST/APValue.h"
18 #include "clang/AST/ASTContext.h"
19 #include "clang/AST/StmtVisitor.h"
20 #include "llvm/Constants.h"
21 #include "llvm/Function.h"
22 #include "llvm/GlobalVariable.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Target/TargetData.h"
25 using namespace clang;
26 using namespace CodeGen;
27 
28 namespace  {
29 class VISIBILITY_HIDDEN ConstExprEmitter :
30   public StmtVisitor<ConstExprEmitter, llvm::Constant*> {
31   CodeGenModule &CGM;
32   CodeGenFunction *CGF;
33 public:
34   ConstExprEmitter(CodeGenModule &cgm, CodeGenFunction *cgf)
35     : CGM(cgm), CGF(cgf) {
36   }
37 
38   //===--------------------------------------------------------------------===//
39   //                            Visitor Methods
40   //===--------------------------------------------------------------------===//
41 
42   llvm::Constant *VisitStmt(Stmt *S) {
43     return 0;
44   }
45 
46   llvm::Constant *VisitParenExpr(ParenExpr *PE) {
47     return Visit(PE->getSubExpr());
48   }
49 
50   llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
51     return Visit(E->getInitializer());
52   }
53 
54   llvm::Constant *VisitCastExpr(CastExpr* E) {
55     // GCC cast to union extension
56     if (E->getType()->isUnionType()) {
57       const llvm::Type *Ty = ConvertType(E->getType());
58       Expr *SubExpr = E->getSubExpr();
59       return EmitUnion(CGM.EmitConstantExpr(SubExpr, SubExpr->getType(), CGF),
60                        Ty);
61     }
62     if (CGM.getContext().getCanonicalType(E->getSubExpr()->getType()) ==
63         CGM.getContext().getCanonicalType(E->getType())) {
64       return Visit(E->getSubExpr());
65     }
66     return 0;
67   }
68 
69   llvm::Constant *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
70     return Visit(DAE->getExpr());
71   }
72 
73   llvm::Constant *EmitArrayInitialization(InitListExpr *ILE) {
74     std::vector<llvm::Constant*> Elts;
75     const llvm::ArrayType *AType =
76         cast<llvm::ArrayType>(ConvertType(ILE->getType()));
77     unsigned NumInitElements = ILE->getNumInits();
78     // FIXME: Check for wide strings
79     // FIXME: Check for NumInitElements exactly equal to 1??
80     if (NumInitElements > 0 &&
81         (isa<StringLiteral>(ILE->getInit(0)) ||
82          isa<ObjCEncodeExpr>(ILE->getInit(0))) &&
83         ILE->getType()->getArrayElementTypeNoTypeQual()->isCharType())
84       return Visit(ILE->getInit(0));
85     const llvm::Type *ElemTy = AType->getElementType();
86     unsigned NumElements = AType->getNumElements();
87 
88     // Initialising an array requires us to automatically
89     // initialise any elements that have not been initialised explicitly
90     unsigned NumInitableElts = std::min(NumInitElements, NumElements);
91 
92     // Copy initializer elements.
93     unsigned i = 0;
94     bool RewriteType = false;
95     for (; i < NumInitableElts; ++i) {
96       Expr *Init = ILE->getInit(i);
97       llvm::Constant *C = CGM.EmitConstantExpr(Init, Init->getType(), CGF);
98       if (!C)
99         return 0;
100       RewriteType |= (C->getType() != ElemTy);
101       Elts.push_back(C);
102     }
103 
104     // Initialize remaining array elements.
105     for (; i < NumElements; ++i)
106       Elts.push_back(llvm::Constant::getNullValue(ElemTy));
107 
108     if (RewriteType) {
109       // FIXME: Try to avoid packing the array
110       std::vector<const llvm::Type*> Types;
111       for (unsigned i = 0; i < Elts.size(); ++i)
112         Types.push_back(Elts[i]->getType());
113       const llvm::StructType *SType = llvm::StructType::get(Types, true);
114       return llvm::ConstantStruct::get(SType, Elts);
115     }
116 
117     return llvm::ConstantArray::get(AType, Elts);
118   }
119 
120   void InsertBitfieldIntoStruct(std::vector<llvm::Constant*>& Elts,
121                                 FieldDecl* Field, Expr* E) {
122     // Calculate the value to insert
123     llvm::Constant *C = CGM.EmitConstantExpr(E, Field->getType(), CGF);
124     if (!C)
125       return;
126 
127     llvm::ConstantInt *CI = dyn_cast<llvm::ConstantInt>(C);
128     if (!CI) {
129       CGM.ErrorUnsupported(E, "bitfield initialization");
130       return;
131     }
132     llvm::APInt V = CI->getValue();
133 
134     // Calculate information about the relevant field
135     const llvm::Type* Ty = CI->getType();
136     const llvm::TargetData &TD = CGM.getTypes().getTargetData();
137     unsigned size = TD.getTypePaddedSizeInBits(Ty);
138     unsigned fieldOffset = CGM.getTypes().getLLVMFieldNo(Field) * size;
139     CodeGenTypes::BitFieldInfo bitFieldInfo =
140         CGM.getTypes().getBitFieldInfo(Field);
141     fieldOffset += bitFieldInfo.Begin;
142 
143     // Find where to start the insertion
144     // FIXME: This is O(n^2) in the number of bit-fields!
145     // FIXME: This won't work if the struct isn't completely packed!
146     unsigned offset = 0, i = 0;
147     while (offset < (fieldOffset & -8))
148       offset += TD.getTypePaddedSizeInBits(Elts[i++]->getType());
149 
150     // Advance over 0 sized elements (must terminate in bounds since
151     // the bitfield must have a size).
152     while (TD.getTypePaddedSizeInBits(Elts[i]->getType()) == 0)
153       ++i;
154 
155     // Promote the size of V if necessary
156     // FIXME: This should never occur, but currently it can because
157     // initializer constants are cast to bool, and because clang is
158     // not enforcing bitfield width limits.
159     if (bitFieldInfo.Size > V.getBitWidth())
160       V.zext(bitFieldInfo.Size);
161 
162     // Insert the bits into the struct
163     // FIXME: This algorthm is only correct on X86!
164     // FIXME: THis algorthm assumes bit-fields only have byte-size elements!
165     unsigned bitsToInsert = bitFieldInfo.Size;
166     unsigned curBits = std::min(8 - (fieldOffset & 7), bitsToInsert);
167     unsigned byte = V.getLoBits(curBits).getZExtValue() << (fieldOffset & 7);
168     do {
169       llvm::Constant* byteC = llvm::ConstantInt::get(llvm::Type::Int8Ty, byte);
170       Elts[i] = llvm::ConstantExpr::getOr(Elts[i], byteC);
171       ++i;
172       V = V.lshr(curBits);
173       bitsToInsert -= curBits;
174 
175       if (!bitsToInsert)
176         break;
177 
178       curBits = bitsToInsert > 8 ? 8 : bitsToInsert;
179       byte = V.getLoBits(curBits).getZExtValue();
180     } while (true);
181   }
182 
183   llvm::Constant *EmitStructInitialization(InitListExpr *ILE) {
184     const llvm::StructType *SType =
185         cast<llvm::StructType>(ConvertType(ILE->getType()));
186     RecordDecl *RD = ILE->getType()->getAsRecordType()->getDecl();
187     std::vector<llvm::Constant*> Elts;
188 
189     // Initialize the whole structure to zero.
190     for (unsigned i = 0; i < SType->getNumElements(); ++i) {
191       const llvm::Type *FieldTy = SType->getElementType(i);
192       Elts.push_back(llvm::Constant::getNullValue(FieldTy));
193     }
194 
195     // Copy initializer elements. Skip padding fields.
196     unsigned EltNo = 0;  // Element no in ILE
197     int FieldNo = 0; // Field no in RecordDecl
198     bool RewriteType = false;
199     for (RecordDecl::field_iterator Field = RD->field_begin(CGM.getContext()),
200                                  FieldEnd = RD->field_end(CGM.getContext());
201          EltNo < ILE->getNumInits() && Field != FieldEnd; ++Field) {
202       FieldNo++;
203       if (!Field->getIdentifier())
204         continue;
205 
206       if (Field->isBitField()) {
207         InsertBitfieldIntoStruct(Elts, *Field, ILE->getInit(EltNo));
208       } else {
209         unsigned FieldNo = CGM.getTypes().getLLVMFieldNo(*Field);
210         llvm::Constant *C = CGM.EmitConstantExpr(ILE->getInit(EltNo),
211                                                  Field->getType(), CGF);
212         if (!C) return 0;
213         RewriteType |= (C->getType() != Elts[FieldNo]->getType());
214         Elts[FieldNo] = C;
215       }
216       EltNo++;
217     }
218 
219     if (RewriteType) {
220       // FIXME: Make this work for non-packed structs
221       assert(SType->isPacked() && "Cannot recreate unpacked structs");
222       std::vector<const llvm::Type*> Types;
223       for (unsigned i = 0; i < Elts.size(); ++i)
224         Types.push_back(Elts[i]->getType());
225       SType = llvm::StructType::get(Types, true);
226     }
227 
228     return llvm::ConstantStruct::get(SType, Elts);
229   }
230 
231   llvm::Constant *EmitUnion(llvm::Constant *C, const llvm::Type *Ty) {
232     if (!C)
233       return 0;
234 
235     // Build a struct with the union sub-element as the first member,
236     // and padded to the appropriate size
237     std::vector<llvm::Constant*> Elts;
238     std::vector<const llvm::Type*> Types;
239     Elts.push_back(C);
240     Types.push_back(C->getType());
241     unsigned CurSize = CGM.getTargetData().getTypePaddedSize(C->getType());
242     unsigned TotalSize = CGM.getTargetData().getTypePaddedSize(Ty);
243     while (CurSize < TotalSize) {
244       Elts.push_back(llvm::Constant::getNullValue(llvm::Type::Int8Ty));
245       Types.push_back(llvm::Type::Int8Ty);
246       CurSize++;
247     }
248 
249     // This always generates a packed struct
250     // FIXME: Try to generate an unpacked struct when we can
251     llvm::StructType* STy = llvm::StructType::get(Types, true);
252     return llvm::ConstantStruct::get(STy, Elts);
253   }
254 
255   llvm::Constant *EmitUnionInitialization(InitListExpr *ILE) {
256     const llvm::Type *Ty = ConvertType(ILE->getType());
257 
258     // If this is an empty initializer list, we value-initialize the
259     // union.
260     if (ILE->getNumInits() == 0)
261       return llvm::Constant::getNullValue(Ty);
262 
263     FieldDecl* curField = ILE->getInitializedFieldInUnion();
264     if (!curField) {
265       // There's no field to initialize, so value-initialize the union.
266 #ifndef NDEBUG
267       // Make sure that it's really an empty and not a failure of
268       // semantic analysis.
269       RecordDecl *RD = ILE->getType()->getAsRecordType()->getDecl();
270       for (RecordDecl::field_iterator Field = RD->field_begin(CGM.getContext()),
271                                    FieldEnd = RD->field_end(CGM.getContext());
272            Field != FieldEnd; ++Field)
273         assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
274 #endif
275       return llvm::Constant::getNullValue(Ty);
276     }
277 
278     if (curField->isBitField()) {
279       // Create a dummy struct for bit-field insertion
280       unsigned NumElts = CGM.getTargetData().getTypePaddedSize(Ty) / 8;
281       llvm::Constant* NV = llvm::Constant::getNullValue(llvm::Type::Int8Ty);
282       std::vector<llvm::Constant*> Elts(NumElts, NV);
283 
284       InsertBitfieldIntoStruct(Elts, curField, ILE->getInit(0));
285       const llvm::ArrayType *RetTy =
286           llvm::ArrayType::get(NV->getType(), NumElts);
287       return llvm::ConstantArray::get(RetTy, Elts);
288     }
289 
290     Expr *Init = ILE->getInit(0);
291     return EmitUnion(CGM.EmitConstantExpr(Init, Init->getType(), CGF), Ty);
292   }
293 
294   llvm::Constant *EmitVectorInitialization(InitListExpr *ILE) {
295     const llvm::VectorType *VType =
296         cast<llvm::VectorType>(ConvertType(ILE->getType()));
297     const llvm::Type *ElemTy = VType->getElementType();
298     std::vector<llvm::Constant*> Elts;
299     unsigned NumElements = VType->getNumElements();
300     unsigned NumInitElements = ILE->getNumInits();
301 
302     unsigned NumInitableElts = std::min(NumInitElements, NumElements);
303 
304     // Copy initializer elements.
305     unsigned i = 0;
306     for (; i < NumInitableElts; ++i) {
307       Expr *Init = ILE->getInit(i);
308       llvm::Constant *C = CGM.EmitConstantExpr(Init, Init->getType(), CGF);
309       if (!C)
310         return 0;
311       Elts.push_back(C);
312     }
313 
314     for (; i < NumElements; ++i)
315       Elts.push_back(llvm::Constant::getNullValue(ElemTy));
316 
317     return llvm::ConstantVector::get(VType, Elts);
318   }
319 
320   llvm::Constant *VisitImplicitValueInitExpr(ImplicitValueInitExpr* E) {
321     const llvm::Type* RetTy = CGM.getTypes().ConvertType(E->getType());
322     return llvm::Constant::getNullValue(RetTy);
323   }
324 
325   llvm::Constant *VisitInitListExpr(InitListExpr *ILE) {
326     if (ILE->getType()->isScalarType()) {
327       // We have a scalar in braces. Just use the first element.
328       if (ILE->getNumInits() > 0) {
329         Expr *Init = ILE->getInit(0);
330         return CGM.EmitConstantExpr(Init, Init->getType(), CGF);
331       }
332       const llvm::Type* RetTy = CGM.getTypes().ConvertType(ILE->getType());
333       return llvm::Constant::getNullValue(RetTy);
334     }
335 
336     if (ILE->getType()->isArrayType())
337       return EmitArrayInitialization(ILE);
338 
339     if (ILE->getType()->isStructureType())
340       return EmitStructInitialization(ILE);
341 
342     if (ILE->getType()->isUnionType())
343       return EmitUnionInitialization(ILE);
344 
345     if (ILE->getType()->isVectorType())
346       return EmitVectorInitialization(ILE);
347 
348     assert(0 && "Unable to handle InitListExpr");
349     // Get rid of control reaches end of void function warning.
350     // Not reached.
351     return 0;
352   }
353 
354   llvm::Constant *VisitStringLiteral(StringLiteral *E) {
355     assert(!E->getType()->isPointerType() && "Strings are always arrays");
356 
357     // This must be a string initializing an array in a static initializer.
358     // Don't emit it as the address of the string, emit the string data itself
359     // as an inline array.
360     return llvm::ConstantArray::get(CGM.GetStringForStringLiteral(E), false);
361   }
362 
363   llvm::Constant *VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
364     // This must be an @encode initializing an array in a static initializer.
365     // Don't emit it as the address of the string, emit the string data itself
366     // as an inline array.
367     std::string Str;
368     CGM.getContext().getObjCEncodingForType(E->getEncodedType(), Str);
369     const ConstantArrayType *CAT = cast<ConstantArrayType>(E->getType());
370 
371     // Resize the string to the right size, adding zeros at the end, or
372     // truncating as needed.
373     Str.resize(CAT->getSize().getZExtValue(), '\0');
374     return llvm::ConstantArray::get(Str, false);
375   }
376 
377   llvm::Constant *VisitUnaryExtension(const UnaryOperator *E) {
378     return Visit(E->getSubExpr());
379   }
380 
381   // Utility methods
382   const llvm::Type *ConvertType(QualType T) {
383     return CGM.getTypes().ConvertType(T);
384   }
385 
386 public:
387   llvm::Constant *EmitLValue(Expr *E) {
388     switch (E->getStmtClass()) {
389     default: break;
390     case Expr::CompoundLiteralExprClass: {
391       // Note that due to the nature of compound literals, this is guaranteed
392       // to be the only use of the variable, so we just generate it here.
393       CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E);
394       llvm::Constant* C = Visit(CLE->getInitializer());
395       // FIXME: "Leaked" on failure.
396       if (C)
397         C = new llvm::GlobalVariable(C->getType(),
398                                      E->getType().isConstQualified(),
399                                      llvm::GlobalValue::InternalLinkage,
400                                      C, ".compoundliteral", &CGM.getModule());
401       return C;
402     }
403     case Expr::DeclRefExprClass:
404     case Expr::QualifiedDeclRefExprClass: {
405       NamedDecl *Decl = cast<DeclRefExpr>(E)->getDecl();
406       if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Decl))
407         return CGM.GetAddrOfFunction(FD);
408       if (const VarDecl* VD = dyn_cast<VarDecl>(Decl)) {
409         // We can never refer to a variable with local storage.
410         if (!VD->hasLocalStorage()) {
411           if (VD->isFileVarDecl() || VD->hasExternalStorage())
412             return CGM.GetAddrOfGlobalVar(VD);
413           else if (VD->isBlockVarDecl()) {
414             assert(CGF && "Can't access static local vars without CGF");
415             return CGF->GetAddrOfStaticLocalVar(VD);
416           }
417         }
418       }
419       break;
420     }
421     case Expr::StringLiteralClass:
422       return CGM.GetAddrOfConstantStringFromLiteral(cast<StringLiteral>(E));
423     case Expr::ObjCEncodeExprClass:
424       return CGM.GetAddrOfConstantStringFromObjCEncode(cast<ObjCEncodeExpr>(E));
425     case Expr::ObjCStringLiteralClass: {
426       ObjCStringLiteral* SL = cast<ObjCStringLiteral>(E);
427       llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(SL);
428       return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
429     }
430     case Expr::PredefinedExprClass: {
431       // __func__/__FUNCTION__ -> "".  __PRETTY_FUNCTION__ -> "top level".
432       std::string Str;
433       if (cast<PredefinedExpr>(E)->getIdentType() ==
434           PredefinedExpr::PrettyFunction)
435         Str = "top level";
436 
437       return CGM.GetAddrOfConstantCString(Str, ".tmp");
438     }
439     case Expr::AddrLabelExprClass: {
440       assert(CGF && "Invalid address of label expression outside function.");
441       unsigned id = CGF->GetIDForAddrOfLabel(cast<AddrLabelExpr>(E)->getLabel());
442       llvm::Constant *C = llvm::ConstantInt::get(llvm::Type::Int32Ty, id);
443       return llvm::ConstantExpr::getIntToPtr(C, ConvertType(E->getType()));
444     }
445     case Expr::CallExprClass: {
446       CallExpr* CE = cast<CallExpr>(E);
447       if (CE->isBuiltinCall(CGM.getContext()) !=
448             Builtin::BI__builtin___CFStringMakeConstantString)
449         break;
450       const Expr *Arg = CE->getArg(0)->IgnoreParenCasts();
451       const StringLiteral *Literal = cast<StringLiteral>(Arg);
452       // FIXME: need to deal with UCN conversion issues.
453       return CGM.GetAddrOfConstantCFString(Literal);
454     }
455     case Expr::BlockExprClass: {
456       std::string FunctionName;
457       if (CGF)
458         FunctionName = CGF->CurFn->getName();
459       else
460         FunctionName = "global";
461 
462       return CGM.GetAddrOfGlobalBlock(cast<BlockExpr>(E), FunctionName.c_str());
463     }
464     }
465 
466     return 0;
467   }
468 };
469 
470 }  // end anonymous namespace.
471 
472 llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E,
473                                                 QualType DestType,
474                                                 CodeGenFunction *CGF) {
475   Expr::EvalResult Result;
476 
477   bool Success = false;
478 
479   if (DestType->isReferenceType()) {
480     // If the destination type is a reference type, we need to evaluate it
481     // as an lvalue.
482     if (E->EvaluateAsLValue(Result, Context)) {
483       if (const Expr *LVBase = Result.Val.getLValueBase()) {
484         if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) {
485           const ValueDecl *VD = cast<ValueDecl>(DRE->getDecl());
486 
487           // We can only initialize a reference with an lvalue if the lvalue
488           // is not a reference itself.
489           Success = !VD->getType()->isReferenceType();
490         }
491       }
492     }
493   } else
494     Success = E->Evaluate(Result, Context);
495 
496   if (Success) {
497     assert(!Result.HasSideEffects &&
498            "Constant expr should not have any side effects!");
499     switch (Result.Val.getKind()) {
500     case APValue::Uninitialized:
501       assert(0 && "Constant expressions should be initialized.");
502       return 0;
503     case APValue::LValue: {
504       const llvm::Type *DestTy = getTypes().ConvertTypeForMem(DestType);
505       llvm::Constant *Offset =
506         llvm::ConstantInt::get(llvm::Type::Int64Ty,
507                                Result.Val.getLValueOffset());
508 
509       llvm::Constant *C;
510       if (const Expr *LVBase = Result.Val.getLValueBase()) {
511         C = ConstExprEmitter(*this, CGF).EmitLValue(const_cast<Expr*>(LVBase));
512 
513         // Apply offset if necessary.
514         if (!Offset->isNullValue()) {
515           const llvm::Type *Type =
516             llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
517           llvm::Constant *Casted = llvm::ConstantExpr::getBitCast(C, Type);
518           Casted = llvm::ConstantExpr::getGetElementPtr(Casted, &Offset, 1);
519           C = llvm::ConstantExpr::getBitCast(Casted, C->getType());
520         }
521 
522         // Convert to the appropriate type; this could be an lvalue for
523         // an integer.
524         if (isa<llvm::PointerType>(DestTy))
525           return llvm::ConstantExpr::getBitCast(C, DestTy);
526 
527         return llvm::ConstantExpr::getPtrToInt(C, DestTy);
528       } else {
529         C = Offset;
530 
531         // Convert to the appropriate type; this could be an lvalue for
532         // an integer.
533         if (isa<llvm::PointerType>(DestTy))
534           return llvm::ConstantExpr::getIntToPtr(C, DestTy);
535 
536         // If the types don't match this should only be a truncate.
537         if (C->getType() != DestTy)
538           return llvm::ConstantExpr::getTrunc(C, DestTy);
539 
540         return C;
541       }
542     }
543     case APValue::Int: {
544       llvm::Constant *C = llvm::ConstantInt::get(Result.Val.getInt());
545 
546       if (C->getType() == llvm::Type::Int1Ty) {
547         const llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType());
548         C = llvm::ConstantExpr::getZExt(C, BoolTy);
549       }
550       return C;
551     }
552     case APValue::ComplexInt: {
553       llvm::Constant *Complex[2];
554 
555       Complex[0] = llvm::ConstantInt::get(Result.Val.getComplexIntReal());
556       Complex[1] = llvm::ConstantInt::get(Result.Val.getComplexIntImag());
557 
558       return llvm::ConstantStruct::get(Complex, 2);
559     }
560     case APValue::Float:
561       return llvm::ConstantFP::get(Result.Val.getFloat());
562     case APValue::ComplexFloat: {
563       llvm::Constant *Complex[2];
564 
565       Complex[0] = llvm::ConstantFP::get(Result.Val.getComplexFloatReal());
566       Complex[1] = llvm::ConstantFP::get(Result.Val.getComplexFloatImag());
567 
568       return llvm::ConstantStruct::get(Complex, 2);
569     }
570     case APValue::Vector: {
571       llvm::SmallVector<llvm::Constant *, 4> Inits;
572       unsigned NumElts = Result.Val.getVectorLength();
573 
574       for (unsigned i = 0; i != NumElts; ++i) {
575         APValue &Elt = Result.Val.getVectorElt(i);
576         if (Elt.isInt())
577           Inits.push_back(llvm::ConstantInt::get(Elt.getInt()));
578         else
579           Inits.push_back(llvm::ConstantFP::get(Elt.getFloat()));
580       }
581       return llvm::ConstantVector::get(&Inits[0], Inits.size());
582     }
583     }
584   }
585 
586   llvm::Constant* C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E));
587   if (C && C->getType() == llvm::Type::Int1Ty) {
588     const llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType());
589     C = llvm::ConstantExpr::getZExt(C, BoolTy);
590   }
591   return C;
592 }
593