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 | |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 14 | #include "CodeGenFunction.h" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 15 | #include "CGDebugInfo.h" |
| 16 | #include "CodeGenModule.h" |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 17 | #include "TargetInfo.h" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 18 | #include "clang/AST/StmtVisitor.h" |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 19 | #include "clang/Basic/Builtins.h" |
Chris Lattner | 7d22bf0 | 2009-03-05 08:04:57 +0000 | [diff] [blame] | 20 | #include "clang/Basic/PrettyStackTrace.h" |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 21 | #include "clang/Basic/TargetInfo.h" |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 22 | #include "clang/Sema/LoopHint.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 23 | #include "clang/Sema/SemaDiagnostic.h" |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringExtras.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 25 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 3b844ba | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 26 | #include "llvm/IR/DataLayout.h" |
| 27 | #include "llvm/IR/InlineAsm.h" |
| 28 | #include "llvm/IR/Intrinsics.h" |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 29 | #include "llvm/IR/MDBuilder.h" |
| 30 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | using namespace clang; |
| 32 | using namespace CodeGen; |
| 33 | |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | // Statement Emission |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 38 | void CodeGenFunction::EmitStopPoint(const Stmt *S) { |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 39 | if (CGDebugInfo *DI = getDebugInfo()) { |
Eric Christopher | 73fb350 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 40 | SourceLocation Loc; |
Adrian Prantl | 2736f2e | 2013-06-18 00:27:36 +0000 | [diff] [blame] | 41 | Loc = S->getLocStart(); |
Eric Christopher | 73fb350 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 42 | DI->EmitLocation(Builder, Loc); |
Adrian Prantl | fa6b079 | 2013-05-02 17:30:20 +0000 | [diff] [blame] | 43 | |
Adrian Prantl | 4008088 | 2013-05-07 22:26:03 +0000 | [diff] [blame] | 44 | LastStopPoint = Loc; |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 48 | void CodeGenFunction::EmitStmt(const Stmt *S) { |
| 49 | assert(S && "Null statement?"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 50 | PGO.setCurrentStmt(S); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 51 | |
Eric Christopher | f9aac38 | 2011-09-26 15:03:19 +0000 | [diff] [blame] | 52 | // These statements have their own debug info handling. |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 53 | if (EmitSimpleStmt(S)) |
| 54 | return; |
| 55 | |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 56 | // Check if we are generating unreachable code. |
| 57 | if (!HaveInsertPoint()) { |
| 58 | // If so, and the statement doesn't contain a label, then we do not need to |
| 59 | // generate actual code. This is safe because (1) the current point is |
| 60 | // unreachable, so we don't need to execute the code, and (2) we've already |
| 61 | // handled the statements which update internal data structures (like the |
| 62 | // local variable map) which could be used by subsequent statements. |
| 63 | if (!ContainsLabel(S)) { |
| 64 | // Verify that any decl statements were handled as simple, they may be in |
| 65 | // scope of subsequent reachable statements. |
| 66 | assert(!isa<DeclStmt>(*S) && "Unexpected DeclStmt!"); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | // Otherwise, make a new block to hold the code. |
| 71 | EnsureInsertPoint(); |
| 72 | } |
| 73 | |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 74 | // Generate a stoppoint if we are emitting debug info. |
| 75 | EmitStopPoint(S); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 76 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 77 | switch (S->getStmtClass()) { |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 78 | case Stmt::NoStmtClass: |
| 79 | case Stmt::CXXCatchStmtClass: |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 80 | case Stmt::SEHExceptStmtClass: |
| 81 | case Stmt::SEHFinallyStmtClass: |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 82 | case Stmt::MSDependentExistsStmtClass: |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 83 | llvm_unreachable("invalid statement class to emit generically"); |
| 84 | case Stmt::NullStmtClass: |
| 85 | case Stmt::CompoundStmtClass: |
| 86 | case Stmt::DeclStmtClass: |
| 87 | case Stmt::LabelStmtClass: |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 88 | case Stmt::AttributedStmtClass: |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 89 | case Stmt::GotoStmtClass: |
| 90 | case Stmt::BreakStmtClass: |
| 91 | case Stmt::ContinueStmtClass: |
| 92 | case Stmt::DefaultStmtClass: |
| 93 | case Stmt::CaseStmtClass: |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 94 | case Stmt::SEHLeaveStmtClass: |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 95 | llvm_unreachable("should have emitted these statements as simple"); |
Daniel Dunbar | cd5e60e | 2009-07-19 08:23:12 +0000 | [diff] [blame] | 96 | |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 97 | #define STMT(Type, Base) |
| 98 | #define ABSTRACT_STMT(Op) |
| 99 | #define EXPR(Type, Base) \ |
| 100 | case Stmt::Type##Class: |
| 101 | #include "clang/AST/StmtNodes.inc" |
John McCall | cd5b22e | 2011-01-12 03:41:02 +0000 | [diff] [blame] | 102 | { |
| 103 | // Remember the block we came in on. |
| 104 | llvm::BasicBlock *incoming = Builder.GetInsertBlock(); |
| 105 | assert(incoming && "expression emission must have an insertion point"); |
| 106 | |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 107 | EmitIgnoredExpr(cast<Expr>(S)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 108 | |
John McCall | cd5b22e | 2011-01-12 03:41:02 +0000 | [diff] [blame] | 109 | llvm::BasicBlock *outgoing = Builder.GetInsertBlock(); |
| 110 | assert(outgoing && "expression emission cleared block!"); |
| 111 | |
| 112 | // The expression emitters assume (reasonably!) that the insertion |
| 113 | // point is always set. To maintain that, the call-emission code |
| 114 | // for noreturn functions has to enter a new block with no |
| 115 | // predecessors. We want to kill that block and mark the current |
| 116 | // insertion point unreachable in the common case of a call like |
| 117 | // "exit();". Since expression emission doesn't otherwise create |
| 118 | // blocks with no predecessors, we can just test for that. |
| 119 | // However, we must be careful not to do this to our incoming |
| 120 | // block, because *statement* emission does sometimes create |
| 121 | // reachable blocks which will have no predecessors until later in |
| 122 | // the function. This occurs with, e.g., labels that are not |
| 123 | // reachable by fallthrough. |
| 124 | if (incoming != outgoing && outgoing->use_empty()) { |
| 125 | outgoing->eraseFromParent(); |
| 126 | Builder.ClearInsertionPoint(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 127 | } |
| 128 | break; |
John McCall | cd5b22e | 2011-01-12 03:41:02 +0000 | [diff] [blame] | 129 | } |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 130 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | case Stmt::IndirectGotoStmtClass: |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 132 | EmitIndirectGotoStmt(cast<IndirectGotoStmt>(*S)); break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 133 | |
| 134 | case Stmt::IfStmtClass: EmitIfStmt(cast<IfStmt>(*S)); break; |
| 135 | case Stmt::WhileStmtClass: EmitWhileStmt(cast<WhileStmt>(*S)); break; |
| 136 | case Stmt::DoStmtClass: EmitDoStmt(cast<DoStmt>(*S)); break; |
| 137 | case Stmt::ForStmtClass: EmitForStmt(cast<ForStmt>(*S)); break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 138 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 139 | case Stmt::ReturnStmtClass: EmitReturnStmt(cast<ReturnStmt>(*S)); break; |
Daniel Dunbar | a4275d1 | 2008-10-02 18:02:06 +0000 | [diff] [blame] | 140 | |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 141 | case Stmt::SwitchStmtClass: EmitSwitchStmt(cast<SwitchStmt>(*S)); break; |
Chad Rosier | d1a8d2e | 2012-08-28 21:11:24 +0000 | [diff] [blame] | 142 | case Stmt::GCCAsmStmtClass: // Intentional fall-through. |
| 143 | case Stmt::MSAsmStmtClass: EmitAsmStmt(cast<AsmStmt>(*S)); break; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 144 | case Stmt::CoroutineBodyStmtClass: |
| 145 | case Stmt::CoreturnStmtClass: |
| 146 | CGM.ErrorUnsupported(S, "coroutine"); |
| 147 | break; |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 148 | case Stmt::CapturedStmtClass: { |
| 149 | const CapturedStmt *CS = cast<CapturedStmt>(S); |
| 150 | EmitCapturedStmt(*CS, CS->getCapturedRegionKind()); |
| 151 | } |
Tareq A. Siraj | 051303c | 2013-04-16 18:53:08 +0000 | [diff] [blame] | 152 | break; |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 153 | case Stmt::ObjCAtTryStmtClass: |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 154 | EmitObjCAtTryStmt(cast<ObjCAtTryStmt>(*S)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | break; |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 156 | case Stmt::ObjCAtCatchStmtClass: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 157 | llvm_unreachable( |
| 158 | "@catch statements should be handled by EmitObjCAtTryStmt"); |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 159 | case Stmt::ObjCAtFinallyStmtClass: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 160 | llvm_unreachable( |
| 161 | "@finally statements should be handled by EmitObjCAtTryStmt"); |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 162 | case Stmt::ObjCAtThrowStmtClass: |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 163 | EmitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(*S)); |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 164 | break; |
| 165 | case Stmt::ObjCAtSynchronizedStmtClass: |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 166 | EmitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(*S)); |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 167 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 168 | case Stmt::ObjCForCollectionStmtClass: |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 169 | EmitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(*S)); |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 170 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 171 | case Stmt::ObjCAutoreleasePoolStmtClass: |
| 172 | EmitObjCAutoreleasePoolStmt(cast<ObjCAutoreleasePoolStmt>(*S)); |
| 173 | break; |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 174 | |
Anders Carlsson | 6815e94 | 2009-09-27 18:58:34 +0000 | [diff] [blame] | 175 | case Stmt::CXXTryStmtClass: |
| 176 | EmitCXXTryStmt(cast<CXXTryStmt>(*S)); |
| 177 | break; |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 178 | case Stmt::CXXForRangeStmtClass: |
| 179 | EmitCXXForRangeStmt(cast<CXXForRangeStmt>(*S)); |
Reid Kleckner | 98592d9 | 2013-09-16 21:46:30 +0000 | [diff] [blame] | 180 | break; |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 181 | case Stmt::SEHTryStmtClass: |
Reid Kleckner | 98592d9 | 2013-09-16 21:46:30 +0000 | [diff] [blame] | 182 | EmitSEHTryStmt(cast<SEHTryStmt>(*S)); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 183 | break; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 184 | case Stmt::OMPParallelDirectiveClass: |
| 185 | EmitOMPParallelDirective(cast<OMPParallelDirective>(*S)); |
| 186 | break; |
| 187 | case Stmt::OMPSimdDirectiveClass: |
| 188 | EmitOMPSimdDirective(cast<OMPSimdDirective>(*S)); |
| 189 | break; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 190 | case Stmt::OMPForDirectiveClass: |
| 191 | EmitOMPForDirective(cast<OMPForDirective>(*S)); |
| 192 | break; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 193 | case Stmt::OMPForSimdDirectiveClass: |
| 194 | EmitOMPForSimdDirective(cast<OMPForSimdDirective>(*S)); |
| 195 | break; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 196 | case Stmt::OMPSectionsDirectiveClass: |
| 197 | EmitOMPSectionsDirective(cast<OMPSectionsDirective>(*S)); |
| 198 | break; |
| 199 | case Stmt::OMPSectionDirectiveClass: |
| 200 | EmitOMPSectionDirective(cast<OMPSectionDirective>(*S)); |
| 201 | break; |
| 202 | case Stmt::OMPSingleDirectiveClass: |
| 203 | EmitOMPSingleDirective(cast<OMPSingleDirective>(*S)); |
| 204 | break; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 205 | case Stmt::OMPMasterDirectiveClass: |
| 206 | EmitOMPMasterDirective(cast<OMPMasterDirective>(*S)); |
| 207 | break; |
| 208 | case Stmt::OMPCriticalDirectiveClass: |
| 209 | EmitOMPCriticalDirective(cast<OMPCriticalDirective>(*S)); |
| 210 | break; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 211 | case Stmt::OMPParallelForDirectiveClass: |
| 212 | EmitOMPParallelForDirective(cast<OMPParallelForDirective>(*S)); |
| 213 | break; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 214 | case Stmt::OMPParallelForSimdDirectiveClass: |
| 215 | EmitOMPParallelForSimdDirective(cast<OMPParallelForSimdDirective>(*S)); |
| 216 | break; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 217 | case Stmt::OMPParallelSectionsDirectiveClass: |
| 218 | EmitOMPParallelSectionsDirective(cast<OMPParallelSectionsDirective>(*S)); |
| 219 | break; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 220 | case Stmt::OMPTaskDirectiveClass: |
| 221 | EmitOMPTaskDirective(cast<OMPTaskDirective>(*S)); |
| 222 | break; |
| 223 | case Stmt::OMPTaskyieldDirectiveClass: |
| 224 | EmitOMPTaskyieldDirective(cast<OMPTaskyieldDirective>(*S)); |
| 225 | break; |
| 226 | case Stmt::OMPBarrierDirectiveClass: |
| 227 | EmitOMPBarrierDirective(cast<OMPBarrierDirective>(*S)); |
| 228 | break; |
| 229 | case Stmt::OMPTaskwaitDirectiveClass: |
| 230 | EmitOMPTaskwaitDirective(cast<OMPTaskwaitDirective>(*S)); |
| 231 | break; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 232 | case Stmt::OMPTaskgroupDirectiveClass: |
| 233 | EmitOMPTaskgroupDirective(cast<OMPTaskgroupDirective>(*S)); |
| 234 | break; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 235 | case Stmt::OMPFlushDirectiveClass: |
| 236 | EmitOMPFlushDirective(cast<OMPFlushDirective>(*S)); |
| 237 | break; |
| 238 | case Stmt::OMPOrderedDirectiveClass: |
| 239 | EmitOMPOrderedDirective(cast<OMPOrderedDirective>(*S)); |
| 240 | break; |
| 241 | case Stmt::OMPAtomicDirectiveClass: |
| 242 | EmitOMPAtomicDirective(cast<OMPAtomicDirective>(*S)); |
| 243 | break; |
| 244 | case Stmt::OMPTargetDirectiveClass: |
| 245 | EmitOMPTargetDirective(cast<OMPTargetDirective>(*S)); |
| 246 | break; |
| 247 | case Stmt::OMPTeamsDirectiveClass: |
| 248 | EmitOMPTeamsDirective(cast<OMPTeamsDirective>(*S)); |
| 249 | break; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 250 | case Stmt::OMPCancellationPointDirectiveClass: |
| 251 | EmitOMPCancellationPointDirective(cast<OMPCancellationPointDirective>(*S)); |
| 252 | break; |
| 253 | case Stmt::OMPCancelDirectiveClass: |
| 254 | EmitOMPCancelDirective(cast<OMPCancelDirective>(*S)); |
| 255 | break; |
| 256 | case Stmt::OMPTargetDataDirectiveClass: |
| 257 | EmitOMPTargetDataDirective(cast<OMPTargetDataDirective>(*S)); |
| 258 | break; |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 259 | case Stmt::OMPTargetEnterDataDirectiveClass: |
| 260 | EmitOMPTargetEnterDataDirective(cast<OMPTargetEnterDataDirective>(*S)); |
| 261 | break; |
| 262 | case Stmt::OMPTargetExitDataDirectiveClass: |
| 263 | EmitOMPTargetExitDataDirective(cast<OMPTargetExitDataDirective>(*S)); |
| 264 | break; |
| 265 | case Stmt::OMPTargetParallelDirectiveClass: |
| 266 | EmitOMPTargetParallelDirective(cast<OMPTargetParallelDirective>(*S)); |
| 267 | break; |
| 268 | case Stmt::OMPTargetParallelForDirectiveClass: |
| 269 | EmitOMPTargetParallelForDirective(cast<OMPTargetParallelForDirective>(*S)); |
| 270 | break; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 271 | case Stmt::OMPTaskLoopDirectiveClass: |
| 272 | EmitOMPTaskLoopDirective(cast<OMPTaskLoopDirective>(*S)); |
| 273 | break; |
| 274 | case Stmt::OMPTaskLoopSimdDirectiveClass: |
| 275 | EmitOMPTaskLoopSimdDirective(cast<OMPTaskLoopSimdDirective>(*S)); |
| 276 | break; |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 277 | case Stmt::OMPDistributeDirectiveClass: |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 278 | EmitOMPDistributeDirective(cast<OMPDistributeDirective>(*S)); |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 279 | break; |
| 280 | case Stmt::OMPTargetUpdateDirectiveClass: |
| 281 | EmitOMPTargetUpdateDirective(cast<OMPTargetUpdateDirective>(*S)); |
| 282 | break; |
| 283 | case Stmt::OMPDistributeParallelForDirectiveClass: |
| 284 | EmitOMPDistributeParallelForDirective( |
| 285 | cast<OMPDistributeParallelForDirective>(*S)); |
| 286 | break; |
| 287 | case Stmt::OMPDistributeParallelForSimdDirectiveClass: |
| 288 | EmitOMPDistributeParallelForSimdDirective( |
| 289 | cast<OMPDistributeParallelForSimdDirective>(*S)); |
| 290 | break; |
| 291 | case Stmt::OMPDistributeSimdDirectiveClass: |
| 292 | EmitOMPDistributeSimdDirective(cast<OMPDistributeSimdDirective>(*S)); |
| 293 | break; |
| 294 | case Stmt::OMPTargetParallelForSimdDirectiveClass: |
| 295 | EmitOMPTargetParallelForSimdDirective( |
| 296 | cast<OMPTargetParallelForSimdDirective>(*S)); |
| 297 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 301 | bool CodeGenFunction::EmitSimpleStmt(const Stmt *S) { |
| 302 | switch (S->getStmtClass()) { |
| 303 | default: return false; |
| 304 | case Stmt::NullStmtClass: break; |
| 305 | case Stmt::CompoundStmtClass: EmitCompoundStmt(cast<CompoundStmt>(*S)); break; |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 306 | case Stmt::DeclStmtClass: EmitDeclStmt(cast<DeclStmt>(*S)); break; |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 307 | case Stmt::LabelStmtClass: EmitLabelStmt(cast<LabelStmt>(*S)); break; |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 308 | case Stmt::AttributedStmtClass: |
| 309 | EmitAttributedStmt(cast<AttributedStmt>(*S)); break; |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 310 | case Stmt::GotoStmtClass: EmitGotoStmt(cast<GotoStmt>(*S)); break; |
| 311 | case Stmt::BreakStmtClass: EmitBreakStmt(cast<BreakStmt>(*S)); break; |
| 312 | case Stmt::ContinueStmtClass: EmitContinueStmt(cast<ContinueStmt>(*S)); break; |
| 313 | case Stmt::DefaultStmtClass: EmitDefaultStmt(cast<DefaultStmt>(*S)); break; |
| 314 | case Stmt::CaseStmtClass: EmitCaseStmt(cast<CaseStmt>(*S)); break; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 315 | case Stmt::SEHLeaveStmtClass: EmitSEHLeaveStmt(cast<SEHLeaveStmt>(*S)); break; |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | return true; |
| 319 | } |
| 320 | |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 321 | /// EmitCompoundStmt - Emit a compound statement {..} node. If GetLast is true, |
| 322 | /// this captures the expression result of the last sub-statement and returns it |
| 323 | /// (for use by the statement expression extension). |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 324 | Address CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast, |
| 325 | AggValueSlot AggSlot) { |
Chris Lattner | 7d22bf0 | 2009-03-05 08:04:57 +0000 | [diff] [blame] | 326 | PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),S.getLBracLoc(), |
| 327 | "LLVM IR generation of compound statement ('{}')"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 328 | |
Eric Christopher | fdc5d56 | 2012-02-23 00:43:07 +0000 | [diff] [blame] | 329 | // Keep track of the current cleanup stack depth, including debug scopes. |
| 330 | LexicalScope Scope(*this, S.getSourceRange()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 331 | |
David Blaikie | a650485 | 2013-01-26 22:16:26 +0000 | [diff] [blame] | 332 | return EmitCompoundStmtWithoutScope(S, GetLast, AggSlot); |
| 333 | } |
| 334 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 335 | Address |
Eli Friedman | 2ac2fa7 | 2013-06-10 22:04:49 +0000 | [diff] [blame] | 336 | CodeGenFunction::EmitCompoundStmtWithoutScope(const CompoundStmt &S, |
| 337 | bool GetLast, |
| 338 | AggValueSlot AggSlot) { |
David Blaikie | a650485 | 2013-01-26 22:16:26 +0000 | [diff] [blame] | 339 | |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 340 | for (CompoundStmt::const_body_iterator I = S.body_begin(), |
| 341 | E = S.body_end()-GetLast; I != E; ++I) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 342 | EmitStmt(*I); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 343 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 344 | Address RetAlloca = Address::invalid(); |
Eli Friedman | 2ac2fa7 | 2013-06-10 22:04:49 +0000 | [diff] [blame] | 345 | if (GetLast) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 346 | // 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] | 347 | // at the end of a statement expression, they yield the value of their |
| 348 | // subexpression. Handle this by walking through all labels we encounter, |
| 349 | // emitting them before we evaluate the subexpr. |
| 350 | const Stmt *LastStmt = S.body_back(); |
| 351 | while (const LabelStmt *LS = dyn_cast<LabelStmt>(LastStmt)) { |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 352 | EmitLabel(LS->getDecl()); |
Anders Carlsson | 17d28a3 | 2008-12-12 05:52:00 +0000 | [diff] [blame] | 353 | LastStmt = LS->getSubStmt(); |
| 354 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 355 | |
Anders Carlsson | 17d28a3 | 2008-12-12 05:52:00 +0000 | [diff] [blame] | 356 | EnsureInsertPoint(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 357 | |
Eli Friedman | 2ac2fa7 | 2013-06-10 22:04:49 +0000 | [diff] [blame] | 358 | QualType ExprTy = cast<Expr>(LastStmt)->getType(); |
| 359 | if (hasAggregateEvaluationKind(ExprTy)) { |
| 360 | EmitAggExpr(cast<Expr>(LastStmt), AggSlot); |
| 361 | } else { |
| 362 | // We can't return an RValue here because there might be cleanups at |
| 363 | // the end of the StmtExpr. Because of that, we have to emit the result |
| 364 | // here into a temporary alloca. |
| 365 | RetAlloca = CreateMemTemp(ExprTy); |
| 366 | EmitAnyExprToMem(cast<Expr>(LastStmt), RetAlloca, Qualifiers(), |
| 367 | /*IsInit*/false); |
| 368 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 369 | |
Anders Carlsson | 17d28a3 | 2008-12-12 05:52:00 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Eli Friedman | 2ac2fa7 | 2013-06-10 22:04:49 +0000 | [diff] [blame] | 372 | return RetAlloca; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Daniel Dunbar | aa5bd87 | 2009-04-01 04:37:47 +0000 | [diff] [blame] | 375 | void CodeGenFunction::SimplifyForwardingBlocks(llvm::BasicBlock *BB) { |
| 376 | llvm::BranchInst *BI = dyn_cast<llvm::BranchInst>(BB->getTerminator()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 377 | |
Daniel Dunbar | aa5bd87 | 2009-04-01 04:37:47 +0000 | [diff] [blame] | 378 | // If there is a cleanup stack, then we it isn't worth trying to |
| 379 | // simplify this block (we would need to remove it from the scope map |
| 380 | // and cleanup entry). |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 381 | if (!EHStack.empty()) |
Daniel Dunbar | aa5bd87 | 2009-04-01 04:37:47 +0000 | [diff] [blame] | 382 | return; |
| 383 | |
| 384 | // Can only simplify direct branches. |
| 385 | if (!BI || !BI->isUnconditional()) |
| 386 | return; |
| 387 | |
Eli Friedman | 3d7c780 | 2012-10-26 23:23:35 +0000 | [diff] [blame] | 388 | // Can only simplify empty blocks. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 389 | if (BI->getIterator() != BB->begin()) |
Eli Friedman | 3d7c780 | 2012-10-26 23:23:35 +0000 | [diff] [blame] | 390 | return; |
| 391 | |
Daniel Dunbar | aa5bd87 | 2009-04-01 04:37:47 +0000 | [diff] [blame] | 392 | BB->replaceAllUsesWith(BI->getSuccessor(0)); |
| 393 | BI->eraseFromParent(); |
| 394 | BB->eraseFromParent(); |
| 395 | } |
| 396 | |
Daniel Dunbar | a0c21a8 | 2008-11-13 01:24:05 +0000 | [diff] [blame] | 397 | void CodeGenFunction::EmitBlock(llvm::BasicBlock *BB, bool IsFinished) { |
John McCall | 548ce5e | 2010-04-21 11:18:06 +0000 | [diff] [blame] | 398 | llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); |
| 399 | |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 400 | // Fall out of the current block (if necessary). |
| 401 | EmitBranch(BB); |
Daniel Dunbar | a0c21a8 | 2008-11-13 01:24:05 +0000 | [diff] [blame] | 402 | |
| 403 | if (IsFinished && BB->use_empty()) { |
| 404 | delete BB; |
| 405 | return; |
| 406 | } |
| 407 | |
John McCall | 839cbaa | 2010-04-21 10:29:06 +0000 | [diff] [blame] | 408 | // Place the block after the current block, if possible, or else at |
| 409 | // the end of the function. |
John McCall | 548ce5e | 2010-04-21 11:18:06 +0000 | [diff] [blame] | 410 | if (CurBB && CurBB->getParent()) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 411 | CurFn->getBasicBlockList().insertAfter(CurBB->getIterator(), BB); |
John McCall | 839cbaa | 2010-04-21 10:29:06 +0000 | [diff] [blame] | 412 | else |
| 413 | CurFn->getBasicBlockList().push_back(BB); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 414 | Builder.SetInsertPoint(BB); |
| 415 | } |
| 416 | |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 417 | void CodeGenFunction::EmitBranch(llvm::BasicBlock *Target) { |
| 418 | // Emit a branch from the current block to the target one if this |
| 419 | // was a real block. If this was just a fall-through block after a |
| 420 | // terminator, don't emit it. |
| 421 | llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); |
| 422 | |
| 423 | if (!CurBB || CurBB->getTerminator()) { |
| 424 | // If there is no insert point or the previous block is already |
| 425 | // terminated, don't touch it. |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 426 | } else { |
| 427 | // Otherwise, create a fall-through branch. |
| 428 | Builder.CreateBr(Target); |
| 429 | } |
Daniel Dunbar | 5e08ad3 | 2008-11-11 22:06:59 +0000 | [diff] [blame] | 430 | |
| 431 | Builder.ClearInsertionPoint(); |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 432 | } |
| 433 | |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 434 | void CodeGenFunction::EmitBlockAfterUses(llvm::BasicBlock *block) { |
| 435 | bool inserted = false; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 436 | for (llvm::User *u : block->users()) { |
| 437 | if (llvm::Instruction *insn = dyn_cast<llvm::Instruction>(u)) { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 438 | CurFn->getBasicBlockList().insertAfter(insn->getParent()->getIterator(), |
| 439 | block); |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 440 | inserted = true; |
| 441 | break; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | if (!inserted) |
| 446 | CurFn->getBasicBlockList().push_back(block); |
| 447 | |
| 448 | Builder.SetInsertPoint(block); |
| 449 | } |
| 450 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 451 | CodeGenFunction::JumpDest |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 452 | CodeGenFunction::getJumpDestForLabel(const LabelDecl *D) { |
| 453 | JumpDest &Dest = LabelMap[D]; |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 454 | if (Dest.isValid()) return Dest; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 455 | |
| 456 | // Create, but don't insert, the new block. |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 457 | Dest = JumpDest(createBasicBlock(D->getName()), |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 458 | EHScopeStack::stable_iterator::invalid(), |
| 459 | NextCleanupDestIndex++); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 460 | return Dest; |
| 461 | } |
| 462 | |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 463 | void CodeGenFunction::EmitLabel(const LabelDecl *D) { |
Nadav Rotem | 495cfa4 | 2013-03-23 06:43:35 +0000 | [diff] [blame] | 464 | // Add this label to the current lexical scope if we're within any |
| 465 | // normal cleanups. Jumps "in" to this label --- when permitted by |
| 466 | // the language --- may need to be routed around such cleanups. |
| 467 | if (EHStack.hasNormalCleanups() && CurLexicalScope) |
| 468 | CurLexicalScope->addLabel(D); |
| 469 | |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 470 | JumpDest &Dest = LabelMap[D]; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 471 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 472 | // 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] | 473 | // ahead and create a destination at the current scope. |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 474 | if (!Dest.isValid()) { |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 475 | Dest = getJumpDestInCurrentScope(D->getName()); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 476 | |
| 477 | // Otherwise, we need to give this label a target depth and remove |
| 478 | // it from the branch-fixups list. |
| 479 | } else { |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 480 | assert(!Dest.getScopeDepth().isValid() && "already emitted label!"); |
Nadav Rotem | 495cfa4 | 2013-03-23 06:43:35 +0000 | [diff] [blame] | 481 | Dest.setScopeDepth(EHStack.stable_begin()); |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 482 | ResolveBranchFixups(Dest.getBlock()); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 483 | } |
| 484 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 485 | EmitBlock(Dest.getBlock()); |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 486 | incrementProfileCounter(D->getStmt()); |
Chris Lattner | 91d723d | 2008-07-26 20:23:23 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Nadav Rotem | 495cfa4 | 2013-03-23 06:43:35 +0000 | [diff] [blame] | 489 | /// Change the cleanup scope of the labels in this lexical scope to |
| 490 | /// match the scope of the enclosing context. |
| 491 | void CodeGenFunction::LexicalScope::rescopeLabels() { |
| 492 | assert(!Labels.empty()); |
| 493 | EHScopeStack::stable_iterator innermostScope |
| 494 | = CGF.EHStack.getInnermostNormalCleanup(); |
| 495 | |
| 496 | // Change the scope depth of all the labels. |
| 497 | for (SmallVectorImpl<const LabelDecl*>::const_iterator |
| 498 | i = Labels.begin(), e = Labels.end(); i != e; ++i) { |
| 499 | assert(CGF.LabelMap.count(*i)); |
| 500 | JumpDest &dest = CGF.LabelMap.find(*i)->second; |
| 501 | assert(dest.getScopeDepth().isValid()); |
| 502 | assert(innermostScope.encloses(dest.getScopeDepth())); |
| 503 | dest.setScopeDepth(innermostScope); |
| 504 | } |
| 505 | |
| 506 | // Reparent the labels if the new scope also has cleanups. |
| 507 | if (innermostScope != EHScopeStack::stable_end() && ParentScope) { |
| 508 | ParentScope->Labels.append(Labels.begin(), Labels.end()); |
| 509 | } |
| 510 | } |
| 511 | |
Chris Lattner | 91d723d | 2008-07-26 20:23:23 +0000 | [diff] [blame] | 512 | |
| 513 | void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) { |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 514 | EmitLabel(S.getDecl()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 515 | EmitStmt(S.getSubStmt()); |
| 516 | } |
| 517 | |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 518 | void CodeGenFunction::EmitAttributedStmt(const AttributedStmt &S) { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 519 | const Stmt *SubStmt = S.getSubStmt(); |
| 520 | switch (SubStmt->getStmtClass()) { |
| 521 | case Stmt::DoStmtClass: |
| 522 | EmitDoStmt(cast<DoStmt>(*SubStmt), S.getAttrs()); |
| 523 | break; |
| 524 | case Stmt::ForStmtClass: |
| 525 | EmitForStmt(cast<ForStmt>(*SubStmt), S.getAttrs()); |
| 526 | break; |
| 527 | case Stmt::WhileStmtClass: |
| 528 | EmitWhileStmt(cast<WhileStmt>(*SubStmt), S.getAttrs()); |
| 529 | break; |
| 530 | case Stmt::CXXForRangeStmtClass: |
| 531 | EmitCXXForRangeStmt(cast<CXXForRangeStmt>(*SubStmt), S.getAttrs()); |
| 532 | break; |
| 533 | default: |
| 534 | EmitStmt(SubStmt); |
| 535 | } |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 538 | void CodeGenFunction::EmitGotoStmt(const GotoStmt &S) { |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 539 | // If this code is reachable then emit a stop point (if generating |
| 540 | // debug info). We have to do this ourselves because we are on the |
| 541 | // "simple" statement path. |
| 542 | if (HaveInsertPoint()) |
| 543 | EmitStopPoint(&S); |
Mike Stump | 36a2ada | 2009-02-07 12:52:26 +0000 | [diff] [blame] | 544 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 545 | EmitBranchThroughCleanup(getJumpDestForLabel(S.getLabel())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 548 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 549 | void CodeGenFunction::EmitIndirectGotoStmt(const IndirectGotoStmt &S) { |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 550 | if (const LabelDecl *Target = S.getConstantTarget()) { |
John McCall | 95c225d | 2010-10-28 08:53:48 +0000 | [diff] [blame] | 551 | EmitBranchThroughCleanup(getJumpDestForLabel(Target)); |
| 552 | return; |
| 553 | } |
| 554 | |
Chris Lattner | 49c952f | 2009-11-06 18:10:47 +0000 | [diff] [blame] | 555 | // Ensure that we have an i8* for our PHI node. |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 556 | llvm::Value *V = Builder.CreateBitCast(EmitScalarExpr(S.getTarget()), |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 557 | Int8PtrTy, "addr"); |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 558 | llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 559 | |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 560 | // Get the basic block for the indirect goto. |
| 561 | llvm::BasicBlock *IndGotoBB = GetIndirectGotoBlock(); |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 562 | |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 563 | // The first instruction in the block has to be the PHI for the switch dest, |
| 564 | // add an entry for this branch. |
| 565 | cast<llvm::PHINode>(IndGotoBB->begin())->addIncoming(V, CurBB); |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 566 | |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 567 | EmitBranch(IndGotoBB); |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Chris Lattner | 62b72f6 | 2008-11-11 07:24:28 +0000 | [diff] [blame] | 570 | void CodeGenFunction::EmitIfStmt(const IfStmt &S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 571 | // C99 6.8.4.1: The first substatement is executed if the expression compares |
| 572 | // unequal to 0. The condition must be a scalar type. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 573 | LexicalScope ConditionScope(*this, S.getCond()->getSourceRange()); |
Adrian Prantl | 8d37858 | 2013-06-08 00:16:55 +0000 | [diff] [blame] | 574 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 575 | if (S.getInit()) |
| 576 | EmitStmt(S.getInit()); |
| 577 | |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 578 | if (S.getConditionVariable()) |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 579 | EmitAutoVarDecl(*S.getConditionVariable()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 580 | |
Chris Lattner | 9bc47e2 | 2008-11-12 07:46:33 +0000 | [diff] [blame] | 581 | // If the condition constant folds and can be elided, try to avoid emitting |
| 582 | // the condition and the dead arm of the if/else. |
Chris Lattner | c2c9001 | 2011-02-27 23:02:32 +0000 | [diff] [blame] | 583 | bool CondConstant; |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 584 | if (ConstantFoldsToSimpleInteger(S.getCond(), CondConstant, |
| 585 | S.isConstexpr())) { |
Chris Lattner | 62b72f6 | 2008-11-11 07:24:28 +0000 | [diff] [blame] | 586 | // Figure out which block (then or else) is executed. |
Chris Lattner | c2c9001 | 2011-02-27 23:02:32 +0000 | [diff] [blame] | 587 | const Stmt *Executed = S.getThen(); |
| 588 | const Stmt *Skipped = S.getElse(); |
| 589 | if (!CondConstant) // Condition false? |
Chris Lattner | 62b72f6 | 2008-11-11 07:24:28 +0000 | [diff] [blame] | 590 | std::swap(Executed, Skipped); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 591 | |
Chris Lattner | 62b72f6 | 2008-11-11 07:24:28 +0000 | [diff] [blame] | 592 | // If the skipped block has no labels in it, just emit the executed block. |
| 593 | // This avoids emitting dead code and simplifies the CFG substantially. |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 594 | if (S.isConstexpr() || !ContainsLabel(Skipped)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 595 | if (CondConstant) |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 596 | incrementProfileCounter(&S); |
Douglas Gregor | 01234bb | 2009-11-24 16:43:22 +0000 | [diff] [blame] | 597 | if (Executed) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 598 | RunCleanupsScope ExecutedScope(*this); |
Chris Lattner | 62b72f6 | 2008-11-11 07:24:28 +0000 | [diff] [blame] | 599 | EmitStmt(Executed); |
Douglas Gregor | 01234bb | 2009-11-24 16:43:22 +0000 | [diff] [blame] | 600 | } |
Chris Lattner | 62b72f6 | 2008-11-11 07:24:28 +0000 | [diff] [blame] | 601 | return; |
| 602 | } |
| 603 | } |
Chris Lattner | 9bc47e2 | 2008-11-12 07:46:33 +0000 | [diff] [blame] | 604 | |
| 605 | // Otherwise, the condition did not fold, or we couldn't elide it. Just emit |
| 606 | // the conditional branch. |
Daniel Dunbar | 781d7ca | 2008-11-13 00:47:57 +0000 | [diff] [blame] | 607 | llvm::BasicBlock *ThenBlock = createBasicBlock("if.then"); |
| 608 | llvm::BasicBlock *ContBlock = createBasicBlock("if.end"); |
| 609 | llvm::BasicBlock *ElseBlock = ContBlock; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 610 | if (S.getElse()) |
Daniel Dunbar | 781d7ca | 2008-11-13 00:47:57 +0000 | [diff] [blame] | 611 | ElseBlock = createBasicBlock("if.else"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 612 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 613 | EmitBranchOnBoolExpr(S.getCond(), ThenBlock, ElseBlock, |
| 614 | getProfileCount(S.getThen())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 615 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 616 | // Emit the 'then' code. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 617 | EmitBlock(ThenBlock); |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 618 | incrementProfileCounter(&S); |
Douglas Gregor | 01234bb | 2009-11-24 16:43:22 +0000 | [diff] [blame] | 619 | { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 620 | RunCleanupsScope ThenScope(*this); |
Douglas Gregor | 01234bb | 2009-11-24 16:43:22 +0000 | [diff] [blame] | 621 | EmitStmt(S.getThen()); |
| 622 | } |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 623 | { |
| 624 | auto CurBlock = Builder.GetInsertBlock(); |
| 625 | EmitBranch(ContBlock); |
| 626 | // Eliminate any empty blocks that may have been created by nested |
| 627 | // control flow statements in the 'then' clause. |
| 628 | if (CurBlock) |
| 629 | SimplifyForwardingBlocks(CurBlock); |
| 630 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 631 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 632 | // Emit the 'else' code if present. |
| 633 | if (const Stmt *Else = S.getElse()) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 634 | { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 635 | // There is no need to emit line number for an unconditional branch. |
| 636 | auto NL = ApplyDebugLocation::CreateEmpty(*this); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 637 | EmitBlock(ElseBlock); |
| 638 | } |
Douglas Gregor | 01234bb | 2009-11-24 16:43:22 +0000 | [diff] [blame] | 639 | { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 640 | RunCleanupsScope ElseScope(*this); |
Douglas Gregor | 01234bb | 2009-11-24 16:43:22 +0000 | [diff] [blame] | 641 | EmitStmt(Else); |
| 642 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 643 | { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 644 | // There is no need to emit line number for an unconditional branch. |
| 645 | auto NL = ApplyDebugLocation::CreateEmpty(*this); |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 646 | auto CurBlock = Builder.GetInsertBlock(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 647 | EmitBranch(ContBlock); |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 648 | // Eliminate any empty blocks that may have been created by nested |
| 649 | // control flow statements emitted in the 'else' clause. |
| 650 | if (CurBlock) |
| 651 | SimplifyForwardingBlocks(CurBlock); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 652 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 653 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 654 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 655 | // Emit the continuation block for code after the if. |
Daniel Dunbar | c22d665 | 2008-11-13 01:54:24 +0000 | [diff] [blame] | 656 | EmitBlock(ContBlock, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 659 | void CodeGenFunction::EmitWhileStmt(const WhileStmt &S, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 660 | ArrayRef<const Attr *> WhileAttrs) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 661 | // Emit the header for the loop, which will also become |
| 662 | // the continue target. |
| 663 | JumpDest LoopHeader = getJumpDestInCurrentScope("while.cond"); |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 664 | EmitBlock(LoopHeader.getBlock()); |
Mike Stump | 72cac2c | 2009-02-07 18:08:12 +0000 | [diff] [blame] | 665 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 666 | LoopStack.push(LoopHeader.getBlock(), CGM.getContext(), WhileAttrs, |
| 667 | Builder.getCurrentDebugLocation()); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 668 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 669 | // Create an exit block for when the condition fails, which will |
| 670 | // also become the break target. |
| 671 | JumpDest LoopExit = getJumpDestInCurrentScope("while.end"); |
Mike Stump | 72cac2c | 2009-02-07 18:08:12 +0000 | [diff] [blame] | 672 | |
| 673 | // Store the blocks to use for break and continue. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 674 | BreakContinueStack.push_back(BreakContinue(LoopExit, LoopHeader)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 675 | |
Douglas Gregor | 5656e14 | 2009-11-24 21:15:44 +0000 | [diff] [blame] | 676 | // C++ [stmt.while]p2: |
| 677 | // When the condition of a while statement is a declaration, the |
| 678 | // scope of the variable that is declared extends from its point |
| 679 | // of declaration (3.3.2) to the end of the while statement. |
| 680 | // [...] |
| 681 | // The object created in a condition is destroyed and created |
| 682 | // with each iteration of the loop. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 683 | RunCleanupsScope ConditionScope(*this); |
Douglas Gregor | 5656e14 | 2009-11-24 21:15:44 +0000 | [diff] [blame] | 684 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 685 | if (S.getConditionVariable()) |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 686 | EmitAutoVarDecl(*S.getConditionVariable()); |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 687 | |
Mike Stump | 16b1620 | 2009-02-07 17:18:33 +0000 | [diff] [blame] | 688 | // Evaluate the conditional in the while header. C99 6.8.5.1: The |
| 689 | // evaluation of the controlling expression takes place before each |
| 690 | // execution of the loop body. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 691 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 692 | |
Devang Patel | 2c30d8f | 2007-10-09 20:51:27 +0000 | [diff] [blame] | 693 | // while(1) is common, avoid extra exit blocks. Be sure |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 694 | // to correctly handle break/continue though. |
Devang Patel | 2c30d8f | 2007-10-09 20:51:27 +0000 | [diff] [blame] | 695 | bool EmitBoolCondBranch = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 696 | if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal)) |
Devang Patel | 2c30d8f | 2007-10-09 20:51:27 +0000 | [diff] [blame] | 697 | if (C->isOne()) |
| 698 | EmitBoolCondBranch = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 699 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 700 | // As long as the condition is true, go to the loop body. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 701 | llvm::BasicBlock *LoopBody = createBasicBlock("while.body"); |
| 702 | if (EmitBoolCondBranch) { |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 703 | llvm::BasicBlock *ExitBlock = LoopExit.getBlock(); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 704 | if (ConditionScope.requiresCleanups()) |
| 705 | ExitBlock = createBasicBlock("while.exit"); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 706 | Builder.CreateCondBr( |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 707 | BoolCondVal, LoopBody, ExitBlock, |
| 708 | createProfileWeightsForLoop(S.getCond(), getProfileCount(S.getBody()))); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 709 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 710 | if (ExitBlock != LoopExit.getBlock()) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 711 | EmitBlock(ExitBlock); |
| 712 | EmitBranchThroughCleanup(LoopExit); |
| 713 | } |
| 714 | } |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 715 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 716 | // Emit the loop body. We have to emit this in a cleanup scope |
| 717 | // because it might be a singleton DeclStmt. |
Douglas Gregor | 5656e14 | 2009-11-24 21:15:44 +0000 | [diff] [blame] | 718 | { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 719 | RunCleanupsScope BodyScope(*this); |
Douglas Gregor | 5656e14 | 2009-11-24 21:15:44 +0000 | [diff] [blame] | 720 | EmitBlock(LoopBody); |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 721 | incrementProfileCounter(&S); |
Douglas Gregor | 5656e14 | 2009-11-24 21:15:44 +0000 | [diff] [blame] | 722 | EmitStmt(S.getBody()); |
| 723 | } |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 724 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 725 | BreakContinueStack.pop_back(); |
| 726 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 727 | // Immediately force cleanup. |
| 728 | ConditionScope.ForceCleanup(); |
Douglas Gregor | 5656e14 | 2009-11-24 21:15:44 +0000 | [diff] [blame] | 729 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 730 | EmitStopPoint(&S); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 731 | // Branch to the loop header again. |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 732 | EmitBranch(LoopHeader.getBlock()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 733 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 734 | LoopStack.pop(); |
| 735 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 736 | // Emit the exit block. |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 737 | EmitBlock(LoopExit.getBlock(), true); |
Douglas Gregor | 5656e14 | 2009-11-24 21:15:44 +0000 | [diff] [blame] | 738 | |
Daniel Dunbar | aa5bd87 | 2009-04-01 04:37:47 +0000 | [diff] [blame] | 739 | // The LoopHeader typically is just a branch if we skipped emitting |
| 740 | // a branch, try to erase it. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 741 | if (!EmitBoolCondBranch) |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 742 | SimplifyForwardingBlocks(LoopHeader.getBlock()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 745 | void CodeGenFunction::EmitDoStmt(const DoStmt &S, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 746 | ArrayRef<const Attr *> DoAttrs) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 747 | JumpDest LoopExit = getJumpDestInCurrentScope("do.end"); |
| 748 | JumpDest LoopCond = getJumpDestInCurrentScope("do.cond"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 749 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 750 | uint64_t ParentCount = getCurrentProfileCount(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 751 | |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 752 | // Store the blocks to use for break and continue. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 753 | BreakContinueStack.push_back(BreakContinue(LoopExit, LoopCond)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 754 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 755 | // Emit the body of the loop. |
| 756 | llvm::BasicBlock *LoopBody = createBasicBlock("do.body"); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 757 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 758 | LoopStack.push(LoopBody, CGM.getContext(), DoAttrs, |
| 759 | Builder.getCurrentDebugLocation()); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 760 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 761 | EmitBlockWithFallThrough(LoopBody, &S); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 762 | { |
| 763 | RunCleanupsScope BodyScope(*this); |
| 764 | EmitStmt(S.getBody()); |
| 765 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 766 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 767 | EmitBlock(LoopCond.getBlock()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 768 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 769 | // C99 6.8.5.2: "The evaluation of the controlling expression takes place |
| 770 | // after each execution of the loop body." |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 771 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 772 | // Evaluate the conditional in the while header. |
| 773 | // C99 6.8.5p2/p4: The first substatement is executed if the expression |
| 774 | // compares unequal to 0. The condition must be a scalar type. |
| 775 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
Devang Patel | 05f6e6b | 2007-10-09 20:33:39 +0000 | [diff] [blame] | 776 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 777 | BreakContinueStack.pop_back(); |
| 778 | |
Devang Patel | 05f6e6b | 2007-10-09 20:33:39 +0000 | [diff] [blame] | 779 | // "do {} while (0)" is common in macros, avoid extra blocks. Be sure |
| 780 | // to correctly handle break/continue though. |
| 781 | bool EmitBoolCondBranch = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 782 | if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal)) |
Devang Patel | 05f6e6b | 2007-10-09 20:33:39 +0000 | [diff] [blame] | 783 | if (C->isZero()) |
| 784 | EmitBoolCondBranch = false; |
| 785 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 786 | // As long as the condition is true, iterate the loop. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 787 | if (EmitBoolCondBranch) { |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 788 | uint64_t BackedgeCount = getProfileCount(S.getBody()) - ParentCount; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 789 | Builder.CreateCondBr( |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 790 | BoolCondVal, LoopBody, LoopExit.getBlock(), |
| 791 | createProfileWeightsForLoop(S.getCond(), BackedgeCount)); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 792 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 793 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 794 | LoopStack.pop(); |
| 795 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 796 | // Emit the exit block. |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 797 | EmitBlock(LoopExit.getBlock()); |
Devang Patel | 05f6e6b | 2007-10-09 20:33:39 +0000 | [diff] [blame] | 798 | |
Daniel Dunbar | aa5bd87 | 2009-04-01 04:37:47 +0000 | [diff] [blame] | 799 | // The DoCond block typically is just a branch if we skipped |
| 800 | // emitting a branch, try to erase it. |
| 801 | if (!EmitBoolCondBranch) |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 802 | SimplifyForwardingBlocks(LoopCond.getBlock()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 805 | void CodeGenFunction::EmitForStmt(const ForStmt &S, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 806 | ArrayRef<const Attr *> ForAttrs) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 807 | JumpDest LoopExit = getJumpDestInCurrentScope("for.end"); |
| 808 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 809 | LexicalScope ForScope(*this, S.getSourceRange()); |
Devang Patel | 0554e0e | 2010-08-25 00:28:56 +0000 | [diff] [blame] | 810 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 811 | llvm::DebugLoc DL = Builder.getCurrentDebugLocation(); |
| 812 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 813 | // Evaluate the first part before the loop. |
| 814 | if (S.getInit()) |
| 815 | EmitStmt(S.getInit()); |
| 816 | |
| 817 | // Start the loop with a block that tests the condition. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 818 | // If there's an increment, the continue scope will be overwritten |
| 819 | // later. |
| 820 | JumpDest Continue = getJumpDestInCurrentScope("for.cond"); |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 821 | llvm::BasicBlock *CondBlock = Continue.getBlock(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 822 | EmitBlock(CondBlock); |
| 823 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 824 | LoopStack.push(CondBlock, CGM.getContext(), ForAttrs, DL); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 825 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 826 | // If the for loop doesn't have an increment we can just use the |
| 827 | // condition as the continue block. Otherwise we'll need to create |
| 828 | // a block for it (in the current scope, i.e. in the scope of the |
| 829 | // condition), and that we will become our continue block. |
| 830 | if (S.getInc()) |
| 831 | Continue = getJumpDestInCurrentScope("for.inc"); |
| 832 | |
| 833 | // Store the blocks to use for break and continue. |
| 834 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
| 835 | |
Douglas Gregor | d975206 | 2009-11-25 01:51:31 +0000 | [diff] [blame] | 836 | // Create a cleanup scope for the condition variable cleanups. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 837 | LexicalScope ConditionScope(*this, S.getSourceRange()); |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 838 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 839 | if (S.getCond()) { |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 840 | // If the for statement has a condition scope, emit the local variable |
| 841 | // declaration. |
Douglas Gregor | d975206 | 2009-11-25 01:51:31 +0000 | [diff] [blame] | 842 | if (S.getConditionVariable()) { |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 843 | EmitAutoVarDecl(*S.getConditionVariable()); |
Douglas Gregor | d975206 | 2009-11-25 01:51:31 +0000 | [diff] [blame] | 844 | } |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 845 | |
Justin Bogner | fd93e4a | 2013-11-04 16:13:18 +0000 | [diff] [blame] | 846 | llvm::BasicBlock *ExitBlock = LoopExit.getBlock(); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 847 | // If there are any cleanups between here and the loop-exit scope, |
| 848 | // create a block to stage a loop exit along. |
| 849 | if (ForScope.requiresCleanups()) |
| 850 | ExitBlock = createBasicBlock("for.cond.cleanup"); |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 851 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 852 | // As long as the condition is true, iterate the loop. |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 853 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 854 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 855 | // C99 6.8.5p2/p4: The first substatement is executed if the expression |
| 856 | // compares unequal to 0. The condition must be a scalar type. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 857 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 858 | Builder.CreateCondBr( |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 859 | BoolCondVal, ForBody, ExitBlock, |
| 860 | createProfileWeightsForLoop(S.getCond(), getProfileCount(S.getBody()))); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 861 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 862 | if (ExitBlock != LoopExit.getBlock()) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 863 | EmitBlock(ExitBlock); |
| 864 | EmitBranchThroughCleanup(LoopExit); |
| 865 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 866 | |
| 867 | EmitBlock(ForBody); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 868 | } else { |
| 869 | // Treat it as a non-zero constant. Don't even create a new block for the |
| 870 | // body, just fall into it. |
| 871 | } |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 872 | incrementProfileCounter(&S); |
Mike Stump | 3e9da66 | 2009-02-07 23:02:10 +0000 | [diff] [blame] | 873 | |
Douglas Gregor | d975206 | 2009-11-25 01:51:31 +0000 | [diff] [blame] | 874 | { |
| 875 | // Create a separate cleanup scope for the body, in case it is not |
| 876 | // a compound statement. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 877 | RunCleanupsScope BodyScope(*this); |
Douglas Gregor | d975206 | 2009-11-25 01:51:31 +0000 | [diff] [blame] | 878 | EmitStmt(S.getBody()); |
| 879 | } |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 880 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 881 | // If there is an increment, emit it next. |
Daniel Dunbar | ad12b6d | 2008-09-28 00:19:22 +0000 | [diff] [blame] | 882 | if (S.getInc()) { |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 883 | EmitBlock(Continue.getBlock()); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 884 | EmitStmt(S.getInc()); |
Daniel Dunbar | ad12b6d | 2008-09-28 00:19:22 +0000 | [diff] [blame] | 885 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 886 | |
Douglas Gregor | 45d3fe1 | 2010-05-21 18:36:48 +0000 | [diff] [blame] | 887 | BreakContinueStack.pop_back(); |
Douglas Gregor | d975206 | 2009-11-25 01:51:31 +0000 | [diff] [blame] | 888 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 889 | ConditionScope.ForceCleanup(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 890 | |
| 891 | EmitStopPoint(&S); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 892 | EmitBranch(CondBlock); |
| 893 | |
| 894 | ForScope.ForceCleanup(); |
| 895 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 896 | LoopStack.pop(); |
| 897 | |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 898 | // Emit the fall-through block. |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 899 | EmitBlock(LoopExit.getBlock(), true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 902 | void |
| 903 | CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 904 | ArrayRef<const Attr *> ForAttrs) { |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 905 | JumpDest LoopExit = getJumpDestInCurrentScope("for.end"); |
| 906 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 907 | LexicalScope ForScope(*this, S.getSourceRange()); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 908 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 909 | llvm::DebugLoc DL = Builder.getCurrentDebugLocation(); |
| 910 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 911 | // Evaluate the first pieces before the loop. |
| 912 | EmitStmt(S.getRangeStmt()); |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 913 | EmitStmt(S.getBeginStmt()); |
| 914 | EmitStmt(S.getEndStmt()); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 915 | |
| 916 | // Start the loop with a block that tests the condition. |
| 917 | // If there's an increment, the continue scope will be overwritten |
| 918 | // later. |
| 919 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 920 | EmitBlock(CondBlock); |
| 921 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 922 | LoopStack.push(CondBlock, CGM.getContext(), ForAttrs, DL); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 923 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 924 | // If there are any cleanups between here and the loop-exit scope, |
| 925 | // create a block to stage a loop exit along. |
| 926 | llvm::BasicBlock *ExitBlock = LoopExit.getBlock(); |
| 927 | if (ForScope.requiresCleanups()) |
| 928 | ExitBlock = createBasicBlock("for.cond.cleanup"); |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 929 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 930 | // The loop body, consisting of the specified body and the loop variable. |
| 931 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 932 | |
| 933 | // The body is executed if the expression, contextually converted |
| 934 | // to bool, is true. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 935 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 936 | Builder.CreateCondBr( |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 937 | BoolCondVal, ForBody, ExitBlock, |
| 938 | createProfileWeightsForLoop(S.getCond(), getProfileCount(S.getBody()))); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 939 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 940 | if (ExitBlock != LoopExit.getBlock()) { |
| 941 | EmitBlock(ExitBlock); |
| 942 | EmitBranchThroughCleanup(LoopExit); |
| 943 | } |
| 944 | |
| 945 | EmitBlock(ForBody); |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 946 | incrementProfileCounter(&S); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 947 | |
| 948 | // Create a block for the increment. In case of a 'continue', we jump there. |
| 949 | JumpDest Continue = getJumpDestInCurrentScope("for.inc"); |
| 950 | |
| 951 | // Store the blocks to use for break and continue. |
| 952 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
| 953 | |
| 954 | { |
| 955 | // Create a separate cleanup scope for the loop variable and body. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 956 | LexicalScope BodyScope(*this, S.getSourceRange()); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 957 | EmitStmt(S.getLoopVarStmt()); |
| 958 | EmitStmt(S.getBody()); |
| 959 | } |
| 960 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 961 | EmitStopPoint(&S); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 962 | // If there is an increment, emit it next. |
| 963 | EmitBlock(Continue.getBlock()); |
| 964 | EmitStmt(S.getInc()); |
| 965 | |
| 966 | BreakContinueStack.pop_back(); |
| 967 | |
| 968 | EmitBranch(CondBlock); |
| 969 | |
| 970 | ForScope.ForceCleanup(); |
| 971 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 972 | LoopStack.pop(); |
| 973 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 974 | // Emit the fall-through block. |
| 975 | EmitBlock(LoopExit.getBlock(), true); |
| 976 | } |
| 977 | |
Daniel Dunbar | 29e0bcc | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 978 | void CodeGenFunction::EmitReturnOfRValue(RValue RV, QualType Ty) { |
| 979 | if (RV.isScalar()) { |
| 980 | Builder.CreateStore(RV.getScalarVal(), ReturnValue); |
| 981 | } else if (RV.isAggregate()) { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 982 | EmitAggregateCopy(ReturnValue, RV.getAggregateAddress(), Ty); |
Daniel Dunbar | 29e0bcc | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 983 | } else { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 984 | EmitStoreOfComplex(RV.getComplexVal(), MakeAddrLValue(ReturnValue, Ty), |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 985 | /*init*/ true); |
Daniel Dunbar | 29e0bcc | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 986 | } |
Anders Carlsson | 82d8ef0 | 2009-02-09 20:31:03 +0000 | [diff] [blame] | 987 | EmitBranchThroughCleanup(ReturnBlock); |
Daniel Dunbar | 29e0bcc | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 988 | } |
| 989 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 990 | /// EmitReturnStmt - Note that due to GCC extensions, this can have an operand |
| 991 | /// if the function returns void, or may be missing one if the function returns |
| 992 | /// non-void. Fun stuff :). |
| 993 | void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) { |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 994 | // Returning from an outlined SEH helper is UB, and we already warn on it. |
| 995 | if (IsOutlinedSEHHelper) { |
| 996 | Builder.CreateUnreachable(); |
| 997 | Builder.ClearInsertionPoint(); |
| 998 | } |
| 999 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1000 | // Emit the result value, even if unused, to evalute the side effects. |
| 1001 | const Expr *RV = S.getRetValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1002 | |
John McCall | 9f357de | 2012-09-25 06:56:03 +0000 | [diff] [blame] | 1003 | // Treat block literals in a return expression as if they appeared |
| 1004 | // in their own scope. This permits a small, easily-implemented |
| 1005 | // exception to our over-conservative rules about not jumping to |
| 1006 | // statements following block literals with non-trivial cleanups. |
| 1007 | RunCleanupsScope cleanupScope(*this); |
| 1008 | if (const ExprWithCleanups *cleanups = |
| 1009 | dyn_cast_or_null<ExprWithCleanups>(RV)) { |
| 1010 | enterFullExpression(cleanups); |
| 1011 | RV = cleanups->getSubExpr(); |
| 1012 | } |
| 1013 | |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 1014 | // FIXME: Clean this up by using an LValue for ReturnTemp, |
| 1015 | // EmitStoreThroughLValue, and EmitAnyExpr. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1016 | if (getLangOpts().ElideConstructors && |
| 1017 | S.getNRVOCandidate() && S.getNRVOCandidate()->isNRVOVariable()) { |
Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 1018 | // Apply the named return value optimization for this return statement, |
| 1019 | // which means doing nothing: the appropriate result has already been |
| 1020 | // constructed into the NRVO variable. |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 1021 | |
Douglas Gregor | 3d91bbc | 2010-05-17 15:52:46 +0000 | [diff] [blame] | 1022 | // If there is an NRVO flag for this variable, set it to 1 into indicate |
| 1023 | // that the cleanup code should not destroy the variable. |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1024 | if (llvm::Value *NRVOFlag = NRVOFlags[S.getNRVOCandidate()]) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1025 | Builder.CreateFlagStore(Builder.getTrue(), NRVOFlag); |
| 1026 | } else if (!ReturnValue.isValid() || (RV && RV->getType()->isVoidType())) { |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 1027 | // Make sure not to return anything, but evaluate the expression |
| 1028 | // for side effects. |
| 1029 | if (RV) |
Eli Friedman | 144ac61 | 2008-05-22 01:22:33 +0000 | [diff] [blame] | 1030 | EmitAnyExpr(RV); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1031 | } else if (!RV) { |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 1032 | // Do nothing (return value is left uninitialized) |
Eli Friedman | d54b6ac | 2009-05-27 04:56:12 +0000 | [diff] [blame] | 1033 | } else if (FnRetTy->isReferenceType()) { |
| 1034 | // If this function returns a reference, take the address of the expression |
| 1035 | // rather than the value. |
Richard Smith | d4ec562 | 2013-06-12 23:38:09 +0000 | [diff] [blame] | 1036 | RValue Result = EmitReferenceBindingToExpr(RV); |
Douglas Gregor | 33fd1fc | 2010-03-24 23:14:04 +0000 | [diff] [blame] | 1037 | Builder.CreateStore(Result.getScalarVal(), ReturnValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1038 | } else { |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1039 | switch (getEvaluationKind(RV->getType())) { |
| 1040 | case TEK_Scalar: |
| 1041 | Builder.CreateStore(EmitScalarExpr(RV), ReturnValue); |
| 1042 | break; |
| 1043 | case TEK_Complex: |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1044 | EmitComplexExprIntoLValue(RV, MakeAddrLValue(ReturnValue, RV->getType()), |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1045 | /*isInit*/ true); |
| 1046 | break; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1047 | case TEK_Aggregate: |
| 1048 | EmitAggExpr(RV, AggValueSlot::forAddr(ReturnValue, |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1049 | Qualifiers(), |
| 1050 | AggValueSlot::IsDestructed, |
| 1051 | AggValueSlot::DoesNotNeedGCBarriers, |
| 1052 | AggValueSlot::IsNotAliased)); |
| 1053 | break; |
| 1054 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1055 | } |
Eli Friedman | 144ac61 | 2008-05-22 01:22:33 +0000 | [diff] [blame] | 1056 | |
Adrian Prantl | ddb379e | 2013-05-07 22:41:09 +0000 | [diff] [blame] | 1057 | ++NumReturnExprs; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1058 | if (!RV || RV->isEvaluatable(getContext())) |
Adrian Prantl | ddb379e | 2013-05-07 22:41:09 +0000 | [diff] [blame] | 1059 | ++NumSimpleReturnExprs; |
Adrian Prantl | fa6b079 | 2013-05-02 17:30:20 +0000 | [diff] [blame] | 1060 | |
John McCall | 9f357de | 2012-09-25 06:56:03 +0000 | [diff] [blame] | 1061 | cleanupScope.ForceCleanup(); |
Anders Carlsson | 82d8ef0 | 2009-02-09 20:31:03 +0000 | [diff] [blame] | 1062 | EmitBranchThroughCleanup(ReturnBlock); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) { |
Devang Patel | 9198126 | 2011-06-04 00:38:02 +0000 | [diff] [blame] | 1066 | // As long as debug info is modeled with instructions, we have to ensure we |
| 1067 | // have a place to insert here and write the stop point here. |
Eric Christopher | 2b124ea | 2012-04-10 05:04:07 +0000 | [diff] [blame] | 1068 | if (HaveInsertPoint()) |
Devang Patel | 9198126 | 2011-06-04 00:38:02 +0000 | [diff] [blame] | 1069 | EmitStopPoint(&S); |
| 1070 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1071 | for (const auto *I : S.decls()) |
| 1072 | EmitDecl(*I); |
Chris Lattner | 6fa5f09 | 2007-07-12 15:43:07 +0000 | [diff] [blame] | 1073 | } |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 1074 | |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 1075 | void CodeGenFunction::EmitBreakStmt(const BreakStmt &S) { |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 1076 | assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!"); |
| 1077 | |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 1078 | // If this code is reachable then emit a stop point (if generating |
| 1079 | // debug info). We have to do this ourselves because we are on the |
| 1080 | // "simple" statement path. |
| 1081 | if (HaveInsertPoint()) |
| 1082 | EmitStopPoint(&S); |
Mike Stump | ec9771d | 2009-02-08 09:22:19 +0000 | [diff] [blame] | 1083 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1084 | EmitBranchThroughCleanup(BreakContinueStack.back().BreakBlock); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 1087 | void CodeGenFunction::EmitContinueStmt(const ContinueStmt &S) { |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 1088 | assert(!BreakContinueStack.empty() && "continue stmt not in a loop!"); |
| 1089 | |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 1090 | // If this code is reachable then emit a stop point (if generating |
| 1091 | // debug info). We have to do this ourselves because we are on the |
| 1092 | // "simple" statement path. |
| 1093 | if (HaveInsertPoint()) |
| 1094 | EmitStopPoint(&S); |
Mike Stump | ec9771d | 2009-02-08 09:22:19 +0000 | [diff] [blame] | 1095 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1096 | EmitBranchThroughCleanup(BreakContinueStack.back().ContinueBlock); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 1097 | } |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 1098 | |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 1099 | /// EmitCaseStmtRange - If case statement range is not too big then |
| 1100 | /// add multiple cases to switch instruction, one for each value within |
| 1101 | /// the range. If range is too big then emit "if" condition check. |
| 1102 | void CodeGenFunction::EmitCaseStmtRange(const CaseStmt &S) { |
Daniel Dunbar | 4efde8d | 2008-07-24 01:18:41 +0000 | [diff] [blame] | 1103 | assert(S.getRHS() && "Expected RHS value in CaseStmt"); |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 1104 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1105 | llvm::APSInt LHS = S.getLHS()->EvaluateKnownConstInt(getContext()); |
| 1106 | llvm::APSInt RHS = S.getRHS()->EvaluateKnownConstInt(getContext()); |
Daniel Dunbar | 4efde8d | 2008-07-24 01:18:41 +0000 | [diff] [blame] | 1107 | |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 1108 | // Emit the code for this case. We do this first to make sure it is |
| 1109 | // properly chained from our predecessor before generating the |
| 1110 | // switch machinery to enter this block. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1111 | llvm::BasicBlock *CaseDest = createBasicBlock("sw.bb"); |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 1112 | EmitBlockWithFallThrough(CaseDest, &S); |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 1113 | EmitStmt(S.getSubStmt()); |
| 1114 | |
Daniel Dunbar | 4efde8d | 2008-07-24 01:18:41 +0000 | [diff] [blame] | 1115 | // If range is empty, do nothing. |
| 1116 | if (LHS.isSigned() ? RHS.slt(LHS) : RHS.ult(LHS)) |
| 1117 | return; |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 1118 | |
| 1119 | llvm::APInt Range = RHS - LHS; |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 1120 | // FIXME: parameters such as this should not be hardcoded. |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 1121 | if (Range.ult(llvm::APInt(Range.getBitWidth(), 64))) { |
| 1122 | // Range is small enough to add multiple switch instruction cases. |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 1123 | uint64_t Total = getProfileCount(&S); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1124 | unsigned NCases = Range.getZExtValue() + 1; |
| 1125 | // We only have one region counter for the entire set of cases here, so we |
| 1126 | // need to divide the weights evenly between the generated cases, ensuring |
| 1127 | // that the total weight is preserved. E.g., a weight of 5 over three cases |
| 1128 | // will be distributed as weights of 2, 2, and 1. |
| 1129 | uint64_t Weight = Total / NCases, Rem = Total % NCases; |
| 1130 | for (unsigned I = 0; I != NCases; ++I) { |
| 1131 | if (SwitchWeights) |
| 1132 | SwitchWeights->push_back(Weight + (Rem ? 1 : 0)); |
| 1133 | if (Rem) |
| 1134 | Rem--; |
Chris Lattner | 97d5437 | 2011-04-19 20:53:45 +0000 | [diff] [blame] | 1135 | SwitchInsn->addCase(Builder.getInt(LHS), CaseDest); |
Devang Patel | 2d79d0f | 2007-10-05 20:54:07 +0000 | [diff] [blame] | 1136 | LHS++; |
| 1137 | } |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 1138 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 1141 | // The range is too big. Emit "if" condition into a new block, |
| 1142 | // making sure to save and restore the current insertion point. |
| 1143 | llvm::BasicBlock *RestoreBB = Builder.GetInsertBlock(); |
Devang Patel | 2d79d0f | 2007-10-05 20:54:07 +0000 | [diff] [blame] | 1144 | |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 1145 | // Push this test onto the chain of range checks (which terminates |
| 1146 | // in the default basic block). The switch's default will be changed |
| 1147 | // to the top of this chain after switch emission is complete. |
| 1148 | llvm::BasicBlock *FalseDest = CaseRangeBlock; |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 1149 | CaseRangeBlock = createBasicBlock("sw.caserange"); |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 1150 | |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 1151 | CurFn->getBasicBlockList().push_back(CaseRangeBlock); |
| 1152 | Builder.SetInsertPoint(CaseRangeBlock); |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 1153 | |
| 1154 | // Emit range check. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1155 | llvm::Value *Diff = |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1156 | Builder.CreateSub(SwitchInsn->getCondition(), Builder.getInt(LHS)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1157 | llvm::Value *Cond = |
Chris Lattner | 97d5437 | 2011-04-19 20:53:45 +0000 | [diff] [blame] | 1158 | Builder.CreateICmpULE(Diff, Builder.getInt(Range), "inbounds"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1159 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1160 | llvm::MDNode *Weights = nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1161 | if (SwitchWeights) { |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 1162 | uint64_t ThisCount = getProfileCount(&S); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1163 | uint64_t DefaultCount = (*SwitchWeights)[0]; |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 1164 | Weights = createProfileWeights(ThisCount, DefaultCount); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1165 | |
| 1166 | // Since we're chaining the switch default through each large case range, we |
| 1167 | // need to update the weight for the default, ie, the first case, to include |
| 1168 | // this case. |
| 1169 | (*SwitchWeights)[0] += ThisCount; |
| 1170 | } |
| 1171 | Builder.CreateCondBr(Cond, CaseDest, FalseDest, Weights); |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 1172 | |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 1173 | // Restore the appropriate insertion point. |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 1174 | if (RestoreBB) |
| 1175 | Builder.SetInsertPoint(RestoreBB); |
| 1176 | else |
| 1177 | Builder.ClearInsertionPoint(); |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | void CodeGenFunction::EmitCaseStmt(const CaseStmt &S) { |
Fariborz Jahanian | d66715d | 2012-01-16 17:35:57 +0000 | [diff] [blame] | 1181 | // If there is no enclosing switch instance that we're aware of, then this |
| 1182 | // case statement and its block can be elided. This situation only happens |
| 1183 | // when we've constant-folded the switch, are emitting the constant case, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1184 | // and part of the constant case includes another case statement. For |
Fariborz Jahanian | d66715d | 2012-01-16 17:35:57 +0000 | [diff] [blame] | 1185 | // instance: switch (4) { case 4: do { case 5: } while (1); } |
Fariborz Jahanian | 303b4f9 | 2012-01-17 23:55:19 +0000 | [diff] [blame] | 1186 | if (!SwitchInsn) { |
| 1187 | EmitStmt(S.getSubStmt()); |
Fariborz Jahanian | d66715d | 2012-01-16 17:35:57 +0000 | [diff] [blame] | 1188 | return; |
Fariborz Jahanian | 303b4f9 | 2012-01-17 23:55:19 +0000 | [diff] [blame] | 1189 | } |
Fariborz Jahanian | d66715d | 2012-01-16 17:35:57 +0000 | [diff] [blame] | 1190 | |
Chris Lattner | b11f919 | 2011-04-17 00:54:30 +0000 | [diff] [blame] | 1191 | // Handle case ranges. |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 1192 | if (S.getRHS()) { |
| 1193 | EmitCaseStmtRange(S); |
| 1194 | return; |
| 1195 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1196 | |
Chris Lattner | 97d5437 | 2011-04-19 20:53:45 +0000 | [diff] [blame] | 1197 | llvm::ConstantInt *CaseVal = |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1198 | Builder.getInt(S.getLHS()->EvaluateKnownConstInt(getContext())); |
Chris Lattner | 97d5437 | 2011-04-19 20:53:45 +0000 | [diff] [blame] | 1199 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1200 | // If the body of the case is just a 'break', try to not emit an empty block. |
| 1201 | // If we're profiling or we're not optimizing, leave the block in for better |
| 1202 | // debug and coverage analysis. |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1203 | if (!CGM.getCodeGenOpts().hasProfileClangInstr() && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1204 | CGM.getCodeGenOpts().OptimizationLevel > 0 && |
Chad Rosier | 1708360 | 2012-08-24 18:31:16 +0000 | [diff] [blame] | 1205 | isa<BreakStmt>(S.getSubStmt())) { |
Chris Lattner | b11f919 | 2011-04-17 00:54:30 +0000 | [diff] [blame] | 1206 | JumpDest Block = BreakContinueStack.back().BreakBlock; |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 1207 | |
Chris Lattner | b11f919 | 2011-04-17 00:54:30 +0000 | [diff] [blame] | 1208 | // Only do this optimization if there are no cleanups that need emitting. |
| 1209 | if (isObviouslyBranchWithoutCleanups(Block)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1210 | if (SwitchWeights) |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 1211 | SwitchWeights->push_back(getProfileCount(&S)); |
Chris Lattner | 97d5437 | 2011-04-19 20:53:45 +0000 | [diff] [blame] | 1212 | SwitchInsn->addCase(CaseVal, Block.getBlock()); |
Chris Lattner | 4210486 | 2011-04-17 23:21:26 +0000 | [diff] [blame] | 1213 | |
| 1214 | // If there was a fallthrough into this case, make sure to redirect it to |
| 1215 | // the end of the switch as well. |
| 1216 | if (Builder.GetInsertBlock()) { |
| 1217 | Builder.CreateBr(Block.getBlock()); |
| 1218 | Builder.ClearInsertionPoint(); |
| 1219 | } |
Chris Lattner | b11f919 | 2011-04-17 00:54:30 +0000 | [diff] [blame] | 1220 | return; |
| 1221 | } |
| 1222 | } |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 1223 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1224 | llvm::BasicBlock *CaseDest = createBasicBlock("sw.bb"); |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 1225 | EmitBlockWithFallThrough(CaseDest, &S); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1226 | if (SwitchWeights) |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 1227 | SwitchWeights->push_back(getProfileCount(&S)); |
Chris Lattner | 97d5437 | 2011-04-19 20:53:45 +0000 | [diff] [blame] | 1228 | SwitchInsn->addCase(CaseVal, CaseDest); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1229 | |
Chris Lattner | 5512f28 | 2009-03-04 04:46:18 +0000 | [diff] [blame] | 1230 | // Recursively emitting the statement is acceptable, but is not wonderful for |
| 1231 | // code where we have many case statements nested together, i.e.: |
| 1232 | // case 1: |
| 1233 | // case 2: |
| 1234 | // case 3: etc. |
| 1235 | // Handling this recursively will create a new block for each case statement |
| 1236 | // that falls through to the next case which is IR intensive. It also causes |
| 1237 | // deep recursion which can run into stack depth limitations. Handle |
| 1238 | // sequential non-range case statements specially. |
| 1239 | const CaseStmt *CurCase = &S; |
| 1240 | const CaseStmt *NextCase = dyn_cast<CaseStmt>(S.getSubStmt()); |
| 1241 | |
Chris Lattner | 97d5437 | 2011-04-19 20:53:45 +0000 | [diff] [blame] | 1242 | // Otherwise, iteratively add consecutive cases to this switch stmt. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1243 | while (NextCase && NextCase->getRHS() == nullptr) { |
Chris Lattner | 5512f28 | 2009-03-04 04:46:18 +0000 | [diff] [blame] | 1244 | CurCase = NextCase; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1245 | llvm::ConstantInt *CaseVal = |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1246 | Builder.getInt(CurCase->getLHS()->EvaluateKnownConstInt(getContext())); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1247 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1248 | if (SwitchWeights) |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 1249 | SwitchWeights->push_back(getProfileCount(NextCase)); |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1250 | if (CGM.getCodeGenOpts().hasProfileClangInstr()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1251 | CaseDest = createBasicBlock("sw.bb"); |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 1252 | EmitBlockWithFallThrough(CaseDest, &S); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1253 | } |
| 1254 | |
Chris Lattner | 97d5437 | 2011-04-19 20:53:45 +0000 | [diff] [blame] | 1255 | SwitchInsn->addCase(CaseVal, CaseDest); |
Chris Lattner | 5512f28 | 2009-03-04 04:46:18 +0000 | [diff] [blame] | 1256 | NextCase = dyn_cast<CaseStmt>(CurCase->getSubStmt()); |
| 1257 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1258 | |
Chris Lattner | 5512f28 | 2009-03-04 04:46:18 +0000 | [diff] [blame] | 1259 | // Normal default recursion for non-cases. |
| 1260 | EmitStmt(CurCase->getSubStmt()); |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | void CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S) { |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 1264 | llvm::BasicBlock *DefaultBlock = SwitchInsn->getDefaultDest(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1265 | assert(DefaultBlock->empty() && |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 1266 | "EmitDefaultStmt: Default block already defined?"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1267 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 1268 | EmitBlockWithFallThrough(DefaultBlock, &S); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1269 | |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 1270 | EmitStmt(S.getSubStmt()); |
| 1271 | } |
| 1272 | |
Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 1273 | /// CollectStatementsForCase - Given the body of a 'switch' statement and a |
| 1274 | /// constant value that is being switched on, see if we can dead code eliminate |
| 1275 | /// the body of the switch to a simple series of statements to emit. Basically, |
| 1276 | /// on a switch (5) we want to find these statements: |
| 1277 | /// case 5: |
| 1278 | /// printf(...); <-- |
| 1279 | /// ++i; <-- |
| 1280 | /// break; |
| 1281 | /// |
| 1282 | /// and add them to the ResultStmts vector. If it is unsafe to do this |
| 1283 | /// transformation (for example, one of the elided statements contains a label |
| 1284 | /// that might be jumped to), return CSFC_Failure. If we handled it and 'S' |
| 1285 | /// should include statements after it (e.g. the printf() line is a substmt of |
| 1286 | /// the case) then return CSFC_FallThrough. If we handled it and found a break |
| 1287 | /// statement, then return CSFC_Success. |
| 1288 | /// |
| 1289 | /// If Case is non-null, then we are looking for the specified case, checking |
| 1290 | /// that nothing we jump over contains labels. If Case is null, then we found |
| 1291 | /// the case and are looking for the break. |
| 1292 | /// |
| 1293 | /// If the recursive walk actually finds our Case, then we set FoundCase to |
| 1294 | /// true. |
| 1295 | /// |
| 1296 | enum CSFC_Result { CSFC_Failure, CSFC_FallThrough, CSFC_Success }; |
| 1297 | static CSFC_Result CollectStatementsForCase(const Stmt *S, |
| 1298 | const SwitchCase *Case, |
| 1299 | bool &FoundCase, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1300 | SmallVectorImpl<const Stmt*> &ResultStmts) { |
Chris Lattner | 3858938 | 2011-02-28 01:02:29 +0000 | [diff] [blame] | 1301 | // If this is a null statement, just succeed. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1302 | if (!S) |
Chris Lattner | 3858938 | 2011-02-28 01:02:29 +0000 | [diff] [blame] | 1303 | return Case ? CSFC_Success : CSFC_FallThrough; |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 1304 | |
Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 1305 | // If this is the switchcase (case 4: or default) that we're looking for, then |
| 1306 | // we're in business. Just add the substatement. |
| 1307 | if (const SwitchCase *SC = dyn_cast<SwitchCase>(S)) { |
| 1308 | if (S == Case) { |
| 1309 | FoundCase = true; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1310 | return CollectStatementsForCase(SC->getSubStmt(), nullptr, FoundCase, |
Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 1311 | ResultStmts); |
| 1312 | } |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 1313 | |
Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 1314 | // Otherwise, this is some other case or default statement, just ignore it. |
| 1315 | return CollectStatementsForCase(SC->getSubStmt(), Case, FoundCase, |
| 1316 | ResultStmts); |
| 1317 | } |
Chris Lattner | 3858938 | 2011-02-28 01:02:29 +0000 | [diff] [blame] | 1318 | |
| 1319 | // If we are in the live part of the code and we found our break statement, |
| 1320 | // return a success! |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1321 | if (!Case && isa<BreakStmt>(S)) |
Chris Lattner | 3858938 | 2011-02-28 01:02:29 +0000 | [diff] [blame] | 1322 | return CSFC_Success; |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 1323 | |
Chris Lattner | 3858938 | 2011-02-28 01:02:29 +0000 | [diff] [blame] | 1324 | // If this is a switch statement, then it might contain the SwitchCase, the |
| 1325 | // break, or neither. |
| 1326 | if (const CompoundStmt *CS = dyn_cast<CompoundStmt>(S)) { |
| 1327 | // Handle this as two cases: we might be looking for the SwitchCase (if so |
| 1328 | // the skipped statements must be skippable) or we might already have it. |
| 1329 | CompoundStmt::const_body_iterator I = CS->body_begin(), E = CS->body_end(); |
| 1330 | if (Case) { |
Chris Lattner | 3f06e27 | 2011-02-28 07:22:44 +0000 | [diff] [blame] | 1331 | // Keep track of whether we see a skipped declaration. The code could be |
| 1332 | // using the declaration even if it is skipped, so we can't optimize out |
| 1333 | // the decl if the kept statements might refer to it. |
| 1334 | bool HadSkippedDecl = false; |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 1335 | |
Chris Lattner | 3858938 | 2011-02-28 01:02:29 +0000 | [diff] [blame] | 1336 | // If we're looking for the case, just see if we can skip each of the |
| 1337 | // substatements. |
| 1338 | for (; Case && I != E; ++I) { |
Eli Friedman | 4d50934 | 2011-05-21 19:15:39 +0000 | [diff] [blame] | 1339 | HadSkippedDecl |= isa<DeclStmt>(*I); |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 1340 | |
Chris Lattner | 3858938 | 2011-02-28 01:02:29 +0000 | [diff] [blame] | 1341 | switch (CollectStatementsForCase(*I, Case, FoundCase, ResultStmts)) { |
| 1342 | case CSFC_Failure: return CSFC_Failure; |
| 1343 | case CSFC_Success: |
| 1344 | // A successful result means that either 1) that the statement doesn't |
| 1345 | // have the case and is skippable, or 2) does contain the case value |
Chris Lattner | 9467110 | 2011-02-28 07:16:14 +0000 | [diff] [blame] | 1346 | // and also contains the break to exit the switch. In the later case, |
| 1347 | // we just verify the rest of the statements are elidable. |
| 1348 | if (FoundCase) { |
Chris Lattner | 3f06e27 | 2011-02-28 07:22:44 +0000 | [diff] [blame] | 1349 | // If we found the case and skipped declarations, we can't do the |
| 1350 | // optimization. |
| 1351 | if (HadSkippedDecl) |
| 1352 | return CSFC_Failure; |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 1353 | |
Chris Lattner | 9467110 | 2011-02-28 07:16:14 +0000 | [diff] [blame] | 1354 | for (++I; I != E; ++I) |
| 1355 | if (CodeGenFunction::ContainsLabel(*I, true)) |
| 1356 | return CSFC_Failure; |
| 1357 | return CSFC_Success; |
| 1358 | } |
Chris Lattner | 3858938 | 2011-02-28 01:02:29 +0000 | [diff] [blame] | 1359 | break; |
| 1360 | case CSFC_FallThrough: |
| 1361 | // If we have a fallthrough condition, then we must have found the |
| 1362 | // case started to include statements. Consider the rest of the |
| 1363 | // statements in the compound statement as candidates for inclusion. |
| 1364 | assert(FoundCase && "Didn't find case but returned fallthrough?"); |
| 1365 | // We recursively found Case, so we're not looking for it anymore. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1366 | Case = nullptr; |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 1367 | |
Chris Lattner | 3f06e27 | 2011-02-28 07:22:44 +0000 | [diff] [blame] | 1368 | // If we found the case and skipped declarations, we can't do the |
| 1369 | // optimization. |
| 1370 | if (HadSkippedDecl) |
| 1371 | return CSFC_Failure; |
Chris Lattner | 3858938 | 2011-02-28 01:02:29 +0000 | [diff] [blame] | 1372 | break; |
| 1373 | } |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | // If we have statements in our range, then we know that the statements are |
| 1378 | // live and need to be added to the set of statements we're tracking. |
| 1379 | for (; I != E; ++I) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1380 | switch (CollectStatementsForCase(*I, nullptr, FoundCase, ResultStmts)) { |
Chris Lattner | 3858938 | 2011-02-28 01:02:29 +0000 | [diff] [blame] | 1381 | case CSFC_Failure: return CSFC_Failure; |
| 1382 | case CSFC_FallThrough: |
| 1383 | // A fallthrough result means that the statement was simple and just |
| 1384 | // included in ResultStmt, keep adding them afterwards. |
| 1385 | break; |
| 1386 | case CSFC_Success: |
| 1387 | // A successful result means that we found the break statement and |
| 1388 | // stopped statement inclusion. We just ensure that any leftover stmts |
| 1389 | // are skippable and return success ourselves. |
| 1390 | for (++I; I != E; ++I) |
| 1391 | if (CodeGenFunction::ContainsLabel(*I, true)) |
| 1392 | return CSFC_Failure; |
| 1393 | return CSFC_Success; |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 1394 | } |
Chris Lattner | 3858938 | 2011-02-28 01:02:29 +0000 | [diff] [blame] | 1395 | } |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 1396 | |
Chris Lattner | 3858938 | 2011-02-28 01:02:29 +0000 | [diff] [blame] | 1397 | return Case ? CSFC_Success : CSFC_FallThrough; |
| 1398 | } |
| 1399 | |
Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 1400 | // Okay, this is some other statement that we don't handle explicitly, like a |
| 1401 | // for statement or increment etc. If we are skipping over this statement, |
| 1402 | // just verify it doesn't have labels, which would make it invalid to elide. |
| 1403 | if (Case) { |
Chris Lattner | 3f06e27 | 2011-02-28 07:22:44 +0000 | [diff] [blame] | 1404 | if (CodeGenFunction::ContainsLabel(S, true)) |
Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 1405 | return CSFC_Failure; |
| 1406 | return CSFC_Success; |
| 1407 | } |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 1408 | |
Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 1409 | // Otherwise, we want to include this statement. Everything is cool with that |
| 1410 | // so long as it doesn't contain a break out of the switch we're in. |
| 1411 | if (CodeGenFunction::containsBreak(S)) return CSFC_Failure; |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 1412 | |
Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 1413 | // Otherwise, everything is great. Include the statement and tell the caller |
| 1414 | // that we fall through and include the next statement as well. |
| 1415 | ResultStmts.push_back(S); |
| 1416 | return CSFC_FallThrough; |
| 1417 | } |
| 1418 | |
| 1419 | /// FindCaseStatementsForValue - Find the case statement being jumped to and |
| 1420 | /// then invoke CollectStatementsForCase to find the list of statements to emit |
| 1421 | /// for a switch on constant. See the comment above CollectStatementsForCase |
| 1422 | /// for more details. |
| 1423 | static bool FindCaseStatementsForValue(const SwitchStmt &S, |
Richard Trieu | e1ecdc1 | 2012-07-23 20:21:35 +0000 | [diff] [blame] | 1424 | const llvm::APSInt &ConstantCondValue, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1425 | SmallVectorImpl<const Stmt*> &ResultStmts, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1426 | ASTContext &C, |
| 1427 | const SwitchCase *&ResultCase) { |
Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 1428 | // First step, find the switch case that is being branched to. We can do this |
| 1429 | // efficiently by scanning the SwitchCase list. |
| 1430 | const SwitchCase *Case = S.getSwitchCaseList(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1431 | const DefaultStmt *DefaultCase = nullptr; |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 1432 | |
Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 1433 | for (; Case; Case = Case->getNextSwitchCase()) { |
| 1434 | // It's either a default or case. Just remember the default statement in |
| 1435 | // case we're not jumping to any numbered cases. |
| 1436 | if (const DefaultStmt *DS = dyn_cast<DefaultStmt>(Case)) { |
| 1437 | DefaultCase = DS; |
| 1438 | continue; |
| 1439 | } |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 1440 | |
Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 1441 | // Check to see if this case is the one we're looking for. |
| 1442 | const CaseStmt *CS = cast<CaseStmt>(Case); |
| 1443 | // Don't handle case ranges yet. |
| 1444 | if (CS->getRHS()) return false; |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 1445 | |
Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 1446 | // If we found our case, remember it as 'case'. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1447 | if (CS->getLHS()->EvaluateKnownConstInt(C) == ConstantCondValue) |
Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 1448 | break; |
| 1449 | } |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 1450 | |
Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 1451 | // If we didn't find a matching case, we use a default if it exists, or we |
| 1452 | // elide the whole switch body! |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1453 | if (!Case) { |
Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 1454 | // It is safe to elide the body of the switch if it doesn't contain labels |
| 1455 | // etc. If it is safe, return successfully with an empty ResultStmts list. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1456 | if (!DefaultCase) |
Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 1457 | return !CodeGenFunction::ContainsLabel(&S); |
| 1458 | Case = DefaultCase; |
| 1459 | } |
| 1460 | |
| 1461 | // Ok, we know which case is being jumped to, try to collect all the |
| 1462 | // statements that follow it. This can fail for a variety of reasons. Also, |
| 1463 | // check to see that the recursive walk actually found our case statement. |
| 1464 | // Insane cases like this can fail to find it in the recursive walk since we |
| 1465 | // don't handle every stmt kind: |
| 1466 | // switch (4) { |
| 1467 | // while (1) { |
| 1468 | // case 4: ... |
| 1469 | bool FoundCase = false; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1470 | ResultCase = Case; |
Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 1471 | return CollectStatementsForCase(S.getBody(), Case, FoundCase, |
| 1472 | ResultStmts) != CSFC_Failure && |
| 1473 | FoundCase; |
| 1474 | } |
| 1475 | |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 1476 | void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) { |
Fariborz Jahanian | 985df1c | 2012-01-17 23:39:50 +0000 | [diff] [blame] | 1477 | // Handle nested switch statements. |
| 1478 | llvm::SwitchInst *SavedSwitchInsn = SwitchInsn; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1479 | SmallVector<uint64_t, 16> *SavedSwitchWeights = SwitchWeights; |
Fariborz Jahanian | 985df1c | 2012-01-17 23:39:50 +0000 | [diff] [blame] | 1480 | llvm::BasicBlock *SavedCRBlock = CaseRangeBlock; |
| 1481 | |
Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 1482 | // See if we can constant fold the condition of the switch and therefore only |
| 1483 | // emit the live case statement (if any) of the switch. |
Richard Trieu | e1ecdc1 | 2012-07-23 20:21:35 +0000 | [diff] [blame] | 1484 | llvm::APSInt ConstantCondValue; |
Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 1485 | if (ConstantFoldsToSimpleInteger(S.getCond(), ConstantCondValue)) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1486 | SmallVector<const Stmt*, 4> CaseStmts; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1487 | const SwitchCase *Case = nullptr; |
Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 1488 | if (FindCaseStatementsForValue(S, ConstantCondValue, CaseStmts, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1489 | getContext(), Case)) { |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 1490 | if (Case) |
| 1491 | incrementProfileCounter(Case); |
Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 1492 | RunCleanupsScope ExecutedScope(*this); |
| 1493 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1494 | if (S.getInit()) |
| 1495 | EmitStmt(S.getInit()); |
| 1496 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1497 | // Emit the condition variable if needed inside the entire cleanup scope |
| 1498 | // used by this special case for constant folded switches. |
| 1499 | if (S.getConditionVariable()) |
| 1500 | EmitAutoVarDecl(*S.getConditionVariable()); |
| 1501 | |
Fariborz Jahanian | 985df1c | 2012-01-17 23:39:50 +0000 | [diff] [blame] | 1502 | // At this point, we are no longer "within" a switch instance, so |
| 1503 | // we can temporarily enforce this to ensure that any embedded case |
| 1504 | // statements are not emitted. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1505 | SwitchInsn = nullptr; |
Fariborz Jahanian | 985df1c | 2012-01-17 23:39:50 +0000 | [diff] [blame] | 1506 | |
Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 1507 | // Okay, we can dead code eliminate everything except this case. Emit the |
| 1508 | // specified series of statements and we're good. |
| 1509 | for (unsigned i = 0, e = CaseStmts.size(); i != e; ++i) |
| 1510 | EmitStmt(CaseStmts[i]); |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 1511 | incrementProfileCounter(&S); |
Fariborz Jahanian | 985df1c | 2012-01-17 23:39:50 +0000 | [diff] [blame] | 1512 | |
Eric Christopher | fc65ec8 | 2012-04-10 05:04:04 +0000 | [diff] [blame] | 1513 | // Now we want to restore the saved switch instance so that nested |
| 1514 | // switches continue to function properly |
Fariborz Jahanian | 985df1c | 2012-01-17 23:39:50 +0000 | [diff] [blame] | 1515 | SwitchInsn = SavedSwitchInsn; |
| 1516 | |
Chris Lattner | fda0f1f | 2011-02-28 00:22:07 +0000 | [diff] [blame] | 1517 | return; |
| 1518 | } |
| 1519 | } |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 1520 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1521 | JumpDest SwitchExit = getJumpDestInCurrentScope("sw.epilog"); |
| 1522 | |
| 1523 | RunCleanupsScope ConditionScope(*this); |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1524 | |
| 1525 | if (S.getInit()) |
| 1526 | EmitStmt(S.getInit()); |
| 1527 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1528 | if (S.getConditionVariable()) |
| 1529 | EmitAutoVarDecl(*S.getConditionVariable()); |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 1530 | llvm::Value *CondV = EmitScalarExpr(S.getCond()); |
| 1531 | |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 1532 | // Create basic block to hold stuff that comes after switch |
| 1533 | // statement. We also need to create a default block now so that |
| 1534 | // explicit case ranges tests can have a place to jump to on |
| 1535 | // failure. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 1536 | llvm::BasicBlock *DefaultBlock = createBasicBlock("sw.default"); |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 1537 | SwitchInsn = Builder.CreateSwitch(CondV, DefaultBlock); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1538 | if (PGO.haveRegionCounts()) { |
| 1539 | // Walk the SwitchCase list to find how many there are. |
| 1540 | uint64_t DefaultCount = 0; |
| 1541 | unsigned NumCases = 0; |
| 1542 | for (const SwitchCase *Case = S.getSwitchCaseList(); |
| 1543 | Case; |
| 1544 | Case = Case->getNextSwitchCase()) { |
| 1545 | if (isa<DefaultStmt>(Case)) |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 1546 | DefaultCount = getProfileCount(Case); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1547 | NumCases += 1; |
| 1548 | } |
| 1549 | SwitchWeights = new SmallVector<uint64_t, 16>(); |
| 1550 | SwitchWeights->reserve(NumCases); |
| 1551 | // The default needs to be first. We store the edge count, so we already |
| 1552 | // know the right weight. |
| 1553 | SwitchWeights->push_back(DefaultCount); |
| 1554 | } |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 1555 | CaseRangeBlock = DefaultBlock; |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 1556 | |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 1557 | // Clear the insertion point to indicate we are in unreachable code. |
| 1558 | Builder.ClearInsertionPoint(); |
Eli Friedman | d28a80d | 2008-05-12 16:08:04 +0000 | [diff] [blame] | 1559 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1560 | // All break statements jump to NextBlock. If BreakContinueStack is non-empty |
Devang Patel | e9b8c0a | 2007-10-30 20:59:40 +0000 | [diff] [blame] | 1561 | // then reuse last ContinueBlock. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1562 | JumpDest OuterContinue; |
Anders Carlsson | e4b6d34 | 2009-02-10 05:52:02 +0000 | [diff] [blame] | 1563 | if (!BreakContinueStack.empty()) |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1564 | OuterContinue = BreakContinueStack.back().ContinueBlock; |
Anders Carlsson | e4b6d34 | 2009-02-10 05:52:02 +0000 | [diff] [blame] | 1565 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1566 | BreakContinueStack.push_back(BreakContinue(SwitchExit, OuterContinue)); |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 1567 | |
| 1568 | // Emit switch body. |
| 1569 | EmitStmt(S.getBody()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1570 | |
Anders Carlsson | e4b6d34 | 2009-02-10 05:52:02 +0000 | [diff] [blame] | 1571 | BreakContinueStack.pop_back(); |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 1572 | |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 1573 | // Update the default block in case explicit case range tests have |
| 1574 | // been chained on top. |
Stepan Dyatkovskiy | ab14ae2 | 2012-02-01 07:50:21 +0000 | [diff] [blame] | 1575 | SwitchInsn->setDefaultDest(CaseRangeBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1576 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1577 | // If a default was never emitted: |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 1578 | if (!DefaultBlock->getParent()) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1579 | // If we have cleanups, emit the default block so that there's a |
| 1580 | // place to jump through the cleanups from. |
| 1581 | if (ConditionScope.requiresCleanups()) { |
| 1582 | EmitBlock(DefaultBlock); |
| 1583 | |
| 1584 | // Otherwise, just forward the default block to the switch end. |
| 1585 | } else { |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1586 | DefaultBlock->replaceAllUsesWith(SwitchExit.getBlock()); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1587 | delete DefaultBlock; |
| 1588 | } |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 1589 | } |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 1590 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1591 | ConditionScope.ForceCleanup(); |
| 1592 | |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 1593 | // Emit continuation. |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1594 | EmitBlock(SwitchExit.getBlock(), true); |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 1595 | incrementProfileCounter(&S); |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 1596 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1597 | // If the switch has a condition wrapped by __builtin_unpredictable, |
| 1598 | // create metadata that specifies that the switch is unpredictable. |
| 1599 | // Don't bother if not optimizing because that metadata would not be used. |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1600 | auto *Call = dyn_cast<CallExpr>(S.getCond()); |
| 1601 | if (Call && CGM.getCodeGenOpts().OptimizationLevel != 0) { |
| 1602 | auto *FD = dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl()); |
| 1603 | if (FD && FD->getBuiltinID() == Builtin::BI__builtin_unpredictable) { |
| 1604 | llvm::MDBuilder MDHelper(getLLVMContext()); |
| 1605 | SwitchInsn->setMetadata(llvm::LLVMContext::MD_unpredictable, |
| 1606 | MDHelper.createUnpredictable()); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1607 | } |
| 1608 | } |
| 1609 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1610 | if (SwitchWeights) { |
| 1611 | assert(SwitchWeights->size() == 1 + SwitchInsn->getNumCases() && |
| 1612 | "switch weights do not match switch cases"); |
| 1613 | // If there's only one jump destination there's no sense weighting it. |
| 1614 | if (SwitchWeights->size() > 1) |
| 1615 | SwitchInsn->setMetadata(llvm::LLVMContext::MD_prof, |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 1616 | createProfileWeights(*SwitchWeights)); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1617 | delete SwitchWeights; |
| 1618 | } |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 1619 | SwitchInsn = SavedSwitchInsn; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1620 | SwitchWeights = SavedSwitchWeights; |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 1621 | CaseRangeBlock = SavedCRBlock; |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 1622 | } |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1623 | |
Chris Lattner | 2819fa8 | 2009-04-26 17:57:12 +0000 | [diff] [blame] | 1624 | static std::string |
Daniel Dunbar | 444be73 | 2009-11-13 05:51:54 +0000 | [diff] [blame] | 1625 | SimplifyConstraint(const char *Constraint, const TargetInfo &Target, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1626 | SmallVectorImpl<TargetInfo::ConstraintInfo> *OutCons=nullptr) { |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1627 | std::string Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1628 | |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1629 | while (*Constraint) { |
| 1630 | switch (*Constraint) { |
| 1631 | default: |
Stuart Hastings | 002333f | 2011-06-07 23:45:05 +0000 | [diff] [blame] | 1632 | Result += Target.convertConstraint(Constraint); |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1633 | break; |
| 1634 | // Ignore these |
| 1635 | case '*': |
| 1636 | case '?': |
| 1637 | case '!': |
John Thompson | ef44e11 | 2010-08-10 19:20:14 +0000 | [diff] [blame] | 1638 | case '=': // Will see this and the following in mult-alt constraints. |
| 1639 | case '+': |
| 1640 | break; |
Ulrich Weigand | e6b3dba | 2012-10-29 12:20:54 +0000 | [diff] [blame] | 1641 | case '#': // Ignore the rest of the constraint alternative. |
| 1642 | while (Constraint[1] && Constraint[1] != ',') |
Eric Christopher | 7b309b0 | 2013-07-10 20:14:36 +0000 | [diff] [blame] | 1643 | Constraint++; |
Ulrich Weigand | e6b3dba | 2012-10-29 12:20:54 +0000 | [diff] [blame] | 1644 | break; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1645 | case '&': |
| 1646 | case '%': |
| 1647 | Result += *Constraint; |
| 1648 | while (Constraint[1] && Constraint[1] == *Constraint) |
| 1649 | Constraint++; |
| 1650 | break; |
John Thompson | 2f474ea | 2010-09-18 01:15:13 +0000 | [diff] [blame] | 1651 | case ',': |
| 1652 | Result += "|"; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1653 | break; |
| 1654 | case 'g': |
| 1655 | Result += "imr"; |
| 1656 | break; |
Anders Carlsson | 300fb5d | 2009-01-18 02:06:20 +0000 | [diff] [blame] | 1657 | case '[': { |
Chris Lattner | 2819fa8 | 2009-04-26 17:57:12 +0000 | [diff] [blame] | 1658 | assert(OutCons && |
Anders Carlsson | 300fb5d | 2009-01-18 02:06:20 +0000 | [diff] [blame] | 1659 | "Must pass output names to constraints with a symbolic name"); |
| 1660 | unsigned Index; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1661 | bool result = Target.resolveSymbolicName(Constraint, *OutCons, Index); |
Chris Lattner | cbf40f9 | 2011-01-05 18:41:53 +0000 | [diff] [blame] | 1662 | assert(result && "Could not resolve symbolic name"); (void)result; |
Anders Carlsson | 300fb5d | 2009-01-18 02:06:20 +0000 | [diff] [blame] | 1663 | Result += llvm::utostr(Index); |
| 1664 | break; |
| 1665 | } |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1666 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1667 | |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1668 | Constraint++; |
| 1669 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1670 | |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1671 | return Result; |
| 1672 | } |
| 1673 | |
Rafael Espindola | 03117d1 | 2011-01-01 21:12:33 +0000 | [diff] [blame] | 1674 | /// AddVariableConstraints - Look at AsmExpr and if it is a variable declared |
| 1675 | /// as using a particular register add that as a constraint that will be used |
| 1676 | /// in this asm stmt. |
Rafael Espindola | 0ec89f9 | 2010-12-30 22:59:32 +0000 | [diff] [blame] | 1677 | static std::string |
Rafael Espindola | 03117d1 | 2011-01-01 21:12:33 +0000 | [diff] [blame] | 1678 | AddVariableConstraints(const std::string &Constraint, const Expr &AsmExpr, |
| 1679 | const TargetInfo &Target, CodeGenModule &CGM, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1680 | const AsmStmt &Stmt, const bool EarlyClobber) { |
Rafael Espindola | 0ec89f9 | 2010-12-30 22:59:32 +0000 | [diff] [blame] | 1681 | const DeclRefExpr *AsmDeclRef = dyn_cast<DeclRefExpr>(&AsmExpr); |
| 1682 | if (!AsmDeclRef) |
| 1683 | return Constraint; |
| 1684 | const ValueDecl &Value = *AsmDeclRef->getDecl(); |
| 1685 | const VarDecl *Variable = dyn_cast<VarDecl>(&Value); |
| 1686 | if (!Variable) |
| 1687 | return Constraint; |
Eli Friedman | a43ef3e | 2012-03-15 23:12:51 +0000 | [diff] [blame] | 1688 | if (Variable->getStorageClass() != SC_Register) |
| 1689 | return Constraint; |
Rafael Espindola | 0ec89f9 | 2010-12-30 22:59:32 +0000 | [diff] [blame] | 1690 | AsmLabelAttr *Attr = Variable->getAttr<AsmLabelAttr>(); |
| 1691 | if (!Attr) |
| 1692 | return Constraint; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1693 | StringRef Register = Attr->getLabel(); |
Rafael Espindola | baf8695 | 2011-01-01 21:47:03 +0000 | [diff] [blame] | 1694 | assert(Target.isValidGCCRegisterName(Register)); |
Eric Christopher | e3e07a5 | 2011-06-17 01:53:34 +0000 | [diff] [blame] | 1695 | // We're using validateOutputConstraint here because we only care if |
| 1696 | // this is a register constraint. |
| 1697 | TargetInfo::ConstraintInfo Info(Constraint, ""); |
| 1698 | if (Target.validateOutputConstraint(Info) && |
| 1699 | !Info.allowsRegister()) { |
Rafael Espindola | 0ec89f9 | 2010-12-30 22:59:32 +0000 | [diff] [blame] | 1700 | CGM.ErrorUnsupported(&Stmt, "__asm__"); |
| 1701 | return Constraint; |
| 1702 | } |
Eric Christopher | 43fec87 | 2011-06-21 00:07:10 +0000 | [diff] [blame] | 1703 | // Canonicalize the register here before returning it. |
| 1704 | Register = Target.getNormalizedGCCRegisterName(Register); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1705 | return (EarlyClobber ? "&{" : "{") + Register.str() + "}"; |
Rafael Espindola | 0ec89f9 | 2010-12-30 22:59:32 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
Eli Friedman | 6d7cfd7 | 2010-07-16 00:55:21 +0000 | [diff] [blame] | 1708 | llvm::Value* |
Chad Rosier | 42b6055 | 2012-08-23 20:00:18 +0000 | [diff] [blame] | 1709 | CodeGenFunction::EmitAsmInputLValue(const TargetInfo::ConstraintInfo &Info, |
Eli Friedman | 6d7cfd7 | 2010-07-16 00:55:21 +0000 | [diff] [blame] | 1710 | LValue InputValue, QualType InputType, |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1711 | std::string &ConstraintStr, |
| 1712 | SourceLocation Loc) { |
Anders Carlsson | 6347172 | 2009-01-11 19:32:54 +0000 | [diff] [blame] | 1713 | llvm::Value *Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1714 | if (Info.allowsRegister() || !Info.allowsMemory()) { |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1715 | if (CodeGenFunction::hasScalarEvaluationKind(InputType)) { |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1716 | Arg = EmitLoadOfLValue(InputValue, Loc).getScalarVal(); |
Anders Carlsson | 6347172 | 2009-01-11 19:32:54 +0000 | [diff] [blame] | 1717 | } else { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1718 | llvm::Type *Ty = ConvertType(InputType); |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1719 | uint64_t Size = CGM.getDataLayout().getTypeSizeInBits(Ty); |
Anders Carlsson | ebaae2a | 2009-01-12 02:22:13 +0000 | [diff] [blame] | 1720 | if (Size <= 64 && llvm::isPowerOf2_64(Size)) { |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1721 | Ty = llvm::IntegerType::get(getLLVMContext(), Size); |
Anders Carlsson | ebaae2a | 2009-01-12 02:22:13 +0000 | [diff] [blame] | 1722 | Ty = llvm::PointerType::getUnqual(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1723 | |
Eli Friedman | 6d7cfd7 | 2010-07-16 00:55:21 +0000 | [diff] [blame] | 1724 | Arg = Builder.CreateLoad(Builder.CreateBitCast(InputValue.getAddress(), |
| 1725 | Ty)); |
Anders Carlsson | ebaae2a | 2009-01-12 02:22:13 +0000 | [diff] [blame] | 1726 | } else { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1727 | Arg = InputValue.getPointer(); |
Anders Carlsson | ebaae2a | 2009-01-12 02:22:13 +0000 | [diff] [blame] | 1728 | ConstraintStr += '*'; |
| 1729 | } |
Anders Carlsson | 6347172 | 2009-01-11 19:32:54 +0000 | [diff] [blame] | 1730 | } |
| 1731 | } else { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1732 | Arg = InputValue.getPointer(); |
Anders Carlsson | 6347172 | 2009-01-11 19:32:54 +0000 | [diff] [blame] | 1733 | ConstraintStr += '*'; |
| 1734 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1735 | |
Anders Carlsson | 6347172 | 2009-01-11 19:32:54 +0000 | [diff] [blame] | 1736 | return Arg; |
| 1737 | } |
| 1738 | |
Chad Rosier | 42b6055 | 2012-08-23 20:00:18 +0000 | [diff] [blame] | 1739 | llvm::Value* CodeGenFunction::EmitAsmInput( |
Eli Friedman | 6d7cfd7 | 2010-07-16 00:55:21 +0000 | [diff] [blame] | 1740 | const TargetInfo::ConstraintInfo &Info, |
| 1741 | const Expr *InputExpr, |
| 1742 | std::string &ConstraintStr) { |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 1743 | // If this can't be a register or memory, i.e., has to be a constant |
| 1744 | // (immediate or symbolic), try to emit it as such. |
| 1745 | if (!Info.allowsRegister() && !Info.allowsMemory()) { |
| 1746 | llvm::APSInt Result; |
| 1747 | if (InputExpr->EvaluateAsInt(Result, getContext())) |
| 1748 | return llvm::ConstantInt::get(getLLVMContext(), Result); |
| 1749 | assert(!Info.requiresImmediateConstant() && |
| 1750 | "Required-immediate inlineasm arg isn't constant?"); |
| 1751 | } |
| 1752 | |
Eli Friedman | 6d7cfd7 | 2010-07-16 00:55:21 +0000 | [diff] [blame] | 1753 | if (Info.allowsRegister() || !Info.allowsMemory()) |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1754 | if (CodeGenFunction::hasScalarEvaluationKind(InputExpr->getType())) |
Eli Friedman | 6d7cfd7 | 2010-07-16 00:55:21 +0000 | [diff] [blame] | 1755 | return EmitScalarExpr(InputExpr); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1756 | if (InputExpr->getStmtClass() == Expr::CXXThisExprClass) |
| 1757 | return EmitScalarExpr(InputExpr); |
Eli Friedman | 6d7cfd7 | 2010-07-16 00:55:21 +0000 | [diff] [blame] | 1758 | InputExpr = InputExpr->IgnoreParenNoopCasts(getContext()); |
| 1759 | LValue Dest = EmitLValue(InputExpr); |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1760 | return EmitAsmInputLValue(Info, Dest, InputExpr->getType(), ConstraintStr, |
| 1761 | InputExpr->getExprLoc()); |
Eli Friedman | 6d7cfd7 | 2010-07-16 00:55:21 +0000 | [diff] [blame] | 1762 | } |
| 1763 | |
Chris Lattner | 47fc7e9 | 2010-11-17 05:58:54 +0000 | [diff] [blame] | 1764 | /// getAsmSrcLocInfo - Return the !srcloc metadata node to attach to an inline |
Chris Lattner | 5d93653 | 2010-11-17 08:25:26 +0000 | [diff] [blame] | 1765 | /// asm call instruction. The !srcloc MDNode contains a list of constant |
| 1766 | /// integers which are the source locations of the start of each line in the |
| 1767 | /// asm. |
Chris Lattner | 47fc7e9 | 2010-11-17 05:58:54 +0000 | [diff] [blame] | 1768 | static llvm::MDNode *getAsmSrcLocInfo(const StringLiteral *Str, |
| 1769 | CodeGenFunction &CGF) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1770 | SmallVector<llvm::Metadata *, 8> Locs; |
Chris Lattner | 5d93653 | 2010-11-17 08:25:26 +0000 | [diff] [blame] | 1771 | // Add the location of the first line to the MDNode. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1772 | Locs.push_back(llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 1773 | CGF.Int32Ty, Str->getLocStart().getRawEncoding()))); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1774 | StringRef StrVal = Str->getString(); |
Chris Lattner | 5d93653 | 2010-11-17 08:25:26 +0000 | [diff] [blame] | 1775 | if (!StrVal.empty()) { |
| 1776 | const SourceManager &SM = CGF.CGM.getContext().getSourceManager(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1777 | const LangOptions &LangOpts = CGF.CGM.getLangOpts(); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1778 | unsigned StartToken = 0; |
| 1779 | unsigned ByteOffset = 0; |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 1780 | |
Chris Lattner | 5d93653 | 2010-11-17 08:25:26 +0000 | [diff] [blame] | 1781 | // Add the location of the start of each subsequent line of the asm to the |
| 1782 | // MDNode. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1783 | for (unsigned i = 0, e = StrVal.size() - 1; i != e; ++i) { |
Chris Lattner | 5d93653 | 2010-11-17 08:25:26 +0000 | [diff] [blame] | 1784 | if (StrVal[i] != '\n') continue; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1785 | SourceLocation LineLoc = Str->getLocationOfByte( |
| 1786 | i + 1, SM, LangOpts, CGF.getTarget(), &StartToken, &ByteOffset); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1787 | Locs.push_back(llvm::ConstantAsMetadata::get( |
| 1788 | llvm::ConstantInt::get(CGF.Int32Ty, LineLoc.getRawEncoding()))); |
Chris Lattner | 5d93653 | 2010-11-17 08:25:26 +0000 | [diff] [blame] | 1789 | } |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 1790 | } |
| 1791 | |
Jay Foad | 6f14165 | 2011-04-21 19:59:12 +0000 | [diff] [blame] | 1792 | return llvm::MDNode::get(CGF.getLLVMContext(), Locs); |
Chris Lattner | 47fc7e9 | 2010-11-17 05:58:54 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
Chad Rosier | a23b91d | 2012-08-28 18:54:39 +0000 | [diff] [blame] | 1795 | void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) { |
Chad Rosier | be3ace8 | 2012-08-24 17:05:45 +0000 | [diff] [blame] | 1796 | // Assemble the final asm string. |
Chad Rosier | da083b2 | 2012-08-27 20:23:31 +0000 | [diff] [blame] | 1797 | std::string AsmString = S.generateAsmString(getContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1798 | |
Chris Lattner | 481fef9 | 2009-05-03 07:05:00 +0000 | [diff] [blame] | 1799 | // Get all the output and input constraints together. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1800 | SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos; |
| 1801 | SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos; |
Chris Lattner | 481fef9 | 2009-05-03 07:05:00 +0000 | [diff] [blame] | 1802 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1803 | for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) { |
John McCall | aeeacf7 | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 1804 | StringRef Name; |
| 1805 | if (const GCCAsmStmt *GAS = dyn_cast<GCCAsmStmt>(&S)) |
| 1806 | Name = GAS->getOutputName(i); |
| 1807 | TargetInfo::ConstraintInfo Info(S.getOutputConstraint(i), Name); |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1808 | bool IsValid = getTarget().validateOutputConstraint(Info); (void)IsValid; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1809 | assert(IsValid && "Failed to parse output constraint"); |
Chris Lattner | 481fef9 | 2009-05-03 07:05:00 +0000 | [diff] [blame] | 1810 | OutputConstraintInfos.push_back(Info); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
Chris Lattner | 481fef9 | 2009-05-03 07:05:00 +0000 | [diff] [blame] | 1813 | for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) { |
John McCall | aeeacf7 | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 1814 | StringRef Name; |
| 1815 | if (const GCCAsmStmt *GAS = dyn_cast<GCCAsmStmt>(&S)) |
| 1816 | Name = GAS->getInputName(i); |
| 1817 | TargetInfo::ConstraintInfo Info(S.getInputConstraint(i), Name); |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1818 | bool IsValid = |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1819 | getTarget().validateInputConstraint(OutputConstraintInfos, Info); |
Chris Lattner | b992259 | 2010-03-03 21:52:23 +0000 | [diff] [blame] | 1820 | assert(IsValid && "Failed to parse input constraint"); (void)IsValid; |
Chris Lattner | 481fef9 | 2009-05-03 07:05:00 +0000 | [diff] [blame] | 1821 | InputConstraintInfos.push_back(Info); |
| 1822 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1823 | |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1824 | std::string Constraints; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1825 | |
Chris Lattner | ede9d90 | 2009-05-03 07:53:25 +0000 | [diff] [blame] | 1826 | std::vector<LValue> ResultRegDests; |
| 1827 | std::vector<QualType> ResultRegQualTys; |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1828 | std::vector<llvm::Type *> ResultRegTypes; |
| 1829 | std::vector<llvm::Type *> ResultTruncRegTypes; |
Chad Rosier | d1c0c94 | 2012-05-01 19:53:37 +0000 | [diff] [blame] | 1830 | std::vector<llvm::Type *> ArgTypes; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1831 | std::vector<llvm::Value*> Args; |
Anders Carlsson | f39a421 | 2008-02-05 20:01:53 +0000 | [diff] [blame] | 1832 | |
| 1833 | // Keep track of inout constraints. |
| 1834 | std::string InOutConstraints; |
| 1835 | std::vector<llvm::Value*> InOutArgs; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1836 | std::vector<llvm::Type*> InOutArgTypes; |
Anders Carlsson | 03eb543 | 2009-01-27 20:38:24 +0000 | [diff] [blame] | 1837 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1838 | // An inline asm can be marked readonly if it meets the following conditions: |
| 1839 | // - it doesn't have any sideeffects |
| 1840 | // - it doesn't clobber memory |
| 1841 | // - it doesn't return a value by-reference |
| 1842 | // It can be marked readnone if it doesn't have any input memory constraints |
| 1843 | // in addition to meeting the conditions listed above. |
| 1844 | bool ReadOnly = true, ReadNone = true; |
| 1845 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1846 | for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) { |
Chris Lattner | 481fef9 | 2009-05-03 07:05:00 +0000 | [diff] [blame] | 1847 | TargetInfo::ConstraintInfo &Info = OutputConstraintInfos[i]; |
Anders Carlsson | 03eb543 | 2009-01-27 20:38:24 +0000 | [diff] [blame] | 1848 | |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1849 | // Simplify the output constraint. |
Chris Lattner | 481fef9 | 2009-05-03 07:05:00 +0000 | [diff] [blame] | 1850 | std::string OutputConstraint(S.getOutputConstraint(i)); |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1851 | OutputConstraint = SimplifyConstraint(OutputConstraint.c_str() + 1, |
| 1852 | getTarget()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1853 | |
Chris Lattner | 810f6d5 | 2009-03-13 17:38:01 +0000 | [diff] [blame] | 1854 | const Expr *OutExpr = S.getOutputExpr(i); |
| 1855 | OutExpr = OutExpr->IgnoreParenNoopCasts(getContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1856 | |
Eric Christopher | a18f539 | 2011-06-03 14:52:25 +0000 | [diff] [blame] | 1857 | OutputConstraint = AddVariableConstraints(OutputConstraint, *OutExpr, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1858 | getTarget(), CGM, S, |
| 1859 | Info.earlyClobber()); |
Rafael Espindola | 0ec89f9 | 2010-12-30 22:59:32 +0000 | [diff] [blame] | 1860 | |
Chris Lattner | 810f6d5 | 2009-03-13 17:38:01 +0000 | [diff] [blame] | 1861 | LValue Dest = EmitLValue(OutExpr); |
Chris Lattner | ede9d90 | 2009-05-03 07:53:25 +0000 | [diff] [blame] | 1862 | if (!Constraints.empty()) |
Anders Carlsson | bad3a94 | 2009-05-01 00:16:04 +0000 | [diff] [blame] | 1863 | Constraints += ','; |
| 1864 | |
Chris Lattner | a077b5c | 2009-05-03 08:21:20 +0000 | [diff] [blame] | 1865 | // If this is a register output, then make the inline asm return it |
| 1866 | // by-value. If this is a memory result, return the value by-reference. |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1867 | if (!Info.allowsMemory() && hasScalarEvaluationKind(OutExpr->getType())) { |
Chris Lattner | a077b5c | 2009-05-03 08:21:20 +0000 | [diff] [blame] | 1868 | Constraints += "=" + OutputConstraint; |
Chris Lattner | ede9d90 | 2009-05-03 07:53:25 +0000 | [diff] [blame] | 1869 | ResultRegQualTys.push_back(OutExpr->getType()); |
| 1870 | ResultRegDests.push_back(Dest); |
Chris Lattner | a077b5c | 2009-05-03 08:21:20 +0000 | [diff] [blame] | 1871 | ResultRegTypes.push_back(ConvertTypeForMem(OutExpr->getType())); |
| 1872 | ResultTruncRegTypes.push_back(ResultRegTypes.back()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1873 | |
Chris Lattner | a077b5c | 2009-05-03 08:21:20 +0000 | [diff] [blame] | 1874 | // If this output is tied to an input, and if the input is larger, then |
| 1875 | // we need to set the actual result type of the inline asm node to be the |
| 1876 | // same as the input type. |
| 1877 | if (Info.hasMatchingInput()) { |
Chris Lattner | ebfc985 | 2009-05-03 08:38:58 +0000 | [diff] [blame] | 1878 | unsigned InputNo; |
| 1879 | for (InputNo = 0; InputNo != S.getNumInputs(); ++InputNo) { |
| 1880 | TargetInfo::ConstraintInfo &Input = InputConstraintInfos[InputNo]; |
Chris Lattner | aab64d0 | 2010-04-23 17:27:29 +0000 | [diff] [blame] | 1881 | if (Input.hasTiedOperand() && Input.getTiedOperand() == i) |
Chris Lattner | a077b5c | 2009-05-03 08:21:20 +0000 | [diff] [blame] | 1882 | break; |
Chris Lattner | ebfc985 | 2009-05-03 08:38:58 +0000 | [diff] [blame] | 1883 | } |
| 1884 | assert(InputNo != S.getNumInputs() && "Didn't find matching input!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1885 | |
Chris Lattner | a077b5c | 2009-05-03 08:21:20 +0000 | [diff] [blame] | 1886 | QualType InputTy = S.getInputExpr(InputNo)->getType(); |
Chris Lattner | aab64d0 | 2010-04-23 17:27:29 +0000 | [diff] [blame] | 1887 | QualType OutputType = OutExpr->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1888 | |
Chris Lattner | a077b5c | 2009-05-03 08:21:20 +0000 | [diff] [blame] | 1889 | uint64_t InputSize = getContext().getTypeSize(InputTy); |
Chris Lattner | aab64d0 | 2010-04-23 17:27:29 +0000 | [diff] [blame] | 1890 | if (getContext().getTypeSize(OutputType) < InputSize) { |
| 1891 | // Form the asm to return the value as a larger integer or fp type. |
| 1892 | ResultRegTypes.back() = ConvertType(InputTy); |
Chris Lattner | a077b5c | 2009-05-03 08:21:20 +0000 | [diff] [blame] | 1893 | } |
| 1894 | } |
Tim Northover | 1bea653 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 1895 | if (llvm::Type* AdjTy = |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1896 | getTargetHooks().adjustInlineAsmType(*this, OutputConstraint, |
| 1897 | ResultRegTypes.back())) |
Dale Johannesen | f6e2c20 | 2010-10-29 23:12:32 +0000 | [diff] [blame] | 1898 | ResultRegTypes.back() = AdjTy; |
Tim Northover | 1bea653 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 1899 | else { |
| 1900 | CGM.getDiags().Report(S.getAsmLoc(), |
| 1901 | diag::err_asm_invalid_type_in_input) |
| 1902 | << OutExpr->getType() << OutputConstraint; |
| 1903 | } |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1904 | } else { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1905 | ArgTypes.push_back(Dest.getAddress().getType()); |
| 1906 | Args.push_back(Dest.getPointer()); |
Anders Carlsson | f39a421 | 2008-02-05 20:01:53 +0000 | [diff] [blame] | 1907 | Constraints += "=*"; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1908 | Constraints += OutputConstraint; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1909 | ReadOnly = ReadNone = false; |
Anders Carlsson | f39a421 | 2008-02-05 20:01:53 +0000 | [diff] [blame] | 1910 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1911 | |
Chris Lattner | 44def07 | 2009-04-26 07:16:29 +0000 | [diff] [blame] | 1912 | if (Info.isReadWrite()) { |
Anders Carlsson | f39a421 | 2008-02-05 20:01:53 +0000 | [diff] [blame] | 1913 | InOutConstraints += ','; |
Anders Carlsson | 6347172 | 2009-01-11 19:32:54 +0000 | [diff] [blame] | 1914 | |
Anders Carlsson | fca9361 | 2009-08-04 18:18:36 +0000 | [diff] [blame] | 1915 | const Expr *InputExpr = S.getOutputExpr(i); |
Chad Rosier | 42b6055 | 2012-08-23 20:00:18 +0000 | [diff] [blame] | 1916 | llvm::Value *Arg = EmitAsmInputLValue(Info, Dest, InputExpr->getType(), |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1917 | InOutConstraints, |
| 1918 | InputExpr->getExprLoc()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1919 | |
Bill Wendling | acb5310 | 2012-03-22 23:25:07 +0000 | [diff] [blame] | 1920 | if (llvm::Type* AdjTy = |
Tim Northover | 1bea653 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 1921 | getTargetHooks().adjustInlineAsmType(*this, OutputConstraint, |
| 1922 | Arg->getType())) |
Bill Wendling | acb5310 | 2012-03-22 23:25:07 +0000 | [diff] [blame] | 1923 | Arg = Builder.CreateBitCast(Arg, AdjTy); |
| 1924 | |
Chris Lattner | 44def07 | 2009-04-26 07:16:29 +0000 | [diff] [blame] | 1925 | if (Info.allowsRegister()) |
Anders Carlsson | 9f2505b | 2009-01-11 21:23:27 +0000 | [diff] [blame] | 1926 | InOutConstraints += llvm::utostr(i); |
| 1927 | else |
| 1928 | InOutConstraints += OutputConstraint; |
Anders Carlsson | 2763b3a | 2009-01-11 19:46:50 +0000 | [diff] [blame] | 1929 | |
Anders Carlsson | fca9361 | 2009-08-04 18:18:36 +0000 | [diff] [blame] | 1930 | InOutArgTypes.push_back(Arg->getType()); |
| 1931 | InOutArgs.push_back(Arg); |
Anders Carlsson | f39a421 | 2008-02-05 20:01:53 +0000 | [diff] [blame] | 1932 | } |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1933 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1934 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1935 | // If this is a Microsoft-style asm blob, store the return registers (EAX:EDX) |
| 1936 | // to the return value slot. Only do this when returning in registers. |
| 1937 | if (isa<MSAsmStmt>(&S)) { |
| 1938 | const ABIArgInfo &RetAI = CurFnInfo->getReturnInfo(); |
| 1939 | if (RetAI.isDirect() || RetAI.isExtend()) { |
| 1940 | // Make a fake lvalue for the return value slot. |
| 1941 | LValue ReturnSlot = MakeAddrLValue(ReturnValue, FnRetTy); |
| 1942 | CGM.getTargetCodeGenInfo().addReturnRegisterOutputs( |
| 1943 | *this, ReturnSlot, Constraints, ResultRegTypes, ResultTruncRegTypes, |
| 1944 | ResultRegDests, AsmString, S.getNumOutputs()); |
| 1945 | SawAsmBlock = true; |
| 1946 | } |
| 1947 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1948 | |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1949 | for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) { |
| 1950 | const Expr *InputExpr = S.getInputExpr(i); |
| 1951 | |
Chris Lattner | 481fef9 | 2009-05-03 07:05:00 +0000 | [diff] [blame] | 1952 | TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i]; |
| 1953 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1954 | if (Info.allowsMemory()) |
| 1955 | ReadNone = false; |
| 1956 | |
Chris Lattner | ede9d90 | 2009-05-03 07:53:25 +0000 | [diff] [blame] | 1957 | if (!Constraints.empty()) |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1958 | Constraints += ','; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1959 | |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1960 | // Simplify the input constraint. |
Chris Lattner | 481fef9 | 2009-05-03 07:05:00 +0000 | [diff] [blame] | 1961 | std::string InputConstraint(S.getInputConstraint(i)); |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1962 | InputConstraint = SimplifyConstraint(InputConstraint.c_str(), getTarget(), |
Chris Lattner | 2819fa8 | 2009-04-26 17:57:12 +0000 | [diff] [blame] | 1963 | &OutputConstraintInfos); |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1964 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1965 | InputConstraint = AddVariableConstraints( |
| 1966 | InputConstraint, *InputExpr->IgnoreParenNoopCasts(getContext()), |
| 1967 | getTarget(), CGM, S, false /* No EarlyClobber */); |
Rafael Espindola | 0ec89f9 | 2010-12-30 22:59:32 +0000 | [diff] [blame] | 1968 | |
Chad Rosier | 42b6055 | 2012-08-23 20:00:18 +0000 | [diff] [blame] | 1969 | llvm::Value *Arg = EmitAsmInput(Info, InputExpr, Constraints); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1970 | |
Chris Lattner | 4df4ee0 | 2009-05-03 07:27:51 +0000 | [diff] [blame] | 1971 | // If this input argument is tied to a larger output result, extend the |
| 1972 | // input to be the same size as the output. The LLVM backend wants to see |
| 1973 | // the input and output of a matching constraint be the same size. Note |
| 1974 | // that GCC does not define what the top bits are here. We use zext because |
| 1975 | // that is usually cheaper, but LLVM IR should really get an anyext someday. |
| 1976 | if (Info.hasTiedOperand()) { |
| 1977 | unsigned Output = Info.getTiedOperand(); |
Chris Lattner | aab64d0 | 2010-04-23 17:27:29 +0000 | [diff] [blame] | 1978 | QualType OutputType = S.getOutputExpr(Output)->getType(); |
Chris Lattner | 4df4ee0 | 2009-05-03 07:27:51 +0000 | [diff] [blame] | 1979 | QualType InputTy = InputExpr->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1980 | |
Chris Lattner | aab64d0 | 2010-04-23 17:27:29 +0000 | [diff] [blame] | 1981 | if (getContext().getTypeSize(OutputType) > |
Chris Lattner | 4df4ee0 | 2009-05-03 07:27:51 +0000 | [diff] [blame] | 1982 | getContext().getTypeSize(InputTy)) { |
| 1983 | // Use ptrtoint as appropriate so that we can do our extension. |
| 1984 | if (isa<llvm::PointerType>(Arg->getType())) |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1985 | Arg = Builder.CreatePtrToInt(Arg, IntPtrTy); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1986 | llvm::Type *OutputTy = ConvertType(OutputType); |
Chris Lattner | aab64d0 | 2010-04-23 17:27:29 +0000 | [diff] [blame] | 1987 | if (isa<llvm::IntegerType>(OutputTy)) |
| 1988 | Arg = Builder.CreateZExt(Arg, OutputTy); |
Peter Collingbourne | 93f1322 | 2011-07-29 00:24:50 +0000 | [diff] [blame] | 1989 | else if (isa<llvm::PointerType>(OutputTy)) |
| 1990 | Arg = Builder.CreateZExt(Arg, IntPtrTy); |
| 1991 | else { |
| 1992 | assert(OutputTy->isFloatingPointTy() && "Unexpected output type"); |
Chris Lattner | aab64d0 | 2010-04-23 17:27:29 +0000 | [diff] [blame] | 1993 | Arg = Builder.CreateFPExt(Arg, OutputTy); |
Peter Collingbourne | 93f1322 | 2011-07-29 00:24:50 +0000 | [diff] [blame] | 1994 | } |
Chris Lattner | 4df4ee0 | 2009-05-03 07:27:51 +0000 | [diff] [blame] | 1995 | } |
| 1996 | } |
Bill Wendling | acb5310 | 2012-03-22 23:25:07 +0000 | [diff] [blame] | 1997 | if (llvm::Type* AdjTy = |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1998 | getTargetHooks().adjustInlineAsmType(*this, InputConstraint, |
| 1999 | Arg->getType())) |
Dale Johannesen | f6e2c20 | 2010-10-29 23:12:32 +0000 | [diff] [blame] | 2000 | Arg = Builder.CreateBitCast(Arg, AdjTy); |
Tim Northover | 1bea653 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 2001 | else |
| 2002 | CGM.getDiags().Report(S.getAsmLoc(), diag::err_asm_invalid_type_in_input) |
| 2003 | << InputExpr->getType() << InputConstraint; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2004 | |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 2005 | ArgTypes.push_back(Arg->getType()); |
| 2006 | Args.push_back(Arg); |
| 2007 | Constraints += InputConstraint; |
| 2008 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2009 | |
Anders Carlsson | f39a421 | 2008-02-05 20:01:53 +0000 | [diff] [blame] | 2010 | // Append the "input" part of inout constraints last. |
| 2011 | for (unsigned i = 0, e = InOutArgs.size(); i != e; i++) { |
| 2012 | ArgTypes.push_back(InOutArgTypes[i]); |
| 2013 | Args.push_back(InOutArgs[i]); |
| 2014 | } |
| 2015 | Constraints += InOutConstraints; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2016 | |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 2017 | // Clobbers |
| 2018 | for (unsigned i = 0, e = S.getNumClobbers(); i != e; i++) { |
Chad Rosier | 33f0558 | 2012-08-27 23:47:56 +0000 | [diff] [blame] | 2019 | StringRef Clobber = S.getClobber(i); |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 2020 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2021 | if (Clobber == "memory") |
| 2022 | ReadOnly = ReadNone = false; |
| 2023 | else if (Clobber != "cc") |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2024 | Clobber = getTarget().getNormalizedGCCRegisterName(Clobber); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2025 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2026 | if (!Constraints.empty()) |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 2027 | Constraints += ','; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2028 | |
Anders Carlsson | ea04175 | 2008-02-06 00:11:32 +0000 | [diff] [blame] | 2029 | Constraints += "~{"; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 2030 | Constraints += Clobber; |
Anders Carlsson | ea04175 | 2008-02-06 00:11:32 +0000 | [diff] [blame] | 2031 | Constraints += '}'; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 2032 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2033 | |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 2034 | // Add machine specific clobbers |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 2035 | std::string MachineClobbers = getTarget().getClobbers(); |
Eli Friedman | ccf614c | 2008-12-21 01:15:32 +0000 | [diff] [blame] | 2036 | if (!MachineClobbers.empty()) { |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 2037 | if (!Constraints.empty()) |
| 2038 | Constraints += ','; |
Eli Friedman | ccf614c | 2008-12-21 01:15:32 +0000 | [diff] [blame] | 2039 | Constraints += MachineClobbers; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 2040 | } |
Anders Carlsson | bad3a94 | 2009-05-01 00:16:04 +0000 | [diff] [blame] | 2041 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2042 | llvm::Type *ResultType; |
Chris Lattner | a077b5c | 2009-05-03 08:21:20 +0000 | [diff] [blame] | 2043 | if (ResultRegTypes.empty()) |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2044 | ResultType = VoidTy; |
Chris Lattner | a077b5c | 2009-05-03 08:21:20 +0000 | [diff] [blame] | 2045 | else if (ResultRegTypes.size() == 1) |
| 2046 | ResultType = ResultRegTypes[0]; |
Anders Carlsson | bad3a94 | 2009-05-01 00:16:04 +0000 | [diff] [blame] | 2047 | else |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 2048 | ResultType = llvm::StructType::get(getLLVMContext(), ResultRegTypes); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2049 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2050 | llvm::FunctionType *FTy = |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 2051 | llvm::FunctionType::get(ResultType, ArgTypes, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2052 | |
Chad Rosier | 2ab7d43 | 2012-09-04 19:50:17 +0000 | [diff] [blame] | 2053 | bool HasSideEffect = S.isVolatile() || S.getNumOutputs() == 0; |
Chad Rosier | fcf75a3 | 2012-09-05 19:01:07 +0000 | [diff] [blame] | 2054 | llvm::InlineAsm::AsmDialect AsmDialect = isa<MSAsmStmt>(&S) ? |
| 2055 | llvm::InlineAsm::AD_Intel : llvm::InlineAsm::AD_ATT; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2056 | llvm::InlineAsm *IA = |
Chad Rosier | 790cbd8 | 2012-09-04 23:08:24 +0000 | [diff] [blame] | 2057 | llvm::InlineAsm::get(FTy, AsmString, Constraints, HasSideEffect, |
Chad Rosier | fcf75a3 | 2012-09-05 19:01:07 +0000 | [diff] [blame] | 2058 | /* IsAlignStack */ false, AsmDialect); |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 2059 | llvm::CallInst *Result = Builder.CreateCall(IA, Args); |
Bill Wendling | 785b778 | 2012-12-07 23:17:26 +0000 | [diff] [blame] | 2060 | Result->addAttribute(llvm::AttributeSet::FunctionIndex, |
Peter Collingbourne | 15e05e9 | 2013-03-02 01:20:22 +0000 | [diff] [blame] | 2061 | llvm::Attribute::NoUnwind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2062 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2063 | if (isa<MSAsmStmt>(&S)) { |
| 2064 | // If the assembly contains any labels, mark the call noduplicate to prevent |
| 2065 | // defining the same ASM label twice (PR23715). This is pretty hacky, but it |
| 2066 | // works. |
| 2067 | if (AsmString.find("__MSASMLABEL_") != std::string::npos) |
| 2068 | Result->addAttribute(llvm::AttributeSet::FunctionIndex, |
| 2069 | llvm::Attribute::NoDuplicate); |
| 2070 | } |
| 2071 | |
| 2072 | // Attach readnone and readonly attributes. |
| 2073 | if (!HasSideEffect) { |
| 2074 | if (ReadNone) |
| 2075 | Result->addAttribute(llvm::AttributeSet::FunctionIndex, |
| 2076 | llvm::Attribute::ReadNone); |
| 2077 | else if (ReadOnly) |
| 2078 | Result->addAttribute(llvm::AttributeSet::FunctionIndex, |
| 2079 | llvm::Attribute::ReadOnly); |
| 2080 | } |
| 2081 | |
Chris Lattner | fc1a9c3 | 2010-04-07 05:46:54 +0000 | [diff] [blame] | 2082 | // Slap the source location of the inline asm into a !srcloc metadata on the |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2083 | // call. |
| 2084 | if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(&S)) { |
Chad Rosier | a23b91d | 2012-08-28 18:54:39 +0000 | [diff] [blame] | 2085 | Result->setMetadata("srcloc", getAsmSrcLocInfo(gccAsmStmt->getAsmString(), |
| 2086 | *this)); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2087 | } else { |
| 2088 | // At least put the line number on MS inline asm blobs. |
| 2089 | auto Loc = llvm::ConstantInt::get(Int32Ty, S.getAsmLoc().getRawEncoding()); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2090 | Result->setMetadata("srcloc", |
| 2091 | llvm::MDNode::get(getLLVMContext(), |
| 2092 | llvm::ConstantAsMetadata::get(Loc))); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2093 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2094 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 2095 | if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice) { |
| 2096 | // Conservatively, mark all inline asm blocks in CUDA as convergent |
| 2097 | // (meaning, they may call an intrinsically convergent op, such as bar.sync, |
| 2098 | // and so can't have certain optimizations applied around them). |
| 2099 | Result->addAttribute(llvm::AttributeSet::FunctionIndex, |
| 2100 | llvm::Attribute::Convergent); |
| 2101 | } |
| 2102 | |
Chris Lattner | a077b5c | 2009-05-03 08:21:20 +0000 | [diff] [blame] | 2103 | // Extract all of the register value results from the asm. |
| 2104 | std::vector<llvm::Value*> RegResults; |
| 2105 | if (ResultRegTypes.size() == 1) { |
| 2106 | RegResults.push_back(Result); |
Anders Carlsson | bad3a94 | 2009-05-01 00:16:04 +0000 | [diff] [blame] | 2107 | } else { |
Chris Lattner | a077b5c | 2009-05-03 08:21:20 +0000 | [diff] [blame] | 2108 | for (unsigned i = 0, e = ResultRegTypes.size(); i != e; ++i) { |
Anders Carlsson | bad3a94 | 2009-05-01 00:16:04 +0000 | [diff] [blame] | 2109 | llvm::Value *Tmp = Builder.CreateExtractValue(Result, i, "asmresult"); |
Chris Lattner | a077b5c | 2009-05-03 08:21:20 +0000 | [diff] [blame] | 2110 | RegResults.push_back(Tmp); |
Anders Carlsson | bad3a94 | 2009-05-01 00:16:04 +0000 | [diff] [blame] | 2111 | } |
| 2112 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2113 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2114 | assert(RegResults.size() == ResultRegTypes.size()); |
| 2115 | assert(RegResults.size() == ResultTruncRegTypes.size()); |
| 2116 | assert(RegResults.size() == ResultRegDests.size()); |
Chris Lattner | a077b5c | 2009-05-03 08:21:20 +0000 | [diff] [blame] | 2117 | for (unsigned i = 0, e = RegResults.size(); i != e; ++i) { |
| 2118 | llvm::Value *Tmp = RegResults[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2119 | |
Chris Lattner | a077b5c | 2009-05-03 08:21:20 +0000 | [diff] [blame] | 2120 | // If the result type of the LLVM IR asm doesn't match the result type of |
| 2121 | // the expression, do the conversion. |
| 2122 | if (ResultRegTypes[i] != ResultTruncRegTypes[i]) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2123 | llvm::Type *TruncTy = ResultTruncRegTypes[i]; |
Chad Rosier | 6f61ba2 | 2012-06-20 17:43:05 +0000 | [diff] [blame] | 2124 | |
Chris Lattner | aab64d0 | 2010-04-23 17:27:29 +0000 | [diff] [blame] | 2125 | // Truncate the integer result to the right size, note that TruncTy can be |
| 2126 | // a pointer. |
| 2127 | if (TruncTy->isFloatingPointTy()) |
| 2128 | Tmp = Builder.CreateFPTrunc(Tmp, TruncTy); |
Dan Gohman | 2dca88f | 2010-04-24 04:55:02 +0000 | [diff] [blame] | 2129 | else if (TruncTy->isPointerTy() && Tmp->getType()->isIntegerTy()) { |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2130 | uint64_t ResSize = CGM.getDataLayout().getTypeSizeInBits(TruncTy); |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 2131 | Tmp = Builder.CreateTrunc(Tmp, |
| 2132 | llvm::IntegerType::get(getLLVMContext(), (unsigned)ResSize)); |
Chris Lattner | a077b5c | 2009-05-03 08:21:20 +0000 | [diff] [blame] | 2133 | Tmp = Builder.CreateIntToPtr(Tmp, TruncTy); |
Dan Gohman | 2dca88f | 2010-04-24 04:55:02 +0000 | [diff] [blame] | 2134 | } else if (Tmp->getType()->isPointerTy() && TruncTy->isIntegerTy()) { |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2135 | uint64_t TmpSize =CGM.getDataLayout().getTypeSizeInBits(Tmp->getType()); |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 2136 | Tmp = Builder.CreatePtrToInt(Tmp, |
| 2137 | llvm::IntegerType::get(getLLVMContext(), (unsigned)TmpSize)); |
Dan Gohman | 2dca88f | 2010-04-24 04:55:02 +0000 | [diff] [blame] | 2138 | Tmp = Builder.CreateTrunc(Tmp, TruncTy); |
| 2139 | } else if (TruncTy->isIntegerTy()) { |
| 2140 | Tmp = Builder.CreateTrunc(Tmp, TruncTy); |
Dale Johannesen | f6e2c20 | 2010-10-29 23:12:32 +0000 | [diff] [blame] | 2141 | } else if (TruncTy->isVectorTy()) { |
| 2142 | Tmp = Builder.CreateBitCast(Tmp, TruncTy); |
Chris Lattner | a077b5c | 2009-05-03 08:21:20 +0000 | [diff] [blame] | 2143 | } |
| 2144 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2145 | |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 2146 | EmitStoreThroughLValue(RValue::get(Tmp), ResultRegDests[i]); |
Chris Lattner | a077b5c | 2009-05-03 08:21:20 +0000 | [diff] [blame] | 2147 | } |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 2148 | } |
Tareq A. Siraj | 051303c | 2013-04-16 18:53:08 +0000 | [diff] [blame] | 2149 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2150 | LValue CodeGenFunction::InitCapturedStruct(const CapturedStmt &S) { |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 2151 | const RecordDecl *RD = S.getCapturedRecordDecl(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2152 | QualType RecordTy = getContext().getRecordType(RD); |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 2153 | |
| 2154 | // Initialize the captured struct. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2155 | LValue SlotLV = |
| 2156 | MakeAddrLValue(CreateMemTemp(RecordTy, "agg.captured"), RecordTy); |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 2157 | |
| 2158 | RecordDecl::field_iterator CurField = RD->field_begin(); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2159 | for (CapturedStmt::const_capture_init_iterator I = S.capture_init_begin(), |
| 2160 | E = S.capture_init_end(); |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 2161 | I != E; ++I, ++CurField) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2162 | LValue LV = EmitLValueForFieldInitialization(SlotLV, *CurField); |
| 2163 | if (CurField->hasCapturedVLAType()) { |
| 2164 | auto VAT = CurField->getCapturedVLAType(); |
| 2165 | EmitStoreThroughLValue(RValue::get(VLASizeMap[VAT->getSizeExpr()]), LV); |
| 2166 | } else { |
| 2167 | EmitInitializerForField(*CurField, LV, *I, None); |
| 2168 | } |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 2169 | } |
| 2170 | |
| 2171 | return SlotLV; |
| 2172 | } |
| 2173 | |
| 2174 | /// Generate an outlined function for the body of a CapturedStmt, store any |
| 2175 | /// captured variables into the captured struct, and call the outlined function. |
| 2176 | llvm::Function * |
| 2177 | CodeGenFunction::EmitCapturedStmt(const CapturedStmt &S, CapturedRegionKind K) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2178 | LValue CapStruct = InitCapturedStruct(S); |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 2179 | |
| 2180 | // Emit the CapturedDecl |
| 2181 | CodeGenFunction CGF(CGM, true); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2182 | CGCapturedStmtRAII CapInfoRAII(CGF, new CGCapturedStmtInfo(S, K)); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2183 | llvm::Function *F = CGF.GenerateCapturedStmtFunction(S); |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 2184 | delete CGF.CapturedStmtInfo; |
| 2185 | |
| 2186 | // Emit call to the helper function. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2187 | EmitCallOrInvoke(F, CapStruct.getPointer()); |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 2188 | |
| 2189 | return F; |
| 2190 | } |
| 2191 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2192 | Address CodeGenFunction::GenerateCapturedStmtArgument(const CapturedStmt &S) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2193 | LValue CapStruct = InitCapturedStruct(S); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2194 | return CapStruct.getAddress(); |
| 2195 | } |
| 2196 | |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 2197 | /// Creates the outlined function for a CapturedStmt. |
| 2198 | llvm::Function * |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2199 | CodeGenFunction::GenerateCapturedStmtFunction(const CapturedStmt &S) { |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 2200 | assert(CapturedStmtInfo && |
| 2201 | "CapturedStmtInfo should be set when generating the captured function"); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2202 | const CapturedDecl *CD = S.getCapturedDecl(); |
| 2203 | const RecordDecl *RD = S.getCapturedRecordDecl(); |
| 2204 | SourceLocation Loc = S.getLocStart(); |
| 2205 | assert(CD->hasBody() && "missing CapturedDecl body"); |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 2206 | |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 2207 | // Build the argument list. |
| 2208 | ASTContext &Ctx = CGM.getContext(); |
| 2209 | FunctionArgList Args; |
| 2210 | Args.append(CD->param_begin(), CD->param_end()); |
| 2211 | |
| 2212 | // Create the function declaration. |
| 2213 | FunctionType::ExtInfo ExtInfo; |
| 2214 | const CGFunctionInfo &FuncInfo = |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 2215 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, Args); |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 2216 | llvm::FunctionType *FuncLLVMTy = CGM.getTypes().GetFunctionType(FuncInfo); |
| 2217 | |
| 2218 | llvm::Function *F = |
| 2219 | llvm::Function::Create(FuncLLVMTy, llvm::GlobalValue::InternalLinkage, |
| 2220 | CapturedStmtInfo->getHelperName(), &CGM.getModule()); |
| 2221 | CGM.SetInternalFunctionAttributes(CD, F, FuncInfo); |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 2222 | if (CD->isNothrow()) |
| 2223 | F->addFnAttr(llvm::Attribute::NoUnwind); |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 2224 | |
| 2225 | // Generate the function. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2226 | StartFunction(CD, Ctx.VoidTy, F, FuncInfo, Args, |
| 2227 | CD->getLocation(), |
| 2228 | CD->getBody()->getLocStart()); |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 2229 | // Set the context parameter in CapturedStmtInfo. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2230 | Address DeclPtr = GetAddrOfLocalVar(CD->getContextParam()); |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 2231 | CapturedStmtInfo->setContextValue(Builder.CreateLoad(DeclPtr)); |
| 2232 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2233 | // Initialize variable-length arrays. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2234 | LValue Base = MakeNaturalAlignAddrLValue(CapturedStmtInfo->getContextValue(), |
| 2235 | Ctx.getTagDeclType(RD)); |
| 2236 | for (auto *FD : RD->fields()) { |
| 2237 | if (FD->hasCapturedVLAType()) { |
| 2238 | auto *ExprArg = EmitLoadOfLValue(EmitLValueForField(Base, FD), |
| 2239 | S.getLocStart()).getScalarVal(); |
| 2240 | auto VAT = FD->getCapturedVLAType(); |
| 2241 | VLASizeMap[VAT->getSizeExpr()] = ExprArg; |
| 2242 | } |
| 2243 | } |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2244 | |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 2245 | // If 'this' is captured, load it into CXXThisValue. |
| 2246 | if (CapturedStmtInfo->isCXXThisExprCaptured()) { |
| 2247 | FieldDecl *FD = CapturedStmtInfo->getThisFieldDecl(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2248 | LValue ThisLValue = EmitLValueForField(Base, FD); |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2249 | CXXThisValue = EmitLoadOfLValue(ThisLValue, Loc).getScalarVal(); |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 2250 | } |
| 2251 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2252 | PGO.assignRegionCounters(GlobalDecl(CD), F); |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 2253 | CapturedStmtInfo->EmitBody(*this, CD->getBody()); |
| 2254 | FinishFunction(CD->getBodyRBrace()); |
| 2255 | |
| 2256 | return F; |
Tareq A. Siraj | 051303c | 2013-04-16 18:53:08 +0000 | [diff] [blame] | 2257 | } |