diff --git a/include/llvm/BasicBlock.h b/include/llvm/BasicBlock.h index b569a7aeaf0..edb32833035 100644 --- a/include/llvm/BasicBlock.h +++ b/include/llvm/BasicBlock.h @@ -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(this)->getFirstNonPHI(); + } /// removeFromParent - This method unlinks 'this' from the containing /// function, but does not delete it. diff --git a/lib/Transforms/Instrumentation/ProfilingUtils.cpp b/lib/Transforms/Instrumentation/ProfilingUtils.cpp index d94522f6c7d..48071f11569 100644 --- a/lib/Transforms/Instrumentation/ProfilingUtils.cpp +++ b/lib/Transforms/Instrumentation/ProfilingUtils.cpp @@ -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(InsertPos) || isa(InsertPos)) + BasicBlock::iterator InsertPos = BB->getFirstNonPHI(); + while (isa(InsertPos)) ++InsertPos; // Create the getelementptr constant expression diff --git a/lib/Transforms/Instrumentation/RSProfiling.cpp b/lib/Transforms/Instrumentation/RSProfiling.cpp index a9406c89d41..5bd62c8197d 100644 --- a/lib/Transforms/Instrumentation/RSProfiling.cpp +++ b/lib/Transforms/Instrumentation/RSProfiling.cpp @@ -265,14 +265,11 @@ void GlobalRandomCounterOpt::PrepFunction(Function* F) { new StoreInst(l, Counter, bib); BasicBlock* bb = cast(bib)->getNormalDest(); - BasicBlock::iterator i = bb->begin(); - while (isa(i)) - ++i; + BasicBlock::iterator i = bb->getFirstNonPHI(); l = new LoadInst(Counter, "counter", i); bb = cast(bib)->getUnwindDest(); - i = bb->begin(); - while (isa(i)) ++i; + i = bb->getFirstNonPHI(); l = new LoadInst(Counter, "counter", i); new StoreInst(l, AI, i); } else if (isa(&*bib) || isa(&*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(InsertPos) || isa(InsertPos)) + BasicBlock::iterator InsertPos = BB->getFirstNonPHI(); + while (isa(InsertPos)) ++InsertPos; // Create the getelementptr constant expression diff --git a/lib/Transforms/Scalar/CodeGenPrepare.cpp b/lib/Transforms/Scalar/CodeGenPrepare.cpp index 959a8320045..732aa5b5479 100644 --- a/lib/Transforms/Scalar/CodeGenPrepare.cpp +++ b/lib/Transforms/Scalar/CodeGenPrepare.cpp @@ -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(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(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(InsertPt)) ++InsertPt; + BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI(); InsertedTrunc = new TruncInst(I, Src->getType(), "", InsertPt); } diff --git a/lib/Transforms/Scalar/IndVarSimplify.cpp b/lib/Transforms/Scalar/IndVarSimplify.cpp index ed377658932..f529281be1b 100644 --- a/lib/Transforms/Scalar/IndVarSimplify.cpp +++ b/lib/Transforms/Scalar/IndVarSimplify.cpp @@ -319,8 +319,7 @@ void IndVarSimplify::RewriteLoopExitValues(Loop *L) { BlockToInsertInto = ExitBlocks[0]; else BlockToInsertInto = Preheader; - BasicBlock::iterator InsertPt = BlockToInsertInto->begin(); - while (isa(InsertPt)) ++InsertPt; + BasicBlock::iterator InsertPt = BlockToInsertInto->getFirstNonPHI(); bool HasConstantItCount = isa(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(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 diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 2d300dc68ff..d7c6c797710 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -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(Caller)) { - BasicBlock::iterator I = II->getNormalDest()->begin(); - while (isa(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(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(InsertPos)) ++InsertPos; + BasicBlock::iterator InsertPos = DestBlock->getFirstNonPHI(); I->moveBefore(InsertPos); ++NumSunkInst; diff --git a/lib/Transforms/Scalar/JumpThreading.cpp b/lib/Transforms/Scalar/JumpThreading.cpp index 25f41d38a4e..407d081b781 100644 --- a/lib/Transforms/Scalar/JumpThreading.cpp +++ b/lib/Transforms/Scalar/JumpThreading.cpp @@ -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(*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. diff --git a/lib/Transforms/Scalar/LICM.cpp b/lib/Transforms/Scalar/LICM.cpp index e227713d4ef..d9d5f0f75cb 100644 --- a/lib/Transforms/Scalar/LICM.cpp +++ b/lib/Transforms/Scalar/LICM.cpp @@ -472,8 +472,7 @@ void LICM::sink(Instruction &I) { // nodes in it. I.removeFromParent(); - BasicBlock::iterator InsertPt = ExitBlocks[0]->begin(); - while (isa(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(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(*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) { diff --git a/lib/Transforms/Scalar/LoopRotation.cpp b/lib/Transforms/Scalar/LoopRotation.cpp index 08d557e983c..ebba18c44ee 100644 --- a/lib/Transforms/Scalar/LoopRotation.cpp +++ b/lib/Transforms/Scalar/LoopRotation.cpp @@ -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(In)) { - InsertPoint = II->getNormalDest()->begin(); - while (isa(InsertPoint)) - ++InsertPoint; + InsertPoint = II->getNormalDest()->getFirstNonPHI(); } else { InsertPoint = I; // call ++InsertPoint; diff --git a/lib/Transforms/Scalar/LoopUnswitch.cpp b/lib/Transforms/Scalar/LoopUnswitch.cpp index 78069870507..767ea1d30ee 100644 --- a/lib/Transforms/Scalar/LoopUnswitch.cpp +++ b/lib/Transforms/Scalar/LoopUnswitch.cpp @@ -682,8 +682,7 @@ void LoopUnswitch::SplitExitEdges(Loop *L, InsertedPHIs.insert(NewLCSSA); } - BasicBlock::iterator InsertPt = EndBlock->begin(); - while (dyn_cast(InsertPt)) ++InsertPt; + BasicBlock::iterator InsertPt = EndBlock->getFirstNonPHI(); for (BasicBlock::iterator I = MiddleBlock->begin(); (OldLCSSA = dyn_cast(I)) && InsertedPHIs.count(OldLCSSA) == 0; ++I) { diff --git a/lib/Transforms/Scalar/TailDuplication.cpp b/lib/Transforms/Scalar/TailDuplication.cpp index 3fd09f107cd..d0998ab5a65 100644 --- a/lib/Transforms/Scalar/TailDuplication.cpp +++ b/lib/Transforms/Scalar/TailDuplication.cpp @@ -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(*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(BBI)) ++BBI; + BasicBlock::iterator BBI = DestBlock->getFirstNonPHI(); while (!isa(BBI)) { Instruction *I = BBI++; diff --git a/lib/Transforms/Utils/CodeExtractor.cpp b/lib/Transforms/Utils/CodeExtractor.cpp index 5cefb8db11e..cb7a5293b7f 100644 --- a/lib/Transforms/Utils/CodeExtractor.cpp +++ b/lib/Transforms/Utils/CodeExtractor.cpp @@ -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(AfterPHIs)) ++AfterPHIs; + BasicBlock::iterator AfterPHIs = Header->getFirstNonPHI(); BasicBlock *NewBB = Header->splitBasicBlock(AfterPHIs, Header->getName()+".ce"); diff --git a/lib/Transforms/Utils/LCSSA.cpp b/lib/Transforms/Utils/LCSSA.cpp index 5def5f99ad8..316784d3bef 100644 --- a/lib/Transforms/Utils/LCSSA.cpp +++ b/lib/Transforms/Utils/LCSSA.cpp @@ -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(I)) { - InsertPoint = II->getNormalDest()->begin(); - while (isa(InsertPoint)) - ++InsertPoint; + InsertPoint = II->getNormalDest()->getFirstNonPHI(); } else { InsertPoint = I; InsertPoint++; diff --git a/lib/Transforms/Utils/LowerInvoke.cpp b/lib/Transforms/Utils/LowerInvoke.cpp index 4b7e3a3314b..0f86765a70d 100644 --- a/lib/Transforms/Utils/LowerInvoke.cpp +++ b/lib/Transforms/Utils/LowerInvoke.cpp @@ -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(NI)) ++NI; + BasicBlock::iterator NI = II->getNormalDest()->getFirstNonPHI(); // nonvolatile. new StoreInst(Constant::getNullValue(Type::Int32Ty), InvokeNum, false, NI); diff --git a/lib/Transforms/Utils/SimplifyCFG.cpp b/lib/Transforms/Utils/SimplifyCFG.cpp index e3e4cb722a1..a29716b141e 100644 --- a/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/lib/Transforms/Utils/SimplifyCFG.cpp @@ -1493,8 +1493,7 @@ bool llvm::SimplifyCFG(BasicBlock *BB) { } } else if (BranchInst *BI = dyn_cast(BB->getTerminator())) { if (BI->isUnconditional()) { - BasicBlock::iterator BBI = BB->begin(); // Skip over phi nodes... - while (isa(*BBI)) ++BBI; + BasicBlock::iterator BBI = BB->getFirstNonPHI(); BasicBlock *Succ = BI->getSuccessor(0); if (BBI->isTerminator() && // Terminator is the only non-phi instruction! diff --git a/lib/VMCore/BasicBlock.cpp b/lib/VMCore/BasicBlock.cpp index 17469d6bdb2..64711fea0c7 100644 --- a/lib/VMCore/BasicBlock.cpp +++ b/lib/VMCore/BasicBlock.cpp @@ -143,15 +143,14 @@ const TerminatorInst *BasicBlock::getTerminator() const { return dyn_cast(&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(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(i)) ++i; + return &*i; } void BasicBlock::dropAllReferences() {