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
14e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta#include "CGDebugInfo.h"
15e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta#include "CodeGenModule.h"
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenFunction.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"
2217d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson#include "llvm/InlineAsm.h"
2317d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson#include "llvm/Intrinsics.h"
24ebaae2a3103b443d50444d335ab5ab0ff7680da2Anders Carlsson#include "llvm/Target/TargetData.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
2013379320c10001d7e1ee5d7e7142c417f797cfe82Chris Lattner  for (CompoundStmt::const_body_iterator I = S.body_begin(),
2023379320c10001d7e1ee5d7e7142c417f797cfe82Chris Lattner       E = S.body_end()-GetLast; I != E; ++I)
2035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    EmitStmt(*I);
204e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta
20517d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson  RValue RV;
2061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!GetLast)
20717d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson    RV = RValue::get(0);
20817d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson  else {
2091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // We have to special case labels here.  They are statements, but when put
21017d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson    // at the end of a statement expression, they yield the value of their
21117d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson    // subexpression.  Handle this by walking through all labels we encounter,
21217d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson    // emitting them before we evaluate the subexpr.
21317d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson    const Stmt *LastStmt = S.body_back();
21417d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson    while (const LabelStmt *LS = dyn_cast<LabelStmt>(LastStmt)) {
215ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner      EmitLabel(LS->getDecl());
21617d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson      LastStmt = LS->getSubStmt();
21717d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson    }
2181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21917d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson    EnsureInsertPoint();
2201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
221558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    RV = EmitAnyExpr(cast<Expr>(LastStmt), AggSlot);
22291d723da7b68be5245c3ac58aa2a36d04658cfb8Chris Lattner  }
223a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar
22417d28a3e0b0efaba14534d0e6d6a307283d96b9fAnders Carlsson  return RV;
2255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
227aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbarvoid CodeGenFunction::SimplifyForwardingBlocks(llvm::BasicBlock *BB) {
228aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  llvm::BranchInst *BI = dyn_cast<llvm::BranchInst>(BB->getTerminator());
2291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
230aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  // If there is a cleanup stack, then we it isn't worth trying to
231aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  // simplify this block (we would need to remove it from the scope map
232aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  // and cleanup entry).
233f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!EHStack.empty())
234aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar    return;
235aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar
236aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  // Can only simplify direct branches.
237aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  if (!BI || !BI->isUnconditional())
238aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar    return;
239aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar
240aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  BB->replaceAllUsesWith(BI->getSuccessor(0));
241aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  BI->eraseFromParent();
242aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  BB->eraseFromParent();
243aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar}
244aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar
245a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbarvoid CodeGenFunction::EmitBlock(llvm::BasicBlock *BB, bool IsFinished) {
246548ce5e78215a34f409d597bb2c1e9f897a8eda3John McCall  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
247548ce5e78215a34f409d597bb2c1e9f897a8eda3John McCall
248d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  // Fall out of the current block (if necessary).
249d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  EmitBranch(BB);
250a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar
251a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar  if (IsFinished && BB->use_empty()) {
252a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar    delete BB;
253a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar    return;
254a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar  }
255a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar
256839cbaa004a24e8f1ea14db5ed76e3d25ed28996John McCall  // Place the block after the current block, if possible, or else at
257839cbaa004a24e8f1ea14db5ed76e3d25ed28996John McCall  // the end of the function.
258548ce5e78215a34f409d597bb2c1e9f897a8eda3John McCall  if (CurBB && CurBB->getParent())
259548ce5e78215a34f409d597bb2c1e9f897a8eda3John McCall    CurFn->getBasicBlockList().insertAfter(CurBB, BB);
260839cbaa004a24e8f1ea14db5ed76e3d25ed28996John McCall  else
261839cbaa004a24e8f1ea14db5ed76e3d25ed28996John McCall    CurFn->getBasicBlockList().push_back(BB);
262d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  Builder.SetInsertPoint(BB);
263d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar}
264d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar
265d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbarvoid CodeGenFunction::EmitBranch(llvm::BasicBlock *Target) {
266d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  // Emit a branch from the current block to the target one if this
267d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  // was a real block.  If this was just a fall-through block after a
268d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  // terminator, don't emit it.
269d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
270d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar
271d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  if (!CurBB || CurBB->getTerminator()) {
272d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar    // If there is no insert point or the previous block is already
273d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar    // terminated, don't touch it.
2745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } else {
2755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Otherwise, create a fall-through branch.
276d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar    Builder.CreateBr(Target);
2775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2785e08ad3cc62ab94649959ae227a9a411a729bf49Daniel Dunbar
2795e08ad3cc62ab94649959ae227a9a411a729bf49Daniel Dunbar  Builder.ClearInsertionPoint();
2805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
282777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCallvoid CodeGenFunction::EmitBlockAfterUses(llvm::BasicBlock *block) {
283777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  bool inserted = false;
284777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  for (llvm::BasicBlock::use_iterator
285777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall         i = block->use_begin(), e = block->use_end(); i != e; ++i) {
286777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    if (llvm::Instruction *insn = dyn_cast<llvm::Instruction>(*i)) {
287777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      CurFn->getBasicBlockList().insertAfter(insn->getParent(), block);
288777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      inserted = true;
289777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      break;
290777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    }
291777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  }
292777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
293777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  if (!inserted)
294777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    CurFn->getBasicBlockList().push_back(block);
295777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
296777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  Builder.SetInsertPoint(block);
297777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall}
298777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
299f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallCodeGenFunction::JumpDest
300ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris LattnerCodeGenFunction::getJumpDestForLabel(const LabelDecl *D) {
301ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  JumpDest &Dest = LabelMap[D];
302ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  if (Dest.isValid()) return Dest;
303f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
304f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Create, but don't insert, the new block.
305ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  Dest = JumpDest(createBasicBlock(D->getName()),
306ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall                  EHScopeStack::stable_iterator::invalid(),
307ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall                  NextCleanupDestIndex++);
308f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  return Dest;
309f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
310f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
311ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattnervoid CodeGenFunction::EmitLabel(const LabelDecl *D) {
312ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  JumpDest &Dest = LabelMap[D];
313f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
314ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  // If we didn't need a forward reference to this label, just go
315f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // ahead and create a destination at the current scope.
316ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  if (!Dest.isValid()) {
317ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner    Dest = getJumpDestInCurrentScope(D->getName());
318f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
319f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Otherwise, we need to give this label a target depth and remove
320f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // it from the branch-fixups list.
321f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  } else {
322ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    assert(!Dest.getScopeDepth().isValid() && "already emitted label!");
323ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    Dest = JumpDest(Dest.getBlock(),
324ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall                    EHStack.stable_begin(),
325ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall                    Dest.getDestIndex());
326f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
327ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    ResolveBranchFixups(Dest.getBlock());
328f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
329f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
330ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(Dest.getBlock());
33191d723da7b68be5245c3ac58aa2a36d04658cfb8Chris Lattner}
33291d723da7b68be5245c3ac58aa2a36d04658cfb8Chris Lattner
33391d723da7b68be5245c3ac58aa2a36d04658cfb8Chris Lattner
33491d723da7b68be5245c3ac58aa2a36d04658cfb8Chris Lattnervoid CodeGenFunction::EmitLabelStmt(const LabelStmt &S) {
335ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  EmitLabel(S.getDecl());
3365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  EmitStmt(S.getSubStmt());
3375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
339534986f2b21e6050bf00163cd6423fd92155a6edRichard Smithvoid CodeGenFunction::EmitAttributedStmt(const AttributedStmt &S) {
340534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith  EmitStmt(S.getSubStmt());
341534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith}
342534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith
3435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid CodeGenFunction::EmitGotoStmt(const GotoStmt &S) {
3440912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // If this code is reachable then emit a stop point (if generating
3450912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // debug info). We have to do this ourselves because we are on the
3460912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // "simple" statement path.
3470912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  if (HaveInsertPoint())
3480912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar    EmitStopPoint(&S);
34936a2ada69fdb457b0e46d0ef452c150b360d8888Mike Stump
350f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitBranchThroughCleanup(getJumpDestForLabel(S.getLabel()));
3515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3533d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
3540ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbarvoid CodeGenFunction::EmitIndirectGotoStmt(const IndirectGotoStmt &S) {
355ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  if (const LabelDecl *Target = S.getConstantTarget()) {
35695c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall    EmitBranchThroughCleanup(getJumpDestForLabel(Target));
35795c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall    return;
35895c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall  }
35995c225de9fa3d79f70ef5008c0279580a7d9dcadJohn McCall
36049c952f853fe2d15dd9c9ff2a29c696bd18fca13Chris Lattner  // Ensure that we have an i8* for our PHI node.
361d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  llvm::Value *V = Builder.CreateBitCast(EmitScalarExpr(S.getTarget()),
362d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall                                         Int8PtrTy, "addr");
3633d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
3643d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
3653d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  // Get the basic block for the indirect goto.
3663d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  llvm::BasicBlock *IndGotoBB = GetIndirectGotoBlock();
3676f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
3683d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  // The first instruction in the block has to be the PHI for the switch dest,
3693d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  // add an entry for this branch.
3703d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  cast<llvm::PHINode>(IndGotoBB->begin())->addIncoming(V, CurBB);
3716f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
3723d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  EmitBranch(IndGotoBB);
3730ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar}
3740ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar
37562b72f642207ba2ba433d686df924dc9594e9897Chris Lattnervoid CodeGenFunction::EmitIfStmt(const IfStmt &S) {
3765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.8.4.1: The first substatement is executed if the expression compares
3775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // unequal to 0.  The condition must be a scalar type.
378f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  RunCleanupsScope ConditionScope(*this);
37901234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor
3808cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor  if (S.getConditionVariable())
381b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall    EmitAutoVarDecl(*S.getConditionVariable());
3821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3839bc47e29dce8f095be7a6d07dbb02a5a7a112949Chris Lattner  // If the condition constant folds and can be elided, try to avoid emitting
3849bc47e29dce8f095be7a6d07dbb02a5a7a112949Chris Lattner  // the condition and the dead arm of the if/else.
385c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner  bool CondConstant;
386c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner  if (ConstantFoldsToSimpleInteger(S.getCond(), CondConstant)) {
38762b72f642207ba2ba433d686df924dc9594e9897Chris Lattner    // Figure out which block (then or else) is executed.
388c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner    const Stmt *Executed = S.getThen();
389c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner    const Stmt *Skipped  = S.getElse();
390c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner    if (!CondConstant)  // Condition false?
39162b72f642207ba2ba433d686df924dc9594e9897Chris Lattner      std::swap(Executed, Skipped);
3921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
39362b72f642207ba2ba433d686df924dc9594e9897Chris Lattner    // If the skipped block has no labels in it, just emit the executed block.
39462b72f642207ba2ba433d686df924dc9594e9897Chris Lattner    // This avoids emitting dead code and simplifies the CFG substantially.
3959bc47e29dce8f095be7a6d07dbb02a5a7a112949Chris Lattner    if (!ContainsLabel(Skipped)) {
39601234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor      if (Executed) {
397f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall        RunCleanupsScope ExecutedScope(*this);
39862b72f642207ba2ba433d686df924dc9594e9897Chris Lattner        EmitStmt(Executed);
39901234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor      }
40062b72f642207ba2ba433d686df924dc9594e9897Chris Lattner      return;
40162b72f642207ba2ba433d686df924dc9594e9897Chris Lattner    }
40262b72f642207ba2ba433d686df924dc9594e9897Chris Lattner  }
4039bc47e29dce8f095be7a6d07dbb02a5a7a112949Chris Lattner
4049bc47e29dce8f095be7a6d07dbb02a5a7a112949Chris Lattner  // Otherwise, the condition did not fold, or we couldn't elide it.  Just emit
4059bc47e29dce8f095be7a6d07dbb02a5a7a112949Chris Lattner  // the conditional branch.
406781d7ca9b2fd626ef34bdc3fe06765eeff7ab2bcDaniel Dunbar  llvm::BasicBlock *ThenBlock = createBasicBlock("if.then");
407781d7ca9b2fd626ef34bdc3fe06765eeff7ab2bcDaniel Dunbar  llvm::BasicBlock *ContBlock = createBasicBlock("if.end");
408781d7ca9b2fd626ef34bdc3fe06765eeff7ab2bcDaniel Dunbar  llvm::BasicBlock *ElseBlock = ContBlock;
4095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (S.getElse())
410781d7ca9b2fd626ef34bdc3fe06765eeff7ab2bcDaniel Dunbar    ElseBlock = createBasicBlock("if.else");
411781d7ca9b2fd626ef34bdc3fe06765eeff7ab2bcDaniel Dunbar  EmitBranchOnBoolExpr(S.getCond(), ThenBlock, ElseBlock);
4121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Emit the 'then' code.
41401234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor  EmitBlock(ThenBlock);
41501234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor  {
416f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    RunCleanupsScope ThenScope(*this);
41701234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor    EmitStmt(S.getThen());
41801234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor  }
419d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  EmitBranch(ContBlock);
4201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Emit the 'else' code if present.
4225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (const Stmt *Else = S.getElse()) {
423acd723666777b4ac1f79a97b6a300e6cf919d519Devang Patel    // There is no need to emit line number for unconditional branch.
424acd723666777b4ac1f79a97b6a300e6cf919d519Devang Patel    if (getDebugInfo())
425acd723666777b4ac1f79a97b6a300e6cf919d519Devang Patel      Builder.SetCurrentDebugLocation(llvm::DebugLoc());
4265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    EmitBlock(ElseBlock);
42701234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor    {
428f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      RunCleanupsScope ElseScope(*this);
42901234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor      EmitStmt(Else);
43001234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor    }
431acd723666777b4ac1f79a97b6a300e6cf919d519Devang Patel    // There is no need to emit line number for unconditional branch.
432acd723666777b4ac1f79a97b6a300e6cf919d519Devang Patel    if (getDebugInfo())
433acd723666777b4ac1f79a97b6a300e6cf919d519Devang Patel      Builder.SetCurrentDebugLocation(llvm::DebugLoc());
434d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar    EmitBranch(ContBlock);
4355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Emit the continuation block for code after the if.
438c22d665ede76f70228055d638a087f4bd438292dDaniel Dunbar  EmitBlock(ContBlock, true);
4395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid CodeGenFunction::EmitWhileStmt(const WhileStmt &S) {
442f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Emit the header for the loop, which will also become
443f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // the continue target.
444f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest LoopHeader = getJumpDestInCurrentScope("while.cond");
445ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(LoopHeader.getBlock());
446f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
447f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Create an exit block for when the condition fails, which will
448f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // also become the break target.
449f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest LoopExit = getJumpDestInCurrentScope("while.end");
45072cac2ccce8058833f56358e3391e28a8ddeeaa4Mike Stump
45172cac2ccce8058833f56358e3391e28a8ddeeaa4Mike Stump  // Store the blocks to use for break and continue.
452f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  BreakContinueStack.push_back(BreakContinue(LoopExit, LoopHeader));
4531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4545656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  // C++ [stmt.while]p2:
4555656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  //   When the condition of a while statement is a declaration, the
4565656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  //   scope of the variable that is declared extends from its point
4575656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  //   of declaration (3.3.2) to the end of the while statement.
4585656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  //   [...]
4595656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  //   The object created in a condition is destroyed and created
4605656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  //   with each iteration of the loop.
461f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  RunCleanupsScope ConditionScope(*this);
4625656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor
463f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (S.getConditionVariable())
464b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall    EmitAutoVarDecl(*S.getConditionVariable());
4656f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
46616b16206741f5139c4ad870632db8f9ea4c6c943Mike Stump  // Evaluate the conditional in the while header.  C99 6.8.5.1: The
46716b16206741f5139c4ad870632db8f9ea4c6c943Mike Stump  // evaluation of the controlling expression takes place before each
46816b16206741f5139c4ad870632db8f9ea4c6c943Mike Stump  // execution of the loop body.
4695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
4706f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
4712c30d8fee8981ca4f20a477456dae1b722b53f1dDevang Patel  // while(1) is common, avoid extra exit blocks.  Be sure
4725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // to correctly handle break/continue though.
4732c30d8fee8981ca4f20a477456dae1b722b53f1dDevang Patel  bool EmitBoolCondBranch = true;
4741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal))
4752c30d8fee8981ca4f20a477456dae1b722b53f1dDevang Patel    if (C->isOne())
4762c30d8fee8981ca4f20a477456dae1b722b53f1dDevang Patel      EmitBoolCondBranch = false;
4771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // As long as the condition is true, go to the loop body.
479f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *LoopBody = createBasicBlock("while.body");
480f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (EmitBoolCondBranch) {
481ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    llvm::BasicBlock *ExitBlock = LoopExit.getBlock();
482f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    if (ConditionScope.requiresCleanups())
483f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      ExitBlock = createBasicBlock("while.exit");
484f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
485f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock);
486f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
487ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    if (ExitBlock != LoopExit.getBlock()) {
488f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      EmitBlock(ExitBlock);
489f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      EmitBranchThroughCleanup(LoopExit);
490f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    }
491f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
4926f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
493f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Emit the loop body.  We have to emit this in a cleanup scope
494f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // because it might be a singleton DeclStmt.
4955656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  {
496f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    RunCleanupsScope BodyScope(*this);
4975656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    EmitBlock(LoopBody);
4985656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    EmitStmt(S.getBody());
4995656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  }
500da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner
5011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  BreakContinueStack.pop_back();
5021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
503f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Immediately force cleanup.
504f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ConditionScope.ForceCleanup();
5055656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor
506f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Branch to the loop header again.
507ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBranch(LoopHeader.getBlock());
5081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Emit the exit block.
510ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(LoopExit.getBlock(), true);
5115656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor
512aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  // The LoopHeader typically is just a branch if we skipped emitting
513aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  // a branch, try to erase it.
514f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!EmitBoolCondBranch)
515ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    SimplifyForwardingBlocks(LoopHeader.getBlock());
5165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid CodeGenFunction::EmitDoStmt(const DoStmt &S) {
519f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest LoopExit = getJumpDestInCurrentScope("do.end");
520f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest LoopCond = getJumpDestInCurrentScope("do.cond");
5211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
522da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner  // Store the blocks to use for break and continue.
523f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  BreakContinueStack.push_back(BreakContinue(LoopExit, LoopCond));
5241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
525f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Emit the body of the loop.
526f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *LoopBody = createBasicBlock("do.body");
527f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitBlock(LoopBody);
528f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  {
529f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    RunCleanupsScope BodyScope(*this);
530f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    EmitStmt(S.getBody());
531f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
5321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
533e4b6d342c29d5cb9d311756100df1603810fa892Anders Carlsson  BreakContinueStack.pop_back();
5341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
535ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(LoopCond.getBlock());
5361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.8.5.2: "The evaluation of the controlling expression takes place
5385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // after each execution of the loop body."
5391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Evaluate the conditional in the while header.
5415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.8.5p2/p4: The first substatement is executed if the expression
5425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // compares unequal to 0.  The condition must be a scalar type.
5435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
54405f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel
54505f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel  // "do {} while (0)" is common in macros, avoid extra blocks.  Be sure
54605f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel  // to correctly handle break/continue though.
54705f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel  bool EmitBoolCondBranch = true;
5481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal))
54905f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel    if (C->isZero())
55005f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel      EmitBoolCondBranch = false;
55105f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel
5525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // As long as the condition is true, iterate the loop.
55305f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel  if (EmitBoolCondBranch)
554ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    Builder.CreateCondBr(BoolCondVal, LoopBody, LoopExit.getBlock());
5551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Emit the exit block.
557ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(LoopExit.getBlock());
55805f6e6bb3a3e8e604c64f04f4ba7ef0e7569cf4eDevang Patel
559aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  // The DoCond block typically is just a branch if we skipped
560aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  // emitting a branch, try to erase it.
561aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  if (!EmitBoolCondBranch)
562ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    SimplifyForwardingBlocks(LoopCond.getBlock());
5635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid CodeGenFunction::EmitForStmt(const ForStmt &S) {
566f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest LoopExit = getJumpDestInCurrentScope("for.end");
567f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
568f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  RunCleanupsScope ForScope(*this);
569da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner
5700554e0e30d24d9ad7d5e12f8e7583ebb5c9715bfDevang Patel  CGDebugInfo *DI = getDebugInfo();
57173fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher  if (DI)
57273fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher    DI->EmitLexicalBlockStart(Builder, S.getSourceRange().getBegin());
5730554e0e30d24d9ad7d5e12f8e7583ebb5c9715bfDevang Patel
5745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Evaluate the first part before the loop.
5755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (S.getInit())
5765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    EmitStmt(S.getInit());
5775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Start the loop with a block that tests the condition.
579f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // If there's an increment, the continue scope will be overwritten
580f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // later.
581f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest Continue = getJumpDestInCurrentScope("for.cond");
582ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::BasicBlock *CondBlock = Continue.getBlock();
5835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  EmitBlock(CondBlock);
5845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
585d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor  // Create a cleanup scope for the condition variable cleanups.
586f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  RunCleanupsScope ConditionScope(*this);
5876f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
588d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor  llvm::Value *BoolCondVal = 0;
5895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (S.getCond()) {
59099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    // If the for statement has a condition scope, emit the local variable
59199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    // declaration.
592ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    llvm::BasicBlock *ExitBlock = LoopExit.getBlock();
593d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor    if (S.getConditionVariable()) {
594b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall      EmitAutoVarDecl(*S.getConditionVariable());
595d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor    }
596f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
597f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    // If there are any cleanups between here and the loop-exit scope,
598f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    // create a block to stage a loop exit along.
599f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    if (ForScope.requiresCleanups())
600f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      ExitBlock = createBasicBlock("for.cond.cleanup");
6016f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
60231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    // As long as the condition is true, iterate the loop.
6039615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar    llvm::BasicBlock *ForBody = createBasicBlock("for.body");
6041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.8.5p2/p4: The first substatement is executed if the expression
6065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // compares unequal to 0.  The condition must be a scalar type.
607d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor    BoolCondVal = EvaluateExprAsBool(S.getCond());
608f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock);
609f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
610ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    if (ExitBlock != LoopExit.getBlock()) {
611f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      EmitBlock(ExitBlock);
612f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      EmitBranchThroughCleanup(LoopExit);
613f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    }
6141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EmitBlock(ForBody);
6165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } else {
6175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Treat it as a non-zero constant.  Don't even create a new block for the
6185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // body, just fall into it.
6195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
6205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If the for loop doesn't have an increment we can just use the
622f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // condition as the continue block.  Otherwise we'll need to create
623f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // a block for it (in the current scope, i.e. in the scope of the
624f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // condition), and that we will become our continue block.
625da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner  if (S.getInc())
626f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    Continue = getJumpDestInCurrentScope("for.inc");
6271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
628da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner  // Store the blocks to use for break and continue.
629f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
6303e9da66ac7e88d64d30ee777588677320660cf84Mike Stump
631d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor  {
632d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor    // Create a separate cleanup scope for the body, in case it is not
633d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor    // a compound statement.
634f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    RunCleanupsScope BodyScope(*this);
635d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor    EmitStmt(S.getBody());
636d975206755e26a391f4a1cd8bf8f96a6a65b05e6Douglas Gregor  }
637da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner
6385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If there is an increment, emit it next.
639ad12b6d643aba6c36f5cec4c9beb4977a12eace4Daniel Dunbar  if (S.getInc()) {
640ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    EmitBlock(Continue.getBlock());
641883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner    EmitStmt(S.getInc());
642ad12b6d643aba6c36f5cec4c9beb4977a12eace4Daniel Dunbar  }
6431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
64445d3fe1898d3726d269a0bd2ccb8527102e29d79Douglas Gregor  BreakContinueStack.pop_back();
645f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
646f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ConditionScope.ForceCleanup();
647f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitBranch(CondBlock);
648f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
649f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ForScope.ForceCleanup();
650f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
65173fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher  if (DI)
65273fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher    DI->EmitLexicalBlockEnd(Builder, S.getSourceRange().getEnd());
6535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
654da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner  // Emit the fall-through block.
655ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(LoopExit.getBlock(), true);
6565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
6575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
658ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithvoid CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S) {
659ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  JumpDest LoopExit = getJumpDestInCurrentScope("for.end");
660ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
661ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  RunCleanupsScope ForScope(*this);
662ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
663ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  CGDebugInfo *DI = getDebugInfo();
66473fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher  if (DI)
66573fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher    DI->EmitLexicalBlockStart(Builder, S.getSourceRange().getBegin());
666ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
667ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // Evaluate the first pieces before the loop.
668ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  EmitStmt(S.getRangeStmt());
669ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  EmitStmt(S.getBeginEndStmt());
670ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
671ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // Start the loop with a block that tests the condition.
672ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // If there's an increment, the continue scope will be overwritten
673ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // later.
674ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
675ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  EmitBlock(CondBlock);
676ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
677ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // If there are any cleanups between here and the loop-exit scope,
678ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // create a block to stage a loop exit along.
679ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  llvm::BasicBlock *ExitBlock = LoopExit.getBlock();
680ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  if (ForScope.requiresCleanups())
681ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    ExitBlock = createBasicBlock("for.cond.cleanup");
6826f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
683ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // The loop body, consisting of the specified body and the loop variable.
684ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  llvm::BasicBlock *ForBody = createBasicBlock("for.body");
685ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
686ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // The body is executed if the expression, contextually converted
687ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // to bool, is true.
688ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
689ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock);
690ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
691ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  if (ExitBlock != LoopExit.getBlock()) {
692ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    EmitBlock(ExitBlock);
693ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    EmitBranchThroughCleanup(LoopExit);
694ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
695ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
696ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  EmitBlock(ForBody);
697ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
698ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // Create a block for the increment. In case of a 'continue', we jump there.
699ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  JumpDest Continue = getJumpDestInCurrentScope("for.inc");
700ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
701ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // Store the blocks to use for break and continue.
702ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
703ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
704ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  {
705ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    // Create a separate cleanup scope for the loop variable and body.
706ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    RunCleanupsScope BodyScope(*this);
707ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    EmitStmt(S.getLoopVarStmt());
708ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    EmitStmt(S.getBody());
709ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  }
710ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
711ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // If there is an increment, emit it next.
712ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  EmitBlock(Continue.getBlock());
713ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  EmitStmt(S.getInc());
714ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
715ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  BreakContinueStack.pop_back();
716ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
717ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  EmitBranch(CondBlock);
718ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
719ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  ForScope.ForceCleanup();
720ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
72173fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher  if (DI)
72273fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher    DI->EmitLexicalBlockEnd(Builder, S.getSourceRange().getEnd());
723ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
724ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  // Emit the fall-through block.
725ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  EmitBlock(LoopExit.getBlock(), true);
726ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith}
727ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
72829e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbarvoid CodeGenFunction::EmitReturnOfRValue(RValue RV, QualType Ty) {
72929e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar  if (RV.isScalar()) {
73029e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar    Builder.CreateStore(RV.getScalarVal(), ReturnValue);
73129e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar  } else if (RV.isAggregate()) {
732649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier    EmitAggregateCopy(ReturnValue, RV.getAggregateAddr(), Ty);
73329e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar  } else {
73429e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar    StoreComplexToAddr(RV.getComplexVal(), ReturnValue, false);
73529e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar  }
73682d8ef0be44ddba608c1ce5c8b6b48da83bc1821Anders Carlsson  EmitBranchThroughCleanup(ReturnBlock);
73729e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar}
73829e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar
7395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// EmitReturnStmt - Note that due to GCC extensions, this can have an operand
7405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// if the function returns void, or may be missing one if the function returns
7415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// non-void.  Fun stuff :).
7425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) {
7435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Emit the result value, even if unused, to evalute the side effects.
7445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const Expr *RV = S.getRetValue();
7451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7465ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar  // FIXME: Clean this up by using an LValue for ReturnTemp,
7475ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar  // EmitStoreThroughLValue, and EmitAnyExpr.
748d86c477fb5d3fc34864afecbbb5443da9355e8fbDouglas Gregor  if (S.getNRVOCandidate() && S.getNRVOCandidate()->isNRVOVariable() &&
749d86c477fb5d3fc34864afecbbb5443da9355e8fbDouglas Gregor      !Target.useGlobalsForAutomaticVariables()) {
750d86c477fb5d3fc34864afecbbb5443da9355e8fbDouglas Gregor    // Apply the named return value optimization for this return statement,
751d86c477fb5d3fc34864afecbbb5443da9355e8fbDouglas Gregor    // which means doing nothing: the appropriate result has already been
752d86c477fb5d3fc34864afecbbb5443da9355e8fbDouglas Gregor    // constructed into the NRVO variable.
7536f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
7543d91bbcdab155181556969cad6ec97014405acedDouglas Gregor    // If there is an NRVO flag for this variable, set it to 1 into indicate
7553d91bbcdab155181556969cad6ec97014405acedDouglas Gregor    // that the cleanup code should not destroy the variable.
756d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall    if (llvm::Value *NRVOFlag = NRVOFlags[S.getNRVOCandidate()])
757d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall      Builder.CreateStore(Builder.getTrue(), NRVOFlag);
758d86c477fb5d3fc34864afecbbb5443da9355e8fbDouglas Gregor  } else if (!ReturnValue) {
7595ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar    // Make sure not to return anything, but evaluate the expression
7605ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar    // for side effects.
7615ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar    if (RV)
762144ac61f9005a0da4327d4e62a4c453923b7bc0cEli Friedman      EmitAnyExpr(RV);
7635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } else if (RV == 0) {
7645ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar    // Do nothing (return value is left uninitialized)
765d54b6ac2f4f6f0bd0076cbfa885b57277066f06cEli Friedman  } else if (FnRetTy->isReferenceType()) {
766d54b6ac2f4f6f0bd0076cbfa885b57277066f06cEli Friedman    // If this function returns a reference, take the address of the expression
767d54b6ac2f4f6f0bd0076cbfa885b57277066f06cEli Friedman    // rather than the value.
76832f36baa6c8d491c374af622b4e3ac28d597453cAnders Carlsson    RValue Result = EmitReferenceBindingToExpr(RV, /*InitializedDecl=*/0);
76933fd1fc1814a5573c972840d49317989e20deaceDouglas Gregor    Builder.CreateStore(Result.getScalarVal(), ReturnValue);
7704b0029d5f8742760981a3bd58004f75454090a61Chris Lattner  } else if (!hasAggregateLLVMType(RV->getType())) {
7715ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar    Builder.CreateStore(EmitScalarExpr(RV), ReturnValue);
7729b2dc287177394a8f73833e2ad4f7ca8cd6f22bbChris Lattner  } else if (RV->getType()->isAnyComplexType()) {
7735ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar    EmitComplexExprIntoAddr(RV, ReturnValue, false);
7745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } else {
775d7722d9d76a851e7897f4127626616d3b1b8e530Eli Friedman    CharUnits Alignment = getContext().getTypeAlignInChars(RV->getType());
776f394078fde147dcf27e9b6a7965517388d64dcb6Eli Friedman    EmitAggExpr(RV, AggValueSlot::forAddr(ReturnValue, Alignment, Qualifiers(),
7777c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall                                          AggValueSlot::IsDestructed,
778410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall                                          AggValueSlot::DoesNotNeedGCBarriers,
779649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier                                          AggValueSlot::IsNotAliased));
7805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
781144ac61f9005a0da4327d4e62a4c453923b7bc0cEli Friedman
78282d8ef0be44ddba608c1ce5c8b6b48da83bc1821Anders Carlsson  EmitBranchThroughCleanup(ReturnBlock);
7835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid CodeGenFunction::EmitDeclStmt(const DeclStmt &S) {
7869198126067a447f8aaccf9fff09be294c8bcb81eDevang Patel  // As long as debug info is modeled with instructions, we have to ensure we
7879198126067a447f8aaccf9fff09be294c8bcb81eDevang Patel  // have a place to insert here and write the stop point here.
7882b124ea9d2c27c6d002ecd4623f6321e305d907eEric Christopher  if (HaveInsertPoint())
7899198126067a447f8aaccf9fff09be294c8bcb81eDevang Patel    EmitStopPoint(&S);
7909198126067a447f8aaccf9fff09be294c8bcb81eDevang Patel
791e4ea1f42c97a436df3e0ae8e129e6bc624ee6790Ted Kremenek  for (DeclStmt::const_decl_iterator I = S.decl_begin(), E = S.decl_end();
792e4ea1f42c97a436df3e0ae8e129e6bc624ee6790Ted Kremenek       I != E; ++I)
793e4ea1f42c97a436df3e0ae8e129e6bc624ee6790Ted Kremenek    EmitDecl(**I);
7946fa5f0943a84233b2e1ec9716eae55643225bfd4Chris Lattner}
795da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner
7960912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbarvoid CodeGenFunction::EmitBreakStmt(const BreakStmt &S) {
797da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner  assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!");
798da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner
7990912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // If this code is reachable then emit a stop point (if generating
8000912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // debug info). We have to do this ourselves because we are on the
8010912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // "simple" statement path.
8020912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  if (HaveInsertPoint())
8030912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar    EmitStopPoint(&S);
804ec9771d57f94cc204491b3174e88069d08cdd684Mike Stump
805f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest Block = BreakContinueStack.back().BreakBlock;
80682d8ef0be44ddba608c1ce5c8b6b48da83bc1821Anders Carlsson  EmitBranchThroughCleanup(Block);
807da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner}
808da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner
8090912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbarvoid CodeGenFunction::EmitContinueStmt(const ContinueStmt &S) {
810da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner  assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
811da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner
8120912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // If this code is reachable then emit a stop point (if generating
8130912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // debug info). We have to do this ourselves because we are on the
8140912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // "simple" statement path.
8150912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  if (HaveInsertPoint())
8160912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar    EmitStopPoint(&S);
817ec9771d57f94cc204491b3174e88069d08cdd684Mike Stump
818f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest Block = BreakContinueStack.back().ContinueBlock;
81982d8ef0be44ddba608c1ce5c8b6b48da83bc1821Anders Carlsson  EmitBranchThroughCleanup(Block);
820da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner}
82151b09f2c528c8460b5465c676173324e44176d62Devang Patel
822c049e4f406a7f7179eba98659044a32508e53289Devang Patel/// EmitCaseStmtRange - If case statement range is not too big then
823c049e4f406a7f7179eba98659044a32508e53289Devang Patel/// add multiple cases to switch instruction, one for each value within
824c049e4f406a7f7179eba98659044a32508e53289Devang Patel/// the range. If range is too big then emit "if" condition check.
825c049e4f406a7f7179eba98659044a32508e53289Devang Patelvoid CodeGenFunction::EmitCaseStmtRange(const CaseStmt &S) {
8264efde8d2c10b091bc9588de18c2c71dca2979e49Daniel Dunbar  assert(S.getRHS() && "Expected RHS value in CaseStmt");
827c049e4f406a7f7179eba98659044a32508e53289Devang Patel
828a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  llvm::APSInt LHS = S.getLHS()->EvaluateKnownConstInt(getContext());
829a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  llvm::APSInt RHS = S.getRHS()->EvaluateKnownConstInt(getContext());
8304efde8d2c10b091bc9588de18c2c71dca2979e49Daniel Dunbar
83116f23570999cac1fa13597386938450843003840Daniel Dunbar  // Emit the code for this case. We do this first to make sure it is
83216f23570999cac1fa13597386938450843003840Daniel Dunbar  // properly chained from our predecessor before generating the
83316f23570999cac1fa13597386938450843003840Daniel Dunbar  // switch machinery to enter this block.
834f84dcda7e2ab2f6d5be5a8c52d22ef4c442dd762Daniel Dunbar  EmitBlock(createBasicBlock("sw.bb"));
83516f23570999cac1fa13597386938450843003840Daniel Dunbar  llvm::BasicBlock *CaseDest = Builder.GetInsertBlock();
83616f23570999cac1fa13597386938450843003840Daniel Dunbar  EmitStmt(S.getSubStmt());
83716f23570999cac1fa13597386938450843003840Daniel Dunbar
8384efde8d2c10b091bc9588de18c2c71dca2979e49Daniel Dunbar  // If range is empty, do nothing.
8394efde8d2c10b091bc9588de18c2c71dca2979e49Daniel Dunbar  if (LHS.isSigned() ? RHS.slt(LHS) : RHS.ult(LHS))
8404efde8d2c10b091bc9588de18c2c71dca2979e49Daniel Dunbar    return;
841c049e4f406a7f7179eba98659044a32508e53289Devang Patel
842c049e4f406a7f7179eba98659044a32508e53289Devang Patel  llvm::APInt Range = RHS - LHS;
84316f23570999cac1fa13597386938450843003840Daniel Dunbar  // FIXME: parameters such as this should not be hardcoded.
844c049e4f406a7f7179eba98659044a32508e53289Devang Patel  if (Range.ult(llvm::APInt(Range.getBitWidth(), 64))) {
845c049e4f406a7f7179eba98659044a32508e53289Devang Patel    // Range is small enough to add multiple switch instruction cases.
8464efde8d2c10b091bc9588de18c2c71dca2979e49Daniel Dunbar    for (unsigned i = 0, e = Range.getZExtValue() + 1; i != e; ++i) {
84797d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner      SwitchInsn->addCase(Builder.getInt(LHS), CaseDest);
8482d79d0f3ac0ea77b7bdfc3dd11de8cc3ccd91b8cDevang Patel      LHS++;
8492d79d0f3ac0ea77b7bdfc3dd11de8cc3ccd91b8cDevang Patel    }
850c049e4f406a7f7179eba98659044a32508e53289Devang Patel    return;
8511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
8521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
85316f23570999cac1fa13597386938450843003840Daniel Dunbar  // The range is too big. Emit "if" condition into a new block,
85416f23570999cac1fa13597386938450843003840Daniel Dunbar  // making sure to save and restore the current insertion point.
85516f23570999cac1fa13597386938450843003840Daniel Dunbar  llvm::BasicBlock *RestoreBB = Builder.GetInsertBlock();
85616f23570999cac1fa13597386938450843003840Daniel Dunbar
85716f23570999cac1fa13597386938450843003840Daniel Dunbar  // Push this test onto the chain of range checks (which terminates
85816f23570999cac1fa13597386938450843003840Daniel Dunbar  // in the default basic block). The switch's default will be changed
85916f23570999cac1fa13597386938450843003840Daniel Dunbar  // to the top of this chain after switch emission is complete.
86016f23570999cac1fa13597386938450843003840Daniel Dunbar  llvm::BasicBlock *FalseDest = CaseRangeBlock;
86155e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  CaseRangeBlock = createBasicBlock("sw.caserange");
86216f23570999cac1fa13597386938450843003840Daniel Dunbar
86316f23570999cac1fa13597386938450843003840Daniel Dunbar  CurFn->getBasicBlockList().push_back(CaseRangeBlock);
86416f23570999cac1fa13597386938450843003840Daniel Dunbar  Builder.SetInsertPoint(CaseRangeBlock);
865c049e4f406a7f7179eba98659044a32508e53289Devang Patel
866c049e4f406a7f7179eba98659044a32508e53289Devang Patel  // Emit range check.
8671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::Value *Diff =
868578faa837b552403e2002b97fdfbfde14f2448e5Benjamin Kramer    Builder.CreateSub(SwitchInsn->getCondition(), Builder.getInt(LHS));
8691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::Value *Cond =
87097d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner    Builder.CreateICmpULE(Diff, Builder.getInt(Range), "inbounds");
871c049e4f406a7f7179eba98659044a32508e53289Devang Patel  Builder.CreateCondBr(Cond, CaseDest, FalseDest);
872c049e4f406a7f7179eba98659044a32508e53289Devang Patel
87316f23570999cac1fa13597386938450843003840Daniel Dunbar  // Restore the appropriate insertion point.
874a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar  if (RestoreBB)
875a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar    Builder.SetInsertPoint(RestoreBB);
876a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar  else
877a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar    Builder.ClearInsertionPoint();
878c049e4f406a7f7179eba98659044a32508e53289Devang Patel}
8792d79d0f3ac0ea77b7bdfc3dd11de8cc3ccd91b8cDevang Patel
880c049e4f406a7f7179eba98659044a32508e53289Devang Patelvoid CodeGenFunction::EmitCaseStmt(const CaseStmt &S) {
881d66715d83e64b6bfd0bddebe51874b1b7a64abefFariborz Jahanian  // If there is no enclosing switch instance that we're aware of, then this
882d66715d83e64b6bfd0bddebe51874b1b7a64abefFariborz Jahanian  // case statement and its block can be elided.  This situation only happens
883d66715d83e64b6bfd0bddebe51874b1b7a64abefFariborz Jahanian  // when we've constant-folded the switch, are emitting the constant case,
884d66715d83e64b6bfd0bddebe51874b1b7a64abefFariborz Jahanian  // and part of the constant case includes another case statement.  For
885d66715d83e64b6bfd0bddebe51874b1b7a64abefFariborz Jahanian  // instance: switch (4) { case 4: do { case 5: } while (1); }
886303b4f946470a054cea8f91af54008aeb3c09507Fariborz Jahanian  if (!SwitchInsn) {
887303b4f946470a054cea8f91af54008aeb3c09507Fariborz Jahanian    EmitStmt(S.getSubStmt());
888d66715d83e64b6bfd0bddebe51874b1b7a64abefFariborz Jahanian    return;
889303b4f946470a054cea8f91af54008aeb3c09507Fariborz Jahanian  }
890d66715d83e64b6bfd0bddebe51874b1b7a64abefFariborz Jahanian
891b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner  // Handle case ranges.
892c049e4f406a7f7179eba98659044a32508e53289Devang Patel  if (S.getRHS()) {
893c049e4f406a7f7179eba98659044a32508e53289Devang Patel    EmitCaseStmtRange(S);
894c049e4f406a7f7179eba98659044a32508e53289Devang Patel    return;
895c049e4f406a7f7179eba98659044a32508e53289Devang Patel  }
8961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
89797d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner  llvm::ConstantInt *CaseVal =
898a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith    Builder.getInt(S.getLHS()->EvaluateKnownConstInt(getContext()));
89997d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner
900421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner  // If the body of the case is just a 'break', and if there was no fallthrough,
901421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner  // try to not emit an empty block.
90217083601193528a5d3770b688ffdf700f7df3c45Chad Rosier  if ((CGM.getCodeGenOpts().OptimizationLevel > 0) &&
90317083601193528a5d3770b688ffdf700f7df3c45Chad Rosier      isa<BreakStmt>(S.getSubStmt())) {
904b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner    JumpDest Block = BreakContinueStack.back().BreakBlock;
9056f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
906b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner    // Only do this optimization if there are no cleanups that need emitting.
907b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner    if (isObviouslyBranchWithoutCleanups(Block)) {
90897d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner      SwitchInsn->addCase(CaseVal, Block.getBlock());
909421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner
910421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner      // If there was a fallthrough into this case, make sure to redirect it to
911421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner      // the end of the switch as well.
912421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner      if (Builder.GetInsertBlock()) {
913421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner        Builder.CreateBr(Block.getBlock());
914421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner        Builder.ClearInsertionPoint();
915421048698b6b6bf86754190bcfe98a0ed82ee5b5Chris Lattner      }
916b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner      return;
917b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner    }
918b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner  }
9196f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
920f84dcda7e2ab2f6d5be5a8c52d22ef4c442dd762Daniel Dunbar  EmitBlock(createBasicBlock("sw.bb"));
921c049e4f406a7f7179eba98659044a32508e53289Devang Patel  llvm::BasicBlock *CaseDest = Builder.GetInsertBlock();
92297d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner  SwitchInsn->addCase(CaseVal, CaseDest);
9231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9245512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  // Recursively emitting the statement is acceptable, but is not wonderful for
9255512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  // code where we have many case statements nested together, i.e.:
9265512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  //  case 1:
9275512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  //    case 2:
9285512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  //      case 3: etc.
9295512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  // Handling this recursively will create a new block for each case statement
9305512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  // that falls through to the next case which is IR intensive.  It also causes
9315512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  // deep recursion which can run into stack depth limitations.  Handle
9325512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  // sequential non-range case statements specially.
9335512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  const CaseStmt *CurCase = &S;
9345512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  const CaseStmt *NextCase = dyn_cast<CaseStmt>(S.getSubStmt());
9355512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner
93697d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner  // Otherwise, iteratively add consecutive cases to this switch stmt.
9375512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  while (NextCase && NextCase->getRHS() == 0) {
9385512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner    CurCase = NextCase;
93997d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner    llvm::ConstantInt *CaseVal =
940a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith      Builder.getInt(CurCase->getLHS()->EvaluateKnownConstInt(getContext()));
94197d5437f48f8d935bf053915ab3d250edfe5fad5Chris Lattner    SwitchInsn->addCase(CaseVal, CaseDest);
9425512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner    NextCase = dyn_cast<CaseStmt>(CurCase->getSubStmt());
9435512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  }
9441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9455512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  // Normal default recursion for non-cases.
9465512f28fa7b26e87e613dc1558b29b2a89647809Chris Lattner  EmitStmt(CurCase->getSubStmt());
94751b09f2c528c8460b5465c676173324e44176d62Devang Patel}
94851b09f2c528c8460b5465c676173324e44176d62Devang Patel
94951b09f2c528c8460b5465c676173324e44176d62Devang Patelvoid CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S) {
95016f23570999cac1fa13597386938450843003840Daniel Dunbar  llvm::BasicBlock *DefaultBlock = SwitchInsn->getDefaultDest();
9511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(DefaultBlock->empty() &&
95255e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar         "EmitDefaultStmt: Default block already defined?");
95316f23570999cac1fa13597386938450843003840Daniel Dunbar  EmitBlock(DefaultBlock);
95451b09f2c528c8460b5465c676173324e44176d62Devang Patel  EmitStmt(S.getSubStmt());
95551b09f2c528c8460b5465c676173324e44176d62Devang Patel}
95651b09f2c528c8460b5465c676173324e44176d62Devang Patel
957fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// CollectStatementsForCase - Given the body of a 'switch' statement and a
958fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// constant value that is being switched on, see if we can dead code eliminate
959fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// the body of the switch to a simple series of statements to emit.  Basically,
960fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// on a switch (5) we want to find these statements:
961fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner///    case 5:
962fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner///      printf(...);    <--
963fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner///      ++i;            <--
964fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner///      break;
965fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner///
966fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// and add them to the ResultStmts vector.  If it is unsafe to do this
967fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// transformation (for example, one of the elided statements contains a label
968fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// that might be jumped to), return CSFC_Failure.  If we handled it and 'S'
969fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// should include statements after it (e.g. the printf() line is a substmt of
970fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// the case) then return CSFC_FallThrough.  If we handled it and found a break
971fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// statement, then return CSFC_Success.
972fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner///
973fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// If Case is non-null, then we are looking for the specified case, checking
974fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// that nothing we jump over contains labels.  If Case is null, then we found
975fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// the case and are looking for the break.
976fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner///
977fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// If the recursive walk actually finds our Case, then we set FoundCase to
978fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// true.
979fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner///
980fda0f1f5a278548b012401be07e287c1697fc41cChris Lattnerenum CSFC_Result { CSFC_Failure, CSFC_FallThrough, CSFC_Success };
981fda0f1f5a278548b012401be07e287c1697fc41cChris Lattnerstatic CSFC_Result CollectStatementsForCase(const Stmt *S,
982fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner                                            const SwitchCase *Case,
983fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner                                            bool &FoundCase,
9845f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                              SmallVectorImpl<const Stmt*> &ResultStmts) {
9853858938b043bac2f046304ff99a54905acdcc6ddChris Lattner  // If this is a null statement, just succeed.
9863858938b043bac2f046304ff99a54905acdcc6ddChris Lattner  if (S == 0)
9873858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    return Case ? CSFC_Success : CSFC_FallThrough;
9886f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
989fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // If this is the switchcase (case 4: or default) that we're looking for, then
990fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // we're in business.  Just add the substatement.
991fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  if (const SwitchCase *SC = dyn_cast<SwitchCase>(S)) {
992fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    if (S == Case) {
993fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      FoundCase = true;
994fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      return CollectStatementsForCase(SC->getSubStmt(), 0, FoundCase,
995fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner                                      ResultStmts);
996fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    }
9976f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
998fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    // Otherwise, this is some other case or default statement, just ignore it.
999fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    return CollectStatementsForCase(SC->getSubStmt(), Case, FoundCase,
1000fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner                                    ResultStmts);
1001fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  }
10023858938b043bac2f046304ff99a54905acdcc6ddChris Lattner
10033858938b043bac2f046304ff99a54905acdcc6ddChris Lattner  // If we are in the live part of the code and we found our break statement,
10043858938b043bac2f046304ff99a54905acdcc6ddChris Lattner  // return a success!
10053858938b043bac2f046304ff99a54905acdcc6ddChris Lattner  if (Case == 0 && isa<BreakStmt>(S))
10063858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    return CSFC_Success;
10076f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
10083858938b043bac2f046304ff99a54905acdcc6ddChris Lattner  // If this is a switch statement, then it might contain the SwitchCase, the
10093858938b043bac2f046304ff99a54905acdcc6ddChris Lattner  // break, or neither.
10103858938b043bac2f046304ff99a54905acdcc6ddChris Lattner  if (const CompoundStmt *CS = dyn_cast<CompoundStmt>(S)) {
10113858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    // Handle this as two cases: we might be looking for the SwitchCase (if so
10123858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    // the skipped statements must be skippable) or we might already have it.
10133858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    CompoundStmt::const_body_iterator I = CS->body_begin(), E = CS->body_end();
10143858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    if (Case) {
10153f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner      // Keep track of whether we see a skipped declaration.  The code could be
10163f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner      // using the declaration even if it is skipped, so we can't optimize out
10173f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner      // the decl if the kept statements might refer to it.
10183f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner      bool HadSkippedDecl = false;
10196f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
10203858938b043bac2f046304ff99a54905acdcc6ddChris Lattner      // If we're looking for the case, just see if we can skip each of the
10213858938b043bac2f046304ff99a54905acdcc6ddChris Lattner      // substatements.
10223858938b043bac2f046304ff99a54905acdcc6ddChris Lattner      for (; Case && I != E; ++I) {
10234d509341bd5db06a517daa311379f52bb540bc34Eli Friedman        HadSkippedDecl |= isa<DeclStmt>(*I);
10246f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
10253858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        switch (CollectStatementsForCase(*I, Case, FoundCase, ResultStmts)) {
10263858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        case CSFC_Failure: return CSFC_Failure;
10273858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        case CSFC_Success:
10283858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          // A successful result means that either 1) that the statement doesn't
10293858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          // have the case and is skippable, or 2) does contain the case value
10309467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner          // and also contains the break to exit the switch.  In the later case,
10319467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner          // we just verify the rest of the statements are elidable.
10329467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner          if (FoundCase) {
10333f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner            // If we found the case and skipped declarations, we can't do the
10343f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner            // optimization.
10353f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner            if (HadSkippedDecl)
10363f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner              return CSFC_Failure;
10376f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
10389467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner            for (++I; I != E; ++I)
10399467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner              if (CodeGenFunction::ContainsLabel(*I, true))
10409467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner                return CSFC_Failure;
10419467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner            return CSFC_Success;
10429467110fcef8a3e4caf9e5d022cff0322afe6e8bChris Lattner          }
10433858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          break;
10443858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        case CSFC_FallThrough:
10453858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          // If we have a fallthrough condition, then we must have found the
10463858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          // case started to include statements.  Consider the rest of the
10473858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          // statements in the compound statement as candidates for inclusion.
10483858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          assert(FoundCase && "Didn't find case but returned fallthrough?");
10493858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          // We recursively found Case, so we're not looking for it anymore.
10503858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          Case = 0;
10516f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
10523f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner          // If we found the case and skipped declarations, we can't do the
10533f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner          // optimization.
10543f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner          if (HadSkippedDecl)
10553f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner            return CSFC_Failure;
10563858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          break;
10573858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        }
10583858938b043bac2f046304ff99a54905acdcc6ddChris Lattner      }
10593858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    }
10603858938b043bac2f046304ff99a54905acdcc6ddChris Lattner
10613858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    // If we have statements in our range, then we know that the statements are
10623858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    // live and need to be added to the set of statements we're tracking.
10633858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    for (; I != E; ++I) {
10643858938b043bac2f046304ff99a54905acdcc6ddChris Lattner      switch (CollectStatementsForCase(*I, 0, FoundCase, ResultStmts)) {
10653858938b043bac2f046304ff99a54905acdcc6ddChris Lattner      case CSFC_Failure: return CSFC_Failure;
10663858938b043bac2f046304ff99a54905acdcc6ddChris Lattner      case CSFC_FallThrough:
10673858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        // A fallthrough result means that the statement was simple and just
10683858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        // included in ResultStmt, keep adding them afterwards.
10693858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        break;
10703858938b043bac2f046304ff99a54905acdcc6ddChris Lattner      case CSFC_Success:
10713858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        // A successful result means that we found the break statement and
10723858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        // stopped statement inclusion.  We just ensure that any leftover stmts
10733858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        // are skippable and return success ourselves.
10743858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        for (++I; I != E; ++I)
10753858938b043bac2f046304ff99a54905acdcc6ddChris Lattner          if (CodeGenFunction::ContainsLabel(*I, true))
10763858938b043bac2f046304ff99a54905acdcc6ddChris Lattner            return CSFC_Failure;
10773858938b043bac2f046304ff99a54905acdcc6ddChris Lattner        return CSFC_Success;
10786f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier      }
10793858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    }
10806f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
10813858938b043bac2f046304ff99a54905acdcc6ddChris Lattner    return Case ? CSFC_Success : CSFC_FallThrough;
10823858938b043bac2f046304ff99a54905acdcc6ddChris Lattner  }
10833858938b043bac2f046304ff99a54905acdcc6ddChris Lattner
1084fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // Okay, this is some other statement that we don't handle explicitly, like a
1085fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // for statement or increment etc.  If we are skipping over this statement,
1086fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // just verify it doesn't have labels, which would make it invalid to elide.
1087fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  if (Case) {
10883f06e274736eab9821ce0dc2bd8e166fe0e3aa7eChris Lattner    if (CodeGenFunction::ContainsLabel(S, true))
1089fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      return CSFC_Failure;
1090fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    return CSFC_Success;
1091fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  }
10926f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
1093fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // Otherwise, we want to include this statement.  Everything is cool with that
1094fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // so long as it doesn't contain a break out of the switch we're in.
1095fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  if (CodeGenFunction::containsBreak(S)) return CSFC_Failure;
10966f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
1097fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // Otherwise, everything is great.  Include the statement and tell the caller
1098fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // that we fall through and include the next statement as well.
1099fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  ResultStmts.push_back(S);
1100fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  return CSFC_FallThrough;
1101fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner}
1102fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner
1103fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// FindCaseStatementsForValue - Find the case statement being jumped to and
1104fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// then invoke CollectStatementsForCase to find the list of statements to emit
1105fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// for a switch on constant.  See the comment above CollectStatementsForCase
1106fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner/// for more details.
1107fda0f1f5a278548b012401be07e287c1697fc41cChris Lattnerstatic bool FindCaseStatementsForValue(const SwitchStmt &S,
1108e1ecdc168175719d74e112bcacd4aae5e12d4631Richard Trieu                                       const llvm::APSInt &ConstantCondValue,
11095f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                SmallVectorImpl<const Stmt*> &ResultStmts,
1110fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner                                       ASTContext &C) {
1111fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // First step, find the switch case that is being branched to.  We can do this
1112fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // efficiently by scanning the SwitchCase list.
1113fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  const SwitchCase *Case = S.getSwitchCaseList();
1114fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  const DefaultStmt *DefaultCase = 0;
11156f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
1116fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  for (; Case; Case = Case->getNextSwitchCase()) {
1117fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    // It's either a default or case.  Just remember the default statement in
1118fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    // case we're not jumping to any numbered cases.
1119fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    if (const DefaultStmt *DS = dyn_cast<DefaultStmt>(Case)) {
1120fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      DefaultCase = DS;
1121fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      continue;
1122fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    }
11236f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
1124fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    // Check to see if this case is the one we're looking for.
1125fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    const CaseStmt *CS = cast<CaseStmt>(Case);
1126fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    // Don't handle case ranges yet.
1127fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    if (CS->getRHS()) return false;
11286f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
1129fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    // If we found our case, remember it as 'case'.
1130a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith    if (CS->getLHS()->EvaluateKnownConstInt(C) == ConstantCondValue)
1131fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      break;
1132fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  }
11336f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
1134fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // If we didn't find a matching case, we use a default if it exists, or we
1135fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // elide the whole switch body!
1136fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  if (Case == 0) {
1137fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    // It is safe to elide the body of the switch if it doesn't contain labels
1138fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    // etc.  If it is safe, return successfully with an empty ResultStmts list.
1139fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    if (DefaultCase == 0)
1140fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      return !CodeGenFunction::ContainsLabel(&S);
1141fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    Case = DefaultCase;
1142fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  }
1143fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner
1144fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // Ok, we know which case is being jumped to, try to collect all the
1145fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // statements that follow it.  This can fail for a variety of reasons.  Also,
1146fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // check to see that the recursive walk actually found our case statement.
1147fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // Insane cases like this can fail to find it in the recursive walk since we
1148fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // don't handle every stmt kind:
1149fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // switch (4) {
1150fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  //   while (1) {
1151fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  //     case 4: ...
1152fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  bool FoundCase = false;
1153fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  return CollectStatementsForCase(S.getBody(), Case, FoundCase,
1154fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner                                  ResultStmts) != CSFC_Failure &&
1155fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner         FoundCase;
1156fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner}
1157fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner
115851b09f2c528c8460b5465c676173324e44176d62Devang Patelvoid CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) {
1159f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest SwitchExit = getJumpDestInCurrentScope("sw.epilog");
1160f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1161f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  RunCleanupsScope ConditionScope(*this);
1162d3d5301c44138b92bf01286183f5bf310cdd37cfDouglas Gregor
1163d3d5301c44138b92bf01286183f5bf310cdd37cfDouglas Gregor  if (S.getConditionVariable())
1164b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall    EmitAutoVarDecl(*S.getConditionVariable());
1165d3d5301c44138b92bf01286183f5bf310cdd37cfDouglas Gregor
1166985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian  // Handle nested switch statements.
1167985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian  llvm::SwitchInst *SavedSwitchInsn = SwitchInsn;
1168985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian  llvm::BasicBlock *SavedCRBlock = CaseRangeBlock;
1169985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian
1170fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // See if we can constant fold the condition of the switch and therefore only
1171fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  // emit the live case statement (if any) of the switch.
1172e1ecdc168175719d74e112bcacd4aae5e12d4631Richard Trieu  llvm::APSInt ConstantCondValue;
1173fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  if (ConstantFoldsToSimpleInteger(S.getCond(), ConstantCondValue)) {
11745f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<const Stmt*, 4> CaseStmts;
1175fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    if (FindCaseStatementsForValue(S, ConstantCondValue, CaseStmts,
1176fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner                                   getContext())) {
1177fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      RunCleanupsScope ExecutedScope(*this);
1178fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner
1179985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian      // At this point, we are no longer "within" a switch instance, so
1180985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian      // we can temporarily enforce this to ensure that any embedded case
1181985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian      // statements are not emitted.
1182985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian      SwitchInsn = 0;
1183985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian
1184fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      // Okay, we can dead code eliminate everything except this case.  Emit the
1185fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      // specified series of statements and we're good.
1186fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      for (unsigned i = 0, e = CaseStmts.size(); i != e; ++i)
1187fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner        EmitStmt(CaseStmts[i]);
1188985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian
1189fc65ec83f6719f06e460b61f83a3ba1e80c18d04Eric Christopher      // Now we want to restore the saved switch instance so that nested
1190fc65ec83f6719f06e460b61f83a3ba1e80c18d04Eric Christopher      // switches continue to function properly
1191985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian      SwitchInsn = SavedSwitchInsn;
1192985df1c1f2d0666a09bc03f3593929286b0dea65Fariborz Jahanian
1193fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner      return;
1194fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner    }
1195fda0f1f5a278548b012401be07e287c1697fc41cChris Lattner  }
11966f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
119751b09f2c528c8460b5465c676173324e44176d62Devang Patel  llvm::Value *CondV = EmitScalarExpr(S.getCond());
119851b09f2c528c8460b5465c676173324e44176d62Devang Patel
119916f23570999cac1fa13597386938450843003840Daniel Dunbar  // Create basic block to hold stuff that comes after switch
120016f23570999cac1fa13597386938450843003840Daniel Dunbar  // statement. We also need to create a default block now so that
120116f23570999cac1fa13597386938450843003840Daniel Dunbar  // explicit case ranges tests can have a place to jump to on
120216f23570999cac1fa13597386938450843003840Daniel Dunbar  // failure.
120355e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  llvm::BasicBlock *DefaultBlock = createBasicBlock("sw.default");
120416f23570999cac1fa13597386938450843003840Daniel Dunbar  SwitchInsn = Builder.CreateSwitch(CondV, DefaultBlock);
120516f23570999cac1fa13597386938450843003840Daniel Dunbar  CaseRangeBlock = DefaultBlock;
120651b09f2c528c8460b5465c676173324e44176d62Devang Patel
12070912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  // Clear the insertion point to indicate we are in unreachable code.
12080912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  Builder.ClearInsertionPoint();
1209d28a80d64616b66c91d28bb4c08ca2d8c594de4eEli Friedman
1210e9b8c0a38549692f1b8f688c05c35442fc620865Devang Patel  // All break statements jump to NextBlock. If BreakContinueStack is non empty
1211e9b8c0a38549692f1b8f688c05c35442fc620865Devang Patel  // then reuse last ContinueBlock.
1212f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest OuterContinue;
1213e4b6d342c29d5cb9d311756100df1603810fa892Anders Carlsson  if (!BreakContinueStack.empty())
1214f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    OuterContinue = BreakContinueStack.back().ContinueBlock;
1215e4b6d342c29d5cb9d311756100df1603810fa892Anders Carlsson
1216f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  BreakContinueStack.push_back(BreakContinue(SwitchExit, OuterContinue));
121751b09f2c528c8460b5465c676173324e44176d62Devang Patel
121851b09f2c528c8460b5465c676173324e44176d62Devang Patel  // Emit switch body.
121951b09f2c528c8460b5465c676173324e44176d62Devang Patel  EmitStmt(S.getBody());
12201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1221e4b6d342c29d5cb9d311756100df1603810fa892Anders Carlsson  BreakContinueStack.pop_back();
122251b09f2c528c8460b5465c676173324e44176d62Devang Patel
122316f23570999cac1fa13597386938450843003840Daniel Dunbar  // Update the default block in case explicit case range tests have
122416f23570999cac1fa13597386938450843003840Daniel Dunbar  // been chained on top.
1225ab14ae2ab16088b6a7f69eac6e152c3e9f9ea01bStepan Dyatkovskiy  SwitchInsn->setDefaultDest(CaseRangeBlock);
12261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1227f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // If a default was never emitted:
122816f23570999cac1fa13597386938450843003840Daniel Dunbar  if (!DefaultBlock->getParent()) {
1229f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    // If we have cleanups, emit the default block so that there's a
1230f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    // place to jump through the cleanups from.
1231f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    if (ConditionScope.requiresCleanups()) {
1232f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      EmitBlock(DefaultBlock);
1233f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1234f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    // Otherwise, just forward the default block to the switch end.
1235f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    } else {
1236ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      DefaultBlock->replaceAllUsesWith(SwitchExit.getBlock());
1237f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      delete DefaultBlock;
1238f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    }
123916f23570999cac1fa13597386938450843003840Daniel Dunbar  }
124016f23570999cac1fa13597386938450843003840Daniel Dunbar
1241ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  ConditionScope.ForceCleanup();
1242ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
124316f23570999cac1fa13597386938450843003840Daniel Dunbar  // Emit continuation.
1244ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(SwitchExit.getBlock(), true);
124551b09f2c528c8460b5465c676173324e44176d62Devang Patel
124651b09f2c528c8460b5465c676173324e44176d62Devang Patel  SwitchInsn = SavedSwitchInsn;
1247c049e4f406a7f7179eba98659044a32508e53289Devang Patel  CaseRangeBlock = SavedCRBlock;
124851b09f2c528c8460b5465c676173324e44176d62Devang Patel}
1249fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson
12502819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattnerstatic std::string
1251444be7366d0a1e172c0290a1ea54c1cb16b5947cDaniel DunbarSimplifyConstraint(const char *Constraint, const TargetInfo &Target,
12525f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                 SmallVectorImpl<TargetInfo::ConstraintInfo> *OutCons=0) {
1253fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  std::string Result;
12541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1255fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  while (*Constraint) {
1256fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    switch (*Constraint) {
1257fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    default:
1258002333f8b2cf1a8614e532f6ce366b21af85142cStuart Hastings      Result += Target.convertConstraint(Constraint);
1259fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson      break;
1260fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    // Ignore these
1261fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    case '*':
1262fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    case '?':
1263fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    case '!':
1264ef44e1110711276ffffe4b22d4ba0cebd49cb330John Thompson    case '=': // Will see this and the following in mult-alt constraints.
1265ef44e1110711276ffffe4b22d4ba0cebd49cb330John Thompson    case '+':
1266ef44e1110711276ffffe4b22d4ba0cebd49cb330John Thompson      break;
12672f474ea9ef7505df5d092287c48c19974222293bJohn Thompson    case ',':
12682f474ea9ef7505df5d092287c48c19974222293bJohn Thompson      Result += "|";
1269fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson      break;
1270fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    case 'g':
1271fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson      Result += "imr";
1272fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson      break;
1273300fb5d0ef7edc87f3fdba17fc8b1184013b35aeAnders Carlsson    case '[': {
12742819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner      assert(OutCons &&
1275300fb5d0ef7edc87f3fdba17fc8b1184013b35aeAnders Carlsson             "Must pass output names to constraints with a symbolic name");
1276300fb5d0ef7edc87f3fdba17fc8b1184013b35aeAnders Carlsson      unsigned Index;
12771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      bool result = Target.resolveSymbolicName(Constraint,
12782819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner                                               &(*OutCons)[0],
12792819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner                                               OutCons->size(), Index);
1280cbf40f913aa2aa5de6e0540fed209405d00a2c69Chris Lattner      assert(result && "Could not resolve symbolic name"); (void)result;
1281300fb5d0ef7edc87f3fdba17fc8b1184013b35aeAnders Carlsson      Result += llvm::utostr(Index);
1282300fb5d0ef7edc87f3fdba17fc8b1184013b35aeAnders Carlsson      break;
1283300fb5d0ef7edc87f3fdba17fc8b1184013b35aeAnders Carlsson    }
1284fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    }
12851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1286fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    Constraint++;
1287fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  }
12881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1289fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  return Result;
1290fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson}
1291fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson
129203117d1b2e32d18652401b12d9049871992bf3adRafael Espindola/// AddVariableConstraints - Look at AsmExpr and if it is a variable declared
129303117d1b2e32d18652401b12d9049871992bf3adRafael Espindola/// as using a particular register add that as a constraint that will be used
129403117d1b2e32d18652401b12d9049871992bf3adRafael Espindola/// in this asm stmt.
12950ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindolastatic std::string
129603117d1b2e32d18652401b12d9049871992bf3adRafael EspindolaAddVariableConstraints(const std::string &Constraint, const Expr &AsmExpr,
129703117d1b2e32d18652401b12d9049871992bf3adRafael Espindola                       const TargetInfo &Target, CodeGenModule &CGM,
1298a23b91d542e336be5051ac54f394e860fb756911Chad Rosier                       const AsmStmt &Stmt) {
12990ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola  const DeclRefExpr *AsmDeclRef = dyn_cast<DeclRefExpr>(&AsmExpr);
13000ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola  if (!AsmDeclRef)
13010ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola    return Constraint;
13020ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola  const ValueDecl &Value = *AsmDeclRef->getDecl();
13030ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola  const VarDecl *Variable = dyn_cast<VarDecl>(&Value);
13040ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola  if (!Variable)
13050ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola    return Constraint;
1306a43ef3e0511dc48d98d61598163c9deddc09cb9cEli Friedman  if (Variable->getStorageClass() != SC_Register)
1307a43ef3e0511dc48d98d61598163c9deddc09cb9cEli Friedman    return Constraint;
13080ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola  AsmLabelAttr *Attr = Variable->getAttr<AsmLabelAttr>();
13090ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola  if (!Attr)
13100ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola    return Constraint;
13115f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef Register = Attr->getLabel();
1312baf86955a9a390f2643a1ea9806832eb4a92f716Rafael Espindola  assert(Target.isValidGCCRegisterName(Register));
1313e3e07a5b3490bc2977859d56bac211afac2236fbEric Christopher  // We're using validateOutputConstraint here because we only care if
1314e3e07a5b3490bc2977859d56bac211afac2236fbEric Christopher  // this is a register constraint.
1315e3e07a5b3490bc2977859d56bac211afac2236fbEric Christopher  TargetInfo::ConstraintInfo Info(Constraint, "");
1316e3e07a5b3490bc2977859d56bac211afac2236fbEric Christopher  if (Target.validateOutputConstraint(Info) &&
1317e3e07a5b3490bc2977859d56bac211afac2236fbEric Christopher      !Info.allowsRegister()) {
13180ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola    CGM.ErrorUnsupported(&Stmt, "__asm__");
13190ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola    return Constraint;
13200ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola  }
132143fec879a527c74ff01d8aa2bf94a12432249fc7Eric Christopher  // Canonicalize the register here before returning it.
132243fec879a527c74ff01d8aa2bf94a12432249fc7Eric Christopher  Register = Target.getNormalizedGCCRegisterName(Register);
13230ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola  return "{" + Register.str() + "}";
13240ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola}
13250ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola
13266d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedmanllvm::Value*
132742b60551eff3a424e191b293bfd606559dc96bffChad RosierCodeGenFunction::EmitAsmInputLValue(const TargetInfo::ConstraintInfo &Info,
13286d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman                                    LValue InputValue, QualType InputType,
13296d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman                                    std::string &ConstraintStr) {
1330634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson  llvm::Value *Arg;
13311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Info.allowsRegister() || !Info.allowsMemory()) {
13326d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman    if (!CodeGenFunction::hasAggregateLLVMType(InputType)) {
1333545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall      Arg = EmitLoadOfLValue(InputValue).getScalarVal();
1334634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson    } else {
13352acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner      llvm::Type *Ty = ConvertType(InputType);
1336ebaae2a3103b443d50444d335ab5ab0ff7680da2Anders Carlsson      uint64_t Size = CGM.getTargetData().getTypeSizeInBits(Ty);
1337ebaae2a3103b443d50444d335ab5ab0ff7680da2Anders Carlsson      if (Size <= 64 && llvm::isPowerOf2_64(Size)) {
1338d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall        Ty = llvm::IntegerType::get(getLLVMContext(), Size);
1339ebaae2a3103b443d50444d335ab5ab0ff7680da2Anders Carlsson        Ty = llvm::PointerType::getUnqual(Ty);
13401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13416d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman        Arg = Builder.CreateLoad(Builder.CreateBitCast(InputValue.getAddress(),
13426d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman                                                       Ty));
1343ebaae2a3103b443d50444d335ab5ab0ff7680da2Anders Carlsson      } else {
13446d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman        Arg = InputValue.getAddress();
1345ebaae2a3103b443d50444d335ab5ab0ff7680da2Anders Carlsson        ConstraintStr += '*';
1346ebaae2a3103b443d50444d335ab5ab0ff7680da2Anders Carlsson      }
1347634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson    }
1348634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson  } else {
13496d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman    Arg = InputValue.getAddress();
1350634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson    ConstraintStr += '*';
1351634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson  }
13521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1353634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson  return Arg;
1354634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson}
1355634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson
135642b60551eff3a424e191b293bfd606559dc96bffChad Rosierllvm::Value* CodeGenFunction::EmitAsmInput(
13576d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman                                         const TargetInfo::ConstraintInfo &Info,
13586d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman                                           const Expr *InputExpr,
13596d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman                                           std::string &ConstraintStr) {
13606d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman  if (Info.allowsRegister() || !Info.allowsMemory())
13616d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman    if (!CodeGenFunction::hasAggregateLLVMType(InputExpr->getType()))
13626d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman      return EmitScalarExpr(InputExpr);
13636d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman
13646d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman  InputExpr = InputExpr->IgnoreParenNoopCasts(getContext());
13656d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman  LValue Dest = EmitLValue(InputExpr);
136642b60551eff3a424e191b293bfd606559dc96bffChad Rosier  return EmitAsmInputLValue(Info, Dest, InputExpr->getType(), ConstraintStr);
13676d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman}
13686d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman
136947fc7e93b6af9e6a967231133a2f4bc626eeed26Chris Lattner/// getAsmSrcLocInfo - Return the !srcloc metadata node to attach to an inline
13705d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner/// asm call instruction.  The !srcloc MDNode contains a list of constant
13715d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner/// integers which are the source locations of the start of each line in the
13725d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner/// asm.
137347fc7e93b6af9e6a967231133a2f4bc626eeed26Chris Lattnerstatic llvm::MDNode *getAsmSrcLocInfo(const StringLiteral *Str,
137447fc7e93b6af9e6a967231133a2f4bc626eeed26Chris Lattner                                      CodeGenFunction &CGF) {
13755f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Value *, 8> Locs;
13765d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner  // Add the location of the first line to the MDNode.
13775d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner  Locs.push_back(llvm::ConstantInt::get(CGF.Int32Ty,
13785d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner                                        Str->getLocStart().getRawEncoding()));
13795f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef StrVal = Str->getString();
13805d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner  if (!StrVal.empty()) {
13815d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner    const SourceManager &SM = CGF.CGM.getContext().getSourceManager();
13824e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    const LangOptions &LangOpts = CGF.CGM.getLangOpts();
13836f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
13845d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner    // Add the location of the start of each subsequent line of the asm to the
13855d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner    // MDNode.
13865d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner    for (unsigned i = 0, e = StrVal.size()-1; i != e; ++i) {
13875d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner      if (StrVal[i] != '\n') continue;
13885d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner      SourceLocation LineLoc = Str->getLocationOfByte(i+1, SM, LangOpts,
13895d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner                                                      CGF.Target);
13905d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner      Locs.push_back(llvm::ConstantInt::get(CGF.Int32Ty,
13915d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner                                            LineLoc.getRawEncoding()));
13925d93653247eeedaff5f0712178953b63d71a0b3bChris Lattner    }
13936f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier  }
13946f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
13956f141659cab11109d9931d92d0988f8850778de3Jay Foad  return llvm::MDNode::get(CGF.getLLVMContext(), Locs);
139647fc7e93b6af9e6a967231133a2f4bc626eeed26Chris Lattner}
139747fc7e93b6af9e6a967231133a2f4bc626eeed26Chris Lattner
1398a23b91d542e336be5051ac54f394e860fb756911Chad Rosiervoid CodeGenFunction::EmitAsmStmt(const AsmStmt &S) {
1399be3ace834ee7438915e73d2115365d57d03ceb99Chad Rosier  // Assemble the final asm string.
1400da083b2ce8db27ce6e508cb77cb12c0fc8b7cad9Chad Rosier  std::string AsmString = S.generateAsmString(getContext());
14011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1402481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner  // Get all the output and input constraints together.
14035f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos;
14045f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos;
1405481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner
14061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) {
1407481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner    TargetInfo::ConstraintInfo Info(S.getOutputConstraint(i),
1408481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner                                    S.getOutputName(i));
1409b992259f7790d3fb9fc5c2eb7182d7af9d64f9acChris Lattner    bool IsValid = Target.validateOutputConstraint(Info); (void)IsValid;
1410b992259f7790d3fb9fc5c2eb7182d7af9d64f9acChris Lattner    assert(IsValid && "Failed to parse output constraint");
1411481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner    OutputConstraintInfos.push_back(Info);
14121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
14131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1414481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner  for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) {
1415481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner    TargetInfo::ConstraintInfo Info(S.getInputConstraint(i),
1416481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner                                    S.getInputName(i));
1417b992259f7790d3fb9fc5c2eb7182d7af9d64f9acChris Lattner    bool IsValid = Target.validateInputConstraint(OutputConstraintInfos.data(),
1418b992259f7790d3fb9fc5c2eb7182d7af9d64f9acChris Lattner                                                  S.getNumOutputs(), Info);
1419b992259f7790d3fb9fc5c2eb7182d7af9d64f9acChris Lattner    assert(IsValid && "Failed to parse input constraint"); (void)IsValid;
1420481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner    InputConstraintInfos.push_back(Info);
1421481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner  }
14221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1423fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  std::string Constraints;
14241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1425ede9d900809c4fd0298d52f5a63088ecb8302275Chris Lattner  std::vector<LValue> ResultRegDests;
1426ede9d900809c4fd0298d52f5a63088ecb8302275Chris Lattner  std::vector<QualType> ResultRegQualTys;
1427ef6de3da8572607f786303c07150daa6e140ab19Jay Foad  std::vector<llvm::Type *> ResultRegTypes;
1428ef6de3da8572607f786303c07150daa6e140ab19Jay Foad  std::vector<llvm::Type *> ResultTruncRegTypes;
1429d1c0c940ebc8e55d5c4672f61abedf87d6ea36f4Chad Rosier  std::vector<llvm::Type *> ArgTypes;
1430fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  std::vector<llvm::Value*> Args;
1431f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson
1432f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson  // Keep track of inout constraints.
1433f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson  std::string InOutConstraints;
1434f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson  std::vector<llvm::Value*> InOutArgs;
14359cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  std::vector<llvm::Type*> InOutArgTypes;
143603eb543cf7ebee463b33b5802b83ac92c21770cfAnders Carlsson
14371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) {
1438481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner    TargetInfo::ConstraintInfo &Info = OutputConstraintInfos[i];
143903eb543cf7ebee463b33b5802b83ac92c21770cfAnders Carlsson
1440fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    // Simplify the output constraint.
1441481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner    std::string OutputConstraint(S.getOutputConstraint(i));
1442a5694b8b0096215137bf1c273764ec93ac4898fdLauro Ramos Venancio    OutputConstraint = SimplifyConstraint(OutputConstraint.c_str() + 1, Target);
14431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1444810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    const Expr *OutExpr = S.getOutputExpr(i);
1445810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    OutExpr = OutExpr->IgnoreParenNoopCasts(getContext());
14461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1447a18f539628a6506bae6af52eacd541cebefff762Eric Christopher    OutputConstraint = AddVariableConstraints(OutputConstraint, *OutExpr,
1448a18f539628a6506bae6af52eacd541cebefff762Eric Christopher                                              Target, CGM, S);
14490ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola
1450810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    LValue Dest = EmitLValue(OutExpr);
1451ede9d900809c4fd0298d52f5a63088ecb8302275Chris Lattner    if (!Constraints.empty())
1452bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson      Constraints += ',';
1453bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson
1454a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    // If this is a register output, then make the inline asm return it
1455a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    // by-value.  If this is a memory result, return the value by-reference.
1456ede9d900809c4fd0298d52f5a63088ecb8302275Chris Lattner    if (!Info.allowsMemory() && !hasAggregateLLVMType(OutExpr->getType())) {
1457a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      Constraints += "=" + OutputConstraint;
1458ede9d900809c4fd0298d52f5a63088ecb8302275Chris Lattner      ResultRegQualTys.push_back(OutExpr->getType());
1459ede9d900809c4fd0298d52f5a63088ecb8302275Chris Lattner      ResultRegDests.push_back(Dest);
1460a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      ResultRegTypes.push_back(ConvertTypeForMem(OutExpr->getType()));
1461a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      ResultTruncRegTypes.push_back(ResultRegTypes.back());
14621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1463a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      // If this output is tied to an input, and if the input is larger, then
1464a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      // we need to set the actual result type of the inline asm node to be the
1465a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      // same as the input type.
1466a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      if (Info.hasMatchingInput()) {
1467ebfc9857de58a326c84629915d9ffe3f36d8cc26Chris Lattner        unsigned InputNo;
1468ebfc9857de58a326c84629915d9ffe3f36d8cc26Chris Lattner        for (InputNo = 0; InputNo != S.getNumInputs(); ++InputNo) {
1469ebfc9857de58a326c84629915d9ffe3f36d8cc26Chris Lattner          TargetInfo::ConstraintInfo &Input = InputConstraintInfos[InputNo];
1470aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner          if (Input.hasTiedOperand() && Input.getTiedOperand() == i)
1471a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner            break;
1472ebfc9857de58a326c84629915d9ffe3f36d8cc26Chris Lattner        }
1473ebfc9857de58a326c84629915d9ffe3f36d8cc26Chris Lattner        assert(InputNo != S.getNumInputs() && "Didn't find matching input!");
14741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1475a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner        QualType InputTy = S.getInputExpr(InputNo)->getType();
1476aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner        QualType OutputType = OutExpr->getType();
14771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1478a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner        uint64_t InputSize = getContext().getTypeSize(InputTy);
1479aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner        if (getContext().getTypeSize(OutputType) < InputSize) {
1480aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner          // Form the asm to return the value as a larger integer or fp type.
1481aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner          ResultRegTypes.back() = ConvertType(InputTy);
1482a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner        }
1483a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      }
1484ef6de3da8572607f786303c07150daa6e140ab19Jay Foad      if (llvm::Type* AdjTy =
14854b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne            getTargetHooks().adjustInlineAsmType(*this, OutputConstraint,
14864b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne                                                 ResultRegTypes.back()))
1487f6e2c2039f76fa58799f6d155892d54fc95755e1Dale Johannesen        ResultRegTypes.back() = AdjTy;
1488fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    } else {
1489fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson      ArgTypes.push_back(Dest.getAddress()->getType());
1490cad3ab611ebd3bee3ce6395d649640047f904cdeAnders Carlsson      Args.push_back(Dest.getAddress());
1491f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson      Constraints += "=*";
1492fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson      Constraints += OutputConstraint;
1493f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson    }
14941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
149544def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner    if (Info.isReadWrite()) {
1496f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson      InOutConstraints += ',';
1497634717238844cf3f51039411be1b27fe1fac622eAnders Carlsson
1498fca9361839ecc53224c764517e62fc0e15166004Anders Carlsson      const Expr *InputExpr = S.getOutputExpr(i);
149942b60551eff3a424e191b293bfd606559dc96bffChad Rosier      llvm::Value *Arg = EmitAsmInputLValue(Info, Dest, InputExpr->getType(),
15006d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman                                            InOutConstraints);
15011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1502acb5310542b82e7acf5f5a91fa619e4b7efeff5cBill Wendling      if (llvm::Type* AdjTy =
1503acb5310542b82e7acf5f5a91fa619e4b7efeff5cBill Wendling            getTargetHooks().adjustInlineAsmType(*this, OutputConstraint,
1504acb5310542b82e7acf5f5a91fa619e4b7efeff5cBill Wendling                                                 Arg->getType()))
1505acb5310542b82e7acf5f5a91fa619e4b7efeff5cBill Wendling        Arg = Builder.CreateBitCast(Arg, AdjTy);
1506acb5310542b82e7acf5f5a91fa619e4b7efeff5cBill Wendling
150744def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner      if (Info.allowsRegister())
15089f2505b934745b18d580ade4dac7b8b16952a30cAnders Carlsson        InOutConstraints += llvm::utostr(i);
15099f2505b934745b18d580ade4dac7b8b16952a30cAnders Carlsson      else
15109f2505b934745b18d580ade4dac7b8b16952a30cAnders Carlsson        InOutConstraints += OutputConstraint;
15112763b3af0a527c3a63cb058b90c22db0b7bcf558Anders Carlsson
1512fca9361839ecc53224c764517e62fc0e15166004Anders Carlsson      InOutArgTypes.push_back(Arg->getType());
1513fca9361839ecc53224c764517e62fc0e15166004Anders Carlsson      InOutArgs.push_back(Arg);
1514f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson    }
1515fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  }
15161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1517fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  unsigned NumConstraints = S.getNumOutputs() + S.getNumInputs();
15181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1519fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) {
1520fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    const Expr *InputExpr = S.getInputExpr(i);
1521fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson
1522481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner    TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i];
1523481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner
1524ede9d900809c4fd0298d52f5a63088ecb8302275Chris Lattner    if (!Constraints.empty())
1525fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson      Constraints += ',';
15261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1527fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    // Simplify the input constraint.
1528481fef9e25128fe87b19e41c48f771ee20c33cbeChris Lattner    std::string InputConstraint(S.getInputConstraint(i));
1529300fb5d0ef7edc87f3fdba17fc8b1184013b35aeAnders Carlsson    InputConstraint = SimplifyConstraint(InputConstraint.c_str(), Target,
15302819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner                                         &OutputConstraintInfos);
1531fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson
15320ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola    InputConstraint =
153303117d1b2e32d18652401b12d9049871992bf3adRafael Espindola      AddVariableConstraints(InputConstraint,
15340ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola                            *InputExpr->IgnoreParenNoopCasts(getContext()),
15350ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola                            Target, CGM, S);
15360ec89f928ce5b3294c2ed644ec1a42656e1af39dRafael Espindola
153742b60551eff3a424e191b293bfd606559dc96bffChad Rosier    llvm::Value *Arg = EmitAsmInput(Info, InputExpr, Constraints);
15381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15394df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner    // If this input argument is tied to a larger output result, extend the
15404df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner    // input to be the same size as the output.  The LLVM backend wants to see
15414df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner    // the input and output of a matching constraint be the same size.  Note
15424df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner    // that GCC does not define what the top bits are here.  We use zext because
15434df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner    // that is usually cheaper, but LLVM IR should really get an anyext someday.
15444df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner    if (Info.hasTiedOperand()) {
15454df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner      unsigned Output = Info.getTiedOperand();
1546aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      QualType OutputType = S.getOutputExpr(Output)->getType();
15474df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner      QualType InputTy = InputExpr->getType();
15481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1549aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      if (getContext().getTypeSize(OutputType) >
15504df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner          getContext().getTypeSize(InputTy)) {
15514df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner        // Use ptrtoint as appropriate so that we can do our extension.
15524df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner        if (isa<llvm::PointerType>(Arg->getType()))
155377b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner          Arg = Builder.CreatePtrToInt(Arg, IntPtrTy);
15542acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner        llvm::Type *OutputTy = ConvertType(OutputType);
1555aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner        if (isa<llvm::IntegerType>(OutputTy))
1556aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner          Arg = Builder.CreateZExt(Arg, OutputTy);
155793f1322684e928a559286ba1c7cb83af077aa658Peter Collingbourne        else if (isa<llvm::PointerType>(OutputTy))
155893f1322684e928a559286ba1c7cb83af077aa658Peter Collingbourne          Arg = Builder.CreateZExt(Arg, IntPtrTy);
155993f1322684e928a559286ba1c7cb83af077aa658Peter Collingbourne        else {
156093f1322684e928a559286ba1c7cb83af077aa658Peter Collingbourne          assert(OutputTy->isFloatingPointTy() && "Unexpected output type");
1561aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner          Arg = Builder.CreateFPExt(Arg, OutputTy);
156293f1322684e928a559286ba1c7cb83af077aa658Peter Collingbourne        }
15634df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner      }
15644df4ee0ff6f804e9d3dd478712e3b5b20cd3bf2fChris Lattner    }
1565acb5310542b82e7acf5f5a91fa619e4b7efeff5cBill Wendling    if (llvm::Type* AdjTy =
15664b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne              getTargetHooks().adjustInlineAsmType(*this, InputConstraint,
15674b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne                                                   Arg->getType()))
1568f6e2c2039f76fa58799f6d155892d54fc95755e1Dale Johannesen      Arg = Builder.CreateBitCast(Arg, AdjTy);
15691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1570fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    ArgTypes.push_back(Arg->getType());
1571fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    Args.push_back(Arg);
1572fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    Constraints += InputConstraint;
1573fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  }
15741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1575f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson  // Append the "input" part of inout constraints last.
1576f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson  for (unsigned i = 0, e = InOutArgs.size(); i != e; i++) {
1577f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson    ArgTypes.push_back(InOutArgTypes[i]);
1578f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson    Args.push_back(InOutArgs[i]);
1579f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson  }
1580f39a4211bdea6c136562a5225e5a84d54e62dc8fAnders Carlsson  Constraints += InOutConstraints;
15811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1582fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  // Clobbers
1583fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  for (unsigned i = 0, e = S.getNumClobbers(); i != e; i++) {
158433f0558f75f70061707d1388e305b8f92f4e55deChad Rosier    StringRef Clobber = S.getClobber(i);
1585fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson
1586de31fd7eeebdc64fb043463e7f515dab8eccac8dEric Christopher    if (Clobber != "memory" && Clobber != "cc")
158783c021c6d33aa173cf1a6e3bc61006dabb042703Anders Carlsson    Clobber = Target.getNormalizedGCCRegisterName(Clobber);
15881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1589ea041758d49215167e473a515b8d46e77b170ccfAnders Carlsson    if (i != 0 || NumConstraints != 0)
1590fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson      Constraints += ',';
15911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1592ea041758d49215167e473a515b8d46e77b170ccfAnders Carlsson    Constraints += "~{";
1593fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    Constraints += Clobber;
1594ea041758d49215167e473a515b8d46e77b170ccfAnders Carlsson    Constraints += '}';
1595fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  }
15961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1597fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  // Add machine specific clobbers
1598ccf614c479ac93326a01e7b373b30759eed7807fEli Friedman  std::string MachineClobbers = Target.getClobbers();
1599ccf614c479ac93326a01e7b373b30759eed7807fEli Friedman  if (!MachineClobbers.empty()) {
1600fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    if (!Constraints.empty())
1601fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson      Constraints += ',';
1602ccf614c479ac93326a01e7b373b30759eed7807fEli Friedman    Constraints += MachineClobbers;
1603fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  }
1604bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson
16052acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *ResultType;
1606a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner  if (ResultRegTypes.empty())
16078b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    ResultType = VoidTy;
1608a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner  else if (ResultRegTypes.size() == 1)
1609a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    ResultType = ResultRegTypes[0];
1610bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson  else
1611d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall    ResultType = llvm::StructType::get(getLLVMContext(), ResultRegTypes);
16121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16132acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FTy =
1614fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson    llvm::FunctionType::get(ResultType, ArgTypes, false);
16151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16162ab7d43e450333d52fdf087bf2558a74dbe3c9fdChad Rosier  bool HasSideEffect = S.isVolatile() || S.getNumOutputs() == 0;
1617fcf75a3d8c072eabbf25c9b33ccbb1b8bd042cfcChad Rosier  llvm::InlineAsm::AsmDialect AsmDialect = isa<MSAsmStmt>(&S) ?
1618fcf75a3d8c072eabbf25c9b33ccbb1b8bd042cfcChad Rosier    llvm::InlineAsm::AD_Intel : llvm::InlineAsm::AD_ATT;
16191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::InlineAsm *IA =
1620790cbd84d07f0dc9253ade9f67e6ed8c43f08a59Chad Rosier    llvm::InlineAsm::get(FTy, AsmString, Constraints, HasSideEffect,
1621fcf75a3d8c072eabbf25c9b33ccbb1b8bd042cfcChad Rosier                         /* IsAlignStack */ false, AsmDialect);
16224c7d9f1507d0f102bd4133bba63348636facd469Jay Foad  llvm::CallInst *Result = Builder.CreateCall(IA, Args);
1623bc0822bad87ac4d2dcac8e1b71960301656a2699Anders Carlsson  Result->addAttribute(~0, llvm::Attribute::NoUnwind);
16241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1625fc1a9c312d249a2c7458f763f848ba42685c23f8Chris Lattner  // Slap the source location of the inline asm into a !srcloc metadata on the
1626a23b91d542e336be5051ac54f394e860fb756911Chad Rosier  // call.  FIXME: Handle metadata for MS-style inline asms.
1627a23b91d542e336be5051ac54f394e860fb756911Chad Rosier  if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(&S))
1628a23b91d542e336be5051ac54f394e860fb756911Chad Rosier    Result->setMetadata("srcloc", getAsmSrcLocInfo(gccAsmStmt->getAsmString(),
1629a23b91d542e336be5051ac54f394e860fb756911Chad Rosier                                                   *this));
16301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1631a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner  // Extract all of the register value results from the asm.
1632a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner  std::vector<llvm::Value*> RegResults;
1633a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner  if (ResultRegTypes.size() == 1) {
1634a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    RegResults.push_back(Result);
1635bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson  } else {
1636a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    for (unsigned i = 0, e = ResultRegTypes.size(); i != e; ++i) {
1637bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson      llvm::Value *Tmp = Builder.CreateExtractValue(Result, i, "asmresult");
1638a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      RegResults.push_back(Tmp);
1639a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    }
1640a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner  }
16411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1642a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner  for (unsigned i = 0, e = RegResults.size(); i != e; ++i) {
1643a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    llvm::Value *Tmp = RegResults[i];
16441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1645a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    // If the result type of the LLVM IR asm doesn't match the result type of
1646a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    // the expression, do the conversion.
1647a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner    if (ResultRegTypes[i] != ResultTruncRegTypes[i]) {
16482acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner      llvm::Type *TruncTy = ResultTruncRegTypes[i];
16496f61ba2e57aac6fc76638addce18ed59137ab9c1Chad Rosier
1650aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      // Truncate the integer result to the right size, note that TruncTy can be
1651aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      // a pointer.
1652aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      if (TruncTy->isFloatingPointTy())
1653aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner        Tmp = Builder.CreateFPTrunc(Tmp, TruncTy);
16542dca88fc6d3e606a8131712be1800e3a4b90ca3eDan Gohman      else if (TruncTy->isPointerTy() && Tmp->getType()->isIntegerTy()) {
1655aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner        uint64_t ResSize = CGM.getTargetData().getTypeSizeInBits(TruncTy);
1656d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall        Tmp = Builder.CreateTrunc(Tmp,
1657d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall                   llvm::IntegerType::get(getLLVMContext(), (unsigned)ResSize));
1658a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner        Tmp = Builder.CreateIntToPtr(Tmp, TruncTy);
16592dca88fc6d3e606a8131712be1800e3a4b90ca3eDan Gohman      } else if (Tmp->getType()->isPointerTy() && TruncTy->isIntegerTy()) {
16602dca88fc6d3e606a8131712be1800e3a4b90ca3eDan Gohman        uint64_t TmpSize =CGM.getTargetData().getTypeSizeInBits(Tmp->getType());
1661d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall        Tmp = Builder.CreatePtrToInt(Tmp,
1662d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall                   llvm::IntegerType::get(getLLVMContext(), (unsigned)TmpSize));
16632dca88fc6d3e606a8131712be1800e3a4b90ca3eDan Gohman        Tmp = Builder.CreateTrunc(Tmp, TruncTy);
16642dca88fc6d3e606a8131712be1800e3a4b90ca3eDan Gohman      } else if (TruncTy->isIntegerTy()) {
16652dca88fc6d3e606a8131712be1800e3a4b90ca3eDan Gohman        Tmp = Builder.CreateTrunc(Tmp, TruncTy);
1666f6e2c2039f76fa58799f6d155892d54fc95755e1Dale Johannesen      } else if (TruncTy->isVectorTy()) {
1667f6e2c2039f76fa58799f6d155892d54fc95755e1Dale Johannesen        Tmp = Builder.CreateBitCast(Tmp, TruncTy);
1668a077b5c8631596f8d7a588933a9de5d08e9ba428Chris Lattner      }
1669bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson    }
16701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1671545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall    EmitStoreThroughLValue(RValue::get(Tmp), ResultRegDests[i]);
1672bad3a94d506874355fc15b336c6f0ed360e46a06Anders Carlsson  }
1673fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson}
1674