[llvm-commits] [llvm] r51499 - in /llvm/trunk: include/llvm/ lib/Transforms/Instrumentation/ lib/Transforms/Scalar/ lib/Transforms/Utils/ lib/VMCore/

Dan Gohman gohman at apple.com
Fri May 23 14:05:58 PDT 2008


Author: djg
Date: Fri May 23 16:05:58 2008
New Revision: 51499

URL: http://llvm.org/viewvc/llvm-project?rev=51499&view=rev
Log:
Tidy up BasicBlock::getFirstNonPHI, and change a bunch of places to
use it instead of duplicating its functionality.

Modified:
    llvm/trunk/include/llvm/BasicBlock.h
    llvm/trunk/lib/Transforms/Instrumentation/ProfilingUtils.cpp
    llvm/trunk/lib/Transforms/Instrumentation/RSProfiling.cpp
    llvm/trunk/lib/Transforms/Scalar/CodeGenPrepare.cpp
    llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp
    llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
    llvm/trunk/lib/Transforms/Scalar/JumpThreading.cpp
    llvm/trunk/lib/Transforms/Scalar/LICM.cpp
    llvm/trunk/lib/Transforms/Scalar/LoopRotation.cpp
    llvm/trunk/lib/Transforms/Scalar/LoopUnswitch.cpp
    llvm/trunk/lib/Transforms/Scalar/TailDuplication.cpp
    llvm/trunk/lib/Transforms/Utils/CodeExtractor.cpp
    llvm/trunk/lib/Transforms/Utils/LCSSA.cpp
    llvm/trunk/lib/Transforms/Utils/LowerInvoke.cpp
    llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp
    llvm/trunk/lib/VMCore/BasicBlock.cpp

Modified: llvm/trunk/include/llvm/BasicBlock.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/BasicBlock.h?rev=51499&r1=51498&r2=51499&view=diff

==============================================================================
--- llvm/trunk/include/llvm/BasicBlock.h (original)
+++ llvm/trunk/include/llvm/BasicBlock.h Fri May 23 16:05:58 2008
@@ -7,8 +7,8 @@
 //
 //===----------------------------------------------------------------------===//
 //
-//
 // This file contains the declaration of the BasicBlock class.
+//
 //===----------------------------------------------------------------------===//
 
 #ifndef LLVM_BASICBLOCK_H
@@ -106,6 +106,9 @@
   /// the first instruction, which might be PHI.
   /// Returns 0 is there's no non-PHI instruction.
   Instruction* getFirstNonPHI();
+  const Instruction* getFirstNonPHI() const {
+    return const_cast<BasicBlock*>(this)->getFirstNonPHI();
+  }
   
   /// removeFromParent - This method unlinks 'this' from the containing
   /// function, but does not delete it.

