blob: 9fd1df7080794873a35d1496ce6b8a2d1c351fd3 [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 Grosser83628182013-05-07 08:11:54 +000022#include "polly/CodeGen/IslAst.h"
Tobias Grosser5624d3c2015-12-21 12:38:56 +000023#include "polly/CodeGen/IslNodeBuilder.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"
Tobias Grosser58e58542016-02-19 11:07:12 +000027#include "polly/Options.h"
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000028#include "polly/ScopInfo.h"
Tobias Grosser0ee50f62013-04-10 06:55:31 +000029#include "polly/Support/ScopHelper.h"
Chandler Carruth66ef16b2015-09-09 22:13:56 +000030#include "llvm/Analysis/AliasAnalysis.h"
31#include "llvm/Analysis/BasicAliasAnalysis.h"
32#include "llvm/Analysis/GlobalsModRef.h"
Michael Kruse82a1c7d2015-08-14 20:10:27 +000033#include "llvm/Analysis/PostDominators.h"
Chandler Carruth66ef16b2015-09-09 22:13:56 +000034#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Tobias Grosserc2bb0cb2015-09-25 09:49:19 +000035#include "llvm/IR/Module.h"
36#include "llvm/IR/Verifier.h"
37#include "llvm/Support/Debug.h"
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000038
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000039using namespace polly;
40using namespace llvm;
41
Tobias Grosser09d30692015-05-12 07:45:52 +000042#define DEBUG_TYPE "polly-codegen"
Chandler Carruth95fef942014-04-22 03:30:19 +000043
Tobias Grosser58e58542016-02-19 11:07:12 +000044static cl::opt<bool> Verify("polly-codegen-verify",
45 cl::desc("Verify the function generated by Polly"),
46 cl::Hidden, cl::init(true), cl::ZeroOrMore,
47 cl::cat(PollyCategory));
48
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000049namespace {
Tobias Grosser09d30692015-05-12 07:45:52 +000050class CodeGeneration : public ScopPass {
Tobias Grosser1bb59b02012-12-29 23:47:38 +000051public:
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000052 static char ID;
53
Tobias Grosser09d30692015-05-12 07:45:52 +000054 CodeGeneration() : ScopPass(ID) {}
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000055
Tobias Grossere3c05582014-11-15 21:32:53 +000056 /// @brief The datalayout used
57 const DataLayout *DL;
58
Johannes Doerfert38262242014-09-10 14:50:23 +000059 /// @name The analysis passes we need to generate code.
60 ///
61 ///{
62 LoopInfo *LI;
63 IslAstInfo *AI;
64 DominatorTree *DT;
65 ScalarEvolution *SE;
Michael Kruse22370882015-08-11 14:39:21 +000066 RegionInfo *RI;
Johannes Doerfert38262242014-09-10 14:50:23 +000067 ///}
68
Tobias Grosser58e58542016-02-19 11:07:12 +000069 void verifyGeneratedFunction(Scop &S, Function &F) {
70 if (!verifyFunction(F, &errs()) || !Verify)
71 return;
Johannes Doerfert0b169c02015-02-27 17:37:05 +000072
73 DEBUG({
74 errs() << "== ISL Codegen created an invalid function ==\n\n== The "
75 "SCoP ==\n";
76 S.print(errs());
77 errs() << "\n== The isl AST ==\n";
Johannes Doerfert3fe584d2015-03-01 18:40:25 +000078 AI->printScop(errs(), S);
Johannes Doerfert0b169c02015-02-27 17:37:05 +000079 errs() << "\n== The invalid function ==\n";
80 F.print(errs());
Johannes Doerfert0b169c02015-02-27 17:37:05 +000081 });
82
Tobias Grosser58e58542016-02-19 11:07:12 +000083 llvm_unreachable("Polly generated function could not be verified. Add "
84 "-polly-codegen-verify=false to disable this assertion.");
Johannes Doerfert0b169c02015-02-27 17:37:05 +000085 }
86
Michael Kruse9c483c52015-08-11 14:47:37 +000087 // CodeGeneration adds a lot of BBs without updating the RegionInfo
88 // We make all created BBs belong to the scop's parent region without any
89 // nested structure to keep the RegionInfo verifier happy.
90 void fixRegionInfo(Function *F, Region *ParentRegion) {
91 for (BasicBlock &BB : *F) {
92 if (RI->getRegionFor(&BB))
93 continue;
94
95 RI->setRegionFor(&BB, ParentRegion);
96 }
97 }
98
Tobias Grosserbfb6a962016-03-23 06:57:51 +000099 /// @brief Mark a basic block unreachable.
100 ///
101 /// Marks the basic block @p Block unreachable by equipping it with an
102 /// UnreachableInst.
103 void markBlockUnreachable(BasicBlock &Block, PollyIRBuilder &Builder) {
104 auto *OrigTerminator = Block.getTerminator();
105 Builder.SetInsertPoint(OrigTerminator);
106 Builder.CreateUnreachable();
107 OrigTerminator->eraseFromParent();
108 }
109
Johannes Doerfert45be6442015-09-27 15:43:29 +0000110 /// @brief Generate LLVM-IR for the SCoP @p S.
Johannes Doerfert909a3bf2015-03-01 18:42:08 +0000111 bool runOnScop(Scop &S) override {
Johannes Doerfert38262242014-09-10 14:50:23 +0000112 AI = &getAnalysis<IslAstInfo>();
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000113
114 // Check if we created an isl_ast root node, otherwise exit.
115 isl_ast_node *AstRoot = AI->getAst();
116 if (!AstRoot)
117 return false;
118
119 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Johannes Doerfert38262242014-09-10 14:50:23 +0000120 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Tobias Grosserc5bcf242015-08-17 10:57:08 +0000121 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
Johannes Doerfert3f52e352016-05-23 12:38:05 +0000122 DL = &S.getFunction().getParent()->getDataLayout();
Michael Kruse22370882015-08-11 14:39:21 +0000123 RI = &getAnalysis<RegionInfoPass>().getRegionInfo();
124 Region *R = &S.getRegion();
125 assert(!R->isTopLevelRegion() && "Top level regions are not supported");
Tobias Grosser0ee50f62013-04-10 06:55:31 +0000126
Tobias Grosserd78616f2015-10-04 11:19:13 +0000127 ScopAnnotator Annotator;
Tobias Grosser6325cd22015-04-27 10:43:10 +0000128 Annotator.buildAliasScopes(S);
Johannes Doerfertecdf2632014-10-02 15:31:24 +0000129
Michael Kruse22370882015-08-11 14:39:21 +0000130 simplifyRegion(R, DT, LI, RI);
131 assert(R->isSimple());
Johannes Doerfertef744432016-05-23 12:42:38 +0000132 BasicBlock *EnteringBB = S.getEnteringBlock();
Michael Kruse22370882015-08-11 14:39:21 +0000133 assert(EnteringBB);
Johannes Doerfert38262242014-09-10 14:50:23 +0000134 PollyIRBuilder Builder = createPollyIRBuilder(EnteringBB, Annotator);
Johannes Doerfert9744c4a2014-08-12 18:35:54 +0000135
Tobias Grossere3c05582014-11-15 21:32:53 +0000136 IslNodeBuilder NodeBuilder(Builder, Annotator, this, *DL, *LI, *SE, *DT, S);
Johannes Doerfert38262242014-09-10 14:50:23 +0000137
Tobias Grosser67942382015-03-28 09:34:40 +0000138 // Only build the run-time condition and parameters _after_ having
139 // introduced the conditional branch. This is important as the conditional
140 // branch will guard the original scop from new induction variables that
141 // the SCEVExpander may introduce while code generating the parameters and
142 // which may introduce scalar dependences that prevent us from correctly
143 // code generating this scop.
144 BasicBlock *StartBlock =
145 executeScopConditionally(S, this, Builder.getTrue());
Tobias Grosserbfb6a962016-03-23 06:57:51 +0000146 auto *SplitBlock = StartBlock->getSinglePredecessor();
Johannes Doerfert09e36972015-10-07 20:17:36 +0000147
148 // First generate code for the hoisted invariant loads and transitively the
149 // parameters they reference. Afterwards, for the remaining parameters that
150 // might reference the hoisted loads. Finally, build the runtime check
151 // that might reference both hoisted loads as well as parameters.
Johannes Doerfertc4898502015-11-07 19:46:04 +0000152 // If the hoisting fails we have to bail and execute the original code.
Tobias Grosser67942382015-03-28 09:34:40 +0000153 Builder.SetInsertPoint(SplitBlock->getTerminator());
Johannes Doerfertc4898502015-11-07 19:46:04 +0000154 if (!NodeBuilder.preloadInvariantLoads()) {
Johannes Doerfert1dd6e372015-11-08 16:34:17 +0000155
Tobias Grosserbfb6a962016-03-23 06:57:51 +0000156 // Patch the introduced branch condition to ensure that we always execute
157 // the original SCoP.
Johannes Doerfertc4898502015-11-07 19:46:04 +0000158 auto *FalseI1 = Builder.getFalse();
Johannes Doerfert37977072015-11-08 17:57:41 +0000159 auto *SplitBBTerm = Builder.GetInsertBlock()->getTerminator();
160 SplitBBTerm->setOperand(0, FalseI1);
Johannes Doerfert1dd6e372015-11-08 16:34:17 +0000161
Tobias Grosserbfb6a962016-03-23 06:57:51 +0000162 // Since the other branch is hence ignored we mark it as unreachable and
163 // adjust the dominator tree accordingly.
164 auto *ExitingBlock = StartBlock->getUniqueSuccessor();
165 assert(ExitingBlock);
166 auto *MergeBlock = ExitingBlock->getUniqueSuccessor();
167 assert(MergeBlock);
168 markBlockUnreachable(*StartBlock, Builder);
169 markBlockUnreachable(*ExitingBlock, Builder);
Johannes Doerfertef744432016-05-23 12:42:38 +0000170 auto *ExitingBB = S.getExitingBlock();
Tobias Grosserbfb6a962016-03-23 06:57:51 +0000171 assert(ExitingBB);
172 DT->changeImmediateDominator(MergeBlock, ExitingBB);
173 DT->eraseNode(ExitingBlock);
174
175 isl_ast_node_free(AstRoot);
Johannes Doerfert1dd6e372015-11-08 16:34:17 +0000176 } else {
Roman Gareevd7754a12016-07-30 09:25:51 +0000177 NodeBuilder.allocateNewArrays();
Johannes Doerfert1dd6e372015-11-08 16:34:17 +0000178 NodeBuilder.addParameters(S.getContext());
Tobias Grosser0aa29532016-08-08 15:41:52 +0000179 Value *RTC = NodeBuilder.createRTC(AI->getRunCondition());
Johannes Doerfert404a0f82016-05-12 15:12:43 +0000180
Tobias Grosser3717aa52016-06-11 19:17:15 +0000181 Builder.GetInsertBlock()->getTerminator()->setOperand(0, RTC);
182 Builder.SetInsertPoint(&StartBlock->front());
183
184 NodeBuilder.create(AstRoot);
Tobias Grosser8ed5e592016-07-25 09:15:57 +0000185 NodeBuilder.finalize();
Johannes Doerfert1dd6e372015-11-08 16:34:17 +0000186 fixRegionInfo(EnteringBB->getParent(), R->getParent());
Johannes Doerfertc4898502015-11-07 19:46:04 +0000187 }
188
Johannes Doerfert6a6a6712016-06-06 12:13:24 +0000189 Function *F = EnteringBB->getParent();
190 verifyGeneratedFunction(S, *F);
Johannes Doerferta9dc5292016-04-08 18:16:02 +0000191 for (auto *SubF : NodeBuilder.getParallelSubfunctions())
192 verifyGeneratedFunction(S, *SubF);
Tobias Grosser652f7802016-02-14 20:56:49 +0000193
Michael Kruse4c86a1d2015-11-26 12:36:25 +0000194 // Mark the function such that we run additional cleanup passes on this
195 // function (e.g. mem2reg to rediscover phi nodes).
Michael Kruse4c86a1d2015-11-26 12:36:25 +0000196 F->addFnAttr("polly-optimized");
197
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000198 return true;
199 }
200
Johannes Doerfert45be6442015-09-27 15:43:29 +0000201 /// @brief Register all analyses and transformation required.
Johannes Doerfert909a3bf2015-03-01 18:42:08 +0000202 void getAnalysisUsage(AnalysisUsage &AU) const override {
Tobias Grosser42aff302014-01-13 22:29:56 +0000203 AU.addRequired<DominatorTreeWrapperPass>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000204 AU.addRequired<IslAstInfo>();
Matt Arsenault8ca36812014-07-19 18:40:17 +0000205 AU.addRequired<RegionInfoPass>();
Tobias Grosserc5bcf242015-08-17 10:57:08 +0000206 AU.addRequired<ScalarEvolutionWrapperPass>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000207 AU.addRequired<ScopDetection>();
Johannes Doerfert99191c72016-05-31 09:41:04 +0000208 AU.addRequired<ScopInfoRegionPass>();
Chandler Carruthf5579872015-01-17 14:16:56 +0000209 AU.addRequired<LoopInfoWrapperPass>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000210
Johannes Doerfertf6557f92015-03-04 22:43:40 +0000211 AU.addPreserved<DependenceInfo>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000212
Chandler Carruth66ef16b2015-09-09 22:13:56 +0000213 AU.addPreserved<AAResultsWrapperPass>();
214 AU.addPreserved<BasicAAWrapperPass>();
Chandler Carruthf5579872015-01-17 14:16:56 +0000215 AU.addPreserved<LoopInfoWrapperPass>();
Tobias Grosser42aff302014-01-13 22:29:56 +0000216 AU.addPreserved<DominatorTreeWrapperPass>();
Chandler Carruth66ef16b2015-09-09 22:13:56 +0000217 AU.addPreserved<GlobalsAAWrapperPass>();
Hongbin Zhengdefd0982016-02-25 17:54:42 +0000218 AU.addPreserved<PostDominatorTreeWrapperPass>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000219 AU.addPreserved<IslAstInfo>();
220 AU.addPreserved<ScopDetection>();
Tobias Grosserc5bcf242015-08-17 10:57:08 +0000221 AU.addPreserved<ScalarEvolutionWrapperPass>();
Chandler Carruth66ef16b2015-09-09 22:13:56 +0000222 AU.addPreserved<SCEVAAWrapperPass>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000223
224 // FIXME: We do not yet add regions for the newly generated code to the
225 // region tree.
Matt Arsenault8ca36812014-07-19 18:40:17 +0000226 AU.addPreserved<RegionInfoPass>();
Johannes Doerfert99191c72016-05-31 09:41:04 +0000227 AU.addPreserved<ScopInfoRegionPass>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000228 }
229};
Tobias Grosser522478d2016-06-23 22:17:27 +0000230} // namespace
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000231
Tobias Grosser09d30692015-05-12 07:45:52 +0000232char CodeGeneration::ID = 1;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000233
Tobias Grosser09d30692015-05-12 07:45:52 +0000234Pass *polly::createCodeGenerationPass() { return new CodeGeneration(); }
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000235
Tobias Grosser09d30692015-05-12 07:45:52 +0000236INITIALIZE_PASS_BEGIN(CodeGeneration, "polly-codegen",
Tobias Grosser7242ad92013-02-22 08:07:06 +0000237 "Polly - Create LLVM-IR from SCoPs", false, false);
Johannes Doerfertf6557f92015-03-04 22:43:40 +0000238INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
Tobias Grosser42aff302014-01-13 22:29:56 +0000239INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
Chandler Carruthf5579872015-01-17 14:16:56 +0000240INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
Matt Arsenault8ca36812014-07-19 18:40:17 +0000241INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
Tobias Grosserc5bcf242015-08-17 10:57:08 +0000242INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass);
Tobias Grosser7242ad92013-02-22 08:07:06 +0000243INITIALIZE_PASS_DEPENDENCY(ScopDetection);
Tobias Grosser09d30692015-05-12 07:45:52 +0000244INITIALIZE_PASS_END(CodeGeneration, "polly-codegen",
Tobias Grosser7242ad92013-02-22 08:07:06 +0000245 "Polly - Create LLVM-IR from SCoPs", false, false)