[llvm] 212f344 - [InstCombine] Handle constant expression result in tryFactorization()

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 27 07:26:26 PST 2025


Author: Nikita Popov
Date: 2025-01-27T16:25:37+01:00
New Revision: 212f344b84b400c0a9dedfa3c1ec6af9d9d30223

URL: https://github.com/llvm/llvm-project/commit/212f344b84b400c0a9dedfa3c1ec6af9d9d30223
DIFF: https://github.com/llvm/llvm-project/commit/212f344b84b400c0a9dedfa3c1ec6af9d9d30223.diff

LOG: [InstCombine] Handle constant expression result in tryFactorization()

If IRBuilder folds the result to a constant expression, don't try
to set nowrap flags on it.

Fixes https://github.com/llvm/llvm-project/issues/124526.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
    llvm/test/Transforms/InstCombine/add2.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index cad17c511b6d03..a64c188575e6c3 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -732,7 +732,7 @@ static Value *tryFactorization(BinaryOperator &I, const SimplifyQuery &SQ,
   RetVal->takeName(&I);
 
   // Try to add no-overflow flags to the final value.
-  if (isa<OverflowingBinaryOperator>(RetVal)) {
+  if (isa<BinaryOperator>(RetVal)) {
     bool HasNSW = false;
     bool HasNUW = false;
     if (isa<OverflowingBinaryOperator>(&I)) {

diff  --git a/llvm/test/Transforms/InstCombine/add2.ll b/llvm/test/Transforms/InstCombine/add2.ll
index ae80ab2e92ad15..c474a33c48a2bc 100644
--- a/llvm/test/Transforms/InstCombine/add2.ll
+++ b/llvm/test/Transforms/InstCombine/add2.ll
@@ -321,6 +321,17 @@ define i16 @mul_add_to_mul_9(i16 %a) {
   ret i16 %add
 }
 
+ at g = external global i8
+
+define i32 @shl_add_to_shl_constexpr() {
+; CHECK-LABEL: @shl_add_to_shl_constexpr(
+; CHECK-NEXT:    ret i32 mul (i32 ptrtoint (ptr @g to i32), i32 4)
+;
+  %shl = shl i32 ptrtoint (ptr @g to i32), 1
+  %add = add i32 %shl, %shl
+  ret i32 %add
+}
+
 ; This test and the next test verify that when a range metadata is attached to
 ; llvm.cttz, ValueTracking correctly intersects the range specified by the
 ; metadata and the range implied by the intrinsic.


        


More information about the llvm-commits mailing list