bool ComputeValueKnownInPredecessors(Value *V, BasicBlock *BB,
PredValueInfo &Result,
ConstantPreference Preference,
+ bool &Changed,
Instruction *CxtI = nullptr);
bool ProcessThreadableEdges(Value *Cond, BasicBlock *BB,
ConstantPreference Preference,
///
/// This returns true if there were any known values.
///
-bool JumpThreading::
-ComputeValueKnownInPredecessors(Value *V, BasicBlock *BB, PredValueInfo &Result,
- ConstantPreference Preference,
- Instruction *CxtI) {
+bool JumpThreading::ComputeValueKnownInPredecessors(
+ Value *V, BasicBlock *BB, PredValueInfo &Result,
+ ConstantPreference Preference, bool &Changed, Instruction *CxtI) {
// This method walks up use-def chains recursively. Because of this, we could
// get into an infinite loop going around loops in the use-def chain. To
// prevent this, keep track of what (value, block) pairs we've already visited
// stack pops back out again.
RecursionSetRemover remover(RecursionSet, std::make_pair(V, BB));
+ // Simplify the instruction before inferring the value.
+ Instruction *I = dyn_cast<Instruction>(V);
+ if (I && !isa<PHINode>(I))
+ if (auto *NewV = SimplifyInstruction(I, BB->getModule()->getDataLayout())) {
+ I->replaceAllUsesWith(NewV);
+ I->eraseFromParent();
+ V = NewV;
+ Changed = true;
+ }
+
// If V is a constant, then it is known in all predecessors.
if (Constant *KC = getKnownConstant(V, Preference)) {
for (BasicBlock *Pred : predecessors(BB))
// If V is a non-instruction value, or an instruction in a different block,
// then it can't be derived from a PHI.
- Instruction *I = dyn_cast<Instruction>(V);
+ I = dyn_cast<Instruction>(V);
if (!I || I->getParent() != BB) {
// Okay, if this is a live-in value, see if it has a known value at the end
if (I->getOpcode() == Instruction::Or ||
I->getOpcode() == Instruction::And) {
ComputeValueKnownInPredecessors(I->getOperand(0), BB, LHSVals,
- WantInteger, CxtI);
+ WantInteger, Changed, CxtI);
ComputeValueKnownInPredecessors(I->getOperand(1), BB, RHSVals,
- WantInteger, CxtI);
+ WantInteger, Changed, CxtI);
if (LHSVals.empty() && RHSVals.empty())
return false;
if (I->getOpcode() == Instruction::Xor &&
isa<ConstantInt>(I->getOperand(1)) &&
cast<ConstantInt>(I->getOperand(1))->isOne()) {
- ComputeValueKnownInPredecessors(I->getOperand(0), BB, Result,
- WantInteger, CxtI);
+ ComputeValueKnownInPredecessors(I->getOperand(0), BB, Result, WantInteger,
+ Changed, CxtI);
if (Result.empty())
return false;
if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) {
PredValueInfoTy LHSVals;
ComputeValueKnownInPredecessors(BO->getOperand(0), BB, LHSVals,
- WantInteger, CxtI);
+ WantInteger, Changed, CxtI);
// Try to use constant folding to simplify the binary operator.
for (const auto &LHSVal : LHSVals) {
if (Constant *CmpConst = dyn_cast<Constant>(Cmp->getOperand(1))) {
PredValueInfoTy LHSVals;
ComputeValueKnownInPredecessors(I->getOperand(0), BB, LHSVals,
- WantInteger, CxtI);
+ WantInteger, Changed, CxtI);
for (const auto &LHSVal : LHSVals) {
Constant *V = LHSVal.first;
PredValueInfoTy Conds;
if ((TrueVal || FalseVal) &&
ComputeValueKnownInPredecessors(SI->getCondition(), BB, Conds,
- WantInteger, CxtI)) {
+ WantInteger, Changed, CxtI)) {
for (auto &C : Conds) {
Constant *Cond = C.first;
return false;
PredValueInfoTy PredValues;
- if (!ComputeValueKnownInPredecessors(Cond, BB, PredValues, Preference, CxtI))
- return false;
+ bool Changed = false;
+ if (!ComputeValueKnownInPredecessors(Cond, BB, PredValues, Preference,
+ Changed, CxtI))
+ return Changed;
assert(!PredValues.empty() &&
"ComputeValueKnownInPredecessors returned true with no values");
// If all edges were unthreadable, we fail.
if (PredToDestList.empty())
- return false;
+ return Changed;
// Determine which is the most common successor. If we have many inputs and
// this block is a switch, we want to start by threading the batch that goes
getSuccessor(GetBestDestForJumpOnUndef(BB));
// Ok, try to thread it!
- return ThreadEdge(BB, PredsToFactor, MostPopularDest);
+ Changed |= ThreadEdge(BB, PredsToFactor, MostPopularDest);
+ return Changed;
}
/// ProcessBranchOnPHI - We have an otherwise unthreadable conditional branch on
PredValueInfoTy XorOpValues;
bool isLHS = true;
+ bool Changed = false;
if (!ComputeValueKnownInPredecessors(BO->getOperand(0), BB, XorOpValues,
- WantInteger, BO)) {
+ WantInteger, Changed, BO)) {
assert(XorOpValues.empty());
if (!ComputeValueKnownInPredecessors(BO->getOperand(1), BB, XorOpValues,
- WantInteger, BO))
- return false;
+ WantInteger, Changed, BO))
+ return Changed;
isLHS = false;
}
}
// Try to duplicate BB into PredBB.
- return DuplicateCondBranchOnPHIIntoPred(BB, BlocksToFoldInto);
+ Changed |= DuplicateCondBranchOnPHIIntoPred(BB, BlocksToFoldInto);
+ return Changed;
}