Index: include/llvm/Transforms/Utils/SSAUpdater.h =================================================================== --- include/llvm/Transforms/Utils/SSAUpdater.h +++ include/llvm/Transforms/Utils/SSAUpdater.h @@ -55,6 +55,9 @@ /// the vector. SmallVectorImpl *InsertedPHIs; + /// False if we should not create new Phis but only find an existing. + bool AllowPhiCreation; + public: /// If InsertedPHIs is specified, it will be filled /// in with all PHI Nodes created by rewriting. @@ -69,6 +72,9 @@ /// PHI nodes get a name based on 'Name'. void Initialize(Type *Ty, StringRef Name); + /// \brief Sets whether we are allowed creation new Phi node. + void SetAllowPhiCreation(bool Val) { AllowPhiCreation = Val; } + /// \brief Indicate that a rewritten value is available in the specified block /// with the specified value. void AddAvailableValue(BasicBlock *BB, Value *V); Index: include/llvm/Transforms/Utils/SSAUpdaterImpl.h =================================================================== --- include/llvm/Transforms/Utils/SSAUpdaterImpl.h +++ include/llvm/Transforms/Utils/SSAUpdaterImpl.h @@ -68,11 +68,13 @@ typedef DenseMap BBMapTy; BBMapTy BBMap; BumpPtrAllocator Allocator; + bool AllowPhiCreation; public: explicit SSAUpdaterImpl(UpdaterT *U, AvailableValsTy *A, - SmallVectorImpl *Ins) : - Updater(U), AvailableVals(A), InsertedPHIs(Ins) { } + SmallVectorImpl *Ins, bool APC = true) + : Updater(U), AvailableVals(A), InsertedPHIs(Ins), AllowPhiCreation(APC) { + } /// GetValue - Check to see if AvailableVals has an entry for the specified /// BB and if so, return it. If not, construct SSA form by first @@ -91,11 +93,13 @@ FindDominators(&BlockList, PseudoEntry); FindPHIPlacement(&BlockList); - FindAvailableVals(&BlockList); + if (!FindAvailableVals(&BlockList)) + return Traits::GetUndefVal(BB, Updater); return BBMap[BB]->DefBB->AvailableVal; } +private: /// BuildBlockList - Starting from the specified basic block, traverse back /// through its predecessors until reaching blocks with known values. /// Create BBInfo structures for the blocks and append them to the block @@ -316,7 +320,9 @@ /// earlier, and if not, create a new PHI. Visit all the block's /// predecessors to calculate the available value for each one and fill in /// the incoming values for a new PHI. - void FindAvailableVals(BlockListTy *BlockList) { + /// Return false if new Phi creation is required but we are not allowed to do + /// that. + bool FindAvailableVals(BlockListTy *BlockList) { // Go through the worklist in forward order (i.e., backward through the CFG) // and check if existing PHIs can be used. If not, create empty PHIs where // they are needed. @@ -332,6 +338,10 @@ if (Info->AvailableVal) continue; + // If we are not allowed to create a new Phi then bail out. + if (!AllowPhiCreation) + return false; + ValT PHI = Traits::CreateEmptyPHI(Info->BB, Info->NumPreds, Updater); Info->AvailableVal = PHI; (*AvailableVals)[Info->BB] = PHI; @@ -371,6 +381,7 @@ // If the client wants to know about all new instructions, tell it. if (InsertedPHIs) InsertedPHIs->push_back(PHI); } + return true; } /// FindExistingPHI - Look through the PHI nodes in a block to see if any of Index: lib/Transforms/Utils/SSAUpdater.cpp =================================================================== --- lib/Transforms/Utils/SSAUpdater.cpp +++ lib/Transforms/Utils/SSAUpdater.cpp @@ -44,8 +44,8 @@ return *static_cast(AV); } -SSAUpdater::SSAUpdater(SmallVectorImpl *NewPHI) - : InsertedPHIs(NewPHI) {} +SSAUpdater::SSAUpdater(SmallVectorImpl *NewPHI) + : InsertedPHIs(NewPHI), AllowPhiCreation(true) {} SSAUpdater::~SSAUpdater() { delete static_cast(AV); @@ -155,6 +155,9 @@ } } + if (!AllowPhiCreation) + return UndefValue::get(ProtoType); + // Ok, we have no way out, insert a new one now. PHINode *InsertedPHI = PHINode::Create(ProtoType, PredValues.size(), ProtoName, &BB->front()); @@ -324,7 +327,8 @@ if (Value *V = AvailableVals[BB]) return V; - SSAUpdaterImpl Impl(this, &AvailableVals, InsertedPHIs); + SSAUpdaterImpl Impl(this, &AvailableVals, InsertedPHIs, + AllowPhiCreation); return Impl.GetValue(BB); }