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"
191bea653e0d0f0182ed6e0deb5c18ad1123bb3bbdTim Northover#include "clang/Sema/SemaDiagnostic.h"
207d22bf00dbabca86ba791f56a99e006181fa22ddChris Lattner#include "clang/Basic/PrettyStackTrace.h"
21fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson#include "clang/Basic/TargetInfo.h"
22fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson#include "llvm/ADT/StringExtras.h"
233b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/DataLayout.h"
243b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/InlineAsm.h"
253b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/Intrinsics.h"
26524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir#include "llvm/Support/CallSite.h"
275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen;
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                              Statement Emission
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
340912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbarvoid CodeGenFunction::EmitStopPoint(const Stmt *S) {
35e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  if (CGDebugInfo *DI = getDebugInfo()) {
3673fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher    SourceLocation Loc;
372736f2eca78b498dd75c739a76f833ed43cdb43bAdrian Prantl    Loc = S->getLocStart();
3873fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher    DI->EmitLocation(Builder, Loc);
39fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl
404008088b002ba98ecbafdcc2d1f2ef1f41236b09Adrian Prantl    LastStopPoint = Loc;
410912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  }
420912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar}
430912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar
445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid CodeGenFunction::EmitStmt(const Stmt *S) {
455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(S && "Null statement?");
46a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar
47f9aac38d1290c17192a74b5bc2de52b9fb1a87caEric Christopher  // These statements have their own debug info handling.
480912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  if (EmitSimpleStmt(S))
490912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar    return;
500912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar
51d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  // Check if we are generating unreachable code.
52d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  if (!HaveInsertPoint()) {
53d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar    // If so, and the statement doesn't contain a label, then we do not need to
54d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar    // generate actual code. This is safe because (1) the current point is
55d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar    // unreachable, so we don't need to execute the code, and (2) we've already
56d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar    // handled the statements which update internal data structures (like the
57d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar    // local variable map) which could be used by subsequent statements.
58d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar    if (!ContainsLabel(S)) {
59d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar      // Verify that any decl statements were handled as simple, they may be in
60d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar      // scope of subsequent reachable statements.
61d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar      assert(!isa<DeclStmt>(*S) && "Unexpected DeclStmt!");
62d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar      return;
63d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar    }
64d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar
65d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar    // Otherwise, make a new block to hold the code.
66d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar    EnsureInsertPoint();
67d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  }
68d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar
690912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // Generate a stoppoint if we are emitting debug info.
700912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  EmitStopPoint(S);
71e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta
725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (S->getStmtClass()) {
732a41637a995affa1563f4d82a8b026e326a2faa0John McCall  case Stmt::NoStmtClass:
742a41637a995affa1563f4d82a8b026e326a2faa0John McCall  case Stmt::CXXCatchStmtClass:
7528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  case Stmt::SEHExceptStmtClass:
7628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  case Stmt::SEHFinallyStmtClass:
77ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor  case Stmt::MSDependentExistsStmtClass:
784fa7eab771ab8212e1058bd1a91061ff120c8fbbAlexey Bataev  case Stmt::OMPParallelDirectiveClass:
792a41637a995affa1563f4d82a8b026e326a2faa0John McCall    llvm_unreachable("invalid statement class to emit generically");
802a41637a995affa1563f4d82a8b026e326a2faa0John McCall  case Stmt::NullStmtClass:
812a41637a995affa1563f4d82a8b026e326a2faa0John McCall  case Stmt::CompoundStmtClass:
822a41637a995affa1563f4d82a8b026e326a2faa0John McCall  case Stmt::DeclStmtClass:
832a41637a995affa1563f4d82a8b026e326a2faa0John McCall  case Stmt::LabelStmtClass:
84534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith  case Stmt::AttributedStmtClass:
852a41637a995affa1563f4d82a8b026e326a2faa0John McCall  case Stmt::GotoStmtClass:
862a41637a995affa1563f4d82a8b026e326a2faa0John McCall  case Stmt::BreakStmtClass:
872a41637a995affa1563f4d82a8b026e326a2faa0John McCall  case Stmt::ContinueStmtClass:
882a41637a995affa1563f4d82a8b026e326a2faa0John McCall  case Stmt::DefaultStmtClass:
892a41637a995affa1563f4d82a8b026e326a2faa0John McCall  case Stmt::CaseStmtClass:
902a41637a995affa1563f4d82a8b026e326a2faa0John McCall    llvm_unreachable("should have emitted these statements as simple");
91cd5e60e1d4093b9a757cc85e35fccc093f8f8527Daniel Dunbar
922a41637a995affa1563f4d82a8b026e326a2faa0John McCall#define STMT(Type, Base)
932a41637a995affa1563f4d82a8b026e326a2faa0John McCall#define ABSTRACT_STMT(Op)
942a41637a995affa1563f4d82a8b026e326a2faa0John McCall#define EXPR(Type, Base) \
952a41637a995affa1563f4d82a8b026e326a2faa0John McCall  case Stmt::Type##Class:
962a41637a995affa1563f4d82a8b026e326a2faa0John McCall#include "clang/AST/StmtNodes.inc"
97cd5b22e12b6513163dd131589746c194090f14e6John McCall  {
98cd5b22e12b6513163dd131589746c194090f14e6John McCall    // Remember the block we came in on.
99cd5b22e12b6513163dd131589746c194090f14e6John McCall    llvm::BasicBlock *incoming = Builder.GetInsertBlock();
100cd5b22e12b6513163dd131589746c194090f14e6John McCall    assert(incoming && "expression emission must have an insertion point");
101cd5b22e12b6513163dd131589746c194090f14e6John McCall
1022a41637a995affa1563f4d82a8b026e326a2faa0John McCall    EmitIgnoredExpr(cast<Expr>(S));
1031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
104cd5b22e12b6513163dd131589746c194090f14e6John McCall    llvm::BasicBlock *outgoing = Builder.GetInsertBlock();
105cd5b22e12b6513163dd131589746c194090f14e6John McCall    assert(outgoing && "expression emission cleared block!");
106cd5b22e12b6513163dd131589746c194090f14e6John McCall
107cd5b22e12b6513163dd131589746c194090f14e6John McCall    // The expression emitters assume (reasonably!) that the insertion
108cd5b22e12b6513163dd131589746c194090f14e6John McCall    // point is always set.  To maintain that, the call-emission code
109cd5b22e12b6513163dd131589746c194090f14e6John McCall    // for noreturn functions has to enter a new block with no
110cd5b22e12b6513163dd131589746c194090f14e6John McCall    // predecessors.  We want to kill that block and mark the current
111cd5b22e12b6513163dd131589746c194090f14e6John McCall    // insertion point unreachable in the common case of a call like
112cd5b22e12b6513163dd131589746c194090f14e6John McCall    // "exit();".  Since expression emission doesn't otherwise create
113cd5b22e12b6513163dd131589746c194090f14e6John McCall    // blocks with no predecessors, we can just test for that.
114cd5b22e12b6513163dd131589746c194090f14e6John McCall    // However, we must be careful not to do this to our incoming
115cd5b22e12b6513163dd131589746c194090f14e6John McCall    // block, because *statement* emission does sometimes create
116cd5b22e12b6513163dd131589746c194090f14e6John McCall    // reachable blocks which will have no predecessors until later in
117cd5b22e12b6513163dd131589746c194090f14e6John McCall    // the function.  This occurs with, e.g., labels that are not
118cd5b22e12b6513163dd131589746c194090f14e6John McCall    // reachable by fallthrough.
119cd5b22e12b6513163dd131589746c194090f14e6John McCall    if (incoming != outgoing && outgoing->use_empty()) {
120cd5b22e12b6513163dd131589746c194090f14e6John McCall      outgoing->eraseFromParent();
121cd5b22e12b6513163dd131589746c194090f14e6John McCall      Builder.ClearInsertionPoint();
1225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
124cd5b22e12b6513163dd131589746c194090f14e6John McCall  }
1252a41637a995affa1563f4d82a8b026e326a2faa0John McCall
1261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case Stmt::IndirectGotoStmtClass:
1270ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar    EmitIndirectGotoStmt(cast<IndirectGotoStmt>(*S)); break;
1285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Stmt::IfStmtClass:       EmitIfStmt(cast<IfStmt>(*S));             break;
1305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Stmt::WhileStmtClass:    EmitWhileStmt(cast<WhileStmt>(*S));       break;
1315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Stmt::DoStmtClass:       EmitDoStmt(cast<DoStmt>(*S));             break;
1325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Stmt::ForStmtClass:      EmitForStmt(cast<ForStmt>(*S));           break;
1331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Stmt::ReturnStmtClass:   EmitReturnStmt(cast<ReturnStmt>(*S));     break;
135a4275d194b656867bdcdb725b2a7ba3251a1a638Daniel Dunbar
13651b09f2c528c8460b5465c676173324e44176d62Devang Patel  case Stmt::SwitchStmtClass:   EmitSwitchStmt(cast<SwitchStmt>(*S));     break;
137d1a8d2ef757ad880d48b2249a1619bf8209e9cf8Chad Rosier  case Stmt::GCCAsmStmtClass:   // Intentional fall-through.
138d1a8d2ef757ad880d48b2249a1619bf8209e9cf8Chad Rosier  case Stmt::MSAsmStmtClass:    EmitAsmStmt(cast<AsmStmt>(*S));           break;
139051303ce09291dfbed537fa33b0d8a4d92c82b75Tareq A. Siraj  case Stmt::CapturedStmtClass:
140524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    EmitCapturedStmt(cast<CapturedStmt>(*S), CR_Default);
141051303ce09291dfbed537fa33b0d8a4d92c82b75Tareq A. Siraj    break;
1420a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar  case Stmt::ObjCAtTryStmtClass:
14364d5d6c5903157c521af496479d06dc26032d718Anders Carlsson    EmitObjCAtTryStmt(cast<ObjCAtTryStmt>(*S));
1441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    break;
1450a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar  case Stmt::ObjCAtCatchStmtClass:
146b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable(
147b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie                    "@catch statements should be handled by EmitObjCAtTryStmt");
1480a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar  case Stmt::ObjCAtFinallyStmtClass:
149b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable(
150b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie                  "@finally statements should be handled by EmitObjCAtTryStmt");
1510a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar  case Stmt::ObjCAtThrowStmtClass:
15264d5d6c5903157c521af496479d06dc26032d718Anders Carlsson    EmitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(*S));
1530a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar    break;
1540a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar  case Stmt::ObjCAtSynchronizedStmtClass:
15510cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattner    EmitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(*S));
1560a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar    break;
1571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case Stmt::ObjCForCollectionStmtClass:
1583d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson    EmitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(*S));
1590a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar    break;
160f85e193739c953358c865005855253af4f68a497John McCall  case Stmt::ObjCAutoreleasePoolStmtClass:
161f85e193739c953358c865005855253af4f68a497John McCall    EmitObjCAutoreleasePoolStmt(cast<ObjCAutoreleasePoolStmt>(*S));
162f85e193739c953358c865005855253af4f68a497John McCall    break;
1636f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
1646815e941998659a55c20c147861b0f437928c3d8Anders Carlsson  case Stmt::CXXTryStmtClass:
1656815e941998659a55c20c147861b0f437928c3d8Anders Carlsson    EmitCXXTryStmt(cast<CXXTryStmt>(*S));
1666815e941998659a55c20c147861b0f437928c3d8Anders Carlsson    break;
167ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  case Stmt::CXXForRangeStmtClass:
168ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    EmitCXXForRangeStmt(cast<CXXForRangeStmt>(*S));
16928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  case Stmt::SEHTryStmtClass:
17028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    // FIXME Not yet implemented
171ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    break;
1725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1750912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbarbool CodeGenFunction::EmitSimpleStmt(const Stmt *S) {
1760912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  switch (S->getStmtClass()) {
1770912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  default: return false;
1780912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  case Stmt::NullStmtClass: break;
1790912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  case Stmt::CompoundStmtClass: EmitCompoundStmt(cast<CompoundStmt>(*S)); break;
180d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  case Stmt::DeclStmtClass:     EmitDeclStmt(cast<DeclStmt>(*S));         break;
1810912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  case Stmt::LabelStmtClass:    EmitLabelStmt(cast<LabelStmt>(*S));       break;
182534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith  case Stmt::AttributedStmtClass:
183534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith                            EmitAttributedStmt(cast<AttributedStmt>(*S)); break;
1840912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  case Stmt::GotoStmtClass:     EmitGotoStmt(cast<GotoStmt>(*S));         break;
1850912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  case Stmt::BreakStmtClass:    EmitBreakStmt(cast<BreakStmt>(*S));       break;
1860912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  case Stmt::ContinueStmtClass: EmitContinueStmt(cast<ContinueStmt>(*S)); break;
1870912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  case Stmt::DefaultStmtClass:  EmitDefaultStmt(cast<DefaultStmt>(*S));   break;
1880912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  case Stmt::CaseStmtClass:     EmitCaseStmt(cast<CaseStmt>(*S));         break;
1890912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  }
1900912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar
1910912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  return true;
1920912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar}
1930912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar
1943379320c10001d7e1ee5d7e7142c417f797cfe82Chris Lattner/// EmitCompoundStmt - Emit a compound statement {..} node.  If GetLast is true,
1953379320c10001d7e1ee5d7e7142c417f797cfe82Chris Lattner/// this captures the expression result of the last sub-statement and returns it
1963379320c10001d7e1ee5d7e7142c417f797cfe82Chris Lattner/// (for use by the statement expression extension).
1972ac2fa7726eb2f6fe445e098489e92931c31f7caEli Friedmanllvm::Value* CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast,
1982ac2fa7726eb2f6fe445e098489e92931c31f7caEli Friedman                                               AggValueSlot AggSlot) {
1997d22bf00dbabca86ba791f56a99e006181fa22ddChris Lattner  PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),S.getLBracLoc(),
2007d22bf00dbabca86ba791f56a99e006181fa22ddChris Lattner                             "LLVM IR generation of compound statement ('{}')");
2011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
202fdc5d565b30bd2009ec98aac4b5846a740aff767Eric Christopher  // Keep track of the current cleanup stack depth, including debug scopes.
203fdc5d565b30bd2009ec98aac4b5846a740aff767Eric Christopher  LexicalScope Scope(*this, S.getSourceRange());
2041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
205a6504853d297c30cfa271f4710af5a3d5db59449David Blaikie  return EmitCompoundStmtWithoutScope(S, GetLast, AggSlot);
206a6504853d297c30cfa271f4710af5a3d5db59449David Blaikie}
207a6504853d297c30cfa271f4710af5a3d5db59449David Blaikie
2082ac2fa7726eb2f6fe445e098489e92931c31f7caEli Friedmanllvm::Value*
2092ac2fa7726eb2f6fe445e098489e92931c31f7caEli FriedmanCodeGenFunction::EmitCompoundStmtWithoutScope(const CompoundStmt &S,
2102ac2fa7726eb2f6fe445e098489e92931c31f7caEli Friedman                                              bool GetLast,
2112ac2fa7726eb2f6fe445e098489e92931c31f7caEli Friedman                                              AggValueSlot AggSlot) {
212a6504853d297c30cfa271f4710af5a3d5db59449David Blaikie
2133379320c10001d7e1ee5d7e7142c417f797cfe82Chris Lattner  for (CompoundStmt::const_body_iterator I = S.body_begin(),
2143379320c10001d7e1ee5d7e7142c417f797cfe82Chris Lattner       E = S.body_end()-GetLast; I != E; ++I)
2155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    EmitStmt(*I);
216e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta
2172ac2fa7726eb2f6fe445e098489e92931c31f7caEli Friedman  llvm::Value *RetAlloca = 0;
2182ac2fa7726eb2f6fe445e098489e92931c31f7caEli Friedman  if (GetLast) {
2191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // We have to special case labels here.  They are statements, but when put
22017d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson    // at the end of a statement expression, they yield the value of their
22117d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson    // subexpression.  Handle this by walking through all labels we encounter,
22217d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson    // emitting them before we evaluate the subexpr.
22317d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson    const Stmt *LastStmt = S.body_back();
22417d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson    while (const LabelStmt *LS = dyn_cast<LabelStmt>(LastStmt)) {
225ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner      EmitLabel(LS->getDecl());
22617d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson      LastStmt = LS->getSubStmt();
22717d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson    }
2281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22917d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson    EnsureInsertPoint();
2301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2312ac2fa7726eb2f6fe445e098489e92931c31f7caEli Friedman    QualType ExprTy = cast<Expr>(LastStmt)->getType();
2322ac2fa7726eb2f6fe445e098489e92931c31f7caEli Friedman    if (hasAggregateEvaluationKind(ExprTy)) {
2332ac2fa7726eb2f6fe445e098489e92931c31f7caEli Friedman      EmitAggExpr(cast<Expr>(LastStmt), AggSlot);
2342ac2fa7726eb2f6fe445e098489e92931c31f7caEli Friedman    } else {
2352ac2fa7726eb2f6fe445e098489e92931c31f7caEli Friedman      // We can't return an RValue here because there might be cleanups at
2362ac2fa7726eb2f6fe445e098489e92931c31f7caEli Friedman      // the end of the StmtExpr.  Because of that, we have to emit the result
2372ac2fa7726eb2f6fe445e098489e92931c31f7caEli Friedman      // here into a temporary alloca.
2382ac2fa7726eb2f6fe445e098489e92931c31f7caEli Friedman      RetAlloca = CreateMemTemp(ExprTy);
2392ac2fa7726eb2f6fe445e098489e92931c31f7caEli Friedman      EmitAnyExprToMem(cast<Expr>(LastStmt), RetAlloca, Qualifiers(),
2402ac2fa7726eb2f6fe445e098489e92931c31f7caEli Friedman                       /*IsInit*/false);
2412ac2fa7726eb2f6fe445e098489e92931c31f7caEli Friedman    }
2422ac2fa7726eb2f6fe445e098489e92931c31f7caEli Friedman
24391d723da7b68be5245c3ac58aa2a36d04658cfb8Chris Lattner  }
244a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar
2452ac2fa7726eb2f6fe445e098489e92931c31f7caEli Friedman  return RetAlloca;
2465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
248aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbarvoid CodeGenFunction::SimplifyForwardingBlocks(llvm::BasicBlock *BB) {
249aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  llvm::BranchInst *BI = dyn_cast<llvm::BranchInst>(BB->getTerminator());
2501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
251aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  // If there is a cleanup stack, then we it isn't worth trying to
252aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  // simplify this block (we would need to remove it from the scope map
253aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  // and cleanup entry).
254f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!EHStack.empty())
255aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar    return;
256aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar
257aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  // Can only simplify direct branches.
258aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  if (!BI || !BI->isUnconditional())
259aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar    return;
260aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar
2613d7c780d93ebcb84a1224de8c44835ea43e82b15Eli Friedman  // Can only simplify empty blocks.
2623d7c780d93ebcb84a1224de8c44835ea43e82b15Eli Friedman  if (BI != BB->begin())
2633d7c780d93ebcb84a1224de8c44835ea43e82b15Eli Friedman    return;
2643d7c780d93ebcb84a1224de8c44835ea43e82b15Eli Friedman
265aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  BB->replaceAllUsesWith(BI->getSuccessor(0));
266aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  BI->eraseFromParent();
267aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  BB->eraseFromParent();
268aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar}
269aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar
270a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbarvoid CodeGenFunction::EmitBlock(llvm::BasicBlock *BB, bool IsFinished) {
271548ce5e78215a34f409d597bb2c1e9f897a8eda3John McCall  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
272548ce5e78215a34f409d597bb2c1e9f897a8eda3John McCall
273d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  // Fall out of the current block (if necessary).
274d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  EmitBranch(BB);
275a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar
276a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar  if (IsFinished && BB->use_empty()) {
277a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar    delete BB;
278a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar    return;
279a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar  }
280a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar
281839cbaa004a24e8f1ea14db5ed76e3d25ed28996John McCall  // Place the block after the current block, if possible, or else at
282839cbaa004a24e8f1ea14db5ed76e3d25ed28996John McCall  // the end of the function.
283548ce5e78215a34f409d597bb2c1e9f897a8eda3John McCall  if (CurBB && CurBB->getParent())
284548ce5e78215a34f409d597bb2c1e9f897a8eda3John McCall    CurFn->getBasicBlockList().insertAfter(CurBB, BB);
285839cbaa004a24e8f1ea14db5ed76e3d25ed28996John McCall  else
286839cbaa004a24e8f1ea14db5ed76e3d25ed28996John McCall    CurFn->getBasicBlockList().push_back(BB);
287d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  Builder.SetInsertPoint(BB);
288d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar}
289d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar
290d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbarvoid CodeGenFunction::EmitBranch(llvm::BasicBlock *Target) {
291d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  // Emit a branch from the current block to the target one if this
292d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  // was a real block.  If this was just a fall-through block after a
293d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  // terminator, don't emit it.
294d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
295d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar
296d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  if (!CurBB || CurBB->getTerminator()) {
297d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar    // If there is no insert point or the previous block is already
298d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar    // terminated, don't touch it.
2995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } else {
3005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Otherwise, create a fall-through branch.
301d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar    Builder.CreateBr(Target);
3025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3035e08ad3cc62ab94649959ae227a9a411a729bf49Daniel Dunbar
3045e08ad3cc62ab94649959ae227a9a411a729bf49Daniel Dunbar  Builder.ClearInsertionPoint();
3055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
307777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCallvoid CodeGenFunction::EmitBlockAfterUses(llvm::BasicBlock *block) {
308777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  bool inserted = false;
309777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  for (llvm::BasicBlock::use_iterator
310777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall         i = block->use_begin(), e = block->use_end(); i != e; ++i) {
311777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    if (llvm::Instruction *insn = dyn_cast<llvm::Instruction>(*i)) {
312777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      CurFn->getBasicBlockList().insertAfter(insn->getParent(), block);
313777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      inserted = true;
314777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      break;
315777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    }
316777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  }
317777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
318777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  if (!inserted)
319777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    CurFn->getBasicBlockList().push_back(block);
320777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
321777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  Builder.SetInsertPoint(block);
322777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall}
323777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
324f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallCodeGenFunction::JumpDest
325ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris LattnerCodeGenFunction::getJumpDestForLabel(const LabelDecl *D) {
326ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  JumpDest &Dest = LabelMap[D];
327ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  if (Dest.isValid()) return Dest;
328f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
329f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Create, but don't insert, the new block.
330ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  Dest = JumpDest(createBasicBlock(D->getName()),
331ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall                  EHScopeStack::stable_iterator::invalid(),
332ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall                  NextCleanupDestIndex++);
333f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  return Dest;
334f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
335f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
336ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattnervoid CodeGenFunction::EmitLabel(const LabelDecl *D) {
337495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem  // Add this label to the current lexical scope if we're within any
338495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem  // normal cleanups.  Jumps "in" to this label --- when permitted by
339495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem  // the language --- may need to be routed around such cleanups.
340495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem  if (EHStack.hasNormalCleanups() && CurLexicalScope)
341495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem    CurLexicalScope->addLabel(D);
342495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem
343ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  JumpDest &Dest = LabelMap[D];
344f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
345ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  // If we didn't need a forward reference to this label, just go
346f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // ahead and create a destination at the current scope.
347ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  if (!Dest.isValid()) {
348ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner    Dest = getJumpDestInCurrentScope(D->getName());
349f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
350f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Otherwise, we need to give this label a target depth and remove
351f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // it from the branch-fixups list.
352f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  } else {
353ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    assert(!Dest.getScopeDepth().isValid() && "already emitted label!");
354495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem    Dest.setScopeDepth(EHStack.stable_begin());
355ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    ResolveBranchFixups(Dest.getBlock());
356f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
357f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
358ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(Dest.getBlock());
35991d723da7b68be5245c3ac58aa2a36d04658cfb8Chris Lattner}
36091d723da7b68be5245c3ac58aa2a36d04658cfb8Chris Lattner
361495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem/// Change the cleanup scope of the labels in this lexical scope to
362495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem/// match the scope of the enclosing context.
363495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotemvoid CodeGenFunction::LexicalScope::rescopeLabels() {
364495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem  assert(!Labels.empty());
365495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem  EHScopeStack::stable_iterator innermostScope
366495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem    = CGF.EHStack.getInnermostNormalCleanup();
367495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem
368495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem  // Change the scope depth of all the labels.
369495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem  for (SmallVectorImpl<const LabelDecl*>::const_iterator
370495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem         i = Labels.begin(), e = Labels.end(); i != e; ++i) {
371495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem    assert(CGF.LabelMap.count(*i));
372495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem    JumpDest &dest = CGF.LabelMap.find(*i)->second;
373495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem    assert(dest.getScopeDepth().isValid());
374495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem    assert(innermostScope.encloses(dest.getScopeDepth()));
375495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem    dest.setScopeDepth(innermostScope);
376495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem  }
377495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem
378495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem  // Reparent the labels if the new scope also has cleanups.
379495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem  if (innermostScope != EHScopeStack::stable_end() && ParentScope) {
380495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem    ParentScope->Labels.append(Labels.begin(), Labels.end());
381495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem  }
382495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem}
383495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem
38491d723da7b68be5245c3ac58aa2a36d04658cfb8Chris Lattner
38591d723da7b68be5245c3ac58aa2a36d04658cfb8Chris Lattnervoid CodeGenFunction::EmitLabelStmt(const LabelStmt &S) {
386ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  EmitLabel(S.getDecl());
3875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  EmitStmt(S.getSubStmt());
3885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
390534986f2b21e6050bf00163cd6423fd92155a6edRichard Smithvoid CodeGenFunction::EmitAttributedStmt(const AttributedStmt &S) {
391534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith  EmitStmt(S.getSubStmt());
392534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith}
393534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith
3945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid CodeGenFunction::EmitGotoStmt(const GotoStmt &S) {
3950912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // If this code is reachable then emit a stop point (if generating
3960912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // debug info). We have to do this ourselves because we are on the
3970912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // "simple" statement path.
3980912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  if (HaveInsertPoint())
3990912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar    EmitStopPoint(&S);
40036a2ada69fdb457b0e46d0ef452c150b360d8888Mike Stump
401f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitBranchThroughCleanup(getJumpDestForLabel(S.getLabel()));
4025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4043d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
4050ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbarvoid CodeGenFunction::EmitIndirectGotoStmt(const IndirectGotoStmt &S) {
406ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  if (const LabelDecl *Target = S.getConstantTarget()) {
40795c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall    EmitBranchThroughCleanup(getJumpDestForLabel(Target));
40895c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall    return;
40995c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall  }
41095c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall
41149c952f853fe2d15dd9c9ff2a29c696bd18fca13Chris Lattner  // Ensure that we have an i8* for our PHI node.
412d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  llvm::Value *V = Builder.CreateBitCast(EmitScalarExpr(S.getTarget()),
413d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall                                         Int8PtrTy, "addr");
4143d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
4153d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
4163d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  // Get the basic block for the indirect goto.
4173d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  llvm::BasicBlock *IndGotoBB = GetIndirectGotoBlock();
4186f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
4193d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  // The first instruction in the block has to be the PHI for the switch dest,
4203d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  // add an entry for this branch.
4213d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  cast<llvm::PHINode>(IndGotoBB->begin())->addIncoming(V, CurBB);
4226f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
4233d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  EmitBranch(IndGotoBB);
4240ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar}
4250ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar
42662b72f642207ba2ba433d686df924dc9594e9897Chris Lattnervoid CodeGenFunction::EmitIfStmt(const IfStmt &S) {
4275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.8.4.1: The first substatement is executed if the expression compares
4285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // unequal to 0.  The condition must be a scalar type.
4297b309b0107093d0d2b4df2ce1deddd9bd4698cffEric Christopher  LexicalScope ConditionScope(*this, S.getSourceRange());
4308d378585f66c0562a818f60d3b003a6f55054dbeAdrian Prantl
4318cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor  if (S.getConditionVariable())
432b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall    EmitAutoVarDecl(*S.getConditionVariable());
4331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4349bc47e29dce8f095be7a6d07dbb02a5a7a112949Chris Lattner  // If the condition constant folds and can be elided, try to avoid emitting
4359bc47e29dce8f095be7a6d07dbb02a5a7a112949Chris Lattner  // the condition and the dead arm of the if/else.
436c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner  bool CondConstant;
437c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner  if (ConstantFoldsToSimpleInteger(S.getCond(), CondConstant)) {
43862b72f642207ba2ba433d686df924dc9594e9897Chris Lattner    // Figure out which block (then or else) is executed.
439c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner    const Stmt *Executed = S.getThen();
440c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner    const Stmt *Skipped  = S.getElse();
441c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner    if (!CondConstant)  // Condition false?
44262b72f642207ba2ba433d686df924dc9594e9897Chris Lattner      std::swap(Executed, Skipped);
4431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
44462b72f642207ba2ba433d686df924dc9594e9897Chris Lattner    // If the skipped block has no labels in it, just emit the executed block.
44562b72f642207ba2ba433d686df924dc9594e9897Chris Lattner    // This avoids emitting dead code and simplifies the CFG substantially.
4469bc47e29dce8f095be7a6d07dbb02a5a7a112949Chris Lattner    if (!ContainsLabel(Skipped)) {
44701234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor      if (Executed) {
448f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall        RunCleanupsScope ExecutedScope(*this);
44962b72f642207ba2ba433d686df924dc9594e9897Chris Lattner        EmitStmt(Executed);
45001234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor      }
45162b72f642207ba2ba433d686df924dc9594e9897Chris Lattner      return;
45262b72f642207ba2ba433d686df924dc9594e9897Chris Lattner    }
45362b72f642207ba2ba433d686df924dc9594e9897Chris Lattner  }
4549bc47e29dce8f095be7a6d07dbb02a5a7a112949Chris Lattner
4559bc47e29dce8f095be7a6d07dbb02a5a7a112949Chris Lattner  // Otherwise, the condition did not fold, or we couldn't elide it.  Just emit
4569bc47e29dce8f095be7a6d07dbb02a5a7a112949Chris Lattner  // the conditional branch.
457781d7ca9b2fd626ef34bdc3fe06765eeff7ab2bcDaniel Dunbar  llvm::BasicBlock *ThenBlock = createBasicBlock("if.then");
458781d7ca9b2fd626ef34bdc3fe06765eeff7ab2bcDaniel Dunbar  llvm::BasicBlock *ContBlock = createBasicBlock("if.end");
459781d7ca9b2fd626ef34bdc3fe06765eeff7ab2bcDaniel Dunbar  llvm::BasicBlock *ElseBlock = ContBlock;
4605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (S.getElse())
461781d7ca9b2fd626ef34bdc3fe06765eeff7ab2bcDaniel Dunbar    ElseBlock = createBasicBlock("if.else");
462781d7ca9b2fd626ef34bdc3fe06765eeff7ab2bcDaniel Dunbar  EmitBranchOnBoolExpr(S.getCond(), ThenBlock, ElseBlock);
4631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Emit the 'then' code.
46501234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor  EmitBlock(ThenBlock);
46601234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor  {
467f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    RunCleanupsScope ThenScope(*this);
46801234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor    EmitStmt(S.getThen());
46901234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor  }
470d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  EmitBranch(ContBlock);
4711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Emit the 'else' code if present.
4735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (const Stmt *Else = S.getElse()) {
474acd723666777b4ac1f79a97b6a300e6cf919d519Devang Patel    // There is no need to emit line number for unconditional branch.
475acd723666777b4ac1f79a97b6a300e6cf919d519Devang Patel    if (getDebugInfo())
476acd723666777b4ac1f79a97b6a300e6cf919d519Devang Patel      Builder.SetCurrentDebugLocation(llvm::DebugLoc());
4775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    EmitBlock(ElseBlock);
47801234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor    {
479f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      RunCleanupsScope ElseScope(*this);
48001234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor      EmitStmt(Else);
48101234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor    }
482acd723666777b4ac1f79a97b6a300e6cf919d519Devang Patel    // There is no need to emit line number for unconditional branch.
483acd723666777b4ac1f79a97b6a300e6cf919d519Devang Patel    if (getDebugInfo())
484acd723666777b4ac1f79a97b6a300e6cf919d519Devang Patel      Builder.SetCurrentDebugLocation(llvm::DebugLoc());
485d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar    EmitBranch(ContBlock);
4865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Emit the continuation block for code after the if.
489c22d665ede76f70228055d638a087f4bd438292dDaniel Dunbar  EmitBlock(ContBlock, true);
4905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid CodeGenFunction::EmitWhileStmt(const WhileStmt &S) {
493f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Emit the header for the loop, which will also become
494f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // the continue target.
495f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest LoopHeader = getJumpDestInCurrentScope("while.cond");
496ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(LoopHeader.getBlock());
497f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
498f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Create an exit block for when the condition fails, which will
499f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // also become the break target.
500f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest LoopExit = getJumpDestInCurrentScope("while.end");
50172cac2ccce8058833f56358e3391e28a8ddeeaa4Mike Stump
50272cac2ccce8058833f56358e3391e28a8ddeeaa4Mike Stump  // Store the blocks to use for break and continue.
503f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  BreakContinueStack.push_back(BreakContinue(LoopExit, LoopHeader));
5041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5055656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  // C++ [stmt.while]p2:
5065656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  //   When the condition of a while statement is a declaration, the
5075656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  //   scope of the variable that is declared extends from its point
5085656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  //   of declaration (3.3.2) to the end of the while statement.
5095656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  //   [...]
5105656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  //   The object created in a condition is destroyed and created
5115656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  //   with each iteration of the loop.
512f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  RunCleanupsScope ConditionScope(*this);
5135656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor
514f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (S.getConditionVariable())
515b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall    EmitAutoVarDecl(*S.getConditionVariable());
5166f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
51716b16206741f5139c4ad870632db8f9ea4c6c943Mike Stump  // Evaluate the conditional in the while header.  C99 6.8.5.1: The
51816b16206741f5139c4ad870632db8f9ea4c6c943Mike Stump  // evaluation of the controlling expression takes place before each
51916b16206741f5139c4ad870632db8f9ea4c6c943Mike Stump  // execution of the loop body.
5205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
5216f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
5222c30d8fee8981ca4f20a477456dae1b722b53f1dDevang Patel  // while(1) is common, avoid extra exit blocks.  Be sure
5235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // to correctly handle break/continue though.
5242c30d8fee8981ca4f20a477456dae1b722b53f1dDevang Patel  bool EmitBoolCondBranch = true;
5251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal))
5262c30d8fee8981ca4f20a477456dae1b722b53f1dDevang Patel    if (C->isOne())
5272c30d8fee8981ca4f20a477456dae1b722b53f1dDevang Patel      EmitBoolCondBranch = false;
5281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // As long as the condition is true, go to the loop body.
530f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *LoopBody = createBasicBlock("while.body");
531f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (EmitBoolCondBranch) {
532ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    llvm::BasicBlock *ExitBlock = LoopExit.getBlock();
533f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    if (ConditionScope.requiresCleanups())
534f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      ExitBlock = createBasicBlock("while.exit");
535f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
536f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock);
537f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
538ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    if (ExitBlock != LoopExit.getBlock()) {
539f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      EmitBlock(ExitBlock);
540f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      EmitBranchThroughCleanup(LoopExit);
541f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    }
542f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
5436f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
544f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Emit the loop body.  We have to emit this in a cleanup scope
545f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // because it might be a singleton DeclStmt.
5465656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  {
547f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    RunCleanupsScope BodyScope(*this);
5485656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    EmitBlock(LoopBody);
5495656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    EmitStmt(S.getBody());
5505656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  }
551da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner
5521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  BreakContinueStack.pop_back();
5531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
554f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Immediately force cleanup.
555f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ConditionScope.ForceCleanup();
5565656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor
557f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Branch to the loop header again.
558ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBranch(LoopHeader.getBlock());
5591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Emit the exit block.
561ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(LoopExit.getBlock(), true);
5625656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor
563aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  // The LoopHeader typically is just a branch if we skipped emitting
564aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  // a branch, try to erase it.
565f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!EmitBoolCondBranch)
566ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    SimplifyForwardingBlocks(LoopHeader.getBlock());
5675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid CodeGenFunction::EmitDoStmt(const DoStmt &S) {
570f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest LoopExit = getJumpDestInCurrentScope("do.end");
571f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest LoopCond = getJumpDestInCurrentScope("do.cond");
5721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
573da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner  // Store the blocks to use for break and continue.
574f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  BreakContinueStack.push_back(BreakContinue(LoopExit, LoopCond));
5751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
576f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Emit the body of the loop.
577f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *LoopBody = createBasicBlock("do.body");
578f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitBlock(LoopBody);
579f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  {
580f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    RunCleanupsScope BodyScope(*this);
581f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    EmitStmt(S.getBody());
582f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
5831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
584e4b6d342c29d5cb9d311756100df1603810fa892Anders Carlsson  BreakContinueStack.pop_back();
5851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
586ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(LoopCond.getBlock());
5871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.8.5.2: "The evaluation of the controlling expression takes place
5895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // after each execution of the loop body."
5901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Evaluate the conditional in the while header.
5925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.8.5p2/p4: The first substatement is executed if the expression
5935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // compares unequal to 0.  The condition must be a scalar type.
5945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
59505f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel
59605f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel  // "do {} while (0)" is common in macros, avoid extra blocks.  Be sure
59705f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel  // to correctly handle break/continue though.
59805f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel  bool EmitBoolCondBranch = true;
5991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal))
60005f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel    if (C->isZero())
60105f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel      EmitBoolCondBranch = false;
60205f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel
6035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // As long as the condition is true, iterate the loop.
60405f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel  if (EmitBoolCondBranch)
605ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    Builder.CreateCondBr(BoolCondVal, LoopBody, LoopExit.getBlock());
6061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Emit the exit block.
608ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(LoopExit.getBlock());
60905f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel
610aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  // The DoCond block typically is just a branch if we skipped
611aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  // emitting a branch, try to erase it.
612aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  if (!EmitBoolCondBranch)
613ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    SimplifyForwardingBlocks(LoopCond.getBlock());
6145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
6155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid CodeGenFunction::EmitForStmt(const ForStmt &S) {
617f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest LoopExit = getJumpDestInCurrentScope("for.end");
618f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
619f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  RunCleanupsScope ForScope(*this);
620da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner
6210554e0e30d24d9ad7d5e12f8e7583ebb5c9715bfDevang Patel  CGDebugInfo *DI = getDebugInfo();
62273fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher  if (DI)
62373fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher    DI->EmitLexicalBlockStart(Builder, S.getSourceRange().getBegin());
6240554e0e30d24d9ad7d5e12f8e7583ebb5c9715bfDevang Patel
6255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Evaluate the first part before the loop.
6265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (S.getInit())
6275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    EmitStmt(S.getInit());
6285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Start the loop with a block that tests the condition.
630f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // If there's an increment, the continue scope will be overwritten
631f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // later.
632f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest Continue = getJumpDestInCurrentScope("for.cond");
633ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::BasicBlock *CondBlock = Continue.getBlock();
6345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  EmitBlock(CondBlock);
6355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
636d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor  // Create a cleanup scope for the condition variable cleanups.
637f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  RunCleanupsScope ConditionScope(*this);
6386f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
639d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor  llvm::Value *BoolCondVal = 0;
6405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (S.getCond()) {
64199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    // If the for statement has a condition scope, emit the local variable
64299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    // declaration.
643ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    llvm::BasicBlock *ExitBlock = LoopExit.getBlock();
644d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor    if (S.getConditionVariable()) {
645b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall      EmitAutoVarDecl(*S.getConditionVariable());
646d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor    }
647f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
648f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    // If there are any cleanups between here and the loop-exit scope,
649f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    // create a block to stage a loop exit along.
650f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    if (ForScope.requiresCleanups())
651f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      ExitBlock = createBasicBlock("for.cond.cleanup");
6526f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
65331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    // As long as the condition is true, iterate the loop.
6549615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar    llvm::BasicBlock *ForBody = createBasicBlock("for.body");
6551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.8.5p2/p4: The first substatement is executed if the expression
6575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // compares unequal to 0.  The condition must be a scalar type.
658d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor    BoolCondVal = EvaluateExprAsBool(S.getCond());
659f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock);
660f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
661ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    if (ExitBlock != LoopExit.getBlock()) {
662f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      EmitBlock(ExitBlock);
663f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      EmitBranchThroughCleanup(LoopExit);
664f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    }
6651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EmitBlock(ForBody);
6675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } else {
6685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Treat it as a non-zero constant.  Don't even create a new block for the
6695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // body, just fall into it.
6705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
6715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If the for loop doesn't have an increment we can just use the
673f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // condition as the continue block.  Otherwise we'll need to create
674f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // a block for it (in the current scope, i.e. in the scope of the
675f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // condition), and that we will become our continue block.
676da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner  if (S.getInc())
677f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    Continue = getJumpDestInCurrentScope("for.inc");
6781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
679da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner  // Store the blocks to use for break and continue.
680f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
6813e9da66ac7e88d64d30ee777588677320660cf84Mike Stump
682d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor  {
683d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor    // Create a separate cleanup scope for the body, in case it is not
684d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor    // a compound statement.
685f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    RunCleanupsScope BodyScope(*this);
686d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor    EmitStmt(S.getBody());
687d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor  }
688da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner
6895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If there is an increment, emit it next.
690ad12b6d643aba6c36f5cec4c9beb4977a12eace4Daniel Dunbar  if (S.getInc()) {
691ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    EmitBlock(Continue.getBlock());
692883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner    EmitStmt(S.getInc());
693ad12b6d643aba6c36f5cec4c9beb4977a12eace4Daniel Dunbar  }
6941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
69545d3fe1898d3726d269a0bd2ccb8527102e29d79Douglas Gregor  BreakContinueStack.pop_back();
696f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
697f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ConditionScope.ForceCleanup();
698f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitBranch(CondBlock);
699f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
700f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ForScope.ForceCleanup();
701f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
70273fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher  if (DI)
70373fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher    DI->EmitLexicalBlockEnd(Builder, S.getSourceRange().getEnd());
7045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
705da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner  // Emit the fall-through block.
706ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(LoopExit.getBlock(), true);
7075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
709ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithvoid CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S) {
710ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  JumpDest LoopExit = getJumpDestInCurrentScope("for.end");
711ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
712ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  RunCleanupsScope ForScope(*this);
713ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
714ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  CGDebugInfo *DI = getDebugInfo();
71573fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher  if (DI)
71673fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher    DI->EmitLexicalBlockStart(Builder, S.getSourceRange().getBegin());
717ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
718ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // Evaluate the first pieces before the loop.
719ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  EmitStmt(S.getRangeStmt());
720ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  EmitStmt(S.getBeginEndStmt());
721ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
722ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // Start the loop with a block that tests the condition.
723ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // If there's an increment, the continue scope will be overwritten
724ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // later.
725ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
726ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  EmitBlock(CondBlock);
727ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
728ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // If there are any cleanups between here and the loop-exit scope,
729ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // create a block to stage a loop exit along.
730ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  llvm::BasicBlock *ExitBlock = LoopExit.getBlock();
731ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  if (ForScope.requiresCleanups())
732ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    ExitBlock = createBasicBlock("for.cond.cleanup");
7336f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
734ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // The loop body, consisting of the specified body and the loop variable.
735ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  llvm::BasicBlock *ForBody = createBasicBlock("for.body");
736ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
737ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // The body is executed if the expression, contextually converted
738ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // to bool, is true.
739ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
740ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock);
741ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
742ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  if (ExitBlock != LoopExit.getBlock()) {
743ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    EmitBlock(ExitBlock);
744ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    EmitBranchThroughCleanup(LoopExit);
745ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
746ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
747ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  EmitBlock(ForBody);
748ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
749ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // Create a block for the increment. In case of a 'continue', we jump there.
750ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  JumpDest Continue = getJumpDestInCurrentScope("for.inc");
751ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
752ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // Store the blocks to use for break and continue.
753ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
754ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
755ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  {
756ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    // Create a separate cleanup scope for the loop variable and body.
757ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    RunCleanupsScope BodyScope(*this);
758ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    EmitStmt(S.getLoopVarStmt());
759ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    EmitStmt(S.getBody());
760ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
761ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
762ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // If there is an increment, emit it next.
763ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  EmitBlock(Continue.getBlock());
764ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  EmitStmt(S.getInc());
765ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
766ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  BreakContinueStack.pop_back();
767ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
768ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  EmitBranch(CondBlock);
769ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
770ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  ForScope.ForceCleanup();
771ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
77273fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher  if (DI)
77373fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher    DI->EmitLexicalBlockEnd(Builder, S.getSourceRange().getEnd());
774ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
775ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // Emit the fall-through block.
776ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  EmitBlock(LoopExit.getBlock(), true);
777ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith}
778ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
77929e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbarvoid CodeGenFunction::EmitReturnOfRValue(RValue RV, QualType Ty) {
78029e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar  if (RV.isScalar()) {
78129e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar    Builder.CreateStore(RV.getScalarVal(), ReturnValue);
78229e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar  } else if (RV.isAggregate()) {
783649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier    EmitAggregateCopy(ReturnValue, RV.getAggregateAddr(), Ty);
78429e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar  } else {
7859d232c884ea9872d6555df0fd7359699819bc1f1John McCall    EmitStoreOfComplex(RV.getComplexVal(),
7869d232c884ea9872d6555df0fd7359699819bc1f1John McCall                       MakeNaturalAlignAddrLValue(ReturnValue, Ty),
7879d232c884ea9872d6555df0fd7359699819bc1f1John McCall                       /*init*/ true);
78829e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar  }
78982d8ef0be44ddba608c1ce5c8b6b48da83bc1821Anders Carlsson  EmitBranchThroughCleanup(ReturnBlock);
79029e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar}
79129e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar
7925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// EmitReturnStmt - Note that due to GCC extensions, this can have an operand
7935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// if the function returns void, or may be missing one if the function returns
7945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// non-void.  Fun stuff :).
7955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) {
7965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Emit the result value, even if unused, to evalute the side effects.
7975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const Expr *RV = S.getRetValue();
7981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7999f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall  // Treat block literals in a return expression as if they appeared
8009f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall  // in their own scope.  This permits a small, easily-implemented
8019f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall  // exception to our over-conservative rules about not jumping to
8029f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall  // statements following block literals with non-trivial cleanups.
8039f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall  RunCleanupsScope cleanupScope(*this);
8049f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall  if (const ExprWithCleanups *cleanups =
8059f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall        dyn_cast_or_null<ExprWithCleanups>(RV)) {
8069f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall    enterFullExpression(cleanups);
8079f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall    RV = cleanups->getSubExpr();
8089f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall  }
8099f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall
8105ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar  // FIXME: Clean this up by using an LValue for ReturnTemp,
8115ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar  // EmitStoreThroughLValue, and EmitAnyExpr.
8126c82fc65e45c668a36b06536e66563fe937a3096Rafael Espindola  if (S.getNRVOCandidate() && S.getNRVOCandidate()->isNRVOVariable()) {
813d86c477fb5d3fc34864afecbbb5443da9355e8fbDouglas Gregor    // Apply the named return value optimization for this return statement,
814d86c477fb5d3fc34864afecbbb5443da9355e8fbDouglas Gregor    // which means doing nothing: the appropriate result has already been
815d86c477fb5d3fc34864afecbbb5443da9355e8fbDouglas Gregor    // constructed into the NRVO variable.
8166f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
8173d91bbcdab155181556969cad6ec97014405acedDouglas Gregor    // If there is an NRVO flag for this variable, set it to 1 into indicate
8183d91bbcdab155181556969cad6ec97014405acedDouglas Gregor    // that the cleanup code should not destroy the variable.
819d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall    if (llvm::Value *NRVOFlag = NRVOFlags[S.getNRVOCandidate()])
820d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall      Builder.CreateStore(Builder.getTrue(), NRVOFlag);
821d86c477fb5d3fc34864afecbbb5443da9355e8fbDouglas Gregor  } else if (!ReturnValue) {
8225ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar    // Make sure not to return anything, but evaluate the expression
8235ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar    // for side effects.
8245ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar    if (RV)
825144ac61f9005a0da4327d4e62a4c453923b7bc0cEli Friedman      EmitAnyExpr(RV);
8265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } else if (RV == 0) {
8275ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar    // Do nothing (return value is left uninitialized)
828d54b6ac2f4f6f0bd0076cbfa885b57277066f06cEli Friedman  } else if (FnRetTy->isReferenceType()) {
829d54b6ac2f4f6f0bd0076cbfa885b57277066f06cEli Friedman    // If this function returns a reference, take the address of the expression
830d54b6ac2f4f6f0bd0076cbfa885b57277066f06cEli Friedman    // rather than the value.
831d4ec562b3aaf50ea9015f82c96ebfd05a35fc7efRichard Smith    RValue Result = EmitReferenceBindingToExpr(RV);
83233fd1fc1814a5573c972840d49317989e20deaceDouglas Gregor    Builder.CreateStore(Result.getScalarVal(), ReturnValue);
8335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } else {
8349d232c884ea9872d6555df0fd7359699819bc1f1John McCall    switch (getEvaluationKind(RV->getType())) {
8359d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case TEK_Scalar:
8369d232c884ea9872d6555df0fd7359699819bc1f1John McCall      Builder.CreateStore(EmitScalarExpr(RV), ReturnValue);
8379d232c884ea9872d6555df0fd7359699819bc1f1John McCall      break;
8389d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case TEK_Complex:
8399d232c884ea9872d6555df0fd7359699819bc1f1John McCall      EmitComplexExprIntoLValue(RV,
8409d232c884ea9872d6555df0fd7359699819bc1f1John McCall                     MakeNaturalAlignAddrLValue(ReturnValue, RV->getType()),
8419d232c884ea9872d6555df0fd7359699819bc1f1John McCall                                /*isInit*/ true);
8429d232c884ea9872d6555df0fd7359699819bc1f1John McCall      break;
8439d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case TEK_Aggregate: {
8449d232c884ea9872d6555df0fd7359699819bc1f1John McCall      CharUnits Alignment = getContext().getTypeAlignInChars(RV->getType());
8459d232c884ea9872d6555df0fd7359699819bc1f1John McCall      EmitAggExpr(RV, AggValueSlot::forAddr(ReturnValue, Alignment,
8469d232c884ea9872d6555df0fd7359699819bc1f1John McCall                                            Qualifiers(),
8479d232c884ea9872d6555df0fd7359699819bc1f1John McCall                                            AggValueSlot::IsDestructed,
8489d232c884ea9872d6555df0fd7359699819bc1f1John McCall                                            AggValueSlot::DoesNotNeedGCBarriers,
8499d232c884ea9872d6555df0fd7359699819bc1f1John McCall                                            AggValueSlot::IsNotAliased));
8509d232c884ea9872d6555df0fd7359699819bc1f1John McCall      break;
8519d232c884ea9872d6555df0fd7359699819bc1f1John McCall    }
8529d232c884ea9872d6555df0fd7359699819bc1f1John McCall    }
8535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
854144ac61f9005a0da4327d4e62a4c453923b7bc0cEli Friedman
855ddb379ef3aaba9f1b77e9db6704a2d7d3378d69bAdrian Prantl  ++NumReturnExprs;
856fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl  if (RV == 0 || RV->isEvaluatable(getContext()))
857ddb379ef3aaba9f1b77e9db6704a2d7d3378d69bAdrian Prantl    ++NumSimpleReturnExprs;
858fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl
8599f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall  cleanupScope.ForceCleanup();
86082d8ef0be44ddba608c1ce5c8b6b48da83bc1821Anders Carlsson  EmitBranchThroughCleanup(ReturnBlock);
8615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid CodeGenFunction::EmitDeclStmt(const DeclStmt &S) {
8649198126067a447f8aaccf9fff09be294c8bcb81eDevang Patel  // As long as debug info is modeled with instructions, we have to ensure we
8659198126067a447f8aaccf9fff09be294c8bcb81eDevang Patel  // have a place to insert here and write the stop point here.
8662b124ea9d2c27c6d002ecd4623f6321e305d907eEric Christopher  if (HaveInsertPoint())
8679198126067a447f8aaccf9fff09be294c8bcb81eDevang Patel    EmitStopPoint(&S);
8689198126067a447f8aaccf9fff09be294c8bcb81eDevang Patel
869e4ea1f42c97a436df3e0ae8e129e6bc624ee6790Ted Kremenek  for (DeclStmt::const_decl_iterator I = S.decl_begin(), E = S.decl_end();
870e4ea1f42c97a436df3e0ae8e129e6bc624ee6790Ted Kremenek       I != E; ++I)
871e4ea1f42c97a436df3e0ae8e129e6bc624ee6790Ted Kremenek    EmitDecl(**I);
8726fa5f0943a84233b2e1ec9716eae55643225bfd4Chris Lattner}
873da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner
8740912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbarvoid CodeGenFunction::EmitBreakStmt(const BreakStmt &S) {
875da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner  assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!");
876da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner
8770912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // If this code is reachable then emit a stop point (if generating
8780912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // debug info). We have to do this ourselves because we are on the
8790912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // "simple" statement path.
8800912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  if (HaveInsertPoint())
8810912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar    EmitStopPoint(&S);
882ec9771d57f94cc204491b3174e88069d08cdd684Mike Stump
883f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest Block = BreakContinueStack.back().BreakBlock;
88482d8ef0be44ddba608c1ce5c8b6b48da83bc1821Anders Carlsson  EmitBranchThroughCleanup(Block);
885da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner}
886da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner
8870912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbarvoid CodeGenFunction::EmitContinueStmt(const ContinueStmt &S) {
888da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner  assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
889da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner
8900912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // If this code is reachable then emit a stop point (if generating
8910912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // debug info). We have to do this ourselves because we are on the
8920912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // "simple" statement path.
8930912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  if (HaveInsertPoint())
8940912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar    EmitStopPoint(&S);
895ec9771d57f94cc204491b3174e88069d08cdd684Mike Stump
896f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest Block = BreakContinueStack.back().ContinueBlock;
89782d8ef0be44ddba608c1ce5c8b6b48da83bc1821Anders Carlsson  EmitBranchThroughCleanup(Block);
898da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner}
89951b09f2c528c8460b5465c676173324e44176d62Devang Patel
900c049e4f406a7f7179eba98659044a32508e53289Devang Patel/// EmitCaseStmtRange - If case statement range is not too big then
901c049e4f406a7f7179eba98659044a32508e53289Devang Patel/// add multiple cases to switch instruction, one for each value within
902c049e4f406a7f7179eba98659044a32508e53289Devang Patel/// the range. If range is too big then emit "if" condition check.
903c049e4f406a7f7179eba98659044a32508e53289Devang Patelvoid CodeGenFunction::EmitCaseStmtRange(const CaseStmt &S) {
9044efde8d2c10b091bc9588de18c2c71dca2979e49Daniel Dunbar  assert(S.getRHS() && "Expected RHS value in CaseStmt");
905c049e4f406a7f7179eba98659044a32508e53289Devang Patel
906a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  llvm::APSInt LHS = S.getLHS()->EvaluateKnownConstInt(getContext());
907a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  llvm::APSInt RHS = S.getRHS()->EvaluateKnownConstInt(getContext());
9084efde8d2c10b091bc9588de18c2c71dca2979e49Daniel Dunbar
90916f23570999cac1fa13597386938450843003840Daniel Dunbar  // Emit the code for this case. We do this first to make sure it is
91016f23570999cac1fa13597386938450843003840Daniel Dunbar  // properly chained from our predecessor before generating the
91116f23570999cac1fa13597386938450843003840Daniel Dunbar  // switch machinery to enter this block.
912f84dcda7e2ab2f6d5be5a8c52d22ef4c442dd762Daniel Dunbar  EmitBlock(createBasicBlock("sw.bb"));
91316f23570999cac1fa13597386938450843003840Daniel Dunbar  llvm::BasicBlock *CaseDest = Builder.GetInsertBlock();
91416f23570999cac1fa13597386938450843003840Daniel Dunbar  EmitStmt(S.getSubStmt());
91516f23570999cac1fa13597386938450843003840Daniel Dunbar
9164efde8d2c10b091bc9588de18c2c71dca2979e49Daniel Dunbar  // If range is empty, do nothing.
9174efde8d2c10b091bc9588de18c2c71dca2979e49Daniel Dunbar  if (LHS.isSigned() ? RHS.slt(LHS) : RHS.ult(LHS))
9184efde8d2c10b091bc9588de18c2c71dca2979e49Daniel Dunbar    return;
919c049e4f406a7f7179eba98659044a32508e53289Devang Patel
920c049e4f406a7f7179eba98659044a32508e53289Devang Patel  llvm::APInt Range = RHS - LHS;
92116f23570999cac1fa13597386938450843003840Daniel Dunbar  // FIXME: parameters such as this should not be hardcoded.
922c049e4f406a7f7179eba98659044a32508e53289Devang Patel  if (Range.ult(llvm::APInt(Range.getBitWidth(), 64))) {
923c049e4f406a7f7179eba98659044a32508e53289Devang Patel    // Range is small enough to add multiple switch instruction cases.
9244efde8d2c10b091bc9588de18c2c71dca2979e49Daniel Dunbar    for (unsigned i = 0, e = Range.getZExtValue() + 1; i != e; ++i) {
92597d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner      SwitchInsn->addCase(Builder.getInt(LHS), CaseDest);
9262d79d0f3ac0ea77b7bdfc3dd11de8cc3ccd91b8cDevang Patel      LHS++;
9272d79d0f3ac0ea77b7bdfc3dd11de8cc3ccd91b8cDevang Patel    }
928c049e4f406a7f7179eba98659044a32508e53289Devang Patel    return;
9291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
9301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
93116f23570999cac1fa13597386938450843003840Daniel Dunbar  // The range is too big. Emit "if" condition into a new block,
93216f23570999cac1fa13597386938450843003840Daniel Dunbar  // making sure to save and restore the current insertion point.
93316f23570999cac1fa13597386938450843003840Daniel Dunbar  llvm::BasicBlock *RestoreBB = Builder.GetInsertBlock();
93416f23570999cac1fa13597386938450843003840Daniel Dunbar
93516f23570999cac1fa13597386938450843003840Daniel Dunbar  // Push this test onto the chain of range checks (which terminates
93616f23570999cac1fa13597386938450843003840Daniel Dunbar  // in the default basic block). The switch's default will be changed
93716f23570999cac1fa13597386938450843003840Daniel Dunbar  // to the top of this chain after switch emission is complete.
93816f23570999cac1fa13597386938450843003840Daniel Dunbar  llvm::BasicBlock *FalseDest = CaseRangeBlock;
93955e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  CaseRangeBlock = createBasicBlock("sw.caserange");
94016f23570999cac1fa13597386938450843003840Daniel Dunbar
94116f23570999cac1fa13597386938450843003840Daniel Dunbar  CurFn->getBasicBlockList().push_back(CaseRangeBlock);
94216f23570999cac1fa13597386938450843003840Daniel Dunbar  Builder.SetInsertPoint(CaseRangeBlock);
943c049e4f406a7f7179eba98659044a32508e53289Devang Patel
944c049e4f406a7f7179eba98659044a32508e53289Devang Patel  // Emit range check.
9451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::Value *Diff =
946578faa837b552403e2002b97fdfbfde14f2448e5Benjamin Kramer    Builder.CreateSub(SwitchInsn->getCondition(), Builder.getInt(LHS));
9471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::Value *Cond =
94897d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner    Builder.CreateICmpULE(Diff, Builder.getInt(Range), "inbounds");
949c049e4f406a7f7179eba98659044a32508e53289Devang Patel  Builder.CreateCondBr(Cond, CaseDest, FalseDest);
950c049e4f406a7f7179eba98659044a32508e53289Devang Patel
95116f23570999cac1fa13597386938450843003840Daniel Dunbar  // Restore the appropriate insertion point.
952a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar  if (RestoreBB)
953a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar    Builder.SetInsertPoint(RestoreBB);
954a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar  else
955a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar    Builder.ClearInsertionPoint();
956c049e4f406a7f7179eba98659044a32508e53289Devang Patel}
9572d79d0f3ac0ea77b7bdfc3dd11de8cc3ccd91b8cDevang Patel
958c049e4f406a7f7179eba98659044a32508e53289Devang Patelvoid CodeGenFunction::EmitCaseStmt(const CaseStmt &S) {
959d66715d83e64b6bfd0bddebe51874b1b7a64abefFariborz Jahanian  // If there is no enclosing switch instance that we're aware of, then this
960d66715d83e64b6bfd0bddebe51874b1b7a64abefFariborz Jahanian  // case statement and its block can be elided.  This situation only happens
961d66715d83e64b6bfd0bddebe51874b1b7a64abefFariborz Jahanian  // when we've constant-folded the switch, are emitting the constant case,
962d66715d83e64b6bfd0bddebe51874b1b7a64abefFariborz Jahanian  // and part of the constant case includes another case statement.  For
963d66715d83e64b6bfd0bddebe51874b1b7a64abefFariborz Jahanian  // instance: switch (4) { case 4: do { case 5: } while (1); }
964303b4f946470a054cea8f91af54008aeb3c09507Fariborz Jahanian  if (!SwitchInsn) {
965303b4f946470a054cea8f91af54008aeb3c09507Fariborz Jahanian    EmitStmt(S.getSubStmt());
966d66715d83e64b6bfd0bddebe51874b1b7a64abefFariborz Jahanian    return;
967303b4f946470a054cea8f91af54008aeb3c09507Fariborz Jahanian  }
968d66715d83e64b6bfd0bddebe51874b1b7a64abefFariborz Jahanian
969b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner  // Handle case ranges.
970c049e4f406a7f7179eba98659044a32508e53289Devang Patel  if (S.getRHS()) {
971c049e4f406a7f7179eba98659044a32508e53289Devang Patel    EmitCaseStmtRange(S);
972c049e4f406a7f7179eba98659044a32508e53289Devang Patel    return;
973c049e4f406a7f7179eba98659044a32508e53289Devang Patel  }
9741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
97597d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner  llvm::ConstantInt *CaseVal =
976a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith    Builder.getInt(S.getLHS()->EvaluateKnownConstInt(getContext()));
97797d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner
978421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner  // If the body of the case is just a 'break', and if there was no fallthrough,
979421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner  // try to not emit an empty block.
98017083601193528a5d3770b688ffdf700f7df3c45Chad Rosier  if ((CGM.getCodeGenOpts().OptimizationLevel > 0) &&
98117083601193528a5d3770b688ffdf700f7df3c45Chad Rosier      isa<BreakStmt>(S.getSubStmt())) {
982b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner    JumpDest Block = BreakContinueStack.back().BreakBlock;
9836f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
984b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner    // Only do this optimization if there are no cleanups that need emitting.
985b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner    if (isObviouslyBranchWithoutCleanups(Block)) {
98697d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner      SwitchInsn->addCase(CaseVal, Block.getBlock());
987421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner
988421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner      // If there was a fallthrough into this case, make sure to redirect it to
989421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner      // the end of the switch as well.
990421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner      if (Builder.GetInsertBlock()) {
991421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner        Builder.CreateBr(Block.getBlock());
992421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner        Builder.ClearInsertionPoint();
993421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner      }
994b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner      return;
995b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner    }
996b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner  }
9976f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
998f84dcda7e2ab2f6d5be5a8c52d22ef4c442dd762Daniel Dunbar  EmitBlock(createBasicBlock("sw.bb"));
999c049e4f406a7f7179eba98659044a32508e53289Devang Patel  llvm::BasicBlock *CaseDest = Builder.GetInsertBlock();
100097d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner  SwitchInsn->addCase(CaseVal, CaseDest);
10011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10025512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  // Recursively emitting the statement is acceptable, but is not wonderful for
10035512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  // code where we have many case statements nested together, i.e.:
10045512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  //  case 1:
10055512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  //    case 2:
10065512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  //      case 3: etc.
10075512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  // Handling this recursively will create a new block for each case statement
10085512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  // that falls through to the next case which is IR intensive.  It also causes
10095512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  // deep recursion which can run into stack depth limitations.  Handle
10105512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  // sequential non-range case statements specially.
10115512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  const CaseStmt *CurCase = &S;
10125512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  const CaseStmt *NextCase = dyn_cast<CaseStmt>(S.getSubStmt());
10135512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner
101497d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner  // Otherwise, iteratively add consecutive cases to this switch stmt.
10155512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  while (NextCase && NextCase->getRHS() == 0) {
10165512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner    CurCase = NextCase;
101797d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner    llvm::ConstantInt *CaseVal =
1018a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith      Builder.getInt(CurCase->getLHS()->EvaluateKnownConstInt(getContext()));
101997d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner    SwitchInsn->addCase(CaseVal, CaseDest);
10205512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner    NextCase = dyn_cast<CaseStmt>(CurCase->getSubStmt());
10215512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  }
10221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10235512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  // Normal default recursion for non-cases.
10245512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  EmitStmt(CurCase->getSubStmt());
102551b09f2c528c8460b5465c676173324e44176d62Devang Patel}
102651b09f2c528c8460b5465c676173324e44176d62Devang Patel
102751b09f2c528c8460b5465c676173324e44176d62Devang Patelvoid CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S) {
102816f23570999cac1fa13597386938450843003840Daniel Dunbar  llvm::BasicBlock *DefaultBlock = SwitchInsn->getDefaultDest();
10291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(DefaultBlock->empty() &&
103055e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar         "EmitDefaultStmt: Default block already defined?");
103116f23570999cac1fa13597386938450843003840Daniel Dunbar  EmitBlock(DefaultBlock);
103251b09f2c528c8460b5465c676173324e44176d62Devang Patel  EmitStmt(S.getSubStmt());
103351b09f2c528c8460b5465c676173324e44176d62Devang Patel}
103451b09f2c528c8460b5465c676173324e44176d62Devang Patel
1035fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// CollectStatementsForCase - Given the body of a 'switch' statement and a
1036fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// constant value that is being switched on, see if we can dead code eliminate
1037fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// the body of the switch to a simple series of statements to emit.  Basically,
1038fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// on a switch (5) we want to find these statements:
1039fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner///    case 5:
1040fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner///      printf(...);    <--
1041fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner///      ++i;            <--
1042fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner///      break;
1043fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner///
1044fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// and add them to the ResultStmts vector.  If it is unsafe to do this
1045fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// transformation (for example, one of the elided statements contains a label
1046fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// that might be jumped to), return CSFC_Failure.  If we handled it and 'S'
1047fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// should include statements after it (e.g. the printf() line is a substmt of
1048fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// the case) then return CSFC_FallThrough.  If we handled it and found a break
1049fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// statement, then return CSFC_Success.
1050fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner///
1051fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// If Case is non-null, then we are looking for the specified case, checking
1052fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// that nothing we jump over contains labels.  If Case is null, then we found
1053fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// the case and are looking for the break.
1054fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner///
1055fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// If the recursive walk actually finds our Case, then we set FoundCase to
1056fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// true.
1057fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner///
1058fda0f1f5a278548b012401be07e287c1697fc41cChris Lattnerenum CSFC_Result { CSFC_Failure, CSFC_FallThrough, CSFC_Success };
1059fda0f1f5a278548b012401be07e287c1697fc41cChris Lattnerstatic CSFC_Result CollectStatementsForCase(const Stmt *S,
1060fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner                                            const SwitchCase *Case,
1061fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner                                            bool &FoundCase,
10625f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                              SmallVectorImpl<const Stmt*> &ResultStmts) {
10633858938b043bac2f046304ff99a54905acdcc6ddChris Lattner  // If this is a null statement, just succeed.
10643858938b043bac2f046304ff99a54905acdcc6ddChris Lattner  if (S == 0)
10653858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    return Case ? CSFC_Success : CSFC_FallThrough;
10666f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
1067fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // If this is the switchcase (case 4: or default) that we're looking for, then
1068fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // we're in business.  Just add the substatement.
1069fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  if (const SwitchCase *SC = dyn_cast<SwitchCase>(S)) {
1070fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    if (S == Case) {
1071fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      FoundCase = true;
1072fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      return CollectStatementsForCase(SC->getSubStmt(), 0, FoundCase,
1073fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner                                      ResultStmts);
1074fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    }
10756f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
1076fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    // Otherwise, this is some other case or default statement, just ignore it.
1077fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    return CollectStatementsForCase(SC->getSubStmt(), Case, FoundCase,
1078fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner                                    ResultStmts);
1079fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  }
10803858938b043bac2f046304ff99a54905acdcc6ddChris Lattner
10813858938b043bac2f046304ff99a54905acdcc6ddChris Lattner  // If we are in the live part of the code and we found our break statement,
10823858938b043bac2f046304ff99a54905acdcc6ddChris Lattner  // return a success!
10833858938b043bac2f046304ff99a54905acdcc6ddChris Lattner  if (Case == 0 && isa<BreakStmt>(S))
10843858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    return CSFC_Success;
10856f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
10863858938b043bac2f046304ff99a54905acdcc6ddChris Lattner  // If this is a switch statement, then it might contain the SwitchCase, the
10873858938b043bac2f046304ff99a54905acdcc6ddChris Lattner  // break, or neither.
10883858938b043bac2f046304ff99a54905acdcc6ddChris Lattner  if (const CompoundStmt *CS = dyn_cast<CompoundStmt>(S)) {
10893858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    // Handle this as two cases: we might be looking for the SwitchCase (if so
10903858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    // the skipped statements must be skippable) or we might already have it.
10913858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    CompoundStmt::const_body_iterator I = CS->body_begin(), E = CS->body_end();
10923858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    if (Case) {
10933f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner      // Keep track of whether we see a skipped declaration.  The code could be
10943f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner      // using the declaration even if it is skipped, so we can't optimize out
10953f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner      // the decl if the kept statements might refer to it.
10963f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner      bool HadSkippedDecl = false;
10976f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
10983858938b043bac2f046304ff99a54905acdcc6ddChris Lattner      // If we're looking for the case, just see if we can skip each of the
10993858938b043bac2f046304ff99a54905acdcc6ddChris Lattner      // substatements.
11003858938b043bac2f046304ff99a54905acdcc6ddChris Lattner      for (; Case && I != E; ++I) {
11014d509341bd5db06a517daa311379f52bb540bc34Eli Friedman        HadSkippedDecl |= isa<DeclStmt>(*I);
11026f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
11033858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        switch (CollectStatementsForCase(*I, Case, FoundCase, ResultStmts)) {
11043858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        case CSFC_Failure: return CSFC_Failure;
11053858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        case CSFC_Success:
11063858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          // A successful result means that either 1) that the statement doesn't
11073858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          // have the case and is skippable, or 2) does contain the case value
11089467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner          // and also contains the break to exit the switch.  In the later case,
11099467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner          // we just verify the rest of the statements are elidable.
11109467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner          if (FoundCase) {
11113f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner            // If we found the case and skipped declarations, we can't do the
11123f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner            // optimization.
11133f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner            if (HadSkippedDecl)
11143f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner              return CSFC_Failure;
11156f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
11169467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner            for (++I; I != E; ++I)
11179467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner              if (CodeGenFunction::ContainsLabel(*I, true))
11189467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner                return CSFC_Failure;
11199467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner            return CSFC_Success;
11209467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner          }
11213858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          break;
11223858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        case CSFC_FallThrough:
11233858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          // If we have a fallthrough condition, then we must have found the
11243858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          // case started to include statements.  Consider the rest of the
11253858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          // statements in the compound statement as candidates for inclusion.
11263858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          assert(FoundCase && "Didn't find case but returned fallthrough?");
11273858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          // We recursively found Case, so we're not looking for it anymore.
11283858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          Case = 0;
11296f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
11303f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner          // If we found the case and skipped declarations, we can't do the
11313f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner          // optimization.
11323f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner          if (HadSkippedDecl)
11333f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner            return CSFC_Failure;
11343858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          break;
11353858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        }
11363858938b043bac2f046304ff99a54905acdcc6ddChris Lattner      }
11373858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    }
11383858938b043bac2f046304ff99a54905acdcc6ddChris Lattner
11393858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    // If we have statements in our range, then we know that the statements are
11403858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    // live and need to be added to the set of statements we're tracking.
11413858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    for (; I != E; ++I) {
11423858938b043bac2f046304ff99a54905acdcc6ddChris Lattner      switch (CollectStatementsForCase(*I, 0, FoundCase, ResultStmts)) {
11433858938b043bac2f046304ff99a54905acdcc6ddChris Lattner      case CSFC_Failure: return CSFC_Failure;
11443858938b043bac2f046304ff99a54905acdcc6ddChris Lattner      case CSFC_FallThrough:
11453858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        // A fallthrough result means that the statement was simple and just
11463858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        // included in ResultStmt, keep adding them afterwards.
11473858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        break;
11483858938b043bac2f046304ff99a54905acdcc6ddChris Lattner      case CSFC_Success:
11493858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        // A successful result means that we found the break statement and
11503858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        // stopped statement inclusion.  We just ensure that any leftover stmts
11513858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        // are skippable and return success ourselves.
11523858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        for (++I; I != E; ++I)
11533858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          if (CodeGenFunction::ContainsLabel(*I, true))
11543858938b043bac2f046304ff99a54905acdcc6ddChris Lattner            return CSFC_Failure;
11553858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        return CSFC_Success;
11566f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier      }
11573858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    }
11586f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
11593858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    return Case ? CSFC_Success : CSFC_FallThrough;
11603858938b043bac2f046304ff99a54905acdcc6ddChris Lattner  }
11613858938b043bac2f046304ff99a54905acdcc6ddChris Lattner
1162fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // Okay, this is some other statement that we don't handle explicitly, like a
1163fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // for statement or increment etc.  If we are skipping over this statement,
1164fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // just verify it doesn't have labels, which would make it invalid to elide.
1165fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  if (Case) {
11663f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner    if (CodeGenFunction::ContainsLabel(S, true))
1167fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      return CSFC_Failure;
1168fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    return CSFC_Success;
1169fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  }
11706f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
1171fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // Otherwise, we want to include this statement.  Everything is cool with that
1172fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // so long as it doesn't contain a break out of the switch we're in.
1173fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  if (CodeGenFunction::containsBreak(S)) return CSFC_Failure;
11746f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
1175fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // Otherwise, everything is great.  Include the statement and tell the caller
1176fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // that we fall through and include the next statement as well.
1177fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  ResultStmts.push_back(S);
1178fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  return CSFC_FallThrough;
1179fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner}
1180fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner
1181fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// FindCaseStatementsForValue - Find the case statement being jumped to and
1182fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// then invoke CollectStatementsForCase to find the list of statements to emit
1183fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// for a switch on constant.  See the comment above CollectStatementsForCase
1184fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// for more details.
1185fda0f1f5a278548b012401be07e287c1697fc41cChris Lattnerstatic bool FindCaseStatementsForValue(const SwitchStmt &S,
1186e1ecdc168175719d74e112bcacd4aae5e12d4631Richard Trieu                                       const llvm::APSInt &ConstantCondValue,
11875f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                SmallVectorImpl<const Stmt*> &ResultStmts,
1188fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner                                       ASTContext &C) {
1189fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // First step, find the switch case that is being branched to.  We can do this
1190fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // efficiently by scanning the SwitchCase list.
1191fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  const SwitchCase *Case = S.getSwitchCaseList();
1192fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  const DefaultStmt *DefaultCase = 0;
11936f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
1194fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  for (; Case; Case = Case->getNextSwitchCase()) {
1195fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    // It's either a default or case.  Just remember the default statement in
1196fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    // case we're not jumping to any numbered cases.
1197fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    if (const DefaultStmt *DS = dyn_cast<DefaultStmt>(Case)) {
1198fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      DefaultCase = DS;
1199fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      continue;
1200fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    }
12016f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
1202fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    // Check to see if this case is the one we're looking for.
1203fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    const CaseStmt *CS = cast<CaseStmt>(Case);
1204fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    // Don't handle case ranges yet.
1205fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    if (CS->getRHS()) return false;
12066f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
1207fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    // If we found our case, remember it as 'case'.
1208a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith    if (CS->getLHS()->EvaluateKnownConstInt(C) == ConstantCondValue)
1209fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      break;
1210fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  }
12116f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
1212fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // If we didn't find a matching case, we use a default if it exists, or we
1213fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // elide the whole switch body!
1214fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  if (Case == 0) {
1215fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    // It is safe to elide the body of the switch if it doesn't contain labels
1216fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    // etc.  If it is safe, return successfully with an empty ResultStmts list.
1217fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    if (DefaultCase == 0)
1218fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      return !CodeGenFunction::ContainsLabel(&S);
1219fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    Case = DefaultCase;
1220fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  }
1221fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner
1222fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // Ok, we know which case is being jumped to, try to collect all the
1223fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // statements that follow it.  This can fail for a variety of reasons.  Also,
1224fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // check to see that the recursive walk actually found our case statement.
1225fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // Insane cases like this can fail to find it in the recursive walk since we
1226fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // don't handle every stmt kind:
1227fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // switch (4) {
1228fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  //   while (1) {
1229fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  //     case 4: ...
1230fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  bool FoundCase = false;
1231fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  return CollectStatementsForCase(S.getBody(), Case, FoundCase,
1232fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner                                  ResultStmts) != CSFC_Failure &&
1233fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner         FoundCase;
1234fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner}
1235fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner
123651b09f2c528c8460b5465c676173324e44176d62Devang Patelvoid CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) {
1237f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest SwitchExit = getJumpDestInCurrentScope("sw.epilog");
1238f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1239f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  RunCleanupsScope ConditionScope(*this);
1240d3d5301c44138b92bf01286183f5bf310cdd37cfDouglas Gregor
1241d3d5301c44138b92bf01286183f5bf310cdd37cfDouglas Gregor  if (S.getConditionVariable())
1242b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall    EmitAutoVarDecl(*S.getConditionVariable());
1243d3d5301c44138b92bf01286183f5bf310cdd37cfDouglas Gregor
1244985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian  // Handle nested switch statements.
1245985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian  llvm::SwitchInst *SavedSwitchInsn = SwitchInsn;
1246985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian  llvm::BasicBlock *SavedCRBlock = CaseRangeBlock;
1247985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian
1248fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // See if we can constant fold the condition of the switch and therefore only
1249fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // emit the live case statement (if any) of the switch.
1250e1ecdc168175719d74e112bcacd4aae5e12d4631Richard Trieu  llvm::APSInt ConstantCondValue;
1251fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  if (ConstantFoldsToSimpleInteger(S.getCond(), ConstantCondValue)) {
12525f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<const Stmt*, 4> CaseStmts;
1253fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    if (FindCaseStatementsForValue(S, ConstantCondValue, CaseStmts,
1254fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner                                   getContext())) {
1255fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      RunCleanupsScope ExecutedScope(*this);
1256fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner
1257985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian      // At this point, we are no longer "within" a switch instance, so
1258985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian      // we can temporarily enforce this to ensure that any embedded case
1259985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian      // statements are not emitted.
1260985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian      SwitchInsn = 0;
1261985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian
1262fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      // Okay, we can dead code eliminate everything except this case.  Emit the
1263fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      // specified series of statements and we're good.
1264fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      for (unsigned i = 0, e = CaseStmts.size(); i != e; ++i)
1265fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner        EmitStmt(CaseStmts[i]);
1266985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian
1267fc65ec83f6719f06e460b61f83a3ba1e80c18d04Eric Christopher      // Now we want to restore the saved switch instance so that nested
1268fc65ec83f6719f06e460b61f83a3ba1e80c18d04Eric Christopher      // switches continue to function properly
1269985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian      SwitchInsn = SavedSwitchInsn;
1270985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian
1271fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      return;
1272fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    }
1273fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  }
12746f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
127551b09f2c528c8460b5465c676173324e44176d62Devang Patel  llvm::Value *CondV = EmitScalarExpr(S.getCond());
127651b09f2c528c8460b5465c676173324e44176d62Devang Patel
127716f23570999cac1fa13597386938450843003840Daniel Dunbar  // Create basic block to hold stuff that comes after switch
127816f23570999cac1fa13597386938450843003840Daniel Dunbar  // statement. We also need to create a default block now so that
127916f23570999cac1fa13597386938450843003840Daniel Dunbar  // explicit case ranges tests can have a place to jump to on
128016f23570999cac1fa13597386938450843003840Daniel Dunbar  // failure.
128155e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  llvm::BasicBlock *DefaultBlock = createBasicBlock("sw.default");
128216f23570999cac1fa13597386938450843003840Daniel Dunbar  SwitchInsn = Builder.CreateSwitch(CondV, DefaultBlock);
128316f23570999cac1fa13597386938450843003840Daniel Dunbar  CaseRangeBlock = DefaultBlock;
128451b09f2c528c8460b5465c676173324e44176d62Devang Patel
12850912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // Clear the insertion point to indicate we are in unreachable code.
12860912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  Builder.ClearInsertionPoint();
1287d28a80d64616b66c91d28bb4c08ca2d8c594de4eEli Friedman
1288e9b8c0a38549692f1b8f688c05c35442fc620865Devang Patel  // All break statements jump to NextBlock. If BreakContinueStack is non empty
1289e9b8c0a38549692f1b8f688c05c35442fc620865Devang Patel  // then reuse last ContinueBlock.
1290f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest OuterContinue;
1291e4b6d342c29d5cb9d311756100df1603810fa892Anders Carlsson  if (!BreakContinueStack.empty())
1292f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    OuterContinue = BreakContinueStack.back().ContinueBlock;
1293e4b6d342c29d5cb9d311756100df1603810fa892Anders Carlsson
1294f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  BreakContinueStack.push_back(BreakContinue(SwitchExit, OuterContinue));
129551b09f2c528c8460b5465c676173324e44176d62Devang Patel
129651b09f2c528c8460b5465c676173324e44176d62Devang Patel  // Emit switch body.
129751b09f2c528c8460b5465c676173324e44176d62Devang Patel  EmitStmt(S.getBody());
12981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1299e4b6d342c29d5cb9d311756100df1603810fa892Anders Carlsson  BreakContinueStack.pop_back();
130051b09f2c528c8460b5465c676173324e44176d62Devang Patel
130116f23570999cac1fa13597386938450843003840Daniel Dunbar  // Update the default block in case explicit case range tests have
130216f23570999cac1fa13597386938450843003840Daniel Dunbar  // been chained on top.
1303ab14ae2ab16088b6a7f69eac6e152c3e9f9ea01bStepan Dyatkovskiy  SwitchInsn->setDefaultDest(CaseRangeBlock);
13041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1305f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // If a default was never emitted:
130616f23570999cac1fa13597386938450843003840Daniel Dunbar  if (!DefaultBlock->getParent()) {
1307f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    // If we have cleanups, emit the default block so that there's a
1308f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    // place to jump through the cleanups from.
1309f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    if (ConditionScope.requiresCleanups()) {
1310f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      EmitBlock(DefaultBlock);
1311f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1312f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    // Otherwise, just forward the default block to the switch end.
1313f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    } else {
1314ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      DefaultBlock->replaceAllUsesWith(SwitchExit.getBlock());
1315f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      delete DefaultBlock;
1316f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    }
131716f23570999cac1fa13597386938450843003840Daniel Dunbar  }
131816f23570999cac1fa13597386938450843003840Daniel Dunbar
1319ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  ConditionScope.ForceCleanup();
1320ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
132116f23570999cac1fa13597386938450843003840Daniel Dunbar  // Emit continuation.
1322ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(SwitchExit.getBlock(), true);
132351b09f2c528c8460b5465c676173324e44176d62Devang Patel
132451b09f2c528c8460b5465c676173324e44176d62Devang Patel  SwitchInsn = SavedSwitchInsn;
1325c049e4f406a7f7179eba98659044a32508e53289Devang Patel  CaseRangeBlock = SavedCRBlock;
132651b09f2c528c8460b5465c676173324e44176d62Devang Patel}
1327fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson
13282819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattnerstatic std::string
1329444be7366d0a1e172c0290a1ea54c1cb16b5947cDaniel DunbarSimplifyConstraint(const char *Constraint, const TargetInfo &Target,
13305f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                 SmallVectorImpl<TargetInfo::ConstraintInfo> *OutCons=0) {
1331fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  std::string Result;
13321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1333fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  while (*Constraint) {
1334fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    switch (*Constraint) {
1335fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    default:
1336002333f8b2cf1a8614e532f6ce366b21af85142cStuart Hastings      Result += Target.convertConstraint(Constraint);
1337fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson      break;
1338fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    // Ignore these
1339fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    case '*':
1340fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    case '?':
1341fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    case '!':
1342ef44e1110711276ffffe4b22d4ba0cebd49cb330John Thompson    case '=': // Will see this and the following in mult-alt constraints.
1343ef44e1110711276ffffe4b22d4ba0cebd49cb330John Thompson    case '+':
1344ef44e1110711276ffffe4b22d4ba0cebd49cb330John Thompson      break;
1345e6b3dbae38f62164cab3989380cd940242d80120Ulrich Weigand    case '#': // Ignore the rest of the constraint alternative.
1346e6b3dbae38f62164cab3989380cd940242d80120Ulrich Weigand      while (Constraint[1] && Constraint[1] != ',')
13477b309b0107093d0d2b4df2ce1deddd9bd4698cffEric Christopher        Constraint++;
1348e6b3dbae38f62164cab3989380cd940242d80120Ulrich Weigand      break;
13492f474ea9ef7505df5d092287c48c19974222293bJohn Thompson    case ',':
13502f474ea9ef7505df5d092287c48c19974222293bJohn Thompson      Result += "|";
1351fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson      break;
1352fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    case 'g':
1353fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson      Result += "imr";
1354fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson      break;
1355300fb5d0ef7edc87f3fdba17fc8b1184013b35aeAnders Carlsson    case '[': {
13562819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner      assert(OutCons &&
1357300fb5d0ef7edc87f3fdba17fc8b1184013b35aeAnders Carlsson             "Must pass output names to constraints with a symbolic name");
1358300fb5d0ef7edc87f3fdba17fc8b1184013b35aeAnders Carlsson      unsigned Index;
13591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      bool result = Target.resolveSymbolicName(Constraint,
13602819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner                                               &(*OutCons)[0],
13612819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner                                               OutCons->size(), Index);
1362cbf40f913aa2aa5de6e0540fed209405d00a2c69Chris Lattner      assert(result && "Could not resolve symbolic name"); (void)result;
1363300fb5d0ef7edc87f3fdba17fc8b1184013b35aeAnders Carlsson      Result += llvm::utostr(Index);
1364300fb5d0ef7edc87f3fdba17fc8b1184013b35aeAnders Carlsson      break;
1365300fb5d0ef7edc87f3fdba17fc8b1184013b35aeAnders Carlsson    }
1366fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    }
13671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1368fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    Constraint++;
1369fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  }
13701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1371fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  return Result;
1372fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson}
1373fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson
137403117d1b2e32d18652401b12d9049871992bf3adRafael Espindola/// AddVariableConstraints - Look at AsmExpr and if it is a variable declared
137503117d1b2e32d18652401b12d9049871992bf3adRafael Espindola/// as using a particular register add that as a constraint that will be used
137603117d1b2e32d18652401b12d9049871992bf3adRafael Espindola/// in this asm stmt.
13770ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindolastatic std::string
137803117d1b2e32d18652401b12d9049871992bf3adRafael EspindolaAddVariableConstraints(const std::string &Constraint, const Expr &AsmExpr,
137903117d1b2e32d18652401b12d9049871992bf3adRafael Espindola                       const TargetInfo &Target, CodeGenModule &CGM,
1380a23b91d542e336be5051ac54f394e860fb756911Chad Rosier                       const AsmStmt &Stmt) {
13810ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola  const DeclRefExpr *AsmDeclRef = dyn_cast<DeclRefExpr>(&AsmExpr);
13820ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola  if (!AsmDeclRef)
13830ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola    return Constraint;
13840ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola  const ValueDecl &Value = *AsmDeclRef->getDecl();
13850ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola  const VarDecl *Variable = dyn_cast<VarDecl>(&Value);
13860ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola  if (!Variable)
13870ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola    return Constraint;
1388a43ef3e0511dc48d98d61598163c9deddc09cb9cEli Friedman  if (Variable->getStorageClass() != SC_Register)
1389a43ef3e0511dc48d98d61598163c9deddc09cb9cEli Friedman    return Constraint;
13900ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola  AsmLabelAttr *Attr = Variable->getAttr<AsmLabelAttr>();
13910ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola  if (!Attr)
13920ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola    return Constraint;
13935f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef Register = Attr->getLabel();
1394baf86955a9a390f2643a1ea9806832eb4a92f716Rafael Espindola  assert(Target.isValidGCCRegisterName(Register));
1395e3e07a5b3490bc2977859d56bac211afac2236fbEric Christopher  // We're using validateOutputConstraint here because we only care if
1396e3e07a5b3490bc2977859d56bac211afac2236fbEric Christopher  // this is a register constraint.
1397e3e07a5b3490bc2977859d56bac211afac2236fbEric Christopher  TargetInfo::ConstraintInfo Info(Constraint, "");
1398e3e07a5b3490bc2977859d56bac211afac2236fbEric Christopher  if (Target.validateOutputConstraint(Info) &&
1399e3e07a5b3490bc2977859d56bac211afac2236fbEric Christopher      !Info.allowsRegister()) {
14000ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola    CGM.ErrorUnsupported(&Stmt, "__asm__");
14010ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola    return Constraint;
14020ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola  }
140343fec879a527c74ff01d8aa2bf94a12432249fc7Eric Christopher  // Canonicalize the register here before returning it.
140443fec879a527c74ff01d8aa2bf94a12432249fc7Eric Christopher  Register = Target.getNormalizedGCCRegisterName(Register);
14050ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola  return "{" + Register.str() + "}";
14060ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola}
14070ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola
14086d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedmanllvm::Value*
140942b60551eff3a424e191b293bfd606559dc96bffChad RosierCodeGenFunction::EmitAsmInputLValue(const TargetInfo::ConstraintInfo &Info,
14106d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman                                    LValue InputValue, QualType InputType,
14116d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman                                    std::string &ConstraintStr) {
1412634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson  llvm::Value *Arg;
14131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Info.allowsRegister() || !Info.allowsMemory()) {
14149d232c884ea9872d6555df0fd7359699819bc1f1John McCall    if (CodeGenFunction::hasScalarEvaluationKind(InputType)) {
1415545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall      Arg = EmitLoadOfLValue(InputValue).getScalarVal();
1416634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson    } else {
14172acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner      llvm::Type *Ty = ConvertType(InputType);
141825a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow      uint64_t Size = CGM.getDataLayout().getTypeSizeInBits(Ty);
1419ebaae2a3103b443d50444d335ab5ab0ff7680da2Anders Carlsson      if (Size <= 64 && llvm::isPowerOf2_64(Size)) {
1420d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall        Ty = llvm::IntegerType::get(getLLVMContext(), Size);
1421ebaae2a3103b443d50444d335ab5ab0ff7680da2Anders Carlsson        Ty = llvm::PointerType::getUnqual(Ty);
14221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14236d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman        Arg = Builder.CreateLoad(Builder.CreateBitCast(InputValue.getAddress(),
14246d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman                                                       Ty));
1425ebaae2a3103b443d50444d335ab5ab0ff7680da2Anders Carlsson      } else {
14266d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman        Arg = InputValue.getAddress();
1427ebaae2a3103b443d50444d335ab5ab0ff7680da2Anders Carlsson        ConstraintStr += '*';
1428ebaae2a3103b443d50444d335ab5ab0ff7680da2Anders Carlsson      }
1429634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson    }
1430634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson  } else {
14316d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman    Arg = InputValue.getAddress();
1432634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson    ConstraintStr += '*';
1433634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson  }
14341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1435634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson  return Arg;
1436634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson}
1437634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson
143842b60551eff3a424e191b293bfd606559dc96bffChad Rosierllvm::Value* CodeGenFunction::EmitAsmInput(
14396d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman                                         const TargetInfo::ConstraintInfo &Info,
14406d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman                                           const Expr *InputExpr,
14416d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman                                           std::string &ConstraintStr) {
14426d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman  if (Info.allowsRegister() || !Info.allowsMemory())
14439d232c884ea9872d6555df0fd7359699819bc1f1John McCall    if (CodeGenFunction::hasScalarEvaluationKind(InputExpr->getType()))
14446d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman      return EmitScalarExpr(InputExpr);
14456d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman
14466d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman  InputExpr = InputExpr->IgnoreParenNoopCasts(getContext());
14476d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman  LValue Dest = EmitLValue(InputExpr);
144842b60551eff3a424e191b293bfd606559dc96bffChad Rosier  return EmitAsmInputLValue(Info, Dest, InputExpr->getType(), ConstraintStr);
14496d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman}
14506d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman
145147fc7e93b6af9e6a967231133a2f4bc626eeed26Chris Lattner/// getAsmSrcLocInfo - Return the !srcloc metadata node to attach to an inline
14525d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner/// asm call instruction.  The !srcloc MDNode contains a list of constant
14535d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner/// integers which are the source locations of the start of each line in the
14545d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner/// asm.
145547fc7e93b6af9e6a967231133a2f4bc626eeed26Chris Lattnerstatic llvm::MDNode *getAsmSrcLocInfo(const StringLiteral *Str,
145647fc7e93b6af9e6a967231133a2f4bc626eeed26Chris Lattner                                      CodeGenFunction &CGF) {
14575f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Value *, 8> Locs;
14585d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner  // Add the location of the first line to the MDNode.
14595d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner  Locs.push_back(llvm::ConstantInt::get(CGF.Int32Ty,
14605d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner                                        Str->getLocStart().getRawEncoding()));
14615f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef StrVal = Str->getString();
14625d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner  if (!StrVal.empty()) {
14635d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner    const SourceManager &SM = CGF.CGM.getContext().getSourceManager();
14644e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    const LangOptions &LangOpts = CGF.CGM.getLangOpts();
14656f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
14665d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner    // Add the location of the start of each subsequent line of the asm to the
14675d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner    // MDNode.
14685d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner    for (unsigned i = 0, e = StrVal.size()-1; i != e; ++i) {
14695d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner      if (StrVal[i] != '\n') continue;
14705d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner      SourceLocation LineLoc = Str->getLocationOfByte(i+1, SM, LangOpts,
147164aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall                                                      CGF.getTarget());
14725d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner      Locs.push_back(llvm::ConstantInt::get(CGF.Int32Ty,
14735d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner                                            LineLoc.getRawEncoding()));
14745d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner    }
14756f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier  }
14766f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
14776f141659cab11109d9931d92d0988f8850778de3Jay Foad  return llvm::MDNode::get(CGF.getLLVMContext(), Locs);
147847fc7e93b6af9e6a967231133a2f4bc626eeed26Chris Lattner}
147947fc7e93b6af9e6a967231133a2f4bc626eeed26Chris Lattner
1480a23b91d542e336be5051ac54f394e860fb756911Chad Rosiervoid CodeGenFunction::EmitAsmStmt(const AsmStmt &S) {
1481be3ace834ee7438915e73d2115365d57d03ceb99Chad Rosier  // Assemble the final asm string.
1482da083b2ce8db27ce6e508cb77cb12c0fc8b7cad9Chad Rosier  std::string AsmString = S.generateAsmString(getContext());
14831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1484481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner  // Get all the output and input constraints together.
14855f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos;
14865f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos;
1487481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner
14881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) {
1489aeeacf725c9e0ddd64ea9764bd008e5b6873ce51John McCall    StringRef Name;
1490aeeacf725c9e0ddd64ea9764bd008e5b6873ce51John McCall    if (const GCCAsmStmt *GAS = dyn_cast<GCCAsmStmt>(&S))
1491aeeacf725c9e0ddd64ea9764bd008e5b6873ce51John McCall      Name = GAS->getOutputName(i);
1492aeeacf725c9e0ddd64ea9764bd008e5b6873ce51John McCall    TargetInfo::ConstraintInfo Info(S.getOutputConstraint(i), Name);
149364aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall    bool IsValid = getTarget().validateOutputConstraint(Info); (void)IsValid;
1494b992259f7790d3fb9fc5c2eb7182d7af9d64f9acChris Lattner    assert(IsValid && "Failed to parse output constraint");
1495481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner    OutputConstraintInfos.push_back(Info);
14961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
14971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1498481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner  for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) {
1499aeeacf725c9e0ddd64ea9764bd008e5b6873ce51John McCall    StringRef Name;
1500aeeacf725c9e0ddd64ea9764bd008e5b6873ce51John McCall    if (const GCCAsmStmt *GAS = dyn_cast<GCCAsmStmt>(&S))
1501aeeacf725c9e0ddd64ea9764bd008e5b6873ce51John McCall      Name = GAS->getInputName(i);
1502aeeacf725c9e0ddd64ea9764bd008e5b6873ce51John McCall    TargetInfo::ConstraintInfo Info(S.getInputConstraint(i), Name);
150364aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall    bool IsValid =
150464aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall      getTarget().validateInputConstraint(OutputConstraintInfos.data(),
150564aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall                                          S.getNumOutputs(), Info);
1506b992259f7790d3fb9fc5c2eb7182d7af9d64f9acChris Lattner    assert(IsValid && "Failed to parse input constraint"); (void)IsValid;
1507481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner    InputConstraintInfos.push_back(Info);
1508481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner  }
15091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1510fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  std::string Constraints;
15111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1512ede9d900809c4fd0298d52f5a63088ecb8302275Chris Lattner  std::vector<LValue> ResultRegDests;
1513ede9d900809c4fd0298d52f5a63088ecb8302275Chris Lattner  std::vector<QualType> ResultRegQualTys;
1514ef6de3da8572607f786303c07150daa6e140ab19Jay Foad  std::vector<llvm::Type *> ResultRegTypes;
1515ef6de3da8572607f786303c07150daa6e140ab19Jay Foad  std::vector<llvm::Type *> ResultTruncRegTypes;
1516d1c0c940ebc8e55d5c4672f61abedf87d6ea36f4Chad Rosier  std::vector<llvm::Type *> ArgTypes;
1517fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  std::vector<llvm::Value*> Args;
1518f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson
1519f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson  // Keep track of inout constraints.
1520f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson  std::string InOutConstraints;
1521f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson  std::vector<llvm::Value*> InOutArgs;
15229cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  std::vector<llvm::Type*> InOutArgTypes;
152303eb543cf7ebee463b33b5802b83ac92c21770cfAnders Carlsson
15241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) {
1525481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner    TargetInfo::ConstraintInfo &Info = OutputConstraintInfos[i];
152603eb543cf7ebee463b33b5802b83ac92c21770cfAnders Carlsson
1527fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    // Simplify the output constraint.
1528481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner    std::string OutputConstraint(S.getOutputConstraint(i));
152964aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall    OutputConstraint = SimplifyConstraint(OutputConstraint.c_str() + 1,
153064aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall                                          getTarget());
15311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1532810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    const Expr *OutExpr = S.getOutputExpr(i);
1533810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    OutExpr = OutExpr->IgnoreParenNoopCasts(getContext());
15341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1535a18f539628a6506bae6af52eacd541cebefff762Eric Christopher    OutputConstraint = AddVariableConstraints(OutputConstraint, *OutExpr,
153664aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall                                              getTarget(), CGM, S);
15370ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola
1538810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    LValue Dest = EmitLValue(OutExpr);
1539ede9d900809c4fd0298d52f5a63088ecb8302275Chris Lattner    if (!Constraints.empty())
1540bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson      Constraints += ',';
1541bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson
1542a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    // If this is a register output, then make the inline asm return it
1543a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    // by-value.  If this is a memory result, return the value by-reference.
15449d232c884ea9872d6555df0fd7359699819bc1f1John McCall    if (!Info.allowsMemory() && hasScalarEvaluationKind(OutExpr->getType())) {
1545a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      Constraints += "=" + OutputConstraint;
1546ede9d900809c4fd0298d52f5a63088ecb8302275Chris Lattner      ResultRegQualTys.push_back(OutExpr->getType());
1547ede9d900809c4fd0298d52f5a63088ecb8302275Chris Lattner      ResultRegDests.push_back(Dest);
1548a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      ResultRegTypes.push_back(ConvertTypeForMem(OutExpr->getType()));
1549a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      ResultTruncRegTypes.push_back(ResultRegTypes.back());
15501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1551a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      // If this output is tied to an input, and if the input is larger, then
1552a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      // we need to set the actual result type of the inline asm node to be the
1553a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      // same as the input type.
1554a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      if (Info.hasMatchingInput()) {
1555ebfc9857de58a326c84629915d9ffe3f36d8cc26Chris Lattner        unsigned InputNo;
1556ebfc9857de58a326c84629915d9ffe3f36d8cc26Chris Lattner        for (InputNo = 0; InputNo != S.getNumInputs(); ++InputNo) {
1557ebfc9857de58a326c84629915d9ffe3f36d8cc26Chris Lattner          TargetInfo::ConstraintInfo &Input = InputConstraintInfos[InputNo];
1558aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner          if (Input.hasTiedOperand() && Input.getTiedOperand() == i)
1559a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner            break;
1560ebfc9857de58a326c84629915d9ffe3f36d8cc26Chris Lattner        }
1561ebfc9857de58a326c84629915d9ffe3f36d8cc26Chris Lattner        assert(InputNo != S.getNumInputs() && "Didn't find matching input!");
15621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1563a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner        QualType InputTy = S.getInputExpr(InputNo)->getType();
1564aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner        QualType OutputType = OutExpr->getType();
15651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1566a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner        uint64_t InputSize = getContext().getTypeSize(InputTy);
1567aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner        if (getContext().getTypeSize(OutputType) < InputSize) {
1568aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner          // Form the asm to return the value as a larger integer or fp type.
1569aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner          ResultRegTypes.back() = ConvertType(InputTy);
1570a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner        }
1571a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      }
15721bea653e0d0f0182ed6e0deb5c18ad1123bb3bbdTim Northover      if (llvm::Type* AdjTy =
15734b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne            getTargetHooks().adjustInlineAsmType(*this, OutputConstraint,
15744b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne                                                 ResultRegTypes.back()))
1575f6e2c2039f76fa58799f6d155892d54fc95755e1Dale Johannesen        ResultRegTypes.back() = AdjTy;
15761bea653e0d0f0182ed6e0deb5c18ad1123bb3bbdTim Northover      else {
15771bea653e0d0f0182ed6e0deb5c18ad1123bb3bbdTim Northover        CGM.getDiags().Report(S.getAsmLoc(),
15781bea653e0d0f0182ed6e0deb5c18ad1123bb3bbdTim Northover                              diag::err_asm_invalid_type_in_input)
15791bea653e0d0f0182ed6e0deb5c18ad1123bb3bbdTim Northover            << OutExpr->getType() << OutputConstraint;
15801bea653e0d0f0182ed6e0deb5c18ad1123bb3bbdTim Northover      }
1581fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    } else {
1582fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson      ArgTypes.push_back(Dest.getAddress()->getType());
1583cad3ab611ebd3bee3ce6395d649640047f904cdeAnders Carlsson      Args.push_back(Dest.getAddress());
1584f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson      Constraints += "=*";
1585fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson      Constraints += OutputConstraint;
1586f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson    }
15871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
158844def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner    if (Info.isReadWrite()) {
1589f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson      InOutConstraints += ',';
1590634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson
1591fca9361839ecc53224c764517e62fc0e15166004Anders Carlsson      const Expr *InputExpr = S.getOutputExpr(i);
159242b60551eff3a424e191b293bfd606559dc96bffChad Rosier      llvm::Value *Arg = EmitAsmInputLValue(Info, Dest, InputExpr->getType(),
15936d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman                                            InOutConstraints);
15941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1595acb5310542b82e7acf5f5a91fa619e4b7efeff5cBill Wendling      if (llvm::Type* AdjTy =
15961bea653e0d0f0182ed6e0deb5c18ad1123bb3bbdTim Northover          getTargetHooks().adjustInlineAsmType(*this, OutputConstraint,
15971bea653e0d0f0182ed6e0deb5c18ad1123bb3bbdTim Northover                                               Arg->getType()))
1598acb5310542b82e7acf5f5a91fa619e4b7efeff5cBill Wendling        Arg = Builder.CreateBitCast(Arg, AdjTy);
1599acb5310542b82e7acf5f5a91fa619e4b7efeff5cBill Wendling
160044def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner      if (Info.allowsRegister())
16019f2505b934745b18d580ade4dac7b8b16952a30cAnders Carlsson        InOutConstraints += llvm::utostr(i);
16029f2505b934745b18d580ade4dac7b8b16952a30cAnders Carlsson      else
16039f2505b934745b18d580ade4dac7b8b16952a30cAnders Carlsson        InOutConstraints += OutputConstraint;
16042763b3af0a527c3a63cb058b90c22db0b7bcf558Anders Carlsson
1605fca9361839ecc53224c764517e62fc0e15166004Anders Carlsson      InOutArgTypes.push_back(Arg->getType());
1606fca9361839ecc53224c764517e62fc0e15166004Anders Carlsson      InOutArgs.push_back(Arg);
1607f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson    }
1608fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  }
16091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1610fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  unsigned NumConstraints = S.getNumOutputs() + S.getNumInputs();
16111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1612fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) {
1613fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    const Expr *InputExpr = S.getInputExpr(i);
1614fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson
1615481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner    TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i];
1616481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner
1617ede9d900809c4fd0298d52f5a63088ecb8302275Chris Lattner    if (!Constraints.empty())
1618fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson      Constraints += ',';
16191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1620fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    // Simplify the input constraint.
1621481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner    std::string InputConstraint(S.getInputConstraint(i));
162264aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall    InputConstraint = SimplifyConstraint(InputConstraint.c_str(), getTarget(),
16232819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner                                         &OutputConstraintInfos);
1624fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson
16250ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola    InputConstraint =
162603117d1b2e32d18652401b12d9049871992bf3adRafael Espindola      AddVariableConstraints(InputConstraint,
16270ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola                            *InputExpr->IgnoreParenNoopCasts(getContext()),
162864aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall                            getTarget(), CGM, S);
16290ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola
163042b60551eff3a424e191b293bfd606559dc96bffChad Rosier    llvm::Value *Arg = EmitAsmInput(Info, InputExpr, Constraints);
16311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16324df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner    // If this input argument is tied to a larger output result, extend the
16334df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner    // input to be the same size as the output.  The LLVM backend wants to see
16344df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner    // the input and output of a matching constraint be the same size.  Note
16354df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner    // that GCC does not define what the top bits are here.  We use zext because
16364df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner    // that is usually cheaper, but LLVM IR should really get an anyext someday.
16374df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner    if (Info.hasTiedOperand()) {
16384df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner      unsigned Output = Info.getTiedOperand();
1639aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      QualType OutputType = S.getOutputExpr(Output)->getType();
16404df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner      QualType InputTy = InputExpr->getType();
16411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1642aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      if (getContext().getTypeSize(OutputType) >
16434df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner          getContext().getTypeSize(InputTy)) {
16444df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner        // Use ptrtoint as appropriate so that we can do our extension.
16454df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner        if (isa<llvm::PointerType>(Arg->getType()))
164677b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner          Arg = Builder.CreatePtrToInt(Arg, IntPtrTy);
16472acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner        llvm::Type *OutputTy = ConvertType(OutputType);
1648aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner        if (isa<llvm::IntegerType>(OutputTy))
1649aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner          Arg = Builder.CreateZExt(Arg, OutputTy);
165093f1322684e928a559286ba1c7cb83af077aa658Peter Collingbourne        else if (isa<llvm::PointerType>(OutputTy))
165193f1322684e928a559286ba1c7cb83af077aa658Peter Collingbourne          Arg = Builder.CreateZExt(Arg, IntPtrTy);
165293f1322684e928a559286ba1c7cb83af077aa658Peter Collingbourne        else {
165393f1322684e928a559286ba1c7cb83af077aa658Peter Collingbourne          assert(OutputTy->isFloatingPointTy() && "Unexpected output type");
1654aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner          Arg = Builder.CreateFPExt(Arg, OutputTy);
165593f1322684e928a559286ba1c7cb83af077aa658Peter Collingbourne        }
16564df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner      }
16574df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner    }
1658acb5310542b82e7acf5f5a91fa619e4b7efeff5cBill Wendling    if (llvm::Type* AdjTy =
16594b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne              getTargetHooks().adjustInlineAsmType(*this, InputConstraint,
16604b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne                                                   Arg->getType()))
1661f6e2c2039f76fa58799f6d155892d54fc95755e1Dale Johannesen      Arg = Builder.CreateBitCast(Arg, AdjTy);
16621bea653e0d0f0182ed6e0deb5c18ad1123bb3bbdTim Northover    else
16631bea653e0d0f0182ed6e0deb5c18ad1123bb3bbdTim Northover      CGM.getDiags().Report(S.getAsmLoc(), diag::err_asm_invalid_type_in_input)
16641bea653e0d0f0182ed6e0deb5c18ad1123bb3bbdTim Northover          << InputExpr->getType() << InputConstraint;
16651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1666fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    ArgTypes.push_back(Arg->getType());
1667fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    Args.push_back(Arg);
1668fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    Constraints += InputConstraint;
1669fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  }
16701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1671f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson  // Append the "input" part of inout constraints last.
1672f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson  for (unsigned i = 0, e = InOutArgs.size(); i != e; i++) {
1673f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson    ArgTypes.push_back(InOutArgTypes[i]);
1674f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson    Args.push_back(InOutArgs[i]);
1675f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson  }
1676f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson  Constraints += InOutConstraints;
16771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1678fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  // Clobbers
1679fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  for (unsigned i = 0, e = S.getNumClobbers(); i != e; i++) {
168033f0558f75f70061707d1388e305b8f92f4e55deChad Rosier    StringRef Clobber = S.getClobber(i);
1681fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson
1682de31fd7eeebdc64fb043463e7f515dab8eccac8dEric Christopher    if (Clobber != "memory" && Clobber != "cc")
168364aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall    Clobber = getTarget().getNormalizedGCCRegisterName(Clobber);
16841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1685ea041758d49215167e473a515b8d46e77b170ccfAnders Carlsson    if (i != 0 || NumConstraints != 0)
1686fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson      Constraints += ',';
16871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1688ea041758d49215167e473a515b8d46e77b170ccfAnders Carlsson    Constraints += "~{";
1689fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    Constraints += Clobber;
1690ea041758d49215167e473a515b8d46e77b170ccfAnders Carlsson    Constraints += '}';
1691fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  }
16921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1693fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  // Add machine specific clobbers
169464aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall  std::string MachineClobbers = getTarget().getClobbers();
1695ccf614c479ac93326a01e7b373b30759eed7807fEli Friedman  if (!MachineClobbers.empty()) {
1696fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    if (!Constraints.empty())
1697fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson      Constraints += ',';
1698ccf614c479ac93326a01e7b373b30759eed7807fEli Friedman    Constraints += MachineClobbers;
1699fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  }
1700bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson
17012acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *ResultType;
1702a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner  if (ResultRegTypes.empty())
17038b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    ResultType = VoidTy;
1704a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner  else if (ResultRegTypes.size() == 1)
1705a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    ResultType = ResultRegTypes[0];
1706bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson  else
1707d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall    ResultType = llvm::StructType::get(getLLVMContext(), ResultRegTypes);
17081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17092acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FTy =
1710fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    llvm::FunctionType::get(ResultType, ArgTypes, false);
17111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17122ab7d43e450333d52fdf087bf2558a74dbe3c9fdChad Rosier  bool HasSideEffect = S.isVolatile() || S.getNumOutputs() == 0;
1713fcf75a3d8c072eabbf25c9b33ccbb1b8bd042cfcChad Rosier  llvm::InlineAsm::AsmDialect AsmDialect = isa<MSAsmStmt>(&S) ?
1714fcf75a3d8c072eabbf25c9b33ccbb1b8bd042cfcChad Rosier    llvm::InlineAsm::AD_Intel : llvm::InlineAsm::AD_ATT;
17151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::InlineAsm *IA =
1716790cbd84d07f0dc9253ade9f67e6ed8c43f08a59Chad Rosier    llvm::InlineAsm::get(FTy, AsmString, Constraints, HasSideEffect,
1717fcf75a3d8c072eabbf25c9b33ccbb1b8bd042cfcChad Rosier                         /* IsAlignStack */ false, AsmDialect);
17184c7d9f1507d0f102bd4133bba63348636facd469Jay Foad  llvm::CallInst *Result = Builder.CreateCall(IA, Args);
1719785b778203a474c6e4b9e17ae91cd2a358868877Bill Wendling  Result->addAttribute(llvm::AttributeSet::FunctionIndex,
172015e05e932ccf9ba2621394834b62684b42d6fd40Peter Collingbourne                       llvm::Attribute::NoUnwind);
17211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1722fc1a9c312d249a2c7458f763f848ba42685c23f8Chris Lattner  // Slap the source location of the inline asm into a !srcloc metadata on the
1723a23b91d542e336be5051ac54f394e860fb756911Chad Rosier  // call.  FIXME: Handle metadata for MS-style inline asms.
1724a23b91d542e336be5051ac54f394e860fb756911Chad Rosier  if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(&S))
1725a23b91d542e336be5051ac54f394e860fb756911Chad Rosier    Result->setMetadata("srcloc", getAsmSrcLocInfo(gccAsmStmt->getAsmString(),
1726a23b91d542e336be5051ac54f394e860fb756911Chad Rosier                                                   *this));
17271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1728a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner  // Extract all of the register value results from the asm.
1729a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner  std::vector<llvm::Value*> RegResults;
1730a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner  if (ResultRegTypes.size() == 1) {
1731a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    RegResults.push_back(Result);
1732bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson  } else {
1733a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    for (unsigned i = 0, e = ResultRegTypes.size(); i != e; ++i) {
1734bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson      llvm::Value *Tmp = Builder.CreateExtractValue(Result, i, "asmresult");
1735a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      RegResults.push_back(Tmp);
1736a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    }
1737a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner  }
17381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1739a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner  for (unsigned i = 0, e = RegResults.size(); i != e; ++i) {
1740a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    llvm::Value *Tmp = RegResults[i];
17411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1742a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    // If the result type of the LLVM IR asm doesn't match the result type of
1743a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    // the expression, do the conversion.
1744a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    if (ResultRegTypes[i] != ResultTruncRegTypes[i]) {
17452acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner      llvm::Type *TruncTy = ResultTruncRegTypes[i];
17466f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
1747aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      // Truncate the integer result to the right size, note that TruncTy can be
1748aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      // a pointer.
1749aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      if (TruncTy->isFloatingPointTy())
1750aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner        Tmp = Builder.CreateFPTrunc(Tmp, TruncTy);
17512dca88fc6d3e606a8131712be1800e3a4b90ca3eDan Gohman      else if (TruncTy->isPointerTy() && Tmp->getType()->isIntegerTy()) {
175225a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow        uint64_t ResSize = CGM.getDataLayout().getTypeSizeInBits(TruncTy);
1753d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall        Tmp = Builder.CreateTrunc(Tmp,
1754d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall                   llvm::IntegerType::get(getLLVMContext(), (unsigned)ResSize));
1755a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner        Tmp = Builder.CreateIntToPtr(Tmp, TruncTy);
17562dca88fc6d3e606a8131712be1800e3a4b90ca3eDan Gohman      } else if (Tmp->getType()->isPointerTy() && TruncTy->isIntegerTy()) {
175725a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow        uint64_t TmpSize =CGM.getDataLayout().getTypeSizeInBits(Tmp->getType());
1758d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall        Tmp = Builder.CreatePtrToInt(Tmp,
1759d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall                   llvm::IntegerType::get(getLLVMContext(), (unsigned)TmpSize));
17602dca88fc6d3e606a8131712be1800e3a4b90ca3eDan Gohman        Tmp = Builder.CreateTrunc(Tmp, TruncTy);
17612dca88fc6d3e606a8131712be1800e3a4b90ca3eDan Gohman      } else if (TruncTy->isIntegerTy()) {
17622dca88fc6d3e606a8131712be1800e3a4b90ca3eDan Gohman        Tmp = Builder.CreateTrunc(Tmp, TruncTy);
1763f6e2c2039f76fa58799f6d155892d54fc95755e1Dale Johannesen      } else if (TruncTy->isVectorTy()) {
1764f6e2c2039f76fa58799f6d155892d54fc95755e1Dale Johannesen        Tmp = Builder.CreateBitCast(Tmp, TruncTy);
1765a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      }
1766bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson    }
17671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1768545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall    EmitStoreThroughLValue(RValue::get(Tmp), ResultRegDests[i]);
1769bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson  }
1770fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson}
1771051303ce09291dfbed537fa33b0d8a4d92c82b75Tareq A. Siraj
1772524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuirstatic LValue InitCapturedStruct(CodeGenFunction &CGF, const CapturedStmt &S) {
1773524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  const RecordDecl *RD = S.getCapturedRecordDecl();
1774524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  QualType RecordTy = CGF.getContext().getRecordType(RD);
1775524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
1776524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  // Initialize the captured struct.
1777524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  LValue SlotLV = CGF.MakeNaturalAlignAddrLValue(
1778524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir                    CGF.CreateMemTemp(RecordTy, "agg.captured"), RecordTy);
1779524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
1780524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  RecordDecl::field_iterator CurField = RD->field_begin();
1781524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  for (CapturedStmt::capture_init_iterator I = S.capture_init_begin(),
1782524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir                                           E = S.capture_init_end();
1783524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir       I != E; ++I, ++CurField) {
1784524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    LValue LV = CGF.EmitLValueForFieldInitialization(SlotLV, *CurField);
1785524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    CGF.EmitInitializerForField(*CurField, LV, *I, ArrayRef<VarDecl *>());
1786524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  }
1787524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
1788524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  return SlotLV;
1789524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir}
1790524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
1791524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir/// Generate an outlined function for the body of a CapturedStmt, store any
1792524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir/// captured variables into the captured struct, and call the outlined function.
1793524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuirllvm::Function *
1794524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben LangmuirCodeGenFunction::EmitCapturedStmt(const CapturedStmt &S, CapturedRegionKind K) {
1795524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  const CapturedDecl *CD = S.getCapturedDecl();
1796524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  const RecordDecl *RD = S.getCapturedRecordDecl();
1797524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  assert(CD->hasBody() && "missing CapturedDecl body");
1798524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
1799524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  LValue CapStruct = InitCapturedStruct(*this, S);
1800524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
1801524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  // Emit the CapturedDecl
1802524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  CodeGenFunction CGF(CGM, true);
1803524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  CGF.CapturedStmtInfo = new CGCapturedStmtInfo(S, K);
1804524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  llvm::Function *F = CGF.GenerateCapturedStmtFunction(CD, RD);
1805524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  delete CGF.CapturedStmtInfo;
1806524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
1807524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  // Emit call to the helper function.
1808524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  EmitCallOrInvoke(F, CapStruct.getAddress());
1809524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
1810524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  return F;
1811524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir}
1812524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
1813524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir/// Creates the outlined function for a CapturedStmt.
1814524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuirllvm::Function *
1815524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben LangmuirCodeGenFunction::GenerateCapturedStmtFunction(const CapturedDecl *CD,
1816524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir                                              const RecordDecl *RD) {
1817524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  assert(CapturedStmtInfo &&
1818524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    "CapturedStmtInfo should be set when generating the captured function");
1819524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
1820524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  // Check if we should generate debug info for this function.
1821524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  maybeInitializeDebugInfo();
1822524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
1823524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  // Build the argument list.
1824524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  ASTContext &Ctx = CGM.getContext();
1825524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  FunctionArgList Args;
1826524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  Args.append(CD->param_begin(), CD->param_end());
1827524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
1828524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  // Create the function declaration.
1829524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  FunctionType::ExtInfo ExtInfo;
1830524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  const CGFunctionInfo &FuncInfo =
1831524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    CGM.getTypes().arrangeFunctionDeclaration(Ctx.VoidTy, Args, ExtInfo,
1832524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir                                              /*IsVariadic=*/false);
1833524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  llvm::FunctionType *FuncLLVMTy = CGM.getTypes().GetFunctionType(FuncInfo);
1834524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
1835524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  llvm::Function *F =
1836524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    llvm::Function::Create(FuncLLVMTy, llvm::GlobalValue::InternalLinkage,
1837524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir                           CapturedStmtInfo->getHelperName(), &CGM.getModule());
1838524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  CGM.SetInternalFunctionAttributes(CD, F, FuncInfo);
1839524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
1840524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  // Generate the function.
1841524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  StartFunction(CD, Ctx.VoidTy, F, FuncInfo, Args, CD->getBody()->getLocStart());
1842524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
1843524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  // Set the context parameter in CapturedStmtInfo.
1844524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  llvm::Value *DeclPtr = LocalDeclMap[CD->getContextParam()];
1845524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  assert(DeclPtr && "missing context parameter for CapturedStmt");
1846524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  CapturedStmtInfo->setContextValue(Builder.CreateLoad(DeclPtr));
1847524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
1848524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  // If 'this' is captured, load it into CXXThisValue.
1849524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  if (CapturedStmtInfo->isCXXThisExprCaptured()) {
1850524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    FieldDecl *FD = CapturedStmtInfo->getThisFieldDecl();
1851524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    LValue LV = MakeNaturalAlignAddrLValue(CapturedStmtInfo->getContextValue(),
1852524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir                                           Ctx.getTagDeclType(RD));
1853524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    LValue ThisLValue = EmitLValueForField(LV, FD);
1854524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
1855524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    CXXThisValue = EmitLoadOfLValue(ThisLValue).getScalarVal();
1856524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  }
1857524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
1858524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  CapturedStmtInfo->EmitBody(*this, CD->getBody());
1859524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  FinishFunction(CD->getBodyRBrace());
1860524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
1861524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  return F;
1862051303ce09291dfbed537fa33b0d8a4d92c82b75Tareq A. Siraj}
1863