109d30697STobias Grosser //===------ CodeGeneration.cpp - Code generate the Scops using ISL. ----======//
209d30697STobias Grosser //
309d30697STobias Grosser //                     The LLVM Compiler Infrastructure
409d30697STobias Grosser //
509d30697STobias Grosser // This file is distributed under the University of Illinois Open Source
609d30697STobias Grosser // License. See LICENSE.TXT for details.
709d30697STobias Grosser //
809d30697STobias Grosser //===----------------------------------------------------------------------===//
909d30697STobias Grosser //
1009d30697STobias Grosser // The CodeGeneration pass takes a Scop created by ScopInfo and translates it
1109d30697STobias Grosser // back to LLVM-IR using the ISL code generator.
1209d30697STobias Grosser //
1309d30697STobias Grosser // The Scop describes the high level memory behaviour of a control flow region.
1409d30697STobias Grosser // Transformation passes can update the schedule (execution order) of statements
1509d30697STobias Grosser // in the Scop. ISL is used to generate an abstract syntax tree that reflects
1609d30697STobias Grosser // the updated execution order. This clast is used to create new LLVM-IR that is
1709d30697STobias Grosser // computationally equivalent to the original control flow region, but executes
1809d30697STobias Grosser // its code in the new execution order defined by the changed schedule.
1909d30697STobias Grosser //
2009d30697STobias Grosser //===----------------------------------------------------------------------===//
2109d30697STobias Grosser 
2209d30697STobias Grosser #include "polly/CodeGen/IslNodeBuilder.h"
2309d30697STobias Grosser #include "polly/CodeGen/IslAst.h"
2409d30697STobias Grosser #include "polly/CodeGen/Utils.h"
2509d30697STobias Grosser #include "polly/DependenceInfo.h"
2609d30697STobias Grosser #include "polly/LinkAllPasses.h"
2709d30697STobias Grosser #include "polly/ScopInfo.h"
2809d30697STobias Grosser #include "polly/Support/ScopHelper.h"
2909d30697STobias Grosser #include "polly/TempScopInfo.h"
3009d30697STobias Grosser #include "llvm/IR/Module.h"
3109d30697STobias Grosser #include "llvm/IR/Verifier.h"
3209d30697STobias Grosser #include "llvm/Support/Debug.h"
3309d30697STobias Grosser 
3409d30697STobias Grosser using namespace polly;
3509d30697STobias Grosser using namespace llvm;
3609d30697STobias Grosser 
3709d30697STobias Grosser #define DEBUG_TYPE "polly-codegen"
3809d30697STobias Grosser 
3909d30697STobias Grosser namespace {
4009d30697STobias Grosser class CodeGeneration : public ScopPass {
4109d30697STobias Grosser public:
4209d30697STobias Grosser   static char ID;
4309d30697STobias Grosser 
4409d30697STobias Grosser   CodeGeneration() : ScopPass(ID) {}
4509d30697STobias Grosser 
4609d30697STobias Grosser   /// @brief The datalayout used
4709d30697STobias Grosser   const DataLayout *DL;
4809d30697STobias Grosser 
4909d30697STobias Grosser   /// @name The analysis passes we need to generate code.
5009d30697STobias Grosser   ///
5109d30697STobias Grosser   ///{
5209d30697STobias Grosser   LoopInfo *LI;
5309d30697STobias Grosser   IslAstInfo *AI;
5409d30697STobias Grosser   DominatorTree *DT;
5509d30697STobias Grosser   ScalarEvolution *SE;
5609d30697STobias Grosser   ///}
5709d30697STobias Grosser 
5809d30697STobias Grosser   /// @brief The loop annotator to generate llvm.loop metadata.
5909d30697STobias Grosser   ScopAnnotator Annotator;
6009d30697STobias Grosser 
6109d30697STobias Grosser   /// @brief Build the runtime condition.
6209d30697STobias Grosser   ///
6309d30697STobias Grosser   /// Build the condition that evaluates at run-time to true iff all
6409d30697STobias Grosser   /// assumptions taken for the SCoP hold, and to false otherwise.
6509d30697STobias Grosser   ///
6609d30697STobias Grosser   /// @return A value evaluating to true/false if execution is save/unsafe.
6709d30697STobias Grosser   Value *buildRTC(PollyIRBuilder &Builder, IslExprBuilder &ExprBuilder) {
6809d30697STobias Grosser     Builder.SetInsertPoint(Builder.GetInsertBlock()->getTerminator());
6909d30697STobias Grosser     Value *RTC = ExprBuilder.create(AI->getRunCondition());
7009d30697STobias Grosser     if (!RTC->getType()->isIntegerTy(1))
7109d30697STobias Grosser       RTC = Builder.CreateIsNotNull(RTC);
7209d30697STobias Grosser     return RTC;
7309d30697STobias Grosser   }
7409d30697STobias Grosser 
7509d30697STobias Grosser   bool verifyGeneratedFunction(Scop &S, Function &F) {
7609d30697STobias Grosser     if (!verifyFunction(F))
7709d30697STobias Grosser       return false;
7809d30697STobias Grosser 
7909d30697STobias Grosser     DEBUG({
8009d30697STobias Grosser       errs() << "== ISL Codegen created an invalid function ==\n\n== The "
8109d30697STobias Grosser                 "SCoP ==\n";
8209d30697STobias Grosser       S.print(errs());
8309d30697STobias Grosser       errs() << "\n== The isl AST ==\n";
8409d30697STobias Grosser       AI->printScop(errs(), S);
8509d30697STobias Grosser       errs() << "\n== The invalid function ==\n";
8609d30697STobias Grosser       F.print(errs());
8709d30697STobias Grosser       errs() << "\n== The errors ==\n";
8809d30697STobias Grosser       verifyFunction(F, &errs());
8909d30697STobias Grosser     });
9009d30697STobias Grosser 
9109d30697STobias Grosser     return true;
9209d30697STobias Grosser   }
9309d30697STobias Grosser 
9409d30697STobias Grosser   bool runOnScop(Scop &S) override {
9509d30697STobias Grosser     AI = &getAnalysis<IslAstInfo>();
9609d30697STobias Grosser 
9709d30697STobias Grosser     // Check if we created an isl_ast root node, otherwise exit.
9809d30697STobias Grosser     isl_ast_node *AstRoot = AI->getAst();
9909d30697STobias Grosser     if (!AstRoot)
10009d30697STobias Grosser       return false;
10109d30697STobias Grosser 
10209d30697STobias Grosser     LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
10309d30697STobias Grosser     DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
10409d30697STobias Grosser     SE = &getAnalysis<ScalarEvolution>();
10509d30697STobias Grosser     DL = &S.getRegion().getEntry()->getParent()->getParent()->getDataLayout();
10609d30697STobias Grosser 
10709d30697STobias Grosser     assert(!S.getRegion().isTopLevelRegion() &&
10809d30697STobias Grosser            "Top level regions are not supported");
10909d30697STobias Grosser 
11009d30697STobias Grosser     Annotator.buildAliasScopes(S);
11109d30697STobias Grosser 
11209d30697STobias Grosser     BasicBlock *EnteringBB = simplifyRegion(&S, this);
11309d30697STobias Grosser     PollyIRBuilder Builder = createPollyIRBuilder(EnteringBB, Annotator);
11409d30697STobias Grosser 
11509d30697STobias Grosser     IslNodeBuilder NodeBuilder(Builder, Annotator, this, *DL, *LI, *SE, *DT, S);
11609d30697STobias Grosser 
11709d30697STobias Grosser     // Only build the run-time condition and parameters _after_ having
11809d30697STobias Grosser     // introduced the conditional branch. This is important as the conditional
11909d30697STobias Grosser     // branch will guard the original scop from new induction variables that
12009d30697STobias Grosser     // the SCEVExpander may introduce while code generating the parameters and
12109d30697STobias Grosser     // which may introduce scalar dependences that prevent us from correctly
12209d30697STobias Grosser     // code generating this scop.
12309d30697STobias Grosser     BasicBlock *StartBlock =
12409d30697STobias Grosser         executeScopConditionally(S, this, Builder.getTrue());
12509d30697STobias Grosser     auto SplitBlock = StartBlock->getSinglePredecessor();
12609d30697STobias Grosser     Builder.SetInsertPoint(SplitBlock->getTerminator());
12709d30697STobias Grosser     NodeBuilder.addParameters(S.getContext());
12809d30697STobias Grosser     Value *RTC = buildRTC(Builder, NodeBuilder.getExprBuilder());
12909d30697STobias Grosser     SplitBlock->getTerminator()->setOperand(0, RTC);
13009d30697STobias Grosser     Builder.SetInsertPoint(StartBlock->begin());
13109d30697STobias Grosser 
13209d30697STobias Grosser     NodeBuilder.create(AstRoot);
13309d30697STobias Grosser 
134*ecff11dcSJohannes Doerfert     NodeBuilder.finalizeSCoP(S);
135*ecff11dcSJohannes Doerfert 
13609d30697STobias Grosser     assert(!verifyGeneratedFunction(S, *EnteringBB->getParent()) &&
13709d30697STobias Grosser            "Verification of generated function failed");
13809d30697STobias Grosser     return true;
13909d30697STobias Grosser   }
14009d30697STobias Grosser 
14109d30697STobias Grosser   void printScop(raw_ostream &, Scop &) const override {}
14209d30697STobias Grosser 
14309d30697STobias Grosser   void getAnalysisUsage(AnalysisUsage &AU) const override {
14409d30697STobias Grosser     AU.addRequired<DominatorTreeWrapperPass>();
14509d30697STobias Grosser     AU.addRequired<IslAstInfo>();
14609d30697STobias Grosser     AU.addRequired<RegionInfoPass>();
14709d30697STobias Grosser     AU.addRequired<ScalarEvolution>();
14809d30697STobias Grosser     AU.addRequired<ScopDetection>();
14909d30697STobias Grosser     AU.addRequired<ScopInfo>();
15009d30697STobias Grosser     AU.addRequired<LoopInfoWrapperPass>();
15109d30697STobias Grosser 
15209d30697STobias Grosser     AU.addPreserved<DependenceInfo>();
15309d30697STobias Grosser 
15409d30697STobias Grosser     AU.addPreserved<LoopInfoWrapperPass>();
15509d30697STobias Grosser     AU.addPreserved<DominatorTreeWrapperPass>();
15609d30697STobias Grosser     AU.addPreserved<IslAstInfo>();
15709d30697STobias Grosser     AU.addPreserved<ScopDetection>();
15809d30697STobias Grosser     AU.addPreserved<ScalarEvolution>();
15909d30697STobias Grosser 
16009d30697STobias Grosser     // FIXME: We do not yet add regions for the newly generated code to the
16109d30697STobias Grosser     //        region tree.
16209d30697STobias Grosser     AU.addPreserved<RegionInfoPass>();
16309d30697STobias Grosser     AU.addPreserved<TempScopInfo>();
16409d30697STobias Grosser     AU.addPreserved<ScopInfo>();
16509d30697STobias Grosser     AU.addPreservedID(IndependentBlocksID);
16609d30697STobias Grosser   }
16709d30697STobias Grosser };
16809d30697STobias Grosser }
16909d30697STobias Grosser 
17009d30697STobias Grosser char CodeGeneration::ID = 1;
17109d30697STobias Grosser 
17209d30697STobias Grosser Pass *polly::createCodeGenerationPass() { return new CodeGeneration(); }
17309d30697STobias Grosser 
17409d30697STobias Grosser INITIALIZE_PASS_BEGIN(CodeGeneration, "polly-codegen",
17509d30697STobias Grosser                       "Polly - Create LLVM-IR from SCoPs", false, false);
17609d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
17709d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
17809d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
17909d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
18009d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(ScalarEvolution);
18109d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(ScopDetection);
18209d30697STobias Grosser INITIALIZE_PASS_END(CodeGeneration, "polly-codegen",
18309d30697STobias Grosser                     "Polly - Create LLVM-IR from SCoPs", false, false)
184