2015-05-12 07:45:52 +00:00
|
|
|
//===------ CodeGeneration.cpp - Code generate the Scops using ISL. ----======//
|
2012-05-07 16:20:07 +00:00
|
|
|
//
|
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
|
//
|
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
//
|
2015-05-12 07:45:52 +00:00
|
|
|
// The CodeGeneration pass takes a Scop created by ScopInfo and translates it
|
2012-05-07 16:20:07 +00:00
|
|
|
// back to LLVM-IR using the ISL code generator.
|
|
|
|
|
//
|
|
|
|
|
// The Scop describes the high level memory behaviour of a control flow region.
|
|
|
|
|
// Transformation passes can update the schedule (execution order) of statements
|
|
|
|
|
// in the Scop. ISL is used to generate an abstract syntax tree that reflects
|
|
|
|
|
// the updated execution order. This clast is used to create new LLVM-IR that is
|
|
|
|
|
// computationally equivalent to the original control flow region, but executes
|
2015-04-21 11:37:25 +00:00
|
|
|
// its code in the new execution order defined by the changed schedule.
|
2012-05-07 16:20:07 +00:00
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2015-04-27 12:17:22 +00:00
|
|
|
|
2013-05-07 08:11:54 +00:00
|
|
|
#include "polly/CodeGen/IslAst.h"
|
2015-12-21 12:38:56 +00:00
|
|
|
#include "polly/CodeGen/IslNodeBuilder.h"
|
2012-10-02 19:50:43 +00:00
|
|
|
#include "polly/CodeGen/Utils.h"
|
2015-03-04 22:43:40 +00:00
|
|
|
#include "polly/DependenceInfo.h"
|
2013-05-07 08:11:54 +00:00
|
|
|
#include "polly/LinkAllPasses.h"
|
2016-02-19 11:07:12 +00:00
|
|
|
#include "polly/Options.h"
|
2013-05-07 08:11:54 +00:00
|
|
|
#include "polly/ScopInfo.h"
|
2013-04-10 06:55:31 +00:00
|
|
|
#include "polly/Support/ScopHelper.h"
|
2015-09-09 22:13:56 +00:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
|
|
|
|
#include "llvm/Analysis/BasicAliasAnalysis.h"
|
|
|
|
|
#include "llvm/Analysis/GlobalsModRef.h"
|
2015-08-14 20:10:27 +00:00
|
|
|
#include "llvm/Analysis/PostDominators.h"
|
2015-09-09 22:13:56 +00:00
|
|
|
#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
|
2015-09-25 09:49:19 +00:00
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
|
#include "llvm/IR/Verifier.h"
|
|
|
|
|
#include "llvm/Support/Debug.h"
|
2012-10-02 19:50:43 +00:00
|
|
|
|
|
|
|
|
using namespace polly;
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
2015-05-12 07:45:52 +00:00
|
|
|
#define DEBUG_TYPE "polly-codegen"
|
2014-04-22 03:30:19 +00:00
|
|
|
|
2016-02-19 11:07:12 +00:00
|
|
|
static cl::opt<bool> Verify("polly-codegen-verify",
|
|
|
|
|
cl::desc("Verify the function generated by Polly"),
|
|
|
|
|
cl::Hidden, cl::init(true), cl::ZeroOrMore,
|
|
|
|
|
cl::cat(PollyCategory));
|
|
|
|
|
|
2012-10-02 19:50:43 +00:00
|
|
|
namespace {
|
2015-05-12 07:45:52 +00:00
|
|
|
class CodeGeneration : public ScopPass {
|
2012-12-29 23:47:38 +00:00
|
|
|
public:
|
2012-10-02 19:50:43 +00:00
|
|
|
static char ID;
|
|
|
|
|
|
2015-05-12 07:45:52 +00:00
|
|
|
CodeGeneration() : ScopPass(ID) {}
|
2012-10-02 19:50:43 +00:00
|
|
|
|
2014-11-15 21:32:53 +00:00
|
|
|
/// @brief The datalayout used
|
|
|
|
|
const DataLayout *DL;
|
|
|
|
|
|
2014-09-10 14:50:23 +00:00
|
|
|
/// @name The analysis passes we need to generate code.
|
|
|
|
|
///
|
|
|
|
|
///{
|
|
|
|
|
LoopInfo *LI;
|
|
|
|
|
IslAstInfo *AI;
|
|
|
|
|
DominatorTree *DT;
|
|
|
|
|
ScalarEvolution *SE;
|
2015-08-11 14:39:21 +00:00
|
|
|
RegionInfo *RI;
|
2014-09-10 14:50:23 +00:00
|
|
|
///}
|
|
|
|
|
|
|
|
|
|
/// @brief Build the runtime condition.
|
|
|
|
|
///
|
|
|
|
|
/// Build the condition that evaluates at run-time to true iff all
|
|
|
|
|
/// assumptions taken for the SCoP hold, and to false otherwise.
|
|
|
|
|
///
|
|
|
|
|
/// @return A value evaluating to true/false if execution is save/unsafe.
|
|
|
|
|
Value *buildRTC(PollyIRBuilder &Builder, IslExprBuilder &ExprBuilder) {
|
|
|
|
|
Builder.SetInsertPoint(Builder.GetInsertBlock()->getTerminator());
|
|
|
|
|
Value *RTC = ExprBuilder.create(AI->getRunCondition());
|
2014-09-18 11:17:17 +00:00
|
|
|
if (!RTC->getType()->isIntegerTy(1))
|
|
|
|
|
RTC = Builder.CreateIsNotNull(RTC);
|
|
|
|
|
return RTC;
|
2014-09-10 14:50:23 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-19 11:07:12 +00:00
|
|
|
void verifyGeneratedFunction(Scop &S, Function &F) {
|
|
|
|
|
if (!verifyFunction(F, &errs()) || !Verify)
|
|
|
|
|
return;
|
2015-02-27 17:37:05 +00:00
|
|
|
|
|
|
|
|
DEBUG({
|
|
|
|
|
errs() << "== ISL Codegen created an invalid function ==\n\n== The "
|
|
|
|
|
"SCoP ==\n";
|
|
|
|
|
S.print(errs());
|
|
|
|
|
errs() << "\n== The isl AST ==\n";
|
2015-03-01 18:40:25 +00:00
|
|
|
AI->printScop(errs(), S);
|
2015-02-27 17:37:05 +00:00
|
|
|
errs() << "\n== The invalid function ==\n";
|
|
|
|
|
F.print(errs());
|
|
|
|
|
});
|
|
|
|
|
|
2016-02-19 11:07:12 +00:00
|
|
|
llvm_unreachable("Polly generated function could not be verified. Add "
|
|
|
|
|
"-polly-codegen-verify=false to disable this assertion.");
|
2015-02-27 17:37:05 +00:00
|
|
|
}
|
|
|
|
|
|
2015-08-11 14:47:37 +00:00
|
|
|
// CodeGeneration adds a lot of BBs without updating the RegionInfo
|
|
|
|
|
// We make all created BBs belong to the scop's parent region without any
|
|
|
|
|
// nested structure to keep the RegionInfo verifier happy.
|
|
|
|
|
void fixRegionInfo(Function *F, Region *ParentRegion) {
|
|
|
|
|
for (BasicBlock &BB : *F) {
|
|
|
|
|
if (RI->getRegionFor(&BB))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
RI->setRegionFor(&BB, ParentRegion);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-23 06:57:51 +00:00
|
|
|
/// @brief Mark a basic block unreachable.
|
|
|
|
|
///
|
|
|
|
|
/// Marks the basic block @p Block unreachable by equipping it with an
|
|
|
|
|
/// UnreachableInst.
|
|
|
|
|
void markBlockUnreachable(BasicBlock &Block, PollyIRBuilder &Builder) {
|
|
|
|
|
auto *OrigTerminator = Block.getTerminator();
|
|
|
|
|
Builder.SetInsertPoint(OrigTerminator);
|
|
|
|
|
Builder.CreateUnreachable();
|
|
|
|
|
OrigTerminator->eraseFromParent();
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-27 15:43:29 +00:00
|
|
|
/// @brief Generate LLVM-IR for the SCoP @p S.
|
2015-03-01 18:42:08 +00:00
|
|
|
bool runOnScop(Scop &S) override {
|
2014-09-10 14:50:23 +00:00
|
|
|
AI = &getAnalysis<IslAstInfo>();
|
2015-02-11 17:25:09 +00:00
|
|
|
|
|
|
|
|
// Check if we created an isl_ast root node, otherwise exit.
|
|
|
|
|
isl_ast_node *AstRoot = AI->getAst();
|
|
|
|
|
if (!AstRoot)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
|
2014-09-10 14:50:23 +00:00
|
|
|
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
2015-08-17 10:57:08 +00:00
|
|
|
SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
|
2016-05-23 12:38:05 +00:00
|
|
|
DL = &S.getFunction().getParent()->getDataLayout();
|
2015-08-11 14:39:21 +00:00
|
|
|
RI = &getAnalysis<RegionInfoPass>().getRegionInfo();
|
|
|
|
|
Region *R = &S.getRegion();
|
|
|
|
|
assert(!R->isTopLevelRegion() && "Top level regions are not supported");
|
2013-04-10 06:55:31 +00:00
|
|
|
|
2015-10-04 11:19:13 +00:00
|
|
|
ScopAnnotator Annotator;
|
2015-04-27 10:43:10 +00:00
|
|
|
Annotator.buildAliasScopes(S);
|
2014-10-02 15:31:24 +00:00
|
|
|
|
2015-08-11 14:39:21 +00:00
|
|
|
simplifyRegion(R, DT, LI, RI);
|
|
|
|
|
assert(R->isSimple());
|
2016-05-23 12:42:38 +00:00
|
|
|
BasicBlock *EnteringBB = S.getEnteringBlock();
|
2015-08-11 14:39:21 +00:00
|
|
|
assert(EnteringBB);
|
2014-09-10 14:50:23 +00:00
|
|
|
PollyIRBuilder Builder = createPollyIRBuilder(EnteringBB, Annotator);
|
2013-11-17 03:18:25 +00:00
|
|
|
|
2014-11-15 21:32:53 +00:00
|
|
|
IslNodeBuilder NodeBuilder(Builder, Annotator, this, *DL, *LI, *SE, *DT, S);
|
2016-05-12 15:12:43 +00:00
|
|
|
IslExprBuilder &ExprBuilder = NodeBuilder.getExprBuilder();
|
2014-09-10 14:50:23 +00:00
|
|
|
|
2015-03-28 09:34:40 +00:00
|
|
|
// Only build the run-time condition and parameters _after_ having
|
|
|
|
|
// introduced the conditional branch. This is important as the conditional
|
|
|
|
|
// branch will guard the original scop from new induction variables that
|
|
|
|
|
// the SCEVExpander may introduce while code generating the parameters and
|
|
|
|
|
// which may introduce scalar dependences that prevent us from correctly
|
|
|
|
|
// code generating this scop.
|
|
|
|
|
BasicBlock *StartBlock =
|
|
|
|
|
executeScopConditionally(S, this, Builder.getTrue());
|
2016-03-23 06:57:51 +00:00
|
|
|
auto *SplitBlock = StartBlock->getSinglePredecessor();
|
2015-10-07 20:17:36 +00:00
|
|
|
|
|
|
|
|
// First generate code for the hoisted invariant loads and transitively the
|
|
|
|
|
// parameters they reference. Afterwards, for the remaining parameters that
|
|
|
|
|
// might reference the hoisted loads. Finally, build the runtime check
|
|
|
|
|
// that might reference both hoisted loads as well as parameters.
|
2015-11-07 19:46:04 +00:00
|
|
|
// If the hoisting fails we have to bail and execute the original code.
|
2015-03-28 09:34:40 +00:00
|
|
|
Builder.SetInsertPoint(SplitBlock->getTerminator());
|
2015-11-07 19:46:04 +00:00
|
|
|
if (!NodeBuilder.preloadInvariantLoads()) {
|
2015-11-08 16:34:17 +00:00
|
|
|
|
2016-03-23 06:57:51 +00:00
|
|
|
// Patch the introduced branch condition to ensure that we always execute
|
|
|
|
|
// the original SCoP.
|
2015-11-07 19:46:04 +00:00
|
|
|
auto *FalseI1 = Builder.getFalse();
|
2015-11-08 17:57:41 +00:00
|
|
|
auto *SplitBBTerm = Builder.GetInsertBlock()->getTerminator();
|
|
|
|
|
SplitBBTerm->setOperand(0, FalseI1);
|
2015-11-07 19:46:04 +00:00
|
|
|
|
2016-03-23 06:57:51 +00:00
|
|
|
// Since the other branch is hence ignored we mark it as unreachable and
|
|
|
|
|
// adjust the dominator tree accordingly.
|
|
|
|
|
auto *ExitingBlock = StartBlock->getUniqueSuccessor();
|
|
|
|
|
assert(ExitingBlock);
|
|
|
|
|
auto *MergeBlock = ExitingBlock->getUniqueSuccessor();
|
|
|
|
|
assert(MergeBlock);
|
|
|
|
|
markBlockUnreachable(*StartBlock, Builder);
|
|
|
|
|
markBlockUnreachable(*ExitingBlock, Builder);
|
2016-05-23 12:42:38 +00:00
|
|
|
auto *ExitingBB = S.getExitingBlock();
|
2016-03-23 06:57:51 +00:00
|
|
|
assert(ExitingBB);
|
|
|
|
|
DT->changeImmediateDominator(MergeBlock, ExitingBB);
|
|
|
|
|
DT->eraseNode(ExitingBlock);
|
|
|
|
|
|
|
|
|
|
isl_ast_node_free(AstRoot);
|
2015-11-08 16:34:17 +00:00
|
|
|
} else {
|
2015-10-07 20:17:36 +00:00
|
|
|
|
2015-11-08 16:34:17 +00:00
|
|
|
NodeBuilder.addParameters(S.getContext());
|
2014-08-12 18:35:54 +00:00
|
|
|
|
2016-05-12 15:12:43 +00:00
|
|
|
ExprBuilder.setTrackOverflow(true);
|
|
|
|
|
Value *RTC = buildRTC(Builder, ExprBuilder);
|
|
|
|
|
Value *OverflowHappened = Builder.CreateNot(
|
|
|
|
|
ExprBuilder.getOverflowState(), "polly.rtc.overflown");
|
|
|
|
|
RTC = Builder.CreateAnd(RTC, OverflowHappened, "polly.rtc.result");
|
|
|
|
|
ExprBuilder.setTrackOverflow(false);
|
|
|
|
|
|
2016-06-11 19:17:15 +00:00
|
|
|
Builder.GetInsertBlock()->getTerminator()->setOperand(0, RTC);
|
|
|
|
|
Builder.SetInsertPoint(&StartBlock->front());
|
|
|
|
|
|
|
|
|
|
NodeBuilder.create(AstRoot);
|
2015-11-08 16:34:17 +00:00
|
|
|
|
|
|
|
|
NodeBuilder.finalizeSCoP(S);
|
|
|
|
|
fixRegionInfo(EnteringBB->getParent(), R->getParent());
|
|
|
|
|
}
|
2015-05-22 23:43:58 +00:00
|
|
|
|
2016-06-06 12:13:24 +00:00
|
|
|
Function *F = EnteringBB->getParent();
|
|
|
|
|
verifyGeneratedFunction(S, *F);
|
2016-04-08 18:16:02 +00:00
|
|
|
for (auto *SubF : NodeBuilder.getParallelSubfunctions())
|
|
|
|
|
verifyGeneratedFunction(S, *SubF);
|
2016-02-14 20:56:49 +00:00
|
|
|
|
2015-11-26 12:36:25 +00:00
|
|
|
// Mark the function such that we run additional cleanup passes on this
|
|
|
|
|
// function (e.g. mem2reg to rediscover phi nodes).
|
|
|
|
|
F->addFnAttr("polly-optimized");
|
|
|
|
|
|
2012-10-02 19:50:43 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-27 15:43:29 +00:00
|
|
|
/// @brief Register all analyses and transformation required.
|
2015-03-01 18:42:08 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2014-01-13 22:29:56 +00:00
|
|
|
AU.addRequired<DominatorTreeWrapperPass>();
|
2012-10-02 19:50:43 +00:00
|
|
|
AU.addRequired<IslAstInfo>();
|
2014-07-19 18:40:17 +00:00
|
|
|
AU.addRequired<RegionInfoPass>();
|
2015-08-17 10:57:08 +00:00
|
|
|
AU.addRequired<ScalarEvolutionWrapperPass>();
|
2012-10-02 19:50:43 +00:00
|
|
|
AU.addRequired<ScopDetection>();
|
2016-05-31 09:41:04 +00:00
|
|
|
AU.addRequired<ScopInfoRegionPass>();
|
2015-01-17 14:16:56 +00:00
|
|
|
AU.addRequired<LoopInfoWrapperPass>();
|
2012-10-02 19:50:43 +00:00
|
|
|
|
2015-03-04 22:43:40 +00:00
|
|
|
AU.addPreserved<DependenceInfo>();
|
2012-10-02 19:50:43 +00:00
|
|
|
|
2015-09-09 22:13:56 +00:00
|
|
|
AU.addPreserved<AAResultsWrapperPass>();
|
|
|
|
|
AU.addPreserved<BasicAAWrapperPass>();
|
2015-01-17 14:16:56 +00:00
|
|
|
AU.addPreserved<LoopInfoWrapperPass>();
|
2014-01-13 22:29:56 +00:00
|
|
|
AU.addPreserved<DominatorTreeWrapperPass>();
|
2015-09-09 22:13:56 +00:00
|
|
|
AU.addPreserved<GlobalsAAWrapperPass>();
|
2016-02-25 17:54:42 +00:00
|
|
|
AU.addPreserved<PostDominatorTreeWrapperPass>();
|
2012-10-02 19:50:43 +00:00
|
|
|
AU.addPreserved<IslAstInfo>();
|
|
|
|
|
AU.addPreserved<ScopDetection>();
|
2015-08-17 10:57:08 +00:00
|
|
|
AU.addPreserved<ScalarEvolutionWrapperPass>();
|
2015-09-09 22:13:56 +00:00
|
|
|
AU.addPreserved<SCEVAAWrapperPass>();
|
2012-10-02 19:50:43 +00:00
|
|
|
|
|
|
|
|
// FIXME: We do not yet add regions for the newly generated code to the
|
|
|
|
|
// region tree.
|
2014-07-19 18:40:17 +00:00
|
|
|
AU.addPreserved<RegionInfoPass>();
|
2016-05-31 09:41:04 +00:00
|
|
|
AU.addPreserved<ScopInfoRegionPass>();
|
2012-10-02 19:50:43 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-12 07:45:52 +00:00
|
|
|
char CodeGeneration::ID = 1;
|
2012-10-02 19:50:43 +00:00
|
|
|
|
2015-05-12 07:45:52 +00:00
|
|
|
Pass *polly::createCodeGenerationPass() { return new CodeGeneration(); }
|
2013-02-22 08:07:06 +00:00
|
|
|
|
2015-05-12 07:45:52 +00:00
|
|
|
INITIALIZE_PASS_BEGIN(CodeGeneration, "polly-codegen",
|
2013-02-22 08:07:06 +00:00
|
|
|
"Polly - Create LLVM-IR from SCoPs", false, false);
|
2015-03-04 22:43:40 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
|
2014-01-13 22:29:56 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
|
2015-01-17 14:16:56 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
|
2014-07-19 18:40:17 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
|
2015-08-17 10:57:08 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass);
|
2013-02-22 08:07:06 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(ScopDetection);
|
2015-05-12 07:45:52 +00:00
|
|
|
INITIALIZE_PASS_END(CodeGeneration, "polly-codegen",
|
2013-02-22 08:07:06 +00:00
|
|
|
"Polly - Create LLVM-IR from SCoPs", false, false)
|