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