[PATCH] D140798: [InstCombine] Fold zero check followed by decrement to usub.sat

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 4 01:58:53 PST 2023


nikic added inline comments.


================
Comment at: llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp:816
+      if (Pred == ICmpInst::ICMP_NE) {
+        Pred = ICmpInst::getSwappedPredicate(Pred);
+        std::swap(TrueVal, FalseVal);
----------------
Should be getInversePredicate(), though it doesn't actually matter here (as you are not checking the Pred afterwards anymore).


================
Comment at: llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp:825
+    const APInt *Added;
+    if (match(FalseVal, m_Add(m_Specific(A), m_APInt(Added)))) {
+      // add a, -1
----------------
`m_APInt(Added)` can be replaced with `m_AllOnes()` here. Below you'd create the constant with value `1` then.


================
Comment at: llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp:839-848
   // (b > a) ? 0 : a - b -> (b <= a) ? a - b : 0
   if (match(TrueVal, m_Zero())) {
     Pred = ICmpInst::getInversePredicate(Pred);
     std::swap(TrueVal, FalseVal);
   }
   if (!match(FalseVal, m_Zero()))
     return nullptr;
----------------
You could take this piece of code (starting at `if (match(TrueVal`) and move it to the top of the function, because this bit of logic is the same for both cases.


================
Comment at: llvm/test/Transforms/InstCombine/saturating-add-sub.ll:615
+;
+  %i = icmp eq i8 %a, 1
+  %i1 = sub i8 %a, 1
----------------
2 would be better, to prevent the select from folding away.


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

https://reviews.llvm.org/D140798



More information about the llvm-commits mailing list