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/IslAst.h" 235624d3c9STobias Grosser #include "polly/CodeGen/IslNodeBuilder.h" 2409d30697STobias Grosser #include "polly/CodeGen/Utils.h" 2509d30697STobias Grosser #include "polly/DependenceInfo.h" 2609d30697STobias Grosser #include "polly/LinkAllPasses.h" 2758e58544STobias Grosser #include "polly/Options.h" 2809d30697STobias Grosser #include "polly/ScopInfo.h" 2909d30697STobias Grosser #include "polly/Support/ScopHelper.h" 3066ef16b2SChandler Carruth #include "llvm/Analysis/AliasAnalysis.h" 3166ef16b2SChandler Carruth #include "llvm/Analysis/BasicAliasAnalysis.h" 3266ef16b2SChandler Carruth #include "llvm/Analysis/GlobalsModRef.h" 3382a1c7deSMichael Kruse #include "llvm/Analysis/PostDominators.h" 3466ef16b2SChandler Carruth #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" 35c2bb0cbeSTobias Grosser #include "llvm/IR/Module.h" 36c2bb0cbeSTobias Grosser #include "llvm/IR/Verifier.h" 37c2bb0cbeSTobias Grosser #include "llvm/Support/Debug.h" 3809d30697STobias Grosser 3909d30697STobias Grosser using namespace polly; 4009d30697STobias Grosser using namespace llvm; 4109d30697STobias Grosser 4209d30697STobias Grosser #define DEBUG_TYPE "polly-codegen" 4309d30697STobias Grosser 4458e58544STobias Grosser static cl::opt<bool> Verify("polly-codegen-verify", 4558e58544STobias Grosser cl::desc("Verify the function generated by Polly"), 4658e58544STobias Grosser cl::Hidden, cl::init(true), cl::ZeroOrMore, 4758e58544STobias Grosser cl::cat(PollyCategory)); 4858e58544STobias Grosser 4909d30697STobias Grosser namespace { 5009d30697STobias Grosser class CodeGeneration : public ScopPass { 5109d30697STobias Grosser public: 5209d30697STobias Grosser static char ID; 5309d30697STobias Grosser 5409d30697STobias Grosser CodeGeneration() : ScopPass(ID) {} 5509d30697STobias Grosser 5609d30697STobias Grosser /// @brief The datalayout used 5709d30697STobias Grosser const DataLayout *DL; 5809d30697STobias Grosser 5909d30697STobias Grosser /// @name The analysis passes we need to generate code. 6009d30697STobias Grosser /// 6109d30697STobias Grosser ///{ 6209d30697STobias Grosser LoopInfo *LI; 6309d30697STobias Grosser IslAstInfo *AI; 6409d30697STobias Grosser DominatorTree *DT; 6509d30697STobias Grosser ScalarEvolution *SE; 6622370884SMichael Kruse RegionInfo *RI; 6709d30697STobias Grosser ///} 6809d30697STobias Grosser 6909d30697STobias Grosser /// @brief Build the runtime condition. 7009d30697STobias Grosser /// 7109d30697STobias Grosser /// Build the condition that evaluates at run-time to true iff all 7209d30697STobias Grosser /// assumptions taken for the SCoP hold, and to false otherwise. 7309d30697STobias Grosser /// 7409d30697STobias Grosser /// @return A value evaluating to true/false if execution is save/unsafe. 7509d30697STobias Grosser Value *buildRTC(PollyIRBuilder &Builder, IslExprBuilder &ExprBuilder) { 7609d30697STobias Grosser Builder.SetInsertPoint(Builder.GetInsertBlock()->getTerminator()); 7709d30697STobias Grosser Value *RTC = ExprBuilder.create(AI->getRunCondition()); 7809d30697STobias Grosser if (!RTC->getType()->isIntegerTy(1)) 7909d30697STobias Grosser RTC = Builder.CreateIsNotNull(RTC); 8009d30697STobias Grosser return RTC; 8109d30697STobias Grosser } 8209d30697STobias Grosser 8358e58544STobias Grosser void verifyGeneratedFunction(Scop &S, Function &F) { 8458e58544STobias Grosser if (!verifyFunction(F, &errs()) || !Verify) 8558e58544STobias Grosser return; 8609d30697STobias Grosser 8709d30697STobias Grosser DEBUG({ 8809d30697STobias Grosser errs() << "== ISL Codegen created an invalid function ==\n\n== The " 8909d30697STobias Grosser "SCoP ==\n"; 9009d30697STobias Grosser S.print(errs()); 9109d30697STobias Grosser errs() << "\n== The isl AST ==\n"; 9209d30697STobias Grosser AI->printScop(errs(), S); 9309d30697STobias Grosser errs() << "\n== The invalid function ==\n"; 9409d30697STobias Grosser F.print(errs()); 9509d30697STobias Grosser }); 9609d30697STobias Grosser 9758e58544STobias Grosser llvm_unreachable("Polly generated function could not be verified. Add " 9858e58544STobias Grosser "-polly-codegen-verify=false to disable this assertion."); 9909d30697STobias Grosser } 10009d30697STobias Grosser 1019c483c58SMichael Kruse // CodeGeneration adds a lot of BBs without updating the RegionInfo 1029c483c58SMichael Kruse // We make all created BBs belong to the scop's parent region without any 1039c483c58SMichael Kruse // nested structure to keep the RegionInfo verifier happy. 1049c483c58SMichael Kruse void fixRegionInfo(Function *F, Region *ParentRegion) { 1059c483c58SMichael Kruse for (BasicBlock &BB : *F) { 1069c483c58SMichael Kruse if (RI->getRegionFor(&BB)) 1079c483c58SMichael Kruse continue; 1089c483c58SMichael Kruse 1099c483c58SMichael Kruse RI->setRegionFor(&BB, ParentRegion); 1109c483c58SMichael Kruse } 1119c483c58SMichael Kruse } 1129c483c58SMichael Kruse 113bfb6a968STobias Grosser /// @brief Mark a basic block unreachable. 114bfb6a968STobias Grosser /// 115bfb6a968STobias Grosser /// Marks the basic block @p Block unreachable by equipping it with an 116bfb6a968STobias Grosser /// UnreachableInst. 117bfb6a968STobias Grosser void markBlockUnreachable(BasicBlock &Block, PollyIRBuilder &Builder) { 118bfb6a968STobias Grosser auto *OrigTerminator = Block.getTerminator(); 119bfb6a968STobias Grosser Builder.SetInsertPoint(OrigTerminator); 120bfb6a968STobias Grosser Builder.CreateUnreachable(); 121bfb6a968STobias Grosser OrigTerminator->eraseFromParent(); 122bfb6a968STobias Grosser } 123bfb6a968STobias Grosser 12445be6446SJohannes Doerfert /// @brief Generate LLVM-IR for the SCoP @p S. 12509d30697STobias Grosser bool runOnScop(Scop &S) override { 12609d30697STobias Grosser AI = &getAnalysis<IslAstInfo>(); 12709d30697STobias Grosser 12809d30697STobias Grosser // Check if we created an isl_ast root node, otherwise exit. 12909d30697STobias Grosser isl_ast_node *AstRoot = AI->getAst(); 13009d30697STobias Grosser if (!AstRoot) 13109d30697STobias Grosser return false; 13209d30697STobias Grosser 13309d30697STobias Grosser LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 13409d30697STobias Grosser DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 135c5bcf246STobias Grosser SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); 1363f52e354SJohannes Doerfert DL = &S.getFunction().getParent()->getDataLayout(); 13722370884SMichael Kruse RI = &getAnalysis<RegionInfoPass>().getRegionInfo(); 13822370884SMichael Kruse Region *R = &S.getRegion(); 13922370884SMichael Kruse assert(!R->isTopLevelRegion() && "Top level regions are not supported"); 14009d30697STobias Grosser 141d78616f9STobias Grosser ScopAnnotator Annotator; 14209d30697STobias Grosser Annotator.buildAliasScopes(S); 14309d30697STobias Grosser 14422370884SMichael Kruse simplifyRegion(R, DT, LI, RI); 14522370884SMichael Kruse assert(R->isSimple()); 146*ef74443cSJohannes Doerfert BasicBlock *EnteringBB = S.getEnteringBlock(); 14722370884SMichael Kruse assert(EnteringBB); 14809d30697STobias Grosser PollyIRBuilder Builder = createPollyIRBuilder(EnteringBB, Annotator); 14909d30697STobias Grosser 15009d30697STobias Grosser IslNodeBuilder NodeBuilder(Builder, Annotator, this, *DL, *LI, *SE, *DT, S); 151404a0f81SJohannes Doerfert IslExprBuilder &ExprBuilder = NodeBuilder.getExprBuilder(); 15209d30697STobias Grosser 15309d30697STobias Grosser // Only build the run-time condition and parameters _after_ having 15409d30697STobias Grosser // introduced the conditional branch. This is important as the conditional 15509d30697STobias Grosser // branch will guard the original scop from new induction variables that 15609d30697STobias Grosser // the SCEVExpander may introduce while code generating the parameters and 15709d30697STobias Grosser // which may introduce scalar dependences that prevent us from correctly 15809d30697STobias Grosser // code generating this scop. 15909d30697STobias Grosser BasicBlock *StartBlock = 16009d30697STobias Grosser executeScopConditionally(S, this, Builder.getTrue()); 161bfb6a968STobias Grosser auto *SplitBlock = StartBlock->getSinglePredecessor(); 16209e3697fSJohannes Doerfert 16309e3697fSJohannes Doerfert // First generate code for the hoisted invariant loads and transitively the 16409e3697fSJohannes Doerfert // parameters they reference. Afterwards, for the remaining parameters that 16509e3697fSJohannes Doerfert // might reference the hoisted loads. Finally, build the runtime check 16609e3697fSJohannes Doerfert // that might reference both hoisted loads as well as parameters. 167c4898504SJohannes Doerfert // If the hoisting fails we have to bail and execute the original code. 16809d30697STobias Grosser Builder.SetInsertPoint(SplitBlock->getTerminator()); 169c4898504SJohannes Doerfert if (!NodeBuilder.preloadInvariantLoads()) { 1701dd6e37aSJohannes Doerfert 171bfb6a968STobias Grosser // Patch the introduced branch condition to ensure that we always execute 172bfb6a968STobias Grosser // the original SCoP. 173c4898504SJohannes Doerfert auto *FalseI1 = Builder.getFalse(); 17437977076SJohannes Doerfert auto *SplitBBTerm = Builder.GetInsertBlock()->getTerminator(); 17537977076SJohannes Doerfert SplitBBTerm->setOperand(0, FalseI1); 1761dd6e37aSJohannes Doerfert 177bfb6a968STobias Grosser // Since the other branch is hence ignored we mark it as unreachable and 178bfb6a968STobias Grosser // adjust the dominator tree accordingly. 179bfb6a968STobias Grosser auto *ExitingBlock = StartBlock->getUniqueSuccessor(); 180bfb6a968STobias Grosser assert(ExitingBlock); 181bfb6a968STobias Grosser auto *MergeBlock = ExitingBlock->getUniqueSuccessor(); 182bfb6a968STobias Grosser assert(MergeBlock); 183bfb6a968STobias Grosser markBlockUnreachable(*StartBlock, Builder); 184bfb6a968STobias Grosser markBlockUnreachable(*ExitingBlock, Builder); 185*ef74443cSJohannes Doerfert auto *ExitingBB = S.getExitingBlock(); 186bfb6a968STobias Grosser assert(ExitingBB); 187bfb6a968STobias Grosser DT->changeImmediateDominator(MergeBlock, ExitingBB); 188bfb6a968STobias Grosser DT->eraseNode(ExitingBlock); 189bfb6a968STobias Grosser 190bfb6a968STobias Grosser isl_ast_node_free(AstRoot); 1911dd6e37aSJohannes Doerfert } else { 192c4898504SJohannes Doerfert 19309e3697fSJohannes Doerfert NodeBuilder.addParameters(S.getContext()); 19409e3697fSJohannes Doerfert 195404a0f81SJohannes Doerfert ExprBuilder.setTrackOverflow(true); 196404a0f81SJohannes Doerfert Value *RTC = buildRTC(Builder, ExprBuilder); 197404a0f81SJohannes Doerfert Value *OverflowHappened = Builder.CreateNot( 198404a0f81SJohannes Doerfert ExprBuilder.getOverflowState(), "polly.rtc.overflown"); 199404a0f81SJohannes Doerfert RTC = Builder.CreateAnd(RTC, OverflowHappened, "polly.rtc.result"); 200404a0f81SJohannes Doerfert ExprBuilder.setTrackOverflow(false); 201404a0f81SJohannes Doerfert 202c1db67e2SJohannes Doerfert Builder.GetInsertBlock()->getTerminator()->setOperand(0, RTC); 203b8f58b53SDuncan P. N. Exon Smith Builder.SetInsertPoint(&StartBlock->front()); 20409d30697STobias Grosser 20509d30697STobias Grosser NodeBuilder.create(AstRoot); 20609d30697STobias Grosser 207ecff11dcSJohannes Doerfert NodeBuilder.finalizeSCoP(S); 2089c483c58SMichael Kruse fixRegionInfo(EnteringBB->getParent(), R->getParent()); 2091dd6e37aSJohannes Doerfert } 210ecff11dcSJohannes Doerfert 21158e58544STobias Grosser verifyGeneratedFunction(S, *EnteringBB->getParent()); 212a9dc5294SJohannes Doerfert for (auto *SubF : NodeBuilder.getParallelSubfunctions()) 213a9dc5294SJohannes Doerfert verifyGeneratedFunction(S, *SubF); 214652f7808STobias Grosser 2154c86a1d9SMichael Kruse // Mark the function such that we run additional cleanup passes on this 2164c86a1d9SMichael Kruse // function (e.g. mem2reg to rediscover phi nodes). 2174c86a1d9SMichael Kruse Function *F = EnteringBB->getParent(); 2184c86a1d9SMichael Kruse F->addFnAttr("polly-optimized"); 2194c86a1d9SMichael Kruse 22009d30697STobias Grosser return true; 22109d30697STobias Grosser } 22209d30697STobias Grosser 22345be6446SJohannes Doerfert /// @brief Register all analyses and transformation required. 22409d30697STobias Grosser void getAnalysisUsage(AnalysisUsage &AU) const override { 22509d30697STobias Grosser AU.addRequired<DominatorTreeWrapperPass>(); 22609d30697STobias Grosser AU.addRequired<IslAstInfo>(); 22709d30697STobias Grosser AU.addRequired<RegionInfoPass>(); 228c5bcf246STobias Grosser AU.addRequired<ScalarEvolutionWrapperPass>(); 22909d30697STobias Grosser AU.addRequired<ScopDetection>(); 23009d30697STobias Grosser AU.addRequired<ScopInfo>(); 23109d30697STobias Grosser AU.addRequired<LoopInfoWrapperPass>(); 23209d30697STobias Grosser 23309d30697STobias Grosser AU.addPreserved<DependenceInfo>(); 23409d30697STobias Grosser 23566ef16b2SChandler Carruth AU.addPreserved<AAResultsWrapperPass>(); 23666ef16b2SChandler Carruth AU.addPreserved<BasicAAWrapperPass>(); 23709d30697STobias Grosser AU.addPreserved<LoopInfoWrapperPass>(); 23809d30697STobias Grosser AU.addPreserved<DominatorTreeWrapperPass>(); 23966ef16b2SChandler Carruth AU.addPreserved<GlobalsAAWrapperPass>(); 240defd0986SHongbin Zheng AU.addPreserved<PostDominatorTreeWrapperPass>(); 24109d30697STobias Grosser AU.addPreserved<IslAstInfo>(); 24209d30697STobias Grosser AU.addPreserved<ScopDetection>(); 243c5bcf246STobias Grosser AU.addPreserved<ScalarEvolutionWrapperPass>(); 24466ef16b2SChandler Carruth AU.addPreserved<SCEVAAWrapperPass>(); 24509d30697STobias Grosser 24609d30697STobias Grosser // FIXME: We do not yet add regions for the newly generated code to the 24709d30697STobias Grosser // region tree. 24809d30697STobias Grosser AU.addPreserved<RegionInfoPass>(); 24909d30697STobias Grosser AU.addPreserved<ScopInfo>(); 25009d30697STobias Grosser } 25109d30697STobias Grosser }; 25209d30697STobias Grosser } 25309d30697STobias Grosser 25409d30697STobias Grosser char CodeGeneration::ID = 1; 25509d30697STobias Grosser 25609d30697STobias Grosser Pass *polly::createCodeGenerationPass() { return new CodeGeneration(); } 25709d30697STobias Grosser 25809d30697STobias Grosser INITIALIZE_PASS_BEGIN(CodeGeneration, "polly-codegen", 25909d30697STobias Grosser "Polly - Create LLVM-IR from SCoPs", false, false); 26009d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(DependenceInfo); 26109d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); 26209d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); 26309d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); 264c5bcf246STobias Grosser INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass); 26509d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(ScopDetection); 26609d30697STobias Grosser INITIALIZE_PASS_END(CodeGeneration, "polly-codegen", 26709d30697STobias Grosser "Polly - Create LLVM-IR from SCoPs", false, false) 268