Tidy up BasicBlock::getFirstNonPHI, and change a bunch of places to
authorDan Gohman <gohman@apple.com>
Fri, 23 May 2008 21:05:58 +0000 (21:05 +0000)
committerDan Gohman <gohman@apple.com>
Fri, 23 May 2008 21:05:58 +0000 (21:05 +0000)
use it instead of duplicating its functionality.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@51499 91177308-0d34-0410-b5e6-96231b3b80d8

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

index b569a7aeaf0c42506c98c6ed76ba224267825a89..edb32833035a1bbea46badc151b9ddfb94da6592 100644 (file)
@@ -7,8 +7,8 @@
 //
 //===----------------------------------------------------------------------===//
 //
-//
 // This file contains the declaration of the BasicBlock class.
+//
 //===----------------------------------------------------------------------===//
 
 #ifndef LLVM_BASICBLOCK_H
@@ -106,6 +106,9 @@ public:
   /// 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.
index d94522f6c7d34e8b7c2f4f2417492ec21dadf191..48071f1156925c5853fe7b4def18ac6c87aaf2c3 100644 (file)
@@ -100,8 +100,8 @@ void llvm::InsertProfilingInitCall(Function *MainFn, const char *FnName,
 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
index a9406c89d41d3b2dce41509161770856ad2ca64c..5bd62c8197dd0eda0b0378101d6824ae82499e63 100644 (file)
@@ -265,14 +265,11 @@ void GlobalRandomCounterOpt::PrepFunction(Function* F) {
         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 @@ bool RSProfilers_std::isProfiling(Value* v) {
 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
index 959a83200450bf46f657903e69c4d6442ea2ab1b..732aa5b5479c154bb54d132d8fb194b836925a26 100644 (file)
@@ -385,8 +385,7 @@ static bool OptimizeNoopCopyExpression(CastInst *CI, const TargetLowering &TLI){
     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 @@ static bool OptimizeCmpExpression(CmpInst *CI){
     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 @@ bool CodeGenPrepare::OptimizeExtUses(Instruction *I) {
     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);
     }
index ed377658932cde2d9b957d59a19d500131456035..f529281be1bbca1c3b9cb004507f135aae92bd5c 100644 (file)
@@ -319,8 +319,7 @@ void IndVarSimplify::RewriteLoopExitValues(Loop *L) {
     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 @@ bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
   // 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
index 2d300dc68fffb9df1622ac12716660fd00475569..d7c6c7977103eb11ca539f9ea7522967af9f5b6b 100644 (file)
@@ -9592,8 +9592,7 @@ bool InstCombiner::transformConstExprCastCall(CallSite CS) {
       // 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 @@ bool InstCombiner::SimplifyStoreAtEndOfBlock(StoreInst &SI) {
   
   // 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 @@ static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) {
         return false;
   }
 
-  BasicBlock::iterator InsertPos = DestBlock->begin();
-  while (isa<PHINode>(InsertPos)) ++InsertPos;
+  BasicBlock::iterator InsertPos = DestBlock->getFirstNonPHI();
 
   I->moveBefore(InsertPos);
   ++NumSunkInst;
index 25f41d38a4e5afa6ff826a1a6cf97ff2a234b15d..407d081b78157e60202b9cfd0d60f70a1c5fc68c 100644 (file)
@@ -116,9 +116,8 @@ BasicBlock *JumpThreading::FactorCommonPHIPreds(PHINode *PN, Constant *CstVal) {
 /// 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.
index e227713d4efa0d15da4ca09e5afce54de7b5fedb..d9d5f0f75cb6433eb07028cb9b9b2e202f968e56 100644 (file)
@@ -472,8 +472,7 @@ void LICM::sink(Instruction &I) {
       // 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 @@ void LICM::sink(Instruction &I) {
         // 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 @@ void LICM::PromoteValuesInLoop() {
       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) {
index 08d557e983c8b94b74c48be634382fb8329a72c8..ebba18c44ee20cfed7992befa4bed53dff673d80 100644 (file)
@@ -256,9 +256,7 @@ bool LoopRotate::rotateLoop(Loop *Lp, LPPassManager &LPM) {
         // 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;
index 78069870507502707f37de633ede4b890456e84d..767ea1d30eea832ea50827e7d18e3d9e674a0f62 100644 (file)
@@ -682,8 +682,7 @@ void LoopUnswitch::SplitExitEdges(Loop *L,
         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) {
index 3fd09f107cd859f6b0c1fcc30fbe43ffc915bed3..d0998ab5a65d5984d7443985da79f179aa60e357 100644 (file)
@@ -118,8 +118,7 @@ bool TailDup::shouldEliminateUnconditionalBranch(TerminatorInst *TI,
   ++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 @@ void TailDup::eliminateUnconditionalBranch(BranchInst *Branch) {
     // 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++;
 
index 5cefb8db11e3005995df57554b43b471ad218d59..cb7a5293b7f48a54b509ae60704eed2a943dbbbd 100644 (file)
@@ -126,8 +126,7 @@ void CodeExtractor::severSplitPHINodes(BasicBlock *&Header) {
   // 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");
 
index 5def5f99ad80a83605d7abdb8a77a1e81980a120..316784d3befb2c49287cdf45182b43ce610c4f5d 100644 (file)
@@ -225,9 +225,7 @@ void LCSSA::getLoopValuesUsedOutsideLoop(Loop *L,
             // 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++;
index 4b7e3a3314b4b21c230740931100b99c3447034a..0f86765a70de85c5a0723d13ec18970c74e4bc28 100644 (file)
@@ -282,8 +282,7 @@ void LowerInvoke::rewriteExpensiveInvoke(InvokeInst *II, unsigned InvokeNo,
   // 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);
   
index e3e4cb722a18e19fb702cb2f295b085d468e65e0..a29716b141e3d6c5d6aaca8dbb85e373aee9c9c9 100644 (file)
@@ -1493,8 +1493,7 @@ bool llvm::SimplifyCFG(BasicBlock *BB) {
     }
   } 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!
index 17469d6bdb2f9b1d61f03977eddd909249568ad9..64711fea0c7751861c084cb6f5dfff8e0fbc929b 100644 (file)
@@ -143,15 +143,14 @@ const TerminatorInst *BasicBlock::getTerminator() const {
   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() {