[PATCH] D153963: [InstCombine] Fold binop of select and cast of select condition

Noah Goldstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 5 10:22:47 PDT 2023


goldstein.w.n added inline comments.


================
Comment at: llvm/lib/Transforms/InstCombine/InstructionCombining.cpp:883
+  case Instruction::Add:
+  case Instruction::Sub:
+    break;
----------------
I don't see why the op must the `Add` or `Sub`. AFAICT the key is that `(zext C)` can be treated as `1` on the true arm and `0` on the false arm so we can just constant apply `(select c, (binop 1, T), (binop 0, F))` which afaict works for just about any binop. (for `sext` its just `-1` and `0`).


================
Comment at: llvm/lib/Transforms/InstCombine/InstructionCombining.cpp:907
+                                     : InstCombiner::SubOne(C);
+    return Opc == Instruction::Add ? InstCombiner::SubOne(C)
+                                   : InstCombiner::AddOne(C);
----------------
I think cleaner would be:

```
Constant * CastConst = IsZExt ? Constant::getOne() : Constant::getAllOnes();
return Builder.CreateBinOp(Opc, C, CastConst);
```



CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153963/new/

https://reviews.llvm.org/D153963



More information about the llvm-commits mailing list