[llvm] 8d3894f - [TypePromotion] Fix another case for sext vs zext in promoted constant.

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Fri May 20 09:30:41 PDT 2022


Author: Craig Topper
Date: 2022-05-20T09:30:07-07:00
New Revision: 8d3894f67ebf475e4393abeab91736bf534ff8f8

URL: https://github.com/llvm/llvm-project/commit/8d3894f67ebf475e4393abeab91736bf534ff8f8
DIFF: https://github.com/llvm/llvm-project/commit/8d3894f67ebf475e4393abeab91736bf534ff8f8.diff

LOG: [TypePromotion] Fix another case for sext vs zext in promoted constant.

If the SafeWrap operation is a subtract, we negated the constant
to treat the subtract as an addition. The sext was based on the
operation being addition. So we really need to do (neg (sext (neg C)))
when promoting the constant. This is equivalent to (sext C) for
every value of C except the min signed value. For min signed value
we need to do (zext C) instead.

Fixes PR55490.

Differential Revision: https://reviews.llvm.org/D125653

Added: 
    

Modified: 
    llvm/lib/CodeGen/TypePromotion.cpp
    llvm/test/Transforms/TypePromotion/ARM/icmps.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/TypePromotion.cpp b/llvm/lib/CodeGen/TypePromotion.cpp
index 016feb0df2da1..ea1369587609e 100644
--- a/llvm/lib/CodeGen/TypePromotion.cpp
+++ b/llvm/lib/CodeGen/TypePromotion.cpp
@@ -484,7 +484,10 @@ void IRPromoter::PromoteTree() {
         continue;
 
       if (auto *Const = dyn_cast<ConstantInt>(Op)) {
-        Constant *NewConst = (SafeWrap.contains(I) && i == 1)
+        // For subtract, we don't need to sext the constant. We only put it in
+        // SafeWrap because SafeWrap.size() is used elsewhere.
+        Constant *NewConst = (SafeWrap.contains(I) && i == 1 &&
+                              I->getOpcode() != Instruction::Sub)
                                  ? ConstantExpr::getSExt(Const, ExtTy)
                                  : ConstantExpr::getZExt(Const, ExtTy);
         I->setOperand(i, NewConst);

diff  --git a/llvm/test/Transforms/TypePromotion/ARM/icmps.ll b/llvm/test/Transforms/TypePromotion/ARM/icmps.ll
index 880ea18f43882..5caa3a65266fd 100644
--- a/llvm/test/Transforms/TypePromotion/ARM/icmps.ll
+++ b/llvm/test/Transforms/TypePromotion/ARM/icmps.ll
@@ -361,3 +361,14 @@ define i32 @degenerateicmp() {
   %3 = select i1 %2, i32 1, i32 0
   ret i32 %3
 }
+
+define i1 @pr55490() {
+; CHECK-LABEL: @pr55490(
+; CHECK-NEXT:    [[TMP1:%.*]] = sub i32 10, 8
+; CHECK-NEXT:    [[TMP2:%.*]] = icmp ult i32 [[TMP1]], 3
+; CHECK-NEXT:    ret i1 [[TMP2]]
+;
+  %1 = sub i4 -6, -8
+  %2 = icmp ult i4 %1, 3
+  ret i1 %2
+}


        


More information about the llvm-commits mailing list