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