blob: c92141e6559ca5393cd154bba31d316751061518 [file] [log] [blame]
Sebastian Pop082cea82012-05-07 16:20:07 +00001//===------ IslCodeGeneration.cpp - Code generate the Scops using ISL. ----===//
2//
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//
10// The IslCodeGeneration pass takes a Scop created by ScopInfo and translates it
11// 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"
Tobias Grosser83628182013-05-07 08:11:54 +000029#include "polly/TempScopInfo.h"
Tobias Grossere3c05582014-11-15 21:32:53 +000030
Tobias Grosser83628182013-05-07 08:11:54 +000031#include "llvm/IR/Module.h"
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000032#include "llvm/Support/Debug.h"
Johannes Doerfert0b169c02015-02-27 17:37:05 +000033#include "llvm/IR/Verifier.h"
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000034
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000035using namespace polly;
36using namespace llvm;
37
Chandler Carruth95fef942014-04-22 03:30:19 +000038#define DEBUG_TYPE "polly-codegen-isl"
39
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000040namespace {
41class IslCodeGeneration : public ScopPass {
Tobias Grosser1bb59b02012-12-29 23:47:38 +000042public:
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000043 static char ID;
44
45 IslCodeGeneration() : ScopPass(ID) {}
46
Tobias Grossere3c05582014-11-15 21:32:53 +000047 /// @brief The datalayout used
48 const DataLayout *DL;
49
Johannes Doerfert38262242014-09-10 14:50:23 +000050 /// @name The analysis passes we need to generate code.
51 ///
52 ///{
53 LoopInfo *LI;
54 IslAstInfo *AI;
55 DominatorTree *DT;
56 ScalarEvolution *SE;
57 ///}
58
59 /// @brief The loop annotator to generate llvm.loop metadata.
Johannes Doerfert51d1c742014-10-02 15:32:17 +000060 ScopAnnotator Annotator;
Johannes Doerfert38262242014-09-10 14:50:23 +000061
62 /// @brief Build the runtime condition.
63 ///
64 /// Build the condition that evaluates at run-time to true iff all
65 /// assumptions taken for the SCoP hold, and to false otherwise.
66 ///
67 /// @return A value evaluating to true/false if execution is save/unsafe.
68 Value *buildRTC(PollyIRBuilder &Builder, IslExprBuilder &ExprBuilder) {
69 Builder.SetInsertPoint(Builder.GetInsertBlock()->getTerminator());
70 Value *RTC = ExprBuilder.create(AI->getRunCondition());
Johannes Doerfertb164c792014-09-18 11:17:17 +000071 if (!RTC->getType()->isIntegerTy(1))
72 RTC = Builder.CreateIsNotNull(RTC);
73 return RTC;
Johannes Doerfert38262242014-09-10 14:50:23 +000074 }
75
Johannes Doerfert0b169c02015-02-27 17:37:05 +000076 bool verifyGeneratedFunction(Scop &S, Function &F) {
77 if (!verifyFunction(F))
78 return false;
79
80 DEBUG({
81 errs() << "== ISL Codegen created an invalid function ==\n\n== The "
82 "SCoP ==\n";
83 S.print(errs());
84 errs() << "\n== The isl AST ==\n";
Johannes Doerfert3fe584d2015-03-01 18:40:25 +000085 AI->printScop(errs(), S);
Johannes Doerfert0b169c02015-02-27 17:37:05 +000086 errs() << "\n== The invalid function ==\n";
87 F.print(errs());
88 errs() << "\n== The errors ==\n";
89 verifyFunction(F, &errs());
90 });
91
92 return true;
93 }
94
Johannes Doerfert909a3bf2015-03-01 18:42:08 +000095 bool runOnScop(Scop &S) override {
Johannes Doerfert38262242014-09-10 14:50:23 +000096 AI = &getAnalysis<IslAstInfo>();
Johannes Doerfert7ceb0402015-02-11 17:25:09 +000097
98 // Check if we created an isl_ast root node, otherwise exit.
99 isl_ast_node *AstRoot = AI->getAst();
100 if (!AstRoot)
101 return false;
102
103 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Johannes Doerfert38262242014-09-10 14:50:23 +0000104 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
105 SE = &getAnalysis<ScalarEvolution>();
Tobias Grosser140b3942015-03-05 09:48:20 +0000106 DL = &S.getRegion().getEntry()->getParent()->getParent()->getDataLayout();
Tobias Grosser0ee50f62013-04-10 06:55:31 +0000107
Tobias Grossere602a072013-05-07 07:30:56 +0000108 assert(!S.getRegion().isTopLevelRegion() &&
109 "Top level regions are not supported");
Tobias Grosser0ee50f62013-04-10 06:55:31 +0000110
Tobias Grosser6325cd22015-04-27 10:43:10 +0000111 Annotator.buildAliasScopes(S);
Johannes Doerfertecdf2632014-10-02 15:31:24 +0000112
Johannes Doerfert38262242014-09-10 14:50:23 +0000113 BasicBlock *EnteringBB = simplifyRegion(&S, this);
114 PollyIRBuilder Builder = createPollyIRBuilder(EnteringBB, Annotator);
Johannes Doerfert9744c4a2014-08-12 18:35:54 +0000115
Tobias Grossere3c05582014-11-15 21:32:53 +0000116 IslNodeBuilder NodeBuilder(Builder, Annotator, this, *DL, *LI, *SE, *DT, S);
Johannes Doerfert38262242014-09-10 14:50:23 +0000117
Tobias Grosser67942382015-03-28 09:34:40 +0000118 // Only build the run-time condition and parameters _after_ having
119 // introduced the conditional branch. This is important as the conditional
120 // branch will guard the original scop from new induction variables that
121 // the SCEVExpander may introduce while code generating the parameters and
122 // which may introduce scalar dependences that prevent us from correctly
123 // code generating this scop.
124 BasicBlock *StartBlock =
125 executeScopConditionally(S, this, Builder.getTrue());
126 auto SplitBlock = StartBlock->getSinglePredecessor();
127 Builder.SetInsertPoint(SplitBlock->getTerminator());
128 NodeBuilder.addParameters(S.getContext());
Johannes Doerfert38262242014-09-10 14:50:23 +0000129 Value *RTC = buildRTC(Builder, NodeBuilder.getExprBuilder());
Tobias Grosser67942382015-03-28 09:34:40 +0000130 SplitBlock->getTerminator()->setOperand(0, RTC);
Tobias Grosser28735942014-08-16 09:09:15 +0000131 Builder.SetInsertPoint(StartBlock->begin());
132
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000133 NodeBuilder.create(AstRoot);
Johannes Doerfert0b169c02015-02-27 17:37:05 +0000134
135 assert(!verifyGeneratedFunction(S, *EnteringBB->getParent()) &&
136 "Verification of generated function failed");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000137 return true;
138 }
139
Johannes Doerfert909a3bf2015-03-01 18:42:08 +0000140 void printScop(raw_ostream &, Scop &) const override {}
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000141
Johannes Doerfert909a3bf2015-03-01 18:42:08 +0000142 void getAnalysisUsage(AnalysisUsage &AU) const override {
Tobias Grosser42aff302014-01-13 22:29:56 +0000143 AU.addRequired<DominatorTreeWrapperPass>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000144 AU.addRequired<IslAstInfo>();
Matt Arsenault8ca36812014-07-19 18:40:17 +0000145 AU.addRequired<RegionInfoPass>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000146 AU.addRequired<ScalarEvolution>();
147 AU.addRequired<ScopDetection>();
148 AU.addRequired<ScopInfo>();
Chandler Carruthf5579872015-01-17 14:16:56 +0000149 AU.addRequired<LoopInfoWrapperPass>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000150
Johannes Doerfertf6557f92015-03-04 22:43:40 +0000151 AU.addPreserved<DependenceInfo>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000152
Chandler Carruthf5579872015-01-17 14:16:56 +0000153 AU.addPreserved<LoopInfoWrapperPass>();
Tobias Grosser42aff302014-01-13 22:29:56 +0000154 AU.addPreserved<DominatorTreeWrapperPass>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000155 AU.addPreserved<IslAstInfo>();
156 AU.addPreserved<ScopDetection>();
157 AU.addPreserved<ScalarEvolution>();
158
159 // FIXME: We do not yet add regions for the newly generated code to the
160 // region tree.
Matt Arsenault8ca36812014-07-19 18:40:17 +0000161 AU.addPreserved<RegionInfoPass>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000162 AU.addPreserved<TempScopInfo>();
163 AU.addPreserved<ScopInfo>();
164 AU.addPreservedID(IndependentBlocksID);
165 }
166};
167}
168
169char IslCodeGeneration::ID = 1;
170
Tobias Grosser7242ad92013-02-22 08:07:06 +0000171Pass *polly::createIslCodeGenerationPass() { return new IslCodeGeneration(); }
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000172
Tobias Grosser7242ad92013-02-22 08:07:06 +0000173INITIALIZE_PASS_BEGIN(IslCodeGeneration, "polly-codegen-isl",
174 "Polly - Create LLVM-IR from SCoPs", false, false);
Johannes Doerfertf6557f92015-03-04 22:43:40 +0000175INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
Tobias Grosser42aff302014-01-13 22:29:56 +0000176INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
Chandler Carruthf5579872015-01-17 14:16:56 +0000177INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
Matt Arsenault8ca36812014-07-19 18:40:17 +0000178INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
Tobias Grosser7242ad92013-02-22 08:07:06 +0000179INITIALIZE_PASS_DEPENDENCY(ScalarEvolution);
180INITIALIZE_PASS_DEPENDENCY(ScopDetection);
181INITIALIZE_PASS_END(IslCodeGeneration, "polly-codegen-isl",
182 "Polly - Create LLVM-IR from SCoPs", false, false)