1 //===--- CGStmt.cpp - Emit LLVM Code from Statements ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This contains code to emit Stmt nodes as LLVM code.
12 //===----------------------------------------------------------------------===//
14 #include "CGDebugInfo.h"
15 #include "CodeGenModule.h"
16 #include "CodeGenFunction.h"
17 #include "TargetInfo.h"
18 #include "clang/AST/StmtVisitor.h"
19 #include "clang/Basic/PrettyStackTrace.h"
20 #include "clang/Basic/TargetInfo.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "llvm/InlineAsm.h"
23 #include "llvm/Intrinsics.h"
24 #include "llvm/Target/TargetData.h"
25 using namespace clang
;
26 using namespace CodeGen
;
28 //===----------------------------------------------------------------------===//
30 //===----------------------------------------------------------------------===//
32 void CodeGenFunction::EmitStopPoint(const Stmt
*S
) {
33 if (CGDebugInfo
*DI
= getDebugInfo()) {
35 DI
->setLocation(S
->getLocEnd());
37 DI
->setLocation(S
->getLocStart());
38 DI
->UpdateLineDirectiveRegion(Builder
);
39 DI
->EmitStopPoint(Builder
);
43 void CodeGenFunction::EmitStmt(const Stmt
*S
) {
44 assert(S
&& "Null statement?");
46 // Check if we can handle this without bothering to generate an
47 // insert point or debug info.
48 if (EmitSimpleStmt(S
))
51 // Check if we are generating unreachable code.
52 if (!HaveInsertPoint()) {
53 // If so, and the statement doesn't contain a label, then we do not need to
54 // generate actual code. This is safe because (1) the current point is
55 // unreachable, so we don't need to execute the code, and (2) we've already
56 // handled the statements which update internal data structures (like the
57 // local variable map) which could be used by subsequent statements.
58 if (!ContainsLabel(S
)) {
59 // Verify that any decl statements were handled as simple, they may be in
60 // scope of subsequent reachable statements.
61 assert(!isa
<DeclStmt
>(*S
) && "Unexpected DeclStmt!");
65 // Otherwise, make a new block to hold the code.
69 // Generate a stoppoint if we are emitting debug info.
72 switch (S
->getStmtClass()) {
73 case Stmt::NoStmtClass
:
74 case Stmt::CXXCatchStmtClass
:
75 llvm_unreachable("invalid statement class to emit generically");
76 case Stmt::NullStmtClass
:
77 case Stmt::CompoundStmtClass
:
78 case Stmt::DeclStmtClass
:
79 case Stmt::LabelStmtClass
:
80 case Stmt::GotoStmtClass
:
81 case Stmt::BreakStmtClass
:
82 case Stmt::ContinueStmtClass
:
83 case Stmt::DefaultStmtClass
:
84 case Stmt::CaseStmtClass
:
85 llvm_unreachable("should have emitted these statements as simple");
87 #define STMT(Type, Base)
88 #define ABSTRACT_STMT(Op)
89 #define EXPR(Type, Base) \
90 case Stmt::Type##Class:
91 #include "clang/AST/StmtNodes.inc"
93 // Remember the block we came in on.
94 llvm::BasicBlock
*incoming
= Builder
.GetInsertBlock();
95 assert(incoming
&& "expression emission must have an insertion point");
97 EmitIgnoredExpr(cast
<Expr
>(S
));
99 llvm::BasicBlock
*outgoing
= Builder
.GetInsertBlock();
100 assert(outgoing
&& "expression emission cleared block!");
102 // The expression emitters assume (reasonably!) that the insertion
103 // point is always set. To maintain that, the call-emission code
104 // for noreturn functions has to enter a new block with no
105 // predecessors. We want to kill that block and mark the current
106 // insertion point unreachable in the common case of a call like
107 // "exit();". Since expression emission doesn't otherwise create
108 // blocks with no predecessors, we can just test for that.
109 // However, we must be careful not to do this to our incoming
110 // block, because *statement* emission does sometimes create
111 // reachable blocks which will have no predecessors until later in
112 // the function. This occurs with, e.g., labels that are not
113 // reachable by fallthrough.
114 if (incoming
!= outgoing
&& outgoing
->use_empty()) {
115 outgoing
->eraseFromParent();
116 Builder
.ClearInsertionPoint();
121 case Stmt::IndirectGotoStmtClass
:
122 EmitIndirectGotoStmt(cast
<IndirectGotoStmt
>(*S
)); break;
124 case Stmt::IfStmtClass
: EmitIfStmt(cast
<IfStmt
>(*S
)); break;
125 case Stmt::WhileStmtClass
: EmitWhileStmt(cast
<WhileStmt
>(*S
)); break;
126 case Stmt::DoStmtClass
: EmitDoStmt(cast
<DoStmt
>(*S
)); break;
127 case Stmt::ForStmtClass
: EmitForStmt(cast
<ForStmt
>(*S
)); break;
129 case Stmt::ReturnStmtClass
: EmitReturnStmt(cast
<ReturnStmt
>(*S
)); break;
131 case Stmt::SwitchStmtClass
: EmitSwitchStmt(cast
<SwitchStmt
>(*S
)); break;
132 case Stmt::AsmStmtClass
: EmitAsmStmt(cast
<AsmStmt
>(*S
)); break;
134 case Stmt::ObjCAtTryStmtClass
:
135 EmitObjCAtTryStmt(cast
<ObjCAtTryStmt
>(*S
));
137 case Stmt::ObjCAtCatchStmtClass
:
138 assert(0 && "@catch statements should be handled by EmitObjCAtTryStmt");
140 case Stmt::ObjCAtFinallyStmtClass
:
141 assert(0 && "@finally statements should be handled by EmitObjCAtTryStmt");
143 case Stmt::ObjCAtThrowStmtClass
:
144 EmitObjCAtThrowStmt(cast
<ObjCAtThrowStmt
>(*S
));
146 case Stmt::ObjCAtSynchronizedStmtClass
:
147 EmitObjCAtSynchronizedStmt(cast
<ObjCAtSynchronizedStmt
>(*S
));
149 case Stmt::ObjCForCollectionStmtClass
:
150 EmitObjCForCollectionStmt(cast
<ObjCForCollectionStmt
>(*S
));
153 case Stmt::CXXTryStmtClass
:
154 EmitCXXTryStmt(cast
<CXXTryStmt
>(*S
));
159 bool CodeGenFunction::EmitSimpleStmt(const Stmt
*S
) {
160 switch (S
->getStmtClass()) {
161 default: return false;
162 case Stmt::NullStmtClass
: break;
163 case Stmt::CompoundStmtClass
: EmitCompoundStmt(cast
<CompoundStmt
>(*S
)); break;
164 case Stmt::DeclStmtClass
: EmitDeclStmt(cast
<DeclStmt
>(*S
)); break;
165 case Stmt::LabelStmtClass
: EmitLabelStmt(cast
<LabelStmt
>(*S
)); break;
166 case Stmt::GotoStmtClass
: EmitGotoStmt(cast
<GotoStmt
>(*S
)); break;
167 case Stmt::BreakStmtClass
: EmitBreakStmt(cast
<BreakStmt
>(*S
)); break;
168 case Stmt::ContinueStmtClass
: EmitContinueStmt(cast
<ContinueStmt
>(*S
)); break;
169 case Stmt::DefaultStmtClass
: EmitDefaultStmt(cast
<DefaultStmt
>(*S
)); break;
170 case Stmt::CaseStmtClass
: EmitCaseStmt(cast
<CaseStmt
>(*S
)); break;
176 /// EmitCompoundStmt - Emit a compound statement {..} node. If GetLast is true,
177 /// this captures the expression result of the last sub-statement and returns it
178 /// (for use by the statement expression extension).
179 RValue
CodeGenFunction::EmitCompoundStmt(const CompoundStmt
&S
, bool GetLast
,
180 AggValueSlot AggSlot
) {
181 PrettyStackTraceLoc
CrashInfo(getContext().getSourceManager(),S
.getLBracLoc(),
182 "LLVM IR generation of compound statement ('{}')");
184 CGDebugInfo
*DI
= getDebugInfo();
186 DI
->setLocation(S
.getLBracLoc());
187 DI
->EmitRegionStart(Builder
);
190 // Keep track of the current cleanup stack depth.
191 RunCleanupsScope
Scope(*this);
193 for (CompoundStmt::const_body_iterator I
= S
.body_begin(),
194 E
= S
.body_end()-GetLast
; I
!= E
; ++I
)
198 DI
->setLocation(S
.getRBracLoc());
199 DI
->EmitRegionEnd(Builder
);
206 // We have to special case labels here. They are statements, but when put
207 // at the end of a statement expression, they yield the value of their
208 // subexpression. Handle this by walking through all labels we encounter,
209 // emitting them before we evaluate the subexpr.
210 const Stmt
*LastStmt
= S
.body_back();
211 while (const LabelStmt
*LS
= dyn_cast
<LabelStmt
>(LastStmt
)) {
212 EmitLabel(LS
->getDecl());
213 LastStmt
= LS
->getSubStmt();
218 RV
= EmitAnyExpr(cast
<Expr
>(LastStmt
), AggSlot
);
224 void CodeGenFunction::SimplifyForwardingBlocks(llvm::BasicBlock
*BB
) {
225 llvm::BranchInst
*BI
= dyn_cast
<llvm::BranchInst
>(BB
->getTerminator());
227 // If there is a cleanup stack, then we it isn't worth trying to
228 // simplify this block (we would need to remove it from the scope map
229 // and cleanup entry).
230 if (!EHStack
.empty())
233 // Can only simplify direct branches.
234 if (!BI
|| !BI
->isUnconditional())
237 BB
->replaceAllUsesWith(BI
->getSuccessor(0));
238 BI
->eraseFromParent();
239 BB
->eraseFromParent();
242 void CodeGenFunction::EmitBlock(llvm::BasicBlock
*BB
, bool IsFinished
) {
243 llvm::BasicBlock
*CurBB
= Builder
.GetInsertBlock();
245 // Fall out of the current block (if necessary).
248 if (IsFinished
&& BB
->use_empty()) {
253 // Place the block after the current block, if possible, or else at
254 // the end of the function.
255 if (CurBB
&& CurBB
->getParent())
256 CurFn
->getBasicBlockList().insertAfter(CurBB
, BB
);
258 CurFn
->getBasicBlockList().push_back(BB
);
259 Builder
.SetInsertPoint(BB
);
262 void CodeGenFunction::EmitBranch(llvm::BasicBlock
*Target
) {
263 // Emit a branch from the current block to the target one if this
264 // was a real block. If this was just a fall-through block after a
265 // terminator, don't emit it.
266 llvm::BasicBlock
*CurBB
= Builder
.GetInsertBlock();
268 if (!CurBB
|| CurBB
->getTerminator()) {
269 // If there is no insert point or the previous block is already
270 // terminated, don't touch it.
272 // Otherwise, create a fall-through branch.
273 Builder
.CreateBr(Target
);
276 Builder
.ClearInsertionPoint();
279 CodeGenFunction::JumpDest
280 CodeGenFunction::getJumpDestForLabel(const LabelDecl
*D
) {
281 JumpDest
&Dest
= LabelMap
[D
];
282 if (Dest
.isValid()) return Dest
;
284 // Create, but don't insert, the new block.
285 Dest
= JumpDest(createBasicBlock(D
->getName()),
286 EHScopeStack::stable_iterator::invalid(),
287 NextCleanupDestIndex
++);
291 void CodeGenFunction::EmitLabel(const LabelDecl
*D
) {
292 JumpDest
&Dest
= LabelMap
[D
];
294 // If we didn't need a forward reference to this label, just go
295 // ahead and create a destination at the current scope.
296 if (!Dest
.isValid()) {
297 Dest
= getJumpDestInCurrentScope(D
->getName());
299 // Otherwise, we need to give this label a target depth and remove
300 // it from the branch-fixups list.
302 assert(!Dest
.getScopeDepth().isValid() && "already emitted label!");
303 Dest
= JumpDest(Dest
.getBlock(),
304 EHStack
.stable_begin(),
305 Dest
.getDestIndex());
307 ResolveBranchFixups(Dest
.getBlock());
310 EmitBlock(Dest
.getBlock());
314 void CodeGenFunction::EmitLabelStmt(const LabelStmt
&S
) {
315 EmitLabel(S
.getDecl());
316 EmitStmt(S
.getSubStmt());
319 void CodeGenFunction::EmitGotoStmt(const GotoStmt
&S
) {
320 // If this code is reachable then emit a stop point (if generating
321 // debug info). We have to do this ourselves because we are on the
322 // "simple" statement path.
323 if (HaveInsertPoint())
326 EmitBranchThroughCleanup(getJumpDestForLabel(S
.getLabel()));
330 void CodeGenFunction::EmitIndirectGotoStmt(const IndirectGotoStmt
&S
) {
331 if (const LabelDecl
*Target
= S
.getConstantTarget()) {
332 EmitBranchThroughCleanup(getJumpDestForLabel(Target
));
336 // Ensure that we have an i8* for our PHI node.
337 llvm::Value
*V
= Builder
.CreateBitCast(EmitScalarExpr(S
.getTarget()),
339 llvm::BasicBlock
*CurBB
= Builder
.GetInsertBlock();
342 // Get the basic block for the indirect goto.
343 llvm::BasicBlock
*IndGotoBB
= GetIndirectGotoBlock();
345 // The first instruction in the block has to be the PHI for the switch dest,
346 // add an entry for this branch.
347 cast
<llvm::PHINode
>(IndGotoBB
->begin())->addIncoming(V
, CurBB
);
349 EmitBranch(IndGotoBB
);
352 void CodeGenFunction::EmitIfStmt(const IfStmt
&S
) {
353 // C99 6.8.4.1: The first substatement is executed if the expression compares
354 // unequal to 0. The condition must be a scalar type.
355 RunCleanupsScope
ConditionScope(*this);
357 if (S
.getConditionVariable())
358 EmitAutoVarDecl(*S
.getConditionVariable());
360 // If the condition constant folds and can be elided, try to avoid emitting
361 // the condition and the dead arm of the if/else.
362 if (int Cond
= ConstantFoldsToSimpleInteger(S
.getCond())) {
363 // Figure out which block (then or else) is executed.
364 const Stmt
*Executed
= S
.getThen(), *Skipped
= S
.getElse();
365 if (Cond
== -1) // Condition false?
366 std::swap(Executed
, Skipped
);
368 // If the skipped block has no labels in it, just emit the executed block.
369 // This avoids emitting dead code and simplifies the CFG substantially.
370 if (!ContainsLabel(Skipped
)) {
372 RunCleanupsScope
ExecutedScope(*this);
379 // Otherwise, the condition did not fold, or we couldn't elide it. Just emit
380 // the conditional branch.
381 llvm::BasicBlock
*ThenBlock
= createBasicBlock("if.then");
382 llvm::BasicBlock
*ContBlock
= createBasicBlock("if.end");
383 llvm::BasicBlock
*ElseBlock
= ContBlock
;
385 ElseBlock
= createBasicBlock("if.else");
386 EmitBranchOnBoolExpr(S
.getCond(), ThenBlock
, ElseBlock
);
388 // Emit the 'then' code.
389 EmitBlock(ThenBlock
);
391 RunCleanupsScope
ThenScope(*this);
392 EmitStmt(S
.getThen());
394 EmitBranch(ContBlock
);
396 // Emit the 'else' code if present.
397 if (const Stmt
*Else
= S
.getElse()) {
398 EmitBlock(ElseBlock
);
400 RunCleanupsScope
ElseScope(*this);
403 EmitBranch(ContBlock
);
406 // Emit the continuation block for code after the if.
407 EmitBlock(ContBlock
, true);
410 void CodeGenFunction::EmitWhileStmt(const WhileStmt
&S
) {
411 // Emit the header for the loop, which will also become
412 // the continue target.
413 JumpDest LoopHeader
= getJumpDestInCurrentScope("while.cond");
414 EmitBlock(LoopHeader
.getBlock());
416 // Create an exit block for when the condition fails, which will
417 // also become the break target.
418 JumpDest LoopExit
= getJumpDestInCurrentScope("while.end");
420 // Store the blocks to use for break and continue.
421 BreakContinueStack
.push_back(BreakContinue(LoopExit
, LoopHeader
));
423 // C++ [stmt.while]p2:
424 // When the condition of a while statement is a declaration, the
425 // scope of the variable that is declared extends from its point
426 // of declaration (3.3.2) to the end of the while statement.
428 // The object created in a condition is destroyed and created
429 // with each iteration of the loop.
430 RunCleanupsScope
ConditionScope(*this);
432 if (S
.getConditionVariable())
433 EmitAutoVarDecl(*S
.getConditionVariable());
435 // Evaluate the conditional in the while header. C99 6.8.5.1: The
436 // evaluation of the controlling expression takes place before each
437 // execution of the loop body.
438 llvm::Value
*BoolCondVal
= EvaluateExprAsBool(S
.getCond());
440 // while(1) is common, avoid extra exit blocks. Be sure
441 // to correctly handle break/continue though.
442 bool EmitBoolCondBranch
= true;
443 if (llvm::ConstantInt
*C
= dyn_cast
<llvm::ConstantInt
>(BoolCondVal
))
445 EmitBoolCondBranch
= false;
447 // As long as the condition is true, go to the loop body.
448 llvm::BasicBlock
*LoopBody
= createBasicBlock("while.body");
449 if (EmitBoolCondBranch
) {
450 llvm::BasicBlock
*ExitBlock
= LoopExit
.getBlock();
451 if (ConditionScope
.requiresCleanups())
452 ExitBlock
= createBasicBlock("while.exit");
454 Builder
.CreateCondBr(BoolCondVal
, LoopBody
, ExitBlock
);
456 if (ExitBlock
!= LoopExit
.getBlock()) {
457 EmitBlock(ExitBlock
);
458 EmitBranchThroughCleanup(LoopExit
);
462 // Emit the loop body. We have to emit this in a cleanup scope
463 // because it might be a singleton DeclStmt.
465 RunCleanupsScope
BodyScope(*this);
467 EmitStmt(S
.getBody());
470 BreakContinueStack
.pop_back();
472 // Immediately force cleanup.
473 ConditionScope
.ForceCleanup();
475 // Branch to the loop header again.
476 EmitBranch(LoopHeader
.getBlock());
478 // Emit the exit block.
479 EmitBlock(LoopExit
.getBlock(), true);
481 // The LoopHeader typically is just a branch if we skipped emitting
482 // a branch, try to erase it.
483 if (!EmitBoolCondBranch
)
484 SimplifyForwardingBlocks(LoopHeader
.getBlock());
487 void CodeGenFunction::EmitDoStmt(const DoStmt
&S
) {
488 JumpDest LoopExit
= getJumpDestInCurrentScope("do.end");
489 JumpDest LoopCond
= getJumpDestInCurrentScope("do.cond");
491 // Store the blocks to use for break and continue.
492 BreakContinueStack
.push_back(BreakContinue(LoopExit
, LoopCond
));
494 // Emit the body of the loop.
495 llvm::BasicBlock
*LoopBody
= createBasicBlock("do.body");
498 RunCleanupsScope
BodyScope(*this);
499 EmitStmt(S
.getBody());
502 BreakContinueStack
.pop_back();
504 EmitBlock(LoopCond
.getBlock());
506 // C99 6.8.5.2: "The evaluation of the controlling expression takes place
507 // after each execution of the loop body."
509 // Evaluate the conditional in the while header.
510 // C99 6.8.5p2/p4: The first substatement is executed if the expression
511 // compares unequal to 0. The condition must be a scalar type.
512 llvm::Value
*BoolCondVal
= EvaluateExprAsBool(S
.getCond());
514 // "do {} while (0)" is common in macros, avoid extra blocks. Be sure
515 // to correctly handle break/continue though.
516 bool EmitBoolCondBranch
= true;
517 if (llvm::ConstantInt
*C
= dyn_cast
<llvm::ConstantInt
>(BoolCondVal
))
519 EmitBoolCondBranch
= false;
521 // As long as the condition is true, iterate the loop.
522 if (EmitBoolCondBranch
)
523 Builder
.CreateCondBr(BoolCondVal
, LoopBody
, LoopExit
.getBlock());
525 // Emit the exit block.
526 EmitBlock(LoopExit
.getBlock());
528 // The DoCond block typically is just a branch if we skipped
529 // emitting a branch, try to erase it.
530 if (!EmitBoolCondBranch
)
531 SimplifyForwardingBlocks(LoopCond
.getBlock());
534 void CodeGenFunction::EmitForStmt(const ForStmt
&S
) {
535 JumpDest LoopExit
= getJumpDestInCurrentScope("for.end");
537 RunCleanupsScope
ForScope(*this);
539 CGDebugInfo
*DI
= getDebugInfo();
541 DI
->setLocation(S
.getSourceRange().getBegin());
542 DI
->EmitRegionStart(Builder
);
545 // Evaluate the first part before the loop.
547 EmitStmt(S
.getInit());
549 // Start the loop with a block that tests the condition.
550 // If there's an increment, the continue scope will be overwritten
552 JumpDest Continue
= getJumpDestInCurrentScope("for.cond");
553 llvm::BasicBlock
*CondBlock
= Continue
.getBlock();
554 EmitBlock(CondBlock
);
556 // Create a cleanup scope for the condition variable cleanups.
557 RunCleanupsScope
ConditionScope(*this);
559 llvm::Value
*BoolCondVal
= 0;
561 // If the for statement has a condition scope, emit the local variable
563 llvm::BasicBlock
*ExitBlock
= LoopExit
.getBlock();
564 if (S
.getConditionVariable()) {
565 EmitAutoVarDecl(*S
.getConditionVariable());
568 // If there are any cleanups between here and the loop-exit scope,
569 // create a block to stage a loop exit along.
570 if (ForScope
.requiresCleanups())
571 ExitBlock
= createBasicBlock("for.cond.cleanup");
573 // As long as the condition is true, iterate the loop.
574 llvm::BasicBlock
*ForBody
= createBasicBlock("for.body");
576 // C99 6.8.5p2/p4: The first substatement is executed if the expression
577 // compares unequal to 0. The condition must be a scalar type.
578 BoolCondVal
= EvaluateExprAsBool(S
.getCond());
579 Builder
.CreateCondBr(BoolCondVal
, ForBody
, ExitBlock
);
581 if (ExitBlock
!= LoopExit
.getBlock()) {
582 EmitBlock(ExitBlock
);
583 EmitBranchThroughCleanup(LoopExit
);
588 // Treat it as a non-zero constant. Don't even create a new block for the
589 // body, just fall into it.
592 // If the for loop doesn't have an increment we can just use the
593 // condition as the continue block. Otherwise we'll need to create
594 // a block for it (in the current scope, i.e. in the scope of the
595 // condition), and that we will become our continue block.
597 Continue
= getJumpDestInCurrentScope("for.inc");
599 // Store the blocks to use for break and continue.
600 BreakContinueStack
.push_back(BreakContinue(LoopExit
, Continue
));
603 // Create a separate cleanup scope for the body, in case it is not
604 // a compound statement.
605 RunCleanupsScope
BodyScope(*this);
606 EmitStmt(S
.getBody());
609 // If there is an increment, emit it next.
611 EmitBlock(Continue
.getBlock());
612 EmitStmt(S
.getInc());
615 BreakContinueStack
.pop_back();
617 ConditionScope
.ForceCleanup();
618 EmitBranch(CondBlock
);
620 ForScope
.ForceCleanup();
623 DI
->setLocation(S
.getSourceRange().getEnd());
624 DI
->EmitRegionEnd(Builder
);
627 // Emit the fall-through block.
628 EmitBlock(LoopExit
.getBlock(), true);
631 void CodeGenFunction::EmitReturnOfRValue(RValue RV
, QualType Ty
) {
633 Builder
.CreateStore(RV
.getScalarVal(), ReturnValue
);
634 } else if (RV
.isAggregate()) {
635 EmitAggregateCopy(ReturnValue
, RV
.getAggregateAddr(), Ty
);
637 StoreComplexToAddr(RV
.getComplexVal(), ReturnValue
, false);
639 EmitBranchThroughCleanup(ReturnBlock
);
642 /// EmitReturnStmt - Note that due to GCC extensions, this can have an operand
643 /// if the function returns void, or may be missing one if the function returns
644 /// non-void. Fun stuff :).
645 void CodeGenFunction::EmitReturnStmt(const ReturnStmt
&S
) {
646 // Emit the result value, even if unused, to evalute the side effects.
647 const Expr
*RV
= S
.getRetValue();
649 // FIXME: Clean this up by using an LValue for ReturnTemp,
650 // EmitStoreThroughLValue, and EmitAnyExpr.
651 if (S
.getNRVOCandidate() && S
.getNRVOCandidate()->isNRVOVariable() &&
652 !Target
.useGlobalsForAutomaticVariables()) {
653 // Apply the named return value optimization for this return statement,
654 // which means doing nothing: the appropriate result has already been
655 // constructed into the NRVO variable.
657 // If there is an NRVO flag for this variable, set it to 1 into indicate
658 // that the cleanup code should not destroy the variable.
659 if (llvm::Value
*NRVOFlag
= NRVOFlags
[S
.getNRVOCandidate()])
660 Builder
.CreateStore(Builder
.getTrue(), NRVOFlag
);
661 } else if (!ReturnValue
) {
662 // Make sure not to return anything, but evaluate the expression
666 } else if (RV
== 0) {
667 // Do nothing (return value is left uninitialized)
668 } else if (FnRetTy
->isReferenceType()) {
669 // If this function returns a reference, take the address of the expression
670 // rather than the value.
671 RValue Result
= EmitReferenceBindingToExpr(RV
, /*InitializedDecl=*/0);
672 Builder
.CreateStore(Result
.getScalarVal(), ReturnValue
);
673 } else if (!hasAggregateLLVMType(RV
->getType())) {
674 Builder
.CreateStore(EmitScalarExpr(RV
), ReturnValue
);
675 } else if (RV
->getType()->isAnyComplexType()) {
676 EmitComplexExprIntoAddr(RV
, ReturnValue
, false);
678 EmitAggExpr(RV
, AggValueSlot::forAddr(ReturnValue
, false, true));
681 EmitBranchThroughCleanup(ReturnBlock
);
684 void CodeGenFunction::EmitDeclStmt(const DeclStmt
&S
) {
685 // As long as debug info is modeled with instructions, we have to ensure we
686 // have a place to insert here and write the stop point here.
687 if (getDebugInfo()) {
692 for (DeclStmt::const_decl_iterator I
= S
.decl_begin(), E
= S
.decl_end();
697 void CodeGenFunction::EmitBreakStmt(const BreakStmt
&S
) {
698 assert(!BreakContinueStack
.empty() && "break stmt not in a loop or switch!");
700 // If this code is reachable then emit a stop point (if generating
701 // debug info). We have to do this ourselves because we are on the
702 // "simple" statement path.
703 if (HaveInsertPoint())
706 JumpDest Block
= BreakContinueStack
.back().BreakBlock
;
707 EmitBranchThroughCleanup(Block
);
710 void CodeGenFunction::EmitContinueStmt(const ContinueStmt
&S
) {
711 assert(!BreakContinueStack
.empty() && "continue stmt not in a loop!");
713 // If this code is reachable then emit a stop point (if generating
714 // debug info). We have to do this ourselves because we are on the
715 // "simple" statement path.
716 if (HaveInsertPoint())
719 JumpDest Block
= BreakContinueStack
.back().ContinueBlock
;
720 EmitBranchThroughCleanup(Block
);
723 /// EmitCaseStmtRange - If case statement range is not too big then
724 /// add multiple cases to switch instruction, one for each value within
725 /// the range. If range is too big then emit "if" condition check.
726 void CodeGenFunction::EmitCaseStmtRange(const CaseStmt
&S
) {
727 assert(S
.getRHS() && "Expected RHS value in CaseStmt");
729 llvm::APSInt LHS
= S
.getLHS()->EvaluateAsInt(getContext());
730 llvm::APSInt RHS
= S
.getRHS()->EvaluateAsInt(getContext());
732 // Emit the code for this case. We do this first to make sure it is
733 // properly chained from our predecessor before generating the
734 // switch machinery to enter this block.
735 EmitBlock(createBasicBlock("sw.bb"));
736 llvm::BasicBlock
*CaseDest
= Builder
.GetInsertBlock();
737 EmitStmt(S
.getSubStmt());
739 // If range is empty, do nothing.
740 if (LHS
.isSigned() ? RHS
.slt(LHS
) : RHS
.ult(LHS
))
743 llvm::APInt Range
= RHS
- LHS
;
744 // FIXME: parameters such as this should not be hardcoded.
745 if (Range
.ult(llvm::APInt(Range
.getBitWidth(), 64))) {
746 // Range is small enough to add multiple switch instruction cases.
747 for (unsigned i
= 0, e
= Range
.getZExtValue() + 1; i
!= e
; ++i
) {
748 SwitchInsn
->addCase(llvm::ConstantInt::get(getLLVMContext(), LHS
),
755 // The range is too big. Emit "if" condition into a new block,
756 // making sure to save and restore the current insertion point.
757 llvm::BasicBlock
*RestoreBB
= Builder
.GetInsertBlock();
759 // Push this test onto the chain of range checks (which terminates
760 // in the default basic block). The switch's default will be changed
761 // to the top of this chain after switch emission is complete.
762 llvm::BasicBlock
*FalseDest
= CaseRangeBlock
;
763 CaseRangeBlock
= createBasicBlock("sw.caserange");
765 CurFn
->getBasicBlockList().push_back(CaseRangeBlock
);
766 Builder
.SetInsertPoint(CaseRangeBlock
);
770 Builder
.CreateSub(SwitchInsn
->getCondition(),
771 llvm::ConstantInt::get(getLLVMContext(), LHS
), "tmp");
773 Builder
.CreateICmpULE(Diff
, llvm::ConstantInt::get(getLLVMContext(), Range
),
775 Builder
.CreateCondBr(Cond
, CaseDest
, FalseDest
);
777 // Restore the appropriate insertion point.
779 Builder
.SetInsertPoint(RestoreBB
);
781 Builder
.ClearInsertionPoint();
784 void CodeGenFunction::EmitCaseStmt(const CaseStmt
&S
) {
786 EmitCaseStmtRange(S
);
790 EmitBlock(createBasicBlock("sw.bb"));
791 llvm::BasicBlock
*CaseDest
= Builder
.GetInsertBlock();
792 llvm::APSInt CaseVal
= S
.getLHS()->EvaluateAsInt(getContext());
793 SwitchInsn
->addCase(llvm::ConstantInt::get(getLLVMContext(), CaseVal
),
796 // Recursively emitting the statement is acceptable, but is not wonderful for
797 // code where we have many case statements nested together, i.e.:
801 // Handling this recursively will create a new block for each case statement
802 // that falls through to the next case which is IR intensive. It also causes
803 // deep recursion which can run into stack depth limitations. Handle
804 // sequential non-range case statements specially.
805 const CaseStmt
*CurCase
= &S
;
806 const CaseStmt
*NextCase
= dyn_cast
<CaseStmt
>(S
.getSubStmt());
808 // Otherwise, iteratively add consequtive cases to this switch stmt.
809 while (NextCase
&& NextCase
->getRHS() == 0) {
811 CaseVal
= CurCase
->getLHS()->EvaluateAsInt(getContext());
812 SwitchInsn
->addCase(llvm::ConstantInt::get(getLLVMContext(), CaseVal
),
815 NextCase
= dyn_cast
<CaseStmt
>(CurCase
->getSubStmt());
818 // Normal default recursion for non-cases.
819 EmitStmt(CurCase
->getSubStmt());
822 void CodeGenFunction::EmitDefaultStmt(const DefaultStmt
&S
) {
823 llvm::BasicBlock
*DefaultBlock
= SwitchInsn
->getDefaultDest();
824 assert(DefaultBlock
->empty() &&
825 "EmitDefaultStmt: Default block already defined?");
826 EmitBlock(DefaultBlock
);
827 EmitStmt(S
.getSubStmt());
830 void CodeGenFunction::EmitSwitchStmt(const SwitchStmt
&S
) {
831 JumpDest SwitchExit
= getJumpDestInCurrentScope("sw.epilog");
833 RunCleanupsScope
ConditionScope(*this);
835 if (S
.getConditionVariable())
836 EmitAutoVarDecl(*S
.getConditionVariable());
838 llvm::Value
*CondV
= EmitScalarExpr(S
.getCond());
840 // Handle nested switch statements.
841 llvm::SwitchInst
*SavedSwitchInsn
= SwitchInsn
;
842 llvm::BasicBlock
*SavedCRBlock
= CaseRangeBlock
;
844 // Create basic block to hold stuff that comes after switch
845 // statement. We also need to create a default block now so that
846 // explicit case ranges tests can have a place to jump to on
848 llvm::BasicBlock
*DefaultBlock
= createBasicBlock("sw.default");
849 SwitchInsn
= Builder
.CreateSwitch(CondV
, DefaultBlock
);
850 CaseRangeBlock
= DefaultBlock
;
852 // Clear the insertion point to indicate we are in unreachable code.
853 Builder
.ClearInsertionPoint();
855 // All break statements jump to NextBlock. If BreakContinueStack is non empty
856 // then reuse last ContinueBlock.
857 JumpDest OuterContinue
;
858 if (!BreakContinueStack
.empty())
859 OuterContinue
= BreakContinueStack
.back().ContinueBlock
;
861 BreakContinueStack
.push_back(BreakContinue(SwitchExit
, OuterContinue
));
864 EmitStmt(S
.getBody());
866 BreakContinueStack
.pop_back();
868 // Update the default block in case explicit case range tests have
869 // been chained on top.
870 SwitchInsn
->setSuccessor(0, CaseRangeBlock
);
872 // If a default was never emitted:
873 if (!DefaultBlock
->getParent()) {
874 // If we have cleanups, emit the default block so that there's a
875 // place to jump through the cleanups from.
876 if (ConditionScope
.requiresCleanups()) {
877 EmitBlock(DefaultBlock
);
879 // Otherwise, just forward the default block to the switch end.
881 DefaultBlock
->replaceAllUsesWith(SwitchExit
.getBlock());
886 ConditionScope
.ForceCleanup();
888 // Emit continuation.
889 EmitBlock(SwitchExit
.getBlock(), true);
891 SwitchInsn
= SavedSwitchInsn
;
892 CaseRangeBlock
= SavedCRBlock
;
896 SimplifyConstraint(const char *Constraint
, const TargetInfo
&Target
,
897 llvm::SmallVectorImpl
<TargetInfo::ConstraintInfo
> *OutCons
=0) {
900 while (*Constraint
) {
901 switch (*Constraint
) {
903 Result
+= Target
.convertConstraint(*Constraint
);
909 case '=': // Will see this and the following in mult-alt constraints.
920 "Must pass output names to constraints with a symbolic name");
922 bool result
= Target
.resolveSymbolicName(Constraint
,
924 OutCons
->size(), Index
);
925 assert(result
&& "Could not resolve symbolic name"); (void)result
;
926 Result
+= llvm::utostr(Index
);
937 /// AddVariableConstraints - Look at AsmExpr and if it is a variable declared
938 /// as using a particular register add that as a constraint that will be used
939 /// in this asm stmt.
941 AddVariableConstraints(const std::string
&Constraint
, const Expr
&AsmExpr
,
942 const TargetInfo
&Target
, CodeGenModule
&CGM
,
943 const AsmStmt
&Stmt
) {
944 const DeclRefExpr
*AsmDeclRef
= dyn_cast
<DeclRefExpr
>(&AsmExpr
);
947 const ValueDecl
&Value
= *AsmDeclRef
->getDecl();
948 const VarDecl
*Variable
= dyn_cast
<VarDecl
>(&Value
);
951 AsmLabelAttr
*Attr
= Variable
->getAttr
<AsmLabelAttr
>();
954 llvm::StringRef Register
= Attr
->getLabel();
955 assert(Target
.isValidGCCRegisterName(Register
));
956 // FIXME: We should check which registers are compatible with "r" or "x".
957 if (Constraint
!= "r" && Constraint
!= "x") {
958 CGM
.ErrorUnsupported(&Stmt
, "__asm__");
961 return "{" + Register
.str() + "}";
965 CodeGenFunction::EmitAsmInputLValue(const AsmStmt
&S
,
966 const TargetInfo::ConstraintInfo
&Info
,
967 LValue InputValue
, QualType InputType
,
968 std::string
&ConstraintStr
) {
970 if (Info
.allowsRegister() || !Info
.allowsMemory()) {
971 if (!CodeGenFunction::hasAggregateLLVMType(InputType
)) {
972 Arg
= EmitLoadOfLValue(InputValue
, InputType
).getScalarVal();
974 const llvm::Type
*Ty
= ConvertType(InputType
);
975 uint64_t Size
= CGM
.getTargetData().getTypeSizeInBits(Ty
);
976 if (Size
<= 64 && llvm::isPowerOf2_64(Size
)) {
977 Ty
= llvm::IntegerType::get(getLLVMContext(), Size
);
978 Ty
= llvm::PointerType::getUnqual(Ty
);
980 Arg
= Builder
.CreateLoad(Builder
.CreateBitCast(InputValue
.getAddress(),
983 Arg
= InputValue
.getAddress();
984 ConstraintStr
+= '*';
988 Arg
= InputValue
.getAddress();
989 ConstraintStr
+= '*';
995 llvm::Value
* CodeGenFunction::EmitAsmInput(const AsmStmt
&S
,
996 const TargetInfo::ConstraintInfo
&Info
,
997 const Expr
*InputExpr
,
998 std::string
&ConstraintStr
) {
999 if (Info
.allowsRegister() || !Info
.allowsMemory())
1000 if (!CodeGenFunction::hasAggregateLLVMType(InputExpr
->getType()))
1001 return EmitScalarExpr(InputExpr
);
1003 InputExpr
= InputExpr
->IgnoreParenNoopCasts(getContext());
1004 LValue Dest
= EmitLValue(InputExpr
);
1005 return EmitAsmInputLValue(S
, Info
, Dest
, InputExpr
->getType(), ConstraintStr
);
1008 /// getAsmSrcLocInfo - Return the !srcloc metadata node to attach to an inline
1009 /// asm call instruction. The !srcloc MDNode contains a list of constant
1010 /// integers which are the source locations of the start of each line in the
1012 static llvm::MDNode
*getAsmSrcLocInfo(const StringLiteral
*Str
,
1013 CodeGenFunction
&CGF
) {
1014 llvm::SmallVector
<llvm::Value
*, 8> Locs
;
1015 // Add the location of the first line to the MDNode.
1016 Locs
.push_back(llvm::ConstantInt::get(CGF
.Int32Ty
,
1017 Str
->getLocStart().getRawEncoding()));
1018 llvm::StringRef StrVal
= Str
->getString();
1019 if (!StrVal
.empty()) {
1020 const SourceManager
&SM
= CGF
.CGM
.getContext().getSourceManager();
1021 const LangOptions
&LangOpts
= CGF
.CGM
.getLangOptions();
1023 // Add the location of the start of each subsequent line of the asm to the
1025 for (unsigned i
= 0, e
= StrVal
.size()-1; i
!= e
; ++i
) {
1026 if (StrVal
[i
] != '\n') continue;
1027 SourceLocation LineLoc
= Str
->getLocationOfByte(i
+1, SM
, LangOpts
,
1029 Locs
.push_back(llvm::ConstantInt::get(CGF
.Int32Ty
,
1030 LineLoc
.getRawEncoding()));
1034 return llvm::MDNode::get(CGF
.getLLVMContext(), Locs
.data(), Locs
.size());
1037 void CodeGenFunction::EmitAsmStmt(const AsmStmt
&S
) {
1038 // Analyze the asm string to decompose it into its pieces. We know that Sema
1039 // has already done this, so it is guaranteed to be successful.
1040 llvm::SmallVector
<AsmStmt::AsmStringPiece
, 4> Pieces
;
1042 S
.AnalyzeAsmString(Pieces
, getContext(), DiagOffs
);
1044 // Assemble the pieces into the final asm string.
1045 std::string AsmString
;
1046 for (unsigned i
= 0, e
= Pieces
.size(); i
!= e
; ++i
) {
1047 if (Pieces
[i
].isString())
1048 AsmString
+= Pieces
[i
].getString();
1049 else if (Pieces
[i
].getModifier() == '\0')
1050 AsmString
+= '$' + llvm::utostr(Pieces
[i
].getOperandNo());
1052 AsmString
+= "${" + llvm::utostr(Pieces
[i
].getOperandNo()) + ':' +
1053 Pieces
[i
].getModifier() + '}';
1056 // Get all the output and input constraints together.
1057 llvm::SmallVector
<TargetInfo::ConstraintInfo
, 4> OutputConstraintInfos
;
1058 llvm::SmallVector
<TargetInfo::ConstraintInfo
, 4> InputConstraintInfos
;
1060 for (unsigned i
= 0, e
= S
.getNumOutputs(); i
!= e
; i
++) {
1061 TargetInfo::ConstraintInfo
Info(S
.getOutputConstraint(i
),
1062 S
.getOutputName(i
));
1063 bool IsValid
= Target
.validateOutputConstraint(Info
); (void)IsValid
;
1064 assert(IsValid
&& "Failed to parse output constraint");
1065 OutputConstraintInfos
.push_back(Info
);
1068 for (unsigned i
= 0, e
= S
.getNumInputs(); i
!= e
; i
++) {
1069 TargetInfo::ConstraintInfo
Info(S
.getInputConstraint(i
),
1071 bool IsValid
= Target
.validateInputConstraint(OutputConstraintInfos
.data(),
1072 S
.getNumOutputs(), Info
);
1073 assert(IsValid
&& "Failed to parse input constraint"); (void)IsValid
;
1074 InputConstraintInfos
.push_back(Info
);
1077 std::string Constraints
;
1079 std::vector
<LValue
> ResultRegDests
;
1080 std::vector
<QualType
> ResultRegQualTys
;
1081 std::vector
<const llvm::Type
*> ResultRegTypes
;
1082 std::vector
<const llvm::Type
*> ResultTruncRegTypes
;
1083 std::vector
<const llvm::Type
*> ArgTypes
;
1084 std::vector
<llvm::Value
*> Args
;
1086 // Keep track of inout constraints.
1087 std::string InOutConstraints
;
1088 std::vector
<llvm::Value
*> InOutArgs
;
1089 std::vector
<const llvm::Type
*> InOutArgTypes
;
1091 for (unsigned i
= 0, e
= S
.getNumOutputs(); i
!= e
; i
++) {
1092 TargetInfo::ConstraintInfo
&Info
= OutputConstraintInfos
[i
];
1094 // Simplify the output constraint.
1095 std::string
OutputConstraint(S
.getOutputConstraint(i
));
1096 OutputConstraint
= SimplifyConstraint(OutputConstraint
.c_str() + 1, Target
);
1098 const Expr
*OutExpr
= S
.getOutputExpr(i
);
1099 OutExpr
= OutExpr
->IgnoreParenNoopCasts(getContext());
1101 OutputConstraint
= AddVariableConstraints(OutputConstraint
, *OutExpr
, Target
,
1104 LValue Dest
= EmitLValue(OutExpr
);
1105 if (!Constraints
.empty())
1108 // If this is a register output, then make the inline asm return it
1109 // by-value. If this is a memory result, return the value by-reference.
1110 if (!Info
.allowsMemory() && !hasAggregateLLVMType(OutExpr
->getType())) {
1111 Constraints
+= "=" + OutputConstraint
;
1112 ResultRegQualTys
.push_back(OutExpr
->getType());
1113 ResultRegDests
.push_back(Dest
);
1114 ResultRegTypes
.push_back(ConvertTypeForMem(OutExpr
->getType()));
1115 ResultTruncRegTypes
.push_back(ResultRegTypes
.back());
1117 // If this output is tied to an input, and if the input is larger, then
1118 // we need to set the actual result type of the inline asm node to be the
1119 // same as the input type.
1120 if (Info
.hasMatchingInput()) {
1122 for (InputNo
= 0; InputNo
!= S
.getNumInputs(); ++InputNo
) {
1123 TargetInfo::ConstraintInfo
&Input
= InputConstraintInfos
[InputNo
];
1124 if (Input
.hasTiedOperand() && Input
.getTiedOperand() == i
)
1127 assert(InputNo
!= S
.getNumInputs() && "Didn't find matching input!");
1129 QualType InputTy
= S
.getInputExpr(InputNo
)->getType();
1130 QualType OutputType
= OutExpr
->getType();
1132 uint64_t InputSize
= getContext().getTypeSize(InputTy
);
1133 if (getContext().getTypeSize(OutputType
) < InputSize
) {
1134 // Form the asm to return the value as a larger integer or fp type.
1135 ResultRegTypes
.back() = ConvertType(InputTy
);
1138 if (const llvm::Type
* AdjTy
=
1139 getTargetHooks().adjustInlineAsmType(*this, OutputConstraint
,
1140 ResultRegTypes
.back()))
1141 ResultRegTypes
.back() = AdjTy
;
1143 ArgTypes
.push_back(Dest
.getAddress()->getType());
1144 Args
.push_back(Dest
.getAddress());
1145 Constraints
+= "=*";
1146 Constraints
+= OutputConstraint
;
1149 if (Info
.isReadWrite()) {
1150 InOutConstraints
+= ',';
1152 const Expr
*InputExpr
= S
.getOutputExpr(i
);
1153 llvm::Value
*Arg
= EmitAsmInputLValue(S
, Info
, Dest
, InputExpr
->getType(),
1156 if (Info
.allowsRegister())
1157 InOutConstraints
+= llvm::utostr(i
);
1159 InOutConstraints
+= OutputConstraint
;
1161 InOutArgTypes
.push_back(Arg
->getType());
1162 InOutArgs
.push_back(Arg
);
1166 unsigned NumConstraints
= S
.getNumOutputs() + S
.getNumInputs();
1168 for (unsigned i
= 0, e
= S
.getNumInputs(); i
!= e
; i
++) {
1169 const Expr
*InputExpr
= S
.getInputExpr(i
);
1171 TargetInfo::ConstraintInfo
&Info
= InputConstraintInfos
[i
];
1173 if (!Constraints
.empty())
1176 // Simplify the input constraint.
1177 std::string
InputConstraint(S
.getInputConstraint(i
));
1178 InputConstraint
= SimplifyConstraint(InputConstraint
.c_str(), Target
,
1179 &OutputConstraintInfos
);
1182 AddVariableConstraints(InputConstraint
,
1183 *InputExpr
->IgnoreParenNoopCasts(getContext()),
1186 llvm::Value
*Arg
= EmitAsmInput(S
, Info
, InputExpr
, Constraints
);
1188 // If this input argument is tied to a larger output result, extend the
1189 // input to be the same size as the output. The LLVM backend wants to see
1190 // the input and output of a matching constraint be the same size. Note
1191 // that GCC does not define what the top bits are here. We use zext because
1192 // that is usually cheaper, but LLVM IR should really get an anyext someday.
1193 if (Info
.hasTiedOperand()) {
1194 unsigned Output
= Info
.getTiedOperand();
1195 QualType OutputType
= S
.getOutputExpr(Output
)->getType();
1196 QualType InputTy
= InputExpr
->getType();
1198 if (getContext().getTypeSize(OutputType
) >
1199 getContext().getTypeSize(InputTy
)) {
1200 // Use ptrtoint as appropriate so that we can do our extension.
1201 if (isa
<llvm::PointerType
>(Arg
->getType()))
1202 Arg
= Builder
.CreatePtrToInt(Arg
, IntPtrTy
);
1203 const llvm::Type
*OutputTy
= ConvertType(OutputType
);
1204 if (isa
<llvm::IntegerType
>(OutputTy
))
1205 Arg
= Builder
.CreateZExt(Arg
, OutputTy
);
1207 Arg
= Builder
.CreateFPExt(Arg
, OutputTy
);
1210 if (const llvm::Type
* AdjTy
=
1211 getTargetHooks().adjustInlineAsmType(*this, InputConstraint
,
1213 Arg
= Builder
.CreateBitCast(Arg
, AdjTy
);
1215 ArgTypes
.push_back(Arg
->getType());
1216 Args
.push_back(Arg
);
1217 Constraints
+= InputConstraint
;
1220 // Append the "input" part of inout constraints last.
1221 for (unsigned i
= 0, e
= InOutArgs
.size(); i
!= e
; i
++) {
1222 ArgTypes
.push_back(InOutArgTypes
[i
]);
1223 Args
.push_back(InOutArgs
[i
]);
1225 Constraints
+= InOutConstraints
;
1228 for (unsigned i
= 0, e
= S
.getNumClobbers(); i
!= e
; i
++) {
1229 llvm::StringRef Clobber
= S
.getClobber(i
)->getString();
1231 Clobber
= Target
.getNormalizedGCCRegisterName(Clobber
);
1233 if (i
!= 0 || NumConstraints
!= 0)
1236 Constraints
+= "~{";
1237 Constraints
+= Clobber
;
1241 // Add machine specific clobbers
1242 std::string MachineClobbers
= Target
.getClobbers();
1243 if (!MachineClobbers
.empty()) {
1244 if (!Constraints
.empty())
1246 Constraints
+= MachineClobbers
;
1249 const llvm::Type
*ResultType
;
1250 if (ResultRegTypes
.empty())
1251 ResultType
= llvm::Type::getVoidTy(getLLVMContext());
1252 else if (ResultRegTypes
.size() == 1)
1253 ResultType
= ResultRegTypes
[0];
1255 ResultType
= llvm::StructType::get(getLLVMContext(), ResultRegTypes
);
1257 const llvm::FunctionType
*FTy
=
1258 llvm::FunctionType::get(ResultType
, ArgTypes
, false);
1260 llvm::InlineAsm
*IA
=
1261 llvm::InlineAsm::get(FTy
, AsmString
, Constraints
,
1262 S
.isVolatile() || S
.getNumOutputs() == 0);
1263 llvm::CallInst
*Result
= Builder
.CreateCall(IA
, Args
.begin(), Args
.end());
1264 Result
->addAttribute(~0, llvm::Attribute::NoUnwind
);
1266 // Slap the source location of the inline asm into a !srcloc metadata on the
1268 Result
->setMetadata("srcloc", getAsmSrcLocInfo(S
.getAsmString(), *this));
1270 // Extract all of the register value results from the asm.
1271 std::vector
<llvm::Value
*> RegResults
;
1272 if (ResultRegTypes
.size() == 1) {
1273 RegResults
.push_back(Result
);
1275 for (unsigned i
= 0, e
= ResultRegTypes
.size(); i
!= e
; ++i
) {
1276 llvm::Value
*Tmp
= Builder
.CreateExtractValue(Result
, i
, "asmresult");
1277 RegResults
.push_back(Tmp
);
1281 for (unsigned i
= 0, e
= RegResults
.size(); i
!= e
; ++i
) {
1282 llvm::Value
*Tmp
= RegResults
[i
];
1284 // If the result type of the LLVM IR asm doesn't match the result type of
1285 // the expression, do the conversion.
1286 if (ResultRegTypes
[i
] != ResultTruncRegTypes
[i
]) {
1287 const llvm::Type
*TruncTy
= ResultTruncRegTypes
[i
];
1289 // Truncate the integer result to the right size, note that TruncTy can be
1291 if (TruncTy
->isFloatingPointTy())
1292 Tmp
= Builder
.CreateFPTrunc(Tmp
, TruncTy
);
1293 else if (TruncTy
->isPointerTy() && Tmp
->getType()->isIntegerTy()) {
1294 uint64_t ResSize
= CGM
.getTargetData().getTypeSizeInBits(TruncTy
);
1295 Tmp
= Builder
.CreateTrunc(Tmp
,
1296 llvm::IntegerType::get(getLLVMContext(), (unsigned)ResSize
));
1297 Tmp
= Builder
.CreateIntToPtr(Tmp
, TruncTy
);
1298 } else if (Tmp
->getType()->isPointerTy() && TruncTy
->isIntegerTy()) {
1299 uint64_t TmpSize
=CGM
.getTargetData().getTypeSizeInBits(Tmp
->getType());
1300 Tmp
= Builder
.CreatePtrToInt(Tmp
,
1301 llvm::IntegerType::get(getLLVMContext(), (unsigned)TmpSize
));
1302 Tmp
= Builder
.CreateTrunc(Tmp
, TruncTy
);
1303 } else if (TruncTy
->isIntegerTy()) {
1304 Tmp
= Builder
.CreateTrunc(Tmp
, TruncTy
);
1305 } else if (TruncTy
->isVectorTy()) {
1306 Tmp
= Builder
.CreateBitCast(Tmp
, TruncTy
);
1310 EmitStoreThroughLValue(RValue::get(Tmp
), ResultRegDests
[i
],
1311 ResultRegQualTys
[i
]);