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"
3382a1c7deSMichael Kruse #include "llvm/Analysis/PostDominators.h"
3409d30697STobias Grosser 
3509d30697STobias Grosser using namespace polly;
3609d30697STobias Grosser using namespace llvm;
3709d30697STobias Grosser 
3809d30697STobias Grosser #define DEBUG_TYPE "polly-codegen"
3909d30697STobias Grosser 
4009d30697STobias Grosser namespace {
4109d30697STobias Grosser class CodeGeneration : public ScopPass {
4209d30697STobias Grosser public:
4309d30697STobias Grosser   static char ID;
4409d30697STobias Grosser 
4509d30697STobias Grosser   CodeGeneration() : ScopPass(ID) {}
4609d30697STobias Grosser 
4709d30697STobias Grosser   /// @brief The datalayout used
4809d30697STobias Grosser   const DataLayout *DL;
4909d30697STobias Grosser 
5009d30697STobias Grosser   /// @name The analysis passes we need to generate code.
5109d30697STobias Grosser   ///
5209d30697STobias Grosser   ///{
5309d30697STobias Grosser   LoopInfo *LI;
5409d30697STobias Grosser   IslAstInfo *AI;
5509d30697STobias Grosser   DominatorTree *DT;
5609d30697STobias Grosser   ScalarEvolution *SE;
5722370884SMichael Kruse   RegionInfo *RI;
5809d30697STobias Grosser   ///}
5909d30697STobias Grosser 
6009d30697STobias Grosser   /// @brief The loop annotator to generate llvm.loop metadata.
6109d30697STobias Grosser   ScopAnnotator Annotator;
6209d30697STobias Grosser 
6309d30697STobias Grosser   /// @brief Build the runtime condition.
6409d30697STobias Grosser   ///
6509d30697STobias Grosser   /// Build the condition that evaluates at run-time to true iff all
6609d30697STobias Grosser   /// assumptions taken for the SCoP hold, and to false otherwise.
6709d30697STobias Grosser   ///
6809d30697STobias Grosser   /// @return A value evaluating to true/false if execution is save/unsafe.
6909d30697STobias Grosser   Value *buildRTC(PollyIRBuilder &Builder, IslExprBuilder &ExprBuilder) {
7009d30697STobias Grosser     Builder.SetInsertPoint(Builder.GetInsertBlock()->getTerminator());
7109d30697STobias Grosser     Value *RTC = ExprBuilder.create(AI->getRunCondition());
7209d30697STobias Grosser     if (!RTC->getType()->isIntegerTy(1))
7309d30697STobias Grosser       RTC = Builder.CreateIsNotNull(RTC);
7409d30697STobias Grosser     return RTC;
7509d30697STobias Grosser   }
7609d30697STobias Grosser 
7709d30697STobias Grosser   bool verifyGeneratedFunction(Scop &S, Function &F) {
7809d30697STobias Grosser     if (!verifyFunction(F))
7909d30697STobias Grosser       return false;
8009d30697STobias Grosser 
8109d30697STobias Grosser     DEBUG({
8209d30697STobias Grosser       errs() << "== ISL Codegen created an invalid function ==\n\n== The "
8309d30697STobias Grosser                 "SCoP ==\n";
8409d30697STobias Grosser       S.print(errs());
8509d30697STobias Grosser       errs() << "\n== The isl AST ==\n";
8609d30697STobias Grosser       AI->printScop(errs(), S);
8709d30697STobias Grosser       errs() << "\n== The invalid function ==\n";
8809d30697STobias Grosser       F.print(errs());
8909d30697STobias Grosser       errs() << "\n== The errors ==\n";
9009d30697STobias Grosser       verifyFunction(F, &errs());
9109d30697STobias Grosser     });
9209d30697STobias Grosser 
9309d30697STobias Grosser     return true;
9409d30697STobias Grosser   }
9509d30697STobias Grosser 
969c483c58SMichael Kruse   // CodeGeneration adds a lot of BBs without updating the RegionInfo
979c483c58SMichael Kruse   // We make all created BBs belong to the scop's parent region without any
989c483c58SMichael Kruse   // nested structure to keep the RegionInfo verifier happy.
999c483c58SMichael Kruse   void fixRegionInfo(Function *F, Region *ParentRegion) {
1009c483c58SMichael Kruse     for (BasicBlock &BB : *F) {
1019c483c58SMichael Kruse       if (RI->getRegionFor(&BB))
1029c483c58SMichael Kruse         continue;
1039c483c58SMichael Kruse 
1049c483c58SMichael Kruse       RI->setRegionFor(&BB, ParentRegion);
1059c483c58SMichael Kruse     }
1069c483c58SMichael Kruse   }
1079c483c58SMichael Kruse 
10809d30697STobias Grosser   bool runOnScop(Scop &S) override {
10909d30697STobias Grosser     AI = &getAnalysis<IslAstInfo>();
11009d30697STobias Grosser 
11109d30697STobias Grosser     // Check if we created an isl_ast root node, otherwise exit.
11209d30697STobias Grosser     isl_ast_node *AstRoot = AI->getAst();
11309d30697STobias Grosser     if (!AstRoot)
11409d30697STobias Grosser       return false;
11509d30697STobias Grosser 
11609d30697STobias Grosser     LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
11709d30697STobias Grosser     DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
118*c5bcf246STobias Grosser     SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
11909d30697STobias Grosser     DL = &S.getRegion().getEntry()->getParent()->getParent()->getDataLayout();
12022370884SMichael Kruse     RI = &getAnalysis<RegionInfoPass>().getRegionInfo();
12122370884SMichael Kruse     Region *R = &S.getRegion();
12222370884SMichael Kruse     assert(!R->isTopLevelRegion() && "Top level regions are not supported");
12309d30697STobias Grosser 
12409d30697STobias Grosser     Annotator.buildAliasScopes(S);
12509d30697STobias Grosser 
12622370884SMichael Kruse     simplifyRegion(R, DT, LI, RI);
12722370884SMichael Kruse     assert(R->isSimple());
12822370884SMichael Kruse     BasicBlock *EnteringBB = S.getRegion().getEnteringBlock();
12922370884SMichael Kruse     assert(EnteringBB);
13009d30697STobias Grosser     PollyIRBuilder Builder = createPollyIRBuilder(EnteringBB, Annotator);
13109d30697STobias Grosser 
13209d30697STobias Grosser     IslNodeBuilder NodeBuilder(Builder, Annotator, this, *DL, *LI, *SE, *DT, S);
13309d30697STobias Grosser 
13409d30697STobias Grosser     // Only build the run-time condition and parameters _after_ having
13509d30697STobias Grosser     // introduced the conditional branch. This is important as the conditional
13609d30697STobias Grosser     // branch will guard the original scop from new induction variables that
13709d30697STobias Grosser     // the SCEVExpander may introduce while code generating the parameters and
13809d30697STobias Grosser     // which may introduce scalar dependences that prevent us from correctly
13909d30697STobias Grosser     // code generating this scop.
14009d30697STobias Grosser     BasicBlock *StartBlock =
14109d30697STobias Grosser         executeScopConditionally(S, this, Builder.getTrue());
14209d30697STobias Grosser     auto SplitBlock = StartBlock->getSinglePredecessor();
14309d30697STobias Grosser     Builder.SetInsertPoint(SplitBlock->getTerminator());
14409d30697STobias Grosser     NodeBuilder.addParameters(S.getContext());
14509d30697STobias Grosser     Value *RTC = buildRTC(Builder, NodeBuilder.getExprBuilder());
14609d30697STobias Grosser     SplitBlock->getTerminator()->setOperand(0, RTC);
14709d30697STobias Grosser     Builder.SetInsertPoint(StartBlock->begin());
14809d30697STobias Grosser 
14909d30697STobias Grosser     NodeBuilder.create(AstRoot);
15009d30697STobias Grosser 
151ecff11dcSJohannes Doerfert     NodeBuilder.finalizeSCoP(S);
1529c483c58SMichael Kruse     fixRegionInfo(EnteringBB->getParent(), R->getParent());
153ecff11dcSJohannes Doerfert 
15409d30697STobias Grosser     assert(!verifyGeneratedFunction(S, *EnteringBB->getParent()) &&
15509d30697STobias Grosser            "Verification of generated function failed");
15609d30697STobias Grosser     return true;
15709d30697STobias Grosser   }
15809d30697STobias Grosser 
15909d30697STobias Grosser   void printScop(raw_ostream &, Scop &) const override {}
16009d30697STobias Grosser 
16109d30697STobias Grosser   void getAnalysisUsage(AnalysisUsage &AU) const override {
16209d30697STobias Grosser     AU.addRequired<DominatorTreeWrapperPass>();
16309d30697STobias Grosser     AU.addRequired<IslAstInfo>();
16409d30697STobias Grosser     AU.addRequired<RegionInfoPass>();
165*c5bcf246STobias Grosser     AU.addRequired<ScalarEvolutionWrapperPass>();
16609d30697STobias Grosser     AU.addRequired<ScopDetection>();
16709d30697STobias Grosser     AU.addRequired<ScopInfo>();
16809d30697STobias Grosser     AU.addRequired<LoopInfoWrapperPass>();
16909d30697STobias Grosser 
17009d30697STobias Grosser     AU.addPreserved<DependenceInfo>();
17109d30697STobias Grosser 
17209d30697STobias Grosser     AU.addPreserved<LoopInfoWrapperPass>();
17309d30697STobias Grosser     AU.addPreserved<DominatorTreeWrapperPass>();
17482a1c7deSMichael Kruse     AU.addPreserved<PostDominatorTree>();
17509d30697STobias Grosser     AU.addPreserved<IslAstInfo>();
17609d30697STobias Grosser     AU.addPreserved<ScopDetection>();
177*c5bcf246STobias Grosser     AU.addPreserved<ScalarEvolutionWrapperPass>();
17809d30697STobias Grosser 
17909d30697STobias Grosser     // FIXME: We do not yet add regions for the newly generated code to the
18009d30697STobias Grosser     //        region tree.
18109d30697STobias Grosser     AU.addPreserved<RegionInfoPass>();
18209d30697STobias Grosser     AU.addPreserved<TempScopInfo>();
18309d30697STobias Grosser     AU.addPreserved<ScopInfo>();
18409d30697STobias Grosser     AU.addPreservedID(IndependentBlocksID);
18509d30697STobias Grosser   }
18609d30697STobias Grosser };
18709d30697STobias Grosser }
18809d30697STobias Grosser 
18909d30697STobias Grosser char CodeGeneration::ID = 1;
19009d30697STobias Grosser 
19109d30697STobias Grosser Pass *polly::createCodeGenerationPass() { return new CodeGeneration(); }
19209d30697STobias Grosser 
19309d30697STobias Grosser INITIALIZE_PASS_BEGIN(CodeGeneration, "polly-codegen",
19409d30697STobias Grosser                       "Polly - Create LLVM-IR from SCoPs", false, false);
19509d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
19609d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
19709d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
19809d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
199*c5bcf246STobias Grosser INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass);
20009d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(ScopDetection);
20109d30697STobias Grosser INITIALIZE_PASS_END(CodeGeneration, "polly-codegen",
20209d30697STobias Grosser                     "Polly - Create LLVM-IR from SCoPs", false, false)
203