[cfe-commits] r59037 - in /cfe/trunk/lib/CodeGen: CGExprAgg.cpp CGExprComplex.cpp CGExprScalar.cpp CGObjCMac.cpp CGStmt.cpp CodeGenFunction.h

Daniel Dunbar daniel at zuster.org
Tue Nov 11 01:41:29 PST 2008


Author: ddunbar
Date: Tue Nov 11 03:41:28 2008
New Revision: 59037

URL: http://llvm.org/viewvc/llvm-project?rev=59037&view=rev
Log:
Add CodeGenFunction::EmitBranch.
 - Emits an unconditional branch, with extra logic to avoid generating
   spurious branches out of dummy blocks.

Modified:
    cfe/trunk/lib/CodeGen/CGExprAgg.cpp
    cfe/trunk/lib/CodeGen/CGExprComplex.cpp
    cfe/trunk/lib/CodeGen/CGExprScalar.cpp
    cfe/trunk/lib/CodeGen/CGObjCMac.cpp
    cfe/trunk/lib/CodeGen/CGStmt.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.h

Modified: cfe/trunk/lib/CodeGen/CGExprAgg.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprAgg.cpp?rev=59037&r1=59036&r2=59037&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprAgg.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprAgg.cpp Tue Nov 11 03:41:28 2008
@@ -242,14 +242,12 @@
   assert(E->getLHS() && "Must have LHS for aggregate value");
 
   Visit(E->getLHS());
-  Builder.CreateBr(ContBlock);
-  LHSBlock = Builder.GetInsertBlock();
+  CGF.EmitBranch(ContBlock);
   
   CGF.EmitBlock(RHSBlock);
   
   Visit(E->getRHS());
-  Builder.CreateBr(ContBlock);
-  RHSBlock = Builder.GetInsertBlock();
+  CGF.EmitBranch(ContBlock);
   
   CGF.EmitBlock(ContBlock);
 }

Modified: cfe/trunk/lib/CodeGen/CGExprComplex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprComplex.cpp?rev=59037&r1=59036&r2=59037&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprComplex.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprComplex.cpp Tue Nov 11 03:41:28 2008
@@ -479,14 +479,14 @@
   assert(E->getLHS() && "Must have LHS for complex value");
 
   ComplexPairTy LHS = Visit(E->getLHS());
-  Builder.CreateBr(ContBlock);
   LHSBlock = Builder.GetInsertBlock();
+  CGF.EmitBranch(ContBlock);
   
   CGF.EmitBlock(RHSBlock);
   
   ComplexPairTy RHS = Visit(E->getRHS());
-  Builder.CreateBr(ContBlock);
   RHSBlock = Builder.GetInsertBlock();
+  CGF.EmitBranch(ContBlock);
   
   CGF.EmitBlock(ContBlock);
   

Modified: cfe/trunk/lib/CodeGen/CGExprScalar.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprScalar.cpp?rev=59037&r1=59036&r2=59037&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprScalar.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprScalar.cpp Tue Nov 11 03:41:28 2008
@@ -1139,14 +1139,14 @@
   else    // Perform promotions, to handle cases like "short ?: int"
     LHS = EmitScalarConversion(CondVal, E->getCond()->getType(), E->getType());
   
-  Builder.CreateBr(ContBlock);
   LHSBlock = Builder.GetInsertBlock();
+  CGF.EmitBranch(ContBlock);
   
   CGF.EmitBlock(RHSBlock);
   
   Value *RHS = Visit(E->getRHS());
-  Builder.CreateBr(ContBlock);
   RHSBlock = Builder.GetInsertBlock();
+  CGF.EmitBranch(ContBlock);
   
   CGF.EmitBlock(ContBlock);
   

