From 30019c88f4b9227460335cbafab0f770eb356083 Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Sat, 3 Jun 2006 23:22:50 +0000 Subject: [PATCH] Fix a bug in Phi-noded insertion. Also, update some comments to reflect what's actually going on. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@28677 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Utils/LCSSA.cpp | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/lib/Transforms/Utils/LCSSA.cpp b/lib/Transforms/Utils/LCSSA.cpp index e23c440e91f..95c936b2049 100644 --- a/lib/Transforms/Utils/LCSSA.cpp +++ b/lib/Transforms/Utils/LCSSA.cpp @@ -36,9 +36,7 @@ #include "llvm/Analysis/LoopInfo.h" #include "llvm/Support/CFG.h" #include -#include #include -#include using namespace llvm; @@ -69,7 +67,6 @@ namespace { AU.addRequiredID(LoopSimplifyID); AU.addPreservedID(LoopSimplifyID); AU.addRequired(); - AU.addPreserved(); AU.addRequired(); AU.addRequired(); } @@ -137,6 +134,10 @@ void LCSSA::processInstruction(Instruction* Instr, ++NumLCSSA; // We are applying the transformation std::map Phis; + + // Add the base instruction to the Phis list. This makes tracking down + // the dominating values easier when we're filling in Phi nodes. This will + // be removed later, before we perform use replacement. Phis[Instr->getParent()] = Instr; // Phi nodes that need to be IDF-processed @@ -150,12 +151,19 @@ void LCSSA::processInstruction(Instruction* Instr, Phis[*BBI] = phi; } + // Phi nodes that need to have their incoming values filled. + std::vector needIncomingValues; + // Calculate the IDF of these LCSSA Phi nodes, inserting new Phi's where // necessary. Keep track of these new Phi's in Phis. while (!workList.empty()) { PHINode *CurPHI = workList.back(); workList.pop_back(); + // Even though we've removed this Phi from the work list, we still need + // to fill in its incoming values. + needIncomingValues.push_back(CurPHI); + // Get the current Phi's DF, and insert Phi nodes. Add these new // nodes to our worklist. DominanceFrontier::const_iterator it = DF->find(CurPHI->getParent()); @@ -172,14 +180,14 @@ void LCSSA::processInstruction(Instruction* Instr, } } } - - // Get the predecessor blocks of the current Phi, and use them to hook up - // the operands of the current Phi to any members of DFPhis that dominate - // it. This is a nop for the Phis inserted directly in the exit blocks, - // since they are not dominated by any members of DFPhis. - for (pred_iterator PI = pred_begin(CurPHI->getParent()), - E = pred_end(CurPHI->getParent()); PI != E; ++PI) - CurPHI->addIncoming(getValueDominatingBlock(*PI, Phis), + } + + // Fill in all Phis we've inserted that need their incoming values filled in. + for (std::vector::iterator IVI = needIncomingValues.begin(), + IVE = needIncomingValues.end(); IVI != IVE; ++IVI) { + for (pred_iterator PI = pred_begin((*IVI)->getParent()), + E = pred_end((*IVI)->getParent()); PI != E; ++PI) + (*IVI)->addIncoming(getValueDominatingBlock(*PI, Phis), *PI); } @@ -197,7 +205,8 @@ void LCSSA::processInstruction(Instruction* Instr, Uses.push_back(use); } - // Deliberately remove the initial instruction from Phis set. + // Deliberately remove the initial instruction from Phis set. It would mess + // up use-replacement. Phis.erase(Instr->getParent()); for (std::vector::iterator II = Uses.begin(), IE = Uses.end();