Skip to content

Commit 098d3f9

Browse files
committed
[InstSimplify] Simplify to vector constants when possible
InstSimplify should do all transformations that ConstProp does, but one thing that ConstProp does that InstSimplify wouldn't is inline vector instructions that are constants, e.g. into a ret. Previously vector instructions wouldn't be inlined in InstSimplify because llvm::Simplify*Instruction() would return nullptr for specific instructions, such as vector instructions that were actually constants, if it couldn't simplify them. This changes SimplifyInsertElementInst, SimplifyExtractElementInst, and SimplifyShuffleVectorInst to return a vector constant when possible. Reviewed By: efriedma Differential Revision: https://reviews.llvm.org/D85946
1 parent 1446c18 commit 098d3f9

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4302,7 +4302,7 @@ Value *llvm::SimplifyInsertElementInst(Value *Vec, Value *Val, Value *Idx,
43024302
auto *ValC = dyn_cast<Constant>(Val);
43034303
auto *IdxC = dyn_cast<Constant>(Idx);
43044304
if (VecC && ValC && IdxC)
4305-
return ConstantFoldInsertElementInstruction(VecC, ValC, IdxC);
4305+
return ConstantExpr::getInsertElement(VecC, ValC, IdxC);
43064306

43074307
// For fixed-length vector, fold into undef if index is out of bounds.
43084308
if (auto *CI = dyn_cast<ConstantInt>(Idx)) {
@@ -4367,7 +4367,7 @@ static Value *SimplifyExtractElementInst(Value *Vec, Value *Idx,
43674367
auto *VecVTy = cast<VectorType>(Vec->getType());
43684368
if (auto *CVec = dyn_cast<Constant>(Vec)) {
43694369
if (auto *CIdx = dyn_cast<Constant>(Idx))
4370-
return ConstantFoldExtractElementInstruction(CVec, CIdx);
4370+
return ConstantExpr::getExtractElement(CVec, CIdx);
43714371

43724372
// The index is not relevant if our vector is a splat.
43734373
if (auto *Splat = CVec->getSplatValue())
@@ -4565,8 +4565,8 @@ static Value *SimplifyShuffleVectorInst(Value *Op0, Value *Op1,
45654565
// If all operands are constant, constant fold the shuffle. This
45664566
// transformation depends on the value of the mask which is not known at
45674567
// compile time for scalable vectors
4568-
if (!Scalable && Op0Const && Op1Const)
4569-
return ConstantFoldShuffleVectorInstruction(Op0Const, Op1Const, Mask);
4568+
if (Op0Const && Op1Const)
4569+
return ConstantExpr::getShuffleVector(Op0Const, Op1Const, Mask);
45704570

45714571
// Canonicalization: if only one input vector is constant, it shall be the
45724572
// second one. This transformation depends on the value of the mask which

llvm/test/Analysis/ConstantFolding/vscale-shufflevector.ll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ target triple = "aarch64"
1515
; the compiler. It happens to be the case that this will be the result.
1616

1717
; CHECK-LABEL: define <vscale x 8 x i1> @vscale_version()
18-
; CHECK-NEXT: %splatter = insertelement <vscale x 8 x i1> undef, i1 true, i32 0
19-
; CHECK-NEXT: %foo = shufflevector <vscale x 8 x i1> %splatter, <vscale x 8 x i1> undef, <vscale x 8 x i32> zeroinitializer
20-
; CHECK-NEXT: ret <vscale x 8 x i1> %foo
18+
; CHECK-NEXT: ret <vscale x 8 x i1> shufflevector (<vscale x 8 x i1> insertelement (<vscale x 8 x i1> undef, i1 true, i32 0), <vscale x 8 x i1> undef, <vscale x 8 x i32> zeroinitializer)
2119

2220
define <vscale x 8 x i1> @vscale_version() {
2321
%splatter = insertelement <vscale x 8 x i1> undef, i1 true, i32 0

llvm/test/Transforms/InstSimplify/vscale.ll

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,23 @@ define <vscale x 4 x i32> @insert_extract_element_same_vec_idx_1(<vscale x 4 x i
5151
ret <vscale x 4 x i32> %r
5252
}
5353

54+
define <vscale x 4 x i32> @insertelement_inline_to_ret() {
55+
; CHECK-LABEL: @insertelement_inline_to_ret(
56+
; CHECK-NEXT: ret <vscale x 4 x i32> insertelement (<vscale x 4 x i32> undef, i32 1, i32 0)
57+
;
58+
%i = insertelement <vscale x 4 x i32> undef, i32 1, i32 0
59+
ret <vscale x 4 x i32> %i
60+
}
61+
62+
define <vscale x 4 x i32> @insertelement_shufflevector_inline_to_ret() {
63+
; CHECK-LABEL: @insertelement_shufflevector_inline_to_ret(
64+
; CHECK-NEXT: ret <vscale x 4 x i32> shufflevector (<vscale x 4 x i32> insertelement (<vscale x 4 x i32> undef, i32 1, i32 0), <vscale x 4 x i32> undef, <vscale x 4 x i32> zeroinitializer)
65+
;
66+
%i = insertelement <vscale x 4 x i32> undef, i32 1, i32 0
67+
%i2 = shufflevector <vscale x 4 x i32> %i, <vscale x 4 x i32> undef, <vscale x 4 x i32> zeroinitializer
68+
ret <vscale x 4 x i32> %i2
69+
}
70+
5471
; extractelement
5572

5673
define i32 @extractelement_idx_undef(<vscale x 4 x i32> %a) {
@@ -120,6 +137,16 @@ define <vscale x 2 x i1> @cmp_le_smax_always_true(<vscale x 2 x i64> %x) {
120137
ret <vscale x 2 x i1> %cmp
121138
}
122139

140+
define <vscale x 4 x float> @bitcast() {
141+
; CHECK-LABEL: @bitcast(
142+
; CHECK-NEXT: ret <vscale x 4 x float> bitcast (<vscale x 4 x i32> shufflevector (<vscale x 4 x i32> insertelement (<vscale x 4 x i32> undef, i32 1, i32 0), <vscale x 4 x i32> undef, <vscale x 4 x i32> zeroinitializer) to <vscale x 4 x float>)
143+
;
144+
%i1 = insertelement <vscale x 4 x i32> undef, i32 1, i32 0
145+
%i2 = shufflevector <vscale x 4 x i32> %i1, <vscale x 4 x i32> undef, <vscale x 4 x i32> zeroinitializer
146+
%i3 = bitcast <vscale x 4 x i32> %i2 to <vscale x 4 x float>
147+
ret <vscale x 4 x float> %i3
148+
}
149+
123150
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
124151
;; Memory Access and Addressing Operations
125152
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

0 commit comments

Comments
 (0)