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