[llvm] [InstCombine] Iterative replacement in PtrReplacer (PR #137215)
Anshil Gandhi via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 9 08:18:56 PDT 2025
================
@@ -270,80 +268,119 @@ class PointerReplacer {
} // end anonymous namespace
bool PointerReplacer::collectUsers() {
- if (!collectUsersRecursive(Root))
- return false;
-
- // Ensure that all outstanding (indirect) users of I
- // are inserted into the Worklist. Return false
- // otherwise.
- return llvm::set_is_subset(ValuesToRevisit, Worklist);
-}
+ SmallVector<Instruction *> Worklist;
+ SmallSetVector<Instruction *, 32> ValuesToRevisit;
+
+ auto PushUsersToWorklist = [&](Instruction *Inst) {
+ for (auto *U : Inst->users())
+ if (auto *I = dyn_cast<Instruction>(U))
+ if (!isAvailable(I) && !ValuesToRevisit.contains(I))
+ Worklist.emplace_back(I);
+ };
-bool PointerReplacer::collectUsersRecursive(Instruction &I) {
- for (auto *U : I.users()) {
- auto *Inst = cast<Instruction>(&*U);
+ PushUsersToWorklist(&Root);
+ while (!Worklist.empty()) {
+ Instruction *Inst = Worklist.pop_back_val();
if (auto *Load = dyn_cast<LoadInst>(Inst)) {
if (Load->isVolatile())
return false;
- Worklist.insert(Load);
+ UsersToReplace.insert(Load);
} else if (auto *PHI = dyn_cast<PHINode>(Inst)) {
- // All incoming values must be instructions for replacability
- if (any_of(PHI->incoming_values(),
- [](Value *V) { return !isa<Instruction>(V); }))
- return false;
-
- // If at least one incoming value of the PHI is not in Worklist,
- // store the PHI for revisiting and skip this iteration of the
- // loop.
- if (any_of(PHI->incoming_values(), [this](Value *V) {
- return !isAvailable(cast<Instruction>(V));
+ /// TODO: Handle poison and null pointers for PHI and select.
+ // If all incoming values are available, mark this PHI as
+ // replacable and push it's users into the worklist.
+ bool IsReplacable = true;
+ if (all_of(PHI->incoming_values(), [&](Value *V) {
+ if (!isa<Instruction>(V))
+ return IsReplacable = false;
+ return isAvailable(cast<Instruction>(V));
})) {
- ValuesToRevisit.insert(Inst);
+ UsersToReplace.insert(PHI);
+ PushUsersToWorklist(PHI);
continue;
}
- Worklist.insert(PHI);
- if (!collectUsersRecursive(*PHI))
- return false;
- } else if (auto *SI = dyn_cast<SelectInst>(Inst)) {
- if (!isa<Instruction>(SI->getTrueValue()) ||
- !isa<Instruction>(SI->getFalseValue()))
+ // Either an incoming value is not an instruction or not all
+ // incoming values are available. If this PHI was already
+ // visited prior to this iteration, return false.
+ if (!IsReplacable || !ValuesToRevisit.insert(PHI))
return false;
- if (!isAvailable(cast<Instruction>(SI->getTrueValue())) ||
- !isAvailable(cast<Instruction>(SI->getFalseValue()))) {
- ValuesToRevisit.insert(Inst);
- continue;
+ // Push PHI back into the stack, followed by unavailable
+ // incoming values.
+ Worklist.emplace_back(PHI);
+ for (unsigned Idx = 0; Idx < PHI->getNumIncomingValues(); ++Idx) {
+ auto *IncomingValue = cast<Instruction>(PHI->getIncomingValue(Idx));
+ if (UsersToReplace.contains(IncomingValue))
+ continue;
+ if (!ValuesToRevisit.insert(IncomingValue))
----------------
gandhi56 wrote:
If a value is inserted into `ValuesToRevisit` and is not yet replacable, it will be caught here and the PointerReplacer will return false. Hence, this function does not need to check for unreplaceable elements in `ValuesToRevisit` at the time of returning.
https://github.com/llvm/llvm-project/pull/137215
More information about the llvm-commits
mailing list