15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- CGStmt.cpp - Emit LLVM Code from Statements ----------------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This contains code to emit Stmt nodes as LLVM code.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1455fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "CodeGenFunction.h"
15e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta#include "CGDebugInfo.h"
16e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta#include "CodeGenModule.h"
174b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne#include "TargetInfo.h"
18de7fb8413b13651fd85b7125d08b3c9ac2816d9dDaniel Dunbar#include "clang/AST/StmtVisitor.h"
197d22bf00dbabca86ba791f56a99e006181fa22ddChris Lattner#include "clang/Basic/PrettyStackTrace.h"
20fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson#include "clang/Basic/TargetInfo.h"
21fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson#include "llvm/ADT/StringExtras.h"
223b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/DataLayout.h"
233b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/InlineAsm.h"
243b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/Intrinsics.h"
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen;
275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                              Statement Emission
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
320912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbarvoid CodeGenFunction::EmitStopPoint(const Stmt *S) {
33e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  if (CGDebugInfo *DI = getDebugInfo()) {
3473fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher    SourceLocation Loc;
3560e4fd95ed8c1f99697f4d9f73d07717b6e21048Devang Patel    if (isa<DeclStmt>(S))
3673fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher      Loc = S->getLocEnd();
3760e4fd95ed8c1f99697f4d9f73d07717b6e21048Devang Patel    else
3873fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher      Loc = S->getLocStart();
3973fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher    DI->EmitLocation(Builder, Loc);
400912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  }
410912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar}
420912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar
435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid CodeGenFunction::EmitStmt(const Stmt *S) {
445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(S && "Null statement?");
45a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar
46f9aac38d1290c17192a74b5bc2de52b9fb1a87caEric Christopher  // These statements have their own debug info handling.
470912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  if (EmitSimpleStmt(S))
480912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar    return;
490912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar
50d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  // Check if we are generating unreachable code.
51d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  if (!HaveInsertPoint()) {
52d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar    // If so, and the statement doesn't contain a label, then we do not need to
53d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar    // generate actual code. This is safe because (1) the current point is
54d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar    // unreachable, so we don't need to execute the code, and (2) we've already
55d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar    // handled the statements which update internal data structures (like the
56d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar    // local variable map) which could be used by subsequent statements.
57d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar    if (!ContainsLabel(S)) {
58d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar      // Verify that any decl statements were handled as simple, they may be in
59d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar      // scope of subsequent reachable statements.
60d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar      assert(!isa<DeclStmt>(*S) && "Unexpected DeclStmt!");
61d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar      return;
62d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar    }
63d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar
64d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar    // Otherwise, make a new block to hold the code.
65d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar    EnsureInsertPoint();
66d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  }
67d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar
680912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // Generate a stoppoint if we are emitting debug info.
690912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  EmitStopPoint(S);
70e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta
715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (S->getStmtClass()) {
722a41637a995affa1563f4d82a8b026e326a2faa0John McCall  case Stmt::NoStmtClass:
732a41637a995affa1563f4d82a8b026e326a2faa0John McCall  case Stmt::CXXCatchStmtClass:
7428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  case Stmt::SEHExceptStmtClass:
7528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  case Stmt::SEHFinallyStmtClass:
76ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor  case Stmt::MSDependentExistsStmtClass:
772a41637a995affa1563f4d82a8b026e326a2faa0John McCall    llvm_unreachable("invalid statement class to emit generically");
782a41637a995affa1563f4d82a8b026e326a2faa0John McCall  case Stmt::NullStmtClass:
792a41637a995affa1563f4d82a8b026e326a2faa0John McCall  case Stmt::CompoundStmtClass:
802a41637a995affa1563f4d82a8b026e326a2faa0John McCall  case Stmt::DeclStmtClass:
812a41637a995affa1563f4d82a8b026e326a2faa0John McCall  case Stmt::LabelStmtClass:
82534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith  case Stmt::AttributedStmtClass:
832a41637a995affa1563f4d82a8b026e326a2faa0John McCall  case Stmt::GotoStmtClass:
842a41637a995affa1563f4d82a8b026e326a2faa0John McCall  case Stmt::BreakStmtClass:
852a41637a995affa1563f4d82a8b026e326a2faa0John McCall  case Stmt::ContinueStmtClass:
862a41637a995affa1563f4d82a8b026e326a2faa0John McCall  case Stmt::DefaultStmtClass:
872a41637a995affa1563f4d82a8b026e326a2faa0John McCall  case Stmt::CaseStmtClass:
882a41637a995affa1563f4d82a8b026e326a2faa0John McCall    llvm_unreachable("should have emitted these statements as simple");
89cd5e60e1d4093b9a757cc85e35fccc093f8f8527Daniel Dunbar
902a41637a995affa1563f4d82a8b026e326a2faa0John McCall#define STMT(Type, Base)
912a41637a995affa1563f4d82a8b026e326a2faa0John McCall#define ABSTRACT_STMT(Op)
922a41637a995affa1563f4d82a8b026e326a2faa0John McCall#define EXPR(Type, Base) \
932a41637a995affa1563f4d82a8b026e326a2faa0John McCall  case Stmt::Type##Class:
942a41637a995affa1563f4d82a8b026e326a2faa0John McCall#include "clang/AST/StmtNodes.inc"
95cd5b22e12b6513163dd131589746c194090f14e6John McCall  {
96cd5b22e12b6513163dd131589746c194090f14e6John McCall    // Remember the block we came in on.
97cd5b22e12b6513163dd131589746c194090f14e6John McCall    llvm::BasicBlock *incoming = Builder.GetInsertBlock();
98cd5b22e12b6513163dd131589746c194090f14e6John McCall    assert(incoming && "expression emission must have an insertion point");
99cd5b22e12b6513163dd131589746c194090f14e6John McCall
1002a41637a995affa1563f4d82a8b026e326a2faa0John McCall    EmitIgnoredExpr(cast<Expr>(S));
1011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
102cd5b22e12b6513163dd131589746c194090f14e6John McCall    llvm::BasicBlock *outgoing = Builder.GetInsertBlock();
103cd5b22e12b6513163dd131589746c194090f14e6John McCall    assert(outgoing && "expression emission cleared block!");
104cd5b22e12b6513163dd131589746c194090f14e6John McCall
105cd5b22e12b6513163dd131589746c194090f14e6John McCall    // The expression emitters assume (reasonably!) that the insertion
106cd5b22e12b6513163dd131589746c194090f14e6John McCall    // point is always set.  To maintain that, the call-emission code
107cd5b22e12b6513163dd131589746c194090f14e6John McCall    // for noreturn functions has to enter a new block with no
108cd5b22e12b6513163dd131589746c194090f14e6John McCall    // predecessors.  We want to kill that block and mark the current
109cd5b22e12b6513163dd131589746c194090f14e6John McCall    // insertion point unreachable in the common case of a call like
110cd5b22e12b6513163dd131589746c194090f14e6John McCall    // "exit();".  Since expression emission doesn't otherwise create
111cd5b22e12b6513163dd131589746c194090f14e6John McCall    // blocks with no predecessors, we can just test for that.
112cd5b22e12b6513163dd131589746c194090f14e6John McCall    // However, we must be careful not to do this to our incoming
113cd5b22e12b6513163dd131589746c194090f14e6John McCall    // block, because *statement* emission does sometimes create
114cd5b22e12b6513163dd131589746c194090f14e6John McCall    // reachable blocks which will have no predecessors until later in
115cd5b22e12b6513163dd131589746c194090f14e6John McCall    // the function.  This occurs with, e.g., labels that are not
116cd5b22e12b6513163dd131589746c194090f14e6John McCall    // reachable by fallthrough.
117cd5b22e12b6513163dd131589746c194090f14e6John McCall    if (incoming != outgoing && outgoing->use_empty()) {
118cd5b22e12b6513163dd131589746c194090f14e6John McCall      outgoing->eraseFromParent();
119cd5b22e12b6513163dd131589746c194090f14e6John McCall      Builder.ClearInsertionPoint();
1205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
122cd5b22e12b6513163dd131589746c194090f14e6John McCall  }
1232a41637a995affa1563f4d82a8b026e326a2faa0John McCall
1241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case Stmt::IndirectGotoStmtClass:
1250ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar    EmitIndirectGotoStmt(cast<IndirectGotoStmt>(*S)); break;
1265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Stmt::IfStmtClass:       EmitIfStmt(cast<IfStmt>(*S));             break;
1285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Stmt::WhileStmtClass:    EmitWhileStmt(cast<WhileStmt>(*S));       break;
1295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Stmt::DoStmtClass:       EmitDoStmt(cast<DoStmt>(*S));             break;
1305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Stmt::ForStmtClass:      EmitForStmt(cast<ForStmt>(*S));           break;
1311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Stmt::ReturnStmtClass:   EmitReturnStmt(cast<ReturnStmt>(*S));     break;
133a4275d194b656867bdcdb725b2a7ba3251a1a638Daniel Dunbar
13451b09f2c528c8460b5465c676173324e44176d62Devang Patel  case Stmt::SwitchStmtClass:   EmitSwitchStmt(cast<SwitchStmt>(*S));     break;
135d1a8d2ef757ad880d48b2249a1619bf8209e9cf8Chad Rosier  case Stmt::GCCAsmStmtClass:   // Intentional fall-through.
136d1a8d2ef757ad880d48b2249a1619bf8209e9cf8Chad Rosier  case Stmt::MSAsmStmtClass:    EmitAsmStmt(cast<AsmStmt>(*S));           break;
1370a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar
1380a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar  case Stmt::ObjCAtTryStmtClass:
13964d5d6c5903157c521af496479d06dc26032d718Anders Carlsson    EmitObjCAtTryStmt(cast<ObjCAtTryStmt>(*S));
1401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    break;
1410a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar  case Stmt::ObjCAtCatchStmtClass:
142b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable(
143b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie                    "@catch statements should be handled by EmitObjCAtTryStmt");
1440a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar  case Stmt::ObjCAtFinallyStmtClass:
145b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable(
146b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie                  "@finally statements should be handled by EmitObjCAtTryStmt");
1470a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar  case Stmt::ObjCAtThrowStmtClass:
14864d5d6c5903157c521af496479d06dc26032d718Anders Carlsson    EmitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(*S));
1490a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar    break;
1500a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar  case Stmt::ObjCAtSynchronizedStmtClass:
15110cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattner    EmitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(*S));
1520a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar    break;
1531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case Stmt::ObjCForCollectionStmtClass:
1543d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson    EmitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(*S));
1550a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar    break;
156f85e193739c953358c865005855253af4f68a497John McCall  case Stmt::ObjCAutoreleasePoolStmtClass:
157f85e193739c953358c865005855253af4f68a497John McCall    EmitObjCAutoreleasePoolStmt(cast<ObjCAutoreleasePoolStmt>(*S));
158f85e193739c953358c865005855253af4f68a497John McCall    break;
1596f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
1606815e941998659a55c20c147861b0f437928c3d8Anders Carlsson  case Stmt::CXXTryStmtClass:
1616815e941998659a55c20c147861b0f437928c3d8Anders Carlsson    EmitCXXTryStmt(cast<CXXTryStmt>(*S));
1626815e941998659a55c20c147861b0f437928c3d8Anders Carlsson    break;
163ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  case Stmt::CXXForRangeStmtClass:
164ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    EmitCXXForRangeStmt(cast<CXXForRangeStmt>(*S));
16528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  case Stmt::SEHTryStmtClass:
16628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    // FIXME Not yet implemented
167ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    break;
1685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1710912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbarbool CodeGenFunction::EmitSimpleStmt(const Stmt *S) {
1720912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  switch (S->getStmtClass()) {
1730912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  default: return false;
1740912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  case Stmt::NullStmtClass: break;
1750912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  case Stmt::CompoundStmtClass: EmitCompoundStmt(cast<CompoundStmt>(*S)); break;
176d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  case Stmt::DeclStmtClass:     EmitDeclStmt(cast<DeclStmt>(*S));         break;
1770912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  case Stmt::LabelStmtClass:    EmitLabelStmt(cast<LabelStmt>(*S));       break;
178534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith  case Stmt::AttributedStmtClass:
179534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith                            EmitAttributedStmt(cast<AttributedStmt>(*S)); break;
1800912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  case Stmt::GotoStmtClass:     EmitGotoStmt(cast<GotoStmt>(*S));         break;
1810912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  case Stmt::BreakStmtClass:    EmitBreakStmt(cast<BreakStmt>(*S));       break;
1820912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  case Stmt::ContinueStmtClass: EmitContinueStmt(cast<ContinueStmt>(*S)); break;
1830912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  case Stmt::DefaultStmtClass:  EmitDefaultStmt(cast<DefaultStmt>(*S));   break;
1840912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  case Stmt::CaseStmtClass:     EmitCaseStmt(cast<CaseStmt>(*S));         break;
1850912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  }
1860912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar
1870912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  return true;
1880912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar}
1890912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar
1903379320c10001d7e1ee5d7e7142c417f797cfe82Chris Lattner/// EmitCompoundStmt - Emit a compound statement {..} node.  If GetLast is true,
1913379320c10001d7e1ee5d7e7142c417f797cfe82Chris Lattner/// this captures the expression result of the last sub-statement and returns it
1923379320c10001d7e1ee5d7e7142c417f797cfe82Chris Lattner/// (for use by the statement expression extension).
1939b65551d0b387a7597fb39356a4d8ef10046445eChris LattnerRValue CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast,
194558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall                                         AggValueSlot AggSlot) {
1957d22bf00dbabca86ba791f56a99e006181fa22ddChris Lattner  PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),S.getLBracLoc(),
1967d22bf00dbabca86ba791f56a99e006181fa22ddChris Lattner                             "LLVM IR generation of compound statement ('{}')");
1971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
198fdc5d565b30bd2009ec98aac4b5846a740aff767Eric Christopher  // Keep track of the current cleanup stack depth, including debug scopes.
199fdc5d565b30bd2009ec98aac4b5846a740aff767Eric Christopher  LexicalScope Scope(*this, S.getSourceRange());
2001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
201a6504853d297c30cfa271f4710af5a3d5db59449David Blaikie  return EmitCompoundStmtWithoutScope(S, GetLast, AggSlot);
202a6504853d297c30cfa271f4710af5a3d5db59449David Blaikie}
203a6504853d297c30cfa271f4710af5a3d5db59449David Blaikie
204a6504853d297c30cfa271f4710af5a3d5db59449David BlaikieRValue CodeGenFunction::EmitCompoundStmtWithoutScope(const CompoundStmt &S, bool GetLast,
205a6504853d297c30cfa271f4710af5a3d5db59449David Blaikie                                         AggValueSlot AggSlot) {
206a6504853d297c30cfa271f4710af5a3d5db59449David Blaikie
2073379320c10001d7e1ee5d7e7142c417f797cfe82Chris Lattner  for (CompoundStmt::const_body_iterator I = S.body_begin(),
2083379320c10001d7e1ee5d7e7142c417f797cfe82Chris Lattner       E = S.body_end()-GetLast; I != E; ++I)
2095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    EmitStmt(*I);
210e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta
21117d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson  RValue RV;
2121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!GetLast)
21317d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson    RV = RValue::get(0);
21417d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson  else {
2151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // We have to special case labels here.  They are statements, but when put
21617d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson    // at the end of a statement expression, they yield the value of their
21717d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson    // subexpression.  Handle this by walking through all labels we encounter,
21817d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson    // emitting them before we evaluate the subexpr.
21917d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson    const Stmt *LastStmt = S.body_back();
22017d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson    while (const LabelStmt *LS = dyn_cast<LabelStmt>(LastStmt)) {
221ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner      EmitLabel(LS->getDecl());
22217d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson      LastStmt = LS->getSubStmt();
22317d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson    }
2241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22517d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson    EnsureInsertPoint();
2261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
227558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    RV = EmitAnyExpr(cast<Expr>(LastStmt), AggSlot);
22891d723da7b68be5245c3ac58aa2a36d04658cfb8Chris Lattner  }
229a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar
23017d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson  return RV;
2315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
233aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbarvoid CodeGenFunction::SimplifyForwardingBlocks(llvm::BasicBlock *BB) {
234aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  llvm::BranchInst *BI = dyn_cast<llvm::BranchInst>(BB->getTerminator());
2351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
236aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  // If there is a cleanup stack, then we it isn't worth trying to
237aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  // simplify this block (we would need to remove it from the scope map
238aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  // and cleanup entry).
239f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!EHStack.empty())
240aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar    return;
241aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar
242aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  // Can only simplify direct branches.
243aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  if (!BI || !BI->isUnconditional())
244aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar    return;
245aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar
2463d7c780d93ebcb84a1224de8c44835ea43e82b15Eli Friedman  // Can only simplify empty blocks.
2473d7c780d93ebcb84a1224de8c44835ea43e82b15Eli Friedman  if (BI != BB->begin())
2483d7c780d93ebcb84a1224de8c44835ea43e82b15Eli Friedman    return;
2493d7c780d93ebcb84a1224de8c44835ea43e82b15Eli Friedman
250aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  BB->replaceAllUsesWith(BI->getSuccessor(0));
251aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  BI->eraseFromParent();
252aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  BB->eraseFromParent();
253aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar}
254aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar
255a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbarvoid CodeGenFunction::EmitBlock(llvm::BasicBlock *BB, bool IsFinished) {
256548ce5e78215a34f409d597bb2c1e9f897a8eda3John McCall  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
257548ce5e78215a34f409d597bb2c1e9f897a8eda3John McCall
258d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  // Fall out of the current block (if necessary).
259d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  EmitBranch(BB);
260a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar
261a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar  if (IsFinished && BB->use_empty()) {
262a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar    delete BB;
263a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar    return;
264a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar  }
265a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar
266839cbaa004a24e8f1ea14db5ed76e3d25ed28996John McCall  // Place the block after the current block, if possible, or else at
267839cbaa004a24e8f1ea14db5ed76e3d25ed28996John McCall  // the end of the function.
268548ce5e78215a34f409d597bb2c1e9f897a8eda3John McCall  if (CurBB && CurBB->getParent())
269548ce5e78215a34f409d597bb2c1e9f897a8eda3John McCall    CurFn->getBasicBlockList().insertAfter(CurBB, BB);
270839cbaa004a24e8f1ea14db5ed76e3d25ed28996John McCall  else
271839cbaa004a24e8f1ea14db5ed76e3d25ed28996John McCall    CurFn->getBasicBlockList().push_back(BB);
272d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  Builder.SetInsertPoint(BB);
273d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar}
274d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar
275d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbarvoid CodeGenFunction::EmitBranch(llvm::BasicBlock *Target) {
276d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  // Emit a branch from the current block to the target one if this
277d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  // was a real block.  If this was just a fall-through block after a
278d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  // terminator, don't emit it.
279d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
280d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar
281d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  if (!CurBB || CurBB->getTerminator()) {
282d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar    // If there is no insert point or the previous block is already
283d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar    // terminated, don't touch it.
2845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } else {
2855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Otherwise, create a fall-through branch.
286d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar    Builder.CreateBr(Target);
2875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2885e08ad3cc62ab94649959ae227a9a411a729bf49Daniel Dunbar
2895e08ad3cc62ab94649959ae227a9a411a729bf49Daniel Dunbar  Builder.ClearInsertionPoint();
2905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
292777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCallvoid CodeGenFunction::EmitBlockAfterUses(llvm::BasicBlock *block) {
293777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  bool inserted = false;
294777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  for (llvm::BasicBlock::use_iterator
295777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall         i = block->use_begin(), e = block->use_end(); i != e; ++i) {
296777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    if (llvm::Instruction *insn = dyn_cast<llvm::Instruction>(*i)) {
297777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      CurFn->getBasicBlockList().insertAfter(insn->getParent(), block);
298777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      inserted = true;
299777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      break;
300777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    }
301777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  }
302777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
303777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  if (!inserted)
304777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    CurFn->getBasicBlockList().push_back(block);
305777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
306777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  Builder.SetInsertPoint(block);
307777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall}
308777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
309f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallCodeGenFunction::JumpDest
310ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris LattnerCodeGenFunction::getJumpDestForLabel(const LabelDecl *D) {
311ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  JumpDest &Dest = LabelMap[D];
312ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  if (Dest.isValid()) return Dest;
313f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
314f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Create, but don't insert, the new block.
315ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  Dest = JumpDest(createBasicBlock(D->getName()),
316ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall                  EHScopeStack::stable_iterator::invalid(),
317ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall                  NextCleanupDestIndex++);
318f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  return Dest;
319f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
320f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
321ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattnervoid CodeGenFunction::EmitLabel(const LabelDecl *D) {
322ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  JumpDest &Dest = LabelMap[D];
323f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
324ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  // If we didn't need a forward reference to this label, just go
325f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // ahead and create a destination at the current scope.
326ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  if (!Dest.isValid()) {
327ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner    Dest = getJumpDestInCurrentScope(D->getName());
328f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
329f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Otherwise, we need to give this label a target depth and remove
330f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // it from the branch-fixups list.
331f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  } else {
332ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    assert(!Dest.getScopeDepth().isValid() && "already emitted label!");
333ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    Dest = JumpDest(Dest.getBlock(),
334ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall                    EHStack.stable_begin(),
335ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall                    Dest.getDestIndex());
336f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
337ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    ResolveBranchFixups(Dest.getBlock());
338f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
339f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
340ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(Dest.getBlock());
34191d723da7b68be5245c3ac58aa2a36d04658cfb8Chris Lattner}
34291d723da7b68be5245c3ac58aa2a36d04658cfb8Chris Lattner
34391d723da7b68be5245c3ac58aa2a36d04658cfb8Chris Lattner
34491d723da7b68be5245c3ac58aa2a36d04658cfb8Chris Lattnervoid CodeGenFunction::EmitLabelStmt(const LabelStmt &S) {
345ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  EmitLabel(S.getDecl());
3465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  EmitStmt(S.getSubStmt());
3475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
349534986f2b21e6050bf00163cd6423fd92155a6edRichard Smithvoid CodeGenFunction::EmitAttributedStmt(const AttributedStmt &S) {
350534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith  EmitStmt(S.getSubStmt());
351534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith}
352534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith
3535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid CodeGenFunction::EmitGotoStmt(const GotoStmt &S) {
3540912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // If this code is reachable then emit a stop point (if generating
3550912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // debug info). We have to do this ourselves because we are on the
3560912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // "simple" statement path.
3570912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  if (HaveInsertPoint())
3580912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar    EmitStopPoint(&S);
35936a2ada69fdb457b0e46d0ef452c150b360d8888Mike Stump
360f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitBranchThroughCleanup(getJumpDestForLabel(S.getLabel()));
3615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3633d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
3640ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbarvoid CodeGenFunction::EmitIndirectGotoStmt(const IndirectGotoStmt &S) {
365ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  if (const LabelDecl *Target = S.getConstantTarget()) {
36695c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall    EmitBranchThroughCleanup(getJumpDestForLabel(Target));
36795c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall    return;
36895c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall  }
36995c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall
37049c952f853fe2d15dd9c9ff2a29c696bd18fca13Chris Lattner  // Ensure that we have an i8* for our PHI node.
371d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  llvm::Value *V = Builder.CreateBitCast(EmitScalarExpr(S.getTarget()),
372d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall                                         Int8PtrTy, "addr");
3733d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
3743d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
3753d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  // Get the basic block for the indirect goto.
3763d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  llvm::BasicBlock *IndGotoBB = GetIndirectGotoBlock();
3776f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
3783d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  // The first instruction in the block has to be the PHI for the switch dest,
3793d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  // add an entry for this branch.
3803d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  cast<llvm::PHINode>(IndGotoBB->begin())->addIncoming(V, CurBB);
3816f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
3823d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  EmitBranch(IndGotoBB);
3830ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar}
3840ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar
38562b72f642207ba2ba433d686df924dc9594e9897Chris Lattnervoid CodeGenFunction::EmitIfStmt(const IfStmt &S) {
3865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.8.4.1: The first substatement is executed if the expression compares
3875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // unequal to 0.  The condition must be a scalar type.
388f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  RunCleanupsScope ConditionScope(*this);
38901234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor
3908cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor  if (S.getConditionVariable())
391b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall    EmitAutoVarDecl(*S.getConditionVariable());
3921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3939bc47e29dce8f095be7a6d07dbb02a5a7a112949Chris Lattner  // If the condition constant folds and can be elided, try to avoid emitting
3949bc47e29dce8f095be7a6d07dbb02a5a7a112949Chris Lattner  // the condition and the dead arm of the if/else.
395c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner  bool CondConstant;
396c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner  if (ConstantFoldsToSimpleInteger(S.getCond(), CondConstant)) {
39762b72f642207ba2ba433d686df924dc9594e9897Chris Lattner    // Figure out which block (then or else) is executed.
398c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner    const Stmt *Executed = S.getThen();
399c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner    const Stmt *Skipped  = S.getElse();
400c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner    if (!CondConstant)  // Condition false?
40162b72f642207ba2ba433d686df924dc9594e9897Chris Lattner      std::swap(Executed, Skipped);
4021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
40362b72f642207ba2ba433d686df924dc9594e9897Chris Lattner    // If the skipped block has no labels in it, just emit the executed block.
40462b72f642207ba2ba433d686df924dc9594e9897Chris Lattner    // This avoids emitting dead code and simplifies the CFG substantially.
4059bc47e29dce8f095be7a6d07dbb02a5a7a112949Chris Lattner    if (!ContainsLabel(Skipped)) {
40601234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor      if (Executed) {
407f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall        RunCleanupsScope ExecutedScope(*this);
40862b72f642207ba2ba433d686df924dc9594e9897Chris Lattner        EmitStmt(Executed);
40901234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor      }
41062b72f642207ba2ba433d686df924dc9594e9897Chris Lattner      return;
41162b72f642207ba2ba433d686df924dc9594e9897Chris Lattner    }
41262b72f642207ba2ba433d686df924dc9594e9897Chris Lattner  }
4139bc47e29dce8f095be7a6d07dbb02a5a7a112949Chris Lattner
4149bc47e29dce8f095be7a6d07dbb02a5a7a112949Chris Lattner  // Otherwise, the condition did not fold, or we couldn't elide it.  Just emit
4159bc47e29dce8f095be7a6d07dbb02a5a7a112949Chris Lattner  // the conditional branch.
416781d7ca9b2fd626ef34bdc3fe06765eeff7ab2bcDaniel Dunbar  llvm::BasicBlock *ThenBlock = createBasicBlock("if.then");
417781d7ca9b2fd626ef34bdc3fe06765eeff7ab2bcDaniel Dunbar  llvm::BasicBlock *ContBlock = createBasicBlock("if.end");
418781d7ca9b2fd626ef34bdc3fe06765eeff7ab2bcDaniel Dunbar  llvm::BasicBlock *ElseBlock = ContBlock;
4195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (S.getElse())
420781d7ca9b2fd626ef34bdc3fe06765eeff7ab2bcDaniel Dunbar    ElseBlock = createBasicBlock("if.else");
421781d7ca9b2fd626ef34bdc3fe06765eeff7ab2bcDaniel Dunbar  EmitBranchOnBoolExpr(S.getCond(), ThenBlock, ElseBlock);
4221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Emit the 'then' code.
42401234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor  EmitBlock(ThenBlock);
42501234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor  {
426f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    RunCleanupsScope ThenScope(*this);
42701234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor    EmitStmt(S.getThen());
42801234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor  }
429d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  EmitBranch(ContBlock);
4301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Emit the 'else' code if present.
4325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (const Stmt *Else = S.getElse()) {
433acd723666777b4ac1f79a97b6a300e6cf919d519Devang Patel    // There is no need to emit line number for unconditional branch.
434acd723666777b4ac1f79a97b6a300e6cf919d519Devang Patel    if (getDebugInfo())
435acd723666777b4ac1f79a97b6a300e6cf919d519Devang Patel      Builder.SetCurrentDebugLocation(llvm::DebugLoc());
4365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    EmitBlock(ElseBlock);
43701234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor    {
438f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      RunCleanupsScope ElseScope(*this);
43901234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor      EmitStmt(Else);
44001234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor    }
441acd723666777b4ac1f79a97b6a300e6cf919d519Devang Patel    // There is no need to emit line number for unconditional branch.
442acd723666777b4ac1f79a97b6a300e6cf919d519Devang Patel    if (getDebugInfo())
443acd723666777b4ac1f79a97b6a300e6cf919d519Devang Patel      Builder.SetCurrentDebugLocation(llvm::DebugLoc());
444d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar    EmitBranch(ContBlock);
4455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Emit the continuation block for code after the if.
448c22d665ede76f70228055d638a087f4bd438292dDaniel Dunbar  EmitBlock(ContBlock, true);
4495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid CodeGenFunction::EmitWhileStmt(const WhileStmt &S) {
452f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Emit the header for the loop, which will also become
453f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // the continue target.
454f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest LoopHeader = getJumpDestInCurrentScope("while.cond");
455ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(LoopHeader.getBlock());
456f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
457f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Create an exit block for when the condition fails, which will
458f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // also become the break target.
459f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest LoopExit = getJumpDestInCurrentScope("while.end");
46072cac2ccce8058833f56358e3391e28a8ddeeaa4Mike Stump
46172cac2ccce8058833f56358e3391e28a8ddeeaa4Mike Stump  // Store the blocks to use for break and continue.
462f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  BreakContinueStack.push_back(BreakContinue(LoopExit, LoopHeader));
4631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4645656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  // C++ [stmt.while]p2:
4655656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  //   When the condition of a while statement is a declaration, the
4665656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  //   scope of the variable that is declared extends from its point
4675656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  //   of declaration (3.3.2) to the end of the while statement.
4685656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  //   [...]
4695656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  //   The object created in a condition is destroyed and created
4705656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  //   with each iteration of the loop.
471f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  RunCleanupsScope ConditionScope(*this);
4725656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor
473f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (S.getConditionVariable())
474b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall    EmitAutoVarDecl(*S.getConditionVariable());
4756f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
47616b16206741f5139c4ad870632db8f9ea4c6c943Mike Stump  // Evaluate the conditional in the while header.  C99 6.8.5.1: The
47716b16206741f5139c4ad870632db8f9ea4c6c943Mike Stump  // evaluation of the controlling expression takes place before each
47816b16206741f5139c4ad870632db8f9ea4c6c943Mike Stump  // execution of the loop body.
4795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
4806f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
4812c30d8fee8981ca4f20a477456dae1b722b53f1dDevang Patel  // while(1) is common, avoid extra exit blocks.  Be sure
4825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // to correctly handle break/continue though.
4832c30d8fee8981ca4f20a477456dae1b722b53f1dDevang Patel  bool EmitBoolCondBranch = true;
4841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal))
4852c30d8fee8981ca4f20a477456dae1b722b53f1dDevang Patel    if (C->isOne())
4862c30d8fee8981ca4f20a477456dae1b722b53f1dDevang Patel      EmitBoolCondBranch = false;
4871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // As long as the condition is true, go to the loop body.
489f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *LoopBody = createBasicBlock("while.body");
490f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (EmitBoolCondBranch) {
491ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    llvm::BasicBlock *ExitBlock = LoopExit.getBlock();
492f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    if (ConditionScope.requiresCleanups())
493f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      ExitBlock = createBasicBlock("while.exit");
494f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
495f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock);
496f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
497ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    if (ExitBlock != LoopExit.getBlock()) {
498f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      EmitBlock(ExitBlock);
499f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      EmitBranchThroughCleanup(LoopExit);
500f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    }
501f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
5026f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
503f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Emit the loop body.  We have to emit this in a cleanup scope
504f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // because it might be a singleton DeclStmt.
5055656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  {
506f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    RunCleanupsScope BodyScope(*this);
5075656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    EmitBlock(LoopBody);
5085656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    EmitStmt(S.getBody());
5095656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  }
510da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner
5111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  BreakContinueStack.pop_back();
5121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
513f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Immediately force cleanup.
514f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ConditionScope.ForceCleanup();
5155656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor
516f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Branch to the loop header again.
517ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBranch(LoopHeader.getBlock());
5181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Emit the exit block.
520ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(LoopExit.getBlock(), true);
5215656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor
522aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  // The LoopHeader typically is just a branch if we skipped emitting
523aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  // a branch, try to erase it.
524f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!EmitBoolCondBranch)
525ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    SimplifyForwardingBlocks(LoopHeader.getBlock());
5265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid CodeGenFunction::EmitDoStmt(const DoStmt &S) {
529f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest LoopExit = getJumpDestInCurrentScope("do.end");
530f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest LoopCond = getJumpDestInCurrentScope("do.cond");
5311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
532da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner  // Store the blocks to use for break and continue.
533f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  BreakContinueStack.push_back(BreakContinue(LoopExit, LoopCond));
5341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
535f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Emit the body of the loop.
536f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *LoopBody = createBasicBlock("do.body");
537f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitBlock(LoopBody);
538f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  {
539f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    RunCleanupsScope BodyScope(*this);
540f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    EmitStmt(S.getBody());
541f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
5421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
543e4b6d342c29d5cb9d311756100df1603810fa892Anders Carlsson  BreakContinueStack.pop_back();
5441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
545ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(LoopCond.getBlock());
5461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.8.5.2: "The evaluation of the controlling expression takes place
5485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // after each execution of the loop body."
5491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Evaluate the conditional in the while header.
5515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.8.5p2/p4: The first substatement is executed if the expression
5525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // compares unequal to 0.  The condition must be a scalar type.
5535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
55405f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel
55505f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel  // "do {} while (0)" is common in macros, avoid extra blocks.  Be sure
55605f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel  // to correctly handle break/continue though.
55705f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel  bool EmitBoolCondBranch = true;
5581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal))
55905f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel    if (C->isZero())
56005f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel      EmitBoolCondBranch = false;
56105f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel
5625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // As long as the condition is true, iterate the loop.
56305f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel  if (EmitBoolCondBranch)
564ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    Builder.CreateCondBr(BoolCondVal, LoopBody, LoopExit.getBlock());
5651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Emit the exit block.
567ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(LoopExit.getBlock());
56805f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel
569aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  // The DoCond block typically is just a branch if we skipped
570aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  // emitting a branch, try to erase it.
571aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  if (!EmitBoolCondBranch)
572ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    SimplifyForwardingBlocks(LoopCond.getBlock());
5735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid CodeGenFunction::EmitForStmt(const ForStmt &S) {
576f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest LoopExit = getJumpDestInCurrentScope("for.end");
577f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
578f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  RunCleanupsScope ForScope(*this);
579da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner
5800554e0e30d24d9ad7d5e12f8e7583ebb5c9715bfDevang Patel  CGDebugInfo *DI = getDebugInfo();
58173fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher  if (DI)
58273fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher    DI->EmitLexicalBlockStart(Builder, S.getSourceRange().getBegin());
5830554e0e30d24d9ad7d5e12f8e7583ebb5c9715bfDevang Patel
5845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Evaluate the first part before the loop.
5855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (S.getInit())
5865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    EmitStmt(S.getInit());
5875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Start the loop with a block that tests the condition.
589f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // If there's an increment, the continue scope will be overwritten
590f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // later.
591f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest Continue = getJumpDestInCurrentScope("for.cond");
592ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::BasicBlock *CondBlock = Continue.getBlock();
5935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  EmitBlock(CondBlock);
5945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
595d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor  // Create a cleanup scope for the condition variable cleanups.
596f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  RunCleanupsScope ConditionScope(*this);
5976f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
598d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor  llvm::Value *BoolCondVal = 0;
5995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (S.getCond()) {
60099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    // If the for statement has a condition scope, emit the local variable
60199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    // declaration.
602ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    llvm::BasicBlock *ExitBlock = LoopExit.getBlock();
603d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor    if (S.getConditionVariable()) {
604b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall      EmitAutoVarDecl(*S.getConditionVariable());
605d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor    }
606f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
607f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    // If there are any cleanups between here and the loop-exit scope,
608f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    // create a block to stage a loop exit along.
609f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    if (ForScope.requiresCleanups())
610f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      ExitBlock = createBasicBlock("for.cond.cleanup");
6116f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
61231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    // As long as the condition is true, iterate the loop.
6139615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar    llvm::BasicBlock *ForBody = createBasicBlock("for.body");
6141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.8.5p2/p4: The first substatement is executed if the expression
6165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // compares unequal to 0.  The condition must be a scalar type.
617d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor    BoolCondVal = EvaluateExprAsBool(S.getCond());
618f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock);
619f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
620ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    if (ExitBlock != LoopExit.getBlock()) {
621f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      EmitBlock(ExitBlock);
622f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      EmitBranchThroughCleanup(LoopExit);
623f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    }
6241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EmitBlock(ForBody);
6265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } else {
6275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Treat it as a non-zero constant.  Don't even create a new block for the
6285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // body, just fall into it.
6295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
6305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If the for loop doesn't have an increment we can just use the
632f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // condition as the continue block.  Otherwise we'll need to create
633f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // a block for it (in the current scope, i.e. in the scope of the
634f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // condition), and that we will become our continue block.
635da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner  if (S.getInc())
636f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    Continue = getJumpDestInCurrentScope("for.inc");
6371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
638da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner  // Store the blocks to use for break and continue.
639f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
6403e9da66ac7e88d64d30ee777588677320660cf84Mike Stump
641d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor  {
642d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor    // Create a separate cleanup scope for the body, in case it is not
643d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor    // a compound statement.
644f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    RunCleanupsScope BodyScope(*this);
645d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor    EmitStmt(S.getBody());
646d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor  }
647da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner
6485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If there is an increment, emit it next.
649ad12b6d643aba6c36f5cec4c9beb4977a12eace4Daniel Dunbar  if (S.getInc()) {
650ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    EmitBlock(Continue.getBlock());
651883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner    EmitStmt(S.getInc());
652ad12b6d643aba6c36f5cec4c9beb4977a12eace4Daniel Dunbar  }
6531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
65445d3fe1898d3726d269a0bd2ccb8527102e29d79Douglas Gregor  BreakContinueStack.pop_back();
655f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
656f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ConditionScope.ForceCleanup();
657f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitBranch(CondBlock);
658f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
659f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ForScope.ForceCleanup();
660f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
66173fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher  if (DI)
66273fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher    DI->EmitLexicalBlockEnd(Builder, S.getSourceRange().getEnd());
6635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
664da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner  // Emit the fall-through block.
665ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(LoopExit.getBlock(), true);
6665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
6675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
668ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithvoid CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S) {
669ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  JumpDest LoopExit = getJumpDestInCurrentScope("for.end");
670ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
671ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  RunCleanupsScope ForScope(*this);
672ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
673ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  CGDebugInfo *DI = getDebugInfo();
67473fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher  if (DI)
67573fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher    DI->EmitLexicalBlockStart(Builder, S.getSourceRange().getBegin());
676ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
677ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // Evaluate the first pieces before the loop.
678ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  EmitStmt(S.getRangeStmt());
679ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  EmitStmt(S.getBeginEndStmt());
680ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
681ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // Start the loop with a block that tests the condition.
682ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // If there's an increment, the continue scope will be overwritten
683ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // later.
684ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
685ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  EmitBlock(CondBlock);
686ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
687ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // If there are any cleanups between here and the loop-exit scope,
688ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // create a block to stage a loop exit along.
689ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  llvm::BasicBlock *ExitBlock = LoopExit.getBlock();
690ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  if (ForScope.requiresCleanups())
691ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    ExitBlock = createBasicBlock("for.cond.cleanup");
6926f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
693ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // The loop body, consisting of the specified body and the loop variable.
694ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  llvm::BasicBlock *ForBody = createBasicBlock("for.body");
695ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
696ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // The body is executed if the expression, contextually converted
697ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // to bool, is true.
698ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
699ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock);
700ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
701ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  if (ExitBlock != LoopExit.getBlock()) {
702ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    EmitBlock(ExitBlock);
703ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    EmitBranchThroughCleanup(LoopExit);
704ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
705ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
706ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  EmitBlock(ForBody);
707ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
708ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // Create a block for the increment. In case of a 'continue', we jump there.
709ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  JumpDest Continue = getJumpDestInCurrentScope("for.inc");
710ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
711ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // Store the blocks to use for break and continue.
712ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
713ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
714ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  {
715ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    // Create a separate cleanup scope for the loop variable and body.
716ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    RunCleanupsScope BodyScope(*this);
717ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    EmitStmt(S.getLoopVarStmt());
718ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    EmitStmt(S.getBody());
719ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
720ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
721ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // If there is an increment, emit it next.
722ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  EmitBlock(Continue.getBlock());
723ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  EmitStmt(S.getInc());
724ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
725ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  BreakContinueStack.pop_back();
726ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
727ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  EmitBranch(CondBlock);
728ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
729ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  ForScope.ForceCleanup();
730ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
73173fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher  if (DI)
73273fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher    DI->EmitLexicalBlockEnd(Builder, S.getSourceRange().getEnd());
733ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
734ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // Emit the fall-through block.
735ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  EmitBlock(LoopExit.getBlock(), true);
736ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith}
737ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
73829e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbarvoid CodeGenFunction::EmitReturnOfRValue(RValue RV, QualType Ty) {
73929e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar  if (RV.isScalar()) {
74029e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar    Builder.CreateStore(RV.getScalarVal(), ReturnValue);
74129e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar  } else if (RV.isAggregate()) {
742649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier    EmitAggregateCopy(ReturnValue, RV.getAggregateAddr(), Ty);
74329e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar  } else {
7449d232c884ea9872d6555df0fd7359699819bc1f1John McCall    EmitStoreOfComplex(RV.getComplexVal(),
7459d232c884ea9872d6555df0fd7359699819bc1f1John McCall                       MakeNaturalAlignAddrLValue(ReturnValue, Ty),
7469d232c884ea9872d6555df0fd7359699819bc1f1John McCall                       /*init*/ true);
74729e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar  }
74882d8ef0be44ddba608c1ce5c8b6b48da83bc1821Anders Carlsson  EmitBranchThroughCleanup(ReturnBlock);
74929e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar}
75029e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar
7515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// EmitReturnStmt - Note that due to GCC extensions, this can have an operand
7525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// if the function returns void, or may be missing one if the function returns
7535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// non-void.  Fun stuff :).
7545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) {
7555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Emit the result value, even if unused, to evalute the side effects.
7565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const Expr *RV = S.getRetValue();
7571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7589f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall  // Treat block literals in a return expression as if they appeared
7599f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall  // in their own scope.  This permits a small, easily-implemented
7609f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall  // exception to our over-conservative rules about not jumping to
7619f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall  // statements following block literals with non-trivial cleanups.
7629f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall  RunCleanupsScope cleanupScope(*this);
7639f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall  if (const ExprWithCleanups *cleanups =
7649f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall        dyn_cast_or_null<ExprWithCleanups>(RV)) {
7659f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall    enterFullExpression(cleanups);
7669f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall    RV = cleanups->getSubExpr();
7679f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall  }
7689f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall
7695ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar  // FIXME: Clean this up by using an LValue for ReturnTemp,
7705ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar  // EmitStoreThroughLValue, and EmitAnyExpr.
771d86c477fb5d3fc34864afecbbb5443da9355e8fbDouglas Gregor  if (S.getNRVOCandidate() && S.getNRVOCandidate()->isNRVOVariable() &&
772d86c477fb5d3fc34864afecbbb5443da9355e8fbDouglas Gregor      !Target.useGlobalsForAutomaticVariables()) {
773d86c477fb5d3fc34864afecbbb5443da9355e8fbDouglas Gregor    // Apply the named return value optimization for this return statement,
774d86c477fb5d3fc34864afecbbb5443da9355e8fbDouglas Gregor    // which means doing nothing: the appropriate result has already been
775d86c477fb5d3fc34864afecbbb5443da9355e8fbDouglas Gregor    // constructed into the NRVO variable.
7766f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
7773d91bbcdab155181556969cad6ec97014405acedDouglas Gregor    // If there is an NRVO flag for this variable, set it to 1 into indicate
7783d91bbcdab155181556969cad6ec97014405acedDouglas Gregor    // that the cleanup code should not destroy the variable.
779d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall    if (llvm::Value *NRVOFlag = NRVOFlags[S.getNRVOCandidate()])
780d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall      Builder.CreateStore(Builder.getTrue(), NRVOFlag);
781d86c477fb5d3fc34864afecbbb5443da9355e8fbDouglas Gregor  } else if (!ReturnValue) {
7825ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar    // Make sure not to return anything, but evaluate the expression
7835ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar    // for side effects.
7845ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar    if (RV)
785144ac61f9005a0da4327d4e62a4c453923b7bc0cEli Friedman      EmitAnyExpr(RV);
7865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } else if (RV == 0) {
7875ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar    // Do nothing (return value is left uninitialized)
788d54b6ac2f4f6f0bd0076cbfa885b57277066f06cEli Friedman  } else if (FnRetTy->isReferenceType()) {
789d54b6ac2f4f6f0bd0076cbfa885b57277066f06cEli Friedman    // If this function returns a reference, take the address of the expression
790d54b6ac2f4f6f0bd0076cbfa885b57277066f06cEli Friedman    // rather than the value.
79132f36baa6c8d491c374af622b4e3ac28d597453cAnders Carlsson    RValue Result = EmitReferenceBindingToExpr(RV, /*InitializedDecl=*/0);
79233fd1fc1814a5573c972840d49317989e20deaceDouglas Gregor    Builder.CreateStore(Result.getScalarVal(), ReturnValue);
7935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } else {
7949d232c884ea9872d6555df0fd7359699819bc1f1John McCall    switch (getEvaluationKind(RV->getType())) {
7959d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case TEK_Scalar:
7969d232c884ea9872d6555df0fd7359699819bc1f1John McCall      Builder.CreateStore(EmitScalarExpr(RV), ReturnValue);
7979d232c884ea9872d6555df0fd7359699819bc1f1John McCall      break;
7989d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case TEK_Complex:
7999d232c884ea9872d6555df0fd7359699819bc1f1John McCall      EmitComplexExprIntoLValue(RV,
8009d232c884ea9872d6555df0fd7359699819bc1f1John McCall                     MakeNaturalAlignAddrLValue(ReturnValue, RV->getType()),
8019d232c884ea9872d6555df0fd7359699819bc1f1John McCall                                /*isInit*/ true);
8029d232c884ea9872d6555df0fd7359699819bc1f1John McCall      break;
8039d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case TEK_Aggregate: {
8049d232c884ea9872d6555df0fd7359699819bc1f1John McCall      CharUnits Alignment = getContext().getTypeAlignInChars(RV->getType());
8059d232c884ea9872d6555df0fd7359699819bc1f1John McCall      EmitAggExpr(RV, AggValueSlot::forAddr(ReturnValue, Alignment,
8069d232c884ea9872d6555df0fd7359699819bc1f1John McCall                                            Qualifiers(),
8079d232c884ea9872d6555df0fd7359699819bc1f1John McCall                                            AggValueSlot::IsDestructed,
8089d232c884ea9872d6555df0fd7359699819bc1f1John McCall                                            AggValueSlot::DoesNotNeedGCBarriers,
8099d232c884ea9872d6555df0fd7359699819bc1f1John McCall                                            AggValueSlot::IsNotAliased));
8109d232c884ea9872d6555df0fd7359699819bc1f1John McCall      break;
8119d232c884ea9872d6555df0fd7359699819bc1f1John McCall    }
8129d232c884ea9872d6555df0fd7359699819bc1f1John McCall    }
8135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
814144ac61f9005a0da4327d4e62a4c453923b7bc0cEli Friedman
8159f357de8d5823f9b13cf33ad1f6af1dd69b7669fJohn McCall  cleanupScope.ForceCleanup();
81682d8ef0be44ddba608c1ce5c8b6b48da83bc1821Anders Carlsson  EmitBranchThroughCleanup(ReturnBlock);
8175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid CodeGenFunction::EmitDeclStmt(const DeclStmt &S) {
8209198126067a447f8aaccf9fff09be294c8bcb81eDevang Patel  // As long as debug info is modeled with instructions, we have to ensure we
8219198126067a447f8aaccf9fff09be294c8bcb81eDevang Patel  // have a place to insert here and write the stop point here.
8222b124ea9d2c27c6d002ecd4623f6321e305d907eEric Christopher  if (HaveInsertPoint())
8239198126067a447f8aaccf9fff09be294c8bcb81eDevang Patel    EmitStopPoint(&S);
8249198126067a447f8aaccf9fff09be294c8bcb81eDevang Patel
825e4ea1f42c97a436df3e0ae8e129e6bc624ee6790Ted Kremenek  for (DeclStmt::const_decl_iterator I = S.decl_begin(), E = S.decl_end();
826e4ea1f42c97a436df3e0ae8e129e6bc624ee6790Ted Kremenek       I != E; ++I)
827e4ea1f42c97a436df3e0ae8e129e6bc624ee6790Ted Kremenek    EmitDecl(**I);
8286fa5f0943a84233b2e1ec9716eae55643225bfd4Chris Lattner}
829da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner
8300912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbarvoid CodeGenFunction::EmitBreakStmt(const BreakStmt &S) {
831da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner  assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!");
832da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner
8330912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // If this code is reachable then emit a stop point (if generating
8340912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // debug info). We have to do this ourselves because we are on the
8350912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // "simple" statement path.
8360912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  if (HaveInsertPoint())
8370912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar    EmitStopPoint(&S);
838ec9771d57f94cc204491b3174e88069d08cdd684Mike Stump
839f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest Block = BreakContinueStack.back().BreakBlock;
84082d8ef0be44ddba608c1ce5c8b6b48da83bc1821Anders Carlsson  EmitBranchThroughCleanup(Block);
841da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner}
842da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner
8430912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbarvoid CodeGenFunction::EmitContinueStmt(const ContinueStmt &S) {
844da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner  assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
845da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner
8460912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // If this code is reachable then emit a stop point (if generating
8470912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // debug info). We have to do this ourselves because we are on the
8480912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // "simple" statement path.
8490912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  if (HaveInsertPoint())
8500912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar    EmitStopPoint(&S);
851ec9771d57f94cc204491b3174e88069d08cdd684Mike Stump
852f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest Block = BreakContinueStack.back().ContinueBlock;
85382d8ef0be44ddba608c1ce5c8b6b48da83bc1821Anders Carlsson  EmitBranchThroughCleanup(Block);
854da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner}
85551b09f2c528c8460b5465c676173324e44176d62Devang Patel
856c049e4f406a7f7179eba98659044a32508e53289Devang Patel/// EmitCaseStmtRange - If case statement range is not too big then
857c049e4f406a7f7179eba98659044a32508e53289Devang Patel/// add multiple cases to switch instruction, one for each value within
858c049e4f406a7f7179eba98659044a32508e53289Devang Patel/// the range. If range is too big then emit "if" condition check.
859c049e4f406a7f7179eba98659044a32508e53289Devang Patelvoid CodeGenFunction::EmitCaseStmtRange(const CaseStmt &S) {
8604efde8d2c10b091bc9588de18c2c71dca2979e49Daniel Dunbar  assert(S.getRHS() && "Expected RHS value in CaseStmt");
861c049e4f406a7f7179eba98659044a32508e53289Devang Patel
862a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  llvm::APSInt LHS = S.getLHS()->EvaluateKnownConstInt(getContext());
863a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  llvm::APSInt RHS = S.getRHS()->EvaluateKnownConstInt(getContext());
8644efde8d2c10b091bc9588de18c2c71dca2979e49Daniel Dunbar
86516f23570999cac1fa13597386938450843003840Daniel Dunbar  // Emit the code for this case. We do this first to make sure it is
86616f23570999cac1fa13597386938450843003840Daniel Dunbar  // properly chained from our predecessor before generating the
86716f23570999cac1fa13597386938450843003840Daniel Dunbar  // switch machinery to enter this block.
868f84dcda7e2ab2f6d5be5a8c52d22ef4c442dd762Daniel Dunbar  EmitBlock(createBasicBlock("sw.bb"));
86916f23570999cac1fa13597386938450843003840Daniel Dunbar  llvm::BasicBlock *CaseDest = Builder.GetInsertBlock();
87016f23570999cac1fa13597386938450843003840Daniel Dunbar  EmitStmt(S.getSubStmt());
87116f23570999cac1fa13597386938450843003840Daniel Dunbar
8724efde8d2c10b091bc9588de18c2c71dca2979e49Daniel Dunbar  // If range is empty, do nothing.
8734efde8d2c10b091bc9588de18c2c71dca2979e49Daniel Dunbar  if (LHS.isSigned() ? RHS.slt(LHS) : RHS.ult(LHS))
8744efde8d2c10b091bc9588de18c2c71dca2979e49Daniel Dunbar    return;
875c049e4f406a7f7179eba98659044a32508e53289Devang Patel
876c049e4f406a7f7179eba98659044a32508e53289Devang Patel  llvm::APInt Range = RHS - LHS;
87716f23570999cac1fa13597386938450843003840Daniel Dunbar  // FIXME: parameters such as this should not be hardcoded.
878c049e4f406a7f7179eba98659044a32508e53289Devang Patel  if (Range.ult(llvm::APInt(Range.getBitWidth(), 64))) {
879c049e4f406a7f7179eba98659044a32508e53289Devang Patel    // Range is small enough to add multiple switch instruction cases.
8804efde8d2c10b091bc9588de18c2c71dca2979e49Daniel Dunbar    for (unsigned i = 0, e = Range.getZExtValue() + 1; i != e; ++i) {
88197d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner      SwitchInsn->addCase(Builder.getInt(LHS), CaseDest);
8822d79d0f3ac0ea77b7bdfc3dd11de8cc3ccd91b8cDevang Patel      LHS++;
8832d79d0f3ac0ea77b7bdfc3dd11de8cc3ccd91b8cDevang Patel    }
884c049e4f406a7f7179eba98659044a32508e53289Devang Patel    return;
8851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
8861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
88716f23570999cac1fa13597386938450843003840Daniel Dunbar  // The range is too big. Emit "if" condition into a new block,
88816f23570999cac1fa13597386938450843003840Daniel Dunbar  // making sure to save and restore the current insertion point.
88916f23570999cac1fa13597386938450843003840Daniel Dunbar  llvm::BasicBlock *RestoreBB = Builder.GetInsertBlock();
89016f23570999cac1fa13597386938450843003840Daniel Dunbar
89116f23570999cac1fa13597386938450843003840Daniel Dunbar  // Push this test onto the chain of range checks (which terminates
89216f23570999cac1fa13597386938450843003840Daniel Dunbar  // in the default basic block). The switch's default will be changed
89316f23570999cac1fa13597386938450843003840Daniel Dunbar  // to the top of this chain after switch emission is complete.
89416f23570999cac1fa13597386938450843003840Daniel Dunbar  llvm::BasicBlock *FalseDest = CaseRangeBlock;
89555e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  CaseRangeBlock = createBasicBlock("sw.caserange");
89616f23570999cac1fa13597386938450843003840Daniel Dunbar
89716f23570999cac1fa13597386938450843003840Daniel Dunbar  CurFn->getBasicBlockList().push_back(CaseRangeBlock);
89816f23570999cac1fa13597386938450843003840Daniel Dunbar  Builder.SetInsertPoint(CaseRangeBlock);
899c049e4f406a7f7179eba98659044a32508e53289Devang Patel
900c049e4f406a7f7179eba98659044a32508e53289Devang Patel  // Emit range check.
9011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::Value *Diff =
902578faa837b552403e2002b97fdfbfde14f2448e5Benjamin Kramer    Builder.CreateSub(SwitchInsn->getCondition(), Builder.getInt(LHS));
9031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::Value *Cond =
90497d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner    Builder.CreateICmpULE(Diff, Builder.getInt(Range), "inbounds");
905c049e4f406a7f7179eba98659044a32508e53289Devang Patel  Builder.CreateCondBr(Cond, CaseDest, FalseDest);
906c049e4f406a7f7179eba98659044a32508e53289Devang Patel
90716f23570999cac1fa13597386938450843003840Daniel Dunbar  // Restore the appropriate insertion point.
908a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar  if (RestoreBB)
909a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar    Builder.SetInsertPoint(RestoreBB);
910a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar  else
911a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar    Builder.ClearInsertionPoint();
912c049e4f406a7f7179eba98659044a32508e53289Devang Patel}
9132d79d0f3ac0ea77b7bdfc3dd11de8cc3ccd91b8cDevang Patel
914c049e4f406a7f7179eba98659044a32508e53289Devang Patelvoid CodeGenFunction::EmitCaseStmt(const CaseStmt &S) {
915d66715d83e64b6bfd0bddebe51874b1b7a64abefFariborz Jahanian  // If there is no enclosing switch instance that we're aware of, then this
916d66715d83e64b6bfd0bddebe51874b1b7a64abefFariborz Jahanian  // case statement and its block can be elided.  This situation only happens
917d66715d83e64b6bfd0bddebe51874b1b7a64abefFariborz Jahanian  // when we've constant-folded the switch, are emitting the constant case,
918d66715d83e64b6bfd0bddebe51874b1b7a64abefFariborz Jahanian  // and part of the constant case includes another case statement.  For
919d66715d83e64b6bfd0bddebe51874b1b7a64abefFariborz Jahanian  // instance: switch (4) { case 4: do { case 5: } while (1); }
920303b4f946470a054cea8f91af54008aeb3c09507Fariborz Jahanian  if (!SwitchInsn) {
921303b4f946470a054cea8f91af54008aeb3c09507Fariborz Jahanian    EmitStmt(S.getSubStmt());
922d66715d83e64b6bfd0bddebe51874b1b7a64abefFariborz Jahanian    return;
923303b4f946470a054cea8f91af54008aeb3c09507Fariborz Jahanian  }
924d66715d83e64b6bfd0bddebe51874b1b7a64abefFariborz Jahanian
925b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner  // Handle case ranges.
926c049e4f406a7f7179eba98659044a32508e53289Devang Patel  if (S.getRHS()) {
927c049e4f406a7f7179eba98659044a32508e53289Devang Patel    EmitCaseStmtRange(S);
928c049e4f406a7f7179eba98659044a32508e53289Devang Patel    return;
929c049e4f406a7f7179eba98659044a32508e53289Devang Patel  }
9301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
93197d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner  llvm::ConstantInt *CaseVal =
932a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith    Builder.getInt(S.getLHS()->EvaluateKnownConstInt(getContext()));
93397d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner
934421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner  // If the body of the case is just a 'break', and if there was no fallthrough,
935421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner  // try to not emit an empty block.
93617083601193528a5d3770b688ffdf700f7df3c45Chad Rosier  if ((CGM.getCodeGenOpts().OptimizationLevel > 0) &&
93717083601193528a5d3770b688ffdf700f7df3c45Chad Rosier      isa<BreakStmt>(S.getSubStmt())) {
938b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner    JumpDest Block = BreakContinueStack.back().BreakBlock;
9396f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
940b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner    // Only do this optimization if there are no cleanups that need emitting.
941b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner    if (isObviouslyBranchWithoutCleanups(Block)) {
94297d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner      SwitchInsn->addCase(CaseVal, Block.getBlock());
943421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner
944421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner      // If there was a fallthrough into this case, make sure to redirect it to
945421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner      // the end of the switch as well.
946421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner      if (Builder.GetInsertBlock()) {
947421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner        Builder.CreateBr(Block.getBlock());
948421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner        Builder.ClearInsertionPoint();
949421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner      }
950b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner      return;
951b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner    }
952b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner  }
9536f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
954f84dcda7e2ab2f6d5be5a8c52d22ef4c442dd762Daniel Dunbar  EmitBlock(createBasicBlock("sw.bb"));
955c049e4f406a7f7179eba98659044a32508e53289Devang Patel  llvm::BasicBlock *CaseDest = Builder.GetInsertBlock();
95697d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner  SwitchInsn->addCase(CaseVal, CaseDest);
9571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9585512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  // Recursively emitting the statement is acceptable, but is not wonderful for
9595512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  // code where we have many case statements nested together, i.e.:
9605512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  //  case 1:
9615512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  //    case 2:
9625512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  //      case 3: etc.
9635512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  // Handling this recursively will create a new block for each case statement
9645512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  // that falls through to the next case which is IR intensive.  It also causes
9655512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  // deep recursion which can run into stack depth limitations.  Handle
9665512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  // sequential non-range case statements specially.
9675512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  const CaseStmt *CurCase = &S;
9685512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  const CaseStmt *NextCase = dyn_cast<CaseStmt>(S.getSubStmt());
9695512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner
97097d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner  // Otherwise, iteratively add consecutive cases to this switch stmt.
9715512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  while (NextCase && NextCase->getRHS() == 0) {
9725512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner    CurCase = NextCase;
97397d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner    llvm::ConstantInt *CaseVal =
974a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith      Builder.getInt(CurCase->getLHS()->EvaluateKnownConstInt(getContext()));
97597d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner    SwitchInsn->addCase(CaseVal, CaseDest);
9765512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner    NextCase = dyn_cast<CaseStmt>(CurCase->getSubStmt());
9775512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  }
9781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9795512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  // Normal default recursion for non-cases.
9805512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  EmitStmt(CurCase->getSubStmt());
98151b09f2c528c8460b5465c676173324e44176d62Devang Patel}
98251b09f2c528c8460b5465c676173324e44176d62Devang Patel
98351b09f2c528c8460b5465c676173324e44176d62Devang Patelvoid CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S) {
98416f23570999cac1fa13597386938450843003840Daniel Dunbar  llvm::BasicBlock *DefaultBlock = SwitchInsn->getDefaultDest();
9851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(DefaultBlock->empty() &&
98655e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar         "EmitDefaultStmt: Default block already defined?");
98716f23570999cac1fa13597386938450843003840Daniel Dunbar  EmitBlock(DefaultBlock);
98851b09f2c528c8460b5465c676173324e44176d62Devang Patel  EmitStmt(S.getSubStmt());
98951b09f2c528c8460b5465c676173324e44176d62Devang Patel}
99051b09f2c528c8460b5465c676173324e44176d62Devang Patel
991fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// CollectStatementsForCase - Given the body of a 'switch' statement and a
992fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// constant value that is being switched on, see if we can dead code eliminate
993fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// the body of the switch to a simple series of statements to emit.  Basically,
994fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// on a switch (5) we want to find these statements:
995fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner///    case 5:
996fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner///      printf(...);    <--
997fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner///      ++i;            <--
998fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner///      break;
999fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner///
1000fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// and add them to the ResultStmts vector.  If it is unsafe to do this
1001fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// transformation (for example, one of the elided statements contains a label
1002fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// that might be jumped to), return CSFC_Failure.  If we handled it and 'S'
1003fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// should include statements after it (e.g. the printf() line is a substmt of
1004fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// the case) then return CSFC_FallThrough.  If we handled it and found a break
1005fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// statement, then return CSFC_Success.
1006fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner///
1007fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// If Case is non-null, then we are looking for the specified case, checking
1008fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// that nothing we jump over contains labels.  If Case is null, then we found
1009fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// the case and are looking for the break.
1010fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner///
1011fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// If the recursive walk actually finds our Case, then we set FoundCase to
1012fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// true.
1013fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner///
1014fda0f1f5a278548b012401be07e287c1697fc41cChris Lattnerenum CSFC_Result { CSFC_Failure, CSFC_FallThrough, CSFC_Success };
1015fda0f1f5a278548b012401be07e287c1697fc41cChris Lattnerstatic CSFC_Result CollectStatementsForCase(const Stmt *S,
1016fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner                                            const SwitchCase *Case,
1017fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner                                            bool &FoundCase,
10185f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                              SmallVectorImpl<const Stmt*> &ResultStmts) {
10193858938b043bac2f046304ff99a54905acdcc6ddChris Lattner  // If this is a null statement, just succeed.
10203858938b043bac2f046304ff99a54905acdcc6ddChris Lattner  if (S == 0)
10213858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    return Case ? CSFC_Success : CSFC_FallThrough;
10226f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
1023fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // If this is the switchcase (case 4: or default) that we're looking for, then
1024fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // we're in business.  Just add the substatement.
1025fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  if (const SwitchCase *SC = dyn_cast<SwitchCase>(S)) {
1026fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    if (S == Case) {
1027fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      FoundCase = true;
1028fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      return CollectStatementsForCase(SC->getSubStmt(), 0, FoundCase,
1029fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner                                      ResultStmts);
1030fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    }
10316f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
1032fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    // Otherwise, this is some other case or default statement, just ignore it.
1033fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    return CollectStatementsForCase(SC->getSubStmt(), Case, FoundCase,
1034fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner                                    ResultStmts);
1035fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  }
10363858938b043bac2f046304ff99a54905acdcc6ddChris Lattner
10373858938b043bac2f046304ff99a54905acdcc6ddChris Lattner  // If we are in the live part of the code and we found our break statement,
10383858938b043bac2f046304ff99a54905acdcc6ddChris Lattner  // return a success!
10393858938b043bac2f046304ff99a54905acdcc6ddChris Lattner  if (Case == 0 && isa<BreakStmt>(S))
10403858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    return CSFC_Success;
10416f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
10423858938b043bac2f046304ff99a54905acdcc6ddChris Lattner  // If this is a switch statement, then it might contain the SwitchCase, the
10433858938b043bac2f046304ff99a54905acdcc6ddChris Lattner  // break, or neither.
10443858938b043bac2f046304ff99a54905acdcc6ddChris Lattner  if (const CompoundStmt *CS = dyn_cast<CompoundStmt>(S)) {
10453858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    // Handle this as two cases: we might be looking for the SwitchCase (if so
10463858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    // the skipped statements must be skippable) or we might already have it.
10473858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    CompoundStmt::const_body_iterator I = CS->body_begin(), E = CS->body_end();
10483858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    if (Case) {
10493f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner      // Keep track of whether we see a skipped declaration.  The code could be
10503f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner      // using the declaration even if it is skipped, so we can't optimize out
10513f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner      // the decl if the kept statements might refer to it.
10523f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner      bool HadSkippedDecl = false;
10536f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
10543858938b043bac2f046304ff99a54905acdcc6ddChris Lattner      // If we're looking for the case, just see if we can skip each of the
10553858938b043bac2f046304ff99a54905acdcc6ddChris Lattner      // substatements.
10563858938b043bac2f046304ff99a54905acdcc6ddChris Lattner      for (; Case && I != E; ++I) {
10574d509341bd5db06a517daa311379f52bb540bc34Eli Friedman        HadSkippedDecl |= isa<DeclStmt>(*I);
10586f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
10593858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        switch (CollectStatementsForCase(*I, Case, FoundCase, ResultStmts)) {
10603858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        case CSFC_Failure: return CSFC_Failure;
10613858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        case CSFC_Success:
10623858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          // A successful result means that either 1) that the statement doesn't
10633858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          // have the case and is skippable, or 2) does contain the case value
10649467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner          // and also contains the break to exit the switch.  In the later case,
10659467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner          // we just verify the rest of the statements are elidable.
10669467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner          if (FoundCase) {
10673f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner            // If we found the case and skipped declarations, we can't do the
10683f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner            // optimization.
10693f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner            if (HadSkippedDecl)
10703f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner              return CSFC_Failure;
10716f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
10729467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner            for (++I; I != E; ++I)
10739467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner              if (CodeGenFunction::ContainsLabel(*I, true))
10749467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner                return CSFC_Failure;
10759467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner            return CSFC_Success;
10769467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner          }
10773858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          break;
10783858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        case CSFC_FallThrough:
10793858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          // If we have a fallthrough condition, then we must have found the
10803858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          // case started to include statements.  Consider the rest of the
10813858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          // statements in the compound statement as candidates for inclusion.
10823858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          assert(FoundCase && "Didn't find case but returned fallthrough?");
10833858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          // We recursively found Case, so we're not looking for it anymore.
10843858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          Case = 0;
10856f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
10863f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner          // If we found the case and skipped declarations, we can't do the
10873f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner          // optimization.
10883f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner          if (HadSkippedDecl)
10893f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner            return CSFC_Failure;
10903858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          break;
10913858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        }
10923858938b043bac2f046304ff99a54905acdcc6ddChris Lattner      }
10933858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    }
10943858938b043bac2f046304ff99a54905acdcc6ddChris Lattner
10953858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    // If we have statements in our range, then we know that the statements are
10963858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    // live and need to be added to the set of statements we're tracking.
10973858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    for (; I != E; ++I) {
10983858938b043bac2f046304ff99a54905acdcc6ddChris Lattner      switch (CollectStatementsForCase(*I, 0, FoundCase, ResultStmts)) {
10993858938b043bac2f046304ff99a54905acdcc6ddChris Lattner      case CSFC_Failure: return CSFC_Failure;
11003858938b043bac2f046304ff99a54905acdcc6ddChris Lattner      case CSFC_FallThrough:
11013858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        // A fallthrough result means that the statement was simple and just
11023858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        // included in ResultStmt, keep adding them afterwards.
11033858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        break;
11043858938b043bac2f046304ff99a54905acdcc6ddChris Lattner      case CSFC_Success:
11053858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        // A successful result means that we found the break statement and
11063858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        // stopped statement inclusion.  We just ensure that any leftover stmts
11073858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        // are skippable and return success ourselves.
11083858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        for (++I; I != E; ++I)
11093858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          if (CodeGenFunction::ContainsLabel(*I, true))
11103858938b043bac2f046304ff99a54905acdcc6ddChris Lattner            return CSFC_Failure;
11113858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        return CSFC_Success;
11126f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier      }
11133858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    }
11146f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
11153858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    return Case ? CSFC_Success : CSFC_FallThrough;
11163858938b043bac2f046304ff99a54905acdcc6ddChris Lattner  }
11173858938b043bac2f046304ff99a54905acdcc6ddChris Lattner
1118fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // Okay, this is some other statement that we don't handle explicitly, like a
1119fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // for statement or increment etc.  If we are skipping over this statement,
1120fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // just verify it doesn't have labels, which would make it invalid to elide.
1121fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  if (Case) {
11223f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner    if (CodeGenFunction::ContainsLabel(S, true))
1123fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      return CSFC_Failure;
1124fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    return CSFC_Success;
1125fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  }
11266f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
1127fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // Otherwise, we want to include this statement.  Everything is cool with that
1128fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // so long as it doesn't contain a break out of the switch we're in.
1129fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  if (CodeGenFunction::containsBreak(S)) return CSFC_Failure;
11306f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
1131fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // Otherwise, everything is great.  Include the statement and tell the caller
1132fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // that we fall through and include the next statement as well.
1133fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  ResultStmts.push_back(S);
1134fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  return CSFC_FallThrough;
1135fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner}
1136fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner
1137fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// FindCaseStatementsForValue - Find the case statement being jumped to and
1138fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// then invoke CollectStatementsForCase to find the list of statements to emit
1139fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// for a switch on constant.  See the comment above CollectStatementsForCase
1140fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// for more details.
1141fda0f1f5a278548b012401be07e287c1697fc41cChris Lattnerstatic bool FindCaseStatementsForValue(const SwitchStmt &S,
1142e1ecdc168175719d74e112bcacd4aae5e12d4631Richard Trieu                                       const llvm::APSInt &ConstantCondValue,
11435f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                SmallVectorImpl<const Stmt*> &ResultStmts,
1144fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner                                       ASTContext &C) {
1145fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // First step, find the switch case that is being branched to.  We can do this
1146fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // efficiently by scanning the SwitchCase list.
1147fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  const SwitchCase *Case = S.getSwitchCaseList();
1148fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  const DefaultStmt *DefaultCase = 0;
11496f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
1150fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  for (; Case; Case = Case->getNextSwitchCase()) {
1151fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    // It's either a default or case.  Just remember the default statement in
1152fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    // case we're not jumping to any numbered cases.
1153fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    if (const DefaultStmt *DS = dyn_cast<DefaultStmt>(Case)) {
1154fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      DefaultCase = DS;
1155fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      continue;
1156fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    }
11576f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
1158fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    // Check to see if this case is the one we're looking for.
1159fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    const CaseStmt *CS = cast<CaseStmt>(Case);
1160fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    // Don't handle case ranges yet.
1161fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    if (CS->getRHS()) return false;
11626f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
1163fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    // If we found our case, remember it as 'case'.
1164a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith    if (CS->getLHS()->EvaluateKnownConstInt(C) == ConstantCondValue)
1165fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      break;
1166fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  }
11676f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
1168fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // If we didn't find a matching case, we use a default if it exists, or we
1169fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // elide the whole switch body!
1170fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  if (Case == 0) {
1171fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    // It is safe to elide the body of the switch if it doesn't contain labels
1172fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    // etc.  If it is safe, return successfully with an empty ResultStmts list.
1173fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    if (DefaultCase == 0)
1174fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      return !CodeGenFunction::ContainsLabel(&S);
1175fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    Case = DefaultCase;
1176fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  }
1177fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner
1178fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // Ok, we know which case is being jumped to, try to collect all the
1179fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // statements that follow it.  This can fail for a variety of reasons.  Also,
1180fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // check to see that the recursive walk actually found our case statement.
1181fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // Insane cases like this can fail to find it in the recursive walk since we
1182fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // don't handle every stmt kind:
1183fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // switch (4) {
1184fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  //   while (1) {
1185fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  //     case 4: ...
1186fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  bool FoundCase = false;
1187fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  return CollectStatementsForCase(S.getBody(), Case, FoundCase,
1188fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner                                  ResultStmts) != CSFC_Failure &&
1189fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner         FoundCase;
1190fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner}
1191fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner
119251b09f2c528c8460b5465c676173324e44176d62Devang Patelvoid CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) {
1193f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest SwitchExit = getJumpDestInCurrentScope("sw.epilog");
1194f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1195f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  RunCleanupsScope ConditionScope(*this);
1196d3d5301c44138b92bf01286183f5bf310cdd37cfDouglas Gregor
1197d3d5301c44138b92bf01286183f5bf310cdd37cfDouglas Gregor  if (S.getConditionVariable())
1198b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall    EmitAutoVarDecl(*S.getConditionVariable());
1199d3d5301c44138b92bf01286183f5bf310cdd37cfDouglas Gregor
1200985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian  // Handle nested switch statements.
1201985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian  llvm::SwitchInst *SavedSwitchInsn = SwitchInsn;
1202985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian  llvm::BasicBlock *SavedCRBlock = CaseRangeBlock;
1203985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian
1204fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // See if we can constant fold the condition of the switch and therefore only
1205fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // emit the live case statement (if any) of the switch.
1206e1ecdc168175719d74e112bcacd4aae5e12d4631Richard Trieu  llvm::APSInt ConstantCondValue;
1207fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  if (ConstantFoldsToSimpleInteger(S.getCond(), ConstantCondValue)) {
12085f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<const Stmt*, 4> CaseStmts;
1209fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    if (FindCaseStatementsForValue(S, ConstantCondValue, CaseStmts,
1210fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner                                   getContext())) {
1211fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      RunCleanupsScope ExecutedScope(*this);
1212fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner
1213985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian      // At this point, we are no longer "within" a switch instance, so
1214985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian      // we can temporarily enforce this to ensure that any embedded case
1215985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian      // statements are not emitted.
1216985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian      SwitchInsn = 0;
1217985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian
1218fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      // Okay, we can dead code eliminate everything except this case.  Emit the
1219fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      // specified series of statements and we're good.
1220fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      for (unsigned i = 0, e = CaseStmts.size(); i != e; ++i)
1221fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner        EmitStmt(CaseStmts[i]);
1222985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian
1223fc65ec83f6719f06e460b61f83a3ba1e80c18d04Eric Christopher      // Now we want to restore the saved switch instance so that nested
1224fc65ec83f6719f06e460b61f83a3ba1e80c18d04Eric Christopher      // switches continue to function properly
1225985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian      SwitchInsn = SavedSwitchInsn;
1226985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian
1227fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      return;
1228fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    }
1229fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  }
12306f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
123151b09f2c528c8460b5465c676173324e44176d62Devang Patel  llvm::Value *CondV = EmitScalarExpr(S.getCond());
123251b09f2c528c8460b5465c676173324e44176d62Devang Patel
123316f23570999cac1fa13597386938450843003840Daniel Dunbar  // Create basic block to hold stuff that comes after switch
123416f23570999cac1fa13597386938450843003840Daniel Dunbar  // statement. We also need to create a default block now so that
123516f23570999cac1fa13597386938450843003840Daniel Dunbar  // explicit case ranges tests can have a place to jump to on
123616f23570999cac1fa13597386938450843003840Daniel Dunbar  // failure.
123755e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  llvm::BasicBlock *DefaultBlock = createBasicBlock("sw.default");
123816f23570999cac1fa13597386938450843003840Daniel Dunbar  SwitchInsn = Builder.CreateSwitch(CondV, DefaultBlock);
123916f23570999cac1fa13597386938450843003840Daniel Dunbar  CaseRangeBlock = DefaultBlock;
124051b09f2c528c8460b5465c676173324e44176d62Devang Patel
12410912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // Clear the insertion point to indicate we are in unreachable code.
12420912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  Builder.ClearInsertionPoint();
1243d28a80d64616b66c91d28bb4c08ca2d8c594de4eEli Friedman
1244e9b8c0a38549692f1b8f688c05c35442fc620865Devang Patel  // All break statements jump to NextBlock. If BreakContinueStack is non empty
1245e9b8c0a38549692f1b8f688c05c35442fc620865Devang Patel  // then reuse last ContinueBlock.
1246f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest OuterContinue;
1247e4b6d342c29d5cb9d311756100df1603810fa892Anders Carlsson  if (!BreakContinueStack.empty())
1248f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    OuterContinue = BreakContinueStack.back().ContinueBlock;
1249e4b6d342c29d5cb9d311756100df1603810fa892Anders Carlsson
1250f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  BreakContinueStack.push_back(BreakContinue(SwitchExit, OuterContinue));
125151b09f2c528c8460b5465c676173324e44176d62Devang Patel
125251b09f2c528c8460b5465c676173324e44176d62Devang Patel  // Emit switch body.
125351b09f2c528c8460b5465c676173324e44176d62Devang Patel  EmitStmt(S.getBody());
12541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1255e4b6d342c29d5cb9d311756100df1603810fa892Anders Carlsson  BreakContinueStack.pop_back();
125651b09f2c528c8460b5465c676173324e44176d62Devang Patel
125716f23570999cac1fa13597386938450843003840Daniel Dunbar  // Update the default block in case explicit case range tests have
125816f23570999cac1fa13597386938450843003840Daniel Dunbar  // been chained on top.
1259ab14ae2ab16088b6a7f69eac6e152c3e9f9ea01bStepan Dyatkovskiy  SwitchInsn->setDefaultDest(CaseRangeBlock);
12601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1261f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // If a default was never emitted:
126216f23570999cac1fa13597386938450843003840Daniel Dunbar  if (!DefaultBlock->getParent()) {
1263f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    // If we have cleanups, emit the default block so that there's a
1264f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    // place to jump through the cleanups from.
1265f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    if (ConditionScope.requiresCleanups()) {
1266f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      EmitBlock(DefaultBlock);
1267f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1268f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    // Otherwise, just forward the default block to the switch end.
1269f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    } else {
1270ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      DefaultBlock->replaceAllUsesWith(SwitchExit.getBlock());
1271f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      delete DefaultBlock;
1272f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    }
127316f23570999cac1fa13597386938450843003840Daniel Dunbar  }
127416f23570999cac1fa13597386938450843003840Daniel Dunbar
1275ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  ConditionScope.ForceCleanup();
1276ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
127716f23570999cac1fa13597386938450843003840Daniel Dunbar  // Emit continuation.
1278ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(SwitchExit.getBlock(), true);
127951b09f2c528c8460b5465c676173324e44176d62Devang Patel
128051b09f2c528c8460b5465c676173324e44176d62Devang Patel  SwitchInsn = SavedSwitchInsn;
1281c049e4f406a7f7179eba98659044a32508e53289Devang Patel  CaseRangeBlock = SavedCRBlock;
128251b09f2c528c8460b5465c676173324e44176d62Devang Patel}
1283fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson
12842819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattnerstatic std::string
1285444be7366d0a1e172c0290a1ea54c1cb16b5947cDaniel DunbarSimplifyConstraint(const char *Constraint, const TargetInfo &Target,
12865f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                 SmallVectorImpl<TargetInfo::ConstraintInfo> *OutCons=0) {
1287fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  std::string Result;
12881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1289fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  while (*Constraint) {
1290fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    switch (*Constraint) {
1291fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    default:
1292002333f8b2cf1a8614e532f6ce366b21af85142cStuart Hastings      Result += Target.convertConstraint(Constraint);
1293fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson      break;
1294fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    // Ignore these
1295fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    case '*':
1296fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    case '?':
1297fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    case '!':
1298ef44e1110711276ffffe4b22d4ba0cebd49cb330John Thompson    case '=': // Will see this and the following in mult-alt constraints.
1299ef44e1110711276ffffe4b22d4ba0cebd49cb330John Thompson    case '+':
1300ef44e1110711276ffffe4b22d4ba0cebd49cb330John Thompson      break;
1301e6b3dbae38f62164cab3989380cd940242d80120Ulrich Weigand    case '#': // Ignore the rest of the constraint alternative.
1302e6b3dbae38f62164cab3989380cd940242d80120Ulrich Weigand      while (Constraint[1] && Constraint[1] != ',')
1303e6b3dbae38f62164cab3989380cd940242d80120Ulrich Weigand	Constraint++;
1304e6b3dbae38f62164cab3989380cd940242d80120Ulrich Weigand      break;
13052f474ea9ef7505df5d092287c48c19974222293bJohn Thompson    case ',':
13062f474ea9ef7505df5d092287c48c19974222293bJohn Thompson      Result += "|";
1307fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson      break;
1308fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    case 'g':
1309fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson      Result += "imr";
1310fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson      break;
1311300fb5d0ef7edc87f3fdba17fc8b1184013b35aeAnders Carlsson    case '[': {
13122819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner      assert(OutCons &&
1313300fb5d0ef7edc87f3fdba17fc8b1184013b35aeAnders Carlsson             "Must pass output names to constraints with a symbolic name");
1314300fb5d0ef7edc87f3fdba17fc8b1184013b35aeAnders Carlsson      unsigned Index;
13151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      bool result = Target.resolveSymbolicName(Constraint,
13162819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner                                               &(*OutCons)[0],
13172819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner                                               OutCons->size(), Index);
1318cbf40f913aa2aa5de6e0540fed209405d00a2c69Chris Lattner      assert(result && "Could not resolve symbolic name"); (void)result;
1319300fb5d0ef7edc87f3fdba17fc8b1184013b35aeAnders Carlsson      Result += llvm::utostr(Index);
1320300fb5d0ef7edc87f3fdba17fc8b1184013b35aeAnders Carlsson      break;
1321300fb5d0ef7edc87f3fdba17fc8b1184013b35aeAnders Carlsson    }
1322fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    }
13231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1324fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    Constraint++;
1325fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  }
13261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1327fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  return Result;
1328fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson}
1329fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson
133003117d1b2e32d18652401b12d9049871992bf3adRafael Espindola/// AddVariableConstraints - Look at AsmExpr and if it is a variable declared
133103117d1b2e32d18652401b12d9049871992bf3adRafael Espindola/// as using a particular register add that as a constraint that will be used
133203117d1b2e32d18652401b12d9049871992bf3adRafael Espindola/// in this asm stmt.
13330ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindolastatic std::string
133403117d1b2e32d18652401b12d9049871992bf3adRafael EspindolaAddVariableConstraints(const std::string &Constraint, const Expr &AsmExpr,
133503117d1b2e32d18652401b12d9049871992bf3adRafael Espindola                       const TargetInfo &Target, CodeGenModule &CGM,
1336a23b91d542e336be5051ac54f394e860fb756911Chad Rosier                       const AsmStmt &Stmt) {
13370ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola  const DeclRefExpr *AsmDeclRef = dyn_cast<DeclRefExpr>(&AsmExpr);
13380ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola  if (!AsmDeclRef)
13390ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola    return Constraint;
13400ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola  const ValueDecl &Value = *AsmDeclRef->getDecl();
13410ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola  const VarDecl *Variable = dyn_cast<VarDecl>(&Value);
13420ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola  if (!Variable)
13430ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola    return Constraint;
1344a43ef3e0511dc48d98d61598163c9deddc09cb9cEli Friedman  if (Variable->getStorageClass() != SC_Register)
1345a43ef3e0511dc48d98d61598163c9deddc09cb9cEli Friedman    return Constraint;
13460ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola  AsmLabelAttr *Attr = Variable->getAttr<AsmLabelAttr>();
13470ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola  if (!Attr)
13480ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola    return Constraint;
13495f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef Register = Attr->getLabel();
1350baf86955a9a390f2643a1ea9806832eb4a92f716Rafael Espindola  assert(Target.isValidGCCRegisterName(Register));
1351e3e07a5b3490bc2977859d56bac211afac2236fbEric Christopher  // We're using validateOutputConstraint here because we only care if
1352e3e07a5b3490bc2977859d56bac211afac2236fbEric Christopher  // this is a register constraint.
1353e3e07a5b3490bc2977859d56bac211afac2236fbEric Christopher  TargetInfo::ConstraintInfo Info(Constraint, "");
1354e3e07a5b3490bc2977859d56bac211afac2236fbEric Christopher  if (Target.validateOutputConstraint(Info) &&
1355e3e07a5b3490bc2977859d56bac211afac2236fbEric Christopher      !Info.allowsRegister()) {
13560ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola    CGM.ErrorUnsupported(&Stmt, "__asm__");
13570ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola    return Constraint;
13580ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola  }
135943fec879a527c74ff01d8aa2bf94a12432249fc7Eric Christopher  // Canonicalize the register here before returning it.
136043fec879a527c74ff01d8aa2bf94a12432249fc7Eric Christopher  Register = Target.getNormalizedGCCRegisterName(Register);
13610ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola  return "{" + Register.str() + "}";
13620ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola}
13630ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola
13646d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedmanllvm::Value*
136542b60551eff3a424e191b293bfd606559dc96bffChad RosierCodeGenFunction::EmitAsmInputLValue(const TargetInfo::ConstraintInfo &Info,
13666d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman                                    LValue InputValue, QualType InputType,
13676d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman                                    std::string &ConstraintStr) {
1368634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson  llvm::Value *Arg;
13691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Info.allowsRegister() || !Info.allowsMemory()) {
13709d232c884ea9872d6555df0fd7359699819bc1f1John McCall    if (CodeGenFunction::hasScalarEvaluationKind(InputType)) {
1371545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall      Arg = EmitLoadOfLValue(InputValue).getScalarVal();
1372634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson    } else {
13732acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner      llvm::Type *Ty = ConvertType(InputType);
137425a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow      uint64_t Size = CGM.getDataLayout().getTypeSizeInBits(Ty);
1375ebaae2a3103b443d50444d335ab5ab0ff7680da2Anders Carlsson      if (Size <= 64 && llvm::isPowerOf2_64(Size)) {
1376d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall        Ty = llvm::IntegerType::get(getLLVMContext(), Size);
1377ebaae2a3103b443d50444d335ab5ab0ff7680da2Anders Carlsson        Ty = llvm::PointerType::getUnqual(Ty);
13781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13796d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman        Arg = Builder.CreateLoad(Builder.CreateBitCast(InputValue.getAddress(),
13806d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman                                                       Ty));
1381ebaae2a3103b443d50444d335ab5ab0ff7680da2Anders Carlsson      } else {
13826d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman        Arg = InputValue.getAddress();
1383ebaae2a3103b443d50444d335ab5ab0ff7680da2Anders Carlsson        ConstraintStr += '*';
1384ebaae2a3103b443d50444d335ab5ab0ff7680da2Anders Carlsson      }
1385634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson    }
1386634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson  } else {
13876d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman    Arg = InputValue.getAddress();
1388634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson    ConstraintStr += '*';
1389634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson  }
13901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1391634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson  return Arg;
1392634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson}
1393634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson
139442b60551eff3a424e191b293bfd606559dc96bffChad Rosierllvm::Value* CodeGenFunction::EmitAsmInput(
13956d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman                                         const TargetInfo::ConstraintInfo &Info,
13966d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman                                           const Expr *InputExpr,
13976d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman                                           std::string &ConstraintStr) {
13986d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman  if (Info.allowsRegister() || !Info.allowsMemory())
13999d232c884ea9872d6555df0fd7359699819bc1f1John McCall    if (CodeGenFunction::hasScalarEvaluationKind(InputExpr->getType()))
14006d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman      return EmitScalarExpr(InputExpr);
14016d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman
14026d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman  InputExpr = InputExpr->IgnoreParenNoopCasts(getContext());
14036d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman  LValue Dest = EmitLValue(InputExpr);
140442b60551eff3a424e191b293bfd606559dc96bffChad Rosier  return EmitAsmInputLValue(Info, Dest, InputExpr->getType(), ConstraintStr);
14056d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman}
14066d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman
140747fc7e93b6af9e6a967231133a2f4bc626eeed26Chris Lattner/// getAsmSrcLocInfo - Return the !srcloc metadata node to attach to an inline
14085d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner/// asm call instruction.  The !srcloc MDNode contains a list of constant
14095d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner/// integers which are the source locations of the start of each line in the
14105d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner/// asm.
141147fc7e93b6af9e6a967231133a2f4bc626eeed26Chris Lattnerstatic llvm::MDNode *getAsmSrcLocInfo(const StringLiteral *Str,
141247fc7e93b6af9e6a967231133a2f4bc626eeed26Chris Lattner                                      CodeGenFunction &CGF) {
14135f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Value *, 8> Locs;
14145d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner  // Add the location of the first line to the MDNode.
14155d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner  Locs.push_back(llvm::ConstantInt::get(CGF.Int32Ty,
14165d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner                                        Str->getLocStart().getRawEncoding()));
14175f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef StrVal = Str->getString();
14185d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner  if (!StrVal.empty()) {
14195d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner    const SourceManager &SM = CGF.CGM.getContext().getSourceManager();
14204e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    const LangOptions &LangOpts = CGF.CGM.getLangOpts();
14216f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
14225d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner    // Add the location of the start of each subsequent line of the asm to the
14235d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner    // MDNode.
14245d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner    for (unsigned i = 0, e = StrVal.size()-1; i != e; ++i) {
14255d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner      if (StrVal[i] != '\n') continue;
14265d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner      SourceLocation LineLoc = Str->getLocationOfByte(i+1, SM, LangOpts,
14275d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner                                                      CGF.Target);
14285d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner      Locs.push_back(llvm::ConstantInt::get(CGF.Int32Ty,
14295d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner                                            LineLoc.getRawEncoding()));
14305d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner    }
14316f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier  }
14326f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
14336f141659cab11109d9931d92d0988f8850778de3Jay Foad  return llvm::MDNode::get(CGF.getLLVMContext(), Locs);
143447fc7e93b6af9e6a967231133a2f4bc626eeed26Chris Lattner}
143547fc7e93b6af9e6a967231133a2f4bc626eeed26Chris Lattner
1436a23b91d542e336be5051ac54f394e860fb756911Chad Rosiervoid CodeGenFunction::EmitAsmStmt(const AsmStmt &S) {
1437be3ace834ee7438915e73d2115365d57d03ceb99Chad Rosier  // Assemble the final asm string.
1438da083b2ce8db27ce6e508cb77cb12c0fc8b7cad9Chad Rosier  std::string AsmString = S.generateAsmString(getContext());
14391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1440481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner  // Get all the output and input constraints together.
14415f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos;
14425f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos;
1443481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner
14441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) {
1445481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner    TargetInfo::ConstraintInfo Info(S.getOutputConstraint(i),
1446481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner                                    S.getOutputName(i));
1447b992259f7790d3fb9fc5c2eb7182d7af9d64f9acChris Lattner    bool IsValid = Target.validateOutputConstraint(Info); (void)IsValid;
1448b992259f7790d3fb9fc5c2eb7182d7af9d64f9acChris Lattner    assert(IsValid && "Failed to parse output constraint");
1449481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner    OutputConstraintInfos.push_back(Info);
14501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
14511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1452481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner  for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) {
1453481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner    TargetInfo::ConstraintInfo Info(S.getInputConstraint(i),
1454481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner                                    S.getInputName(i));
1455b992259f7790d3fb9fc5c2eb7182d7af9d64f9acChris Lattner    bool IsValid = Target.validateInputConstraint(OutputConstraintInfos.data(),
1456b992259f7790d3fb9fc5c2eb7182d7af9d64f9acChris Lattner                                                  S.getNumOutputs(), Info);
1457b992259f7790d3fb9fc5c2eb7182d7af9d64f9acChris Lattner    assert(IsValid && "Failed to parse input constraint"); (void)IsValid;
1458481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner    InputConstraintInfos.push_back(Info);
1459481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner  }
14601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1461fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  std::string Constraints;
14621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1463ede9d900809c4fd0298d52f5a63088ecb8302275Chris Lattner  std::vector<LValue> ResultRegDests;
1464ede9d900809c4fd0298d52f5a63088ecb8302275Chris Lattner  std::vector<QualType> ResultRegQualTys;
1465ef6de3da8572607f786303c07150daa6e140ab19Jay Foad  std::vector<llvm::Type *> ResultRegTypes;
1466ef6de3da8572607f786303c07150daa6e140ab19Jay Foad  std::vector<llvm::Type *> ResultTruncRegTypes;
1467d1c0c940ebc8e55d5c4672f61abedf87d6ea36f4Chad Rosier  std::vector<llvm::Type *> ArgTypes;
1468fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  std::vector<llvm::Value*> Args;
1469f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson
1470f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson  // Keep track of inout constraints.
1471f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson  std::string InOutConstraints;
1472f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson  std::vector<llvm::Value*> InOutArgs;
14739cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  std::vector<llvm::Type*> InOutArgTypes;
147403eb543cf7ebee463b33b5802b83ac92c21770cfAnders Carlsson
14751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) {
1476481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner    TargetInfo::ConstraintInfo &Info = OutputConstraintInfos[i];
147703eb543cf7ebee463b33b5802b83ac92c21770cfAnders Carlsson
1478fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    // Simplify the output constraint.
1479481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner    std::string OutputConstraint(S.getOutputConstraint(i));
1480a5694b8b0096215137bf1c273764ec93ac4898fdLauro Ramos Venancio    OutputConstraint = SimplifyConstraint(OutputConstraint.c_str() + 1, Target);
14811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1482810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    const Expr *OutExpr = S.getOutputExpr(i);
1483810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    OutExpr = OutExpr->IgnoreParenNoopCasts(getContext());
14841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1485a18f539628a6506bae6af52eacd541cebefff762Eric Christopher    OutputConstraint = AddVariableConstraints(OutputConstraint, *OutExpr,
1486a18f539628a6506bae6af52eacd541cebefff762Eric Christopher                                              Target, CGM, S);
14870ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola
1488810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    LValue Dest = EmitLValue(OutExpr);
1489ede9d900809c4fd0298d52f5a63088ecb8302275Chris Lattner    if (!Constraints.empty())
1490bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson      Constraints += ',';
1491bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson
1492a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    // If this is a register output, then make the inline asm return it
1493a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    // by-value.  If this is a memory result, return the value by-reference.
14949d232c884ea9872d6555df0fd7359699819bc1f1John McCall    if (!Info.allowsMemory() && hasScalarEvaluationKind(OutExpr->getType())) {
1495a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      Constraints += "=" + OutputConstraint;
1496ede9d900809c4fd0298d52f5a63088ecb8302275Chris Lattner      ResultRegQualTys.push_back(OutExpr->getType());
1497ede9d900809c4fd0298d52f5a63088ecb8302275Chris Lattner      ResultRegDests.push_back(Dest);
1498a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      ResultRegTypes.push_back(ConvertTypeForMem(OutExpr->getType()));
1499a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      ResultTruncRegTypes.push_back(ResultRegTypes.back());
15001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1501a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      // If this output is tied to an input, and if the input is larger, then
1502a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      // we need to set the actual result type of the inline asm node to be the
1503a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      // same as the input type.
1504a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      if (Info.hasMatchingInput()) {
1505ebfc9857de58a326c84629915d9ffe3f36d8cc26Chris Lattner        unsigned InputNo;
1506ebfc9857de58a326c84629915d9ffe3f36d8cc26Chris Lattner        for (InputNo = 0; InputNo != S.getNumInputs(); ++InputNo) {
1507ebfc9857de58a326c84629915d9ffe3f36d8cc26Chris Lattner          TargetInfo::ConstraintInfo &Input = InputConstraintInfos[InputNo];
1508aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner          if (Input.hasTiedOperand() && Input.getTiedOperand() == i)
1509a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner            break;
1510ebfc9857de58a326c84629915d9ffe3f36d8cc26Chris Lattner        }
1511ebfc9857de58a326c84629915d9ffe3f36d8cc26Chris Lattner        assert(InputNo != S.getNumInputs() && "Didn't find matching input!");
15121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1513a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner        QualType InputTy = S.getInputExpr(InputNo)->getType();
1514aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner        QualType OutputType = OutExpr->getType();
15151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1516a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner        uint64_t InputSize = getContext().getTypeSize(InputTy);
1517aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner        if (getContext().getTypeSize(OutputType) < InputSize) {
1518aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner          // Form the asm to return the value as a larger integer or fp type.
1519aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner          ResultRegTypes.back() = ConvertType(InputTy);
1520a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner        }
1521a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      }
1522ef6de3da8572607f786303c07150daa6e140ab19Jay Foad      if (llvm::Type* AdjTy =
15234b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne            getTargetHooks().adjustInlineAsmType(*this, OutputConstraint,
15244b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne                                                 ResultRegTypes.back()))
1525f6e2c2039f76fa58799f6d155892d54fc95755e1Dale Johannesen        ResultRegTypes.back() = AdjTy;
1526fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    } else {
1527fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson      ArgTypes.push_back(Dest.getAddress()->getType());
1528cad3ab611ebd3bee3ce6395d649640047f904cdeAnders Carlsson      Args.push_back(Dest.getAddress());
1529f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson      Constraints += "=*";
1530fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson      Constraints += OutputConstraint;
1531f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson    }
15321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
153344def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner    if (Info.isReadWrite()) {
1534f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson      InOutConstraints += ',';
1535634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson
1536fca9361839ecc53224c764517e62fc0e15166004Anders Carlsson      const Expr *InputExpr = S.getOutputExpr(i);
153742b60551eff3a424e191b293bfd606559dc96bffChad Rosier      llvm::Value *Arg = EmitAsmInputLValue(Info, Dest, InputExpr->getType(),
15386d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman                                            InOutConstraints);
15391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1540acb5310542b82e7acf5f5a91fa619e4b7efeff5cBill Wendling      if (llvm::Type* AdjTy =
1541acb5310542b82e7acf5f5a91fa619e4b7efeff5cBill Wendling            getTargetHooks().adjustInlineAsmType(*this, OutputConstraint,
1542acb5310542b82e7acf5f5a91fa619e4b7efeff5cBill Wendling                                                 Arg->getType()))
1543acb5310542b82e7acf5f5a91fa619e4b7efeff5cBill Wendling        Arg = Builder.CreateBitCast(Arg, AdjTy);
1544acb5310542b82e7acf5f5a91fa619e4b7efeff5cBill Wendling
154544def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner      if (Info.allowsRegister())
15469f2505b934745b18d580ade4dac7b8b16952a30cAnders Carlsson        InOutConstraints += llvm::utostr(i);
15479f2505b934745b18d580ade4dac7b8b16952a30cAnders Carlsson      else
15489f2505b934745b18d580ade4dac7b8b16952a30cAnders Carlsson        InOutConstraints += OutputConstraint;
15492763b3af0a527c3a63cb058b90c22db0b7bcf558Anders Carlsson
1550fca9361839ecc53224c764517e62fc0e15166004Anders Carlsson      InOutArgTypes.push_back(Arg->getType());
1551fca9361839ecc53224c764517e62fc0e15166004Anders Carlsson      InOutArgs.push_back(Arg);
1552f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson    }
1553fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  }
15541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1555fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  unsigned NumConstraints = S.getNumOutputs() + S.getNumInputs();
15561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1557fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) {
1558fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    const Expr *InputExpr = S.getInputExpr(i);
1559fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson
1560481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner    TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i];
1561481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner
1562ede9d900809c4fd0298d52f5a63088ecb8302275Chris Lattner    if (!Constraints.empty())
1563fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson      Constraints += ',';
15641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1565fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    // Simplify the input constraint.
1566481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner    std::string InputConstraint(S.getInputConstraint(i));
1567300fb5d0ef7edc87f3fdba17fc8b1184013b35aeAnders Carlsson    InputConstraint = SimplifyConstraint(InputConstraint.c_str(), Target,
15682819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner                                         &OutputConstraintInfos);
1569fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson
15700ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola    InputConstraint =
157103117d1b2e32d18652401b12d9049871992bf3adRafael Espindola      AddVariableConstraints(InputConstraint,
15720ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola                            *InputExpr->IgnoreParenNoopCasts(getContext()),
15730ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola                            Target, CGM, S);
15740ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola
157542b60551eff3a424e191b293bfd606559dc96bffChad Rosier    llvm::Value *Arg = EmitAsmInput(Info, InputExpr, Constraints);
15761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15774df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner    // If this input argument is tied to a larger output result, extend the
15784df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner    // input to be the same size as the output.  The LLVM backend wants to see
15794df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner    // the input and output of a matching constraint be the same size.  Note
15804df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner    // that GCC does not define what the top bits are here.  We use zext because
15814df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner    // that is usually cheaper, but LLVM IR should really get an anyext someday.
15824df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner    if (Info.hasTiedOperand()) {
15834df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner      unsigned Output = Info.getTiedOperand();
1584aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      QualType OutputType = S.getOutputExpr(Output)->getType();
15854df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner      QualType InputTy = InputExpr->getType();
15861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1587aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      if (getContext().getTypeSize(OutputType) >
15884df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner          getContext().getTypeSize(InputTy)) {
15894df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner        // Use ptrtoint as appropriate so that we can do our extension.
15904df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner        if (isa<llvm::PointerType>(Arg->getType()))
159177b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner          Arg = Builder.CreatePtrToInt(Arg, IntPtrTy);
15922acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner        llvm::Type *OutputTy = ConvertType(OutputType);
1593aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner        if (isa<llvm::IntegerType>(OutputTy))
1594aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner          Arg = Builder.CreateZExt(Arg, OutputTy);
159593f1322684e928a559286ba1c7cb83af077aa658Peter Collingbourne        else if (isa<llvm::PointerType>(OutputTy))
159693f1322684e928a559286ba1c7cb83af077aa658Peter Collingbourne          Arg = Builder.CreateZExt(Arg, IntPtrTy);
159793f1322684e928a559286ba1c7cb83af077aa658Peter Collingbourne        else {
159893f1322684e928a559286ba1c7cb83af077aa658Peter Collingbourne          assert(OutputTy->isFloatingPointTy() && "Unexpected output type");
1599aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner          Arg = Builder.CreateFPExt(Arg, OutputTy);
160093f1322684e928a559286ba1c7cb83af077aa658Peter Collingbourne        }
16014df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner      }
16024df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner    }
1603acb5310542b82e7acf5f5a91fa619e4b7efeff5cBill Wendling    if (llvm::Type* AdjTy =
16044b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne              getTargetHooks().adjustInlineAsmType(*this, InputConstraint,
16054b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne                                                   Arg->getType()))
1606f6e2c2039f76fa58799f6d155892d54fc95755e1Dale Johannesen      Arg = Builder.CreateBitCast(Arg, AdjTy);
16071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1608fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    ArgTypes.push_back(Arg->getType());
1609fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    Args.push_back(Arg);
1610fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    Constraints += InputConstraint;
1611fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  }
16121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1613f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson  // Append the "input" part of inout constraints last.
1614f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson  for (unsigned i = 0, e = InOutArgs.size(); i != e; i++) {
1615f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson    ArgTypes.push_back(InOutArgTypes[i]);
1616f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson    Args.push_back(InOutArgs[i]);
1617f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson  }
1618f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson  Constraints += InOutConstraints;
16191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1620fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  // Clobbers
1621fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  for (unsigned i = 0, e = S.getNumClobbers(); i != e; i++) {
162233f0558f75f70061707d1388e305b8f92f4e55deChad Rosier    StringRef Clobber = S.getClobber(i);
1623fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson
1624de31fd7eeebdc64fb043463e7f515dab8eccac8dEric Christopher    if (Clobber != "memory" && Clobber != "cc")
162583c021c6d33aa173cf1a6e3bc61006dabb042703Anders Carlsson    Clobber = Target.getNormalizedGCCRegisterName(Clobber);
16261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1627ea041758d49215167e473a515b8d46e77b170ccfAnders Carlsson    if (i != 0 || NumConstraints != 0)
1628fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson      Constraints += ',';
16291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1630ea041758d49215167e473a515b8d46e77b170ccfAnders Carlsson    Constraints += "~{";
1631fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    Constraints += Clobber;
1632ea041758d49215167e473a515b8d46e77b170ccfAnders Carlsson    Constraints += '}';
1633fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  }
16341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1635fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  // Add machine specific clobbers
1636ccf614c479ac93326a01e7b373b30759eed7807fEli Friedman  std::string MachineClobbers = Target.getClobbers();
1637ccf614c479ac93326a01e7b373b30759eed7807fEli Friedman  if (!MachineClobbers.empty()) {
1638fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    if (!Constraints.empty())
1639fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson      Constraints += ',';
1640ccf614c479ac93326a01e7b373b30759eed7807fEli Friedman    Constraints += MachineClobbers;
1641fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  }
1642bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson
16432acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *ResultType;
1644a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner  if (ResultRegTypes.empty())
16458b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    ResultType = VoidTy;
1646a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner  else if (ResultRegTypes.size() == 1)
1647a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    ResultType = ResultRegTypes[0];
1648bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson  else
1649d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall    ResultType = llvm::StructType::get(getLLVMContext(), ResultRegTypes);
16501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16512acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FTy =
1652fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    llvm::FunctionType::get(ResultType, ArgTypes, false);
16531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16542ab7d43e450333d52fdf087bf2558a74dbe3c9fdChad Rosier  bool HasSideEffect = S.isVolatile() || S.getNumOutputs() == 0;
1655fcf75a3d8c072eabbf25c9b33ccbb1b8bd042cfcChad Rosier  llvm::InlineAsm::AsmDialect AsmDialect = isa<MSAsmStmt>(&S) ?
1656fcf75a3d8c072eabbf25c9b33ccbb1b8bd042cfcChad Rosier    llvm::InlineAsm::AD_Intel : llvm::InlineAsm::AD_ATT;
16571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::InlineAsm *IA =
1658790cbd84d07f0dc9253ade9f67e6ed8c43f08a59Chad Rosier    llvm::InlineAsm::get(FTy, AsmString, Constraints, HasSideEffect,
1659fcf75a3d8c072eabbf25c9b33ccbb1b8bd042cfcChad Rosier                         /* IsAlignStack */ false, AsmDialect);
16604c7d9f1507d0f102bd4133bba63348636facd469Jay Foad  llvm::CallInst *Result = Builder.CreateCall(IA, Args);
1661785b778203a474c6e4b9e17ae91cd2a358868877Bill Wendling  Result->addAttribute(llvm::AttributeSet::FunctionIndex,
166215e05e932ccf9ba2621394834b62684b42d6fd40Peter Collingbourne                       llvm::Attribute::NoUnwind);
16631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1664fc1a9c312d249a2c7458f763f848ba42685c23f8Chris Lattner  // Slap the source location of the inline asm into a !srcloc metadata on the
1665a23b91d542e336be5051ac54f394e860fb756911Chad Rosier  // call.  FIXME: Handle metadata for MS-style inline asms.
1666a23b91d542e336be5051ac54f394e860fb756911Chad Rosier  if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(&S))
1667a23b91d542e336be5051ac54f394e860fb756911Chad Rosier    Result->setMetadata("srcloc", getAsmSrcLocInfo(gccAsmStmt->getAsmString(),
1668a23b91d542e336be5051ac54f394e860fb756911Chad Rosier                                                   *this));
16691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1670a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner  // Extract all of the register value results from the asm.
1671a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner  std::vector<llvm::Value*> RegResults;
1672a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner  if (ResultRegTypes.size() == 1) {
1673a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    RegResults.push_back(Result);
1674bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson  } else {
1675a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    for (unsigned i = 0, e = ResultRegTypes.size(); i != e; ++i) {
1676bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson      llvm::Value *Tmp = Builder.CreateExtractValue(Result, i, "asmresult");
1677a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      RegResults.push_back(Tmp);
1678a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    }
1679a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner  }
16801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1681a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner  for (unsigned i = 0, e = RegResults.size(); i != e; ++i) {
1682a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    llvm::Value *Tmp = RegResults[i];
16831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1684a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    // If the result type of the LLVM IR asm doesn't match the result type of
1685a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    // the expression, do the conversion.
1686a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    if (ResultRegTypes[i] != ResultTruncRegTypes[i]) {
16872acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner      llvm::Type *TruncTy = ResultTruncRegTypes[i];
16886f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
1689aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      // Truncate the integer result to the right size, note that TruncTy can be
1690aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      // a pointer.
1691aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      if (TruncTy->isFloatingPointTy())
1692aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner        Tmp = Builder.CreateFPTrunc(Tmp, TruncTy);
16932dca88fc6d3e606a8131712be1800e3a4b90ca3eDan Gohman      else if (TruncTy->isPointerTy() && Tmp->getType()->isIntegerTy()) {
169425a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow        uint64_t ResSize = CGM.getDataLayout().getTypeSizeInBits(TruncTy);
1695d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall        Tmp = Builder.CreateTrunc(Tmp,
1696d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall                   llvm::IntegerType::get(getLLVMContext(), (unsigned)ResSize));
1697a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner        Tmp = Builder.CreateIntToPtr(Tmp, TruncTy);
16982dca88fc6d3e606a8131712be1800e3a4b90ca3eDan Gohman      } else if (Tmp->getType()->isPointerTy() && TruncTy->isIntegerTy()) {
169925a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow        uint64_t TmpSize =CGM.getDataLayout().getTypeSizeInBits(Tmp->getType());
1700d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall        Tmp = Builder.CreatePtrToInt(Tmp,
1701d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall                   llvm::IntegerType::get(getLLVMContext(), (unsigned)TmpSize));
17022dca88fc6d3e606a8131712be1800e3a4b90ca3eDan Gohman        Tmp = Builder.CreateTrunc(Tmp, TruncTy);
17032dca88fc6d3e606a8131712be1800e3a4b90ca3eDan Gohman      } else if (TruncTy->isIntegerTy()) {
17042dca88fc6d3e606a8131712be1800e3a4b90ca3eDan Gohman        Tmp = Builder.CreateTrunc(Tmp, TruncTy);
1705f6e2c2039f76fa58799f6d155892d54fc95755e1Dale Johannesen      } else if (TruncTy->isVectorTy()) {
1706f6e2c2039f76fa58799f6d155892d54fc95755e1Dale Johannesen        Tmp = Builder.CreateBitCast(Tmp, TruncTy);
1707a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      }
1708bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson    }
17091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1710545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall    EmitStoreThroughLValue(RValue::get(Tmp), ResultRegDests[i]);
1711bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson  }
1712fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson}
1713