[llvm] [LLVM][Reassociate] Extend ConvertShiftToMul to allow for ConstantInt vectors. (PR #137340)

Paul Walker via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 25 07:32:49 PDT 2025


https://github.com/paulwalker-arm created https://github.com/llvm/llvm-project/pull/137340

This has the side effect of fixing the FIXME for when use-constant-int-for-fixed-length-splat becomes the default.

>From 19cfd3ef077f7064ec25974746e6cfecc2370343 Mon Sep 17 00:00:00 2001
From: Paul Walker <paul.walker at arm.com>
Date: Fri, 25 Apr 2025 14:18:35 +0000
Subject: [PATCH] [LLVM][Reassociate] Extend ConvertShiftToMul to allow for
 ConstantInt based vectors.

This has the side effect of fixing the FIXME for when
use-constant-int-for-fixed-length-splat becomes the default.
---
 llvm/lib/Transforms/Scalar/Reassociate.cpp       |  2 +-
 .../Reassociate/fast-ReassociateVector.ll        | 16 +++++++++++-----
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/Reassociate.cpp b/llvm/lib/Transforms/Scalar/Reassociate.cpp
index 8e478b37574f6..a8a11624be63f 100644
--- a/llvm/lib/Transforms/Scalar/Reassociate.cpp
+++ b/llvm/lib/Transforms/Scalar/Reassociate.cpp
@@ -1052,7 +1052,7 @@ static BinaryOperator *ConvertShiftToMul(Instruction *Shl) {
   // bitwidth - 1.
   bool NSW = cast<BinaryOperator>(Shl)->hasNoSignedWrap();
   bool NUW = cast<BinaryOperator>(Shl)->hasNoUnsignedWrap();
-  unsigned BitWidth = Shl->getType()->getIntegerBitWidth();
+  unsigned BitWidth = Shl->getType()->getScalarSizeInBits();
   if (NSW && (NUW || SA->getValue().ult(BitWidth - 1)))
     Mul->setHasNoSignedWrap(true);
   Mul->setHasNoUnsignedWrap(NUW);
diff --git a/llvm/test/Transforms/Reassociate/fast-ReassociateVector.ll b/llvm/test/Transforms/Reassociate/fast-ReassociateVector.ll
index 617d513f71268..d313164b8187a 100644
--- a/llvm/test/Transforms/Reassociate/fast-ReassociateVector.ll
+++ b/llvm/test/Transforms/Reassociate/fast-ReassociateVector.ll
@@ -1,5 +1,6 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt < %s -passes=reassociate -S | FileCheck %s
+; RUN: opt < %s -passes=reassociate -S | FileCheck %s --check-prefixes=CHECK,CHECK-CV
+; RUN: opt < %s -passes=reassociate -S -use-constant-int-for-fixed-length-splat | FileCheck %s --check-prefixes=CHECK,CHECK-CI
 
 ; Check that a*c+b*c is turned into (a+b)*c
 
@@ -370,10 +371,15 @@ define <2 x double> @test11_reassoc(<2 x double> %x, <2 x double> %y) {
 ; FIXME: shifts should be converted to mul to assist further reassociation.
 
 define <2 x i64> @test12(<2 x i64> %b, <2 x i64> %c) {
-; CHECK-LABEL: @test12(
-; CHECK-NEXT:    [[MUL:%.*]] = mul <2 x i64> [[C:%.*]], [[B:%.*]]
-; CHECK-NEXT:    [[SHL:%.*]] = shl <2 x i64> [[MUL]], splat (i64 5)
-; CHECK-NEXT:    ret <2 x i64> [[SHL]]
+; CHECK-CV-LABEL: @test12(
+; CHECK-CV-NEXT:    [[MUL:%.*]] = mul <2 x i64> [[C:%.*]], [[B:%.*]]
+; CHECK-CV-NEXT:    [[SHL:%.*]] = shl <2 x i64> [[MUL]], splat (i64 5)
+; CHECK-CV-NEXT:    ret <2 x i64> [[SHL]]
+;
+; CHECK-CI-LABEL: @test12(
+; CHECK-CI-NEXT:    [[MUL:%.*]] = mul <2 x i64> [[B:%.*]], splat (i64 32)
+; CHECK-CI-NEXT:    [[SHL:%.*]] = mul <2 x i64> [[MUL]], [[C:%.*]]
+; CHECK-CI-NEXT:    ret <2 x i64> [[SHL]]
 ;
   %mul = mul <2 x i64> %c, %b
   %shl = shl <2 x i64> %mul, <i64 5, i64 5>



More information about the llvm-commits mailing list