2016-04-25 01:55:41 +08:00
|
|
|
//===- ScalarEvolutionNormalization.cpp - See below -----------------------===//
|
2010-04-08 06:27:08 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements utilities for working with "normalized" expressions.
|
|
|
|
// See the comments at the top of ScalarEvolutionNormalization.h for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolutionNormalization.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2017-04-14 23:49:59 +08:00
|
|
|
/// TransformKind - Different types of transformations that
|
|
|
|
/// TransformForPostIncUse can do.
|
|
|
|
enum TransformKind {
|
|
|
|
/// Normalize - Normalize according to the given loops.
|
|
|
|
Normalize,
|
|
|
|
/// Denormalize - Perform the inverse transform on the expression with the
|
|
|
|
/// given loop set.
|
|
|
|
Denormalize
|
|
|
|
};
|
|
|
|
|
2017-04-15 00:47:12 +08:00
|
|
|
typedef DenseMap<const SCEV *, const SCEV *> NormalizedCacheTy;
|
2011-10-14 01:21:09 +08:00
|
|
|
|
2017-04-15 00:47:12 +08:00
|
|
|
static const SCEV *transformSubExpr(const TransformKind Kind,
|
|
|
|
NormalizePredTy Pred, ScalarEvolution &SE,
|
|
|
|
NormalizedCacheTy &Cache, const SCEV *S);
|
2011-10-14 01:21:09 +08:00
|
|
|
|
|
|
|
/// Implement post-inc transformation for all valid expression types.
|
2017-04-15 00:47:12 +08:00
|
|
|
static const SCEV *transformImpl(const TransformKind Kind, NormalizePredTy Pred,
|
|
|
|
ScalarEvolution &SE, NormalizedCacheTy &Cache,
|
|
|
|
const SCEV *S) {
|
2010-04-08 06:27:08 +08:00
|
|
|
if (const SCEVCastExpr *X = dyn_cast<SCEVCastExpr>(S)) {
|
|
|
|
const SCEV *O = X->getOperand();
|
2017-04-15 00:47:12 +08:00
|
|
|
const SCEV *N = transformSubExpr(Kind, Pred, SE, Cache, O);
|
2010-04-08 06:27:08 +08:00
|
|
|
if (O != N)
|
|
|
|
switch (S->getSCEVType()) {
|
|
|
|
case scZeroExtend: return SE.getZeroExtendExpr(N, S->getType());
|
|
|
|
case scSignExtend: return SE.getSignExtendExpr(N, S->getType());
|
|
|
|
case scTruncate: return SE.getTruncateExpr(N, S->getType());
|
|
|
|
default: llvm_unreachable("Unexpected SCEVCastExpr kind!");
|
|
|
|
}
|
|
|
|
return S;
|
|
|
|
}
|
2010-07-21 00:32:11 +08:00
|
|
|
|
2010-07-21 01:06:20 +08:00
|
|
|
if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
|
|
|
|
// An addrec. This is the interesting part.
|
|
|
|
SmallVector<const SCEV *, 8> Operands;
|
2017-04-14 09:33:13 +08:00
|
|
|
|
2017-04-14 23:49:53 +08:00
|
|
|
transform(AR->operands(), std::back_inserter(Operands),
|
2017-04-15 00:47:12 +08:00
|
|
|
[&](const SCEV *Op) {
|
|
|
|
return transformSubExpr(Kind, Pred, SE, Cache, Op);
|
|
|
|
});
|
2017-04-14 09:33:13 +08:00
|
|
|
|
2011-03-15 00:50:06 +08:00
|
|
|
// Conservatively use AnyWrap until/unless we need FlagNW.
|
2017-04-14 23:50:07 +08:00
|
|
|
const SCEV *Result =
|
|
|
|
SE.getAddRecExpr(Operands, AR->getLoop(), SCEV::FlagAnyWrap);
|
2010-07-21 01:06:20 +08:00
|
|
|
switch (Kind) {
|
|
|
|
case Normalize:
|
2014-03-19 01:34:03 +08:00
|
|
|
// We want to normalize step expression, because otherwise we might not be
|
|
|
|
// able to denormalize to the original expression.
|
|
|
|
//
|
|
|
|
// Here is an example what will happen if we don't normalize step:
|
|
|
|
// ORIGINAL ISE:
|
|
|
|
// {(100 /u {1,+,1}<%bb16>),+,(100 /u {1,+,1}<%bb16>)}<%bb25>
|
|
|
|
// NORMALIZED ISE:
|
|
|
|
// {((-1 * (100 /u {1,+,1}<%bb16>)) + (100 /u {0,+,1}<%bb16>)),+,
|
|
|
|
// (100 /u {0,+,1}<%bb16>)}<%bb25>
|
|
|
|
// DENORMALIZED BACK ISE:
|
|
|
|
// {((2 * (100 /u {1,+,1}<%bb16>)) + (-1 * (100 /u {2,+,1}<%bb16>))),+,
|
|
|
|
// (100 /u {1,+,1}<%bb16>)}<%bb25>
|
|
|
|
// Note that the initial value changes after normalization +
|
|
|
|
// denormalization, which isn't correct.
|
2017-04-14 23:50:07 +08:00
|
|
|
if (Pred(AR)) {
|
2010-07-21 01:06:20 +08:00
|
|
|
const SCEV *TransformedStep =
|
2017-04-15 00:47:12 +08:00
|
|
|
transformSubExpr(Kind, Pred, SE, Cache, AR->getStepRecurrence(SE));
|
2010-07-21 01:06:20 +08:00
|
|
|
Result = SE.getMinusSCEV(Result, TransformedStep);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Denormalize:
|
2014-03-19 01:34:03 +08:00
|
|
|
// Here we want to normalize step expressions for the same reasons, as
|
|
|
|
// stated above.
|
2017-04-14 23:50:07 +08:00
|
|
|
if (Pred(AR)) {
|
2014-03-19 01:34:03 +08:00
|
|
|
const SCEV *TransformedStep =
|
2017-04-15 00:47:12 +08:00
|
|
|
transformSubExpr(Kind, Pred, SE, Cache, AR->getStepRecurrence(SE));
|
2014-03-19 01:34:03 +08:00
|
|
|
Result = SE.getAddExpr(Result, TransformedStep);
|
|
|
|
}
|
2010-07-21 01:06:20 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2010-04-08 06:27:08 +08:00
|
|
|
if (const SCEVNAryExpr *X = dyn_cast<SCEVNAryExpr>(S)) {
|
|
|
|
SmallVector<const SCEV *, 8> Operands;
|
|
|
|
bool Changed = false;
|
2010-07-21 00:32:11 +08:00
|
|
|
// Transform each operand.
|
2017-04-14 23:50:19 +08:00
|
|
|
for (auto *O : X->operands()) {
|
2017-04-15 00:47:12 +08:00
|
|
|
const SCEV *N = transformSubExpr(Kind, Pred, SE, Cache, O);
|
2010-04-08 06:27:08 +08:00
|
|
|
Changed |= N != O;
|
|
|
|
Operands.push_back(N);
|
|
|
|
}
|
2010-07-21 01:06:20 +08:00
|
|
|
// If any operand actually changed, return a transformed result.
|
2010-04-08 06:27:08 +08:00
|
|
|
if (Changed)
|
|
|
|
switch (S->getSCEVType()) {
|
|
|
|
case scAddExpr: return SE.getAddExpr(Operands);
|
|
|
|
case scMulExpr: return SE.getMulExpr(Operands);
|
|
|
|
case scSMaxExpr: return SE.getSMaxExpr(Operands);
|
|
|
|
case scUMaxExpr: return SE.getUMaxExpr(Operands);
|
|
|
|
default: llvm_unreachable("Unexpected SCEVNAryExpr kind!");
|
|
|
|
}
|
|
|
|
return S;
|
|
|
|
}
|
2010-07-21 00:32:11 +08:00
|
|
|
|
2010-04-08 06:27:08 +08:00
|
|
|
if (const SCEVUDivExpr *X = dyn_cast<SCEVUDivExpr>(S)) {
|
|
|
|
const SCEV *LO = X->getLHS();
|
|
|
|
const SCEV *RO = X->getRHS();
|
2017-04-15 00:47:12 +08:00
|
|
|
const SCEV *LN = transformSubExpr(Kind, Pred, SE, Cache, LO);
|
|
|
|
const SCEV *RN = transformSubExpr(Kind, Pred, SE, Cache, RO);
|
2010-04-08 06:27:08 +08:00
|
|
|
if (LO != LN || RO != RN)
|
|
|
|
return SE.getUDivExpr(LN, RN);
|
|
|
|
return S;
|
|
|
|
}
|
2010-07-21 00:32:11 +08:00
|
|
|
|
2010-04-08 06:27:08 +08:00
|
|
|
llvm_unreachable("Unexpected SCEV kind!");
|
|
|
|
}
|
2011-10-14 01:21:09 +08:00
|
|
|
|
|
|
|
/// Manage recursive transformation across an expression DAG. Revisiting
|
|
|
|
/// expressions would lead to exponential recursion.
|
2017-04-15 00:47:12 +08:00
|
|
|
static const SCEV *transformSubExpr(const TransformKind Kind,
|
|
|
|
NormalizePredTy Pred, ScalarEvolution &SE,
|
|
|
|
NormalizedCacheTy &Cache, const SCEV *S) {
|
2011-10-14 01:21:09 +08:00
|
|
|
if (isa<SCEVConstant>(S) || isa<SCEVUnknown>(S))
|
|
|
|
return S;
|
|
|
|
|
2017-04-15 00:47:12 +08:00
|
|
|
const SCEV *Result = Cache.lookup(S);
|
2011-10-14 02:49:23 +08:00
|
|
|
if (Result)
|
|
|
|
return Result;
|
2011-10-14 01:21:09 +08:00
|
|
|
|
2017-04-15 00:47:12 +08:00
|
|
|
Result = transformImpl(Kind, Pred, SE, Cache, S);
|
|
|
|
Cache[S] = Result;
|
2011-10-14 02:49:23 +08:00
|
|
|
return Result;
|
2011-10-14 01:21:09 +08:00
|
|
|
}
|
|
|
|
|
2017-04-14 23:49:59 +08:00
|
|
|
const SCEV *llvm::normalizeForPostIncUse(const SCEV *S,
|
|
|
|
const PostIncLoopSet &Loops,
|
|
|
|
ScalarEvolution &SE) {
|
2017-04-14 23:50:07 +08:00
|
|
|
auto Pred = [&](const SCEVAddRecExpr *AR) {
|
|
|
|
return Loops.count(AR->getLoop());
|
|
|
|
};
|
2017-04-15 00:47:12 +08:00
|
|
|
NormalizedCacheTy Cache;
|
|
|
|
return transformSubExpr(Normalize, Pred, SE, Cache, S);
|
2017-04-14 23:49:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const SCEV *llvm::normalizeForPostIncUseIf(const SCEV *S, NormalizePredTy Pred,
|
|
|
|
ScalarEvolution &SE) {
|
2017-04-15 00:47:12 +08:00
|
|
|
NormalizedCacheTy Cache;
|
|
|
|
return transformSubExpr(Normalize, Pred, SE, Cache, S);
|
2017-04-14 23:49:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const SCEV *llvm::denormalizeForPostIncUse(const SCEV *S,
|
|
|
|
const PostIncLoopSet &Loops,
|
|
|
|
ScalarEvolution &SE) {
|
2017-04-14 23:50:07 +08:00
|
|
|
auto Pred = [&](const SCEVAddRecExpr *AR) {
|
|
|
|
return Loops.count(AR->getLoop());
|
|
|
|
};
|
2017-04-15 00:47:12 +08:00
|
|
|
NormalizedCacheTy Cache;
|
|
|
|
return transformSubExpr(Denormalize, Pred, SE, Cache, S);
|
2017-04-14 23:49:59 +08:00
|
|
|
}
|