Sebastian Pop | 082cea8 | 2012-05-07 16:20:07 +0000 | [diff] [blame] | 1 | //===------ 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 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 | |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 22 | #include "polly/CodeGen/IslExprBuilder.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 23 | #include "polly/CodeGen/BlockGenerators.h" |
| 24 | #include "polly/CodeGen/CodeGeneration.h" |
| 25 | #include "polly/CodeGen/IslAst.h" |
| 26 | #include "polly/CodeGen/LoopGenerators.h" |
| 27 | #include "polly/CodeGen/Utils.h" |
Johannes Doerfert | f6557f9 | 2015-03-04 22:43:40 +0000 | [diff] [blame] | 28 | #include "polly/DependenceInfo.h" |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 29 | #include "polly/LinkAllPasses.h" |
| 30 | #include "polly/ScopInfo.h" |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 31 | #include "polly/Support/GICHelper.h" |
Tobias Grosser | 0ee50f6 | 2013-04-10 06:55:31 +0000 | [diff] [blame] | 32 | #include "polly/Support/ScopHelper.h" |
Tobias Grosser | e3c0558 | 2014-11-15 21:32:53 +0000 | [diff] [blame] | 33 | #include "polly/Support/SCEVValidator.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 34 | #include "polly/TempScopInfo.h" |
Tobias Grosser | e3c0558 | 2014-11-15 21:32:53 +0000 | [diff] [blame] | 35 | |
| 36 | #include "llvm/ADT/PostOrderIterator.h" |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 37 | #include "llvm/Analysis/PostDominators.h" |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 38 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 39 | #include "llvm/IR/Module.h" |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 40 | #include "llvm/Support/CommandLine.h" |
| 41 | #include "llvm/Support/Debug.h" |
Johannes Doerfert | 0b169c0 | 2015-02-27 17:37:05 +0000 | [diff] [blame] | 42 | #include "llvm/IR/Verifier.h" |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 43 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 44 | |
| 45 | #include "isl/union_map.h" |
| 46 | #include "isl/list.h" |
| 47 | #include "isl/ast.h" |
| 48 | #include "isl/ast_build.h" |
| 49 | #include "isl/set.h" |
| 50 | #include "isl/map.h" |
| 51 | #include "isl/aff.h" |
| 52 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 53 | using namespace polly; |
| 54 | using namespace llvm; |
| 55 | |
Chandler Carruth | 95fef94 | 2014-04-22 03:30:19 +0000 | [diff] [blame] | 56 | #define DEBUG_TYPE "polly-codegen-isl" |
| 57 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 58 | class IslNodeBuilder { |
| 59 | public: |
Johannes Doerfert | 51d1c74 | 2014-10-02 15:32:17 +0000 | [diff] [blame] | 60 | IslNodeBuilder(PollyIRBuilder &Builder, ScopAnnotator &Annotator, Pass *P, |
Tobias Grosser | e3c0558 | 2014-11-15 21:32:53 +0000 | [diff] [blame] | 61 | const DataLayout &DL, LoopInfo &LI, ScalarEvolution &SE, |
| 62 | DominatorTree &DT, Scop &S) |
Tobias Grosser | c989506 | 2015-03-10 15:24:33 +0000 | [diff] [blame] | 63 | : S(S), Builder(Builder), Annotator(Annotator), Rewriter(SE, DL, "polly"), |
Tobias Grosser | bb8d156 | 2015-03-04 19:33:31 +0000 | [diff] [blame] | 64 | ExprBuilder(Builder, IDToValue, Rewriter, DT, LI), |
Johannes Doerfert | f4af99b | 2015-03-08 21:38:35 +0000 | [diff] [blame] | 65 | BlockGen(Builder, LI, SE, DT, &ExprBuilder), RegionGen(BlockGen), P(P), |
| 66 | DL(DL), LI(LI), SE(SE), DT(DT) {} |
Johannes Doerfert | 2ef33e9 | 2014-10-05 11:33:59 +0000 | [diff] [blame] | 67 | |
Tobias Grosser | bb8d156 | 2015-03-04 19:33:31 +0000 | [diff] [blame] | 68 | ~IslNodeBuilder() {} |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 69 | |
| 70 | void addParameters(__isl_take isl_set *Context); |
| 71 | void create(__isl_take isl_ast_node *Node); |
Tobias Grosser | 54ee0ba | 2013-11-17 03:18:25 +0000 | [diff] [blame] | 72 | IslExprBuilder &getExprBuilder() { return ExprBuilder; } |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 73 | |
| 74 | private: |
Tobias Grosser | e3c0558 | 2014-11-15 21:32:53 +0000 | [diff] [blame] | 75 | Scop &S; |
Tobias Grosser | 5103ba7 | 2014-03-04 14:58:49 +0000 | [diff] [blame] | 76 | PollyIRBuilder &Builder; |
Johannes Doerfert | 51d1c74 | 2014-10-02 15:32:17 +0000 | [diff] [blame] | 77 | ScopAnnotator &Annotator; |
Johannes Doerfert | 2ef33e9 | 2014-10-05 11:33:59 +0000 | [diff] [blame] | 78 | |
| 79 | /// @brief A SCEVExpander to create llvm values from SCEVs. |
Tobias Grosser | bb8d156 | 2015-03-04 19:33:31 +0000 | [diff] [blame] | 80 | SCEVExpander Rewriter; |
Johannes Doerfert | 2ef33e9 | 2014-10-05 11:33:59 +0000 | [diff] [blame] | 81 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 82 | IslExprBuilder ExprBuilder; |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 83 | BlockGenerator BlockGen; |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 84 | |
| 85 | /// @brief Generator for region statements. |
| 86 | RegionGenerator RegionGen; |
| 87 | |
Johannes Doerfert | f4af99b | 2015-03-08 21:38:35 +0000 | [diff] [blame] | 88 | Pass *const P; |
Tobias Grosser | e3c0558 | 2014-11-15 21:32:53 +0000 | [diff] [blame] | 89 | const DataLayout &DL; |
Johannes Doerfert | 2ef3f4f | 2014-08-07 17:14:54 +0000 | [diff] [blame] | 90 | LoopInfo &LI; |
| 91 | ScalarEvolution &SE; |
| 92 | DominatorTree &DT; |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 93 | |
Tobias Grosser | e3c0558 | 2014-11-15 21:32:53 +0000 | [diff] [blame] | 94 | /// @brief The current iteration of out-of-scop loops |
| 95 | /// |
| 96 | /// This map provides for a given loop a llvm::Value that contains the current |
| 97 | /// loop iteration. |
| 98 | LoopToScevMapT OutsideLoopIterations; |
| 99 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 100 | // This maps an isl_id* to the Value* it has in the generated program. For now |
| 101 | // on, the only isl_ids that are stored here are the newly calculated loop |
| 102 | // ivs. |
Tobias Grosser | 566ad58 | 2014-08-31 16:21:12 +0000 | [diff] [blame] | 103 | IslExprBuilder::IDToValueTy IDToValue; |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 104 | |
Tobias Grosser | ec7d67e | 2014-11-06 00:27:01 +0000 | [diff] [blame] | 105 | /// Generate code for a given SCEV* |
| 106 | /// |
| 107 | /// This function generates code for a given SCEV expression. It generated |
| 108 | /// code is emmitted at the end of the basic block our Builder currently |
| 109 | /// points to and the resulting value is returned. |
| 110 | /// |
| 111 | /// @param Expr The expression to code generate. |
| 112 | Value *generateSCEV(const SCEV *Expr); |
| 113 | |
Tobias Grosser | e3c0558 | 2014-11-15 21:32:53 +0000 | [diff] [blame] | 114 | /// A set of Value -> Value remappings to apply when generating new code. |
| 115 | /// |
| 116 | /// When generating new code for a ScopStmt this map is used to map certain |
| 117 | /// llvm::Values to new llvm::Values. |
| 118 | ValueMapT ValueMap; |
| 119 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 120 | // Extract the upper bound of this loop |
| 121 | // |
| 122 | // The isl code generation can generate arbitrary expressions to check if the |
| 123 | // upper bound of a loop is reached, but it provides an option to enforce |
| 124 | // 'atomic' upper bounds. An 'atomic upper bound is always of the form |
| 125 | // iv <= expr, where expr is an (arbitrary) expression not containing iv. |
| 126 | // |
| 127 | // This function extracts 'atomic' upper bounds. Polly, in general, requires |
| 128 | // atomic upper bounds for the following reasons: |
| 129 | // |
| 130 | // 1. An atomic upper bound is loop invariant |
| 131 | // |
| 132 | // It must not be calculated at each loop iteration and can often even be |
| 133 | // hoisted out further by the loop invariant code motion. |
| 134 | // |
| 135 | // 2. OpenMP needs a loop invarient upper bound to calculate the number |
| 136 | // of loop iterations. |
| 137 | // |
| 138 | // 3. With the existing code, upper bounds have been easier to implement. |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 139 | __isl_give isl_ast_expr *getUpperBound(__isl_keep isl_ast_node *For, |
| 140 | CmpInst::Predicate &Predicate); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 141 | |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 142 | unsigned getNumberOfIterations(__isl_keep isl_ast_node *For); |
| 143 | |
Tobias Grosser | e3c0558 | 2014-11-15 21:32:53 +0000 | [diff] [blame] | 144 | /// Compute the values and loops referenced in this subtree. |
| 145 | /// |
| 146 | /// This function looks at all ScopStmts scheduled below the provided For node |
| 147 | /// and finds the llvm::Value[s] and llvm::Loops[s] which are referenced but |
| 148 | /// not locally defined. |
| 149 | /// |
| 150 | /// Values that can be synthesized or that are available as globals are |
| 151 | /// considered locally defined. |
| 152 | /// |
| 153 | /// Loops that contain the scop or that are part of the scop are considered |
| 154 | /// locally defined. Loops that are before the scop, but do not contain the |
| 155 | /// scop itself are considered not locally defined. |
| 156 | /// |
| 157 | /// @param For The node defining the subtree. |
| 158 | /// @param Values A vector that will be filled with the Values referenced in |
| 159 | /// this subtree. |
| 160 | /// @param Loops A vector that will be filled with the Loops referenced in |
| 161 | /// this subtree. |
| 162 | void getReferencesInSubtree(__isl_keep isl_ast_node *For, |
| 163 | SetVector<Value *> &Values, |
| 164 | SetVector<const Loop *> &Loops); |
| 165 | |
| 166 | /// Change the llvm::Value(s) used for code generation. |
| 167 | /// |
| 168 | /// When generating code certain values (e.g., references to induction |
| 169 | /// variables or array base pointers) in the original code may be replaced by |
| 170 | /// new values. This function allows to (partially) update the set of values |
| 171 | /// used. A typical use case for this function is the case when we continue |
| 172 | /// code generation in a subfunction/kernel function and need to explicitly |
| 173 | /// pass down certain values. |
| 174 | /// |
| 175 | /// @param NewValues A map that maps certain llvm::Values to new llvm::Values. |
| 176 | void updateValues(ParallelLoopGenerator::ValueToValueMapTy &NewValues); |
| 177 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 178 | void createFor(__isl_take isl_ast_node *For); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 179 | void createForVector(__isl_take isl_ast_node *For, int VectorWidth); |
| 180 | void createForSequential(__isl_take isl_ast_node *For); |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 181 | |
Tobias Grosser | e3c0558 | 2014-11-15 21:32:53 +0000 | [diff] [blame] | 182 | /// Create LLVM-IR that executes a for node thread parallel. |
| 183 | /// |
| 184 | /// @param For The FOR isl_ast_node for which code is generated. |
| 185 | void createForParallel(__isl_take isl_ast_node *For); |
| 186 | |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 187 | /// Generate LLVM-IR that computes the values of the original induction |
| 188 | /// variables in function of the newly generated loop induction variables. |
| 189 | /// |
| 190 | /// Example: |
| 191 | /// |
| 192 | /// // Original |
| 193 | /// for i |
| 194 | /// for j |
| 195 | /// S(i) |
| 196 | /// |
| 197 | /// Schedule: [i,j] -> [i+j, j] |
| 198 | /// |
| 199 | /// // New |
| 200 | /// for c0 |
| 201 | /// for c1 |
| 202 | /// S(c0 - c1, c1) |
| 203 | /// |
| 204 | /// Assuming the original code consists of two loops which are |
| 205 | /// transformed according to a schedule [i,j] -> [c0=i+j,c1=j]. The resulting |
| 206 | /// ast models the original statement as a call expression where each argument |
| 207 | /// is an expression that computes the old induction variables from the new |
| 208 | /// ones, ordered such that the first argument computes the value of induction |
| 209 | /// variable that was outermost in the original code. |
| 210 | /// |
| 211 | /// @param Expr The call expression that represents the statement. |
| 212 | /// @param Stmt The statement that is called. |
| 213 | /// @param VMap The value map into which the mapping from the old induction |
| 214 | /// variable to the new one is inserted. This mapping is used |
| 215 | /// for the classical code generation (not scev-based) and |
| 216 | /// gives an explicit mapping from an original, materialized |
| 217 | /// induction variable. It consequently can only be expressed |
| 218 | /// if there was an explicit induction variable. |
| 219 | /// @param LTS The loop to SCEV map in which the mapping from the original |
| 220 | /// loop to a SCEV representing the new loop iv is added. This |
| 221 | /// mapping does not require an explicit induction variable. |
| 222 | /// Instead, we think in terms of an implicit induction variable |
| 223 | /// that counts the number of times a loop is executed. For each |
| 224 | /// original loop this count, expressed in function of the new |
| 225 | /// induction variables, is added to the LTS map. |
| 226 | void createSubstitutions(__isl_take isl_ast_expr *Expr, ScopStmt *Stmt, |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 227 | ValueMapT &VMap, LoopToScevMapT <S); |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 228 | void createSubstitutionsVector(__isl_take isl_ast_expr *Expr, ScopStmt *Stmt, |
| 229 | VectorValueMapT &VMap, |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 230 | std::vector<LoopToScevMapT> &VLTS, |
| 231 | std::vector<Value *> &IVS, |
| 232 | __isl_take isl_id *IteratorID); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 233 | void createIf(__isl_take isl_ast_node *If); |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 234 | void createUserVector(__isl_take isl_ast_node *User, |
| 235 | std::vector<Value *> &IVS, |
| 236 | __isl_take isl_id *IteratorID, |
| 237 | __isl_take isl_union_map *Schedule); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 238 | void createUser(__isl_take isl_ast_node *User); |
| 239 | void createBlock(__isl_take isl_ast_node *Block); |
| 240 | }; |
| 241 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 242 | __isl_give isl_ast_expr * |
| 243 | IslNodeBuilder::getUpperBound(__isl_keep isl_ast_node *For, |
| 244 | ICmpInst::Predicate &Predicate) { |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 245 | isl_id *UBID, *IteratorID; |
| 246 | isl_ast_expr *Cond, *Iterator, *UB, *Arg0; |
Tobias Grosser | c967d8e | 2012-10-16 07:29:13 +0000 | [diff] [blame] | 247 | isl_ast_op_type Type; |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 248 | |
| 249 | Cond = isl_ast_node_for_get_cond(For); |
| 250 | Iterator = isl_ast_node_for_get_iterator(For); |
Tobias Grosser | 2784b08 | 2015-01-10 07:40:39 +0000 | [diff] [blame] | 251 | isl_ast_expr_get_type(Cond); |
Tobias Grosser | ae2d83e | 2012-12-29 23:57:18 +0000 | [diff] [blame] | 252 | assert(isl_ast_expr_get_type(Cond) == isl_ast_expr_op && |
| 253 | "conditional expression is not an atomic upper bound"); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 254 | |
Tobias Grosser | 2784b08 | 2015-01-10 07:40:39 +0000 | [diff] [blame] | 255 | Type = isl_ast_expr_get_op_type(Cond); |
| 256 | |
Tobias Grosser | c967d8e | 2012-10-16 07:29:13 +0000 | [diff] [blame] | 257 | switch (Type) { |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 258 | case isl_ast_op_le: |
| 259 | Predicate = ICmpInst::ICMP_SLE; |
| 260 | break; |
| 261 | case isl_ast_op_lt: |
| 262 | Predicate = ICmpInst::ICMP_SLT; |
| 263 | break; |
| 264 | default: |
| 265 | llvm_unreachable("Unexpected comparision type in loop conditon"); |
Tobias Grosser | c967d8e | 2012-10-16 07:29:13 +0000 | [diff] [blame] | 266 | } |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 267 | |
| 268 | Arg0 = isl_ast_expr_get_op_arg(Cond, 0); |
| 269 | |
Tobias Grosser | ae2d83e | 2012-12-29 23:57:18 +0000 | [diff] [blame] | 270 | assert(isl_ast_expr_get_type(Arg0) == isl_ast_expr_id && |
| 271 | "conditional expression is not an atomic upper bound"); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 272 | |
| 273 | UBID = isl_ast_expr_get_id(Arg0); |
| 274 | |
Tobias Grosser | ae2d83e | 2012-12-29 23:57:18 +0000 | [diff] [blame] | 275 | assert(isl_ast_expr_get_type(Iterator) == isl_ast_expr_id && |
| 276 | "Could not get the iterator"); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 277 | |
| 278 | IteratorID = isl_ast_expr_get_id(Iterator); |
| 279 | |
Tobias Grosser | ae2d83e | 2012-12-29 23:57:18 +0000 | [diff] [blame] | 280 | assert(UBID == IteratorID && |
| 281 | "conditional expression is not an atomic upper bound"); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 282 | |
| 283 | UB = isl_ast_expr_get_op_arg(Cond, 1); |
| 284 | |
| 285 | isl_ast_expr_free(Cond); |
| 286 | isl_ast_expr_free(Iterator); |
| 287 | isl_ast_expr_free(Arg0); |
| 288 | isl_id_free(IteratorID); |
| 289 | isl_id_free(UBID); |
| 290 | |
| 291 | return UB; |
| 292 | } |
| 293 | |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 294 | unsigned IslNodeBuilder::getNumberOfIterations(__isl_keep isl_ast_node *For) { |
Johannes Doerfert | 94d9082 | 2014-07-23 20:26:25 +0000 | [diff] [blame] | 295 | isl_union_map *Schedule = IslAstInfo::getSchedule(For); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 296 | isl_set *LoopDomain = isl_set_from_union_set(isl_union_map_range(Schedule)); |
Tobias Grosser | b68068b | 2015-04-27 10:38:45 +0000 | [diff] [blame] | 297 | int Dim = isl_set_dim(LoopDomain, isl_dim_set); |
| 298 | |
| 299 | // Calculate a map similar to the identity map, but with the last input |
| 300 | // and output dimension not related. |
| 301 | // [i0, i1, i2, i3] -> [i0, i1, i2, o0] |
| 302 | isl_space *Space = isl_set_get_space(LoopDomain); |
| 303 | Space = isl_space_drop_dims(Space, isl_dim_out, Dim - 1, 1); |
| 304 | Space = isl_space_map_from_set(Space); |
| 305 | isl_map *Identity = isl_map_identity(Space); |
| 306 | Identity = isl_map_add_dims(Identity, isl_dim_in, 1); |
| 307 | Identity = isl_map_add_dims(Identity, isl_dim_out, 1); |
| 308 | |
| 309 | LoopDomain = isl_set_reset_tuple_id(LoopDomain); |
| 310 | |
| 311 | isl_map *Map = isl_map_from_domain_and_range(isl_set_copy(LoopDomain), |
| 312 | isl_set_copy(LoopDomain)); |
| 313 | isl_set_free(LoopDomain); |
| 314 | Map = isl_map_intersect(Map, Identity); |
| 315 | |
| 316 | isl_map *LexMax = isl_map_lexmax(isl_map_copy(Map)); |
| 317 | isl_map *LexMin = isl_map_lexmin(Map); |
| 318 | isl_map *Sub = isl_map_sum(LexMax, isl_map_neg(LexMin)); |
| 319 | |
| 320 | isl_set *Elements = isl_map_range(Sub); |
| 321 | |
| 322 | if (!isl_set_is_singleton(Elements)) { |
| 323 | isl_set_free(Elements); |
Sebastian Pop | 2aa5c24 | 2012-12-18 08:56:51 +0000 | [diff] [blame] | 324 | return -1; |
Tobias Grosser | b68068b | 2015-04-27 10:38:45 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | isl_point *P = isl_set_sample_point(Elements); |
| 328 | |
| 329 | isl_val *V; |
| 330 | V = isl_point_get_coordinate_val(P, isl_dim_set, Dim - 1); |
| 331 | int NumberIterations = isl_val_get_num_si(V); |
| 332 | isl_val_free(V); |
| 333 | isl_point_free(P); |
| 334 | if (NumberIterations == -1) |
| 335 | return -1; |
| 336 | return NumberIterations + 1; |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Tobias Grosser | e3c0558 | 2014-11-15 21:32:53 +0000 | [diff] [blame] | 339 | struct FindValuesUser { |
| 340 | LoopInfo &LI; |
| 341 | ScalarEvolution &SE; |
| 342 | Region &R; |
| 343 | SetVector<Value *> &Values; |
| 344 | SetVector<const SCEV *> &SCEVs; |
| 345 | }; |
| 346 | |
Johannes Doerfert | bbf3084 | 2015-03-02 13:41:53 +0000 | [diff] [blame] | 347 | /// @brief Extract the values and SCEVs needed to generate code for a block. |
| 348 | static int findValuesInBlock(struct FindValuesUser &User, const ScopStmt *Stmt, |
| 349 | const BasicBlock *BB) { |
Tobias Grosser | e3c0558 | 2014-11-15 21:32:53 +0000 | [diff] [blame] | 350 | // Check all the operands of instructions in the basic block. |
| 351 | for (const Instruction &Inst : *BB) { |
| 352 | for (Value *SrcVal : Inst.operands()) { |
| 353 | if (Instruction *OpInst = dyn_cast<Instruction>(SrcVal)) |
| 354 | if (canSynthesize(OpInst, &User.LI, &User.SE, &User.R)) { |
| 355 | User.SCEVs.insert( |
| 356 | User.SE.getSCEVAtScope(OpInst, User.LI.getLoopFor(BB))); |
| 357 | continue; |
| 358 | } |
| 359 | if (Instruction *OpInst = dyn_cast<Instruction>(SrcVal)) |
| 360 | if (Stmt->getParent()->getRegion().contains(OpInst)) |
| 361 | continue; |
| 362 | |
| 363 | if (isa<Instruction>(SrcVal) || isa<Argument>(SrcVal)) |
| 364 | User.Values.insert(SrcVal); |
| 365 | } |
| 366 | } |
Johannes Doerfert | bbf3084 | 2015-03-02 13:41:53 +0000 | [diff] [blame] | 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | /// Extract the values and SCEVs needed to generate code for a ScopStmt. |
| 371 | /// |
| 372 | /// This function extracts a ScopStmt from a given isl_set and computes the |
| 373 | /// Values this statement depends on as well as a set of SCEV expressions that |
| 374 | /// need to be synthesized when generating code for this statment. |
| 375 | static int findValuesInStmt(isl_set *Set, void *UserPtr) { |
| 376 | isl_id *Id = isl_set_get_tuple_id(Set); |
| 377 | struct FindValuesUser &User = *static_cast<struct FindValuesUser *>(UserPtr); |
| 378 | const ScopStmt *Stmt = static_cast<const ScopStmt *>(isl_id_get_user(Id)); |
| 379 | |
| 380 | if (Stmt->isBlockStmt()) |
| 381 | findValuesInBlock(User, Stmt, Stmt->getBasicBlock()); |
| 382 | else { |
| 383 | assert(Stmt->isRegionStmt() && |
| 384 | "Stmt was neither block nor region statement"); |
| 385 | for (const BasicBlock *BB : Stmt->getRegion()->blocks()) |
| 386 | findValuesInBlock(User, Stmt, BB); |
| 387 | } |
| 388 | |
Tobias Grosser | e3c0558 | 2014-11-15 21:32:53 +0000 | [diff] [blame] | 389 | isl_id_free(Id); |
| 390 | isl_set_free(Set); |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | void IslNodeBuilder::getReferencesInSubtree(__isl_keep isl_ast_node *For, |
| 395 | SetVector<Value *> &Values, |
| 396 | SetVector<const Loop *> &Loops) { |
| 397 | |
| 398 | SetVector<const SCEV *> SCEVs; |
| 399 | struct FindValuesUser FindValues = {LI, SE, S.getRegion(), Values, SCEVs}; |
| 400 | |
| 401 | for (const auto &I : IDToValue) |
| 402 | Values.insert(I.second); |
| 403 | |
| 404 | for (const auto &I : OutsideLoopIterations) |
| 405 | Values.insert(cast<SCEVUnknown>(I.second)->getValue()); |
| 406 | |
| 407 | isl_union_set *Schedule = isl_union_map_domain(IslAstInfo::getSchedule(For)); |
| 408 | |
| 409 | isl_union_set_foreach_set(Schedule, findValuesInStmt, &FindValues); |
| 410 | isl_union_set_free(Schedule); |
| 411 | |
| 412 | for (const SCEV *Expr : SCEVs) { |
| 413 | findValues(Expr, Values); |
| 414 | findLoops(Expr, Loops); |
| 415 | } |
| 416 | |
| 417 | Values.remove_if([](const Value *V) { return isa<GlobalValue>(V); }); |
| 418 | |
| 419 | /// Remove loops that contain the scop or that are part of the scop, as they |
| 420 | /// are considered local. This leaves only loops that are before the scop, but |
| 421 | /// do not contain the scop itself. |
| 422 | Loops.remove_if([this](const Loop *L) { |
| 423 | return this->S.getRegion().contains(L) || |
| 424 | L->contains(S.getRegion().getEntry()); |
| 425 | }); |
| 426 | } |
| 427 | |
| 428 | void IslNodeBuilder::updateValues( |
| 429 | ParallelLoopGenerator::ValueToValueMapTy &NewValues) { |
| 430 | SmallPtrSet<Value *, 5> Inserted; |
| 431 | |
| 432 | for (const auto &I : IDToValue) { |
| 433 | IDToValue[I.first] = NewValues[I.second]; |
| 434 | Inserted.insert(I.second); |
| 435 | } |
| 436 | |
| 437 | for (const auto &I : NewValues) { |
| 438 | if (Inserted.count(I.first)) |
| 439 | continue; |
| 440 | |
| 441 | ValueMap[I.first] = I.second; |
| 442 | } |
| 443 | } |
| 444 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 445 | void IslNodeBuilder::createUserVector(__isl_take isl_ast_node *User, |
| 446 | std::vector<Value *> &IVS, |
| 447 | __isl_take isl_id *IteratorID, |
| 448 | __isl_take isl_union_map *Schedule) { |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 449 | isl_ast_expr *Expr = isl_ast_node_user_get_expr(User); |
| 450 | isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0); |
| 451 | isl_id *Id = isl_ast_expr_get_id(StmtExpr); |
| 452 | isl_ast_expr_free(StmtExpr); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 453 | ScopStmt *Stmt = (ScopStmt *)isl_id_get_user(Id); |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 454 | Stmt->setAstBuild(IslAstInfo::getBuild(User)); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 455 | VectorValueMapT VectorMap(IVS.size()); |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 456 | std::vector<LoopToScevMapT> VLTS(IVS.size()); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 457 | |
| 458 | isl_union_set *Domain = isl_union_set_from_set(Stmt->getDomain()); |
| 459 | Schedule = isl_union_map_intersect_domain(Schedule, Domain); |
| 460 | isl_map *S = isl_map_from_union_map(Schedule); |
| 461 | |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 462 | createSubstitutionsVector(Expr, Stmt, VectorMap, VLTS, IVS, IteratorID); |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 463 | VectorBlockGenerator::generate(BlockGen, *Stmt, VectorMap, VLTS, S); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 464 | |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 465 | isl_map_free(S); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 466 | isl_id_free(Id); |
| 467 | isl_ast_node_free(User); |
| 468 | } |
| 469 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 470 | void IslNodeBuilder::createForVector(__isl_take isl_ast_node *For, |
| 471 | int VectorWidth) { |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 472 | isl_ast_node *Body = isl_ast_node_for_get_body(For); |
| 473 | isl_ast_expr *Init = isl_ast_node_for_get_init(For); |
| 474 | isl_ast_expr *Inc = isl_ast_node_for_get_inc(For); |
| 475 | isl_ast_expr *Iterator = isl_ast_node_for_get_iterator(For); |
| 476 | isl_id *IteratorID = isl_ast_expr_get_id(Iterator); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 477 | |
| 478 | Value *ValueLB = ExprBuilder.create(Init); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 479 | Value *ValueInc = ExprBuilder.create(Inc); |
| 480 | |
| 481 | Type *MaxType = ExprBuilder.getType(Iterator); |
| 482 | MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType()); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 483 | MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType()); |
| 484 | |
| 485 | if (MaxType != ValueLB->getType()) |
| 486 | ValueLB = Builder.CreateSExt(ValueLB, MaxType); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 487 | if (MaxType != ValueInc->getType()) |
| 488 | ValueInc = Builder.CreateSExt(ValueInc, MaxType); |
| 489 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 490 | std::vector<Value *> IVS(VectorWidth); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 491 | IVS[0] = ValueLB; |
| 492 | |
| 493 | for (int i = 1; i < VectorWidth; i++) |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 494 | IVS[i] = Builder.CreateAdd(IVS[i - 1], ValueInc, "p_vector_iv"); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 495 | |
Johannes Doerfert | 94d9082 | 2014-07-23 20:26:25 +0000 | [diff] [blame] | 496 | isl_union_map *Schedule = IslAstInfo::getSchedule(For); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 497 | assert(Schedule && "For statement annotation does not contain its schedule"); |
| 498 | |
| 499 | IDToValue[IteratorID] = ValueLB; |
| 500 | |
| 501 | switch (isl_ast_node_get_type(Body)) { |
| 502 | case isl_ast_node_user: |
| 503 | createUserVector(Body, IVS, isl_id_copy(IteratorID), |
| 504 | isl_union_map_copy(Schedule)); |
| 505 | break; |
| 506 | case isl_ast_node_block: { |
| 507 | isl_ast_node_list *List = isl_ast_node_block_get_children(Body); |
| 508 | |
| 509 | for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i) |
| 510 | createUserVector(isl_ast_node_list_get_ast_node(List, i), IVS, |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 511 | isl_id_copy(IteratorID), isl_union_map_copy(Schedule)); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 512 | |
| 513 | isl_ast_node_free(Body); |
| 514 | isl_ast_node_list_free(List); |
| 515 | break; |
| 516 | } |
| 517 | default: |
| 518 | isl_ast_node_dump(Body); |
| 519 | llvm_unreachable("Unhandled isl_ast_node in vectorizer"); |
| 520 | } |
| 521 | |
Tobias Grosser | e3c0558 | 2014-11-15 21:32:53 +0000 | [diff] [blame] | 522 | IDToValue.erase(IDToValue.find(IteratorID)); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 523 | isl_id_free(IteratorID); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 524 | isl_union_map_free(Schedule); |
| 525 | |
| 526 | isl_ast_node_free(For); |
| 527 | isl_ast_expr_free(Iterator); |
| 528 | } |
| 529 | |
| 530 | void IslNodeBuilder::createForSequential(__isl_take isl_ast_node *For) { |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 531 | isl_ast_node *Body; |
| 532 | isl_ast_expr *Init, *Inc, *Iterator, *UB; |
| 533 | isl_id *IteratorID; |
| 534 | Value *ValueLB, *ValueUB, *ValueInc; |
| 535 | Type *MaxType; |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 536 | BasicBlock *ExitBlock; |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 537 | Value *IV; |
Tobias Grosser | c967d8e | 2012-10-16 07:29:13 +0000 | [diff] [blame] | 538 | CmpInst::Predicate Predicate; |
Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 539 | bool Parallel; |
| 540 | |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 541 | Parallel = |
| 542 | IslAstInfo::isParallel(For) && !IslAstInfo::isReductionParallel(For); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 543 | |
| 544 | Body = isl_ast_node_for_get_body(For); |
| 545 | |
| 546 | // isl_ast_node_for_is_degenerate(For) |
| 547 | // |
| 548 | // TODO: For degenerated loops we could generate a plain assignment. |
| 549 | // However, for now we just reuse the logic for normal loops, which will |
| 550 | // create a loop with a single iteration. |
| 551 | |
| 552 | Init = isl_ast_node_for_get_init(For); |
| 553 | Inc = isl_ast_node_for_get_inc(For); |
| 554 | Iterator = isl_ast_node_for_get_iterator(For); |
| 555 | IteratorID = isl_ast_expr_get_id(Iterator); |
Tobias Grosser | c967d8e | 2012-10-16 07:29:13 +0000 | [diff] [blame] | 556 | UB = getUpperBound(For, Predicate); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 557 | |
| 558 | ValueLB = ExprBuilder.create(Init); |
| 559 | ValueUB = ExprBuilder.create(UB); |
| 560 | ValueInc = ExprBuilder.create(Inc); |
| 561 | |
| 562 | MaxType = ExprBuilder.getType(Iterator); |
| 563 | MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType()); |
| 564 | MaxType = ExprBuilder.getWidestType(MaxType, ValueUB->getType()); |
| 565 | MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType()); |
| 566 | |
| 567 | if (MaxType != ValueLB->getType()) |
| 568 | ValueLB = Builder.CreateSExt(ValueLB, MaxType); |
| 569 | if (MaxType != ValueUB->getType()) |
| 570 | ValueUB = Builder.CreateSExt(ValueUB, MaxType); |
| 571 | if (MaxType != ValueInc->getType()) |
| 572 | ValueInc = Builder.CreateSExt(ValueInc, MaxType); |
| 573 | |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame] | 574 | // If we can show that LB <Predicate> UB holds at least once, we can |
| 575 | // omit the GuardBB in front of the loop. |
| 576 | bool UseGuardBB = |
| 577 | !SE.isKnownPredicate(Predicate, SE.getSCEV(ValueLB), SE.getSCEV(ValueUB)); |
Johannes Doerfert | 2ef3f4f | 2014-08-07 17:14:54 +0000 | [diff] [blame] | 578 | IV = createLoop(ValueLB, ValueUB, ValueInc, Builder, P, LI, DT, ExitBlock, |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame] | 579 | Predicate, &Annotator, Parallel, UseGuardBB); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 580 | IDToValue[IteratorID] = IV; |
| 581 | |
| 582 | create(Body); |
| 583 | |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 584 | Annotator.popLoop(Parallel); |
Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 585 | |
Tobias Grosser | e3c0558 | 2014-11-15 21:32:53 +0000 | [diff] [blame] | 586 | IDToValue.erase(IDToValue.find(IteratorID)); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 587 | |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 588 | Builder.SetInsertPoint(ExitBlock->begin()); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 589 | |
| 590 | isl_ast_node_free(For); |
| 591 | isl_ast_expr_free(Iterator); |
| 592 | isl_id_free(IteratorID); |
| 593 | } |
| 594 | |
Tobias Grosser | e3c0558 | 2014-11-15 21:32:53 +0000 | [diff] [blame] | 595 | /// @brief Remove the BBs contained in a (sub)function from the dominator tree. |
| 596 | /// |
| 597 | /// This function removes the basic blocks that are part of a subfunction from |
| 598 | /// the dominator tree. Specifically, when generating code it may happen that at |
| 599 | /// some point the code generation continues in a new sub-function (e.g., when |
| 600 | /// generating OpenMP code). The basic blocks that are created in this |
| 601 | /// sub-function are then still part of the dominator tree of the original |
| 602 | /// function, such that the dominator tree reaches over function boundaries. |
| 603 | /// This is not only incorrect, but also causes crashes. This function now |
| 604 | /// removes from the dominator tree all basic blocks that are dominated (and |
| 605 | /// consequently reachable) from the entry block of this (sub)function. |
| 606 | /// |
| 607 | /// FIXME: A LLVM (function or region) pass should not touch anything outside of |
| 608 | /// the function/region it runs on. Hence, the pure need for this function shows |
| 609 | /// that we do not comply to this rule. At the moment, this does not cause any |
| 610 | /// issues, but we should be aware that such issues may appear. Unfortunately |
| 611 | /// the current LLVM pass infrastructure does not allow to make Polly a module |
| 612 | /// or call-graph pass to solve this issue, as such a pass would not have access |
| 613 | /// to the per-function analyses passes needed by Polly. A future pass manager |
| 614 | /// infrastructure is supposed to enable such kind of access possibly allowing |
| 615 | /// us to create a cleaner solution here. |
| 616 | /// |
| 617 | /// FIXME: Instead of adding the dominance information and then dropping it |
| 618 | /// later on, we should try to just not add it in the first place. This requires |
| 619 | /// some careful testing to make sure this does not break in interaction with |
| 620 | /// the SCEVBuilder and SplitBlock which may rely on the dominator tree or |
| 621 | /// which may try to update it. |
| 622 | /// |
| 623 | /// @param F The function which contains the BBs to removed. |
| 624 | /// @param DT The dominator tree from which to remove the BBs. |
| 625 | static void removeSubFuncFromDomTree(Function *F, DominatorTree &DT) { |
| 626 | DomTreeNode *N = DT.getNode(&F->getEntryBlock()); |
| 627 | std::vector<BasicBlock *> Nodes; |
| 628 | |
| 629 | // We can only remove an element from the dominator tree, if all its children |
| 630 | // have been removed. To ensure this we obtain the list of nodes to remove |
| 631 | // using a post-order tree traversal. |
| 632 | for (po_iterator<DomTreeNode *> I = po_begin(N), E = po_end(N); I != E; ++I) |
| 633 | Nodes.push_back(I->getBlock()); |
| 634 | |
| 635 | for (BasicBlock *BB : Nodes) |
| 636 | DT.eraseNode(BB); |
| 637 | } |
| 638 | |
| 639 | void IslNodeBuilder::createForParallel(__isl_take isl_ast_node *For) { |
| 640 | isl_ast_node *Body; |
| 641 | isl_ast_expr *Init, *Inc, *Iterator, *UB; |
| 642 | isl_id *IteratorID; |
| 643 | Value *ValueLB, *ValueUB, *ValueInc; |
| 644 | Type *MaxType; |
| 645 | Value *IV; |
| 646 | CmpInst::Predicate Predicate; |
| 647 | |
| 648 | Body = isl_ast_node_for_get_body(For); |
| 649 | Init = isl_ast_node_for_get_init(For); |
| 650 | Inc = isl_ast_node_for_get_inc(For); |
| 651 | Iterator = isl_ast_node_for_get_iterator(For); |
| 652 | IteratorID = isl_ast_expr_get_id(Iterator); |
| 653 | UB = getUpperBound(For, Predicate); |
| 654 | |
| 655 | ValueLB = ExprBuilder.create(Init); |
| 656 | ValueUB = ExprBuilder.create(UB); |
| 657 | ValueInc = ExprBuilder.create(Inc); |
| 658 | |
| 659 | // OpenMP always uses SLE. In case the isl generated AST uses a SLT |
| 660 | // expression, we need to adjust the loop blound by one. |
| 661 | if (Predicate == CmpInst::ICMP_SLT) |
| 662 | ValueUB = Builder.CreateAdd( |
| 663 | ValueUB, Builder.CreateSExt(Builder.getTrue(), ValueUB->getType())); |
| 664 | |
| 665 | MaxType = ExprBuilder.getType(Iterator); |
| 666 | MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType()); |
| 667 | MaxType = ExprBuilder.getWidestType(MaxType, ValueUB->getType()); |
| 668 | MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType()); |
| 669 | |
| 670 | if (MaxType != ValueLB->getType()) |
| 671 | ValueLB = Builder.CreateSExt(ValueLB, MaxType); |
| 672 | if (MaxType != ValueUB->getType()) |
| 673 | ValueUB = Builder.CreateSExt(ValueUB, MaxType); |
| 674 | if (MaxType != ValueInc->getType()) |
| 675 | ValueInc = Builder.CreateSExt(ValueInc, MaxType); |
| 676 | |
| 677 | BasicBlock::iterator LoopBody; |
| 678 | |
| 679 | SetVector<Value *> SubtreeValues; |
| 680 | SetVector<const Loop *> Loops; |
| 681 | |
| 682 | getReferencesInSubtree(For, SubtreeValues, Loops); |
| 683 | |
| 684 | // Create for all loops we depend on values that contain the current loop |
| 685 | // iteration. These values are necessary to generate code for SCEVs that |
| 686 | // depend on such loops. As a result we need to pass them to the subfunction. |
| 687 | for (const Loop *L : Loops) { |
| 688 | const SCEV *OuterLIV = SE.getAddRecExpr(SE.getUnknown(Builder.getInt64(0)), |
| 689 | SE.getUnknown(Builder.getInt64(1)), |
| 690 | L, SCEV::FlagAnyWrap); |
| 691 | Value *V = generateSCEV(OuterLIV); |
| 692 | OutsideLoopIterations[L] = SE.getUnknown(V); |
| 693 | SubtreeValues.insert(V); |
| 694 | } |
| 695 | |
| 696 | ParallelLoopGenerator::ValueToValueMapTy NewValues; |
| 697 | ParallelLoopGenerator ParallelLoopGen(Builder, P, LI, DT, DL); |
| 698 | |
| 699 | IV = ParallelLoopGen.createParallelLoop(ValueLB, ValueUB, ValueInc, |
| 700 | SubtreeValues, NewValues, &LoopBody); |
| 701 | BasicBlock::iterator AfterLoop = Builder.GetInsertPoint(); |
| 702 | Builder.SetInsertPoint(LoopBody); |
| 703 | |
| 704 | // Save the current values. |
| 705 | ValueMapT ValueMapCopy = ValueMap; |
| 706 | IslExprBuilder::IDToValueTy IDToValueCopy = IDToValue; |
| 707 | |
| 708 | updateValues(NewValues); |
| 709 | IDToValue[IteratorID] = IV; |
| 710 | |
| 711 | create(Body); |
| 712 | |
| 713 | // Restore the original values. |
| 714 | ValueMap = ValueMapCopy; |
| 715 | IDToValue = IDToValueCopy; |
| 716 | |
| 717 | Builder.SetInsertPoint(AfterLoop); |
| 718 | removeSubFuncFromDomTree((*LoopBody).getParent()->getParent(), DT); |
| 719 | |
| 720 | for (const Loop *L : Loops) |
| 721 | OutsideLoopIterations.erase(L); |
| 722 | |
| 723 | isl_ast_node_free(For); |
| 724 | isl_ast_expr_free(Iterator); |
| 725 | isl_id_free(IteratorID); |
| 726 | } |
| 727 | |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 728 | void IslNodeBuilder::createFor(__isl_take isl_ast_node *For) { |
Tobias Grosser | 7527e3f | 2015-04-05 06:53:21 +0000 | [diff] [blame] | 729 | bool Vector = PollyVectorizerChoice == VECTORIZER_POLLY; |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 730 | |
Johannes Doerfert | ed67f8b | 2014-08-01 08:14:28 +0000 | [diff] [blame] | 731 | if (Vector && IslAstInfo::isInnermostParallel(For) && |
| 732 | !IslAstInfo::isReductionParallel(For)) { |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 733 | int VectorWidth = getNumberOfIterations(For); |
| 734 | if (1 < VectorWidth && VectorWidth <= 16) { |
| 735 | createForVector(For, VectorWidth); |
| 736 | return; |
| 737 | } |
| 738 | } |
Tobias Grosser | e3c0558 | 2014-11-15 21:32:53 +0000 | [diff] [blame] | 739 | |
| 740 | if (IslAstInfo::isExecutedInParallel(For)) { |
| 741 | createForParallel(For); |
| 742 | return; |
| 743 | } |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 744 | createForSequential(For); |
| 745 | } |
| 746 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 747 | void IslNodeBuilder::createIf(__isl_take isl_ast_node *If) { |
| 748 | isl_ast_expr *Cond = isl_ast_node_if_get_cond(If); |
| 749 | |
| 750 | Function *F = Builder.GetInsertBlock()->getParent(); |
| 751 | LLVMContext &Context = F->getContext(); |
| 752 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 753 | BasicBlock *CondBB = |
Chandler Carruth | 5ec3333 | 2015-01-18 10:52:23 +0000 | [diff] [blame] | 754 | SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 755 | CondBB->setName("polly.cond"); |
Chandler Carruth | 5ec3333 | 2015-01-18 10:52:23 +0000 | [diff] [blame] | 756 | BasicBlock *MergeBB = SplitBlock(CondBB, CondBB->begin(), &DT, &LI); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 757 | MergeBB->setName("polly.merge"); |
| 758 | BasicBlock *ThenBB = BasicBlock::Create(Context, "polly.then", F); |
| 759 | BasicBlock *ElseBB = BasicBlock::Create(Context, "polly.else", F); |
| 760 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 761 | DT.addNewBlock(ThenBB, CondBB); |
| 762 | DT.addNewBlock(ElseBB, CondBB); |
| 763 | DT.changeImmediateDominator(MergeBB, CondBB); |
| 764 | |
Tobias Grosser | 3081b0f | 2013-05-16 06:40:24 +0000 | [diff] [blame] | 765 | Loop *L = LI.getLoopFor(CondBB); |
| 766 | if (L) { |
Chandler Carruth | 6adcf56 | 2015-01-18 01:47:30 +0000 | [diff] [blame] | 767 | L->addBasicBlockToLoop(ThenBB, LI); |
| 768 | L->addBasicBlockToLoop(ElseBB, LI); |
Tobias Grosser | 3081b0f | 2013-05-16 06:40:24 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 771 | CondBB->getTerminator()->eraseFromParent(); |
| 772 | |
| 773 | Builder.SetInsertPoint(CondBB); |
| 774 | Value *Predicate = ExprBuilder.create(Cond); |
| 775 | Builder.CreateCondBr(Predicate, ThenBB, ElseBB); |
| 776 | Builder.SetInsertPoint(ThenBB); |
| 777 | Builder.CreateBr(MergeBB); |
| 778 | Builder.SetInsertPoint(ElseBB); |
| 779 | Builder.CreateBr(MergeBB); |
| 780 | Builder.SetInsertPoint(ThenBB->begin()); |
| 781 | |
| 782 | create(isl_ast_node_if_get_then(If)); |
| 783 | |
| 784 | Builder.SetInsertPoint(ElseBB->begin()); |
| 785 | |
| 786 | if (isl_ast_node_if_has_else(If)) |
| 787 | create(isl_ast_node_if_get_else(If)); |
| 788 | |
| 789 | Builder.SetInsertPoint(MergeBB->begin()); |
| 790 | |
| 791 | isl_ast_node_free(If); |
| 792 | } |
| 793 | |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 794 | void IslNodeBuilder::createSubstitutions(isl_ast_expr *Expr, ScopStmt *Stmt, |
| 795 | ValueMapT &VMap, LoopToScevMapT <S) { |
| 796 | assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op && |
| 797 | "Expression of type 'op' expected"); |
| 798 | assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_call && |
| 799 | "Opertation of type 'call' expected"); |
| 800 | for (int i = 0; i < isl_ast_expr_get_op_n_arg(Expr) - 1; ++i) { |
| 801 | isl_ast_expr *SubExpr; |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 802 | Value *V; |
| 803 | |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 804 | SubExpr = isl_ast_expr_get_op_arg(Expr, i + 1); |
| 805 | V = ExprBuilder.create(SubExpr); |
Sebastian Pop | e039bb1 | 2013-03-18 19:09:49 +0000 | [diff] [blame] | 806 | ScalarEvolution *SE = Stmt->getParent()->getSE(); |
| 807 | LTS[Stmt->getLoopForDimension(i)] = SE->getUnknown(V); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Tobias Grosser | e3c0558 | 2014-11-15 21:32:53 +0000 | [diff] [blame] | 810 | // Add the current ValueMap to our per-statement value map. |
| 811 | // |
| 812 | // This is needed e.g. to rewrite array base addresses when moving code |
| 813 | // into a parallely executed subfunction. |
| 814 | VMap.insert(ValueMap.begin(), ValueMap.end()); |
| 815 | |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 816 | isl_ast_expr_free(Expr); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 819 | void IslNodeBuilder::createSubstitutionsVector( |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 820 | __isl_take isl_ast_expr *Expr, ScopStmt *Stmt, VectorValueMapT &VMap, |
| 821 | std::vector<LoopToScevMapT> &VLTS, std::vector<Value *> &IVS, |
| 822 | __isl_take isl_id *IteratorID) { |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 823 | int i = 0; |
| 824 | |
| 825 | Value *OldValue = IDToValue[IteratorID]; |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 826 | for (Value *IV : IVS) { |
| 827 | IDToValue[IteratorID] = IV; |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 828 | createSubstitutions(isl_ast_expr_copy(Expr), Stmt, VMap[i], VLTS[i]); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 829 | i++; |
| 830 | } |
| 831 | |
| 832 | IDToValue[IteratorID] = OldValue; |
| 833 | isl_id_free(IteratorID); |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 834 | isl_ast_expr_free(Expr); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | void IslNodeBuilder::createUser(__isl_take isl_ast_node *User) { |
| 838 | ValueMapT VMap; |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 839 | LoopToScevMapT LTS; |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 840 | isl_id *Id; |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 841 | ScopStmt *Stmt; |
| 842 | |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 843 | isl_ast_expr *Expr = isl_ast_node_user_get_expr(User); |
| 844 | isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0); |
| 845 | Id = isl_ast_expr_get_id(StmtExpr); |
| 846 | isl_ast_expr_free(StmtExpr); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 847 | |
Tobias Grosser | e3c0558 | 2014-11-15 21:32:53 +0000 | [diff] [blame] | 848 | LTS.insert(OutsideLoopIterations.begin(), OutsideLoopIterations.end()); |
| 849 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 850 | Stmt = (ScopStmt *)isl_id_get_user(Id); |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 851 | Stmt->setAstBuild(IslAstInfo::getBuild(User)); |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 852 | |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 853 | createSubstitutions(Expr, Stmt, VMap, LTS); |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 854 | if (Stmt->isBlockStmt()) |
| 855 | BlockGen.copyStmt(*Stmt, VMap, LTS); |
| 856 | else |
| 857 | RegionGen.copyStmt(*Stmt, VMap, LTS); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 858 | |
| 859 | isl_ast_node_free(User); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 860 | isl_id_free(Id); |
| 861 | } |
| 862 | |
| 863 | void IslNodeBuilder::createBlock(__isl_take isl_ast_node *Block) { |
| 864 | isl_ast_node_list *List = isl_ast_node_block_get_children(Block); |
| 865 | |
| 866 | for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i) |
| 867 | create(isl_ast_node_list_get_ast_node(List, i)); |
| 868 | |
| 869 | isl_ast_node_free(Block); |
| 870 | isl_ast_node_list_free(List); |
| 871 | } |
| 872 | |
| 873 | void IslNodeBuilder::create(__isl_take isl_ast_node *Node) { |
| 874 | switch (isl_ast_node_get_type(Node)) { |
| 875 | case isl_ast_node_error: |
Tobias Grosser | 29e36dc | 2015-03-30 17:28:57 +0000 | [diff] [blame] | 876 | llvm_unreachable("code generation error"); |
| 877 | case isl_ast_node_mark: |
| 878 | llvm_unreachable("Mark node unexpected"); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 879 | case isl_ast_node_for: |
| 880 | createFor(Node); |
| 881 | return; |
| 882 | case isl_ast_node_if: |
| 883 | createIf(Node); |
| 884 | return; |
| 885 | case isl_ast_node_user: |
| 886 | createUser(Node); |
| 887 | return; |
| 888 | case isl_ast_node_block: |
| 889 | createBlock(Node); |
| 890 | return; |
| 891 | } |
| 892 | |
| 893 | llvm_unreachable("Unknown isl_ast_node type"); |
| 894 | } |
| 895 | |
| 896 | void IslNodeBuilder::addParameters(__isl_take isl_set *Context) { |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 897 | |
| 898 | for (unsigned i = 0; i < isl_set_dim(Context, isl_dim_param); ++i) { |
| 899 | isl_id *Id; |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 900 | |
| 901 | Id = isl_set_get_dim_id(Context, isl_dim_param, i); |
Tobias Grosser | ec7d67e | 2014-11-06 00:27:01 +0000 | [diff] [blame] | 902 | IDToValue[Id] = generateSCEV((const SCEV *)isl_id_get_user(Id)); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 903 | |
| 904 | isl_id_free(Id); |
| 905 | } |
| 906 | |
Tobias Grosser | e3c0558 | 2014-11-15 21:32:53 +0000 | [diff] [blame] | 907 | // Generate values for the current loop iteration for all surrounding loops. |
| 908 | // |
| 909 | // We may also reference loops outside of the scop which do not contain the |
| 910 | // scop itself, but as the number of such scops may be arbitrarily large we do |
| 911 | // not generate code for them here, but only at the point of code generation |
| 912 | // where these values are needed. |
| 913 | Region &R = S.getRegion(); |
| 914 | Loop *L = LI.getLoopFor(R.getEntry()); |
| 915 | |
| 916 | while (L != nullptr && R.contains(L)) |
| 917 | L = L->getParentLoop(); |
| 918 | |
| 919 | while (L != nullptr) { |
| 920 | const SCEV *OuterLIV = SE.getAddRecExpr(SE.getUnknown(Builder.getInt64(0)), |
| 921 | SE.getUnknown(Builder.getInt64(1)), |
| 922 | L, SCEV::FlagAnyWrap); |
| 923 | Value *V = generateSCEV(OuterLIV); |
| 924 | OutsideLoopIterations[L] = SE.getUnknown(V); |
| 925 | L = L->getParentLoop(); |
| 926 | } |
| 927 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 928 | isl_set_free(Context); |
| 929 | } |
| 930 | |
Tobias Grosser | ec7d67e | 2014-11-06 00:27:01 +0000 | [diff] [blame] | 931 | Value *IslNodeBuilder::generateSCEV(const SCEV *Expr) { |
| 932 | Instruction *InsertLocation = --(Builder.GetInsertBlock()->end()); |
Tobias Grosser | bb8d156 | 2015-03-04 19:33:31 +0000 | [diff] [blame] | 933 | return Rewriter.expandCodeFor(Expr, Expr->getType(), InsertLocation); |
Tobias Grosser | ec7d67e | 2014-11-06 00:27:01 +0000 | [diff] [blame] | 934 | } |
| 935 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 936 | namespace { |
| 937 | class IslCodeGeneration : public ScopPass { |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 938 | public: |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 939 | static char ID; |
| 940 | |
| 941 | IslCodeGeneration() : ScopPass(ID) {} |
| 942 | |
Tobias Grosser | e3c0558 | 2014-11-15 21:32:53 +0000 | [diff] [blame] | 943 | /// @brief The datalayout used |
| 944 | const DataLayout *DL; |
| 945 | |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame] | 946 | /// @name The analysis passes we need to generate code. |
| 947 | /// |
| 948 | ///{ |
| 949 | LoopInfo *LI; |
| 950 | IslAstInfo *AI; |
| 951 | DominatorTree *DT; |
| 952 | ScalarEvolution *SE; |
| 953 | ///} |
| 954 | |
| 955 | /// @brief The loop annotator to generate llvm.loop metadata. |
Johannes Doerfert | 51d1c74 | 2014-10-02 15:32:17 +0000 | [diff] [blame] | 956 | ScopAnnotator Annotator; |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame] | 957 | |
| 958 | /// @brief Build the runtime condition. |
| 959 | /// |
| 960 | /// Build the condition that evaluates at run-time to true iff all |
| 961 | /// assumptions taken for the SCoP hold, and to false otherwise. |
| 962 | /// |
| 963 | /// @return A value evaluating to true/false if execution is save/unsafe. |
| 964 | Value *buildRTC(PollyIRBuilder &Builder, IslExprBuilder &ExprBuilder) { |
| 965 | Builder.SetInsertPoint(Builder.GetInsertBlock()->getTerminator()); |
| 966 | Value *RTC = ExprBuilder.create(AI->getRunCondition()); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 967 | if (!RTC->getType()->isIntegerTy(1)) |
| 968 | RTC = Builder.CreateIsNotNull(RTC); |
| 969 | return RTC; |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Johannes Doerfert | 0b169c0 | 2015-02-27 17:37:05 +0000 | [diff] [blame] | 972 | bool verifyGeneratedFunction(Scop &S, Function &F) { |
| 973 | if (!verifyFunction(F)) |
| 974 | return false; |
| 975 | |
| 976 | DEBUG({ |
| 977 | errs() << "== ISL Codegen created an invalid function ==\n\n== The " |
| 978 | "SCoP ==\n"; |
| 979 | S.print(errs()); |
| 980 | errs() << "\n== The isl AST ==\n"; |
Johannes Doerfert | 3fe584d | 2015-03-01 18:40:25 +0000 | [diff] [blame] | 981 | AI->printScop(errs(), S); |
Johannes Doerfert | 0b169c0 | 2015-02-27 17:37:05 +0000 | [diff] [blame] | 982 | errs() << "\n== The invalid function ==\n"; |
| 983 | F.print(errs()); |
| 984 | errs() << "\n== The errors ==\n"; |
| 985 | verifyFunction(F, &errs()); |
| 986 | }); |
| 987 | |
| 988 | return true; |
| 989 | } |
| 990 | |
Johannes Doerfert | 909a3bf | 2015-03-01 18:42:08 +0000 | [diff] [blame] | 991 | bool runOnScop(Scop &S) override { |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame] | 992 | AI = &getAnalysis<IslAstInfo>(); |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 993 | |
| 994 | // Check if we created an isl_ast root node, otherwise exit. |
| 995 | isl_ast_node *AstRoot = AI->getAst(); |
| 996 | if (!AstRoot) |
| 997 | return false; |
| 998 | |
| 999 | LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame] | 1000 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 1001 | SE = &getAnalysis<ScalarEvolution>(); |
Tobias Grosser | 140b394 | 2015-03-05 09:48:20 +0000 | [diff] [blame] | 1002 | DL = &S.getRegion().getEntry()->getParent()->getParent()->getDataLayout(); |
Tobias Grosser | 0ee50f6 | 2013-04-10 06:55:31 +0000 | [diff] [blame] | 1003 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 1004 | assert(!S.getRegion().isTopLevelRegion() && |
| 1005 | "Top level regions are not supported"); |
Tobias Grosser | 0ee50f6 | 2013-04-10 06:55:31 +0000 | [diff] [blame] | 1006 | |
Tobias Grosser | 6325cd2 | 2015-04-27 10:43:10 +0000 | [diff] [blame] | 1007 | Annotator.buildAliasScopes(S); |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 1008 | |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame] | 1009 | BasicBlock *EnteringBB = simplifyRegion(&S, this); |
| 1010 | PollyIRBuilder Builder = createPollyIRBuilder(EnteringBB, Annotator); |
Johannes Doerfert | 9744c4a | 2014-08-12 18:35:54 +0000 | [diff] [blame] | 1011 | |
Tobias Grosser | e3c0558 | 2014-11-15 21:32:53 +0000 | [diff] [blame] | 1012 | IslNodeBuilder NodeBuilder(Builder, Annotator, this, *DL, *LI, *SE, *DT, S); |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame] | 1013 | |
Tobias Grosser | 6794238 | 2015-03-28 09:34:40 +0000 | [diff] [blame] | 1014 | // Only build the run-time condition and parameters _after_ having |
| 1015 | // introduced the conditional branch. This is important as the conditional |
| 1016 | // branch will guard the original scop from new induction variables that |
| 1017 | // the SCEVExpander may introduce while code generating the parameters and |
| 1018 | // which may introduce scalar dependences that prevent us from correctly |
| 1019 | // code generating this scop. |
| 1020 | BasicBlock *StartBlock = |
| 1021 | executeScopConditionally(S, this, Builder.getTrue()); |
| 1022 | auto SplitBlock = StartBlock->getSinglePredecessor(); |
| 1023 | Builder.SetInsertPoint(SplitBlock->getTerminator()); |
| 1024 | NodeBuilder.addParameters(S.getContext()); |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame] | 1025 | Value *RTC = buildRTC(Builder, NodeBuilder.getExprBuilder()); |
Tobias Grosser | 6794238 | 2015-03-28 09:34:40 +0000 | [diff] [blame] | 1026 | SplitBlock->getTerminator()->setOperand(0, RTC); |
Tobias Grosser | 2873594 | 2014-08-16 09:09:15 +0000 | [diff] [blame] | 1027 | Builder.SetInsertPoint(StartBlock->begin()); |
| 1028 | |
Johannes Doerfert | 7ceb040 | 2015-02-11 17:25:09 +0000 | [diff] [blame] | 1029 | NodeBuilder.create(AstRoot); |
Johannes Doerfert | 0b169c0 | 2015-02-27 17:37:05 +0000 | [diff] [blame] | 1030 | |
| 1031 | assert(!verifyGeneratedFunction(S, *EnteringBB->getParent()) && |
| 1032 | "Verification of generated function failed"); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 1033 | return true; |
| 1034 | } |
| 1035 | |
Johannes Doerfert | 909a3bf | 2015-03-01 18:42:08 +0000 | [diff] [blame] | 1036 | void printScop(raw_ostream &, Scop &) const override {} |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 1037 | |
Johannes Doerfert | 909a3bf | 2015-03-01 18:42:08 +0000 | [diff] [blame] | 1038 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Tobias Grosser | 42aff30 | 2014-01-13 22:29:56 +0000 | [diff] [blame] | 1039 | AU.addRequired<DominatorTreeWrapperPass>(); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 1040 | AU.addRequired<IslAstInfo>(); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 1041 | AU.addRequired<RegionInfoPass>(); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 1042 | AU.addRequired<ScalarEvolution>(); |
| 1043 | AU.addRequired<ScopDetection>(); |
| 1044 | AU.addRequired<ScopInfo>(); |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 1045 | AU.addRequired<LoopInfoWrapperPass>(); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 1046 | |
Johannes Doerfert | f6557f9 | 2015-03-04 22:43:40 +0000 | [diff] [blame] | 1047 | AU.addPreserved<DependenceInfo>(); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 1048 | |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 1049 | AU.addPreserved<LoopInfoWrapperPass>(); |
Tobias Grosser | 42aff30 | 2014-01-13 22:29:56 +0000 | [diff] [blame] | 1050 | AU.addPreserved<DominatorTreeWrapperPass>(); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 1051 | AU.addPreserved<IslAstInfo>(); |
| 1052 | AU.addPreserved<ScopDetection>(); |
| 1053 | AU.addPreserved<ScalarEvolution>(); |
| 1054 | |
| 1055 | // FIXME: We do not yet add regions for the newly generated code to the |
| 1056 | // region tree. |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 1057 | AU.addPreserved<RegionInfoPass>(); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 1058 | AU.addPreserved<TempScopInfo>(); |
| 1059 | AU.addPreserved<ScopInfo>(); |
| 1060 | AU.addPreservedID(IndependentBlocksID); |
| 1061 | } |
| 1062 | }; |
| 1063 | } |
| 1064 | |
| 1065 | char IslCodeGeneration::ID = 1; |
| 1066 | |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 1067 | Pass *polly::createIslCodeGenerationPass() { return new IslCodeGeneration(); } |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 1068 | |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 1069 | INITIALIZE_PASS_BEGIN(IslCodeGeneration, "polly-codegen-isl", |
| 1070 | "Polly - Create LLVM-IR from SCoPs", false, false); |
Johannes Doerfert | f6557f9 | 2015-03-04 22:43:40 +0000 | [diff] [blame] | 1071 | INITIALIZE_PASS_DEPENDENCY(DependenceInfo); |
Tobias Grosser | 42aff30 | 2014-01-13 22:29:56 +0000 | [diff] [blame] | 1072 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 1073 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 1074 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 1075 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution); |
| 1076 | INITIALIZE_PASS_DEPENDENCY(ScopDetection); |
| 1077 | INITIALIZE_PASS_END(IslCodeGeneration, "polly-codegen-isl", |
| 1078 | "Polly - Create LLVM-IR from SCoPs", false, false) |