Skip to content

Commit

Permalink
[CVP] Extract helper from phi processing (NFC)
Browse files Browse the repository at this point in the history
So we can use early returns and avoid those awkward !V checks.
  • Loading branch information
nikic committed Feb 14, 2022
1 parent ee423d9 commit 18bf42c
Showing 1 changed file with 42 additions and 43 deletions.
85 changes: 42 additions & 43 deletions llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
Expand Up @@ -215,6 +215,44 @@ static bool simplifyCommonValuePhi(PHINode *P, LazyValueInfo *LVI,
return true;
}

static Value *getValueOnEdge(LazyValueInfo *LVI, Value *Incoming,
BasicBlock *From, BasicBlock *To,
Instruction *CxtI) {
if (Constant *C = LVI->getConstantOnEdge(Incoming, From, To, CxtI))
return C;

// Look if the incoming value is a select with a scalar condition for which
// LVI can tells us the value. In that case replace the incoming value with
// the appropriate value of the select. This often allows us to remove the
// select later.
auto *SI = dyn_cast<SelectInst>(Incoming);
if (!SI)
return nullptr;

// Once LVI learns to handle vector types, we could also add support
// for vector type constants that are not all zeroes or all ones.
Value *Condition = SI->getCondition();
if (!Condition->getType()->isVectorTy()) {
if (Constant *C = LVI->getConstantOnEdge(Condition, From, To, CxtI)) {
if (C->isOneValue())
return SI->getTrueValue();
if (C->isZeroValue())
return SI->getFalseValue();
}
}

// Look if the select has a constant but LVI tells us that the incoming
// value can never be that constant. In that case replace the incoming
// value with the other value of the select. This often allows us to
// remove the select later.
if (auto *C = dyn_cast<Constant>(SI->getFalseValue()))
if (LVI->getPredicateOnEdge(ICmpInst::ICMP_EQ, SI, C, From, To, CxtI) ==
LazyValueInfo::False)
return SI->getTrueValue();

return nullptr;
}

static bool processPHI(PHINode *P, LazyValueInfo *LVI, DominatorTree *DT,
const SimplifyQuery &SQ) {
bool Changed = false;
Expand All @@ -224,50 +262,11 @@ static bool processPHI(PHINode *P, LazyValueInfo *LVI, DominatorTree *DT,
Value *Incoming = P->getIncomingValue(i);
if (isa<Constant>(Incoming)) continue;

Value *V = LVI->getConstantOnEdge(Incoming, P->getIncomingBlock(i), BB, P);

// Look if the incoming value is a select with a scalar condition for which
// LVI can tells us the value. In that case replace the incoming value with
// the appropriate value of the select. This often allows us to remove the
// select later.
if (!V) {
SelectInst *SI = dyn_cast<SelectInst>(Incoming);
if (!SI) continue;

Value *Condition = SI->getCondition();
if (!Condition->getType()->isVectorTy()) {
if (Constant *C = LVI->getConstantOnEdge(
Condition, P->getIncomingBlock(i), BB, P)) {
if (C->isOneValue()) {
V = SI->getTrueValue();
} else if (C->isZeroValue()) {
V = SI->getFalseValue();
}
// Once LVI learns to handle vector types, we could also add support
// for vector type constants that are not all zeroes or all ones.
}
}

// Look if the select has a constant but LVI tells us that the incoming
// value can never be that constant. In that case replace the incoming
// value with the other value of the select. This often allows us to
// remove the select later.
if (!V) {
Constant *C = dyn_cast<Constant>(SI->getFalseValue());
if (!C) continue;

if (LVI->getPredicateOnEdge(ICmpInst::ICMP_EQ, SI, C,
P->getIncomingBlock(i), BB, P) !=
LazyValueInfo::False)
continue;
V = SI->getTrueValue();
}

LLVM_DEBUG(dbgs() << "CVP: Threading PHI over " << *SI << '\n');
Value *V = getValueOnEdge(LVI, Incoming, P->getIncomingBlock(i), BB, P);
if (V) {
P->setIncomingValue(i, V);
Changed = true;
}

P->setIncomingValue(i, V);
Changed = true;
}

if (Value *V = SimplifyInstruction(P, SQ)) {
Expand Down

0 comments on commit 18bf42c

Please sign in to comment.