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