[llvm] [RISCV] Reverse (add x, (zext c)) back to (select c, (add x, 1), x) (PR #87236)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 1 22:32:33 PDT 2024


topperc wrote:

I think we already have most of a better version in this transform in SelectionDAG

Once we expand to (add (vselect (splat_vector 1), (splat_vector 0))), `foldBinOpIntoSelect` in DAGCombiner is almost able to see the (splat_vector 0) as being the identity constant for the add. The only reason it doesn't is because `isNeutralConstant` doesn't pass `AllowTruncation=true` to `isConstOrConstSplat`.

As a quick hack I tried

```
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index e8d1ac1d3a91..9ff7a2d7ddf4 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -11549,7 +11549,7 @@ bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
                              unsigned OperandNo) {
   // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
   // TODO: Target-specific opcodes could be added.
-  if (auto *Const = isConstOrConstSplat(V)) {
+  if (auto *Const = isConstOrConstSplat(V, false, /*AllowTruncation*/Opcode == ISD::ADD)) {
     switch (Opcode) {
     case ISD::ADD:
     case ISD::OR:
```

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


More information about the llvm-commits mailing list