switch statement code gen.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@42616 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/CodeGen/CGStmt.cpp b/CodeGen/CGStmt.cpp
index f1f915e..2b9a72b 100644
--- a/CodeGen/CGStmt.cpp
+++ b/CodeGen/CGStmt.cpp
@@ -57,6 +57,9 @@
       
   case Stmt::BreakStmtClass:    EmitBreakStmt();                          break;
   case Stmt::ContinueStmtClass: EmitContinueStmt();                       break;
+  case Stmt::SwitchStmtClass:   EmitSwitchStmt(cast<SwitchStmt>(*S));     break;
+  case Stmt::DefaultStmtClass:  EmitDefaultStmt(cast<DefaultStmt>(*S));   break;
+  case Stmt::CaseStmtClass:     EmitCaseStmt(cast<CaseStmt>(*S));         break;
   }
 }
 
@@ -350,3 +353,50 @@
   Builder.CreateBr(Block);
   EmitBlock(new llvm::BasicBlock());
 }
+
+void CodeGenFunction::EmitCaseStmt(const CaseStmt &S) {
+  StartBlock("sw.bb");
+  llvm::Value *V = EmitScalarExpr(S.getLHS());
+  SwitchInsn->addCase(cast<llvm::ConstantInt>(V), Builder.GetInsertBlock());
+  assert (!S.getRHS() && "Case statement range is not yet supported");
+  EmitStmt(S.getSubStmt());
+}
+
+void CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S) {
+  StartBlock("sw.default");
+  // Current insert block is the default destination.
+  SwitchInsn->setSuccessor(0, Builder.GetInsertBlock());
+  EmitStmt(S.getSubStmt());
+}
+
+void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) {
+  llvm::Value *CondV = EmitScalarExpr(S.getCond());
+
+  // Handle nested switch statements.
+  llvm::SwitchInst *SavedSwitchInsn = SwitchInsn;
+
+  // Create basic block to hold stuff that comes after switch statement.
+  // Initially use it to hold DefaultStmt.
+  llvm::BasicBlock *NextBlock = new llvm::BasicBlock("after.sw", CurFn);
+  SwitchInsn = Builder.CreateSwitch(CondV, NextBlock);
+
+  // All break statements jump to NextBlock. If BreakContinueStack is non empty then
+  // reuse last ContinueBlock.
+  llvm::BasicBlock *ContinueBlock = NULL;
+  if (!BreakContinueStack.empty())
+    ContinueBlock = BreakContinueStack.back().ContinueBlock;
+  BreakContinueStack.push_back(BreakContinue(NextBlock, ContinueBlock));
+
+  // Emit switch body.
+  EmitStmt(S.getBody());
+  BreakContinueStack.pop_back();
+
+  // Prune insert block if it is dummy.
+  llvm::BasicBlock *BB = Builder.GetInsertBlock();
+  if (isDummyBlock(BB))
+    BB->eraseFromParent();
+
+  // Place NextBlock as the new insert point.
+  Builder.SetInsertPoint(NextBlock);
+  SwitchInsn = SavedSwitchInsn;
+}
diff --git a/CodeGen/CodeGenFunction.cpp b/CodeGen/CodeGenFunction.cpp
index 363d299..e8a00c8 100644
--- a/CodeGen/CodeGenFunction.cpp
+++ b/CodeGen/CodeGenFunction.cpp
@@ -115,3 +115,13 @@
   return false;
 }
 
+/// StartBlock - Start new block named N. If insert block is a dummy block
+/// then reuse it.
+void CodeGenFunction::StartBlock(const char *N) {
+  llvm::BasicBlock *BB = Builder.GetInsertBlock();
+  if (!isDummyBlock(BB))
+    EmitBlock(new llvm::BasicBlock(N));
+  else
+    BB->setName(N);
+}
+
diff --git a/CodeGen/CodeGenFunction.h b/CodeGen/CodeGenFunction.h
index 2f7e073..f804aaa 100644
--- a/CodeGen/CodeGenFunction.h
+++ b/CodeGen/CodeGenFunction.h
@@ -41,7 +41,10 @@
   class ForStmt;
   class ReturnStmt;
   class DeclStmt;
-  
+  class CaseStmt;
+  class DefaultStmt;
+  class SwitchStmt;
+
   class Expr;
   class DeclRefExpr;
   class StringLiteral;
@@ -237,6 +240,10 @@
   }; 
   llvm::SmallVector<BreakContinue, 8> BreakContinueStack;
   
+  // SwitchInsn - This is used by EmitCaseStmt() and EmitDefaultStmt() to
+  // populate switch instruction
+  llvm::SwitchInst *SwitchInsn;
+
 public:
   CodeGenFunction(CodeGenModule &cgm);
   
@@ -281,6 +288,10 @@
   /// with no predecessors.
   static bool isDummyBlock(const llvm::BasicBlock *BB);
 
+  /// StartBlock - Start new block named N. If insert block is a dummy block
+  /// then reuse it.
+  void StartBlock(const char *N);
+
   //===--------------------------------------------------------------------===//
   //                            Declaration Emission
   //===--------------------------------------------------------------------===//
@@ -308,7 +319,10 @@
   void EmitDeclStmt(const DeclStmt &S);
   void EmitBreakStmt();
   void EmitContinueStmt();
-  
+  void EmitSwitchStmt(const SwitchStmt &S);
+  void EmitDefaultStmt(const DefaultStmt &S);
+  void EmitCaseStmt(const CaseStmt &S);
+
   //===--------------------------------------------------------------------===//
   //                         LValue Expression Emission
   //===--------------------------------------------------------------------===//