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 |
| 18 | // its code in the new execution order defined by the changed scattering. |
| 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 21 | #include "polly/Config/config.h" |
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" |
Johannes Doerfert | 48cf6ec | 2014-07-29 20:50:09 +0000 | [diff] [blame] | 26 | #include "polly/CodeGen/IslExprBuilder.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 27 | #include "polly/CodeGen/LoopGenerators.h" |
| 28 | #include "polly/CodeGen/Utils.h" |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 29 | #include "polly/Dependences.h" |
| 30 | #include "polly/LinkAllPasses.h" |
| 31 | #include "polly/ScopInfo.h" |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 32 | #include "polly/Support/GICHelper.h" |
Tobias Grosser | 0ee50f6 | 2013-04-10 06:55:31 +0000 | [diff] [blame] | 33 | #include "polly/Support/ScopHelper.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 34 | #include "polly/TempScopInfo.h" |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 35 | #include "llvm/Analysis/LoopInfo.h" |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 36 | #include "llvm/Analysis/PostDominators.h" |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 37 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 38 | #include "llvm/IR/Module.h" |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 39 | #include "llvm/Support/CommandLine.h" |
| 40 | #include "llvm/Support/Debug.h" |
Chandler Carruth | 535d52c | 2013-01-02 11:47:44 +0000 | [diff] [blame] | 41 | #include "llvm/IR/DataLayout.h" |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 42 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 43 | |
| 44 | #include "isl/union_map.h" |
| 45 | #include "isl/list.h" |
| 46 | #include "isl/ast.h" |
| 47 | #include "isl/ast_build.h" |
| 48 | #include "isl/set.h" |
| 49 | #include "isl/map.h" |
| 50 | #include "isl/aff.h" |
| 51 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 52 | using namespace polly; |
| 53 | using namespace llvm; |
| 54 | |
Chandler Carruth | 95fef94 | 2014-04-22 03:30:19 +0000 | [diff] [blame] | 55 | #define DEBUG_TYPE "polly-codegen-isl" |
| 56 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 57 | class IslNodeBuilder { |
| 58 | public: |
Johannes Doerfert | 2ef3f4f | 2014-08-07 17:14:54 +0000 | [diff] [blame] | 59 | IslNodeBuilder(PollyIRBuilder &Builder, LoopAnnotator &Annotator, Pass *P, |
| 60 | LoopInfo &LI, ScalarEvolution &SE, DominatorTree &DT) |
Tobias Grosser | 34c8787 | 2014-04-22 16:39:41 +0000 | [diff] [blame] | 61 | : Builder(Builder), Annotator(Annotator), ExprBuilder(Builder, IDToValue), |
Johannes Doerfert | 2ef3f4f | 2014-08-07 17:14:54 +0000 | [diff] [blame] | 62 | P(P), LI(LI), SE(SE), DT(DT) {} |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 63 | |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 64 | /// @brief Add the mappings from array id's to array llvm::Value's. |
| 65 | void addMemoryAccesses(Scop &S); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 66 | void addParameters(__isl_take isl_set *Context); |
| 67 | void create(__isl_take isl_ast_node *Node); |
Tobias Grosser | 54ee0ba | 2013-11-17 03:18:25 +0000 | [diff] [blame] | 68 | IslExprBuilder &getExprBuilder() { return ExprBuilder; } |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 69 | |
| 70 | private: |
Tobias Grosser | 5103ba7 | 2014-03-04 14:58:49 +0000 | [diff] [blame] | 71 | PollyIRBuilder &Builder; |
Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 72 | LoopAnnotator &Annotator; |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 73 | IslExprBuilder ExprBuilder; |
| 74 | Pass *P; |
Johannes Doerfert | 2ef3f4f | 2014-08-07 17:14:54 +0000 | [diff] [blame] | 75 | LoopInfo &LI; |
| 76 | ScalarEvolution &SE; |
| 77 | DominatorTree &DT; |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 78 | |
| 79 | // This maps an isl_id* to the Value* it has in the generated program. For now |
| 80 | // on, the only isl_ids that are stored here are the newly calculated loop |
| 81 | // ivs. |
Tobias Grosser | 566ad58 | 2014-08-31 16:21:12 +0000 | [diff] [blame] | 82 | IslExprBuilder::IDToValueTy IDToValue; |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 83 | |
| 84 | // Extract the upper bound of this loop |
| 85 | // |
| 86 | // The isl code generation can generate arbitrary expressions to check if the |
| 87 | // upper bound of a loop is reached, but it provides an option to enforce |
| 88 | // 'atomic' upper bounds. An 'atomic upper bound is always of the form |
| 89 | // iv <= expr, where expr is an (arbitrary) expression not containing iv. |
| 90 | // |
| 91 | // This function extracts 'atomic' upper bounds. Polly, in general, requires |
| 92 | // atomic upper bounds for the following reasons: |
| 93 | // |
| 94 | // 1. An atomic upper bound is loop invariant |
| 95 | // |
| 96 | // It must not be calculated at each loop iteration and can often even be |
| 97 | // hoisted out further by the loop invariant code motion. |
| 98 | // |
| 99 | // 2. OpenMP needs a loop invarient upper bound to calculate the number |
| 100 | // of loop iterations. |
| 101 | // |
| 102 | // 3. With the existing code, upper bounds have been easier to implement. |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 103 | __isl_give isl_ast_expr *getUpperBound(__isl_keep isl_ast_node *For, |
| 104 | CmpInst::Predicate &Predicate); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 105 | |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 106 | unsigned getNumberOfIterations(__isl_keep isl_ast_node *For); |
| 107 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 108 | void createFor(__isl_take isl_ast_node *For); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 109 | void createForVector(__isl_take isl_ast_node *For, int VectorWidth); |
| 110 | void createForSequential(__isl_take isl_ast_node *For); |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 111 | |
| 112 | /// Generate LLVM-IR that computes the values of the original induction |
| 113 | /// variables in function of the newly generated loop induction variables. |
| 114 | /// |
| 115 | /// Example: |
| 116 | /// |
| 117 | /// // Original |
| 118 | /// for i |
| 119 | /// for j |
| 120 | /// S(i) |
| 121 | /// |
| 122 | /// Schedule: [i,j] -> [i+j, j] |
| 123 | /// |
| 124 | /// // New |
| 125 | /// for c0 |
| 126 | /// for c1 |
| 127 | /// S(c0 - c1, c1) |
| 128 | /// |
| 129 | /// Assuming the original code consists of two loops which are |
| 130 | /// transformed according to a schedule [i,j] -> [c0=i+j,c1=j]. The resulting |
| 131 | /// ast models the original statement as a call expression where each argument |
| 132 | /// is an expression that computes the old induction variables from the new |
| 133 | /// ones, ordered such that the first argument computes the value of induction |
| 134 | /// variable that was outermost in the original code. |
| 135 | /// |
| 136 | /// @param Expr The call expression that represents the statement. |
| 137 | /// @param Stmt The statement that is called. |
| 138 | /// @param VMap The value map into which the mapping from the old induction |
| 139 | /// variable to the new one is inserted. This mapping is used |
| 140 | /// for the classical code generation (not scev-based) and |
| 141 | /// gives an explicit mapping from an original, materialized |
| 142 | /// induction variable. It consequently can only be expressed |
| 143 | /// if there was an explicit induction variable. |
| 144 | /// @param LTS The loop to SCEV map in which the mapping from the original |
| 145 | /// loop to a SCEV representing the new loop iv is added. This |
| 146 | /// mapping does not require an explicit induction variable. |
| 147 | /// Instead, we think in terms of an implicit induction variable |
| 148 | /// that counts the number of times a loop is executed. For each |
| 149 | /// original loop this count, expressed in function of the new |
| 150 | /// induction variables, is added to the LTS map. |
| 151 | void createSubstitutions(__isl_take isl_ast_expr *Expr, ScopStmt *Stmt, |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 152 | ValueMapT &VMap, LoopToScevMapT <S); |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 153 | void createSubstitutionsVector(__isl_take isl_ast_expr *Expr, ScopStmt *Stmt, |
| 154 | VectorValueMapT &VMap, |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 155 | std::vector<LoopToScevMapT> &VLTS, |
| 156 | std::vector<Value *> &IVS, |
| 157 | __isl_take isl_id *IteratorID); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 158 | void createIf(__isl_take isl_ast_node *If); |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 159 | void createUserVector(__isl_take isl_ast_node *User, |
| 160 | std::vector<Value *> &IVS, |
| 161 | __isl_take isl_id *IteratorID, |
| 162 | __isl_take isl_union_map *Schedule); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 163 | void createUser(__isl_take isl_ast_node *User); |
| 164 | void createBlock(__isl_take isl_ast_node *Block); |
| 165 | }; |
| 166 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 167 | __isl_give isl_ast_expr * |
| 168 | IslNodeBuilder::getUpperBound(__isl_keep isl_ast_node *For, |
| 169 | ICmpInst::Predicate &Predicate) { |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 170 | isl_id *UBID, *IteratorID; |
| 171 | isl_ast_expr *Cond, *Iterator, *UB, *Arg0; |
Tobias Grosser | c967d8e | 2012-10-16 07:29:13 +0000 | [diff] [blame] | 172 | isl_ast_op_type Type; |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 173 | |
| 174 | Cond = isl_ast_node_for_get_cond(For); |
| 175 | Iterator = isl_ast_node_for_get_iterator(For); |
Tobias Grosser | c967d8e | 2012-10-16 07:29:13 +0000 | [diff] [blame] | 176 | Type = isl_ast_expr_get_op_type(Cond); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 177 | |
Tobias Grosser | ae2d83e | 2012-12-29 23:57:18 +0000 | [diff] [blame] | 178 | assert(isl_ast_expr_get_type(Cond) == isl_ast_expr_op && |
| 179 | "conditional expression is not an atomic upper bound"); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 180 | |
Tobias Grosser | c967d8e | 2012-10-16 07:29:13 +0000 | [diff] [blame] | 181 | switch (Type) { |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 182 | case isl_ast_op_le: |
| 183 | Predicate = ICmpInst::ICMP_SLE; |
| 184 | break; |
| 185 | case isl_ast_op_lt: |
| 186 | Predicate = ICmpInst::ICMP_SLT; |
| 187 | break; |
| 188 | default: |
| 189 | llvm_unreachable("Unexpected comparision type in loop conditon"); |
Tobias Grosser | c967d8e | 2012-10-16 07:29:13 +0000 | [diff] [blame] | 190 | } |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 191 | |
| 192 | Arg0 = isl_ast_expr_get_op_arg(Cond, 0); |
| 193 | |
Tobias Grosser | ae2d83e | 2012-12-29 23:57:18 +0000 | [diff] [blame] | 194 | assert(isl_ast_expr_get_type(Arg0) == isl_ast_expr_id && |
| 195 | "conditional expression is not an atomic upper bound"); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 196 | |
| 197 | UBID = isl_ast_expr_get_id(Arg0); |
| 198 | |
Tobias Grosser | ae2d83e | 2012-12-29 23:57:18 +0000 | [diff] [blame] | 199 | assert(isl_ast_expr_get_type(Iterator) == isl_ast_expr_id && |
| 200 | "Could not get the iterator"); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 201 | |
| 202 | IteratorID = isl_ast_expr_get_id(Iterator); |
| 203 | |
Tobias Grosser | ae2d83e | 2012-12-29 23:57:18 +0000 | [diff] [blame] | 204 | assert(UBID == IteratorID && |
| 205 | "conditional expression is not an atomic upper bound"); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 206 | |
| 207 | UB = isl_ast_expr_get_op_arg(Cond, 1); |
| 208 | |
| 209 | isl_ast_expr_free(Cond); |
| 210 | isl_ast_expr_free(Iterator); |
| 211 | isl_ast_expr_free(Arg0); |
| 212 | isl_id_free(IteratorID); |
| 213 | isl_id_free(UBID); |
| 214 | |
| 215 | return UB; |
| 216 | } |
| 217 | |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 218 | unsigned IslNodeBuilder::getNumberOfIterations(__isl_keep isl_ast_node *For) { |
Johannes Doerfert | 94d9082 | 2014-07-23 20:26:25 +0000 | [diff] [blame] | 219 | isl_union_map *Schedule = IslAstInfo::getSchedule(For); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 220 | isl_set *LoopDomain = isl_set_from_union_set(isl_union_map_range(Schedule)); |
Sebastian Pop | 2aa5c24 | 2012-12-18 08:56:51 +0000 | [diff] [blame] | 221 | int NumberOfIterations = polly::getNumberOfIterations(LoopDomain); |
| 222 | if (NumberOfIterations == -1) |
| 223 | return -1; |
| 224 | return NumberOfIterations + 1; |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 227 | void IslNodeBuilder::createUserVector(__isl_take isl_ast_node *User, |
| 228 | std::vector<Value *> &IVS, |
| 229 | __isl_take isl_id *IteratorID, |
| 230 | __isl_take isl_union_map *Schedule) { |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 231 | isl_ast_expr *Expr = isl_ast_node_user_get_expr(User); |
| 232 | isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0); |
| 233 | isl_id *Id = isl_ast_expr_get_id(StmtExpr); |
| 234 | isl_ast_expr_free(StmtExpr); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 235 | ScopStmt *Stmt = (ScopStmt *)isl_id_get_user(Id); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 236 | VectorValueMapT VectorMap(IVS.size()); |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 237 | std::vector<LoopToScevMapT> VLTS(IVS.size()); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 238 | |
| 239 | isl_union_set *Domain = isl_union_set_from_set(Stmt->getDomain()); |
| 240 | Schedule = isl_union_map_intersect_domain(Schedule, Domain); |
| 241 | isl_map *S = isl_map_from_union_map(Schedule); |
| 242 | |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 243 | createSubstitutionsVector(Expr, Stmt, VectorMap, VLTS, IVS, IteratorID); |
Johannes Doerfert | 2ef3f4f | 2014-08-07 17:14:54 +0000 | [diff] [blame] | 244 | VectorBlockGenerator::generate(Builder, *Stmt, VectorMap, VLTS, S, P, LI, SE); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 245 | |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 246 | isl_map_free(S); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 247 | isl_id_free(Id); |
| 248 | isl_ast_node_free(User); |
| 249 | } |
| 250 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 251 | void IslNodeBuilder::createForVector(__isl_take isl_ast_node *For, |
| 252 | int VectorWidth) { |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 253 | isl_ast_node *Body = isl_ast_node_for_get_body(For); |
| 254 | isl_ast_expr *Init = isl_ast_node_for_get_init(For); |
| 255 | isl_ast_expr *Inc = isl_ast_node_for_get_inc(For); |
| 256 | isl_ast_expr *Iterator = isl_ast_node_for_get_iterator(For); |
| 257 | isl_id *IteratorID = isl_ast_expr_get_id(Iterator); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 258 | |
| 259 | Value *ValueLB = ExprBuilder.create(Init); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 260 | Value *ValueInc = ExprBuilder.create(Inc); |
| 261 | |
| 262 | Type *MaxType = ExprBuilder.getType(Iterator); |
| 263 | MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType()); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 264 | MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType()); |
| 265 | |
| 266 | if (MaxType != ValueLB->getType()) |
| 267 | ValueLB = Builder.CreateSExt(ValueLB, MaxType); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 268 | if (MaxType != ValueInc->getType()) |
| 269 | ValueInc = Builder.CreateSExt(ValueInc, MaxType); |
| 270 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 271 | std::vector<Value *> IVS(VectorWidth); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 272 | IVS[0] = ValueLB; |
| 273 | |
| 274 | for (int i = 1; i < VectorWidth; i++) |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 275 | IVS[i] = Builder.CreateAdd(IVS[i - 1], ValueInc, "p_vector_iv"); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 276 | |
Johannes Doerfert | 94d9082 | 2014-07-23 20:26:25 +0000 | [diff] [blame] | 277 | isl_union_map *Schedule = IslAstInfo::getSchedule(For); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 278 | assert(Schedule && "For statement annotation does not contain its schedule"); |
| 279 | |
| 280 | IDToValue[IteratorID] = ValueLB; |
| 281 | |
| 282 | switch (isl_ast_node_get_type(Body)) { |
| 283 | case isl_ast_node_user: |
| 284 | createUserVector(Body, IVS, isl_id_copy(IteratorID), |
| 285 | isl_union_map_copy(Schedule)); |
| 286 | break; |
| 287 | case isl_ast_node_block: { |
| 288 | isl_ast_node_list *List = isl_ast_node_block_get_children(Body); |
| 289 | |
| 290 | for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i) |
| 291 | createUserVector(isl_ast_node_list_get_ast_node(List, i), IVS, |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 292 | isl_id_copy(IteratorID), isl_union_map_copy(Schedule)); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 293 | |
| 294 | isl_ast_node_free(Body); |
| 295 | isl_ast_node_list_free(List); |
| 296 | break; |
| 297 | } |
| 298 | default: |
| 299 | isl_ast_node_dump(Body); |
| 300 | llvm_unreachable("Unhandled isl_ast_node in vectorizer"); |
| 301 | } |
| 302 | |
| 303 | IDToValue.erase(IteratorID); |
| 304 | isl_id_free(IteratorID); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 305 | isl_union_map_free(Schedule); |
| 306 | |
| 307 | isl_ast_node_free(For); |
| 308 | isl_ast_expr_free(Iterator); |
| 309 | } |
| 310 | |
| 311 | void IslNodeBuilder::createForSequential(__isl_take isl_ast_node *For) { |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 312 | isl_ast_node *Body; |
| 313 | isl_ast_expr *Init, *Inc, *Iterator, *UB; |
| 314 | isl_id *IteratorID; |
| 315 | Value *ValueLB, *ValueUB, *ValueInc; |
| 316 | Type *MaxType; |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 317 | BasicBlock *ExitBlock; |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 318 | Value *IV; |
Tobias Grosser | c967d8e | 2012-10-16 07:29:13 +0000 | [diff] [blame] | 319 | CmpInst::Predicate Predicate; |
Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 320 | bool Parallel; |
| 321 | |
Johannes Doerfert | ed67f8b | 2014-08-01 08:14:28 +0000 | [diff] [blame] | 322 | Parallel = IslAstInfo::isInnermostParallel(For) && |
| 323 | !IslAstInfo::isReductionParallel(For); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 324 | |
| 325 | Body = isl_ast_node_for_get_body(For); |
| 326 | |
| 327 | // isl_ast_node_for_is_degenerate(For) |
| 328 | // |
| 329 | // TODO: For degenerated loops we could generate a plain assignment. |
| 330 | // However, for now we just reuse the logic for normal loops, which will |
| 331 | // create a loop with a single iteration. |
| 332 | |
| 333 | Init = isl_ast_node_for_get_init(For); |
| 334 | Inc = isl_ast_node_for_get_inc(For); |
| 335 | Iterator = isl_ast_node_for_get_iterator(For); |
| 336 | IteratorID = isl_ast_expr_get_id(Iterator); |
Tobias Grosser | c967d8e | 2012-10-16 07:29:13 +0000 | [diff] [blame] | 337 | UB = getUpperBound(For, Predicate); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 338 | |
| 339 | ValueLB = ExprBuilder.create(Init); |
| 340 | ValueUB = ExprBuilder.create(UB); |
| 341 | ValueInc = ExprBuilder.create(Inc); |
| 342 | |
| 343 | MaxType = ExprBuilder.getType(Iterator); |
| 344 | MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType()); |
| 345 | MaxType = ExprBuilder.getWidestType(MaxType, ValueUB->getType()); |
| 346 | MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType()); |
| 347 | |
| 348 | if (MaxType != ValueLB->getType()) |
| 349 | ValueLB = Builder.CreateSExt(ValueLB, MaxType); |
| 350 | if (MaxType != ValueUB->getType()) |
| 351 | ValueUB = Builder.CreateSExt(ValueUB, MaxType); |
| 352 | if (MaxType != ValueInc->getType()) |
| 353 | ValueInc = Builder.CreateSExt(ValueInc, MaxType); |
| 354 | |
Johannes Doerfert | 2ef3f4f | 2014-08-07 17:14:54 +0000 | [diff] [blame] | 355 | IV = createLoop(ValueLB, ValueUB, ValueInc, Builder, P, LI, DT, ExitBlock, |
| 356 | Predicate, &Annotator, Parallel); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 357 | IDToValue[IteratorID] = IV; |
| 358 | |
| 359 | create(Body); |
| 360 | |
Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 361 | Annotator.End(); |
| 362 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 363 | IDToValue.erase(IteratorID); |
| 364 | |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 365 | Builder.SetInsertPoint(ExitBlock->begin()); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 366 | |
| 367 | isl_ast_node_free(For); |
| 368 | isl_ast_expr_free(Iterator); |
| 369 | isl_id_free(IteratorID); |
| 370 | } |
| 371 | |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 372 | void IslNodeBuilder::createFor(__isl_take isl_ast_node *For) { |
| 373 | bool Vector = PollyVectorizerChoice != VECTORIZER_NONE; |
| 374 | |
Johannes Doerfert | ed67f8b | 2014-08-01 08:14:28 +0000 | [diff] [blame] | 375 | if (Vector && IslAstInfo::isInnermostParallel(For) && |
| 376 | !IslAstInfo::isReductionParallel(For)) { |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 377 | int VectorWidth = getNumberOfIterations(For); |
| 378 | if (1 < VectorWidth && VectorWidth <= 16) { |
| 379 | createForVector(For, VectorWidth); |
| 380 | return; |
| 381 | } |
| 382 | } |
| 383 | createForSequential(For); |
| 384 | } |
| 385 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 386 | void IslNodeBuilder::createIf(__isl_take isl_ast_node *If) { |
| 387 | isl_ast_expr *Cond = isl_ast_node_if_get_cond(If); |
| 388 | |
| 389 | Function *F = Builder.GetInsertBlock()->getParent(); |
| 390 | LLVMContext &Context = F->getContext(); |
| 391 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 392 | BasicBlock *CondBB = |
| 393 | SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 394 | CondBB->setName("polly.cond"); |
| 395 | BasicBlock *MergeBB = SplitBlock(CondBB, CondBB->begin(), P); |
| 396 | MergeBB->setName("polly.merge"); |
| 397 | BasicBlock *ThenBB = BasicBlock::Create(Context, "polly.then", F); |
| 398 | BasicBlock *ElseBB = BasicBlock::Create(Context, "polly.else", F); |
| 399 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 400 | DT.addNewBlock(ThenBB, CondBB); |
| 401 | DT.addNewBlock(ElseBB, CondBB); |
| 402 | DT.changeImmediateDominator(MergeBB, CondBB); |
| 403 | |
Tobias Grosser | 3081b0f | 2013-05-16 06:40:24 +0000 | [diff] [blame] | 404 | Loop *L = LI.getLoopFor(CondBB); |
| 405 | if (L) { |
| 406 | L->addBasicBlockToLoop(ThenBB, LI.getBase()); |
| 407 | L->addBasicBlockToLoop(ElseBB, LI.getBase()); |
| 408 | } |
| 409 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 410 | CondBB->getTerminator()->eraseFromParent(); |
| 411 | |
| 412 | Builder.SetInsertPoint(CondBB); |
| 413 | Value *Predicate = ExprBuilder.create(Cond); |
| 414 | Builder.CreateCondBr(Predicate, ThenBB, ElseBB); |
| 415 | Builder.SetInsertPoint(ThenBB); |
| 416 | Builder.CreateBr(MergeBB); |
| 417 | Builder.SetInsertPoint(ElseBB); |
| 418 | Builder.CreateBr(MergeBB); |
| 419 | Builder.SetInsertPoint(ThenBB->begin()); |
| 420 | |
| 421 | create(isl_ast_node_if_get_then(If)); |
| 422 | |
| 423 | Builder.SetInsertPoint(ElseBB->begin()); |
| 424 | |
| 425 | if (isl_ast_node_if_has_else(If)) |
| 426 | create(isl_ast_node_if_get_else(If)); |
| 427 | |
| 428 | Builder.SetInsertPoint(MergeBB->begin()); |
| 429 | |
| 430 | isl_ast_node_free(If); |
| 431 | } |
| 432 | |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 433 | void IslNodeBuilder::createSubstitutions(isl_ast_expr *Expr, ScopStmt *Stmt, |
| 434 | ValueMapT &VMap, LoopToScevMapT <S) { |
| 435 | assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op && |
| 436 | "Expression of type 'op' expected"); |
| 437 | assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_call && |
| 438 | "Opertation of type 'call' expected"); |
| 439 | for (int i = 0; i < isl_ast_expr_get_op_n_arg(Expr) - 1; ++i) { |
| 440 | isl_ast_expr *SubExpr; |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 441 | Value *V; |
| 442 | |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 443 | SubExpr = isl_ast_expr_get_op_arg(Expr, i + 1); |
| 444 | V = ExprBuilder.create(SubExpr); |
Sebastian Pop | e039bb1 | 2013-03-18 19:09:49 +0000 | [diff] [blame] | 445 | ScalarEvolution *SE = Stmt->getParent()->getSE(); |
| 446 | LTS[Stmt->getLoopForDimension(i)] = SE->getUnknown(V); |
| 447 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 448 | // CreateIntCast can introduce trunc expressions. This is correct, as the |
| 449 | // result will always fit into the type of the original induction variable |
| 450 | // (because we calculate a value of the original induction variable). |
Sebastian Pop | e039bb1 | 2013-03-18 19:09:49 +0000 | [diff] [blame] | 451 | const Value *OldIV = Stmt->getInductionVariableForDimension(i); |
| 452 | if (OldIV) { |
| 453 | V = Builder.CreateIntCast(V, OldIV->getType(), true); |
| 454 | VMap[OldIV] = V; |
| 455 | } |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 458 | isl_ast_expr_free(Expr); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 461 | void IslNodeBuilder::createSubstitutionsVector( |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 462 | __isl_take isl_ast_expr *Expr, ScopStmt *Stmt, VectorValueMapT &VMap, |
| 463 | std::vector<LoopToScevMapT> &VLTS, std::vector<Value *> &IVS, |
| 464 | __isl_take isl_id *IteratorID) { |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 465 | int i = 0; |
| 466 | |
| 467 | Value *OldValue = IDToValue[IteratorID]; |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 468 | for (Value *IV : IVS) { |
| 469 | IDToValue[IteratorID] = IV; |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 470 | createSubstitutions(isl_ast_expr_copy(Expr), Stmt, VMap[i], VLTS[i]); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 471 | i++; |
| 472 | } |
| 473 | |
| 474 | IDToValue[IteratorID] = OldValue; |
| 475 | isl_id_free(IteratorID); |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 476 | isl_ast_expr_free(Expr); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | void IslNodeBuilder::createUser(__isl_take isl_ast_node *User) { |
| 480 | ValueMapT VMap; |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 481 | LoopToScevMapT LTS; |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 482 | isl_id *Id; |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 483 | ScopStmt *Stmt; |
| 484 | |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 485 | isl_ast_expr *Expr = isl_ast_node_user_get_expr(User); |
| 486 | isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0); |
| 487 | Id = isl_ast_expr_get_id(StmtExpr); |
| 488 | isl_ast_expr_free(StmtExpr); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 489 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 490 | Stmt = (ScopStmt *)isl_id_get_user(Id); |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 491 | |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 492 | createSubstitutions(Expr, Stmt, VMap, LTS); |
Johannes Doerfert | 2ef3f4f | 2014-08-07 17:14:54 +0000 | [diff] [blame] | 493 | BlockGenerator::generate(Builder, *Stmt, VMap, LTS, P, LI, SE, |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 494 | IslAstInfo::getBuild(User), &ExprBuilder); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 495 | |
| 496 | isl_ast_node_free(User); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 497 | isl_id_free(Id); |
| 498 | } |
| 499 | |
| 500 | void IslNodeBuilder::createBlock(__isl_take isl_ast_node *Block) { |
| 501 | isl_ast_node_list *List = isl_ast_node_block_get_children(Block); |
| 502 | |
| 503 | for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i) |
| 504 | create(isl_ast_node_list_get_ast_node(List, i)); |
| 505 | |
| 506 | isl_ast_node_free(Block); |
| 507 | isl_ast_node_list_free(List); |
| 508 | } |
| 509 | |
| 510 | void IslNodeBuilder::create(__isl_take isl_ast_node *Node) { |
| 511 | switch (isl_ast_node_get_type(Node)) { |
| 512 | case isl_ast_node_error: |
| 513 | llvm_unreachable("code generation error"); |
| 514 | case isl_ast_node_for: |
| 515 | createFor(Node); |
| 516 | return; |
| 517 | case isl_ast_node_if: |
| 518 | createIf(Node); |
| 519 | return; |
| 520 | case isl_ast_node_user: |
| 521 | createUser(Node); |
| 522 | return; |
| 523 | case isl_ast_node_block: |
| 524 | createBlock(Node); |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | llvm_unreachable("Unknown isl_ast_node type"); |
| 529 | } |
| 530 | |
| 531 | void IslNodeBuilder::addParameters(__isl_take isl_set *Context) { |
Johannes Doerfert | 2ef3f4f | 2014-08-07 17:14:54 +0000 | [diff] [blame] | 532 | SCEVExpander Rewriter(SE, "polly"); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 533 | |
| 534 | for (unsigned i = 0; i < isl_set_dim(Context, isl_dim_param); ++i) { |
| 535 | isl_id *Id; |
| 536 | const SCEV *Scev; |
| 537 | IntegerType *T; |
| 538 | Instruction *InsertLocation; |
| 539 | |
| 540 | Id = isl_set_get_dim_id(Context, isl_dim_param, i); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 541 | Scev = (const SCEV *)isl_id_get_user(Id); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 542 | T = dyn_cast<IntegerType>(Scev->getType()); |
| 543 | InsertLocation = --(Builder.GetInsertBlock()->end()); |
| 544 | Value *V = Rewriter.expandCodeFor(Scev, T, InsertLocation); |
| 545 | IDToValue[Id] = V; |
| 546 | |
| 547 | isl_id_free(Id); |
| 548 | } |
| 549 | |
| 550 | isl_set_free(Context); |
| 551 | } |
| 552 | |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 553 | void IslNodeBuilder::addMemoryAccesses(Scop &S) { |
| 554 | for (ScopStmt *Stmt : S) |
| 555 | for (MemoryAccess *MA : *Stmt) { |
| 556 | isl_id *Id = MA->getArrayId(); |
| 557 | IDToValue[Id] = MA->getBaseAddr(); |
| 558 | isl_id_free(Id); |
| 559 | } |
| 560 | } |
| 561 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 562 | namespace { |
| 563 | class IslCodeGeneration : public ScopPass { |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 564 | public: |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 565 | static char ID; |
| 566 | |
| 567 | IslCodeGeneration() : ScopPass(ID) {} |
| 568 | |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame^] | 569 | /// @name The analysis passes we need to generate code. |
| 570 | /// |
| 571 | ///{ |
| 572 | LoopInfo *LI; |
| 573 | IslAstInfo *AI; |
| 574 | DominatorTree *DT; |
| 575 | ScalarEvolution *SE; |
| 576 | ///} |
| 577 | |
| 578 | /// @brief The loop annotator to generate llvm.loop metadata. |
| 579 | LoopAnnotator Annotator; |
| 580 | |
| 581 | /// @brief Build the runtime condition. |
| 582 | /// |
| 583 | /// Build the condition that evaluates at run-time to true iff all |
| 584 | /// assumptions taken for the SCoP hold, and to false otherwise. |
| 585 | /// |
| 586 | /// @return A value evaluating to true/false if execution is save/unsafe. |
| 587 | Value *buildRTC(PollyIRBuilder &Builder, IslExprBuilder &ExprBuilder) { |
| 588 | Builder.SetInsertPoint(Builder.GetInsertBlock()->getTerminator()); |
| 589 | Value *RTC = ExprBuilder.create(AI->getRunCondition()); |
| 590 | return Builder.CreateIsNotNull(RTC); |
| 591 | } |
| 592 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 593 | bool runOnScop(Scop &S) { |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame^] | 594 | LI = &getAnalysis<LoopInfo>(); |
| 595 | AI = &getAnalysis<IslAstInfo>(); |
| 596 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 597 | SE = &getAnalysis<ScalarEvolution>(); |
Tobias Grosser | 0ee50f6 | 2013-04-10 06:55:31 +0000 | [diff] [blame] | 598 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 599 | assert(!S.getRegion().isTopLevelRegion() && |
| 600 | "Top level regions are not supported"); |
Tobias Grosser | 0ee50f6 | 2013-04-10 06:55:31 +0000 | [diff] [blame] | 601 | |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame^] | 602 | BasicBlock *EnteringBB = simplifyRegion(&S, this); |
| 603 | PollyIRBuilder Builder = createPollyIRBuilder(EnteringBB, Annotator); |
Johannes Doerfert | 9744c4a | 2014-08-12 18:35:54 +0000 | [diff] [blame] | 604 | |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame^] | 605 | IslNodeBuilder NodeBuilder(Builder, Annotator, this, *LI, *SE, *DT); |
Tobias Grosser | 2873594 | 2014-08-16 09:09:15 +0000 | [diff] [blame] | 606 | NodeBuilder.addMemoryAccesses(S); |
| 607 | NodeBuilder.addParameters(S.getContext()); |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame^] | 608 | |
| 609 | Value *RTC = buildRTC(Builder, NodeBuilder.getExprBuilder()); |
| 610 | BasicBlock *StartBlock = executeScopConditionally(S, this, RTC); |
Tobias Grosser | 2873594 | 2014-08-16 09:09:15 +0000 | [diff] [blame] | 611 | Builder.SetInsertPoint(StartBlock->begin()); |
| 612 | |
Johannes Doerfert | 3826224 | 2014-09-10 14:50:23 +0000 | [diff] [blame^] | 613 | NodeBuilder.create(AI->getAst()); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 614 | return true; |
| 615 | } |
| 616 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 617 | virtual void printScop(raw_ostream &OS) const {} |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 618 | |
| 619 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Tobias Grosser | 42aff30 | 2014-01-13 22:29:56 +0000 | [diff] [blame] | 620 | AU.addRequired<DominatorTreeWrapperPass>(); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 621 | AU.addRequired<IslAstInfo>(); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 622 | AU.addRequired<RegionInfoPass>(); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 623 | AU.addRequired<ScalarEvolution>(); |
| 624 | AU.addRequired<ScopDetection>(); |
| 625 | AU.addRequired<ScopInfo>(); |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 626 | AU.addRequired<LoopInfo>(); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 627 | |
| 628 | AU.addPreserved<Dependences>(); |
| 629 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 630 | AU.addPreserved<LoopInfo>(); |
Tobias Grosser | 42aff30 | 2014-01-13 22:29:56 +0000 | [diff] [blame] | 631 | AU.addPreserved<DominatorTreeWrapperPass>(); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 632 | AU.addPreserved<IslAstInfo>(); |
| 633 | AU.addPreserved<ScopDetection>(); |
| 634 | AU.addPreserved<ScalarEvolution>(); |
| 635 | |
| 636 | // FIXME: We do not yet add regions for the newly generated code to the |
| 637 | // region tree. |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 638 | AU.addPreserved<RegionInfoPass>(); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 639 | AU.addPreserved<TempScopInfo>(); |
| 640 | AU.addPreserved<ScopInfo>(); |
| 641 | AU.addPreservedID(IndependentBlocksID); |
| 642 | } |
| 643 | }; |
| 644 | } |
| 645 | |
| 646 | char IslCodeGeneration::ID = 1; |
| 647 | |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 648 | Pass *polly::createIslCodeGenerationPass() { return new IslCodeGeneration(); } |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 649 | |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 650 | INITIALIZE_PASS_BEGIN(IslCodeGeneration, "polly-codegen-isl", |
| 651 | "Polly - Create LLVM-IR from SCoPs", false, false); |
| 652 | INITIALIZE_PASS_DEPENDENCY(Dependences); |
Tobias Grosser | 42aff30 | 2014-01-13 22:29:56 +0000 | [diff] [blame] | 653 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 654 | INITIALIZE_PASS_DEPENDENCY(LoopInfo); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 655 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 656 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution); |
| 657 | INITIALIZE_PASS_DEPENDENCY(ScopDetection); |
| 658 | INITIALIZE_PASS_END(IslCodeGeneration, "polly-codegen-isl", |
| 659 | "Polly - Create LLVM-IR from SCoPs", false, false) |