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; 56*22370884SMichael 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 9509d30697STobias Grosser bool runOnScop(Scop &S) override { 9609d30697STobias Grosser AI = &getAnalysis<IslAstInfo>(); 9709d30697STobias Grosser 9809d30697STobias Grosser // Check if we created an isl_ast root node, otherwise exit. 9909d30697STobias Grosser isl_ast_node *AstRoot = AI->getAst(); 10009d30697STobias Grosser if (!AstRoot) 10109d30697STobias Grosser return false; 10209d30697STobias Grosser 10309d30697STobias Grosser LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 10409d30697STobias Grosser DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 10509d30697STobias Grosser SE = &getAnalysis<ScalarEvolution>(); 10609d30697STobias Grosser DL = &S.getRegion().getEntry()->getParent()->getParent()->getDataLayout(); 107*22370884SMichael Kruse RI = &getAnalysis<RegionInfoPass>().getRegionInfo(); 108*22370884SMichael Kruse Region *R = &S.getRegion(); 109*22370884SMichael Kruse assert(!R->isTopLevelRegion() && "Top level regions are not supported"); 11009d30697STobias Grosser 11109d30697STobias Grosser Annotator.buildAliasScopes(S); 11209d30697STobias Grosser 113*22370884SMichael Kruse simplifyRegion(R, DT, LI, RI); 114*22370884SMichael Kruse assert(R->isSimple()); 115*22370884SMichael Kruse BasicBlock *EnteringBB = S.getRegion().getEnteringBlock(); 116*22370884SMichael Kruse assert(EnteringBB); 11709d30697STobias Grosser PollyIRBuilder Builder = createPollyIRBuilder(EnteringBB, Annotator); 11809d30697STobias Grosser 11909d30697STobias Grosser IslNodeBuilder NodeBuilder(Builder, Annotator, this, *DL, *LI, *SE, *DT, S); 12009d30697STobias Grosser 12109d30697STobias Grosser // Only build the run-time condition and parameters _after_ having 12209d30697STobias Grosser // introduced the conditional branch. This is important as the conditional 12309d30697STobias Grosser // branch will guard the original scop from new induction variables that 12409d30697STobias Grosser // the SCEVExpander may introduce while code generating the parameters and 12509d30697STobias Grosser // which may introduce scalar dependences that prevent us from correctly 12609d30697STobias Grosser // code generating this scop. 12709d30697STobias Grosser BasicBlock *StartBlock = 12809d30697STobias Grosser executeScopConditionally(S, this, Builder.getTrue()); 12909d30697STobias Grosser auto SplitBlock = StartBlock->getSinglePredecessor(); 13009d30697STobias Grosser Builder.SetInsertPoint(SplitBlock->getTerminator()); 13109d30697STobias Grosser NodeBuilder.addParameters(S.getContext()); 13209d30697STobias Grosser Value *RTC = buildRTC(Builder, NodeBuilder.getExprBuilder()); 13309d30697STobias Grosser SplitBlock->getTerminator()->setOperand(0, RTC); 13409d30697STobias Grosser Builder.SetInsertPoint(StartBlock->begin()); 13509d30697STobias Grosser 13609d30697STobias Grosser NodeBuilder.create(AstRoot); 13709d30697STobias Grosser 138ecff11dcSJohannes Doerfert NodeBuilder.finalizeSCoP(S); 139ecff11dcSJohannes Doerfert 14009d30697STobias Grosser assert(!verifyGeneratedFunction(S, *EnteringBB->getParent()) && 14109d30697STobias Grosser "Verification of generated function failed"); 14209d30697STobias Grosser return true; 14309d30697STobias Grosser } 14409d30697STobias Grosser 14509d30697STobias Grosser void printScop(raw_ostream &, Scop &) const override {} 14609d30697STobias Grosser 14709d30697STobias Grosser void getAnalysisUsage(AnalysisUsage &AU) const override { 14809d30697STobias Grosser AU.addRequired<DominatorTreeWrapperPass>(); 14909d30697STobias Grosser AU.addRequired<IslAstInfo>(); 15009d30697STobias Grosser AU.addRequired<RegionInfoPass>(); 15109d30697STobias Grosser AU.addRequired<ScalarEvolution>(); 15209d30697STobias Grosser AU.addRequired<ScopDetection>(); 15309d30697STobias Grosser AU.addRequired<ScopInfo>(); 15409d30697STobias Grosser AU.addRequired<LoopInfoWrapperPass>(); 15509d30697STobias Grosser 15609d30697STobias Grosser AU.addPreserved<DependenceInfo>(); 15709d30697STobias Grosser 15809d30697STobias Grosser AU.addPreserved<LoopInfoWrapperPass>(); 15909d30697STobias Grosser AU.addPreserved<DominatorTreeWrapperPass>(); 16009d30697STobias Grosser AU.addPreserved<IslAstInfo>(); 16109d30697STobias Grosser AU.addPreserved<ScopDetection>(); 16209d30697STobias Grosser AU.addPreserved<ScalarEvolution>(); 16309d30697STobias Grosser 16409d30697STobias Grosser // FIXME: We do not yet add regions for the newly generated code to the 16509d30697STobias Grosser // region tree. 16609d30697STobias Grosser AU.addPreserved<RegionInfoPass>(); 16709d30697STobias Grosser AU.addPreserved<TempScopInfo>(); 16809d30697STobias Grosser AU.addPreserved<ScopInfo>(); 16909d30697STobias Grosser AU.addPreservedID(IndependentBlocksID); 17009d30697STobias Grosser } 17109d30697STobias Grosser }; 17209d30697STobias Grosser } 17309d30697STobias Grosser 17409d30697STobias Grosser char CodeGeneration::ID = 1; 17509d30697STobias Grosser 17609d30697STobias Grosser Pass *polly::createCodeGenerationPass() { return new CodeGeneration(); } 17709d30697STobias Grosser 17809d30697STobias Grosser INITIALIZE_PASS_BEGIN(CodeGeneration, "polly-codegen", 17909d30697STobias Grosser "Polly - Create LLVM-IR from SCoPs", false, false); 18009d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(DependenceInfo); 18109d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); 18209d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); 18309d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); 18409d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(ScalarEvolution); 18509d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(ScopDetection); 18609d30697STobias Grosser INITIALIZE_PASS_END(CodeGeneration, "polly-codegen", 18709d30697STobias Grosser "Polly - Create LLVM-IR from SCoPs", false, false) 188