[llvm] b7baf2e - [IR] Mark mul constant expression as undesirable

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 13 01:27:53 PST 2025


Author: Nikita Popov
Date: 2025-02-13T10:27:36+01:00
New Revision: b7baf2ee8d302aab7cae787645ee7b7ec107e3ee

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

LOG: [IR] Mark mul constant expression as undesirable

This is a (very belated) reland of 0a362f12ec60a49a054befec8620a8e69523af54,
which I originally reverted due to flang test failures.

This marks mul constant expressions as undesirable, which means that
we will no longer create them by default, but they can still be
created explicitly.

Part of:
https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179

Added: 
    

Modified: 
    llvm/lib/IR/Constants.cpp
    llvm/test/Transforms/InstCombine/add2.ll
    llvm/test/Transforms/NewGVN/2009-11-12-MemDepMallocBitCast.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp
index 33f4dc78c6d3f..9b69b1cb059da 100644
--- a/llvm/lib/IR/Constants.cpp
+++ b/llvm/lib/IR/Constants.cpp
@@ -2395,10 +2395,10 @@ bool ConstantExpr::isDesirableBinOp(unsigned Opcode) {
   case Instruction::LShr:
   case Instruction::AShr:
   case Instruction::Shl:
+  case Instruction::Mul:
     return false;
   case Instruction::Add:
   case Instruction::Sub:
-  case Instruction::Mul:
   case Instruction::Xor:
     return true;
   default:

diff  --git a/llvm/test/Transforms/InstCombine/add2.ll b/llvm/test/Transforms/InstCombine/add2.ll
index c474a33c48a2b..375f2616a3028 100644
--- a/llvm/test/Transforms/InstCombine/add2.ll
+++ b/llvm/test/Transforms/InstCombine/add2.ll
@@ -325,7 +325,8 @@ define i16 @mul_add_to_mul_9(i16 %a) {
 
 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)
+; CHECK-NEXT:    [[ADD:%.*]] = shl i32 ptrtoint (ptr @g to i32), 2
+; CHECK-NEXT:    ret i32 [[ADD]]
 ;
   %shl = shl i32 ptrtoint (ptr @g to i32), 1
   %add = add i32 %shl, %shl

diff  --git a/llvm/test/Transforms/NewGVN/2009-11-12-MemDepMallocBitCast.ll b/llvm/test/Transforms/NewGVN/2009-11-12-MemDepMallocBitCast.ll
index c49f651437cbb..10a76a9d6d01f 100644
--- a/llvm/test/Transforms/NewGVN/2009-11-12-MemDepMallocBitCast.ll
+++ b/llvm/test/Transforms/NewGVN/2009-11-12-MemDepMallocBitCast.ll
@@ -5,7 +5,7 @@
 
 define i64 @test() {
 ; CHECK-LABEL: define i64 @test() {
-; CHECK-NEXT:    [[TMP1:%.*]] = tail call ptr @malloc(i64 mul (i64 ptrtoint (ptr getelementptr (i64, ptr null, i64 1) to i64), i64 4))
+; CHECK-NEXT:    [[TMP1:%.*]] = tail call ptr @malloc(i64 mul (i64 4, i64 ptrtoint (ptr getelementptr (i64, ptr null, i64 1) to i64)))
 ; CHECK-NEXT:    store i8 42, ptr [[TMP1]], align 1
 ; CHECK-NEXT:    [[Y:%.*]] = load i64, ptr [[TMP1]], align 4
 ; CHECK-NEXT:    ret i64 [[Y]]


        


More information about the llvm-commits mailing list