Skip to content

Commit 097ba53

Browse files
committed
[VPlan] Use VPTypeInfo in simplifyRecipes.
Replace getTypeForVPValue with the recently added, more general VPTypeAnalysis.
1 parent 95dd0b0 commit 097ba53

File tree

4 files changed

+32
-21
lines changed

4 files changed

+32
-21
lines changed

llvm/lib/Transforms/Vectorize/VPlanAnalysis.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ Type *VPTypeAnalysis::inferScalarTypeForRecipe(const VPReplicateRecipe *R) {
181181
return inferScalarType(R->getOperand(0));
182182
case Instruction::Load:
183183
return cast<LoadInst>(R->getUnderlyingInstr())->getType();
184+
case Instruction::Store:
185+
// FIXME: VPReplicateRecipes with store opcodes still define a result
186+
// VPValue, so we need to handle them here. Remove the code here once this
187+
// is modeled accurately in VPlan.
188+
return Type::getVoidTy(Ctx);
184189
default:
185190
break;
186191
}

llvm/lib/Transforms/Vectorize/VPlanAnalysis.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ class VPTypeAnalysis {
5454

5555
/// Infer the type of \p V. Returns the scalar type of \p V.
5656
Type *inferScalarType(const VPValue *V);
57+
58+
/// Return the LLVMContext used by the analysis.
59+
LLVMContext &getContext() { return Ctx; }
5760
};
5861

5962
} // end namespace llvm

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "VPlanTransforms.h"
1515
#include "VPRecipeBuilder.h"
16+
#include "VPlanAnalysis.h"
1617
#include "VPlanCFG.h"
1718
#include "VPlanDominatorTree.h"
1819
#include "llvm/ADT/PostOrderIterator.h"
@@ -802,17 +803,8 @@ static unsigned getOpcodeForRecipe(VPRecipeBase &R) {
802803
return 0;
803804
}
804805

805-
/// Return the scalar size in bits for \p VPV if possible.
806-
static Type *getTypeForVPValue(VPValue *VPV) {
807-
// TODO: Replace with VPlan type inference once ready.
808-
if (auto *VPC = dyn_cast<VPWidenCastRecipe>(VPV))
809-
return VPC->getResultType();
810-
auto *UV = VPV->getUnderlyingValue();
811-
return UV ? UV->getType() : nullptr;
812-
}
813-
814806
/// Try to simplify recipe \p R.
815-
static void simplifyRecipe(VPRecipeBase &R) {
807+
static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
816808
switch (getOpcodeForRecipe(R)) {
817809
case Instruction::Mul: {
818810
VPValue *A = R.getOperand(0);
@@ -829,9 +821,23 @@ static void simplifyRecipe(VPRecipeBase &R) {
829821
break;
830822
VPValue *A = Zext->getOperand(0);
831823
VPValue *Trunc = R.getVPSingleValue();
832-
Type *TruncToTy = getTypeForVPValue(Trunc);
833-
if (TruncToTy && TruncToTy == getTypeForVPValue(A))
824+
Type *TruncToTy = TypeInfo.inferScalarType(Trunc);
825+
if (TruncToTy && TruncToTy == TypeInfo.inferScalarType(A))
834826
Trunc->replaceAllUsesWith(A);
827+
828+
#ifndef NDEBUG
829+
// Verify that the cached type info is for both A and its users is still
830+
// accurate by comparing it to freshly computed types.
831+
VPTypeAnalysis TypeInfo2(TypeInfo.getContext());
832+
assert(TypeInfo.inferScalarType(A) == TypeInfo2.inferScalarType(A));
833+
for (VPUser *U : A->users()) {
834+
auto *R = dyn_cast<VPRecipeBase>(U);
835+
if (!R)
836+
continue;
837+
for (VPValue *VPV : R->definedValues())
838+
assert(TypeInfo.inferScalarType(VPV) == TypeInfo2.inferScalarType(VPV));
839+
}
840+
#endif
835841
break;
836842
}
837843
default:
@@ -840,12 +846,13 @@ static void simplifyRecipe(VPRecipeBase &R) {
840846
}
841847

842848
/// Try to simplify the recipes in \p Plan.
843-
static void simplifyRecipes(VPlan &Plan) {
849+
static void simplifyRecipes(VPlan &Plan, LLVMContext &Ctx) {
844850
ReversePostOrderTraversal<VPBlockDeepTraversalWrapper<VPBlockBase *>> RPOT(
845851
Plan.getEntry());
852+
VPTypeAnalysis TypeInfo(Ctx);
846853
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(RPOT)) {
847854
for (VPRecipeBase &R : make_early_inc_range(*VPBB)) {
848-
simplifyRecipe(R);
855+
simplifyRecipe(R, TypeInfo);
849856
}
850857
}
851858
}
@@ -855,7 +862,7 @@ void VPlanTransforms::optimize(VPlan &Plan, ScalarEvolution &SE) {
855862
removeRedundantInductionCasts(Plan);
856863

857864
optimizeInductions(Plan, SE);
858-
simplifyRecipes(Plan);
865+
simplifyRecipes(Plan, SE.getContext());
859866
removeDeadRecipes(Plan);
860867

861868
createAndOptimizeReplicateRegions(Plan);

llvm/test/Transforms/LoopVectorize/cast-induction.ll

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,8 @@ define void @redundant_iv_cast(ptr %dst) {
5353
; IC2-NEXT: [[OFFSET_IDX:%.+]] = trunc i32 [[CAN_IV]] to i16
5454
; IC2-NEXT: [[P0:%.+]] = add i16 [[OFFSET_IDX]], 0
5555
; IC2-NEXT: [[P1:%.+]] = add i16 [[OFFSET_IDX]], 1
56-
; IC2-NEXT: [[Z0:%.+]] = zext i16 [[P0]] to i32
57-
; IC2-NEXT: [[Z1:%.+]] = zext i16 [[P1]] to i32
58-
; IC2-NEXT: [[T0:%.+]] = trunc i32 [[Z0]] to i16
59-
; IC2-NEXT: [[T1:%.+]] = trunc i32 [[Z1]] to i16
60-
; IC2: store i16 [[T0]]
61-
; IC2-NEXT: store i16 [[T1]]
56+
; IC2: store i16 [[P0]]
57+
; IC2-NEXT: store i16 [[P1]]
6258
;
6359
entry:
6460
br label %loop

0 commit comments

Comments
 (0)