BlockGenerator: Extract value synthesis into its own function [NFC]

This will allow us to reuse this code in a subsequent commit.

llvm-svn: 248893
This commit is contained in:
Tobias Grosser 2015-09-30 11:56:19 +00:00
parent 029d8531e6
commit 33cb9f9419
2 changed files with 56 additions and 21 deletions

View File

@ -404,6 +404,31 @@ protected:
/// the original value in the non-optimized SCoP.
void createScalarFinalization(Region &R);
/// @brief Try to synthesize a new value
///
/// Given an old value, we try to synthesize it in a new context from its
/// original SCEV expression. We start from the original SCEV expression,
/// then replace outdated parameter and loop references, and finally
/// expand it to code that computes this updated expression.
///
/// @param Stmt The statement to code generate
/// @param Old The old Value
/// @param BBMap A mapping from old values to their new values
/// (for values recalculated within this basic block)
/// @param LTS A mapping from loops virtual canonical induction
/// variable to their new values
/// (for values recalculated in the new ScoP, but not
/// within this basic block)
/// @param L The loop that surrounded the instruction that referenced
/// this value in the original code. This loop is used to
/// evaluate the scalar evolution at the right scope.
///
/// @returns o A newly synthesized value.
/// o NULL, if synthesizing the value failed.
Value *trySynthesizeNewValue(ScopStmt &Stmt, const Value *Old,
ValueMapT &BBMap, LoopToScevMapT &LTS,
Loop *L) const;
/// @brief Get the new version of a value.
///
/// Given an old value, we first check if a new version of this value is

View File

@ -99,27 +99,10 @@ BlockGenerator::BlockGenerator(PollyIRBuilder &B, LoopInfo &LI,
EntryBB(nullptr), PHIOpMap(PHIOpMap), ScalarMap(ScalarMap),
EscapeMap(EscapeMap), GlobalMap(GlobalMap) {}
Value *BlockGenerator::getNewValue(ScopStmt &Stmt, const Value *Old,
ValueMapT &BBMap, LoopToScevMapT &LTS,
Loop *L) const {
// We assume constants never change.
// This avoids map lookups for many calls to this function.
if (isa<Constant>(Old))
return const_cast<Value *>(Old);
if (Value *New = GlobalMap.lookup(Old)) {
if (Value *NewRemapped = GlobalMap.lookup(New))
New = NewRemapped;
if (Old->getType()->getScalarSizeInBits() <
New->getType()->getScalarSizeInBits())
New = Builder.CreateTruncOrBitCast(New, Old->getType());
return New;
}
if (Value *New = BBMap.lookup(Old))
return New;
Value *BlockGenerator::trySynthesizeNewValue(ScopStmt &Stmt, const Value *Old,
ValueMapT &BBMap,
LoopToScevMapT &LTS,
Loop *L) const {
if (SE.isSCEVable(Old->getType()))
if (const SCEV *Scev = SE.getSCEVAtScope(const_cast<Value *>(Old), L)) {
if (!isa<SCEVCouldNotCompute>(Scev)) {
@ -144,6 +127,33 @@ Value *BlockGenerator::getNewValue(ScopStmt &Stmt, const Value *Old,
}
}
return nullptr;
}
Value *BlockGenerator::getNewValue(ScopStmt &Stmt, const Value *Old,
ValueMapT &BBMap, LoopToScevMapT &LTS,
Loop *L) const {
// We assume constants never change.
// This avoids map lookups for many calls to this function.
if (isa<Constant>(Old))
return const_cast<Value *>(Old);
if (Value *New = GlobalMap.lookup(Old)) {
if (Value *NewRemapped = GlobalMap.lookup(New))
New = NewRemapped;
if (Old->getType()->getScalarSizeInBits() <
New->getType()->getScalarSizeInBits())
New = Builder.CreateTruncOrBitCast(New, Old->getType());
return New;
}
if (Value *New = BBMap.lookup(Old))
return New;
if (Value *New = trySynthesizeNewValue(Stmt, Old, BBMap, LTS, L))
return New;
// A scop-constant value defined by a global or a function parameter.
if (isa<GlobalValue>(Old) || isa<Argument>(Old))
return const_cast<Value *>(Old);