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