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