Modified: cfe/trunk/lib/CodeGen/CGObjCMac.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGObjCMac.cpp?rev=59037&r1=59036&r2=59037&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGObjCMac.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGObjCMac.cpp Tue Nov 11 03:41:28 2008
@@ -1725,7 +1725,7 @@
                                              bool ExecuteTryExit) {
   llvm::BasicBlock *Src = Builder.GetInsertBlock();
     
-  if (isDummyBlock(Src))
+  if (!Src || isDummyBlock(Src))
     return;
   
   // Find the destination code for this block. We always use 0 for the

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGStmt.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGStmt.cpp Tue Nov 11 03:41:28 2008
@@ -149,22 +149,30 @@
 }
 
 void CodeGenFunction::EmitBlock(llvm::BasicBlock *BB) {
-  // Emit a branch from this block to the next one if this was a real block.  If
-  // this was just a fall-through block after a terminator, don't emit it.
-  llvm::BasicBlock *LastBB = Builder.GetInsertBlock();
-  
-  if (LastBB->getTerminator()) {
-    // If the previous block is already terminated, don't touch it.
-  } else if (isDummyBlock(LastBB)) {
+  // Fall out of the current block (if necessary).
+  EmitBranch(BB);
+  CurFn->getBasicBlockList().push_back(BB);
+  Builder.SetInsertPoint(BB);
+}
+
+void CodeGenFunction::EmitBranch(llvm::BasicBlock *Target) {
+  // Emit a branch from the current block to the target one if this
+  // was a real block.  If this was just a fall-through block after a
+  // terminator, don't emit it.
+  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
+
+  if (!CurBB || CurBB->getTerminator()) {
+    // If there is no insert point or the previous block is already
+    // terminated, don't touch it.
+  } else if (isDummyBlock(CurBB)) {
     // If the last block was an empty placeholder, remove it now.
     // TODO: cache and reuse these.
-    LastBB->eraseFromParent();
+    CurBB->eraseFromParent();
+    Builder.ClearInsertionPoint();
   } else {
     // Otherwise, create a fall-through branch.
-    Builder.CreateBr(BB);
+    Builder.CreateBr(Target);
   }
-  CurFn->getBasicBlockList().push_back(BB);
-  Builder.SetInsertPoint(BB);
 }
 
 void CodeGenFunction::EmitDummyBlock() {
@@ -189,7 +197,7 @@
     return;
   }
 
-  Builder.CreateBr(getBasicBlockForLabel(S.getLabel()));
+  EmitBranch(getBasicBlockForLabel(S.getLabel()));
   
   // Emit a block after the branch so that dead code after a goto has some place
   // to go.
@@ -252,25 +260,13 @@
   // Emit the 'then' code.
   EmitBlock(ThenBlock);
   EmitStmt(S.getThen());
-  llvm::BasicBlock *BB = Builder.GetInsertBlock();
-  if (isDummyBlock(BB)) {
-    BB->eraseFromParent();
-    Builder.SetInsertPoint(ThenBlock);
-  } else {
-    Builder.CreateBr(ContBlock);
-  }
+  EmitBranch(ContBlock);
   
   // Emit the 'else' code if present.
   if (const Stmt *Else = S.getElse()) {
     EmitBlock(ElseBlock);
     EmitStmt(Else);
-    llvm::BasicBlock *BB = Builder.GetInsertBlock();
-    if (isDummyBlock(BB)) {
-      BB->eraseFromParent();
-      Builder.SetInsertPoint(ElseBlock);
-    } else {
-      Builder.CreateBr(ContBlock);
-    }
+    EmitBranch(ContBlock);
   }
   
   // Emit the continuation block for code after the if.
@@ -314,7 +310,7 @@
   BreakContinueStack.pop_back();
   
   // Cycle to the condition.
-  Builder.CreateBr(LoopHeader);
+  EmitBranch(LoopHeader);
   
   // Emit the exit block.
   EmitBlock(ExitBlock);
@@ -433,7 +429,7 @@
   }
       
   // Finally, branch back up to the condition for the next iteration.
-  Builder.CreateBr(CondBlock);
+  EmitBranch(CondBlock);
 
   // Emit the fall-through block.
   EmitBlock(AfterFor);
@@ -447,7 +443,7 @@
   } else {
     StoreComplexToAddr(RV.getComplexVal(), ReturnValue, false);
   }
-  Builder.CreateBr(ReturnBlock);
+  EmitBranch(ReturnBlock);
 
   // Emit a block after the branch so that dead code after a return has some
   // place to go.
@@ -487,7 +483,7 @@
     }
   } 
 
-  Builder.CreateBr(ReturnBlock);
+  EmitBranch(ReturnBlock);
   
   // Emit a block after the branch so that dead code after a return has some
   // place to go.
@@ -504,7 +500,7 @@
   assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!");
 
   llvm::BasicBlock *Block = BreakContinueStack.back().BreakBlock;
-  Builder.CreateBr(Block);
+  EmitBranch(Block);
   EmitDummyBlock();
 }
 
@@ -512,7 +508,7 @@
   assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
 
   llvm::BasicBlock *Block = BreakContinueStack.back().ContinueBlock;
-  Builder.CreateBr(Block);
+  EmitBranch(Block);
   EmitDummyBlock();
 }
 

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Tue Nov 11 03:41:28 2008
@@ -226,6 +226,11 @@
   
   void EmitBlock(llvm::BasicBlock *BB);
 
+  /// EmitBranch - Emit a branch to the specified basic block from the
+  /// current insert block, taking care to avoid creation of branches
+  /// from dummy blocks.
+  void EmitBranch(llvm::BasicBlock *Block);
+
   /// EmitDummyBlock - Emit a new block which will never be branched
   /// to. This is used to satisfy the invariant that codegen always
   /// has an active unterminated block to dump code into.





More information about the cfe-commits mailing list