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