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 VisitCastExpr(CastExpr *E);
90   void VisitCallExpr(const CallExpr *E);
91   void VisitStmtExpr(const StmtExpr *E);
92   void VisitBinaryOperator(const BinaryOperator *BO);
93   void VisitBinAssign(const BinaryOperator *E);
94   void VisitBinComma(const BinaryOperator *E);
95 
96   void VisitObjCMessageExpr(ObjCMessageExpr *E);
97   void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
98     EmitAggLoadOfLValue(E);
99   }
100   void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
101   void VisitObjCKVCRefExpr(ObjCKVCRefExpr *E);
102 
103   void VisitConditionalOperator(const ConditionalOperator *CO);
104   void VisitChooseExpr(const ChooseExpr *CE);
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::VisitCastExpr(CastExpr *E) {
169   if (E->getCastKind() == CastExpr::CK_ToUnion) {
170     // GCC union extension
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(),
176                                LValue::MakeAddr(CastPtr, 0));
177     return;
178   }
179 
180   // FIXME: Remove the CK_Unknown check here.
181   assert((E->getCastKind() == CastExpr::CK_NoOp ||
182           E->getCastKind() == CastExpr::CK_Unknown) &&
183          "Only no-op casts allowed!");
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   } 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()->getAs<RecordType>())
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::VisitChooseExpr(const ChooseExpr *CE) {
299   Visit(CE->getChosenSubExpr(CGF.getContext()));
300 }
301 
302 void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
303   llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
304   llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
305 
306   if (!ArgPtr) {
307     CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
308     return;
309   }
310 
311   EmitFinalDestCopy(VE, LValue::MakeAddr(ArgPtr, 0));
312 }
313 
314 void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
315   llvm::Value *Val = DestPtr;
316 
317   if (!Val) {
318     // Create a temporary variable.
319     Val = CGF.CreateTempAlloca(CGF.ConvertTypeForMem(E->getType()), "tmp");
320 
321     // FIXME: volatile
322     CGF.EmitAggExpr(E->getSubExpr(), Val, false);
323   } else
324     Visit(E->getSubExpr());
325 
326   CGF.PushCXXTemporary(E->getTemporary(), Val);
327 }
328 
329 void
330 AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) {
331   llvm::Value *Val = DestPtr;
332 
333   if (!Val) {
334     // Create a temporary variable.
335     Val = CGF.CreateTempAlloca(CGF.ConvertTypeForMem(E->getType()), "tmp");
336   }
337 
338   CGF.EmitCXXConstructExpr(Val, E);
339 }
340 
341 void AggExprEmitter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
342   CGF.EmitCXXExprWithTemporaries(E, DestPtr, VolatileDest);
343 }
344 
345 void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
346   // FIXME: Ignore result?
347   // FIXME: Are initializers affected by volatile?
348   if (isa<ImplicitValueInitExpr>(E)) {
349     EmitNullInitializationToLValue(LV, E->getType());
350   } else if (E->getType()->isComplexType()) {
351     CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
352   } else if (CGF.hasAggregateLLVMType(E->getType())) {
353     CGF.EmitAnyExpr(E, LV.getAddress(), false);
354   } else {
355     CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
356   }
357 }
358 
359 void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
360   if (!CGF.hasAggregateLLVMType(T)) {
361     // For non-aggregates, we can store zero
362     llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
363     CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
364   } else {
365     // Otherwise, just memset the whole thing to zero.  This is legal
366     // because in LLVM, all default initializers are guaranteed to have a
367     // bit pattern of all zeros.
368     // FIXME: That isn't true for member pointers!
369     // There's a potential optimization opportunity in combining
370     // memsets; that would be easy for arrays, but relatively
371     // difficult for structures with the current code.
372     CGF.EmitMemSetToZero(LV.getAddress(), T);
373   }
374 }
375 
376 void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
377 #if 0
378   // FIXME: Disabled while we figure out what to do about
379   // test/CodeGen/bitfield.c
380   //
381   // If we can, prefer a copy from a global; this is a lot less code for long
382   // globals, and it's easier for the current optimizers to analyze.
383   // FIXME: Should we really be doing this? Should we try to avoid cases where
384   // we emit a global with a lot of zeros?  Should we try to avoid short
385   // globals?
386   if (E->isConstantInitializer(CGF.getContext(), 0)) {
387     llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, &CGF);
388     llvm::GlobalVariable* GV =
389     new llvm::GlobalVariable(C->getType(), true,
390                              llvm::GlobalValue::InternalLinkage,
391                              C, "", &CGF.CGM.getModule(), 0);
392     EmitFinalDestCopy(E, LValue::MakeAddr(GV, 0));
393     return;
394   }
395 #endif
396   if (E->hadArrayRangeDesignator()) {
397     CGF.ErrorUnsupported(E, "GNU array range designator extension");
398   }
399 
400   // Handle initialization of an array.
401   if (E->getType()->isArrayType()) {
402     const llvm::PointerType *APType =
403       cast<llvm::PointerType>(DestPtr->getType());
404     const llvm::ArrayType *AType =
405       cast<llvm::ArrayType>(APType->getElementType());
406 
407     uint64_t NumInitElements = E->getNumInits();
408 
409     if (E->getNumInits() > 0) {
410       QualType T1 = E->getType();
411       QualType T2 = E->getInit(0)->getType();
412       if (CGF.getContext().hasSameUnqualifiedType(T1, T2)) {
413         EmitAggLoadOfLValue(E->getInit(0));
414         return;
415       }
416     }
417 
418     uint64_t NumArrayElements = AType->getNumElements();
419     QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
420     ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType();
421 
422     unsigned CVRqualifier = ElementType.getCVRQualifiers();
423 
424     for (uint64_t i = 0; i != NumArrayElements; ++i) {
425       llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
426       if (i < NumInitElements)
427         EmitInitializationToLValue(E->getInit(i),
428                                    LValue::MakeAddr(NextVal, CVRqualifier));
429       else
430         EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, CVRqualifier),
431                                        ElementType);
432     }
433     return;
434   }
435 
436   assert(E->getType()->isRecordType() && "Only support structs/unions here!");
437 
438   // Do struct initialization; this code just sets each individual member
439   // to the approprate value.  This makes bitfield support automatic;
440   // the disadvantage is that the generated code is more difficult for
441   // the optimizer, especially with bitfields.
442   unsigned NumInitElements = E->getNumInits();
443   RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl();
444   unsigned CurInitVal = 0;
445 
446   if (E->getType()->isUnionType()) {
447     // Only initialize one field of a union. The field itself is
448     // specified by the initializer list.
449     if (!E->getInitializedFieldInUnion()) {
450       // Empty union; we have nothing to do.
451 
452 #ifndef NDEBUG
453       // Make sure that it's really an empty and not a failure of
454       // semantic analysis.
455       for (RecordDecl::field_iterator Field = SD->field_begin(),
456                                    FieldEnd = SD->field_end();
457            Field != FieldEnd; ++Field)
458         assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
459 #endif
460       return;
461     }
462 
463     // FIXME: volatility
464     FieldDecl *Field = E->getInitializedFieldInUnion();
465     LValue FieldLoc = CGF.EmitLValueForField(DestPtr, Field, true, 0);
466 
467     if (NumInitElements) {
468       // Store the initializer into the field
469       EmitInitializationToLValue(E->getInit(0), FieldLoc);
470     } else {
471       // Default-initialize to null
472       EmitNullInitializationToLValue(FieldLoc, Field->getType());
473     }
474 
475     return;
476   }
477 
478   // Here we iterate over the fields; this makes it simpler to both
479   // default-initialize fields and skip over unnamed fields.
480   for (RecordDecl::field_iterator Field = SD->field_begin(),
481                                FieldEnd = SD->field_end();
482        Field != FieldEnd; ++Field) {
483     // We're done once we hit the flexible array member
484     if (Field->getType()->isIncompleteArrayType())
485       break;
486 
487     if (Field->isUnnamedBitfield())
488       continue;
489 
490     // FIXME: volatility
491     LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *Field, false, 0);
492     // We never generate write-barries for initialized fields.
493     LValue::SetObjCNonGC(FieldLoc, true);
494     if (CurInitVal < NumInitElements) {
495       // Store the initializer into the field
496       EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
497     } else {
498       // We're out of initalizers; default-initialize to null
499       EmitNullInitializationToLValue(FieldLoc, Field->getType());
500     }
501   }
502 }
503 
504 //===----------------------------------------------------------------------===//
505 //                        Entry Points into this File
506 //===----------------------------------------------------------------------===//
507 
508 /// EmitAggExpr - Emit the computation of the specified expression of aggregate
509 /// type.  The result is computed into DestPtr.  Note that if DestPtr is null,
510 /// the value of the aggregate expression is not needed.  If VolatileDest is
511 /// true, DestPtr cannot be 0.
512 void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
513                                   bool VolatileDest, bool IgnoreResult) {
514   assert(E && hasAggregateLLVMType(E->getType()) &&
515          "Invalid aggregate expression to emit");
516   assert ((DestPtr != 0 || VolatileDest == false)
517           && "volatile aggregate can't be 0");
518 
519   AggExprEmitter(*this, DestPtr, VolatileDest, IgnoreResult)
520     .Visit(const_cast<Expr*>(E));
521 }
522 
523 void CodeGenFunction::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
524   assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
525 
526   EmitMemSetToZero(DestPtr, Ty);
527 }
528 
529 void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
530                                         llvm::Value *SrcPtr, QualType Ty,
531                                         bool isVolatile) {
532   assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
533 
534   // Aggregate assignment turns into llvm.memcpy.  This is almost valid per
535   // C99 6.5.16.1p3, which states "If the value being stored in an object is
536   // read from another object that overlaps in anyway the storage of the first
537   // object, then the overlap shall be exact and the two objects shall have
538   // qualified or unqualified versions of a compatible type."
539   //
540   // memcpy is not defined if the source and destination pointers are exactly
541   // equal, but other compilers do this optimization, and almost every memcpy
542   // implementation handles this case safely.  If there is a libc that does not
543   // safely handle this, we can add a target hook.
544   const llvm::Type *BP =
545                 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
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 =
556           llvm::IntegerType::get(VMContext, 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                       llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
574                       llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
575                                              TypeInfo.second/8));
576 }
577