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; 5622370884SMichael Kruse RegionInfo *RI; 5709d30697STobias Grosser ///} 5809d30697STobias Grosser 5909d30697STobias Grosser /// @brief The loop annotator to generate llvm.loop metadata. 6009d30697STobias Grosser ScopAnnotator Annotator; 6109d30697STobias Grosser 6209d30697STobias Grosser /// @brief Build the runtime condition. 6309d30697STobias Grosser /// 6409d30697STobias Grosser /// Build the condition that evaluates at run-time to true iff all 6509d30697STobias Grosser /// assumptions taken for the SCoP hold, and to false otherwise. 6609d30697STobias Grosser /// 6709d30697STobias Grosser /// @return A value evaluating to true/false if execution is save/unsafe. 6809d30697STobias Grosser Value *buildRTC(PollyIRBuilder &Builder, IslExprBuilder &ExprBuilder) { 6909d30697STobias Grosser Builder.SetInsertPoint(Builder.GetInsertBlock()->getTerminator()); 7009d30697STobias Grosser Value *RTC = ExprBuilder.create(AI->getRunCondition()); 7109d30697STobias Grosser if (!RTC->getType()->isIntegerTy(1)) 7209d30697STobias Grosser RTC = Builder.CreateIsNotNull(RTC); 7309d30697STobias Grosser return RTC; 7409d30697STobias Grosser } 7509d30697STobias Grosser 7609d30697STobias Grosser bool verifyGeneratedFunction(Scop &S, Function &F) { 7709d30697STobias Grosser if (!verifyFunction(F)) 7809d30697STobias Grosser return false; 7909d30697STobias Grosser 8009d30697STobias Grosser DEBUG({ 8109d30697STobias Grosser errs() << "== ISL Codegen created an invalid function ==\n\n== The " 8209d30697STobias Grosser "SCoP ==\n"; 8309d30697STobias Grosser S.print(errs()); 8409d30697STobias Grosser errs() << "\n== The isl AST ==\n"; 8509d30697STobias Grosser AI->printScop(errs(), S); 8609d30697STobias Grosser errs() << "\n== The invalid function ==\n"; 8709d30697STobias Grosser F.print(errs()); 8809d30697STobias Grosser errs() << "\n== The errors ==\n"; 8909d30697STobias Grosser verifyFunction(F, &errs()); 9009d30697STobias Grosser }); 9109d30697STobias Grosser 9209d30697STobias Grosser return true; 9309d30697STobias Grosser } 9409d30697STobias Grosser 95*9c483c58SMichael Kruse // CodeGeneration adds a lot of BBs without updating the RegionInfo 96*9c483c58SMichael Kruse // We make all created BBs belong to the scop's parent region without any 97*9c483c58SMichael Kruse // nested structure to keep the RegionInfo verifier happy. 98*9c483c58SMichael Kruse void fixRegionInfo(Function *F, Region *ParentRegion) { 99*9c483c58SMichael Kruse for (BasicBlock &BB : *F) { 100*9c483c58SMichael Kruse if (RI->getRegionFor(&BB)) 101*9c483c58SMichael Kruse continue; 102*9c483c58SMichael Kruse 103*9c483c58SMichael Kruse RI->setRegionFor(&BB, ParentRegion); 104*9c483c58SMichael Kruse } 105*9c483c58SMichael Kruse } 106*9c483c58SMichael Kruse 10709d30697STobias Grosser bool runOnScop(Scop &S) override { 10809d30697STobias Grosser AI = &getAnalysis<IslAstInfo>(); 10909d30697STobias Grosser 11009d30697STobias Grosser // Check if we created an isl_ast root node, otherwise exit. 11109d30697STobias Grosser isl_ast_node *AstRoot = AI->getAst(); 11209d30697STobias Grosser if (!AstRoot) 11309d30697STobias Grosser return false; 11409d30697STobias Grosser 11509d30697STobias Grosser LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 11609d30697STobias Grosser DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 11709d30697STobias Grosser SE = &getAnalysis<ScalarEvolution>(); 11809d30697STobias Grosser DL = &S.getRegion().getEntry()->getParent()->getParent()->getDataLayout(); 11922370884SMichael Kruse RI = &getAnalysis<RegionInfoPass>().getRegionInfo(); 12022370884SMichael Kruse Region *R = &S.getRegion(); 12122370884SMichael Kruse assert(!R->isTopLevelRegion() && "Top level regions are not supported"); 12209d30697STobias Grosser 12309d30697STobias Grosser Annotator.buildAliasScopes(S); 12409d30697STobias Grosser 12522370884SMichael Kruse simplifyRegion(R, DT, LI, RI); 12622370884SMichael Kruse assert(R->isSimple()); 12722370884SMichael Kruse BasicBlock *EnteringBB = S.getRegion().getEnteringBlock(); 12822370884SMichael Kruse assert(EnteringBB); 12909d30697STobias Grosser PollyIRBuilder Builder = createPollyIRBuilder(EnteringBB, Annotator); 13009d30697STobias Grosser 13109d30697STobias Grosser IslNodeBuilder NodeBuilder(Builder, Annotator, this, *DL, *LI, *SE, *DT, S); 13209d30697STobias Grosser 13309d30697STobias Grosser // Only build the run-time condition and parameters _after_ having 13409d30697STobias Grosser // introduced the conditional branch. This is important as the conditional 13509d30697STobias Grosser // branch will guard the original scop from new induction variables that 13609d30697STobias Grosser // the SCEVExpander may introduce while code generating the parameters and 13709d30697STobias Grosser // which may introduce scalar dependences that prevent us from correctly 13809d30697STobias Grosser // code generating this scop. 13909d30697STobias Grosser BasicBlock *StartBlock = 14009d30697STobias Grosser executeScopConditionally(S, this, Builder.getTrue()); 14109d30697STobias Grosser auto SplitBlock = StartBlock->getSinglePredecessor(); 14209d30697STobias Grosser Builder.SetInsertPoint(SplitBlock->getTerminator()); 14309d30697STobias Grosser NodeBuilder.addParameters(S.getContext()); 14409d30697STobias Grosser Value *RTC = buildRTC(Builder, NodeBuilder.getExprBuilder()); 14509d30697STobias Grosser SplitBlock->getTerminator()->setOperand(0, RTC); 14609d30697STobias Grosser Builder.SetInsertPoint(StartBlock->begin()); 14709d30697STobias Grosser 14809d30697STobias Grosser NodeBuilder.create(AstRoot); 14909d30697STobias Grosser 150ecff11dcSJohannes Doerfert NodeBuilder.finalizeSCoP(S); 151*9c483c58SMichael Kruse fixRegionInfo(EnteringBB->getParent(), R->getParent()); 152ecff11dcSJohannes Doerfert 15309d30697STobias Grosser assert(!verifyGeneratedFunction(S, *EnteringBB->getParent()) && 15409d30697STobias Grosser "Verification of generated function failed"); 15509d30697STobias Grosser return true; 15609d30697STobias Grosser } 15709d30697STobias Grosser 15809d30697STobias Grosser void printScop(raw_ostream &, Scop &) const override {} 15909d30697STobias Grosser 16009d30697STobias Grosser void getAnalysisUsage(AnalysisUsage &AU) const override { 16109d30697STobias Grosser AU.addRequired<DominatorTreeWrapperPass>(); 16209d30697STobias Grosser AU.addRequired<IslAstInfo>(); 16309d30697STobias Grosser AU.addRequired<RegionInfoPass>(); 16409d30697STobias Grosser AU.addRequired<ScalarEvolution>(); 16509d30697STobias Grosser AU.addRequired<ScopDetection>(); 16609d30697STobias Grosser AU.addRequired<ScopInfo>(); 16709d30697STobias Grosser AU.addRequired<LoopInfoWrapperPass>(); 16809d30697STobias Grosser 16909d30697STobias Grosser AU.addPreserved<DependenceInfo>(); 17009d30697STobias Grosser 17109d30697STobias Grosser AU.addPreserved<LoopInfoWrapperPass>(); 17209d30697STobias Grosser AU.addPreserved<DominatorTreeWrapperPass>(); 17309d30697STobias Grosser AU.addPreserved<IslAstInfo>(); 17409d30697STobias Grosser AU.addPreserved<ScopDetection>(); 17509d30697STobias Grosser AU.addPreserved<ScalarEvolution>(); 17609d30697STobias Grosser 17709d30697STobias Grosser // FIXME: We do not yet add regions for the newly generated code to the 17809d30697STobias Grosser // region tree. 17909d30697STobias Grosser AU.addPreserved<RegionInfoPass>(); 18009d30697STobias Grosser AU.addPreserved<TempScopInfo>(); 18109d30697STobias Grosser AU.addPreserved<ScopInfo>(); 18209d30697STobias Grosser AU.addPreservedID(IndependentBlocksID); 18309d30697STobias Grosser } 18409d30697STobias Grosser }; 18509d30697STobias Grosser } 18609d30697STobias Grosser 18709d30697STobias Grosser char CodeGeneration::ID = 1; 18809d30697STobias Grosser 18909d30697STobias Grosser Pass *polly::createCodeGenerationPass() { return new CodeGeneration(); } 19009d30697STobias Grosser 19109d30697STobias Grosser INITIALIZE_PASS_BEGIN(CodeGeneration, "polly-codegen", 19209d30697STobias Grosser "Polly - Create LLVM-IR from SCoPs", false, false); 19309d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(DependenceInfo); 19409d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); 19509d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); 19609d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); 19709d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(ScalarEvolution); 19809d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(ScopDetection); 19909d30697STobias Grosser INITIALIZE_PASS_END(CodeGeneration, "polly-codegen", 20009d30697STobias Grosser "Polly - Create LLVM-IR from SCoPs", false, false) 201