19248fde5SEugene Zelenko //===- CodeGeneration.cpp - Code generate the Scops using ISL. ---------======//
209d30697STobias Grosser //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609d30697STobias Grosser //
709d30697STobias Grosser //===----------------------------------------------------------------------===//
809d30697STobias Grosser //
909d30697STobias Grosser // The CodeGeneration pass takes a Scop created by ScopInfo and translates it
1009d30697STobias Grosser // back to LLVM-IR using the ISL code generator.
1109d30697STobias Grosser //
12a6d48f59SMichael Kruse // The Scop describes the high level memory behavior of a control flow region.
1309d30697STobias Grosser // Transformation passes can update the schedule (execution order) of statements
1409d30697STobias Grosser // in the Scop. ISL is used to generate an abstract syntax tree that reflects
1509d30697STobias Grosser // the updated execution order. This clast is used to create new LLVM-IR that is
1609d30697STobias Grosser // computationally equivalent to the original control flow region, but executes
1709d30697STobias Grosser // its code in the new execution order defined by the changed schedule.
1809d30697STobias Grosser //
1909d30697STobias Grosser //===----------------------------------------------------------------------===//
2009d30697STobias Grosser
2178ae52f0SPhilip Pfaffe #include "polly/CodeGen/CodeGeneration.h"
229248fde5SEugene Zelenko #include "polly/CodeGen/IRBuilder.h"
2309d30697STobias Grosser #include "polly/CodeGen/IslAst.h"
245624d3c9STobias Grosser #include "polly/CodeGen/IslNodeBuilder.h"
2565371af2STobias Grosser #include "polly/CodeGen/PerfMonitor.h"
2609d30697STobias Grosser #include "polly/CodeGen/Utils.h"
2709d30697STobias Grosser #include "polly/DependenceInfo.h"
2809d30697STobias Grosser #include "polly/LinkAllPasses.h"
2958e58544STobias Grosser #include "polly/Options.h"
3009d30697STobias Grosser #include "polly/ScopInfo.h"
3109d30697STobias Grosser #include "polly/Support/ScopHelper.h"
329248fde5SEugene Zelenko #include "llvm/ADT/Statistic.h"
3378ae52f0SPhilip Pfaffe #include "llvm/Analysis/LoopInfo.h"
349248fde5SEugene Zelenko #include "llvm/Analysis/RegionInfo.h"
359248fde5SEugene Zelenko #include "llvm/IR/BasicBlock.h"
369248fde5SEugene Zelenko #include "llvm/IR/Dominators.h"
379248fde5SEugene Zelenko #include "llvm/IR/Function.h"
3878ae52f0SPhilip Pfaffe #include "llvm/IR/PassManager.h"
39c2bb0cbeSTobias Grosser #include "llvm/IR/Verifier.h"
4005da2fe5SReid Kleckner #include "llvm/InitializePasses.h"
41c2bb0cbeSTobias Grosser #include "llvm/Support/Debug.h"
429248fde5SEugene Zelenko #include "llvm/Support/ErrorHandling.h"
439248fde5SEugene Zelenko #include "llvm/Support/raw_ostream.h"
449248fde5SEugene Zelenko #include "isl/ast.h"
459248fde5SEugene Zelenko #include <cassert>
4609d30697STobias Grosser
4709d30697STobias Grosser using namespace llvm;
489248fde5SEugene Zelenko using namespace polly;
4909d30697STobias Grosser
5009d30697STobias Grosser #define DEBUG_TYPE "polly-codegen"
5109d30697STobias Grosser
5258e58544STobias Grosser static cl::opt<bool> Verify("polly-codegen-verify",
5358e58544STobias Grosser cl::desc("Verify the function generated by Polly"),
5436c7d79dSFangrui Song cl::Hidden, cl::cat(PollyCategory));
5558e58544STobias Grosser
567b9f5ca2SSiddharth Bhat bool polly::PerfMonitoring;
579248fde5SEugene Zelenko
587b9f5ca2SSiddharth Bhat static cl::opt<bool, true>
597b9f5ca2SSiddharth Bhat XPerfMonitoring("polly-codegen-perf-monitoring",
6065371af2STobias Grosser cl::desc("Add run-time performance monitoring"), cl::Hidden,
61*95a13425SFangrui Song cl::location(polly::PerfMonitoring),
62*95a13425SFangrui Song cl::cat(PollyCategory));
6365371af2STobias Grosser
6406ed5292SMichael Kruse STATISTIC(ScopsProcessed, "Number of SCoP processed");
6506ed5292SMichael Kruse STATISTIC(CodegenedScops, "Number of successfully generated SCoPs");
6606ed5292SMichael Kruse STATISTIC(CodegenedAffineLoops,
6706ed5292SMichael Kruse "Number of original affine loops in SCoPs that have been generated");
6806ed5292SMichael Kruse STATISTIC(CodegenedBoxedLoops,
6906ed5292SMichael Kruse "Number of original boxed loops in SCoPs that have been generated");
7006ed5292SMichael Kruse
7171dfb3ebSSiddharth Bhat namespace polly {
729248fde5SEugene Zelenko
7371dfb3ebSSiddharth Bhat /// Mark a basic block unreachable.
7471dfb3ebSSiddharth Bhat ///
7571dfb3ebSSiddharth Bhat /// Marks the basic block @p Block unreachable by equipping it with an
7671dfb3ebSSiddharth Bhat /// UnreachableInst.
markBlockUnreachable(BasicBlock & Block,PollyIRBuilder & Builder)7771dfb3ebSSiddharth Bhat void markBlockUnreachable(BasicBlock &Block, PollyIRBuilder &Builder) {
7871dfb3ebSSiddharth Bhat auto *OrigTerminator = Block.getTerminator();
7971dfb3ebSSiddharth Bhat Builder.SetInsertPoint(OrigTerminator);
8071dfb3ebSSiddharth Bhat Builder.CreateUnreachable();
8171dfb3ebSSiddharth Bhat OrigTerminator->eraseFromParent();
8271dfb3ebSSiddharth Bhat }
8371dfb3ebSSiddharth Bhat } // namespace polly
8471dfb3ebSSiddharth Bhat
verifyGeneratedFunction(Scop & S,Function & F,IslAstInfo & AI)8578ae52f0SPhilip Pfaffe static void verifyGeneratedFunction(Scop &S, Function &F, IslAstInfo &AI) {
86d439911fSTobias Grosser if (!Verify || !verifyFunction(F, &errs()))
8758e58544STobias Grosser return;
8809d30697STobias Grosser
89349506a9SNicola Zaghen LLVM_DEBUG({
9009d30697STobias Grosser errs() << "== ISL Codegen created an invalid function ==\n\n== The "
9109d30697STobias Grosser "SCoP ==\n";
92cd4c977bSMichael Kruse errs() << S;
9309d30697STobias Grosser errs() << "\n== The isl AST ==\n";
9478ae52f0SPhilip Pfaffe AI.print(errs());
9509d30697STobias Grosser errs() << "\n== The invalid function ==\n";
9609d30697STobias Grosser F.print(errs());
9709d30697STobias Grosser });
9809d30697STobias Grosser
9958e58544STobias Grosser llvm_unreachable("Polly generated function could not be verified. Add "
10058e58544STobias Grosser "-polly-codegen-verify=false to disable this assertion.");
10109d30697STobias Grosser }
10209d30697STobias Grosser
1039c483c58SMichael Kruse // CodeGeneration adds a lot of BBs without updating the RegionInfo
1049c483c58SMichael Kruse // We make all created BBs belong to the scop's parent region without any
1059c483c58SMichael Kruse // nested structure to keep the RegionInfo verifier happy.
fixRegionInfo(Function & F,Region & ParentRegion,RegionInfo & RI)10678ae52f0SPhilip Pfaffe static void fixRegionInfo(Function &F, Region &ParentRegion, RegionInfo &RI) {
10778ae52f0SPhilip Pfaffe for (BasicBlock &BB : F) {
10878ae52f0SPhilip Pfaffe if (RI.getRegionFor(&BB))
1099c483c58SMichael Kruse continue;
1109c483c58SMichael Kruse
11178ae52f0SPhilip Pfaffe RI.setRegionFor(&BB, &ParentRegion);
1129c483c58SMichael Kruse }
1139c483c58SMichael Kruse }
1149c483c58SMichael Kruse
115895f5d80SMichael Kruse /// Remove all lifetime markers (llvm.lifetime.start, llvm.lifetime.end) from
116895f5d80SMichael Kruse /// @R.
117895f5d80SMichael Kruse ///
118895f5d80SMichael Kruse /// CodeGeneration does not copy lifetime markers into the optimized SCoP,
119895f5d80SMichael Kruse /// which would leave the them only in the original path. This can transform
120895f5d80SMichael Kruse /// code such as
121895f5d80SMichael Kruse ///
122895f5d80SMichael Kruse /// llvm.lifetime.start(%p)
123895f5d80SMichael Kruse /// llvm.lifetime.end(%p)
124895f5d80SMichael Kruse ///
125895f5d80SMichael Kruse /// into
126895f5d80SMichael Kruse ///
127895f5d80SMichael Kruse /// if (RTC) {
128895f5d80SMichael Kruse /// // generated code
129895f5d80SMichael Kruse /// } else {
130895f5d80SMichael Kruse /// // original code
131895f5d80SMichael Kruse /// llvm.lifetime.start(%p)
132895f5d80SMichael Kruse /// }
133895f5d80SMichael Kruse /// llvm.lifetime.end(%p)
134895f5d80SMichael Kruse ///
135895f5d80SMichael Kruse /// The current StackColoring algorithm cannot handle if some, but not all,
136895f5d80SMichael Kruse /// paths from the end marker to the entry block cross the start marker. Same
137895f5d80SMichael Kruse /// for start markers that do not always cross the end markers. We avoid any
138895f5d80SMichael Kruse /// issues by removing all lifetime markers, even from the original code.
139895f5d80SMichael Kruse ///
140895f5d80SMichael Kruse /// A better solution could be to hoist all llvm.lifetime.start to the split
141895f5d80SMichael Kruse /// node and all llvm.lifetime.end to the merge node, which should be
142895f5d80SMichael Kruse /// conservatively correct.
removeLifetimeMarkers(Region * R)14378ae52f0SPhilip Pfaffe static void removeLifetimeMarkers(Region *R) {
144895f5d80SMichael Kruse for (auto *BB : R->blocks()) {
145895f5d80SMichael Kruse auto InstIt = BB->begin();
146895f5d80SMichael Kruse auto InstEnd = BB->end();
147895f5d80SMichael Kruse
148895f5d80SMichael Kruse while (InstIt != InstEnd) {
149895f5d80SMichael Kruse auto NextIt = InstIt;
150895f5d80SMichael Kruse ++NextIt;
151895f5d80SMichael Kruse
152895f5d80SMichael Kruse if (auto *IT = dyn_cast<IntrinsicInst>(&*InstIt)) {
153895f5d80SMichael Kruse switch (IT->getIntrinsicID()) {
1549248fde5SEugene Zelenko case Intrinsic::lifetime_start:
1559248fde5SEugene Zelenko case Intrinsic::lifetime_end:
156895f5d80SMichael Kruse BB->getInstList().erase(InstIt);
157895f5d80SMichael Kruse break;
158895f5d80SMichael Kruse default:
159895f5d80SMichael Kruse break;
160895f5d80SMichael Kruse }
161895f5d80SMichael Kruse }
162895f5d80SMichael Kruse
163895f5d80SMichael Kruse InstIt = NextIt;
164895f5d80SMichael Kruse }
165895f5d80SMichael Kruse }
166895f5d80SMichael Kruse }
167895f5d80SMichael Kruse
generateCode(Scop & S,IslAstInfo & AI,LoopInfo & LI,DominatorTree & DT,ScalarEvolution & SE,RegionInfo & RI)1685eeaac22SMichael Kruse static bool generateCode(Scop &S, IslAstInfo &AI, LoopInfo &LI,
1695eeaac22SMichael Kruse DominatorTree &DT, ScalarEvolution &SE,
1705eeaac22SMichael Kruse RegionInfo &RI) {
1710e370cf1SMichael Kruse // Check whether IslAstInfo uses the same isl_ctx. Since -polly-codegen
1720e370cf1SMichael Kruse // reports itself to preserve DependenceInfo and IslAstInfo, we might get
1730e370cf1SMichael Kruse // those analysis that were computed by a different ScopInfo for a different
1740e370cf1SMichael Kruse // Scop structure. When the ScopInfo/Scop object is freed, there is a high
1750e370cf1SMichael Kruse // probability that the new ScopInfo/Scop object will be created at the same
1760e370cf1SMichael Kruse // heap position with the same address. Comparing whether the Scop or ScopInfo
1770e370cf1SMichael Kruse // address is the expected therefore is unreliable.
1780e370cf1SMichael Kruse // Instead, we compare the address of the isl_ctx object. Both, DependenceInfo
1790e370cf1SMichael Kruse // and IslAstInfo must hold a reference to the isl_ctx object to ensure it is
1800e370cf1SMichael Kruse // not freed before the destruction of those analyses which might happen after
1810e370cf1SMichael Kruse // the destruction of the Scop/ScopInfo they refer to. Hence, the isl_ctx
1820e370cf1SMichael Kruse // will not be freed and its space not reused as long there is a
1830e370cf1SMichael Kruse // DependenceInfo or IslAstInfo around.
1840e370cf1SMichael Kruse IslAst &Ast = AI.getIslAst();
1850e370cf1SMichael Kruse if (Ast.getSharedIslCtx() != S.getSharedIslCtx()) {
186349506a9SNicola Zaghen LLVM_DEBUG(dbgs() << "Got an IstAst for a different Scop/isl_ctx\n");
1870e370cf1SMichael Kruse return false;
1880e370cf1SMichael Kruse }
1890e370cf1SMichael Kruse
19009d30697STobias Grosser // Check if we created an isl_ast root node, otherwise exit.
1914170d6cdSpatacca isl::ast_node AstRoot = Ast.getAst();
1924170d6cdSpatacca if (AstRoot.is_null())
19309d30697STobias Grosser return false;
19409d30697STobias Grosser
19506ed5292SMichael Kruse // Collect statistics. Do it before we modify the IR to avoid having it any
19606ed5292SMichael Kruse // influence on the result.
19706ed5292SMichael Kruse auto ScopStats = S.getStatistics();
19806ed5292SMichael Kruse ScopsProcessed++;
19906ed5292SMichael Kruse
20078ae52f0SPhilip Pfaffe auto &DL = S.getFunction().getParent()->getDataLayout();
20122370884SMichael Kruse Region *R = &S.getRegion();
20222370884SMichael Kruse assert(!R->isTopLevelRegion() && "Top level regions are not supported");
20309d30697STobias Grosser
204d78616f9STobias Grosser ScopAnnotator Annotator;
20509d30697STobias Grosser
20678ae52f0SPhilip Pfaffe simplifyRegion(R, &DT, &LI, &RI);
20722370884SMichael Kruse assert(R->isSimple());
208ef74443cSJohannes Doerfert BasicBlock *EnteringBB = S.getEnteringBlock();
20922370884SMichael Kruse assert(EnteringBB);
21055cfb1fbSNikita Popov PollyIRBuilder Builder(EnteringBB->getContext(), ConstantFolder(),
21155cfb1fbSNikita Popov IRInserter(Annotator));
21255cfb1fbSNikita Popov Builder.SetInsertPoint(EnteringBB->getTerminator());
21309d30697STobias Grosser
21409d30697STobias Grosser // Only build the run-time condition and parameters _after_ having
21509d30697STobias Grosser // introduced the conditional branch. This is important as the conditional
21609d30697STobias Grosser // branch will guard the original scop from new induction variables that
21709d30697STobias Grosser // the SCEVExpander may introduce while code generating the parameters and
21809d30697STobias Grosser // which may introduce scalar dependences that prevent us from correctly
21909d30697STobias Grosser // code generating this scop.
220256070d8SAndreas Simbuerger BBPair StartExitBlocks =
22103346c27SSiddharth Bhat std::get<0>(executeScopConditionally(S, Builder.getTrue(), DT, RI, LI));
222256070d8SAndreas Simbuerger BasicBlock *StartBlock = std::get<0>(StartExitBlocks);
223dbb0ef8eSAndreas Simbuerger BasicBlock *ExitBlock = std::get<1>(StartExitBlocks);
224256070d8SAndreas Simbuerger
225895f5d80SMichael Kruse removeLifetimeMarkers(R);
226bfb6a968STobias Grosser auto *SplitBlock = StartBlock->getSinglePredecessor();
22709e3697fSJohannes Doerfert
22878ae52f0SPhilip Pfaffe IslNodeBuilder NodeBuilder(Builder, Annotator, DL, LI, SE, DT, S, StartBlock);
229acf80064SEli Friedman
230214deb79SMichael Kruse // All arrays must have their base pointers known before
231214deb79SMichael Kruse // ScopAnnotator::buildAliasScopes.
232b738ffa8SMichael Kruse NodeBuilder.allocateNewArrays(StartExitBlocks);
233214deb79SMichael Kruse Annotator.buildAliasScopes(S);
234214deb79SMichael Kruse
23565371af2STobias Grosser if (PerfMonitoring) {
23607bee290SSiddharth Bhat PerfMonitor P(S, EnteringBB->getParent()->getParent());
23765371af2STobias Grosser P.initialize();
23865371af2STobias Grosser P.insertRegionStart(SplitBlock->getTerminator());
23965371af2STobias Grosser
240dbb0ef8eSAndreas Simbuerger BasicBlock *MergeBlock = ExitBlock->getUniqueSuccessor();
24165371af2STobias Grosser P.insertRegionEnd(MergeBlock->getTerminator());
24265371af2STobias Grosser }
24365371af2STobias Grosser
24409e3697fSJohannes Doerfert // First generate code for the hoisted invariant loads and transitively the
24509e3697fSJohannes Doerfert // parameters they reference. Afterwards, for the remaining parameters that
24609e3697fSJohannes Doerfert // might reference the hoisted loads. Finally, build the runtime check
24709e3697fSJohannes Doerfert // that might reference both hoisted loads as well as parameters.
248c4898504SJohannes Doerfert // If the hoisting fails we have to bail and execute the original code.
24909d30697STobias Grosser Builder.SetInsertPoint(SplitBlock->getTerminator());
250c4898504SJohannes Doerfert if (!NodeBuilder.preloadInvariantLoads()) {
251bfb6a968STobias Grosser // Patch the introduced branch condition to ensure that we always execute
252bfb6a968STobias Grosser // the original SCoP.
253c4898504SJohannes Doerfert auto *FalseI1 = Builder.getFalse();
25437977076SJohannes Doerfert auto *SplitBBTerm = Builder.GetInsertBlock()->getTerminator();
25537977076SJohannes Doerfert SplitBBTerm->setOperand(0, FalseI1);
2561dd6e37aSJohannes Doerfert
257bfb6a968STobias Grosser // Since the other branch is hence ignored we mark it as unreachable and
258bfb6a968STobias Grosser // adjust the dominator tree accordingly.
259bfb6a968STobias Grosser auto *ExitingBlock = StartBlock->getUniqueSuccessor();
260bfb6a968STobias Grosser assert(ExitingBlock);
261bfb6a968STobias Grosser auto *MergeBlock = ExitingBlock->getUniqueSuccessor();
262bfb6a968STobias Grosser assert(MergeBlock);
263bfb6a968STobias Grosser markBlockUnreachable(*StartBlock, Builder);
264bfb6a968STobias Grosser markBlockUnreachable(*ExitingBlock, Builder);
265ef74443cSJohannes Doerfert auto *ExitingBB = S.getExitingBlock();
266bfb6a968STobias Grosser assert(ExitingBB);
26778ae52f0SPhilip Pfaffe DT.changeImmediateDominator(MergeBlock, ExitingBB);
26878ae52f0SPhilip Pfaffe DT.eraseNode(ExitingBlock);
2691dd6e37aSJohannes Doerfert } else {
2708ea1fc19STobias Grosser NodeBuilder.addParameters(S.getContext().release());
2714170d6cdSpatacca Value *RTC = NodeBuilder.createRTC(AI.getRunCondition().release());
272404a0f81SJohannes Doerfert
2733717aa5dSTobias Grosser Builder.GetInsertBlock()->getTerminator()->setOperand(0, RTC);
274b738ffa8SMichael Kruse
275b738ffa8SMichael Kruse // Explicitly set the insert point to the end of the block to avoid that a
276b738ffa8SMichael Kruse // split at the builder's current
277b738ffa8SMichael Kruse // insert position would move the malloc calls to the wrong BasicBlock.
278b738ffa8SMichael Kruse // Ideally we would just split the block during allocation of the new
279b738ffa8SMichael Kruse // arrays, but this would break the assumption that there are no blocks
280b738ffa8SMichael Kruse // between polly.start and polly.exiting (at this point).
281b738ffa8SMichael Kruse Builder.SetInsertPoint(StartBlock->getTerminator());
2823717aa5dSTobias Grosser
2834170d6cdSpatacca NodeBuilder.create(AstRoot.release());
2848ed5e599STobias Grosser NodeBuilder.finalize();
28578ae52f0SPhilip Pfaffe fixRegionInfo(*EnteringBB->getParent(), *R->getParent(), RI);
28606ed5292SMichael Kruse
28706ed5292SMichael Kruse CodegenedScops++;
28806ed5292SMichael Kruse CodegenedAffineLoops += ScopStats.NumAffineLoops;
28906ed5292SMichael Kruse CodegenedBoxedLoops += ScopStats.NumBoxedLoops;
2901dd6e37aSJohannes Doerfert }
291ecff11dcSJohannes Doerfert
2926a6a671cSJohannes Doerfert Function *F = EnteringBB->getParent();
29378ae52f0SPhilip Pfaffe verifyGeneratedFunction(S, *F, AI);
294a9dc5294SJohannes Doerfert for (auto *SubF : NodeBuilder.getParallelSubfunctions())
29578ae52f0SPhilip Pfaffe verifyGeneratedFunction(S, *SubF, AI);
296652f7808STobias Grosser
2974c86a1d9SMichael Kruse // Mark the function such that we run additional cleanup passes on this
2984c86a1d9SMichael Kruse // function (e.g. mem2reg to rediscover phi nodes).
2994c86a1d9SMichael Kruse F->addFnAttr("polly-optimized");
30009d30697STobias Grosser return true;
30109d30697STobias Grosser }
30209d30697STobias Grosser
3039248fde5SEugene Zelenko namespace {
3049248fde5SEugene Zelenko
305bd93df93SMichael Kruse class CodeGeneration final : public ScopPass {
30678ae52f0SPhilip Pfaffe public:
30778ae52f0SPhilip Pfaffe static char ID;
30878ae52f0SPhilip Pfaffe
309a6d48f59SMichael Kruse /// The data layout used.
31078ae52f0SPhilip Pfaffe const DataLayout *DL;
31178ae52f0SPhilip Pfaffe
31278ae52f0SPhilip Pfaffe /// @name The analysis passes we need to generate code.
31378ae52f0SPhilip Pfaffe ///
31478ae52f0SPhilip Pfaffe ///{
31578ae52f0SPhilip Pfaffe LoopInfo *LI;
31678ae52f0SPhilip Pfaffe IslAstInfo *AI;
31778ae52f0SPhilip Pfaffe DominatorTree *DT;
31878ae52f0SPhilip Pfaffe ScalarEvolution *SE;
31978ae52f0SPhilip Pfaffe RegionInfo *RI;
32078ae52f0SPhilip Pfaffe ///}
32178ae52f0SPhilip Pfaffe
CodeGeneration()3229248fde5SEugene Zelenko CodeGeneration() : ScopPass(ID) {}
3239248fde5SEugene Zelenko
32478ae52f0SPhilip Pfaffe /// Generate LLVM-IR for the SCoP @p S.
runOnScop(Scop & S)32578ae52f0SPhilip Pfaffe bool runOnScop(Scop &S) override {
32602ca346eSSingapuram Sanjay Srivallabh // Skip SCoPs in case they're already code-generated by PPCGCodeGeneration.
32702ca346eSSingapuram Sanjay Srivallabh if (S.isToBeSkipped())
32802ca346eSSingapuram Sanjay Srivallabh return false;
32902ca346eSSingapuram Sanjay Srivallabh
33078ae52f0SPhilip Pfaffe AI = &getAnalysis<IslAstInfoWrapperPass>().getAI();
33178ae52f0SPhilip Pfaffe LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
33278ae52f0SPhilip Pfaffe DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
33378ae52f0SPhilip Pfaffe SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
33478ae52f0SPhilip Pfaffe DL = &S.getFunction().getParent()->getDataLayout();
33578ae52f0SPhilip Pfaffe RI = &getAnalysis<RegionInfoPass>().getRegionInfo();
3365eeaac22SMichael Kruse return generateCode(S, *AI, *LI, *DT, *SE, *RI);
33778ae52f0SPhilip Pfaffe }
33878ae52f0SPhilip Pfaffe
339c80d6979STobias Grosser /// Register all analyses and transformation required.
getAnalysisUsage(AnalysisUsage & AU) const34009d30697STobias Grosser void getAnalysisUsage(AnalysisUsage &AU) const override {
341a4f447c2SMichael Kruse ScopPass::getAnalysisUsage(AU);
342a4f447c2SMichael Kruse
34309d30697STobias Grosser AU.addRequired<DominatorTreeWrapperPass>();
3442b852e2eSPhilip Pfaffe AU.addRequired<IslAstInfoWrapperPass>();
34509d30697STobias Grosser AU.addRequired<RegionInfoPass>();
346c5bcf246STobias Grosser AU.addRequired<ScalarEvolutionWrapperPass>();
3475cc87e3aSPhilip Pfaffe AU.addRequired<ScopDetectionWrapperPass>();
34899191c78SJohannes Doerfert AU.addRequired<ScopInfoRegionPass>();
34909d30697STobias Grosser AU.addRequired<LoopInfoWrapperPass>();
35009d30697STobias Grosser
35109d30697STobias Grosser AU.addPreserved<DependenceInfo>();
3522b852e2eSPhilip Pfaffe AU.addPreserved<IslAstInfoWrapperPass>();
35309d30697STobias Grosser
35409d30697STobias Grosser // FIXME: We do not yet add regions for the newly generated code to the
35509d30697STobias Grosser // region tree.
35609d30697STobias Grosser }
35709d30697STobias Grosser };
358522478d2STobias Grosser } // namespace
35909d30697STobias Grosser
run(Scop & S,ScopAnalysisManager & SAM,ScopStandardAnalysisResults & AR,SPMUpdater & U)3609248fde5SEugene Zelenko PreservedAnalyses CodeGenerationPass::run(Scop &S, ScopAnalysisManager &SAM,
3619248fde5SEugene Zelenko ScopStandardAnalysisResults &AR,
3629248fde5SEugene Zelenko SPMUpdater &U) {
36378ae52f0SPhilip Pfaffe auto &AI = SAM.getResult<IslAstAnalysis>(S, AR);
3645eeaac22SMichael Kruse if (generateCode(S, AI, AR.LI, AR.DT, AR.SE, AR.RI)) {
365f43e7c2eSPhilip Pfaffe U.invalidateScop(S);
36678ae52f0SPhilip Pfaffe return PreservedAnalyses::none();
367f43e7c2eSPhilip Pfaffe }
36878ae52f0SPhilip Pfaffe
36978ae52f0SPhilip Pfaffe return PreservedAnalyses::all();
37078ae52f0SPhilip Pfaffe }
37178ae52f0SPhilip Pfaffe
37209d30697STobias Grosser char CodeGeneration::ID = 1;
37309d30697STobias Grosser
createCodeGenerationPass()37409d30697STobias Grosser Pass *polly::createCodeGenerationPass() { return new CodeGeneration(); }
37509d30697STobias Grosser
37609d30697STobias Grosser INITIALIZE_PASS_BEGIN(CodeGeneration, "polly-codegen",
37709d30697STobias Grosser "Polly - Create LLVM-IR from SCoPs", false, false);
37809d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
37909d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
38009d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
38109d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
382c5bcf246STobias Grosser INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass);
3835cc87e3aSPhilip Pfaffe INITIALIZE_PASS_DEPENDENCY(ScopDetectionWrapperPass);
38409d30697STobias Grosser INITIALIZE_PASS_END(CodeGeneration, "polly-codegen",
38509d30697STobias Grosser "Polly - Create LLVM-IR from SCoPs", false, false)
386