1 //===--- CGCall.cpp - 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 "CGCXXABI.h" 17 #include "ABIInfo.h" 18 #include "CodeGenFunction.h" 19 #include "CodeGenModule.h" 20 #include "clang/Basic/TargetInfo.h" 21 #include "clang/AST/Decl.h" 22 #include "clang/AST/DeclCXX.h" 23 #include "clang/AST/DeclObjC.h" 24 #include "clang/Frontend/CodeGenOptions.h" 25 #include "llvm/Attributes.h" 26 #include "llvm/Support/CallSite.h" 27 #include "llvm/Target/TargetData.h" 28 #include "llvm/InlineAsm.h" 29 #include "llvm/Transforms/Utils/Local.h" 30 using namespace clang; 31 using namespace CodeGen; 32 33 /***/ 34 35 static unsigned ClangCallConvToLLVMCallConv(CallingConv CC) { 36 switch (CC) { 37 default: return llvm::CallingConv::C; 38 case CC_X86StdCall: return llvm::CallingConv::X86_StdCall; 39 case CC_X86FastCall: return llvm::CallingConv::X86_FastCall; 40 case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall; 41 case CC_AAPCS: return llvm::CallingConv::ARM_AAPCS; 42 case CC_AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; 43 // TODO: add support for CC_X86Pascal to llvm 44 } 45 } 46 47 /// Derives the 'this' type for codegen purposes, i.e. ignoring method 48 /// qualification. 49 /// FIXME: address space qualification? 50 static CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) { 51 QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal(); 52 return Context.getPointerType(CanQualType::CreateUnsafe(RecTy)); 53 } 54 55 /// Returns the canonical formal type of the given C++ method. 56 static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) { 57 return MD->getType()->getCanonicalTypeUnqualified() 58 .getAs<FunctionProtoType>(); 59 } 60 61 /// Returns the "extra-canonicalized" return type, which discards 62 /// qualifiers on the return type. Codegen doesn't care about them, 63 /// and it makes ABI code a little easier to be able to assume that 64 /// all parameter and return types are top-level unqualified. 65 static CanQualType GetReturnType(QualType RetTy) { 66 return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType(); 67 } 68 69 const CGFunctionInfo & 70 CodeGenTypes::getFunctionInfo(CanQual<FunctionNoProtoType> FTNP) { 71 return getFunctionInfo(FTNP->getResultType().getUnqualifiedType(), 72 SmallVector<CanQualType, 16>(), 73 FTNP->getExtInfo()); 74 } 75 76 /// \param Args - contains any initial parameters besides those 77 /// in the formal type 78 static const CGFunctionInfo &getFunctionInfo(CodeGenTypes &CGT, 79 SmallVectorImpl<CanQualType> &ArgTys, 80 CanQual<FunctionProtoType> FTP) { 81 // FIXME: Kill copy. 82 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) 83 ArgTys.push_back(FTP->getArgType(i)); 84 CanQualType ResTy = FTP->getResultType().getUnqualifiedType(); 85 return CGT.getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo()); 86 } 87 88 const CGFunctionInfo & 89 CodeGenTypes::getFunctionInfo(CanQual<FunctionProtoType> FTP) { 90 SmallVector<CanQualType, 16> ArgTys; 91 return ::getFunctionInfo(*this, ArgTys, FTP); 92 } 93 94 static CallingConv getCallingConventionForDecl(const Decl *D) { 95 // Set the appropriate calling convention for the Function. 96 if (D->hasAttr<StdCallAttr>()) 97 return CC_X86StdCall; 98 99 if (D->hasAttr<FastCallAttr>()) 100 return CC_X86FastCall; 101 102 if (D->hasAttr<ThisCallAttr>()) 103 return CC_X86ThisCall; 104 105 if (D->hasAttr<PascalAttr>()) 106 return CC_X86Pascal; 107 108 if (PcsAttr *PCS = D->getAttr<PcsAttr>()) 109 return (PCS->getPCS() == PcsAttr::AAPCS ? CC_AAPCS : CC_AAPCS_VFP); 110 111 return CC_C; 112 } 113 114 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXRecordDecl *RD, 115 const FunctionProtoType *FTP) { 116 SmallVector<CanQualType, 16> ArgTys; 117 118 // Add the 'this' pointer. 119 ArgTys.push_back(GetThisType(Context, RD)); 120 121 return ::getFunctionInfo(*this, ArgTys, 122 FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>()); 123 } 124 125 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) { 126 SmallVector<CanQualType, 16> ArgTys; 127 128 assert(!isa<CXXConstructorDecl>(MD) && "wrong method for contructors!"); 129 assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!"); 130 131 // Add the 'this' pointer unless this is a static method. 132 if (MD->isInstance()) 133 ArgTys.push_back(GetThisType(Context, MD->getParent())); 134 135 return ::getFunctionInfo(*this, ArgTys, GetFormalType(MD)); 136 } 137 138 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D, 139 CXXCtorType Type) { 140 SmallVector<CanQualType, 16> ArgTys; 141 ArgTys.push_back(GetThisType(Context, D->getParent())); 142 CanQualType ResTy = Context.VoidTy; 143 144 TheCXXABI.BuildConstructorSignature(D, Type, ResTy, ArgTys); 145 146 CanQual<FunctionProtoType> FTP = GetFormalType(D); 147 148 // Add the formal parameters. 149 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) 150 ArgTys.push_back(FTP->getArgType(i)); 151 152 return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo()); 153 } 154 155 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D, 156 CXXDtorType Type) { 157 SmallVector<CanQualType, 2> ArgTys; 158 ArgTys.push_back(GetThisType(Context, D->getParent())); 159 CanQualType ResTy = Context.VoidTy; 160 161 TheCXXABI.BuildDestructorSignature(D, Type, ResTy, ArgTys); 162 163 CanQual<FunctionProtoType> FTP = GetFormalType(D); 164 assert(FTP->getNumArgs() == 0 && "dtor with formal parameters"); 165 166 return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo()); 167 } 168 169 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) { 170 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) 171 if (MD->isInstance()) 172 return getFunctionInfo(MD); 173 174 CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified(); 175 assert(isa<FunctionType>(FTy)); 176 if (isa<FunctionNoProtoType>(FTy)) 177 return getFunctionInfo(FTy.getAs<FunctionNoProtoType>()); 178 assert(isa<FunctionProtoType>(FTy)); 179 return getFunctionInfo(FTy.getAs<FunctionProtoType>()); 180 } 181 182 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) { 183 SmallVector<CanQualType, 16> ArgTys; 184 ArgTys.push_back(Context.getCanonicalParamType(MD->getSelfDecl()->getType())); 185 ArgTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType())); 186 // FIXME: Kill copy? 187 for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(), 188 e = MD->param_end(); i != e; ++i) { 189 ArgTys.push_back(Context.getCanonicalParamType((*i)->getType())); 190 } 191 192 FunctionType::ExtInfo einfo; 193 einfo = einfo.withCallingConv(getCallingConventionForDecl(MD)); 194 195 if (getContext().getLangOptions().ObjCAutoRefCount && 196 MD->hasAttr<NSReturnsRetainedAttr>()) 197 einfo = einfo.withProducesResult(true); 198 199 return getFunctionInfo(GetReturnType(MD->getResultType()), ArgTys, einfo); 200 } 201 202 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(GlobalDecl GD) { 203 // FIXME: Do we need to handle ObjCMethodDecl? 204 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); 205 206 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) 207 return getFunctionInfo(CD, GD.getCtorType()); 208 209 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD)) 210 return getFunctionInfo(DD, GD.getDtorType()); 211 212 return getFunctionInfo(FD); 213 } 214 215 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy, 216 const CallArgList &Args, 217 const FunctionType::ExtInfo &Info) { 218 // FIXME: Kill copy. 219 SmallVector<CanQualType, 16> ArgTys; 220 for (CallArgList::const_iterator i = Args.begin(), e = Args.end(); 221 i != e; ++i) 222 ArgTys.push_back(Context.getCanonicalParamType(i->Ty)); 223 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info); 224 } 225 226 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy, 227 const FunctionArgList &Args, 228 const FunctionType::ExtInfo &Info) { 229 // FIXME: Kill copy. 230 SmallVector<CanQualType, 16> ArgTys; 231 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); 232 i != e; ++i) 233 ArgTys.push_back(Context.getCanonicalParamType((*i)->getType())); 234 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info); 235 } 236 237 const CGFunctionInfo &CodeGenTypes::getNullaryFunctionInfo() { 238 SmallVector<CanQualType, 1> args; 239 return getFunctionInfo(getContext().VoidTy, args, FunctionType::ExtInfo()); 240 } 241 242 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(CanQualType ResTy, 243 const SmallVectorImpl<CanQualType> &ArgTys, 244 const FunctionType::ExtInfo &Info) { 245 #ifndef NDEBUG 246 for (SmallVectorImpl<CanQualType>::const_iterator 247 I = ArgTys.begin(), E = ArgTys.end(); I != E; ++I) 248 assert(I->isCanonicalAsParam()); 249 #endif 250 251 unsigned CC = ClangCallConvToLLVMCallConv(Info.getCC()); 252 253 // Lookup or create unique function info. 254 llvm::FoldingSetNodeID ID; 255 CGFunctionInfo::Profile(ID, Info, ResTy, ArgTys.begin(), ArgTys.end()); 256 257 void *InsertPos = 0; 258 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos); 259 if (FI) 260 return *FI; 261 262 // Construct the function info. 263 FI = new CGFunctionInfo(CC, Info.getNoReturn(), Info.getProducesResult(), 264 Info.getHasRegParm(), Info.getRegParm(), ResTy, 265 ArgTys.data(), ArgTys.size()); 266 FunctionInfos.InsertNode(FI, InsertPos); 267 268 bool Inserted = FunctionsBeingProcessed.insert(FI); (void)Inserted; 269 assert(Inserted && "Recursively being processed?"); 270 271 // Compute ABI information. 272 getABIInfo().computeInfo(*FI); 273 274 // Loop over all of the computed argument and return value info. If any of 275 // them are direct or extend without a specified coerce type, specify the 276 // default now. 277 ABIArgInfo &RetInfo = FI->getReturnInfo(); 278 if (RetInfo.canHaveCoerceToType() && RetInfo.getCoerceToType() == 0) 279 RetInfo.setCoerceToType(ConvertType(FI->getReturnType())); 280 281 for (CGFunctionInfo::arg_iterator I = FI->arg_begin(), E = FI->arg_end(); 282 I != E; ++I) 283 if (I->info.canHaveCoerceToType() && I->info.getCoerceToType() == 0) 284 I->info.setCoerceToType(ConvertType(I->type)); 285 286 bool Erased = FunctionsBeingProcessed.erase(FI); (void)Erased; 287 assert(Erased && "Not in set?"); 288 289 return *FI; 290 } 291 292 CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention, 293 bool _NoReturn, bool returnsRetained, 294 bool _HasRegParm, unsigned _RegParm, 295 CanQualType ResTy, 296 const CanQualType *ArgTys, 297 unsigned NumArgTys) 298 : CallingConvention(_CallingConvention), 299 EffectiveCallingConvention(_CallingConvention), 300 NoReturn(_NoReturn), ReturnsRetained(returnsRetained), 301 HasRegParm(_HasRegParm), RegParm(_RegParm) 302 { 303 NumArgs = NumArgTys; 304 305 // FIXME: Coallocate with the CGFunctionInfo object. 306 Args = new ArgInfo[1 + NumArgTys]; 307 Args[0].type = ResTy; 308 for (unsigned i = 0; i != NumArgTys; ++i) 309 Args[1 + i].type = ArgTys[i]; 310 } 311 312 /***/ 313 314 void CodeGenTypes::GetExpandedTypes(QualType type, 315 SmallVectorImpl<llvm::Type*> &expandedTypes) { 316 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(type)) { 317 uint64_t NumElts = AT->getSize().getZExtValue(); 318 for (uint64_t Elt = 0; Elt < NumElts; ++Elt) 319 GetExpandedTypes(AT->getElementType(), expandedTypes); 320 } else if (const RecordType *RT = type->getAsStructureType()) { 321 const RecordDecl *RD = RT->getDecl(); 322 assert(!RD->hasFlexibleArrayMember() && 323 "Cannot expand structure with flexible array."); 324 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 325 i != e; ++i) { 326 const FieldDecl *FD = *i; 327 assert(!FD->isBitField() && 328 "Cannot expand structure with bit-field members."); 329 GetExpandedTypes(FD->getType(), expandedTypes); 330 } 331 } else if (const ComplexType *CT = type->getAs<ComplexType>()) { 332 llvm::Type *EltTy = ConvertType(CT->getElementType()); 333 expandedTypes.push_back(EltTy); 334 expandedTypes.push_back(EltTy); 335 } else 336 expandedTypes.push_back(ConvertType(type)); 337 } 338 339 llvm::Function::arg_iterator 340 CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV, 341 llvm::Function::arg_iterator AI) { 342 assert(LV.isSimple() && 343 "Unexpected non-simple lvalue during struct expansion."); 344 llvm::Value *Addr = LV.getAddress(); 345 346 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { 347 unsigned NumElts = AT->getSize().getZExtValue(); 348 QualType EltTy = AT->getElementType(); 349 for (unsigned Elt = 0; Elt < NumElts; ++Elt) { 350 llvm::Value *EltAddr = Builder.CreateConstGEP2_32(Addr, 0, Elt); 351 LValue LV = MakeAddrLValue(EltAddr, EltTy); 352 AI = ExpandTypeFromArgs(EltTy, LV, AI); 353 } 354 } else if (const RecordType *RT = Ty->getAsStructureType()) { 355 RecordDecl *RD = RT->getDecl(); 356 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 357 i != e; ++i) { 358 FieldDecl *FD = *i; 359 QualType FT = FD->getType(); 360 361 // FIXME: What are the right qualifiers here? 362 LValue LV = EmitLValueForField(Addr, FD, 0); 363 AI = ExpandTypeFromArgs(FT, LV, AI); 364 } 365 } else if (const ComplexType *CT = Ty->getAs<ComplexType>()) { 366 QualType EltTy = CT->getElementType(); 367 llvm::Value *RealAddr = Builder.CreateStructGEP(Addr, 0, "real"); 368 EmitStoreThroughLValue(RValue::get(AI++), MakeAddrLValue(RealAddr, EltTy)); 369 llvm::Value *ImagAddr = Builder.CreateStructGEP(Addr, 1, "imag"); 370 EmitStoreThroughLValue(RValue::get(AI++), MakeAddrLValue(ImagAddr, EltTy)); 371 } else { 372 EmitStoreThroughLValue(RValue::get(AI), LV); 373 ++AI; 374 } 375 376 return AI; 377 } 378 379 /// EnterStructPointerForCoercedAccess - Given a struct pointer that we are 380 /// accessing some number of bytes out of it, try to gep into the struct to get 381 /// at its inner goodness. Dive as deep as possible without entering an element 382 /// with an in-memory size smaller than DstSize. 383 static llvm::Value * 384 EnterStructPointerForCoercedAccess(llvm::Value *SrcPtr, 385 llvm::StructType *SrcSTy, 386 uint64_t DstSize, CodeGenFunction &CGF) { 387 // We can't dive into a zero-element struct. 388 if (SrcSTy->getNumElements() == 0) return SrcPtr; 389 390 llvm::Type *FirstElt = SrcSTy->getElementType(0); 391 392 // If the first elt is at least as large as what we're looking for, or if the 393 // first element is the same size as the whole struct, we can enter it. 394 uint64_t FirstEltSize = 395 CGF.CGM.getTargetData().getTypeAllocSize(FirstElt); 396 if (FirstEltSize < DstSize && 397 FirstEltSize < CGF.CGM.getTargetData().getTypeAllocSize(SrcSTy)) 398 return SrcPtr; 399 400 // GEP into the first element. 401 SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive"); 402 403 // If the first element is a struct, recurse. 404 llvm::Type *SrcTy = 405 cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); 406 if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) 407 return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF); 408 409 return SrcPtr; 410 } 411 412 /// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both 413 /// are either integers or pointers. This does a truncation of the value if it 414 /// is too large or a zero extension if it is too small. 415 static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val, 416 llvm::Type *Ty, 417 CodeGenFunction &CGF) { 418 if (Val->getType() == Ty) 419 return Val; 420 421 if (isa<llvm::PointerType>(Val->getType())) { 422 // If this is Pointer->Pointer avoid conversion to and from int. 423 if (isa<llvm::PointerType>(Ty)) 424 return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val"); 425 426 // Convert the pointer to an integer so we can play with its width. 427 Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi"); 428 } 429 430 llvm::Type *DestIntTy = Ty; 431 if (isa<llvm::PointerType>(DestIntTy)) 432 DestIntTy = CGF.IntPtrTy; 433 434 if (Val->getType() != DestIntTy) 435 Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii"); 436 437 if (isa<llvm::PointerType>(Ty)) 438 Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip"); 439 return Val; 440 } 441 442 443 444 /// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as 445 /// a pointer to an object of type \arg Ty. 446 /// 447 /// This safely handles the case when the src type is smaller than the 448 /// destination type; in this situation the values of bits which not 449 /// present in the src are undefined. 450 static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr, 451 llvm::Type *Ty, 452 CodeGenFunction &CGF) { 453 llvm::Type *SrcTy = 454 cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); 455 456 // If SrcTy and Ty are the same, just do a load. 457 if (SrcTy == Ty) 458 return CGF.Builder.CreateLoad(SrcPtr); 459 460 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty); 461 462 if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) { 463 SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF); 464 SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); 465 } 466 467 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy); 468 469 // If the source and destination are integer or pointer types, just do an 470 // extension or truncation to the desired type. 471 if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) && 472 (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) { 473 llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr); 474 return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF); 475 } 476 477 // If load is legal, just bitcast the src pointer. 478 if (SrcSize >= DstSize) { 479 // Generally SrcSize is never greater than DstSize, since this means we are 480 // losing bits. However, this can happen in cases where the structure has 481 // additional padding, for example due to a user specified alignment. 482 // 483 // FIXME: Assert that we aren't truncating non-padding bits when have access 484 // to that information. 485 llvm::Value *Casted = 486 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty)); 487 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted); 488 // FIXME: Use better alignment / avoid requiring aligned load. 489 Load->setAlignment(1); 490 return Load; 491 } 492 493 // Otherwise do coercion through memory. This is stupid, but 494 // simple. 495 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty); 496 llvm::Value *Casted = 497 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy)); 498 llvm::StoreInst *Store = 499 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted); 500 // FIXME: Use better alignment / avoid requiring aligned store. 501 Store->setAlignment(1); 502 return CGF.Builder.CreateLoad(Tmp); 503 } 504 505 // Function to store a first-class aggregate into memory. We prefer to 506 // store the elements rather than the aggregate to be more friendly to 507 // fast-isel. 508 // FIXME: Do we need to recurse here? 509 static void BuildAggStore(CodeGenFunction &CGF, llvm::Value *Val, 510 llvm::Value *DestPtr, bool DestIsVolatile, 511 bool LowAlignment) { 512 // Prefer scalar stores to first-class aggregate stores. 513 if (llvm::StructType *STy = 514 dyn_cast<llvm::StructType>(Val->getType())) { 515 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { 516 llvm::Value *EltPtr = CGF.Builder.CreateConstGEP2_32(DestPtr, 0, i); 517 llvm::Value *Elt = CGF.Builder.CreateExtractValue(Val, i); 518 llvm::StoreInst *SI = CGF.Builder.CreateStore(Elt, EltPtr, 519 DestIsVolatile); 520 if (LowAlignment) 521 SI->setAlignment(1); 522 } 523 } else { 524 CGF.Builder.CreateStore(Val, DestPtr, DestIsVolatile); 525 } 526 } 527 528 /// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src, 529 /// where the source and destination may have different types. 530 /// 531 /// This safely handles the case when the src type is larger than the 532 /// destination type; the upper bits of the src will be lost. 533 static void CreateCoercedStore(llvm::Value *Src, 534 llvm::Value *DstPtr, 535 bool DstIsVolatile, 536 CodeGenFunction &CGF) { 537 llvm::Type *SrcTy = Src->getType(); 538 llvm::Type *DstTy = 539 cast<llvm::PointerType>(DstPtr->getType())->getElementType(); 540 if (SrcTy == DstTy) { 541 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile); 542 return; 543 } 544 545 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy); 546 547 if (llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) { 548 DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF); 549 DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType(); 550 } 551 552 // If the source and destination are integer or pointer types, just do an 553 // extension or truncation to the desired type. 554 if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) && 555 (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) { 556 Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF); 557 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile); 558 return; 559 } 560 561 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy); 562 563 // If store is legal, just bitcast the src pointer. 564 if (SrcSize <= DstSize) { 565 llvm::Value *Casted = 566 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy)); 567 // FIXME: Use better alignment / avoid requiring aligned store. 568 BuildAggStore(CGF, Src, Casted, DstIsVolatile, true); 569 } else { 570 // Otherwise do coercion through memory. This is stupid, but 571 // simple. 572 573 // Generally SrcSize is never greater than DstSize, since this means we are 574 // losing bits. However, this can happen in cases where the structure has 575 // additional padding, for example due to a user specified alignment. 576 // 577 // FIXME: Assert that we aren't truncating non-padding bits when have access 578 // to that information. 579 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy); 580 CGF.Builder.CreateStore(Src, Tmp); 581 llvm::Value *Casted = 582 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy)); 583 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted); 584 // FIXME: Use better alignment / avoid requiring aligned load. 585 Load->setAlignment(1); 586 CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile); 587 } 588 } 589 590 /***/ 591 592 bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) { 593 return FI.getReturnInfo().isIndirect(); 594 } 595 596 bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) { 597 if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) { 598 switch (BT->getKind()) { 599 default: 600 return false; 601 case BuiltinType::Float: 602 return getContext().getTargetInfo().useObjCFPRetForRealType(TargetInfo::Float); 603 case BuiltinType::Double: 604 return getContext().getTargetInfo().useObjCFPRetForRealType(TargetInfo::Double); 605 case BuiltinType::LongDouble: 606 return getContext().getTargetInfo().useObjCFPRetForRealType( 607 TargetInfo::LongDouble); 608 } 609 } 610 611 return false; 612 } 613 614 bool CodeGenModule::ReturnTypeUsesFP2Ret(QualType ResultType) { 615 if (const ComplexType *CT = ResultType->getAs<ComplexType>()) { 616 if (const BuiltinType *BT = CT->getElementType()->getAs<BuiltinType>()) { 617 if (BT->getKind() == BuiltinType::LongDouble) 618 return getContext().getTargetInfo().useObjCFP2RetForComplexLongDouble(); 619 } 620 } 621 622 return false; 623 } 624 625 llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) { 626 const CGFunctionInfo &FI = getFunctionInfo(GD); 627 628 // For definition purposes, don't consider a K&R function variadic. 629 bool Variadic = false; 630 if (const FunctionProtoType *FPT = 631 cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>()) 632 Variadic = FPT->isVariadic(); 633 634 return GetFunctionType(FI, Variadic); 635 } 636 637 llvm::FunctionType * 638 CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool isVariadic) { 639 640 bool Inserted = FunctionsBeingProcessed.insert(&FI); (void)Inserted; 641 assert(Inserted && "Recursively being processed?"); 642 643 SmallVector<llvm::Type*, 8> argTypes; 644 llvm::Type *resultType = 0; 645 646 const ABIArgInfo &retAI = FI.getReturnInfo(); 647 switch (retAI.getKind()) { 648 case ABIArgInfo::Expand: 649 llvm_unreachable("Invalid ABI kind for return argument"); 650 651 case ABIArgInfo::Extend: 652 case ABIArgInfo::Direct: 653 resultType = retAI.getCoerceToType(); 654 break; 655 656 case ABIArgInfo::Indirect: { 657 assert(!retAI.getIndirectAlign() && "Align unused on indirect return."); 658 resultType = llvm::Type::getVoidTy(getLLVMContext()); 659 660 QualType ret = FI.getReturnType(); 661 llvm::Type *ty = ConvertType(ret); 662 unsigned addressSpace = Context.getTargetAddressSpace(ret); 663 argTypes.push_back(llvm::PointerType::get(ty, addressSpace)); 664 break; 665 } 666 667 case ABIArgInfo::Ignore: 668 resultType = llvm::Type::getVoidTy(getLLVMContext()); 669 break; 670 } 671 672 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(), 673 ie = FI.arg_end(); it != ie; ++it) { 674 const ABIArgInfo &argAI = it->info; 675 676 switch (argAI.getKind()) { 677 case ABIArgInfo::Ignore: 678 break; 679 680 case ABIArgInfo::Indirect: { 681 // indirect arguments are always on the stack, which is addr space #0. 682 llvm::Type *LTy = ConvertTypeForMem(it->type); 683 argTypes.push_back(LTy->getPointerTo()); 684 break; 685 } 686 687 case ABIArgInfo::Extend: 688 case ABIArgInfo::Direct: { 689 // Insert a padding type to ensure proper alignment. 690 if (llvm::Type *PaddingType = argAI.getPaddingType()) 691 argTypes.push_back(PaddingType); 692 // If the coerce-to type is a first class aggregate, flatten it. Either 693 // way is semantically identical, but fast-isel and the optimizer 694 // generally likes scalar values better than FCAs. 695 llvm::Type *argType = argAI.getCoerceToType(); 696 if (llvm::StructType *st = dyn_cast<llvm::StructType>(argType)) { 697 for (unsigned i = 0, e = st->getNumElements(); i != e; ++i) 698 argTypes.push_back(st->getElementType(i)); 699 } else { 700 argTypes.push_back(argType); 701 } 702 break; 703 } 704 705 case ABIArgInfo::Expand: 706 GetExpandedTypes(it->type, argTypes); 707 break; 708 } 709 } 710 711 bool Erased = FunctionsBeingProcessed.erase(&FI); (void)Erased; 712 assert(Erased && "Not in set?"); 713 714 return llvm::FunctionType::get(resultType, argTypes, isVariadic); 715 } 716 717 llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) { 718 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 719 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 720 721 if (!isFuncTypeConvertible(FPT)) 722 return llvm::StructType::get(getLLVMContext()); 723 724 const CGFunctionInfo *Info; 725 if (isa<CXXDestructorDecl>(MD)) 726 Info = &getFunctionInfo(cast<CXXDestructorDecl>(MD), GD.getDtorType()); 727 else 728 Info = &getFunctionInfo(MD); 729 return GetFunctionType(*Info, FPT->isVariadic()); 730 } 731 732 void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI, 733 const Decl *TargetDecl, 734 AttributeListType &PAL, 735 unsigned &CallingConv) { 736 llvm::Attributes FuncAttrs; 737 llvm::Attributes RetAttrs; 738 739 CallingConv = FI.getEffectiveCallingConvention(); 740 741 if (FI.isNoReturn()) 742 FuncAttrs |= llvm::Attribute::NoReturn; 743 744 // FIXME: handle sseregparm someday... 745 if (TargetDecl) { 746 if (TargetDecl->hasAttr<ReturnsTwiceAttr>()) 747 FuncAttrs |= llvm::Attribute::ReturnsTwice; 748 if (TargetDecl->hasAttr<NoThrowAttr>()) 749 FuncAttrs |= llvm::Attribute::NoUnwind; 750 else if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) { 751 const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>(); 752 if (FPT && FPT->isNothrow(getContext())) 753 FuncAttrs |= llvm::Attribute::NoUnwind; 754 } 755 756 if (TargetDecl->hasAttr<NoReturnAttr>()) 757 FuncAttrs |= llvm::Attribute::NoReturn; 758 759 if (TargetDecl->hasAttr<ReturnsTwiceAttr>()) 760 FuncAttrs |= llvm::Attribute::ReturnsTwice; 761 762 // 'const' and 'pure' attribute functions are also nounwind. 763 if (TargetDecl->hasAttr<ConstAttr>()) { 764 FuncAttrs |= llvm::Attribute::ReadNone; 765 FuncAttrs |= llvm::Attribute::NoUnwind; 766 } else if (TargetDecl->hasAttr<PureAttr>()) { 767 FuncAttrs |= llvm::Attribute::ReadOnly; 768 FuncAttrs |= llvm::Attribute::NoUnwind; 769 } 770 if (TargetDecl->hasAttr<MallocAttr>()) 771 RetAttrs |= llvm::Attribute::NoAlias; 772 } 773 774 if (CodeGenOpts.OptimizeSize) 775 FuncAttrs |= llvm::Attribute::OptimizeForSize; 776 if (CodeGenOpts.DisableRedZone) 777 FuncAttrs |= llvm::Attribute::NoRedZone; 778 if (CodeGenOpts.NoImplicitFloat) 779 FuncAttrs |= llvm::Attribute::NoImplicitFloat; 780 781 QualType RetTy = FI.getReturnType(); 782 unsigned Index = 1; 783 const ABIArgInfo &RetAI = FI.getReturnInfo(); 784 switch (RetAI.getKind()) { 785 case ABIArgInfo::Extend: 786 if (RetTy->hasSignedIntegerRepresentation()) 787 RetAttrs |= llvm::Attribute::SExt; 788 else if (RetTy->hasUnsignedIntegerRepresentation()) 789 RetAttrs |= llvm::Attribute::ZExt; 790 break; 791 case ABIArgInfo::Direct: 792 case ABIArgInfo::Ignore: 793 break; 794 795 case ABIArgInfo::Indirect: 796 PAL.push_back(llvm::AttributeWithIndex::get(Index, 797 llvm::Attribute::StructRet)); 798 ++Index; 799 // sret disables readnone and readonly 800 FuncAttrs &= ~(llvm::Attribute::ReadOnly | 801 llvm::Attribute::ReadNone); 802 break; 803 804 case ABIArgInfo::Expand: 805 llvm_unreachable("Invalid ABI kind for return argument"); 806 } 807 808 if (RetAttrs) 809 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs)); 810 811 // FIXME: RegParm should be reduced in case of global register variable. 812 signed RegParm; 813 if (FI.getHasRegParm()) 814 RegParm = FI.getRegParm(); 815 else 816 RegParm = CodeGenOpts.NumRegisterParameters; 817 818 unsigned PointerWidth = getContext().getTargetInfo().getPointerWidth(0); 819 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(), 820 ie = FI.arg_end(); it != ie; ++it) { 821 QualType ParamType = it->type; 822 const ABIArgInfo &AI = it->info; 823 llvm::Attributes Attrs; 824 825 // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we 826 // have the corresponding parameter variable. It doesn't make 827 // sense to do it here because parameters are so messed up. 828 switch (AI.getKind()) { 829 case ABIArgInfo::Extend: 830 if (ParamType->isSignedIntegerOrEnumerationType()) 831 Attrs |= llvm::Attribute::SExt; 832 else if (ParamType->isUnsignedIntegerOrEnumerationType()) 833 Attrs |= llvm::Attribute::ZExt; 834 // FALL THROUGH 835 case ABIArgInfo::Direct: 836 if (RegParm > 0 && 837 (ParamType->isIntegerType() || ParamType->isPointerType() || 838 ParamType->isReferenceType())) { 839 RegParm -= 840 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth; 841 if (RegParm >= 0) 842 Attrs |= llvm::Attribute::InReg; 843 } 844 // FIXME: handle sseregparm someday... 845 846 // Increment Index if there is padding. 847 Index += (AI.getPaddingType() != 0); 848 849 if (llvm::StructType *STy = 850 dyn_cast<llvm::StructType>(AI.getCoerceToType())) 851 Index += STy->getNumElements()-1; // 1 will be added below. 852 break; 853 854 case ABIArgInfo::Indirect: 855 if (AI.getIndirectByVal()) 856 Attrs |= llvm::Attribute::ByVal; 857 858 Attrs |= 859 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign()); 860 // byval disables readnone and readonly. 861 FuncAttrs &= ~(llvm::Attribute::ReadOnly | 862 llvm::Attribute::ReadNone); 863 break; 864 865 case ABIArgInfo::Ignore: 866 // Skip increment, no matching LLVM parameter. 867 continue; 868 869 case ABIArgInfo::Expand: { 870 SmallVector<llvm::Type*, 8> types; 871 // FIXME: This is rather inefficient. Do we ever actually need to do 872 // anything here? The result should be just reconstructed on the other 873 // side, so extension should be a non-issue. 874 getTypes().GetExpandedTypes(ParamType, types); 875 Index += types.size(); 876 continue; 877 } 878 } 879 880 if (Attrs) 881 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attrs)); 882 ++Index; 883 } 884 if (FuncAttrs) 885 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs)); 886 } 887 888 /// An argument came in as a promoted argument; demote it back to its 889 /// declared type. 890 static llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF, 891 const VarDecl *var, 892 llvm::Value *value) { 893 llvm::Type *varType = CGF.ConvertType(var->getType()); 894 895 // This can happen with promotions that actually don't change the 896 // underlying type, like the enum promotions. 897 if (value->getType() == varType) return value; 898 899 assert((varType->isIntegerTy() || varType->isFloatingPointTy()) 900 && "unexpected promotion type"); 901 902 if (isa<llvm::IntegerType>(varType)) 903 return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote"); 904 905 return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote"); 906 } 907 908 void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI, 909 llvm::Function *Fn, 910 const FunctionArgList &Args) { 911 // If this is an implicit-return-zero function, go ahead and 912 // initialize the return value. TODO: it might be nice to have 913 // a more general mechanism for this that didn't require synthesized 914 // return statements. 915 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) { 916 if (FD->hasImplicitReturnZero()) { 917 QualType RetTy = FD->getResultType().getUnqualifiedType(); 918 llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy); 919 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy); 920 Builder.CreateStore(Zero, ReturnValue); 921 } 922 } 923 924 // FIXME: We no longer need the types from FunctionArgList; lift up and 925 // simplify. 926 927 // Emit allocs for param decls. Give the LLVM Argument nodes names. 928 llvm::Function::arg_iterator AI = Fn->arg_begin(); 929 930 // Name the struct return argument. 931 if (CGM.ReturnTypeUsesSRet(FI)) { 932 AI->setName("agg.result"); 933 AI->addAttr(llvm::Attribute::NoAlias); 934 ++AI; 935 } 936 937 assert(FI.arg_size() == Args.size() && 938 "Mismatch between function signature & arguments."); 939 unsigned ArgNo = 1; 940 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin(); 941 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); 942 i != e; ++i, ++info_it, ++ArgNo) { 943 const VarDecl *Arg = *i; 944 QualType Ty = info_it->type; 945 const ABIArgInfo &ArgI = info_it->info; 946 947 bool isPromoted = 948 isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted(); 949 950 switch (ArgI.getKind()) { 951 case ABIArgInfo::Indirect: { 952 llvm::Value *V = AI; 953 954 if (hasAggregateLLVMType(Ty)) { 955 // Aggregates and complex variables are accessed by reference. All we 956 // need to do is realign the value, if requested 957 if (ArgI.getIndirectRealign()) { 958 llvm::Value *AlignedTemp = CreateMemTemp(Ty, "coerce"); 959 960 // Copy from the incoming argument pointer to the temporary with the 961 // appropriate alignment. 962 // 963 // FIXME: We should have a common utility for generating an aggregate 964 // copy. 965 llvm::Type *I8PtrTy = Builder.getInt8PtrTy(); 966 CharUnits Size = getContext().getTypeSizeInChars(Ty); 967 llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy); 968 llvm::Value *Src = Builder.CreateBitCast(V, I8PtrTy); 969 Builder.CreateMemCpy(Dst, 970 Src, 971 llvm::ConstantInt::get(IntPtrTy, 972 Size.getQuantity()), 973 ArgI.getIndirectAlign(), 974 false); 975 V = AlignedTemp; 976 } 977 } else { 978 // Load scalar value from indirect argument. 979 CharUnits Alignment = getContext().getTypeAlignInChars(Ty); 980 V = EmitLoadOfScalar(V, false, Alignment.getQuantity(), Ty); 981 982 if (isPromoted) 983 V = emitArgumentDemotion(*this, Arg, V); 984 } 985 EmitParmDecl(*Arg, V, ArgNo); 986 break; 987 } 988 989 case ABIArgInfo::Extend: 990 case ABIArgInfo::Direct: { 991 // Skip the dummy padding argument. 992 if (ArgI.getPaddingType()) 993 ++AI; 994 995 // If we have the trivial case, handle it with no muss and fuss. 996 if (!isa<llvm::StructType>(ArgI.getCoerceToType()) && 997 ArgI.getCoerceToType() == ConvertType(Ty) && 998 ArgI.getDirectOffset() == 0) { 999 assert(AI != Fn->arg_end() && "Argument mismatch!"); 1000 llvm::Value *V = AI; 1001 1002 if (Arg->getType().isRestrictQualified()) 1003 AI->addAttr(llvm::Attribute::NoAlias); 1004 1005 // Ensure the argument is the correct type. 1006 if (V->getType() != ArgI.getCoerceToType()) 1007 V = Builder.CreateBitCast(V, ArgI.getCoerceToType()); 1008 1009 if (isPromoted) 1010 V = emitArgumentDemotion(*this, Arg, V); 1011 1012 EmitParmDecl(*Arg, V, ArgNo); 1013 break; 1014 } 1015 1016 llvm::AllocaInst *Alloca = CreateMemTemp(Ty, Arg->getName()); 1017 1018 // The alignment we need to use is the max of the requested alignment for 1019 // the argument plus the alignment required by our access code below. 1020 unsigned AlignmentToUse = 1021 CGM.getTargetData().getABITypeAlignment(ArgI.getCoerceToType()); 1022 AlignmentToUse = std::max(AlignmentToUse, 1023 (unsigned)getContext().getDeclAlign(Arg).getQuantity()); 1024 1025 Alloca->setAlignment(AlignmentToUse); 1026 llvm::Value *V = Alloca; 1027 llvm::Value *Ptr = V; // Pointer to store into. 1028 1029 // If the value is offset in memory, apply the offset now. 1030 if (unsigned Offs = ArgI.getDirectOffset()) { 1031 Ptr = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy()); 1032 Ptr = Builder.CreateConstGEP1_32(Ptr, Offs); 1033 Ptr = Builder.CreateBitCast(Ptr, 1034 llvm::PointerType::getUnqual(ArgI.getCoerceToType())); 1035 } 1036 1037 // If the coerce-to type is a first class aggregate, we flatten it and 1038 // pass the elements. Either way is semantically identical, but fast-isel 1039 // and the optimizer generally likes scalar values better than FCAs. 1040 llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgI.getCoerceToType()); 1041 if (STy && STy->getNumElements() > 1) { 1042 uint64_t SrcSize = CGM.getTargetData().getTypeAllocSize(STy); 1043 llvm::Type *DstTy = 1044 cast<llvm::PointerType>(Ptr->getType())->getElementType(); 1045 uint64_t DstSize = CGM.getTargetData().getTypeAllocSize(DstTy); 1046 1047 if (SrcSize <= DstSize) { 1048 Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy)); 1049 1050 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { 1051 assert(AI != Fn->arg_end() && "Argument mismatch!"); 1052 AI->setName(Arg->getName() + ".coerce" + Twine(i)); 1053 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i); 1054 Builder.CreateStore(AI++, EltPtr); 1055 } 1056 } else { 1057 llvm::AllocaInst *TempAlloca = 1058 CreateTempAlloca(ArgI.getCoerceToType(), "coerce"); 1059 TempAlloca->setAlignment(AlignmentToUse); 1060 llvm::Value *TempV = TempAlloca; 1061 1062 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { 1063 assert(AI != Fn->arg_end() && "Argument mismatch!"); 1064 AI->setName(Arg->getName() + ".coerce" + Twine(i)); 1065 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(TempV, 0, i); 1066 Builder.CreateStore(AI++, EltPtr); 1067 } 1068 1069 Builder.CreateMemCpy(Ptr, TempV, DstSize, AlignmentToUse); 1070 } 1071 } else { 1072 // Simple case, just do a coerced store of the argument into the alloca. 1073 assert(AI != Fn->arg_end() && "Argument mismatch!"); 1074 AI->setName(Arg->getName() + ".coerce"); 1075 CreateCoercedStore(AI++, Ptr, /*DestIsVolatile=*/false, *this); 1076 } 1077 1078 1079 // Match to what EmitParmDecl is expecting for this type. 1080 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) { 1081 V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty); 1082 if (isPromoted) 1083 V = emitArgumentDemotion(*this, Arg, V); 1084 } 1085 EmitParmDecl(*Arg, V, ArgNo); 1086 continue; // Skip ++AI increment, already done. 1087 } 1088 1089 case ABIArgInfo::Expand: { 1090 // If this structure was expanded into multiple arguments then 1091 // we need to create a temporary and reconstruct it from the 1092 // arguments. 1093 llvm::AllocaInst *Alloca = CreateMemTemp(Ty); 1094 CharUnits Align = getContext().getDeclAlign(Arg); 1095 Alloca->setAlignment(Align.getQuantity()); 1096 LValue LV = MakeAddrLValue(Alloca, Ty, Align); 1097 llvm::Function::arg_iterator End = ExpandTypeFromArgs(Ty, LV, AI); 1098 EmitParmDecl(*Arg, Alloca, ArgNo); 1099 1100 // Name the arguments used in expansion and increment AI. 1101 unsigned Index = 0; 1102 for (; AI != End; ++AI, ++Index) 1103 AI->setName(Arg->getName() + "." + Twine(Index)); 1104 continue; 1105 } 1106 1107 case ABIArgInfo::Ignore: 1108 // Initialize the local variable appropriately. 1109 if (hasAggregateLLVMType(Ty)) 1110 EmitParmDecl(*Arg, CreateMemTemp(Ty), ArgNo); 1111 else 1112 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())), 1113 ArgNo); 1114 1115 // Skip increment, no matching LLVM parameter. 1116 continue; 1117 } 1118 1119 ++AI; 1120 } 1121 assert(AI == Fn->arg_end() && "Argument mismatch!"); 1122 } 1123 1124 static void eraseUnusedBitCasts(llvm::Instruction *insn) { 1125 while (insn->use_empty()) { 1126 llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(insn); 1127 if (!bitcast) return; 1128 1129 // This is "safe" because we would have used a ConstantExpr otherwise. 1130 insn = cast<llvm::Instruction>(bitcast->getOperand(0)); 1131 bitcast->eraseFromParent(); 1132 } 1133 } 1134 1135 /// Try to emit a fused autorelease of a return result. 1136 static llvm::Value *tryEmitFusedAutoreleaseOfResult(CodeGenFunction &CGF, 1137 llvm::Value *result) { 1138 // We must be immediately followed the cast. 1139 llvm::BasicBlock *BB = CGF.Builder.GetInsertBlock(); 1140 if (BB->empty()) return 0; 1141 if (&BB->back() != result) return 0; 1142 1143 llvm::Type *resultType = result->getType(); 1144 1145 // result is in a BasicBlock and is therefore an Instruction. 1146 llvm::Instruction *generator = cast<llvm::Instruction>(result); 1147 1148 SmallVector<llvm::Instruction*,4> insnsToKill; 1149 1150 // Look for: 1151 // %generator = bitcast %type1* %generator2 to %type2* 1152 while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(generator)) { 1153 // We would have emitted this as a constant if the operand weren't 1154 // an Instruction. 1155 generator = cast<llvm::Instruction>(bitcast->getOperand(0)); 1156 1157 // Require the generator to be immediately followed by the cast. 1158 if (generator->getNextNode() != bitcast) 1159 return 0; 1160 1161 insnsToKill.push_back(bitcast); 1162 } 1163 1164 // Look for: 1165 // %generator = call i8* @objc_retain(i8* %originalResult) 1166 // or 1167 // %generator = call i8* @objc_retainAutoreleasedReturnValue(i8* %originalResult) 1168 llvm::CallInst *call = dyn_cast<llvm::CallInst>(generator); 1169 if (!call) return 0; 1170 1171 bool doRetainAutorelease; 1172 1173 if (call->getCalledValue() == CGF.CGM.getARCEntrypoints().objc_retain) { 1174 doRetainAutorelease = true; 1175 } else if (call->getCalledValue() == CGF.CGM.getARCEntrypoints() 1176 .objc_retainAutoreleasedReturnValue) { 1177 doRetainAutorelease = false; 1178 1179 // Look for an inline asm immediately preceding the call and kill it, too. 1180 llvm::Instruction *prev = call->getPrevNode(); 1181 if (llvm::CallInst *asmCall = dyn_cast_or_null<llvm::CallInst>(prev)) 1182 if (asmCall->getCalledValue() 1183 == CGF.CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker) 1184 insnsToKill.push_back(prev); 1185 } else { 1186 return 0; 1187 } 1188 1189 result = call->getArgOperand(0); 1190 insnsToKill.push_back(call); 1191 1192 // Keep killing bitcasts, for sanity. Note that we no longer care 1193 // about precise ordering as long as there's exactly one use. 1194 while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(result)) { 1195 if (!bitcast->hasOneUse()) break; 1196 insnsToKill.push_back(bitcast); 1197 result = bitcast->getOperand(0); 1198 } 1199 1200 // Delete all the unnecessary instructions, from latest to earliest. 1201 for (SmallVectorImpl<llvm::Instruction*>::iterator 1202 i = insnsToKill.begin(), e = insnsToKill.end(); i != e; ++i) 1203 (*i)->eraseFromParent(); 1204 1205 // Do the fused retain/autorelease if we were asked to. 1206 if (doRetainAutorelease) 1207 result = CGF.EmitARCRetainAutoreleaseReturnValue(result); 1208 1209 // Cast back to the result type. 1210 return CGF.Builder.CreateBitCast(result, resultType); 1211 } 1212 1213 /// If this is a +1 of the value of an immutable 'self', remove it. 1214 static llvm::Value *tryRemoveRetainOfSelf(CodeGenFunction &CGF, 1215 llvm::Value *result) { 1216 // This is only applicable to a method with an immutable 'self'. 1217 const ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(CGF.CurCodeDecl); 1218 if (!method) return 0; 1219 const VarDecl *self = method->getSelfDecl(); 1220 if (!self->getType().isConstQualified()) return 0; 1221 1222 // Look for a retain call. 1223 llvm::CallInst *retainCall = 1224 dyn_cast<llvm::CallInst>(result->stripPointerCasts()); 1225 if (!retainCall || 1226 retainCall->getCalledValue() != CGF.CGM.getARCEntrypoints().objc_retain) 1227 return 0; 1228 1229 // Look for an ordinary load of 'self'. 1230 llvm::Value *retainedValue = retainCall->getArgOperand(0); 1231 llvm::LoadInst *load = 1232 dyn_cast<llvm::LoadInst>(retainedValue->stripPointerCasts()); 1233 if (!load || load->isAtomic() || load->isVolatile() || 1234 load->getPointerOperand() != CGF.GetAddrOfLocalVar(self)) 1235 return 0; 1236 1237 // Okay! Burn it all down. This relies for correctness on the 1238 // assumption that the retain is emitted as part of the return and 1239 // that thereafter everything is used "linearly". 1240 llvm::Type *resultType = result->getType(); 1241 eraseUnusedBitCasts(cast<llvm::Instruction>(result)); 1242 assert(retainCall->use_empty()); 1243 retainCall->eraseFromParent(); 1244 eraseUnusedBitCasts(cast<llvm::Instruction>(retainedValue)); 1245 1246 return CGF.Builder.CreateBitCast(load, resultType); 1247 } 1248 1249 /// Emit an ARC autorelease of the result of a function. 1250 /// 1251 /// \return the value to actually return from the function 1252 static llvm::Value *emitAutoreleaseOfResult(CodeGenFunction &CGF, 1253 llvm::Value *result) { 1254 // If we're returning 'self', kill the initial retain. This is a 1255 // heuristic attempt to "encourage correctness" in the really unfortunate 1256 // case where we have a return of self during a dealloc and we desperately 1257 // need to avoid the possible autorelease. 1258 if (llvm::Value *self = tryRemoveRetainOfSelf(CGF, result)) 1259 return self; 1260 1261 // At -O0, try to emit a fused retain/autorelease. 1262 if (CGF.shouldUseFusedARCCalls()) 1263 if (llvm::Value *fused = tryEmitFusedAutoreleaseOfResult(CGF, result)) 1264 return fused; 1265 1266 return CGF.EmitARCAutoreleaseReturnValue(result); 1267 } 1268 1269 /// Heuristically search for a dominating store to the return-value slot. 1270 static llvm::StoreInst *findDominatingStoreToReturnValue(CodeGenFunction &CGF) { 1271 // If there are multiple uses of the return-value slot, just check 1272 // for something immediately preceding the IP. Sometimes this can 1273 // happen with how we generate implicit-returns; it can also happen 1274 // with noreturn cleanups. 1275 if (!CGF.ReturnValue->hasOneUse()) { 1276 llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock(); 1277 if (IP->empty()) return 0; 1278 llvm::StoreInst *store = dyn_cast<llvm::StoreInst>(&IP->back()); 1279 if (!store) return 0; 1280 if (store->getPointerOperand() != CGF.ReturnValue) return 0; 1281 assert(!store->isAtomic() && !store->isVolatile()); // see below 1282 return store; 1283 } 1284 1285 llvm::StoreInst *store = 1286 dyn_cast<llvm::StoreInst>(CGF.ReturnValue->use_back()); 1287 if (!store) return 0; 1288 1289 // These aren't actually possible for non-coerced returns, and we 1290 // only care about non-coerced returns on this code path. 1291 assert(!store->isAtomic() && !store->isVolatile()); 1292 1293 // Now do a first-and-dirty dominance check: just walk up the 1294 // single-predecessors chain from the current insertion point. 1295 llvm::BasicBlock *StoreBB = store->getParent(); 1296 llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock(); 1297 while (IP != StoreBB) { 1298 if (!(IP = IP->getSinglePredecessor())) 1299 return 0; 1300 } 1301 1302 // Okay, the store's basic block dominates the insertion point; we 1303 // can do our thing. 1304 return store; 1305 } 1306 1307 void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) { 1308 // Functions with no result always return void. 1309 if (ReturnValue == 0) { 1310 Builder.CreateRetVoid(); 1311 return; 1312 } 1313 1314 llvm::DebugLoc RetDbgLoc; 1315 llvm::Value *RV = 0; 1316 QualType RetTy = FI.getReturnType(); 1317 const ABIArgInfo &RetAI = FI.getReturnInfo(); 1318 1319 switch (RetAI.getKind()) { 1320 case ABIArgInfo::Indirect: { 1321 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity(); 1322 if (RetTy->isAnyComplexType()) { 1323 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false); 1324 StoreComplexToAddr(RT, CurFn->arg_begin(), false); 1325 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { 1326 // Do nothing; aggregrates get evaluated directly into the destination. 1327 } else { 1328 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(), 1329 false, Alignment, RetTy); 1330 } 1331 break; 1332 } 1333 1334 case ABIArgInfo::Extend: 1335 case ABIArgInfo::Direct: 1336 if (RetAI.getCoerceToType() == ConvertType(RetTy) && 1337 RetAI.getDirectOffset() == 0) { 1338 // The internal return value temp always will have pointer-to-return-type 1339 // type, just do a load. 1340 1341 // If there is a dominating store to ReturnValue, we can elide 1342 // the load, zap the store, and usually zap the alloca. 1343 if (llvm::StoreInst *SI = findDominatingStoreToReturnValue(*this)) { 1344 // Get the stored value and nuke the now-dead store. 1345 RetDbgLoc = SI->getDebugLoc(); 1346 RV = SI->getValueOperand(); 1347 SI->eraseFromParent(); 1348 1349 // If that was the only use of the return value, nuke it as well now. 1350 if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) { 1351 cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent(); 1352 ReturnValue = 0; 1353 } 1354 1355 // Otherwise, we have to do a simple load. 1356 } else { 1357 RV = Builder.CreateLoad(ReturnValue); 1358 } 1359 } else { 1360 llvm::Value *V = ReturnValue; 1361 // If the value is offset in memory, apply the offset now. 1362 if (unsigned Offs = RetAI.getDirectOffset()) { 1363 V = Builder.CreateBitCast(V, Builder.getInt8PtrTy()); 1364 V = Builder.CreateConstGEP1_32(V, Offs); 1365 V = Builder.CreateBitCast(V, 1366 llvm::PointerType::getUnqual(RetAI.getCoerceToType())); 1367 } 1368 1369 RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this); 1370 } 1371 1372 // In ARC, end functions that return a retainable type with a call 1373 // to objc_autoreleaseReturnValue. 1374 if (AutoreleaseResult) { 1375 assert(getLangOptions().ObjCAutoRefCount && 1376 !FI.isReturnsRetained() && 1377 RetTy->isObjCRetainableType()); 1378 RV = emitAutoreleaseOfResult(*this, RV); 1379 } 1380 1381 break; 1382 1383 case ABIArgInfo::Ignore: 1384 break; 1385 1386 case ABIArgInfo::Expand: 1387 llvm_unreachable("Invalid ABI kind for return argument"); 1388 } 1389 1390 llvm::Instruction *Ret = RV ? Builder.CreateRet(RV) : Builder.CreateRetVoid(); 1391 if (!RetDbgLoc.isUnknown()) 1392 Ret->setDebugLoc(RetDbgLoc); 1393 } 1394 1395 void CodeGenFunction::EmitDelegateCallArg(CallArgList &args, 1396 const VarDecl *param) { 1397 // StartFunction converted the ABI-lowered parameter(s) into a 1398 // local alloca. We need to turn that into an r-value suitable 1399 // for EmitCall. 1400 llvm::Value *local = GetAddrOfLocalVar(param); 1401 1402 QualType type = param->getType(); 1403 1404 // For the most part, we just need to load the alloca, except: 1405 // 1) aggregate r-values are actually pointers to temporaries, and 1406 // 2) references to aggregates are pointers directly to the aggregate. 1407 // I don't know why references to non-aggregates are different here. 1408 if (const ReferenceType *ref = type->getAs<ReferenceType>()) { 1409 if (hasAggregateLLVMType(ref->getPointeeType())) 1410 return args.add(RValue::getAggregate(local), type); 1411 1412 // Locals which are references to scalars are represented 1413 // with allocas holding the pointer. 1414 return args.add(RValue::get(Builder.CreateLoad(local)), type); 1415 } 1416 1417 if (type->isAnyComplexType()) { 1418 ComplexPairTy complex = LoadComplexFromAddr(local, /*volatile*/ false); 1419 return args.add(RValue::getComplex(complex), type); 1420 } 1421 1422 if (hasAggregateLLVMType(type)) 1423 return args.add(RValue::getAggregate(local), type); 1424 1425 unsigned alignment = getContext().getDeclAlign(param).getQuantity(); 1426 llvm::Value *value = EmitLoadOfScalar(local, false, alignment, type); 1427 return args.add(RValue::get(value), type); 1428 } 1429 1430 static bool isProvablyNull(llvm::Value *addr) { 1431 return isa<llvm::ConstantPointerNull>(addr); 1432 } 1433 1434 static bool isProvablyNonNull(llvm::Value *addr) { 1435 return isa<llvm::AllocaInst>(addr); 1436 } 1437 1438 /// Emit the actual writing-back of a writeback. 1439 static void emitWriteback(CodeGenFunction &CGF, 1440 const CallArgList::Writeback &writeback) { 1441 llvm::Value *srcAddr = writeback.Address; 1442 assert(!isProvablyNull(srcAddr) && 1443 "shouldn't have writeback for provably null argument"); 1444 1445 llvm::BasicBlock *contBB = 0; 1446 1447 // If the argument wasn't provably non-null, we need to null check 1448 // before doing the store. 1449 bool provablyNonNull = isProvablyNonNull(srcAddr); 1450 if (!provablyNonNull) { 1451 llvm::BasicBlock *writebackBB = CGF.createBasicBlock("icr.writeback"); 1452 contBB = CGF.createBasicBlock("icr.done"); 1453 1454 llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull"); 1455 CGF.Builder.CreateCondBr(isNull, contBB, writebackBB); 1456 CGF.EmitBlock(writebackBB); 1457 } 1458 1459 // Load the value to writeback. 1460 llvm::Value *value = CGF.Builder.CreateLoad(writeback.Temporary); 1461 1462 // Cast it back, in case we're writing an id to a Foo* or something. 1463 value = CGF.Builder.CreateBitCast(value, 1464 cast<llvm::PointerType>(srcAddr->getType())->getElementType(), 1465 "icr.writeback-cast"); 1466 1467 // Perform the writeback. 1468 QualType srcAddrType = writeback.AddressType; 1469 CGF.EmitStoreThroughLValue(RValue::get(value), 1470 CGF.MakeAddrLValue(srcAddr, srcAddrType)); 1471 1472 // Jump to the continuation block. 1473 if (!provablyNonNull) 1474 CGF.EmitBlock(contBB); 1475 } 1476 1477 static void emitWritebacks(CodeGenFunction &CGF, 1478 const CallArgList &args) { 1479 for (CallArgList::writeback_iterator 1480 i = args.writeback_begin(), e = args.writeback_end(); i != e; ++i) 1481 emitWriteback(CGF, *i); 1482 } 1483 1484 /// Emit an argument that's being passed call-by-writeback. That is, 1485 /// we are passing the address of 1486 static void emitWritebackArg(CodeGenFunction &CGF, CallArgList &args, 1487 const ObjCIndirectCopyRestoreExpr *CRE) { 1488 llvm::Value *srcAddr = CGF.EmitScalarExpr(CRE->getSubExpr()); 1489 1490 // The dest and src types don't necessarily match in LLVM terms 1491 // because of the crazy ObjC compatibility rules. 1492 1493 llvm::PointerType *destType = 1494 cast<llvm::PointerType>(CGF.ConvertType(CRE->getType())); 1495 1496 // If the address is a constant null, just pass the appropriate null. 1497 if (isProvablyNull(srcAddr)) { 1498 args.add(RValue::get(llvm::ConstantPointerNull::get(destType)), 1499 CRE->getType()); 1500 return; 1501 } 1502 1503 QualType srcAddrType = 1504 CRE->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType(); 1505 1506 // Create the temporary. 1507 llvm::Value *temp = CGF.CreateTempAlloca(destType->getElementType(), 1508 "icr.temp"); 1509 1510 // Zero-initialize it if we're not doing a copy-initialization. 1511 bool shouldCopy = CRE->shouldCopy(); 1512 if (!shouldCopy) { 1513 llvm::Value *null = 1514 llvm::ConstantPointerNull::get( 1515 cast<llvm::PointerType>(destType->getElementType())); 1516 CGF.Builder.CreateStore(null, temp); 1517 } 1518 1519 llvm::BasicBlock *contBB = 0; 1520 1521 // If the address is *not* known to be non-null, we need to switch. 1522 llvm::Value *finalArgument; 1523 1524 bool provablyNonNull = isProvablyNonNull(srcAddr); 1525 if (provablyNonNull) { 1526 finalArgument = temp; 1527 } else { 1528 llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull"); 1529 1530 finalArgument = CGF.Builder.CreateSelect(isNull, 1531 llvm::ConstantPointerNull::get(destType), 1532 temp, "icr.argument"); 1533 1534 // If we need to copy, then the load has to be conditional, which 1535 // means we need control flow. 1536 if (shouldCopy) { 1537 contBB = CGF.createBasicBlock("icr.cont"); 1538 llvm::BasicBlock *copyBB = CGF.createBasicBlock("icr.copy"); 1539 CGF.Builder.CreateCondBr(isNull, contBB, copyBB); 1540 CGF.EmitBlock(copyBB); 1541 } 1542 } 1543 1544 // Perform a copy if necessary. 1545 if (shouldCopy) { 1546 LValue srcLV = CGF.MakeAddrLValue(srcAddr, srcAddrType); 1547 RValue srcRV = CGF.EmitLoadOfLValue(srcLV); 1548 assert(srcRV.isScalar()); 1549 1550 llvm::Value *src = srcRV.getScalarVal(); 1551 src = CGF.Builder.CreateBitCast(src, destType->getElementType(), 1552 "icr.cast"); 1553 1554 // Use an ordinary store, not a store-to-lvalue. 1555 CGF.Builder.CreateStore(src, temp); 1556 } 1557 1558 // Finish the control flow if we needed it. 1559 if (shouldCopy && !provablyNonNull) 1560 CGF.EmitBlock(contBB); 1561 1562 args.addWriteback(srcAddr, srcAddrType, temp); 1563 args.add(RValue::get(finalArgument), CRE->getType()); 1564 } 1565 1566 void CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E, 1567 QualType type) { 1568 if (const ObjCIndirectCopyRestoreExpr *CRE 1569 = dyn_cast<ObjCIndirectCopyRestoreExpr>(E)) { 1570 assert(getContext().getLangOptions().ObjCAutoRefCount); 1571 assert(getContext().hasSameType(E->getType(), type)); 1572 return emitWritebackArg(*this, args, CRE); 1573 } 1574 1575 assert(type->isReferenceType() == E->isGLValue() && 1576 "reference binding to unmaterialized r-value!"); 1577 1578 if (E->isGLValue()) { 1579 assert(E->getObjectKind() == OK_Ordinary); 1580 return args.add(EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0), 1581 type); 1582 } 1583 1584 if (hasAggregateLLVMType(type) && !E->getType()->isAnyComplexType() && 1585 isa<ImplicitCastExpr>(E) && 1586 cast<CastExpr>(E)->getCastKind() == CK_LValueToRValue) { 1587 LValue L = EmitLValue(cast<CastExpr>(E)->getSubExpr()); 1588 assert(L.isSimple()); 1589 args.add(L.asAggregateRValue(), type, /*NeedsCopy*/true); 1590 return; 1591 } 1592 1593 args.add(EmitAnyExprToTemp(E), type); 1594 } 1595 1596 /// Emits a call or invoke instruction to the given function, depending 1597 /// on the current state of the EH stack. 1598 llvm::CallSite 1599 CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee, 1600 ArrayRef<llvm::Value *> Args, 1601 const Twine &Name) { 1602 llvm::BasicBlock *InvokeDest = getInvokeDest(); 1603 if (!InvokeDest) 1604 return Builder.CreateCall(Callee, Args, Name); 1605 1606 llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont"); 1607 llvm::InvokeInst *Invoke = Builder.CreateInvoke(Callee, ContBB, InvokeDest, 1608 Args, Name); 1609 EmitBlock(ContBB); 1610 return Invoke; 1611 } 1612 1613 llvm::CallSite 1614 CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee, 1615 const Twine &Name) { 1616 return EmitCallOrInvoke(Callee, ArrayRef<llvm::Value *>(), Name); 1617 } 1618 1619 static void checkArgMatches(llvm::Value *Elt, unsigned &ArgNo, 1620 llvm::FunctionType *FTy) { 1621 if (ArgNo < FTy->getNumParams()) 1622 assert(Elt->getType() == FTy->getParamType(ArgNo)); 1623 else 1624 assert(FTy->isVarArg()); 1625 ++ArgNo; 1626 } 1627 1628 void CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV, 1629 SmallVector<llvm::Value*,16> &Args, 1630 llvm::FunctionType *IRFuncTy) { 1631 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { 1632 unsigned NumElts = AT->getSize().getZExtValue(); 1633 QualType EltTy = AT->getElementType(); 1634 llvm::Value *Addr = RV.getAggregateAddr(); 1635 for (unsigned Elt = 0; Elt < NumElts; ++Elt) { 1636 llvm::Value *EltAddr = Builder.CreateConstGEP2_32(Addr, 0, Elt); 1637 LValue LV = MakeAddrLValue(EltAddr, EltTy); 1638 RValue EltRV; 1639 if (EltTy->isAnyComplexType()) 1640 // FIXME: Volatile? 1641 EltRV = RValue::getComplex(LoadComplexFromAddr(LV.getAddress(), false)); 1642 else if (CodeGenFunction::hasAggregateLLVMType(EltTy)) 1643 EltRV = LV.asAggregateRValue(); 1644 else 1645 EltRV = EmitLoadOfLValue(LV); 1646 ExpandTypeToArgs(EltTy, EltRV, Args, IRFuncTy); 1647 } 1648 } else if (const RecordType *RT = Ty->getAsStructureType()) { 1649 RecordDecl *RD = RT->getDecl(); 1650 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion"); 1651 llvm::Value *Addr = RV.getAggregateAddr(); 1652 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 1653 i != e; ++i) { 1654 FieldDecl *FD = *i; 1655 QualType FT = FD->getType(); 1656 1657 // FIXME: What are the right qualifiers here? 1658 LValue LV = EmitLValueForField(Addr, FD, 0); 1659 RValue FldRV; 1660 if (FT->isAnyComplexType()) 1661 // FIXME: Volatile? 1662 FldRV = RValue::getComplex(LoadComplexFromAddr(LV.getAddress(), false)); 1663 else if (CodeGenFunction::hasAggregateLLVMType(FT)) 1664 FldRV = LV.asAggregateRValue(); 1665 else 1666 FldRV = EmitLoadOfLValue(LV); 1667 ExpandTypeToArgs(FT, FldRV, Args, IRFuncTy); 1668 } 1669 } else if (Ty->isAnyComplexType()) { 1670 ComplexPairTy CV = RV.getComplexVal(); 1671 Args.push_back(CV.first); 1672 Args.push_back(CV.second); 1673 } else { 1674 assert(RV.isScalar() && 1675 "Unexpected non-scalar rvalue during struct expansion."); 1676 1677 // Insert a bitcast as needed. 1678 llvm::Value *V = RV.getScalarVal(); 1679 if (Args.size() < IRFuncTy->getNumParams() && 1680 V->getType() != IRFuncTy->getParamType(Args.size())) 1681 V = Builder.CreateBitCast(V, IRFuncTy->getParamType(Args.size())); 1682 1683 Args.push_back(V); 1684 } 1685 } 1686 1687 1688 RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, 1689 llvm::Value *Callee, 1690 ReturnValueSlot ReturnValue, 1691 const CallArgList &CallArgs, 1692 const Decl *TargetDecl, 1693 llvm::Instruction **callOrInvoke) { 1694 // FIXME: We no longer need the types from CallArgs; lift up and simplify. 1695 SmallVector<llvm::Value*, 16> Args; 1696 1697 // Handle struct-return functions by passing a pointer to the 1698 // location that we would like to return into. 1699 QualType RetTy = CallInfo.getReturnType(); 1700 const ABIArgInfo &RetAI = CallInfo.getReturnInfo(); 1701 1702 // IRArgNo - Keep track of the argument number in the callee we're looking at. 1703 unsigned IRArgNo = 0; 1704 llvm::FunctionType *IRFuncTy = 1705 cast<llvm::FunctionType>( 1706 cast<llvm::PointerType>(Callee->getType())->getElementType()); 1707 1708 // If the call returns a temporary with struct return, create a temporary 1709 // alloca to hold the result, unless one is given to us. 1710 if (CGM.ReturnTypeUsesSRet(CallInfo)) { 1711 llvm::Value *Value = ReturnValue.getValue(); 1712 if (!Value) 1713 Value = CreateMemTemp(RetTy); 1714 Args.push_back(Value); 1715 checkArgMatches(Value, IRArgNo, IRFuncTy); 1716 } 1717 1718 assert(CallInfo.arg_size() == CallArgs.size() && 1719 "Mismatch between function signature & arguments."); 1720 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin(); 1721 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end(); 1722 I != E; ++I, ++info_it) { 1723 const ABIArgInfo &ArgInfo = info_it->info; 1724 RValue RV = I->RV; 1725 1726 unsigned TypeAlign = 1727 getContext().getTypeAlignInChars(I->Ty).getQuantity(); 1728 switch (ArgInfo.getKind()) { 1729 case ABIArgInfo::Indirect: { 1730 if (RV.isScalar() || RV.isComplex()) { 1731 // Make a temporary alloca to pass the argument. 1732 llvm::AllocaInst *AI = CreateMemTemp(I->Ty); 1733 if (ArgInfo.getIndirectAlign() > AI->getAlignment()) 1734 AI->setAlignment(ArgInfo.getIndirectAlign()); 1735 Args.push_back(AI); 1736 1737 if (RV.isScalar()) 1738 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, 1739 TypeAlign, I->Ty); 1740 else 1741 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false); 1742 1743 // Validate argument match. 1744 checkArgMatches(AI, IRArgNo, IRFuncTy); 1745 } else { 1746 // We want to avoid creating an unnecessary temporary+copy here; 1747 // however, we need one in two cases: 1748 // 1. If the argument is not byval, and we are required to copy the 1749 // source. (This case doesn't occur on any common architecture.) 1750 // 2. If the argument is byval, RV is not sufficiently aligned, and 1751 // we cannot force it to be sufficiently aligned. 1752 llvm::Value *Addr = RV.getAggregateAddr(); 1753 unsigned Align = ArgInfo.getIndirectAlign(); 1754 const llvm::TargetData *TD = &CGM.getTargetData(); 1755 if ((!ArgInfo.getIndirectByVal() && I->NeedsCopy) || 1756 (ArgInfo.getIndirectByVal() && TypeAlign < Align && 1757 llvm::getOrEnforceKnownAlignment(Addr, Align, TD) < Align)) { 1758 // Create an aligned temporary, and copy to it. 1759 llvm::AllocaInst *AI = CreateMemTemp(I->Ty); 1760 if (Align > AI->getAlignment()) 1761 AI->setAlignment(Align); 1762 Args.push_back(AI); 1763 EmitAggregateCopy(AI, Addr, I->Ty, RV.isVolatileQualified()); 1764 1765 // Validate argument match. 1766 checkArgMatches(AI, IRArgNo, IRFuncTy); 1767 } else { 1768 // Skip the extra memcpy call. 1769 Args.push_back(Addr); 1770 1771 // Validate argument match. 1772 checkArgMatches(Addr, IRArgNo, IRFuncTy); 1773 } 1774 } 1775 break; 1776 } 1777 1778 case ABIArgInfo::Ignore: 1779 break; 1780 1781 case ABIArgInfo::Extend: 1782 case ABIArgInfo::Direct: { 1783 // Insert a padding argument to ensure proper alignment. 1784 if (llvm::Type *PaddingType = ArgInfo.getPaddingType()) { 1785 Args.push_back(llvm::UndefValue::get(PaddingType)); 1786 ++IRArgNo; 1787 } 1788 1789 if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) && 1790 ArgInfo.getCoerceToType() == ConvertType(info_it->type) && 1791 ArgInfo.getDirectOffset() == 0) { 1792 llvm::Value *V; 1793 if (RV.isScalar()) 1794 V = RV.getScalarVal(); 1795 else 1796 V = Builder.CreateLoad(RV.getAggregateAddr()); 1797 1798 // If the argument doesn't match, perform a bitcast to coerce it. This 1799 // can happen due to trivial type mismatches. 1800 if (IRArgNo < IRFuncTy->getNumParams() && 1801 V->getType() != IRFuncTy->getParamType(IRArgNo)) 1802 V = Builder.CreateBitCast(V, IRFuncTy->getParamType(IRArgNo)); 1803 Args.push_back(V); 1804 1805 checkArgMatches(V, IRArgNo, IRFuncTy); 1806 break; 1807 } 1808 1809 // FIXME: Avoid the conversion through memory if possible. 1810 llvm::Value *SrcPtr; 1811 if (RV.isScalar()) { 1812 SrcPtr = CreateMemTemp(I->Ty, "coerce"); 1813 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, TypeAlign, I->Ty); 1814 } else if (RV.isComplex()) { 1815 SrcPtr = CreateMemTemp(I->Ty, "coerce"); 1816 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false); 1817 } else 1818 SrcPtr = RV.getAggregateAddr(); 1819 1820 // If the value is offset in memory, apply the offset now. 1821 if (unsigned Offs = ArgInfo.getDirectOffset()) { 1822 SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy()); 1823 SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs); 1824 SrcPtr = Builder.CreateBitCast(SrcPtr, 1825 llvm::PointerType::getUnqual(ArgInfo.getCoerceToType())); 1826 1827 } 1828 1829 // If the coerce-to type is a first class aggregate, we flatten it and 1830 // pass the elements. Either way is semantically identical, but fast-isel 1831 // and the optimizer generally likes scalar values better than FCAs. 1832 if (llvm::StructType *STy = 1833 dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType())) { 1834 SrcPtr = Builder.CreateBitCast(SrcPtr, 1835 llvm::PointerType::getUnqual(STy)); 1836 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { 1837 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i); 1838 llvm::LoadInst *LI = Builder.CreateLoad(EltPtr); 1839 // We don't know what we're loading from. 1840 LI->setAlignment(1); 1841 Args.push_back(LI); 1842 1843 // Validate argument match. 1844 checkArgMatches(LI, IRArgNo, IRFuncTy); 1845 } 1846 } else { 1847 // In the simple case, just pass the coerced loaded value. 1848 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(), 1849 *this)); 1850 1851 // Validate argument match. 1852 checkArgMatches(Args.back(), IRArgNo, IRFuncTy); 1853 } 1854 1855 break; 1856 } 1857 1858 case ABIArgInfo::Expand: 1859 ExpandTypeToArgs(I->Ty, RV, Args, IRFuncTy); 1860 IRArgNo = Args.size(); 1861 break; 1862 } 1863 } 1864 1865 // If the callee is a bitcast of a function to a varargs pointer to function 1866 // type, check to see if we can remove the bitcast. This handles some cases 1867 // with unprototyped functions. 1868 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee)) 1869 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) { 1870 llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType()); 1871 llvm::FunctionType *CurFT = 1872 cast<llvm::FunctionType>(CurPT->getElementType()); 1873 llvm::FunctionType *ActualFT = CalleeF->getFunctionType(); 1874 1875 if (CE->getOpcode() == llvm::Instruction::BitCast && 1876 ActualFT->getReturnType() == CurFT->getReturnType() && 1877 ActualFT->getNumParams() == CurFT->getNumParams() && 1878 ActualFT->getNumParams() == Args.size() && 1879 (CurFT->isVarArg() || !ActualFT->isVarArg())) { 1880 bool ArgsMatch = true; 1881 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i) 1882 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) { 1883 ArgsMatch = false; 1884 break; 1885 } 1886 1887 // Strip the cast if we can get away with it. This is a nice cleanup, 1888 // but also allows us to inline the function at -O0 if it is marked 1889 // always_inline. 1890 if (ArgsMatch) 1891 Callee = CalleeF; 1892 } 1893 } 1894 1895 unsigned CallingConv; 1896 CodeGen::AttributeListType AttributeList; 1897 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv); 1898 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(), 1899 AttributeList.end()); 1900 1901 llvm::BasicBlock *InvokeDest = 0; 1902 if (!(Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) 1903 InvokeDest = getInvokeDest(); 1904 1905 llvm::CallSite CS; 1906 if (!InvokeDest) { 1907 CS = Builder.CreateCall(Callee, Args); 1908 } else { 1909 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont"); 1910 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest, Args); 1911 EmitBlock(Cont); 1912 } 1913 if (callOrInvoke) 1914 *callOrInvoke = CS.getInstruction(); 1915 1916 CS.setAttributes(Attrs); 1917 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv)); 1918 1919 // If the call doesn't return, finish the basic block and clear the 1920 // insertion point; this allows the rest of IRgen to discard 1921 // unreachable code. 1922 if (CS.doesNotReturn()) { 1923 Builder.CreateUnreachable(); 1924 Builder.ClearInsertionPoint(); 1925 1926 // FIXME: For now, emit a dummy basic block because expr emitters in 1927 // generally are not ready to handle emitting expressions at unreachable 1928 // points. 1929 EnsureInsertPoint(); 1930 1931 // Return a reasonable RValue. 1932 return GetUndefRValue(RetTy); 1933 } 1934 1935 llvm::Instruction *CI = CS.getInstruction(); 1936 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy()) 1937 CI->setName("call"); 1938 1939 // Emit any writebacks immediately. Arguably this should happen 1940 // after any return-value munging. 1941 if (CallArgs.hasWritebacks()) 1942 emitWritebacks(*this, CallArgs); 1943 1944 switch (RetAI.getKind()) { 1945 case ABIArgInfo::Indirect: { 1946 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity(); 1947 if (RetTy->isAnyComplexType()) 1948 return RValue::getComplex(LoadComplexFromAddr(Args[0], false)); 1949 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) 1950 return RValue::getAggregate(Args[0]); 1951 return RValue::get(EmitLoadOfScalar(Args[0], false, Alignment, RetTy)); 1952 } 1953 1954 case ABIArgInfo::Ignore: 1955 // If we are ignoring an argument that had a result, make sure to 1956 // construct the appropriate return value for our caller. 1957 return GetUndefRValue(RetTy); 1958 1959 case ABIArgInfo::Extend: 1960 case ABIArgInfo::Direct: { 1961 llvm::Type *RetIRTy = ConvertType(RetTy); 1962 if (RetAI.getCoerceToType() == RetIRTy && RetAI.getDirectOffset() == 0) { 1963 if (RetTy->isAnyComplexType()) { 1964 llvm::Value *Real = Builder.CreateExtractValue(CI, 0); 1965 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1); 1966 return RValue::getComplex(std::make_pair(Real, Imag)); 1967 } 1968 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { 1969 llvm::Value *DestPtr = ReturnValue.getValue(); 1970 bool DestIsVolatile = ReturnValue.isVolatile(); 1971 1972 if (!DestPtr) { 1973 DestPtr = CreateMemTemp(RetTy, "agg.tmp"); 1974 DestIsVolatile = false; 1975 } 1976 BuildAggStore(*this, CI, DestPtr, DestIsVolatile, false); 1977 return RValue::getAggregate(DestPtr); 1978 } 1979 1980 // If the argument doesn't match, perform a bitcast to coerce it. This 1981 // can happen due to trivial type mismatches. 1982 llvm::Value *V = CI; 1983 if (V->getType() != RetIRTy) 1984 V = Builder.CreateBitCast(V, RetIRTy); 1985 return RValue::get(V); 1986 } 1987 1988 llvm::Value *DestPtr = ReturnValue.getValue(); 1989 bool DestIsVolatile = ReturnValue.isVolatile(); 1990 1991 if (!DestPtr) { 1992 DestPtr = CreateMemTemp(RetTy, "coerce"); 1993 DestIsVolatile = false; 1994 } 1995 1996 // If the value is offset in memory, apply the offset now. 1997 llvm::Value *StorePtr = DestPtr; 1998 if (unsigned Offs = RetAI.getDirectOffset()) { 1999 StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy()); 2000 StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs); 2001 StorePtr = Builder.CreateBitCast(StorePtr, 2002 llvm::PointerType::getUnqual(RetAI.getCoerceToType())); 2003 } 2004 CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this); 2005 2006 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity(); 2007 if (RetTy->isAnyComplexType()) 2008 return RValue::getComplex(LoadComplexFromAddr(DestPtr, false)); 2009 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) 2010 return RValue::getAggregate(DestPtr); 2011 return RValue::get(EmitLoadOfScalar(DestPtr, false, Alignment, RetTy)); 2012 } 2013 2014 case ABIArgInfo::Expand: 2015 llvm_unreachable("Invalid ABI kind for return argument"); 2016 } 2017 2018 llvm_unreachable("Unhandled ABIArgInfo::Kind"); 2019 } 2020 2021 /* VarArg handling */ 2022 2023 llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) { 2024 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this); 2025 } 2026