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

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun May 15 17:55:49 PDT 2022


craig.topper created this revision.
craig.topper added reviewers: samparker, dmgreen.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
craig.topper requested review of this revision.
Herald added a project: LLVM.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D125653

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


Index: llvm/test/Transforms/TypePromotion/ARM/icmps.ll
===================================================================
--- llvm/test/Transforms/TypePromotion/ARM/icmps.ll
+++ llvm/test/Transforms/TypePromotion/ARM/icmps.ll
@@ -361,3 +361,14 @@
   %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
+}
Index: llvm/lib/CodeGen/TypePromotion.cpp
===================================================================
--- llvm/lib/CodeGen/TypePromotion.cpp
+++ llvm/lib/CodeGen/TypePromotion.cpp
@@ -484,7 +484,11 @@
         continue;
 
       if (auto *Const = dyn_cast<ConstantInt>(Op)) {
-        Constant *NewConst = (SafeWrap.contains(I) && i == 1)
+        // For safe wrap subtract, we shouldn't sign extend if the constant is
+        // the minimum signed value.
+        Constant *NewConst = (SafeWrap.contains(I) && i == 1 &&
+                              (I->getOpcode() != Instruction::Sub ||
+                               !Const->isMinValue(/*IsSigned*/true)))
                                  ? ConstantExpr::getSExt(Const, ExtTy)
                                  : ConstantExpr::getZExt(Const, ExtTy);
         I->setOperand(i, NewConst);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125653.429582.patch
Type: text/x-patch
Size: 1411 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220516/695da1a6/attachment.bin>


More information about the llvm-commits mailing list