Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1 | //===--------- ScopInfo.cpp - Create Scops from LLVM IR ------------------===// |
| 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 | // Create a polyhedral description for a static control flow region. |
| 11 | // |
| 12 | // The pass creates a polyhedral description of the Scops detected by the Scop |
| 13 | // detection derived from their LLVM-IR code. |
| 14 | // |
| 15 | // This represantation is shared among several tools in the polyhedral |
| 16 | // community, which are e.g. Cloog, Pluto, Loopo, Graphite. |
| 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Sebastian Pop | 27c10c6 | 2013-03-22 22:07:43 +0000 | [diff] [blame] | 20 | #include "polly/CodeGen/BlockGenerators.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 21 | #include "polly/LinkAllPasses.h" |
Sebastian Pop | 27c10c6 | 2013-03-22 22:07:43 +0000 | [diff] [blame] | 22 | #include "polly/ScopInfo.h" |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 23 | #include "polly/Options.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 24 | #include "polly/Support/GICHelper.h" |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 25 | #include "polly/Support/SCEVValidator.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 26 | #include "polly/Support/ScopHelper.h" |
Sebastian Pop | 27c10c6 | 2013-03-22 22:07:43 +0000 | [diff] [blame] | 27 | #include "polly/TempScopInfo.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SetVector.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/Statistic.h" |
Hongbin Zheng | 86a3774 | 2012-04-25 08:01:38 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringExtras.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 31 | #include "llvm/Analysis/LoopInfo.h" |
| 32 | #include "llvm/Analysis/RegionIterator.h" |
| 33 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Debug.h" |
| 35 | |
| 36 | #include "isl/constraint.h" |
| 37 | #include "isl/set.h" |
| 38 | #include "isl/map.h" |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 39 | #include "isl/union_map.h" |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 40 | #include "isl/aff.h" |
| 41 | #include "isl/printer.h" |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 42 | #include "isl/local_space.h" |
Tobias Grosser | 4a8e356 | 2011-12-07 07:42:51 +0000 | [diff] [blame] | 43 | #include "isl/options.h" |
Tobias Grosser | edab135 | 2013-06-21 06:41:31 +0000 | [diff] [blame] | 44 | #include "isl/val.h" |
Chandler Carruth | 95fef94 | 2014-04-22 03:30:19 +0000 | [diff] [blame] | 45 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 46 | #include <sstream> |
| 47 | #include <string> |
| 48 | #include <vector> |
| 49 | |
| 50 | using namespace llvm; |
| 51 | using namespace polly; |
| 52 | |
Chandler Carruth | 95fef94 | 2014-04-22 03:30:19 +0000 | [diff] [blame] | 53 | #define DEBUG_TYPE "polly-scops" |
| 54 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 55 | STATISTIC(ScopFound, "Number of valid Scops"); |
| 56 | STATISTIC(RichScopFound, "Number of Scops containing a loop"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 57 | |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 58 | // Multiplicative reductions can be disabled seperately as these kind of |
| 59 | // operations can overflow easily. Additive reductions and bit operations |
| 60 | // are in contrast pretty stable. |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 61 | static cl::opt<bool> DisableMultiplicativeReductions( |
| 62 | "polly-disable-multiplicative-reductions", |
| 63 | cl::desc("Disable multiplicative reductions"), cl::Hidden, cl::ZeroOrMore, |
| 64 | cl::init(false), cl::cat(PollyCategory)); |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 65 | |
Johannes Doerfert | 5130c84 | 2014-08-15 01:14:11 +0000 | [diff] [blame] | 66 | static int extractAffine(__isl_take isl_set *Set, __isl_take isl_aff *Aff, |
| 67 | void *User) { |
| 68 | *((isl_aff **)(User)) = Aff; |
| 69 | isl_set_free(Set); |
| 70 | return 0; |
| 71 | } |
| 72 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 73 | /// Translate a 'const SCEV *' expression in an isl_pw_aff. |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 74 | struct SCEVAffinator : public SCEVVisitor<SCEVAffinator, isl_pw_aff *> { |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 75 | public: |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 76 | /// @brief Translate a 'const SCEV *' to an isl_pw_aff. |
| 77 | /// |
| 78 | /// @param Stmt The location at which the scalar evolution expression |
| 79 | /// is evaluated. |
| 80 | /// @param Expr The expression that is translated. |
| 81 | static __isl_give isl_pw_aff *getPwAff(ScopStmt *Stmt, const SCEV *Expr); |
| 82 | |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 83 | private: |
Tobias Grosser | 3cc9974 | 2012-06-06 16:33:15 +0000 | [diff] [blame] | 84 | isl_ctx *Ctx; |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 85 | int NbLoopSpaces; |
Tobias Grosser | 3cc9974 | 2012-06-06 16:33:15 +0000 | [diff] [blame] | 86 | const Scop *S; |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 87 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 88 | SCEVAffinator(const ScopStmt *Stmt); |
| 89 | int getLoopDepth(const Loop *L); |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 90 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 91 | __isl_give isl_pw_aff *visit(const SCEV *Expr); |
| 92 | __isl_give isl_pw_aff *visitConstant(const SCEVConstant *Expr); |
| 93 | __isl_give isl_pw_aff *visitTruncateExpr(const SCEVTruncateExpr *Expr); |
| 94 | __isl_give isl_pw_aff *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr); |
| 95 | __isl_give isl_pw_aff *visitSignExtendExpr(const SCEVSignExtendExpr *Expr); |
| 96 | __isl_give isl_pw_aff *visitAddExpr(const SCEVAddExpr *Expr); |
| 97 | __isl_give isl_pw_aff *visitMulExpr(const SCEVMulExpr *Expr); |
| 98 | __isl_give isl_pw_aff *visitUDivExpr(const SCEVUDivExpr *Expr); |
| 99 | __isl_give isl_pw_aff *visitAddRecExpr(const SCEVAddRecExpr *Expr); |
| 100 | __isl_give isl_pw_aff *visitSMaxExpr(const SCEVSMaxExpr *Expr); |
| 101 | __isl_give isl_pw_aff *visitUMaxExpr(const SCEVUMaxExpr *Expr); |
| 102 | __isl_give isl_pw_aff *visitUnknown(const SCEVUnknown *Expr); |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 103 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 104 | friend struct SCEVVisitor<SCEVAffinator, isl_pw_aff *>; |
| 105 | }; |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 106 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 107 | SCEVAffinator::SCEVAffinator(const ScopStmt *Stmt) |
| 108 | : Ctx(Stmt->getIslCtx()), NbLoopSpaces(Stmt->getNumIterators()), |
| 109 | S(Stmt->getParent()) {} |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 110 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 111 | __isl_give isl_pw_aff *SCEVAffinator::getPwAff(ScopStmt *Stmt, |
| 112 | const SCEV *Scev) { |
| 113 | Scop *S = Stmt->getParent(); |
| 114 | const Region *Reg = &S->getRegion(); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 115 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 116 | S->addParams(getParamsInAffineExpr(Reg, Scev, *S->getSE())); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 117 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 118 | SCEVAffinator Affinator(Stmt); |
| 119 | return Affinator.visit(Scev); |
| 120 | } |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 121 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 122 | __isl_give isl_pw_aff *SCEVAffinator::visit(const SCEV *Expr) { |
| 123 | // In case the scev is a valid parameter, we do not further analyze this |
| 124 | // expression, but create a new parameter in the isl_pw_aff. This allows us |
| 125 | // to treat subexpressions that we cannot translate into an piecewise affine |
| 126 | // expression, as constant parameters of the piecewise affine expression. |
| 127 | if (isl_id *Id = S->getIdForParam(Expr)) { |
| 128 | isl_space *Space = isl_space_set_alloc(Ctx, 1, NbLoopSpaces); |
| 129 | Space = isl_space_set_dim_id(Space, isl_dim_param, 0, Id); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 130 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 131 | isl_set *Domain = isl_set_universe(isl_space_copy(Space)); |
| 132 | isl_aff *Affine = isl_aff_zero_on_domain(isl_local_space_from_space(Space)); |
| 133 | Affine = isl_aff_add_coefficient_si(Affine, isl_dim_param, 0, 1); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 134 | |
| 135 | return isl_pw_aff_alloc(Domain, Affine); |
| 136 | } |
| 137 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 138 | return SCEVVisitor<SCEVAffinator, isl_pw_aff *>::visit(Expr); |
| 139 | } |
| 140 | |
Tobias Grosser | 0d17013 | 2013-10-03 13:09:19 +0000 | [diff] [blame] | 141 | __isl_give isl_pw_aff *SCEVAffinator::visitConstant(const SCEVConstant *Expr) { |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 142 | ConstantInt *Value = Expr->getValue(); |
| 143 | isl_val *v; |
| 144 | |
| 145 | // LLVM does not define if an integer value is interpreted as a signed or |
| 146 | // unsigned value. Hence, without further information, it is unknown how |
| 147 | // this value needs to be converted to GMP. At the moment, we only support |
| 148 | // signed operations. So we just interpret it as signed. Later, there are |
| 149 | // two options: |
| 150 | // |
| 151 | // 1. We always interpret any value as signed and convert the values on |
| 152 | // demand. |
| 153 | // 2. We pass down the signedness of the calculation and use it to interpret |
| 154 | // this constant correctly. |
| 155 | v = isl_valFromAPInt(Ctx, Value->getValue(), /* isSigned */ true); |
| 156 | |
| 157 | isl_space *Space = isl_space_set_alloc(Ctx, 0, NbLoopSpaces); |
| 158 | isl_local_space *ls = isl_local_space_from_space(isl_space_copy(Space)); |
| 159 | isl_aff *Affine = isl_aff_zero_on_domain(ls); |
| 160 | isl_set *Domain = isl_set_universe(Space); |
| 161 | |
| 162 | Affine = isl_aff_add_constant_val(Affine, v); |
| 163 | |
| 164 | return isl_pw_aff_alloc(Domain, Affine); |
| 165 | } |
| 166 | |
| 167 | __isl_give isl_pw_aff * |
| 168 | SCEVAffinator::visitTruncateExpr(const SCEVTruncateExpr *Expr) { |
| 169 | llvm_unreachable("SCEVTruncateExpr not yet supported"); |
| 170 | } |
| 171 | |
| 172 | __isl_give isl_pw_aff * |
| 173 | SCEVAffinator::visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) { |
Johannes Doerfert | 5130c84 | 2014-08-15 01:14:11 +0000 | [diff] [blame] | 174 | ScalarEvolution &SE = *S->getSE(); |
| 175 | |
| 176 | const SCEV *OpS = Expr->getOperand(); |
| 177 | unsigned ModCst = 1 << OpS->getType()->getScalarSizeInBits(); |
| 178 | |
| 179 | // Pattern matching rules to capture some bit and modulo computations: |
| 180 | // |
| 181 | // EXP % 2^C <==> |
| 182 | // [A] (i + c) & (2^C - 1) ==> zext iC {c,+,1}<%for_i> to IXX |
| 183 | // [B] (p + q) & (2^C - 1) ==> zext iC (trunc iXX %p_add_q to iC) to iXX |
| 184 | // [C] (i + p) & (2^C - 1) ==> zext iC {p & (2^C - 1),+,1}<%for_i> to iXX |
| 185 | // ==> zext iC {trunc iXX %p to iC,+,1}<%for_i> to |
| 186 | |
| 187 | // Check for [A] and [C]. |
| 188 | if (auto *OpAR = dyn_cast<SCEVAddRecExpr>(OpS)) { |
| 189 | assert(OpAR->getStepRecurrence(SE)->isOne()); |
| 190 | |
| 191 | const SCEV *OpARStart = OpAR->getStart(); |
| 192 | const SCEV *OpARStep = OpAR->getStepRecurrence(SE); |
| 193 | |
| 194 | // Special case for [C]. |
| 195 | if (auto *OpARStartTR = dyn_cast<SCEVTruncateExpr>(OpARStart)) { |
| 196 | OpARStart = OpARStartTR->getOperand(); |
| 197 | OpARStep = SE.getConstant(OpARStart->getType(), 1); |
| 198 | } |
| 199 | |
| 200 | const SCEV *NewAR = SE.getAddRecExpr(OpARStart, OpARStep, OpAR->getLoop(), |
| 201 | OpAR->getNoWrapFlags()); |
| 202 | return isl_pw_aff_mod_val(visit(NewAR), isl_val_int_from_si(Ctx, ModCst)); |
| 203 | } |
| 204 | |
| 205 | // Check for [B]. |
| 206 | if (auto *OpTR = dyn_cast<SCEVTruncateExpr>(OpS)) |
| 207 | return isl_pw_aff_mod_val(visit(OpTR->getOperand()), |
| 208 | isl_val_int_from_si(Ctx, ModCst)); |
| 209 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 210 | llvm_unreachable("SCEVZeroExtendExpr not yet supported"); |
| 211 | } |
| 212 | |
| 213 | __isl_give isl_pw_aff * |
| 214 | SCEVAffinator::visitSignExtendExpr(const SCEVSignExtendExpr *Expr) { |
| 215 | // Assuming the value is signed, a sign extension is basically a noop. |
| 216 | // TODO: Reconsider this as soon as we support unsigned values. |
| 217 | return visit(Expr->getOperand()); |
| 218 | } |
| 219 | |
| 220 | __isl_give isl_pw_aff *SCEVAffinator::visitAddExpr(const SCEVAddExpr *Expr) { |
| 221 | isl_pw_aff *Sum = visit(Expr->getOperand(0)); |
| 222 | |
| 223 | for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) { |
| 224 | isl_pw_aff *NextSummand = visit(Expr->getOperand(i)); |
| 225 | Sum = isl_pw_aff_add(Sum, NextSummand); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 228 | // TODO: Check for NSW and NUW. |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 229 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 230 | return Sum; |
| 231 | } |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 232 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 233 | __isl_give isl_pw_aff *SCEVAffinator::visitMulExpr(const SCEVMulExpr *Expr) { |
| 234 | isl_pw_aff *Product = visit(Expr->getOperand(0)); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 235 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 236 | for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) { |
| 237 | isl_pw_aff *NextOperand = visit(Expr->getOperand(i)); |
| 238 | |
| 239 | if (!isl_pw_aff_is_cst(Product) && !isl_pw_aff_is_cst(NextOperand)) { |
| 240 | isl_pw_aff_free(Product); |
| 241 | isl_pw_aff_free(NextOperand); |
Tobias Grosser | 5a56cbf | 2014-04-16 07:33:47 +0000 | [diff] [blame] | 242 | return nullptr; |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 245 | Product = isl_pw_aff_mul(Product, NextOperand); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 248 | // TODO: Check for NSW and NUW. |
| 249 | return Product; |
| 250 | } |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 251 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 252 | __isl_give isl_pw_aff *SCEVAffinator::visitUDivExpr(const SCEVUDivExpr *Expr) { |
| 253 | llvm_unreachable("SCEVUDivExpr not yet supported"); |
| 254 | } |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 255 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 256 | __isl_give isl_pw_aff * |
| 257 | SCEVAffinator::visitAddRecExpr(const SCEVAddRecExpr *Expr) { |
| 258 | assert(Expr->isAffine() && "Only affine AddRecurrences allowed"); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 259 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 260 | // Directly generate isl_pw_aff for Expr if 'start' is zero. |
| 261 | if (Expr->getStart()->isZero()) { |
| 262 | assert(S->getRegion().contains(Expr->getLoop()) && |
| 263 | "Scop does not contain the loop referenced in this AddRec"); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 264 | |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 265 | isl_pw_aff *Start = visit(Expr->getStart()); |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 266 | isl_pw_aff *Step = visit(Expr->getOperand(1)); |
| 267 | isl_space *Space = isl_space_set_alloc(Ctx, 0, NbLoopSpaces); |
| 268 | isl_local_space *LocalSpace = isl_local_space_from_space(Space); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 269 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 270 | int loopDimension = getLoopDepth(Expr->getLoop()); |
| 271 | |
| 272 | isl_aff *LAff = isl_aff_set_coefficient_si( |
| 273 | isl_aff_zero_on_domain(LocalSpace), isl_dim_in, loopDimension, 1); |
| 274 | isl_pw_aff *LPwAff = isl_pw_aff_from_aff(LAff); |
| 275 | |
| 276 | // TODO: Do we need to check for NSW and NUW? |
| 277 | return isl_pw_aff_add(Start, isl_pw_aff_mul(Step, LPwAff)); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 280 | // Translate AddRecExpr from '{start, +, inc}' into 'start + {0, +, inc}' |
| 281 | // if 'start' is not zero. |
| 282 | ScalarEvolution &SE = *S->getSE(); |
| 283 | const SCEV *ZeroStartExpr = SE.getAddRecExpr( |
| 284 | SE.getConstant(Expr->getStart()->getType(), 0), |
| 285 | Expr->getStepRecurrence(SE), Expr->getLoop(), SCEV::FlagAnyWrap); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 286 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 287 | isl_pw_aff *ZeroStartResult = visit(ZeroStartExpr); |
| 288 | isl_pw_aff *Start = visit(Expr->getStart()); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 289 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 290 | return isl_pw_aff_add(ZeroStartResult, Start); |
| 291 | } |
| 292 | |
| 293 | __isl_give isl_pw_aff *SCEVAffinator::visitSMaxExpr(const SCEVSMaxExpr *Expr) { |
| 294 | isl_pw_aff *Max = visit(Expr->getOperand(0)); |
| 295 | |
| 296 | for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) { |
| 297 | isl_pw_aff *NextOperand = visit(Expr->getOperand(i)); |
| 298 | Max = isl_pw_aff_max(Max, NextOperand); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 301 | return Max; |
| 302 | } |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 303 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 304 | __isl_give isl_pw_aff *SCEVAffinator::visitUMaxExpr(const SCEVUMaxExpr *Expr) { |
| 305 | llvm_unreachable("SCEVUMaxExpr not yet supported"); |
| 306 | } |
| 307 | |
| 308 | __isl_give isl_pw_aff *SCEVAffinator::visitUnknown(const SCEVUnknown *Expr) { |
Johannes Doerfert | 5130c84 | 2014-08-15 01:14:11 +0000 | [diff] [blame] | 309 | Value *Unknown = Expr->getValue(); |
| 310 | if (auto *BO = dyn_cast<BinaryOperator>(Unknown)) { |
| 311 | isl_pw_aff *RHS, *LHS; |
| 312 | isl_aff *RHSAff; |
| 313 | isl_val *RHSVal; |
| 314 | |
| 315 | assert(BO->getOpcode() == Instruction::SRem && |
| 316 | "Binary operator unknowns are always signed modulo expressions"); |
| 317 | LHS = visit(S->getSE()->getSCEV(BO->getOperand(0))); |
| 318 | RHS = visit(S->getSE()->getSCEV(BO->getOperand(1))); |
| 319 | assert(isl_pw_aff_is_cst(RHS) && |
| 320 | "Modulo expressions are only valid for a constant right hand side"); |
| 321 | assert(isl_pw_aff_n_piece(RHS) == 1 && |
| 322 | "Modulo expressions are only valid for a non split right hand side"); |
| 323 | isl_pw_aff_foreach_piece(RHS, extractAffine, &RHSAff); |
| 324 | assert(isl_aff_is_cst(RHSAff) && |
| 325 | "Modulo expressions are only valid for a constant right hand side"); |
| 326 | RHSVal = isl_aff_get_constant_val(RHSAff); |
| 327 | |
| 328 | isl_pw_aff_free(RHS); |
| 329 | isl_aff_free(RHSAff); |
| 330 | |
| 331 | return isl_pw_aff_mod_val(LHS, RHSVal); |
| 332 | } |
| 333 | |
| 334 | llvm_unreachable("Unknowns are always parameters or modulo expressions"); |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | int SCEVAffinator::getLoopDepth(const Loop *L) { |
| 338 | Loop *outerLoop = S->getRegion().outermostLoopInRegion(const_cast<Loop *>(L)); |
| 339 | assert(outerLoop && "Scop does not contain this loop"); |
| 340 | return L->getLoopDepth() - outerLoop->getLoopDepth(); |
| 341 | } |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 342 | |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 343 | const std::string |
| 344 | MemoryAccess::getReductionOperatorStr(MemoryAccess::ReductionType RT) { |
| 345 | switch (RT) { |
| 346 | case MemoryAccess::RT_NONE: |
| 347 | llvm_unreachable("Requested a reduction operator string for a memory " |
| 348 | "access which isn't a reduction"); |
| 349 | case MemoryAccess::RT_ADD: |
| 350 | return "+"; |
| 351 | case MemoryAccess::RT_MUL: |
| 352 | return "*"; |
| 353 | case MemoryAccess::RT_BOR: |
| 354 | return "|"; |
| 355 | case MemoryAccess::RT_BXOR: |
| 356 | return "^"; |
| 357 | case MemoryAccess::RT_BAND: |
| 358 | return "&"; |
| 359 | } |
| 360 | llvm_unreachable("Unknown reduction type"); |
| 361 | return ""; |
| 362 | } |
| 363 | |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 364 | /// @brief Return the reduction type for a given binary operator |
| 365 | static MemoryAccess::ReductionType getReductionType(const BinaryOperator *BinOp, |
| 366 | const Instruction *Load) { |
| 367 | if (!BinOp) |
| 368 | return MemoryAccess::RT_NONE; |
| 369 | switch (BinOp->getOpcode()) { |
| 370 | case Instruction::FAdd: |
| 371 | if (!BinOp->hasUnsafeAlgebra()) |
| 372 | return MemoryAccess::RT_NONE; |
| 373 | // Fall through |
| 374 | case Instruction::Add: |
| 375 | return MemoryAccess::RT_ADD; |
| 376 | case Instruction::Or: |
| 377 | return MemoryAccess::RT_BOR; |
| 378 | case Instruction::Xor: |
| 379 | return MemoryAccess::RT_BXOR; |
| 380 | case Instruction::And: |
| 381 | return MemoryAccess::RT_BAND; |
| 382 | case Instruction::FMul: |
| 383 | if (!BinOp->hasUnsafeAlgebra()) |
| 384 | return MemoryAccess::RT_NONE; |
| 385 | // Fall through |
| 386 | case Instruction::Mul: |
| 387 | if (DisableMultiplicativeReductions) |
| 388 | return MemoryAccess::RT_NONE; |
| 389 | return MemoryAccess::RT_MUL; |
| 390 | default: |
| 391 | return MemoryAccess::RT_NONE; |
| 392 | } |
| 393 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 394 | //===----------------------------------------------------------------------===// |
| 395 | |
| 396 | MemoryAccess::~MemoryAccess() { |
Tobias Grosser | 54a86e6 | 2011-08-18 06:31:46 +0000 | [diff] [blame] | 397 | isl_map_free(AccessRelation); |
Raghesh Aloor | 129e867 | 2011-08-15 02:33:39 +0000 | [diff] [blame] | 398 | isl_map_free(newAccessRelation); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 401 | isl_id *MemoryAccess::getArrayId() const { |
| 402 | return isl_map_get_tuple_id(AccessRelation, isl_dim_out); |
| 403 | } |
| 404 | |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 405 | isl_map *MemoryAccess::getAccessRelation() const { |
| 406 | return isl_map_copy(AccessRelation); |
| 407 | } |
| 408 | |
| 409 | std::string MemoryAccess::getAccessRelationStr() const { |
| 410 | return stringFromIslObj(AccessRelation); |
| 411 | } |
| 412 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 413 | __isl_give isl_space *MemoryAccess::getAccessRelationSpace() const { |
| 414 | return isl_map_get_space(AccessRelation); |
| 415 | } |
| 416 | |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 417 | isl_map *MemoryAccess::getNewAccessRelation() const { |
| 418 | return isl_map_copy(newAccessRelation); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | isl_basic_map *MemoryAccess::createBasicAccessMap(ScopStmt *Statement) { |
Tobias Grosser | 084d8f7 | 2012-05-29 09:29:44 +0000 | [diff] [blame] | 422 | isl_space *Space = isl_space_set_alloc(Statement->getIslCtx(), 0, 1); |
Tobias Grosser | ed29566 | 2012-09-11 13:50:21 +0000 | [diff] [blame] | 423 | Space = isl_space_align_params(Space, Statement->getDomainSpace()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 424 | |
Tobias Grosser | 084d8f7 | 2012-05-29 09:29:44 +0000 | [diff] [blame] | 425 | return isl_basic_map_from_domain_and_range( |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 426 | isl_basic_set_universe(Statement->getDomainSpace()), |
| 427 | isl_basic_set_universe(Space)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 430 | // Formalize no out-of-bound access assumption |
| 431 | // |
| 432 | // When delinearizing array accesses we optimistically assume that the |
| 433 | // delinearized accesses do not access out of bound locations (the subscript |
| 434 | // expression of each array evaluates for each statement instance that is |
| 435 | // executed to a value that is larger than zero and strictly smaller than the |
| 436 | // size of the corresponding dimension). The only exception is the outermost |
Tobias Grosser | f57d63f | 2014-08-03 21:07:30 +0000 | [diff] [blame] | 437 | // dimension for which we do not need to assume any upper bound. At this point |
| 438 | // we formalize this assumption to ensure that at code generation time the |
| 439 | // relevant run-time checks can be generated. |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 440 | // |
| 441 | // To find the set of constraints necessary to avoid out of bound accesses, we |
| 442 | // first build the set of data locations that are not within array bounds. We |
| 443 | // then apply the reverse access relation to obtain the set of iterations that |
| 444 | // may contain invalid accesses and reduce this set of iterations to the ones |
| 445 | // that are actually executed by intersecting them with the domain of the |
| 446 | // statement. If we now project out all loop dimensions, we obtain a set of |
| 447 | // parameters that may cause statement instances to be executed that may |
| 448 | // possibly yield out of bound memory accesses. The complement of these |
| 449 | // constraints is the set of constraints that needs to be assumed to ensure such |
| 450 | // statement instances are never executed. |
| 451 | void MemoryAccess::assumeNoOutOfBound(const IRAccess &Access) { |
| 452 | isl_space *Space = isl_space_range(getAccessRelationSpace()); |
| 453 | isl_set *Outside = isl_set_empty(isl_space_copy(Space)); |
Tobias Grosser | f57d63f | 2014-08-03 21:07:30 +0000 | [diff] [blame] | 454 | for (int i = 1, Size = Access.Subscripts.size(); i < Size; ++i) { |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 455 | isl_local_space *LS = isl_local_space_from_space(isl_space_copy(Space)); |
| 456 | isl_pw_aff *Var = |
| 457 | isl_pw_aff_var_on_domain(isl_local_space_copy(LS), isl_dim_set, i); |
| 458 | isl_pw_aff *Zero = isl_pw_aff_zero_on_domain(LS); |
| 459 | |
| 460 | isl_set *DimOutside; |
| 461 | |
Tobias Grosser | f57d63f | 2014-08-03 21:07:30 +0000 | [diff] [blame] | 462 | DimOutside = isl_pw_aff_lt_set(isl_pw_aff_copy(Var), Zero); |
| 463 | isl_pw_aff *SizeE = SCEVAffinator::getPwAff(Statement, Access.Sizes[i - 1]); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 464 | |
Tobias Grosser | f57d63f | 2014-08-03 21:07:30 +0000 | [diff] [blame] | 465 | SizeE = isl_pw_aff_drop_dims(SizeE, isl_dim_in, 0, |
| 466 | Statement->getNumIterators()); |
| 467 | SizeE = isl_pw_aff_add_dims(SizeE, isl_dim_in, |
| 468 | isl_space_dim(Space, isl_dim_set)); |
| 469 | SizeE = isl_pw_aff_set_tuple_id(SizeE, isl_dim_in, |
| 470 | isl_space_get_tuple_id(Space, isl_dim_set)); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 471 | |
Tobias Grosser | f57d63f | 2014-08-03 21:07:30 +0000 | [diff] [blame] | 472 | DimOutside = isl_set_union(DimOutside, isl_pw_aff_le_set(SizeE, Var)); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 473 | |
| 474 | Outside = isl_set_union(Outside, DimOutside); |
| 475 | } |
| 476 | |
| 477 | Outside = isl_set_apply(Outside, isl_map_reverse(getAccessRelation())); |
| 478 | Outside = isl_set_intersect(Outside, Statement->getDomain()); |
| 479 | Outside = isl_set_params(Outside); |
| 480 | Outside = isl_set_complement(Outside); |
| 481 | Statement->getParent()->addAssumption(Outside); |
| 482 | isl_space_free(Space); |
| 483 | } |
| 484 | |
Johannes Doerfert | 13c8cf2 | 2014-08-10 08:09:38 +0000 | [diff] [blame] | 485 | MemoryAccess::MemoryAccess(const IRAccess &Access, Instruction *AccInst, |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 486 | ScopStmt *Statement) |
Tobias Grosser | 5a56cbf | 2014-04-16 07:33:47 +0000 | [diff] [blame] | 487 | : Statement(Statement), Inst(AccInst), newAccessRelation(nullptr) { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 488 | |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 489 | isl_ctx *Ctx = Statement->getIslCtx(); |
Tobias Grosser | 9759f85 | 2011-11-10 12:44:55 +0000 | [diff] [blame] | 490 | BaseAddr = Access.getBase(); |
Johannes Doerfert | 79fc23f | 2014-07-24 23:48:02 +0000 | [diff] [blame] | 491 | BaseName = getIslCompatibleName("MemRef_", getBaseAddr(), ""); |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 492 | isl_id *BaseAddrId = isl_id_alloc(Ctx, getBaseName().c_str(), nullptr); |
Tobias Grosser | 5683df4 | 2011-11-09 22:34:34 +0000 | [diff] [blame] | 493 | |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 494 | if (!Access.isAffine()) { |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 495 | // We overapproximate non-affine accesses with a possible access to the |
| 496 | // whole array. For read accesses it does not make a difference, if an |
| 497 | // access must or may happen. However, for write accesses it is important to |
| 498 | // differentiate between writes that must happen and writes that may happen. |
Tobias Grosser | 04d6ae6 | 2013-06-23 06:04:54 +0000 | [diff] [blame] | 499 | AccessRelation = isl_map_from_basic_map(createBasicAccessMap(Statement)); |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 500 | AccessRelation = |
| 501 | isl_map_set_tuple_id(AccessRelation, isl_dim_out, BaseAddrId); |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 502 | Type = Access.isRead() ? READ : MAY_WRITE; |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 503 | return; |
| 504 | } |
| 505 | |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 506 | Type = Access.isRead() ? READ : MUST_WRITE; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 507 | |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 508 | isl_space *Space = isl_space_alloc(Ctx, 0, Statement->getNumIterators(), 0); |
Tobias Grosser | 79baa21 | 2014-04-10 08:38:02 +0000 | [diff] [blame] | 509 | AccessRelation = isl_map_universe(Space); |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 510 | |
Tobias Grosser | 79baa21 | 2014-04-10 08:38:02 +0000 | [diff] [blame] | 511 | for (int i = 0, Size = Access.Subscripts.size(); i < Size; ++i) { |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 512 | isl_pw_aff *Affine = |
| 513 | SCEVAffinator::getPwAff(Statement, Access.Subscripts[i]); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 514 | |
Sebastian Pop | 422e33f | 2014-06-03 18:16:31 +0000 | [diff] [blame] | 515 | if (Size == 1) { |
| 516 | // For the non delinearized arrays, divide the access function of the last |
| 517 | // subscript by the size of the elements in the array. |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 518 | // |
| 519 | // A stride one array access in C expressed as A[i] is expressed in |
| 520 | // LLVM-IR as something like A[i * elementsize]. This hides the fact that |
| 521 | // two subsequent values of 'i' index two values that are stored next to |
| 522 | // each other in memory. By this division we make this characteristic |
| 523 | // obvious again. |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 524 | isl_val *v = isl_val_int_from_si(Ctx, Access.getElemSizeInBytes()); |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 525 | Affine = isl_pw_aff_scale_down_val(Affine, v); |
| 526 | } |
| 527 | |
| 528 | isl_map *SubscriptMap = isl_map_from_pw_aff(Affine); |
| 529 | |
Tobias Grosser | 79baa21 | 2014-04-10 08:38:02 +0000 | [diff] [blame] | 530 | AccessRelation = isl_map_flat_range_product(AccessRelation, SubscriptMap); |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Tobias Grosser | 79baa21 | 2014-04-10 08:38:02 +0000 | [diff] [blame] | 533 | Space = Statement->getDomainSpace(); |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 534 | AccessRelation = isl_map_set_tuple_id( |
| 535 | AccessRelation, isl_dim_in, isl_space_get_tuple_id(Space, isl_dim_set)); |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 536 | AccessRelation = |
| 537 | isl_map_set_tuple_id(AccessRelation, isl_dim_out, BaseAddrId); |
| 538 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 539 | assumeNoOutOfBound(Access); |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 540 | isl_space_free(Space); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 541 | } |
Tobias Grosser | 30b8a09 | 2011-08-18 07:51:37 +0000 | [diff] [blame] | 542 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 543 | void MemoryAccess::realignParams() { |
Tobias Grosser | 6defb5b | 2014-04-10 08:37:44 +0000 | [diff] [blame] | 544 | isl_space *ParamSpace = Statement->getParent()->getParamSpace(); |
Tobias Grosser | 3748705 | 2011-10-06 00:03:42 +0000 | [diff] [blame] | 545 | AccessRelation = isl_map_align_params(AccessRelation, ParamSpace); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 548 | const std::string MemoryAccess::getReductionOperatorStr() const { |
| 549 | return MemoryAccess::getReductionOperatorStr(getReductionType()); |
| 550 | } |
| 551 | |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 552 | raw_ostream &polly::operator<<(raw_ostream &OS, |
| 553 | MemoryAccess::ReductionType RT) { |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 554 | if (RT == MemoryAccess::RT_NONE) |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 555 | OS << "NONE"; |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 556 | else |
| 557 | OS << MemoryAccess::getReductionOperatorStr(RT); |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 558 | return OS; |
| 559 | } |
| 560 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 561 | void MemoryAccess::print(raw_ostream &OS) const { |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 562 | switch (Type) { |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 563 | case READ: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 564 | OS.indent(12) << "ReadAccess :=\t"; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 565 | break; |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 566 | case MUST_WRITE: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 567 | OS.indent(12) << "MustWriteAccess :=\t"; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 568 | break; |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 569 | case MAY_WRITE: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 570 | OS.indent(12) << "MayWriteAccess :=\t"; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 571 | break; |
| 572 | } |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 573 | OS << "[Reduction Type: " << getReductionType() << "]\n"; |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 574 | OS.indent(16) << getAccessRelationStr() << ";\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 577 | void MemoryAccess::dump() const { print(errs()); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 578 | |
| 579 | // Create a map in the size of the provided set domain, that maps from the |
| 580 | // one element of the provided set domain to another element of the provided |
| 581 | // set domain. |
| 582 | // The mapping is limited to all points that are equal in all but the last |
| 583 | // dimension and for which the last dimension of the input is strict smaller |
| 584 | // than the last dimension of the output. |
| 585 | // |
| 586 | // getEqualAndLarger(set[i0, i1, ..., iX]): |
| 587 | // |
| 588 | // set[i0, i1, ..., iX] -> set[o0, o1, ..., oX] |
| 589 | // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1), iX < oX |
| 590 | // |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 591 | static isl_map *getEqualAndLarger(isl_space *setDomain) { |
Tobias Grosser | c327932c | 2012-02-01 14:23:36 +0000 | [diff] [blame] | 592 | isl_space *Space = isl_space_map_from_set(setDomain); |
| 593 | isl_map *Map = isl_map_universe(isl_space_copy(Space)); |
| 594 | isl_local_space *MapLocalSpace = isl_local_space_from_space(Space); |
Sebastian Pop | 4040876 | 2013-10-04 17:14:53 +0000 | [diff] [blame] | 595 | unsigned lastDimension = isl_map_dim(Map, isl_dim_in) - 1; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 596 | |
| 597 | // Set all but the last dimension to be equal for the input and output |
| 598 | // |
| 599 | // input[i0, i1, ..., iX] -> output[o0, o1, ..., oX] |
| 600 | // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1) |
Sebastian Pop | 4040876 | 2013-10-04 17:14:53 +0000 | [diff] [blame] | 601 | for (unsigned i = 0; i < lastDimension; ++i) |
Tobias Grosser | c327932c | 2012-02-01 14:23:36 +0000 | [diff] [blame] | 602 | Map = isl_map_equate(Map, isl_dim_in, i, isl_dim_out, i); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 603 | |
| 604 | // Set the last dimension of the input to be strict smaller than the |
| 605 | // last dimension of the output. |
| 606 | // |
| 607 | // input[?,?,?,...,iX] -> output[?,?,?,...,oX] : iX < oX |
| 608 | // |
Tobias Grosser | edab135 | 2013-06-21 06:41:31 +0000 | [diff] [blame] | 609 | isl_val *v; |
| 610 | isl_ctx *Ctx = isl_map_get_ctx(Map); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 611 | isl_constraint *c = isl_inequality_alloc(isl_local_space_copy(MapLocalSpace)); |
Tobias Grosser | edab135 | 2013-06-21 06:41:31 +0000 | [diff] [blame] | 612 | v = isl_val_int_from_si(Ctx, -1); |
| 613 | c = isl_constraint_set_coefficient_val(c, isl_dim_in, lastDimension, v); |
| 614 | v = isl_val_int_from_si(Ctx, 1); |
| 615 | c = isl_constraint_set_coefficient_val(c, isl_dim_out, lastDimension, v); |
| 616 | v = isl_val_int_from_si(Ctx, -1); |
| 617 | c = isl_constraint_set_constant_val(c, v); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 618 | |
Tobias Grosser | c327932c | 2012-02-01 14:23:36 +0000 | [diff] [blame] | 619 | Map = isl_map_add_constraint(Map, c); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 620 | |
Tobias Grosser | 23b3666 | 2011-10-17 08:32:36 +0000 | [diff] [blame] | 621 | isl_local_space_free(MapLocalSpace); |
Tobias Grosser | c327932c | 2012-02-01 14:23:36 +0000 | [diff] [blame] | 622 | return Map; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 625 | isl_set *MemoryAccess::getStride(__isl_take const isl_map *Schedule) const { |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 626 | isl_map *S = const_cast<isl_map *>(Schedule); |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 627 | isl_map *AccessRelation = getAccessRelation(); |
| 628 | isl_space *Space = isl_space_range(isl_map_get_space(S)); |
| 629 | isl_map *NextScatt = getEqualAndLarger(Space); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 630 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 631 | S = isl_map_reverse(S); |
| 632 | NextScatt = isl_map_lexmin(NextScatt); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 633 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 634 | NextScatt = isl_map_apply_range(NextScatt, isl_map_copy(S)); |
| 635 | NextScatt = isl_map_apply_range(NextScatt, isl_map_copy(AccessRelation)); |
| 636 | NextScatt = isl_map_apply_domain(NextScatt, S); |
| 637 | NextScatt = isl_map_apply_domain(NextScatt, AccessRelation); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 638 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 639 | isl_set *Deltas = isl_map_deltas(NextScatt); |
| 640 | return Deltas; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 643 | bool MemoryAccess::isStrideX(__isl_take const isl_map *Schedule, |
Tobias Grosser | 28dd486 | 2012-01-24 16:42:16 +0000 | [diff] [blame] | 644 | int StrideWidth) const { |
| 645 | isl_set *Stride, *StrideX; |
| 646 | bool IsStrideX; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 647 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 648 | Stride = getStride(Schedule); |
Tobias Grosser | 28dd486 | 2012-01-24 16:42:16 +0000 | [diff] [blame] | 649 | StrideX = isl_set_universe(isl_set_get_space(Stride)); |
| 650 | StrideX = isl_set_fix_si(StrideX, isl_dim_set, 0, StrideWidth); |
| 651 | IsStrideX = isl_set_is_equal(Stride, StrideX); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 652 | |
Tobias Grosser | 28dd486 | 2012-01-24 16:42:16 +0000 | [diff] [blame] | 653 | isl_set_free(StrideX); |
Tobias Grosser | dea9823 | 2012-01-17 20:34:27 +0000 | [diff] [blame] | 654 | isl_set_free(Stride); |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 655 | |
Tobias Grosser | 28dd486 | 2012-01-24 16:42:16 +0000 | [diff] [blame] | 656 | return IsStrideX; |
| 657 | } |
| 658 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 659 | bool MemoryAccess::isStrideZero(const isl_map *Schedule) const { |
| 660 | return isStrideX(Schedule, 0); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Tobias Grosser | 79baa21 | 2014-04-10 08:38:02 +0000 | [diff] [blame] | 663 | bool MemoryAccess::isScalar() const { |
| 664 | return isl_map_n_out(AccessRelation) == 0; |
| 665 | } |
| 666 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 667 | bool MemoryAccess::isStrideOne(const isl_map *Schedule) const { |
| 668 | return isStrideX(Schedule, 1); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 671 | void MemoryAccess::setNewAccessRelation(isl_map *newAccess) { |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 672 | isl_map_free(newAccessRelation); |
Raghesh Aloor | 7a04f4f | 2011-08-03 13:47:59 +0000 | [diff] [blame] | 673 | newAccessRelation = newAccess; |
Raghesh Aloor | 3cb6628 | 2011-07-12 17:14:03 +0000 | [diff] [blame] | 674 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 675 | |
| 676 | //===----------------------------------------------------------------------===// |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 677 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 678 | isl_map *ScopStmt::getScattering() const { return isl_map_copy(Scattering); } |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 679 | |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 680 | void ScopStmt::restrictDomain(__isl_take isl_set *NewDomain) { |
| 681 | assert(isl_set_is_subset(NewDomain, Domain) && |
| 682 | "New domain is not a subset of old domain!"); |
| 683 | isl_set_free(Domain); |
| 684 | Domain = NewDomain; |
| 685 | Scattering = isl_map_intersect_domain(Scattering, isl_set_copy(Domain)); |
| 686 | } |
| 687 | |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 688 | void ScopStmt::setScattering(isl_map *NewScattering) { |
Tobias Grosser | 5a56cbf | 2014-04-16 07:33:47 +0000 | [diff] [blame] | 689 | assert(NewScattering && "New scattering is nullptr"); |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 690 | isl_map_free(Scattering); |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 691 | Scattering = NewScattering; |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 694 | void ScopStmt::buildScattering(SmallVectorImpl<unsigned> &Scatter) { |
Tobias Grosser | 78d8a3d | 2012-01-17 20:34:23 +0000 | [diff] [blame] | 695 | unsigned NbIterators = getNumIterators(); |
| 696 | unsigned NbScatteringDims = Parent.getMaxLoopDepth() * 2 + 1; |
| 697 | |
Tobias Grosser | 084d8f7 | 2012-05-29 09:29:44 +0000 | [diff] [blame] | 698 | isl_space *Space = isl_space_set_alloc(getIslCtx(), 0, NbScatteringDims); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 699 | Space = isl_space_set_tuple_name(Space, isl_dim_out, "scattering"); |
Tobias Grosser | 78d8a3d | 2012-01-17 20:34:23 +0000 | [diff] [blame] | 700 | |
Tobias Grosser | 084d8f7 | 2012-05-29 09:29:44 +0000 | [diff] [blame] | 701 | Scattering = isl_map_from_domain_and_range(isl_set_universe(getDomainSpace()), |
| 702 | isl_set_universe(Space)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 703 | |
| 704 | // Loop dimensions. |
Tobias Grosser | 78d8a3d | 2012-01-17 20:34:23 +0000 | [diff] [blame] | 705 | for (unsigned i = 0; i < NbIterators; ++i) |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 706 | Scattering = |
| 707 | isl_map_equate(Scattering, isl_dim_out, 2 * i + 1, isl_dim_in, i); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 708 | |
| 709 | // Constant dimensions |
Tobias Grosser | 78d8a3d | 2012-01-17 20:34:23 +0000 | [diff] [blame] | 710 | for (unsigned i = 0; i < NbIterators + 1; ++i) |
| 711 | Scattering = isl_map_fix_si(Scattering, isl_dim_out, 2 * i, Scatter[i]); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 712 | |
| 713 | // Fill scattering dimensions. |
Tobias Grosser | 78d8a3d | 2012-01-17 20:34:23 +0000 | [diff] [blame] | 714 | for (unsigned i = 2 * NbIterators + 1; i < NbScatteringDims; ++i) |
| 715 | Scattering = isl_map_fix_si(Scattering, isl_dim_out, i, 0); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 716 | |
Tobias Grosser | 3748705 | 2011-10-06 00:03:42 +0000 | [diff] [blame] | 717 | Scattering = isl_map_align_params(Scattering, Parent.getParamSpace()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | void ScopStmt::buildAccesses(TempScop &tempScop, const Region &CurRegion) { |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 721 | for (auto &&Access : *tempScop.getAccessFunctions(BB)) { |
| 722 | MemAccs.push_back(new MemoryAccess(Access.first, Access.second, this)); |
Tobias Grosser | d6aafa7 | 2014-02-20 21:29:09 +0000 | [diff] [blame] | 723 | |
| 724 | // We do not track locations for scalar memory accesses at the moment. |
| 725 | // |
| 726 | // We do not have a use for this information at the moment. If we need this |
| 727 | // at some point, the "instruction -> access" mapping needs to be enhanced |
| 728 | // as a single instruction could then possibly perform multiple accesses. |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 729 | if (!Access.first.isScalar()) { |
| 730 | assert(!InstructionToAccess.count(Access.second) && |
Tobias Grosser | 3fc9154 | 2014-02-20 21:43:45 +0000 | [diff] [blame] | 731 | "Unexpected 1-to-N mapping on instruction to access map!"); |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 732 | InstructionToAccess[Access.second] = MemAccs.back(); |
Tobias Grosser | d6aafa7 | 2014-02-20 21:29:09 +0000 | [diff] [blame] | 733 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 737 | void ScopStmt::realignParams() { |
Johannes Doerfert | f675289 | 2014-06-13 18:01:45 +0000 | [diff] [blame] | 738 | for (MemoryAccess *MA : *this) |
| 739 | MA->realignParams(); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 740 | |
| 741 | Domain = isl_set_align_params(Domain, Parent.getParamSpace()); |
| 742 | Scattering = isl_map_align_params(Scattering, Parent.getParamSpace()); |
| 743 | } |
| 744 | |
Tobias Grosser | 65b0058 | 2011-11-08 15:41:19 +0000 | [diff] [blame] | 745 | __isl_give isl_set *ScopStmt::buildConditionSet(const Comparison &Comp) { |
Tobias Grosser | a601fbd | 2011-11-09 22:34:44 +0000 | [diff] [blame] | 746 | isl_pw_aff *L = SCEVAffinator::getPwAff(this, Comp.getLHS()); |
| 747 | isl_pw_aff *R = SCEVAffinator::getPwAff(this, Comp.getRHS()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 748 | |
Tobias Grosser | d2795d0 | 2011-08-18 07:51:40 +0000 | [diff] [blame] | 749 | switch (Comp.getPred()) { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 750 | case ICmpInst::ICMP_EQ: |
Tobias Grosser | 048c879 | 2011-10-23 20:59:20 +0000 | [diff] [blame] | 751 | return isl_pw_aff_eq_set(L, R); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 752 | case ICmpInst::ICMP_NE: |
Tobias Grosser | 048c879 | 2011-10-23 20:59:20 +0000 | [diff] [blame] | 753 | return isl_pw_aff_ne_set(L, R); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 754 | case ICmpInst::ICMP_SLT: |
Tobias Grosser | 048c879 | 2011-10-23 20:59:20 +0000 | [diff] [blame] | 755 | return isl_pw_aff_lt_set(L, R); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 756 | case ICmpInst::ICMP_SLE: |
Tobias Grosser | 048c879 | 2011-10-23 20:59:20 +0000 | [diff] [blame] | 757 | return isl_pw_aff_le_set(L, R); |
Tobias Grosser | d2795d0 | 2011-08-18 07:51:40 +0000 | [diff] [blame] | 758 | case ICmpInst::ICMP_SGT: |
Tobias Grosser | 048c879 | 2011-10-23 20:59:20 +0000 | [diff] [blame] | 759 | return isl_pw_aff_gt_set(L, R); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 760 | case ICmpInst::ICMP_SGE: |
Tobias Grosser | 048c879 | 2011-10-23 20:59:20 +0000 | [diff] [blame] | 761 | return isl_pw_aff_ge_set(L, R); |
Tobias Grosser | d2795d0 | 2011-08-18 07:51:40 +0000 | [diff] [blame] | 762 | case ICmpInst::ICMP_ULT: |
| 763 | case ICmpInst::ICMP_UGT: |
| 764 | case ICmpInst::ICMP_ULE: |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 765 | case ICmpInst::ICMP_UGE: |
Tobias Grosser | d2795d0 | 2011-08-18 07:51:40 +0000 | [diff] [blame] | 766 | llvm_unreachable("Unsigned comparisons not yet supported"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 767 | default: |
| 768 | llvm_unreachable("Non integer predicate not supported"); |
| 769 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 772 | __isl_give isl_set *ScopStmt::addLoopBoundsToDomain(__isl_take isl_set *Domain, |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 773 | TempScop &tempScop) { |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 774 | isl_space *Space; |
| 775 | isl_local_space *LocalSpace; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 776 | |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 777 | Space = isl_set_get_space(Domain); |
| 778 | LocalSpace = isl_local_space_from_space(Space); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 779 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 780 | for (int i = 0, e = getNumIterators(); i != e; ++i) { |
Tobias Grosser | 9b13d3d | 2011-10-06 22:32:58 +0000 | [diff] [blame] | 781 | isl_aff *Zero = isl_aff_zero_on_domain(isl_local_space_copy(LocalSpace)); |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 782 | isl_pw_aff *IV = |
| 783 | isl_pw_aff_from_aff(isl_aff_set_coefficient_si(Zero, isl_dim_in, i, 1)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 784 | |
Tobias Grosser | 9b13d3d | 2011-10-06 22:32:58 +0000 | [diff] [blame] | 785 | // 0 <= IV. |
| 786 | isl_set *LowerBound = isl_pw_aff_nonneg_set(isl_pw_aff_copy(IV)); |
| 787 | Domain = isl_set_intersect(Domain, LowerBound); |
| 788 | |
| 789 | // IV <= LatchExecutions. |
Hongbin Zheng | 27f3afb | 2011-04-30 03:26:51 +0000 | [diff] [blame] | 790 | const Loop *L = getLoopForDimension(i); |
Tobias Grosser | 1179afa | 2011-11-02 21:37:51 +0000 | [diff] [blame] | 791 | const SCEV *LatchExecutions = tempScop.getLoopBound(L); |
Tobias Grosser | 9b13d3d | 2011-10-06 22:32:58 +0000 | [diff] [blame] | 792 | isl_pw_aff *UpperBound = SCEVAffinator::getPwAff(this, LatchExecutions); |
| 793 | isl_set *UpperBoundSet = isl_pw_aff_le_set(IV, UpperBound); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 794 | Domain = isl_set_intersect(Domain, UpperBoundSet); |
| 795 | } |
| 796 | |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 797 | isl_local_space_free(LocalSpace); |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 798 | return Domain; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 801 | __isl_give isl_set *ScopStmt::addConditionsToDomain(__isl_take isl_set *Domain, |
| 802 | TempScop &tempScop, |
| 803 | const Region &CurRegion) { |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 804 | const Region *TopRegion = tempScop.getMaxRegion().getParent(), |
Tobias Grosser | d7e5864 | 2013-04-10 06:55:45 +0000 | [diff] [blame] | 805 | *CurrentRegion = &CurRegion; |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 806 | const BasicBlock *BranchingBB = BB; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 807 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 808 | do { |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 809 | if (BranchingBB != CurrentRegion->getEntry()) { |
| 810 | if (const BBCond *Condition = tempScop.getBBCond(BranchingBB)) |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 811 | for (const auto &C : *Condition) { |
| 812 | isl_set *ConditionSet = buildConditionSet(C); |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 813 | Domain = isl_set_intersect(Domain, ConditionSet); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 814 | } |
| 815 | } |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 816 | BranchingBB = CurrentRegion->getEntry(); |
| 817 | CurrentRegion = CurrentRegion->getParent(); |
| 818 | } while (TopRegion != CurrentRegion); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 819 | |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 820 | return Domain; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 823 | __isl_give isl_set *ScopStmt::buildDomain(TempScop &tempScop, |
| 824 | const Region &CurRegion) { |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 825 | isl_space *Space; |
| 826 | isl_set *Domain; |
Tobias Grosser | 084d8f7 | 2012-05-29 09:29:44 +0000 | [diff] [blame] | 827 | isl_id *Id; |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 828 | |
| 829 | Space = isl_space_set_alloc(getIslCtx(), 0, getNumIterators()); |
| 830 | |
Tobias Grosser | 084d8f7 | 2012-05-29 09:29:44 +0000 | [diff] [blame] | 831 | Id = isl_id_alloc(getIslCtx(), getBaseName(), this); |
| 832 | |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 833 | Domain = isl_set_universe(Space); |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 834 | Domain = addLoopBoundsToDomain(Domain, tempScop); |
| 835 | Domain = addConditionsToDomain(Domain, tempScop, CurRegion); |
Tobias Grosser | 084d8f7 | 2012-05-29 09:29:44 +0000 | [diff] [blame] | 836 | Domain = isl_set_set_tuple_id(Domain, Id); |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 837 | |
| 838 | return Domain; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 841 | ScopStmt::ScopStmt(Scop &parent, TempScop &tempScop, const Region &CurRegion, |
Sebastian Pop | 860e021 | 2013-02-15 21:26:44 +0000 | [diff] [blame] | 842 | BasicBlock &bb, SmallVectorImpl<Loop *> &Nest, |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 843 | SmallVectorImpl<unsigned> &Scatter) |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 844 | : Parent(parent), BB(&bb), IVS(Nest.size()), NestLoops(Nest.size()) { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 845 | // Setup the induction variables. |
Sebastian Pop | 860e021 | 2013-02-15 21:26:44 +0000 | [diff] [blame] | 846 | for (unsigned i = 0, e = Nest.size(); i < e; ++i) { |
Sebastian Pop | 27c10c6 | 2013-03-22 22:07:43 +0000 | [diff] [blame] | 847 | if (!SCEVCodegen) { |
| 848 | PHINode *PN = Nest[i]->getCanonicalInductionVariable(); |
| 849 | assert(PN && "Non canonical IV in Scop!"); |
Tobias Grosser | 826b2af | 2013-03-21 16:14:50 +0000 | [diff] [blame] | 850 | IVS[i] = PN; |
Sebastian Pop | 27c10c6 | 2013-03-22 22:07:43 +0000 | [diff] [blame] | 851 | } |
Sebastian Pop | 860e021 | 2013-02-15 21:26:44 +0000 | [diff] [blame] | 852 | NestLoops[i] = Nest[i]; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Johannes Doerfert | 79fc23f | 2014-07-24 23:48:02 +0000 | [diff] [blame] | 855 | BaseName = getIslCompatibleName("Stmt_", &bb, ""); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 856 | |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 857 | Domain = buildDomain(tempScop, CurRegion); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 858 | buildScattering(Scatter); |
| 859 | buildAccesses(tempScop, CurRegion); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 860 | checkForReductions(); |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 863 | /// @brief Collect loads which might form a reduction chain with @p StoreMA |
| 864 | /// |
| 865 | /// Check if the stored value for @p StoreMA is a binary operator with one or |
| 866 | /// two loads as operands. If the binary operand is commutative & associative, |
| 867 | /// used only once (by @p StoreMA) and its load operands are also used only |
| 868 | /// once, we have found a possible reduction chain. It starts at an operand |
| 869 | /// load and includes the binary operator and @p StoreMA. |
| 870 | /// |
| 871 | /// Note: We allow only one use to ensure the load and binary operator cannot |
| 872 | /// escape this block or into any other store except @p StoreMA. |
| 873 | void ScopStmt::collectCandiateReductionLoads( |
| 874 | MemoryAccess *StoreMA, SmallVectorImpl<MemoryAccess *> &Loads) { |
| 875 | auto *Store = dyn_cast<StoreInst>(StoreMA->getAccessInstruction()); |
| 876 | if (!Store) |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 877 | return; |
| 878 | |
| 879 | // Skip if there is not one binary operator between the load and the store |
| 880 | auto *BinOp = dyn_cast<BinaryOperator>(Store->getValueOperand()); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 881 | if (!BinOp) |
| 882 | return; |
| 883 | |
| 884 | // Skip if the binary operators has multiple uses |
| 885 | if (BinOp->getNumUses() != 1) |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 886 | return; |
| 887 | |
| 888 | // Skip if the opcode of the binary operator is not commutative/associative |
| 889 | if (!BinOp->isCommutative() || !BinOp->isAssociative()) |
| 890 | return; |
| 891 | |
Johannes Doerfert | 9890a05 | 2014-07-01 00:32:29 +0000 | [diff] [blame] | 892 | // Skip if the binary operator is outside the current SCoP |
| 893 | if (BinOp->getParent() != Store->getParent()) |
| 894 | return; |
| 895 | |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 896 | // Skip if it is a multiplicative reduction and we disabled them |
| 897 | if (DisableMultiplicativeReductions && |
| 898 | (BinOp->getOpcode() == Instruction::Mul || |
| 899 | BinOp->getOpcode() == Instruction::FMul)) |
| 900 | return; |
| 901 | |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 902 | // Check the binary operator operands for a candidate load |
| 903 | auto *PossibleLoad0 = dyn_cast<LoadInst>(BinOp->getOperand(0)); |
| 904 | auto *PossibleLoad1 = dyn_cast<LoadInst>(BinOp->getOperand(1)); |
| 905 | if (!PossibleLoad0 && !PossibleLoad1) |
| 906 | return; |
| 907 | |
| 908 | // A load is only a candidate if it cannot escape (thus has only this use) |
| 909 | if (PossibleLoad0 && PossibleLoad0->getNumUses() == 1) |
Johannes Doerfert | 9890a05 | 2014-07-01 00:32:29 +0000 | [diff] [blame] | 910 | if (PossibleLoad0->getParent() == Store->getParent()) |
| 911 | Loads.push_back(lookupAccessFor(PossibleLoad0)); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 912 | if (PossibleLoad1 && PossibleLoad1->getNumUses() == 1) |
Johannes Doerfert | 9890a05 | 2014-07-01 00:32:29 +0000 | [diff] [blame] | 913 | if (PossibleLoad1->getParent() == Store->getParent()) |
| 914 | Loads.push_back(lookupAccessFor(PossibleLoad1)); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | /// @brief Check for reductions in this ScopStmt |
| 918 | /// |
| 919 | /// Iterate over all store memory accesses and check for valid binary reduction |
| 920 | /// like chains. For all candidates we check if they have the same base address |
| 921 | /// and there are no other accesses which overlap with them. The base address |
| 922 | /// check rules out impossible reductions candidates early. The overlap check, |
| 923 | /// together with the "only one user" check in collectCandiateReductionLoads, |
| 924 | /// guarantees that none of the intermediate results will escape during |
| 925 | /// execution of the loop nest. We basically check here that no other memory |
| 926 | /// access can access the same memory as the potential reduction. |
| 927 | void ScopStmt::checkForReductions() { |
| 928 | SmallVector<MemoryAccess *, 2> Loads; |
| 929 | SmallVector<std::pair<MemoryAccess *, MemoryAccess *>, 4> Candidates; |
| 930 | |
| 931 | // First collect candidate load-store reduction chains by iterating over all |
| 932 | // stores and collecting possible reduction loads. |
| 933 | for (MemoryAccess *StoreMA : MemAccs) { |
| 934 | if (StoreMA->isRead()) |
| 935 | continue; |
| 936 | |
| 937 | Loads.clear(); |
| 938 | collectCandiateReductionLoads(StoreMA, Loads); |
| 939 | for (MemoryAccess *LoadMA : Loads) |
| 940 | Candidates.push_back(std::make_pair(LoadMA, StoreMA)); |
| 941 | } |
| 942 | |
| 943 | // Then check each possible candidate pair. |
| 944 | for (const auto &CandidatePair : Candidates) { |
| 945 | bool Valid = true; |
| 946 | isl_map *LoadAccs = CandidatePair.first->getAccessRelation(); |
| 947 | isl_map *StoreAccs = CandidatePair.second->getAccessRelation(); |
| 948 | |
| 949 | // Skip those with obviously unequal base addresses. |
| 950 | if (!isl_map_has_equal_space(LoadAccs, StoreAccs)) { |
| 951 | isl_map_free(LoadAccs); |
| 952 | isl_map_free(StoreAccs); |
| 953 | continue; |
| 954 | } |
| 955 | |
| 956 | // And check if the remaining for overlap with other memory accesses. |
| 957 | isl_map *AllAccsRel = isl_map_union(LoadAccs, StoreAccs); |
| 958 | AllAccsRel = isl_map_intersect_domain(AllAccsRel, getDomain()); |
| 959 | isl_set *AllAccs = isl_map_range(AllAccsRel); |
| 960 | |
| 961 | for (MemoryAccess *MA : MemAccs) { |
| 962 | if (MA == CandidatePair.first || MA == CandidatePair.second) |
| 963 | continue; |
| 964 | |
| 965 | isl_map *AccRel = |
| 966 | isl_map_intersect_domain(MA->getAccessRelation(), getDomain()); |
| 967 | isl_set *Accs = isl_map_range(AccRel); |
| 968 | |
| 969 | if (isl_set_has_equal_space(AllAccs, Accs) || isl_set_free(Accs)) { |
| 970 | isl_set *OverlapAccs = isl_set_intersect(Accs, isl_set_copy(AllAccs)); |
| 971 | Valid = Valid && isl_set_is_empty(OverlapAccs); |
| 972 | isl_set_free(OverlapAccs); |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | isl_set_free(AllAccs); |
| 977 | if (!Valid) |
| 978 | continue; |
| 979 | |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 980 | const LoadInst *Load = |
| 981 | dyn_cast<const LoadInst>(CandidatePair.first->getAccessInstruction()); |
| 982 | MemoryAccess::ReductionType RT = |
| 983 | getReductionType(dyn_cast<BinaryOperator>(Load->user_back()), Load); |
| 984 | |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 985 | // If no overlapping access was found we mark the load and store as |
| 986 | // reduction like. |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 987 | CandidatePair.first->markAsReductionLike(RT); |
| 988 | CandidatePair.second->markAsReductionLike(RT); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 989 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 990 | } |
| 991 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 992 | std::string ScopStmt::getDomainStr() const { return stringFromIslObj(Domain); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 993 | |
| 994 | std::string ScopStmt::getScatteringStr() const { |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 995 | return stringFromIslObj(Scattering); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 996 | } |
| 997 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 998 | unsigned ScopStmt::getNumParams() const { return Parent.getNumParams(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 999 | |
| 1000 | unsigned ScopStmt::getNumIterators() const { |
| 1001 | // The final read has one dimension with one element. |
| 1002 | if (!BB) |
| 1003 | return 1; |
| 1004 | |
Sebastian Pop | 860e021 | 2013-02-15 21:26:44 +0000 | [diff] [blame] | 1005 | return NestLoops.size(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | unsigned ScopStmt::getNumScattering() const { |
| 1009 | return isl_map_dim(Scattering, isl_dim_out); |
| 1010 | } |
| 1011 | |
| 1012 | const char *ScopStmt::getBaseName() const { return BaseName.c_str(); } |
| 1013 | |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 1014 | const PHINode * |
| 1015 | ScopStmt::getInductionVariableForDimension(unsigned Dimension) const { |
Sebastian Pop | f30d3b2 | 2013-02-15 21:26:48 +0000 | [diff] [blame] | 1016 | return IVS[Dimension]; |
Hongbin Zheng | 27f3afb | 2011-04-30 03:26:51 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | const Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const { |
Sebastian Pop | 860e021 | 2013-02-15 21:26:44 +0000 | [diff] [blame] | 1020 | return NestLoops[Dimension]; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 1023 | isl_ctx *ScopStmt::getIslCtx() const { return Parent.getIslCtx(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1024 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 1025 | isl_set *ScopStmt::getDomain() const { return isl_set_copy(Domain); } |
Tobias Grosser | d5a7bfc | 2011-05-06 19:52:19 +0000 | [diff] [blame] | 1026 | |
Tobias Grosser | 78d8a3d | 2012-01-17 20:34:23 +0000 | [diff] [blame] | 1027 | isl_space *ScopStmt::getDomainSpace() const { |
| 1028 | return isl_set_get_space(Domain); |
| 1029 | } |
| 1030 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 1031 | isl_id *ScopStmt::getDomainId() const { return isl_set_get_tuple_id(Domain); } |
Tobias Grosser | cd95b77 | 2012-08-30 11:49:38 +0000 | [diff] [blame] | 1032 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1033 | ScopStmt::~ScopStmt() { |
| 1034 | while (!MemAccs.empty()) { |
| 1035 | delete MemAccs.back(); |
| 1036 | MemAccs.pop_back(); |
| 1037 | } |
| 1038 | |
| 1039 | isl_set_free(Domain); |
| 1040 | isl_map_free(Scattering); |
| 1041 | } |
| 1042 | |
| 1043 | void ScopStmt::print(raw_ostream &OS) const { |
| 1044 | OS << "\t" << getBaseName() << "\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1045 | OS.indent(12) << "Domain :=\n"; |
| 1046 | |
| 1047 | if (Domain) { |
| 1048 | OS.indent(16) << getDomainStr() << ";\n"; |
| 1049 | } else |
| 1050 | OS.indent(16) << "n/a\n"; |
| 1051 | |
| 1052 | OS.indent(12) << "Scattering :=\n"; |
| 1053 | |
| 1054 | if (Domain) { |
| 1055 | OS.indent(16) << getScatteringStr() << ";\n"; |
| 1056 | } else |
| 1057 | OS.indent(16) << "n/a\n"; |
| 1058 | |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1059 | for (MemoryAccess *Access : MemAccs) |
| 1060 | Access->print(OS); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | void ScopStmt::dump() const { print(dbgs()); } |
| 1064 | |
| 1065 | //===----------------------------------------------------------------------===// |
| 1066 | /// Scop class implement |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 1067 | |
Tobias Grosser | 7ffe4e8 | 2011-11-17 12:56:10 +0000 | [diff] [blame] | 1068 | void Scop::setContext(__isl_take isl_set *NewContext) { |
Tobias Grosser | ff9b54d | 2011-11-15 11:38:44 +0000 | [diff] [blame] | 1069 | NewContext = isl_set_align_params(NewContext, isl_set_get_space(Context)); |
| 1070 | isl_set_free(Context); |
| 1071 | Context = NewContext; |
| 1072 | } |
| 1073 | |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 1074 | void Scop::addParams(std::vector<const SCEV *> NewParameters) { |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1075 | for (const SCEV *Parameter : NewParameters) { |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 1076 | if (ParameterIds.find(Parameter) != ParameterIds.end()) |
| 1077 | continue; |
| 1078 | |
| 1079 | int dimension = Parameters.size(); |
| 1080 | |
| 1081 | Parameters.push_back(Parameter); |
| 1082 | ParameterIds[Parameter] = dimension; |
| 1083 | } |
| 1084 | } |
| 1085 | |
Tobias Grosser | 9a38ab8 | 2011-11-08 15:41:03 +0000 | [diff] [blame] | 1086 | __isl_give isl_id *Scop::getIdForParam(const SCEV *Parameter) const { |
| 1087 | ParamIdType::const_iterator IdIter = ParameterIds.find(Parameter); |
Tobias Grosser | 76c2e32 | 2011-11-07 12:58:59 +0000 | [diff] [blame] | 1088 | |
Tobias Grosser | 9a38ab8 | 2011-11-08 15:41:03 +0000 | [diff] [blame] | 1089 | if (IdIter == ParameterIds.end()) |
Tobias Grosser | 5a56cbf | 2014-04-16 07:33:47 +0000 | [diff] [blame] | 1090 | return nullptr; |
Tobias Grosser | 76c2e32 | 2011-11-07 12:58:59 +0000 | [diff] [blame] | 1091 | |
Tobias Grosser | 8f99c16 | 2011-11-15 11:38:55 +0000 | [diff] [blame] | 1092 | std::string ParameterName; |
| 1093 | |
| 1094 | if (const SCEVUnknown *ValueParameter = dyn_cast<SCEVUnknown>(Parameter)) { |
| 1095 | Value *Val = ValueParameter->getValue(); |
Tobias Grosser | 29ee0b1 | 2011-11-17 14:52:36 +0000 | [diff] [blame] | 1096 | ParameterName = Val->getName(); |
Tobias Grosser | 8f99c16 | 2011-11-15 11:38:55 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | if (ParameterName == "" || ParameterName.substr(0, 2) == "p_") |
Hongbin Zheng | 86a3774 | 2012-04-25 08:01:38 +0000 | [diff] [blame] | 1100 | ParameterName = "p_" + utostr_32(IdIter->second); |
Tobias Grosser | 8f99c16 | 2011-11-15 11:38:55 +0000 | [diff] [blame] | 1101 | |
Tobias Grosser | 20532b8 | 2014-04-11 17:56:49 +0000 | [diff] [blame] | 1102 | return isl_id_alloc(getIslCtx(), ParameterName.c_str(), |
| 1103 | const_cast<void *>((const void *)Parameter)); |
Tobias Grosser | 76c2e32 | 2011-11-07 12:58:59 +0000 | [diff] [blame] | 1104 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1105 | |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 1106 | void Scop::buildContext() { |
| 1107 | isl_space *Space = isl_space_params_alloc(IslCtx, 0); |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 1108 | Context = isl_set_universe(isl_space_copy(Space)); |
| 1109 | AssumedContext = isl_set_universe(Space); |
Tobias Grosser | 0e27e24 | 2011-10-06 00:03:48 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 1112 | void Scop::addParameterBounds() { |
| 1113 | for (unsigned i = 0; i < isl_set_dim(Context, isl_dim_param); ++i) { |
Tobias Grosser | edab135 | 2013-06-21 06:41:31 +0000 | [diff] [blame] | 1114 | isl_val *V; |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 1115 | isl_id *Id; |
| 1116 | const SCEV *Scev; |
| 1117 | const IntegerType *T; |
| 1118 | |
| 1119 | Id = isl_set_get_dim_id(Context, isl_dim_param, i); |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 1120 | Scev = (const SCEV *)isl_id_get_user(Id); |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 1121 | T = dyn_cast<IntegerType>(Scev->getType()); |
| 1122 | isl_id_free(Id); |
| 1123 | |
| 1124 | assert(T && "Not an integer type"); |
| 1125 | int Width = T->getBitWidth(); |
| 1126 | |
Tobias Grosser | edab135 | 2013-06-21 06:41:31 +0000 | [diff] [blame] | 1127 | V = isl_val_int_from_si(IslCtx, Width - 1); |
| 1128 | V = isl_val_2exp(V); |
| 1129 | V = isl_val_neg(V); |
| 1130 | Context = isl_set_lower_bound_val(Context, isl_dim_param, i, V); |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 1131 | |
Tobias Grosser | edab135 | 2013-06-21 06:41:31 +0000 | [diff] [blame] | 1132 | V = isl_val_int_from_si(IslCtx, Width - 1); |
| 1133 | V = isl_val_2exp(V); |
| 1134 | V = isl_val_sub_ui(V, 1); |
| 1135 | Context = isl_set_upper_bound_val(Context, isl_dim_param, i, V); |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 1136 | } |
| 1137 | } |
| 1138 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1139 | void Scop::realignParams() { |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 1140 | // Add all parameters into a common model. |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 1141 | isl_space *Space = isl_space_params_alloc(IslCtx, ParameterIds.size()); |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 1142 | |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1143 | for (const auto &ParamID : ParameterIds) { |
| 1144 | const SCEV *Parameter = ParamID.first; |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 1145 | isl_id *id = getIdForParam(Parameter); |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1146 | Space = isl_space_set_dim_id(Space, isl_dim_param, ParamID.second, id); |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | // Align the parameters of all data structures to the model. |
| 1150 | Context = isl_set_align_params(Context, Space); |
| 1151 | |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1152 | for (ScopStmt *Stmt : *this) |
| 1153 | Stmt->realignParams(); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1156 | void Scop::simplifyAssumedContext() { |
| 1157 | // The parameter constraints of the iteration domains give us a set of |
| 1158 | // constraints that need to hold for all cases where at least a single |
| 1159 | // statement iteration is executed in the whole scop. We now simplify the |
| 1160 | // assumed context under the assumption that such constraints hold and at |
| 1161 | // least a single statement iteration is executed. For cases where no |
| 1162 | // statement instances are executed, the assumptions we have taken about |
| 1163 | // the executed code do not matter and can be changed. |
| 1164 | // |
| 1165 | // WARNING: This only holds if the assumptions we have taken do not reduce |
| 1166 | // the set of statement instances that are executed. Otherwise we |
| 1167 | // may run into a case where the iteration domains suggest that |
| 1168 | // for a certain set of parameter constraints no code is executed, |
| 1169 | // but in the original program some computation would have been |
| 1170 | // performed. In such a case, modifying the run-time conditions and |
| 1171 | // possibly influencing the run-time check may cause certain scops |
| 1172 | // to not be executed. |
| 1173 | // |
| 1174 | // Example: |
| 1175 | // |
| 1176 | // When delinearizing the following code: |
| 1177 | // |
| 1178 | // for (long i = 0; i < 100; i++) |
| 1179 | // for (long j = 0; j < m; j++) |
| 1180 | // A[i+p][j] = 1.0; |
| 1181 | // |
| 1182 | // we assume that the condition m <= 0 or (m >= 1 and p >= 0) holds as |
| 1183 | // otherwise we would access out of bound data. Now, knowing that code is |
| 1184 | // only executed for the case m >= 0, it is sufficient to assume p >= 0. |
| 1185 | AssumedContext = |
| 1186 | isl_set_gist_params(AssumedContext, isl_union_set_params(getDomains())); |
| 1187 | } |
| 1188 | |
Tobias Grosser | 0e27e24 | 2011-10-06 00:03:48 +0000 | [diff] [blame] | 1189 | Scop::Scop(TempScop &tempScop, LoopInfo &LI, ScalarEvolution &ScalarEvolution, |
| 1190 | isl_ctx *Context) |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 1191 | : SE(&ScalarEvolution), R(tempScop.getMaxRegion()), |
| 1192 | MaxLoopDepth(tempScop.getMaxLoopDepth()) { |
Tobias Grosser | 9a38ab8 | 2011-11-08 15:41:03 +0000 | [diff] [blame] | 1193 | IslCtx = Context; |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 1194 | buildContext(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1195 | |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 1196 | SmallVector<Loop *, 8> NestLoops; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1197 | SmallVector<unsigned, 8> Scatter; |
| 1198 | |
| 1199 | Scatter.assign(MaxLoopDepth + 1, 0); |
| 1200 | |
| 1201 | // Build the iteration domain, access functions and scattering functions |
| 1202 | // traversing the region tree. |
| 1203 | buildScop(tempScop, getRegion(), NestLoops, Scatter, LI); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1204 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1205 | realignParams(); |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 1206 | addParameterBounds(); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1207 | simplifyAssumedContext(); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1208 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1209 | assert(NestLoops.empty() && "NestLoops not empty at top level!"); |
| 1210 | } |
| 1211 | |
| 1212 | Scop::~Scop() { |
| 1213 | isl_set_free(Context); |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 1214 | isl_set_free(AssumedContext); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1215 | |
| 1216 | // Free the statements; |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1217 | for (ScopStmt *Stmt : *this) |
| 1218 | delete Stmt; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 1221 | std::string Scop::getContextStr() const { return stringFromIslObj(Context); } |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1222 | std::string Scop::getAssumedContextStr() const { |
| 1223 | return stringFromIslObj(AssumedContext); |
| 1224 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1225 | |
| 1226 | std::string Scop::getNameStr() const { |
| 1227 | std::string ExitName, EntryName; |
| 1228 | raw_string_ostream ExitStr(ExitName); |
| 1229 | raw_string_ostream EntryStr(EntryName); |
| 1230 | |
Tobias Grosser | f240b48 | 2014-01-09 10:42:15 +0000 | [diff] [blame] | 1231 | R.getEntry()->printAsOperand(EntryStr, false); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1232 | EntryStr.str(); |
| 1233 | |
| 1234 | if (R.getExit()) { |
Tobias Grosser | f240b48 | 2014-01-09 10:42:15 +0000 | [diff] [blame] | 1235 | R.getExit()->printAsOperand(ExitStr, false); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1236 | ExitStr.str(); |
| 1237 | } else |
| 1238 | ExitName = "FunctionExit"; |
| 1239 | |
| 1240 | return EntryName + "---" + ExitName; |
| 1241 | } |
| 1242 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 1243 | __isl_give isl_set *Scop::getContext() const { return isl_set_copy(Context); } |
Tobias Grosser | 3748705 | 2011-10-06 00:03:42 +0000 | [diff] [blame] | 1244 | __isl_give isl_space *Scop::getParamSpace() const { |
| 1245 | return isl_set_get_space(this->Context); |
| 1246 | } |
| 1247 | |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 1248 | __isl_give isl_set *Scop::getAssumedContext() const { |
| 1249 | return isl_set_copy(AssumedContext); |
| 1250 | } |
| 1251 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1252 | void Scop::addAssumption(__isl_take isl_set *Set) { |
| 1253 | AssumedContext = isl_set_intersect(AssumedContext, Set); |
| 1254 | } |
| 1255 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1256 | void Scop::printContext(raw_ostream &OS) const { |
| 1257 | OS << "Context:\n"; |
| 1258 | |
| 1259 | if (!Context) { |
| 1260 | OS.indent(4) << "n/a\n\n"; |
| 1261 | return; |
| 1262 | } |
| 1263 | |
| 1264 | OS.indent(4) << getContextStr() << "\n"; |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 1265 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1266 | OS.indent(4) << "Assumed Context:\n"; |
| 1267 | if (!AssumedContext) { |
| 1268 | OS.indent(4) << "n/a\n\n"; |
| 1269 | return; |
| 1270 | } |
| 1271 | |
| 1272 | OS.indent(4) << getAssumedContextStr() << "\n"; |
| 1273 | |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1274 | for (const SCEV *Parameter : Parameters) { |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 1275 | int Dim = ParameterIds.find(Parameter)->second; |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 1276 | OS.indent(4) << "p" << Dim << ": " << *Parameter << "\n"; |
| 1277 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | void Scop::printStatements(raw_ostream &OS) const { |
| 1281 | OS << "Statements {\n"; |
| 1282 | |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1283 | for (ScopStmt *Stmt : *this) |
| 1284 | OS.indent(4) << *Stmt; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1285 | |
| 1286 | OS.indent(4) << "}\n"; |
| 1287 | } |
| 1288 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1289 | void Scop::print(raw_ostream &OS) const { |
Tobias Grosser | 4eb7ddb | 2014-03-18 18:51:11 +0000 | [diff] [blame] | 1290 | OS.indent(4) << "Function: " << getRegion().getEntry()->getParent()->getName() |
| 1291 | << "\n"; |
Tobias Grosser | 483fdd4 | 2014-03-18 18:05:38 +0000 | [diff] [blame] | 1292 | OS.indent(4) << "Region: " << getNameStr() << "\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1293 | printContext(OS.indent(4)); |
| 1294 | printStatements(OS.indent(4)); |
| 1295 | } |
| 1296 | |
| 1297 | void Scop::dump() const { print(dbgs()); } |
| 1298 | |
Tobias Grosser | 9a38ab8 | 2011-11-08 15:41:03 +0000 | [diff] [blame] | 1299 | isl_ctx *Scop::getIslCtx() const { return IslCtx; } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1300 | |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 1301 | __isl_give isl_union_set *Scop::getDomains() { |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 1302 | isl_union_set *Domain = isl_union_set_empty(getParamSpace()); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 1303 | |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 1304 | for (ScopStmt *Stmt : *this) |
| 1305 | Domain = isl_union_set_add_set(Domain, Stmt->getDomain()); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 1306 | |
| 1307 | return Domain; |
| 1308 | } |
| 1309 | |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 1310 | __isl_give isl_union_map *Scop::getMustWrites() { |
| 1311 | isl_union_map *Write = isl_union_map_empty(this->getParamSpace()); |
| 1312 | |
| 1313 | for (ScopStmt *Stmt : *this) { |
| 1314 | for (MemoryAccess *MA : *Stmt) { |
| 1315 | if (!MA->isMustWrite()) |
| 1316 | continue; |
| 1317 | |
| 1318 | isl_set *Domain = Stmt->getDomain(); |
| 1319 | isl_map *AccessDomain = MA->getAccessRelation(); |
| 1320 | AccessDomain = isl_map_intersect_domain(AccessDomain, Domain); |
| 1321 | Write = isl_union_map_add_map(Write, AccessDomain); |
| 1322 | } |
| 1323 | } |
| 1324 | return isl_union_map_coalesce(Write); |
| 1325 | } |
| 1326 | |
| 1327 | __isl_give isl_union_map *Scop::getMayWrites() { |
| 1328 | isl_union_map *Write = isl_union_map_empty(this->getParamSpace()); |
| 1329 | |
| 1330 | for (ScopStmt *Stmt : *this) { |
| 1331 | for (MemoryAccess *MA : *Stmt) { |
| 1332 | if (!MA->isMayWrite()) |
| 1333 | continue; |
| 1334 | |
| 1335 | isl_set *Domain = Stmt->getDomain(); |
| 1336 | isl_map *AccessDomain = MA->getAccessRelation(); |
| 1337 | AccessDomain = isl_map_intersect_domain(AccessDomain, Domain); |
| 1338 | Write = isl_union_map_add_map(Write, AccessDomain); |
| 1339 | } |
| 1340 | } |
| 1341 | return isl_union_map_coalesce(Write); |
| 1342 | } |
| 1343 | |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1344 | __isl_give isl_union_map *Scop::getWrites() { |
| 1345 | isl_union_map *Write = isl_union_map_empty(this->getParamSpace()); |
| 1346 | |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 1347 | for (ScopStmt *Stmt : *this) { |
Johannes Doerfert | f675289 | 2014-06-13 18:01:45 +0000 | [diff] [blame] | 1348 | for (MemoryAccess *MA : *Stmt) { |
| 1349 | if (!MA->isWrite()) |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1350 | continue; |
| 1351 | |
| 1352 | isl_set *Domain = Stmt->getDomain(); |
Johannes Doerfert | f675289 | 2014-06-13 18:01:45 +0000 | [diff] [blame] | 1353 | isl_map *AccessDomain = MA->getAccessRelation(); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1354 | AccessDomain = isl_map_intersect_domain(AccessDomain, Domain); |
| 1355 | Write = isl_union_map_add_map(Write, AccessDomain); |
| 1356 | } |
| 1357 | } |
| 1358 | return isl_union_map_coalesce(Write); |
| 1359 | } |
| 1360 | |
| 1361 | __isl_give isl_union_map *Scop::getReads() { |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 1362 | isl_union_map *Read = isl_union_map_empty(getParamSpace()); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1363 | |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 1364 | for (ScopStmt *Stmt : *this) { |
Johannes Doerfert | f675289 | 2014-06-13 18:01:45 +0000 | [diff] [blame] | 1365 | for (MemoryAccess *MA : *Stmt) { |
| 1366 | if (!MA->isRead()) |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1367 | continue; |
| 1368 | |
| 1369 | isl_set *Domain = Stmt->getDomain(); |
Johannes Doerfert | f675289 | 2014-06-13 18:01:45 +0000 | [diff] [blame] | 1370 | isl_map *AccessDomain = MA->getAccessRelation(); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1371 | |
| 1372 | AccessDomain = isl_map_intersect_domain(AccessDomain, Domain); |
| 1373 | Read = isl_union_map_add_map(Read, AccessDomain); |
| 1374 | } |
| 1375 | } |
| 1376 | return isl_union_map_coalesce(Read); |
| 1377 | } |
| 1378 | |
| 1379 | __isl_give isl_union_map *Scop::getSchedule() { |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 1380 | isl_union_map *Schedule = isl_union_map_empty(getParamSpace()); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1381 | |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 1382 | for (ScopStmt *Stmt : *this) |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1383 | Schedule = isl_union_map_add_map(Schedule, Stmt->getScattering()); |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 1384 | |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1385 | return isl_union_map_coalesce(Schedule); |
| 1386 | } |
| 1387 | |
| 1388 | bool Scop::restrictDomains(__isl_take isl_union_set *Domain) { |
| 1389 | bool Changed = false; |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 1390 | for (ScopStmt *Stmt : *this) { |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1391 | isl_union_set *StmtDomain = isl_union_set_from_set(Stmt->getDomain()); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1392 | isl_union_set *NewStmtDomain = isl_union_set_intersect( |
| 1393 | isl_union_set_copy(StmtDomain), isl_union_set_copy(Domain)); |
| 1394 | |
| 1395 | if (isl_union_set_is_subset(StmtDomain, NewStmtDomain)) { |
| 1396 | isl_union_set_free(StmtDomain); |
| 1397 | isl_union_set_free(NewStmtDomain); |
| 1398 | continue; |
| 1399 | } |
| 1400 | |
| 1401 | Changed = true; |
| 1402 | |
| 1403 | isl_union_set_free(StmtDomain); |
| 1404 | NewStmtDomain = isl_union_set_coalesce(NewStmtDomain); |
| 1405 | |
| 1406 | if (isl_union_set_is_empty(NewStmtDomain)) { |
| 1407 | Stmt->restrictDomain(isl_set_empty(Stmt->getDomainSpace())); |
| 1408 | isl_union_set_free(NewStmtDomain); |
| 1409 | } else |
| 1410 | Stmt->restrictDomain(isl_set_from_union_set(NewStmtDomain)); |
| 1411 | } |
| 1412 | isl_union_set_free(Domain); |
| 1413 | return Changed; |
| 1414 | } |
| 1415 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1416 | ScalarEvolution *Scop::getSE() const { return SE; } |
| 1417 | |
| 1418 | bool Scop::isTrivialBB(BasicBlock *BB, TempScop &tempScop) { |
| 1419 | if (tempScop.getAccessFunctions(BB)) |
| 1420 | return false; |
| 1421 | |
| 1422 | return true; |
| 1423 | } |
| 1424 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 1425 | void Scop::buildScop(TempScop &tempScop, const Region &CurRegion, |
| 1426 | SmallVectorImpl<Loop *> &NestLoops, |
| 1427 | SmallVectorImpl<unsigned> &Scatter, LoopInfo &LI) { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1428 | Loop *L = castToLoop(CurRegion, LI); |
| 1429 | |
| 1430 | if (L) |
| 1431 | NestLoops.push_back(L); |
| 1432 | |
| 1433 | unsigned loopDepth = NestLoops.size(); |
| 1434 | assert(Scatter.size() > loopDepth && "Scatter not big enough!"); |
| 1435 | |
| 1436 | for (Region::const_element_iterator I = CurRegion.element_begin(), |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 1437 | E = CurRegion.element_end(); |
| 1438 | I != E; ++I) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1439 | if (I->isSubRegion()) |
| 1440 | buildScop(tempScop, *(I->getNodeAs<Region>()), NestLoops, Scatter, LI); |
| 1441 | else { |
| 1442 | BasicBlock *BB = I->getNodeAs<BasicBlock>(); |
| 1443 | |
| 1444 | if (isTrivialBB(BB, tempScop)) |
| 1445 | continue; |
| 1446 | |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 1447 | Stmts.push_back( |
| 1448 | new ScopStmt(*this, tempScop, CurRegion, *BB, NestLoops, Scatter)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1449 | |
| 1450 | // Increasing the Scattering function is OK for the moment, because |
| 1451 | // we are using a depth first iterator and the program is well structured. |
| 1452 | ++Scatter[loopDepth]; |
| 1453 | } |
| 1454 | |
| 1455 | if (!L) |
| 1456 | return; |
| 1457 | |
| 1458 | // Exiting a loop region. |
| 1459 | Scatter[loopDepth] = 0; |
| 1460 | NestLoops.pop_back(); |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 1461 | ++Scatter[loopDepth - 1]; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | //===----------------------------------------------------------------------===// |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 1465 | ScopInfo::ScopInfo() : RegionPass(ID), scop(0) { |
| 1466 | ctx = isl_ctx_alloc(); |
Tobias Grosser | 4a8e356 | 2011-12-07 07:42:51 +0000 | [diff] [blame] | 1467 | isl_options_set_on_error(ctx, ISL_ON_ERROR_ABORT); |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | ScopInfo::~ScopInfo() { |
| 1471 | clear(); |
| 1472 | isl_ctx_free(ctx); |
| 1473 | } |
| 1474 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1475 | void ScopInfo::getAnalysisUsage(AnalysisUsage &AU) const { |
| 1476 | AU.addRequired<LoopInfo>(); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 1477 | AU.addRequired<RegionInfoPass>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1478 | AU.addRequired<ScalarEvolution>(); |
| 1479 | AU.addRequired<TempScopInfo>(); |
| 1480 | AU.setPreservesAll(); |
| 1481 | } |
| 1482 | |
| 1483 | bool ScopInfo::runOnRegion(Region *R, RGPassManager &RGM) { |
| 1484 | LoopInfo &LI = getAnalysis<LoopInfo>(); |
| 1485 | ScalarEvolution &SE = getAnalysis<ScalarEvolution>(); |
| 1486 | |
| 1487 | TempScop *tempScop = getAnalysis<TempScopInfo>().getTempScop(R); |
| 1488 | |
| 1489 | // This region is no Scop. |
| 1490 | if (!tempScop) { |
| 1491 | scop = 0; |
| 1492 | return false; |
| 1493 | } |
| 1494 | |
| 1495 | // Statistics. |
| 1496 | ++ScopFound; |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 1497 | if (tempScop->getMaxLoopDepth() > 0) |
| 1498 | ++RichScopFound; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1499 | |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 1500 | scop = new Scop(*tempScop, LI, SE, ctx); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1501 | |
| 1502 | return false; |
| 1503 | } |
| 1504 | |
| 1505 | char ScopInfo::ID = 0; |
| 1506 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 1507 | Pass *polly::createScopInfoPass() { return new ScopInfo(); } |
| 1508 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 1509 | INITIALIZE_PASS_BEGIN(ScopInfo, "polly-scops", |
| 1510 | "Polly - Create polyhedral description of Scops", false, |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 1511 | false); |
| 1512 | INITIALIZE_PASS_DEPENDENCY(LoopInfo); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 1513 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 1514 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution); |
| 1515 | INITIALIZE_PASS_DEPENDENCY(TempScopInfo); |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 1516 | INITIALIZE_PASS_END(ScopInfo, "polly-scops", |
| 1517 | "Polly - Create polyhedral description of Scops", false, |
| 1518 | false) |