Skip to content

Commit 465e3ce

Browse files
[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.
1 parent c377ce1 commit 465e3ce

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,8 +1791,26 @@ SDValue SelectionDAGBuilder::getValueImpl(const Value *V) {
17911791
if (const Constant *C = dyn_cast<Constant>(V)) {
17921792
EVT VT = TLI.getValueType(DAG.getDataLayout(), V->getType(), true);
17931793

1794-
if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
1795-
return DAG.getConstant(*CI, getCurSDLoc(), VT);
1794+
if (const ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
1795+
SDLoc DL = getCurSDLoc();
1796+
1797+
// DAG.getConstant() may attempt to legalise the vector constant which can
1798+
// significantly change the combines applied to the DAG. To reduce the
1799+
// divergence when enabling ConstantInt based vectors we try to construct
1800+
// the DAG in the same way as shufflevector based splats. TODO: The
1801+
// divergence sometimes leads to better optimisations. Ideally we should
1802+
// prevent DAG.getConstant() from legalising too early but there are some
1803+
// degradations preventing this.
1804+
if (VT.isScalableVector())
1805+
return DAG.getNode(
1806+
ISD::SPLAT_VECTOR, DL, VT,
1807+
DAG.getConstant(CI->getValue(), DL, VT.getVectorElementType()));
1808+
if (VT.isFixedLengthVector())
1809+
return DAG.getSplatBuildVector(
1810+
VT, DL,
1811+
DAG.getConstant(CI->getValue(), DL, VT.getVectorElementType()));
1812+
return DAG.getConstant(*CI, DL, VT);
1813+
}
17961814

17971815
if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
17981816
return DAG.getGlobalAddress(GV, getCurSDLoc(), VT);

llvm/test/CodeGen/AArch64/sve-expand-div.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
22
; RUN: llc -mtriple=aarch64-linux-gnu < %s | FileCheck %s
3+
; RUN: llc -mtriple=aarch64-linux-gnu -use-constant-int-for-scalable-splat < %s | FileCheck %s
34

45
; Check that expensive divides are expanded into a more performant sequence
56

llvm/test/CodeGen/AArch64/sve-fixed-length-sdiv-pow2.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
; RUN: llc -aarch64-sve-vector-bits-min=256 < %s | FileCheck %s -check-prefixes=CHECK,VBITS_GE_256
44
; RUN: llc -aarch64-sve-vector-bits-min=512 < %s | FileCheck %s -check-prefixes=CHECK,VBITS_GE_512
55
; RUN: llc -aarch64-sve-vector-bits-min=2048 < %s | FileCheck %s -check-prefixes=CHECK,VBITS_GE_512
6+
; RUN: llc -aarch64-sve-vector-bits-min=128 -use-constant-int-for-fixed-length-splat < %s | FileCheck %s -check-prefixes=CHECK,VBITS_GE_128
67

78
target triple = "aarch64-unknown-linux-gnu"
89

llvm/test/CodeGen/AArch64/sve-sdiv-pow2.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
22
; RUN: llc < %s | FileCheck %s
3+
; RUN: llc -use-constant-int-for-scalable-splat < %s | FileCheck %s
34

45
target triple = "aarch64-unknown-linux-gnu"
56

0 commit comments

Comments
 (0)