[cfe-commits] r42616 - in /cfe/trunk: CodeGen/CGStmt.cpp CodeGen/CodeGenFunction.cpp CodeGen/CodeGenFunction.h include/clang/AST/Stmt.h test/CodeGen/switch.c

Devang Patel dpatel at apple.com
Thu Oct 4 16:45:32 PDT 2007


Author: dpatel
Date: Thu Oct  4 18:45:31 2007
New Revision: 42616

URL: http://llvm.org/viewvc/llvm-project?rev=42616&view=rev
Log:
switch statement code gen.

Added:
    cfe/trunk/test/CodeGen/switch.c
Modified:
    cfe/trunk/CodeGen/CGStmt.cpp
    cfe/trunk/CodeGen/CodeGenFunction.cpp
    cfe/trunk/CodeGen/CodeGenFunction.h
    cfe/trunk/include/clang/AST/Stmt.h

Modified: cfe/trunk/CodeGen/CGStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CGStmt.cpp?rev=42616&r1=42615&r2=42616&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CGStmt.cpp (original)
+++ cfe/trunk/CodeGen/CGStmt.cpp Thu Oct  4 18:45:31 2007
@@ -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;
+}

Modified: cfe/trunk/CodeGen/CodeGenFunction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CodeGenFunction.cpp?rev=42616&r1=42615&r2=42616&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CodeGenFunction.cpp (original)
+++ cfe/trunk/CodeGen/CodeGenFunction.cpp Thu Oct  4 18:45:31 2007
@@ -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);
+}
+

Modified: cfe/trunk/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CodeGenFunction.h?rev=42616&r1=42615&r2=42616&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/CodeGen/CodeGenFunction.h Thu Oct  4 18:45:31 2007
@@ -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
   //===--------------------------------------------------------------------===//

Modified: cfe/trunk/include/clang/AST/Stmt.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Stmt.h?rev=42616&r1=42615&r2=42616&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Stmt.h (original)
+++ cfe/trunk/include/clang/AST/Stmt.h Thu Oct  4 18:45:31 2007
@@ -271,6 +271,13 @@
   Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
   Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
   virtual Stmt* v_getSubStmt() { return getSubStmt(); }
+  const Expr *getLHS() const { 
+    return reinterpret_cast<const Expr*>(SubExprs[LHS]); 
+  }
+  const Expr *getRHS() const { 
+    return reinterpret_cast<const Expr*>(SubExprs[RHS]); 
+  }
+  const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
 
   virtual SourceRange getSourceRange() const { 
     return SourceRange(CaseLoc, SubExprs[SUBSTMT]->getLocEnd()); 
@@ -294,6 +301,7 @@
     
   Stmt *getSubStmt() { return SubStmt; }
   virtual Stmt* v_getSubStmt() { return getSubStmt(); }
+  const Stmt *getSubStmt() const { return SubStmt; }
     
   SourceLocation getDefaultLoc() const { return DefaultLoc; }
 

Added: cfe/trunk/test/CodeGen/switch.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/switch.c?rev=42616&view=auto

==============================================================================
--- cfe/trunk/test/CodeGen/switch.c (added)
+++ cfe/trunk/test/CodeGen/switch.c Thu Oct  4 18:45:31 2007
@@ -0,0 +1,17 @@
+// RUN: clang %s -emit-llvm | llvm-as | opt -std-compile-opts -disable-output
+
+int foo(int i) {
+  int j = 0;
+  switch (i) {
+  case 1 : 
+    j = 2; break;
+  case 2:
+    j = 3; break;
+  default:
+    j = 42; break;
+  }
+  j = j + 1;
+  return j;
+}
+
+    





More information about the cfe-commits mailing list