[PATCH] D145677: [ConstraintElimination] Fix undefined behaviour in shl decomposition

Zain Jaffal via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 9 03:03:48 PST 2023


zjaffal created this revision.
zjaffal added reviewers: fhahn, meheff.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
zjaffal requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Add checks to prevent decomposing constants bigger than 64.
relates to https://github.com/llvm/llvm-project/issues/61127


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D145677

Files:
  llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
  llvm/test/Transforms/ConstraintElimination/shl.ll


Index: llvm/test/Transforms/ConstraintElimination/shl.ll
===================================================================
--- llvm/test/Transforms/ConstraintElimination/shl.ll
+++ llvm/test/Transforms/ConstraintElimination/shl.ll
@@ -1232,7 +1232,7 @@
 ; CHECK:       main:
 ; CHECK-NEXT:    [[TMP0:%.*]] = shl nuw nsw i64 [[START]], -1
 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp uge i64 [[TMP0]], [[START]]
-; CHECK-NEXT:    ret i1 false
+; CHECK-NEXT:    ret i1 [[TMP1]]
 ; CHECK:       exit:
 ; CHECK-NEXT:    ret i1 false
 ;
@@ -1255,7 +1255,7 @@
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[SHL_UB:%.*]] = shl nuw nsw i256 0, 64
 ; CHECK-NEXT:    [[SHL_CMP:%.*]] = icmp uge i256 [[SHL_UB]], 0
-; CHECK-NEXT:    ret i1 true
+; CHECK-NEXT:    ret i1 [[SHL_CMP]]
 ;
 entry:
   %shl.ub = shl nuw nsw i256 0, 64
@@ -1268,7 +1268,7 @@
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[SHL_UB:%.*]] = shl nuw nsw i256 0, 65
 ; CHECK-NEXT:    [[SHL_CMP:%.*]] = icmp uge i256 [[SHL_UB]], 0
-; CHECK-NEXT:    ret i1 true
+; CHECK-NEXT:    ret i1 [[SHL_CMP]]
 ;
 entry:
   %shl.ub = shl nuw nsw i256 0, 65
Index: llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -370,7 +370,9 @@
   }
 
   if (match(V, m_NUWShl(m_Value(Op1), m_ConstantInt(CI))) && canUseSExt(CI)) {
-    int64_t Mult = int64_t(std::pow(int64_t(2), CI->getSExtValue()));
+    if (CI->getSExtValue() < 0 || CI->getSExtValue() >= 64)
+      return {V, IsKnownNonNegative};
+    int64_t Mult = 1 << CI->getSExtValue();
     auto Result = decompose(Op1, Preconditions, IsSigned, DL);
     Result.mul(Mult);
     return Result;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D145677.503713.patch
Type: text/x-patch
Size: 1761 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230309/e92b52d8/attachment.bin>


More information about the llvm-commits mailing list