Skip to content

Commit 5f39be5

Browse files
authored
[VPlan] Use InstSimplifyFolder instead of TargetFolder (#141222)
For more powerful folding with operands that are not necessarily all-constant, use InstSimplifyFolder instead of TargetFolder in tryToConstantFold, and rename the function tryToFoldLiveIns.
1 parent 2b9ded6 commit 5f39be5

24 files changed

+333
-392
lines changed

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
#include "llvm/ADT/SetVector.h"
2828
#include "llvm/ADT/TypeSwitch.h"
2929
#include "llvm/Analysis/IVDescriptors.h"
30+
#include "llvm/Analysis/InstSimplifyFolder.h"
3031
#include "llvm/Analysis/LoopInfo.h"
31-
#include "llvm/Analysis/TargetFolder.h"
3232
#include "llvm/Analysis/VectorUtils.h"
3333
#include "llvm/IR/Intrinsics.h"
3434
#include "llvm/IR/PatternMatch.h"
@@ -939,20 +939,20 @@ static void recursivelyDeleteDeadRecipes(VPValue *V) {
939939
}
940940
}
941941

942-
/// Try to fold \p R using TargetFolder to a constant. Will succeed and return a
943-
/// non-nullptr Value for a handled \p Opcode if all \p Operands are constant.
944-
static Value *tryToConstantFold(const VPRecipeBase &R, unsigned Opcode,
945-
ArrayRef<VPValue *> Operands,
946-
const DataLayout &DL,
947-
VPTypeAnalysis &TypeInfo) {
942+
/// Try to fold \p R using InstSimplifyFolder. Will succeed and return a
943+
/// non-nullptr Value for a handled \p Opcode if corresponding \p Operands are
944+
/// foldable live-ins.
945+
static Value *tryToFoldLiveIns(const VPRecipeBase &R, unsigned Opcode,
946+
ArrayRef<VPValue *> Operands,
947+
const DataLayout &DL, VPTypeAnalysis &TypeInfo) {
948948
SmallVector<Value *, 4> Ops;
949949
for (VPValue *Op : Operands) {
950950
if (!Op->isLiveIn() || !Op->getLiveInIRValue())
951951
return nullptr;
952952
Ops.push_back(Op->getLiveInIRValue());
953953
}
954954

955-
TargetFolder Folder(DL);
955+
InstSimplifyFolder Folder(DL);
956956
if (Instruction::isBinaryOp(Opcode))
957957
return Folder.FoldBinOp(static_cast<Instruction::BinaryOps>(Opcode), Ops[0],
958958
Ops[1]);
@@ -994,15 +994,16 @@ static Value *tryToConstantFold(const VPRecipeBase &R, unsigned Opcode,
994994
static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
995995
using namespace llvm::VPlanPatternMatch;
996996

997-
// Constant folding.
997+
// Simplification of live-in IR values for SingleDef recipes using
998+
// InstSimplifyFolder.
998999
if (TypeSwitch<VPRecipeBase *, bool>(&R)
9991000
.Case<VPInstruction, VPWidenRecipe, VPWidenCastRecipe,
10001001
VPReplicateRecipe>([&](auto *I) {
10011002
VPlan *Plan = R.getParent()->getPlan();
10021003
const DataLayout &DL =
10031004
Plan->getScalarHeader()->getIRBasicBlock()->getDataLayout();
1004-
Value *V = tryToConstantFold(*I, I->getOpcode(), I->operands(), DL,
1005-
TypeInfo);
1005+
Value *V = tryToFoldLiveIns(*I, I->getOpcode(), I->operands(), DL,
1006+
TypeInfo);
10061007
if (V)
10071008
I->replaceAllUsesWith(Plan->getOrAddLiveIn(V));
10081009
return V;

llvm/test/Transforms/LoopVectorize/AArch64/call-costs.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,10 @@ define void @powi_call(ptr %P) {
7979
; CHECK: [[VECTOR_PH]]:
8080
; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
8181
; CHECK: [[VECTOR_BODY]]:
82-
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds double, ptr [[P]], i64 0
83-
; CHECK-NEXT: [[TMP2:%.*]] = getelementptr inbounds double, ptr [[TMP1]], i32 0
82+
; CHECK-NEXT: [[TMP2:%.*]] = getelementptr inbounds double, ptr [[P]], i32 0
8483
; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <2 x double>, ptr [[TMP2]], align 8
8584
; CHECK-NEXT: [[TMP3:%.*]] = call <2 x double> @llvm.powi.v2f64.i32(<2 x double> [[WIDE_LOAD]], i32 3)
86-
; CHECK-NEXT: [[TMP4:%.*]] = getelementptr inbounds double, ptr [[TMP1]], i32 0
85+
; CHECK-NEXT: [[TMP4:%.*]] = getelementptr inbounds double, ptr [[P]], i32 0
8786
; CHECK-NEXT: store <2 x double> [[TMP3]], ptr [[TMP4]], align 8
8887
; CHECK-NEXT: br label %[[MIDDLE_BLOCK:.*]]
8988
; CHECK: [[MIDDLE_BLOCK]]:

llvm/test/Transforms/LoopVectorize/AArch64/deterministic-type-shrinkage.ll

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -160,45 +160,45 @@ define void @test_shrink_zext_in_preheader(ptr noalias %src, ptr noalias %dst, i
160160
; CHECK: vector.main.loop.iter.check:
161161
; CHECK-NEXT: br i1 false, label [[VEC_EPILOG_PH:%.*]], label [[VECTOR_PH:%.*]]
162162
; CHECK: vector.ph:
163+
; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <16 x i16> poison, i16 [[B]], i64 0
163164
; CHECK-NEXT: [[TMP0:%.*]] = trunc i32 [[A]] to i16
164165
; CHECK-NEXT: [[TMP1:%.*]] = insertelement <16 x i16> poison, i16 [[TMP0]], i64 0
165-
; CHECK-NEXT: [[TMP2:%.*]] = insertelement <16 x i16> poison, i16 [[B]], i64 0
166-
; CHECK-NEXT: [[TMP3:%.*]] = mul <16 x i16> [[TMP1]], [[TMP2]]
167-
; CHECK-NEXT: [[TMP4:%.*]] = lshr <16 x i16> [[TMP3]], <i16 8, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison>
168-
; CHECK-NEXT: [[TMP5:%.*]] = trunc nuw <16 x i16> [[TMP4]] to <16 x i8>
169-
; CHECK-NEXT: [[TMP6:%.*]] = shufflevector <16 x i8> [[TMP5]], <16 x i8> poison, <16 x i32> zeroinitializer
166+
; CHECK-NEXT: [[TMP2:%.*]] = mul <16 x i16> [[TMP1]], [[BROADCAST_SPLATINSERT]]
167+
; CHECK-NEXT: [[TMP3:%.*]] = lshr <16 x i16> [[TMP2]], <i16 8, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison>
168+
; CHECK-NEXT: [[TMP4:%.*]] = trunc nuw <16 x i16> [[TMP3]] to <16 x i8>
169+
; CHECK-NEXT: [[TMP5:%.*]] = shufflevector <16 x i8> [[TMP4]], <16 x i8> poison, <16 x i32> zeroinitializer
170170
; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
171171
; CHECK: vector.body:
172172
; CHECK-NEXT: [[INDEX:%.*]] = phi i32 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
173-
; CHECK-NEXT: [[TMP7:%.*]] = sext i32 [[INDEX]] to i64
174-
; CHECK-NEXT: [[TMP8:%.*]] = getelementptr inbounds i8, ptr [[DST]], i64 [[TMP7]]
175-
; CHECK-NEXT: [[TMP9:%.*]] = getelementptr inbounds nuw i8, ptr [[TMP8]], i64 16
176-
; CHECK-NEXT: store <16 x i8> [[TMP6]], ptr [[TMP8]], align 1
177-
; CHECK-NEXT: store <16 x i8> [[TMP6]], ptr [[TMP9]], align 1
173+
; CHECK-NEXT: [[TMP6:%.*]] = sext i32 [[INDEX]] to i64
174+
; CHECK-NEXT: [[TMP7:%.*]] = getelementptr inbounds i8, ptr [[DST]], i64 [[TMP6]]
175+
; CHECK-NEXT: [[TMP8:%.*]] = getelementptr inbounds nuw i8, ptr [[TMP7]], i64 16
176+
; CHECK-NEXT: store <16 x i8> [[TMP5]], ptr [[TMP7]], align 1
177+
; CHECK-NEXT: store <16 x i8> [[TMP5]], ptr [[TMP8]], align 1
178178
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i32 [[INDEX]], 32
179-
; CHECK-NEXT: [[TMP10:%.*]] = icmp eq i32 [[INDEX_NEXT]], 992
180-
; CHECK-NEXT: br i1 [[TMP10]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP5:![0-9]+]]
179+
; CHECK-NEXT: [[TMP9:%.*]] = icmp eq i32 [[INDEX_NEXT]], 992
180+
; CHECK-NEXT: br i1 [[TMP9]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP5:![0-9]+]]
181181
; CHECK: middle.block:
182182
; CHECK-NEXT: br i1 false, label [[EXIT:%.*]], label [[VEC_EPILOG_ITER_CHECK:%.*]]
183183
; CHECK: vec.epilog.iter.check:
184184
; CHECK-NEXT: br i1 false, label [[VEC_EPILOG_SCALAR_PH]], label [[VEC_EPILOG_PH]]
185185
; CHECK: vec.epilog.ph:
186-
; CHECK-NEXT: [[TMP11:%.*]] = trunc i32 [[A]] to i16
187-
; CHECK-NEXT: [[TMP12:%.*]] = insertelement <8 x i16> poison, i16 [[TMP11]], i64 0
188-
; CHECK-NEXT: [[TMP13:%.*]] = insertelement <8 x i16> poison, i16 [[B]], i64 0
189-
; CHECK-NEXT: [[TMP14:%.*]] = mul <8 x i16> [[TMP12]], [[TMP13]]
190-
; CHECK-NEXT: [[TMP15:%.*]] = lshr <8 x i16> [[TMP14]], <i16 8, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison>
191-
; CHECK-NEXT: [[TMP16:%.*]] = trunc nuw <8 x i16> [[TMP15]] to <8 x i8>
192-
; CHECK-NEXT: [[TMP17:%.*]] = shufflevector <8 x i8> [[TMP16]], <8 x i8> poison, <8 x i32> zeroinitializer
186+
; CHECK-NEXT: [[BROADCAST_SPLATINSERT3:%.*]] = insertelement <8 x i16> poison, i16 [[B]], i64 0
187+
; CHECK-NEXT: [[TMP10:%.*]] = trunc i32 [[A]] to i16
188+
; CHECK-NEXT: [[TMP11:%.*]] = insertelement <8 x i16> poison, i16 [[TMP10]], i64 0
189+
; CHECK-NEXT: [[TMP12:%.*]] = mul <8 x i16> [[TMP11]], [[BROADCAST_SPLATINSERT3]]
190+
; CHECK-NEXT: [[TMP13:%.*]] = lshr <8 x i16> [[TMP12]], <i16 8, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison, i16 poison>
191+
; CHECK-NEXT: [[TMP14:%.*]] = trunc nuw <8 x i16> [[TMP13]] to <8 x i8>
192+
; CHECK-NEXT: [[TMP15:%.*]] = shufflevector <8 x i8> [[TMP14]], <8 x i8> poison, <8 x i32> zeroinitializer
193193
; CHECK-NEXT: br label [[VEC_EPILOG_VECTOR_BODY:%.*]]
194194
; CHECK: vec.epilog.vector.body:
195195
; CHECK-NEXT: [[INDEX7:%.*]] = phi i32 [ 992, [[VEC_EPILOG_PH]] ], [ [[INDEX_NEXT8:%.*]], [[VEC_EPILOG_VECTOR_BODY]] ]
196-
; CHECK-NEXT: [[TMP18:%.*]] = sext i32 [[INDEX7]] to i64
197-
; CHECK-NEXT: [[TMP19:%.*]] = getelementptr inbounds i8, ptr [[DST]], i64 [[TMP18]]
198-
; CHECK-NEXT: store <8 x i8> [[TMP17]], ptr [[TMP19]], align 1
196+
; CHECK-NEXT: [[TMP16:%.*]] = sext i32 [[INDEX7]] to i64
197+
; CHECK-NEXT: [[TMP17:%.*]] = getelementptr inbounds i8, ptr [[DST]], i64 [[TMP16]]
198+
; CHECK-NEXT: store <8 x i8> [[TMP15]], ptr [[TMP17]], align 1
199199
; CHECK-NEXT: [[INDEX_NEXT8]] = add nuw i32 [[INDEX7]], 8
200-
; CHECK-NEXT: [[TMP20:%.*]] = icmp eq i32 [[INDEX_NEXT8]], 1000
201-
; CHECK-NEXT: br i1 [[TMP20]], label [[VEC_EPILOG_MIDDLE_BLOCK:%.*]], label [[VEC_EPILOG_VECTOR_BODY]], !llvm.loop [[LOOP6:![0-9]+]]
200+
; CHECK-NEXT: [[TMP18:%.*]] = icmp eq i32 [[INDEX_NEXT8]], 1000
201+
; CHECK-NEXT: br i1 [[TMP18]], label [[VEC_EPILOG_MIDDLE_BLOCK:%.*]], label [[VEC_EPILOG_VECTOR_BODY]], !llvm.loop [[LOOP6:![0-9]+]]
202202
; CHECK: vec.epilog.middle.block:
203203
; CHECK-NEXT: br i1 true, label [[EXIT]], label [[VEC_EPILOG_SCALAR_PH]]
204204
; CHECK: vec.epilog.scalar.ph:

0 commit comments

Comments
 (0)