2011-04-29 14:27:02 +08:00
|
|
|
//===---- CodePreparation.cpp - Code preparation for Scop Detection -------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2013-03-23 08:13:39 +08:00
|
|
|
// The Polly code preparation pass is executed before SCoP detection. Its only
|
|
|
|
// use is to translate all PHI nodes that can not be expressed by the code
|
2014-11-30 22:33:31 +08:00
|
|
|
// generator into explicit memory dependences.
|
2013-03-23 08:13:39 +08:00
|
|
|
//
|
2014-11-30 22:33:31 +08:00
|
|
|
// Polly's code generation can code generate all PHI nodes that do not
|
2013-03-23 08:13:39 +08:00
|
|
|
// reference parameters within the scop. As the code preparation pass is run
|
|
|
|
// before scop detection, we can not check this condition, because without
|
|
|
|
// a detected scop, we do not know SCEVUnknowns that appear in the SCEV of
|
|
|
|
// a PHI node may later be within or outside of the SCoP. Hence, we follow a
|
|
|
|
// heuristic and translate all PHI nodes that are either directly SCEVUnknown
|
|
|
|
// or SCEVCouldNotCompute. This will hopefully get most of the PHI nodes that
|
|
|
|
// are introduced due to conditional control flow, but not the ones that are
|
|
|
|
// referencing loop counters.
|
|
|
|
//
|
|
|
|
// XXX: In the future, we should remove the need for this pass entirely and
|
|
|
|
// instead add support for scalar dependences to ScopInfo and code generation.
|
2011-04-29 14:27:02 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2013-03-23 08:13:39 +08:00
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
#include "polly/LinkAllPasses.h"
|
2013-03-21 06:41:53 +08:00
|
|
|
#include "polly/CodeGen/BlockGenerators.h"
|
2015-05-09 17:13:42 +08:00
|
|
|
#include "polly/ScopDetection.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
#include "polly/Support/ScopHelper.h"
|
2014-07-20 02:40:17 +08:00
|
|
|
#include "llvm/Analysis/DominanceFrontier.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
|
|
|
#include "llvm/Analysis/RegionInfo.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolution.h"
|
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace polly;
|
|
|
|
|
|
|
|
namespace {
|
2014-04-02 00:01:33 +08:00
|
|
|
|
|
|
|
// Helper function which (for a given PHI node):
|
|
|
|
//
|
|
|
|
// 1) Remembers all incoming values and the associated basic blocks
|
|
|
|
// 2) Demotes the phi node to the stack
|
|
|
|
// 3) Remembers the correlation between the PHI node and the new alloca
|
|
|
|
//
|
|
|
|
// When we combine the information from 1) and 3) we know the values stored
|
|
|
|
// in this alloca at the end of the predecessor basic blocks of the PHI.
|
|
|
|
static void DemotePHI(
|
|
|
|
PHINode *PN, DenseMap<PHINode *, AllocaInst *> &PNallocMap,
|
|
|
|
DenseMap<std::pair<Value *, BasicBlock *>, PHINode *> &ValueLocToPhiMap) {
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
|
|
|
|
auto *InVal = PN->getIncomingValue(i);
|
|
|
|
auto *InBB = PN->getIncomingBlock(i);
|
|
|
|
ValueLocToPhiMap[std::make_pair(InVal, InBB)] = PN;
|
|
|
|
}
|
|
|
|
|
|
|
|
PNallocMap[PN] = DemotePHIToStack(PN);
|
|
|
|
}
|
|
|
|
|
2013-03-23 08:13:39 +08:00
|
|
|
/// @brief Prepare the IR for the scop detection.
|
2011-04-29 14:27:02 +08:00
|
|
|
///
|
2011-10-08 08:30:40 +08:00
|
|
|
class CodePreparation : public FunctionPass {
|
2015-02-16 07:40:18 +08:00
|
|
|
CodePreparation(const CodePreparation &) = delete;
|
2015-02-16 14:40:23 +08:00
|
|
|
const CodePreparation &operator=(const CodePreparation &) = delete;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
LoopInfo *LI;
|
2013-03-21 06:41:53 +08:00
|
|
|
ScalarEvolution *SE;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
bool eliminatePHINodes(Function &F);
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
|
2011-10-08 08:30:40 +08:00
|
|
|
explicit CodePreparation() : FunctionPass(ID) {}
|
|
|
|
~CodePreparation();
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
/// @name FunctionPass interface.
|
|
|
|
//@{
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const;
|
|
|
|
virtual void releaseMemory();
|
|
|
|
virtual bool runOnFunction(Function &F);
|
|
|
|
virtual void print(raw_ostream &OS, const Module *) const;
|
|
|
|
//@}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-03-23 08:13:39 +08:00
|
|
|
void CodePreparation::clear() {}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2013-03-23 08:13:39 +08:00
|
|
|
CodePreparation::~CodePreparation() { clear(); }
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2011-10-08 08:30:40 +08:00
|
|
|
bool CodePreparation::eliminatePHINodes(Function &F) {
|
2014-04-02 00:01:33 +08:00
|
|
|
// The PHINodes that will be demoted.
|
|
|
|
std::vector<PHINode *> PNtoDemote;
|
|
|
|
// The PHINodes that will be deleted (stack slot sharing).
|
2013-03-23 08:13:39 +08:00
|
|
|
std::vector<PHINode *> PNtoDelete;
|
2011-04-29 14:27:02 +08:00
|
|
|
// The PHINodes that will be preserved.
|
2014-04-02 00:01:33 +08:00
|
|
|
std::vector<PHINode *> PNtoPreserve;
|
|
|
|
// Map to remember values stored in PHINodes at the end of basic blocks.
|
|
|
|
DenseMap<std::pair<Value *, BasicBlock *>, PHINode *> ValueLocToPhiMap;
|
|
|
|
// Map from PHINodes to their alloca (after demotion) counterpart.
|
|
|
|
DenseMap<PHINode *, AllocaInst *> PNallocMap;
|
|
|
|
|
|
|
|
// Scan the PHINodes in this function and categorize them to be either:
|
|
|
|
// o Preserved, if they are (canonical) induction variables or can be
|
|
|
|
// synthesized during code generation ('SCEVable')
|
|
|
|
// o Deleted, if they are trivial PHI nodes (one incoming value) and the
|
|
|
|
// incoming value is a PHI node we will demote
|
|
|
|
// o Demoted, if they do not fit any of the previous categories
|
2013-03-23 08:13:39 +08:00
|
|
|
for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI)
|
|
|
|
for (BasicBlock::iterator II = BI->begin(), IE = BI->getFirstNonPHI();
|
|
|
|
II != IE; ++II) {
|
|
|
|
PHINode *PN = cast<PHINode>(II);
|
2014-11-30 22:33:31 +08:00
|
|
|
if (SE->isSCEVable(PN->getType())) {
|
|
|
|
const SCEV *S = SE->getSCEV(PN);
|
|
|
|
if (!isa<SCEVUnknown>(S) && !isa<SCEVCouldNotCompute>(S)) {
|
|
|
|
PNtoPreserve.push_back(PN);
|
|
|
|
continue;
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-23 08:13:39 +08:00
|
|
|
// As DemotePHIToStack does not support invoke edges, we preserve
|
|
|
|
// PHINodes that have invoke edges.
|
2014-04-02 00:01:33 +08:00
|
|
|
if (hasInvokeEdge(PN)) {
|
|
|
|
PNtoPreserve.push_back(PN);
|
|
|
|
} else {
|
|
|
|
if (PN->getNumIncomingValues() == 1)
|
|
|
|
PNtoDelete.push_back(PN);
|
|
|
|
else
|
|
|
|
PNtoDemote.push_back(PN);
|
|
|
|
}
|
2013-03-23 08:13:39 +08:00
|
|
|
}
|
|
|
|
|
2014-04-02 00:01:33 +08:00
|
|
|
if (PNtoDemote.empty() && PNtoDelete.empty())
|
2011-04-29 14:27:02 +08:00
|
|
|
return false;
|
|
|
|
|
2014-04-02 00:01:33 +08:00
|
|
|
while (!PNtoDemote.empty()) {
|
|
|
|
PHINode *PN = PNtoDemote.back();
|
|
|
|
PNtoDemote.pop_back();
|
|
|
|
DemotePHI(PN, PNallocMap, ValueLocToPhiMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
// For each trivial PHI we encountered (and we want to delete) we try to find
|
|
|
|
// the value it will hold in a alloca we already created by PHI demotion. If
|
|
|
|
// we succeed (the incoming value is stored in an alloca at the predecessor
|
|
|
|
// block), we can replace the trivial PHI by the value stored in the alloca.
|
|
|
|
// If not, we will demote this trivial PHI as any other one.
|
|
|
|
for (auto PNIt = PNtoDelete.rbegin(), PNEnd = PNtoDelete.rend();
|
|
|
|
PNIt != PNEnd; ++PNIt) {
|
|
|
|
PHINode *TrivPN = *PNIt;
|
|
|
|
assert(TrivPN->getNumIncomingValues() == 1 && "Assumed trivial PHI");
|
|
|
|
|
|
|
|
auto *InVal = TrivPN->getIncomingValue(0);
|
|
|
|
auto *InBB = TrivPN->getIncomingBlock(0);
|
|
|
|
const auto &ValLocIt = ValueLocToPhiMap.find(std::make_pair(InVal, InBB));
|
|
|
|
if (ValLocIt != ValueLocToPhiMap.end()) {
|
|
|
|
PHINode *InPHI = ValLocIt->second;
|
|
|
|
assert(PNallocMap.count(InPHI) &&
|
|
|
|
"Inconsitent state, PN was not demoted!");
|
|
|
|
auto *InPHIAlloca = PNallocMap[InPHI];
|
|
|
|
PNallocMap[TrivPN] = InPHIAlloca;
|
|
|
|
LoadInst *LI = new LoadInst(InPHIAlloca, "",
|
|
|
|
TrivPN->getParent()->getFirstInsertionPt());
|
|
|
|
TrivPN->replaceAllUsesWith(LI);
|
|
|
|
TrivPN->eraseFromParent();
|
|
|
|
continue;
|
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2014-04-02 00:01:33 +08:00
|
|
|
DemotePHI(TrivPN, PNallocMap, ValueLocToPhiMap);
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
2013-03-23 08:13:39 +08:00
|
|
|
// Move preserved PHINodes to the beginning of the BasicBlock.
|
2014-04-02 00:01:33 +08:00
|
|
|
while (!PNtoPreserve.empty()) {
|
|
|
|
PHINode *PN = PNtoPreserve.back();
|
|
|
|
PNtoPreserve.pop_back();
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
BasicBlock *BB = PN->getParent();
|
|
|
|
if (PN == BB->begin())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
PN->moveBefore(BB->begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-10-08 08:30:40 +08:00
|
|
|
void CodePreparation::getAnalysisUsage(AnalysisUsage &AU) const {
|
2015-01-17 22:16:56 +08:00
|
|
|
AU.addRequired<LoopInfoWrapperPass>();
|
2013-03-21 06:41:53 +08:00
|
|
|
AU.addRequired<ScalarEvolution>();
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2015-01-17 22:16:56 +08:00
|
|
|
AU.addPreserved<LoopInfoWrapperPass>();
|
2014-07-20 02:40:17 +08:00
|
|
|
AU.addPreserved<RegionInfoPass>();
|
2014-01-14 06:29:56 +08:00
|
|
|
AU.addPreserved<DominatorTreeWrapperPass>();
|
2011-04-29 14:27:02 +08:00
|
|
|
AU.addPreserved<DominanceFrontier>();
|
|
|
|
}
|
|
|
|
|
2011-10-08 08:30:40 +08:00
|
|
|
bool CodePreparation::runOnFunction(Function &F) {
|
2015-01-17 22:16:56 +08:00
|
|
|
LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
|
2013-03-21 06:41:53 +08:00
|
|
|
SE = &getAnalysis<ScalarEvolution>();
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
splitEntryBlockForAlloca(&F.getEntryBlock(), this);
|
|
|
|
|
2015-02-28 04:38:51 +08:00
|
|
|
if (!PollyModelPHINodes)
|
|
|
|
eliminatePHINodes(F);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-23 08:13:39 +08:00
|
|
|
void CodePreparation::releaseMemory() { clear(); }
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2013-03-23 08:13:39 +08:00
|
|
|
void CodePreparation::print(raw_ostream &OS, const Module *) const {}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2011-10-08 08:30:40 +08:00
|
|
|
char CodePreparation::ID = 0;
|
|
|
|
char &polly::CodePreparationID = CodePreparation::ID;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2013-03-23 08:13:39 +08:00
|
|
|
Pass *polly::createCodePreparationPass() { return new CodePreparation(); }
|
|
|
|
|
2011-10-08 08:30:40 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(CodePreparation, "polly-prepare",
|
2013-04-10 14:55:45 +08:00
|
|
|
"Polly - Prepare code for polly", false, false)
|
2015-01-17 22:16:56 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
|
2011-10-08 08:30:40 +08:00
|
|
|
INITIALIZE_PASS_END(CodePreparation, "polly-prepare",
|
2013-03-23 08:13:39 +08:00
|
|
|
"Polly - Prepare code for polly", false, false)
|