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 "clang/AST/AST.h"
17 #include "llvm/Constants.h"
18 #include "llvm/Function.h"
19 #include "llvm/GlobalVariable.h"
20 #include "llvm/Support/Compiler.h"
21 #include "llvm/Intrinsics.h"
22 using namespace clang;
23 using namespace CodeGen;
24 
25 //===----------------------------------------------------------------------===//
26 //                        Aggregate Expression Emitter
27 //===----------------------------------------------------------------------===//
28 
29 namespace  {
30 class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> {
31   CodeGenFunction &CGF;
32   llvm::IRBuilder &Builder;
33   llvm::Value *DestPtr;
34   bool VolatileDest;
35 public:
36   AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool volatileDest)
37     : CGF(cgf), Builder(CGF.Builder),
38       DestPtr(destPtr), VolatileDest(volatileDest) {
39   }
40 
41   //===--------------------------------------------------------------------===//
42   //                               Utilities
43   //===--------------------------------------------------------------------===//
44 
45   /// EmitAggLoadOfLValue - Given an expression with aggregate type that
46   /// represents a value lvalue, this method emits the address of the lvalue,
47   /// then loads the result into DestPtr.
48   void EmitAggLoadOfLValue(const Expr *E);
49 
50   void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr,
51                          QualType EltTy);
52 
53   void EmitAggregateClear(llvm::Value *DestPtr, QualType Ty);
54 
55   void EmitNonConstInit(InitListExpr *E);
56 
57   //===--------------------------------------------------------------------===//
58   //                            Visitor Methods
59   //===--------------------------------------------------------------------===//
60 
61   void VisitStmt(Stmt *S) {
62     CGF.WarnUnsupported(S, "aggregate expression");
63   }
64   void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
65 
66   // l-values.
67   void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); }
68   void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
69   void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
70   void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
71   void VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
72       { EmitAggLoadOfLValue(E); }
73 
74   void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
75     EmitAggLoadOfLValue(E);
76   }
77 
78   // Operators.
79   //  case Expr::UnaryOperatorClass:
80   //  case Expr::CastExprClass:
81   void VisitImplicitCastExpr(ImplicitCastExpr *E);
82   void VisitCallExpr(const CallExpr *E);
83   void VisitStmtExpr(const StmtExpr *E);
84   void VisitBinaryOperator(const BinaryOperator *BO);
85   void VisitBinAssign(const BinaryOperator *E);
86   void VisitOverloadExpr(const OverloadExpr *E);
87   void VisitBinComma(const BinaryOperator *E);
88 
89   void VisitObjCMessageExpr(ObjCMessageExpr *E);
90 
91 
92   void VisitConditionalOperator(const ConditionalOperator *CO);
93   void VisitInitListExpr(InitListExpr *E);
94   void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
95     Visit(DAE->getExpr());
96   }
97   void VisitVAArgExpr(VAArgExpr *E);
98 
99   void EmitInitializationToLValue(Expr *E, LValue Address);
100   void EmitNullInitializationToLValue(LValue Address, QualType T);
101   //  case Expr::ChooseExprClass:
102 
103 };
104 }  // end anonymous namespace.
105 
106 //===----------------------------------------------------------------------===//
107 //                                Utilities
108 //===----------------------------------------------------------------------===//
109 
110 void AggExprEmitter::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
111   assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
112 
113   // Aggregate assignment turns into llvm.memset.
114   const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
115   if (DestPtr->getType() != BP)
116     DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
117 
118   // Get size and alignment info for this aggregate.
119   std::pair<uint64_t, unsigned> TypeInfo = CGF.getContext().getTypeInfo(Ty);
120 
121   // FIXME: Handle variable sized types.
122   const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth);
123 
124   llvm::Value *MemSetOps[4] = {
125     DestPtr,
126     llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty),
127     // TypeInfo.first describes size in bits.
128     llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
129     llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8)
130   };
131 
132   Builder.CreateCall(CGF.CGM.getMemSetFn(), MemSetOps, MemSetOps+4);
133 }
134 
135 void AggExprEmitter::EmitAggregateCopy(llvm::Value *DestPtr,
136                                        llvm::Value *SrcPtr, QualType Ty) {
137   assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
138 
139   // Aggregate assignment turns into llvm.memmove.
140   const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
141   if (DestPtr->getType() != BP)
142     DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
143   if (SrcPtr->getType() != BP)
144     SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
145 
146   // Get size and alignment info for this aggregate.
147   std::pair<uint64_t, unsigned> TypeInfo = CGF.getContext().getTypeInfo(Ty);
148 
149   // FIXME: Handle variable sized types.
150   const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth);
151 
152   llvm::Value *MemMoveOps[4] = {
153     DestPtr, SrcPtr,
154     // TypeInfo.first describes size in bits.
155     llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
156     llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8)
157   };
158 
159   Builder.CreateCall(CGF.CGM.getMemMoveFn(), MemMoveOps, MemMoveOps+4);
160 }
161 
162 
163 /// EmitAggLoadOfLValue - Given an expression with aggregate type that
164 /// represents a value lvalue, this method emits the address of the lvalue,
165 /// then loads the result into DestPtr.
166 void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
167   LValue LV = CGF.EmitLValue(E);
168   assert(LV.isSimple() && "Can't have aggregate bitfield, vector, etc");
169   llvm::Value *SrcPtr = LV.getAddress();
170 
171   // If the result is ignored, don't copy from the value.
172   if (DestPtr == 0)
173     // FIXME: If the source is volatile, we must read from it.
174     return;
175 
176   EmitAggregateCopy(DestPtr, SrcPtr, E->getType());
177 }
178 
179 //===----------------------------------------------------------------------===//
180 //                            Visitor Methods
181 //===----------------------------------------------------------------------===//
182 
183 void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E)
184 {
185   QualType STy = E->getSubExpr()->getType().getCanonicalType();
186   QualType Ty = E->getType().getCanonicalType();
187 
188   assert(CGF.getContext().typesAreCompatible(
189              STy.getUnqualifiedType(), Ty.getUnqualifiedType())
190          && "Implicit cast types must be compatible");
191 
192   Visit(E->getSubExpr());
193 }
194 
195 void AggExprEmitter::VisitCallExpr(const CallExpr *E)
196 {
197   RValue RV = CGF.EmitCallExpr(E);
198   assert(RV.isAggregate() && "Return value must be aggregate value!");
199 
200   // If the result is ignored, don't copy from the value.
201   if (DestPtr == 0)
202     // FIXME: If the source is volatile, we must read from it.
203     return;
204 
205   EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
206 }
207 void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E)
208 {
209   RValue RV = RValue::getAggregate(CGF.EmitObjCMessageExpr(E));
210 
211   // If the result is ignored, don't copy from the value.
212   if (DestPtr == 0)
213     return;
214 
215   EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
216 }
217 
218 void AggExprEmitter::VisitOverloadExpr(const OverloadExpr *E)
219 {
220   RValue RV = CGF.EmitCallExpr(E->getFn(), E->arg_begin(),
221                                E->arg_end(CGF.getContext()));
222 
223   assert(RV.isAggregate() && "Return value must be aggregate value!");
224 
225   // If the result is ignored, don't copy from the value.
226   if (DestPtr == 0)
227     // FIXME: If the source is volatile, we must read from it.
228     return;
229 
230   EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
231 }
232 
233 void AggExprEmitter::VisitBinComma(const BinaryOperator *E)
234 {
235   CGF.EmitAnyExpr(E->getLHS());
236   CGF.EmitAggExpr(E->getRHS(), DestPtr, false);
237 }
238 
239 void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
240   CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
241 }
242 
243 void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
244   CGF.WarnUnsupported(E, "aggregate binary expression");
245 }
246 
247 void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
248   // For an assignment to work, the value on the right has
249   // to be compatible with the value on the left.
250   assert(CGF.getContext().typesAreCompatible(
251              E->getLHS()->getType().getUnqualifiedType(),
252              E->getRHS()->getType().getUnqualifiedType())
253          && "Invalid assignment");
254   LValue LHS = CGF.EmitLValue(E->getLHS());
255 
256   // Codegen the RHS so that it stores directly into the LHS.
257   CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), false /*FIXME: VOLATILE LHS*/);
258 
259   if (DestPtr == 0)
260     return;
261 
262   // If the result of the assignment is used, copy the RHS there also.
263   EmitAggregateCopy(DestPtr, LHS.getAddress(), E->getType());
264 }
265 
266 void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
267   llvm::BasicBlock *LHSBlock = llvm::BasicBlock::Create("cond.?");
268   llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("cond.:");
269   llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("cond.cont");
270 
271   llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
272   Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
273 
274   CGF.EmitBlock(LHSBlock);
275 
276   // Handle the GNU extension for missing LHS.
277   assert(E->getLHS() && "Must have LHS for aggregate value");
278 
279   Visit(E->getLHS());
280   Builder.CreateBr(ContBlock);
281   LHSBlock = Builder.GetInsertBlock();
282 
283   CGF.EmitBlock(RHSBlock);
284 
285   Visit(E->getRHS());
286   Builder.CreateBr(ContBlock);
287   RHSBlock = Builder.GetInsertBlock();
288 
289   CGF.EmitBlock(ContBlock);
290 }
291 
292 void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
293   llvm::Value *ArgValue = CGF.EmitLValue(VE->getSubExpr()).getAddress();
294   llvm::Value *V = Builder.CreateVAArg(ArgValue, CGF.ConvertType(VE->getType()));
295   if (DestPtr)
296     // FIXME: volatility
297     Builder.CreateStore(V, DestPtr);
298 }
299 
300 void AggExprEmitter::EmitNonConstInit(InitListExpr *E) {
301 
302   const llvm::PointerType *APType =
303     cast<llvm::PointerType>(DestPtr->getType());
304   const llvm::Type *DestType = APType->getElementType();
305 
306   if (const llvm::ArrayType *AType = dyn_cast<llvm::ArrayType>(DestType)) {
307     unsigned NumInitElements = E->getNumInits();
308 
309     unsigned i;
310     for (i = 0; i != NumInitElements; ++i) {
311       llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
312       Expr *Init = E->getInit(i);
313       if (isa<InitListExpr>(Init))
314         CGF.EmitAggExpr(Init, NextVal, VolatileDest);
315       else
316         // FIXME: volatility
317         Builder.CreateStore(CGF.EmitScalarExpr(Init), NextVal);
318     }
319 
320     // Emit remaining default initializers
321     unsigned NumArrayElements = AType->getNumElements();
322     QualType QType = E->getInit(0)->getType();
323     const llvm::Type *EType = AType->getElementType();
324     for (/*Do not initialize i*/; i < NumArrayElements; ++i) {
325       llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
326       if (EType->isSingleValueType())
327         // FIXME: volatility
328         Builder.CreateStore(llvm::Constant::getNullValue(EType), NextVal);
329       else
330         EmitAggregateClear(NextVal, QType);
331     }
332   } else
333     assert(false && "Invalid initializer");
334 }
335 
336 void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
337   // FIXME: Are initializers affected by volatile?
338   if (E->getType()->isComplexType()) {
339     CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
340   } else if (CGF.hasAggregateLLVMType(E->getType())) {
341     CGF.EmitAnyExpr(E, LV.getAddress(), false);
342   } else {
343     CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
344   }
345 }
346 
347 void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
348   if (!CGF.hasAggregateLLVMType(T)) {
349     // For non-aggregates, we can store zero
350     const llvm::Type *T =
351        cast<llvm::PointerType>(LV.getAddress()->getType())->getElementType();
352     // FIXME: volatility
353     Builder.CreateStore(llvm::Constant::getNullValue(T), LV.getAddress());
354   } else {
355     // Otherwise, just memset the whole thing to zero.  This is legal
356     // because in LLVM, all default initializers are guaranteed to have a
357     // bit pattern of all zeros.
358     // There's a potential optimization opportunity in combining
359     // memsets; that would be easy for arrays, but relatively
360     // difficult for structures with the current code.
361     llvm::Value *MemSet = CGF.CGM.getIntrinsic(llvm::Intrinsic::memset_i64);
362     uint64_t Size = CGF.getContext().getTypeSize(T);
363 
364     const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
365     llvm::Value* DestPtr = Builder.CreateBitCast(LV.getAddress(), BP, "tmp");
366     Builder.CreateCall4(MemSet, DestPtr,
367                         llvm::ConstantInt::get(llvm::Type::Int8Ty, 0),
368                         llvm::ConstantInt::get(llvm::Type::Int64Ty, Size/8),
369                         llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
370   }
371 }
372 
373 void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
374   if (E->isConstantExpr(CGF.getContext(), 0)) {
375     // FIXME: call into const expr emitter so that we can emit
376     // a memcpy instead of storing the individual members.
377     // This is purely for perf; both codepaths lead to equivalent
378     // (although not necessarily identical) code.
379     // It's worth noting that LLVM keeps on getting smarter, though,
380     // so it might not be worth bothering.
381   }
382 
383   // Handle initialization of an array.
384   if (E->getType()->isArrayType()) {
385     const llvm::PointerType *APType =
386       cast<llvm::PointerType>(DestPtr->getType());
387     const llvm::ArrayType *AType =
388       cast<llvm::ArrayType>(APType->getElementType());
389 
390     uint64_t NumInitElements = E->getNumInits();
391 
392     if (E->getNumInits() > 0 &&
393         E->getType().getCanonicalType().getUnqualifiedType() ==
394           E->getInit(0)->getType().getCanonicalType().getUnqualifiedType()) {
395       EmitAggLoadOfLValue(E->getInit(0));
396       return;
397     }
398 
399     uint64_t NumArrayElements = AType->getNumElements();
400     QualType ElementType = E->getType()->getAsArrayType()->getElementType();
401 
402     unsigned CVRqualifier = E->getType().getCanonicalType()->getAsArrayType()
403                             ->getElementType().getCVRQualifiers();
404 
405     for (uint64_t i = 0; i != NumArrayElements; ++i) {
406       llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
407       if (i < NumInitElements)
408         EmitInitializationToLValue(E->getInit(i),
409                                    LValue::MakeAddr(NextVal, CVRqualifier));
410       else
411         EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, CVRqualifier),
412                                        ElementType);
413     }
414     return;
415   }
416 
417   assert(E->getType()->isRecordType() && "Only support structs/unions here!");
418 
419   // Do struct initialization; this code just sets each individual member
420   // to the approprate value.  This makes bitfield support automatic;
421   // the disadvantage is that the generated code is more difficult for
422   // the optimizer, especially with bitfields.
423   unsigned NumInitElements = E->getNumInits();
424   RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
425   unsigned NumMembers = SD->getNumMembers() - SD->hasFlexibleArrayMember();
426   unsigned CurInitVal = 0;
427   bool isUnion = E->getType()->isUnionType();
428 
429   // Here we iterate over the fields; this makes it simpler to both
430   // default-initialize fields and skip over unnamed fields.
431   for (unsigned CurFieldNo = 0; CurFieldNo != NumMembers; ++CurFieldNo) {
432     if (CurInitVal >= NumInitElements) {
433       // No more initializers; we're done.
434       break;
435     }
436 
437     FieldDecl *CurField = SD->getMember(CurFieldNo);
438     if (CurField->getIdentifier() == 0) {
439       // Initializers can't initialize unnamed fields, e.g. "int : 20;"
440       continue;
441     }
442     // FIXME: volatility
443     LValue FieldLoc = CGF.EmitLValueForField(DestPtr, CurField, isUnion,0);
444     if (CurInitVal < NumInitElements) {
445       // Store the initializer into the field
446       // This will probably have to get a bit smarter when we support
447       // designators in initializers
448       EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
449     } else {
450       // We're out of initalizers; default-initialize to null
451       EmitNullInitializationToLValue(FieldLoc, CurField->getType());
452     }
453 
454     // Unions only initialize one field.
455     // (things can get weird with designators, but they aren't
456     // supported yet.)
457     if (E->getType()->isUnionType())
458       break;
459   }
460 }
461 
462 //===----------------------------------------------------------------------===//
463 //                        Entry Points into this File
464 //===----------------------------------------------------------------------===//
465 
466 /// EmitAggExpr - Emit the computation of the specified expression of
467 /// aggregate type.  The result is computed into DestPtr.  Note that if
468 /// DestPtr is null, the value of the aggregate expression is not needed.
469 void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
470                                   bool VolatileDest) {
471   assert(E && hasAggregateLLVMType(E->getType()) &&
472          "Invalid aggregate expression to emit");
473 
474   AggExprEmitter(*this, DestPtr, VolatileDest).Visit(const_cast<Expr*>(E));
475 }
476