1 //===--- CGExprAgg.cpp - Emit LLVM Code from Aggregate 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 Aggregate Expr nodes as LLVM code.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CodeGenFunction.h"
15 #include "CodeGenModule.h"
16 #include "CGObjCRuntime.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/DeclCXX.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/Intrinsics.h"
25 using namespace clang;
26 using namespace CodeGen;
27 
28 //===----------------------------------------------------------------------===//
29 //                        Aggregate Expression Emitter
30 //===----------------------------------------------------------------------===//
31 
32 namespace  {
33 class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> {
34   CodeGenFunction &CGF;
35   CGBuilderTy &Builder;
36   llvm::Value *DestPtr;
37   bool VolatileDest;
38   bool IgnoreResult;
39 
40 public:
41   AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool v,
42                  bool ignore)
43     : CGF(cgf), Builder(CGF.Builder),
44       DestPtr(destPtr), VolatileDest(v), IgnoreResult(ignore) {
45   }
46 
47   //===--------------------------------------------------------------------===//
48   //                               Utilities
49   //===--------------------------------------------------------------------===//
50 
51   /// EmitAggLoadOfLValue - Given an expression with aggregate type that
52   /// represents a value lvalue, this method emits the address of the lvalue,
53   /// then loads the result into DestPtr.
54   void EmitAggLoadOfLValue(const Expr *E);
55 
56   /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
57   void EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore = false);
58   void EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore = false);
59 
60   //===--------------------------------------------------------------------===//
61   //                            Visitor Methods
62   //===--------------------------------------------------------------------===//
63 
64   void VisitStmt(Stmt *S) {
65     CGF.ErrorUnsupported(S, "aggregate expression");
66   }
67   void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
68   void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); }
69 
70   // l-values.
71   void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); }
72   void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
73   void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
74   void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
75   void VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
76     EmitAggLoadOfLValue(E);
77   }
78   void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
79     EmitAggLoadOfLValue(E);
80   }
81   void VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) {
82     EmitAggLoadOfLValue(E);
83   }
84   void VisitPredefinedExpr(const PredefinedExpr *E) {
85     EmitAggLoadOfLValue(E);
86   }
87 
88   // Operators.
89   void VisitCStyleCastExpr(CStyleCastExpr *E);
90   void VisitImplicitCastExpr(ImplicitCastExpr *E);
91   void VisitCallExpr(const CallExpr *E);
92   void VisitStmtExpr(const StmtExpr *E);
93   void VisitBinaryOperator(const BinaryOperator *BO);
94   void VisitBinAssign(const BinaryOperator *E);
95   void VisitBinComma(const BinaryOperator *E);
96 
97   void VisitObjCMessageExpr(ObjCMessageExpr *E);
98   void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
99     EmitAggLoadOfLValue(E);
100   }
101   void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
102   void VisitObjCKVCRefExpr(ObjCKVCRefExpr *E);
103 
104   void VisitConditionalOperator(const ConditionalOperator *CO);
105   void VisitChooseExpr(const ChooseExpr *CE);
106   void VisitInitListExpr(InitListExpr *E);
107   void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
108     Visit(DAE->getExpr());
109   }
110   void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
111   void VisitCXXConstructExpr(const CXXConstructExpr *E);
112   void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E);
113 
114   void VisitVAArgExpr(VAArgExpr *E);
115 
116   void EmitInitializationToLValue(Expr *E, LValue Address);
117   void EmitNullInitializationToLValue(LValue Address, QualType T);
118   //  case Expr::ChooseExprClass:
119 
120 };
121 }  // end anonymous namespace.
122 
123 //===----------------------------------------------------------------------===//
124 //                                Utilities
125 //===----------------------------------------------------------------------===//
126 
127 /// EmitAggLoadOfLValue - Given an expression with aggregate type that
128 /// represents a value lvalue, this method emits the address of the lvalue,
129 /// then loads the result into DestPtr.
130 void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
131   LValue LV = CGF.EmitLValue(E);
132   EmitFinalDestCopy(E, LV);
133 }
134 
135 /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
136 void AggExprEmitter::EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore) {
137   assert(Src.isAggregate() && "value must be aggregate value!");
138 
139   // If the result is ignored, don't copy from the value.
140   if (DestPtr == 0) {
141     if (!Src.isVolatileQualified() || (IgnoreResult && Ignore))
142       return;
143     // If the source is volatile, we must read from it; to do that, we need
144     // some place to put it.
145     DestPtr = CGF.CreateTempAlloca(CGF.ConvertType(E->getType()), "agg.tmp");
146   }
147 
148   // If the result of the assignment is used, copy the LHS there also.
149   // FIXME: Pass VolatileDest as well.  I think we also need to merge volatile
150   // from the source as well, as we can't eliminate it if either operand
151   // is volatile, unless copy has volatile for both source and destination..
152   CGF.EmitAggregateCopy(DestPtr, Src.getAggregateAddr(), E->getType(),
153                         VolatileDest|Src.isVolatileQualified());
154 }
155 
156 /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
157 void AggExprEmitter::EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore) {
158   assert(Src.isSimple() && "Can't have aggregate bitfield, vector, etc");
159 
160   EmitFinalDestCopy(E, RValue::getAggregate(Src.getAddress(),
161                                             Src.isVolatileQualified()),
162                     Ignore);
163 }
164 
165 //===----------------------------------------------------------------------===//
166 //                            Visitor Methods
167 //===----------------------------------------------------------------------===//
168 
169 void AggExprEmitter::VisitCStyleCastExpr(CStyleCastExpr *E) {
170   // GCC union extension
171   if (E->getSubExpr()->getType()->isScalarType()) {
172     QualType PtrTy =
173         CGF.getContext().getPointerType(E->getSubExpr()->getType());
174     llvm::Value *CastPtr = Builder.CreateBitCast(DestPtr,
175                                                  CGF.ConvertType(PtrTy));
176     EmitInitializationToLValue(E->getSubExpr(),
177                                LValue::MakeAddr(CastPtr, 0));
178     return;
179   }
180 
181   Visit(E->getSubExpr());
182 }
183 
184 void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
185   assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(),
186                                                  E->getType()) &&
187          "Implicit cast types must be compatible");
188   Visit(E->getSubExpr());
189 }
190 
191 void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
192   if (E->getCallReturnType()->isReferenceType()) {
193     EmitAggLoadOfLValue(E);
194     return;
195   }
196 
197   RValue RV = CGF.EmitCallExpr(E);
198   EmitFinalDestCopy(E, RV);
199 }
200 
201 void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
202   RValue RV = CGF.EmitObjCMessageExpr(E);
203   EmitFinalDestCopy(E, RV);
204 }
205 
206 void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
207   RValue RV = CGF.EmitObjCPropertyGet(E);
208   EmitFinalDestCopy(E, RV);
209 }
210 
211 void AggExprEmitter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) {
212   RValue RV = CGF.EmitObjCPropertyGet(E);
213   EmitFinalDestCopy(E, RV);
214 }
215 
216 void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
217   CGF.EmitAnyExpr(E->getLHS(), 0, false, true);
218   CGF.EmitAggExpr(E->getRHS(), DestPtr, VolatileDest);
219 }
220 
221 void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
222   CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
223 }
224 
225 void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
226   CGF.ErrorUnsupported(E, "aggregate binary expression");
227 }
228 
229 void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
230   // For an assignment to work, the value on the right has
231   // to be compatible with the value on the left.
232   assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
233                                                  E->getRHS()->getType())
234          && "Invalid assignment");
235   LValue LHS = CGF.EmitLValue(E->getLHS());
236 
237   // We have to special case property setters, otherwise we must have
238   // a simple lvalue (no aggregates inside vectors, bitfields).
239   if (LHS.isPropertyRef()) {
240     llvm::Value *AggLoc = DestPtr;
241     if (!AggLoc)
242       AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
243     CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest);
244     CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(),
245                             RValue::getAggregate(AggLoc, VolatileDest));
246   }
247   else if (LHS.isKVCRef()) {
248     llvm::Value *AggLoc = DestPtr;
249     if (!AggLoc)
250       AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
251     CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest);
252     CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(),
253                             RValue::getAggregate(AggLoc, VolatileDest));
254   } else {
255     if (CGF.getContext().getLangOptions().NeXTRuntime) {
256       QualType LHSTy = E->getLHS()->getType();
257       if (const RecordType *FDTTy = LHSTy.getTypePtr()->getAsRecordType())
258         if (FDTTy->getDecl()->hasObjectMember()) {
259           LValue RHS = CGF.EmitLValue(E->getRHS());
260           CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF, LHS.getAddress(),
261                                       RHS.getAddress(),
262                                       CGF.getContext().getTypeSize(LHSTy) / 8);
263           return;
264         }
265     }
266     // Codegen the RHS so that it stores directly into the LHS.
267     CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), LHS.isVolatileQualified());
268     EmitFinalDestCopy(E, LHS, true);
269   }
270 }
271 
272 void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
273   llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
274   llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
275   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
276 
277   llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
278   Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
279 
280   CGF.PushConditionalTempDestruction();
281   CGF.EmitBlock(LHSBlock);
282 
283   // Handle the GNU extension for missing LHS.
284   assert(E->getLHS() && "Must have LHS for aggregate value");
285 
286   Visit(E->getLHS());
287   CGF.PopConditionalTempDestruction();
288   CGF.EmitBranch(ContBlock);
289 
290   CGF.PushConditionalTempDestruction();
291   CGF.EmitBlock(RHSBlock);
292 
293   Visit(E->getRHS());
294   CGF.PopConditionalTempDestruction();
295   CGF.EmitBranch(ContBlock);
296 
297   CGF.EmitBlock(ContBlock);
298 }
299 
300 void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) {
301   Visit(CE->getChosenSubExpr(CGF.getContext()));
302 }
303 
304 void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
305   llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
306   llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
307 
308   if (!ArgPtr) {
309     CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
310     return;
311   }
312 
313   EmitFinalDestCopy(VE, LValue::MakeAddr(ArgPtr, 0));
314 }
315 
316 void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
317   llvm::Value *Val = DestPtr;
318 
319   if (!Val) {
320     // Create a temporary variable.
321     Val = CGF.CreateTempAlloca(CGF.ConvertTypeForMem(E->getType()), "tmp");
322 
323     // FIXME: volatile
324     CGF.EmitAggExpr(E->getSubExpr(), Val, false);
325   } else
326     Visit(E->getSubExpr());
327 
328   CGF.PushCXXTemporary(E->getTemporary(), Val);
329 }
330 
331 void
332 AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) {
333   llvm::Value *Val = DestPtr;
334 
335   if (!Val) {
336     // Create a temporary variable.
337     Val = CGF.CreateTempAlloca(CGF.ConvertTypeForMem(E->getType()), "tmp");
338   }
339 
340   CGF.EmitCXXConstructExpr(Val, E);
341 }
342 
343 void AggExprEmitter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
344   CGF.EmitCXXExprWithTemporaries(E, DestPtr, VolatileDest);
345 }
346 
347 void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
348   // FIXME: Ignore result?
349   // FIXME: Are initializers affected by volatile?
350   if (isa<ImplicitValueInitExpr>(E)) {
351     EmitNullInitializationToLValue(LV, E->getType());
352   } else if (E->getType()->isComplexType()) {
353     CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
354   } else if (CGF.hasAggregateLLVMType(E->getType())) {
355     CGF.EmitAnyExpr(E, LV.getAddress(), false);
356   } else {
357     CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
358   }
359 }
360 
361 void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
362   if (!CGF.hasAggregateLLVMType(T)) {
363     // For non-aggregates, we can store zero
364     llvm::Value *Null = CGF.getLLVMContext().getNullValue(CGF.ConvertType(T));
365     CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
366   } else {
367     // Otherwise, just memset the whole thing to zero.  This is legal
368     // because in LLVM, all default initializers are guaranteed to have a
369     // bit pattern of all zeros.
370     // FIXME: That isn't true for member pointers!
371     // There's a potential optimization opportunity in combining
372     // memsets; that would be easy for arrays, but relatively
373     // difficult for structures with the current code.
374     CGF.EmitMemSetToZero(LV.getAddress(), T);
375   }
376 }
377 
378 void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
379 #if 0
380   // FIXME: Disabled while we figure out what to do about
381   // test/CodeGen/bitfield.c
382   //
383   // If we can, prefer a copy from a global; this is a lot less code for long
384   // globals, and it's easier for the current optimizers to analyze.
385   // FIXME: Should we really be doing this? Should we try to avoid cases where
386   // we emit a global with a lot of zeros?  Should we try to avoid short
387   // globals?
388   if (E->isConstantInitializer(CGF.getContext(), 0)) {
389     llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, &CGF);
390     llvm::GlobalVariable* GV =
391     new llvm::GlobalVariable(C->getType(), true,
392                              llvm::GlobalValue::InternalLinkage,
393                              C, "", &CGF.CGM.getModule(), 0);
394     EmitFinalDestCopy(E, LValue::MakeAddr(GV, 0));
395     return;
396   }
397 #endif
398   if (E->hadArrayRangeDesignator()) {
399     CGF.ErrorUnsupported(E, "GNU array range designator extension");
400   }
401 
402   // Handle initialization of an array.
403   if (E->getType()->isArrayType()) {
404     const llvm::PointerType *APType =
405       cast<llvm::PointerType>(DestPtr->getType());
406     const llvm::ArrayType *AType =
407       cast<llvm::ArrayType>(APType->getElementType());
408 
409     uint64_t NumInitElements = E->getNumInits();
410 
411     if (E->getNumInits() > 0) {
412       QualType T1 = E->getType();
413       QualType T2 = E->getInit(0)->getType();
414       if (CGF.getContext().hasSameUnqualifiedType(T1, T2)) {
415         EmitAggLoadOfLValue(E->getInit(0));
416         return;
417       }
418     }
419 
420     uint64_t NumArrayElements = AType->getNumElements();
421     QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
422     ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType();
423 
424     unsigned CVRqualifier = ElementType.getCVRQualifiers();
425 
426     for (uint64_t i = 0; i != NumArrayElements; ++i) {
427       llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
428       if (i < NumInitElements)
429         EmitInitializationToLValue(E->getInit(i),
430                                    LValue::MakeAddr(NextVal, CVRqualifier));
431       else
432         EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, CVRqualifier),
433                                        ElementType);
434     }
435     return;
436   }
437 
438   assert(E->getType()->isRecordType() && "Only support structs/unions here!");
439 
440   // Do struct initialization; this code just sets each individual member
441   // to the approprate value.  This makes bitfield support automatic;
442   // the disadvantage is that the generated code is more difficult for
443   // the optimizer, especially with bitfields.
444   unsigned NumInitElements = E->getNumInits();
445   RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
446   unsigned CurInitVal = 0;
447 
448   if (E->getType()->isUnionType()) {
449     // Only initialize one field of a union. The field itself is
450     // specified by the initializer list.
451     if (!E->getInitializedFieldInUnion()) {
452       // Empty union; we have nothing to do.
453 
454 #ifndef NDEBUG
455       // Make sure that it's really an empty and not a failure of
456       // semantic analysis.
457       for (RecordDecl::field_iterator Field = SD->field_begin(),
458                                    FieldEnd = SD->field_end();
459            Field != FieldEnd; ++Field)
460         assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
461 #endif
462       return;
463     }
464 
465     // FIXME: volatility
466     FieldDecl *Field = E->getInitializedFieldInUnion();
467     LValue FieldLoc = CGF.EmitLValueForField(DestPtr, Field, true, 0);
468 
469     if (NumInitElements) {
470       // Store the initializer into the field
471       EmitInitializationToLValue(E->getInit(0), FieldLoc);
472     } else {
473       // Default-initialize to null
474       EmitNullInitializationToLValue(FieldLoc, Field->getType());
475     }
476 
477     return;
478   }
479 
480   // Here we iterate over the fields; this makes it simpler to both
481   // default-initialize fields and skip over unnamed fields.
482   for (RecordDecl::field_iterator Field = SD->field_begin(),
483                                FieldEnd = SD->field_end();
484        Field != FieldEnd; ++Field) {
485     // We're done once we hit the flexible array member
486     if (Field->getType()->isIncompleteArrayType())
487       break;
488 
489     if (Field->isUnnamedBitfield())
490       continue;
491 
492     // FIXME: volatility
493     LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *Field, false, 0);
494     // We never generate write-barries for initialized fields.
495     LValue::SetObjCNonGC(FieldLoc, true);
496     if (CurInitVal < NumInitElements) {
497       // Store the initializer into the field
498       EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
499     } else {
500       // We're out of initalizers; default-initialize to null
501       EmitNullInitializationToLValue(FieldLoc, Field->getType());
502     }
503   }
504 }
505 
506 //===----------------------------------------------------------------------===//
507 //                        Entry Points into this File
508 //===----------------------------------------------------------------------===//
509 
510 /// EmitAggExpr - Emit the computation of the specified expression of aggregate
511 /// type.  The result is computed into DestPtr.  Note that if DestPtr is null,
512 /// the value of the aggregate expression is not needed.  If VolatileDest is
513 /// true, DestPtr cannot be 0.
514 void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
515                                   bool VolatileDest, bool IgnoreResult) {
516   assert(E && hasAggregateLLVMType(E->getType()) &&
517          "Invalid aggregate expression to emit");
518   assert ((DestPtr != 0 || VolatileDest == false)
519           && "volatile aggregate can't be 0");
520 
521   AggExprEmitter(*this, DestPtr, VolatileDest, IgnoreResult)
522     .Visit(const_cast<Expr*>(E));
523 }
524 
525 void CodeGenFunction::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
526   assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
527 
528   EmitMemSetToZero(DestPtr, Ty);
529 }
530 
531 void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
532                                         llvm::Value *SrcPtr, QualType Ty,
533                                         bool isVolatile) {
534   assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
535 
536   // Aggregate assignment turns into llvm.memcpy.  This is almost valid per
537   // C99 6.5.16.1p3, which states "If the value being stored in an object is
538   // read from another object that overlaps in anyway the storage of the first
539   // object, then the overlap shall be exact and the two objects shall have
540   // qualified or unqualified versions of a compatible type."
541   //
542   // memcpy is not defined if the source and destination pointers are exactly
543   // equal, but other compilers do this optimization, and almost every memcpy
544   // implementation handles this case safely.  If there is a libc that does not
545   // safely handle this, we can add a target hook.
546   const llvm::Type *BP = VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty);
547   if (DestPtr->getType() != BP)
548     DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
549   if (SrcPtr->getType() != BP)
550     SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
551 
552   // Get size and alignment info for this aggregate.
553   std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
554 
555   // FIXME: Handle variable sized types.
556   const llvm::Type *IntPtr = VMContext.getIntegerType(LLVMPointerWidth);
557 
558   // FIXME: If we have a volatile struct, the optimizer can remove what might
559   // appear to be `extra' memory ops:
560   //
561   // volatile struct { int i; } a, b;
562   //
563   // int main() {
564   //   a = b;
565   //   a = b;
566   // }
567   //
568   // we need to use a differnt call here.  We use isVolatile to indicate when
569   // either the source or the destination is volatile.
570   Builder.CreateCall4(CGM.getMemCpyFn(),
571                       DestPtr, SrcPtr,
572                       // TypeInfo.first describes size in bits.
573                       VMContext.getConstantInt(IntPtr, TypeInfo.first/8),
574                       VMContext.getConstantInt(llvm::Type::Int32Ty,
575                                              TypeInfo.second/8));
576 }
577