[PATCH] D89955: [ValueTracking] Recognize more integer abs idioms

Fraser Cormack via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 22 06:19:03 PDT 2020


frasercrmck created this revision.
frasercrmck added reviewers: shchenz, RKSimon.
Herald added subscribers: llvm-commits, hiraditya, jholewinski.
Herald added a project: LLVM.
frasercrmck requested review of this revision.

This patch supports additional integer abs idioms where the comparison
is performed in a wider type than the abs result, i.e. where select's
comparison operands are sign-extended versions of the select's
true/false operands:

    %neg = sub i16 0, %a
    %a.prom = sext i16 %a to i32
    %abs.cond = icmp sge i32 %a.prom, 0
    %abs = select i1 %abs.cond, i16 %a, i16 %neg


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D89955

Files:
  llvm/lib/Analysis/ValueTracking.cpp
  llvm/test/CodeGen/NVPTX/idioms.ll


Index: llvm/test/CodeGen/NVPTX/idioms.ll
===================================================================
--- llvm/test/CodeGen/NVPTX/idioms.ll
+++ llvm/test/CodeGen/NVPTX/idioms.ll
@@ -12,6 +12,16 @@
   ret i16 %abs
 }
 
+; CHECK-LABEL: abs_extcmp_i16(
+define i16 @abs_extcmp_i16(i16 %a) {
+; CHECK: abs.s16
+  %neg = sub i16 0, %a
+  %a.prom = sext i16 %a to i32
+  %abs.cond = icmp sge i32 %a.prom, 0
+  %abs = select i1 %abs.cond, i16 %a, i16 %neg
+  ret i16 %abs
+}
+
 ; CHECK-LABEL: abs_i32(
 define i32 @abs_i32(i32 %a) {
 ; CHECK: abs.s32
Index: llvm/lib/Analysis/ValueTracking.cpp
===================================================================
--- llvm/lib/Analysis/ValueTracking.cpp
+++ llvm/lib/Analysis/ValueTracking.cpp
@@ -5738,7 +5738,8 @@
         m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
     auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
     auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
-    if (match(TrueVal, MaybeSExtCmpLHS)) {
+    if (match(TrueVal, MaybeSExtCmpLHS) ||
+        match(CmpLHS, m_SExt(m_Specific(TrueVal)))) {
       // Set the return values. If the compare uses the negated value (-X >s 0),
       // swap the return values because the negated value is always 'RHS'.
       LHS = TrueVal;
@@ -5759,8 +5760,8 @@
       // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
       if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
         return {SPF_NABS, SPNB_NA, false};
-    }
-    else if (match(FalseVal, MaybeSExtCmpLHS)) {
+    } else if (match(FalseVal, MaybeSExtCmpLHS) ||
+               match(CmpLHS, m_SExt(m_Specific(FalseVal)))) {
       // Set the return values. If the compare uses the negated value (-X >s 0),
       // swap the return values because the negated value is always 'RHS'.
       LHS = FalseVal;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D89955.299924.patch
Type: text/x-patch
Size: 1828 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201022/5a741e66/attachment.bin>


More information about the llvm-commits mailing list