[cfe-commits] r42808 - /cfe/trunk/CodeGen/CGStmt.cpp

Devang Patel dpatel at apple.com
Tue Oct 9 13:33:40 PDT 2007


Author: dpatel
Date: Tue Oct  9 15:33:39 2007
New Revision: 42808

URL: http://llvm.org/viewvc/llvm-project?rev=42808&view=rev
Log:
Recognize "do {} while (0)"  idiom and avoid extra basic blocks.

Modified:
    cfe/trunk/CodeGen/CGStmt.cpp

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

==============================================================================
--- cfe/trunk/CodeGen/CGStmt.cpp (original)
+++ cfe/trunk/CodeGen/CGStmt.cpp Tue Oct  9 15:33:39 2007
@@ -198,9 +198,6 @@
 }
 
 void CodeGenFunction::EmitDoStmt(const DoStmt &S) {
-  // TODO: "do {} while (0)" is common in macros, avoid extra blocks.  Be sure
-  // to correctly handle break/continue though.
-
   // Emit the body for the loop, insert it, which will create an uncond br to
   // it.
   llvm::BasicBlock *LoopBody = new llvm::BasicBlock("dobody");
@@ -226,12 +223,28 @@
   // C99 6.8.5p2/p4: The first substatement is executed if the expression
   // compares unequal to 0.  The condition must be a scalar type.
   llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
-  
+
+  // "do {} while (0)" is common in macros, avoid extra blocks.  Be sure
+  // to correctly handle break/continue though.
+  bool EmitBoolCondBranch = true;
+  if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal)) 
+    if (C->isZero())
+      EmitBoolCondBranch = false;
+
+
   // As long as the condition is true, iterate the loop.
-  Builder.CreateCondBr(BoolCondVal, LoopBody, AfterDo);
+  if (EmitBoolCondBranch)
+    Builder.CreateCondBr(BoolCondVal, LoopBody, AfterDo);
   
   // Emit the exit block.
   EmitBlock(AfterDo);
+
+  // If DoCond is a simple forwarding block then eliminate it.
+  if (!EmitBoolCondBranch && &DoCond->front() == DoCond->getTerminator()) {
+    DoCond->replaceAllUsesWith(AfterDo);
+    DoCond->getTerminator()->eraseFromParent();
+    DoCond->eraseFromParent();
+  }
 }
 
 void CodeGenFunction::EmitForStmt(const ForStmt &S) {





More information about the cfe-commits mailing list