[llvm] 465e3ce - [LLVM][CodeGen] Lower ConstantInt vectors like shufflevector base splats. (#144395)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 17 03:09:25 PDT 2025
Author: Paul Walker
Date: 2025-06-17T11:09:22+01:00
New Revision: 465e3ce9f10019db071dc7794ae9ab22f9fc76f7
URL: https://github.com/llvm/llvm-project/commit/465e3ce9f10019db071dc7794ae9ab22f9fc76f7
DIFF: https://github.com/llvm/llvm-project/commit/465e3ce9f10019db071dc7794ae9ab22f9fc76f7.diff
LOG: [LLVM][CodeGen] Lower ConstantInt vectors like shufflevector base splats. (#144395)
ConstantInt vectors utilise DAG.getConstant() when constructing the
initial DAG. This can have the effect of legalising the constant before
the DAG combiner is run, significant altering the generated code. To
mitigate this (hopefully as a temporary measure) we instead try to
construct the DAG in the same way as shufflevector based splats.
Added:
Modified:
llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
llvm/test/CodeGen/AArch64/sve-expand-div.ll
llvm/test/CodeGen/AArch64/sve-fixed-length-sdiv-pow2.ll
llvm/test/CodeGen/AArch64/sve-sdiv-pow2.ll
Removed:
################################################################################
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
index c63eb7fc6b374..4f548cbad5c30 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
@@ -1791,8 +1791,26 @@ SDValue SelectionDAGBuilder::getValueImpl(const Value *V) {
if (const Constant *C = dyn_cast<Constant>(V)) {
EVT VT = TLI.getValueType(DAG.getDataLayout(), V->getType(), true);
- if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
- return DAG.getConstant(*CI, getCurSDLoc(), VT);
+ if (const ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
+ SDLoc DL = getCurSDLoc();
+
+ // DAG.getConstant() may attempt to legalise the vector constant which can
+ // significantly change the combines applied to the DAG. To reduce the
+ // divergence when enabling ConstantInt based vectors we try to construct
+ // the DAG in the same way as shufflevector based splats. TODO: The
+ // divergence sometimes leads to better optimisations. Ideally we should
+ // prevent DAG.getConstant() from legalising too early but there are some
+ // degradations preventing this.
+ if (VT.isScalableVector())
+ return DAG.getNode(
+ ISD::SPLAT_VECTOR, DL, VT,
+ DAG.getConstant(CI->getValue(), DL, VT.getVectorElementType()));
+ if (VT.isFixedLengthVector())
+ return DAG.getSplatBuildVector(
+ VT, DL,
+ DAG.getConstant(CI->getValue(), DL, VT.getVectorElementType()));
+ return DAG.getConstant(*CI, DL, VT);
+ }
if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
return DAG.getGlobalAddress(GV, getCurSDLoc(), VT);
diff --git a/llvm/test/CodeGen/AArch64/sve-expand-div.ll b/llvm/test/CodeGen/AArch64/sve-expand-div.ll
index 180c64e0a7de1..bd6c72a3946c1 100644
--- a/llvm/test/CodeGen/AArch64/sve-expand-div.ll
+++ b/llvm/test/CodeGen/AArch64/sve-expand-div.ll
@@ -1,5 +1,6 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc -mtriple=aarch64-linux-gnu < %s | FileCheck %s
+; RUN: llc -mtriple=aarch64-linux-gnu -use-constant-int-for-scalable-splat < %s | FileCheck %s
; Check that expensive divides are expanded into a more performant sequence
diff --git a/llvm/test/CodeGen/AArch64/sve-fixed-length-sdiv-pow2.ll b/llvm/test/CodeGen/AArch64/sve-fixed-length-sdiv-pow2.ll
index 8b4386e2c2216..45781fa47c6de 100644
--- a/llvm/test/CodeGen/AArch64/sve-fixed-length-sdiv-pow2.ll
+++ b/llvm/test/CodeGen/AArch64/sve-fixed-length-sdiv-pow2.ll
@@ -3,6 +3,7 @@
; RUN: llc -aarch64-sve-vector-bits-min=256 < %s | FileCheck %s -check-prefixes=CHECK,VBITS_GE_256
; RUN: llc -aarch64-sve-vector-bits-min=512 < %s | FileCheck %s -check-prefixes=CHECK,VBITS_GE_512
; RUN: llc -aarch64-sve-vector-bits-min=2048 < %s | FileCheck %s -check-prefixes=CHECK,VBITS_GE_512
+; RUN: llc -aarch64-sve-vector-bits-min=128 -use-constant-int-for-fixed-length-splat < %s | FileCheck %s -check-prefixes=CHECK,VBITS_GE_128
target triple = "aarch64-unknown-linux-gnu"
diff --git a/llvm/test/CodeGen/AArch64/sve-sdiv-pow2.ll b/llvm/test/CodeGen/AArch64/sve-sdiv-pow2.ll
index 4607f225f81ea..a799b51f15cb1 100644
--- a/llvm/test/CodeGen/AArch64/sve-sdiv-pow2.ll
+++ b/llvm/test/CodeGen/AArch64/sve-sdiv-pow2.ll
@@ -1,5 +1,6 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc < %s | FileCheck %s
+; RUN: llc -use-constant-int-for-scalable-splat < %s | FileCheck %s
target triple = "aarch64-unknown-linux-gnu"
More information about the llvm-commits
mailing list