From ddf5ff6f8c584c8672b284346f94a42e6bf96014 Mon Sep 17 00:00:00 2001 From: "Vikram S. Adve" Date: Thu, 8 Nov 2001 05:25:33 +0000 Subject: [PATCH] Bug fix: cannot modify Phi operands while iterating over them! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@1203 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/HoistPHIConstants.cpp | 39 ++++++++++++++++------------ 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/lib/Transforms/HoistPHIConstants.cpp b/lib/Transforms/HoistPHIConstants.cpp index b8e1d7b46f9..6e903498f57 100644 --- a/lib/Transforms/HoistPHIConstants.cpp +++ b/lib/Transforms/HoistPHIConstants.cpp @@ -14,6 +14,7 @@ #include "llvm/BasicBlock.h" #include "llvm/Method.h" #include +#include typedef pair BBConstTy; typedef map CachedCopyMap; @@ -53,23 +54,29 @@ bool HoistPHIConstants::doHoistPHIConstants(Method *M) { bool Changed = false; for (Method::iterator BI = M->begin(), BE = M->end(); BI != BE; ++BI) - for (BasicBlock::iterator II = (*BI)->begin(); II != (*BI)->end(); ++II) { - Instruction *Inst = *II; - if (!isa(Inst)) break; // All PHIs occur at top of BB! + { + vector phis; // normalizing invalidates BB iterator - PHINode *PN = cast(Inst); - for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i) { - Value *Op = PN->getIncomingValue(i); - - //if (isa(Op)) { --- Do for all phi args -- Ruchira - - PN->setIncomingValue(i, - NormalizePhiOperand(PN, Op, PN->getIncomingBlock(i), Cache)); - Changed = true; - - //} - } + for (BasicBlock::iterator II = (*BI)->begin(); II != (*BI)->end(); ++II) + { + if (PHINode *PN = dyn_cast(*II)) + phis.push_back(PN); + else + break; // All PHIs occur at top of BB! + } + + for (vector::iterator PI=phis.begin(); PI != phis.end(); ++PI) + for (unsigned i = 0; i < (*PI)->getNumIncomingValues(); ++i) + { + //if (isa(Op)) {--- Do for all phi args -- Ruchira + (*PI)->setIncomingValue(i, + NormalizePhiOperand((*PI), + (*PI)->getIncomingValue(i), + (*PI)->getIncomingBlock(i), Cache)); + Changed = true; + //} + } } - + return Changed; }