Skip to content

Commit 7fbfcc6

Browse files
committed
[LV/LAA] Use PSE to identify stride multiplies which simplify [mostly nfc]
LV/LAA will speculate that (some) strided access patterns have unit stride, and insert runtime checks if required. LV cost models a multiply by such a stride as free. We did this by keeping around the StrideSet structure, just to check if one of the operands were one of the strides we speculated. We can instead just ask PredicatedScalarEvolution if either of the operands are one (after predicates are applied). We get mostly the same result - PSE can prove it in more cases in theory - and simpler code.
1 parent dcac993 commit 7fbfcc6

File tree

4 files changed

+6
-16
lines changed

4 files changed

+6
-16
lines changed

llvm/include/llvm/Analysis/LoopAccessAnalysis.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -616,9 +616,6 @@ class LoopAccessInfo {
616616
return SymbolicStrides;
617617
}
618618

619-
/// Pointer has a symbolic stride.
620-
bool hasStride(Value *V) const { return StrideSet.count(V); }
621-
622619
/// Print the information about the memory accesses in the loop.
623620
void print(raw_ostream &OS, unsigned Depth = 0) const;
624621

@@ -702,9 +699,6 @@ class LoopAccessInfo {
702699
/// If an access has a symbolic strides, this maps the pointer value to
703700
/// the stride symbol.
704701
DenseMap<Value *, const SCEV *> SymbolicStrides;
705-
706-
/// Set of symbolic strides values.
707-
SmallPtrSet<Value *, 8> StrideSet;
708702
};
709703

710704
Value *stripIntegerCast(Value *V);

llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,6 @@ class LoopVectorizationLegality {
371371
return LAI->getDepChecker().getMaxSafeVectorWidthInBits();
372372
}
373373

374-
bool hasStride(Value *V) { return LAI->hasStride(V); }
375-
376374
/// Returns true if vector representation of the instruction \p I
377375
/// requires mask.
378376
bool isMaskRequired(const Instruction *I) const {

llvm/lib/Analysis/LoopAccessAnalysis.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2758,7 +2758,6 @@ void LoopAccessInfo::collectStridedAccess(Value *MemAccess) {
27582758
// SCEVUnknown as we expect.
27592759
Value *StrideVal = stripIntegerCast(Stride);
27602760
SymbolicStrides[Ptr] = cast<SCEVUnknown>(PSE->getSCEV(StrideVal));
2761-
StrideSet.insert(Stride);
27622761
}
27632762

27642763
LoopAccessInfo::LoopAccessInfo(Loop *L, ScalarEvolution *SE,

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6447,11 +6447,6 @@ static const SCEV *getAddressAccessSCEV(
64476447
return PSE.getSCEV(Ptr);
64486448
}
64496449

6450-
static bool isStrideMul(Instruction *I, LoopVectorizationLegality *Legal) {
6451-
return Legal->hasStride(I->getOperand(0)) ||
6452-
Legal->hasStride(I->getOperand(1));
6453-
}
6454-
64556450
InstructionCost
64566451
LoopVectorizationCostModel::getMemInstScalarizationCost(Instruction *I,
64576452
ElementCount VF) {
@@ -7219,8 +7214,12 @@ LoopVectorizationCostModel::getInstructionCost(Instruction *I, ElementCount VF,
72197214
case Instruction::And:
72207215
case Instruction::Or:
72217216
case Instruction::Xor: {
7222-
// Since we will replace the stride by 1 the multiplication should go away.
7223-
if (I->getOpcode() == Instruction::Mul && isStrideMul(I, Legal))
7217+
// If we're speculating on the stride being 1, the multiplication may
7218+
// fold away. We can generalize this for all operations using the notion
7219+
// of neutral elements. (TODO)
7220+
if (I->getOpcode() == Instruction::Mul &&
7221+
(PSE.getSCEV(I->getOperand(0))->isOne() ||
7222+
PSE.getSCEV(I->getOperand(1))->isOne()))
72247223
return 0;
72257224

72267225
// Detect reduction patterns

0 commit comments

Comments
 (0)