Skip to content

Commit

Permalink
PHINode: introduce setIncomingValueForBlock() function, and use it.
Browse files Browse the repository at this point in the history
Summary:
There is PHINode::getBasicBlockIndex() and PHINode::setIncomingValue()
but no function to replace incoming value for a specified BasicBlock*
predecessor.
Clearly, there are a lot of places that could use that functionality.

Reviewer: craig.topper, lebedev.ri, Meinersbur, kbarton, fhahn
Reviewed By: Meinersbur, fhahn
Subscribers: fhahn, hiraditya, zzheng, jsji, llvm-commits
Tag: LLVM
Differential Revision: https://reviews.llvm.org/D63338

llvm-svn: 363566
  • Loading branch information
whitneywhtsang committed Jun 17, 2019
1 parent 1c91e63 commit 15b7f5b
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 32 deletions.
14 changes: 13 additions & 1 deletion llvm/include/llvm/IR/Instructions.h
Original file line number Diff line number Diff line change
Expand Up @@ -2674,7 +2674,7 @@ class PHINode : public Instruction {
}

/// Replace every incoming basic block \p Old to basic block \p New.
void replaceIncomingBlockWith(BasicBlock *Old, BasicBlock *New) {
void replaceIncomingBlockWith(const BasicBlock *Old, BasicBlock *New) {
assert(New && Old && "PHI node got a null basic block!");
for (unsigned Op = 0, NumOps = getNumOperands(); Op != NumOps; ++Op)
if (getIncomingBlock(Op) == Old)
Expand Down Expand Up @@ -2724,6 +2724,18 @@ class PHINode : public Instruction {
return getIncomingValue(Idx);
}

/// Set every incoming value(s) for block \p BB to \p V.
void setIncomingValueForBlock(const BasicBlock *BB, Value *V) {
assert(BB && "PHI node got a null basic block!");
bool Found = false;
for (unsigned Op = 0, NumOps = getNumOperands(); Op != NumOps; ++Op)
if (getIncomingBlock(Op) == BB) {
Found = true;
setIncomingValue(Op, V);
}
assert(Found && "Invalid basic block argument to set!");
}

/// If the specified PHI node always merges together the
/// same value, return the value, otherwise return null.
Value *hasConstantValue() const;
Expand Down
10 changes: 3 additions & 7 deletions llvm/lib/CodeGen/SafeStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,16 +613,12 @@ Value *SafeStack::moveStaticAllocasToUnsafeStack(
ConstantInt::get(Int32Ty, -Offset));
Value *Replacement = IRBUser.CreateBitCast(Off, AI->getType(), Name);

if (auto *PHI = dyn_cast<PHINode>(User)) {
if (auto *PHI = dyn_cast<PHINode>(User))
// PHI nodes may have multiple incoming edges from the same BB (why??),
// all must be updated at once with the same incoming value.
auto *BB = PHI->getIncomingBlock(U);
for (unsigned I = 0; I < PHI->getNumIncomingValues(); ++I)
if (PHI->getIncomingBlock(I) == BB)
PHI->setIncomingValue(I, Replacement);
} else {
PHI->setIncomingValueForBlock(PHI->getIncomingBlock(U), Replacement);
else
U.set(Replacement);
}
}

AI->eraseFromParent();
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/Transforms/Scalar/GVN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2467,8 +2467,7 @@ void GVN::addDeadBlock(BasicBlock *BB) {

for (BasicBlock::iterator II = B->begin(); isa<PHINode>(II); ++II) {
PHINode &Phi = cast<PHINode>(*II);
Phi.setIncomingValue(Phi.getBasicBlockIndex(P),
UndefValue::get(Phi.getType()));
Phi.setIncomingValueForBlock(P, UndefValue::get(Phi.getType()));
if (MD)
MD->invalidateCachedPointerInfo(&Phi);
}
Expand Down
5 changes: 2 additions & 3 deletions llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1335,9 +1335,8 @@ void LoopConstrainer::rewriteIncomingValuesForPHIs(
const LoopConstrainer::RewrittenRangeInfo &RRI) const {
unsigned PHIIndex = 0;
for (PHINode &PN : LS.Header->phis())
for (unsigned i = 0, e = PN.getNumIncomingValues(); i < e; ++i)
if (PN.getIncomingBlock(i) == ContinuationBlock)
PN.setIncomingValue(i, RRI.PHIValuesAtPseudoExit[PHIIndex++]);
PN.setIncomingValueForBlock(ContinuationBlock,
RRI.PHIValuesAtPseudoExit[PHIIndex++]);

LS.IndVarStart = RRI.IndVarEnd;
}
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/Transforms/Scalar/LoopUnswitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1550,8 +1550,7 @@ void LoopUnswitch::RewriteLoopBodyWithConditionConstant(Loop *L, Value *LIC,
ConstantInt::getTrue(Context), NewSISucc);
// Release the PHI operands for this edge.
for (PHINode &PN : NewSISucc->phis())
PN.setIncomingValue(PN.getBasicBlockIndex(Switch),
UndefValue::get(PN.getType()));
PN.setIncomingValueForBlock(Switch, UndefValue::get(PN.getType()));
// Tell the domtree about the new block. We don't fully update the
// domtree here -- instead we force it to do a full recomputation
// after the pass is complete -- but we do need to inform it of
Expand Down
7 changes: 2 additions & 5 deletions llvm/lib/Transforms/Scalar/StructurizeCFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -628,11 +628,8 @@ void StructurizeCFG::setPhiValues() {
if (!Dominator.resultIsRememberedBlock())
Updater.AddAvailableValue(Dominator.result(), Undef);

for (BasicBlock *FI : From) {
int Idx = Phi->getBasicBlockIndex(FI);
assert(Idx != -1);
Phi->setIncomingValue(Idx, Updater.GetValueAtEndOfBlock(FI));
}
for (BasicBlock *FI : From)
Phi->setIncomingValueForBlock(FI, Updater.GetValueAtEndOfBlock(FI));
}

DeletedPhis.erase(To);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Utils/LoopUnrollPeel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ bool llvm::peelLoop(Loop *L, unsigned PeelCount, LoopInfo *LI,
if (LatchInst && L->contains(LatchInst))
NewVal = LVMap[LatchInst];

PHI->setIncomingValue(PHI->getBasicBlockIndex(NewPreHeader), NewVal);
PHI->setIncomingValueForBlock(NewPreHeader, NewVal);
}

// Adjust the branch weights on the loop exit.
Expand Down
9 changes: 4 additions & 5 deletions llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,10 @@ static void ConnectProlog(Loop *L, Value *BECount, unsigned Count,
// Update the existing PHI node operand with the value from the
// new PHI node. How this is done depends on if the existing
// PHI node is in the original loop block, or the exit block.
if (L->contains(&PN)) {
PN.setIncomingValue(PN.getBasicBlockIndex(NewPreHeader), NewPN);
} else {
if (L->contains(&PN))
PN.setIncomingValueForBlock(NewPreHeader, NewPN);
else
PN.addIncoming(NewPN, PrologExit);
}
}
}

Expand Down Expand Up @@ -264,7 +263,7 @@ static void ConnectEpilog(Loop *L, Value *ModVal, BasicBlock *NewExit,
// Update the existing PHI node operand with the value from the new PHI
// node. Corresponding instruction in epilog loop should be PHI.
PHINode *VPN = cast<PHINode>(VMap[&PN]);
VPN->setIncomingValue(VPN->getBasicBlockIndex(EpilogPreHeader), NewPN);
VPN->setIncomingValueForBlock(EpilogPreHeader, NewPN);
}
}

Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/Transforms/Utils/SimplifyCFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2826,8 +2826,7 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, MemorySSAUpdater *MSSAU,
}
}
// Update PHI Node.
PHIs[i]->setIncomingValue(PHIs[i]->getBasicBlockIndex(PBI->getParent()),
MergedCond);
PHIs[i]->setIncomingValueForBlock(PBI->getParent(), MergedCond);
}

