[llvm] [Instcombine] Ensure simplifyValueKnownNonZero adds instrs in dominance order. (PR #173703)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 26 22:53:12 PST 2025


================
@@ -51,6 +51,16 @@ static Value *simplifyValueKnownNonZero(Value *V, InstCombinerImpl &IC,
   // code.
   if (!V->hasOneUse()) return nullptr;
 
+  // This helper can rewrite an instruction (including its operands) even when
+  // IC.Builder is currently set to the instruction being visited (e.g. a
+  // divide/remainder using V as a non-zero divisor). Ensure that any IR we
+  // create is inserted before the value we are about to rewrite, so it
+  // dominates all uses.
+  IRBuilderBase::InsertPointGuard Guard(IC.Builder);
+  if (auto *VI = dyn_cast<Instruction>(V))
----------------
dtcxzyw wrote:

The root cause is that the recursive call to simplifyValueKnownNonZero modifies a different user instead of the root instruction. I'd suggest changing the insert point before the recursive call:
https://github.com/llvm/llvm-project/blob/08debd7f44614f3b8f8a5b62fe80cc051ce17918/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp#L70-L75

```
+{
  +IRBuilderBase::InsertPointGuard Guard(IC.Builder);
  +IC.Builder.SetInsertPoint(I);
  if (Value *V2 = simplifyValueKnownNonZero(I->getOperand(0), IC, CxtI)) {
        IC.replaceOperand(*I, 0, V2);
        MadeChange = true;
  }
+}
```

https://github.com/llvm/llvm-project/pull/173703


More information about the llvm-commits mailing list