19248fde5SEugene Zelenko //===- 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 // 13a6d48f59SMichael Kruse // The Scop describes the high level memory behavior 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 2278ae52f0SPhilip Pfaffe #include "polly/CodeGen/CodeGeneration.h" 239248fde5SEugene Zelenko #include "polly/CodeGen/IRBuilder.h" 2409d30697STobias Grosser #include "polly/CodeGen/IslAst.h" 255624d3c9STobias Grosser #include "polly/CodeGen/IslNodeBuilder.h" 2665371af2STobias Grosser #include "polly/CodeGen/PerfMonitor.h" 2709d30697STobias Grosser #include "polly/CodeGen/Utils.h" 2809d30697STobias Grosser #include "polly/DependenceInfo.h" 2909d30697STobias Grosser #include "polly/LinkAllPasses.h" 3058e58544STobias Grosser #include "polly/Options.h" 319248fde5SEugene Zelenko #include "polly/ScopDetectionDiagnostic.h" 3209d30697STobias Grosser #include "polly/ScopInfo.h" 3309d30697STobias Grosser #include "polly/Support/ScopHelper.h" 349248fde5SEugene Zelenko #include "llvm/ADT/Statistic.h" 3566ef16b2SChandler Carruth #include "llvm/Analysis/AliasAnalysis.h" 3666ef16b2SChandler Carruth #include "llvm/Analysis/BasicAliasAnalysis.h" 3766ef16b2SChandler Carruth #include "llvm/Analysis/GlobalsModRef.h" 3878ae52f0SPhilip Pfaffe #include "llvm/Analysis/LoopInfo.h" 399248fde5SEugene Zelenko #include "llvm/Analysis/RegionInfo.h" 4066ef16b2SChandler Carruth #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" 419248fde5SEugene Zelenko #include "llvm/IR/BasicBlock.h" 429248fde5SEugene Zelenko #include "llvm/IR/Dominators.h" 439248fde5SEugene Zelenko #include "llvm/IR/Function.h" 449248fde5SEugene Zelenko #include "llvm/IR/Instruction.h" 459248fde5SEugene Zelenko #include "llvm/IR/IntrinsicInst.h" 469248fde5SEugene Zelenko #include "llvm/IR/Intrinsics.h" 47c2bb0cbeSTobias Grosser #include "llvm/IR/Module.h" 4878ae52f0SPhilip Pfaffe #include "llvm/IR/PassManager.h" 49c2bb0cbeSTobias Grosser #include "llvm/IR/Verifier.h" 509248fde5SEugene Zelenko #include "llvm/Pass.h" 519248fde5SEugene Zelenko #include "llvm/Support/Casting.h" 529248fde5SEugene Zelenko #include "llvm/Support/CommandLine.h" 53c2bb0cbeSTobias Grosser #include "llvm/Support/Debug.h" 549248fde5SEugene Zelenko #include "llvm/Support/ErrorHandling.h" 559248fde5SEugene Zelenko #include "llvm/Support/raw_ostream.h" 569248fde5SEugene Zelenko #include "isl/ast.h" 579248fde5SEugene Zelenko #include <cassert> 589248fde5SEugene Zelenko #include <utility> 5909d30697STobias Grosser 6009d30697STobias Grosser using namespace llvm; 619248fde5SEugene Zelenko using namespace polly; 6209d30697STobias Grosser 6309d30697STobias Grosser #define DEBUG_TYPE "polly-codegen" 6409d30697STobias Grosser 6558e58544STobias Grosser static cl::opt<bool> Verify("polly-codegen-verify", 6658e58544STobias Grosser cl::desc("Verify the function generated by Polly"), 67f1372217STobias Grosser cl::Hidden, cl::init(false), cl::ZeroOrMore, 6858e58544STobias Grosser cl::cat(PollyCategory)); 6958e58544STobias Grosser 707b9f5ca2SSiddharth Bhat bool polly::PerfMonitoring; 719248fde5SEugene Zelenko 727b9f5ca2SSiddharth Bhat static cl::opt<bool, true> 737b9f5ca2SSiddharth Bhat XPerfMonitoring("polly-codegen-perf-monitoring", 7465371af2STobias Grosser cl::desc("Add run-time performance monitoring"), cl::Hidden, 757b9f5ca2SSiddharth Bhat cl::location(polly::PerfMonitoring), cl::init(false), 767b9f5ca2SSiddharth Bhat cl::ZeroOrMore, cl::cat(PollyCategory)); 7765371af2STobias Grosser 7806ed5292SMichael Kruse STATISTIC(ScopsProcessed, "Number of SCoP processed"); 7906ed5292SMichael Kruse STATISTIC(CodegenedScops, "Number of successfully generated SCoPs"); 8006ed5292SMichael Kruse STATISTIC(CodegenedAffineLoops, 8106ed5292SMichael Kruse "Number of original affine loops in SCoPs that have been generated"); 8206ed5292SMichael Kruse STATISTIC(CodegenedBoxedLoops, 8306ed5292SMichael Kruse "Number of original boxed loops in SCoPs that have been generated"); 8406ed5292SMichael Kruse 8571dfb3ebSSiddharth Bhat namespace polly { 869248fde5SEugene Zelenko 8771dfb3ebSSiddharth Bhat /// Mark a basic block unreachable. 8871dfb3ebSSiddharth Bhat /// 8971dfb3ebSSiddharth Bhat /// Marks the basic block @p Block unreachable by equipping it with an 9071dfb3ebSSiddharth Bhat /// UnreachableInst. 9171dfb3ebSSiddharth Bhat void markBlockUnreachable(BasicBlock &Block, PollyIRBuilder &Builder) { 9271dfb3ebSSiddharth Bhat auto *OrigTerminator = Block.getTerminator(); 9371dfb3ebSSiddharth Bhat Builder.SetInsertPoint(OrigTerminator); 9471dfb3ebSSiddharth Bhat Builder.CreateUnreachable(); 9571dfb3ebSSiddharth Bhat OrigTerminator->eraseFromParent(); 9671dfb3ebSSiddharth Bhat } 9771dfb3ebSSiddharth Bhat 9871dfb3ebSSiddharth Bhat } // namespace polly 9971dfb3ebSSiddharth Bhat 10078ae52f0SPhilip Pfaffe static void verifyGeneratedFunction(Scop &S, Function &F, IslAstInfo &AI) { 101d439911fSTobias Grosser if (!Verify || !verifyFunction(F, &errs())) 10258e58544STobias Grosser return; 10309d30697STobias Grosser 10409d30697STobias Grosser DEBUG({ 10509d30697STobias Grosser errs() << "== ISL Codegen created an invalid function ==\n\n== The " 10609d30697STobias Grosser "SCoP ==\n"; 107cd4c977bSMichael Kruse errs() << S; 10809d30697STobias Grosser errs() << "\n== The isl AST ==\n"; 10978ae52f0SPhilip Pfaffe AI.print(errs()); 11009d30697STobias Grosser errs() << "\n== The invalid function ==\n"; 11109d30697STobias Grosser F.print(errs()); 11209d30697STobias Grosser }); 11309d30697STobias Grosser 11458e58544STobias Grosser llvm_unreachable("Polly generated function could not be verified. Add " 11558e58544STobias Grosser "-polly-codegen-verify=false to disable this assertion."); 11609d30697STobias Grosser } 11709d30697STobias Grosser 1189c483c58SMichael Kruse // CodeGeneration adds a lot of BBs without updating the RegionInfo 1199c483c58SMichael Kruse // We make all created BBs belong to the scop's parent region without any 1209c483c58SMichael Kruse // nested structure to keep the RegionInfo verifier happy. 12178ae52f0SPhilip Pfaffe static void fixRegionInfo(Function &F, Region &ParentRegion, RegionInfo &RI) { 12278ae52f0SPhilip Pfaffe for (BasicBlock &BB : F) { 12378ae52f0SPhilip Pfaffe if (RI.getRegionFor(&BB)) 1249c483c58SMichael Kruse continue; 1259c483c58SMichael Kruse 12678ae52f0SPhilip Pfaffe RI.setRegionFor(&BB, &ParentRegion); 1279c483c58SMichael Kruse } 1289c483c58SMichael Kruse } 1299c483c58SMichael Kruse 130895f5d80SMichael Kruse /// Remove all lifetime markers (llvm.lifetime.start, llvm.lifetime.end) from 131895f5d80SMichael Kruse /// @R. 132895f5d80SMichael Kruse /// 133895f5d80SMichael Kruse /// CodeGeneration does not copy lifetime markers into the optimized SCoP, 134895f5d80SMichael Kruse /// which would leave the them only in the original path. This can transform 135895f5d80SMichael Kruse /// code such as 136895f5d80SMichael Kruse /// 137895f5d80SMichael Kruse /// llvm.lifetime.start(%p) 138895f5d80SMichael Kruse /// llvm.lifetime.end(%p) 139895f5d80SMichael Kruse /// 140895f5d80SMichael Kruse /// into 141895f5d80SMichael Kruse /// 142895f5d80SMichael Kruse /// if (RTC) { 143895f5d80SMichael Kruse /// // generated code 144895f5d80SMichael Kruse /// } else { 145895f5d80SMichael Kruse /// // original code 146895f5d80SMichael Kruse /// llvm.lifetime.start(%p) 147895f5d80SMichael Kruse /// } 148895f5d80SMichael Kruse /// llvm.lifetime.end(%p) 149895f5d80SMichael Kruse /// 150895f5d80SMichael Kruse /// The current StackColoring algorithm cannot handle if some, but not all, 151895f5d80SMichael Kruse /// paths from the end marker to the entry block cross the start marker. Same 152895f5d80SMichael Kruse /// for start markers that do not always cross the end markers. We avoid any 153895f5d80SMichael Kruse /// issues by removing all lifetime markers, even from the original code. 154895f5d80SMichael Kruse /// 155895f5d80SMichael Kruse /// A better solution could be to hoist all llvm.lifetime.start to the split 156895f5d80SMichael Kruse /// node and all llvm.lifetime.end to the merge node, which should be 157895f5d80SMichael Kruse /// conservatively correct. 15878ae52f0SPhilip Pfaffe static void removeLifetimeMarkers(Region *R) { 159895f5d80SMichael Kruse for (auto *BB : R->blocks()) { 160895f5d80SMichael Kruse auto InstIt = BB->begin(); 161895f5d80SMichael Kruse auto InstEnd = BB->end(); 162895f5d80SMichael Kruse 163895f5d80SMichael Kruse while (InstIt != InstEnd) { 164895f5d80SMichael Kruse auto NextIt = InstIt; 165895f5d80SMichael Kruse ++NextIt; 166895f5d80SMichael Kruse 167895f5d80SMichael Kruse if (auto *IT = dyn_cast<IntrinsicInst>(&*InstIt)) { 168895f5d80SMichael Kruse switch (IT->getIntrinsicID()) { 1699248fde5SEugene Zelenko case Intrinsic::lifetime_start: 1709248fde5SEugene Zelenko case Intrinsic::lifetime_end: 171895f5d80SMichael Kruse BB->getInstList().erase(InstIt); 172895f5d80SMichael Kruse break; 173895f5d80SMichael Kruse default: 174895f5d80SMichael Kruse break; 175895f5d80SMichael Kruse } 176895f5d80SMichael Kruse } 177895f5d80SMichael Kruse 178895f5d80SMichael Kruse InstIt = NextIt; 179895f5d80SMichael Kruse } 180895f5d80SMichael Kruse } 181895f5d80SMichael Kruse } 182895f5d80SMichael Kruse 18378ae52f0SPhilip Pfaffe static bool CodeGen(Scop &S, IslAstInfo &AI, LoopInfo &LI, DominatorTree &DT, 18478ae52f0SPhilip Pfaffe ScalarEvolution &SE, RegionInfo &RI) { 185*0e370cf1SMichael Kruse // Check whether IslAstInfo uses the same isl_ctx. Since -polly-codegen 186*0e370cf1SMichael Kruse // reports itself to preserve DependenceInfo and IslAstInfo, we might get 187*0e370cf1SMichael Kruse // those analysis that were computed by a different ScopInfo for a different 188*0e370cf1SMichael Kruse // Scop structure. When the ScopInfo/Scop object is freed, there is a high 189*0e370cf1SMichael Kruse // probability that the new ScopInfo/Scop object will be created at the same 190*0e370cf1SMichael Kruse // heap position with the same address. Comparing whether the Scop or ScopInfo 191*0e370cf1SMichael Kruse // address is the expected therefore is unreliable. 192*0e370cf1SMichael Kruse // Instead, we compare the address of the isl_ctx object. Both, DependenceInfo 193*0e370cf1SMichael Kruse // and IslAstInfo must hold a reference to the isl_ctx object to ensure it is 194*0e370cf1SMichael Kruse // not freed before the destruction of those analyses which might happen after 195*0e370cf1SMichael Kruse // the destruction of the Scop/ScopInfo they refer to. Hence, the isl_ctx 196*0e370cf1SMichael Kruse // will not be freed and its space not reused as long there is a 197*0e370cf1SMichael Kruse // DependenceInfo or IslAstInfo around. 198*0e370cf1SMichael Kruse IslAst &Ast = AI.getIslAst(); 199*0e370cf1SMichael Kruse if (Ast.getSharedIslCtx() != S.getSharedIslCtx()) { 200*0e370cf1SMichael Kruse DEBUG(dbgs() << "Got an IstAst for a different Scop/isl_ctx\n"); 201*0e370cf1SMichael Kruse return false; 202*0e370cf1SMichael Kruse } 203*0e370cf1SMichael Kruse 20409d30697STobias Grosser // Check if we created an isl_ast root node, otherwise exit. 205*0e370cf1SMichael Kruse isl_ast_node *AstRoot = Ast.getAst(); 20609d30697STobias Grosser if (!AstRoot) 20709d30697STobias Grosser return false; 20809d30697STobias Grosser 20906ed5292SMichael Kruse // Collect statistics. Do it before we modify the IR to avoid having it any 21006ed5292SMichael Kruse // influence on the result. 21106ed5292SMichael Kruse auto ScopStats = S.getStatistics(); 21206ed5292SMichael Kruse ScopsProcessed++; 21306ed5292SMichael Kruse 21478ae52f0SPhilip Pfaffe auto &DL = S.getFunction().getParent()->getDataLayout(); 21522370884SMichael Kruse Region *R = &S.getRegion(); 21622370884SMichael Kruse assert(!R->isTopLevelRegion() && "Top level regions are not supported"); 21709d30697STobias Grosser 218d78616f9STobias Grosser ScopAnnotator Annotator; 21909d30697STobias Grosser 22078ae52f0SPhilip Pfaffe simplifyRegion(R, &DT, &LI, &RI); 22122370884SMichael Kruse assert(R->isSimple()); 222ef74443cSJohannes Doerfert BasicBlock *EnteringBB = S.getEnteringBlock(); 22322370884SMichael Kruse assert(EnteringBB); 22409d30697STobias Grosser PollyIRBuilder Builder = createPollyIRBuilder(EnteringBB, Annotator); 22509d30697STobias Grosser 22609d30697STobias Grosser // Only build the run-time condition and parameters _after_ having 22709d30697STobias Grosser // introduced the conditional branch. This is important as the conditional 22809d30697STobias Grosser // branch will guard the original scop from new induction variables that 22909d30697STobias Grosser // the SCEVExpander may introduce while code generating the parameters and 23009d30697STobias Grosser // which may introduce scalar dependences that prevent us from correctly 23109d30697STobias Grosser // code generating this scop. 232256070d8SAndreas Simbuerger BBPair StartExitBlocks = 23303346c27SSiddharth Bhat std::get<0>(executeScopConditionally(S, Builder.getTrue(), DT, RI, LI)); 234256070d8SAndreas Simbuerger BasicBlock *StartBlock = std::get<0>(StartExitBlocks); 235dbb0ef8eSAndreas Simbuerger BasicBlock *ExitBlock = std::get<1>(StartExitBlocks); 236256070d8SAndreas Simbuerger 237895f5d80SMichael Kruse removeLifetimeMarkers(R); 238bfb6a968STobias Grosser auto *SplitBlock = StartBlock->getSinglePredecessor(); 23909e3697fSJohannes Doerfert 24078ae52f0SPhilip Pfaffe IslNodeBuilder NodeBuilder(Builder, Annotator, DL, LI, SE, DT, S, StartBlock); 241acf80064SEli Friedman 242214deb79SMichael Kruse // All arrays must have their base pointers known before 243214deb79SMichael Kruse // ScopAnnotator::buildAliasScopes. 244b738ffa8SMichael Kruse NodeBuilder.allocateNewArrays(StartExitBlocks); 245214deb79SMichael Kruse Annotator.buildAliasScopes(S); 246214deb79SMichael Kruse 24765371af2STobias Grosser if (PerfMonitoring) { 24807bee290SSiddharth Bhat PerfMonitor P(S, EnteringBB->getParent()->getParent()); 24965371af2STobias Grosser P.initialize(); 25065371af2STobias Grosser P.insertRegionStart(SplitBlock->getTerminator()); 25165371af2STobias Grosser 252dbb0ef8eSAndreas Simbuerger BasicBlock *MergeBlock = ExitBlock->getUniqueSuccessor(); 25365371af2STobias Grosser P.insertRegionEnd(MergeBlock->getTerminator()); 25465371af2STobias Grosser } 25565371af2STobias Grosser 25609e3697fSJohannes Doerfert // First generate code for the hoisted invariant loads and transitively the 25709e3697fSJohannes Doerfert // parameters they reference. Afterwards, for the remaining parameters that 25809e3697fSJohannes Doerfert // might reference the hoisted loads. Finally, build the runtime check 25909e3697fSJohannes Doerfert // that might reference both hoisted loads as well as parameters. 260c4898504SJohannes Doerfert // If the hoisting fails we have to bail and execute the original code. 26109d30697STobias Grosser Builder.SetInsertPoint(SplitBlock->getTerminator()); 262c4898504SJohannes Doerfert if (!NodeBuilder.preloadInvariantLoads()) { 263bfb6a968STobias Grosser // Patch the introduced branch condition to ensure that we always execute 264bfb6a968STobias Grosser // the original SCoP. 265c4898504SJohannes Doerfert auto *FalseI1 = Builder.getFalse(); 26637977076SJohannes Doerfert auto *SplitBBTerm = Builder.GetInsertBlock()->getTerminator(); 26737977076SJohannes Doerfert SplitBBTerm->setOperand(0, FalseI1); 2681dd6e37aSJohannes Doerfert 269bfb6a968STobias Grosser // Since the other branch is hence ignored we mark it as unreachable and 270bfb6a968STobias Grosser // adjust the dominator tree accordingly. 271bfb6a968STobias Grosser auto *ExitingBlock = StartBlock->getUniqueSuccessor(); 272bfb6a968STobias Grosser assert(ExitingBlock); 273bfb6a968STobias Grosser auto *MergeBlock = ExitingBlock->getUniqueSuccessor(); 274bfb6a968STobias Grosser assert(MergeBlock); 275bfb6a968STobias Grosser markBlockUnreachable(*StartBlock, Builder); 276bfb6a968STobias Grosser markBlockUnreachable(*ExitingBlock, Builder); 277ef74443cSJohannes Doerfert auto *ExitingBB = S.getExitingBlock(); 278bfb6a968STobias Grosser assert(ExitingBB); 27978ae52f0SPhilip Pfaffe DT.changeImmediateDominator(MergeBlock, ExitingBB); 28078ae52f0SPhilip Pfaffe DT.eraseNode(ExitingBlock); 281bfb6a968STobias Grosser 282bfb6a968STobias Grosser isl_ast_node_free(AstRoot); 2831dd6e37aSJohannes Doerfert } else { 2848ea1fc19STobias Grosser NodeBuilder.addParameters(S.getContext().release()); 28578ae52f0SPhilip Pfaffe Value *RTC = NodeBuilder.createRTC(AI.getRunCondition()); 286404a0f81SJohannes Doerfert 2873717aa5dSTobias Grosser Builder.GetInsertBlock()->getTerminator()->setOperand(0, RTC); 288b738ffa8SMichael Kruse 289b738ffa8SMichael Kruse // Explicitly set the insert point to the end of the block to avoid that a 290b738ffa8SMichael Kruse // split at the builder's current 291b738ffa8SMichael Kruse // insert position would move the malloc calls to the wrong BasicBlock. 292b738ffa8SMichael Kruse // Ideally we would just split the block during allocation of the new 293b738ffa8SMichael Kruse // arrays, but this would break the assumption that there are no blocks 294b738ffa8SMichael Kruse // between polly.start and polly.exiting (at this point). 295b738ffa8SMichael Kruse Builder.SetInsertPoint(StartBlock->getTerminator()); 2963717aa5dSTobias Grosser 2973717aa5dSTobias Grosser NodeBuilder.create(AstRoot); 2988ed5e599STobias Grosser NodeBuilder.finalize(); 29978ae52f0SPhilip Pfaffe fixRegionInfo(*EnteringBB->getParent(), *R->getParent(), RI); 30006ed5292SMichael Kruse 30106ed5292SMichael Kruse CodegenedScops++; 30206ed5292SMichael Kruse CodegenedAffineLoops += ScopStats.NumAffineLoops; 30306ed5292SMichael Kruse CodegenedBoxedLoops += ScopStats.NumBoxedLoops; 3041dd6e37aSJohannes Doerfert } 305ecff11dcSJohannes Doerfert 3066a6a671cSJohannes Doerfert Function *F = EnteringBB->getParent(); 30778ae52f0SPhilip Pfaffe verifyGeneratedFunction(S, *F, AI); 308a9dc5294SJohannes Doerfert for (auto *SubF : NodeBuilder.getParallelSubfunctions()) 30978ae52f0SPhilip Pfaffe verifyGeneratedFunction(S, *SubF, AI); 310652f7808STobias Grosser 3114c86a1d9SMichael Kruse // Mark the function such that we run additional cleanup passes on this 3124c86a1d9SMichael Kruse // function (e.g. mem2reg to rediscover phi nodes). 3134c86a1d9SMichael Kruse F->addFnAttr("polly-optimized"); 31409d30697STobias Grosser return true; 31509d30697STobias Grosser } 31609d30697STobias Grosser 3179248fde5SEugene Zelenko namespace { 3189248fde5SEugene Zelenko 31978ae52f0SPhilip Pfaffe class CodeGeneration : public ScopPass { 32078ae52f0SPhilip Pfaffe public: 32178ae52f0SPhilip Pfaffe static char ID; 32278ae52f0SPhilip Pfaffe 323a6d48f59SMichael Kruse /// The data layout used. 32478ae52f0SPhilip Pfaffe const DataLayout *DL; 32578ae52f0SPhilip Pfaffe 32678ae52f0SPhilip Pfaffe /// @name The analysis passes we need to generate code. 32778ae52f0SPhilip Pfaffe /// 32878ae52f0SPhilip Pfaffe ///{ 32978ae52f0SPhilip Pfaffe LoopInfo *LI; 33078ae52f0SPhilip Pfaffe IslAstInfo *AI; 33178ae52f0SPhilip Pfaffe DominatorTree *DT; 33278ae52f0SPhilip Pfaffe ScalarEvolution *SE; 33378ae52f0SPhilip Pfaffe RegionInfo *RI; 33478ae52f0SPhilip Pfaffe ///} 33578ae52f0SPhilip Pfaffe 3369248fde5SEugene Zelenko CodeGeneration() : ScopPass(ID) {} 3379248fde5SEugene Zelenko 33878ae52f0SPhilip Pfaffe /// Generate LLVM-IR for the SCoP @p S. 33978ae52f0SPhilip Pfaffe bool runOnScop(Scop &S) override { 34002ca346eSSingapuram Sanjay Srivallabh // Skip SCoPs in case they're already code-generated by PPCGCodeGeneration. 34102ca346eSSingapuram Sanjay Srivallabh if (S.isToBeSkipped()) 34202ca346eSSingapuram Sanjay Srivallabh return false; 34302ca346eSSingapuram Sanjay Srivallabh 34478ae52f0SPhilip Pfaffe AI = &getAnalysis<IslAstInfoWrapperPass>().getAI(); 34578ae52f0SPhilip Pfaffe LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 34678ae52f0SPhilip Pfaffe DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 34778ae52f0SPhilip Pfaffe SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); 34878ae52f0SPhilip Pfaffe DL = &S.getFunction().getParent()->getDataLayout(); 34978ae52f0SPhilip Pfaffe RI = &getAnalysis<RegionInfoPass>().getRegionInfo(); 35078ae52f0SPhilip Pfaffe return CodeGen(S, *AI, *LI, *DT, *SE, *RI); 35178ae52f0SPhilip Pfaffe } 35278ae52f0SPhilip Pfaffe 353c80d6979STobias Grosser /// Register all analyses and transformation required. 35409d30697STobias Grosser void getAnalysisUsage(AnalysisUsage &AU) const override { 355a4f447c2SMichael Kruse ScopPass::getAnalysisUsage(AU); 356a4f447c2SMichael Kruse 35709d30697STobias Grosser AU.addRequired<DominatorTreeWrapperPass>(); 3582b852e2eSPhilip Pfaffe AU.addRequired<IslAstInfoWrapperPass>(); 35909d30697STobias Grosser AU.addRequired<RegionInfoPass>(); 360c5bcf246STobias Grosser AU.addRequired<ScalarEvolutionWrapperPass>(); 3615cc87e3aSPhilip Pfaffe AU.addRequired<ScopDetectionWrapperPass>(); 36299191c78SJohannes Doerfert AU.addRequired<ScopInfoRegionPass>(); 36309d30697STobias Grosser AU.addRequired<LoopInfoWrapperPass>(); 36409d30697STobias Grosser 36509d30697STobias Grosser AU.addPreserved<DependenceInfo>(); 3662b852e2eSPhilip Pfaffe AU.addPreserved<IslAstInfoWrapperPass>(); 36709d30697STobias Grosser 36809d30697STobias Grosser // FIXME: We do not yet add regions for the newly generated code to the 36909d30697STobias Grosser // region tree. 37009d30697STobias Grosser } 37109d30697STobias Grosser }; 3729248fde5SEugene Zelenko 373522478d2STobias Grosser } // namespace 37409d30697STobias Grosser 3759248fde5SEugene Zelenko PreservedAnalyses CodeGenerationPass::run(Scop &S, ScopAnalysisManager &SAM, 3769248fde5SEugene Zelenko ScopStandardAnalysisResults &AR, 3779248fde5SEugene Zelenko SPMUpdater &U) { 37878ae52f0SPhilip Pfaffe auto &AI = SAM.getResult<IslAstAnalysis>(S, AR); 379f43e7c2eSPhilip Pfaffe if (CodeGen(S, AI, AR.LI, AR.DT, AR.SE, AR.RI)) { 380f43e7c2eSPhilip Pfaffe U.invalidateScop(S); 38178ae52f0SPhilip Pfaffe return PreservedAnalyses::none(); 382f43e7c2eSPhilip Pfaffe } 38378ae52f0SPhilip Pfaffe 38478ae52f0SPhilip Pfaffe return PreservedAnalyses::all(); 38578ae52f0SPhilip Pfaffe } 38678ae52f0SPhilip Pfaffe 38709d30697STobias Grosser char CodeGeneration::ID = 1; 38809d30697STobias Grosser 38909d30697STobias Grosser Pass *polly::createCodeGenerationPass() { return new CodeGeneration(); } 39009d30697STobias Grosser 39109d30697STobias Grosser INITIALIZE_PASS_BEGIN(CodeGeneration, "polly-codegen", 39209d30697STobias Grosser "Polly - Create LLVM-IR from SCoPs", false, false); 39309d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(DependenceInfo); 39409d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); 39509d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); 39609d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); 397c5bcf246STobias Grosser INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass); 3985cc87e3aSPhilip Pfaffe INITIALIZE_PASS_DEPENDENCY(ScopDetectionWrapperPass); 39909d30697STobias Grosser INITIALIZE_PASS_END(CodeGeneration, "polly-codegen", 40009d30697STobias Grosser "Polly - Create LLVM-IR from SCoPs", false, false) 401