// PBI is changed to branch to TrueDest below. Remove itself from
Expand Down
6 changes: 2 additions & 4 deletions llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2883,13 +2883,11 @@ BasicBlock *InnerLoopVectorizer::createVectorizedLoopSkeleton() {
BCResumeVal->addIncoming(EndValue, MiddleBlock);

// Fix the scalar body counter (PHI node).
unsigned BlockIdx = OrigPhi->getBasicBlockIndex(ScalarPH);

// The old induction's phi node in the scalar body needs the truncated
// value.
for (BasicBlock *BB : LoopBypassBlocks)
BCResumeVal->addIncoming(II.getStartValue(), BB);
OrigPhi->setIncomingValue(BlockIdx, BCResumeVal);
OrigPhi->setIncomingValueForBlock(ScalarPH, BCResumeVal);
}

// We need the OrigLoop (scalar loop part) latch terminator to help
Expand Down Expand Up @@ -3480,7 +3478,7 @@ void InnerLoopVectorizer::fixFirstOrderRecurrence(PHINode *Phi) {
Start->addIncoming(Incoming, BB);
}

Phi->setIncomingValue(Phi->getBasicBlockIndex(LoopScalarPreHeader), Start);
Phi->setIncomingValueForBlock(LoopScalarPreHeader, Start);
Phi->setName("scalar.recur");

// Finally, fix users of the recurrence outside the loop. The users will need
Expand Down

0 comments on commit 15b7f5b

Please sign in to comment.