 |
|
 |
|
| Files: |
1 |
|
Branches Taken: |
84.9% |
270 / 318 |
| Generated: |
2010-02-10 01:31 |
|
Branches Executed: |
95.6% |
304 / 318 |
| |
|
Line Coverage: |
94.4% |
510 / 540 |
| |
 |
|
 |
1 : //===--- CGStmt.cpp - Emit LLVM Code from Statements ----------------------===//
2 : //
3 : // The LLVM Compiler Infrastructure
4 : //
5 : // This file is distributed under the University of Illinois Open Source
6 : // License. See LICENSE.TXT for details.
7 : //
8 : //===----------------------------------------------------------------------===//
9 : //
10 : // This contains code to emit Stmt nodes as LLVM code.
11 : //
12 : //===----------------------------------------------------------------------===//
13 :
14 : #include "CGDebugInfo.h"
15 : #include "CodeGenModule.h"
16 : #include "CodeGenFunction.h"
17 : #include "clang/AST/StmtVisitor.h"
18 : #include "clang/Basic/PrettyStackTrace.h"
19 : #include "clang/Basic/TargetInfo.h"
20 : #include "llvm/ADT/StringExtras.h"
21 : #include "llvm/InlineAsm.h"
22 : #include "llvm/Intrinsics.h"
23 : #include "llvm/Target/TargetData.h"
24 : using namespace clang;
25 : using namespace CodeGen;
26 :
27 : //===----------------------------------------------------------------------===//
28 : // Statement Emission
29 : //===----------------------------------------------------------------------===//
30 :
31 3923: void CodeGenFunction::EmitStopPoint(const Stmt *S) {
220: branch 1 taken
3703: branch 2 taken
32 3923: if (CGDebugInfo *DI = getDebugInfo()) {
33 220: DI->setLocation(S->getLocStart());
34 220: DI->EmitStopPoint(CurFn, Builder);
35 : }
36 3923: }
37 :
38 7725: void CodeGenFunction::EmitStmt(const Stmt *S) {
0: branch 0 not taken
7725: branch 1 taken
39 7725: assert(S && "Null statement?");
40 :
41 : // Check if we can handle this without bothering to generate an
42 : // insert point or debug info.
3999: branch 1 taken
3726: branch 2 taken
43 7725: if (EmitSimpleStmt(S))
44 3999: return;
45 :
46 : // Check if we are generating unreachable code.
9: branch 1 taken
3717: branch 2 taken
47 3726: if (!HaveInsertPoint()) {
48 : // If so, and the statement doesn't contain a label, then we do not need to
49 : // generate actual code. This is safe because (1) the current point is
50 : // unreachable, so we don't need to execute the code, and (2) we've already
51 : // handled the statements which update internal data structures (like the
52 : // local variable map) which could be used by subsequent statements.
9: branch 1 taken
0: branch 2 not taken
53 9: if (!ContainsLabel(S)) {
54 : // Verify that any decl statements were handled as simple, they may be in
55 : // scope of subsequent reachable statements.
9: branch 1 taken
0: branch 2 not taken
56 9: assert(!isa<DeclStmt>(*S) && "Unexpected DeclStmt!");
57 9: return;
58 : }
59 :
60 : // Otherwise, make a new block to hold the code.
61 0: EnsureInsertPoint();
62 : }
63 :
64 : // Generate a stoppoint if we are emitting debug info.
65 3717: EmitStopPoint(S);
66 :
2479: branch 1 taken
12: branch 2 taken
224: branch 3 taken
63: branch 4 taken
15: branch 5 taken
69: branch 6 taken
752: branch 7 taken
35: branch 8 taken
35: branch 9 taken
11: branch 10 taken
0: branch 11 not taken
0: branch 12 not taken
8: branch 13 taken
8: branch 14 taken
5: branch 15 taken
1: branch 16 taken
67 3717: switch (S->getStmtClass()) {
68 : default:
69 : // Must be an expression in a stmt context. Emit the value (to get
70 : // side-effects) and ignore the result.
0: branch 1 not taken
2479: branch 2 taken
71 2479: if (!isa<Expr>(S))
72 0: ErrorUnsupported(S, "statement");
73 :
74 2479: EmitAnyExpr(cast<Expr>(S), 0, false, true);
75 :
76 : // Expression emitters don't handle unreachable blocks yet, so look for one
77 : // explicitly here. This handles the common case of a call to a noreturn
78 : // function.
2477: branch 1 taken
2: branch 2 taken
79 2479: if (llvm::BasicBlock *CurBB = Builder.GetInsertBlock()) {
106: branch 1 taken
2371: branch 2 taken
46: branch 4 taken
60: branch 5 taken
46: branch 6 taken
2431: branch 7 taken
80 2477: if (CurBB->empty() && CurBB->use_empty()) {
81 46: CurBB->eraseFromParent();
82 46: Builder.ClearInsertionPoint();
83 : }
84 : }
85 2479: break;
86 : case Stmt::IndirectGotoStmtClass:
87 12: EmitIndirectGotoStmt(cast<IndirectGotoStmt>(*S)); break;
88 :
89 224: case Stmt::IfStmtClass: EmitIfStmt(cast<IfStmt>(*S)); break;
90 63: case Stmt::WhileStmtClass: EmitWhileStmt(cast<WhileStmt>(*S)); break;
91 15: case Stmt::DoStmtClass: EmitDoStmt(cast<DoStmt>(*S)); break;
92 69: case Stmt::ForStmtClass: EmitForStmt(cast<ForStmt>(*S)); break;
93 :
94 752: case Stmt::ReturnStmtClass: EmitReturnStmt(cast<ReturnStmt>(*S)); break;
95 :
96 35: case Stmt::SwitchStmtClass: EmitSwitchStmt(cast<SwitchStmt>(*S)); break;
97 35: case Stmt::AsmStmtClass: EmitAsmStmt(cast<AsmStmt>(*S)); break;
98 :
99 : case Stmt::ObjCAtTryStmtClass:
100 11: EmitObjCAtTryStmt(cast<ObjCAtTryStmt>(*S));
101 11: break;
102 : case Stmt::ObjCAtCatchStmtClass:
103 0: assert(0 && "@catch statements should be handled by EmitObjCAtTryStmt");
104 : break;
105 : case Stmt::ObjCAtFinallyStmtClass:
106 0: assert(0 && "@finally statements should be handled by EmitObjCAtTryStmt");
107 : break;
108 : case Stmt::ObjCAtThrowStmtClass:
109 8: EmitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(*S));
110 8: break;
111 : case Stmt::ObjCAtSynchronizedStmtClass:
112 8: EmitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(*S));
113 8: break;
114 : case Stmt::ObjCForCollectionStmtClass:
115 5: EmitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(*S));
116 5: break;
117 :
118 : case Stmt::CXXTryStmtClass:
119 1: EmitCXXTryStmt(cast<CXXTryStmt>(*S));
120 : break;
121 : }
122 : }
123 :
124 7725: bool CodeGenFunction::EmitSimpleStmt(const Stmt *S) {
3726: branch 1 taken
70: branch 2 taken
2593: branch 3 taken
1152: branch 4 taken
31: branch 5 taken
11: branch 6 taken
63: branch 7 taken
12: branch 8 taken
21: branch 9 taken
46: branch 10 taken
125 7725: switch (S->getStmtClass()) {
126 3726: default: return false;
127 70: case Stmt::NullStmtClass: break;
128 2593: case Stmt::CompoundStmtClass: EmitCompoundStmt(cast<CompoundStmt>(*S)); break;
129 1152: case Stmt::DeclStmtClass: EmitDeclStmt(cast<DeclStmt>(*S)); break;
130 31: case Stmt::LabelStmtClass: EmitLabelStmt(cast<LabelStmt>(*S)); break;
131 11: case Stmt::GotoStmtClass: EmitGotoStmt(cast<GotoStmt>(*S)); break;
132 63: case Stmt::BreakStmtClass: EmitBreakStmt(cast<BreakStmt>(*S)); break;
133 12: case Stmt::ContinueStmtClass: EmitContinueStmt(cast<ContinueStmt>(*S)); break;
134 21: case Stmt::DefaultStmtClass: EmitDefaultStmt(cast<DefaultStmt>(*S)); break;
135 46: case Stmt::CaseStmtClass: EmitCaseStmt(cast<CaseStmt>(*S)); break;
136 : }
137 :
138 3999: return true;
139 : }
140 :
141 : /// EmitCompoundStmt - Emit a compound statement {..} node. If GetLast is true,
142 : /// this captures the expression result of the last sub-statement and returns it
143 : /// (for use by the statement expression extension).
144 : RValue CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast,
145 2597: llvm::Value *AggLoc, bool isAggVol) {
146 : PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),S.getLBracLoc(),
147 2597: "LLVM IR generation of compound statement ('{}')");
148 :
149 2597: CGDebugInfo *DI = getDebugInfo();
79: branch 0 taken
2518: branch 1 taken
150 2597: if (DI) {
151 79: DI->setLocation(S.getLBracLoc());
152 79: DI->EmitRegionStart(CurFn, Builder);
153 : }
154 :
155 : // Keep track of the current cleanup stack depth.
156 2597: CleanupScope Scope(*this);
157 :
4569: branch 1 taken
2597: branch 2 taken
158 9763: for (CompoundStmt::const_body_iterator I = S.body_begin(),
159 2597: E = S.body_end()-GetLast; I != E; ++I)
160 4569: EmitStmt(*I);
161 :
79: branch 0 taken
2518: branch 1 taken
162 2597: if (DI) {
163 79: DI->setLocation(S.getLBracLoc());
164 79: DI->EmitRegionEnd(CurFn, Builder);
165 : }
166 :
167 : RValue RV;
2594: branch 0 taken
3: branch 1 taken
168 2597: if (!GetLast)
169 2594: RV = RValue::get(0);
170 : else {
171 : // We have to special case labels here. They are statements, but when put
172 : // at the end of a statement expression, they yield the value of their
173 : // subexpression. Handle this by walking through all labels we encounter,
174 : // emitting them before we evaluate the subexpr.
175 3: const Stmt *LastStmt = S.body_back();
0: branch 1 not taken
3: branch 2 taken
176 3: while (const LabelStmt *LS = dyn_cast<LabelStmt>(LastStmt)) {
177 0: EmitLabel(*LS);
178 0: LastStmt = LS->getSubStmt();
179 : }
180 :
181 3: EnsureInsertPoint();
182 :
183 3: RV = EmitAnyExpr(cast<Expr>(LastStmt), AggLoc);
184 : }
185 :
186 2597: return RV;
187 : }
188 :
189 58: void CodeGenFunction::SimplifyForwardingBlocks(llvm::BasicBlock *BB) {
190 58: llvm::BranchInst *BI = dyn_cast<llvm::BranchInst>(BB->getTerminator());
191 :
192 : // If there is a cleanup stack, then we it isn't worth trying to
193 : // simplify this block (we would need to remove it from the scope map
194 : // and cleanup entry).
2: branch 1 taken
56: branch 2 taken
195 58: if (!CleanupEntries.empty())
196 2: return;
197 :
198 : // Can only simplify direct branches.
56: branch 0 taken
0: branch 1 not taken
0: branch 3 not taken
56: branch 4 taken
0: branch 5 not taken
56: branch 6 taken
199 56: if (!BI || !BI->isUnconditional())
200 0: return;
201 :
202 56: BB->replaceAllUsesWith(BI->getSuccessor(0));
203 56: BI->eraseFromParent();
204 56: BB->eraseFromParent();
205 : }
206 :
207 2602: void CodeGenFunction::EmitBlock(llvm::BasicBlock *BB, bool IsFinished) {
208 : // Fall out of the current block (if necessary).
209 2602: EmitBranch(BB);
210 :
444: branch 0 taken
2158: branch 1 taken
60: branch 3 taken
384: branch 4 taken
60: branch 5 taken
2542: branch 6 taken
211 2602: if (IsFinished && BB->use_empty()) {
60: branch 0 taken
0: branch 1 not taken
212 60: delete BB;
213 60: return;
214 : }
215 :
216 : // If necessary, associate the block with the cleanup stack size.
576: branch 1 taken
1966: branch 2 taken
217 2542: if (!CleanupEntries.empty()) {
218 : // Check if the basic block has already been inserted.
219 576: BlockScopeMap::iterator I = BlockScopes.find(BB);
1: branch 3 taken
575: branch 4 taken
220 576: if (I != BlockScopes.end()) {
0: branch 2 not taken
1: branch 3 taken
221 1: assert(I->second == CleanupEntries.size() - 1);
222 : } else {
223 575: BlockScopes[BB] = CleanupEntries.size() - 1;
224 575: CleanupEntries.back().Blocks.push_back(BB);
225 : }
226 : }
227 :
228 2542: CurFn->getBasicBlockList().push_back(BB);
229 2542: Builder.SetInsertPoint(BB);
230 : }
231 :
232 3282: void CodeGenFunction::EmitBranch(llvm::BasicBlock *Target) {
233 : // Emit a branch from the current block to the target one if this
234 : // was a real block. If this was just a fall-through block after a
235 : // terminator, don't emit it.
236 3282: llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
237 :
2210: branch 0 taken
1072: branch 1 taken
918: branch 3 taken
1292: branch 4 taken
1292: branch 5 taken
1990: branch 6 taken
238 3282: if (!CurBB || CurBB->getTerminator()) {
239 : // If there is no insert point or the previous block is already
240 : // terminated, don't touch it.
241 : } else {
242 : // Otherwise, create a fall-through branch.
243 1292: Builder.CreateBr(Target);
244 : }
245 :
246 3282: Builder.ClearInsertionPoint();
247 3282: }
248 :
249 31: void CodeGenFunction::EmitLabel(const LabelStmt &S) {
250 31: EmitBlock(getBasicBlockForLabel(&S));
251 31: }
252 :
253 :
254 31: void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) {
255 31: EmitLabel(S);
256 31: EmitStmt(S.getSubStmt());
257 31: }
258 :
259 11: void CodeGenFunction::EmitGotoStmt(const GotoStmt &S) {
260 : // If this code is reachable then emit a stop point (if generating
261 : // debug info). We have to do this ourselves because we are on the
262 : // "simple" statement path.
6: branch 1 taken
5: branch 2 taken
263 11: if (HaveInsertPoint())
264 6: EmitStopPoint(&S);
265 :
266 11: EmitBranchThroughCleanup(getBasicBlockForLabel(S.getLabel()));
267 11: }
268 :
269 :
270 12: void CodeGenFunction::EmitIndirectGotoStmt(const IndirectGotoStmt &S) {
271 : // Ensure that we have an i8* for our PHI node.
272 : llvm::Value *V = Builder.CreateBitCast(EmitScalarExpr(S.getTarget()),
273 : llvm::Type::getInt8PtrTy(VMContext),
274 12: "addr");
275 12: llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
276 :
277 :
278 : // Get the basic block for the indirect goto.
279 12: llvm::BasicBlock *IndGotoBB = GetIndirectGotoBlock();
280 :
281 : // The first instruction in the block has to be the PHI for the switch dest,
282 : // add an entry for this branch.
283 12: cast<llvm::PHINode>(IndGotoBB->begin())->addIncoming(V, CurBB);
284 :
285 12: EmitBranch(IndGotoBB);
286 12: }
287 :
288 224: void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
289 : // C99 6.8.4.1: The first substatement is executed if the expression compares
290 : // unequal to 0. The condition must be a scalar type.
291 224: CleanupScope ConditionScope(*this);
292 :
6: branch 1 taken
218: branch 2 taken
293 224: if (S.getConditionVariable())
294 6: EmitLocalBlockVarDecl(*S.getConditionVariable());
295 :
296 : // If the condition constant folds and can be elided, try to avoid emitting
297 : // the condition and the dead arm of the if/else.
15: branch 2 taken
209: branch 3 taken
298 224: if (int Cond = ConstantFoldsToSimpleInteger(S.getCond())) {
299 : // Figure out which block (then or else) is executed.
300 15: const Stmt *Executed = S.getThen(), *Skipped = S.getElse();
11: branch 0 taken
4: branch 1 taken
301 15: if (Cond == -1) // Condition false?
302 11: std::swap(Executed, Skipped);
303 :
304 : // If the skipped block has no labels in it, just emit the executed block.
305 : // This avoids emitting dead code and simplifies the CFG substantially.
15: branch 1 taken
0: branch 2 not taken
306 15: if (!ContainsLabel(Skipped)) {
14: branch 0 taken
1: branch 1 taken
307 15: if (Executed) {
308 14: CleanupScope ExecutedScope(*this);
309 14: EmitStmt(Executed);
310 : }
311 15: return;
312 : }
313 : }
314 :
315 : // Otherwise, the condition did not fold, or we couldn't elide it. Just emit
316 : // the conditional branch.
317 209: llvm::BasicBlock *ThenBlock = createBasicBlock("if.then");
318 209: llvm::BasicBlock *ContBlock = createBasicBlock("if.end");
319 209: llvm::BasicBlock *ElseBlock = ContBlock;
34: branch 1 taken
175: branch 2 taken
320 209: if (S.getElse())
321 34: ElseBlock = createBasicBlock("if.else");
322 209: EmitBranchOnBoolExpr(S.getCond(), ThenBlock, ElseBlock);
323 :
324 : // Emit the 'then' code.
325 209: EmitBlock(ThenBlock);
326 : {
327 209: CleanupScope ThenScope(*this);
328 209: EmitStmt(S.getThen());
329 : }
330 209: EmitBranch(ContBlock);
331 :
332 : // Emit the 'else' code if present.
34: branch 1 taken
175: branch 2 taken
333 209: if (const Stmt *Else = S.getElse()) {
334 34: EmitBlock(ElseBlock);
335 : {
336 34: CleanupScope ElseScope(*this);
337 34: EmitStmt(Else);
338 : }
339 34: EmitBranch(ContBlock);
340 : }
341 :
342 : // Emit the continuation block for code after the if.
209: branch 2 taken
15: branch 3 taken
343 209: EmitBlock(ContBlock, true);
344 : }
345 :
346 63: void CodeGenFunction::EmitWhileStmt(const WhileStmt &S) {
347 : // Emit the header for the loop, insert it, which will create an uncond br to
348 : // it.
349 63: llvm::BasicBlock *LoopHeader = createBasicBlock("while.cond");
350 63: EmitBlock(LoopHeader);
351 :
352 : // Create an exit block for when the condition fails, create a block for the
353 : // body of the loop.
354 63: llvm::BasicBlock *ExitBlock = createBasicBlock("while.end");
355 63: llvm::BasicBlock *LoopBody = createBasicBlock("while.body");
356 63: llvm::BasicBlock *CleanupBlock = 0;
357 63: llvm::BasicBlock *EffectiveExitBlock = ExitBlock;
358 :
359 : // Store the blocks to use for break and continue.
360 63: BreakContinueStack.push_back(BreakContinue(ExitBlock, LoopHeader));
361 :
362 : // C++ [stmt.while]p2:
363 : // When the condition of a while statement is a declaration, the
364 : // scope of the variable that is declared extends from its point
365 : // of declaration (3.3.2) to the end of the while statement.
366 : // [...]
367 : // The object created in a condition is destroyed and created
368 : // with each iteration of the loop.
369 63: CleanupScope ConditionScope(*this);
370 :
4: branch 1 taken
59: branch 2 taken
371 63: if (S.getConditionVariable()) {
372 4: EmitLocalBlockVarDecl(*S.getConditionVariable());
373 :
374 : // If this condition variable requires cleanups, create a basic
375 : // block to handle those cleanups.
3: branch 1 taken
1: branch 2 taken
376 4: if (ConditionScope.requiresCleanups()) {
377 3: CleanupBlock = createBasicBlock("while.cleanup");
378 3: EffectiveExitBlock = CleanupBlock;
379 : }
380 : }
381 :
382 : // Evaluate the conditional in the while header. C99 6.8.5.1: The
383 : // evaluation of the controlling expression takes place before each
384 : // execution of the loop body.
385 63: llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
386 :
387 : // while(1) is common, avoid extra exit blocks. Be sure
388 : // to correctly handle break/continue though.
389 63: bool EmitBoolCondBranch = true;
52: branch 1 taken
11: branch 2 taken
390 63: if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal))
47: branch 1 taken
5: branch 2 taken
391 52: if (C->isOne())
392 47: EmitBoolCondBranch = false;
393 :
394 : // As long as the condition is true, go to the loop body.
16: branch 0 taken
47: branch 1 taken
395 63: if (EmitBoolCondBranch)
396 16: Builder.CreateCondBr(BoolCondVal, LoopBody, EffectiveExitBlock);
397 :
398 : // Emit the loop body.
399 : {
400 63: CleanupScope BodyScope(*this);
401 63: EmitBlock(LoopBody);
402 63: EmitStmt(S.getBody());
403 : }
404 :
405 63: BreakContinueStack.pop_back();
406 :
3: branch 0 taken
60: branch 1 taken
407 63: if (CleanupBlock) {
408 : // If we have a cleanup block, jump there to perform cleanups
409 : // before looping.
410 3: EmitBranch(CleanupBlock);
411 :
412 : // Emit the cleanup block, performing cleanups for the condition
413 : // and then jumping to either the loop header or the exit block.
414 3: EmitBlock(CleanupBlock);
415 3: ConditionScope.ForceCleanup();
416 3: Builder.CreateCondBr(BoolCondVal, LoopHeader, ExitBlock);
417 : } else {
418 : // Cycle to the condition.
419 60: EmitBranch(LoopHeader);
420 : }
421 :
422 : // Emit the exit block.
423 63: EmitBlock(ExitBlock, true);
424 :
425 :
426 : // The LoopHeader typically is just a branch if we skipped emitting
427 : // a branch, try to erase it.
47: branch 0 taken
16: branch 1 taken
47: branch 2 taken
0: branch 3 not taken
428 63: if (!EmitBoolCondBranch && !CleanupBlock)
429 47: SimplifyForwardingBlocks(LoopHeader);
430 63: }
431 :
432 15: void CodeGenFunction::EmitDoStmt(const DoStmt &S) {
433 : // Emit the body for the loop, insert it, which will create an uncond br to
434 : // it.
435 15: llvm::BasicBlock *LoopBody = createBasicBlock("do.body");
436 15: llvm::BasicBlock *AfterDo = createBasicBlock("do.end");
437 15: EmitBlock(LoopBody);
438 :
439 15: llvm::BasicBlock *DoCond = createBasicBlock("do.cond");
440 :
441 : // Store the blocks to use for break and continue.
442 15: BreakContinueStack.push_back(BreakContinue(AfterDo, DoCond));
443 :
444 : // Emit the body of the loop into the block.
445 15: EmitStmt(S.getBody());
446 :
447 15: BreakContinueStack.pop_back();
448 :
449 15: EmitBlock(DoCond);
450 :
451 : // C99 6.8.5.2: "The evaluation of the controlling expression takes place
452 : // after each execution of the loop body."
453 :
454 : // Evaluate the conditional in the while header.
455 : // C99 6.8.5p2/p4: The first substatement is executed if the expression
456 : // compares unequal to 0. The condition must be a scalar type.
457 15: llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
458 :
459 : // "do {} while (0)" is common in macros, avoid extra blocks. Be sure
460 : // to correctly handle break/continue though.
461 15: bool EmitBoolCondBranch = true;
13: branch 1 taken
2: branch 2 taken
462 15: if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal))
11: branch 1 taken
2: branch 2 taken
463 13: if (C->isZero())
464 11: EmitBoolCondBranch = false;
465 :
466 : // As long as the condition is true, iterate the loop.
4: branch 0 taken
11: branch 1 taken
467 15: if (EmitBoolCondBranch)
468 4: Builder.CreateCondBr(BoolCondVal, LoopBody, AfterDo);
469 :
470 : // Emit the exit block.
471 15: EmitBlock(AfterDo);
472 :
473 : // The DoCond block typically is just a branch if we skipped
474 : // emitting a branch, try to erase it.
11: branch 0 taken
4: branch 1 taken
475 15: if (!EmitBoolCondBranch)
476 11: SimplifyForwardingBlocks(DoCond);
477 15: }
478 :
479 69: void CodeGenFunction::EmitForStmt(const ForStmt &S) {
480 : // FIXME: What do we do if the increment (f.e.) contains a stmt expression,
481 : // which contains a continue/break?
482 69: CleanupScope ForScope(*this);
483 :
484 : // Evaluate the first part before the loop.
63: branch 1 taken
6: branch 2 taken
485 69: if (S.getInit())
486 63: EmitStmt(S.getInit());
487 :
488 : // Start the loop with a block that tests the condition.
489 69: llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
490 69: llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
491 69: llvm::BasicBlock *IncBlock = 0;
492 69: llvm::BasicBlock *CondCleanup = 0;
493 69: llvm::BasicBlock *EffectiveExitBlock = AfterFor;
494 69: EmitBlock(CondBlock);
495 :
496 : // Create a cleanup scope for the condition variable cleanups.
497 69: CleanupScope ConditionScope(*this);
498 :
499 69: llvm::Value *BoolCondVal = 0;
63: branch 1 taken
6: branch 2 taken
500 69: if (S.getCond()) {
501 : // If the for statement has a condition scope, emit the local variable
502 : // declaration.
2: branch 1 taken
61: branch 2 taken
503 63: if (S.getConditionVariable()) {
504 2: EmitLocalBlockVarDecl(*S.getConditionVariable());
505 :
1: branch 1 taken
1: branch 2 taken
506 2: if (ConditionScope.requiresCleanups()) {
507 1: CondCleanup = createBasicBlock("for.cond.cleanup");
508 1: EffectiveExitBlock = CondCleanup;
509 : }
510 : }
511 :
512 : // As long as the condition is true, iterate the loop.
513 63: llvm::BasicBlock *ForBody = createBasicBlock("for.body");
514 :
515 : // C99 6.8.5p2/p4: The first substatement is executed if the expression
516 : // compares unequal to 0. The condition must be a scalar type.
517 63: BoolCondVal = EvaluateExprAsBool(S.getCond());
518 63: Builder.CreateCondBr(BoolCondVal, ForBody, EffectiveExitBlock);
519 :
520 63: EmitBlock(ForBody);
521 : } else {
522 : // Treat it as a non-zero constant. Don't even create a new block for the
523 : // body, just fall into it.
524 : }
525 :
526 : // If the for loop doesn't have an increment we can just use the
527 : // condition as the continue block.
528 : llvm::BasicBlock *ContinueBlock;
63: branch 1 taken
6: branch 2 taken
529 69: if (S.getInc())
530 63: ContinueBlock = IncBlock = createBasicBlock("for.inc");
531 : else
532 6: ContinueBlock = CondBlock;
533 :
534 : // Store the blocks to use for break and continue.
535 69: BreakContinueStack.push_back(BreakContinue(AfterFor, ContinueBlock));
536 :
537 : // If the condition is true, execute the body of the for stmt.
538 69: CGDebugInfo *DI = getDebugInfo();
3: branch 0 taken
66: branch 1 taken
539 69: if (DI) {
540 3: DI->setLocation(S.getSourceRange().getBegin());
541 3: DI->EmitRegionStart(CurFn, Builder);
542 : }
543 :
544 : {
545 : // Create a separate cleanup scope for the body, in case it is not
546 : // a compound statement.
547 69: CleanupScope BodyScope(*this);
548 69: EmitStmt(S.getBody());
549 : }
550 :
551 69: BreakContinueStack.pop_back();
552 :
553 : // If there is an increment, emit it next.
63: branch 1 taken
6: branch 2 taken
554 69: if (S.getInc()) {
555 63: EmitBlock(IncBlock);
556 63: EmitStmt(S.getInc());
557 : }
558 :
559 : // Finally, branch back up to the condition for the next iteration.
1: branch 0 taken
68: branch 1 taken
560 69: if (CondCleanup) {
561 : // Branch to the cleanup block.
562 1: EmitBranch(CondCleanup);
563 :
564 : // Emit the cleanup block, which branches back to the loop body or
565 : // outside of the for statement once it is done.
566 1: EmitBlock(CondCleanup);
567 1: ConditionScope.ForceCleanup();
568 1: Builder.CreateCondBr(BoolCondVal, CondBlock, AfterFor);
569 : } else
570 68: EmitBranch(CondBlock);
3: branch 0 taken
66: branch 1 taken
571 69: if (DI) {
572 3: DI->setLocation(S.getSourceRange().getEnd());
573 3: DI->EmitRegionEnd(CurFn, Builder);
574 : }
575 :
576 : // Emit the fall-through block.
577 69: EmitBlock(AfterFor, true);
578 69: }
579 :
580 129: void CodeGenFunction::EmitReturnOfRValue(RValue RV, QualType Ty) {
129: branch 1 taken
0: branch 2 not taken
581 129: if (RV.isScalar()) {
582 129: Builder.CreateStore(RV.getScalarVal(), ReturnValue);
0: branch 1 not taken
0: branch 2 not taken
583 0: } else if (RV.isAggregate()) {
584 0: EmitAggregateCopy(ReturnValue, RV.getAggregateAddr(), Ty);
585 : } else {
586 0: StoreComplexToAddr(RV.getComplexVal(), ReturnValue, false);
587 : }
588 129: EmitBranchThroughCleanup(ReturnBlock);
589 129: }
590 :
591 : /// EmitReturnStmt - Note that due to GCC extensions, this can have an operand
592 : /// if the function returns void, or may be missing one if the function returns
593 : /// non-void. Fun stuff :).
594 752: void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) {
595 : // Emit the result value, even if unused, to evalute the side effects.
596 752: const Expr *RV = S.getRetValue();
597 :
598 : // FIXME: Clean this up by using an LValue for ReturnTemp,
599 : // EmitStoreThroughLValue, and EmitAnyExpr.
15: branch 0 taken
737: branch 1 taken
600 752: if (!ReturnValue) {
601 : // Make sure not to return anything, but evaluate the expression
602 : // for side effects.
1: branch 0 taken
14: branch 1 taken
603 15: if (RV)
604 1: EmitAnyExpr(RV);
736: branch 0 taken
1: branch 1 taken
605 737: } else if (RV == 0) {
606 : // Do nothing (return value is left uninitialized)
41: branch 2 taken
695: branch 3 taken
607 736: } else if (FnRetTy->isReferenceType()) {
608 : // If this function returns a reference, take the address of the expression
609 : // rather than the value.
610 41: Builder.CreateStore(EmitLValue(RV).getAddress(), ReturnValue);
643: branch 2 taken
52: branch 3 taken
611 695: } else if (!hasAggregateLLVMType(RV->getType())) {
612 643: Builder.CreateStore(EmitScalarExpr(RV), ReturnValue);
9: branch 3 taken
43: branch 4 taken
613 52: } else if (RV->getType()->isAnyComplexType()) {
614 9: EmitComplexExprIntoAddr(RV, ReturnValue, false);
615 : } else {
616 43: EmitAggExpr(RV, ReturnValue, false);
617 : }
618 :
619 752: EmitBranchThroughCleanup(ReturnBlock);
620 752: }
621 :
622 1152: void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) {
623 : // As long as debug info is modeled with instructions, we have to ensure we
624 : // have a place to insert here and write the stop point here.
130: branch 1 taken
1022: branch 2 taken
625 1152: if (getDebugInfo()) {
626 130: EnsureInsertPoint();
627 130: EmitStopPoint(&S);
628 : }
629 :
1276: branch 2 taken
1152: branch 3 taken
630 2428: for (DeclStmt::const_decl_iterator I = S.decl_begin(), E = S.decl_end();
631 : I != E; ++I)
632 1276: EmitDecl(**I);
633 1152: }
634 :
635 63: void CodeGenFunction::EmitBreakStmt(const BreakStmt &S) {
63: branch 1 taken
0: branch 2 not taken
636 63: assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!");
637 :
638 : // If this code is reachable then emit a stop point (if generating
639 : // debug info). We have to do this ourselves because we are on the
640 : // "simple" statement path.
63: branch 1 taken
0: branch 2 not taken
641 63: if (HaveInsertPoint())
642 63: EmitStopPoint(&S);
643 :
644 63: llvm::BasicBlock *Block = BreakContinueStack.back().BreakBlock;
645 63: EmitBranchThroughCleanup(Block);
646 63: }
647 :
648 12: void CodeGenFunction::EmitContinueStmt(const ContinueStmt &S) {
12: branch 1 taken
0: branch 2 not taken
649 12: assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
650 :
651 : // If this code is reachable then emit a stop point (if generating
652 : // debug info). We have to do this ourselves because we are on the
653 : // "simple" statement path.
7: branch 1 taken
5: branch 2 taken
654 12: if (HaveInsertPoint())
655 7: EmitStopPoint(&S);
656 :
657 12: llvm::BasicBlock *Block = BreakContinueStack.back().ContinueBlock;
658 12: EmitBranchThroughCleanup(Block);
659 12: }
660 :
661 : /// EmitCaseStmtRange - If case statement range is not too big then
662 : /// add multiple cases to switch instruction, one for each value within
663 : /// the range. If range is too big then emit "if" condition check.
664 15: void CodeGenFunction::EmitCaseStmtRange(const CaseStmt &S) {
15: branch 1 taken
0: branch 2 not taken
665 15: assert(S.getRHS() && "Expected RHS value in CaseStmt");
666 :
667 15: llvm::APSInt LHS = S.getLHS()->EvaluateAsInt(getContext());
668 15: llvm::APSInt RHS = S.getRHS()->EvaluateAsInt(getContext());
669 :
670 : // Emit the code for this case. We do this first to make sure it is
671 : // properly chained from our predecessor before generating the
672 : // switch machinery to enter this block.
673 15: EmitBlock(createBasicBlock("sw.bb"));
674 15: llvm::BasicBlock *CaseDest = Builder.GetInsertBlock();
675 15: EmitStmt(S.getSubStmt());
676 :
677 : // If range is empty, do nothing.
11: branch 1 taken
4: branch 2 taken
4: branch 5 taken
11: branch 6 taken
678 26: if (LHS.isSigned() ? RHS.slt(LHS) : RHS.ult(LHS))
679 10: return;
680 :
681 11: llvm::APInt Range = RHS - LHS;
682 : // FIXME: parameters such as this should not be hardcoded.
6: branch 4 taken
5: branch 5 taken
683 11: if (Range.ult(llvm::APInt(Range.getBitWidth(), 64))) {
684 : // Range is small enough to add multiple switch instruction cases.
19: branch 1 taken
6: branch 2 taken
685 25: for (unsigned i = 0, e = Range.getZExtValue() + 1; i != e; ++i) {
686 19: SwitchInsn->addCase(llvm::ConstantInt::get(VMContext, LHS), CaseDest);
687 19: LHS++;
688 : }
689 : return;
690 : }
691 :
692 : // The range is too big. Emit "if" condition into a new block,
693 : // making sure to save and restore the current insertion point.
694 5: llvm::BasicBlock *RestoreBB = Builder.GetInsertBlock();
695 :
696 : // Push this test onto the chain of range checks (which terminates
697 : // in the default basic block). The switch's default will be changed
698 : // to the top of this chain after switch emission is complete.
699 5: llvm::BasicBlock *FalseDest = CaseRangeBlock;
700 5: CaseRangeBlock = createBasicBlock("sw.caserange");
701 :
702 5: CurFn->getBasicBlockList().push_back(CaseRangeBlock);
703 5: Builder.SetInsertPoint(CaseRangeBlock);
704 :
705 : // Emit range check.
706 : llvm::Value *Diff =
707 : Builder.CreateSub(SwitchInsn->getCondition(),
708 5: llvm::ConstantInt::get(VMContext, LHS), "tmp");
709 : llvm::Value *Cond =
710 : Builder.CreateICmpULE(Diff,
711 5: llvm::ConstantInt::get(VMContext, Range), "tmp");
712 5: Builder.CreateCondBr(Cond, CaseDest, FalseDest);
713 :
714 : // Restore the appropriate insertion point.
3: branch 0 taken
2: branch 1 taken
715 5: if (RestoreBB)
716 3: Builder.SetInsertPoint(RestoreBB);
717 : else
5: branch 2 taken
6: branch 3 taken
5: branch 5 taken
10: branch 6 taken
5: branch 8 taken
10: branch 9 taken
718 2: Builder.ClearInsertionPoint();
719 : }
720 :
721 46: void CodeGenFunction::EmitCaseStmt(const CaseStmt &S) {
15: branch 1 taken
31: branch 2 taken
722 46: if (S.getRHS()) {
723 15: EmitCaseStmtRange(S);
724 15: return;
725 : }
726 :
727 31: EmitBlock(createBasicBlock("sw.bb"));
728 31: llvm::BasicBlock *CaseDest = Builder.GetInsertBlock();
729 31: llvm::APSInt CaseVal = S.getLHS()->EvaluateAsInt(getContext());
730 31: SwitchInsn->addCase(llvm::ConstantInt::get(VMContext, CaseVal), CaseDest);
731 :
732 : // Recursively emitting the statement is acceptable, but is not wonderful for
733 : // code where we have many case statements nested together, i.e.:
734 : // case 1:
735 : // case 2:
736 : // case 3: etc.
737 : // Handling this recursively will create a new block for each case statement
738 : // that falls through to the next case which is IR intensive. It also causes
739 : // deep recursion which can run into stack depth limitations. Handle
740 : // sequential non-range case statements specially.
741 31: const CaseStmt *CurCase = &S;
742 31: const CaseStmt *NextCase = dyn_cast<CaseStmt>(S.getSubStmt());
743 :
744 : // Otherwise, iteratively add consequtive cases to this switch stmt.
8: branch 0 taken
25: branch 1 taken
2: branch 3 taken
6: branch 4 taken
2: branch 5 taken
31: branch 6 taken
745 64: while (NextCase && NextCase->getRHS() == 0) {
746 2: CurCase = NextCase;
747 2: CaseVal = CurCase->getLHS()->EvaluateAsInt(getContext());
748 2: SwitchInsn->addCase(llvm::ConstantInt::get(VMContext, CaseVal), CaseDest);
749 :
750 2: NextCase = dyn_cast<CaseStmt>(CurCase->getSubStmt());
751 : }
752 :
753 : // Normal default recursion for non-cases.
754 31: EmitStmt(CurCase->getSubStmt());
755 : }
756 :
757 21: void CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S) {
758 21: llvm::BasicBlock *DefaultBlock = SwitchInsn->getDefaultDest();
759 : assert(DefaultBlock->empty() &&
21: branch 1 taken
0: branch 2 not taken
760 21: "EmitDefaultStmt: Default block already defined?");
761 21: EmitBlock(DefaultBlock);
762 21: EmitStmt(S.getSubStmt());
763 21: }
764 :
765 35: void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) {
766 35: CleanupScope ConditionScope(*this);
767 :
2: branch 1 taken
33: branch 2 taken
768 35: if (S.getConditionVariable())
769 2: EmitLocalBlockVarDecl(*S.getConditionVariable());
770 :
771 35: llvm::Value *CondV = EmitScalarExpr(S.getCond());
772 :
773 : // Handle nested switch statements.
774 35: llvm::SwitchInst *SavedSwitchInsn = SwitchInsn;
775 35: llvm::BasicBlock *SavedCRBlock = CaseRangeBlock;
776 :
777 : // Create basic block to hold stuff that comes after switch
778 : // statement. We also need to create a default block now so that
779 : // explicit case ranges tests can have a place to jump to on
780 : // failure.
781 35: llvm::BasicBlock *NextBlock = createBasicBlock("sw.epilog");
782 35: llvm::BasicBlock *DefaultBlock = createBasicBlock("sw.default");
783 35: SwitchInsn = Builder.CreateSwitch(CondV, DefaultBlock);
784 35: CaseRangeBlock = DefaultBlock;
785 :
786 : // Clear the insertion point to indicate we are in unreachable code.
787 35: Builder.ClearInsertionPoint();
788 :
789 : // All break statements jump to NextBlock. If BreakContinueStack is non empty
790 : // then reuse last ContinueBlock.
791 35: llvm::BasicBlock *ContinueBlock = 0;
2: branch 1 taken
33: branch 2 taken
792 35: if (!BreakContinueStack.empty())
793 2: ContinueBlock = BreakContinueStack.back().ContinueBlock;
794 :
795 : // Ensure any vlas created between there and here, are undone
796 35: BreakContinueStack.push_back(BreakContinue(NextBlock, ContinueBlock));
797 :
798 : // Emit switch body.
799 35: EmitStmt(S.getBody());
800 :
801 35: BreakContinueStack.pop_back();
802 :
803 : // Update the default block in case explicit case range tests have
804 : // been chained on top.
805 35: SwitchInsn->setSuccessor(0, CaseRangeBlock);
806 :
807 : // If a default was never emitted then reroute any jumps to it and
808 : // discard.
14: branch 1 taken
21: branch 2 taken
809 35: if (!DefaultBlock->getParent()) {
810 14: DefaultBlock->replaceAllUsesWith(NextBlock);
14: branch 0 taken
0: branch 1 not taken
811 14: delete DefaultBlock;
812 : }
813 :
814 : // Emit continuation.
815 35: EmitBlock(NextBlock, true);
816 :
817 35: SwitchInsn = SavedSwitchInsn;
818 35: CaseRangeBlock = SavedCRBlock;
819 35: }
820 :
821 : static std::string
822 : SimplifyConstraint(const char *Constraint, const TargetInfo &Target,
823 44: llvm::SmallVectorImpl<TargetInfo::ConstraintInfo> *OutCons=0) {
824 44: std::string Result;
825 :
49: branch 0 taken
44: branch 1 taken
826 137: while (*Constraint) {
49: branch 0 taken
0: branch 1 not taken
0: branch 2 not taken
0: branch 3 not taken
827 49: switch (*Constraint) {
828 : default:
829 49: Result += Target.convertConstraint(*Constraint);
830 49: break;
831 : // Ignore these
832 : case '*':
833 : case '?':
834 : case '!':
835 0: break;
836 : case 'g':
837 0: Result += "imr";
838 0: break;
839 : case '[': {
840 : assert(OutCons &&
0: branch 0 not taken
0: branch 1 not taken
841 0: "Must pass output names to constraints with a symbolic name");
842 : unsigned Index;
843 : bool result = Target.resolveSymbolicName(Constraint,
844 : &(*OutCons)[0],
845 0: OutCons->size(), Index);
0: branch 0 not taken
0: branch 1 not taken
846 0: assert(result && "Could not resolve symbolic name"); result=result;
847 0: Result += llvm::utostr(Index);
848 : break;
849 : }
850 : }
851 :
852 49: Constraint++;
853 : }
854 :
855 : return Result;
856 : }
857 :
858 : llvm::Value* CodeGenFunction::EmitAsmInput(const AsmStmt &S,
859 : const TargetInfo::ConstraintInfo &Info,
860 : const Expr *InputExpr,
861 31: std::string &ConstraintStr) {
862 : llvm::Value *Arg;
9: branch 1 taken
22: branch 2 taken
3: branch 4 taken
6: branch 5 taken
25: branch 6 taken
6: branch 7 taken
863 31: if (Info.allowsRegister() || !Info.allowsMemory()) {
25: branch 2 taken
0: branch 3 not taken
864 25: if (!CodeGenFunction::hasAggregateLLVMType(InputExpr->getType())) {
865 25: Arg = EmitScalarExpr(InputExpr);
866 : } else {
867 0: InputExpr = InputExpr->IgnoreParenNoopCasts(getContext());
868 0: LValue Dest = EmitLValue(InputExpr);
869 :
870 0: const llvm::Type *Ty = ConvertType(InputExpr->getType());
871 0: uint64_t Size = CGM.getTargetData().getTypeSizeInBits(Ty);
0: branch 0 not taken
0: branch 1 not taken
0: branch 3 not taken
0: branch 4 not taken
0: branch 5 not taken
0: branch 6 not taken
872 0: if (Size <= 64 && llvm::isPowerOf2_64(Size)) {
873 0: Ty = llvm::IntegerType::get(VMContext, Size);
874 0: Ty = llvm::PointerType::getUnqual(Ty);
875 :
876 0: Arg = Builder.CreateLoad(Builder.CreateBitCast(Dest.getAddress(), Ty));
877 : } else {
878 0: Arg = Dest.getAddress();
879 0: ConstraintStr += '*';
880 : }
881 : }
882 : } else {
883 6: InputExpr = InputExpr->IgnoreParenNoopCasts(getContext());
884 6: LValue Dest = EmitLValue(InputExpr);
885 6: Arg = Dest.getAddress();
886 6: ConstraintStr += '*';
887 : }
888 :
889 31: return Arg;
890 : }
891 :
892 35: void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) {
893 : // Analyze the asm string to decompose it into its pieces. We know that Sema
894 : // has already done this, so it is guaranteed to be successful.
895 35: llvm::SmallVector<AsmStmt::AsmStringPiece, 4> Pieces;
896 : unsigned DiagOffs;
897 35: S.AnalyzeAsmString(Pieces, getContext(), DiagOffs);
898 :
899 : // Assemble the pieces into the final asm string.
900 35: std::string AsmString;
44: branch 1 taken
35: branch 2 taken
901 79: for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
32: branch 2 taken
12: branch 3 taken
902 44: if (Pieces[i].isString())
903 32: AsmString += Pieces[i].getString();
11: branch 2 taken
1: branch 3 taken
904 12: else if (Pieces[i].getModifier() == '\0')
905 11: AsmString += '$' + llvm::utostr(Pieces[i].getOperandNo());
906 : else
907 : AsmString += "${" + llvm::utostr(Pieces[i].getOperandNo()) + ':' +
908 1: Pieces[i].getModifier() + '}';
909 : }
910 :
911 : // Get all the output and input constraints together.
912 35: llvm::SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos;
913 35: llvm::SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos;
914 :
21: branch 2 taken
35: branch 3 taken
915 112: for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) {
916 : TargetInfo::ConstraintInfo Info(S.getOutputConstraint(i),
917 21: S.getOutputName(i));
918 : assert(Target.validateOutputConstraint(Info) &&
21: branch 1 taken
0: branch 2 not taken
919 21: "Failed to parse output constraint");
920 21: OutputConstraintInfos.push_back(Info);
921 : }
922 :
23: branch 2 taken
35: branch 3 taken
923 116: for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) {
924 : TargetInfo::ConstraintInfo Info(S.getInputConstraint(i),
925 23: S.getInputName(i));
926 : assert(Target.validateInputConstraint(OutputConstraintInfos.data(),
927 : S.getNumOutputs(),
928 : Info) &&
23: branch 3 taken
0: branch 4 not taken
929 23: "Failed to parse input constraint");
930 23: InputConstraintInfos.push_back(Info);
931 : }
932 :
933 35: std::string Constraints;
934 :
935 35: std::vector<LValue> ResultRegDests;
936 35: std::vector<QualType> ResultRegQualTys;
937 35: std::vector<const llvm::Type *> ResultRegTypes;
938 35: std::vector<const llvm::Type *> ResultTruncRegTypes;
939 35: std::vector<const llvm::Type*> ArgTypes;
940 35: std::vector<llvm::Value*> Args;
941 :
942 : // Keep track of inout constraints.
943 35: std::string InOutConstraints;
944 35: std::vector<llvm::Value*> InOutArgs;
945 35: std::vector<const llvm::Type*> InOutArgTypes;
946 :
21: branch 2 taken
35: branch 3 taken
947 56: for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) {
948 21: TargetInfo::ConstraintInfo &Info = OutputConstraintInfos[i];
949 :
950 : // Simplify the output constraint.
951 21: std::string OutputConstraint(S.getOutputConstraint(i));
952 21: OutputConstraint = SimplifyConstraint(OutputConstraint.c_str() + 1, Target);
953 :
954 21: const Expr *OutExpr = S.getOutputExpr(i);
955 21: OutExpr = OutExpr->IgnoreParenNoopCasts(getContext());
956 :
957 21: LValue Dest = EmitLValue(OutExpr);
4: branch 1 taken
17: branch 2 taken
958 21: if (!Constraints.empty())
959 4: Constraints += ',';
960 :
961 : // If this is a register output, then make the inline asm return it
962 : // by-value. If this is a memory result, return the value by-reference.
18: branch 1 taken
3: branch 2 taken
18: branch 5 taken
0: branch 6 not taken
18: branch 7 taken
3: branch 8 taken
963 21: if (!Info.allowsMemory() && !hasAggregateLLVMType(OutExpr->getType())) {
964 18: Constraints += "=" + OutputConstraint;
965 18: ResultRegQualTys.push_back(OutExpr->getType());
966 18: ResultRegDests.push_back(Dest);
967 18: ResultRegTypes.push_back(ConvertTypeForMem(OutExpr->getType()));
968 18: ResultTruncRegTypes.push_back(ResultRegTypes.back());
969 :
970 : // If this output is tied to an input, and if the input is larger, then
971 : // we need to set the actual result type of the inline asm node to be the
972 : // same as the input type.
5: branch 1 taken
13: branch 2 taken
973 18: if (Info.hasMatchingInput()) {
974 : unsigned InputNo;
6: branch 1 taken
0: branch 2 not taken
975 6: for (InputNo = 0; InputNo != S.getNumInputs(); ++InputNo) {
976 6: TargetInfo::ConstraintInfo &Input = InputConstraintInfos[InputNo];
5: branch 1 taken
1: branch 2 taken
5: branch 4 taken
0: branch 5 not taken
5: branch 6 taken
1: branch 7 taken
977 6: if (Input.hasTiedOperand() &&
978 : Input.getTiedOperand() == i)
979 5: break;
980 : }
5: branch 1 taken
0: branch 2 not taken
981 5: assert(InputNo != S.getNumInputs() && "Didn't find matching input!");
982 :
983 5: QualType InputTy = S.getInputExpr(InputNo)->getType();
984 5: QualType OutputTy = OutExpr->getType();
985 :
986 5: uint64_t InputSize = getContext().getTypeSize(InputTy);
2: branch 2 taken
3: branch 3 taken
987 5: if (getContext().getTypeSize(OutputTy) < InputSize) {
988 : // Form the asm to return the value as a larger integer type.
989 2: ResultRegTypes.back() = llvm::IntegerType::get(VMContext, (unsigned)InputSize);
990 : }
991 : }
992 : } else {
993 3: ArgTypes.push_back(Dest.getAddress()->getType());
994 3: Args.push_back(Dest.getAddress());
995 3: Constraints += "=*";
996 3: Constraints += OutputConstraint;
997 : }
998 :
8: branch 1 taken
13: branch 2 taken
999 21: if (Info.isReadWrite()) {
1000 8: InOutConstraints += ',';
1001 :
1002 8: const Expr *InputExpr = S.getOutputExpr(i);
1003 8: llvm::Value *Arg = EmitAsmInput(S, Info, InputExpr, InOutConstraints);
1004 :
5: branch 1 taken
3: branch 2 taken
1005 8: if (Info.allowsRegister())
1006 5: InOutConstraints += llvm::utostr(i);
1007 : else
1008 3: InOutConstraints += OutputConstraint;
1009 :
1010 8: InOutArgTypes.push_back(Arg->getType());
1011 8: InOutArgs.push_back(Arg);
1012 : }
1013 : }
1014 :
1015 35: unsigned NumConstraints = S.getNumOutputs() + S.getNumInputs();
1016 :
23: branch 2 taken
35: branch 3 taken
1017 58: for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) {
1018 23: const Expr *InputExpr = S.getInputExpr(i);
1019 :
1020 23: TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i];
1021 :
20: branch 1 taken
3: branch 2 taken
1022 23: if (!Constraints.empty())
1023 20: Constraints += ',';
1024 :
1025 : // Simplify the input constraint.
1026 23: std::string InputConstraint(S.getInputConstraint(i));
1027 : InputConstraint = SimplifyConstraint(InputConstraint.c_str(), Target,
1028 23: &OutputConstraintInfos);
1029 :
1030 23: llvm::Value *Arg = EmitAsmInput(S, Info, InputExpr, Constraints);
1031 :
1032 : // If this input argument is tied to a larger output result, extend the
1033 : // input to be the same size as the output. The LLVM backend wants to see
1034 : // the input and output of a matching constraint be the same size. Note
1035 : // that GCC does not define what the top bits are here. We use zext because
1036 : // that is usually cheaper, but LLVM IR should really get an anyext someday.
5: branch 1 taken
18: branch 2 taken
1037 23: if (Info.hasTiedOperand()) {
1038 5: unsigned Output = Info.getTiedOperand();
1039 5: QualType OutputTy = S.getOutputExpr(Output)->getType();
1040 5: QualType InputTy = InputExpr->getType();
1041 :
1: branch 4 taken
4: branch 5 taken
1042 5: if (getContext().getTypeSize(OutputTy) >
1043 : getContext().getTypeSize(InputTy)) {
1044 : // Use ptrtoint as appropriate so that we can do our extension.
0: branch 2 not taken
1: branch 3 taken
1045 1: if (isa<llvm::PointerType>(Arg->getType()))
1046 : Arg = Builder.CreatePtrToInt(Arg,
1047 0: llvm::IntegerType::get(VMContext, LLVMPointerWidth));
1048 1: unsigned OutputSize = (unsigned)getContext().getTypeSize(OutputTy);
1049 1: Arg = Builder.CreateZExt(Arg, llvm::IntegerType::get(VMContext, OutputSize));
1050 : }
1051 : }
1052 :
1053 :
1054 23: ArgTypes.push_back(Arg->getType());
1055 23: Args.push_back(Arg);
1056 23: Constraints += InputConstraint;
1057 : }
1058 :
1059 : // Append the "input" part of inout constraints last.
8: branch 1 taken
35: branch 2 taken
1060 43: for (unsigned i = 0, e = InOutArgs.size(); i != e; i++) {
1061 8: ArgTypes.push_back(InOutArgTypes[i]);
1062 8: Args.push_back(InOutArgs[i]);
1063 : }
1064 35: Constraints += InOutConstraints;
1065 :
1066 : // Clobbers
18: branch 1 taken
35: branch 2 taken
1067 53: for (unsigned i = 0, e = S.getNumClobbers(); i != e; i++) {
1068 18: llvm::StringRef Clobber = S.getClobber(i)->getString();
1069 :
1070 18: Clobber = Target.getNormalizedGCCRegisterName(Clobber);
1071 :
5: branch 0 taken
13: branch 1 taken
0: branch 2 not taken
5: branch 3 taken
1072 18: if (i != 0 || NumConstraints != 0)
1073 13: Constraints += ',';
1074 :
1075 18: Constraints += "~{";
1076 18: Constraints += Clobber;
1077 18: Constraints += '}';
1078 : }
1079 :
1080 : // Add machine specific clobbers
1081 35: std::string MachineClobbers = Target.getClobbers();
29: branch 1 taken
6: branch 2 taken
1082 35: if (!MachineClobbers.empty()) {
20: branch 1 taken
9: branch 2 taken
1083 29: if (!Constraints.empty())
1084 20: Constraints += ',';
1085 29: Constraints += MachineClobbers;
1086 : }
1087 :
1088 : const llvm::Type *ResultType;
20: branch 1 taken
15: branch 2 taken
1089 35: if (ResultRegTypes.empty())
1090 20: ResultType = llvm::Type::getVoidTy(VMContext);
12: branch 1 taken
3: branch 2 taken
1091 15: else if (ResultRegTypes.size() == 1)
1092 12: ResultType = ResultRegTypes[0];
1093 : else
1094 3: ResultType = llvm::StructType::get(VMContext, ResultRegTypes);
1095 :
1096 : const llvm::FunctionType *FTy =
1097 35: llvm::FunctionType::get(ResultType, ArgTypes, false);
1098 :
1099 : llvm::InlineAsm *IA =
1100 : llvm::InlineAsm::get(FTy, AsmString, Constraints,
21: branch 1 taken
14: branch 2 taken
9: branch 4 taken
12: branch 5 taken
1101 35: S.isVolatile() || S.getNumOutputs() == 0);
1102 35: llvm::CallInst *Result = Builder.CreateCall(IA, Args.begin(), Args.end());
1103 35: Result->addAttribute(~0, llvm::Attribute::NoUnwind);
1104 :
1105 :
1106 : // Extract all of the register value results from the asm.
1107 35: std::vector<llvm::Value*> RegResults;
12: branch 1 taken
23: branch 2 taken
1108 35: if (ResultRegTypes.size() == 1) {
1109 12: RegResults.push_back(Result);
1110 : } else {
6: branch 1 taken
23: branch 2 taken
1111 29: for (unsigned i = 0, e = ResultRegTypes.size(); i != e; ++i) {
1112 6: llvm::Value *Tmp = Builder.CreateExtractValue(Result, i, "asmresult");
1113 6: RegResults.push_back(Tmp);
1114 : }
1115 : }
1116 :
18: branch 1 taken
35: branch 2 taken
1117 53: for (unsigned i = 0, e = RegResults.size(); i != e; ++i) {
1118 18: llvm::Value *Tmp = RegResults[i];
1119 :
1120 : // If the result type of the LLVM IR asm doesn't match the result type of
1121 : // the expression, do the conversion.
2: branch 2 taken
16: branch 3 taken
1122 18: if (ResultRegTypes[i] != ResultTruncRegTypes[i]) {
1123 2: const llvm::Type *TruncTy = ResultTruncRegTypes[i];
1124 : // Truncate the integer result to the right size, note that
1125 : // ResultTruncRegTypes can be a pointer.
1126 2: uint64_t ResSize = CGM.getTargetData().getTypeSizeInBits(TruncTy);
1127 2: Tmp = Builder.CreateTrunc(Tmp, llvm::IntegerType::get(VMContext, (unsigned)ResSize));
1128 :
0: branch 1 not taken
2: branch 2 taken
1129 2: if (Tmp->getType() != TruncTy) {
0: branch 1 not taken
0: branch 2 not taken
1130 0: assert(isa<llvm::PointerType>(TruncTy));
1131 0: Tmp = Builder.CreateIntToPtr(Tmp, TruncTy);
1132 : }
1133 : }
1134 :
1135 : EmitStoreThroughLValue(RValue::get(Tmp), ResultRegDests[i],
1136 18: ResultRegQualTys[i]);
1137 35: }
1138 35: }
Generated: 2010-02-10 01:31 by zcov