[cfe-commits] r42811 - in /cfe/trunk: CodeGen/CGStmt.cpp test/CodeGen/whilestmt.c

Devang Patel dpatel at apple.com
Tue Oct 9 13:51:27 PDT 2007


Author: dpatel
Date: Tue Oct  9 15:51:27 2007
New Revision: 42811

URL: http://llvm.org/viewvc/llvm-project?rev=42811&view=rev
Log:
Recognize while(1) and avoid extra blocks.

Added:
    cfe/trunk/test/CodeGen/whilestmt.c
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=42811&r1=42810&r2=42811&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CGStmt.cpp (original)
+++ cfe/trunk/CodeGen/CGStmt.cpp Tue Oct  9 15:51:27 2007
@@ -169,9 +169,13 @@
   // of the controlling expression takes place before each execution of the loop
   // body. 
   llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
-  
-  // TODO: while(1) is common, avoid extra exit blocks, etc.  Be sure
+
+  // while(1) is common, avoid extra exit blocks.  Be sure
   // to correctly handle break/continue though.
+  bool EmitBoolCondBranch = true;
+  if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal)) 
+    if (C->isOne())
+      EmitBoolCondBranch = false;
   
   // Create an exit block for when the condition fails, create a block for the
   // body of the loop.
@@ -179,7 +183,8 @@
   llvm::BasicBlock *LoopBody  = new llvm::BasicBlock("whilebody");
   
   // As long as the condition is true, go to the loop body.
-  Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock);
+  if (EmitBoolCondBranch)
+    Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock);
 
   // Store the blocks to use for break and continue.
   BreakContinueStack.push_back(BreakContinue(ExitBlock, LoopHeader));
@@ -195,6 +200,14 @@
   
   // Emit the exit block.
   EmitBlock(ExitBlock);
+
+  // If LoopHeader is a simple forwarding block then eliminate it.
+  if (!EmitBoolCondBranch 
+      && &LoopHeader->front() == LoopHeader->getTerminator()) {
+    LoopHeader->replaceAllUsesWith(LoopBody);
+    LoopHeader->getTerminator()->eraseFromParent();
+    LoopHeader->eraseFromParent();
+  }
 }
 
 void CodeGenFunction::EmitDoStmt(const DoStmt &S) {
@@ -231,7 +244,6 @@
     if (C->isZero())
       EmitBoolCondBranch = false;
 
-
   // As long as the condition is true, iterate the loop.
   if (EmitBoolCondBranch)
     Builder.CreateCondBr(BoolCondVal, LoopBody, AfterDo);

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

==============================================================================
--- cfe/trunk/test/CodeGen/whilestmt.c (added)
+++ cfe/trunk/test/CodeGen/whilestmt.c Tue Oct  9 15:51:27 2007
@@ -0,0 +1,62 @@
+// RUN: clang %s -emit-llvm
+
+int bar();
+int foo() {
+  int i;
+  i = 1 + 2;
+  while(1) {
+    i = bar();
+    i = bar();
+  };
+  return i;
+}
+
+
+int foo1() {
+  int i;
+  i = 1 + 2;
+  while(1) {
+    i = bar();
+    if (i == 42)
+      break;
+    i = bar();
+  };
+  return i;
+}
+
+
+int foo2() {
+  int i;
+  i = 1 + 2;
+  while(1) {
+    i = bar();
+    if (i == 42)
+      continue;
+    i = bar();
+  };
+  return i;
+}
+
+
+int foo3() {
+  int i;
+  i = 1 + 2;
+  while(1) {
+    i = bar();
+    if (i == 42)
+      break;
+  };
+  return i;
+}
+
+
+int foo4() {
+  int i;
+  i = 1 + 2;
+  while(1) {
+    i = bar();
+    if (i == 42)
+      continue;
+  };
+  return i;
+}





More information about the cfe-commits mailing list