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"
2465371af2STobias Grosser #include "polly/CodeGen/PerfMonitor.h"
2509d30697STobias Grosser #include "polly/CodeGen/Utils.h"
2609d30697STobias Grosser #include "polly/DependenceInfo.h"
2709d30697STobias Grosser #include "polly/LinkAllPasses.h"
2858e58544STobias Grosser #include "polly/Options.h"
2909d30697STobias Grosser #include "polly/ScopInfo.h"
3009d30697STobias Grosser #include "polly/Support/ScopHelper.h"
3166ef16b2SChandler Carruth #include "llvm/Analysis/AliasAnalysis.h"
3266ef16b2SChandler Carruth #include "llvm/Analysis/BasicAliasAnalysis.h"
3366ef16b2SChandler Carruth #include "llvm/Analysis/GlobalsModRef.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"),
46f1372217STobias Grosser                             cl::Hidden, cl::init(false), cl::ZeroOrMore,
4758e58544STobias Grosser                             cl::cat(PollyCategory));
4858e58544STobias Grosser 
4965371af2STobias Grosser static cl::opt<bool>
5065371af2STobias Grosser     PerfMonitoring("polly-codegen-perf-monitoring",
5165371af2STobias Grosser                    cl::desc("Add run-time performance monitoring"), cl::Hidden,
5265371af2STobias Grosser                    cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
5365371af2STobias Grosser 
5409d30697STobias Grosser namespace {
5509d30697STobias Grosser class CodeGeneration : public ScopPass {
5609d30697STobias Grosser public:
5709d30697STobias Grosser   static char ID;
5809d30697STobias Grosser 
5909d30697STobias Grosser   CodeGeneration() : ScopPass(ID) {}
6009d30697STobias Grosser 
61c80d6979STobias Grosser   /// The datalayout used
6209d30697STobias Grosser   const DataLayout *DL;
6309d30697STobias Grosser 
6409d30697STobias Grosser   /// @name The analysis passes we need to generate code.
6509d30697STobias Grosser   ///
6609d30697STobias Grosser   ///{
6709d30697STobias Grosser   LoopInfo *LI;
6809d30697STobias Grosser   IslAstInfo *AI;
6909d30697STobias Grosser   DominatorTree *DT;
7009d30697STobias Grosser   ScalarEvolution *SE;
7122370884SMichael Kruse   RegionInfo *RI;
7209d30697STobias Grosser   ///}
7309d30697STobias Grosser 
7458e58544STobias Grosser   void verifyGeneratedFunction(Scop &S, Function &F) {
75d439911fSTobias Grosser     if (!Verify || !verifyFunction(F, &errs()))
7658e58544STobias Grosser       return;
7709d30697STobias Grosser 
7809d30697STobias Grosser     DEBUG({
7909d30697STobias Grosser       errs() << "== ISL Codegen created an invalid function ==\n\n== The "
8009d30697STobias Grosser                 "SCoP ==\n";
8109d30697STobias Grosser       S.print(errs());
8209d30697STobias Grosser       errs() << "\n== The isl AST ==\n";
83*2b852e2eSPhilip Pfaffe       AI->print(errs());
8409d30697STobias Grosser       errs() << "\n== The invalid function ==\n";
8509d30697STobias Grosser       F.print(errs());
8609d30697STobias Grosser     });
8709d30697STobias Grosser 
8858e58544STobias Grosser     llvm_unreachable("Polly generated function could not be verified. Add "
8958e58544STobias Grosser                      "-polly-codegen-verify=false to disable this assertion.");
9009d30697STobias Grosser   }
9109d30697STobias Grosser 
929c483c58SMichael Kruse   // CodeGeneration adds a lot of BBs without updating the RegionInfo
939c483c58SMichael Kruse   // We make all created BBs belong to the scop's parent region without any
949c483c58SMichael Kruse   // nested structure to keep the RegionInfo verifier happy.
959c483c58SMichael Kruse   void fixRegionInfo(Function *F, Region *ParentRegion) {
969c483c58SMichael Kruse     for (BasicBlock &BB : *F) {
979c483c58SMichael Kruse       if (RI->getRegionFor(&BB))
989c483c58SMichael Kruse         continue;
999c483c58SMichael Kruse 
1009c483c58SMichael Kruse       RI->setRegionFor(&BB, ParentRegion);
1019c483c58SMichael Kruse     }
1029c483c58SMichael Kruse   }
1039c483c58SMichael Kruse 
104c80d6979STobias Grosser   /// Mark a basic block unreachable.
105bfb6a968STobias Grosser   ///
106bfb6a968STobias Grosser   /// Marks the basic block @p Block unreachable by equipping it with an
107bfb6a968STobias Grosser   /// UnreachableInst.
108bfb6a968STobias Grosser   void markBlockUnreachable(BasicBlock &Block, PollyIRBuilder &Builder) {
109bfb6a968STobias Grosser     auto *OrigTerminator = Block.getTerminator();
110bfb6a968STobias Grosser     Builder.SetInsertPoint(OrigTerminator);
111bfb6a968STobias Grosser     Builder.CreateUnreachable();
112bfb6a968STobias Grosser     OrigTerminator->eraseFromParent();
113bfb6a968STobias Grosser   }
114bfb6a968STobias Grosser 
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.
143895f5d80SMichael Kruse   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()) {
154895f5d80SMichael Kruse           case llvm::Intrinsic::lifetime_start:
155895f5d80SMichael Kruse           case llvm::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 
168c80d6979STobias Grosser   /// Generate LLVM-IR for the SCoP @p S.
16909d30697STobias Grosser   bool runOnScop(Scop &S) override {
170*2b852e2eSPhilip Pfaffe     AI = &getAnalysis<IslAstInfoWrapperPass>().getAI();
17109d30697STobias Grosser 
17209d30697STobias Grosser     // Check if we created an isl_ast root node, otherwise exit.
17309d30697STobias Grosser     isl_ast_node *AstRoot = AI->getAst();
17409d30697STobias Grosser     if (!AstRoot)
17509d30697STobias Grosser       return false;
17609d30697STobias Grosser 
17709d30697STobias Grosser     LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
17809d30697STobias Grosser     DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
179c5bcf246STobias Grosser     SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
1803f52e354SJohannes Doerfert     DL = &S.getFunction().getParent()->getDataLayout();
18122370884SMichael Kruse     RI = &getAnalysis<RegionInfoPass>().getRegionInfo();
18222370884SMichael Kruse     Region *R = &S.getRegion();
18322370884SMichael Kruse     assert(!R->isTopLevelRegion() && "Top level regions are not supported");
18409d30697STobias Grosser 
185d78616f9STobias Grosser     ScopAnnotator Annotator;
18609d30697STobias Grosser     Annotator.buildAliasScopes(S);
18709d30697STobias Grosser 
18822370884SMichael Kruse     simplifyRegion(R, DT, LI, RI);
18922370884SMichael Kruse     assert(R->isSimple());
190ef74443cSJohannes Doerfert     BasicBlock *EnteringBB = S.getEnteringBlock();
19122370884SMichael Kruse     assert(EnteringBB);
19209d30697STobias Grosser     PollyIRBuilder Builder = createPollyIRBuilder(EnteringBB, Annotator);
19309d30697STobias Grosser 
19409d30697STobias Grosser     // Only build the run-time condition and parameters _after_ having
19509d30697STobias Grosser     // introduced the conditional branch. This is important as the conditional
19609d30697STobias Grosser     // branch will guard the original scop from new induction variables that
19709d30697STobias Grosser     // the SCEVExpander may introduce while code generating the parameters and
19809d30697STobias Grosser     // which may introduce scalar dependences that prevent us from correctly
19909d30697STobias Grosser     // code generating this scop.
20009d30697STobias Grosser     BasicBlock *StartBlock =
2012d950f36SPhilip Pfaffe         executeScopConditionally(S, Builder.getTrue(), *DT, *RI, *LI);
202895f5d80SMichael Kruse     removeLifetimeMarkers(R);
203bfb6a968STobias Grosser     auto *SplitBlock = StartBlock->getSinglePredecessor();
20409e3697fSJohannes Doerfert 
2052d950f36SPhilip Pfaffe     IslNodeBuilder NodeBuilder(Builder, Annotator, *DL, *LI, *SE, *DT, S,
206acf80064SEli Friedman                                StartBlock);
207acf80064SEli Friedman 
20865371af2STobias Grosser     if (PerfMonitoring) {
20965371af2STobias Grosser       PerfMonitor P(EnteringBB->getParent()->getParent());
21065371af2STobias Grosser       P.initialize();
21165371af2STobias Grosser       P.insertRegionStart(SplitBlock->getTerminator());
21265371af2STobias Grosser 
21365371af2STobias Grosser       BasicBlock *MergeBlock = SplitBlock->getTerminator()
21465371af2STobias Grosser                                    ->getSuccessor(0)
21565371af2STobias Grosser                                    ->getUniqueSuccessor()
21665371af2STobias Grosser                                    ->getUniqueSuccessor();
21765371af2STobias Grosser       P.insertRegionEnd(MergeBlock->getTerminator());
21865371af2STobias Grosser     }
21965371af2STobias Grosser 
22009e3697fSJohannes Doerfert     // First generate code for the hoisted invariant loads and transitively the
22109e3697fSJohannes Doerfert     // parameters they reference. Afterwards, for the remaining parameters that
22209e3697fSJohannes Doerfert     // might reference the hoisted loads. Finally, build the runtime check
22309e3697fSJohannes Doerfert     // that might reference both hoisted loads as well as parameters.
224c4898504SJohannes Doerfert     // If the hoisting fails we have to bail and execute the original code.
22509d30697STobias Grosser     Builder.SetInsertPoint(SplitBlock->getTerminator());
226c4898504SJohannes Doerfert     if (!NodeBuilder.preloadInvariantLoads()) {
2271dd6e37aSJohannes Doerfert 
228bfb6a968STobias Grosser       // Patch the introduced branch condition to ensure that we always execute
229bfb6a968STobias Grosser       // the original SCoP.
230c4898504SJohannes Doerfert       auto *FalseI1 = Builder.getFalse();
23137977076SJohannes Doerfert       auto *SplitBBTerm = Builder.GetInsertBlock()->getTerminator();
23237977076SJohannes Doerfert       SplitBBTerm->setOperand(0, FalseI1);
2331dd6e37aSJohannes Doerfert 
234bfb6a968STobias Grosser       // Since the other branch is hence ignored we mark it as unreachable and
235bfb6a968STobias Grosser       // adjust the dominator tree accordingly.
236bfb6a968STobias Grosser       auto *ExitingBlock = StartBlock->getUniqueSuccessor();
237bfb6a968STobias Grosser       assert(ExitingBlock);
238bfb6a968STobias Grosser       auto *MergeBlock = ExitingBlock->getUniqueSuccessor();
239bfb6a968STobias Grosser       assert(MergeBlock);
240bfb6a968STobias Grosser       markBlockUnreachable(*StartBlock, Builder);
241bfb6a968STobias Grosser       markBlockUnreachable(*ExitingBlock, Builder);
242ef74443cSJohannes Doerfert       auto *ExitingBB = S.getExitingBlock();
243bfb6a968STobias Grosser       assert(ExitingBB);
244bfb6a968STobias Grosser       DT->changeImmediateDominator(MergeBlock, ExitingBB);
245bfb6a968STobias Grosser       DT->eraseNode(ExitingBlock);
246bfb6a968STobias Grosser 
247bfb6a968STobias Grosser       isl_ast_node_free(AstRoot);
2481dd6e37aSJohannes Doerfert     } else {
249d7754a12SRoman Gareev       NodeBuilder.allocateNewArrays();
25009e3697fSJohannes Doerfert       NodeBuilder.addParameters(S.getContext());
2510aa29532STobias Grosser       Value *RTC = NodeBuilder.createRTC(AI->getRunCondition());
252404a0f81SJohannes Doerfert 
2533717aa5dSTobias Grosser       Builder.GetInsertBlock()->getTerminator()->setOperand(0, RTC);
2543717aa5dSTobias Grosser       Builder.SetInsertPoint(&StartBlock->front());
2553717aa5dSTobias Grosser 
2563717aa5dSTobias Grosser       NodeBuilder.create(AstRoot);
2578ed5e599STobias Grosser       NodeBuilder.finalize();
2589c483c58SMichael Kruse       fixRegionInfo(EnteringBB->getParent(), R->getParent());
2591dd6e37aSJohannes Doerfert     }
260ecff11dcSJohannes Doerfert 
2616a6a671cSJohannes Doerfert     Function *F = EnteringBB->getParent();
2626a6a671cSJohannes Doerfert     verifyGeneratedFunction(S, *F);
263a9dc5294SJohannes Doerfert     for (auto *SubF : NodeBuilder.getParallelSubfunctions())
264a9dc5294SJohannes Doerfert       verifyGeneratedFunction(S, *SubF);
265652f7808STobias Grosser 
2664c86a1d9SMichael Kruse     // Mark the function such that we run additional cleanup passes on this
2674c86a1d9SMichael Kruse     // function (e.g. mem2reg to rediscover phi nodes).
2684c86a1d9SMichael Kruse     F->addFnAttr("polly-optimized");
2694c86a1d9SMichael Kruse 
27009d30697STobias Grosser     return true;
27109d30697STobias Grosser   }
27209d30697STobias Grosser 
273c80d6979STobias Grosser   /// Register all analyses and transformation required.
27409d30697STobias Grosser   void getAnalysisUsage(AnalysisUsage &AU) const override {
27509d30697STobias Grosser     AU.addRequired<DominatorTreeWrapperPass>();
276*2b852e2eSPhilip Pfaffe     AU.addRequired<IslAstInfoWrapperPass>();
27709d30697STobias Grosser     AU.addRequired<RegionInfoPass>();
278c5bcf246STobias Grosser     AU.addRequired<ScalarEvolutionWrapperPass>();
2795cc87e3aSPhilip Pfaffe     AU.addRequired<ScopDetectionWrapperPass>();
28099191c78SJohannes Doerfert     AU.addRequired<ScopInfoRegionPass>();
28109d30697STobias Grosser     AU.addRequired<LoopInfoWrapperPass>();
28209d30697STobias Grosser 
28309d30697STobias Grosser     AU.addPreserved<DependenceInfo>();
28409d30697STobias Grosser 
28566ef16b2SChandler Carruth     AU.addPreserved<AAResultsWrapperPass>();
28666ef16b2SChandler Carruth     AU.addPreserved<BasicAAWrapperPass>();
28709d30697STobias Grosser     AU.addPreserved<LoopInfoWrapperPass>();
28809d30697STobias Grosser     AU.addPreserved<DominatorTreeWrapperPass>();
28966ef16b2SChandler Carruth     AU.addPreserved<GlobalsAAWrapperPass>();
290*2b852e2eSPhilip Pfaffe     AU.addPreserved<IslAstInfoWrapperPass>();
2915cc87e3aSPhilip Pfaffe     AU.addPreserved<ScopDetectionWrapperPass>();
292c5bcf246STobias Grosser     AU.addPreserved<ScalarEvolutionWrapperPass>();
29366ef16b2SChandler Carruth     AU.addPreserved<SCEVAAWrapperPass>();
29409d30697STobias Grosser 
29509d30697STobias Grosser     // FIXME: We do not yet add regions for the newly generated code to the
29609d30697STobias Grosser     //        region tree.
29709d30697STobias Grosser     AU.addPreserved<RegionInfoPass>();
29899191c78SJohannes Doerfert     AU.addPreserved<ScopInfoRegionPass>();
29909d30697STobias Grosser   }
30009d30697STobias Grosser };
301522478d2STobias Grosser } // namespace
30209d30697STobias Grosser 
30309d30697STobias Grosser char CodeGeneration::ID = 1;
30409d30697STobias Grosser 
30509d30697STobias Grosser Pass *polly::createCodeGenerationPass() { return new CodeGeneration(); }
30609d30697STobias Grosser 
30709d30697STobias Grosser INITIALIZE_PASS_BEGIN(CodeGeneration, "polly-codegen",
30809d30697STobias Grosser                       "Polly - Create LLVM-IR from SCoPs", false, false);
30909d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
31009d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
31109d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
31209d30697STobias Grosser INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
313c5bcf246STobias Grosser INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass);
3145cc87e3aSPhilip Pfaffe INITIALIZE_PASS_DEPENDENCY(ScopDetectionWrapperPass);
31509d30697STobias Grosser INITIALIZE_PASS_END(CodeGeneration, "polly-codegen",
31609d30697STobias Grosser                     "Polly - Create LLVM-IR from SCoPs", false, false)
317