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