blob: 55c42ce0a8bbfc887d42d017ce333ded8e371151 [file] [log] [blame]
Tobias Grosser09d30692015-05-12 07:45:52 +00001//===------ CodeGeneration.cpp - Code generate the Scops using ISL. ----======//
Sebastian Pop082cea82012-05-07 16:20:07 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Tobias Grosser09d30692015-05-12 07:45:52 +000010// The CodeGeneration pass takes a Scop created by ScopInfo and translates it
Sebastian Pop082cea82012-05-07 16:20:07 +000011// back to LLVM-IR using the ISL code generator.
12//
13// The Scop describes the high level memory behaviour of a control flow region.
14// Transformation passes can update the schedule (execution order) of statements
15// in the Scop. ISL is used to generate an abstract syntax tree that reflects
16// the updated execution order. This clast is used to create new LLVM-IR that is
17// computationally equivalent to the original control flow region, but executes
Tobias Grosser54839312015-04-21 11:37:25 +000018// its code in the new execution order defined by the changed schedule.
Sebastian Pop082cea82012-05-07 16:20:07 +000019//
20//===----------------------------------------------------------------------===//
Tobias Grosserf3ba5b52015-04-27 12:17:22 +000021
Tobias Grosser0c55cb62015-04-27 12:32:24 +000022#include "polly/CodeGen/IslNodeBuilder.h"
Tobias Grosser83628182013-05-07 08:11:54 +000023#include "polly/CodeGen/IslAst.h"
Tobias Grosser83628182013-05-07 08:11:54 +000024#include "polly/CodeGen/Utils.h"
Johannes Doerfertf6557f92015-03-04 22:43:40 +000025#include "polly/DependenceInfo.h"
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000026#include "polly/LinkAllPasses.h"
27#include "polly/ScopInfo.h"
Tobias Grosser0ee50f62013-04-10 06:55:31 +000028#include "polly/Support/ScopHelper.h"
Chandler Carruth66ef16b2015-09-09 22:13:56 +000029#include "llvm/Analysis/AliasAnalysis.h"
30#include "llvm/Analysis/BasicAliasAnalysis.h"
31#include "llvm/Analysis/GlobalsModRef.h"
Michael Kruse82a1c7d2015-08-14 20:10:27 +000032#include "llvm/Analysis/PostDominators.h"
Chandler Carruth66ef16b2015-09-09 22:13:56 +000033#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Tobias Grosserc2bb0cb2015-09-25 09:49:19 +000034#include "llvm/IR/Module.h"
35#include "llvm/IR/Verifier.h"
36#include "llvm/Support/Debug.h"
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000037
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000038using namespace polly;
39using namespace llvm;
40
Tobias Grosser09d30692015-05-12 07:45:52 +000041#define DEBUG_TYPE "polly-codegen"
Chandler Carruth95fef942014-04-22 03:30:19 +000042
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000043namespace {
Tobias Grosser09d30692015-05-12 07:45:52 +000044class CodeGeneration : public ScopPass {
Tobias Grosser1bb59b02012-12-29 23:47:38 +000045public:
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000046 static char ID;
47
Tobias Grosser09d30692015-05-12 07:45:52 +000048 CodeGeneration() : ScopPass(ID) {}
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000049
Tobias Grossere3c05582014-11-15 21:32:53 +000050 /// @brief The datalayout used
51 const DataLayout *DL;
52
Johannes Doerfert38262242014-09-10 14:50:23 +000053 /// @name The analysis passes we need to generate code.
54 ///
55 ///{
56 LoopInfo *LI;
57 IslAstInfo *AI;
58 DominatorTree *DT;
59 ScalarEvolution *SE;
Michael Kruse22370882015-08-11 14:39:21 +000060 RegionInfo *RI;
Johannes Doerfert38262242014-09-10 14:50:23 +000061 ///}
62
Johannes Doerfert38262242014-09-10 14:50:23 +000063 /// @brief Build the runtime condition.
64 ///
65 /// Build the condition that evaluates at run-time to true iff all
66 /// assumptions taken for the SCoP hold, and to false otherwise.
67 ///
68 /// @return A value evaluating to true/false if execution is save/unsafe.
69 Value *buildRTC(PollyIRBuilder &Builder, IslExprBuilder &ExprBuilder) {
70 Builder.SetInsertPoint(Builder.GetInsertBlock()->getTerminator());
71 Value *RTC = ExprBuilder.create(AI->getRunCondition());
Johannes Doerfertb164c792014-09-18 11:17:17 +000072 if (!RTC->getType()->isIntegerTy(1))
73 RTC = Builder.CreateIsNotNull(RTC);
74 return RTC;
Johannes Doerfert38262242014-09-10 14:50:23 +000075 }
76
Johannes Doerfert0b169c02015-02-27 17:37:05 +000077 bool verifyGeneratedFunction(Scop &S, Function &F) {
78 if (!verifyFunction(F))
79 return false;
80
81 DEBUG({
82 errs() << "== ISL Codegen created an invalid function ==\n\n== The "
83 "SCoP ==\n";
84 S.print(errs());
85 errs() << "\n== The isl AST ==\n";
Johannes Doerfert3fe584d2015-03-01 18:40:25 +000086 AI->printScop(errs(), S);
Johannes Doerfert0b169c02015-02-27 17:37:05 +000087 errs() << "\n== The invalid function ==\n";
88 F.print(errs());
89 errs() << "\n== The errors ==\n";
90 verifyFunction(F, &errs());
91 });
92
93 return true;
94 }
95
Michael Kruse9c483c52015-08-11 14:47:37 +000096 // CodeGeneration adds a lot of BBs without updating the RegionInfo
97 // We make all created BBs belong to the scop's parent region without any
98 // nested structure to keep the RegionInfo verifier happy.
99 void fixRegionInfo(Function *F, Region *ParentRegion) {
100 for (BasicBlock &BB : *F) {
101 if (RI->getRegionFor(&BB))
102 continue;
103
104 RI->setRegionFor(&BB, ParentRegion);
105 }
106 }
107
Johannes Doerfert45be6442015-09-27 15:43:29 +0000108 /// @brief Generate LLVM-IR for the SCoP @p S.
Johannes Doerfert909a3bf2015-03-01 18:42:08 +0000109 bool runOnScop(Scop &S) override {
Johannes Doerfert38262242014-09-10 14:50:23 +0000110 AI = &getAnalysis<IslAstInfo>();
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000111
112 // Check if we created an isl_ast root node, otherwise exit.
113 isl_ast_node *AstRoot = AI->getAst();
114 if (!AstRoot)
115 return false;
116
117 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Johannes Doerfert38262242014-09-10 14:50:23 +0000118 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Tobias Grosserc5bcf242015-08-17 10:57:08 +0000119 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
Tobias Grosser140b3942015-03-05 09:48:20 +0000120 DL = &S.getRegion().getEntry()->getParent()->getParent()->getDataLayout();
Michael Kruse22370882015-08-11 14:39:21 +0000121 RI = &getAnalysis<RegionInfoPass>().getRegionInfo();
122 Region *R = &S.getRegion();
123 assert(!R->isTopLevelRegion() && "Top level regions are not supported");
Tobias Grosser0ee50f62013-04-10 06:55:31 +0000124
Tobias Grosserd78616f2015-10-04 11:19:13 +0000125 ScopAnnotator Annotator;
Tobias Grosser6325cd22015-04-27 10:43:10 +0000126 Annotator.buildAliasScopes(S);
Johannes Doerfertecdf2632014-10-02 15:31:24 +0000127
Michael Kruse22370882015-08-11 14:39:21 +0000128 simplifyRegion(R, DT, LI, RI);
129 assert(R->isSimple());
130 BasicBlock *EnteringBB = S.getRegion().getEnteringBlock();
131 assert(EnteringBB);
Johannes Doerfert38262242014-09-10 14:50:23 +0000132 PollyIRBuilder Builder = createPollyIRBuilder(EnteringBB, Annotator);
Johannes Doerfert9744c4a2014-08-12 18:35:54 +0000133
Tobias Grossere3c05582014-11-15 21:32:53 +0000134 IslNodeBuilder NodeBuilder(Builder, Annotator, this, *DL, *LI, *SE, *DT, S);
Johannes Doerfert38262242014-09-10 14:50:23 +0000135
Tobias Grosser67942382015-03-28 09:34:40 +0000136 // Only build the run-time condition and parameters _after_ having
137 // introduced the conditional branch. This is important as the conditional
138 // branch will guard the original scop from new induction variables that
139 // the SCEVExpander may introduce while code generating the parameters and
140 // which may introduce scalar dependences that prevent us from correctly
141 // code generating this scop.
142 BasicBlock *StartBlock =
143 executeScopConditionally(S, this, Builder.getTrue());
144 auto SplitBlock = StartBlock->getSinglePredecessor();
Johannes Doerfert09e36972015-10-07 20:17:36 +0000145
146 // First generate code for the hoisted invariant loads and transitively the
147 // parameters they reference. Afterwards, for the remaining parameters that
148 // might reference the hoisted loads. Finally, build the runtime check
149 // that might reference both hoisted loads as well as parameters.
Johannes Doerfertc4898502015-11-07 19:46:04 +0000150 // If the hoisting fails we have to bail and execute the original code.
Tobias Grosser67942382015-03-28 09:34:40 +0000151 Builder.SetInsertPoint(SplitBlock->getTerminator());
Johannes Doerfertc4898502015-11-07 19:46:04 +0000152 if (!NodeBuilder.preloadInvariantLoads()) {
Johannes Doerfert1dd6e372015-11-08 16:34:17 +0000153
Johannes Doerfertc4898502015-11-07 19:46:04 +0000154 auto *FalseI1 = Builder.getFalse();
Johannes Doerfert37977072015-11-08 17:57:41 +0000155 auto *SplitBBTerm = Builder.GetInsertBlock()->getTerminator();
156 SplitBBTerm->setOperand(0, FalseI1);
157 auto *StartBBTerm = StartBlock->getTerminator();
158 Builder.SetInsertPoint(StartBBTerm);
159 Builder.CreateUnreachable();
160 StartBBTerm->eraseFromParent();
Johannes Doerfertc4898502015-11-07 19:46:04 +0000161 isl_ast_node_free(AstRoot);
Johannes Doerfert1dd6e372015-11-08 16:34:17 +0000162
163 } else {
164
165 NodeBuilder.addParameters(S.getContext());
166
167 Value *RTC = buildRTC(Builder, NodeBuilder.getExprBuilder());
168 Builder.GetInsertBlock()->getTerminator()->setOperand(0, RTC);
169 Builder.SetInsertPoint(&StartBlock->front());
170
171 NodeBuilder.create(AstRoot);
172
173 NodeBuilder.finalizeSCoP(S);
174 fixRegionInfo(EnteringBB->getParent(), R->getParent());
Johannes Doerfertc4898502015-11-07 19:46:04 +0000175 }
176
Johannes Doerfert0b169c02015-02-27 17:37:05 +0000177 assert(!verifyGeneratedFunction(S, *EnteringBB->getParent()) &&
178 "Verification of generated function failed");
Michael Kruse4c86a1d2015-11-26 12:36:25 +0000179
180 // Mark the function such that we run additional cleanup passes on this
181 // function (e.g. mem2reg to rediscover phi nodes).
182 Function *F = EnteringBB->getParent();
183 F->addFnAttr("polly-optimized");
184
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000185 return true;
186 }
187
Johannes Doerfert45be6442015-09-27 15:43:29 +0000188 /// @brief Register all analyses and transformation required.
Johannes Doerfert909a3bf2015-03-01 18:42:08 +0000189 void getAnalysisUsage(AnalysisUsage &AU) const override {
Tobias Grosser42aff302014-01-13 22:29:56 +0000190 AU.addRequired<DominatorTreeWrapperPass>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000191 AU.addRequired<IslAstInfo>();
Matt Arsenault8ca36812014-07-19 18:40:17 +0000192 AU.addRequired<RegionInfoPass>();
Tobias Grosserc5bcf242015-08-17 10:57:08 +0000193 AU.addRequired<ScalarEvolutionWrapperPass>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000194 AU.addRequired<ScopDetection>();
195 AU.addRequired<ScopInfo>();
Chandler Carruthf5579872015-01-17 14:16:56 +0000196 AU.addRequired<LoopInfoWrapperPass>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000197
Johannes Doerfertf6557f92015-03-04 22:43:40 +0000198 AU.addPreserved<DependenceInfo>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000199
Chandler Carruth66ef16b2015-09-09 22:13:56 +0000200 AU.addPreserved<AAResultsWrapperPass>();
201 AU.addPreserved<BasicAAWrapperPass>();
Chandler Carruthf5579872015-01-17 14:16:56 +0000202 AU.addPreserved<LoopInfoWrapperPass>();
Tobias Grosser42aff302014-01-13 22:29:56 +0000203 AU.addPreserved<DominatorTreeWrapperPass>();
Chandler Carruth66ef16b2015-09-09 22:13:56 +0000204 AU.addPreserved<GlobalsAAWrapperPass>();
Michael Kruse82a1c7d2015-08-14 20:10:27 +0000205 AU.addPreserved<PostDominatorTree>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000206 AU.addPreserved<IslAstInfo>();
207 AU.addPreserved<ScopDetection>();
Tobias Grosserc5bcf242015-08-17 10:57:08 +0000208 AU.addPreserved<ScalarEvolutionWrapperPass>();
Chandler Carruth66ef16b2015-09-09 22:13:56 +0000209 AU.addPreserved<SCEVAAWrapperPass>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000210
211 // FIXME: We do not yet add regions for the newly generated code to the
212 // region tree.
Matt Arsenault8ca36812014-07-19 18:40:17 +0000213 AU.addPreserved<RegionInfoPass>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000214 AU.addPreserved<ScopInfo>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000215 }
216};
217}
218
Tobias Grosser09d30692015-05-12 07:45:52 +0000219char CodeGeneration::ID = 1;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000220
Tobias Grosser09d30692015-05-12 07:45:52 +0000221Pass *polly::createCodeGenerationPass() { return new CodeGeneration(); }
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000222
Tobias Grosser09d30692015-05-12 07:45:52 +0000223INITIALIZE_PASS_BEGIN(CodeGeneration, "polly-codegen",
Tobias Grosser7242ad92013-02-22 08:07:06 +0000224 "Polly - Create LLVM-IR from SCoPs", false, false);
Johannes Doerfertf6557f92015-03-04 22:43:40 +0000225INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
Tobias Grosser42aff302014-01-13 22:29:56 +0000226INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
Chandler Carruthf5579872015-01-17 14:16:56 +0000227INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
Matt Arsenault8ca36812014-07-19 18:40:17 +0000228INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
Tobias Grosserc5bcf242015-08-17 10:57:08 +0000229INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass);
Tobias Grosser7242ad92013-02-22 08:07:06 +0000230INITIALIZE_PASS_DEPENDENCY(ScopDetection);
Tobias Grosser09d30692015-05-12 07:45:52 +0000231INITIALIZE_PASS_END(CodeGeneration, "polly-codegen",
Tobias Grosser7242ad92013-02-22 08:07:06 +0000232 "Polly - Create LLVM-IR from SCoPs", false, false)