[PATCH] D156499: [InstCombine] Fold abs of known sign operand when source is sub

Allen zhong via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 5 02:38:48 PDT 2023

Allen marked an inline comment as done.
Allen added inline comments.

Comment at: llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp:1537
     if (std::optional<bool> Sign = getKnownSign(IIOperand, II, DL, &AC, &DT)) {
+      // abs(x) -> x if x >= 0 (include abs(x-y) --> x-y where x >= y)
goldstein.w.n wrote:
> Sorry, noticed this a bit late, but is there a way we can avoid essentially recreating the logic below and instead just add something like `getKnownSignOrZero` which wraps `getKnownSign` but also tries `SLE` if the `SLT` analysis isn't fruitful? Then you could just replace the function here and we wouldn't need to duplicate so much codes.
Thanks for your idea. I try that, it work, but will missing **nsw** for some case, such as src_sub_abs_sleF.
This is because we don't create the  a **nsw** with the two operands of IIOperand, ie
when we match **match(IIOperand, m_NSWSub(m_Value(X), m_Value(Y)))**
  - BinaryOperator::CreateNSWSub(Y, X) will final has a extra **nsw**
  - BinaryOperator::CreateNSWNeg(IIOperand) will miss the **nsw**, so need a following PR D157187



More information about the llvm-commits mailing list