CGStmt.cpp revision fa6b079b1231366696f6a497c6a084c73a35c85d
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- CGStmt.cpp - Emit LLVM Code from Statements ----------------------===// 25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// The LLVM Compiler Infrastructure 45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source 60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details. 75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This contains code to emit Stmt nodes as LLVM code. 115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1455fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "CodeGenFunction.h" 15e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta#include "CGDebugInfo.h" 16e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta#include "CodeGenModule.h" 174b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne#include "TargetInfo.h" 18de7fb8413b13651fd85b7125d08b3c9ac2816d9dDaniel Dunbar#include "clang/AST/StmtVisitor.h" 197d22bf00dbabca86ba791f56a99e006181fa22ddChris Lattner#include "clang/Basic/PrettyStackTrace.h" 20fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson#include "clang/Basic/TargetInfo.h" 21fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson#include "llvm/ADT/StringExtras.h" 223b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/DataLayout.h" 233b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/InlineAsm.h" 243b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/Intrinsics.h" 255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen; 275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Statement Emission 305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 320912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbarvoid CodeGenFunction::EmitStopPoint(const Stmt *S) { 33e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson if (CGDebugInfo *DI = getDebugInfo()) { 3473fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher SourceLocation Loc; 3560e4fd95ed8c1f99697f4d9f73d07717b6e21048Devang Patel if (isa<DeclStmt>(S)) 3673fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher Loc = S->getLocEnd(); 3760e4fd95ed8c1f99697f4d9f73d07717b6e21048Devang Patel else 3873fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher Loc = S->getLocStart(); 3973fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher DI->EmitLocation(Builder, Loc); 40fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl 41fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl if (++NumStopPoints == 1) 42fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl FirstStopPoint = Loc; 430912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar } 440912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar} 450912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar 465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid CodeGenFunction::EmitStmt(const Stmt *S) { 475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(S && "Null statement?"); 48a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar 49f9aac38d1290c17192a74b5bc2de52b9fb1a87caEric Christopher // These statements have their own debug info handling. 500912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar if (EmitSimpleStmt(S)) 510912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar return; 520912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar 53d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar // Check if we are generating unreachable code. 54d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar if (!HaveInsertPoint()) { 55d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar // If so, and the statement doesn't contain a label, then we do not need to 56d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar // generate actual code. This is safe because (1) the current point is 57d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar // unreachable, so we don't need to execute the code, and (2) we've already 58d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar // handled the statements which update internal data structures (like the 59d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar // local variable map) which could be used by subsequent statements. 60d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar if (!ContainsLabel(S)) { 61d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar // Verify that any decl statements were handled as simple, they may be in 62d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar // scope of subsequent reachable statements. 63d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar assert(!isa<DeclStmt>(*S) && "Unexpected DeclStmt!"); 64d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar return; 65d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar } 66d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar 67d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar // Otherwise, make a new block to hold the code. 68d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar EnsureInsertPoint(); 69d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar } 70d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar 710912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar // Generate a stoppoint if we are emitting debug info. 720912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar EmitStopPoint(S); 73e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta 745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (S->getStmtClass()) { 752a41637a995affa1563f4d82a8b026e326a2faa0John McCall case Stmt::NoStmtClass: 762a41637a995affa1563f4d82a8b026e326a2faa0John McCall case Stmt::CXXCatchStmtClass: 7728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley case Stmt::SEHExceptStmtClass: 7828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley case Stmt::SEHFinallyStmtClass: 79ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor case Stmt::MSDependentExistsStmtClass: 802a41637a995affa1563f4d82a8b026e326a2faa0John McCall llvm_unreachable("invalid statement class to emit generically"); 812a41637a995affa1563f4d82a8b026e326a2faa0John McCall case Stmt::NullStmtClass: 822a41637a995affa1563f4d82a8b026e326a2faa0John McCall case Stmt::CompoundStmtClass: 832a41637a995affa1563f4d82a8b026e326a2faa0John McCall case Stmt::DeclStmtClass: 842a41637a995affa1563f4d82a8b026e326a2faa0John McCall case Stmt::LabelStmtClass: 85534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith case Stmt::AttributedStmtClass: 862a41637a995affa1563f4d82a8b026e326a2faa0John McCall case Stmt::GotoStmtClass: 872a41637a995affa1563f4d82a8b026e326a2faa0John McCall case Stmt::BreakStmtClass: 882a41637a995affa1563f4d82a8b026e326a2faa0John McCall case Stmt::ContinueStmtClass: 892a41637a995affa1563f4d82a8b026e326a2faa0John McCall case Stmt::DefaultStmtClass: 902a41637a995affa1563f4d82a8b026e326a2faa0John McCall case Stmt::CaseStmtClass: 912a41637a995affa1563f4d82a8b026e326a2faa0John McCall llvm_unreachable("should have emitted these statements as simple"); 92cd5e60e1d4093b9a757cc85e35fccc093f8f8527Daniel Dunbar 932a41637a995affa1563f4d82a8b026e326a2faa0John McCall#define STMT(Type, Base) 942a41637a995affa1563f4d82a8b026e326a2faa0John McCall#define ABSTRACT_STMT(Op) 952a41637a995affa1563f4d82a8b026e326a2faa0John McCall#define EXPR(Type, Base) \ 962a41637a995affa1563f4d82a8b026e326a2faa0John McCall case Stmt::Type##Class: 972a41637a995affa1563f4d82a8b026e326a2faa0John McCall#include "clang/AST/StmtNodes.inc" 98cd5b22e12b6513163dd131589746c194090f14e6John McCall { 99cd5b22e12b6513163dd131589746c194090f14e6John McCall // Remember the block we came in on. 100cd5b22e12b6513163dd131589746c194090f14e6John McCall llvm::BasicBlock *incoming = Builder.GetInsertBlock(); 101cd5b22e12b6513163dd131589746c194090f14e6John McCall assert(incoming && "expression emission must have an insertion point"); 102cd5b22e12b6513163dd131589746c194090f14e6John McCall 1032a41637a995affa1563f4d82a8b026e326a2faa0John McCall EmitIgnoredExpr(cast<Expr>(S)); 1041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 105cd5b22e12b6513163dd131589746c194090f14e6John McCall llvm::BasicBlock *outgoing = Builder.GetInsertBlock(); 106cd5b22e12b6513163dd131589746c194090f14e6John McCall assert(outgoing && "expression emission cleared block!"); 107cd5b22e12b6513163dd131589746c194090f14e6John McCall 108cd5b22e12b6513163dd131589746c194090f14e6John McCall // The expression emitters assume (reasonably!) that the insertion 109cd5b22e12b6513163dd131589746c194090f14e6John McCall // point is always set. To maintain that, the call-emission code 110cd5b22e12b6513163dd131589746c194090f14e6John McCall // for noreturn functions has to enter a new block with no 111cd5b22e12b6513163dd131589746c194090f14e6John McCall // predecessors. We want to kill that block and mark the current 112cd5b22e12b6513163dd131589746c194090f14e6John McCall // insertion point unreachable in the common case of a call like 113cd5b22e12b6513163dd131589746c194090f14e6John McCall // "exit();". Since expression emission doesn't otherwise create 114cd5b22e12b6513163dd131589746c194090f14e6John McCall // blocks with no predecessors, we can just test for that. 115cd5b22e12b6513163dd131589746c194090f14e6John McCall // However, we must be careful not to do this to our incoming 116cd5b22e12b6513163dd131589746c194090f14e6John McCall // block, because *statement* emission does sometimes create 117cd5b22e12b6513163dd131589746c194090f14e6John McCall // reachable blocks which will have no predecessors until later in 118cd5b22e12b6513163dd131589746c194090f14e6John McCall // the function. This occurs with, e.g., labels that are not 119cd5b22e12b6513163dd131589746c194090f14e6John McCall // reachable by fallthrough. 120cd5b22e12b6513163dd131589746c194090f14e6John McCall if (incoming != outgoing && outgoing->use_empty()) { 121cd5b22e12b6513163dd131589746c194090f14e6John McCall outgoing->eraseFromParent(); 122cd5b22e12b6513163dd131589746c194090f14e6John McCall Builder.ClearInsertionPoint(); 1235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 125cd5b22e12b6513163dd131589746c194090f14e6John McCall } 1262a41637a995affa1563f4d82a8b026e326a2faa0John McCall 1271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump case Stmt::IndirectGotoStmtClass: 1280ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar EmitIndirectGotoStmt(cast<IndirectGotoStmt>(*S)); break; 1295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Stmt::IfStmtClass: EmitIfStmt(cast<IfStmt>(*S)); break; 1315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Stmt::WhileStmtClass: EmitWhileStmt(cast<WhileStmt>(*S)); break; 1325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Stmt::DoStmtClass: EmitDoStmt(cast<DoStmt>(*S)); break; 1335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Stmt::ForStmtClass: EmitForStmt(cast<ForStmt>(*S)); break; 1341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Stmt::ReturnStmtClass: EmitReturnStmt(cast<ReturnStmt>(*S)); break; 136a4275d194b656867bdcdb725b2a7ba3251a1a638Daniel Dunbar 13751b09f2c528c8460b5465c676173324e44176d62Devang Patel case Stmt::SwitchStmtClass: EmitSwitchStmt(cast<SwitchStmt>(*S)); break; 138d1a8d2ef757ad880d48b2249a1619bf8209e9cf8Chad Rosier case Stmt::GCCAsmStmtClass: // Intentional fall-through. 139d1a8d2ef757ad880d48b2249a1619bf8209e9cf8Chad Rosier case Stmt::MSAsmStmtClass: EmitAsmStmt(cast<AsmStmt>(*S)); break; 140051303ce09291dfbed537fa33b0d8a4d92c82b75Tareq A. Siraj case Stmt::CapturedStmtClass: 141051303ce09291dfbed537fa33b0d8a4d92c82b75Tareq A. Siraj EmitCapturedStmt(cast<CapturedStmt>(*S)); 142051303ce09291dfbed537fa33b0d8a4d92c82b75Tareq A. Siraj break; 1430a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar case Stmt::ObjCAtTryStmtClass: 14464d5d6c5903157c521af496479d06dc26032d718Anders Carlsson EmitObjCAtTryStmt(cast<ObjCAtTryStmt>(*S)); 1451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump break; 1460a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar case Stmt::ObjCAtCatchStmtClass: 147b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie llvm_unreachable( 148b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie "@catch statements should be handled by EmitObjCAtTryStmt"); 1490a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar case Stmt::ObjCAtFinallyStmtClass: 150b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie llvm_unreachable( 151b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie "@finally statements should be handled by EmitObjCAtTryStmt"); 1520a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar case Stmt::ObjCAtThrowStmtClass: 15364d5d6c5903157c521af496479d06dc26032d718Anders Carlsson EmitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(*S)); 1540a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar break; 1550a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar case Stmt::ObjCAtSynchronizedStmtClass: 15610cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattner EmitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(*S)); 1570a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar break; 1581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump case Stmt::ObjCForCollectionStmtClass: 1593d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson EmitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(*S)); 1600a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar break; 161f85e193739c953358c865005855253af4f68a497John McCall case Stmt::ObjCAutoreleasePoolStmtClass: 162f85e193739c953358c865005855253af4f68a497John McCall EmitObjCAutoreleasePoolStmt(cast<ObjCAutoreleasePoolStmt>(*S)); 163f85e193739c953358c865005855253af4f68a497John McCall break; 1646f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 1656815e941998659a55c20c147861b0f437928c3d8Anders Carlsson case Stmt::CXXTryStmtClass: 1666815e941998659a55c20c147861b0f437928c3d8Anders Carlsson EmitCXXTryStmt(cast<CXXTryStmt>(*S)); 1676815e941998659a55c20c147861b0f437928c3d8Anders Carlsson break; 168ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith case Stmt::CXXForRangeStmtClass: 169ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith EmitCXXForRangeStmt(cast<CXXForRangeStmt>(*S)); 17028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley case Stmt::SEHTryStmtClass: 17128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley // FIXME Not yet implemented 172ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith break; 1735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 1755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1760912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbarbool CodeGenFunction::EmitSimpleStmt(const Stmt *S) { 1770912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar switch (S->getStmtClass()) { 1780912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar default: return false; 1790912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar case Stmt::NullStmtClass: break; 1800912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar case Stmt::CompoundStmtClass: EmitCompoundStmt(cast<CompoundStmt>(*S)); break; 181d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar case Stmt::DeclStmtClass: EmitDeclStmt(cast<DeclStmt>(*S)); break; 1820912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar case Stmt::LabelStmtClass: EmitLabelStmt(cast<LabelStmt>(*S)); break; 183534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith case Stmt::AttributedStmtClass: 184534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith EmitAttributedStmt(cast<AttributedStmt>(*S)); break; 1850912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar case Stmt::GotoStmtClass: EmitGotoStmt(cast<GotoStmt>(*S)); break; 1860912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar case Stmt::BreakStmtClass: EmitBreakStmt(cast<BreakStmt>(*S)); break; 1870912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar case Stmt::ContinueStmtClass: EmitContinueStmt(cast<ContinueStmt>(*S)); break; 1880912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar case Stmt::DefaultStmtClass: EmitDefaultStmt(cast<DefaultStmt>(*S)); break; 1890912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar case Stmt::CaseStmtClass: EmitCaseStmt(cast<CaseStmt>(*S)); break; 1900912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar } 1910912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar 1920912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar return true; 1930912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar} 1940912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar 1953379320c10001d7e1ee5d7e7142c417f797cfe82Chris Lattner/// EmitCompoundStmt - Emit a compound statement {..} node. If GetLast is true, 1963379320c10001d7e1ee5d7e7142c417f797cfe82Chris Lattner/// this captures the expression result of the last sub-statement and returns it 1973379320c10001d7e1ee5d7e7142c417f797cfe82Chris Lattner/// (for use by the statement expression extension). 1989b65551d0b387a7597fb39356a4d8ef10046445eChris LattnerRValue CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast, 199558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall AggValueSlot AggSlot) { 2007d22bf00dbabca86ba791f56a99e006181fa22ddChris Lattner PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),S.getLBracLoc(), 2017d22bf00dbabca86ba791f56a99e006181fa22ddChris Lattner "LLVM IR generation of compound statement ('{}')"); 2021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 203fdc5d565b30bd2009ec98aac4b5846a740aff767Eric Christopher // Keep track of the current cleanup stack depth, including debug scopes. 204fdc5d565b30bd2009ec98aac4b5846a740aff767Eric Christopher LexicalScope Scope(*this, S.getSourceRange()); 2051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 206a6504853d297c30cfa271f4710af5a3d5db59449David Blaikie return EmitCompoundStmtWithoutScope(S, GetLast, AggSlot); 207a6504853d297c30cfa271f4710af5a3d5db59449David Blaikie} 208a6504853d297c30cfa271f4710af5a3d5db59449David Blaikie 209a6504853d297c30cfa271f4710af5a3d5db59449David BlaikieRValue CodeGenFunction::EmitCompoundStmtWithoutScope(const CompoundStmt &S, bool GetLast, 210a6504853d297c30cfa271f4710af5a3d5db59449David Blaikie AggValueSlot AggSlot) { 211a6504853d297c30cfa271f4710af5a3d5db59449David Blaikie 2123379320c10001d7e1ee5d7e7142c417f797cfe82Chris Lattner for (CompoundStmt::const_body_iterator I = S.body_begin(), 2133379320c10001d7e1ee5d7e7142c417f797cfe82Chris Lattner E = S.body_end()-GetLast; I != E; ++I) 2145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer EmitStmt(*I); 215e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta 21617d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson RValue RV; 2171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (!GetLast) 21817d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson RV = RValue::get(0); 21917d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson else { 2201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // We have to special case labels here. They are statements, but when put 22117d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson // at the end of a statement expression, they yield the value of their 22217d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson // subexpression. Handle this by walking through all labels we encounter, 22317d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson // emitting them before we evaluate the subexpr. 22417d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson const Stmt *LastStmt = S.body_back(); 22517d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson while (const LabelStmt *LS = dyn_cast<LabelStmt>(LastStmt)) { 226ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner EmitLabel(LS->getDecl()); 22717d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson LastStmt = LS->getSubStmt(); 22817d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson } 2291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 23017d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson EnsureInsertPoint(); 2311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 232558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall RV = EmitAnyExpr(cast<Expr>(LastStmt), AggSlot); 23391d723da7b68be5245c3ac58aa2a36d04658cfb8Chris Lattner } 234a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar 23517d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson return RV; 2365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 2375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 238aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbarvoid CodeGenFunction::SimplifyForwardingBlocks(llvm::BasicBlock *BB) { 239aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar llvm::BranchInst *BI = dyn_cast<llvm::BranchInst>(BB->getTerminator()); 2401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 241aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar // If there is a cleanup stack, then we it isn't worth trying to 242aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar // simplify this block (we would need to remove it from the scope map 243aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar // and cleanup entry). 244f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall if (!EHStack.empty()) 245aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar return; 246aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar 247aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar // Can only simplify direct branches. 248aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar if (!BI || !BI->isUnconditional()) 249aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar return; 250aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar 2513d7c780d93ebcb84a1224de8c44835ea43e82b15Eli Friedman // Can only simplify empty blocks. 2523d7c780d93ebcb84a1224de8c44835ea43e82b15Eli Friedman if (BI != BB->begin()) 2533d7c780d93ebcb84a1224de8c44835ea43e82b15Eli Friedman return; 2543d7c780d93ebcb84a1224de8c44835ea43e82b15Eli Friedman 255aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar BB->replaceAllUsesWith(BI->getSuccessor(0)); 256aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar BI->eraseFromParent(); 257aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar BB->eraseFromParent(); 258aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar} 259aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar 260a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbarvoid CodeGenFunction::EmitBlock(llvm::BasicBlock *BB, bool IsFinished) { 261548ce5e78215a34f409d597bb2c1e9f897a8eda3John McCall llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); 262548ce5e78215a34f409d597bb2c1e9f897a8eda3John McCall 263d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar // Fall out of the current block (if necessary). 264d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar EmitBranch(BB); 265a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar 266a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar if (IsFinished && BB->use_empty()) { 267a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar delete BB; 268a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar return; 269a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar } 270a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar 271839cbaa004a24e8f1ea14db5ed76e3d25ed28996John McCall // Place the block after the current block, if possible, or else at 272839cbaa004a24e8f1ea14db5ed76e3d25ed28996John McCall // the end of the function. 273548ce5e78215a34f409d597bb2c1e9f897a8eda3John McCall if (CurBB && CurBB->getParent()) 274548ce5e78215a34f409d597bb2c1e9f897a8eda3John McCall CurFn->getBasicBlockList().insertAfter(CurBB, BB); 275839cbaa004a24e8f1ea14db5ed76e3d25ed28996John McCall else 276839cbaa004a24e8f1ea14db5ed76e3d25ed28996John McCall CurFn->getBasicBlockList().push_back(BB); 277d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar Builder.SetInsertPoint(BB); 278d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar} 279d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar 280d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbarvoid CodeGenFunction::EmitBranch(llvm::BasicBlock *Target) { 281d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar // Emit a branch from the current block to the target one if this 282d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar // was a real block. If this was just a fall-through block after a 283d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar // terminator, don't emit it. 284d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); 285d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar 286d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar if (!CurBB || CurBB->getTerminator()) { 287d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar // If there is no insert point or the previous block is already 288d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar // terminated, don't touch it. 2895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 2905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, create a fall-through branch. 291d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar Builder.CreateBr(Target); 2925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 2935e08ad3cc62ab94649959ae227a9a411a729bf49Daniel Dunbar 2945e08ad3cc62ab94649959ae227a9a411a729bf49Daniel Dunbar Builder.ClearInsertionPoint(); 2955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 2965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 297777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCallvoid CodeGenFunction::EmitBlockAfterUses(llvm::BasicBlock *block) { 298777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall bool inserted = false; 299777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall for (llvm::BasicBlock::use_iterator 300777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall i = block->use_begin(), e = block->use_end(); i != e; ++i) { 301777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall if (llvm::Instruction *insn = dyn_cast<llvm::Instruction>(*i)) { 302777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall CurFn->getBasicBlockList().insertAfter(insn->getParent(), block); 303777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall inserted = true; 304777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall break; 305777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall } 306777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall } 307777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall 308777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall if (!inserted) 309777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall CurFn->getBasicBlockList().push_back(block); 310777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall 311777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall Builder.SetInsertPoint(block); 312777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall} 313777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall 314f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallCodeGenFunction::JumpDest 315ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris LattnerCodeGenFunction::getJumpDestForLabel(const LabelDecl *D) { 316ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner JumpDest &Dest = LabelMap[D]; 317ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall if (Dest.isValid()) return Dest; 318f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall 319f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall // Create, but don't insert, the new block. 320ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner Dest = JumpDest(createBasicBlock(D->getName()), 321ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall EHScopeStack::stable_iterator::invalid(), 322ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall NextCleanupDestIndex++); 323f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall return Dest; 324f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall} 325f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall 326ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattnervoid CodeGenFunction::EmitLabel(const LabelDecl *D) { 327495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem // Add this label to the current lexical scope if we're within any 328495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem // normal cleanups. Jumps "in" to this label --- when permitted by 329495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem // the language --- may need to be routed around such cleanups. 330495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem if (EHStack.hasNormalCleanups() && CurLexicalScope) 331495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem CurLexicalScope->addLabel(D); 332495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem 333ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner JumpDest &Dest = LabelMap[D]; 334f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall 335ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall // If we didn't need a forward reference to this label, just go 336f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall // ahead and create a destination at the current scope. 337ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall if (!Dest.isValid()) { 338ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner Dest = getJumpDestInCurrentScope(D->getName()); 339f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall 340f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall // Otherwise, we need to give this label a target depth and remove 341f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall // it from the branch-fixups list. 342f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall } else { 343ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall assert(!Dest.getScopeDepth().isValid() && "already emitted label!"); 344495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem Dest.setScopeDepth(EHStack.stable_begin()); 345ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall ResolveBranchFixups(Dest.getBlock()); 346f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall } 347f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall 348ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall EmitBlock(Dest.getBlock()); 34991d723da7b68be5245c3ac58aa2a36d04658cfb8Chris Lattner} 35091d723da7b68be5245c3ac58aa2a36d04658cfb8Chris Lattner 351495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem/// Change the cleanup scope of the labels in this lexical scope to 352495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem/// match the scope of the enclosing context. 353495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotemvoid CodeGenFunction::LexicalScope::rescopeLabels() { 354495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem assert(!Labels.empty()); 355495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem EHScopeStack::stable_iterator innermostScope 356495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem = CGF.EHStack.getInnermostNormalCleanup(); 357495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem 358495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem // Change the scope depth of all the labels. 359495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem for (SmallVectorImpl<const LabelDecl*>::const_iterator 360495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem i = Labels.begin(), e = Labels.end(); i != e; ++i) { 361495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem assert(CGF.LabelMap.count(*i)); 362495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem JumpDest &dest = CGF.LabelMap.find(*i)->second; 363495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem assert(dest.getScopeDepth().isValid()); 364495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem assert(innermostScope.encloses(dest.getScopeDepth())); 365495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem dest.setScopeDepth(innermostScope); 366495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem } 367495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem 368495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem // Reparent the labels if the new scope also has cleanups. 369495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem if (innermostScope != EHScopeStack::stable_end() && ParentScope) { 370495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem ParentScope->Labels.append(Labels.begin(), Labels.end()); 371495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem } 372495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem} 373495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem 37491d723da7b68be5245c3ac58aa2a36d04658cfb8Chris Lattner 37591d723da7b68be5245c3ac58aa2a36d04658cfb8Chris Lattnervoid CodeGenFunction::EmitLabelStmt(const LabelStmt &S) { 376ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner EmitLabel(S.getDecl()); 3775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer EmitStmt(S.getSubStmt()); 3785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 380534986f2b21e6050bf00163cd6423fd92155a6edRichard Smithvoid CodeGenFunction::EmitAttributedStmt(const AttributedStmt &S) { 381534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith EmitStmt(S.getSubStmt()); 382534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith} 383534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith 3845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid CodeGenFunction::EmitGotoStmt(const GotoStmt &S) { 3850912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar // If this code is reachable then emit a stop point (if generating 3860912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar // debug info). We have to do this ourselves because we are on the 3870912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar // "simple" statement path. 3880912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar if (HaveInsertPoint()) 3890912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar EmitStopPoint(&S); 39036a2ada69fdb457b0e46d0ef452c150b360d8888Mike Stump 391f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall EmitBranchThroughCleanup(getJumpDestForLabel(S.getLabel())); 3925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3943d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner 3950ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbarvoid CodeGenFunction::EmitIndirectGotoStmt(const IndirectGotoStmt &S) { 396ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner if (const LabelDecl *Target = S.getConstantTarget()) { 39795c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall EmitBranchThroughCleanup(getJumpDestForLabel(Target)); 39895c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall return; 39995c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall } 40095c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall 40149c952f853fe2d15dd9c9ff2a29c696bd18fca13Chris Lattner // Ensure that we have an i8* for our PHI node. 402d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner llvm::Value *V = Builder.CreateBitCast(EmitScalarExpr(S.getTarget()), 403d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall Int8PtrTy, "addr"); 4043d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); 4053d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner 4063d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner // Get the basic block for the indirect goto. 4073d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner llvm::BasicBlock *IndGotoBB = GetIndirectGotoBlock(); 4086f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 4093d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner // The first instruction in the block has to be the PHI for the switch dest, 4103d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner // add an entry for this branch. 4113d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner cast<llvm::PHINode>(IndGotoBB->begin())->addIncoming(V, CurBB); 4126f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 4133d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner EmitBranch(IndGotoBB); 4140ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar} 4150ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar 41662b72f642207ba2ba433d686df924dc9594e9897Chris Lattnervoid CodeGenFunction::EmitIfStmt(const IfStmt &S) { 4175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.8.4.1: The first substatement is executed if the expression compares 4185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // unequal to 0. The condition must be a scalar type. 419f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall RunCleanupsScope ConditionScope(*this); 42001234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor 4218cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor if (S.getConditionVariable()) 422b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall EmitAutoVarDecl(*S.getConditionVariable()); 4231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4249bc47e29dce8f095be7a6d07dbb02a5a7a112949Chris Lattner // If the condition constant folds and can be elided, try to avoid emitting 4259bc47e29dce8f095be7a6d07dbb02a5a7a112949Chris Lattner // the condition and the dead arm of the if/else. 426c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner bool CondConstant; 427c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner if (ConstantFoldsToSimpleInteger(S.getCond(), CondConstant)) { 42862b72f642207ba2ba433d686df924dc9594e9897Chris Lattner // Figure out which block (then or else) is executed. 429c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner const Stmt *Executed = S.getThen(); 430c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner const Stmt *Skipped = S.getElse(); 431c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner if (!CondConstant) // Condition false? 43262b72f642207ba2ba433d686df924dc9594e9897Chris Lattner std::swap(Executed, Skipped); 4331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 43462b72f642207ba2ba433d686df924dc9594e9897Chris Lattner // If the skipped block has no labels in it, just emit the executed block. 43562b72f642207ba2ba433d686df924dc9594e9897Chris Lattner // This avoids emitting dead code and simplifies the CFG substantially. 4369bc47e29dce8f095be7a6d07dbb02a5a7a112949Chris Lattner if (!ContainsLabel(Skipped)) { 43701234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor if (Executed) { 438f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall RunCleanupsScope ExecutedScope(*this); 43962b72f642207ba2ba433d686df924dc9594e9897Chris Lattner EmitStmt(Executed); 44001234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor } 44162b72f642207ba2ba433d686df924dc9594e9897Chris Lattner return; 44262b72f642207ba2ba433d686df924dc9594e9897Chris Lattner } 44362b72f642207ba2ba433d686df924dc9594e9897Chris Lattner } 4449bc47e29dce8f095be7a6d07dbb02a5a7a112949Chris Lattner 4459bc47e29dce8f095be7a6d07dbb02a5a7a112949Chris Lattner // Otherwise, the condition did not fold, or we couldn't elide it. Just emit 4469bc47e29dce8f095be7a6d07dbb02a5a7a112949Chris Lattner // the conditional branch. 447781d7ca9b2fd626ef34bdc3fe06765eeff7ab2bcDaniel Dunbar llvm::BasicBlock *ThenBlock = createBasicBlock("if.then"); 448781d7ca9b2fd626ef34bdc3fe06765eeff7ab2bcDaniel Dunbar llvm::BasicBlock *ContBlock = createBasicBlock("if.end"); 449781d7ca9b2fd626ef34bdc3fe06765eeff7ab2bcDaniel Dunbar llvm::BasicBlock *ElseBlock = ContBlock; 4505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (S.getElse()) 451781d7ca9b2fd626ef34bdc3fe06765eeff7ab2bcDaniel Dunbar ElseBlock = createBasicBlock("if.else"); 452781d7ca9b2fd626ef34bdc3fe06765eeff7ab2bcDaniel Dunbar EmitBranchOnBoolExpr(S.getCond(), ThenBlock, ElseBlock); 4531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Emit the 'then' code. 45501234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor EmitBlock(ThenBlock); 45601234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor { 457f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall RunCleanupsScope ThenScope(*this); 45801234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor EmitStmt(S.getThen()); 45901234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor } 460d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar EmitBranch(ContBlock); 4611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Emit the 'else' code if present. 4635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (const Stmt *Else = S.getElse()) { 464acd723666777b4ac1f79a97b6a300e6cf919d519Devang Patel // There is no need to emit line number for unconditional branch. 465acd723666777b4ac1f79a97b6a300e6cf919d519Devang Patel if (getDebugInfo()) 466acd723666777b4ac1f79a97b6a300e6cf919d519Devang Patel Builder.SetCurrentDebugLocation(llvm::DebugLoc()); 4675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer EmitBlock(ElseBlock); 46801234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor { 469f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall RunCleanupsScope ElseScope(*this); 47001234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor EmitStmt(Else); 47101234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor } 472acd723666777b4ac1f79a97b6a300e6cf919d519Devang Patel // There is no need to emit line number for unconditional branch. 473acd723666777b4ac1f79a97b6a300e6cf919d519Devang Patel if (getDebugInfo()) 474acd723666777b4ac1f79a97b6a300e6cf919d519Devang Patel Builder.SetCurrentDebugLocation(llvm::DebugLoc()); 475d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar EmitBranch(ContBlock); 4765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 4771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Emit the continuation block for code after the if. 479c22d665ede76f70228055d638a087f4bd438292dDaniel Dunbar EmitBlock(ContBlock, true); 4805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 4815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid CodeGenFunction::EmitWhileStmt(const WhileStmt &S) { 483f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall // Emit the header for the loop, which will also become 484f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall // the continue target. 485f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall JumpDest LoopHeader = getJumpDestInCurrentScope("while.cond"); 486ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall EmitBlock(LoopHeader.getBlock()); 487f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall 488f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall // Create an exit block for when the condition fails, which will 489f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall // also become the break target. 490f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall JumpDest LoopExit = getJumpDestInCurrentScope("while.end"); 49172cac2ccce8058833f56358e3391e28a8ddeeaa4Mike Stump 49272cac2ccce8058833f56358e3391e28a8ddeeaa4Mike Stump // Store the blocks to use for break and continue. 493f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall BreakContinueStack.push_back(BreakContinue(LoopExit, LoopHeader)); 4941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4955656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor // C++ [stmt.while]p2: 4965656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor // When the condition of a while statement is a declaration, the 4975656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor // scope of the variable that is declared extends from its point 4985656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor // of declaration (3.3.2) to the end of the while statement. 4995656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor // [...] 5005656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor // The object created in a condition is destroyed and created 5015656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor // with each iteration of the loop. 502f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall RunCleanupsScope ConditionScope(*this); 5035656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor 504f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall if (S.getConditionVariable()) 505b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall EmitAutoVarDecl(*S.getConditionVariable()); 5066f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 50716b16206741f5139c4ad870632db8f9ea4c6c943Mike Stump // Evaluate the conditional in the while header. C99 6.8.5.1: The 50816b16206741f5139c4ad870632db8f9ea4c6c943Mike Stump // evaluation of the controlling expression takes place before each 50916b16206741f5139c4ad870632db8f9ea4c6c943Mike Stump // execution of the loop body. 5105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); 5116f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 5122c30d8fee8981ca4f20a477456dae1b722b53f1dDevang Patel // while(1) is common, avoid extra exit blocks. Be sure 5135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // to correctly handle break/continue though. 5142c30d8fee8981ca4f20a477456dae1b722b53f1dDevang Patel bool EmitBoolCondBranch = true; 5151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal)) 5162c30d8fee8981ca4f20a477456dae1b722b53f1dDevang Patel if (C->isOne()) 5172c30d8fee8981ca4f20a477456dae1b722b53f1dDevang Patel EmitBoolCondBranch = false; 5181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // As long as the condition is true, go to the loop body. 520f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall llvm::BasicBlock *LoopBody = createBasicBlock("while.body"); 521f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall if (EmitBoolCondBranch) { 522ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall llvm::BasicBlock *ExitBlock = LoopExit.getBlock(); 523f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall if (ConditionScope.requiresCleanups()) 524f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall ExitBlock = createBasicBlock("while.exit"); 525f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall 526f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock); 527f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall 528ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall if (ExitBlock != LoopExit.getBlock()) { 529f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall EmitBlock(ExitBlock); 530f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall EmitBranchThroughCleanup(LoopExit); 531f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall } 532f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall } 5336f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 534f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall // Emit the loop body. We have to emit this in a cleanup scope 535f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall // because it might be a singleton DeclStmt. 5365656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor { 537f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall RunCleanupsScope BodyScope(*this); 5385656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor EmitBlock(LoopBody); 5395656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor EmitStmt(S.getBody()); 5405656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor } 541da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner 5421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump BreakContinueStack.pop_back(); 5431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 544f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall // Immediately force cleanup. 545f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall ConditionScope.ForceCleanup(); 5465656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor 547f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall // Branch to the loop header again. 548ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall EmitBranch(LoopHeader.getBlock()); 5491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Emit the exit block. 551ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall EmitBlock(LoopExit.getBlock(), true); 5525656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor 553aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar // The LoopHeader typically is just a branch if we skipped emitting 554aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar // a branch, try to erase it. 555f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall if (!EmitBoolCondBranch) 556ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall SimplifyForwardingBlocks(LoopHeader.getBlock()); 5575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 5585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 5595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid CodeGenFunction::EmitDoStmt(const DoStmt &S) { 560f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall JumpDest LoopExit = getJumpDestInCurrentScope("do.end"); 561f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall JumpDest LoopCond = getJumpDestInCurrentScope("do.cond"); 5621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 563da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner // Store the blocks to use for break and continue. 564f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall BreakContinueStack.push_back(BreakContinue(LoopExit, LoopCond)); 5651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 566f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall // Emit the body of the loop. 567f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall llvm::BasicBlock *LoopBody = createBasicBlock("do.body"); 568f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall EmitBlock(LoopBody); 569f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall { 570f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall RunCleanupsScope BodyScope(*this); 571f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall EmitStmt(S.getBody()); 572f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall } 5731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 574e4b6d342c29d5cb9d311756100df1603810fa892Anders Carlsson BreakContinueStack.pop_back(); 5751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 576ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall EmitBlock(LoopCond.getBlock()); 5771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.8.5.2: "The evaluation of the controlling expression takes place 5795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // after each execution of the loop body." 5801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Evaluate the conditional in the while header. 5825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.8.5p2/p4: The first substatement is executed if the expression 5835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // compares unequal to 0. The condition must be a scalar type. 5845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); 58505f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel 58605f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel // "do {} while (0)" is common in macros, avoid extra blocks. Be sure 58705f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel // to correctly handle break/continue though. 58805f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel bool EmitBoolCondBranch = true; 5891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal)) 59005f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel if (C->isZero()) 59105f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel EmitBoolCondBranch = false; 59205f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel 5935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // As long as the condition is true, iterate the loop. 59405f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel if (EmitBoolCondBranch) 595ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall Builder.CreateCondBr(BoolCondVal, LoopBody, LoopExit.getBlock()); 5961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Emit the exit block. 598ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall EmitBlock(LoopExit.getBlock()); 59905f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel 600aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar // The DoCond block typically is just a branch if we skipped 601aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar // emitting a branch, try to erase it. 602aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar if (!EmitBoolCondBranch) 603ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall SimplifyForwardingBlocks(LoopCond.getBlock()); 6045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 6055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 6065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid CodeGenFunction::EmitForStmt(const ForStmt &S) { 607f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall JumpDest LoopExit = getJumpDestInCurrentScope("for.end"); 608f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall 609f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall RunCleanupsScope ForScope(*this); 610da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner 6110554e0e30d24d9ad7d5e12f8e7583ebb5c9715bfDevang Patel CGDebugInfo *DI = getDebugInfo(); 61273fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher if (DI) 61373fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher DI->EmitLexicalBlockStart(Builder, S.getSourceRange().getBegin()); 6140554e0e30d24d9ad7d5e12f8e7583ebb5c9715bfDevang Patel 6155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Evaluate the first part before the loop. 6165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (S.getInit()) 6175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer EmitStmt(S.getInit()); 6185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 6195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Start the loop with a block that tests the condition. 620f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall // If there's an increment, the continue scope will be overwritten 621f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall // later. 622f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall JumpDest Continue = getJumpDestInCurrentScope("for.cond"); 623ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall llvm::BasicBlock *CondBlock = Continue.getBlock(); 6245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer EmitBlock(CondBlock); 6255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 626d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor // Create a cleanup scope for the condition variable cleanups. 627f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall RunCleanupsScope ConditionScope(*this); 6286f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 629d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor llvm::Value *BoolCondVal = 0; 6305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (S.getCond()) { 63199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor // If the for statement has a condition scope, emit the local variable 63299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor // declaration. 633ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall llvm::BasicBlock *ExitBlock = LoopExit.getBlock(); 634d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor if (S.getConditionVariable()) { 635b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall EmitAutoVarDecl(*S.getConditionVariable()); 636d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor } 637f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall 638f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall // If there are any cleanups between here and the loop-exit scope, 639f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall // create a block to stage a loop exit along. 640f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall if (ForScope.requiresCleanups()) 641f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall ExitBlock = createBasicBlock("for.cond.cleanup"); 6426f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 64331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // As long as the condition is true, iterate the loop. 6449615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar llvm::BasicBlock *ForBody = createBasicBlock("for.body"); 6451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.8.5p2/p4: The first substatement is executed if the expression 6475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // compares unequal to 0. The condition must be a scalar type. 648d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor BoolCondVal = EvaluateExprAsBool(S.getCond()); 649f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock); 650f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall 651ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall if (ExitBlock != LoopExit.getBlock()) { 652f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall EmitBlock(ExitBlock); 653f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall EmitBranchThroughCleanup(LoopExit); 654f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall } 6551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump EmitBlock(ForBody); 6575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 6585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Treat it as a non-zero constant. Don't even create a new block for the 6595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // body, just fall into it. 6605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 6615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 6621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // If the for loop doesn't have an increment we can just use the 663f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall // condition as the continue block. Otherwise we'll need to create 664f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall // a block for it (in the current scope, i.e. in the scope of the 665f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall // condition), and that we will become our continue block. 666da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner if (S.getInc()) 667f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall Continue = getJumpDestInCurrentScope("for.inc"); 6681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 669da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner // Store the blocks to use for break and continue. 670f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); 6713e9da66ac7e88d64d30ee777588677320660cf84Mike Stump 672d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor { 673d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor // Create a separate cleanup scope for the body, in case it is not 674d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor // a compound statement. 675f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall RunCleanupsScope BodyScope(*this); 676d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor EmitStmt(S.getBody()); 677d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor } 678da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner 6795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If there is an increment, emit it next. 680ad12b6d643aba6c36f5cec4c9beb4977a12eace4Daniel Dunbar if (S.getInc()) { 681ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall EmitBlock(Continue.getBlock()); 682883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner EmitStmt(S.getInc()); 683ad12b6d643aba6c36f5cec4c9beb4977a12eace4Daniel Dunbar } 6841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 68545d3fe1898d3726d269a0bd2ccb8527102e29d79Douglas Gregor BreakContinueStack.pop_back(); 686f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall 687f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall ConditionScope.ForceCleanup(); 688f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall EmitBranch(CondBlock); 689f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall 690f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall ForScope.ForceCleanup(); 691f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall 69273fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher if (DI) 69373fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher DI->EmitLexicalBlockEnd(Builder, S.getSourceRange().getEnd()); 6945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 695da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner // Emit the fall-through block. 696ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall EmitBlock(LoopExit.getBlock(), true); 6975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 6985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 699ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithvoid CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S) { 700ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith JumpDest LoopExit = getJumpDestInCurrentScope("for.end"); 701ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 702ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith RunCleanupsScope ForScope(*this); 703ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 704ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith CGDebugInfo *DI = getDebugInfo(); 70573fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher if (DI) 70673fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher DI->EmitLexicalBlockStart(Builder, S.getSourceRange().getBegin()); 707ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 708ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Evaluate the first pieces before the loop. 709ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith EmitStmt(S.getRangeStmt()); 710ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith EmitStmt(S.getBeginEndStmt()); 711ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 712ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Start the loop with a block that tests the condition. 713ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // If there's an increment, the continue scope will be overwritten 714ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // later. 715ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); 716ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith EmitBlock(CondBlock); 717ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 718ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // If there are any cleanups between here and the loop-exit scope, 719ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // create a block to stage a loop exit along. 720ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith llvm::BasicBlock *ExitBlock = LoopExit.getBlock(); 721ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (ForScope.requiresCleanups()) 722ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ExitBlock = createBasicBlock("for.cond.cleanup"); 7236f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 724ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // The loop body, consisting of the specified body and the loop variable. 725ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith llvm::BasicBlock *ForBody = createBasicBlock("for.body"); 726ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 727ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // The body is executed if the expression, contextually converted 728ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // to bool, is true. 729ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); 730ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock); 731ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 732ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (ExitBlock != LoopExit.getBlock()) { 733ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith EmitBlock(ExitBlock); 734ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith EmitBranchThroughCleanup(LoopExit); 735ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 736ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 737ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith EmitBlock(ForBody); 738ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 739ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Create a block for the increment. In case of a 'continue', we jump there. 740ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith JumpDest Continue = getJumpDestInCurrentScope("for.inc"); 741ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 742ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Store the blocks to use for break and continue. 743ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); 744ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 745ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith { 746ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Create a separate cleanup scope for the loop variable and body. 747ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith RunCleanupsScope BodyScope(*this); 748ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith EmitStmt(S.getLoopVarStmt()); 749ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith EmitStmt(S.getBody()); 750ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 751ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 752ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // If there is an increment, emit it next. 753ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith EmitBlock(Continue.getBlock()); 754ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith EmitStmt(S.getInc()); 755ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 756ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BreakContinueStack.pop_back(); 757ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 758ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith EmitBranch(CondBlock); 759ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 760ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ForScope.ForceCleanup(); 761ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 76273fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher if (DI) 76373fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher DI->EmitLexicalBlockEnd(Builder, S.getSourceRange().getEnd()); 764ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 765ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Emit the fall-through block. 766ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith EmitBlock(LoopExit.getBlock(), true); 767ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 768ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 76929e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbarvoid CodeGenFunction::EmitReturnOfRValue(RValue RV, QualType Ty) { 77029e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar if (RV.isScalar()) { 77129e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar Builder.CreateStore(RV.getScalarVal(), ReturnValue); 77229e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar } else if (RV.isAggregate()) { 773649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier EmitAggregateCopy(ReturnValue, RV.getAggregateAddr(), Ty); 77429e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar } else { 7759d232c884ea9872d6555df0fd7359699819bc1f1John McCall EmitStoreOfComplex(RV.getComplexVal(), 7769d232c884ea9872d6555df0fd7359699819bc1f1John McCall MakeNaturalAlignAddrLValue(ReturnValue, Ty), 7779d232c884ea9872d6555df0fd7359699819bc1f1John McCall /*init*/ true); 77829e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar } 77982d8ef0be44ddba608c1ce5c8b6b48da83bc1821Anders Carlsson EmitBranchThroughCleanup(ReturnBlock); 78029e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar} 78129e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar 7825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// EmitReturnStmt - Note that due to GCC extensions, this can have an operand 7835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// if the function returns void, or may be missing one if the function returns 7845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// non-void. Fun stuff :). 7855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) { 7865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Emit the result value, even if unused, to evalute the side effects. 7875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const Expr *RV = S.getRetValue(); 7881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7899f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall // Treat block literals in a return expression as if they appeared 7909f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall // in their own scope. This permits a small, easily-implemented 7919f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall // exception to our over-conservative rules about not jumping to 7929f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall // statements following block literals with non-trivial cleanups. 7939f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall RunCleanupsScope cleanupScope(*this); 7949f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall if (const ExprWithCleanups *cleanups = 7959f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall dyn_cast_or_null<ExprWithCleanups>(RV)) { 7969f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall enterFullExpression(cleanups); 7979f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall RV = cleanups->getSubExpr(); 7989f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall } 7999f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall 8005ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar // FIXME: Clean this up by using an LValue for ReturnTemp, 8015ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar // EmitStoreThroughLValue, and EmitAnyExpr. 8026c82fc65e45c668a36b06536e66563fe937a3096Rafael Espindola if (S.getNRVOCandidate() && S.getNRVOCandidate()->isNRVOVariable()) { 803d86c477fb5d3fc34864afecbbb5443da9355e8fbDouglas Gregor // Apply the named return value optimization for this return statement, 804d86c477fb5d3fc34864afecbbb5443da9355e8fbDouglas Gregor // which means doing nothing: the appropriate result has already been 805d86c477fb5d3fc34864afecbbb5443da9355e8fbDouglas Gregor // constructed into the NRVO variable. 8066f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 8073d91bbcdab155181556969cad6ec97014405acedDouglas Gregor // If there is an NRVO flag for this variable, set it to 1 into indicate 8083d91bbcdab155181556969cad6ec97014405acedDouglas Gregor // that the cleanup code should not destroy the variable. 809d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall if (llvm::Value *NRVOFlag = NRVOFlags[S.getNRVOCandidate()]) 810d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall Builder.CreateStore(Builder.getTrue(), NRVOFlag); 811d86c477fb5d3fc34864afecbbb5443da9355e8fbDouglas Gregor } else if (!ReturnValue) { 8125ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar // Make sure not to return anything, but evaluate the expression 8135ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar // for side effects. 8145ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar if (RV) 815144ac61f9005a0da4327d4e62a4c453923b7bc0cEli Friedman EmitAnyExpr(RV); 8165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (RV == 0) { 8175ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar // Do nothing (return value is left uninitialized) 818d54b6ac2f4f6f0bd0076cbfa885b57277066f06cEli Friedman } else if (FnRetTy->isReferenceType()) { 819d54b6ac2f4f6f0bd0076cbfa885b57277066f06cEli Friedman // If this function returns a reference, take the address of the expression 820d54b6ac2f4f6f0bd0076cbfa885b57277066f06cEli Friedman // rather than the value. 82132f36baa6c8d491c374af622b4e3ac28d597453cAnders Carlsson RValue Result = EmitReferenceBindingToExpr(RV, /*InitializedDecl=*/0); 82233fd1fc1814a5573c972840d49317989e20deaceDouglas Gregor Builder.CreateStore(Result.getScalarVal(), ReturnValue); 8235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 8249d232c884ea9872d6555df0fd7359699819bc1f1John McCall switch (getEvaluationKind(RV->getType())) { 8259d232c884ea9872d6555df0fd7359699819bc1f1John McCall case TEK_Scalar: 8269d232c884ea9872d6555df0fd7359699819bc1f1John McCall Builder.CreateStore(EmitScalarExpr(RV), ReturnValue); 8279d232c884ea9872d6555df0fd7359699819bc1f1John McCall break; 8289d232c884ea9872d6555df0fd7359699819bc1f1John McCall case TEK_Complex: 8299d232c884ea9872d6555df0fd7359699819bc1f1John McCall EmitComplexExprIntoLValue(RV, 8309d232c884ea9872d6555df0fd7359699819bc1f1John McCall MakeNaturalAlignAddrLValue(ReturnValue, RV->getType()), 8319d232c884ea9872d6555df0fd7359699819bc1f1John McCall /*isInit*/ true); 8329d232c884ea9872d6555df0fd7359699819bc1f1John McCall break; 8339d232c884ea9872d6555df0fd7359699819bc1f1John McCall case TEK_Aggregate: { 8349d232c884ea9872d6555df0fd7359699819bc1f1John McCall CharUnits Alignment = getContext().getTypeAlignInChars(RV->getType()); 8359d232c884ea9872d6555df0fd7359699819bc1f1John McCall EmitAggExpr(RV, AggValueSlot::forAddr(ReturnValue, Alignment, 8369d232c884ea9872d6555df0fd7359699819bc1f1John McCall Qualifiers(), 8379d232c884ea9872d6555df0fd7359699819bc1f1John McCall AggValueSlot::IsDestructed, 8389d232c884ea9872d6555df0fd7359699819bc1f1John McCall AggValueSlot::DoesNotNeedGCBarriers, 8399d232c884ea9872d6555df0fd7359699819bc1f1John McCall AggValueSlot::IsNotAliased)); 8409d232c884ea9872d6555df0fd7359699819bc1f1John McCall break; 8419d232c884ea9872d6555df0fd7359699819bc1f1John McCall } 8429d232c884ea9872d6555df0fd7359699819bc1f1John McCall } 8435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 844144ac61f9005a0da4327d4e62a4c453923b7bc0cEli Friedman 845fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl if (RV == 0 || RV->isEvaluatable(getContext())) 846fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl ++NumSimpleReturnExprs; 847fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl 8489f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall cleanupScope.ForceCleanup(); 84982d8ef0be44ddba608c1ce5c8b6b48da83bc1821Anders Carlsson EmitBranchThroughCleanup(ReturnBlock); 8505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 8515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 8525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid CodeGenFunction::EmitDeclStmt(const DeclStmt &S) { 8539198126067a447f8aaccf9fff09be294c8bcb81eDevang Patel // As long as debug info is modeled with instructions, we have to ensure we 8549198126067a447f8aaccf9fff09be294c8bcb81eDevang Patel // have a place to insert here and write the stop point here. 8552b124ea9d2c27c6d002ecd4623f6321e305d907eEric Christopher if (HaveInsertPoint()) 8569198126067a447f8aaccf9fff09be294c8bcb81eDevang Patel EmitStopPoint(&S); 8579198126067a447f8aaccf9fff09be294c8bcb81eDevang Patel 858e4ea1f42c97a436df3e0ae8e129e6bc624ee6790Ted Kremenek for (DeclStmt::const_decl_iterator I = S.decl_begin(), E = S.decl_end(); 859e4ea1f42c97a436df3e0ae8e129e6bc624ee6790Ted Kremenek I != E; ++I) 860e4ea1f42c97a436df3e0ae8e129e6bc624ee6790Ted Kremenek EmitDecl(**I); 8616fa5f0943a84233b2e1ec9716eae55643225bfd4Chris Lattner} 862da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner 8630912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbarvoid CodeGenFunction::EmitBreakStmt(const BreakStmt &S) { 864da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!"); 865da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner 8660912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar // If this code is reachable then emit a stop point (if generating 8670912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar // debug info). We have to do this ourselves because we are on the 8680912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar // "simple" statement path. 8690912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar if (HaveInsertPoint()) 8700912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar EmitStopPoint(&S); 871ec9771d57f94cc204491b3174e88069d08cdd684Mike Stump 872f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall JumpDest Block = BreakContinueStack.back().BreakBlock; 87382d8ef0be44ddba608c1ce5c8b6b48da83bc1821Anders Carlsson EmitBranchThroughCleanup(Block); 874da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner} 875da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner 8760912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbarvoid CodeGenFunction::EmitContinueStmt(const ContinueStmt &S) { 877da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner assert(!BreakContinueStack.empty() && "continue stmt not in a loop!"); 878da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner 8790912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar // If this code is reachable then emit a stop point (if generating 8800912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar // debug info). We have to do this ourselves because we are on the 8810912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar // "simple" statement path. 8820912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar if (HaveInsertPoint()) 8830912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar EmitStopPoint(&S); 884ec9771d57f94cc204491b3174e88069d08cdd684Mike Stump 885f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall JumpDest Block = BreakContinueStack.back().ContinueBlock; 88682d8ef0be44ddba608c1ce5c8b6b48da83bc1821Anders Carlsson EmitBranchThroughCleanup(Block); 887da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner} 88851b09f2c528c8460b5465c676173324e44176d62Devang Patel 889c049e4f406a7f7179eba98659044a32508e53289Devang Patel/// EmitCaseStmtRange - If case statement range is not too big then 890c049e4f406a7f7179eba98659044a32508e53289Devang Patel/// add multiple cases to switch instruction, one for each value within 891c049e4f406a7f7179eba98659044a32508e53289Devang Patel/// the range. If range is too big then emit "if" condition check. 892c049e4f406a7f7179eba98659044a32508e53289Devang Patelvoid CodeGenFunction::EmitCaseStmtRange(const CaseStmt &S) { 8934efde8d2c10b091bc9588de18c2c71dca2979e49Daniel Dunbar assert(S.getRHS() && "Expected RHS value in CaseStmt"); 894c049e4f406a7f7179eba98659044a32508e53289Devang Patel 895a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith llvm::APSInt LHS = S.getLHS()->EvaluateKnownConstInt(getContext()); 896a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith llvm::APSInt RHS = S.getRHS()->EvaluateKnownConstInt(getContext()); 8974efde8d2c10b091bc9588de18c2c71dca2979e49Daniel Dunbar 89816f23570999cac1fa13597386938450843003840Daniel Dunbar // Emit the code for this case. We do this first to make sure it is 89916f23570999cac1fa13597386938450843003840Daniel Dunbar // properly chained from our predecessor before generating the 90016f23570999cac1fa13597386938450843003840Daniel Dunbar // switch machinery to enter this block. 901f84dcda7e2ab2f6d5be5a8c52d22ef4c442dd762Daniel Dunbar EmitBlock(createBasicBlock("sw.bb")); 90216f23570999cac1fa13597386938450843003840Daniel Dunbar llvm::BasicBlock *CaseDest = Builder.GetInsertBlock(); 90316f23570999cac1fa13597386938450843003840Daniel Dunbar EmitStmt(S.getSubStmt()); 90416f23570999cac1fa13597386938450843003840Daniel Dunbar 9054efde8d2c10b091bc9588de18c2c71dca2979e49Daniel Dunbar // If range is empty, do nothing. 9064efde8d2c10b091bc9588de18c2c71dca2979e49Daniel Dunbar if (LHS.isSigned() ? RHS.slt(LHS) : RHS.ult(LHS)) 9074efde8d2c10b091bc9588de18c2c71dca2979e49Daniel Dunbar return; 908c049e4f406a7f7179eba98659044a32508e53289Devang Patel 909c049e4f406a7f7179eba98659044a32508e53289Devang Patel llvm::APInt Range = RHS - LHS; 91016f23570999cac1fa13597386938450843003840Daniel Dunbar // FIXME: parameters such as this should not be hardcoded. 911c049e4f406a7f7179eba98659044a32508e53289Devang Patel if (Range.ult(llvm::APInt(Range.getBitWidth(), 64))) { 912c049e4f406a7f7179eba98659044a32508e53289Devang Patel // Range is small enough to add multiple switch instruction cases. 9134efde8d2c10b091bc9588de18c2c71dca2979e49Daniel Dunbar for (unsigned i = 0, e = Range.getZExtValue() + 1; i != e; ++i) { 91497d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner SwitchInsn->addCase(Builder.getInt(LHS), CaseDest); 9152d79d0f3ac0ea77b7bdfc3dd11de8cc3ccd91b8cDevang Patel LHS++; 9162d79d0f3ac0ea77b7bdfc3dd11de8cc3ccd91b8cDevang Patel } 917c049e4f406a7f7179eba98659044a32508e53289Devang Patel return; 9181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } 9191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 92016f23570999cac1fa13597386938450843003840Daniel Dunbar // The range is too big. Emit "if" condition into a new block, 92116f23570999cac1fa13597386938450843003840Daniel Dunbar // making sure to save and restore the current insertion point. 92216f23570999cac1fa13597386938450843003840Daniel Dunbar llvm::BasicBlock *RestoreBB = Builder.GetInsertBlock(); 92316f23570999cac1fa13597386938450843003840Daniel Dunbar 92416f23570999cac1fa13597386938450843003840Daniel Dunbar // Push this test onto the chain of range checks (which terminates 92516f23570999cac1fa13597386938450843003840Daniel Dunbar // in the default basic block). The switch's default will be changed 92616f23570999cac1fa13597386938450843003840Daniel Dunbar // to the top of this chain after switch emission is complete. 92716f23570999cac1fa13597386938450843003840Daniel Dunbar llvm::BasicBlock *FalseDest = CaseRangeBlock; 92855e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar CaseRangeBlock = createBasicBlock("sw.caserange"); 92916f23570999cac1fa13597386938450843003840Daniel Dunbar 93016f23570999cac1fa13597386938450843003840Daniel Dunbar CurFn->getBasicBlockList().push_back(CaseRangeBlock); 93116f23570999cac1fa13597386938450843003840Daniel Dunbar Builder.SetInsertPoint(CaseRangeBlock); 932c049e4f406a7f7179eba98659044a32508e53289Devang Patel 933c049e4f406a7f7179eba98659044a32508e53289Devang Patel // Emit range check. 9341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Value *Diff = 935578faa837b552403e2002b97fdfbfde14f2448e5Benjamin Kramer Builder.CreateSub(SwitchInsn->getCondition(), Builder.getInt(LHS)); 9361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Value *Cond = 93797d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner Builder.CreateICmpULE(Diff, Builder.getInt(Range), "inbounds"); 938c049e4f406a7f7179eba98659044a32508e53289Devang Patel Builder.CreateCondBr(Cond, CaseDest, FalseDest); 939c049e4f406a7f7179eba98659044a32508e53289Devang Patel 94016f23570999cac1fa13597386938450843003840Daniel Dunbar // Restore the appropriate insertion point. 941a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar if (RestoreBB) 942a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar Builder.SetInsertPoint(RestoreBB); 943a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar else 944a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar Builder.ClearInsertionPoint(); 945c049e4f406a7f7179eba98659044a32508e53289Devang Patel} 9462d79d0f3ac0ea77b7bdfc3dd11de8cc3ccd91b8cDevang Patel 947c049e4f406a7f7179eba98659044a32508e53289Devang Patelvoid CodeGenFunction::EmitCaseStmt(const CaseStmt &S) { 948d66715d83e64b6bfd0bddebe51874b1b7a64abefFariborz Jahanian // If there is no enclosing switch instance that we're aware of, then this 949d66715d83e64b6bfd0bddebe51874b1b7a64abefFariborz Jahanian // case statement and its block can be elided. This situation only happens 950d66715d83e64b6bfd0bddebe51874b1b7a64abefFariborz Jahanian // when we've constant-folded the switch, are emitting the constant case, 951d66715d83e64b6bfd0bddebe51874b1b7a64abefFariborz Jahanian // and part of the constant case includes another case statement. For 952d66715d83e64b6bfd0bddebe51874b1b7a64abefFariborz Jahanian // instance: switch (4) { case 4: do { case 5: } while (1); } 953303b4f946470a054cea8f91af54008aeb3c09507Fariborz Jahanian if (!SwitchInsn) { 954303b4f946470a054cea8f91af54008aeb3c09507Fariborz Jahanian EmitStmt(S.getSubStmt()); 955d66715d83e64b6bfd0bddebe51874b1b7a64abefFariborz Jahanian return; 956303b4f946470a054cea8f91af54008aeb3c09507Fariborz Jahanian } 957d66715d83e64b6bfd0bddebe51874b1b7a64abefFariborz Jahanian 958b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner // Handle case ranges. 959c049e4f406a7f7179eba98659044a32508e53289Devang Patel if (S.getRHS()) { 960c049e4f406a7f7179eba98659044a32508e53289Devang Patel EmitCaseStmtRange(S); 961c049e4f406a7f7179eba98659044a32508e53289Devang Patel return; 962c049e4f406a7f7179eba98659044a32508e53289Devang Patel } 9631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 96497d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner llvm::ConstantInt *CaseVal = 965a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith Builder.getInt(S.getLHS()->EvaluateKnownConstInt(getContext())); 96697d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner 967421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner // If the body of the case is just a 'break', and if there was no fallthrough, 968421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner // try to not emit an empty block. 96917083601193528a5d3770b688ffdf700f7df3c45Chad Rosier if ((CGM.getCodeGenOpts().OptimizationLevel > 0) && 97017083601193528a5d3770b688ffdf700f7df3c45Chad Rosier isa<BreakStmt>(S.getSubStmt())) { 971b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner JumpDest Block = BreakContinueStack.back().BreakBlock; 9726f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 973b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner // Only do this optimization if there are no cleanups that need emitting. 974b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner if (isObviouslyBranchWithoutCleanups(Block)) { 97597d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner SwitchInsn->addCase(CaseVal, Block.getBlock()); 976421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner 977421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner // If there was a fallthrough into this case, make sure to redirect it to 978421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner // the end of the switch as well. 979421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner if (Builder.GetInsertBlock()) { 980421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner Builder.CreateBr(Block.getBlock()); 981421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner Builder.ClearInsertionPoint(); 982421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner } 983b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner return; 984b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner } 985b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner } 9866f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 987f84dcda7e2ab2f6d5be5a8c52d22ef4c442dd762Daniel Dunbar EmitBlock(createBasicBlock("sw.bb")); 988c049e4f406a7f7179eba98659044a32508e53289Devang Patel llvm::BasicBlock *CaseDest = Builder.GetInsertBlock(); 98997d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner SwitchInsn->addCase(CaseVal, CaseDest); 9901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9915512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner // Recursively emitting the statement is acceptable, but is not wonderful for 9925512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner // code where we have many case statements nested together, i.e.: 9935512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner // case 1: 9945512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner // case 2: 9955512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner // case 3: etc. 9965512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner // Handling this recursively will create a new block for each case statement 9975512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner // that falls through to the next case which is IR intensive. It also causes 9985512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner // deep recursion which can run into stack depth limitations. Handle 9995512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner // sequential non-range case statements specially. 10005512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner const CaseStmt *CurCase = &S; 10015512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner const CaseStmt *NextCase = dyn_cast<CaseStmt>(S.getSubStmt()); 10025512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner 100397d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner // Otherwise, iteratively add consecutive cases to this switch stmt. 10045512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner while (NextCase && NextCase->getRHS() == 0) { 10055512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner CurCase = NextCase; 100697d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner llvm::ConstantInt *CaseVal = 1007a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith Builder.getInt(CurCase->getLHS()->EvaluateKnownConstInt(getContext())); 100897d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner SwitchInsn->addCase(CaseVal, CaseDest); 10095512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner NextCase = dyn_cast<CaseStmt>(CurCase->getSubStmt()); 10105512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner } 10111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10125512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner // Normal default recursion for non-cases. 10135512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner EmitStmt(CurCase->getSubStmt()); 101451b09f2c528c8460b5465c676173324e44176d62Devang Patel} 101551b09f2c528c8460b5465c676173324e44176d62Devang Patel 101651b09f2c528c8460b5465c676173324e44176d62Devang Patelvoid CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S) { 101716f23570999cac1fa13597386938450843003840Daniel Dunbar llvm::BasicBlock *DefaultBlock = SwitchInsn->getDefaultDest(); 10181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(DefaultBlock->empty() && 101955e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar "EmitDefaultStmt: Default block already defined?"); 102016f23570999cac1fa13597386938450843003840Daniel Dunbar EmitBlock(DefaultBlock); 102151b09f2c528c8460b5465c676173324e44176d62Devang Patel EmitStmt(S.getSubStmt()); 102251b09f2c528c8460b5465c676173324e44176d62Devang Patel} 102351b09f2c528c8460b5465c676173324e44176d62Devang Patel 1024fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// CollectStatementsForCase - Given the body of a 'switch' statement and a 1025fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// constant value that is being switched on, see if we can dead code eliminate 1026fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// the body of the switch to a simple series of statements to emit. Basically, 1027fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// on a switch (5) we want to find these statements: 1028fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// case 5: 1029fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// printf(...); <-- 1030fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// ++i; <-- 1031fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// break; 1032fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// 1033fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// and add them to the ResultStmts vector. If it is unsafe to do this 1034fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// transformation (for example, one of the elided statements contains a label 1035fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// that might be jumped to), return CSFC_Failure. If we handled it and 'S' 1036fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// should include statements after it (e.g. the printf() line is a substmt of 1037fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// the case) then return CSFC_FallThrough. If we handled it and found a break 1038fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// statement, then return CSFC_Success. 1039fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// 1040fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// If Case is non-null, then we are looking for the specified case, checking 1041fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// that nothing we jump over contains labels. If Case is null, then we found 1042fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// the case and are looking for the break. 1043fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// 1044fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// If the recursive walk actually finds our Case, then we set FoundCase to 1045fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// true. 1046fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// 1047fda0f1f5a278548b012401be07e287c1697fc41cChris Lattnerenum CSFC_Result { CSFC_Failure, CSFC_FallThrough, CSFC_Success }; 1048fda0f1f5a278548b012401be07e287c1697fc41cChris Lattnerstatic CSFC_Result CollectStatementsForCase(const Stmt *S, 1049fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner const SwitchCase *Case, 1050fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner bool &FoundCase, 10515f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVectorImpl<const Stmt*> &ResultStmts) { 10523858938b043bac2f046304ff99a54905acdcc6ddChris Lattner // If this is a null statement, just succeed. 10533858938b043bac2f046304ff99a54905acdcc6ddChris Lattner if (S == 0) 10543858938b043bac2f046304ff99a54905acdcc6ddChris Lattner return Case ? CSFC_Success : CSFC_FallThrough; 10556f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 1056fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // If this is the switchcase (case 4: or default) that we're looking for, then 1057fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // we're in business. Just add the substatement. 1058fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner if (const SwitchCase *SC = dyn_cast<SwitchCase>(S)) { 1059fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner if (S == Case) { 1060fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner FoundCase = true; 1061fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner return CollectStatementsForCase(SC->getSubStmt(), 0, FoundCase, 1062fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner ResultStmts); 1063fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner } 10646f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 1065fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // Otherwise, this is some other case or default statement, just ignore it. 1066fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner return CollectStatementsForCase(SC->getSubStmt(), Case, FoundCase, 1067fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner ResultStmts); 1068fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner } 10693858938b043bac2f046304ff99a54905acdcc6ddChris Lattner 10703858938b043bac2f046304ff99a54905acdcc6ddChris Lattner // If we are in the live part of the code and we found our break statement, 10713858938b043bac2f046304ff99a54905acdcc6ddChris Lattner // return a success! 10723858938b043bac2f046304ff99a54905acdcc6ddChris Lattner if (Case == 0 && isa<BreakStmt>(S)) 10733858938b043bac2f046304ff99a54905acdcc6ddChris Lattner return CSFC_Success; 10746f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 10753858938b043bac2f046304ff99a54905acdcc6ddChris Lattner // If this is a switch statement, then it might contain the SwitchCase, the 10763858938b043bac2f046304ff99a54905acdcc6ddChris Lattner // break, or neither. 10773858938b043bac2f046304ff99a54905acdcc6ddChris Lattner if (const CompoundStmt *CS = dyn_cast<CompoundStmt>(S)) { 10783858938b043bac2f046304ff99a54905acdcc6ddChris Lattner // Handle this as two cases: we might be looking for the SwitchCase (if so 10793858938b043bac2f046304ff99a54905acdcc6ddChris Lattner // the skipped statements must be skippable) or we might already have it. 10803858938b043bac2f046304ff99a54905acdcc6ddChris Lattner CompoundStmt::const_body_iterator I = CS->body_begin(), E = CS->body_end(); 10813858938b043bac2f046304ff99a54905acdcc6ddChris Lattner if (Case) { 10823f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner // Keep track of whether we see a skipped declaration. The code could be 10833f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner // using the declaration even if it is skipped, so we can't optimize out 10843f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner // the decl if the kept statements might refer to it. 10853f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner bool HadSkippedDecl = false; 10866f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 10873858938b043bac2f046304ff99a54905acdcc6ddChris Lattner // If we're looking for the case, just see if we can skip each of the 10883858938b043bac2f046304ff99a54905acdcc6ddChris Lattner // substatements. 10893858938b043bac2f046304ff99a54905acdcc6ddChris Lattner for (; Case && I != E; ++I) { 10904d509341bd5db06a517daa311379f52bb540bc34Eli Friedman HadSkippedDecl |= isa<DeclStmt>(*I); 10916f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 10923858938b043bac2f046304ff99a54905acdcc6ddChris Lattner switch (CollectStatementsForCase(*I, Case, FoundCase, ResultStmts)) { 10933858938b043bac2f046304ff99a54905acdcc6ddChris Lattner case CSFC_Failure: return CSFC_Failure; 10943858938b043bac2f046304ff99a54905acdcc6ddChris Lattner case CSFC_Success: 10953858938b043bac2f046304ff99a54905acdcc6ddChris Lattner // A successful result means that either 1) that the statement doesn't 10963858938b043bac2f046304ff99a54905acdcc6ddChris Lattner // have the case and is skippable, or 2) does contain the case value 10979467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner // and also contains the break to exit the switch. In the later case, 10989467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner // we just verify the rest of the statements are elidable. 10999467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner if (FoundCase) { 11003f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner // If we found the case and skipped declarations, we can't do the 11013f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner // optimization. 11023f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner if (HadSkippedDecl) 11033f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner return CSFC_Failure; 11046f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 11059467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner for (++I; I != E; ++I) 11069467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner if (CodeGenFunction::ContainsLabel(*I, true)) 11079467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner return CSFC_Failure; 11089467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner return CSFC_Success; 11099467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner } 11103858938b043bac2f046304ff99a54905acdcc6ddChris Lattner break; 11113858938b043bac2f046304ff99a54905acdcc6ddChris Lattner case CSFC_FallThrough: 11123858938b043bac2f046304ff99a54905acdcc6ddChris Lattner // If we have a fallthrough condition, then we must have found the 11133858938b043bac2f046304ff99a54905acdcc6ddChris Lattner // case started to include statements. Consider the rest of the 11143858938b043bac2f046304ff99a54905acdcc6ddChris Lattner // statements in the compound statement as candidates for inclusion. 11153858938b043bac2f046304ff99a54905acdcc6ddChris Lattner assert(FoundCase && "Didn't find case but returned fallthrough?"); 11163858938b043bac2f046304ff99a54905acdcc6ddChris Lattner // We recursively found Case, so we're not looking for it anymore. 11173858938b043bac2f046304ff99a54905acdcc6ddChris Lattner Case = 0; 11186f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 11193f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner // If we found the case and skipped declarations, we can't do the 11203f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner // optimization. 11213f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner if (HadSkippedDecl) 11223f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner return CSFC_Failure; 11233858938b043bac2f046304ff99a54905acdcc6ddChris Lattner break; 11243858938b043bac2f046304ff99a54905acdcc6ddChris Lattner } 11253858938b043bac2f046304ff99a54905acdcc6ddChris Lattner } 11263858938b043bac2f046304ff99a54905acdcc6ddChris Lattner } 11273858938b043bac2f046304ff99a54905acdcc6ddChris Lattner 11283858938b043bac2f046304ff99a54905acdcc6ddChris Lattner // If we have statements in our range, then we know that the statements are 11293858938b043bac2f046304ff99a54905acdcc6ddChris Lattner // live and need to be added to the set of statements we're tracking. 11303858938b043bac2f046304ff99a54905acdcc6ddChris Lattner for (; I != E; ++I) { 11313858938b043bac2f046304ff99a54905acdcc6ddChris Lattner switch (CollectStatementsForCase(*I, 0, FoundCase, ResultStmts)) { 11323858938b043bac2f046304ff99a54905acdcc6ddChris Lattner case CSFC_Failure: return CSFC_Failure; 11333858938b043bac2f046304ff99a54905acdcc6ddChris Lattner case CSFC_FallThrough: 11343858938b043bac2f046304ff99a54905acdcc6ddChris Lattner // A fallthrough result means that the statement was simple and just 11353858938b043bac2f046304ff99a54905acdcc6ddChris Lattner // included in ResultStmt, keep adding them afterwards. 11363858938b043bac2f046304ff99a54905acdcc6ddChris Lattner break; 11373858938b043bac2f046304ff99a54905acdcc6ddChris Lattner case CSFC_Success: 11383858938b043bac2f046304ff99a54905acdcc6ddChris Lattner // A successful result means that we found the break statement and 11393858938b043bac2f046304ff99a54905acdcc6ddChris Lattner // stopped statement inclusion. We just ensure that any leftover stmts 11403858938b043bac2f046304ff99a54905acdcc6ddChris Lattner // are skippable and return success ourselves. 11413858938b043bac2f046304ff99a54905acdcc6ddChris Lattner for (++I; I != E; ++I) 11423858938b043bac2f046304ff99a54905acdcc6ddChris Lattner if (CodeGenFunction::ContainsLabel(*I, true)) 11433858938b043bac2f046304ff99a54905acdcc6ddChris Lattner return CSFC_Failure; 11443858938b043bac2f046304ff99a54905acdcc6ddChris Lattner return CSFC_Success; 11456f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier } 11463858938b043bac2f046304ff99a54905acdcc6ddChris Lattner } 11476f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 11483858938b043bac2f046304ff99a54905acdcc6ddChris Lattner return Case ? CSFC_Success : CSFC_FallThrough; 11493858938b043bac2f046304ff99a54905acdcc6ddChris Lattner } 11503858938b043bac2f046304ff99a54905acdcc6ddChris Lattner 1151fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // Okay, this is some other statement that we don't handle explicitly, like a 1152fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // for statement or increment etc. If we are skipping over this statement, 1153fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // just verify it doesn't have labels, which would make it invalid to elide. 1154fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner if (Case) { 11553f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner if (CodeGenFunction::ContainsLabel(S, true)) 1156fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner return CSFC_Failure; 1157fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner return CSFC_Success; 1158fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner } 11596f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 1160fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // Otherwise, we want to include this statement. Everything is cool with that 1161fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // so long as it doesn't contain a break out of the switch we're in. 1162fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner if (CodeGenFunction::containsBreak(S)) return CSFC_Failure; 11636f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 1164fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // Otherwise, everything is great. Include the statement and tell the caller 1165fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // that we fall through and include the next statement as well. 1166fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner ResultStmts.push_back(S); 1167fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner return CSFC_FallThrough; 1168fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner} 1169fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner 1170fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// FindCaseStatementsForValue - Find the case statement being jumped to and 1171fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// then invoke CollectStatementsForCase to find the list of statements to emit 1172fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// for a switch on constant. See the comment above CollectStatementsForCase 1173fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// for more details. 1174fda0f1f5a278548b012401be07e287c1697fc41cChris Lattnerstatic bool FindCaseStatementsForValue(const SwitchStmt &S, 1175e1ecdc168175719d74e112bcacd4aae5e12d4631Richard Trieu const llvm::APSInt &ConstantCondValue, 11765f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVectorImpl<const Stmt*> &ResultStmts, 1177fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner ASTContext &C) { 1178fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // First step, find the switch case that is being branched to. We can do this 1179fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // efficiently by scanning the SwitchCase list. 1180fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner const SwitchCase *Case = S.getSwitchCaseList(); 1181fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner const DefaultStmt *DefaultCase = 0; 11826f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 1183fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner for (; Case; Case = Case->getNextSwitchCase()) { 1184fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // It's either a default or case. Just remember the default statement in 1185fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // case we're not jumping to any numbered cases. 1186fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner if (const DefaultStmt *DS = dyn_cast<DefaultStmt>(Case)) { 1187fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner DefaultCase = DS; 1188fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner continue; 1189fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner } 11906f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 1191fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // Check to see if this case is the one we're looking for. 1192fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner const CaseStmt *CS = cast<CaseStmt>(Case); 1193fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // Don't handle case ranges yet. 1194fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner if (CS->getRHS()) return false; 11956f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 1196fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // If we found our case, remember it as 'case'. 1197a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith if (CS->getLHS()->EvaluateKnownConstInt(C) == ConstantCondValue) 1198fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner break; 1199fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner } 12006f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 1201fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // If we didn't find a matching case, we use a default if it exists, or we 1202fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // elide the whole switch body! 1203fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner if (Case == 0) { 1204fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // It is safe to elide the body of the switch if it doesn't contain labels 1205fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // etc. If it is safe, return successfully with an empty ResultStmts list. 1206fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner if (DefaultCase == 0) 1207fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner return !CodeGenFunction::ContainsLabel(&S); 1208fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner Case = DefaultCase; 1209fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner } 1210fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner 1211fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // Ok, we know which case is being jumped to, try to collect all the 1212fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // statements that follow it. This can fail for a variety of reasons. Also, 1213fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // check to see that the recursive walk actually found our case statement. 1214fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // Insane cases like this can fail to find it in the recursive walk since we 1215fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // don't handle every stmt kind: 1216fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // switch (4) { 1217fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // while (1) { 1218fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // case 4: ... 1219fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner bool FoundCase = false; 1220fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner return CollectStatementsForCase(S.getBody(), Case, FoundCase, 1221fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner ResultStmts) != CSFC_Failure && 1222fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner FoundCase; 1223fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner} 1224fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner 122551b09f2c528c8460b5465c676173324e44176d62Devang Patelvoid CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) { 1226f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall JumpDest SwitchExit = getJumpDestInCurrentScope("sw.epilog"); 1227f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall 1228f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall RunCleanupsScope ConditionScope(*this); 1229d3d5301c44138b92bf01286183f5bf310cdd37cfDouglas Gregor 1230d3d5301c44138b92bf01286183f5bf310cdd37cfDouglas Gregor if (S.getConditionVariable()) 1231b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall EmitAutoVarDecl(*S.getConditionVariable()); 1232d3d5301c44138b92bf01286183f5bf310cdd37cfDouglas Gregor 1233985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian // Handle nested switch statements. 1234985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian llvm::SwitchInst *SavedSwitchInsn = SwitchInsn; 1235985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian llvm::BasicBlock *SavedCRBlock = CaseRangeBlock; 1236985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian 1237fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // See if we can constant fold the condition of the switch and therefore only 1238fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // emit the live case statement (if any) of the switch. 1239e1ecdc168175719d74e112bcacd4aae5e12d4631Richard Trieu llvm::APSInt ConstantCondValue; 1240fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner if (ConstantFoldsToSimpleInteger(S.getCond(), ConstantCondValue)) { 12415f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<const Stmt*, 4> CaseStmts; 1242fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner if (FindCaseStatementsForValue(S, ConstantCondValue, CaseStmts, 1243fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner getContext())) { 1244fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner RunCleanupsScope ExecutedScope(*this); 1245fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner 1246985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian // At this point, we are no longer "within" a switch instance, so 1247985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian // we can temporarily enforce this to ensure that any embedded case 1248985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian // statements are not emitted. 1249985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian SwitchInsn = 0; 1250985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian 1251fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // Okay, we can dead code eliminate everything except this case. Emit the 1252fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner // specified series of statements and we're good. 1253fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner for (unsigned i = 0, e = CaseStmts.size(); i != e; ++i) 1254fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner EmitStmt(CaseStmts[i]); 1255985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian 1256fc65ec83f6719f06e460b61f83a3ba1e80c18d04Eric Christopher // Now we want to restore the saved switch instance so that nested 1257fc65ec83f6719f06e460b61f83a3ba1e80c18d04Eric Christopher // switches continue to function properly 1258985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian SwitchInsn = SavedSwitchInsn; 1259985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian 1260fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner return; 1261fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner } 1262fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner } 12636f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 126451b09f2c528c8460b5465c676173324e44176d62Devang Patel llvm::Value *CondV = EmitScalarExpr(S.getCond()); 126551b09f2c528c8460b5465c676173324e44176d62Devang Patel 126616f23570999cac1fa13597386938450843003840Daniel Dunbar // Create basic block to hold stuff that comes after switch 126716f23570999cac1fa13597386938450843003840Daniel Dunbar // statement. We also need to create a default block now so that 126816f23570999cac1fa13597386938450843003840Daniel Dunbar // explicit case ranges tests can have a place to jump to on 126916f23570999cac1fa13597386938450843003840Daniel Dunbar // failure. 127055e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar llvm::BasicBlock *DefaultBlock = createBasicBlock("sw.default"); 127116f23570999cac1fa13597386938450843003840Daniel Dunbar SwitchInsn = Builder.CreateSwitch(CondV, DefaultBlock); 127216f23570999cac1fa13597386938450843003840Daniel Dunbar CaseRangeBlock = DefaultBlock; 127351b09f2c528c8460b5465c676173324e44176d62Devang Patel 12740912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar // Clear the insertion point to indicate we are in unreachable code. 12750912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar Builder.ClearInsertionPoint(); 1276d28a80d64616b66c91d28bb4c08ca2d8c594de4eEli Friedman 1277e9b8c0a38549692f1b8f688c05c35442fc620865Devang Patel // All break statements jump to NextBlock. If BreakContinueStack is non empty 1278e9b8c0a38549692f1b8f688c05c35442fc620865Devang Patel // then reuse last ContinueBlock. 1279f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall JumpDest OuterContinue; 1280e4b6d342c29d5cb9d311756100df1603810fa892Anders Carlsson if (!BreakContinueStack.empty()) 1281f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall OuterContinue = BreakContinueStack.back().ContinueBlock; 1282e4b6d342c29d5cb9d311756100df1603810fa892Anders Carlsson 1283f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall BreakContinueStack.push_back(BreakContinue(SwitchExit, OuterContinue)); 128451b09f2c528c8460b5465c676173324e44176d62Devang Patel 128551b09f2c528c8460b5465c676173324e44176d62Devang Patel // Emit switch body. 128651b09f2c528c8460b5465c676173324e44176d62Devang Patel EmitStmt(S.getBody()); 12871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1288e4b6d342c29d5cb9d311756100df1603810fa892Anders Carlsson BreakContinueStack.pop_back(); 128951b09f2c528c8460b5465c676173324e44176d62Devang Patel 129016f23570999cac1fa13597386938450843003840Daniel Dunbar // Update the default block in case explicit case range tests have 129116f23570999cac1fa13597386938450843003840Daniel Dunbar // been chained on top. 1292ab14ae2ab16088b6a7f69eac6e152c3e9f9ea01bStepan Dyatkovskiy SwitchInsn->setDefaultDest(CaseRangeBlock); 12931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1294f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall // If a default was never emitted: 129516f23570999cac1fa13597386938450843003840Daniel Dunbar if (!DefaultBlock->getParent()) { 1296f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall // If we have cleanups, emit the default block so that there's a 1297f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall // place to jump through the cleanups from. 1298f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall if (ConditionScope.requiresCleanups()) { 1299f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall EmitBlock(DefaultBlock); 1300f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall 1301f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall // Otherwise, just forward the default block to the switch end. 1302f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall } else { 1303ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall DefaultBlock->replaceAllUsesWith(SwitchExit.getBlock()); 1304f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall delete DefaultBlock; 1305f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall } 130616f23570999cac1fa13597386938450843003840Daniel Dunbar } 130716f23570999cac1fa13597386938450843003840Daniel Dunbar 1308ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall ConditionScope.ForceCleanup(); 1309ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall 131016f23570999cac1fa13597386938450843003840Daniel Dunbar // Emit continuation. 1311ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall EmitBlock(SwitchExit.getBlock(), true); 131251b09f2c528c8460b5465c676173324e44176d62Devang Patel 131351b09f2c528c8460b5465c676173324e44176d62Devang Patel SwitchInsn = SavedSwitchInsn; 1314c049e4f406a7f7179eba98659044a32508e53289Devang Patel CaseRangeBlock = SavedCRBlock; 131551b09f2c528c8460b5465c676173324e44176d62Devang Patel} 1316fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson 13172819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattnerstatic std::string 1318444be7366d0a1e172c0290a1ea54c1cb16b5947cDaniel DunbarSimplifyConstraint(const char *Constraint, const TargetInfo &Target, 13195f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVectorImpl<TargetInfo::ConstraintInfo> *OutCons=0) { 1320fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson std::string Result; 13211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1322fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson while (*Constraint) { 1323fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson switch (*Constraint) { 1324fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson default: 1325002333f8b2cf1a8614e532f6ce366b21af85142cStuart Hastings Result += Target.convertConstraint(Constraint); 1326fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson break; 1327fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson // Ignore these 1328fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson case '*': 1329fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson case '?': 1330fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson case '!': 1331ef44e1110711276ffffe4b22d4ba0cebd49cb330John Thompson case '=': // Will see this and the following in mult-alt constraints. 1332ef44e1110711276ffffe4b22d4ba0cebd49cb330John Thompson case '+': 1333ef44e1110711276ffffe4b22d4ba0cebd49cb330John Thompson break; 1334e6b3dbae38f62164cab3989380cd940242d80120Ulrich Weigand case '#': // Ignore the rest of the constraint alternative. 1335e6b3dbae38f62164cab3989380cd940242d80120Ulrich Weigand while (Constraint[1] && Constraint[1] != ',') 1336e6b3dbae38f62164cab3989380cd940242d80120Ulrich Weigand Constraint++; 1337e6b3dbae38f62164cab3989380cd940242d80120Ulrich Weigand break; 13382f474ea9ef7505df5d092287c48c19974222293bJohn Thompson case ',': 13392f474ea9ef7505df5d092287c48c19974222293bJohn Thompson Result += "|"; 1340fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson break; 1341fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson case 'g': 1342fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson Result += "imr"; 1343fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson break; 1344300fb5d0ef7edc87f3fdba17fc8b1184013b35aeAnders Carlsson case '[': { 13452819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner assert(OutCons && 1346300fb5d0ef7edc87f3fdba17fc8b1184013b35aeAnders Carlsson "Must pass output names to constraints with a symbolic name"); 1347300fb5d0ef7edc87f3fdba17fc8b1184013b35aeAnders Carlsson unsigned Index; 13481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump bool result = Target.resolveSymbolicName(Constraint, 13492819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner &(*OutCons)[0], 13502819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner OutCons->size(), Index); 1351cbf40f913aa2aa5de6e0540fed209405d00a2c69Chris Lattner assert(result && "Could not resolve symbolic name"); (void)result; 1352300fb5d0ef7edc87f3fdba17fc8b1184013b35aeAnders Carlsson Result += llvm::utostr(Index); 1353300fb5d0ef7edc87f3fdba17fc8b1184013b35aeAnders Carlsson break; 1354300fb5d0ef7edc87f3fdba17fc8b1184013b35aeAnders Carlsson } 1355fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson } 13561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1357fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson Constraint++; 1358fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson } 13591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1360fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson return Result; 1361fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson} 1362fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson 136303117d1b2e32d18652401b12d9049871992bf3adRafael Espindola/// AddVariableConstraints - Look at AsmExpr and if it is a variable declared 136403117d1b2e32d18652401b12d9049871992bf3adRafael Espindola/// as using a particular register add that as a constraint that will be used 136503117d1b2e32d18652401b12d9049871992bf3adRafael Espindola/// in this asm stmt. 13660ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindolastatic std::string 136703117d1b2e32d18652401b12d9049871992bf3adRafael EspindolaAddVariableConstraints(const std::string &Constraint, const Expr &AsmExpr, 136803117d1b2e32d18652401b12d9049871992bf3adRafael Espindola const TargetInfo &Target, CodeGenModule &CGM, 1369a23b91d542e336be5051ac54f394e860fb756911Chad Rosier const AsmStmt &Stmt) { 13700ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola const DeclRefExpr *AsmDeclRef = dyn_cast<DeclRefExpr>(&AsmExpr); 13710ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola if (!AsmDeclRef) 13720ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola return Constraint; 13730ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola const ValueDecl &Value = *AsmDeclRef->getDecl(); 13740ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola const VarDecl *Variable = dyn_cast<VarDecl>(&Value); 13750ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola if (!Variable) 13760ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola return Constraint; 1377a43ef3e0511dc48d98d61598163c9deddc09cb9cEli Friedman if (Variable->getStorageClass() != SC_Register) 1378a43ef3e0511dc48d98d61598163c9deddc09cb9cEli Friedman return Constraint; 13790ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola AsmLabelAttr *Attr = Variable->getAttr<AsmLabelAttr>(); 13800ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola if (!Attr) 13810ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola return Constraint; 13825f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef Register = Attr->getLabel(); 1383baf86955a9a390f2643a1ea9806832eb4a92f716Rafael Espindola assert(Target.isValidGCCRegisterName(Register)); 1384e3e07a5b3490bc2977859d56bac211afac2236fbEric Christopher // We're using validateOutputConstraint here because we only care if 1385e3e07a5b3490bc2977859d56bac211afac2236fbEric Christopher // this is a register constraint. 1386e3e07a5b3490bc2977859d56bac211afac2236fbEric Christopher TargetInfo::ConstraintInfo Info(Constraint, ""); 1387e3e07a5b3490bc2977859d56bac211afac2236fbEric Christopher if (Target.validateOutputConstraint(Info) && 1388e3e07a5b3490bc2977859d56bac211afac2236fbEric Christopher !Info.allowsRegister()) { 13890ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola CGM.ErrorUnsupported(&Stmt, "__asm__"); 13900ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola return Constraint; 13910ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola } 139243fec879a527c74ff01d8aa2bf94a12432249fc7Eric Christopher // Canonicalize the register here before returning it. 139343fec879a527c74ff01d8aa2bf94a12432249fc7Eric Christopher Register = Target.getNormalizedGCCRegisterName(Register); 13940ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola return "{" + Register.str() + "}"; 13950ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola} 13960ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola 13976d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedmanllvm::Value* 139842b60551eff3a424e191b293bfd606559dc96bffChad RosierCodeGenFunction::EmitAsmInputLValue(const TargetInfo::ConstraintInfo &Info, 13996d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman LValue InputValue, QualType InputType, 14006d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman std::string &ConstraintStr) { 1401634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson llvm::Value *Arg; 14021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (Info.allowsRegister() || !Info.allowsMemory()) { 14039d232c884ea9872d6555df0fd7359699819bc1f1John McCall if (CodeGenFunction::hasScalarEvaluationKind(InputType)) { 1404545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall Arg = EmitLoadOfLValue(InputValue).getScalarVal(); 1405634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson } else { 14062acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *Ty = ConvertType(InputType); 140725a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow uint64_t Size = CGM.getDataLayout().getTypeSizeInBits(Ty); 1408ebaae2a3103b443d50444d335ab5ab0ff7680da2Anders Carlsson if (Size <= 64 && llvm::isPowerOf2_64(Size)) { 1409d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall Ty = llvm::IntegerType::get(getLLVMContext(), Size); 1410ebaae2a3103b443d50444d335ab5ab0ff7680da2Anders Carlsson Ty = llvm::PointerType::getUnqual(Ty); 14111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14126d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman Arg = Builder.CreateLoad(Builder.CreateBitCast(InputValue.getAddress(), 14136d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman Ty)); 1414ebaae2a3103b443d50444d335ab5ab0ff7680da2Anders Carlsson } else { 14156d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman Arg = InputValue.getAddress(); 1416ebaae2a3103b443d50444d335ab5ab0ff7680da2Anders Carlsson ConstraintStr += '*'; 1417ebaae2a3103b443d50444d335ab5ab0ff7680da2Anders Carlsson } 1418634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson } 1419634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson } else { 14206d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman Arg = InputValue.getAddress(); 1421634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson ConstraintStr += '*'; 1422634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson } 14231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1424634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson return Arg; 1425634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson} 1426634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson 142742b60551eff3a424e191b293bfd606559dc96bffChad Rosierllvm::Value* CodeGenFunction::EmitAsmInput( 14286d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman const TargetInfo::ConstraintInfo &Info, 14296d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman const Expr *InputExpr, 14306d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman std::string &ConstraintStr) { 14316d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman if (Info.allowsRegister() || !Info.allowsMemory()) 14329d232c884ea9872d6555df0fd7359699819bc1f1John McCall if (CodeGenFunction::hasScalarEvaluationKind(InputExpr->getType())) 14336d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman return EmitScalarExpr(InputExpr); 14346d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman 14356d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman InputExpr = InputExpr->IgnoreParenNoopCasts(getContext()); 14366d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman LValue Dest = EmitLValue(InputExpr); 143742b60551eff3a424e191b293bfd606559dc96bffChad Rosier return EmitAsmInputLValue(Info, Dest, InputExpr->getType(), ConstraintStr); 14386d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman} 14396d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman 144047fc7e93b6af9e6a967231133a2f4bc626eeed26Chris Lattner/// getAsmSrcLocInfo - Return the !srcloc metadata node to attach to an inline 14415d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner/// asm call instruction. The !srcloc MDNode contains a list of constant 14425d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner/// integers which are the source locations of the start of each line in the 14435d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner/// asm. 144447fc7e93b6af9e6a967231133a2f4bc626eeed26Chris Lattnerstatic llvm::MDNode *getAsmSrcLocInfo(const StringLiteral *Str, 144547fc7e93b6af9e6a967231133a2f4bc626eeed26Chris Lattner CodeGenFunction &CGF) { 14465f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<llvm::Value *, 8> Locs; 14475d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner // Add the location of the first line to the MDNode. 14485d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner Locs.push_back(llvm::ConstantInt::get(CGF.Int32Ty, 14495d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner Str->getLocStart().getRawEncoding())); 14505f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef StrVal = Str->getString(); 14515d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner if (!StrVal.empty()) { 14525d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner const SourceManager &SM = CGF.CGM.getContext().getSourceManager(); 14534e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie const LangOptions &LangOpts = CGF.CGM.getLangOpts(); 14546f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 14555d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner // Add the location of the start of each subsequent line of the asm to the 14565d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner // MDNode. 14575d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner for (unsigned i = 0, e = StrVal.size()-1; i != e; ++i) { 14585d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner if (StrVal[i] != '\n') continue; 14595d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner SourceLocation LineLoc = Str->getLocationOfByte(i+1, SM, LangOpts, 146064aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall CGF.getTarget()); 14615d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner Locs.push_back(llvm::ConstantInt::get(CGF.Int32Ty, 14625d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner LineLoc.getRawEncoding())); 14635d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner } 14646f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier } 14656f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 14666f141659cab11109d9931d92d0988f8850778de3Jay Foad return llvm::MDNode::get(CGF.getLLVMContext(), Locs); 146747fc7e93b6af9e6a967231133a2f4bc626eeed26Chris Lattner} 146847fc7e93b6af9e6a967231133a2f4bc626eeed26Chris Lattner 1469a23b91d542e336be5051ac54f394e860fb756911Chad Rosiervoid CodeGenFunction::EmitAsmStmt(const AsmStmt &S) { 1470be3ace834ee7438915e73d2115365d57d03ceb99Chad Rosier // Assemble the final asm string. 1471da083b2ce8db27ce6e508cb77cb12c0fc8b7cad9Chad Rosier std::string AsmString = S.generateAsmString(getContext()); 14721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1473481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner // Get all the output and input constraints together. 14745f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos; 14755f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos; 1476481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner 14771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) { 1478481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner TargetInfo::ConstraintInfo Info(S.getOutputConstraint(i), 1479481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner S.getOutputName(i)); 148064aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall bool IsValid = getTarget().validateOutputConstraint(Info); (void)IsValid; 1481b992259f7790d3fb9fc5c2eb7182d7af9d64f9acChris Lattner assert(IsValid && "Failed to parse output constraint"); 1482481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner OutputConstraintInfos.push_back(Info); 14831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } 14841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1485481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) { 1486481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner TargetInfo::ConstraintInfo Info(S.getInputConstraint(i), 1487481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner S.getInputName(i)); 148864aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall bool IsValid = 148964aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall getTarget().validateInputConstraint(OutputConstraintInfos.data(), 149064aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall S.getNumOutputs(), Info); 1491b992259f7790d3fb9fc5c2eb7182d7af9d64f9acChris Lattner assert(IsValid && "Failed to parse input constraint"); (void)IsValid; 1492481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner InputConstraintInfos.push_back(Info); 1493481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner } 14941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1495fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson std::string Constraints; 14961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1497ede9d900809c4fd0298d52f5a63088ecb8302275Chris Lattner std::vector<LValue> ResultRegDests; 1498ede9d900809c4fd0298d52f5a63088ecb8302275Chris Lattner std::vector<QualType> ResultRegQualTys; 1499ef6de3da8572607f786303c07150daa6e140ab19Jay Foad std::vector<llvm::Type *> ResultRegTypes; 1500ef6de3da8572607f786303c07150daa6e140ab19Jay Foad std::vector<llvm::Type *> ResultTruncRegTypes; 1501d1c0c940ebc8e55d5c4672f61abedf87d6ea36f4Chad Rosier std::vector<llvm::Type *> ArgTypes; 1502fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson std::vector<llvm::Value*> Args; 1503f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson 1504f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson // Keep track of inout constraints. 1505f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson std::string InOutConstraints; 1506f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson std::vector<llvm::Value*> InOutArgs; 15079cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner std::vector<llvm::Type*> InOutArgTypes; 150803eb543cf7ebee463b33b5802b83ac92c21770cfAnders Carlsson 15091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) { 1510481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner TargetInfo::ConstraintInfo &Info = OutputConstraintInfos[i]; 151103eb543cf7ebee463b33b5802b83ac92c21770cfAnders Carlsson 1512fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson // Simplify the output constraint. 1513481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner std::string OutputConstraint(S.getOutputConstraint(i)); 151464aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall OutputConstraint = SimplifyConstraint(OutputConstraint.c_str() + 1, 151564aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall getTarget()); 15161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1517810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner const Expr *OutExpr = S.getOutputExpr(i); 1518810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner OutExpr = OutExpr->IgnoreParenNoopCasts(getContext()); 15191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1520a18f539628a6506bae6af52eacd541cebefff762Eric Christopher OutputConstraint = AddVariableConstraints(OutputConstraint, *OutExpr, 152164aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall getTarget(), CGM, S); 15220ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola 1523810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner LValue Dest = EmitLValue(OutExpr); 1524ede9d900809c4fd0298d52f5a63088ecb8302275Chris Lattner if (!Constraints.empty()) 1525bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson Constraints += ','; 1526bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson 1527a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner // If this is a register output, then make the inline asm return it 1528a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner // by-value. If this is a memory result, return the value by-reference. 15299d232c884ea9872d6555df0fd7359699819bc1f1John McCall if (!Info.allowsMemory() && hasScalarEvaluationKind(OutExpr->getType())) { 1530a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner Constraints += "=" + OutputConstraint; 1531ede9d900809c4fd0298d52f5a63088ecb8302275Chris Lattner ResultRegQualTys.push_back(OutExpr->getType()); 1532ede9d900809c4fd0298d52f5a63088ecb8302275Chris Lattner ResultRegDests.push_back(Dest); 1533a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner ResultRegTypes.push_back(ConvertTypeForMem(OutExpr->getType())); 1534a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner ResultTruncRegTypes.push_back(ResultRegTypes.back()); 15351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1536a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner // If this output is tied to an input, and if the input is larger, then 1537a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner // we need to set the actual result type of the inline asm node to be the 1538a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner // same as the input type. 1539a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner if (Info.hasMatchingInput()) { 1540ebfc9857de58a326c84629915d9ffe3f36d8cc26Chris Lattner unsigned InputNo; 1541ebfc9857de58a326c84629915d9ffe3f36d8cc26Chris Lattner for (InputNo = 0; InputNo != S.getNumInputs(); ++InputNo) { 1542ebfc9857de58a326c84629915d9ffe3f36d8cc26Chris Lattner TargetInfo::ConstraintInfo &Input = InputConstraintInfos[InputNo]; 1543aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner if (Input.hasTiedOperand() && Input.getTiedOperand() == i) 1544a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner break; 1545ebfc9857de58a326c84629915d9ffe3f36d8cc26Chris Lattner } 1546ebfc9857de58a326c84629915d9ffe3f36d8cc26Chris Lattner assert(InputNo != S.getNumInputs() && "Didn't find matching input!"); 15471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1548a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner QualType InputTy = S.getInputExpr(InputNo)->getType(); 1549aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner QualType OutputType = OutExpr->getType(); 15501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1551a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner uint64_t InputSize = getContext().getTypeSize(InputTy); 1552aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner if (getContext().getTypeSize(OutputType) < InputSize) { 1553aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // Form the asm to return the value as a larger integer or fp type. 1554aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner ResultRegTypes.back() = ConvertType(InputTy); 1555a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner } 1556a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner } 1557ef6de3da8572607f786303c07150daa6e140ab19Jay Foad if (llvm::Type* AdjTy = 15584b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne getTargetHooks().adjustInlineAsmType(*this, OutputConstraint, 15594b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne ResultRegTypes.back())) 1560f6e2c2039f76fa58799f6d155892d54fc95755e1Dale Johannesen ResultRegTypes.back() = AdjTy; 1561fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson } else { 1562fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson ArgTypes.push_back(Dest.getAddress()->getType()); 1563cad3ab611ebd3bee3ce6395d649640047f904cdeAnders Carlsson Args.push_back(Dest.getAddress()); 1564f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson Constraints += "=*"; 1565fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson Constraints += OutputConstraint; 1566f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson } 15671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 156844def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner if (Info.isReadWrite()) { 1569f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson InOutConstraints += ','; 1570634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson 1571fca9361839ecc53224c764517e62fc0e15166004Anders Carlsson const Expr *InputExpr = S.getOutputExpr(i); 157242b60551eff3a424e191b293bfd606559dc96bffChad Rosier llvm::Value *Arg = EmitAsmInputLValue(Info, Dest, InputExpr->getType(), 15736d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman InOutConstraints); 15741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1575acb5310542b82e7acf5f5a91fa619e4b7efeff5cBill Wendling if (llvm::Type* AdjTy = 1576acb5310542b82e7acf5f5a91fa619e4b7efeff5cBill Wendling getTargetHooks().adjustInlineAsmType(*this, OutputConstraint, 1577acb5310542b82e7acf5f5a91fa619e4b7efeff5cBill Wendling Arg->getType())) 1578acb5310542b82e7acf5f5a91fa619e4b7efeff5cBill Wendling Arg = Builder.CreateBitCast(Arg, AdjTy); 1579acb5310542b82e7acf5f5a91fa619e4b7efeff5cBill Wendling 158044def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner if (Info.allowsRegister()) 15819f2505b934745b18d580ade4dac7b8b16952a30cAnders Carlsson InOutConstraints += llvm::utostr(i); 15829f2505b934745b18d580ade4dac7b8b16952a30cAnders Carlsson else 15839f2505b934745b18d580ade4dac7b8b16952a30cAnders Carlsson InOutConstraints += OutputConstraint; 15842763b3af0a527c3a63cb058b90c22db0b7bcf558Anders Carlsson 1585fca9361839ecc53224c764517e62fc0e15166004Anders Carlsson InOutArgTypes.push_back(Arg->getType()); 1586fca9361839ecc53224c764517e62fc0e15166004Anders Carlsson InOutArgs.push_back(Arg); 1587f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson } 1588fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson } 15891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1590fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson unsigned NumConstraints = S.getNumOutputs() + S.getNumInputs(); 15911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1592fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) { 1593fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson const Expr *InputExpr = S.getInputExpr(i); 1594fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson 1595481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i]; 1596481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner 1597ede9d900809c4fd0298d52f5a63088ecb8302275Chris Lattner if (!Constraints.empty()) 1598fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson Constraints += ','; 15991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1600fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson // Simplify the input constraint. 1601481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner std::string InputConstraint(S.getInputConstraint(i)); 160264aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall InputConstraint = SimplifyConstraint(InputConstraint.c_str(), getTarget(), 16032819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner &OutputConstraintInfos); 1604fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson 16050ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola InputConstraint = 160603117d1b2e32d18652401b12d9049871992bf3adRafael Espindola AddVariableConstraints(InputConstraint, 16070ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola *InputExpr->IgnoreParenNoopCasts(getContext()), 160864aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall getTarget(), CGM, S); 16090ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola 161042b60551eff3a424e191b293bfd606559dc96bffChad Rosier llvm::Value *Arg = EmitAsmInput(Info, InputExpr, Constraints); 16111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16124df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner // If this input argument is tied to a larger output result, extend the 16134df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner // input to be the same size as the output. The LLVM backend wants to see 16144df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner // the input and output of a matching constraint be the same size. Note 16154df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner // that GCC does not define what the top bits are here. We use zext because 16164df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner // that is usually cheaper, but LLVM IR should really get an anyext someday. 16174df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner if (Info.hasTiedOperand()) { 16184df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner unsigned Output = Info.getTiedOperand(); 1619aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner QualType OutputType = S.getOutputExpr(Output)->getType(); 16204df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner QualType InputTy = InputExpr->getType(); 16211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1622aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner if (getContext().getTypeSize(OutputType) > 16234df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner getContext().getTypeSize(InputTy)) { 16244df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner // Use ptrtoint as appropriate so that we can do our extension. 16254df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner if (isa<llvm::PointerType>(Arg->getType())) 162677b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner Arg = Builder.CreatePtrToInt(Arg, IntPtrTy); 16272acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *OutputTy = ConvertType(OutputType); 1628aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner if (isa<llvm::IntegerType>(OutputTy)) 1629aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner Arg = Builder.CreateZExt(Arg, OutputTy); 163093f1322684e928a559286ba1c7cb83af077aa658Peter Collingbourne else if (isa<llvm::PointerType>(OutputTy)) 163193f1322684e928a559286ba1c7cb83af077aa658Peter Collingbourne Arg = Builder.CreateZExt(Arg, IntPtrTy); 163293f1322684e928a559286ba1c7cb83af077aa658Peter Collingbourne else { 163393f1322684e928a559286ba1c7cb83af077aa658Peter Collingbourne assert(OutputTy->isFloatingPointTy() && "Unexpected output type"); 1634aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner Arg = Builder.CreateFPExt(Arg, OutputTy); 163593f1322684e928a559286ba1c7cb83af077aa658Peter Collingbourne } 16364df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner } 16374df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner } 1638acb5310542b82e7acf5f5a91fa619e4b7efeff5cBill Wendling if (llvm::Type* AdjTy = 16394b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne getTargetHooks().adjustInlineAsmType(*this, InputConstraint, 16404b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne Arg->getType())) 1641f6e2c2039f76fa58799f6d155892d54fc95755e1Dale Johannesen Arg = Builder.CreateBitCast(Arg, AdjTy); 16421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1643fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson ArgTypes.push_back(Arg->getType()); 1644fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson Args.push_back(Arg); 1645fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson Constraints += InputConstraint; 1646fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson } 16471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1648f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson // Append the "input" part of inout constraints last. 1649f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson for (unsigned i = 0, e = InOutArgs.size(); i != e; i++) { 1650f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson ArgTypes.push_back(InOutArgTypes[i]); 1651f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson Args.push_back(InOutArgs[i]); 1652f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson } 1653f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson Constraints += InOutConstraints; 16541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1655fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson // Clobbers 1656fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson for (unsigned i = 0, e = S.getNumClobbers(); i != e; i++) { 165733f0558f75f70061707d1388e305b8f92f4e55deChad Rosier StringRef Clobber = S.getClobber(i); 1658fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson 1659de31fd7eeebdc64fb043463e7f515dab8eccac8dEric Christopher if (Clobber != "memory" && Clobber != "cc") 166064aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall Clobber = getTarget().getNormalizedGCCRegisterName(Clobber); 16611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1662ea041758d49215167e473a515b8d46e77b170ccfAnders Carlsson if (i != 0 || NumConstraints != 0) 1663fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson Constraints += ','; 16641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1665ea041758d49215167e473a515b8d46e77b170ccfAnders Carlsson Constraints += "~{"; 1666fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson Constraints += Clobber; 1667ea041758d49215167e473a515b8d46e77b170ccfAnders Carlsson Constraints += '}'; 1668fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson } 16691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1670fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson // Add machine specific clobbers 167164aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall std::string MachineClobbers = getTarget().getClobbers(); 1672ccf614c479ac93326a01e7b373b30759eed7807fEli Friedman if (!MachineClobbers.empty()) { 1673fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson if (!Constraints.empty()) 1674fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson Constraints += ','; 1675ccf614c479ac93326a01e7b373b30759eed7807fEli Friedman Constraints += MachineClobbers; 1676fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson } 1677bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson 16782acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *ResultType; 1679a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner if (ResultRegTypes.empty()) 16808b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner ResultType = VoidTy; 1681a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner else if (ResultRegTypes.size() == 1) 1682a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner ResultType = ResultRegTypes[0]; 1683bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson else 1684d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall ResultType = llvm::StructType::get(getLLVMContext(), ResultRegTypes); 16851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16862acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *FTy = 1687fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson llvm::FunctionType::get(ResultType, ArgTypes, false); 16881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16892ab7d43e450333d52fdf087bf2558a74dbe3c9fdChad Rosier bool HasSideEffect = S.isVolatile() || S.getNumOutputs() == 0; 1690fcf75a3d8c072eabbf25c9b33ccbb1b8bd042cfcChad Rosier llvm::InlineAsm::AsmDialect AsmDialect = isa<MSAsmStmt>(&S) ? 1691fcf75a3d8c072eabbf25c9b33ccbb1b8bd042cfcChad Rosier llvm::InlineAsm::AD_Intel : llvm::InlineAsm::AD_ATT; 16921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::InlineAsm *IA = 1693790cbd84d07f0dc9253ade9f67e6ed8c43f08a59Chad Rosier llvm::InlineAsm::get(FTy, AsmString, Constraints, HasSideEffect, 1694fcf75a3d8c072eabbf25c9b33ccbb1b8bd042cfcChad Rosier /* IsAlignStack */ false, AsmDialect); 16954c7d9f1507d0f102bd4133bba63348636facd469Jay Foad llvm::CallInst *Result = Builder.CreateCall(IA, Args); 1696785b778203a474c6e4b9e17ae91cd2a358868877Bill Wendling Result->addAttribute(llvm::AttributeSet::FunctionIndex, 169715e05e932ccf9ba2621394834b62684b42d6fd40Peter Collingbourne llvm::Attribute::NoUnwind); 16981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1699fc1a9c312d249a2c7458f763f848ba42685c23f8Chris Lattner // Slap the source location of the inline asm into a !srcloc metadata on the 1700a23b91d542e336be5051ac54f394e860fb756911Chad Rosier // call. FIXME: Handle metadata for MS-style inline asms. 1701a23b91d542e336be5051ac54f394e860fb756911Chad Rosier if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(&S)) 1702a23b91d542e336be5051ac54f394e860fb756911Chad Rosier Result->setMetadata("srcloc", getAsmSrcLocInfo(gccAsmStmt->getAsmString(), 1703a23b91d542e336be5051ac54f394e860fb756911Chad Rosier *this)); 17041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1705a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner // Extract all of the register value results from the asm. 1706a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner std::vector<llvm::Value*> RegResults; 1707a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner if (ResultRegTypes.size() == 1) { 1708a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner RegResults.push_back(Result); 1709bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson } else { 1710a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner for (unsigned i = 0, e = ResultRegTypes.size(); i != e; ++i) { 1711bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson llvm::Value *Tmp = Builder.CreateExtractValue(Result, i, "asmresult"); 1712a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner RegResults.push_back(Tmp); 1713a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner } 1714a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner } 17151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1716a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner for (unsigned i = 0, e = RegResults.size(); i != e; ++i) { 1717a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner llvm::Value *Tmp = RegResults[i]; 17181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1719a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner // If the result type of the LLVM IR asm doesn't match the result type of 1720a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner // the expression, do the conversion. 1721a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner if (ResultRegTypes[i] != ResultTruncRegTypes[i]) { 17222acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *TruncTy = ResultTruncRegTypes[i]; 17236f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier 1724aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // Truncate the integer result to the right size, note that TruncTy can be 1725aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // a pointer. 1726aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner if (TruncTy->isFloatingPointTy()) 1727aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner Tmp = Builder.CreateFPTrunc(Tmp, TruncTy); 17282dca88fc6d3e606a8131712be1800e3a4b90ca3eDan Gohman else if (TruncTy->isPointerTy() && Tmp->getType()->isIntegerTy()) { 172925a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow uint64_t ResSize = CGM.getDataLayout().getTypeSizeInBits(TruncTy); 1730d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall Tmp = Builder.CreateTrunc(Tmp, 1731d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall llvm::IntegerType::get(getLLVMContext(), (unsigned)ResSize)); 1732a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner Tmp = Builder.CreateIntToPtr(Tmp, TruncTy); 17332dca88fc6d3e606a8131712be1800e3a4b90ca3eDan Gohman } else if (Tmp->getType()->isPointerTy() && TruncTy->isIntegerTy()) { 173425a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow uint64_t TmpSize =CGM.getDataLayout().getTypeSizeInBits(Tmp->getType()); 1735d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall Tmp = Builder.CreatePtrToInt(Tmp, 1736d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall llvm::IntegerType::get(getLLVMContext(), (unsigned)TmpSize)); 17372dca88fc6d3e606a8131712be1800e3a4b90ca3eDan Gohman Tmp = Builder.CreateTrunc(Tmp, TruncTy); 17382dca88fc6d3e606a8131712be1800e3a4b90ca3eDan Gohman } else if (TruncTy->isIntegerTy()) { 17392dca88fc6d3e606a8131712be1800e3a4b90ca3eDan Gohman Tmp = Builder.CreateTrunc(Tmp, TruncTy); 1740f6e2c2039f76fa58799f6d155892d54fc95755e1Dale Johannesen } else if (TruncTy->isVectorTy()) { 1741f6e2c2039f76fa58799f6d155892d54fc95755e1Dale Johannesen Tmp = Builder.CreateBitCast(Tmp, TruncTy); 1742a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner } 1743bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson } 17441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1745545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall EmitStoreThroughLValue(RValue::get(Tmp), ResultRegDests[i]); 1746bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson } 1747fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson} 1748051303ce09291dfbed537fa33b0d8a4d92c82b75Tareq A. Siraj 1749051303ce09291dfbed537fa33b0d8a4d92c82b75Tareq A. Sirajvoid CodeGenFunction::EmitCapturedStmt(const CapturedStmt &S) { 1750051303ce09291dfbed537fa33b0d8a4d92c82b75Tareq A. Siraj llvm_unreachable("not implemented yet"); 1751051303ce09291dfbed537fa33b0d8a4d92c82b75Tareq A. Siraj} 1752