Modified: llvm/trunk/lib/Transforms/Instrumentation/ProfilingUtils.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/ProfilingUtils.cpp?rev=51499&r1=51498&r2=51499&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Instrumentation/ProfilingUtils.cpp (original)
+++ llvm/trunk/lib/Transforms/Instrumentation/ProfilingUtils.cpp Fri May 23 16:05:58 2008
@@ -100,8 +100,8 @@
 void llvm::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum,
                                    GlobalValue *CounterArray) {
   // Insert the increment after any alloca or PHI instructions...
-  BasicBlock::iterator InsertPos = BB->begin();
-  while (isa<AllocaInst>(InsertPos) || isa<PHINode>(InsertPos))
+  BasicBlock::iterator InsertPos = BB->getFirstNonPHI();
+  while (isa<AllocaInst>(InsertPos))
     ++InsertPos;
 
   // Create the getelementptr constant expression

Modified: llvm/trunk/lib/Transforms/Instrumentation/RSProfiling.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/RSProfiling.cpp?rev=51499&r1=51498&r2=51499&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Instrumentation/RSProfiling.cpp (original)
+++ llvm/trunk/lib/Transforms/Instrumentation/RSProfiling.cpp Fri May 23 16:05:58 2008
@@ -265,14 +265,11 @@
         new StoreInst(l, Counter, bib);
         
         BasicBlock* bb = cast<InvokeInst>(bib)->getNormalDest();
-        BasicBlock::iterator i = bb->begin();
-        while (isa<PHINode>(i))
-          ++i;
+        BasicBlock::iterator i = bb->getFirstNonPHI();
         l = new LoadInst(Counter, "counter", i);
         
         bb = cast<InvokeInst>(bib)->getUnwindDest();
-        i = bb->begin();
-        while (isa<PHINode>(i)) ++i;
+        i = bb->getFirstNonPHI();
         l = new LoadInst(Counter, "counter", i);
         new StoreInst(l, AI, i);
       } else if (isa<UnwindInst>(&*bib) || isa<ReturnInst>(&*bib)) {
@@ -343,8 +340,8 @@
 void RSProfilers_std::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum,
                                           GlobalValue *CounterArray) {
   // Insert the increment after any alloca or PHI instructions...
-  BasicBlock::iterator InsertPos = BB->begin();
-  while (isa<AllocaInst>(InsertPos) || isa<PHINode>(InsertPos))
+  BasicBlock::iterator InsertPos = BB->getFirstNonPHI();
+  while (isa<AllocaInst>(InsertPos))
     ++InsertPos;
   
   // Create the getelementptr constant expression

Modified: llvm/trunk/lib/Transforms/Scalar/CodeGenPrepare.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/CodeGenPrepare.cpp?rev=51499&r1=51498&r2=51499&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/CodeGenPrepare.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/CodeGenPrepare.cpp Fri May 23 16:05:58 2008
@@ -385,8 +385,7 @@
     CastInst *&InsertedCast = InsertedCasts[UserBB];
 
     if (!InsertedCast) {
-      BasicBlock::iterator InsertPt = UserBB->begin();
-      while (isa<PHINode>(InsertPt)) ++InsertPt;
+      BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI();
       
       InsertedCast = 
         CastInst::Create(CI->getOpcode(), CI->getOperand(0), CI->getType(), "", 
@@ -443,8 +442,7 @@
     CmpInst *&InsertedCmp = InsertedCmps[UserBB];
 
     if (!InsertedCmp) {
-      BasicBlock::iterator InsertPt = UserBB->begin();
-      while (isa<PHINode>(InsertPt)) ++InsertPt;
+      BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI();
       
       InsertedCmp = 
         CmpInst::Create(CI->getOpcode(), CI->getPredicate(), CI->getOperand(0), 
@@ -1039,8 +1037,7 @@
     Instruction *&InsertedTrunc = InsertedTruncs[UserBB];
 
     if (!InsertedTrunc) {
-      BasicBlock::iterator InsertPt = UserBB->begin();
-      while (isa<PHINode>(InsertPt)) ++InsertPt;
+      BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI();
       
       InsertedTrunc = new TruncInst(I, Src->getType(), "", InsertPt);
     }

Modified: llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp?rev=51499&r1=51498&r2=51499&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp Fri May 23 16:05:58 2008
@@ -319,8 +319,7 @@
     BlockToInsertInto = ExitBlocks[0];
   else
     BlockToInsertInto = Preheader;
-  BasicBlock::iterator InsertPt = BlockToInsertInto->begin();
-  while (isa<PHINode>(InsertPt)) ++InsertPt;
+  BasicBlock::iterator InsertPt = BlockToInsertInto->getFirstNonPHI();
 
   bool HasConstantItCount = isa<SCEVConstant>(SE->getIterationCount(L));
 
@@ -535,8 +534,7 @@
   // Now that we have a canonical induction variable, we can rewrite any
   // recurrences in terms of the induction variable.  Start with the auxillary
   // induction variables, and recursively rewrite any of their uses.
-  BasicBlock::iterator InsertPt = Header->begin();
-  while (isa<PHINode>(InsertPt)) ++InsertPt;
+  BasicBlock::iterator InsertPt = Header->getFirstNonPHI();
 
   // If there were induction variables of other sizes, cast the primary
   // induction variable to the right size for them, avoiding the need for the

Modified: llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp?rev=51499&r1=51498&r2=51499&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Fri May 23 16:05:58 2008
@@ -9592,8 +9592,7 @@
       // If this is an invoke instruction, we should insert it after the first
       // non-phi, instruction in the normal successor block.
       if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
-        BasicBlock::iterator I = II->getNormalDest()->begin();
-        while (isa<PHINode>(I)) ++I;
+        BasicBlock::iterator I = II->getNormalDest()->getFirstNonPHI();
         InsertNewInstBefore(NC, *I);
       } else {
         // Otherwise, it's a call, just insert cast right after the call instr
@@ -11068,8 +11067,7 @@
   
   // Advance to a place where it is safe to insert the new store and
   // insert it.
-  BBI = DestBB->begin();
-  while (isa<PHINode>(BBI)) ++BBI;
+  BBI = DestBB->getFirstNonPHI();
   InsertNewInstBefore(new StoreInst(MergedVal, SI.getOperand(1),
                                     OtherStore->isVolatile()), *BBI);
   
@@ -11737,8 +11735,7 @@
         return false;
   }
 
-  BasicBlock::iterator InsertPos = DestBlock->begin();
-  while (isa<PHINode>(InsertPos)) ++InsertPos;
+  BasicBlock::iterator InsertPos = DestBlock->getFirstNonPHI();
 
   I->moveBefore(InsertPos);
   ++NumSunkInst;

Modified: llvm/trunk/lib/Transforms/Scalar/JumpThreading.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/JumpThreading.cpp?rev=51499&r1=51498&r2=51499&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/JumpThreading.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/JumpThreading.cpp Fri May 23 16:05:58 2008
@@ -116,9 +116,8 @@
 /// getJumpThreadDuplicationCost - Return the cost of duplicating this block to
 /// thread across it.
 static unsigned getJumpThreadDuplicationCost(const BasicBlock *BB) {
-  BasicBlock::const_iterator I = BB->begin();
   /// Ignore PHI nodes, these will be flattened when duplication happens.
-  while (isa<PHINode>(*I)) ++I;
+  BasicBlock::const_iterator I = BB->getFirstNonPHI();
 
   // Sum up the cost of each instruction until we get to the terminator.  Don't
   // include the terminator because the copy won't include it.

Modified: llvm/trunk/lib/Transforms/Scalar/LICM.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LICM.cpp?rev=51499&r1=51498&r2=51499&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/LICM.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/LICM.cpp Fri May 23 16:05:58 2008
@@ -472,8 +472,7 @@
       // nodes in it.
       I.removeFromParent();
 
-      BasicBlock::iterator InsertPt = ExitBlocks[0]->begin();
-      while (isa<PHINode>(InsertPt)) ++InsertPt;
+      BasicBlock::iterator InsertPt = ExitBlocks[0]->getFirstNonPHI();
       ExitBlocks[0]->getInstList().insert(InsertPt, &I);
     }
   } else if (ExitBlocks.empty()) {
@@ -542,8 +541,7 @@
         // If we haven't already processed this exit block, do so now.
         if (InsertedBlocks.insert(ExitBlock).second) {
           // Insert the code after the last PHI node...
-          BasicBlock::iterator InsertPt = ExitBlock->begin();
-          while (isa<PHINode>(InsertPt)) ++InsertPt;
+          BasicBlock::iterator InsertPt = ExitBlock->getFirstNonPHI();
 
           // If this is the first exit block processed, just move the original
           // instruction, otherwise clone the original instruction and insert
@@ -735,9 +733,7 @@
       continue;
   
     // Copy all of the allocas into their memory locations.
-    BasicBlock::iterator BI = ExitBlocks[i]->begin();
-    while (isa<PHINode>(*BI))
-      ++BI;             // Skip over all of the phi nodes in the block.
+    BasicBlock::iterator BI = ExitBlocks[i]->getFirstNonPHI();
     Instruction *InsertPos = BI;
     unsigned PVN = 0;
     for (unsigned i = 0, e = PromotedValues.size(); i != e; ++i) {

Modified: llvm/trunk/lib/Transforms/Scalar/LoopRotation.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LoopRotation.cpp?rev=51499&r1=51498&r2=51499&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/LoopRotation.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/LoopRotation.cpp Fri May 23 16:05:58 2008
@@ -256,9 +256,7 @@
         // nodes will be created for all getResults later.
         BasicBlock::iterator InsertPoint;
         if (InvokeInst *II = dyn_cast<InvokeInst>(In)) {
-          InsertPoint = II->getNormalDest()->begin();
-          while (isa<PHINode>(InsertPoint)) 
-            ++InsertPoint;
+          InsertPoint = II->getNormalDest()->getFirstNonPHI();
         } else {
           InsertPoint = I;  // call
           ++InsertPoint;

Modified: llvm/trunk/lib/Transforms/Scalar/LoopUnswitch.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LoopUnswitch.cpp?rev=51499&r1=51498&r2=51499&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/LoopUnswitch.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/LoopUnswitch.cpp Fri May 23 16:05:58 2008
@@ -682,8 +682,7 @@
         InsertedPHIs.insert(NewLCSSA);
       }
 
-      BasicBlock::iterator InsertPt = EndBlock->begin();
-      while (dyn_cast<PHINode>(InsertPt)) ++InsertPt;
+      BasicBlock::iterator InsertPt = EndBlock->getFirstNonPHI();
       for (BasicBlock::iterator I = MiddleBlock->begin();
          (OldLCSSA = dyn_cast<PHINode>(I)) && InsertedPHIs.count(OldLCSSA) == 0;
          ++I) {

Modified: llvm/trunk/lib/Transforms/Scalar/TailDuplication.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/TailDuplication.cpp?rev=51499&r1=51498&r2=51499&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/TailDuplication.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/TailDuplication.cpp Fri May 23 16:05:58 2008
@@ -118,8 +118,7 @@
   ++PI;
   if (PI == PE) return false;  // Exactly one predecessor!
 
-  BasicBlock::iterator I = Dest->begin();
-  while (isa<PHINode>(*I)) ++I;
+  BasicBlock::iterator I = Dest->getFirstNonPHI();
 
   for (unsigned Size = 0; I != Dest->end(); ++I) {
     if (Size == Threshold) return false;  // The block is too large.
@@ -254,8 +253,7 @@
     // If there are non-phi instructions in DestBlock that have no operands
     // defined in DestBlock, and if the instruction has no side effects, we can
     // move the instruction to DomBlock instead of duplicating it.
-    BasicBlock::iterator BBI = DestBlock->begin();
-    while (isa<PHINode>(BBI)) ++BBI;
+    BasicBlock::iterator BBI = DestBlock->getFirstNonPHI();
     while (!isa<TerminatorInst>(BBI)) {
       Instruction *I = BBI++;
 

Modified: llvm/trunk/lib/Transforms/Utils/CodeExtractor.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/CodeExtractor.cpp?rev=51499&r1=51498&r2=51499&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Utils/CodeExtractor.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/CodeExtractor.cpp Fri May 23 16:05:58 2008
@@ -126,8 +126,7 @@
   // containing PHI nodes merging values from outside of the region, and a
   // second that contains all of the code for the block and merges back any
   // incoming values from inside of the region.
-  BasicBlock::iterator AfterPHIs = Header->begin();
-  while (isa<PHINode>(AfterPHIs)) ++AfterPHIs;
+  BasicBlock::iterator AfterPHIs = Header->getFirstNonPHI();
   BasicBlock *NewBB = Header->splitBasicBlock(AfterPHIs,
                                               Header->getName()+".ce");
 

Modified: llvm/trunk/lib/Transforms/Utils/LCSSA.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/LCSSA.cpp?rev=51499&r1=51498&r2=51499&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Utils/LCSSA.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/LCSSA.cpp Fri May 23 16:05:58 2008
@@ -225,9 +225,7 @@
             // immediately here. It will be processed in next iteration.
             BasicBlock::iterator InsertPoint;
             if (InvokeInst *II = dyn_cast<InvokeInst>(I)) {
-              InsertPoint = II->getNormalDest()->begin();
-              while (isa<PHINode>(InsertPoint))
-                ++InsertPoint;
+              InsertPoint = II->getNormalDest()->getFirstNonPHI();
             } else {
               InsertPoint = I;
               InsertPoint++;

Modified: llvm/trunk/lib/Transforms/Utils/LowerInvoke.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/LowerInvoke.cpp?rev=51499&r1=51498&r2=51499&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Utils/LowerInvoke.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/LowerInvoke.cpp Fri May 23 16:05:58 2008
@@ -282,8 +282,7 @@
   // location afterward.
   new StoreInst(InvokeNoC, InvokeNum, true, II);  // volatile
   
-  BasicBlock::iterator NI = II->getNormalDest()->begin();
-  while (isa<PHINode>(NI)) ++NI;
+  BasicBlock::iterator NI = II->getNormalDest()->getFirstNonPHI();
   // nonvolatile.
   new StoreInst(Constant::getNullValue(Type::Int32Ty), InvokeNum, false, NI);
   

Modified: llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp?rev=51499&r1=51498&r2=51499&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp Fri May 23 16:05:58 2008
@@ -1493,8 +1493,7 @@
     }
   } else if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) {
     if (BI->isUnconditional()) {
-      BasicBlock::iterator BBI = BB->begin();  // Skip over phi nodes...
-      while (isa<PHINode>(*BBI)) ++BBI;
+      BasicBlock::iterator BBI = BB->getFirstNonPHI();
 
       BasicBlock *Succ = BI->getSuccessor(0);
       if (BBI->isTerminator() &&  // Terminator is the only non-phi instruction!

Modified: llvm/trunk/lib/VMCore/BasicBlock.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/BasicBlock.cpp?rev=51499&r1=51498&r2=51499&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/BasicBlock.cpp (original)
+++ llvm/trunk/lib/VMCore/BasicBlock.cpp Fri May 23 16:05:58 2008
@@ -143,15 +143,14 @@
   return dyn_cast<TerminatorInst>(&InstList.back());
 }
 
-Instruction* BasicBlock::getFirstNonPHI()
-{
-    BasicBlock::iterator i = begin();
-    // All valid basic blocks should have a terminator,
-    // which is not a PHINode. If we have invalid basic
-    // block we'll get assert when dereferencing past-the-end
-    // iterator.
-    while (isa<PHINode>(i)) ++i;
-    return &*i;
+Instruction* BasicBlock::getFirstNonPHI() {
+  BasicBlock::iterator i = begin();
+  // All valid basic blocks should have a terminator,
+  // which is not a PHINode. If we have an invalid basic
+  // block we'll get an assertion failure when dereferencing
+  // a past-the-end iterator.
+  while (isa<PHINode>(i)) ++i;
+  return &*i;
 }
 
 void BasicBlock::dropAllReferences() {





More information about the llvm-commits mailing list