[analyzer] Use the new registration mechanism on the non-path-sensitive-checkers:
[clang.git] / lib / CodeGen / CGStmt.cpp
blobf809c009ce11f286cb7298328a1fa8c28a7ad025
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.
12 //===----------------------------------------------------------------------===//
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;
27 //===----------------------------------------------------------------------===//
28 // Statement Emission
29 //===----------------------------------------------------------------------===//
31 void CodeGenFunction::EmitStopPoint(const Stmt *S) {
32 if (CGDebugInfo *DI = getDebugInfo()) {
33 if (isa<DeclStmt>(S))
34 DI->setLocation(S->getLocEnd());
35 else
36 DI->setLocation(S->getLocStart());
37 DI->UpdateLineDirectiveRegion(Builder);
38 DI->EmitStopPoint(Builder);
42 void CodeGenFunction::EmitStmt(const Stmt *S) {
43 assert(S && "Null statement?");
45 // Check if we can handle this without bothering to generate an
46 // insert point or debug info.
47 if (EmitSimpleStmt(S))
48 return;
50 // Check if we are generating unreachable code.
51 if (!HaveInsertPoint()) {
52 // If so, and the statement doesn't contain a label, then we do not need to
53 // generate actual code. This is safe because (1) the current point is
54 // unreachable, so we don't need to execute the code, and (2) we've already
55 // handled the statements which update internal data structures (like the
56 // local variable map) which could be used by subsequent statements.
57 if (!ContainsLabel(S)) {
58 // Verify that any decl statements were handled as simple, they may be in
59 // scope of subsequent reachable statements.
60 assert(!isa<DeclStmt>(*S) && "Unexpected DeclStmt!");
61 return;
64 // Otherwise, make a new block to hold the code.
65 EnsureInsertPoint();
68 // Generate a stoppoint if we are emitting debug info.
69 EmitStopPoint(S);
71 switch (S->getStmtClass()) {
72 case Stmt::NoStmtClass:
73 case Stmt::CXXCatchStmtClass:
74 llvm_unreachable("invalid statement class to emit generically");
75 case Stmt::NullStmtClass:
76 case Stmt::CompoundStmtClass:
77 case Stmt::DeclStmtClass:
78 case Stmt::LabelStmtClass:
79 case Stmt::GotoStmtClass:
80 case Stmt::BreakStmtClass:
81 case Stmt::ContinueStmtClass:
82 case Stmt::DefaultStmtClass:
83 case Stmt::CaseStmtClass:
84 llvm_unreachable("should have emitted these statements as simple");
86 #define STMT(Type, Base)
87 #define ABSTRACT_STMT(Op)
88 #define EXPR(Type, Base) \
89 case Stmt::Type##Class:
90 #include "clang/AST/StmtNodes.inc"
92 // Remember the block we came in on.
93 llvm::BasicBlock *incoming = Builder.GetInsertBlock();
94 assert(incoming && "expression emission must have an insertion point");
96 EmitIgnoredExpr(cast<Expr>(S));
98 llvm::BasicBlock *outgoing = Builder.GetInsertBlock();
99 assert(outgoing && "expression emission cleared block!");
101 // The expression emitters assume (reasonably!) that the insertion
102 // point is always set. To maintain that, the call-emission code
103 // for noreturn functions has to enter a new block with no
104 // predecessors. We want to kill that block and mark the current
105 // insertion point unreachable in the common case of a call like
106 // "exit();". Since expression emission doesn't otherwise create
107 // blocks with no predecessors, we can just test for that.
108 // However, we must be careful not to do this to our incoming
109 // block, because *statement* emission does sometimes create
110 // reachable blocks which will have no predecessors until later in
111 // the function. This occurs with, e.g., labels that are not
112 // reachable by fallthrough.
113 if (incoming != outgoing && outgoing->use_empty()) {
114 outgoing->eraseFromParent();
115 Builder.ClearInsertionPoint();
117 break;
120 case Stmt::IndirectGotoStmtClass:
121 EmitIndirectGotoStmt(cast<IndirectGotoStmt>(*S)); break;
123 case Stmt::IfStmtClass: EmitIfStmt(cast<IfStmt>(*S)); break;
124 case Stmt::WhileStmtClass: EmitWhileStmt(cast<WhileStmt>(*S)); break;
125 case Stmt::DoStmtClass: EmitDoStmt(cast<DoStmt>(*S)); break;
126 case Stmt::ForStmtClass: EmitForStmt(cast<ForStmt>(*S)); break;
128 case Stmt::ReturnStmtClass: EmitReturnStmt(cast<ReturnStmt>(*S)); break;
130 case Stmt::SwitchStmtClass: EmitSwitchStmt(cast<SwitchStmt>(*S)); break;
131 case Stmt::AsmStmtClass: EmitAsmStmt(cast<AsmStmt>(*S)); break;
133 case Stmt::ObjCAtTryStmtClass:
134 EmitObjCAtTryStmt(cast<ObjCAtTryStmt>(*S));
135 break;
136 case Stmt::ObjCAtCatchStmtClass:
137 assert(0 && "@catch statements should be handled by EmitObjCAtTryStmt");
138 break;
139 case Stmt::ObjCAtFinallyStmtClass:
140 assert(0 && "@finally statements should be handled by EmitObjCAtTryStmt");
141 break;
142 case Stmt::ObjCAtThrowStmtClass:
143 EmitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(*S));
144 break;
145 case Stmt::ObjCAtSynchronizedStmtClass:
146 EmitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(*S));
147 break;
148 case Stmt::ObjCForCollectionStmtClass:
149 EmitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(*S));
150 break;
152 case Stmt::CXXTryStmtClass:
153 EmitCXXTryStmt(cast<CXXTryStmt>(*S));
154 break;
158 bool CodeGenFunction::EmitSimpleStmt(const Stmt *S) {
159 switch (S->getStmtClass()) {
160 default: return false;
161 case Stmt::NullStmtClass: break;
162 case Stmt::CompoundStmtClass: EmitCompoundStmt(cast<CompoundStmt>(*S)); break;
163 case Stmt::DeclStmtClass: EmitDeclStmt(cast<DeclStmt>(*S)); break;
164 case Stmt::LabelStmtClass: EmitLabelStmt(cast<LabelStmt>(*S)); break;
165 case Stmt::GotoStmtClass: EmitGotoStmt(cast<GotoStmt>(*S)); break;
166 case Stmt::BreakStmtClass: EmitBreakStmt(cast<BreakStmt>(*S)); break;
167 case Stmt::ContinueStmtClass: EmitContinueStmt(cast<ContinueStmt>(*S)); break;
168 case Stmt::DefaultStmtClass: EmitDefaultStmt(cast<DefaultStmt>(*S)); break;
169 case Stmt::CaseStmtClass: EmitCaseStmt(cast<CaseStmt>(*S)); break;
172 return true;
175 /// EmitCompoundStmt - Emit a compound statement {..} node. If GetLast is true,
176 /// this captures the expression result of the last sub-statement and returns it
177 /// (for use by the statement expression extension).
178 RValue CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast,
179 AggValueSlot AggSlot) {
180 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),S.getLBracLoc(),
181 "LLVM IR generation of compound statement ('{}')");
183 CGDebugInfo *DI = getDebugInfo();
184 if (DI) {
185 DI->setLocation(S.getLBracLoc());
186 DI->EmitRegionStart(Builder);
189 // Keep track of the current cleanup stack depth.
190 RunCleanupsScope Scope(*this);
192 for (CompoundStmt::const_body_iterator I = S.body_begin(),
193 E = S.body_end()-GetLast; I != E; ++I)
194 EmitStmt(*I);
196 if (DI) {
197 DI->setLocation(S.getRBracLoc());
198 DI->EmitRegionEnd(Builder);
201 RValue RV;
202 if (!GetLast)
203 RV = RValue::get(0);
204 else {
205 // We have to special case labels here. They are statements, but when put
206 // at the end of a statement expression, they yield the value of their
207 // subexpression. Handle this by walking through all labels we encounter,
208 // emitting them before we evaluate the subexpr.
209 const Stmt *LastStmt = S.body_back();
210 while (const LabelStmt *LS = dyn_cast<LabelStmt>(LastStmt)) {
211 EmitLabel(LS->getDecl());
212 LastStmt = LS->getSubStmt();
215 EnsureInsertPoint();
217 RV = EmitAnyExpr(cast<Expr>(LastStmt), AggSlot);
220 return RV;
223 void CodeGenFunction::SimplifyForwardingBlocks(llvm::BasicBlock *BB) {
224 llvm::BranchInst *BI = dyn_cast<llvm::BranchInst>(BB->getTerminator());
226 // If there is a cleanup stack, then we it isn't worth trying to
227 // simplify this block (we would need to remove it from the scope map
228 // and cleanup entry).
229 if (!EHStack.empty())
230 return;
232 // Can only simplify direct branches.
233 if (!BI || !BI->isUnconditional())
234 return;
236 BB->replaceAllUsesWith(BI->getSuccessor(0));
237 BI->eraseFromParent();
238 BB->eraseFromParent();
241 void CodeGenFunction::EmitBlock(llvm::BasicBlock *BB, bool IsFinished) {
242 llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
244 // Fall out of the current block (if necessary).
245 EmitBranch(BB);
247 if (IsFinished && BB->use_empty()) {
248 delete BB;
249 return;
252 // Place the block after the current block, if possible, or else at
253 // the end of the function.
254 if (CurBB && CurBB->getParent())
255 CurFn->getBasicBlockList().insertAfter(CurBB, BB);
256 else
257 CurFn->getBasicBlockList().push_back(BB);
258 Builder.SetInsertPoint(BB);
261 void CodeGenFunction::EmitBranch(llvm::BasicBlock *Target) {
262 // Emit a branch from the current block to the target one if this
263 // was a real block. If this was just a fall-through block after a
264 // terminator, don't emit it.
265 llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
267 if (!CurBB || CurBB->getTerminator()) {
268 // If there is no insert point or the previous block is already
269 // terminated, don't touch it.
270 } else {
271 // Otherwise, create a fall-through branch.
272 Builder.CreateBr(Target);
275 Builder.ClearInsertionPoint();
278 CodeGenFunction::JumpDest
279 CodeGenFunction::getJumpDestForLabel(const LabelDecl *D) {
280 JumpDest &Dest = LabelMap[D];
281 if (Dest.isValid()) return Dest;
283 // Create, but don't insert, the new block.
284 Dest = JumpDest(createBasicBlock(D->getName()),
285 EHScopeStack::stable_iterator::invalid(),
286 NextCleanupDestIndex++);
287 return Dest;
290 void CodeGenFunction::EmitLabel(const LabelDecl *D) {
291 JumpDest &Dest = LabelMap[D];
293 // If we didn't need a forward reference to this label, just go
294 // ahead and create a destination at the current scope.
295 if (!Dest.isValid()) {
296 Dest = getJumpDestInCurrentScope(D->getName());
298 // Otherwise, we need to give this label a target depth and remove
299 // it from the branch-fixups list.
300 } else {
301 assert(!Dest.getScopeDepth().isValid() && "already emitted label!");
302 Dest = JumpDest(Dest.getBlock(),
303 EHStack.stable_begin(),
304 Dest.getDestIndex());
306 ResolveBranchFixups(Dest.getBlock());
309 EmitBlock(Dest.getBlock());
313 void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) {
314 EmitLabel(S.getDecl());
315 EmitStmt(S.getSubStmt());
318 void CodeGenFunction::EmitGotoStmt(const GotoStmt &S) {
319 // If this code is reachable then emit a stop point (if generating
320 // debug info). We have to do this ourselves because we are on the
321 // "simple" statement path.
322 if (HaveInsertPoint())
323 EmitStopPoint(&S);
325 EmitBranchThroughCleanup(getJumpDestForLabel(S.getLabel()));
329 void CodeGenFunction::EmitIndirectGotoStmt(const IndirectGotoStmt &S) {
330 if (const LabelDecl *Target = S.getConstantTarget()) {
331 EmitBranchThroughCleanup(getJumpDestForLabel(Target));
332 return;
335 // Ensure that we have an i8* for our PHI node.
336 llvm::Value *V = Builder.CreateBitCast(EmitScalarExpr(S.getTarget()),
337 Int8PtrTy, "addr");
338 llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
341 // Get the basic block for the indirect goto.
342 llvm::BasicBlock *IndGotoBB = GetIndirectGotoBlock();
344 // The first instruction in the block has to be the PHI for the switch dest,
345 // add an entry for this branch.
346 cast<llvm::PHINode>(IndGotoBB->begin())->addIncoming(V, CurBB);
348 EmitBranch(IndGotoBB);
351 void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
352 // C99 6.8.4.1: The first substatement is executed if the expression compares
353 // unequal to 0. The condition must be a scalar type.
354 RunCleanupsScope ConditionScope(*this);
356 if (S.getConditionVariable())
357 EmitAutoVarDecl(*S.getConditionVariable());
359 // If the condition constant folds and can be elided, try to avoid emitting
360 // the condition and the dead arm of the if/else.
361 if (int Cond = ConstantFoldsToSimpleInteger(S.getCond())) {
362 // Figure out which block (then or else) is executed.
363 const Stmt *Executed = S.getThen(), *Skipped = S.getElse();
364 if (Cond == -1) // Condition false?
365 std::swap(Executed, Skipped);
367 // If the skipped block has no labels in it, just emit the executed block.
368 // This avoids emitting dead code and simplifies the CFG substantially.
369 if (!ContainsLabel(Skipped)) {
370 if (Executed) {
371 RunCleanupsScope ExecutedScope(*this);
372 EmitStmt(Executed);
374 return;
378 // Otherwise, the condition did not fold, or we couldn't elide it. Just emit
379 // the conditional branch.
380 llvm::BasicBlock *ThenBlock = createBasicBlock("if.then");
381 llvm::BasicBlock *ContBlock = createBasicBlock("if.end");
382 llvm::BasicBlock *ElseBlock = ContBlock;
383 if (S.getElse())
384 ElseBlock = createBasicBlock("if.else");
385 EmitBranchOnBoolExpr(S.getCond(), ThenBlock, ElseBlock);
387 // Emit the 'then' code.
388 EmitBlock(ThenBlock);
390 RunCleanupsScope ThenScope(*this);
391 EmitStmt(S.getThen());
393 EmitBranch(ContBlock);
395 // Emit the 'else' code if present.
396 if (const Stmt *Else = S.getElse()) {
397 EmitBlock(ElseBlock);
399 RunCleanupsScope ElseScope(*this);
400 EmitStmt(Else);
402 EmitBranch(ContBlock);
405 // Emit the continuation block for code after the if.
406 EmitBlock(ContBlock, true);
409 void CodeGenFunction::EmitWhileStmt(const WhileStmt &S) {
410 // Emit the header for the loop, which will also become
411 // the continue target.
412 JumpDest LoopHeader = getJumpDestInCurrentScope("while.cond");
413 EmitBlock(LoopHeader.getBlock());
415 // Create an exit block for when the condition fails, which will
416 // also become the break target.
417 JumpDest LoopExit = getJumpDestInCurrentScope("while.end");
419 // Store the blocks to use for break and continue.
420 BreakContinueStack.push_back(BreakContinue(LoopExit, LoopHeader));
422 // C++ [stmt.while]p2:
423 // When the condition of a while statement is a declaration, the
424 // scope of the variable that is declared extends from its point
425 // of declaration (3.3.2) to the end of the while statement.
426 // [...]
427 // The object created in a condition is destroyed and created
428 // with each iteration of the loop.
429 RunCleanupsScope ConditionScope(*this);
431 if (S.getConditionVariable())
432 EmitAutoVarDecl(*S.getConditionVariable());
434 // Evaluate the conditional in the while header. C99 6.8.5.1: The
435 // evaluation of the controlling expression takes place before each
436 // execution of the loop body.
437 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
439 // while(1) is common, avoid extra exit blocks. Be sure
440 // to correctly handle break/continue though.
441 bool EmitBoolCondBranch = true;
442 if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal))
443 if (C->isOne())
444 EmitBoolCondBranch = false;
446 // As long as the condition is true, go to the loop body.
447 llvm::BasicBlock *LoopBody = createBasicBlock("while.body");
448 if (EmitBoolCondBranch) {
449 llvm::BasicBlock *ExitBlock = LoopExit.getBlock();
450 if (ConditionScope.requiresCleanups())
451 ExitBlock = createBasicBlock("while.exit");
453 Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock);
455 if (ExitBlock != LoopExit.getBlock()) {
456 EmitBlock(ExitBlock);
457 EmitBranchThroughCleanup(LoopExit);
461 // Emit the loop body. We have to emit this in a cleanup scope
462 // because it might be a singleton DeclStmt.
464 RunCleanupsScope BodyScope(*this);
465 EmitBlock(LoopBody);
466 EmitStmt(S.getBody());
469 BreakContinueStack.pop_back();
471 // Immediately force cleanup.
472 ConditionScope.ForceCleanup();
474 // Branch to the loop header again.
475 EmitBranch(LoopHeader.getBlock());
477 // Emit the exit block.
478 EmitBlock(LoopExit.getBlock(), true);
480 // The LoopHeader typically is just a branch if we skipped emitting
481 // a branch, try to erase it.
482 if (!EmitBoolCondBranch)
483 SimplifyForwardingBlocks(LoopHeader.getBlock());
486 void CodeGenFunction::EmitDoStmt(const DoStmt &S) {
487 JumpDest LoopExit = getJumpDestInCurrentScope("do.end");
488 JumpDest LoopCond = getJumpDestInCurrentScope("do.cond");
490 // Store the blocks to use for break and continue.
491 BreakContinueStack.push_back(BreakContinue(LoopExit, LoopCond));
493 // Emit the body of the loop.
494 llvm::BasicBlock *LoopBody = createBasicBlock("do.body");
495 EmitBlock(LoopBody);
497 RunCleanupsScope BodyScope(*this);
498 EmitStmt(S.getBody());
501 BreakContinueStack.pop_back();
503 EmitBlock(LoopCond.getBlock());
505 // C99 6.8.5.2: "The evaluation of the controlling expression takes place
506 // after each execution of the loop body."
508 // Evaluate the conditional in the while header.
509 // C99 6.8.5p2/p4: The first substatement is executed if the expression
510 // compares unequal to 0. The condition must be a scalar type.
511 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
513 // "do {} while (0)" is common in macros, avoid extra blocks. Be sure
514 // to correctly handle break/continue though.
515 bool EmitBoolCondBranch = true;
516 if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal))
517 if (C->isZero())
518 EmitBoolCondBranch = false;
520 // As long as the condition is true, iterate the loop.
521 if (EmitBoolCondBranch)
522 Builder.CreateCondBr(BoolCondVal, LoopBody, LoopExit.getBlock());
524 // Emit the exit block.
525 EmitBlock(LoopExit.getBlock());
527 // The DoCond block typically is just a branch if we skipped
528 // emitting a branch, try to erase it.
529 if (!EmitBoolCondBranch)
530 SimplifyForwardingBlocks(LoopCond.getBlock());
533 void CodeGenFunction::EmitForStmt(const ForStmt &S) {
534 JumpDest LoopExit = getJumpDestInCurrentScope("for.end");
536 RunCleanupsScope ForScope(*this);
538 CGDebugInfo *DI = getDebugInfo();
539 if (DI) {
540 DI->setLocation(S.getSourceRange().getBegin());
541 DI->EmitRegionStart(Builder);
544 // Evaluate the first part before the loop.
545 if (S.getInit())
546 EmitStmt(S.getInit());
548 // Start the loop with a block that tests the condition.
549 // If there's an increment, the continue scope will be overwritten
550 // later.
551 JumpDest Continue = getJumpDestInCurrentScope("for.cond");
552 llvm::BasicBlock *CondBlock = Continue.getBlock();
553 EmitBlock(CondBlock);
555 // Create a cleanup scope for the condition variable cleanups.
556 RunCleanupsScope ConditionScope(*this);
558 llvm::Value *BoolCondVal = 0;
559 if (S.getCond()) {
560 // If the for statement has a condition scope, emit the local variable
561 // declaration.
562 llvm::BasicBlock *ExitBlock = LoopExit.getBlock();
563 if (S.getConditionVariable()) {
564 EmitAutoVarDecl(*S.getConditionVariable());
567 // If there are any cleanups between here and the loop-exit scope,
568 // create a block to stage a loop exit along.
569 if (ForScope.requiresCleanups())
570 ExitBlock = createBasicBlock("for.cond.cleanup");
572 // As long as the condition is true, iterate the loop.
573 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
575 // C99 6.8.5p2/p4: The first substatement is executed if the expression
576 // compares unequal to 0. The condition must be a scalar type.
577 BoolCondVal = EvaluateExprAsBool(S.getCond());
578 Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock);
580 if (ExitBlock != LoopExit.getBlock()) {
581 EmitBlock(ExitBlock);
582 EmitBranchThroughCleanup(LoopExit);
585 EmitBlock(ForBody);
586 } else {
587 // Treat it as a non-zero constant. Don't even create a new block for the
588 // body, just fall into it.
591 // If the for loop doesn't have an increment we can just use the
592 // condition as the continue block. Otherwise we'll need to create
593 // a block for it (in the current scope, i.e. in the scope of the
594 // condition), and that we will become our continue block.
595 if (S.getInc())
596 Continue = getJumpDestInCurrentScope("for.inc");
598 // Store the blocks to use for break and continue.
599 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
602 // Create a separate cleanup scope for the body, in case it is not
603 // a compound statement.
604 RunCleanupsScope BodyScope(*this);
605 EmitStmt(S.getBody());
608 // If there is an increment, emit it next.
609 if (S.getInc()) {
610 EmitBlock(Continue.getBlock());
611 EmitStmt(S.getInc());
614 BreakContinueStack.pop_back();
616 ConditionScope.ForceCleanup();
617 EmitBranch(CondBlock);
619 ForScope.ForceCleanup();
621 if (DI) {
622 DI->setLocation(S.getSourceRange().getEnd());
623 DI->EmitRegionEnd(Builder);
626 // Emit the fall-through block.
627 EmitBlock(LoopExit.getBlock(), true);
630 void CodeGenFunction::EmitReturnOfRValue(RValue RV, QualType Ty) {
631 if (RV.isScalar()) {
632 Builder.CreateStore(RV.getScalarVal(), ReturnValue);
633 } else if (RV.isAggregate()) {
634 EmitAggregateCopy(ReturnValue, RV.getAggregateAddr(), Ty);
635 } else {
636 StoreComplexToAddr(RV.getComplexVal(), ReturnValue, false);
638 EmitBranchThroughCleanup(ReturnBlock);
641 /// EmitReturnStmt - Note that due to GCC extensions, this can have an operand
642 /// if the function returns void, or may be missing one if the function returns
643 /// non-void. Fun stuff :).
644 void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) {
645 // Emit the result value, even if unused, to evalute the side effects.
646 const Expr *RV = S.getRetValue();
648 // FIXME: Clean this up by using an LValue for ReturnTemp,
649 // EmitStoreThroughLValue, and EmitAnyExpr.
650 if (S.getNRVOCandidate() && S.getNRVOCandidate()->isNRVOVariable() &&
651 !Target.useGlobalsForAutomaticVariables()) {
652 // Apply the named return value optimization for this return statement,
653 // which means doing nothing: the appropriate result has already been
654 // constructed into the NRVO variable.
656 // If there is an NRVO flag for this variable, set it to 1 into indicate
657 // that the cleanup code should not destroy the variable.
658 if (llvm::Value *NRVOFlag = NRVOFlags[S.getNRVOCandidate()])
659 Builder.CreateStore(Builder.getTrue(), NRVOFlag);
660 } else if (!ReturnValue) {
661 // Make sure not to return anything, but evaluate the expression
662 // for side effects.
663 if (RV)
664 EmitAnyExpr(RV);
665 } else if (RV == 0) {
666 // Do nothing (return value is left uninitialized)
667 } else if (FnRetTy->isReferenceType()) {
668 // If this function returns a reference, take the address of the expression
669 // rather than the value.
670 RValue Result = EmitReferenceBindingToExpr(RV, /*InitializedDecl=*/0);
671 Builder.CreateStore(Result.getScalarVal(), ReturnValue);
672 } else if (!hasAggregateLLVMType(RV->getType())) {
673 Builder.CreateStore(EmitScalarExpr(RV), ReturnValue);
674 } else if (RV->getType()->isAnyComplexType()) {
675 EmitComplexExprIntoAddr(RV, ReturnValue, false);
676 } else {
677 EmitAggExpr(RV, AggValueSlot::forAddr(ReturnValue, false, true));
680 EmitBranchThroughCleanup(ReturnBlock);
683 void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) {
684 // As long as debug info is modeled with instructions, we have to ensure we
685 // have a place to insert here and write the stop point here.
686 if (getDebugInfo()) {
687 EnsureInsertPoint();
688 EmitStopPoint(&S);
691 for (DeclStmt::const_decl_iterator I = S.decl_begin(), E = S.decl_end();
692 I != E; ++I)
693 EmitDecl(**I);
696 void CodeGenFunction::EmitBreakStmt(const BreakStmt &S) {
697 assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!");
699 // If this code is reachable then emit a stop point (if generating
700 // debug info). We have to do this ourselves because we are on the
701 // "simple" statement path.
702 if (HaveInsertPoint())
703 EmitStopPoint(&S);
705 JumpDest Block = BreakContinueStack.back().BreakBlock;
706 EmitBranchThroughCleanup(Block);
709 void CodeGenFunction::EmitContinueStmt(const ContinueStmt &S) {
710 assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
712 // If this code is reachable then emit a stop point (if generating
713 // debug info). We have to do this ourselves because we are on the
714 // "simple" statement path.
715 if (HaveInsertPoint())
716 EmitStopPoint(&S);
718 JumpDest Block = BreakContinueStack.back().ContinueBlock;
719 EmitBranchThroughCleanup(Block);
722 /// EmitCaseStmtRange - If case statement range is not too big then
723 /// add multiple cases to switch instruction, one for each value within
724 /// the range. If range is too big then emit "if" condition check.
725 void CodeGenFunction::EmitCaseStmtRange(const CaseStmt &S) {
726 assert(S.getRHS() && "Expected RHS value in CaseStmt");
728 llvm::APSInt LHS = S.getLHS()->EvaluateAsInt(getContext());
729 llvm::APSInt RHS = S.getRHS()->EvaluateAsInt(getContext());
731 // Emit the code for this case. We do this first to make sure it is
732 // properly chained from our predecessor before generating the
733 // switch machinery to enter this block.
734 EmitBlock(createBasicBlock("sw.bb"));
735 llvm::BasicBlock *CaseDest = Builder.GetInsertBlock();
736 EmitStmt(S.getSubStmt());
738 // If range is empty, do nothing.
739 if (LHS.isSigned() ? RHS.slt(LHS) : RHS.ult(LHS))
740 return;
742 llvm::APInt Range = RHS - LHS;
743 // FIXME: parameters such as this should not be hardcoded.
744 if (Range.ult(llvm::APInt(Range.getBitWidth(), 64))) {
745 // Range is small enough to add multiple switch instruction cases.
746 for (unsigned i = 0, e = Range.getZExtValue() + 1; i != e; ++i) {
747 SwitchInsn->addCase(llvm::ConstantInt::get(getLLVMContext(), LHS),
748 CaseDest);
749 LHS++;
751 return;
754 // The range is too big. Emit "if" condition into a new block,
755 // making sure to save and restore the current insertion point.
756 llvm::BasicBlock *RestoreBB = Builder.GetInsertBlock();
758 // Push this test onto the chain of range checks (which terminates
759 // in the default basic block). The switch's default will be changed
760 // to the top of this chain after switch emission is complete.
761 llvm::BasicBlock *FalseDest = CaseRangeBlock;
762 CaseRangeBlock = createBasicBlock("sw.caserange");
764 CurFn->getBasicBlockList().push_back(CaseRangeBlock);
765 Builder.SetInsertPoint(CaseRangeBlock);
767 // Emit range check.
768 llvm::Value *Diff =
769 Builder.CreateSub(SwitchInsn->getCondition(),
770 llvm::ConstantInt::get(getLLVMContext(), LHS), "tmp");
771 llvm::Value *Cond =
772 Builder.CreateICmpULE(Diff, llvm::ConstantInt::get(getLLVMContext(), Range),
773 "inbounds");
774 Builder.CreateCondBr(Cond, CaseDest, FalseDest);
776 // Restore the appropriate insertion point.
777 if (RestoreBB)
778 Builder.SetInsertPoint(RestoreBB);
779 else
780 Builder.ClearInsertionPoint();
783 void CodeGenFunction::EmitCaseStmt(const CaseStmt &S) {
784 if (S.getRHS()) {
785 EmitCaseStmtRange(S);
786 return;
789 EmitBlock(createBasicBlock("sw.bb"));
790 llvm::BasicBlock *CaseDest = Builder.GetInsertBlock();
791 llvm::APSInt CaseVal = S.getLHS()->EvaluateAsInt(getContext());
792 SwitchInsn->addCase(llvm::ConstantInt::get(getLLVMContext(), CaseVal),
793 CaseDest);
795 // Recursively emitting the statement is acceptable, but is not wonderful for
796 // code where we have many case statements nested together, i.e.:
797 // case 1:
798 // case 2:
799 // case 3: etc.
800 // Handling this recursively will create a new block for each case statement
801 // that falls through to the next case which is IR intensive. It also causes
802 // deep recursion which can run into stack depth limitations. Handle
803 // sequential non-range case statements specially.
804 const CaseStmt *CurCase = &S;
805 const CaseStmt *NextCase = dyn_cast<CaseStmt>(S.getSubStmt());
807 // Otherwise, iteratively add consequtive cases to this switch stmt.
808 while (NextCase && NextCase->getRHS() == 0) {
809 CurCase = NextCase;
810 CaseVal = CurCase->getLHS()->EvaluateAsInt(getContext());
811 SwitchInsn->addCase(llvm::ConstantInt::get(getLLVMContext(), CaseVal),
812 CaseDest);
814 NextCase = dyn_cast<CaseStmt>(CurCase->getSubStmt());
817 // Normal default recursion for non-cases.
818 EmitStmt(CurCase->getSubStmt());
821 void CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S) {
822 llvm::BasicBlock *DefaultBlock = SwitchInsn->getDefaultDest();
823 assert(DefaultBlock->empty() &&
824 "EmitDefaultStmt: Default block already defined?");
825 EmitBlock(DefaultBlock);
826 EmitStmt(S.getSubStmt());
829 void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) {
830 JumpDest SwitchExit = getJumpDestInCurrentScope("sw.epilog");
832 RunCleanupsScope ConditionScope(*this);
834 if (S.getConditionVariable())
835 EmitAutoVarDecl(*S.getConditionVariable());
837 llvm::Value *CondV = EmitScalarExpr(S.getCond());
839 // Handle nested switch statements.
840 llvm::SwitchInst *SavedSwitchInsn = SwitchInsn;
841 llvm::BasicBlock *SavedCRBlock = CaseRangeBlock;
843 // Create basic block to hold stuff that comes after switch
844 // statement. We also need to create a default block now so that
845 // explicit case ranges tests can have a place to jump to on
846 // failure.
847 llvm::BasicBlock *DefaultBlock = createBasicBlock("sw.default");
848 SwitchInsn = Builder.CreateSwitch(CondV, DefaultBlock);
849 CaseRangeBlock = DefaultBlock;
851 // Clear the insertion point to indicate we are in unreachable code.
852 Builder.ClearInsertionPoint();
854 // All break statements jump to NextBlock. If BreakContinueStack is non empty
855 // then reuse last ContinueBlock.
856 JumpDest OuterContinue;
857 if (!BreakContinueStack.empty())
858 OuterContinue = BreakContinueStack.back().ContinueBlock;
860 BreakContinueStack.push_back(BreakContinue(SwitchExit, OuterContinue));
862 // Emit switch body.
863 EmitStmt(S.getBody());
865 BreakContinueStack.pop_back();
867 // Update the default block in case explicit case range tests have
868 // been chained on top.
869 SwitchInsn->setSuccessor(0, CaseRangeBlock);
871 // If a default was never emitted:
872 if (!DefaultBlock->getParent()) {
873 // If we have cleanups, emit the default block so that there's a
874 // place to jump through the cleanups from.
875 if (ConditionScope.requiresCleanups()) {
876 EmitBlock(DefaultBlock);
878 // Otherwise, just forward the default block to the switch end.
879 } else {
880 DefaultBlock->replaceAllUsesWith(SwitchExit.getBlock());
881 delete DefaultBlock;
885 ConditionScope.ForceCleanup();
887 // Emit continuation.
888 EmitBlock(SwitchExit.getBlock(), true);
890 SwitchInsn = SavedSwitchInsn;
891 CaseRangeBlock = SavedCRBlock;
894 static std::string
895 SimplifyConstraint(const char *Constraint, const TargetInfo &Target,
896 llvm::SmallVectorImpl<TargetInfo::ConstraintInfo> *OutCons=0) {
897 std::string Result;
899 while (*Constraint) {
900 switch (*Constraint) {
901 default:
902 Result += Target.convertConstraint(*Constraint);
903 break;
904 // Ignore these
905 case '*':
906 case '?':
907 case '!':
908 case '=': // Will see this and the following in mult-alt constraints.
909 case '+':
910 break;
911 case ',':
912 Result += "|";
913 break;
914 case 'g':
915 Result += "imr";
916 break;
917 case '[': {
918 assert(OutCons &&
919 "Must pass output names to constraints with a symbolic name");
920 unsigned Index;
921 bool result = Target.resolveSymbolicName(Constraint,
922 &(*OutCons)[0],
923 OutCons->size(), Index);
924 assert(result && "Could not resolve symbolic name"); (void)result;
925 Result += llvm::utostr(Index);
926 break;
930 Constraint++;
933 return Result;
936 /// AddVariableConstraints - Look at AsmExpr and if it is a variable declared
937 /// as using a particular register add that as a constraint that will be used
938 /// in this asm stmt.
939 static std::string
940 AddVariableConstraints(const std::string &Constraint, const Expr &AsmExpr,
941 const TargetInfo &Target, CodeGenModule &CGM,
942 const AsmStmt &Stmt) {
943 const DeclRefExpr *AsmDeclRef = dyn_cast<DeclRefExpr>(&AsmExpr);
944 if (!AsmDeclRef)
945 return Constraint;
946 const ValueDecl &Value = *AsmDeclRef->getDecl();
947 const VarDecl *Variable = dyn_cast<VarDecl>(&Value);
948 if (!Variable)
949 return Constraint;
950 AsmLabelAttr *Attr = Variable->getAttr<AsmLabelAttr>();
951 if (!Attr)
952 return Constraint;
953 llvm::StringRef Register = Attr->getLabel();
954 assert(Target.isValidGCCRegisterName(Register));
955 // FIXME: We should check which registers are compatible with "r" or "x".
956 if (Constraint != "r" && Constraint != "x") {
957 CGM.ErrorUnsupported(&Stmt, "__asm__");
958 return Constraint;
960 return "{" + Register.str() + "}";
963 llvm::Value*
964 CodeGenFunction::EmitAsmInputLValue(const AsmStmt &S,
965 const TargetInfo::ConstraintInfo &Info,
966 LValue InputValue, QualType InputType,
967 std::string &ConstraintStr) {
968 llvm::Value *Arg;
969 if (Info.allowsRegister() || !Info.allowsMemory()) {
970 if (!CodeGenFunction::hasAggregateLLVMType(InputType)) {
971 Arg = EmitLoadOfLValue(InputValue, InputType).getScalarVal();
972 } else {
973 const llvm::Type *Ty = ConvertType(InputType);
974 uint64_t Size = CGM.getTargetData().getTypeSizeInBits(Ty);
975 if (Size <= 64 && llvm::isPowerOf2_64(Size)) {
976 Ty = llvm::IntegerType::get(getLLVMContext(), Size);
977 Ty = llvm::PointerType::getUnqual(Ty);
979 Arg = Builder.CreateLoad(Builder.CreateBitCast(InputValue.getAddress(),
980 Ty));
981 } else {
982 Arg = InputValue.getAddress();
983 ConstraintStr += '*';
986 } else {
987 Arg = InputValue.getAddress();
988 ConstraintStr += '*';
991 return Arg;
994 llvm::Value* CodeGenFunction::EmitAsmInput(const AsmStmt &S,
995 const TargetInfo::ConstraintInfo &Info,
996 const Expr *InputExpr,
997 std::string &ConstraintStr) {
998 if (Info.allowsRegister() || !Info.allowsMemory())
999 if (!CodeGenFunction::hasAggregateLLVMType(InputExpr->getType()))
1000 return EmitScalarExpr(InputExpr);
1002 InputExpr = InputExpr->IgnoreParenNoopCasts(getContext());
1003 LValue Dest = EmitLValue(InputExpr);
1004 return EmitAsmInputLValue(S, Info, Dest, InputExpr->getType(), ConstraintStr);
1007 /// getAsmSrcLocInfo - Return the !srcloc metadata node to attach to an inline
1008 /// asm call instruction. The !srcloc MDNode contains a list of constant
1009 /// integers which are the source locations of the start of each line in the
1010 /// asm.
1011 static llvm::MDNode *getAsmSrcLocInfo(const StringLiteral *Str,
1012 CodeGenFunction &CGF) {
1013 llvm::SmallVector<llvm::Value *, 8> Locs;
1014 // Add the location of the first line to the MDNode.
1015 Locs.push_back(llvm::ConstantInt::get(CGF.Int32Ty,
1016 Str->getLocStart().getRawEncoding()));
1017 llvm::StringRef StrVal = Str->getString();
1018 if (!StrVal.empty()) {
1019 const SourceManager &SM = CGF.CGM.getContext().getSourceManager();
1020 const LangOptions &LangOpts = CGF.CGM.getLangOptions();
1022 // Add the location of the start of each subsequent line of the asm to the
1023 // MDNode.
1024 for (unsigned i = 0, e = StrVal.size()-1; i != e; ++i) {
1025 if (StrVal[i] != '\n') continue;
1026 SourceLocation LineLoc = Str->getLocationOfByte(i+1, SM, LangOpts,
1027 CGF.Target);
1028 Locs.push_back(llvm::ConstantInt::get(CGF.Int32Ty,
1029 LineLoc.getRawEncoding()));
1033 return llvm::MDNode::get(CGF.getLLVMContext(), Locs.data(), Locs.size());
1036 void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) {
1037 // Analyze the asm string to decompose it into its pieces. We know that Sema
1038 // has already done this, so it is guaranteed to be successful.
1039 llvm::SmallVector<AsmStmt::AsmStringPiece, 4> Pieces;
1040 unsigned DiagOffs;
1041 S.AnalyzeAsmString(Pieces, getContext(), DiagOffs);
1043 // Assemble the pieces into the final asm string.
1044 std::string AsmString;
1045 for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
1046 if (Pieces[i].isString())
1047 AsmString += Pieces[i].getString();
1048 else if (Pieces[i].getModifier() == '\0')
1049 AsmString += '$' + llvm::utostr(Pieces[i].getOperandNo());
1050 else
1051 AsmString += "${" + llvm::utostr(Pieces[i].getOperandNo()) + ':' +
1052 Pieces[i].getModifier() + '}';
1055 // Get all the output and input constraints together.
1056 llvm::SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos;
1057 llvm::SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos;
1059 for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) {
1060 TargetInfo::ConstraintInfo Info(S.getOutputConstraint(i),
1061 S.getOutputName(i));
1062 bool IsValid = Target.validateOutputConstraint(Info); (void)IsValid;
1063 assert(IsValid && "Failed to parse output constraint");
1064 OutputConstraintInfos.push_back(Info);
1067 for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) {
1068 TargetInfo::ConstraintInfo Info(S.getInputConstraint(i),
1069 S.getInputName(i));
1070 bool IsValid = Target.validateInputConstraint(OutputConstraintInfos.data(),
1071 S.getNumOutputs(), Info);
1072 assert(IsValid && "Failed to parse input constraint"); (void)IsValid;
1073 InputConstraintInfos.push_back(Info);
1076 std::string Constraints;
1078 std::vector<LValue> ResultRegDests;
1079 std::vector<QualType> ResultRegQualTys;
1080 std::vector<const llvm::Type *> ResultRegTypes;
1081 std::vector<const llvm::Type *> ResultTruncRegTypes;
1082 std::vector<const llvm::Type*> ArgTypes;
1083 std::vector<llvm::Value*> Args;
1085 // Keep track of inout constraints.
1086 std::string InOutConstraints;
1087 std::vector<llvm::Value*> InOutArgs;
1088 std::vector<const llvm::Type*> InOutArgTypes;
1090 for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) {
1091 TargetInfo::ConstraintInfo &Info = OutputConstraintInfos[i];
1093 // Simplify the output constraint.
1094 std::string OutputConstraint(S.getOutputConstraint(i));
1095 OutputConstraint = SimplifyConstraint(OutputConstraint.c_str() + 1, Target);
1097 const Expr *OutExpr = S.getOutputExpr(i);
1098 OutExpr = OutExpr->IgnoreParenNoopCasts(getContext());
1100 OutputConstraint = AddVariableConstraints(OutputConstraint, *OutExpr, Target,
1101 CGM, S);
1103 LValue Dest = EmitLValue(OutExpr);
1104 if (!Constraints.empty())
1105 Constraints += ',';
1107 // If this is a register output, then make the inline asm return it
1108 // by-value. If this is a memory result, return the value by-reference.
1109 if (!Info.allowsMemory() && !hasAggregateLLVMType(OutExpr->getType())) {
1110 Constraints += "=" + OutputConstraint;
1111 ResultRegQualTys.push_back(OutExpr->getType());
1112 ResultRegDests.push_back(Dest);
1113 ResultRegTypes.push_back(ConvertTypeForMem(OutExpr->getType()));
1114 ResultTruncRegTypes.push_back(ResultRegTypes.back());
1116 // If this output is tied to an input, and if the input is larger, then
1117 // we need to set the actual result type of the inline asm node to be the
1118 // same as the input type.
1119 if (Info.hasMatchingInput()) {
1120 unsigned InputNo;
1121 for (InputNo = 0; InputNo != S.getNumInputs(); ++InputNo) {
1122 TargetInfo::ConstraintInfo &Input = InputConstraintInfos[InputNo];
1123 if (Input.hasTiedOperand() && Input.getTiedOperand() == i)
1124 break;
1126 assert(InputNo != S.getNumInputs() && "Didn't find matching input!");
1128 QualType InputTy = S.getInputExpr(InputNo)->getType();
1129 QualType OutputType = OutExpr->getType();
1131 uint64_t InputSize = getContext().getTypeSize(InputTy);
1132 if (getContext().getTypeSize(OutputType) < InputSize) {
1133 // Form the asm to return the value as a larger integer or fp type.
1134 ResultRegTypes.back() = ConvertType(InputTy);
1137 if (const llvm::Type* AdjTy =
1138 Target.adjustInlineAsmType(OutputConstraint, ResultRegTypes.back(),
1139 getLLVMContext()))
1140 ResultRegTypes.back() = AdjTy;
1141 } else {
1142 ArgTypes.push_back(Dest.getAddress()->getType());
1143 Args.push_back(Dest.getAddress());
1144 Constraints += "=*";
1145 Constraints += OutputConstraint;
1148 if (Info.isReadWrite()) {
1149 InOutConstraints += ',';
1151 const Expr *InputExpr = S.getOutputExpr(i);
1152 llvm::Value *Arg = EmitAsmInputLValue(S, Info, Dest, InputExpr->getType(),
1153 InOutConstraints);
1155 if (Info.allowsRegister())
1156 InOutConstraints += llvm::utostr(i);
1157 else
1158 InOutConstraints += OutputConstraint;
1160 InOutArgTypes.push_back(Arg->getType());
1161 InOutArgs.push_back(Arg);
1165 unsigned NumConstraints = S.getNumOutputs() + S.getNumInputs();
1167 for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) {
1168 const Expr *InputExpr = S.getInputExpr(i);
1170 TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i];
1172 if (!Constraints.empty())
1173 Constraints += ',';
1175 // Simplify the input constraint.
1176 std::string InputConstraint(S.getInputConstraint(i));
1177 InputConstraint = SimplifyConstraint(InputConstraint.c_str(), Target,
1178 &OutputConstraintInfos);
1180 InputConstraint =
1181 AddVariableConstraints(InputConstraint,
1182 *InputExpr->IgnoreParenNoopCasts(getContext()),
1183 Target, CGM, S);
1185 llvm::Value *Arg = EmitAsmInput(S, Info, InputExpr, Constraints);
1187 // If this input argument is tied to a larger output result, extend the
1188 // input to be the same size as the output. The LLVM backend wants to see
1189 // the input and output of a matching constraint be the same size. Note
1190 // that GCC does not define what the top bits are here. We use zext because
1191 // that is usually cheaper, but LLVM IR should really get an anyext someday.
1192 if (Info.hasTiedOperand()) {
1193 unsigned Output = Info.getTiedOperand();
1194 QualType OutputType = S.getOutputExpr(Output)->getType();
1195 QualType InputTy = InputExpr->getType();
1197 if (getContext().getTypeSize(OutputType) >
1198 getContext().getTypeSize(InputTy)) {
1199 // Use ptrtoint as appropriate so that we can do our extension.
1200 if (isa<llvm::PointerType>(Arg->getType()))
1201 Arg = Builder.CreatePtrToInt(Arg, IntPtrTy);
1202 const llvm::Type *OutputTy = ConvertType(OutputType);
1203 if (isa<llvm::IntegerType>(OutputTy))
1204 Arg = Builder.CreateZExt(Arg, OutputTy);
1205 else
1206 Arg = Builder.CreateFPExt(Arg, OutputTy);
1209 if (const llvm::Type* AdjTy =
1210 Target.adjustInlineAsmType(InputConstraint, Arg->getType(),
1211 getLLVMContext()))
1212 Arg = Builder.CreateBitCast(Arg, AdjTy);
1214 ArgTypes.push_back(Arg->getType());
1215 Args.push_back(Arg);
1216 Constraints += InputConstraint;
1219 // Append the "input" part of inout constraints last.
1220 for (unsigned i = 0, e = InOutArgs.size(); i != e; i++) {
1221 ArgTypes.push_back(InOutArgTypes[i]);
1222 Args.push_back(InOutArgs[i]);
1224 Constraints += InOutConstraints;
1226 // Clobbers
1227 for (unsigned i = 0, e = S.getNumClobbers(); i != e; i++) {
1228 llvm::StringRef Clobber = S.getClobber(i)->getString();
1230 Clobber = Target.getNormalizedGCCRegisterName(Clobber);
1232 if (i != 0 || NumConstraints != 0)
1233 Constraints += ',';
1235 Constraints += "~{";
1236 Constraints += Clobber;
1237 Constraints += '}';
1240 // Add machine specific clobbers
1241 std::string MachineClobbers = Target.getClobbers();
1242 if (!MachineClobbers.empty()) {
1243 if (!Constraints.empty())
1244 Constraints += ',';
1245 Constraints += MachineClobbers;
1248 const llvm::Type *ResultType;
1249 if (ResultRegTypes.empty())
1250 ResultType = llvm::Type::getVoidTy(getLLVMContext());
1251 else if (ResultRegTypes.size() == 1)
1252 ResultType = ResultRegTypes[0];
1253 else
1254 ResultType = llvm::StructType::get(getLLVMContext(), ResultRegTypes);
1256 const llvm::FunctionType *FTy =
1257 llvm::FunctionType::get(ResultType, ArgTypes, false);
1259 llvm::InlineAsm *IA =
1260 llvm::InlineAsm::get(FTy, AsmString, Constraints,
1261 S.isVolatile() || S.getNumOutputs() == 0);
1262 llvm::CallInst *Result = Builder.CreateCall(IA, Args.begin(), Args.end());
1263 Result->addAttribute(~0, llvm::Attribute::NoUnwind);
1265 // Slap the source location of the inline asm into a !srcloc metadata on the
1266 // call.
1267 Result->setMetadata("srcloc", getAsmSrcLocInfo(S.getAsmString(), *this));
1269 // Extract all of the register value results from the asm.
1270 std::vector<llvm::Value*> RegResults;
1271 if (ResultRegTypes.size() == 1) {
1272 RegResults.push_back(Result);
1273 } else {
1274 for (unsigned i = 0, e = ResultRegTypes.size(); i != e; ++i) {
1275 llvm::Value *Tmp = Builder.CreateExtractValue(Result, i, "asmresult");
1276 RegResults.push_back(Tmp);
1280 for (unsigned i = 0, e = RegResults.size(); i != e; ++i) {
1281 llvm::Value *Tmp = RegResults[i];
1283 // If the result type of the LLVM IR asm doesn't match the result type of
1284 // the expression, do the conversion.
1285 if (ResultRegTypes[i] != ResultTruncRegTypes[i]) {
1286 const llvm::Type *TruncTy = ResultTruncRegTypes[i];
1288 // Truncate the integer result to the right size, note that TruncTy can be
1289 // a pointer.
1290 if (TruncTy->isFloatingPointTy())
1291 Tmp = Builder.CreateFPTrunc(Tmp, TruncTy);
1292 else if (TruncTy->isPointerTy() && Tmp->getType()->isIntegerTy()) {
1293 uint64_t ResSize = CGM.getTargetData().getTypeSizeInBits(TruncTy);
1294 Tmp = Builder.CreateTrunc(Tmp,
1295 llvm::IntegerType::get(getLLVMContext(), (unsigned)ResSize));
1296 Tmp = Builder.CreateIntToPtr(Tmp, TruncTy);
1297 } else if (Tmp->getType()->isPointerTy() && TruncTy->isIntegerTy()) {
1298 uint64_t TmpSize =CGM.getTargetData().getTypeSizeInBits(Tmp->getType());
1299 Tmp = Builder.CreatePtrToInt(Tmp,
1300 llvm::IntegerType::get(getLLVMContext(), (unsigned)TmpSize));
1301 Tmp = Builder.CreateTrunc(Tmp, TruncTy);
1302 } else if (TruncTy->isIntegerTy()) {
1303 Tmp = Builder.CreateTrunc(Tmp, TruncTy);
1304 } else if (TruncTy->isVectorTy()) {
1305 Tmp = Builder.CreateBitCast(Tmp, TruncTy);
1309 EmitStoreThroughLValue(RValue::get(Tmp), ResultRegDests[i],
1310 ResultRegQualTys[i]);