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