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