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 "CGCXXABI.h"
16 #include "CGObjCRuntime.h"
17 #include "CGRecordLayout.h"
18 #include "CodeGenModule.h"
19 #include "clang/AST/APValue.h"
20 #include "clang/AST/ASTContext.h"
21 #include "clang/AST/RecordLayout.h"
22 #include "clang/AST/StmtVisitor.h"
23 #include "clang/Basic/Builtins.h"
24 #include "llvm/IR/Constants.h"
25 #include "llvm/IR/DataLayout.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/IR/GlobalVariable.h"
28 using namespace clang;
29 using namespace CodeGen;
30
31 //===----------------------------------------------------------------------===//
32 // ConstStructBuilder
33 //===----------------------------------------------------------------------===//
34
35 namespace {
36 class ConstStructBuilder {
37 CodeGenModule &CGM;
38 CodeGenFunction *CGF;
39
40 bool Packed;
41 CharUnits NextFieldOffsetInChars;
42 CharUnits LLVMStructAlignment;
43 SmallVector<llvm::Constant *, 32> Elements;
44 public:
45 static llvm::Constant *BuildStruct(CodeGenModule &CGM, CodeGenFunction *CGF,
46 InitListExpr *ILE);
47 static llvm::Constant *BuildStruct(CodeGenModule &CGM, CodeGenFunction *CGF,
48 const APValue &Value, QualType ValTy);
49
50 private:
ConstStructBuilder(CodeGenModule & CGM,CodeGenFunction * CGF)51 ConstStructBuilder(CodeGenModule &CGM, CodeGenFunction *CGF)
52 : CGM(CGM), CGF(CGF), Packed(false),
53 NextFieldOffsetInChars(CharUnits::Zero()),
54 LLVMStructAlignment(CharUnits::One()) { }
55
56 void AppendVTablePointer(BaseSubobject Base, llvm::Constant *VTable,
57 const CXXRecordDecl *VTableClass);
58
59 void AppendField(const FieldDecl *Field, uint64_t FieldOffset,
60 llvm::Constant *InitExpr);
61
62 void AppendBytes(CharUnits FieldOffsetInChars, llvm::Constant *InitCst);
63
64 void AppendBitField(const FieldDecl *Field, uint64_t FieldOffset,
65 llvm::ConstantInt *InitExpr);
66
67 void AppendPadding(CharUnits PadSize);
68
69 void AppendTailPadding(CharUnits RecordSize);
70
71 void ConvertStructToPacked();
72
73 bool Build(InitListExpr *ILE);
74 void Build(const APValue &Val, const RecordDecl *RD, bool IsPrimaryBase,
75 llvm::Constant *VTable, const CXXRecordDecl *VTableClass,
76 CharUnits BaseOffset);
77 llvm::Constant *Finalize(QualType Ty);
78
getAlignment(const llvm::Constant * C) const79 CharUnits getAlignment(const llvm::Constant *C) const {
80 if (Packed) return CharUnits::One();
81 return CharUnits::fromQuantity(
82 CGM.getDataLayout().getABITypeAlignment(C->getType()));
83 }
84
getSizeInChars(const llvm::Constant * C) const85 CharUnits getSizeInChars(const llvm::Constant *C) const {
86 return CharUnits::fromQuantity(
87 CGM.getDataLayout().getTypeAllocSize(C->getType()));
88 }
89 };
90
AppendVTablePointer(BaseSubobject Base,llvm::Constant * VTable,const CXXRecordDecl * VTableClass)91 void ConstStructBuilder::AppendVTablePointer(BaseSubobject Base,
92 llvm::Constant *VTable,
93 const CXXRecordDecl *VTableClass) {
94 // Find the appropriate vtable within the vtable group.
95 uint64_t AddressPoint =
96 CGM.getVTableContext().getVTableLayout(VTableClass).getAddressPoint(Base);
97 llvm::Value *Indices[] = {
98 llvm::ConstantInt::get(CGM.Int64Ty, 0),
99 llvm::ConstantInt::get(CGM.Int64Ty, AddressPoint)
100 };
101 llvm::Constant *VTableAddressPoint =
102 llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, Indices);
103
104 // Add the vtable at the start of the object.
105 AppendBytes(Base.getBaseOffset(), VTableAddressPoint);
106 }
107
108 void ConstStructBuilder::
AppendField(const FieldDecl * Field,uint64_t FieldOffset,llvm::Constant * InitCst)109 AppendField(const FieldDecl *Field, uint64_t FieldOffset,
110 llvm::Constant *InitCst) {
111 const ASTContext &Context = CGM.getContext();
112
113 CharUnits FieldOffsetInChars = Context.toCharUnitsFromBits(FieldOffset);
114
115 AppendBytes(FieldOffsetInChars, InitCst);
116 }
117
118 void ConstStructBuilder::
AppendBytes(CharUnits FieldOffsetInChars,llvm::Constant * InitCst)119 AppendBytes(CharUnits FieldOffsetInChars, llvm::Constant *InitCst) {
120
121 assert(NextFieldOffsetInChars <= FieldOffsetInChars
122 && "Field offset mismatch!");
123
124 CharUnits FieldAlignment = getAlignment(InitCst);
125
126 // Round up the field offset to the alignment of the field type.
127 CharUnits AlignedNextFieldOffsetInChars =
128 NextFieldOffsetInChars.RoundUpToAlignment(FieldAlignment);
129
130 if (AlignedNextFieldOffsetInChars > FieldOffsetInChars) {
131 assert(!Packed && "Alignment is wrong even with a packed struct!");
132
133 // Convert the struct to a packed struct.
134 ConvertStructToPacked();
135
136 AlignedNextFieldOffsetInChars = NextFieldOffsetInChars;
137 }
138
139 if (AlignedNextFieldOffsetInChars < FieldOffsetInChars) {
140 // We need to append padding.
141 AppendPadding(FieldOffsetInChars - NextFieldOffsetInChars);
142
143 assert(NextFieldOffsetInChars == FieldOffsetInChars &&
144 "Did not add enough padding!");
145
146 AlignedNextFieldOffsetInChars = NextFieldOffsetInChars;
147 }
148
149 // Add the field.
150 Elements.push_back(InitCst);
151 NextFieldOffsetInChars = AlignedNextFieldOffsetInChars +
152 getSizeInChars(InitCst);
153
154 if (Packed)
155 assert(LLVMStructAlignment == CharUnits::One() &&
156 "Packed struct not byte-aligned!");
157 else
158 LLVMStructAlignment = std::max(LLVMStructAlignment, FieldAlignment);
159 }
160
AppendBitField(const FieldDecl * Field,uint64_t FieldOffset,llvm::ConstantInt * CI)161 void ConstStructBuilder::AppendBitField(const FieldDecl *Field,
162 uint64_t FieldOffset,
163 llvm::ConstantInt *CI) {
164 const ASTContext &Context = CGM.getContext();
165 const uint64_t CharWidth = Context.getCharWidth();
166 uint64_t NextFieldOffsetInBits = Context.toBits(NextFieldOffsetInChars);
167 if (FieldOffset > NextFieldOffsetInBits) {
168 // We need to add padding.
169 CharUnits PadSize = Context.toCharUnitsFromBits(
170 llvm::RoundUpToAlignment(FieldOffset - NextFieldOffsetInBits,
171 Context.getTargetInfo().getCharAlign()));
172
173 AppendPadding(PadSize);
174 }
175
176 uint64_t FieldSize = Field->getBitWidthValue(Context);
177
178 llvm::APInt FieldValue = CI->getValue();
179
180 // Promote the size of FieldValue if necessary
181 // FIXME: This should never occur, but currently it can because initializer
182 // constants are cast to bool, and because clang is not enforcing bitfield
183 // width limits.
184 if (FieldSize > FieldValue.getBitWidth())
185 FieldValue = FieldValue.zext(FieldSize);
186
187 // Truncate the size of FieldValue to the bit field size.
188 if (FieldSize < FieldValue.getBitWidth())
189 FieldValue = FieldValue.trunc(FieldSize);
190
191 NextFieldOffsetInBits = Context.toBits(NextFieldOffsetInChars);
192 if (FieldOffset < NextFieldOffsetInBits) {
193 // Either part of the field or the entire field can go into the previous
194 // byte.
195 assert(!Elements.empty() && "Elements can't be empty!");
196
197 unsigned BitsInPreviousByte = NextFieldOffsetInBits - FieldOffset;
198
199 bool FitsCompletelyInPreviousByte =
200 BitsInPreviousByte >= FieldValue.getBitWidth();
201
202 llvm::APInt Tmp = FieldValue;
203
204 if (!FitsCompletelyInPreviousByte) {
205 unsigned NewFieldWidth = FieldSize - BitsInPreviousByte;
206
207 if (CGM.getDataLayout().isBigEndian()) {
208 Tmp = Tmp.lshr(NewFieldWidth);
209 Tmp = Tmp.trunc(BitsInPreviousByte);
210
211 // We want the remaining high bits.
212 FieldValue = FieldValue.trunc(NewFieldWidth);
213 } else {
214 Tmp = Tmp.trunc(BitsInPreviousByte);
215
216 // We want the remaining low bits.
217 FieldValue = FieldValue.lshr(BitsInPreviousByte);
218 FieldValue = FieldValue.trunc(NewFieldWidth);
219 }
220 }
221
222 Tmp = Tmp.zext(CharWidth);
223 if (CGM.getDataLayout().isBigEndian()) {
224 if (FitsCompletelyInPreviousByte)
225 Tmp = Tmp.shl(BitsInPreviousByte - FieldValue.getBitWidth());
226 } else {
227 Tmp = Tmp.shl(CharWidth - BitsInPreviousByte);
228 }
229
230 // 'or' in the bits that go into the previous byte.
231 llvm::Value *LastElt = Elements.back();
232 if (llvm::ConstantInt *Val = dyn_cast<llvm::ConstantInt>(LastElt))
233 Tmp |= Val->getValue();
234 else {
235 assert(isa<llvm::UndefValue>(LastElt));
236 // If there is an undef field that we're adding to, it can either be a
237 // scalar undef (in which case, we just replace it with our field) or it
238 // is an array. If it is an array, we have to pull one byte off the
239 // array so that the other undef bytes stay around.
240 if (!isa<llvm::IntegerType>(LastElt->getType())) {
241 // The undef padding will be a multibyte array, create a new smaller
242 // padding and then an hole for our i8 to get plopped into.
243 assert(isa<llvm::ArrayType>(LastElt->getType()) &&
244 "Expected array padding of undefs");
245 llvm::ArrayType *AT = cast<llvm::ArrayType>(LastElt->getType());
246 assert(AT->getElementType()->isIntegerTy(CharWidth) &&
247 AT->getNumElements() != 0 &&
248 "Expected non-empty array padding of undefs");
249
250 // Remove the padding array.
251 NextFieldOffsetInChars -= CharUnits::fromQuantity(AT->getNumElements());
252 Elements.pop_back();
253
254 // Add the padding back in two chunks.
255 AppendPadding(CharUnits::fromQuantity(AT->getNumElements()-1));
256 AppendPadding(CharUnits::One());
257 assert(isa<llvm::UndefValue>(Elements.back()) &&
258 Elements.back()->getType()->isIntegerTy(CharWidth) &&
259 "Padding addition didn't work right");
260 }
261 }
262
263 Elements.back() = llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp);
264
265 if (FitsCompletelyInPreviousByte)
266 return;
267 }
268
269 while (FieldValue.getBitWidth() > CharWidth) {
270 llvm::APInt Tmp;
271
272 if (CGM.getDataLayout().isBigEndian()) {
273 // We want the high bits.
274 Tmp =
275 FieldValue.lshr(FieldValue.getBitWidth() - CharWidth).trunc(CharWidth);
276 } else {
277 // We want the low bits.
278 Tmp = FieldValue.trunc(CharWidth);
279
280 FieldValue = FieldValue.lshr(CharWidth);
281 }
282
283 Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp));
284 ++NextFieldOffsetInChars;
285
286 FieldValue = FieldValue.trunc(FieldValue.getBitWidth() - CharWidth);
287 }
288
289 assert(FieldValue.getBitWidth() > 0 &&
290 "Should have at least one bit left!");
291 assert(FieldValue.getBitWidth() <= CharWidth &&
292 "Should not have more than a byte left!");
293
294 if (FieldValue.getBitWidth() < CharWidth) {
295 if (CGM.getDataLayout().isBigEndian()) {
296 unsigned BitWidth = FieldValue.getBitWidth();
297
298 FieldValue = FieldValue.zext(CharWidth) << (CharWidth - BitWidth);
299 } else
300 FieldValue = FieldValue.zext(CharWidth);
301 }
302
303 // Append the last element.
304 Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(),
305 FieldValue));
306 ++NextFieldOffsetInChars;
307 }
308
AppendPadding(CharUnits PadSize)309 void ConstStructBuilder::AppendPadding(CharUnits PadSize) {
310 if (PadSize.isZero())
311 return;
312
313 llvm::Type *Ty = CGM.Int8Ty;
314 if (PadSize > CharUnits::One())
315 Ty = llvm::ArrayType::get(Ty, PadSize.getQuantity());
316
317 llvm::Constant *C = llvm::UndefValue::get(Ty);
318 Elements.push_back(C);
319 assert(getAlignment(C) == CharUnits::One() &&
320 "Padding must have 1 byte alignment!");
321
322 NextFieldOffsetInChars += getSizeInChars(C);
323 }
324
AppendTailPadding(CharUnits RecordSize)325 void ConstStructBuilder::AppendTailPadding(CharUnits RecordSize) {
326 assert(NextFieldOffsetInChars <= RecordSize &&
327 "Size mismatch!");
328
329 AppendPadding(RecordSize - NextFieldOffsetInChars);
330 }
331
ConvertStructToPacked()332 void ConstStructBuilder::ConvertStructToPacked() {
333 SmallVector<llvm::Constant *, 16> PackedElements;
334 CharUnits ElementOffsetInChars = CharUnits::Zero();
335
336 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
337 llvm::Constant *C = Elements[i];
338
339 CharUnits ElementAlign = CharUnits::fromQuantity(
340 CGM.getDataLayout().getABITypeAlignment(C->getType()));
341 CharUnits AlignedElementOffsetInChars =
342 ElementOffsetInChars.RoundUpToAlignment(ElementAlign);
343
344 if (AlignedElementOffsetInChars > ElementOffsetInChars) {
345 // We need some padding.
346 CharUnits NumChars =
347 AlignedElementOffsetInChars - ElementOffsetInChars;
348
349 llvm::Type *Ty = CGM.Int8Ty;
350 if (NumChars > CharUnits::One())
351 Ty = llvm::ArrayType::get(Ty, NumChars.getQuantity());
352
353 llvm::Constant *Padding = llvm::UndefValue::get(Ty);
354 PackedElements.push_back(Padding);
355 ElementOffsetInChars += getSizeInChars(Padding);
356 }
357
358 PackedElements.push_back(C);
359 ElementOffsetInChars += getSizeInChars(C);
360 }
361
362 assert(ElementOffsetInChars == NextFieldOffsetInChars &&
363 "Packing the struct changed its size!");
364
365 Elements.swap(PackedElements);
366 LLVMStructAlignment = CharUnits::One();
367 Packed = true;
368 }
369
Build(InitListExpr * ILE)370 bool ConstStructBuilder::Build(InitListExpr *ILE) {
371 RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
372 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
373
374 unsigned FieldNo = 0;
375 unsigned ElementNo = 0;
376
377 for (RecordDecl::field_iterator Field = RD->field_begin(),
378 FieldEnd = RD->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
379 // If this is a union, skip all the fields that aren't being initialized.
380 if (RD->isUnion() && ILE->getInitializedFieldInUnion() != *Field)
381 continue;
382
383 // Don't emit anonymous bitfields, they just affect layout.
384 if (Field->isUnnamedBitfield())
385 continue;
386
387 // Get the initializer. A struct can include fields without initializers,
388 // we just use explicit null values for them.
389 llvm::Constant *EltInit;
390 if (ElementNo < ILE->getNumInits())
391 EltInit = CGM.EmitConstantExpr(ILE->getInit(ElementNo++),
392 Field->getType(), CGF);
393 else
394 EltInit = CGM.EmitNullConstant(Field->getType());
395
396 if (!EltInit)
397 return false;
398
399 if (!Field->isBitField()) {
400 // Handle non-bitfield members.
401 AppendField(*Field, Layout.getFieldOffset(FieldNo), EltInit);
402 } else {
403 // Otherwise we have a bitfield.
404 AppendBitField(*Field, Layout.getFieldOffset(FieldNo),
405 cast<llvm::ConstantInt>(EltInit));
406 }
407 }
408
409 return true;
410 }
411
412 namespace {
413 struct BaseInfo {
BaseInfo__anonf9222f1d0111::__anonf9222f1d0211::BaseInfo414 BaseInfo(const CXXRecordDecl *Decl, CharUnits Offset, unsigned Index)
415 : Decl(Decl), Offset(Offset), Index(Index) {
416 }
417
418 const CXXRecordDecl *Decl;
419 CharUnits Offset;
420 unsigned Index;
421
operator <__anonf9222f1d0111::__anonf9222f1d0211::BaseInfo422 bool operator<(const BaseInfo &O) const { return Offset < O.Offset; }
423 };
424 }
425
Build(const APValue & Val,const RecordDecl * RD,bool IsPrimaryBase,llvm::Constant * VTable,const CXXRecordDecl * VTableClass,CharUnits Offset)426 void ConstStructBuilder::Build(const APValue &Val, const RecordDecl *RD,
427 bool IsPrimaryBase, llvm::Constant *VTable,
428 const CXXRecordDecl *VTableClass,
429 CharUnits Offset) {
430 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
431
432 if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) {
433 // Add a vtable pointer, if we need one and it hasn't already been added.
434 if (CD->isDynamicClass() && !IsPrimaryBase)
435 AppendVTablePointer(BaseSubobject(CD, Offset), VTable, VTableClass);
436
437 // Accumulate and sort bases, in order to visit them in address order, which
438 // may not be the same as declaration order.
439 SmallVector<BaseInfo, 8> Bases;
440 Bases.reserve(CD->getNumBases());
441 unsigned BaseNo = 0;
442 for (CXXRecordDecl::base_class_const_iterator Base = CD->bases_begin(),
443 BaseEnd = CD->bases_end(); Base != BaseEnd; ++Base, ++BaseNo) {
444 assert(!Base->isVirtual() && "should not have virtual bases here");
445 const CXXRecordDecl *BD = Base->getType()->getAsCXXRecordDecl();
446 CharUnits BaseOffset = Layout.getBaseClassOffset(BD);
447 Bases.push_back(BaseInfo(BD, BaseOffset, BaseNo));
448 }
449 std::stable_sort(Bases.begin(), Bases.end());
450
451 for (unsigned I = 0, N = Bases.size(); I != N; ++I) {
452 BaseInfo &Base = Bases[I];
453
454 bool IsPrimaryBase = Layout.getPrimaryBase() == Base.Decl;
455 Build(Val.getStructBase(Base.Index), Base.Decl, IsPrimaryBase,
456 VTable, VTableClass, Offset + Base.Offset);
457 }
458 }
459
460 unsigned FieldNo = 0;
461 uint64_t OffsetBits = CGM.getContext().toBits(Offset);
462
463 for (RecordDecl::field_iterator Field = RD->field_begin(),
464 FieldEnd = RD->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
465 // If this is a union, skip all the fields that aren't being initialized.
466 if (RD->isUnion() && Val.getUnionField() != *Field)
467 continue;
468
469 // Don't emit anonymous bitfields, they just affect layout.
470 if (Field->isUnnamedBitfield())
471 continue;
472
473 // Emit the value of the initializer.
474 const APValue &FieldValue =
475 RD->isUnion() ? Val.getUnionValue() : Val.getStructField(FieldNo);
476 llvm::Constant *EltInit =
477 CGM.EmitConstantValueForMemory(FieldValue, Field->getType(), CGF);
478 assert(EltInit && "EmitConstantValue can't fail");
479
480 if (!Field->isBitField()) {
481 // Handle non-bitfield members.
482 AppendField(*Field, Layout.getFieldOffset(FieldNo) + OffsetBits, EltInit);
483 } else {
484 // Otherwise we have a bitfield.
485 AppendBitField(*Field, Layout.getFieldOffset(FieldNo) + OffsetBits,
486 cast<llvm::ConstantInt>(EltInit));
487 }
488 }
489 }
490
Finalize(QualType Ty)491 llvm::Constant *ConstStructBuilder::Finalize(QualType Ty) {
492 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
493 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
494
495 CharUnits LayoutSizeInChars = Layout.getSize();
496
497 if (NextFieldOffsetInChars > LayoutSizeInChars) {
498 // If the struct is bigger than the size of the record type,
499 // we must have a flexible array member at the end.
500 assert(RD->hasFlexibleArrayMember() &&
501 "Must have flexible array member if struct is bigger than type!");
502
503 // No tail padding is necessary.
504 } else {
505 // Append tail padding if necessary.
506 AppendTailPadding(LayoutSizeInChars);
507
508 CharUnits LLVMSizeInChars =
509 NextFieldOffsetInChars.RoundUpToAlignment(LLVMStructAlignment);
510
511 // Check if we need to convert the struct to a packed struct.
512 if (NextFieldOffsetInChars <= LayoutSizeInChars &&
513 LLVMSizeInChars > LayoutSizeInChars) {
514 assert(!Packed && "Size mismatch!");
515
516 ConvertStructToPacked();
517 assert(NextFieldOffsetInChars <= LayoutSizeInChars &&
518 "Converting to packed did not help!");
519 }
520
521 assert(LayoutSizeInChars == NextFieldOffsetInChars &&
522 "Tail padding mismatch!");
523 }
524
525 // Pick the type to use. If the type is layout identical to the ConvertType
526 // type then use it, otherwise use whatever the builder produced for us.
527 llvm::StructType *STy =
528 llvm::ConstantStruct::getTypeForElements(CGM.getLLVMContext(),
529 Elements, Packed);
530 llvm::Type *ValTy = CGM.getTypes().ConvertType(Ty);
531 if (llvm::StructType *ValSTy = dyn_cast<llvm::StructType>(ValTy)) {
532 if (ValSTy->isLayoutIdentical(STy))
533 STy = ValSTy;
534 }
535
536 llvm::Constant *Result = llvm::ConstantStruct::get(STy, Elements);
537
538 assert(NextFieldOffsetInChars.RoundUpToAlignment(getAlignment(Result)) ==
539 getSizeInChars(Result) && "Size mismatch!");
540
541 return Result;
542 }
543
BuildStruct(CodeGenModule & CGM,CodeGenFunction * CGF,InitListExpr * ILE)544 llvm::Constant *ConstStructBuilder::BuildStruct(CodeGenModule &CGM,
545 CodeGenFunction *CGF,
546 InitListExpr *ILE) {
547 ConstStructBuilder Builder(CGM, CGF);
548
549 if (!Builder.Build(ILE))
550 return 0;
551
552 return Builder.Finalize(ILE->getType());
553 }
554
BuildStruct(CodeGenModule & CGM,CodeGenFunction * CGF,const APValue & Val,QualType ValTy)555 llvm::Constant *ConstStructBuilder::BuildStruct(CodeGenModule &CGM,
556 CodeGenFunction *CGF,
557 const APValue &Val,
558 QualType ValTy) {
559 ConstStructBuilder Builder(CGM, CGF);
560
561 const RecordDecl *RD = ValTy->castAs<RecordType>()->getDecl();
562 const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD);
563 llvm::Constant *VTable = 0;
564 if (CD && CD->isDynamicClass())
565 VTable = CGM.getVTables().GetAddrOfVTable(CD);
566
567 Builder.Build(Val, RD, false, VTable, CD, CharUnits::Zero());
568
569 return Builder.Finalize(ValTy);
570 }
571
572
573 //===----------------------------------------------------------------------===//
574 // ConstExprEmitter
575 //===----------------------------------------------------------------------===//
576
577 /// This class only needs to handle two cases:
578 /// 1) Literals (this is used by APValue emission to emit literals).
579 /// 2) Arrays, structs and unions (outside C++11 mode, we don't currently
580 /// constant fold these types).
581 class ConstExprEmitter :
582 public StmtVisitor<ConstExprEmitter, llvm::Constant*> {
583 CodeGenModule &CGM;
584 CodeGenFunction *CGF;
585 llvm::LLVMContext &VMContext;
586 public:
ConstExprEmitter(CodeGenModule & cgm,CodeGenFunction * cgf)587 ConstExprEmitter(CodeGenModule &cgm, CodeGenFunction *cgf)
588 : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()) {
589 }
590
591 //===--------------------------------------------------------------------===//
592 // Visitor Methods
593 //===--------------------------------------------------------------------===//
594
VisitStmt(Stmt * S)595 llvm::Constant *VisitStmt(Stmt *S) {
596 return 0;
597 }
598
VisitParenExpr(ParenExpr * PE)599 llvm::Constant *VisitParenExpr(ParenExpr *PE) {
600 return Visit(PE->getSubExpr());
601 }
602
603 llvm::Constant *
VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr * PE)604 VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE) {
605 return Visit(PE->getReplacement());
606 }
607
VisitGenericSelectionExpr(GenericSelectionExpr * GE)608 llvm::Constant *VisitGenericSelectionExpr(GenericSelectionExpr *GE) {
609 return Visit(GE->getResultExpr());
610 }
611
VisitChooseExpr(ChooseExpr * CE)612 llvm::Constant *VisitChooseExpr(ChooseExpr *CE) {
613 return Visit(CE->getChosenSubExpr());
614 }
615
VisitCompoundLiteralExpr(CompoundLiteralExpr * E)616 llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
617 return Visit(E->getInitializer());
618 }
619
VisitCastExpr(CastExpr * E)620 llvm::Constant *VisitCastExpr(CastExpr* E) {
621 Expr *subExpr = E->getSubExpr();
622 llvm::Constant *C = CGM.EmitConstantExpr(subExpr, subExpr->getType(), CGF);
623 if (!C) return 0;
624
625 llvm::Type *destType = ConvertType(E->getType());
626
627 switch (E->getCastKind()) {
628 case CK_ToUnion: {
629 // GCC cast to union extension
630 assert(E->getType()->isUnionType() &&
631 "Destination type is not union type!");
632
633 // Build a struct with the union sub-element as the first member,
634 // and padded to the appropriate size
635 SmallVector<llvm::Constant*, 2> Elts;
636 SmallVector<llvm::Type*, 2> Types;
637 Elts.push_back(C);
638 Types.push_back(C->getType());
639 unsigned CurSize = CGM.getDataLayout().getTypeAllocSize(C->getType());
640 unsigned TotalSize = CGM.getDataLayout().getTypeAllocSize(destType);
641
642 assert(CurSize <= TotalSize && "Union size mismatch!");
643 if (unsigned NumPadBytes = TotalSize - CurSize) {
644 llvm::Type *Ty = CGM.Int8Ty;
645 if (NumPadBytes > 1)
646 Ty = llvm::ArrayType::get(Ty, NumPadBytes);
647
648 Elts.push_back(llvm::UndefValue::get(Ty));
649 Types.push_back(Ty);
650 }
651
652 llvm::StructType* STy =
653 llvm::StructType::get(C->getType()->getContext(), Types, false);
654 return llvm::ConstantStruct::get(STy, Elts);
655 }
656
657 case CK_LValueToRValue:
658 case CK_AtomicToNonAtomic:
659 case CK_NonAtomicToAtomic:
660 case CK_NoOp:
661 case CK_ConstructorConversion:
662 return C;
663
664 case CK_Dependent: llvm_unreachable("saw dependent cast!");
665
666 case CK_BuiltinFnToFnPtr:
667 llvm_unreachable("builtin functions are handled elsewhere");
668
669 case CK_ReinterpretMemberPointer:
670 case CK_DerivedToBaseMemberPointer:
671 case CK_BaseToDerivedMemberPointer:
672 return CGM.getCXXABI().EmitMemberPointerConversion(E, C);
673
674 // These will never be supported.
675 case CK_ObjCObjectLValueCast:
676 case CK_ARCProduceObject:
677 case CK_ARCConsumeObject:
678 case CK_ARCReclaimReturnedObject:
679 case CK_ARCExtendBlockObject:
680 case CK_CopyAndAutoreleaseBlockObject:
681 return 0;
682
683 // These don't need to be handled here because Evaluate knows how to
684 // evaluate them in the cases where they can be folded.
685 case CK_BitCast:
686 case CK_ToVoid:
687 case CK_Dynamic:
688 case CK_LValueBitCast:
689 case CK_NullToMemberPointer:
690 case CK_UserDefinedConversion:
691 case CK_CPointerToObjCPointerCast:
692 case CK_BlockPointerToObjCPointerCast:
693 case CK_AnyPointerToBlockPointerCast:
694 case CK_ArrayToPointerDecay:
695 case CK_FunctionToPointerDecay:
696 case CK_BaseToDerived:
697 case CK_DerivedToBase:
698 case CK_UncheckedDerivedToBase:
699 case CK_MemberPointerToBoolean:
700 case CK_VectorSplat:
701 case CK_FloatingRealToComplex:
702 case CK_FloatingComplexToReal:
703 case CK_FloatingComplexToBoolean:
704 case CK_FloatingComplexCast:
705 case CK_FloatingComplexToIntegralComplex:
706 case CK_IntegralRealToComplex:
707 case CK_IntegralComplexToReal:
708 case CK_IntegralComplexToBoolean:
709 case CK_IntegralComplexCast:
710 case CK_IntegralComplexToFloatingComplex:
711 case CK_PointerToIntegral:
712 case CK_PointerToBoolean:
713 case CK_NullToPointer:
714 case CK_IntegralCast:
715 case CK_IntegralToPointer:
716 case CK_IntegralToBoolean:
717 case CK_IntegralToFloating:
718 case CK_FloatingToIntegral:
719 case CK_FloatingToBoolean:
720 case CK_FloatingCast:
721 case CK_ZeroToOCLEvent:
722 return 0;
723 }
724 llvm_unreachable("Invalid CastKind");
725 }
726
VisitCXXDefaultArgExpr(CXXDefaultArgExpr * DAE)727 llvm::Constant *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
728 return Visit(DAE->getExpr());
729 }
730
VisitCXXDefaultInitExpr(CXXDefaultInitExpr * DIE)731 llvm::Constant *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
732 // No need for a DefaultInitExprScope: we don't handle 'this' in a
733 // constant expression.
734 return Visit(DIE->getExpr());
735 }
736
VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr * E)737 llvm::Constant *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
738 return Visit(E->GetTemporaryExpr());
739 }
740
EmitArrayInitialization(InitListExpr * ILE)741 llvm::Constant *EmitArrayInitialization(InitListExpr *ILE) {
742 if (ILE->isStringLiteralInit())
743 return Visit(ILE->getInit(0));
744
745 llvm::ArrayType *AType =
746 cast<llvm::ArrayType>(ConvertType(ILE->getType()));
747 llvm::Type *ElemTy = AType->getElementType();
748 unsigned NumInitElements = ILE->getNumInits();
749 unsigned NumElements = AType->getNumElements();
750
751 // Initialising an array requires us to automatically
752 // initialise any elements that have not been initialised explicitly
753 unsigned NumInitableElts = std::min(NumInitElements, NumElements);
754
755 // Copy initializer elements.
756 std::vector<llvm::Constant*> Elts;
757 Elts.reserve(NumInitableElts + NumElements);
758
759 bool RewriteType = false;
760 for (unsigned i = 0; i < NumInitableElts; ++i) {
761 Expr *Init = ILE->getInit(i);
762 llvm::Constant *C = CGM.EmitConstantExpr(Init, Init->getType(), CGF);
763 if (!C)
764 return 0;
765 RewriteType |= (C->getType() != ElemTy);
766 Elts.push_back(C);
767 }
768
769 // Initialize remaining array elements.
770 // FIXME: This doesn't handle member pointers correctly!
771 llvm::Constant *fillC;
772 if (Expr *filler = ILE->getArrayFiller())
773 fillC = CGM.EmitConstantExpr(filler, filler->getType(), CGF);
774 else
775 fillC = llvm::Constant::getNullValue(ElemTy);
776 if (!fillC)
777 return 0;
778 RewriteType |= (fillC->getType() != ElemTy);
779 Elts.resize(NumElements, fillC);
780
781 if (RewriteType) {
782 // FIXME: Try to avoid packing the array
783 std::vector<llvm::Type*> Types;
784 Types.reserve(NumInitableElts + NumElements);
785 for (unsigned i = 0, e = Elts.size(); i < e; ++i)
786 Types.push_back(Elts[i]->getType());
787 llvm::StructType *SType = llvm::StructType::get(AType->getContext(),
788 Types, true);
789 return llvm::ConstantStruct::get(SType, Elts);
790 }
791
792 return llvm::ConstantArray::get(AType, Elts);
793 }
794
EmitRecordInitialization(InitListExpr * ILE)795 llvm::Constant *EmitRecordInitialization(InitListExpr *ILE) {
796 return ConstStructBuilder::BuildStruct(CGM, CGF, ILE);
797 }
798
VisitImplicitValueInitExpr(ImplicitValueInitExpr * E)799 llvm::Constant *VisitImplicitValueInitExpr(ImplicitValueInitExpr* E) {
800 return CGM.EmitNullConstant(E->getType());
801 }
802
VisitInitListExpr(InitListExpr * ILE)803 llvm::Constant *VisitInitListExpr(InitListExpr *ILE) {
804 if (ILE->getType()->isArrayType())
805 return EmitArrayInitialization(ILE);
806
807 if (ILE->getType()->isRecordType())
808 return EmitRecordInitialization(ILE);
809
810 return 0;
811 }
812
VisitCXXConstructExpr(CXXConstructExpr * E)813 llvm::Constant *VisitCXXConstructExpr(CXXConstructExpr *E) {
814 if (!E->getConstructor()->isTrivial())
815 return 0;
816
817 QualType Ty = E->getType();
818
819 // FIXME: We should not have to call getBaseElementType here.
820 const RecordType *RT =
821 CGM.getContext().getBaseElementType(Ty)->getAs<RecordType>();
822 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
823
824 // If the class doesn't have a trivial destructor, we can't emit it as a
825 // constant expr.
826 if (!RD->hasTrivialDestructor())
827 return 0;
828
829 // Only copy and default constructors can be trivial.
830
831
832 if (E->getNumArgs()) {
833 assert(E->getNumArgs() == 1 && "trivial ctor with > 1 argument");
834 assert(E->getConstructor()->isCopyOrMoveConstructor() &&
835 "trivial ctor has argument but isn't a copy/move ctor");
836
837 Expr *Arg = E->getArg(0);
838 assert(CGM.getContext().hasSameUnqualifiedType(Ty, Arg->getType()) &&
839 "argument to copy ctor is of wrong type");
840
841 return Visit(Arg);
842 }
843
844 return CGM.EmitNullConstant(Ty);
845 }
846
VisitStringLiteral(StringLiteral * E)847 llvm::Constant *VisitStringLiteral(StringLiteral *E) {
848 return CGM.GetConstantArrayFromStringLiteral(E);
849 }
850
VisitObjCEncodeExpr(ObjCEncodeExpr * E)851 llvm::Constant *VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
852 // This must be an @encode initializing an array in a static initializer.
853 // Don't emit it as the address of the string, emit the string data itself
854 // as an inline array.
855 std::string Str;
856 CGM.getContext().getObjCEncodingForType(E->getEncodedType(), Str);
857 const ConstantArrayType *CAT = cast<ConstantArrayType>(E->getType());
858
859 // Resize the string to the right size, adding zeros at the end, or
860 // truncating as needed.
861 Str.resize(CAT->getSize().getZExtValue(), '\0');
862 return llvm::ConstantDataArray::getString(VMContext, Str, false);
863 }
864
VisitUnaryExtension(const UnaryOperator * E)865 llvm::Constant *VisitUnaryExtension(const UnaryOperator *E) {
866 return Visit(E->getSubExpr());
867 }
868
869 // Utility methods
ConvertType(QualType T)870 llvm::Type *ConvertType(QualType T) {
871 return CGM.getTypes().ConvertType(T);
872 }
873
874 public:
EmitLValue(APValue::LValueBase LVBase)875 llvm::Constant *EmitLValue(APValue::LValueBase LVBase) {
876 if (const ValueDecl *Decl = LVBase.dyn_cast<const ValueDecl*>()) {
877 if (Decl->hasAttr<WeakRefAttr>())
878 return CGM.GetWeakRefReference(Decl);
879 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Decl))
880 return CGM.GetAddrOfFunction(FD);
881 if (const VarDecl* VD = dyn_cast<VarDecl>(Decl)) {
882 // We can never refer to a variable with local storage.
883 if (!VD->hasLocalStorage()) {
884 if (VD->isFileVarDecl() || VD->hasExternalStorage())
885 return CGM.GetAddrOfGlobalVar(VD);
886 else if (VD->isLocalVarDecl())
887 return CGM.getStaticLocalDeclAddress(VD);
888 }
889 }
890 return 0;
891 }
892
893 Expr *E = const_cast<Expr*>(LVBase.get<const Expr*>());
894 switch (E->getStmtClass()) {
895 default: break;
896 case Expr::CompoundLiteralExprClass: {
897 // Note that due to the nature of compound literals, this is guaranteed
898 // to be the only use of the variable, so we just generate it here.
899 CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E);
900 llvm::Constant* C = CGM.EmitConstantExpr(CLE->getInitializer(),
901 CLE->getType(), CGF);
902 // FIXME: "Leaked" on failure.
903 if (C)
904 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(),
905 E->getType().isConstant(CGM.getContext()),
906 llvm::GlobalValue::InternalLinkage,
907 C, ".compoundliteral", 0,
908 llvm::GlobalVariable::NotThreadLocal,
909 CGM.getContext().getTargetAddressSpace(E->getType()));
910 return C;
911 }
912 case Expr::StringLiteralClass:
913 return CGM.GetAddrOfConstantStringFromLiteral(cast<StringLiteral>(E));
914 case Expr::ObjCEncodeExprClass:
915 return CGM.GetAddrOfConstantStringFromObjCEncode(cast<ObjCEncodeExpr>(E));
916 case Expr::ObjCStringLiteralClass: {
917 ObjCStringLiteral* SL = cast<ObjCStringLiteral>(E);
918 llvm::Constant *C =
919 CGM.getObjCRuntime().GenerateConstantString(SL->getString());
920 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
921 }
922 case Expr::PredefinedExprClass: {
923 unsigned Type = cast<PredefinedExpr>(E)->getIdentType();
924 if (CGF) {
925 LValue Res = CGF->EmitPredefinedLValue(cast<PredefinedExpr>(E));
926 return cast<llvm::Constant>(Res.getAddress());
927 } else if (Type == PredefinedExpr::PrettyFunction) {
928 return CGM.GetAddrOfConstantCString("top level", ".tmp");
929 }
930
931 return CGM.GetAddrOfConstantCString("", ".tmp");
932 }
933 case Expr::AddrLabelExprClass: {
934 assert(CGF && "Invalid address of label expression outside function.");
935 llvm::Constant *Ptr =
936 CGF->GetAddrOfLabel(cast<AddrLabelExpr>(E)->getLabel());
937 return llvm::ConstantExpr::getBitCast(Ptr, ConvertType(E->getType()));
938 }
939 case Expr::CallExprClass: {
940 CallExpr* CE = cast<CallExpr>(E);
941 unsigned builtin = CE->isBuiltinCall();
942 if (builtin !=
943 Builtin::BI__builtin___CFStringMakeConstantString &&
944 builtin !=
945 Builtin::BI__builtin___NSStringMakeConstantString)
946 break;
947 const Expr *Arg = CE->getArg(0)->IgnoreParenCasts();
948 const StringLiteral *Literal = cast<StringLiteral>(Arg);
949 if (builtin ==
950 Builtin::BI__builtin___NSStringMakeConstantString) {
951 return CGM.getObjCRuntime().GenerateConstantString(Literal);
952 }
953 // FIXME: need to deal with UCN conversion issues.
954 return CGM.GetAddrOfConstantCFString(Literal);
955 }
956 case Expr::BlockExprClass: {
957 std::string FunctionName;
958 if (CGF)
959 FunctionName = CGF->CurFn->getName();
960 else
961 FunctionName = "global";
962
963 return CGM.GetAddrOfGlobalBlock(cast<BlockExpr>(E), FunctionName.c_str());
964 }
965 case Expr::CXXTypeidExprClass: {
966 CXXTypeidExpr *Typeid = cast<CXXTypeidExpr>(E);
967 QualType T;
968 if (Typeid->isTypeOperand())
969 T = Typeid->getTypeOperand();
970 else
971 T = Typeid->getExprOperand()->getType();
972 return CGM.GetAddrOfRTTIDescriptor(T);
973 }
974 case Expr::CXXUuidofExprClass: {
975 return CGM.GetAddrOfUuidDescriptor(cast<CXXUuidofExpr>(E));
976 }
977 case Expr::MaterializeTemporaryExprClass: {
978 MaterializeTemporaryExpr *MTE = cast<MaterializeTemporaryExpr>(E);
979 assert(MTE->getStorageDuration() == SD_Static);
980 SmallVector<const Expr *, 2> CommaLHSs;
981 SmallVector<SubobjectAdjustment, 2> Adjustments;
982 const Expr *Inner = MTE->GetTemporaryExpr()
983 ->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments);
984 return CGM.GetAddrOfGlobalTemporary(MTE, Inner);
985 }
986 }
987
988 return 0;
989 }
990 };
991
992 } // end anonymous namespace.
993
EmitConstantInit(const VarDecl & D,CodeGenFunction * CGF)994 llvm::Constant *CodeGenModule::EmitConstantInit(const VarDecl &D,
995 CodeGenFunction *CGF) {
996 // Make a quick check if variable can be default NULL initialized
997 // and avoid going through rest of code which may do, for c++11,
998 // initialization of memory to all NULLs.
999 if (!D.hasLocalStorage()) {
1000 QualType Ty = D.getType();
1001 if (Ty->isArrayType())
1002 Ty = Context.getBaseElementType(Ty);
1003 if (Ty->isRecordType())
1004 if (const CXXConstructExpr *E =
1005 dyn_cast_or_null<CXXConstructExpr>(D.getInit())) {
1006 const CXXConstructorDecl *CD = E->getConstructor();
1007 if (CD->isTrivial() && CD->isDefaultConstructor())
1008 return EmitNullConstant(D.getType());
1009 }
1010 }
1011
1012 if (const APValue *Value = D.evaluateValue())
1013 return EmitConstantValueForMemory(*Value, D.getType(), CGF);
1014
1015 // FIXME: Implement C++11 [basic.start.init]p2: if the initializer of a
1016 // reference is a constant expression, and the reference binds to a temporary,
1017 // then constant initialization is performed. ConstExprEmitter will
1018 // incorrectly emit a prvalue constant in this case, and the calling code
1019 // interprets that as the (pointer) value of the reference, rather than the
1020 // desired value of the referee.
1021 if (D.getType()->isReferenceType())
1022 return 0;
1023
1024 const Expr *E = D.getInit();
1025 assert(E && "No initializer to emit");
1026
1027 llvm::Constant* C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E));
1028 if (C && C->getType()->isIntegerTy(1)) {
1029 llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType());
1030 C = llvm::ConstantExpr::getZExt(C, BoolTy);
1031 }
1032 return C;
1033 }
1034
EmitConstantExpr(const Expr * E,QualType DestType,CodeGenFunction * CGF)1035 llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E,
1036 QualType DestType,
1037 CodeGenFunction *CGF) {
1038 Expr::EvalResult Result;
1039
1040 bool Success = false;
1041
1042 if (DestType->isReferenceType())
1043 Success = E->EvaluateAsLValue(Result, Context);
1044 else
1045 Success = E->EvaluateAsRValue(Result, Context);
1046
1047 llvm::Constant *C = 0;
1048 if (Success && !Result.HasSideEffects)
1049 C = EmitConstantValue(Result.Val, DestType, CGF);
1050 else
1051 C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E));
1052
1053 if (C && C->getType()->isIntegerTy(1)) {
1054 llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType());
1055 C = llvm::ConstantExpr::getZExt(C, BoolTy);
1056 }
1057 return C;
1058 }
1059
EmitConstantValue(const APValue & Value,QualType DestType,CodeGenFunction * CGF)1060 llvm::Constant *CodeGenModule::EmitConstantValue(const APValue &Value,
1061 QualType DestType,
1062 CodeGenFunction *CGF) {
1063 switch (Value.getKind()) {
1064 case APValue::Uninitialized:
1065 llvm_unreachable("Constant expressions should be initialized.");
1066 case APValue::LValue: {
1067 llvm::Type *DestTy = getTypes().ConvertTypeForMem(DestType);
1068 llvm::Constant *Offset =
1069 llvm::ConstantInt::get(Int64Ty, Value.getLValueOffset().getQuantity());
1070
1071 llvm::Constant *C;
1072 if (APValue::LValueBase LVBase = Value.getLValueBase()) {
1073 // An array can be represented as an lvalue referring to the base.
1074 if (isa<llvm::ArrayType>(DestTy)) {
1075 assert(Offset->isNullValue() && "offset on array initializer");
1076 return ConstExprEmitter(*this, CGF).Visit(
1077 const_cast<Expr*>(LVBase.get<const Expr*>()));
1078 }
1079
1080 C = ConstExprEmitter(*this, CGF).EmitLValue(LVBase);
1081
1082 // Apply offset if necessary.
1083 if (!Offset->isNullValue()) {
1084 llvm::Constant *Casted = llvm::ConstantExpr::getBitCast(C, Int8PtrTy);
1085 Casted = llvm::ConstantExpr::getGetElementPtr(Casted, Offset);
1086 C = llvm::ConstantExpr::getBitCast(Casted, C->getType());
1087 }
1088
1089 // Convert to the appropriate type; this could be an lvalue for
1090 // an integer.
1091 if (isa<llvm::PointerType>(DestTy))
1092 return llvm::ConstantExpr::getBitCast(C, DestTy);
1093
1094 return llvm::ConstantExpr::getPtrToInt(C, DestTy);
1095 } else {
1096 C = Offset;
1097
1098 // Convert to the appropriate type; this could be an lvalue for
1099 // an integer.
1100 if (isa<llvm::PointerType>(DestTy))
1101 return llvm::ConstantExpr::getIntToPtr(C, DestTy);
1102
1103 // If the types don't match this should only be a truncate.
1104 if (C->getType() != DestTy)
1105 return llvm::ConstantExpr::getTrunc(C, DestTy);
1106
1107 return C;
1108 }
1109 }
1110 case APValue::Int:
1111 return llvm::ConstantInt::get(VMContext, Value.getInt());
1112 case APValue::ComplexInt: {
1113 llvm::Constant *Complex[2];
1114
1115 Complex[0] = llvm::ConstantInt::get(VMContext,
1116 Value.getComplexIntReal());
1117 Complex[1] = llvm::ConstantInt::get(VMContext,
1118 Value.getComplexIntImag());
1119
1120 // FIXME: the target may want to specify that this is packed.
1121 llvm::StructType *STy = llvm::StructType::get(Complex[0]->getType(),
1122 Complex[1]->getType(),
1123 NULL);
1124 return llvm::ConstantStruct::get(STy, Complex);
1125 }
1126 case APValue::Float: {
1127 const llvm::APFloat &Init = Value.getFloat();
1128 if (&Init.getSemantics() == &llvm::APFloat::IEEEhalf &&
1129 !Context.getLangOpts().NativeHalfType)
1130 return llvm::ConstantInt::get(VMContext, Init.bitcastToAPInt());
1131 else
1132 return llvm::ConstantFP::get(VMContext, Init);
1133 }
1134 case APValue::ComplexFloat: {
1135 llvm::Constant *Complex[2];
1136
1137 Complex[0] = llvm::ConstantFP::get(VMContext,
1138 Value.getComplexFloatReal());
1139 Complex[1] = llvm::ConstantFP::get(VMContext,
1140 Value.getComplexFloatImag());
1141
1142 // FIXME: the target may want to specify that this is packed.
1143 llvm::StructType *STy = llvm::StructType::get(Complex[0]->getType(),
1144 Complex[1]->getType(),
1145 NULL);
1146 return llvm::ConstantStruct::get(STy, Complex);
1147 }
1148 case APValue::Vector: {
1149 SmallVector<llvm::Constant *, 4> Inits;
1150 unsigned NumElts = Value.getVectorLength();
1151
1152 for (unsigned i = 0; i != NumElts; ++i) {
1153 const APValue &Elt = Value.getVectorElt(i);
1154 if (Elt.isInt())
1155 Inits.push_back(llvm::ConstantInt::get(VMContext, Elt.getInt()));
1156 else
1157 Inits.push_back(llvm::ConstantFP::get(VMContext, Elt.getFloat()));
1158 }
1159 return llvm::ConstantVector::get(Inits);
1160 }
1161 case APValue::AddrLabelDiff: {
1162 const AddrLabelExpr *LHSExpr = Value.getAddrLabelDiffLHS();
1163 const AddrLabelExpr *RHSExpr = Value.getAddrLabelDiffRHS();
1164 llvm::Constant *LHS = EmitConstantExpr(LHSExpr, LHSExpr->getType(), CGF);
1165 llvm::Constant *RHS = EmitConstantExpr(RHSExpr, RHSExpr->getType(), CGF);
1166
1167 // Compute difference
1168 llvm::Type *ResultType = getTypes().ConvertType(DestType);
1169 LHS = llvm::ConstantExpr::getPtrToInt(LHS, IntPtrTy);
1170 RHS = llvm::ConstantExpr::getPtrToInt(RHS, IntPtrTy);
1171 llvm::Constant *AddrLabelDiff = llvm::ConstantExpr::getSub(LHS, RHS);
1172
1173 // LLVM is a bit sensitive about the exact format of the
1174 // address-of-label difference; make sure to truncate after
1175 // the subtraction.
1176 return llvm::ConstantExpr::getTruncOrBitCast(AddrLabelDiff, ResultType);
1177 }
1178 case APValue::Struct:
1179 case APValue::Union:
1180 return ConstStructBuilder::BuildStruct(*this, CGF, Value, DestType);
1181 case APValue::Array: {
1182 const ArrayType *CAT = Context.getAsArrayType(DestType);
1183 unsigned NumElements = Value.getArraySize();
1184 unsigned NumInitElts = Value.getArrayInitializedElts();
1185
1186 std::vector<llvm::Constant*> Elts;
1187 Elts.reserve(NumElements);
1188
1189 // Emit array filler, if there is one.
1190 llvm::Constant *Filler = 0;
1191 if (Value.hasArrayFiller())
1192 Filler = EmitConstantValueForMemory(Value.getArrayFiller(),
1193 CAT->getElementType(), CGF);
1194
1195 // Emit initializer elements.
1196 llvm::Type *CommonElementType = 0;
1197 for (unsigned I = 0; I < NumElements; ++I) {
1198 llvm::Constant *C = Filler;
1199 if (I < NumInitElts)
1200 C = EmitConstantValueForMemory(Value.getArrayInitializedElt(I),
1201 CAT->getElementType(), CGF);
1202 else
1203 assert(Filler && "Missing filler for implicit elements of initializer");
1204 if (I == 0)
1205 CommonElementType = C->getType();
1206 else if (C->getType() != CommonElementType)
1207 CommonElementType = 0;
1208 Elts.push_back(C);
1209 }
1210
1211 if (!CommonElementType) {
1212 // FIXME: Try to avoid packing the array
1213 std::vector<llvm::Type*> Types;
1214 Types.reserve(NumElements);
1215 for (unsigned i = 0, e = Elts.size(); i < e; ++i)
1216 Types.push_back(Elts[i]->getType());
1217 llvm::StructType *SType = llvm::StructType::get(VMContext, Types, true);
1218 return llvm::ConstantStruct::get(SType, Elts);
1219 }
1220
1221 llvm::ArrayType *AType =
1222 llvm::ArrayType::get(CommonElementType, NumElements);
1223 return llvm::ConstantArray::get(AType, Elts);
1224 }
1225 case APValue::MemberPointer:
1226 return getCXXABI().EmitMemberPointer(Value, DestType);
1227 }
1228 llvm_unreachable("Unknown APValue kind");
1229 }
1230
1231 llvm::Constant *
EmitConstantValueForMemory(const APValue & Value,QualType DestType,CodeGenFunction * CGF)1232 CodeGenModule::EmitConstantValueForMemory(const APValue &Value,
1233 QualType DestType,
1234 CodeGenFunction *CGF) {
1235 llvm::Constant *C = EmitConstantValue(Value, DestType, CGF);
1236 if (C->getType()->isIntegerTy(1)) {
1237 llvm::Type *BoolTy = getTypes().ConvertTypeForMem(DestType);
1238 C = llvm::ConstantExpr::getZExt(C, BoolTy);
1239 }
1240 return C;
1241 }
1242
1243 llvm::Constant *
GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr * E)1244 CodeGenModule::GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *E) {
1245 assert(E->isFileScope() && "not a file-scope compound literal expr");
1246 return ConstExprEmitter(*this, 0).EmitLValue(E);
1247 }
1248
1249 llvm::Constant *
getMemberPointerConstant(const UnaryOperator * uo)1250 CodeGenModule::getMemberPointerConstant(const UnaryOperator *uo) {
1251 // Member pointer constants always have a very particular form.
1252 const MemberPointerType *type = cast<MemberPointerType>(uo->getType());
1253 const ValueDecl *decl = cast<DeclRefExpr>(uo->getSubExpr())->getDecl();
1254
1255 // A member function pointer.
1256 if (const CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(decl))
1257 return getCXXABI().EmitMemberPointer(method);
1258
1259 // Otherwise, a member data pointer.
1260 uint64_t fieldOffset = getContext().getFieldOffset(decl);
1261 CharUnits chars = getContext().toCharUnitsFromBits((int64_t) fieldOffset);
1262 return getCXXABI().EmitMemberDataPointer(type, chars);
1263 }
1264
1265 static void
FillInNullDataMemberPointers(CodeGenModule & CGM,QualType T,SmallVectorImpl<llvm::Constant * > & Elements,uint64_t StartOffset)1266 FillInNullDataMemberPointers(CodeGenModule &CGM, QualType T,
1267 SmallVectorImpl<llvm::Constant *> &Elements,
1268 uint64_t StartOffset) {
1269 assert(StartOffset % CGM.getContext().getCharWidth() == 0 &&
1270 "StartOffset not byte aligned!");
1271
1272 if (CGM.getTypes().isZeroInitializable(T))
1273 return;
1274
1275 if (const ConstantArrayType *CAT =
1276 CGM.getContext().getAsConstantArrayType(T)) {
1277 QualType ElementTy = CAT->getElementType();
1278 uint64_t ElementSize = CGM.getContext().getTypeSize(ElementTy);
1279
1280 for (uint64_t I = 0, E = CAT->getSize().getZExtValue(); I != E; ++I) {
1281 FillInNullDataMemberPointers(CGM, ElementTy, Elements,
1282 StartOffset + I * ElementSize);
1283 }
1284 } else if (const RecordType *RT = T->getAs<RecordType>()) {
1285 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1286 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1287
1288 // Go through all bases and fill in any null pointer to data members.
1289 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1290 E = RD->bases_end(); I != E; ++I) {
1291 if (I->isVirtual()) {
1292 // Ignore virtual bases.
1293 continue;
1294 }
1295
1296 const CXXRecordDecl *BaseDecl =
1297 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1298
1299 // Ignore empty bases.
1300 if (BaseDecl->isEmpty())
1301 continue;
1302
1303 // Ignore bases that don't have any pointer to data members.
1304 if (CGM.getTypes().isZeroInitializable(BaseDecl))
1305 continue;
1306
1307 uint64_t BaseOffset =
1308 CGM.getContext().toBits(Layout.getBaseClassOffset(BaseDecl));
1309 FillInNullDataMemberPointers(CGM, I->getType(),
1310 Elements, StartOffset + BaseOffset);
1311 }
1312
1313 // Visit all fields.
1314 unsigned FieldNo = 0;
1315 for (RecordDecl::field_iterator I = RD->field_begin(),
1316 E = RD->field_end(); I != E; ++I, ++FieldNo) {
1317 QualType FieldType = I->getType();
1318
1319 if (CGM.getTypes().isZeroInitializable(FieldType))
1320 continue;
1321
1322 uint64_t FieldOffset = StartOffset + Layout.getFieldOffset(FieldNo);
1323 FillInNullDataMemberPointers(CGM, FieldType, Elements, FieldOffset);
1324 }
1325 } else {
1326 assert(T->isMemberPointerType() && "Should only see member pointers here!");
1327 assert(!T->getAs<MemberPointerType>()->getPointeeType()->isFunctionType() &&
1328 "Should only see pointers to data members here!");
1329
1330 CharUnits StartIndex = CGM.getContext().toCharUnitsFromBits(StartOffset);
1331 CharUnits EndIndex = StartIndex + CGM.getContext().getTypeSizeInChars(T);
1332
1333 // FIXME: hardcodes Itanium member pointer representation!
1334 llvm::Constant *NegativeOne =
1335 llvm::ConstantInt::get(CGM.Int8Ty, -1ULL, /*isSigned*/true);
1336
1337 // Fill in the null data member pointer.
1338 for (CharUnits I = StartIndex; I != EndIndex; ++I)
1339 Elements[I.getQuantity()] = NegativeOne;
1340 }
1341 }
1342
1343 static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM,
1344 llvm::Type *baseType,
1345 const CXXRecordDecl *base);
1346
EmitNullConstant(CodeGenModule & CGM,const CXXRecordDecl * record,bool asCompleteObject)1347 static llvm::Constant *EmitNullConstant(CodeGenModule &CGM,
1348 const CXXRecordDecl *record,
1349 bool asCompleteObject) {
1350 const CGRecordLayout &layout = CGM.getTypes().getCGRecordLayout(record);
1351 llvm::StructType *structure =
1352 (asCompleteObject ? layout.getLLVMType()
1353 : layout.getBaseSubobjectLLVMType());
1354
1355 unsigned numElements = structure->getNumElements();
1356 std::vector<llvm::Constant *> elements(numElements);
1357
1358 // Fill in all the bases.
1359 for (CXXRecordDecl::base_class_const_iterator
1360 I = record->bases_begin(), E = record->bases_end(); I != E; ++I) {
1361 if (I->isVirtual()) {
1362 // Ignore virtual bases; if we're laying out for a complete
1363 // object, we'll lay these out later.
1364 continue;
1365 }
1366
1367 const CXXRecordDecl *base =
1368 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
1369
1370 // Ignore empty bases.
1371 if (base->isEmpty())
1372 continue;
1373
1374 unsigned fieldIndex = layout.getNonVirtualBaseLLVMFieldNo(base);
1375 llvm::Type *baseType = structure->getElementType(fieldIndex);
1376 elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base);
1377 }
1378
1379 // Fill in all the fields.
1380 for (RecordDecl::field_iterator I = record->field_begin(),
1381 E = record->field_end(); I != E; ++I) {
1382 const FieldDecl *field = *I;
1383
1384 // Fill in non-bitfields. (Bitfields always use a zero pattern, which we
1385 // will fill in later.)
1386 if (!field->isBitField()) {
1387 unsigned fieldIndex = layout.getLLVMFieldNo(field);
1388 elements[fieldIndex] = CGM.EmitNullConstant(field->getType());
1389 }
1390
1391 // For unions, stop after the first named field.
1392 if (record->isUnion() && field->getDeclName())
1393 break;
1394 }
1395
1396 // Fill in the virtual bases, if we're working with the complete object.
1397 if (asCompleteObject) {
1398 for (CXXRecordDecl::base_class_const_iterator
1399 I = record->vbases_begin(), E = record->vbases_end(); I != E; ++I) {
1400 const CXXRecordDecl *base =
1401 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
1402
1403 // Ignore empty bases.
1404 if (base->isEmpty())
1405 continue;
1406
1407 unsigned fieldIndex = layout.getVirtualBaseIndex(base);
1408
1409 // We might have already laid this field out.
1410 if (elements[fieldIndex]) continue;
1411
1412 llvm::Type *baseType = structure->getElementType(fieldIndex);
1413 elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base);
1414 }
1415 }
1416
1417 // Now go through all other fields and zero them out.
1418 for (unsigned i = 0; i != numElements; ++i) {
1419 if (!elements[i])
1420 elements[i] = llvm::Constant::getNullValue(structure->getElementType(i));
1421 }
1422
1423 return llvm::ConstantStruct::get(structure, elements);
1424 }
1425
1426 /// Emit the null constant for a base subobject.
EmitNullConstantForBase(CodeGenModule & CGM,llvm::Type * baseType,const CXXRecordDecl * base)1427 static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM,
1428 llvm::Type *baseType,
1429 const CXXRecordDecl *base) {
1430 const CGRecordLayout &baseLayout = CGM.getTypes().getCGRecordLayout(base);
1431
1432 // Just zero out bases that don't have any pointer to data members.
1433 if (baseLayout.isZeroInitializableAsBase())
1434 return llvm::Constant::getNullValue(baseType);
1435
1436 // If the base type is a struct, we can just use its null constant.
1437 if (isa<llvm::StructType>(baseType)) {
1438 return EmitNullConstant(CGM, base, /*complete*/ false);
1439 }
1440
1441 // Otherwise, some bases are represented as arrays of i8 if the size
1442 // of the base is smaller than its corresponding LLVM type. Figure
1443 // out how many elements this base array has.
1444 llvm::ArrayType *baseArrayType = cast<llvm::ArrayType>(baseType);
1445 unsigned numBaseElements = baseArrayType->getNumElements();
1446
1447 // Fill in null data member pointers.
1448 SmallVector<llvm::Constant *, 16> baseElements(numBaseElements);
1449 FillInNullDataMemberPointers(CGM, CGM.getContext().getTypeDeclType(base),
1450 baseElements, 0);
1451
1452 // Now go through all other elements and zero them out.
1453 if (numBaseElements) {
1454 llvm::Constant *i8_zero = llvm::Constant::getNullValue(CGM.Int8Ty);
1455 for (unsigned i = 0; i != numBaseElements; ++i) {
1456 if (!baseElements[i])
1457 baseElements[i] = i8_zero;
1458 }
1459 }
1460
1461 return llvm::ConstantArray::get(baseArrayType, baseElements);
1462 }
1463
EmitNullConstant(QualType T)1464 llvm::Constant *CodeGenModule::EmitNullConstant(QualType T) {
1465 if (getTypes().isZeroInitializable(T))
1466 return llvm::Constant::getNullValue(getTypes().ConvertTypeForMem(T));
1467
1468 if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(T)) {
1469 llvm::ArrayType *ATy =
1470 cast<llvm::ArrayType>(getTypes().ConvertTypeForMem(T));
1471
1472 QualType ElementTy = CAT->getElementType();
1473
1474 llvm::Constant *Element = EmitNullConstant(ElementTy);
1475 unsigned NumElements = CAT->getSize().getZExtValue();
1476
1477 if (Element->isNullValue())
1478 return llvm::ConstantAggregateZero::get(ATy);
1479
1480 SmallVector<llvm::Constant *, 8> Array(NumElements, Element);
1481 return llvm::ConstantArray::get(ATy, Array);
1482 }
1483
1484 if (const RecordType *RT = T->getAs<RecordType>()) {
1485 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1486 return ::EmitNullConstant(*this, RD, /*complete object*/ true);
1487 }
1488
1489 assert(T->isMemberPointerType() && "Should only see member pointers here!");
1490 assert(!T->getAs<MemberPointerType>()->getPointeeType()->isFunctionType() &&
1491 "Should only see pointers to data members here!");
1492
1493 // Itanium C++ ABI 2.3:
1494 // A NULL pointer is represented as -1.
1495 return getCXXABI().EmitNullMemberPointer(T->castAs<MemberPointerType>());
1496 }
1497
1498 llvm::Constant *
EmitNullConstantForBase(const CXXRecordDecl * Record)1499 CodeGenModule::EmitNullConstantForBase(const CXXRecordDecl *Record) {
1500 return ::EmitNullConstant(*this, Record, false);
1501 }
1502