 |
|
 |
|
| Files: |
1 |
|
Branches Taken: |
80.4% |
262 / 326 |
| Generated: |
2010-02-10 01:31 |
|
Branches Executed: |
96.3% |
314 / 326 |
| |
|
Line Coverage: |
91.6% |
458 / 500 |
| |
 |
|
 |
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/CodeGen/CodeGenOptions.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 14606: static unsigned ClangCallConvToLLVMCallConv(CallingConv CC) {
14586: branch 0 taken
9: branch 1 taken
11: branch 2 taken
37 14606: switch (CC) {
38 14586: default: return llvm::CallingConv::C;
39 9: case CC_X86StdCall: return llvm::CallingConv::X86_StdCall;
40 11: case CC_X86FastCall: return llvm::CallingConv::X86_FastCall;
41 : }
42 : }
43 :
44 : const
45 235: CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionNoProtoType *FTNP) {
46 : return getFunctionInfo(FTNP->getResultType(),
47 : llvm::SmallVector<QualType, 16>(),
48 235: FTNP->getCallConv(), FTNP->getNoReturnAttr());
49 : }
50 :
51 : const
52 1095: CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionProtoType *FTP) {
53 1095: llvm::SmallVector<QualType, 16> ArgTys;
54 : // FIXME: Kill copy.
936: branch 1 taken
1095: branch 2 taken
55 2031: for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
56 936: ArgTys.push_back(FTP->getArgType(i));
57 : return getFunctionInfo(FTP->getResultType(), ArgTys,
58 1095: FTP->getCallConv(), FTP->getNoReturnAttr());
59 : }
60 :
61 582: static CallingConv getCallingConventionForDecl(const Decl *D) {
62 : // Set the appropriate calling convention for the Function.
0: branch 1 not taken
582: branch 2 taken
63 582: if (D->hasAttr<StdCallAttr>())
64 0: return CC_X86StdCall;
65 :
0: branch 1 not taken
582: branch 2 taken
66 582: if (D->hasAttr<FastCallAttr>())
67 0: return CC_X86FastCall;
68 :
69 582: return CC_C;
70 : }
71 :
72 : const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXRecordDecl *RD,
73 10: const FunctionProtoType *FTP) {
74 10: llvm::SmallVector<QualType, 16> ArgTys;
75 :
76 : // Add the 'this' pointer.
77 10: ArgTys.push_back(Context.getPointerType(Context.getTagDeclType(RD)));
78 :
3: branch 1 taken
10: branch 2 taken
79 13: for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
80 3: ArgTys.push_back(FTP->getArgType(i));
81 :
82 : // FIXME: Set calling convention correctly, it needs to be associated with the
83 : // type somehow.
84 : return getFunctionInfo(FTP->getResultType(), ArgTys,
85 10: FTP->getCallConv(), FTP->getNoReturnAttr());
86 : }
87 :
88 2677: const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
89 2677: llvm::SmallVector<QualType, 16> ArgTys;
90 : // Add the 'this' pointer unless this is a static method.
2671: branch 1 taken
6: branch 2 taken
91 2677: if (MD->isInstance())
92 2671: ArgTys.push_back(MD->getThisType(Context));
93 :
94 2677: const FunctionProtoType *FTP = MD->getType()->getAs<FunctionProtoType>();
248: branch 1 taken
2677: branch 2 taken
95 2925: for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
96 248: ArgTys.push_back(FTP->getArgType(i));
97 : return getFunctionInfo(FTP->getResultType(), ArgTys, FTP->getCallConv(),
98 2677: FTP->getNoReturnAttr());
99 : }
100 :
101 : const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D,
102 1696: CXXCtorType Type) {
103 1696: llvm::SmallVector<QualType, 16> ArgTys;
104 :
105 : // Add the 'this' pointer.
106 1696: ArgTys.push_back(D->getThisType(Context));
107 :
108 : // Check if we need to add a VTT parameter (which has type void **).
749: branch 0 taken
947: branch 1 taken
250: branch 4 taken
499: branch 5 taken
250: branch 6 taken
1446: branch 7 taken
109 1696: if (Type == Ctor_Base && D->getParent()->getNumVBases() != 0)
110 250: ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
111 :
112 1696: const FunctionProtoType *FTP = D->getType()->getAs<FunctionProtoType>();
440: branch 1 taken
1696: branch 2 taken
113 2136: for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
114 440: ArgTys.push_back(FTP->getArgType(i));
115 : return getFunctionInfo(FTP->getResultType(), ArgTys, FTP->getCallConv(),
116 1696: FTP->getNoReturnAttr());
117 : }
118 :
119 : const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D,
120 475: CXXDtorType Type) {
121 475: llvm::SmallVector<QualType, 16> ArgTys;
122 :
123 : // Add the 'this' pointer.
124 475: ArgTys.push_back(D->getThisType(Context));
125 :
126 : // Check if we need to add a VTT parameter (which has type void **).
105: branch 0 taken
370: branch 1 taken
4: branch 4 taken
101: branch 5 taken
4: branch 6 taken
471: branch 7 taken
127 475: if (Type == Dtor_Base && D->getParent()->getNumVBases() != 0)
128 4: ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
129 :
130 475: const FunctionProtoType *FTP = D->getType()->getAs<FunctionProtoType>();
0: branch 1 not taken
475: branch 2 taken
131 475: for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
132 0: ArgTys.push_back(FTP->getArgType(i));
133 : return getFunctionInfo(FTP->getResultType(), ArgTys, FTP->getCallConv(),
134 475: FTP->getNoReturnAttr());
135 : }
136 :
137 2309: const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
532: branch 1 taken
1777: branch 2 taken
138 2309: if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
518: branch 1 taken
14: branch 2 taken
139 532: if (MD->isInstance())
140 518: return getFunctionInfo(MD);
141 :
142 1791: const FunctionType *FTy = FD->getType()->getAs<FunctionType>();
422: branch 1 taken
1369: branch 2 taken
143 1791: if (const FunctionNoProtoType *FNTP = dyn_cast<FunctionNoProtoType>(FTy))
144 : return getFunctionInfo(FNTP->getResultType(),
145 : llvm::SmallVector<QualType, 16>(),
146 422: FNTP->getCallConv(), FNTP->getNoReturnAttr());
147 :
148 1369: const FunctionProtoType *FPT = cast<FunctionProtoType>(FTy);
149 1369: llvm::SmallVector<QualType, 16> ArgTys;
150 : // FIXME: Kill copy.
1074: branch 1 taken
1369: branch 2 taken
151 2443: for (unsigned i = 0, e = FPT->getNumArgs(); i != e; ++i)
152 1074: ArgTys.push_back(FPT->getArgType(i));
153 : return getFunctionInfo(FPT->getResultType(), ArgTys,
154 1369: FPT->getCallConv(), FPT->getNoReturnAttr());
155 : }
156 :
157 582: const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
158 582: llvm::SmallVector<QualType, 16> ArgTys;
159 582: ArgTys.push_back(MD->getSelfDecl()->getType());
160 582: ArgTys.push_back(Context.getObjCSelType());
161 : // FIXME: Kill copy?
248: branch 1 taken
582: branch 2 taken
162 1412: for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
163 582: e = MD->param_end(); i != e; ++i)
164 248: ArgTys.push_back((*i)->getType());
165 : return getFunctionInfo(MD->getResultType(), ArgTys,
166 : getCallingConventionForDecl(MD),
167 582: /*NoReturn*/ false);
168 : }
169 :
170 2984: const CGFunctionInfo &CodeGenTypes::getFunctionInfo(GlobalDecl GD) {
171 : // FIXME: Do we need to handle ObjCMethodDecl?
172 2984: const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
173 :
532: branch 1 taken
2452: branch 2 taken
174 2984: if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
175 532: return getFunctionInfo(CD, GD.getCtorType());
176 :
147: branch 1 taken
2305: branch 2 taken
177 2452: if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
178 147: return getFunctionInfo(DD, GD.getDtorType());
179 :
180 2305: return getFunctionInfo(FD);
181 : }
182 :
183 : const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
184 : const CallArgList &Args,
185 : CallingConv CC,
186 2847: bool NoReturn) {
187 : // FIXME: Kill copy.
188 2847: llvm::SmallVector<QualType, 16> ArgTys;
4357: branch 2 taken
2847: branch 3 taken
189 7204: for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
190 : i != e; ++i)
191 4357: ArgTys.push_back(i->second);
192 2847: return getFunctionInfo(ResTy, ArgTys, CC, NoReturn);
193 : }
194 :
195 : const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
196 : const FunctionArgList &Args,
197 : CallingConv CC,
198 3173: bool NoReturn) {
199 : // FIXME: Kill copy.
200 3173: llvm::SmallVector<QualType, 16> ArgTys;
3130: branch 2 taken
3173: branch 3 taken
201 6303: for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
202 : i != e; ++i)
203 3130: ArgTys.push_back(i->second);
204 3173: return getFunctionInfo(ResTy, ArgTys, CC, NoReturn);
205 : }
206 :
207 : const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
208 : const llvm::SmallVector<QualType, 16> &ArgTys,
209 : CallingConv CallConv,
210 14606: bool NoReturn) {
211 14606: unsigned CC = ClangCallConvToLLVMCallConv(CallConv);
212 :
213 : // Lookup or create unique function info.
214 14606: llvm::FoldingSetNodeID ID;
215 : CGFunctionInfo::Profile(ID, CC, NoReturn, ResTy,
216 14606: ArgTys.begin(), ArgTys.end());
217 :
218 14606: void *InsertPos = 0;
219 14606: CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
11724: branch 0 taken
2882: branch 1 taken
220 14606: if (FI)
221 11724: return *FI;
222 :
223 : // Construct the function info.
224 2882: FI = new CGFunctionInfo(CC, NoReturn, ResTy, ArgTys);
225 2882: FunctionInfos.InsertNode(FI, InsertPos);
226 :
227 : // Compute ABI information.
228 2882: getABIInfo().computeInfo(*FI, getContext(), TheModule.getContext());
229 :
230 2882: return *FI;
231 : }
232 :
233 : CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
234 : bool _NoReturn,
235 : QualType ResTy,
236 2882: const llvm::SmallVector<QualType, 16> &ArgTys)
237 : : CallingConvention(_CallingConvention),
238 : EffectiveCallingConvention(_CallingConvention),
239 2882: NoReturn(_NoReturn)
240 : {
241 2882: NumArgs = ArgTys.size();
6632: branch 2 taken
2882: branch 3 taken
0: branch 6 not taken
0: branch 7 not taken
242 2882: Args = new ArgInfo[1 + NumArgs];
243 2882: Args[0].type = ResTy;
3750: branch 0 taken
2882: branch 1 taken
2882: branch 2 taken
2882: branch 3 taken
244 6632: for (unsigned i = 0; i < NumArgs; ++i)
245 3750: Args[1 + i].type = ArgTys[i];
246 2882: }
247 :
248 : /***/
249 :
250 : void CodeGenTypes::GetExpandedTypes(QualType Ty,
251 41: std::vector<const llvm::Type*> &ArgTys) {
252 41: const RecordType *RT = Ty->getAsStructureType();
0: branch 0 not taken
41: branch 1 taken
253 41: assert(RT && "Can only expand structure types.");
254 41: const RecordDecl *RD = RT->getDecl();
255 : assert(!RD->hasFlexibleArrayMember() &&
41: branch 1 taken
0: branch 2 not taken
256 41: "Cannot expand structure with flexible array.");
257 :
89: branch 4 taken
41: branch 5 taken
258 130: for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
259 : i != e; ++i) {
260 89: const FieldDecl *FD = *i;
261 : assert(!FD->isBitField() &&
89: branch 1 taken
0: branch 2 not taken
262 89: "Cannot expand structure with bit-field members.");
263 :
264 89: QualType FT = FD->getType();
0: branch 1 not taken
89: branch 2 taken
265 89: if (CodeGenFunction::hasAggregateLLVMType(FT)) {
266 0: GetExpandedTypes(FT, ArgTys);
267 : } else {
268 89: ArgTys.push_back(ConvertType(FT));
269 : }
270 : }
271 41: }
272 :
273 : llvm::Function::arg_iterator
274 : CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
275 15: llvm::Function::arg_iterator AI) {
276 15: const RecordType *RT = Ty->getAsStructureType();
0: branch 0 not taken
15: branch 1 taken
277 15: assert(RT && "Can only expand structure types.");
278 :
279 15: RecordDecl *RD = RT->getDecl();
280 : assert(LV.isSimple() &&
15: branch 1 taken
0: branch 2 not taken
281 15: "Unexpected non-simple lvalue during struct expansion.");
282 15: llvm::Value *Addr = LV.getAddress();
29: branch 4 taken
15: branch 5 taken
283 44: for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
284 : i != e; ++i) {
285 29: FieldDecl *FD = *i;
286 29: QualType FT = FD->getType();
287 :
288 : // FIXME: What are the right qualifiers here?
289 29: LValue LV = EmitLValueForField(Addr, FD, 0);
0: branch 1 not taken
29: branch 2 taken
290 29: if (CodeGenFunction::hasAggregateLLVMType(FT)) {
291 0: AI = ExpandTypeFromArgs(FT, LV, AI);
292 : } else {
293 29: EmitStoreThroughLValue(RValue::get(AI), LV, FT);
294 29: ++AI;
295 : }
296 : }
297 :
298 15: return AI;
299 : }
300 :
301 : void
302 : CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
303 6: llvm::SmallVector<llvm::Value*, 16> &Args) {
304 6: const RecordType *RT = Ty->getAsStructureType();
0: branch 0 not taken
6: branch 1 taken
305 6: assert(RT && "Can only expand structure types.");
306 :
307 6: RecordDecl *RD = RT->getDecl();
6: branch 1 taken
0: branch 2 not taken
308 6: assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
309 6: llvm::Value *Addr = RV.getAggregateAddr();
18: branch 4 taken
6: branch 5 taken
310 24: for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
311 : i != e; ++i) {
312 18: FieldDecl *FD = *i;
313 18: QualType FT = FD->getType();
314 :
315 : // FIXME: What are the right qualifiers here?
316 18: LValue LV = EmitLValueForField(Addr, FD, 0);
0: branch 1 not taken
18: branch 2 taken
317 18: if (CodeGenFunction::hasAggregateLLVMType(FT)) {
318 0: ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
319 : } else {
320 18: RValue RV = EmitLoadOfLValue(LV, FT);
321 : assert(RV.isScalar() &&
18: branch 1 taken
0: branch 2 not taken
322 18: "Unexpected non-scalar rvalue during struct expansion.");
323 18: Args.push_back(RV.getScalarVal());
324 : }
325 : }
326 6: }
327 :
328 : /// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
329 : /// a pointer to an object of type \arg Ty.
330 : ///
331 : /// This safely handles the case when the src type is smaller than the
332 : /// destination type; in this situation the values of bits which not
333 : /// present in the src are undefined.
334 : static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
335 : const llvm::Type *Ty,
336 106: CodeGenFunction &CGF) {
337 : const llvm::Type *SrcTy =
338 106: cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
339 106: uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
340 106: uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
341 :
342 : // If load is legal, just bitcast the src pointer.
93: branch 0 taken
13: branch 1 taken
343 106: if (SrcSize >= DstSize) {
344 : // Generally SrcSize is never greater than DstSize, since this means we are
345 : // losing bits. However, this can happen in cases where the structure has
346 : // additional padding, for example due to a user specified alignment.
347 : //
348 : // FIXME: Assert that we aren't truncating non-padding bits when have access
349 : // to that information.
350 : llvm::Value *Casted =
351 93: CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
352 93: llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
353 : // FIXME: Use better alignment / avoid requiring aligned load.
354 93: Load->setAlignment(1);
355 93: return Load;
356 : } else {
357 : // Otherwise do coercion through memory. This is stupid, but
358 : // simple.
359 13: llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
360 : llvm::Value *Casted =
361 13: CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
362 : llvm::StoreInst *Store =
363 13: CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
364 : // FIXME: Use better alignment / avoid requiring aligned store.
365 13: Store->setAlignment(1);
366 13: return CGF.Builder.CreateLoad(Tmp);
367 : }
368 : }
369 :
370 : /// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
371 : /// where the source and destination may have different types.
372 : ///
373 : /// This safely handles the case when the src type is larger than the
374 : /// destination type; the upper bits of the src will be lost.
375 : static void CreateCoercedStore(llvm::Value *Src,
376 : llvm::Value *DstPtr,
377 : bool DstIsVolatile,
378 88: CodeGenFunction &CGF) {
379 88: const llvm::Type *SrcTy = Src->getType();
380 : const llvm::Type *DstTy =
381 88: cast<llvm::PointerType>(DstPtr->getType())->getElementType();
382 :
383 88: uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
384 88: uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
385 :
386 : // If store is legal, just bitcast the src pointer.
47: branch 0 taken
41: branch 1 taken
387 88: if (SrcSize <= DstSize) {
388 : llvm::Value *Casted =
389 47: CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
390 : // FIXME: Use better alignment / avoid requiring aligned store.
391 47: CGF.Builder.CreateStore(Src, Casted, DstIsVolatile)->setAlignment(1);
392 : } else {
393 : // Otherwise do coercion through memory. This is stupid, but
394 : // simple.
395 :
396 : // Generally SrcSize is never greater than DstSize, since this means we are
397 : // losing bits. However, this can happen in cases where the structure has
398 : // additional padding, for example due to a user specified alignment.
399 : //
400 : // FIXME: Assert that we aren't truncating non-padding bits when have access
401 : // to that information.
402 41: llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
403 41: CGF.Builder.CreateStore(Src, Tmp);
404 : llvm::Value *Casted =
405 41: CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
406 41: llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
407 : // FIXME: Use better alignment / avoid requiring aligned load.
408 41: Load->setAlignment(1);
409 41: CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
410 : }
411 88: }
412 :
413 : /***/
414 :
415 6146: bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
416 6146: return FI.getReturnInfo().isIndirect();
417 : }
418 :
419 : const llvm::FunctionType *
420 5660: CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
421 5660: std::vector<const llvm::Type*> ArgTys;
422 :
423 5660: const llvm::Type *ResultType = 0;
424 :
425 5660: QualType RetTy = FI.getReturnType();
426 5660: const ABIArgInfo &RetAI = FI.getReturnInfo();
0: branch 1 not taken
1553: branch 2 taken
106: branch 3 taken
3887: branch 4 taken
114: branch 5 taken
0: branch 6 not taken
427 5660: switch (RetAI.getKind()) {
428 : case ABIArgInfo::Expand:
429 0: assert(0 && "Invalid ABI kind for return argument");
430 :
431 : case ABIArgInfo::Extend:
432 : case ABIArgInfo::Direct:
433 1553: ResultType = ConvertType(RetTy);
434 1553: break;
435 :
436 : case ABIArgInfo::Indirect: {
106: branch 1 taken
0: branch 2 not taken
437 106: assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
438 106: ResultType = llvm::Type::getVoidTy(getLLVMContext());
439 106: const llvm::Type *STy = ConvertType(RetTy);
440 106: ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
441 106: break;
442 : }
443 :
444 : case ABIArgInfo::Ignore:
445 3887: ResultType = llvm::Type::getVoidTy(getLLVMContext());
446 3887: break;
447 :
448 : case ABIArgInfo::Coerce:
449 114: ResultType = RetAI.getCoerceToType();
450 : break;
451 : }
452 :
6911: branch 1 taken
5660: branch 2 taken
453 18231: for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
454 5660: ie = FI.arg_end(); it != ie; ++it) {
455 6911: const ABIArgInfo &AI = it->info;
456 :
8: branch 1 taken
56: branch 2 taken
105: branch 3 taken
6722: branch 4 taken
20: branch 5 taken
0: branch 6 not taken
457 6911: switch (AI.getKind()) {
458 : case ABIArgInfo::Ignore:
459 8: break;
460 :
461 : case ABIArgInfo::Coerce:
462 56: ArgTys.push_back(AI.getCoerceToType());
463 56: break;
464 :
465 : case ABIArgInfo::Indirect: {
466 : // indirect arguments are always on the stack, which is addr space #0.
467 105: const llvm::Type *LTy = ConvertTypeForMem(it->type);
468 105: ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
469 105: break;
470 : }
471 :
472 : case ABIArgInfo::Extend:
473 : case ABIArgInfo::Direct:
474 6722: ArgTys.push_back(ConvertType(it->type));
475 6722: break;
476 :
477 : case ABIArgInfo::Expand:
478 20: GetExpandedTypes(it->type, ArgTys);
479 : break;
480 : }
481 : }
482 :
483 5660: return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
484 : }
485 :
486 1325: static bool HasIncompleteReturnTypeOrArgumentTypes(const FunctionProtoType *T) {
2: branch 3 taken
1323: branch 4 taken
487 1325: if (const TagType *TT = T->getResultType()->getAs<TagType>()) {
2: branch 2 taken
0: branch 3 not taken
488 2: if (!TT->getDecl()->isDefinition())
489 2: return true;
490 : }
491 :
39: branch 1 taken
1323: branch 2 taken
492 1362: for (unsigned i = 0, e = T->getNumArgs(); i != e; ++i) {
0: branch 3 not taken
39: branch 4 taken
493 39: if (const TagType *TT = T->getArgType(i)->getAs<TagType>()) {
0: branch 2 not taken
0: branch 3 not taken
494 0: if (!TT->getDecl()->isDefinition())
495 0: return true;
496 : }
497 : }
498 :
499 1323: return false;
500 : }
501 :
502 : const llvm::Type *
503 1325: CodeGenTypes::GetFunctionTypeForVtable(const CXXMethodDecl *MD) {
504 1325: const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
505 :
1323: branch 1 taken
2: branch 2 taken
506 1325: if (!HasIncompleteReturnTypeOrArgumentTypes(FPT))
507 1323: return GetFunctionType(getFunctionInfo(MD), FPT->isVariadic());
508 :
509 2: return llvm::OpaqueType::get(getLLVMContext());
510 : }
511 :
512 : void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
513 : const Decl *TargetDecl,
514 : AttributeListType &PAL,
515 6161: unsigned &CallingConv) {
516 6161: unsigned FuncAttrs = 0;
517 6161: unsigned RetAttrs = 0;
518 :
519 6161: CallingConv = FI.getEffectiveCallingConvention();
520 :
71: branch 1 taken
6090: branch 2 taken
521 6161: if (FI.isNoReturn())
522 71: FuncAttrs |= llvm::Attribute::NoReturn;
523 :
524 : // FIXME: handle sseregparm someday...
5435: branch 0 taken
726: branch 1 taken
525 6161: if (TargetDecl) {
22: branch 1 taken
5413: branch 2 taken
526 5435: if (TargetDecl->hasAttr<NoThrowAttr>())
527 22: FuncAttrs |= llvm::Attribute::NoUnwind;
0: branch 1 not taken
5435: branch 2 taken
528 5435: if (TargetDecl->hasAttr<NoReturnAttr>())
529 0: FuncAttrs |= llvm::Attribute::NoReturn;
11: branch 1 taken
5424: branch 2 taken
530 5435: if (TargetDecl->hasAttr<ConstAttr>())
531 11: FuncAttrs |= llvm::Attribute::ReadNone;
3: branch 1 taken
5421: branch 2 taken
532 5424: else if (TargetDecl->hasAttr<PureAttr>())
533 3: FuncAttrs |= llvm::Attribute::ReadOnly;
59: branch 1 taken
5376: branch 2 taken
534 5435: if (TargetDecl->hasAttr<MallocAttr>())
535 59: RetAttrs |= llvm::Attribute::NoAlias;
536 : }
537 :
30: branch 0 taken
6131: branch 1 taken
538 6161: if (CodeGenOpts.OptimizeSize)
539 30: FuncAttrs |= llvm::Attribute::OptimizeForSize;
1: branch 0 taken
6160: branch 1 taken
540 6161: if (CodeGenOpts.DisableRedZone)
541 1: FuncAttrs |= llvm::Attribute::NoRedZone;
0: branch 0 not taken
6161: branch 1 taken
542 6161: if (CodeGenOpts.NoImplicitFloat)
543 0: FuncAttrs |= llvm::Attribute::NoImplicitFloat;
544 :
545 6161: QualType RetTy = FI.getReturnType();
546 6161: unsigned Index = 1;
547 6161: const ABIArgInfo &RetAI = FI.getReturnInfo();
65: branch 1 taken
2157: branch 2 taken
153: branch 3 taken
3786: branch 4 taken
0: branch 5 not taken
0: branch 6 not taken
548 6161: switch (RetAI.getKind()) {
549 : case ABIArgInfo::Extend:
14: branch 2 taken
51: branch 3 taken
550 65: if (RetTy->isSignedIntegerType()) {
551 14: RetAttrs |= llvm::Attribute::SExt;
51: branch 2 taken
0: branch 3 not taken
552 51: } else if (RetTy->isUnsignedIntegerType()) {
553 51: RetAttrs |= llvm::Attribute::ZExt;
554 : }
555 : // FALLTHROUGH
556 : case ABIArgInfo::Direct:
557 2222: break;
558 :
559 : case ABIArgInfo::Indirect:
560 : PAL.push_back(llvm::AttributeWithIndex::get(Index,
561 : llvm::Attribute::StructRet |
562 153: llvm::Attribute::NoAlias));
563 153: ++Index;
564 : // sret disables readnone and readonly
565 : FuncAttrs &= ~(llvm::Attribute::ReadOnly |
566 153: llvm::Attribute::ReadNone);
567 153: break;
568 :
569 : case ABIArgInfo::Ignore:
570 : case ABIArgInfo::Coerce:
571 3786: break;
572 :
573 : case ABIArgInfo::Expand:
574 0: assert(0 && "Invalid ABI kind for return argument");
575 : }
576 :
124: branch 0 taken
6037: branch 1 taken
577 6161: if (RetAttrs)
578 124: PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
579 :
580 : // FIXME: we need to honour command line settings also...
581 : // FIXME: RegParm should be reduced in case of nested functions and/or global
582 : // register variable.
583 6161: signed RegParm = 0;
5435: branch 0 taken
726: branch 1 taken
584 6161: if (TargetDecl)
4: branch 0 taken
5431: branch 1 taken
585 5435: if (const RegparmAttr *RegParmAttr
586 5435: = TargetDecl->getAttr<RegparmAttr>())
587 4: RegParm = RegParmAttr->getNumParams();
588 :
589 6161: unsigned PointerWidth = getContext().Target.getPointerWidth(0);
7642: branch 1 taken
6161: branch 2 taken
590 19964: for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
591 6161: ie = FI.arg_end(); it != ie; ++it) {
592 7642: QualType ParamType = it->type;
593 7642: const ABIArgInfo &AI = it->info;
594 7642: unsigned Attributes = 0;
595 :
5: branch 1 taken
7637: branch 2 taken
596 7642: if (ParamType.isRestrictQualified())
597 5: Attributes |= llvm::Attribute::NoAlias;
598 :
70: branch 1 taken
152: branch 2 taken
126: branch 3 taken
7261: branch 4 taken
12: branch 5 taken
21: branch 6 taken
0: branch 7 not taken
599 7642: switch (AI.getKind()) {
600 : case ABIArgInfo::Coerce:
601 70: break;
602 :
603 : case ABIArgInfo::Indirect:
122: branch 1 taken
30: branch 2 taken
604 152: if (AI.getIndirectByVal())
605 122: Attributes |= llvm::Attribute::ByVal;
606 :
607 : Attributes |=
608 152: llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
609 : // byval disables readnone and readonly.
610 : FuncAttrs &= ~(llvm::Attribute::ReadOnly |
611 152: llvm::Attribute::ReadNone);
612 152: break;
613 :
614 : case ABIArgInfo::Extend:
28: branch 2 taken
98: branch 3 taken
615 126: if (ParamType->isSignedIntegerType()) {
616 28: Attributes |= llvm::Attribute::SExt;
98: branch 2 taken
0: branch 3 not taken
617 98: } else if (ParamType->isUnsignedIntegerType()) {
618 98: Attributes |= llvm::Attribute::ZExt;
619 : }
620 : // FALLS THROUGH
621 : case ABIArgInfo::Direct:
12: branch 0 taken
7375: branch 1 taken
6: branch 4 taken
6: branch 5 taken
4: branch 8 taken
2: branch 9 taken
10: branch 10 taken
7377: branch 11 taken
622 7387: if (RegParm > 0 &&
623 : (ParamType->isIntegerType() || ParamType->isPointerType())) {
624 : RegParm -=
625 10: (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
10: branch 0 taken
0: branch 1 not taken
626 10: if (RegParm >= 0)
627 10: Attributes |= llvm::Attribute::InReg;
628 : }
629 : // FIXME: handle sseregparm someday...
630 7387: break;
631 :
632 : case ABIArgInfo::Ignore:
633 : // Skip increment, no matching LLVM parameter.
634 12: continue;
635 :
636 : case ABIArgInfo::Expand: {
637 21: std::vector<const llvm::Type*> Tys;
638 : // FIXME: This is rather inefficient. Do we ever actually need to do
639 : // anything here? The result should be just reconstructed on the other
640 : // side, so extension should be a non-issue.
641 21: getTypes().GetExpandedTypes(ParamType, Tys);
642 21: Index += Tys.size();
643 21: continue;
644 : }
645 : }
646 :
261: branch 0 taken
7348: branch 1 taken
647 7609: if (Attributes)
648 261: PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
649 7609: ++Index;
650 : }
123: branch 0 taken
6038: branch 1 taken
651 6161: if (FuncAttrs)
652 123: PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
653 6161: }
654 :
655 : void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
656 : llvm::Function *Fn,
657 3076: const FunctionArgList &Args) {
658 : // If this is an implicit-return-zero function, go ahead and
659 : // initialize the return value. TODO: it might be nice to have
660 : // a more general mechanism for this that didn't require synthesized
661 : // return statements.
2600: branch 1 taken
476: branch 2 taken
662 3076: if (const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
107: branch 1 taken
2493: branch 2 taken
663 2600: if (FD->hasImplicitReturnZero()) {
664 107: QualType RetTy = FD->getResultType().getUnqualifiedType();
665 107: const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
666 107: llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
667 107: Builder.CreateStore(Zero, ReturnValue);
668 : }
669 : }
670 :
671 : // FIXME: We no longer need the types from FunctionArgList; lift up and
672 : // simplify.
673 :
674 : // Emit allocs for param decls. Give the LLVM Argument nodes names.
675 3076: llvm::Function::arg_iterator AI = Fn->arg_begin();
676 :
677 : // Name the struct return argument.
70: branch 1 taken
3006: branch 2 taken
678 3076: if (CGM.ReturnTypeUsesSret(FI)) {
679 70: AI->setName("agg.result");
680 70: ++AI;
681 : }
682 :
683 : assert(FI.arg_size() == Args.size() &&
3076: branch 2 taken
0: branch 3 not taken
684 3076: "Mismatch between function signature & arguments.");
685 3076: CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
2994: branch 2 taken
3076: branch 3 taken
686 6070: for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
687 : i != e; ++i, ++info_it) {
688 2994: const VarDecl *Arg = i->first;
689 2994: QualType Ty = info_it->type;
690 2994: const ABIArgInfo &ArgI = info_it->info;
691 :
76: branch 1 taken
2843: branch 2 taken
15: branch 3 taken
8: branch 4 taken
52: branch 5 taken
0: branch 6 not taken
692 2994: switch (ArgI.getKind()) {
693 : case ABIArgInfo::Indirect: {
694 76: llvm::Value* V = AI;
0: branch 1 not taken
76: branch 2 taken
695 76: if (hasAggregateLLVMType(Ty)) {
696 : // Do nothing, aggregates and complex variables are accessed by
697 : // reference.
698 : } else {
699 : // Load scalar value from indirect argument.
700 0: V = EmitLoadOfScalar(V, false, Ty);
0: branch 3 not taken
0: branch 4 not taken
701 0: if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
702 : // This must be a promotion, for something like
703 : // "void a(x) short x; {..."
704 0: V = EmitScalarConversion(V, Ty, Arg->getType());
705 : }
706 : }
707 76: EmitParmDecl(*Arg, V);
708 76: break;
709 : }
710 :
711 : case ABIArgInfo::Extend:
712 : case ABIArgInfo::Direct: {
2843: branch 2 taken
0: branch 3 not taken
713 2843: assert(AI != Fn->arg_end() && "Argument mismatch!");
714 2843: llvm::Value* V = AI;
0: branch 1 not taken
2843: branch 2 taken
715 2843: if (hasAggregateLLVMType(Ty)) {
716 : // Create a temporary alloca to hold the argument; the rest of
717 : // codegen expects to access aggregates & complex values by
718 : // reference.
719 0: V = CreateMemTemp(Ty);
720 0: Builder.CreateStore(AI, V);
721 : } else {
11: branch 3 taken
2832: branch 4 taken
722 2843: if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
723 : // This must be a promotion, for something like
724 : // "void a(x) short x; {..."
725 11: V = EmitScalarConversion(V, Ty, Arg->getType());
726 : }
727 : }
728 2843: EmitParmDecl(*Arg, V);
729 2843: break;
730 : }
731 :
732 : case ABIArgInfo::Expand: {
733 : // If this structure was expanded into multiple arguments then
734 : // we need to create a temporary and reconstruct it from the
735 : // arguments.
736 15: llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
737 : // FIXME: What are the right qualifiers here?
738 : llvm::Function::arg_iterator End =
739 15: ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp, Qualifiers()), AI);
740 15: EmitParmDecl(*Arg, Temp);
741 :
742 : // Name the arguments used in expansion and increment AI.
743 15: unsigned Index = 0;
29: branch 2 taken
15: branch 3 taken
744 44: for (; AI != End; ++AI, ++Index)
745 29: AI->setName(Arg->getName() + "." + llvm::Twine(Index));
746 15: continue;
747 : }
748 :
749 : case ABIArgInfo::Ignore:
750 : // Initialize the local variable appropriately.
8: branch 1 taken
0: branch 2 not taken
751 8: if (hasAggregateLLVMType(Ty)) {
752 8: EmitParmDecl(*Arg, CreateMemTemp(Ty));
753 : } else {
754 0: EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
755 : }
756 :
757 : // Skip increment, no matching LLVM parameter.
758 8: continue;
759 :
760 : case ABIArgInfo::Coerce: {
52: branch 2 taken
0: branch 3 not taken
761 52: assert(AI != Fn->arg_end() && "Argument mismatch!");
762 : // FIXME: This is very wasteful; EmitParmDecl is just going to drop the
763 : // result in a new alloca anyway, so we could just store into that
764 : // directly if we broke the abstraction down more.
765 52: llvm::Value *V = CreateMemTemp(Ty, "coerce");
766 52: CreateCoercedStore(AI, V, /*DestIsVolatile=*/false, *this);
767 : // Match to what EmitParmDecl is expecting for this type.
2: branch 1 taken
50: branch 2 taken
768 52: if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
769 2: V = EmitLoadOfScalar(V, false, Ty);
0: branch 3 not taken
2: branch 4 taken
770 2: if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
771 : // This must be a promotion, for something like
772 : // "void a(x) short x; {..."
773 0: V = EmitScalarConversion(V, Ty, Arg->getType());
774 : }
775 : }
776 52: EmitParmDecl(*Arg, V);
777 : break;
778 : }
779 : }
780 :
781 2971: ++AI;
782 : }
3076: branch 2 taken
0: branch 3 not taken
783 3076: assert(AI == Fn->arg_end() && "Argument mismatch!");
784 3076: }
785 :
786 : void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
787 3076: llvm::Value *ReturnValue) {
788 3076: llvm::Value *RV = 0;
789 :
790 : // Functions with no result always return void.
1016: branch 0 taken
2060: branch 1 taken
791 3076: if (ReturnValue) {
792 1016: QualType RetTy = FI.getReturnType();
793 1016: const ABIArgInfo &RetAI = FI.getReturnInfo();
794 :
70: branch 1 taken
853: branch 2 taken
4: branch 3 taken
89: branch 4 taken
0: branch 5 not taken
0: branch 6 not taken
795 1016: switch (RetAI.getKind()) {
796 : case ABIArgInfo::Indirect:
6: branch 2 taken
64: branch 3 taken
797 70: if (RetTy->isAnyComplexType()) {
798 6: ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
799 6: StoreComplexToAddr(RT, CurFn->arg_begin(), false);
2: branch 1 taken
62: branch 2 taken
800 64: } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
801 : // Do nothing; aggregrates get evaluated directly into the destination.
802 : } else {
803 : EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
804 2: false, RetTy);
805 : }
806 70: break;
807 :
808 : case ABIArgInfo::Extend:
809 : case ABIArgInfo::Direct:
810 : // The internal return value temp always will have
811 : // pointer-to-return-type type.
812 853: RV = Builder.CreateLoad(ReturnValue);
813 853: break;
814 :
815 : case ABIArgInfo::Ignore:
816 4: break;
817 :
818 : case ABIArgInfo::Coerce:
819 89: RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
820 89: break;
821 :
822 : case ABIArgInfo::Expand:
823 0: assert(0 && "Invalid ABI kind for return argument");
824 : }
825 : }
826 :
942: branch 0 taken
2134: branch 1 taken
827 3076: if (RV) {
828 942: Builder.CreateRet(RV);
829 : } else {
830 2134: Builder.CreateRetVoid();
831 : }
832 3076: }
833 :
834 2200: RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
187: branch 2 taken
2013: branch 3 taken
835 2200: if (ArgType->isReferenceType())
836 187: return EmitReferenceBindingToExpr(E);
837 :
838 2013: return EmitAnyExprToTemp(E);
839 : }
840 :
841 : RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
842 : llvm::Value *Callee,
843 : ReturnValueSlot ReturnValue,
844 : const CallArgList &CallArgs,
845 2851: const Decl *TargetDecl) {
846 : // FIXME: We no longer need the types from CallArgs; lift up and simplify.
847 2851: llvm::SmallVector<llvm::Value*, 16> Args;
848 :
849 : // Handle struct-return functions by passing a pointer to the
850 : // location that we would like to return into.
851 2851: QualType RetTy = CallInfo.getReturnType();
852 2851: const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
853 :
854 :
855 : // If the call returns a temporary with struct return, create a temporary
856 : // alloca to hold the result, unless one is given to us.
56: branch 1 taken
2795: branch 2 taken
857 2851: if (CGM.ReturnTypeUsesSret(CallInfo)) {
858 56: llvm::Value *Value = ReturnValue.getValue();
28: branch 0 taken
28: branch 1 taken
859 56: if (!Value)
860 28: Value = CreateMemTemp(RetTy);
861 56: Args.push_back(Value);
862 : }
863 :
864 : assert(CallInfo.arg_size() == CallArgs.size() &&
2851: branch 2 taken
0: branch 3 not taken
865 2851: "Mismatch between function signature & arguments.");
866 2851: CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
4361: branch 2 taken
2851: branch 3 taken
867 7212: for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
868 : I != E; ++I, ++info_it) {
869 4361: const ABIArgInfo &ArgInfo = info_it->info;
870 4361: RValue RV = I->first;
871 :
66: branch 1 taken
4268: branch 2 taken
4: branch 3 taken
17: branch 4 taken
6: branch 5 taken
0: branch 6 not taken
872 4361: switch (ArgInfo.getKind()) {
873 : case ABIArgInfo::Indirect:
66: branch 1 taken
0: branch 2 not taken
13: branch 4 taken
53: branch 5 taken
13: branch 6 taken
53: branch 7 taken
874 66: if (RV.isScalar() || RV.isComplex()) {
875 : // Make a temporary alloca to pass the argument.
876 13: Args.push_back(CreateMemTemp(I->second));
0: branch 1 not taken
13: branch 2 taken
877 13: if (RV.isScalar())
878 0: EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, I->second);
879 : else
880 13: StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
881 : } else {
882 53: Args.push_back(RV.getAggregateAddr());
883 : }
884 66: break;
885 :
886 : case ABIArgInfo::Extend:
887 : case ABIArgInfo::Direct:
4268: branch 1 taken
0: branch 2 not taken
888 4268: if (RV.isScalar()) {
889 4268: Args.push_back(RV.getScalarVal());
0: branch 1 not taken
0: branch 2 not taken
890 0: } else if (RV.isComplex()) {
891 0: llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
892 0: Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
893 0: Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
894 0: Args.push_back(Tmp);
895 : } else {
896 0: Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
897 : }
898 4268: break;
899 :
900 : case ABIArgInfo::Ignore:
901 4: break;
902 :
903 : case ABIArgInfo::Coerce: {
904 : // FIXME: Avoid the conversion through memory if possible.
905 : llvm::Value *SrcPtr;
0: branch 1 not taken
17: branch 2 taken
906 17: if (RV.isScalar()) {
907 0: SrcPtr = CreateMemTemp(I->second, "coerce");
908 0: EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, I->second);
4: branch 1 taken
13: branch 2 taken
909 17: } else if (RV.isComplex()) {
910 4: SrcPtr = CreateMemTemp(I->second, "coerce");
911 4: StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
912 : } else
913 13: SrcPtr = RV.getAggregateAddr();
914 : Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
915 17: *this));
916 17: break;
917 : }
918 :
919 : case ABIArgInfo::Expand:
920 6: ExpandTypeToArgs(I->second, RV, Args);
921 : break;
922 : }
923 : }
924 :
925 : // If the callee is a bitcast of a function to a varargs pointer to function
926 : // type, check to see if we can remove the bitcast. This handles some cases
927 : // with unprototyped functions.
239: branch 1 taken
2612: branch 2 taken
928 2851: if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
239: branch 2 taken
0: branch 3 not taken
929 239: if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
930 239: const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
931 : const llvm::FunctionType *CurFT =
932 239: cast<llvm::FunctionType>(CurPT->getElementType());
933 239: const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
934 :
239: branch 1 taken
0: branch 2 not taken
99: branch 5 taken
140: branch 6 taken
83: branch 9 taken
16: branch 10 taken
82: branch 13 taken
1: branch 14 taken
82: branch 15 taken
157: branch 16 taken
935 239: if (CE->getOpcode() == llvm::Instruction::BitCast &&
936 : ActualFT->getReturnType() == CurFT->getReturnType() &&
937 : ActualFT->getNumParams() == CurFT->getNumParams() &&
938 : ActualFT->getNumParams() == Args.size()) {
939 82: bool ArgsMatch = true;
127: branch 1 taken
77: branch 2 taken
940 204: for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
5: branch 2 taken
122: branch 3 taken
941 127: if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
942 5: ArgsMatch = false;
943 5: break;
944 : }
945 :
946 : // Strip the cast if we can get away with it. This is a nice cleanup,
947 : // but also allows us to inline the function at -O0 if it is marked
948 : // always_inline.
77: branch 0 taken
5: branch 1 taken
949 82: if (ArgsMatch)
950 77: Callee = CalleeF;
951 : }
952 : }
953 :
954 :
955 2851: llvm::BasicBlock *InvokeDest = getInvokeDest();
956 : unsigned CallingConv;
957 2851: CodeGen::AttributeListType AttributeList;
958 2851: CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
959 : llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
960 2851: AttributeList.end());
961 :
962 2851: llvm::CallSite CS;
79: branch 0 taken
2772: branch 1 taken
0: branch 3 not taken
79: branch 4 taken
2772: branch 5 taken
79: branch 6 taken
963 2851: if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
964 2772: CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
965 : } else {
966 79: llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
967 : CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
968 79: Args.data(), Args.data()+Args.size());
969 79: EmitBlock(Cont);
970 : }
971 :
972 2851: CS.setAttributes(Attrs);
973 2851: CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
974 :
975 : // If the call doesn't return, finish the basic block and clear the
976 : // insertion point; this allows the rest of IRgen to discard
977 : // unreachable code.
59: branch 1 taken
2792: branch 2 taken
978 2851: if (CS.doesNotReturn()) {
979 59: Builder.CreateUnreachable();
980 59: Builder.ClearInsertionPoint();
981 :
982 : // FIXME: For now, emit a dummy basic block because expr emitters in
983 : // generally are not ready to handle emitting expressions at unreachable
984 : // points.
985 59: EnsureInsertPoint();
986 :
987 : // Return a reasonable RValue.
988 59: return GetUndefRValue(RetTy);
989 : }
990 :
991 2792: llvm::Instruction *CI = CS.getInstruction();
2792: branch 1 taken
0: branch 2 not taken
1265: branch 5 taken
1527: branch 6 taken
1265: branch 7 taken
1527: branch 8 taken
992 2792: if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
993 1265: CI->setName("call");
994 :
56: branch 1 taken
1229: branch 2 taken
1471: branch 3 taken
36: branch 4 taken
0: branch 5 not taken
0: branch 6 not taken
995 2792: switch (RetAI.getKind()) {
996 : case ABIArgInfo::Indirect:
3: branch 2 taken
53: branch 3 taken
997 56: if (RetTy->isAnyComplexType())
998 3: return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
53: branch 1 taken
0: branch 2 not taken
999 53: if (CodeGenFunction::hasAggregateLLVMType(RetTy))
1000 53: return RValue::getAggregate(Args[0]);
1001 0: return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
1002 :
1003 : case ABIArgInfo::Extend:
1004 : case ABIArgInfo::Direct:
0: branch 2 not taken
1229: branch 3 taken
1005 1229: if (RetTy->isAnyComplexType()) {
1006 0: llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1007 0: llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1008 0: return RValue::getComplex(std::make_pair(Real, Imag));
1009 : }
0: branch 1 not taken
1229: branch 2 taken
1010 1229: if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1011 0: llvm::Value *DestPtr = ReturnValue.getValue();
1012 0: bool DestIsVolatile = ReturnValue.isVolatile();
1013 :
0: branch 0 not taken
0: branch 1 not taken
1014 0: if (!DestPtr) {
1015 0: DestPtr = CreateMemTemp(RetTy, "agg.tmp");
1016 0: DestIsVolatile = false;
1017 : }
1018 0: Builder.CreateStore(CI, DestPtr, DestIsVolatile);
1019 0: return RValue::getAggregate(DestPtr);
1020 : }
1021 1229: return RValue::get(CI);
1022 :
1023 : case ABIArgInfo::Ignore:
1024 : // If we are ignoring an argument that had a result, make sure to
1025 : // construct the appropriate return value for our caller.
1026 1471: return GetUndefRValue(RetTy);
1027 :
1028 : case ABIArgInfo::Coerce: {
1029 36: llvm::Value *DestPtr = ReturnValue.getValue();
1030 36: bool DestIsVolatile = ReturnValue.isVolatile();
1031 :
33: branch 0 taken
3: branch 1 taken
1032 36: if (!DestPtr) {
1033 33: DestPtr = CreateMemTemp(RetTy, "coerce");
1034 33: DestIsVolatile = false;
1035 : }
1036 :
1037 36: CreateCoercedStore(CI, DestPtr, DestIsVolatile, *this);
11: branch 2 taken
25: branch 3 taken
1038 36: if (RetTy->isAnyComplexType())
1039 11: return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
20: branch 1 taken
5: branch 2 taken
1040 25: if (CodeGenFunction::hasAggregateLLVMType(RetTy))
1041 20: return RValue::getAggregate(DestPtr);
1042 5: return RValue::get(EmitLoadOfScalar(DestPtr, false, RetTy));
1043 : }
1044 :
1045 : case ABIArgInfo::Expand:
1046 0: assert(0 && "Invalid ABI kind for return argument");
1047 : }
1048 :
1049 0: assert(0 && "Unhandled ABIArgInfo::Kind");
1050 2851: return RValue::get(0);
1051 : }
1052 :
1053 : /* VarArg handling */
1054 :
1055 9: llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1056 9: return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1057 : }
Generated: 2010-02-10 01:31 by zcov