Issue 179127
Summary [DAG] Add isKnownNonZero to enable optimizations
Labels new issue
Assignees
Reporter fbrv
    DAGCombiner has optimizations that depend on proving values are non-zero but  cannot easily do so, forcing conservative defaults.

### Examples

`BuildLogBase2` in DAGCombiner.cpp has a `KnownNonZero` parameter:
```cpp
/// Determines the LogBase2 value for a non-null input value using the
/// transform: LogBase2(V) = (EltBits - 1) - ctlz(V).
SDValue DAGCombiner::BuildLogBase2(SDValue V, const SDLoc &DL,
 bool KnownNonZero, bool InexpensiveOnly,
```

Declaration defaults to `false`:
```cpp
SDValue BuildLogBase2(SDValue V, const SDLoc &DL,
                      bool KnownNeverZero = false,
```

**All call sites use the default** - none pass `true` because there's no way to  prove a value is non-zero:
```cpp
if (SDValue LogBase2 = BuildLogBase2(N1, DL)) {  // Uses default false
```

Similarly, `takeInexpensiveLog2` has an `AssumeNonZero` flag that resorts to  checking instruction flags as a workaround:
```cpp
if (AssumeNonZero || Op->getFlags().hasNoUnsignedWrap() ||
 Op->getFlags().hasNoSignedWrap() || isOneConstant(Op.getOperand(0)))
```

### Proposed Solution

Add to SelectionDAG:
```cpp
bool SelectionDAG::isKnownNonZero(SDValue Op, unsigned Depth = 0) const;
```

This should enable:
- `BuildLogBase2` calls with `KnownNonZero = true` when provable
- Simpler non-zero checks without flag inspection
- Match IR's `llvm::isKnownNonZero()` capabilities

---
If makes sense I can create a POC patch
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to