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"
2966ef16b2SChandler Carruth #include "llvm/Analysis/AliasAnalysis.h"
3066ef16b2SChandler Carruth #include "llvm/Analysis/BasicAliasAnalysis.h"
3166ef16b2SChandler Carruth #include "llvm/Analysis/GlobalsModRef.h"
3282a1c7deSMichael Kruse #include "llvm/Analysis/PostDominators.h"
3366ef16b2SChandler Carruth #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
34*c2bb0cbeSTobias Grosser #include "llvm/IR/Module.h"
35*c2bb0cbeSTobias Grosser #include "llvm/IR/Verifier.h"
36*c2bb0cbeSTobias Grosser #include "llvm/Support/Debug.h"
3709d30697STobias Grosser 
3809d30697STobias Grosser using namespace polly;
3909d30697STobias Grosser using namespace llvm;
4009d30697STobias Grosser 
4109d30697STobias Grosser #define DEBUG_TYPE "polly-codegen"
4209d30697STobias Grosser 
4309d30697STobias Grosser namespace {
4409d30697STobias Grosser class CodeGeneration : public ScopPass {
4509d30697STobias Grosser public:
4609d30697STobias Grosser   static char ID;
4709d30697STobias Grosser 
4809d30697STobias Grosser   CodeGeneration() : ScopPass(ID) {}
4909d30697STobias Grosser 
5009d30697STobias Grosser   /// @brief The datalayout used
5109d30697STobias Grosser   const DataLayout *DL;
5209d30697STobias Grosser 
5309d30697STobias Grosser   /// @name The analysis passes we need to generate code.
5409d30697STobias Grosser   ///
5509d30697STobias Grosser   ///{
5609d30697STobias Grosser   LoopInfo *LI;
5709d30697STobias Grosser   IslAstInfo *AI;
5809d30697STobias Grosser   DominatorTree *DT;
5909d30697STobias Grosser   ScalarEvolution *SE;
6022370884SMichael Kruse   RegionInfo *RI;
6109d30697STobias Grosser   ///}
6209d30697STobias Grosser 
6309d30697STobias Grosser   /// @brief The loop annotator to generate llvm.loop metadata.
6409d30697STobias Grosser   ScopAnnotator Annotator;
6509d30697STobias Grosser 
6609d30697STobias Grosser   /// @brief Build the runtime condition.
6709d30697STobias Grosser   ///
6809d30697STobias Grosser   /// Build the condition that evaluates at run-time to true iff all
6909d30697STobias Grosser   /// assumptions taken for the SCoP hold, and to false otherwise.
7009d30697STobias Grosser   ///
7109d30697STobias Grosser   /// @return A value evaluating to true/false if execution is save/unsafe.
7209d30697STobias Grosser   Value *buildRTC(PollyIRBuilder &Builder, IslExprBuilder &ExprBuilder) {
7309d30697STobias Grosser     Builder.SetInsertPoint(Builder.GetInsertBlock()->getTerminator());
7409d30697STobias Grosser     Value *RTC = ExprBuilder.create(AI->getRunCondition());
7509d30697STobias Grosser     if (!RTC->getType()->isIntegerTy(1))
7609d30697STobias Grosser       RTC = Builder.CreateIsNotNull(RTC);
7709d30697STobias Grosser     return RTC;
7809d30697STobias Grosser   }
7909d30697STobias Grosser 
8009d30697STobias Grosser   bool verifyGeneratedFunction(Scop &S, Function &F) {
8109d30697STobias Grosser     if (!verifyFunction(F))
8209d30697STobias Grosser       return false;
8309d30697STobias Grosser 
8409d30697STobias Grosser     DEBUG({
8509d30697STobias Grosser       errs() << "== ISL Codegen created an invalid function ==\n\n== The "
8609d30697STobias Grosser                 "SCoP ==\n";
8709d30697STobias Grosser       S.print(errs());
8809d30697STobias Grosser       errs() << "\n== The isl AST ==\n";
8909d30697STobias Grosser       AI->printScop(errs(), S);
9009d30697STobias Grosser       errs() << "\n== The invalid function ==\n";
9109d30697STobias Grosser       F.print(errs());
9209d30697STobias Grosser       errs() << "\n== The errors ==\n";
9309d30697STobias Grosser       verifyFunction(F, &errs());
9409d30697STobias Grosser     });
9509d30697STobias Grosser 
9609d30697STobias Grosser     return true;
9709d30697STobias Grosser   }
9809d30697STobias Grosser 
999c483c58SMichael Kruse   // CodeGeneration adds a lot of BBs without updating the RegionInfo
1009c483c58SMichael Kruse   // We make all created BBs belong to the scop's parent region without any
1019c483c58SMichael Kruse   // nested structure to keep the RegionInfo verifier happy.
1029c483c58SMichael Kruse   void fixRegionInfo(Function *F, Region *ParentRegion) {
1039c483c58SMichael Kruse     for (BasicBlock &BB : *F) {
1049c483c58SMichael Kruse       if (RI->getRegionFor(&BB))
1059c483c58SMichael Kruse         continue;
1069c483c58SMichael Kruse 
1079c483c58SMichael Kruse       RI->setRegionFor(&BB, ParentRegion);
1089c483c58SMichael Kruse     }
1099c483c58SMichael Kruse   }
1109c483c58SMichael Kruse 
11109d30697STobias Grosser   bool runOnScop(Scop &S) override {
11209d30697STobias Grosser     AI = &getAnalysis<IslAstInfo>();
11309d30697STobias Grosser 
11409d30697STobias Grosser     // Check if we created an isl_ast root node, otherwise exit.
11509d30697STobias Grosser     isl_ast_node *AstRoot = AI->getAst();
11609d30697STobias Grosser     if (!AstRoot)
11709d30697STobias Grosser       return false;
11809d30697STobias Grosser 
11909d30697STobias Grosser     LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
12009d30697STobias Grosser     DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
121c5bcf246STobias Grosser     SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
12209d30697STobias Grosser     DL = &S.getRegion().getEntry()->getParent()->getParent()->getDataLayout();
12322370884SMichael Kruse     RI = &getAnalysis<RegionInfoPass>().getRegionInfo();
12422370884SMichael Kruse     Region *R = &S.getRegion();
12522370884SMichael Kruse     assert(!R->isTopLevelRegion() && "Top level regions are not supported");
12609d30697STobias Grosser 
12709d30697STobias Grosser     Annotator.buildAliasScopes(S);
12809d30697STobias Grosser 
12922370884SMichael Kruse     simplifyRegion(R, DT, LI, RI);
13022370884SMichael Kruse     assert(R->isSimple());
13122370884SMichael Kruse     BasicBlock *EnteringBB = S.getRegion().getEnteringBlock();
13222370884SMichael Kruse     assert(EnteringBB);
13309d30697STobias Grosser     PollyIRBuilder Builder = createPollyIRBuilder(EnteringBB, Annotator);
13409d30697STobias Grosser 
13509d30697STobias Grosser     IslNodeBuilder NodeBuilder(Builder, Annotator, this, *DL, *LI, *SE, *DT, S);
13609d30697STobias Grosser 
13709d30697STobias Grosser     // Only build the run-time condition and parameters _after_ having
13809d30697STobias Grosser     // introduced the conditional branch. This is important as the conditional
13909d30697STobias Grosser     // branch will guard the original scop from new induction variables that
14009d30697STobias Grosser     // the SCEVExpander may introduce while code generating the parameters and
14109d30697STobias Grosser     // which may introduce scalar dependences that prevent us from correctly
14209d30697STobias Grosser     // code generating this scop.
14309d30697STobias Grosser     BasicBlock *StartBlock =
14409d30697STobias Grosser         executeScopConditionally(S, this, Builder.getTrue());
14509d30697STobias Grosser     auto SplitBlock = StartBlock->getSinglePredecessor();
14609d30697STobias Grosser     Builder.SetInsertPoint(SplitBlock->getTerminator());
14709d30697STobias Grosser     NodeBuilder.addParameters(S.getContext());
14809d30697STobias Grosser     Value *RTC = buildRTC(Builder, NodeBuilder.getExprBuilder());
14909d30697STobias Grosser     SplitBlock->getTerminator()->setOperand(0, RTC);
15009d30697STobias Grosser     Builder.SetInsertPoint(StartBlock->begin());
15109d30697STobias Grosser 
15209d30697STobias Grosser     NodeBuilder.create(AstRoot);
15309d30697STobias Grosser 
154ecff11dcSJohannes Doerfert     NodeBuilder.finalizeSCoP(S);
1559c483c58SMichael Kruse     fixRegionInfo(EnteringBB->getParent(), R->getParent());
156ecff11dcSJohannes Doerfert 
15709d30697STobias Grosser     assert(!verifyGeneratedFunction(S, *EnteringBB->getParent()) &&
15809d30697STobias Grosser            "Verification of generated function failed");
15909d30697STobias Grosser     return true;
16009d30697STobias Grosser   }
16109d30697STobias Grosser 
16209d30697STobias Grosser   void printScop(raw_ostream &, Scop &) const override {}
16309d30697STobias Grosser 
16409d30697STobias Grosser   void getAnalysisUsage(AnalysisUsage &AU) const override {
16509d30697STobias Grosser     AU.addRequired<DominatorTreeWrapperPass>();
16609d30697STobias Grosser     AU.addRequired<IslAstInfo>();
16709d30697STobias Grosser     AU.addRequired<RegionInfoPass>();
168c5bcf246STobias Grosser     AU.addRequired<ScalarEvolutionWrapperPass>();
16909d30697STobias Grosser     AU.addRequired<ScopDetection>();
17009d30697STobias Grosser     AU.addRequired<ScopInfo>();
17109d30697STobias Grosser     AU.addRequired<LoopInfoWrapperPass>();
17209d30697STobias Grosser 
17309d30697STobias Grosser     AU.addPreserved<DependenceInfo>();
17409d30697STobias Grosser 
17566ef16b2SChandler Carruth     AU.addPreserved<AAResultsWrapperPass>();
17666ef16b2SChandler Carruth     AU.addPreserved<BasicAAWrapperPass>();
17709d30697STobias Grosser     AU.addPreserved<LoopInfoWrapperPass>();
17809d30697STobias Grosser     AU.addPreserved<DominatorTreeWrapperPass>();
17966ef16b2SChandler Carruth     AU.addPreserved<GlobalsAAWrapperPass>();
18082a1c7deSMichael Kruse     AU.addPreserved<PostDominatorTree>();
18109d30697STobias Grosser     AU.addPreserved<IslAstInfo>();
18209d30697STobias Grosser     AU.addPreserved<ScopDetection>();
183c5bcf246STobias Grosser     AU.addPreserved<ScalarEvolutionWrapperPass>();
18466ef16b2SChandler Carruth     AU.addPreserved<SCEVAAWrapperPass>();
18509d30697STobias Grosser 
18609d30697STobias Grosser     // FIXME: We do not yet add regions for the newly generated code to the
18709d30697STobias Grosser     //        region tree.
18809d30697STobias Grosser     AU.addPreserved<RegionInfoPass>();
18909d30697STobias Grosser     AU.addPreserved<ScopInfo>();
19009d30697STobias Grosser     AU.addPreservedID(IndependentBlocksID);
19109d30697STobias Grosser   }
19209d30697STobias Grosser };
19309d30697STobias Grosser }
19409d30697STobias Grosser 
19509d30697STobias Grosser char CodeGeneration::ID = 1;
19609d30697STobias Grosser 
19709d30697STobias Grosser Pass *polly::createCodeGenerationPass() { return new CodeGeneration(); }
19809d30697STobias Grosser 
19909d30697STobias Grosser INITIALIZE_PASS_BEGIN(CodeGeneration, "polly-codegen",
20009d30697STobias Grosser                       "Polly - Create LLVM-IR from SCoPs", false, false);
20109d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
20209d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
20309d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
20409d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
205c5bcf246STobias Grosser INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass);
20609d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(ScopDetection);
20709d30697STobias Grosser INITIALIZE_PASS_END(CodeGeneration, "polly-codegen",
20809d30697STobias Grosser                     "Polly - Create LLVM-IR from SCoPs", false, false)
209