Tobias Grosser | 09d3069 | 2015-05-12 07:45:52 +0000 | [diff] [blame^] | 1 | //===------ CodeGeneration.cpp - Code generate the Scops using ISL. ----======// |
Sebastian Pop | 082cea8 | 2012-05-07 16:20:07 +0000 | [diff] [blame] | 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 | // |
Tobias Grosser | 09d3069 | 2015-05-12 07:45:52 +0000 | [diff] [blame^] | 10 | // The CodeGeneration pass takes a Scop created by ScopInfo and translates it |
Sebastian Pop | 082cea8 | 2012-05-07 16:20:07 +0000 | [diff] [blame] | 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 Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 18 | // its code in the new execution order defined by the changed schedule. |
Sebastian Pop | 082cea8 | 2012-05-07 16:20:07 +0000 | [diff] [blame] | 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
Tobias Grosser | f3ba5b5 | 2015-04-27 12:17:22 +0000 | [diff] [blame] | 21 | |
Tobias Grosser | 0c55cb6 | 2015-04-27 12:32:24 +0000 | [diff] [blame] | 22 | #include "polly/CodeGen/IslNodeBuilder.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 23 | #include "polly/CodeGen/IslAst.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 24 | #include "polly/CodeGen/Utils.h" |
Johannes Doerfert | f6557f9 | 2015-03-04 22:43:40 +0000 | [diff] [blame] | 25 | #include "polly/DependenceInfo.h" |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 26 | #include "polly/LinkAllPasses.h" |
| 27 | #include "polly/ScopInfo.h" |
Tobias Grosser | 0ee50f6 | 2013-04-10 06:55:31 +0000 | [diff] [blame] | 28 | #include "polly/Support/ScopHelper.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 29 | #include "polly/TempScopInfo.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Module.h" |
Johannes Doerfert | 0b169c0 | 2015-02-27 17:37:05 +0000 | [diff] [blame] | 31 | #include "llvm/IR/Verifier.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Debug.h" |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 33 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 34 | using namespace polly; |
| 35 | using namespace llvm; |
| 36 | |
Tobias Grosser | 09d3069 | 2015-05-12 07:45:52 +0000 | [diff] [blame^] | 37 | #define DEBUG_TYPE "polly-codegen" |
Chandler Carruth | 95fef94 | 2014-04-22 03:30:19 +0000 | [diff] [blame] | 38 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 39 | namespace { |
Tobias Grosser | 09d3069 | 2015-05-12 07:45:52 +0000 | [diff] [blame^] | 40 | class CodeGeneration : public ScopPass { |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 41 | public: |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 42 | static char ID; |
| 43 | |
Tobias Grosser | 09d3069 | 2015-05-12 07:45:52 +0000 | [diff] [blame^] | 44 | CodeGeneration() : ScopPass(ID) {} |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 45 | |
Tobias Grosser | e3c0558 | 2014-11-15 21:32:53 +0000 | [diff] [blame] | 46 | /// @brief The datalayout used |
| 47 | const DataLayout *DL; |
| 48 | |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame] | 49 | /// @name The analysis passes we need to generate code. |
| 50 | /// |
| 51 | ///{ |
| 52 | LoopInfo *LI; |
| 53 | IslAstInfo *AI; |
| 54 | DominatorTree *DT; |
| 55 | ScalarEvolution *SE; |
| 56 | ///} |
| 57 | |
| 58 | /// @brief The loop annotator to generate llvm.loop metadata. |
Johannes Doerfert | 51d1c74 | 2014-10-02 15:32:17 +0000 | [diff] [blame] | 59 | ScopAnnotator Annotator; |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame] | 60 | |
| 61 | /// @brief Build the runtime condition. |
| 62 | /// |
| 63 | /// Build the condition that evaluates at run-time to true iff all |
| 64 | /// assumptions taken for the SCoP hold, and to false otherwise. |
| 65 | /// |
| 66 | /// @return A value evaluating to true/false if execution is save/unsafe. |
| 67 | Value *buildRTC(PollyIRBuilder &Builder, IslExprBuilder &ExprBuilder) { |
| 68 | Builder.SetInsertPoint(Builder.GetInsertBlock()->getTerminator()); |
| 69 | Value *RTC = ExprBuilder.create(AI->getRunCondition()); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 70 | if (!RTC->getType()->isIntegerTy(1)) |
| 71 | RTC = Builder.CreateIsNotNull(RTC); |
| 72 | return RTC; |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Johannes Doerfert | 0b169c0 | 2015-02-27 17:37:05 +0000 | [diff] [blame] | 75 | bool verifyGeneratedFunction(Scop &S, Function &F) { |
| 76 | if (!verifyFunction(F)) |
| 77 | return false; |
| 78 | |
| 79 | DEBUG({ |
| 80 | errs() << "== ISL Codegen created an invalid function ==\n\n== The " |
| 81 | "SCoP ==\n"; |
| 82 | S.print(errs()); |
| 83 | errs() << "\n== The isl AST ==\n"; |
Johannes Doerfert | 3fe584d | 2015-03-01 18:40:25 +0000 | [diff] [blame] | 84 | AI->printScop(errs(), S); |
Johannes Doerfert | 0b169c0 | 2015-02-27 17:37:05 +0000 | [diff] [blame] | 85 | errs() << "\n== The invalid function ==\n"; |
| 86 | F.print(errs()); |
| 87 | errs() << "\n== The errors ==\n"; |
| 88 | verifyFunction(F, &errs()); |
| 89 | }); |
| 90 | |
| 91 | return true; |
| 92 | } |
| 93 | |
Johannes Doerfert | 909a3bf | 2015-03-01 18:42:08 +0000 | [diff] [blame] | 94 | bool runOnScop(Scop &S) override { |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame] | 95 | AI = &getAnalysis<IslAstInfo>(); |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 96 | |
| 97 | // Check if we created an isl_ast root node, otherwise exit. |
| 98 | isl_ast_node *AstRoot = AI->getAst(); |
| 99 | if (!AstRoot) |
| 100 | return false; |
| 101 | |
| 102 | LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame] | 103 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 104 | SE = &getAnalysis<ScalarEvolution>(); |
Tobias Grosser | 140b394 | 2015-03-05 09:48:20 +0000 | [diff] [blame] | 105 | DL = &S.getRegion().getEntry()->getParent()->getParent()->getDataLayout(); |
Tobias Grosser | 0ee50f6 | 2013-04-10 06:55:31 +0000 | [diff] [blame] | 106 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 107 | assert(!S.getRegion().isTopLevelRegion() && |
| 108 | "Top level regions are not supported"); |
Tobias Grosser | 0ee50f6 | 2013-04-10 06:55:31 +0000 | [diff] [blame] | 109 | |
Tobias Grosser | 6325cd2 | 2015-04-27 10:43:10 +0000 | [diff] [blame] | 110 | Annotator.buildAliasScopes(S); |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 111 | |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame] | 112 | BasicBlock *EnteringBB = simplifyRegion(&S, this); |
| 113 | PollyIRBuilder Builder = createPollyIRBuilder(EnteringBB, Annotator); |
Johannes Doerfert | 9744c4a | 2014-08-12 18:35:54 +0000 | [diff] [blame] | 114 | |
Tobias Grosser | e3c0558 | 2014-11-15 21:32:53 +0000 | [diff] [blame] | 115 | IslNodeBuilder NodeBuilder(Builder, Annotator, this, *DL, *LI, *SE, *DT, S); |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame] | 116 | |
Tobias Grosser | 6794238 | 2015-03-28 09:34:40 +0000 | [diff] [blame] | 117 | // Only build the run-time condition and parameters _after_ having |
| 118 | // introduced the conditional branch. This is important as the conditional |
| 119 | // branch will guard the original scop from new induction variables that |
| 120 | // the SCEVExpander may introduce while code generating the parameters and |
| 121 | // which may introduce scalar dependences that prevent us from correctly |
| 122 | // code generating this scop. |
| 123 | BasicBlock *StartBlock = |
| 124 | executeScopConditionally(S, this, Builder.getTrue()); |
| 125 | auto SplitBlock = StartBlock->getSinglePredecessor(); |
| 126 | Builder.SetInsertPoint(SplitBlock->getTerminator()); |
| 127 | NodeBuilder.addParameters(S.getContext()); |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame] | 128 | Value *RTC = buildRTC(Builder, NodeBuilder.getExprBuilder()); |
Tobias Grosser | 6794238 | 2015-03-28 09:34:40 +0000 | [diff] [blame] | 129 | SplitBlock->getTerminator()->setOperand(0, RTC); |
Tobias Grosser | 2873594 | 2014-08-16 09:09:15 +0000 | [diff] [blame] | 130 | Builder.SetInsertPoint(StartBlock->begin()); |
| 131 | |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 132 | NodeBuilder.create(AstRoot); |
Johannes Doerfert | 0b169c0 | 2015-02-27 17:37:05 +0000 | [diff] [blame] | 133 | |
| 134 | assert(!verifyGeneratedFunction(S, *EnteringBB->getParent()) && |
| 135 | "Verification of generated function failed"); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 136 | return true; |
| 137 | } |
| 138 | |
Johannes Doerfert | 909a3bf | 2015-03-01 18:42:08 +0000 | [diff] [blame] | 139 | void printScop(raw_ostream &, Scop &) const override {} |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 140 | |
Johannes Doerfert | 909a3bf | 2015-03-01 18:42:08 +0000 | [diff] [blame] | 141 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Tobias Grosser | 42aff30 | 2014-01-13 22:29:56 +0000 | [diff] [blame] | 142 | AU.addRequired<DominatorTreeWrapperPass>(); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 143 | AU.addRequired<IslAstInfo>(); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 144 | AU.addRequired<RegionInfoPass>(); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 145 | AU.addRequired<ScalarEvolution>(); |
| 146 | AU.addRequired<ScopDetection>(); |
| 147 | AU.addRequired<ScopInfo>(); |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 148 | AU.addRequired<LoopInfoWrapperPass>(); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 149 | |
Johannes Doerfert | f6557f9 | 2015-03-04 22:43:40 +0000 | [diff] [blame] | 150 | AU.addPreserved<DependenceInfo>(); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 151 | |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 152 | AU.addPreserved<LoopInfoWrapperPass>(); |
Tobias Grosser | 42aff30 | 2014-01-13 22:29:56 +0000 | [diff] [blame] | 153 | AU.addPreserved<DominatorTreeWrapperPass>(); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 154 | AU.addPreserved<IslAstInfo>(); |
| 155 | AU.addPreserved<ScopDetection>(); |
| 156 | AU.addPreserved<ScalarEvolution>(); |
| 157 | |
| 158 | // FIXME: We do not yet add regions for the newly generated code to the |
| 159 | // region tree. |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 160 | AU.addPreserved<RegionInfoPass>(); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 161 | AU.addPreserved<TempScopInfo>(); |
| 162 | AU.addPreserved<ScopInfo>(); |
| 163 | AU.addPreservedID(IndependentBlocksID); |
| 164 | } |
| 165 | }; |
| 166 | } |
| 167 | |
Tobias Grosser | 09d3069 | 2015-05-12 07:45:52 +0000 | [diff] [blame^] | 168 | char CodeGeneration::ID = 1; |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 169 | |
Tobias Grosser | 09d3069 | 2015-05-12 07:45:52 +0000 | [diff] [blame^] | 170 | Pass *polly::createCodeGenerationPass() { return new CodeGeneration(); } |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 171 | |
Tobias Grosser | 09d3069 | 2015-05-12 07:45:52 +0000 | [diff] [blame^] | 172 | INITIALIZE_PASS_BEGIN(CodeGeneration, "polly-codegen", |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 173 | "Polly - Create LLVM-IR from SCoPs", false, false); |
Johannes Doerfert | f6557f9 | 2015-03-04 22:43:40 +0000 | [diff] [blame] | 174 | INITIALIZE_PASS_DEPENDENCY(DependenceInfo); |
Tobias Grosser | 42aff30 | 2014-01-13 22:29:56 +0000 | [diff] [blame] | 175 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 176 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 177 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 178 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution); |
| 179 | INITIALIZE_PASS_DEPENDENCY(ScopDetection); |
Tobias Grosser | 09d3069 | 2015-05-12 07:45:52 +0000 | [diff] [blame^] | 180 | INITIALIZE_PASS_END(CodeGeneration, "polly-codegen", |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 181 | "Polly - Create LLVM-IR from SCoPs", false, false) |