1 //===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===// 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 // These classes wrap the information about a call or function 11 // definition used to handle ABI compliancy. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "CGCall.h" 16 #include "CodeGenFunction.h" 17 #include "CodeGenModule.h" 18 #include "clang/Basic/TargetInfo.h" 19 #include "clang/AST/Decl.h" 20 #include "clang/AST/DeclCXX.h" 21 #include "clang/AST/DeclObjC.h" 22 #include "clang/Frontend/CompileOptions.h" 23 #include "llvm/Attributes.h" 24 #include "llvm/Support/CallSite.h" 25 #include "llvm/Target/TargetData.h" 26 27 #include "ABIInfo.h" 28 29 using namespace clang; 30 using namespace CodeGen; 31 32 /***/ 33 34 // FIXME: Use iterator and sidestep silly type array creation. 35 36 const 37 CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionNoProtoType *FTNP) { 38 return getFunctionInfo(FTNP->getResultType(), 39 llvm::SmallVector<QualType, 16>()); 40 } 41 42 const 43 CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionProtoType *FTP) { 44 llvm::SmallVector<QualType, 16> ArgTys; 45 // FIXME: Kill copy. 46 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) 47 ArgTys.push_back(FTP->getArgType(i)); 48 return getFunctionInfo(FTP->getResultType(), ArgTys); 49 } 50 51 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) { 52 llvm::SmallVector<QualType, 16> ArgTys; 53 // Add the 'this' pointer unless this is a static method. 54 if (MD->isInstance()) 55 ArgTys.push_back(MD->getThisType(Context)); 56 57 const FunctionProtoType *FTP = MD->getType()->getAsFunctionProtoType(); 58 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) 59 ArgTys.push_back(FTP->getArgType(i)); 60 return getFunctionInfo(FTP->getResultType(), ArgTys); 61 } 62 63 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) { 64 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) 65 if (MD->isInstance()) 66 return getFunctionInfo(MD); 67 68 const FunctionType *FTy = FD->getType()->getAsFunctionType(); 69 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FTy)) 70 return getFunctionInfo(FTP); 71 return getFunctionInfo(cast<FunctionNoProtoType>(FTy)); 72 } 73 74 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) { 75 llvm::SmallVector<QualType, 16> ArgTys; 76 ArgTys.push_back(MD->getSelfDecl()->getType()); 77 ArgTys.push_back(Context.getObjCSelType()); 78 // FIXME: Kill copy? 79 for (ObjCMethodDecl::param_iterator i = MD->param_begin(), 80 e = MD->param_end(); i != e; ++i) 81 ArgTys.push_back((*i)->getType()); 82 return getFunctionInfo(MD->getResultType(), ArgTys); 83 } 84 85 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy, 86 const CallArgList &Args) { 87 // FIXME: Kill copy. 88 llvm::SmallVector<QualType, 16> ArgTys; 89 for (CallArgList::const_iterator i = Args.begin(), e = Args.end(); 90 i != e; ++i) 91 ArgTys.push_back(i->second); 92 return getFunctionInfo(ResTy, ArgTys); 93 } 94 95 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy, 96 const FunctionArgList &Args) { 97 // FIXME: Kill copy. 98 llvm::SmallVector<QualType, 16> ArgTys; 99 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); 100 i != e; ++i) 101 ArgTys.push_back(i->second); 102 return getFunctionInfo(ResTy, ArgTys); 103 } 104 105 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy, 106 const llvm::SmallVector<QualType, 16> &ArgTys) { 107 // Lookup or create unique function info. 108 llvm::FoldingSetNodeID ID; 109 CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end()); 110 111 void *InsertPos = 0; 112 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos); 113 if (FI) 114 return *FI; 115 116 // Construct the function info. 117 FI = new CGFunctionInfo(ResTy, ArgTys); 118 FunctionInfos.InsertNode(FI, InsertPos); 119 120 // Compute ABI information. 121 getABIInfo().computeInfo(*FI, getContext()); 122 123 return *FI; 124 } 125 126 CGFunctionInfo::CGFunctionInfo(QualType ResTy, 127 const llvm::SmallVector<QualType, 16> &ArgTys) { 128 NumArgs = ArgTys.size(); 129 Args = new ArgInfo[1 + NumArgs]; 130 Args[0].type = ResTy; 131 for (unsigned i = 0; i < NumArgs; ++i) 132 Args[1 + i].type = ArgTys[i]; 133 } 134 135 /***/ 136 137 void CodeGenTypes::GetExpandedTypes(QualType Ty, 138 std::vector<const llvm::Type*> &ArgTys) { 139 const RecordType *RT = Ty->getAsStructureType(); 140 assert(RT && "Can only expand structure types."); 141 const RecordDecl *RD = RT->getDecl(); 142 assert(!RD->hasFlexibleArrayMember() && 143 "Cannot expand structure with flexible array."); 144 145 for (RecordDecl::field_iterator i = RD->field_begin(Context), 146 e = RD->field_end(Context); i != e; ++i) { 147 const FieldDecl *FD = *i; 148 assert(!FD->isBitField() && 149 "Cannot expand structure with bit-field members."); 150 151 QualType FT = FD->getType(); 152 if (CodeGenFunction::hasAggregateLLVMType(FT)) { 153 GetExpandedTypes(FT, ArgTys); 154 } else { 155 ArgTys.push_back(ConvertType(FT)); 156 } 157 } 158 } 159 160 llvm::Function::arg_iterator 161 CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV, 162 llvm::Function::arg_iterator AI) { 163 const RecordType *RT = Ty->getAsStructureType(); 164 assert(RT && "Can only expand structure types."); 165 166 RecordDecl *RD = RT->getDecl(); 167 assert(LV.isSimple() && 168 "Unexpected non-simple lvalue during struct expansion."); 169 llvm::Value *Addr = LV.getAddress(); 170 for (RecordDecl::field_iterator i = RD->field_begin(getContext()), 171 e = RD->field_end(getContext()); i != e; ++i) { 172 FieldDecl *FD = *i; 173 QualType FT = FD->getType(); 174 175 // FIXME: What are the right qualifiers here? 176 LValue LV = EmitLValueForField(Addr, FD, false, 0); 177 if (CodeGenFunction::hasAggregateLLVMType(FT)) { 178 AI = ExpandTypeFromArgs(FT, LV, AI); 179 } else { 180 EmitStoreThroughLValue(RValue::get(AI), LV, FT); 181 ++AI; 182 } 183 } 184 185 return AI; 186 } 187 188 void 189 CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV, 190 llvm::SmallVector<llvm::Value*, 16> &Args) { 191 const RecordType *RT = Ty->getAsStructureType(); 192 assert(RT && "Can only expand structure types."); 193 194 RecordDecl *RD = RT->getDecl(); 195 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion"); 196 llvm::Value *Addr = RV.getAggregateAddr(); 197 for (RecordDecl::field_iterator i = RD->field_begin(getContext()), 198 e = RD->field_end(getContext()); i != e; ++i) { 199 FieldDecl *FD = *i; 200 QualType FT = FD->getType(); 201 202 // FIXME: What are the right qualifiers here? 203 LValue LV = EmitLValueForField(Addr, FD, false, 0); 204 if (CodeGenFunction::hasAggregateLLVMType(FT)) { 205 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args); 206 } else { 207 RValue RV = EmitLoadOfLValue(LV, FT); 208 assert(RV.isScalar() && 209 "Unexpected non-scalar rvalue during struct expansion."); 210 Args.push_back(RV.getScalarVal()); 211 } 212 } 213 } 214 215 /// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as 216 /// a pointer to an object of type \arg Ty. 217 /// 218 /// This safely handles the case when the src type is smaller than the 219 /// destination type; in this situation the values of bits which not 220 /// present in the src are undefined. 221 static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr, 222 const llvm::Type *Ty, 223 CodeGenFunction &CGF) { 224 const llvm::Type *SrcTy = 225 cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); 226 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy); 227 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty); 228 229 // If load is legal, just bitcast the src pointer. 230 if (SrcSize >= DstSize) { 231 // Generally SrcSize is never greater than DstSize, since this means we are 232 // losing bits. However, this can happen in cases where the structure has 233 // additional padding, for example due to a user specified alignment. 234 // 235 // FIXME: Assert that we aren't truncating non-padding bits when have access 236 // to that information. 237 llvm::Value *Casted = 238 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty)); 239 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted); 240 // FIXME: Use better alignment / avoid requiring aligned load. 241 Load->setAlignment(1); 242 return Load; 243 } else { 244 // Otherwise do coercion through memory. This is stupid, but 245 // simple. 246 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty); 247 llvm::Value *Casted = 248 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy)); 249 llvm::StoreInst *Store = 250 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted); 251 // FIXME: Use better alignment / avoid requiring aligned store. 252 Store->setAlignment(1); 253 return CGF.Builder.CreateLoad(Tmp); 254 } 255 } 256 257 /// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src, 258 /// where the source and destination may have different types. 259 /// 260 /// This safely handles the case when the src type is larger than the 261 /// destination type; the upper bits of the src will be lost. 262 static void CreateCoercedStore(llvm::Value *Src, 263 llvm::Value *DstPtr, 264 CodeGenFunction &CGF) { 265 const llvm::Type *SrcTy = Src->getType(); 266 const llvm::Type *DstTy = 267 cast<llvm::PointerType>(DstPtr->getType())->getElementType(); 268 269 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy); 270 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy); 271 272 // If store is legal, just bitcast the src pointer. 273 if (SrcSize <= DstSize) { 274 llvm::Value *Casted = 275 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy)); 276 // FIXME: Use better alignment / avoid requiring aligned store. 277 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1); 278 } else { 279 // Otherwise do coercion through memory. This is stupid, but 280 // simple. 281 282 // Generally SrcSize is never greater than DstSize, since this means we are 283 // losing bits. However, this can happen in cases where the structure has 284 // additional padding, for example due to a user specified alignment. 285 // 286 // FIXME: Assert that we aren't truncating non-padding bits when have access 287 // to that information. 288 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy); 289 CGF.Builder.CreateStore(Src, Tmp); 290 llvm::Value *Casted = 291 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy)); 292 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted); 293 // FIXME: Use better alignment / avoid requiring aligned load. 294 Load->setAlignment(1); 295 CGF.Builder.CreateStore(Load, DstPtr); 296 } 297 } 298 299 /***/ 300 301 bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) { 302 return FI.getReturnInfo().isIndirect(); 303 } 304 305 const llvm::FunctionType * 306 CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) { 307 std::vector<const llvm::Type*> ArgTys; 308 309 const llvm::Type *ResultType = 0; 310 311 QualType RetTy = FI.getReturnType(); 312 const ABIArgInfo &RetAI = FI.getReturnInfo(); 313 switch (RetAI.getKind()) { 314 case ABIArgInfo::Expand: 315 assert(0 && "Invalid ABI kind for return argument"); 316 317 case ABIArgInfo::Extend: 318 case ABIArgInfo::Direct: 319 ResultType = ConvertType(RetTy); 320 break; 321 322 case ABIArgInfo::Indirect: { 323 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return."); 324 ResultType = llvm::Type::VoidTy; 325 const llvm::Type *STy = ConvertType(RetTy); 326 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace())); 327 break; 328 } 329 330 case ABIArgInfo::Ignore: 331 ResultType = llvm::Type::VoidTy; 332 break; 333 334 case ABIArgInfo::Coerce: 335 ResultType = RetAI.getCoerceToType(); 336 break; 337 } 338 339 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(), 340 ie = FI.arg_end(); it != ie; ++it) { 341 const ABIArgInfo &AI = it->info; 342 343 switch (AI.getKind()) { 344 case ABIArgInfo::Ignore: 345 break; 346 347 case ABIArgInfo::Coerce: 348 ArgTys.push_back(AI.getCoerceToType()); 349 break; 350 351 case ABIArgInfo::Indirect: { 352 // indirect arguments are always on the stack, which is addr space #0. 353 const llvm::Type *LTy = ConvertTypeForMem(it->type); 354 ArgTys.push_back(llvm::PointerType::getUnqual(LTy)); 355 break; 356 } 357 358 case ABIArgInfo::Extend: 359 case ABIArgInfo::Direct: 360 ArgTys.push_back(ConvertType(it->type)); 361 break; 362 363 case ABIArgInfo::Expand: 364 GetExpandedTypes(it->type, ArgTys); 365 break; 366 } 367 } 368 369 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic); 370 } 371 372 void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI, 373 const Decl *TargetDecl, 374 AttributeListType &PAL) { 375 unsigned FuncAttrs = 0; 376 unsigned RetAttrs = 0; 377 378 // FIXME: handle sseregparm someday... 379 if (TargetDecl) { 380 if (TargetDecl->hasAttr<NoThrowAttr>()) 381 FuncAttrs |= llvm::Attribute::NoUnwind; 382 if (TargetDecl->hasAttr<NoReturnAttr>()) 383 FuncAttrs |= llvm::Attribute::NoReturn; 384 if (TargetDecl->hasAttr<ConstAttr>()) 385 FuncAttrs |= llvm::Attribute::ReadNone; 386 else if (TargetDecl->hasAttr<PureAttr>()) 387 FuncAttrs |= llvm::Attribute::ReadOnly; 388 } 389 390 if (CompileOpts.DisableRedZone) 391 FuncAttrs |= llvm::Attribute::NoRedZone; 392 if (CompileOpts.NoImplicitFloat) 393 FuncAttrs |= llvm::Attribute::NoImplicitFloat; 394 395 QualType RetTy = FI.getReturnType(); 396 unsigned Index = 1; 397 const ABIArgInfo &RetAI = FI.getReturnInfo(); 398 switch (RetAI.getKind()) { 399 case ABIArgInfo::Extend: 400 if (RetTy->isSignedIntegerType()) { 401 RetAttrs |= llvm::Attribute::SExt; 402 } else if (RetTy->isUnsignedIntegerType()) { 403 RetAttrs |= llvm::Attribute::ZExt; 404 } 405 // FALLTHROUGH 406 case ABIArgInfo::Direct: 407 break; 408 409 case ABIArgInfo::Indirect: 410 PAL.push_back(llvm::AttributeWithIndex::get(Index, 411 llvm::Attribute::StructRet | 412 llvm::Attribute::NoAlias)); 413 ++Index; 414 // sret disables readnone and readonly 415 FuncAttrs &= ~(llvm::Attribute::ReadOnly | 416 llvm::Attribute::ReadNone); 417 break; 418 419 case ABIArgInfo::Ignore: 420 case ABIArgInfo::Coerce: 421 break; 422 423 case ABIArgInfo::Expand: 424 assert(0 && "Invalid ABI kind for return argument"); 425 } 426 427 if (RetAttrs) 428 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs)); 429 430 // FIXME: we need to honour command line settings also... 431 // FIXME: RegParm should be reduced in case of nested functions and/or global 432 // register variable. 433 signed RegParm = 0; 434 if (TargetDecl) 435 if (const RegparmAttr *RegParmAttr = TargetDecl->getAttr<RegparmAttr>()) 436 RegParm = RegParmAttr->getNumParams(); 437 438 unsigned PointerWidth = getContext().Target.getPointerWidth(0); 439 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(), 440 ie = FI.arg_end(); it != ie; ++it) { 441 QualType ParamType = it->type; 442 const ABIArgInfo &AI = it->info; 443 unsigned Attributes = 0; 444 445 switch (AI.getKind()) { 446 case ABIArgInfo::Coerce: 447 break; 448 449 case ABIArgInfo::Indirect: 450 Attributes |= llvm::Attribute::ByVal; 451 Attributes |= 452 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign()); 453 // byval disables readnone and readonly. 454 FuncAttrs &= ~(llvm::Attribute::ReadOnly | 455 llvm::Attribute::ReadNone); 456 break; 457 458 case ABIArgInfo::Extend: 459 if (ParamType->isSignedIntegerType()) { 460 Attributes |= llvm::Attribute::SExt; 461 } else if (ParamType->isUnsignedIntegerType()) { 462 Attributes |= llvm::Attribute::ZExt; 463 } 464 // FALLS THROUGH 465 case ABIArgInfo::Direct: 466 if (RegParm > 0 && 467 (ParamType->isIntegerType() || ParamType->isPointerType())) { 468 RegParm -= 469 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth; 470 if (RegParm >= 0) 471 Attributes |= llvm::Attribute::InReg; 472 } 473 // FIXME: handle sseregparm someday... 474 break; 475 476 case ABIArgInfo::Ignore: 477 // Skip increment, no matching LLVM parameter. 478 continue; 479 480 case ABIArgInfo::Expand: { 481 std::vector<const llvm::Type*> Tys; 482 // FIXME: This is rather inefficient. Do we ever actually need to do 483 // anything here? The result should be just reconstructed on the other 484 // side, so extension should be a non-issue. 485 getTypes().GetExpandedTypes(ParamType, Tys); 486 Index += Tys.size(); 487 continue; 488 } 489 } 490 491 if (Attributes) 492 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes)); 493 ++Index; 494 } 495 if (FuncAttrs) 496 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs)); 497 } 498 499 void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI, 500 llvm::Function *Fn, 501 const FunctionArgList &Args) { 502 // FIXME: We no longer need the types from FunctionArgList; lift up and 503 // simplify. 504 505 // Emit allocs for param decls. Give the LLVM Argument nodes names. 506 llvm::Function::arg_iterator AI = Fn->arg_begin(); 507 508 // Name the struct return argument. 509 if (CGM.ReturnTypeUsesSret(FI)) { 510 AI->setName("agg.result"); 511 ++AI; 512 } 513 514 assert(FI.arg_size() == Args.size() && 515 "Mismatch between function signature & arguments."); 516 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin(); 517 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); 518 i != e; ++i, ++info_it) { 519 const VarDecl *Arg = i->first; 520 QualType Ty = info_it->type; 521 const ABIArgInfo &ArgI = info_it->info; 522 523 switch (ArgI.getKind()) { 524 case ABIArgInfo::Indirect: { 525 llvm::Value* V = AI; 526 if (hasAggregateLLVMType(Ty)) { 527 // Do nothing, aggregates and complex variables are accessed by 528 // reference. 529 } else { 530 // Load scalar value from indirect argument. 531 V = EmitLoadOfScalar(V, false, Ty); 532 if (!getContext().typesAreCompatible(Ty, Arg->getType())) { 533 // This must be a promotion, for something like 534 // "void a(x) short x; {..." 535 V = EmitScalarConversion(V, Ty, Arg->getType()); 536 } 537 } 538 EmitParmDecl(*Arg, V); 539 break; 540 } 541 542 case ABIArgInfo::Extend: 543 case ABIArgInfo::Direct: { 544 assert(AI != Fn->arg_end() && "Argument mismatch!"); 545 llvm::Value* V = AI; 546 if (hasAggregateLLVMType(Ty)) { 547 // Create a temporary alloca to hold the argument; the rest of 548 // codegen expects to access aggregates & complex values by 549 // reference. 550 V = CreateTempAlloca(ConvertTypeForMem(Ty)); 551 Builder.CreateStore(AI, V); 552 } else { 553 if (!getContext().typesAreCompatible(Ty, Arg->getType())) { 554 // This must be a promotion, for something like 555 // "void a(x) short x; {..." 556 V = EmitScalarConversion(V, Ty, Arg->getType()); 557 } 558 } 559 EmitParmDecl(*Arg, V); 560 break; 561 } 562 563 case ABIArgInfo::Expand: { 564 // If this structure was expanded into multiple arguments then 565 // we need to create a temporary and reconstruct it from the 566 // arguments. 567 std::string Name = Arg->getNameAsString(); 568 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty), 569 (Name + ".addr").c_str()); 570 // FIXME: What are the right qualifiers here? 571 llvm::Function::arg_iterator End = 572 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI); 573 EmitParmDecl(*Arg, Temp); 574 575 // Name the arguments used in expansion and increment AI. 576 unsigned Index = 0; 577 for (; AI != End; ++AI, ++Index) 578 AI->setName(Name + "." + llvm::utostr(Index)); 579 continue; 580 } 581 582 case ABIArgInfo::Ignore: 583 // Initialize the local variable appropriately. 584 if (hasAggregateLLVMType(Ty)) { 585 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty))); 586 } else { 587 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType()))); 588 } 589 590 // Skip increment, no matching LLVM parameter. 591 continue; 592 593 case ABIArgInfo::Coerce: { 594 assert(AI != Fn->arg_end() && "Argument mismatch!"); 595 // FIXME: This is very wasteful; EmitParmDecl is just going to drop the 596 // result in a new alloca anyway, so we could just store into that 597 // directly if we broke the abstraction down more. 598 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce"); 599 CreateCoercedStore(AI, V, *this); 600 // Match to what EmitParmDecl is expecting for this type. 601 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) { 602 V = EmitLoadOfScalar(V, false, Ty); 603 if (!getContext().typesAreCompatible(Ty, Arg->getType())) { 604 // This must be a promotion, for something like 605 // "void a(x) short x; {..." 606 V = EmitScalarConversion(V, Ty, Arg->getType()); 607 } 608 } 609 EmitParmDecl(*Arg, V); 610 break; 611 } 612 } 613 614 ++AI; 615 } 616 assert(AI == Fn->arg_end() && "Argument mismatch!"); 617 } 618 619 void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI, 620 llvm::Value *ReturnValue) { 621 llvm::Value *RV = 0; 622 623 // Functions with no result always return void. 624 if (ReturnValue) { 625 QualType RetTy = FI.getReturnType(); 626 const ABIArgInfo &RetAI = FI.getReturnInfo(); 627 628 switch (RetAI.getKind()) { 629 case ABIArgInfo::Indirect: 630 if (RetTy->isAnyComplexType()) { 631 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false); 632 StoreComplexToAddr(RT, CurFn->arg_begin(), false); 633 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { 634 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy); 635 } else { 636 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(), 637 false, RetTy); 638 } 639 break; 640 641 case ABIArgInfo::Extend: 642 case ABIArgInfo::Direct: 643 // The internal return value temp always will have 644 // pointer-to-return-type type. 645 RV = Builder.CreateLoad(ReturnValue); 646 break; 647 648 case ABIArgInfo::Ignore: 649 break; 650 651 case ABIArgInfo::Coerce: 652 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this); 653 break; 654 655 case ABIArgInfo::Expand: 656 assert(0 && "Invalid ABI kind for return argument"); 657 } 658 } 659 660 if (RV) { 661 Builder.CreateRet(RV); 662 } else { 663 Builder.CreateRetVoid(); 664 } 665 } 666 667 RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) { 668 if (ArgType->isReferenceType()) 669 return EmitReferenceBindingToExpr(E, ArgType); 670 671 return EmitAnyExprToTemp(E); 672 } 673 674 RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, 675 llvm::Value *Callee, 676 const CallArgList &CallArgs, 677 const Decl *TargetDecl) { 678 // FIXME: We no longer need the types from CallArgs; lift up and simplify. 679 llvm::SmallVector<llvm::Value*, 16> Args; 680 681 // Handle struct-return functions by passing a pointer to the 682 // location that we would like to return into. 683 QualType RetTy = CallInfo.getReturnType(); 684 const ABIArgInfo &RetAI = CallInfo.getReturnInfo(); 685 if (CGM.ReturnTypeUsesSret(CallInfo)) { 686 // Create a temporary alloca to hold the result of the call. :( 687 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy))); 688 } 689 690 assert(CallInfo.arg_size() == CallArgs.size() && 691 "Mismatch between function signature & arguments."); 692 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin(); 693 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end(); 694 I != E; ++I, ++info_it) { 695 const ABIArgInfo &ArgInfo = info_it->info; 696 RValue RV = I->first; 697 698 switch (ArgInfo.getKind()) { 699 case ABIArgInfo::Indirect: 700 if (RV.isScalar() || RV.isComplex()) { 701 // Make a temporary alloca to pass the argument. 702 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second))); 703 if (RV.isScalar()) 704 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, I->second); 705 else 706 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false); 707 } else { 708 Args.push_back(RV.getAggregateAddr()); 709 } 710 break; 711 712 case ABIArgInfo::Extend: 713 case ABIArgInfo::Direct: 714 if (RV.isScalar()) { 715 Args.push_back(RV.getScalarVal()); 716 } else if (RV.isComplex()) { 717 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second)); 718 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0); 719 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1); 720 Args.push_back(Tmp); 721 } else { 722 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr())); 723 } 724 break; 725 726 case ABIArgInfo::Ignore: 727 break; 728 729 case ABIArgInfo::Coerce: { 730 // FIXME: Avoid the conversion through memory if possible. 731 llvm::Value *SrcPtr; 732 if (RV.isScalar()) { 733 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce"); 734 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, I->second); 735 } else if (RV.isComplex()) { 736 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce"); 737 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false); 738 } else 739 SrcPtr = RV.getAggregateAddr(); 740 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(), 741 *this)); 742 break; 743 } 744 745 case ABIArgInfo::Expand: 746 ExpandTypeToArgs(I->second, RV, Args); 747 break; 748 } 749 } 750 751 llvm::BasicBlock *InvokeDest = getInvokeDest(); 752 CodeGen::AttributeListType AttributeList; 753 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList); 754 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(), 755 AttributeList.end()); 756 757 llvm::CallSite CS; 758 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) { 759 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size()); 760 } else { 761 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont"); 762 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest, 763 Args.data(), Args.data()+Args.size()); 764 EmitBlock(Cont); 765 } 766 767 CS.setAttributes(Attrs); 768 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee->stripPointerCasts())) 769 CS.setCallingConv(F->getCallingConv()); 770 771 // If the call doesn't return, finish the basic block and clear the 772 // insertion point; this allows the rest of IRgen to discard 773 // unreachable code. 774 if (CS.doesNotReturn()) { 775 Builder.CreateUnreachable(); 776 Builder.ClearInsertionPoint(); 777 778 // FIXME: For now, emit a dummy basic block because expr emitters in 779 // generally are not ready to handle emitting expressions at unreachable 780 // points. 781 EnsureInsertPoint(); 782 783 // Return a reasonable RValue. 784 return GetUndefRValue(RetTy); 785 } 786 787 llvm::Instruction *CI = CS.getInstruction(); 788 if (Builder.isNamePreserving() && CI->getType() != llvm::Type::VoidTy) 789 CI->setName("call"); 790 791 switch (RetAI.getKind()) { 792 case ABIArgInfo::Indirect: 793 if (RetTy->isAnyComplexType()) 794 return RValue::getComplex(LoadComplexFromAddr(Args[0], false)); 795 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) 796 return RValue::getAggregate(Args[0]); 797 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy)); 798 799 case ABIArgInfo::Extend: 800 case ABIArgInfo::Direct: 801 if (RetTy->isAnyComplexType()) { 802 llvm::Value *Real = Builder.CreateExtractValue(CI, 0); 803 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1); 804 return RValue::getComplex(std::make_pair(Real, Imag)); 805 } 806 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { 807 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp"); 808 Builder.CreateStore(CI, V); 809 return RValue::getAggregate(V); 810 } 811 return RValue::get(CI); 812 813 case ABIArgInfo::Ignore: 814 // If we are ignoring an argument that had a result, make sure to 815 // construct the appropriate return value for our caller. 816 return GetUndefRValue(RetTy); 817 818 case ABIArgInfo::Coerce: { 819 // FIXME: Avoid the conversion through memory if possible. 820 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce"); 821 CreateCoercedStore(CI, V, *this); 822 if (RetTy->isAnyComplexType()) 823 return RValue::getComplex(LoadComplexFromAddr(V, false)); 824 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) 825 return RValue::getAggregate(V); 826 return RValue::get(EmitLoadOfScalar(V, false, RetTy)); 827 } 828 829 case ABIArgInfo::Expand: 830 assert(0 && "Invalid ABI kind for return argument"); 831 } 832 833 assert(0 && "Unhandled ABIArgInfo::Kind"); 834 return RValue::get(0); 835 } 836 837 /* VarArg handling */ 838 839 llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) { 840 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this); 841 } 842