Skip to content

Commit 88704a1

Browse files
committed
Define EfficiencyLevel
1 parent 0c37d3f commit 88704a1

File tree

1 file changed

+42
-32
lines changed

1 file changed

+42
-32
lines changed

llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -215,20 +215,23 @@ class StraightLineStrengthReduce {
215215
/// Cost model: Evaluate the computational efficiency of the candidate.
216216
///
217217
/// Efficiency levels (higher is better):
218-
/// 5 - No instruction:
219-
/// [Variable] or [Const]
220-
/// 4 - One instruction with one variable:
221-
/// [Variable + Const] or [Variable * Const]
222-
/// 3 - One instruction with two variables:
223-
/// [Variable + Variable] or [Variable * Variable]
224-
/// 2 - Two instructions with one variable:
225-
/// [Const + Const * Variable]
226-
/// 1 - Two instructions with two variables:
227-
/// [Variable + Const * Variable]
228-
static unsigned getComputationEfficiency(Kind CandidateKind,
229-
const ConstantInt *Index,
230-
const Value *Stride,
231-
const SCEV *Base = nullptr) {
218+
/// ZeroInst (5) - [Variable] or [Const]
219+
/// OneInstOneVar (4) - [Variable + Const] or [Variable * Const]
220+
/// OneInstTwoVar (3) - [Variable + Variable] or [Variable * Variable]
221+
/// TwoInstOneVar (2) - [Const + Const * Variable]
222+
/// TwoInstTwoVar (1) - [Variable + Const * Variable]
223+
enum EfficiencyLevel : unsigned {
224+
Unknown = 0,
225+
TwoInstTwoVar = 1,
226+
TwoInstOneVar = 2,
227+
OneInstTwoVar = 3,
228+
OneInstOneVar = 4,
229+
ZeroInst = 5
230+
};
231+
232+
static EfficiencyLevel
233+
getComputationEfficiency(Kind CandidateKind, const ConstantInt *Index,
234+
const Value *Stride, const SCEV *Base = nullptr) {
232235
bool IsConstantBase = false;
233236
bool IsZeroBase = false;
234237
// When evaluating the efficiency of a rewrite, if the Basis's SCEV is
@@ -243,42 +246,47 @@ class StraightLineStrengthReduce {
243246
IsConstantStride && cast<ConstantInt>(Stride)->isZero();
244247
// All constants
245248
if (IsConstantBase && IsConstantStride)
246-
return 5;
249+
return ZeroInst;
247250

248251
// [(Base + Index) * Stride]
249252
if (CandidateKind == Mul) {
250253
if (IsZeroStride)
251-
return 5;
254+
return ZeroInst;
252255
if (Index->isZero())
253-
return (IsConstantStride || IsConstantBase) ? 4 : 3;
256+
return (IsConstantStride || IsConstantBase) ? OneInstOneVar
257+
: OneInstTwoVar;
254258

255259
if (IsConstantBase)
256-
return IsZeroBase && (Index->isOne() || Index->isMinusOne()) ? 5 : 4;
260+
return IsZeroBase && (Index->isOne() || Index->isMinusOne())
261+
? ZeroInst
262+
: OneInstOneVar;
257263

258264
if (IsConstantStride) {
259265
auto *CI = cast<ConstantInt>(Stride);
260-
return (CI->isOne() || CI->isMinusOne()) ? 4 : 2;
266+
return (CI->isOne() || CI->isMinusOne()) ? OneInstOneVar
267+
: TwoInstOneVar;
261268
}
262-
return 1;
269+
return TwoInstTwoVar;
263270
}
264271

265272
// Base + Index * Stride
266273
assert(CandidateKind == Add || CandidateKind == GEP);
267274
if (Index->isZero() || IsZeroStride)
268-
return 5;
275+
return ZeroInst;
269276

270277
bool IsSimpleIndex = Index->isOne() || Index->isMinusOne();
271278

272279
if (IsConstantBase)
273-
return IsZeroBase ? (IsSimpleIndex ? 5 : 4) : (IsSimpleIndex ? 4 : 2);
280+
return IsZeroBase ? (IsSimpleIndex ? ZeroInst : OneInstOneVar)
281+
: (IsSimpleIndex ? OneInstOneVar : TwoInstOneVar);
274282

275283
if (IsConstantStride)
276-
return IsZeroStride ? 5 : 4;
284+
return IsZeroStride ? ZeroInst : OneInstOneVar;
277285

278286
if (IsSimpleIndex)
279-
return 3;
287+
return OneInstTwoVar;
280288

281-
return 1;
289+
return TwoInstTwoVar;
282290
}
283291

284292
// Evaluate if the given delta is profitable to rewrite this candidate.
@@ -299,12 +307,13 @@ class StraightLineStrengthReduce {
299307
}
300308

301309
// Evaluate the rewrite profit of this candidate with its Basis
302-
unsigned getRewriteProfit() const {
303-
return Basis ? getRewriteProfit(Delta, DeltaKind) : 0;
310+
EfficiencyLevel getRewriteProfit() const {
311+
return Basis ? getRewriteProfit(Delta, DeltaKind) : Unknown;
304312
}
305313

306314
// Evaluate the rewrite profit of this candidate with a given delta
307-
unsigned getRewriteProfit(const Value *Delta, const DKind DeltaKind) const {
315+
EfficiencyLevel getRewriteProfit(const Value *Delta,
316+
const DKind DeltaKind) const {
308317
switch (DeltaKind) {
309318
case BaseDelta: // [X + Delta]
310319
return getComputationEfficiency(
@@ -316,12 +325,13 @@ class StraightLineStrengthReduce {
316325
return getComputationEfficiency(CandidateKind, cast<ConstantInt>(Delta),
317326
Stride);
318327
default:
319-
return 0;
328+
return Unknown;
320329
}
321330
}
322331

323332
bool isHighEfficiency() const {
324-
return getComputationEfficiency(CandidateKind, Index, Stride, Base) >= 4;
333+
return getComputationEfficiency(CandidateKind, Index, Stride, Base) >=
334+
OneInstOneVar;
325335
}
326336

327337
// Verify that this candidate has valid delta components relative to the
@@ -769,8 +779,8 @@ void StraightLineStrengthReduce::setBasisAndDeltaFor(Candidate &C) {
769779
// Z = A + 3
770780
// Return the delta info for C aginst the new Basis
771781
auto StraightLineStrengthReduce::compressPath(Candidate &C,
772-
Candidate *Basis) const
773-
-> DeltaInfo {
782+
Candidate *Basis) const
783+
-> DeltaInfo {
774784
if (!Basis || !Basis->Basis || C.CandidateKind == Candidate::Mul)
775785
return {};
776786
Candidate *Root = Basis;

0 commit comments

Comments
 (0)