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