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 | // |
Tobias Grosser | a5605d3 | 2014-10-29 19:58:28 +0000 | [diff] [blame] | 15 | // This representation is shared among several tools in the polyhedral |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 16 | // community, which are e.g. Cloog, Pluto, Loopo, Graphite. |
| 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 20 | #include "polly/LinkAllPasses.h" |
Sebastian Pop | 27c10c6 | 2013-03-22 22:07:43 +0000 | [diff] [blame] | 21 | #include "polly/ScopInfo.h" |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 22 | #include "polly/Options.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 23 | #include "polly/Support/GICHelper.h" |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 24 | #include "polly/Support/SCEVValidator.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 25 | #include "polly/Support/ScopHelper.h" |
Sebastian Pop | 27c10c6 | 2013-03-22 22:07:43 +0000 | [diff] [blame] | 26 | #include "polly/TempScopInfo.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SetVector.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/Statistic.h" |
Hongbin Zheng | 86a3774 | 2012-04-25 08:01:38 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/StringExtras.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/LoopInfo.h" |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 31 | #include "llvm/Analysis/AliasAnalysis.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 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 | 9e7b17b | 2014-08-18 00:40:13 +0000 | [diff] [blame] | 58 | // Multiplicative reductions can be disabled separately as these kind of |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 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 | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 66 | static cl::opt<unsigned> RunTimeChecksMaxParameters( |
| 67 | "polly-rtc-max-parameters", |
| 68 | cl::desc("The maximal number of parameters allowed in RTCs."), cl::Hidden, |
| 69 | cl::ZeroOrMore, cl::init(8), cl::cat(PollyCategory)); |
| 70 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 71 | /// Translate a 'const SCEV *' expression in an isl_pw_aff. |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 72 | struct SCEVAffinator : public SCEVVisitor<SCEVAffinator, isl_pw_aff *> { |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 73 | public: |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 74 | /// @brief Translate a 'const SCEV *' to an isl_pw_aff. |
| 75 | /// |
| 76 | /// @param Stmt The location at which the scalar evolution expression |
| 77 | /// is evaluated. |
| 78 | /// @param Expr The expression that is translated. |
| 79 | static __isl_give isl_pw_aff *getPwAff(ScopStmt *Stmt, const SCEV *Expr); |
| 80 | |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 81 | private: |
Tobias Grosser | 3cc9974 | 2012-06-06 16:33:15 +0000 | [diff] [blame] | 82 | isl_ctx *Ctx; |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 83 | int NbLoopSpaces; |
Tobias Grosser | 3cc9974 | 2012-06-06 16:33:15 +0000 | [diff] [blame] | 84 | const Scop *S; |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 85 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 86 | SCEVAffinator(const ScopStmt *Stmt); |
| 87 | int getLoopDepth(const Loop *L); |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 88 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 89 | __isl_give isl_pw_aff *visit(const SCEV *Expr); |
| 90 | __isl_give isl_pw_aff *visitConstant(const SCEVConstant *Expr); |
| 91 | __isl_give isl_pw_aff *visitTruncateExpr(const SCEVTruncateExpr *Expr); |
| 92 | __isl_give isl_pw_aff *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr); |
| 93 | __isl_give isl_pw_aff *visitSignExtendExpr(const SCEVSignExtendExpr *Expr); |
| 94 | __isl_give isl_pw_aff *visitAddExpr(const SCEVAddExpr *Expr); |
| 95 | __isl_give isl_pw_aff *visitMulExpr(const SCEVMulExpr *Expr); |
| 96 | __isl_give isl_pw_aff *visitUDivExpr(const SCEVUDivExpr *Expr); |
| 97 | __isl_give isl_pw_aff *visitAddRecExpr(const SCEVAddRecExpr *Expr); |
| 98 | __isl_give isl_pw_aff *visitSMaxExpr(const SCEVSMaxExpr *Expr); |
| 99 | __isl_give isl_pw_aff *visitUMaxExpr(const SCEVUMaxExpr *Expr); |
| 100 | __isl_give isl_pw_aff *visitUnknown(const SCEVUnknown *Expr); |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 101 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 102 | friend struct SCEVVisitor<SCEVAffinator, isl_pw_aff *>; |
| 103 | }; |
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 | SCEVAffinator::SCEVAffinator(const ScopStmt *Stmt) |
| 106 | : Ctx(Stmt->getIslCtx()), NbLoopSpaces(Stmt->getNumIterators()), |
| 107 | S(Stmt->getParent()) {} |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 108 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 109 | __isl_give isl_pw_aff *SCEVAffinator::getPwAff(ScopStmt *Stmt, |
| 110 | const SCEV *Scev) { |
| 111 | Scop *S = Stmt->getParent(); |
| 112 | const Region *Reg = &S->getRegion(); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 113 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 114 | S->addParams(getParamsInAffineExpr(Reg, Scev, *S->getSE())); |
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 | SCEVAffinator Affinator(Stmt); |
| 117 | return Affinator.visit(Scev); |
| 118 | } |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 119 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 120 | __isl_give isl_pw_aff *SCEVAffinator::visit(const SCEV *Expr) { |
| 121 | // In case the scev is a valid parameter, we do not further analyze this |
| 122 | // expression, but create a new parameter in the isl_pw_aff. This allows us |
| 123 | // to treat subexpressions that we cannot translate into an piecewise affine |
| 124 | // expression, as constant parameters of the piecewise affine expression. |
| 125 | if (isl_id *Id = S->getIdForParam(Expr)) { |
| 126 | isl_space *Space = isl_space_set_alloc(Ctx, 1, NbLoopSpaces); |
| 127 | Space = isl_space_set_dim_id(Space, isl_dim_param, 0, Id); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 128 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 129 | isl_set *Domain = isl_set_universe(isl_space_copy(Space)); |
| 130 | isl_aff *Affine = isl_aff_zero_on_domain(isl_local_space_from_space(Space)); |
| 131 | Affine = isl_aff_add_coefficient_si(Affine, isl_dim_param, 0, 1); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 132 | |
| 133 | return isl_pw_aff_alloc(Domain, Affine); |
| 134 | } |
| 135 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 136 | return SCEVVisitor<SCEVAffinator, isl_pw_aff *>::visit(Expr); |
| 137 | } |
| 138 | |
Tobias Grosser | 0d17013 | 2013-10-03 13:09:19 +0000 | [diff] [blame] | 139 | __isl_give isl_pw_aff *SCEVAffinator::visitConstant(const SCEVConstant *Expr) { |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 140 | ConstantInt *Value = Expr->getValue(); |
| 141 | isl_val *v; |
| 142 | |
| 143 | // LLVM does not define if an integer value is interpreted as a signed or |
| 144 | // unsigned value. Hence, without further information, it is unknown how |
| 145 | // this value needs to be converted to GMP. At the moment, we only support |
| 146 | // signed operations. So we just interpret it as signed. Later, there are |
| 147 | // two options: |
| 148 | // |
| 149 | // 1. We always interpret any value as signed and convert the values on |
| 150 | // demand. |
| 151 | // 2. We pass down the signedness of the calculation and use it to interpret |
| 152 | // this constant correctly. |
| 153 | v = isl_valFromAPInt(Ctx, Value->getValue(), /* isSigned */ true); |
| 154 | |
| 155 | isl_space *Space = isl_space_set_alloc(Ctx, 0, NbLoopSpaces); |
Johannes Doerfert | 9c14737 | 2014-11-19 15:36:59 +0000 | [diff] [blame] | 156 | isl_local_space *ls = isl_local_space_from_space(Space); |
| 157 | return isl_pw_aff_from_aff(isl_aff_val_on_domain(ls, v)); |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | __isl_give isl_pw_aff * |
| 161 | SCEVAffinator::visitTruncateExpr(const SCEVTruncateExpr *Expr) { |
| 162 | llvm_unreachable("SCEVTruncateExpr not yet supported"); |
| 163 | } |
| 164 | |
| 165 | __isl_give isl_pw_aff * |
| 166 | SCEVAffinator::visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) { |
| 167 | llvm_unreachable("SCEVZeroExtendExpr not yet supported"); |
| 168 | } |
| 169 | |
| 170 | __isl_give isl_pw_aff * |
| 171 | SCEVAffinator::visitSignExtendExpr(const SCEVSignExtendExpr *Expr) { |
| 172 | // Assuming the value is signed, a sign extension is basically a noop. |
| 173 | // TODO: Reconsider this as soon as we support unsigned values. |
| 174 | return visit(Expr->getOperand()); |
| 175 | } |
| 176 | |
| 177 | __isl_give isl_pw_aff *SCEVAffinator::visitAddExpr(const SCEVAddExpr *Expr) { |
| 178 | isl_pw_aff *Sum = visit(Expr->getOperand(0)); |
| 179 | |
| 180 | for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) { |
| 181 | isl_pw_aff *NextSummand = visit(Expr->getOperand(i)); |
| 182 | Sum = isl_pw_aff_add(Sum, NextSummand); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 185 | // TODO: Check for NSW and NUW. |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 186 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 187 | return Sum; |
| 188 | } |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 189 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 190 | __isl_give isl_pw_aff *SCEVAffinator::visitMulExpr(const SCEVMulExpr *Expr) { |
| 191 | isl_pw_aff *Product = visit(Expr->getOperand(0)); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 192 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 193 | for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) { |
| 194 | isl_pw_aff *NextOperand = visit(Expr->getOperand(i)); |
| 195 | |
| 196 | if (!isl_pw_aff_is_cst(Product) && !isl_pw_aff_is_cst(NextOperand)) { |
| 197 | isl_pw_aff_free(Product); |
| 198 | isl_pw_aff_free(NextOperand); |
Tobias Grosser | 5a56cbf | 2014-04-16 07:33:47 +0000 | [diff] [blame] | 199 | return nullptr; |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 202 | Product = isl_pw_aff_mul(Product, NextOperand); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 205 | // TODO: Check for NSW and NUW. |
| 206 | return Product; |
| 207 | } |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 208 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 209 | __isl_give isl_pw_aff *SCEVAffinator::visitUDivExpr(const SCEVUDivExpr *Expr) { |
| 210 | llvm_unreachable("SCEVUDivExpr not yet supported"); |
| 211 | } |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 212 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 213 | __isl_give isl_pw_aff * |
| 214 | SCEVAffinator::visitAddRecExpr(const SCEVAddRecExpr *Expr) { |
| 215 | assert(Expr->isAffine() && "Only affine AddRecurrences allowed"); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 216 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 217 | // Directly generate isl_pw_aff for Expr if 'start' is zero. |
| 218 | if (Expr->getStart()->isZero()) { |
| 219 | assert(S->getRegion().contains(Expr->getLoop()) && |
| 220 | "Scop does not contain the loop referenced in this AddRec"); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 221 | |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 222 | isl_pw_aff *Start = visit(Expr->getStart()); |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 223 | isl_pw_aff *Step = visit(Expr->getOperand(1)); |
| 224 | isl_space *Space = isl_space_set_alloc(Ctx, 0, NbLoopSpaces); |
| 225 | isl_local_space *LocalSpace = isl_local_space_from_space(Space); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 226 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 227 | int loopDimension = getLoopDepth(Expr->getLoop()); |
| 228 | |
| 229 | isl_aff *LAff = isl_aff_set_coefficient_si( |
| 230 | isl_aff_zero_on_domain(LocalSpace), isl_dim_in, loopDimension, 1); |
| 231 | isl_pw_aff *LPwAff = isl_pw_aff_from_aff(LAff); |
| 232 | |
| 233 | // TODO: Do we need to check for NSW and NUW? |
| 234 | return isl_pw_aff_add(Start, isl_pw_aff_mul(Step, LPwAff)); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 237 | // Translate AddRecExpr from '{start, +, inc}' into 'start + {0, +, inc}' |
| 238 | // if 'start' is not zero. |
| 239 | ScalarEvolution &SE = *S->getSE(); |
| 240 | const SCEV *ZeroStartExpr = SE.getAddRecExpr( |
| 241 | SE.getConstant(Expr->getStart()->getType(), 0), |
| 242 | Expr->getStepRecurrence(SE), Expr->getLoop(), SCEV::FlagAnyWrap); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 243 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 244 | isl_pw_aff *ZeroStartResult = visit(ZeroStartExpr); |
| 245 | isl_pw_aff *Start = visit(Expr->getStart()); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 246 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 247 | return isl_pw_aff_add(ZeroStartResult, Start); |
| 248 | } |
| 249 | |
| 250 | __isl_give isl_pw_aff *SCEVAffinator::visitSMaxExpr(const SCEVSMaxExpr *Expr) { |
| 251 | isl_pw_aff *Max = visit(Expr->getOperand(0)); |
| 252 | |
| 253 | for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) { |
| 254 | isl_pw_aff *NextOperand = visit(Expr->getOperand(i)); |
| 255 | Max = isl_pw_aff_max(Max, NextOperand); |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 258 | return Max; |
| 259 | } |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 260 | |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 261 | __isl_give isl_pw_aff *SCEVAffinator::visitUMaxExpr(const SCEVUMaxExpr *Expr) { |
| 262 | llvm_unreachable("SCEVUMaxExpr not yet supported"); |
| 263 | } |
| 264 | |
| 265 | __isl_give isl_pw_aff *SCEVAffinator::visitUnknown(const SCEVUnknown *Expr) { |
Tobias Grosser | f4daf34 | 2014-08-16 09:08:55 +0000 | [diff] [blame] | 266 | llvm_unreachable("Unknowns are always parameters"); |
Tobias Grosser | 0695ee4 | 2013-09-17 03:30:31 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | int SCEVAffinator::getLoopDepth(const Loop *L) { |
| 270 | Loop *outerLoop = S->getRegion().outermostLoopInRegion(const_cast<Loop *>(L)); |
| 271 | assert(outerLoop && "Scop does not contain this loop"); |
| 272 | return L->getLoopDepth() - outerLoop->getLoopDepth(); |
| 273 | } |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 274 | |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 275 | ScopArrayInfo::ScopArrayInfo(Value *BasePtr, Type *AccessType, isl_ctx *Ctx, |
| 276 | const SmallVector<const SCEV *, 4> &DimensionSizes) |
| 277 | : BasePtr(BasePtr), AccessType(AccessType), DimensionSizes(DimensionSizes) { |
| 278 | const std::string BasePtrName = getIslCompatibleName("MemRef_", BasePtr, ""); |
| 279 | Id = isl_id_alloc(Ctx, BasePtrName.c_str(), this); |
| 280 | } |
| 281 | |
| 282 | ScopArrayInfo::~ScopArrayInfo() { isl_id_free(Id); } |
| 283 | |
| 284 | isl_id *ScopArrayInfo::getBasePtrId() const { return isl_id_copy(Id); } |
| 285 | |
| 286 | void ScopArrayInfo::dump() const { print(errs()); } |
| 287 | |
| 288 | void ScopArrayInfo::print(raw_ostream &OS) const { |
| 289 | OS << "ScopArrayInfo:\n"; |
| 290 | OS << " Base: " << *getBasePtr() << "\n"; |
| 291 | OS << " Type: " << *getType() << "\n"; |
| 292 | OS << " Dimension Sizes:\n"; |
| 293 | for (unsigned u = 0; u < getNumberOfDimensions(); u++) |
| 294 | OS << " " << u << ") " << *DimensionSizes[u] << "\n"; |
| 295 | OS << "\n"; |
| 296 | } |
| 297 | |
| 298 | const ScopArrayInfo * |
| 299 | ScopArrayInfo::getFromAccessFunction(__isl_keep isl_pw_multi_aff *PMA) { |
| 300 | isl_id *Id = isl_pw_multi_aff_get_tuple_id(PMA, isl_dim_out); |
| 301 | assert(Id && "Output dimension didn't have an ID"); |
| 302 | return getFromId(Id); |
| 303 | } |
| 304 | |
| 305 | const ScopArrayInfo *ScopArrayInfo::getFromId(isl_id *Id) { |
| 306 | void *User = isl_id_get_user(Id); |
| 307 | const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); |
| 308 | isl_id_free(Id); |
| 309 | return SAI; |
| 310 | } |
| 311 | |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 312 | const std::string |
| 313 | MemoryAccess::getReductionOperatorStr(MemoryAccess::ReductionType RT) { |
| 314 | switch (RT) { |
| 315 | case MemoryAccess::RT_NONE: |
| 316 | llvm_unreachable("Requested a reduction operator string for a memory " |
| 317 | "access which isn't a reduction"); |
| 318 | case MemoryAccess::RT_ADD: |
| 319 | return "+"; |
| 320 | case MemoryAccess::RT_MUL: |
| 321 | return "*"; |
| 322 | case MemoryAccess::RT_BOR: |
| 323 | return "|"; |
| 324 | case MemoryAccess::RT_BXOR: |
| 325 | return "^"; |
| 326 | case MemoryAccess::RT_BAND: |
| 327 | return "&"; |
| 328 | } |
| 329 | llvm_unreachable("Unknown reduction type"); |
| 330 | return ""; |
| 331 | } |
| 332 | |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 333 | /// @brief Return the reduction type for a given binary operator |
| 334 | static MemoryAccess::ReductionType getReductionType(const BinaryOperator *BinOp, |
| 335 | const Instruction *Load) { |
| 336 | if (!BinOp) |
| 337 | return MemoryAccess::RT_NONE; |
| 338 | switch (BinOp->getOpcode()) { |
| 339 | case Instruction::FAdd: |
| 340 | if (!BinOp->hasUnsafeAlgebra()) |
| 341 | return MemoryAccess::RT_NONE; |
| 342 | // Fall through |
| 343 | case Instruction::Add: |
| 344 | return MemoryAccess::RT_ADD; |
| 345 | case Instruction::Or: |
| 346 | return MemoryAccess::RT_BOR; |
| 347 | case Instruction::Xor: |
| 348 | return MemoryAccess::RT_BXOR; |
| 349 | case Instruction::And: |
| 350 | return MemoryAccess::RT_BAND; |
| 351 | case Instruction::FMul: |
| 352 | if (!BinOp->hasUnsafeAlgebra()) |
| 353 | return MemoryAccess::RT_NONE; |
| 354 | // Fall through |
| 355 | case Instruction::Mul: |
| 356 | if (DisableMultiplicativeReductions) |
| 357 | return MemoryAccess::RT_NONE; |
| 358 | return MemoryAccess::RT_MUL; |
| 359 | default: |
| 360 | return MemoryAccess::RT_NONE; |
| 361 | } |
| 362 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 363 | //===----------------------------------------------------------------------===// |
| 364 | |
| 365 | MemoryAccess::~MemoryAccess() { |
Tobias Grosser | 54a86e6 | 2011-08-18 06:31:46 +0000 | [diff] [blame] | 366 | isl_map_free(AccessRelation); |
Raghesh Aloor | 129e867 | 2011-08-15 02:33:39 +0000 | [diff] [blame] | 367 | isl_map_free(newAccessRelation); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Johannes Doerfert | 8f7124c | 2014-09-12 11:00:49 +0000 | [diff] [blame] | 370 | static MemoryAccess::AccessType getMemoryAccessType(const IRAccess &Access) { |
| 371 | switch (Access.getType()) { |
| 372 | case IRAccess::READ: |
| 373 | return MemoryAccess::READ; |
| 374 | case IRAccess::MUST_WRITE: |
| 375 | return MemoryAccess::MUST_WRITE; |
| 376 | case IRAccess::MAY_WRITE: |
| 377 | return MemoryAccess::MAY_WRITE; |
| 378 | } |
| 379 | llvm_unreachable("Unknown IRAccess type!"); |
| 380 | } |
| 381 | |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 382 | const ScopArrayInfo *MemoryAccess::getScopArrayInfo() const { |
| 383 | isl_id *ArrayId = getArrayId(); |
| 384 | void *User = isl_id_get_user(ArrayId); |
| 385 | const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); |
| 386 | isl_id_free(ArrayId); |
| 387 | return SAI; |
| 388 | } |
| 389 | |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 390 | isl_id *MemoryAccess::getArrayId() const { |
| 391 | return isl_map_get_tuple_id(AccessRelation, isl_dim_out); |
| 392 | } |
| 393 | |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 394 | isl_pw_multi_aff * |
| 395 | MemoryAccess::applyScheduleToAccessRelation(isl_union_map *USchedule) const { |
| 396 | isl_map *Schedule, *ScheduledAccRel; |
| 397 | isl_union_set *UDomain; |
| 398 | |
| 399 | UDomain = isl_union_set_from_set(getStatement()->getDomain()); |
| 400 | USchedule = isl_union_map_intersect_domain(USchedule, UDomain); |
| 401 | Schedule = isl_map_from_union_map(USchedule); |
| 402 | ScheduledAccRel = isl_map_apply_domain(getAccessRelation(), Schedule); |
| 403 | return isl_pw_multi_aff_from_map(ScheduledAccRel); |
| 404 | } |
| 405 | |
| 406 | isl_map *MemoryAccess::getOriginalAccessRelation() const { |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 407 | return isl_map_copy(AccessRelation); |
| 408 | } |
| 409 | |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 410 | std::string MemoryAccess::getOriginalAccessRelationStr() const { |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 411 | return stringFromIslObj(AccessRelation); |
| 412 | } |
| 413 | |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 414 | __isl_give isl_space *MemoryAccess::getOriginalAccessRelationSpace() const { |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 415 | return isl_map_get_space(AccessRelation); |
| 416 | } |
| 417 | |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 418 | isl_map *MemoryAccess::getNewAccessRelation() const { |
| 419 | return isl_map_copy(newAccessRelation); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | isl_basic_map *MemoryAccess::createBasicAccessMap(ScopStmt *Statement) { |
Tobias Grosser | 084d8f7 | 2012-05-29 09:29:44 +0000 | [diff] [blame] | 423 | isl_space *Space = isl_space_set_alloc(Statement->getIslCtx(), 0, 1); |
Tobias Grosser | ed29566 | 2012-09-11 13:50:21 +0000 | [diff] [blame] | 424 | Space = isl_space_align_params(Space, Statement->getDomainSpace()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 425 | |
Tobias Grosser | 084d8f7 | 2012-05-29 09:29:44 +0000 | [diff] [blame] | 426 | return isl_basic_map_from_domain_and_range( |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 427 | isl_basic_set_universe(Statement->getDomainSpace()), |
| 428 | isl_basic_set_universe(Space)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 431 | // Formalize no out-of-bound access assumption |
| 432 | // |
| 433 | // When delinearizing array accesses we optimistically assume that the |
| 434 | // delinearized accesses do not access out of bound locations (the subscript |
| 435 | // expression of each array evaluates for each statement instance that is |
| 436 | // executed to a value that is larger than zero and strictly smaller than the |
| 437 | // size of the corresponding dimension). The only exception is the outermost |
Tobias Grosser | f57d63f | 2014-08-03 21:07:30 +0000 | [diff] [blame] | 438 | // dimension for which we do not need to assume any upper bound. At this point |
| 439 | // we formalize this assumption to ensure that at code generation time the |
| 440 | // relevant run-time checks can be generated. |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 441 | // |
| 442 | // To find the set of constraints necessary to avoid out of bound accesses, we |
| 443 | // first build the set of data locations that are not within array bounds. We |
| 444 | // then apply the reverse access relation to obtain the set of iterations that |
| 445 | // may contain invalid accesses and reduce this set of iterations to the ones |
| 446 | // that are actually executed by intersecting them with the domain of the |
| 447 | // statement. If we now project out all loop dimensions, we obtain a set of |
| 448 | // parameters that may cause statement instances to be executed that may |
| 449 | // possibly yield out of bound memory accesses. The complement of these |
| 450 | // constraints is the set of constraints that needs to be assumed to ensure such |
| 451 | // statement instances are never executed. |
| 452 | void MemoryAccess::assumeNoOutOfBound(const IRAccess &Access) { |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 453 | isl_space *Space = isl_space_range(getOriginalAccessRelationSpace()); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 454 | isl_set *Outside = isl_set_empty(isl_space_copy(Space)); |
Tobias Grosser | f57d63f | 2014-08-03 21:07:30 +0000 | [diff] [blame] | 455 | for (int i = 1, Size = Access.Subscripts.size(); i < Size; ++i) { |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 456 | isl_local_space *LS = isl_local_space_from_space(isl_space_copy(Space)); |
| 457 | isl_pw_aff *Var = |
| 458 | isl_pw_aff_var_on_domain(isl_local_space_copy(LS), isl_dim_set, i); |
| 459 | isl_pw_aff *Zero = isl_pw_aff_zero_on_domain(LS); |
| 460 | |
| 461 | isl_set *DimOutside; |
| 462 | |
Tobias Grosser | f57d63f | 2014-08-03 21:07:30 +0000 | [diff] [blame] | 463 | DimOutside = isl_pw_aff_lt_set(isl_pw_aff_copy(Var), Zero); |
| 464 | isl_pw_aff *SizeE = SCEVAffinator::getPwAff(Statement, Access.Sizes[i - 1]); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 465 | |
Tobias Grosser | f57d63f | 2014-08-03 21:07:30 +0000 | [diff] [blame] | 466 | SizeE = isl_pw_aff_drop_dims(SizeE, isl_dim_in, 0, |
| 467 | Statement->getNumIterators()); |
| 468 | SizeE = isl_pw_aff_add_dims(SizeE, isl_dim_in, |
| 469 | isl_space_dim(Space, isl_dim_set)); |
| 470 | SizeE = isl_pw_aff_set_tuple_id(SizeE, isl_dim_in, |
| 471 | isl_space_get_tuple_id(Space, isl_dim_set)); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 472 | |
Tobias Grosser | f57d63f | 2014-08-03 21:07:30 +0000 | [diff] [blame] | 473 | DimOutside = isl_set_union(DimOutside, isl_pw_aff_le_set(SizeE, Var)); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 474 | |
| 475 | Outside = isl_set_union(Outside, DimOutside); |
| 476 | } |
| 477 | |
| 478 | Outside = isl_set_apply(Outside, isl_map_reverse(getAccessRelation())); |
| 479 | Outside = isl_set_intersect(Outside, Statement->getDomain()); |
| 480 | Outside = isl_set_params(Outside); |
| 481 | Outside = isl_set_complement(Outside); |
| 482 | Statement->getParent()->addAssumption(Outside); |
| 483 | isl_space_free(Space); |
| 484 | } |
| 485 | |
Johannes Doerfert | 13c8cf2 | 2014-08-10 08:09:38 +0000 | [diff] [blame] | 486 | MemoryAccess::MemoryAccess(const IRAccess &Access, Instruction *AccInst, |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 487 | ScopStmt *Statement, const ScopArrayInfo *SAI) |
Johannes Doerfert | 4c7ce47 | 2014-10-08 10:11:33 +0000 | [diff] [blame] | 488 | : AccType(getMemoryAccessType(Access)), Statement(Statement), Inst(AccInst), |
Johannes Doerfert | 8f7124c | 2014-09-12 11:00:49 +0000 | [diff] [blame] | 489 | newAccessRelation(nullptr) { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 490 | |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 491 | isl_ctx *Ctx = Statement->getIslCtx(); |
Tobias Grosser | 9759f85 | 2011-11-10 12:44:55 +0000 | [diff] [blame] | 492 | BaseAddr = Access.getBase(); |
Johannes Doerfert | 79fc23f | 2014-07-24 23:48:02 +0000 | [diff] [blame] | 493 | BaseName = getIslCompatibleName("MemRef_", getBaseAddr(), ""); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 494 | |
| 495 | isl_id *BaseAddrId = SAI->getBasePtrId(); |
Tobias Grosser | 5683df4 | 2011-11-09 22:34:34 +0000 | [diff] [blame] | 496 | |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 497 | if (!Access.isAffine()) { |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 498 | // We overapproximate non-affine accesses with a possible access to the |
| 499 | // whole array. For read accesses it does not make a difference, if an |
| 500 | // access must or may happen. However, for write accesses it is important to |
| 501 | // differentiate between writes that must happen and writes that may happen. |
Tobias Grosser | 04d6ae6 | 2013-06-23 06:04:54 +0000 | [diff] [blame] | 502 | AccessRelation = isl_map_from_basic_map(createBasicAccessMap(Statement)); |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 503 | AccessRelation = |
| 504 | isl_map_set_tuple_id(AccessRelation, isl_dim_out, BaseAddrId); |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 505 | return; |
| 506 | } |
| 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 { |
Johannes Doerfert | 4c7ce47 | 2014-10-08 10:11:33 +0000 | [diff] [blame] | 562 | switch (AccType) { |
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"; |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 574 | OS.indent(16) << getOriginalAccessRelationStr() << ";\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); |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 627 | isl_map *AccessRelation = getAccessRelation(); |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 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 | |
Johannes Doerfert | 75bd66e | 2014-10-31 23:16:02 +0000 | [diff] [blame] | 720 | void ScopStmt::buildAccesses(TempScop &tempScop) { |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 721 | for (const auto &AccessPair : *tempScop.getAccessFunctions(BB)) { |
| 722 | const IRAccess &Access = AccessPair.first; |
| 723 | Instruction *AccessInst = AccessPair.second; |
| 724 | |
Johannes Doerfert | 80ef110 | 2014-11-07 08:31:31 +0000 | [diff] [blame] | 725 | Type *AccessType = getAccessInstType(AccessInst)->getPointerTo(); |
| 726 | const ScopArrayInfo *SAI = getParent()->getOrCreateScopArrayInfo( |
| 727 | Access.getBase(), AccessType, Access.Sizes); |
| 728 | |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 729 | MemAccs.push_back(new MemoryAccess(Access, AccessInst, this, SAI)); |
Tobias Grosser | d6aafa7 | 2014-02-20 21:29:09 +0000 | [diff] [blame] | 730 | |
| 731 | // We do not track locations for scalar memory accesses at the moment. |
| 732 | // |
| 733 | // We do not have a use for this information at the moment. If we need this |
| 734 | // at some point, the "instruction -> access" mapping needs to be enhanced |
| 735 | // as a single instruction could then possibly perform multiple accesses. |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 736 | if (!Access.isScalar()) { |
| 737 | assert(!InstructionToAccess.count(AccessInst) && |
Tobias Grosser | 3fc9154 | 2014-02-20 21:43:45 +0000 | [diff] [blame] | 738 | "Unexpected 1-to-N mapping on instruction to access map!"); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 739 | InstructionToAccess[AccessInst] = MemAccs.back(); |
Tobias Grosser | d6aafa7 | 2014-02-20 21:29:09 +0000 | [diff] [blame] | 740 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 741 | } |
| 742 | } |
| 743 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 744 | void ScopStmt::realignParams() { |
Johannes Doerfert | f675289 | 2014-06-13 18:01:45 +0000 | [diff] [blame] | 745 | for (MemoryAccess *MA : *this) |
| 746 | MA->realignParams(); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 747 | |
| 748 | Domain = isl_set_align_params(Domain, Parent.getParamSpace()); |
| 749 | Scattering = isl_map_align_params(Scattering, Parent.getParamSpace()); |
| 750 | } |
| 751 | |
Tobias Grosser | 65b0058 | 2011-11-08 15:41:19 +0000 | [diff] [blame] | 752 | __isl_give isl_set *ScopStmt::buildConditionSet(const Comparison &Comp) { |
Tobias Grosser | a601fbd | 2011-11-09 22:34:44 +0000 | [diff] [blame] | 753 | isl_pw_aff *L = SCEVAffinator::getPwAff(this, Comp.getLHS()); |
| 754 | isl_pw_aff *R = SCEVAffinator::getPwAff(this, Comp.getRHS()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 755 | |
Tobias Grosser | d2795d0 | 2011-08-18 07:51:40 +0000 | [diff] [blame] | 756 | switch (Comp.getPred()) { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 757 | case ICmpInst::ICMP_EQ: |
Tobias Grosser | 048c879 | 2011-10-23 20:59:20 +0000 | [diff] [blame] | 758 | return isl_pw_aff_eq_set(L, R); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 759 | case ICmpInst::ICMP_NE: |
Tobias Grosser | 048c879 | 2011-10-23 20:59:20 +0000 | [diff] [blame] | 760 | return isl_pw_aff_ne_set(L, R); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 761 | case ICmpInst::ICMP_SLT: |
Tobias Grosser | 048c879 | 2011-10-23 20:59:20 +0000 | [diff] [blame] | 762 | return isl_pw_aff_lt_set(L, R); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 763 | case ICmpInst::ICMP_SLE: |
Tobias Grosser | 048c879 | 2011-10-23 20:59:20 +0000 | [diff] [blame] | 764 | return isl_pw_aff_le_set(L, R); |
Tobias Grosser | d2795d0 | 2011-08-18 07:51:40 +0000 | [diff] [blame] | 765 | case ICmpInst::ICMP_SGT: |
Tobias Grosser | 048c879 | 2011-10-23 20:59:20 +0000 | [diff] [blame] | 766 | return isl_pw_aff_gt_set(L, R); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 767 | case ICmpInst::ICMP_SGE: |
Tobias Grosser | 048c879 | 2011-10-23 20:59:20 +0000 | [diff] [blame] | 768 | return isl_pw_aff_ge_set(L, R); |
Tobias Grosser | d2795d0 | 2011-08-18 07:51:40 +0000 | [diff] [blame] | 769 | case ICmpInst::ICMP_ULT: |
| 770 | case ICmpInst::ICMP_UGT: |
| 771 | case ICmpInst::ICMP_ULE: |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 772 | case ICmpInst::ICMP_UGE: |
Tobias Grosser | d2795d0 | 2011-08-18 07:51:40 +0000 | [diff] [blame] | 773 | llvm_unreachable("Unsigned comparisons not yet supported"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 774 | default: |
| 775 | llvm_unreachable("Non integer predicate not supported"); |
| 776 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 779 | __isl_give isl_set *ScopStmt::addLoopBoundsToDomain(__isl_take isl_set *Domain, |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 780 | TempScop &tempScop) { |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 781 | isl_space *Space; |
| 782 | isl_local_space *LocalSpace; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 783 | |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 784 | Space = isl_set_get_space(Domain); |
| 785 | LocalSpace = isl_local_space_from_space(Space); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 786 | |
Johannes Doerfert | 5ad8a6a | 2014-11-01 01:14:56 +0000 | [diff] [blame] | 787 | ScalarEvolution *SE = getParent()->getSE(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 788 | for (int i = 0, e = getNumIterators(); i != e; ++i) { |
Tobias Grosser | 9b13d3d | 2011-10-06 22:32:58 +0000 | [diff] [blame] | 789 | 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] | 790 | isl_pw_aff *IV = |
| 791 | 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] | 792 | |
Tobias Grosser | 9b13d3d | 2011-10-06 22:32:58 +0000 | [diff] [blame] | 793 | // 0 <= IV. |
| 794 | isl_set *LowerBound = isl_pw_aff_nonneg_set(isl_pw_aff_copy(IV)); |
| 795 | Domain = isl_set_intersect(Domain, LowerBound); |
| 796 | |
| 797 | // IV <= LatchExecutions. |
Hongbin Zheng | 27f3afb | 2011-04-30 03:26:51 +0000 | [diff] [blame] | 798 | const Loop *L = getLoopForDimension(i); |
Johannes Doerfert | 5ad8a6a | 2014-11-01 01:14:56 +0000 | [diff] [blame] | 799 | const SCEV *LatchExecutions = SE->getBackedgeTakenCount(L); |
Tobias Grosser | 9b13d3d | 2011-10-06 22:32:58 +0000 | [diff] [blame] | 800 | isl_pw_aff *UpperBound = SCEVAffinator::getPwAff(this, LatchExecutions); |
| 801 | isl_set *UpperBoundSet = isl_pw_aff_le_set(IV, UpperBound); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 802 | Domain = isl_set_intersect(Domain, UpperBoundSet); |
| 803 | } |
| 804 | |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 805 | isl_local_space_free(LocalSpace); |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 806 | return Domain; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 809 | __isl_give isl_set *ScopStmt::addConditionsToDomain(__isl_take isl_set *Domain, |
| 810 | TempScop &tempScop, |
| 811 | const Region &CurRegion) { |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 812 | const Region *TopRegion = tempScop.getMaxRegion().getParent(), |
Tobias Grosser | d7e5864 | 2013-04-10 06:55:45 +0000 | [diff] [blame] | 813 | *CurrentRegion = &CurRegion; |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 814 | const BasicBlock *BranchingBB = BB; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 815 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 816 | do { |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 817 | if (BranchingBB != CurrentRegion->getEntry()) { |
| 818 | if (const BBCond *Condition = tempScop.getBBCond(BranchingBB)) |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 819 | for (const auto &C : *Condition) { |
| 820 | isl_set *ConditionSet = buildConditionSet(C); |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 821 | Domain = isl_set_intersect(Domain, ConditionSet); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 822 | } |
| 823 | } |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 824 | BranchingBB = CurrentRegion->getEntry(); |
| 825 | CurrentRegion = CurrentRegion->getParent(); |
| 826 | } while (TopRegion != CurrentRegion); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 827 | |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 828 | return Domain; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 831 | __isl_give isl_set *ScopStmt::buildDomain(TempScop &tempScop, |
| 832 | const Region &CurRegion) { |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 833 | isl_space *Space; |
| 834 | isl_set *Domain; |
Tobias Grosser | 084d8f7 | 2012-05-29 09:29:44 +0000 | [diff] [blame] | 835 | isl_id *Id; |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 836 | |
| 837 | Space = isl_space_set_alloc(getIslCtx(), 0, getNumIterators()); |
| 838 | |
Tobias Grosser | 084d8f7 | 2012-05-29 09:29:44 +0000 | [diff] [blame] | 839 | Id = isl_id_alloc(getIslCtx(), getBaseName(), this); |
| 840 | |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 841 | Domain = isl_set_universe(Space); |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 842 | Domain = addLoopBoundsToDomain(Domain, tempScop); |
| 843 | Domain = addConditionsToDomain(Domain, tempScop, CurRegion); |
Tobias Grosser | 084d8f7 | 2012-05-29 09:29:44 +0000 | [diff] [blame] | 844 | Domain = isl_set_set_tuple_id(Domain, Id); |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 845 | |
| 846 | return Domain; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Tobias Grosser | 7b50bee | 2014-11-25 10:51:12 +0000 | [diff] [blame] | 849 | void ScopStmt::deriveAssumptionsFromGEP(GetElementPtrInst *GEP) { |
| 850 | int Dimension = 0; |
| 851 | isl_ctx *Ctx = Parent.getIslCtx(); |
| 852 | isl_local_space *LSpace = isl_local_space_from_space(getDomainSpace()); |
| 853 | Type *Ty = GEP->getPointerOperandType(); |
| 854 | ScalarEvolution &SE = *Parent.getSE(); |
| 855 | |
| 856 | if (auto *PtrTy = dyn_cast<PointerType>(Ty)) { |
| 857 | Dimension = 1; |
| 858 | Ty = PtrTy->getElementType(); |
| 859 | } |
| 860 | |
| 861 | while (auto ArrayTy = dyn_cast<ArrayType>(Ty)) { |
| 862 | unsigned int Operand = 1 + Dimension; |
| 863 | |
| 864 | if (GEP->getNumOperands() <= Operand) |
| 865 | break; |
| 866 | |
| 867 | const SCEV *Expr = SE.getSCEV(GEP->getOperand(Operand)); |
| 868 | |
| 869 | if (isAffineExpr(&Parent.getRegion(), Expr, SE)) { |
| 870 | isl_pw_aff *AccessOffset = SCEVAffinator::getPwAff(this, Expr); |
| 871 | AccessOffset = |
| 872 | isl_pw_aff_set_tuple_id(AccessOffset, isl_dim_in, getDomainId()); |
| 873 | |
| 874 | isl_pw_aff *DimSize = isl_pw_aff_from_aff(isl_aff_val_on_domain( |
| 875 | isl_local_space_copy(LSpace), |
| 876 | isl_val_int_from_si(Ctx, ArrayTy->getNumElements()))); |
| 877 | |
| 878 | isl_set *OutOfBound = isl_pw_aff_ge_set(AccessOffset, DimSize); |
| 879 | OutOfBound = isl_set_intersect(getDomain(), OutOfBound); |
| 880 | OutOfBound = isl_set_params(OutOfBound); |
| 881 | isl_set *InBound = isl_set_complement(OutOfBound); |
| 882 | isl_set *Executed = isl_set_params(getDomain()); |
| 883 | |
| 884 | // A => B == !A or B |
| 885 | isl_set *InBoundIfExecuted = |
| 886 | isl_set_union(isl_set_complement(Executed), InBound); |
| 887 | |
| 888 | Parent.addAssumption(InBoundIfExecuted); |
| 889 | } |
| 890 | |
| 891 | Dimension += 1; |
| 892 | Ty = ArrayTy->getElementType(); |
| 893 | } |
| 894 | |
| 895 | isl_local_space_free(LSpace); |
| 896 | } |
| 897 | |
| 898 | void ScopStmt::deriveAssumptions() { |
| 899 | for (Instruction &Inst : *BB) |
| 900 | if (auto *GEP = dyn_cast<GetElementPtrInst>(&Inst)) |
| 901 | deriveAssumptionsFromGEP(GEP); |
| 902 | } |
| 903 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 904 | ScopStmt::ScopStmt(Scop &parent, TempScop &tempScop, const Region &CurRegion, |
Sebastian Pop | 860e021 | 2013-02-15 21:26:44 +0000 | [diff] [blame] | 905 | BasicBlock &bb, SmallVectorImpl<Loop *> &Nest, |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 906 | SmallVectorImpl<unsigned> &Scatter) |
Tobias Grosser | 683b8e4 | 2014-11-30 14:33:31 +0000 | [diff] [blame] | 907 | : Parent(parent), BB(&bb), NestLoops(Nest.size()) { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 908 | // Setup the induction variables. |
Tobias Grosser | 683b8e4 | 2014-11-30 14:33:31 +0000 | [diff] [blame] | 909 | for (unsigned i = 0, e = Nest.size(); i < e; ++i) |
Sebastian Pop | 860e021 | 2013-02-15 21:26:44 +0000 | [diff] [blame] | 910 | NestLoops[i] = Nest[i]; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 911 | |
Johannes Doerfert | 79fc23f | 2014-07-24 23:48:02 +0000 | [diff] [blame] | 912 | BaseName = getIslCompatibleName("Stmt_", &bb, ""); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 913 | |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 914 | Domain = buildDomain(tempScop, CurRegion); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 915 | buildScattering(Scatter); |
Johannes Doerfert | 75bd66e | 2014-10-31 23:16:02 +0000 | [diff] [blame] | 916 | buildAccesses(tempScop); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 917 | checkForReductions(); |
Tobias Grosser | 7b50bee | 2014-11-25 10:51:12 +0000 | [diff] [blame] | 918 | deriveAssumptions(); |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 921 | /// @brief Collect loads which might form a reduction chain with @p StoreMA |
| 922 | /// |
| 923 | /// Check if the stored value for @p StoreMA is a binary operator with one or |
| 924 | /// two loads as operands. If the binary operand is commutative & associative, |
| 925 | /// used only once (by @p StoreMA) and its load operands are also used only |
| 926 | /// once, we have found a possible reduction chain. It starts at an operand |
| 927 | /// load and includes the binary operator and @p StoreMA. |
| 928 | /// |
| 929 | /// Note: We allow only one use to ensure the load and binary operator cannot |
| 930 | /// escape this block or into any other store except @p StoreMA. |
| 931 | void ScopStmt::collectCandiateReductionLoads( |
| 932 | MemoryAccess *StoreMA, SmallVectorImpl<MemoryAccess *> &Loads) { |
| 933 | auto *Store = dyn_cast<StoreInst>(StoreMA->getAccessInstruction()); |
| 934 | if (!Store) |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 935 | return; |
| 936 | |
| 937 | // Skip if there is not one binary operator between the load and the store |
| 938 | auto *BinOp = dyn_cast<BinaryOperator>(Store->getValueOperand()); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 939 | if (!BinOp) |
| 940 | return; |
| 941 | |
| 942 | // Skip if the binary operators has multiple uses |
| 943 | if (BinOp->getNumUses() != 1) |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 944 | return; |
| 945 | |
| 946 | // Skip if the opcode of the binary operator is not commutative/associative |
| 947 | if (!BinOp->isCommutative() || !BinOp->isAssociative()) |
| 948 | return; |
| 949 | |
Johannes Doerfert | 9890a05 | 2014-07-01 00:32:29 +0000 | [diff] [blame] | 950 | // Skip if the binary operator is outside the current SCoP |
| 951 | if (BinOp->getParent() != Store->getParent()) |
| 952 | return; |
| 953 | |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 954 | // Skip if it is a multiplicative reduction and we disabled them |
| 955 | if (DisableMultiplicativeReductions && |
| 956 | (BinOp->getOpcode() == Instruction::Mul || |
| 957 | BinOp->getOpcode() == Instruction::FMul)) |
| 958 | return; |
| 959 | |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 960 | // Check the binary operator operands for a candidate load |
| 961 | auto *PossibleLoad0 = dyn_cast<LoadInst>(BinOp->getOperand(0)); |
| 962 | auto *PossibleLoad1 = dyn_cast<LoadInst>(BinOp->getOperand(1)); |
| 963 | if (!PossibleLoad0 && !PossibleLoad1) |
| 964 | return; |
| 965 | |
| 966 | // A load is only a candidate if it cannot escape (thus has only this use) |
| 967 | if (PossibleLoad0 && PossibleLoad0->getNumUses() == 1) |
Johannes Doerfert | 9890a05 | 2014-07-01 00:32:29 +0000 | [diff] [blame] | 968 | if (PossibleLoad0->getParent() == Store->getParent()) |
| 969 | Loads.push_back(lookupAccessFor(PossibleLoad0)); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 970 | if (PossibleLoad1 && PossibleLoad1->getNumUses() == 1) |
Johannes Doerfert | 9890a05 | 2014-07-01 00:32:29 +0000 | [diff] [blame] | 971 | if (PossibleLoad1->getParent() == Store->getParent()) |
| 972 | Loads.push_back(lookupAccessFor(PossibleLoad1)); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | /// @brief Check for reductions in this ScopStmt |
| 976 | /// |
| 977 | /// Iterate over all store memory accesses and check for valid binary reduction |
| 978 | /// like chains. For all candidates we check if they have the same base address |
| 979 | /// and there are no other accesses which overlap with them. The base address |
| 980 | /// check rules out impossible reductions candidates early. The overlap check, |
| 981 | /// together with the "only one user" check in collectCandiateReductionLoads, |
| 982 | /// guarantees that none of the intermediate results will escape during |
| 983 | /// execution of the loop nest. We basically check here that no other memory |
| 984 | /// access can access the same memory as the potential reduction. |
| 985 | void ScopStmt::checkForReductions() { |
| 986 | SmallVector<MemoryAccess *, 2> Loads; |
| 987 | SmallVector<std::pair<MemoryAccess *, MemoryAccess *>, 4> Candidates; |
| 988 | |
| 989 | // First collect candidate load-store reduction chains by iterating over all |
| 990 | // stores and collecting possible reduction loads. |
| 991 | for (MemoryAccess *StoreMA : MemAccs) { |
| 992 | if (StoreMA->isRead()) |
| 993 | continue; |
| 994 | |
| 995 | Loads.clear(); |
| 996 | collectCandiateReductionLoads(StoreMA, Loads); |
| 997 | for (MemoryAccess *LoadMA : Loads) |
| 998 | Candidates.push_back(std::make_pair(LoadMA, StoreMA)); |
| 999 | } |
| 1000 | |
| 1001 | // Then check each possible candidate pair. |
| 1002 | for (const auto &CandidatePair : Candidates) { |
| 1003 | bool Valid = true; |
| 1004 | isl_map *LoadAccs = CandidatePair.first->getAccessRelation(); |
| 1005 | isl_map *StoreAccs = CandidatePair.second->getAccessRelation(); |
| 1006 | |
| 1007 | // Skip those with obviously unequal base addresses. |
| 1008 | if (!isl_map_has_equal_space(LoadAccs, StoreAccs)) { |
| 1009 | isl_map_free(LoadAccs); |
| 1010 | isl_map_free(StoreAccs); |
| 1011 | continue; |
| 1012 | } |
| 1013 | |
| 1014 | // And check if the remaining for overlap with other memory accesses. |
| 1015 | isl_map *AllAccsRel = isl_map_union(LoadAccs, StoreAccs); |
| 1016 | AllAccsRel = isl_map_intersect_domain(AllAccsRel, getDomain()); |
| 1017 | isl_set *AllAccs = isl_map_range(AllAccsRel); |
| 1018 | |
| 1019 | for (MemoryAccess *MA : MemAccs) { |
| 1020 | if (MA == CandidatePair.first || MA == CandidatePair.second) |
| 1021 | continue; |
| 1022 | |
| 1023 | isl_map *AccRel = |
| 1024 | isl_map_intersect_domain(MA->getAccessRelation(), getDomain()); |
| 1025 | isl_set *Accs = isl_map_range(AccRel); |
| 1026 | |
| 1027 | if (isl_set_has_equal_space(AllAccs, Accs) || isl_set_free(Accs)) { |
| 1028 | isl_set *OverlapAccs = isl_set_intersect(Accs, isl_set_copy(AllAccs)); |
| 1029 | Valid = Valid && isl_set_is_empty(OverlapAccs); |
| 1030 | isl_set_free(OverlapAccs); |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | isl_set_free(AllAccs); |
| 1035 | if (!Valid) |
| 1036 | continue; |
| 1037 | |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1038 | const LoadInst *Load = |
| 1039 | dyn_cast<const LoadInst>(CandidatePair.first->getAccessInstruction()); |
| 1040 | MemoryAccess::ReductionType RT = |
| 1041 | getReductionType(dyn_cast<BinaryOperator>(Load->user_back()), Load); |
| 1042 | |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1043 | // If no overlapping access was found we mark the load and store as |
| 1044 | // reduction like. |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1045 | CandidatePair.first->markAsReductionLike(RT); |
| 1046 | CandidatePair.second->markAsReductionLike(RT); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1047 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 1050 | std::string ScopStmt::getDomainStr() const { return stringFromIslObj(Domain); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1051 | |
| 1052 | std::string ScopStmt::getScatteringStr() const { |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 1053 | return stringFromIslObj(Scattering); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 1056 | unsigned ScopStmt::getNumParams() const { return Parent.getNumParams(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1057 | |
| 1058 | unsigned ScopStmt::getNumIterators() const { |
| 1059 | // The final read has one dimension with one element. |
| 1060 | if (!BB) |
| 1061 | return 1; |
| 1062 | |
Sebastian Pop | 860e021 | 2013-02-15 21:26:44 +0000 | [diff] [blame] | 1063 | return NestLoops.size(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | unsigned ScopStmt::getNumScattering() const { |
| 1067 | return isl_map_dim(Scattering, isl_dim_out); |
| 1068 | } |
| 1069 | |
| 1070 | const char *ScopStmt::getBaseName() const { return BaseName.c_str(); } |
| 1071 | |
Hongbin Zheng | 27f3afb | 2011-04-30 03:26:51 +0000 | [diff] [blame] | 1072 | const Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const { |
Sebastian Pop | 860e021 | 2013-02-15 21:26:44 +0000 | [diff] [blame] | 1073 | return NestLoops[Dimension]; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 1076 | isl_ctx *ScopStmt::getIslCtx() const { return Parent.getIslCtx(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1077 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 1078 | isl_set *ScopStmt::getDomain() const { return isl_set_copy(Domain); } |
Tobias Grosser | d5a7bfc | 2011-05-06 19:52:19 +0000 | [diff] [blame] | 1079 | |
Tobias Grosser | 78d8a3d | 2012-01-17 20:34:23 +0000 | [diff] [blame] | 1080 | isl_space *ScopStmt::getDomainSpace() const { |
| 1081 | return isl_set_get_space(Domain); |
| 1082 | } |
| 1083 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 1084 | isl_id *ScopStmt::getDomainId() const { return isl_set_get_tuple_id(Domain); } |
Tobias Grosser | cd95b77 | 2012-08-30 11:49:38 +0000 | [diff] [blame] | 1085 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1086 | ScopStmt::~ScopStmt() { |
| 1087 | while (!MemAccs.empty()) { |
| 1088 | delete MemAccs.back(); |
| 1089 | MemAccs.pop_back(); |
| 1090 | } |
| 1091 | |
| 1092 | isl_set_free(Domain); |
| 1093 | isl_map_free(Scattering); |
| 1094 | } |
| 1095 | |
| 1096 | void ScopStmt::print(raw_ostream &OS) const { |
| 1097 | OS << "\t" << getBaseName() << "\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1098 | OS.indent(12) << "Domain :=\n"; |
| 1099 | |
| 1100 | if (Domain) { |
| 1101 | OS.indent(16) << getDomainStr() << ";\n"; |
| 1102 | } else |
| 1103 | OS.indent(16) << "n/a\n"; |
| 1104 | |
| 1105 | OS.indent(12) << "Scattering :=\n"; |
| 1106 | |
| 1107 | if (Domain) { |
| 1108 | OS.indent(16) << getScatteringStr() << ";\n"; |
| 1109 | } else |
| 1110 | OS.indent(16) << "n/a\n"; |
| 1111 | |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1112 | for (MemoryAccess *Access : MemAccs) |
| 1113 | Access->print(OS); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | void ScopStmt::dump() const { print(dbgs()); } |
| 1117 | |
| 1118 | //===----------------------------------------------------------------------===// |
| 1119 | /// Scop class implement |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 1120 | |
Tobias Grosser | 7ffe4e8 | 2011-11-17 12:56:10 +0000 | [diff] [blame] | 1121 | void Scop::setContext(__isl_take isl_set *NewContext) { |
Tobias Grosser | ff9b54d | 2011-11-15 11:38:44 +0000 | [diff] [blame] | 1122 | NewContext = isl_set_align_params(NewContext, isl_set_get_space(Context)); |
| 1123 | isl_set_free(Context); |
| 1124 | Context = NewContext; |
| 1125 | } |
| 1126 | |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 1127 | void Scop::addParams(std::vector<const SCEV *> NewParameters) { |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1128 | for (const SCEV *Parameter : NewParameters) { |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 1129 | if (ParameterIds.find(Parameter) != ParameterIds.end()) |
| 1130 | continue; |
| 1131 | |
| 1132 | int dimension = Parameters.size(); |
| 1133 | |
| 1134 | Parameters.push_back(Parameter); |
| 1135 | ParameterIds[Parameter] = dimension; |
| 1136 | } |
| 1137 | } |
| 1138 | |
Tobias Grosser | 9a38ab8 | 2011-11-08 15:41:03 +0000 | [diff] [blame] | 1139 | __isl_give isl_id *Scop::getIdForParam(const SCEV *Parameter) const { |
| 1140 | ParamIdType::const_iterator IdIter = ParameterIds.find(Parameter); |
Tobias Grosser | 76c2e32 | 2011-11-07 12:58:59 +0000 | [diff] [blame] | 1141 | |
Tobias Grosser | 9a38ab8 | 2011-11-08 15:41:03 +0000 | [diff] [blame] | 1142 | if (IdIter == ParameterIds.end()) |
Tobias Grosser | 5a56cbf | 2014-04-16 07:33:47 +0000 | [diff] [blame] | 1143 | return nullptr; |
Tobias Grosser | 76c2e32 | 2011-11-07 12:58:59 +0000 | [diff] [blame] | 1144 | |
Tobias Grosser | 8f99c16 | 2011-11-15 11:38:55 +0000 | [diff] [blame] | 1145 | std::string ParameterName; |
| 1146 | |
| 1147 | if (const SCEVUnknown *ValueParameter = dyn_cast<SCEVUnknown>(Parameter)) { |
| 1148 | Value *Val = ValueParameter->getValue(); |
Tobias Grosser | 29ee0b1 | 2011-11-17 14:52:36 +0000 | [diff] [blame] | 1149 | ParameterName = Val->getName(); |
Tobias Grosser | 8f99c16 | 2011-11-15 11:38:55 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | if (ParameterName == "" || ParameterName.substr(0, 2) == "p_") |
Hongbin Zheng | 86a3774 | 2012-04-25 08:01:38 +0000 | [diff] [blame] | 1153 | ParameterName = "p_" + utostr_32(IdIter->second); |
Tobias Grosser | 8f99c16 | 2011-11-15 11:38:55 +0000 | [diff] [blame] | 1154 | |
Tobias Grosser | 20532b8 | 2014-04-11 17:56:49 +0000 | [diff] [blame] | 1155 | return isl_id_alloc(getIslCtx(), ParameterName.c_str(), |
| 1156 | const_cast<void *>((const void *)Parameter)); |
Tobias Grosser | 76c2e32 | 2011-11-07 12:58:59 +0000 | [diff] [blame] | 1157 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1158 | |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 1159 | void Scop::buildContext() { |
| 1160 | isl_space *Space = isl_space_params_alloc(IslCtx, 0); |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 1161 | Context = isl_set_universe(isl_space_copy(Space)); |
| 1162 | AssumedContext = isl_set_universe(Space); |
Tobias Grosser | 0e27e24 | 2011-10-06 00:03:48 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 1165 | void Scop::addParameterBounds() { |
| 1166 | 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] | 1167 | isl_val *V; |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 1168 | isl_id *Id; |
| 1169 | const SCEV *Scev; |
| 1170 | const IntegerType *T; |
| 1171 | |
| 1172 | Id = isl_set_get_dim_id(Context, isl_dim_param, i); |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 1173 | Scev = (const SCEV *)isl_id_get_user(Id); |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 1174 | T = dyn_cast<IntegerType>(Scev->getType()); |
| 1175 | isl_id_free(Id); |
| 1176 | |
| 1177 | assert(T && "Not an integer type"); |
| 1178 | int Width = T->getBitWidth(); |
| 1179 | |
Tobias Grosser | edab135 | 2013-06-21 06:41:31 +0000 | [diff] [blame] | 1180 | V = isl_val_int_from_si(IslCtx, Width - 1); |
| 1181 | V = isl_val_2exp(V); |
| 1182 | V = isl_val_neg(V); |
| 1183 | Context = isl_set_lower_bound_val(Context, isl_dim_param, i, V); |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 1184 | |
Tobias Grosser | edab135 | 2013-06-21 06:41:31 +0000 | [diff] [blame] | 1185 | V = isl_val_int_from_si(IslCtx, Width - 1); |
| 1186 | V = isl_val_2exp(V); |
| 1187 | V = isl_val_sub_ui(V, 1); |
| 1188 | Context = isl_set_upper_bound_val(Context, isl_dim_param, i, V); |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 1189 | } |
| 1190 | } |
| 1191 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1192 | void Scop::realignParams() { |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 1193 | // Add all parameters into a common model. |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 1194 | isl_space *Space = isl_space_params_alloc(IslCtx, ParameterIds.size()); |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 1195 | |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1196 | for (const auto &ParamID : ParameterIds) { |
| 1197 | const SCEV *Parameter = ParamID.first; |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 1198 | isl_id *id = getIdForParam(Parameter); |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1199 | 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] | 1200 | } |
| 1201 | |
| 1202 | // Align the parameters of all data structures to the model. |
| 1203 | Context = isl_set_align_params(Context, Space); |
| 1204 | |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1205 | for (ScopStmt *Stmt : *this) |
| 1206 | Stmt->realignParams(); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1209 | void Scop::simplifyAssumedContext() { |
| 1210 | // The parameter constraints of the iteration domains give us a set of |
| 1211 | // constraints that need to hold for all cases where at least a single |
| 1212 | // statement iteration is executed in the whole scop. We now simplify the |
| 1213 | // assumed context under the assumption that such constraints hold and at |
| 1214 | // least a single statement iteration is executed. For cases where no |
| 1215 | // statement instances are executed, the assumptions we have taken about |
| 1216 | // the executed code do not matter and can be changed. |
| 1217 | // |
| 1218 | // WARNING: This only holds if the assumptions we have taken do not reduce |
| 1219 | // the set of statement instances that are executed. Otherwise we |
| 1220 | // may run into a case where the iteration domains suggest that |
| 1221 | // for a certain set of parameter constraints no code is executed, |
| 1222 | // but in the original program some computation would have been |
| 1223 | // performed. In such a case, modifying the run-time conditions and |
| 1224 | // possibly influencing the run-time check may cause certain scops |
| 1225 | // to not be executed. |
| 1226 | // |
| 1227 | // Example: |
| 1228 | // |
| 1229 | // When delinearizing the following code: |
| 1230 | // |
| 1231 | // for (long i = 0; i < 100; i++) |
| 1232 | // for (long j = 0; j < m; j++) |
| 1233 | // A[i+p][j] = 1.0; |
| 1234 | // |
| 1235 | // we assume that the condition m <= 0 or (m >= 1 and p >= 0) holds as |
| 1236 | // otherwise we would access out of bound data. Now, knowing that code is |
| 1237 | // only executed for the case m >= 0, it is sufficient to assume p >= 0. |
| 1238 | AssumedContext = |
| 1239 | isl_set_gist_params(AssumedContext, isl_union_set_params(getDomains())); |
| 1240 | } |
| 1241 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1242 | /// @brief Add the minimal/maximal access in @p Set to @p User. |
| 1243 | static int buildMinMaxAccess(__isl_take isl_set *Set, void *User) { |
| 1244 | Scop::MinMaxVectorTy *MinMaxAccesses = (Scop::MinMaxVectorTy *)User; |
| 1245 | isl_pw_multi_aff *MinPMA, *MaxPMA; |
| 1246 | isl_pw_aff *LastDimAff; |
| 1247 | isl_aff *OneAff; |
| 1248 | unsigned Pos; |
| 1249 | |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 1250 | // Restrict the number of parameters involved in the access as the lexmin/ |
| 1251 | // lexmax computation will take too long if this number is high. |
| 1252 | // |
| 1253 | // Experiments with a simple test case using an i7 4800MQ: |
| 1254 | // |
| 1255 | // #Parameters involved | Time (in sec) |
| 1256 | // 6 | 0.01 |
| 1257 | // 7 | 0.04 |
| 1258 | // 8 | 0.12 |
| 1259 | // 9 | 0.40 |
| 1260 | // 10 | 1.54 |
| 1261 | // 11 | 6.78 |
| 1262 | // 12 | 30.38 |
| 1263 | // |
| 1264 | if (isl_set_n_param(Set) > RunTimeChecksMaxParameters) { |
| 1265 | unsigned InvolvedParams = 0; |
| 1266 | for (unsigned u = 0, e = isl_set_n_param(Set); u < e; u++) |
| 1267 | if (isl_set_involves_dims(Set, isl_dim_param, u, 1)) |
| 1268 | InvolvedParams++; |
| 1269 | |
| 1270 | if (InvolvedParams > RunTimeChecksMaxParameters) { |
| 1271 | isl_set_free(Set); |
| 1272 | return -1; |
| 1273 | } |
| 1274 | } |
| 1275 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1276 | MinPMA = isl_set_lexmin_pw_multi_aff(isl_set_copy(Set)); |
| 1277 | MaxPMA = isl_set_lexmax_pw_multi_aff(isl_set_copy(Set)); |
| 1278 | |
Johannes Doerfert | 219b20e | 2014-10-07 14:37:59 +0000 | [diff] [blame] | 1279 | MinPMA = isl_pw_multi_aff_coalesce(MinPMA); |
| 1280 | MaxPMA = isl_pw_multi_aff_coalesce(MaxPMA); |
| 1281 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1282 | // Adjust the last dimension of the maximal access by one as we want to |
| 1283 | // enclose the accessed memory region by MinPMA and MaxPMA. The pointer |
| 1284 | // we test during code generation might now point after the end of the |
| 1285 | // allocated array but we will never dereference it anyway. |
| 1286 | assert(isl_pw_multi_aff_dim(MaxPMA, isl_dim_out) && |
| 1287 | "Assumed at least one output dimension"); |
| 1288 | Pos = isl_pw_multi_aff_dim(MaxPMA, isl_dim_out) - 1; |
| 1289 | LastDimAff = isl_pw_multi_aff_get_pw_aff(MaxPMA, Pos); |
| 1290 | OneAff = isl_aff_zero_on_domain( |
| 1291 | isl_local_space_from_space(isl_pw_aff_get_domain_space(LastDimAff))); |
| 1292 | OneAff = isl_aff_add_constant_si(OneAff, 1); |
| 1293 | LastDimAff = isl_pw_aff_add(LastDimAff, isl_pw_aff_from_aff(OneAff)); |
| 1294 | MaxPMA = isl_pw_multi_aff_set_pw_aff(MaxPMA, Pos, LastDimAff); |
| 1295 | |
| 1296 | MinMaxAccesses->push_back(std::make_pair(MinPMA, MaxPMA)); |
| 1297 | |
| 1298 | isl_set_free(Set); |
| 1299 | return 0; |
| 1300 | } |
| 1301 | |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 1302 | static __isl_give isl_set *getAccessDomain(MemoryAccess *MA) { |
| 1303 | isl_set *Domain = MA->getStatement()->getDomain(); |
| 1304 | Domain = isl_set_project_out(Domain, isl_dim_set, 0, isl_set_n_dim(Domain)); |
| 1305 | return isl_set_reset_tuple_id(Domain); |
| 1306 | } |
| 1307 | |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 1308 | bool Scop::buildAliasGroups(AliasAnalysis &AA) { |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1309 | // To create sound alias checks we perform the following steps: |
| 1310 | // o) Use the alias analysis and an alias set tracker to build alias sets |
| 1311 | // for all memory accesses inside the SCoP. |
| 1312 | // o) For each alias set we then map the aliasing pointers back to the |
| 1313 | // memory accesses we know, thus obtain groups of memory accesses which |
| 1314 | // might alias. |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 1315 | // o) We divide each group based on the domains of the minimal/maximal |
| 1316 | // accesses. That means two minimal/maximal accesses are only in a group |
| 1317 | // if their access domains intersect, otherwise they are in different |
| 1318 | // ones. |
Johannes Doerfert | 1377173 | 2014-10-01 12:40:46 +0000 | [diff] [blame] | 1319 | // o) We split groups such that they contain at most one read only base |
| 1320 | // address. |
| 1321 | // o) For each group with more than one base pointer we then compute minimal |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1322 | // and maximal accesses to each array in this group. |
| 1323 | using AliasGroupTy = SmallVector<MemoryAccess *, 4>; |
| 1324 | |
| 1325 | AliasSetTracker AST(AA); |
| 1326 | |
| 1327 | DenseMap<Value *, MemoryAccess *> PtrToAcc; |
Johannes Doerfert | 1377173 | 2014-10-01 12:40:46 +0000 | [diff] [blame] | 1328 | DenseSet<Value *> HasWriteAccess; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1329 | for (ScopStmt *Stmt : *this) { |
Johannes Doerfert | f1ee262 | 2014-10-06 17:43:00 +0000 | [diff] [blame] | 1330 | |
| 1331 | // Skip statements with an empty domain as they will never be executed. |
| 1332 | isl_set *StmtDomain = Stmt->getDomain(); |
| 1333 | bool StmtDomainEmpty = isl_set_is_empty(StmtDomain); |
| 1334 | isl_set_free(StmtDomain); |
| 1335 | if (StmtDomainEmpty) |
| 1336 | continue; |
| 1337 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1338 | for (MemoryAccess *MA : *Stmt) { |
| 1339 | if (MA->isScalar()) |
| 1340 | continue; |
Johannes Doerfert | 1377173 | 2014-10-01 12:40:46 +0000 | [diff] [blame] | 1341 | if (!MA->isRead()) |
| 1342 | HasWriteAccess.insert(MA->getBaseAddr()); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1343 | Instruction *Acc = MA->getAccessInstruction(); |
| 1344 | PtrToAcc[getPointerOperand(*Acc)] = MA; |
| 1345 | AST.add(Acc); |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | SmallVector<AliasGroupTy, 4> AliasGroups; |
| 1350 | for (AliasSet &AS : AST) { |
Johannes Doerfert | 74f6869 | 2014-10-08 02:23:48 +0000 | [diff] [blame] | 1351 | if (AS.isMustAlias() || AS.isForwardingAliasSet()) |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1352 | continue; |
| 1353 | AliasGroupTy AG; |
| 1354 | for (auto PR : AS) |
| 1355 | AG.push_back(PtrToAcc[PR.getValue()]); |
| 1356 | assert(AG.size() > 1 && |
| 1357 | "Alias groups should contain at least two accesses"); |
| 1358 | AliasGroups.push_back(std::move(AG)); |
| 1359 | } |
| 1360 | |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 1361 | // Split the alias groups based on their domain. |
| 1362 | for (unsigned u = 0; u < AliasGroups.size(); u++) { |
| 1363 | AliasGroupTy NewAG; |
| 1364 | AliasGroupTy &AG = AliasGroups[u]; |
| 1365 | AliasGroupTy::iterator AGI = AG.begin(); |
| 1366 | isl_set *AGDomain = getAccessDomain(*AGI); |
| 1367 | while (AGI != AG.end()) { |
| 1368 | MemoryAccess *MA = *AGI; |
| 1369 | isl_set *MADomain = getAccessDomain(MA); |
| 1370 | if (isl_set_is_disjoint(AGDomain, MADomain)) { |
| 1371 | NewAG.push_back(MA); |
| 1372 | AGI = AG.erase(AGI); |
| 1373 | isl_set_free(MADomain); |
| 1374 | } else { |
| 1375 | AGDomain = isl_set_union(AGDomain, MADomain); |
| 1376 | AGI++; |
| 1377 | } |
| 1378 | } |
| 1379 | if (NewAG.size() > 1) |
| 1380 | AliasGroups.push_back(std::move(NewAG)); |
| 1381 | isl_set_free(AGDomain); |
| 1382 | } |
| 1383 | |
Johannes Doerfert | 1377173 | 2014-10-01 12:40:46 +0000 | [diff] [blame] | 1384 | DenseMap<const Value *, SmallPtrSet<MemoryAccess *, 8>> ReadOnlyPairs; |
| 1385 | SmallPtrSet<const Value *, 4> NonReadOnlyBaseValues; |
| 1386 | for (AliasGroupTy &AG : AliasGroups) { |
| 1387 | NonReadOnlyBaseValues.clear(); |
| 1388 | ReadOnlyPairs.clear(); |
| 1389 | |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 1390 | if (AG.size() < 2) { |
| 1391 | AG.clear(); |
| 1392 | continue; |
| 1393 | } |
| 1394 | |
Johannes Doerfert | 1377173 | 2014-10-01 12:40:46 +0000 | [diff] [blame] | 1395 | for (auto II = AG.begin(); II != AG.end();) { |
| 1396 | Value *BaseAddr = (*II)->getBaseAddr(); |
| 1397 | if (HasWriteAccess.count(BaseAddr)) { |
| 1398 | NonReadOnlyBaseValues.insert(BaseAddr); |
| 1399 | II++; |
| 1400 | } else { |
| 1401 | ReadOnlyPairs[BaseAddr].insert(*II); |
| 1402 | II = AG.erase(II); |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | // If we don't have read only pointers check if there are at least two |
| 1407 | // non read only pointers, otherwise clear the alias group. |
| 1408 | if (ReadOnlyPairs.empty()) { |
| 1409 | if (NonReadOnlyBaseValues.size() <= 1) |
| 1410 | AG.clear(); |
| 1411 | continue; |
| 1412 | } |
| 1413 | |
| 1414 | // If we don't have non read only pointers clear the alias group. |
| 1415 | if (NonReadOnlyBaseValues.empty()) { |
| 1416 | AG.clear(); |
| 1417 | continue; |
| 1418 | } |
| 1419 | |
| 1420 | // If we have both read only and non read only base pointers we combine |
| 1421 | // the non read only ones with exactly one read only one at a time into a |
| 1422 | // new alias group and clear the old alias group in the end. |
| 1423 | for (const auto &ReadOnlyPair : ReadOnlyPairs) { |
| 1424 | AliasGroupTy AGNonReadOnly = AG; |
| 1425 | for (MemoryAccess *MA : ReadOnlyPair.second) |
| 1426 | AGNonReadOnly.push_back(MA); |
| 1427 | AliasGroups.push_back(std::move(AGNonReadOnly)); |
| 1428 | } |
| 1429 | AG.clear(); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 1432 | bool Valid = true; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1433 | for (AliasGroupTy &AG : AliasGroups) { |
Johannes Doerfert | 1377173 | 2014-10-01 12:40:46 +0000 | [diff] [blame] | 1434 | if (AG.empty()) |
| 1435 | continue; |
| 1436 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1437 | MinMaxVectorTy *MinMaxAccesses = new MinMaxVectorTy(); |
| 1438 | MinMaxAccesses->reserve(AG.size()); |
| 1439 | |
| 1440 | isl_union_map *Accesses = isl_union_map_empty(getParamSpace()); |
| 1441 | for (MemoryAccess *MA : AG) |
| 1442 | Accesses = isl_union_map_add_map(Accesses, MA->getAccessRelation()); |
| 1443 | Accesses = isl_union_map_intersect_domain(Accesses, getDomains()); |
| 1444 | |
| 1445 | isl_union_set *Locations = isl_union_map_range(Accesses); |
| 1446 | Locations = isl_union_set_intersect_params(Locations, getAssumedContext()); |
| 1447 | Locations = isl_union_set_coalesce(Locations); |
| 1448 | Locations = isl_union_set_detect_equalities(Locations); |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 1449 | Valid = (0 == isl_union_set_foreach_set(Locations, buildMinMaxAccess, |
| 1450 | MinMaxAccesses)); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1451 | isl_union_set_free(Locations); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1452 | MinMaxAliasGroups.push_back(MinMaxAccesses); |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 1453 | |
| 1454 | if (!Valid) |
| 1455 | break; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1456 | } |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 1457 | |
| 1458 | return Valid; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
Johannes Doerfert | e3da05a | 2014-11-01 00:12:13 +0000 | [diff] [blame] | 1461 | static unsigned getMaxLoopDepthInRegion(const Region &R, LoopInfo &LI) { |
| 1462 | unsigned MinLD = INT_MAX, MaxLD = 0; |
| 1463 | for (BasicBlock *BB : R.blocks()) { |
| 1464 | if (Loop *L = LI.getLoopFor(BB)) { |
| 1465 | unsigned LD = L->getLoopDepth(); |
| 1466 | MinLD = std::min(MinLD, LD); |
| 1467 | MaxLD = std::max(MaxLD, LD); |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | // Handle the case that there is no loop in the SCoP first. |
| 1472 | if (MaxLD == 0) |
| 1473 | return 1; |
| 1474 | |
| 1475 | assert(MinLD >= 1 && "Minimal loop depth should be at least one"); |
| 1476 | assert(MaxLD >= MinLD && |
| 1477 | "Maximal loop depth was smaller than mininaml loop depth?"); |
| 1478 | return MaxLD - MinLD + 1; |
| 1479 | } |
| 1480 | |
Tobias Grosser | 3f29619 | 2015-01-01 23:01:11 +0000 | [diff] [blame^] | 1481 | void Scop::dropConstantScheduleDims() { |
| 1482 | isl_union_map *FullSchedule = getSchedule(); |
| 1483 | |
| 1484 | if (isl_union_map_n_map(FullSchedule) == 0) { |
| 1485 | isl_union_map_free(FullSchedule); |
| 1486 | return; |
| 1487 | } |
| 1488 | |
| 1489 | isl_set *ScheduleSpace = |
| 1490 | isl_set_from_union_set(isl_union_map_range(FullSchedule)); |
| 1491 | isl_map *DropDimMap = isl_set_identity(isl_set_copy(ScheduleSpace)); |
| 1492 | |
| 1493 | int NumDimsDropped = 0; |
| 1494 | for (unsigned i = 0; i < isl_set_dim(ScheduleSpace, isl_dim_set); i++) |
| 1495 | if (i % 2 == 0) { |
| 1496 | isl_val *FixedVal = |
| 1497 | isl_set_plain_get_val_if_fixed(ScheduleSpace, isl_dim_set, i); |
| 1498 | if (isl_val_is_int(FixedVal)) { |
| 1499 | DropDimMap = |
| 1500 | isl_map_project_out(DropDimMap, isl_dim_out, i - NumDimsDropped, 1); |
| 1501 | NumDimsDropped++; |
| 1502 | } |
| 1503 | isl_val_free(FixedVal); |
| 1504 | } |
| 1505 | |
| 1506 | DropDimMap = isl_map_set_tuple_id( |
| 1507 | DropDimMap, isl_dim_out, isl_map_get_tuple_id(DropDimMap, isl_dim_in)); |
| 1508 | for (auto *S : *this) { |
| 1509 | isl_map *Schedule = S->getScattering(); |
| 1510 | Schedule = isl_map_apply_range(Schedule, isl_map_copy(DropDimMap)); |
| 1511 | S->setScattering(Schedule); |
| 1512 | } |
| 1513 | isl_set_free(ScheduleSpace); |
| 1514 | isl_map_free(DropDimMap); |
| 1515 | } |
| 1516 | |
Tobias Grosser | 0e27e24 | 2011-10-06 00:03:48 +0000 | [diff] [blame] | 1517 | Scop::Scop(TempScop &tempScop, LoopInfo &LI, ScalarEvolution &ScalarEvolution, |
| 1518 | isl_ctx *Context) |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 1519 | : SE(&ScalarEvolution), R(tempScop.getMaxRegion()), |
Johannes Doerfert | e3da05a | 2014-11-01 00:12:13 +0000 | [diff] [blame] | 1520 | MaxLoopDepth(getMaxLoopDepthInRegion(tempScop.getMaxRegion(), LI)) { |
Tobias Grosser | 9a38ab8 | 2011-11-08 15:41:03 +0000 | [diff] [blame] | 1521 | IslCtx = Context; |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 1522 | buildContext(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1523 | |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 1524 | SmallVector<Loop *, 8> NestLoops; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1525 | SmallVector<unsigned, 8> Scatter; |
| 1526 | |
| 1527 | Scatter.assign(MaxLoopDepth + 1, 0); |
| 1528 | |
| 1529 | // Build the iteration domain, access functions and scattering functions |
| 1530 | // traversing the region tree. |
| 1531 | buildScop(tempScop, getRegion(), NestLoops, Scatter, LI); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1532 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1533 | realignParams(); |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 1534 | addParameterBounds(); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1535 | simplifyAssumedContext(); |
Tobias Grosser | 3f29619 | 2015-01-01 23:01:11 +0000 | [diff] [blame^] | 1536 | dropConstantScheduleDims(); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1537 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1538 | assert(NestLoops.empty() && "NestLoops not empty at top level!"); |
| 1539 | } |
| 1540 | |
| 1541 | Scop::~Scop() { |
| 1542 | isl_set_free(Context); |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 1543 | isl_set_free(AssumedContext); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1544 | |
| 1545 | // Free the statements; |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1546 | for (ScopStmt *Stmt : *this) |
| 1547 | delete Stmt; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1548 | |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 1549 | // Free the ScopArrayInfo objects. |
| 1550 | for (auto &ScopArrayInfoPair : ScopArrayInfoMap) |
| 1551 | delete ScopArrayInfoPair.second; |
| 1552 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1553 | // Free the alias groups |
| 1554 | for (MinMaxVectorTy *MinMaxAccesses : MinMaxAliasGroups) { |
| 1555 | for (MinMaxAccessTy &MMA : *MinMaxAccesses) { |
| 1556 | isl_pw_multi_aff_free(MMA.first); |
| 1557 | isl_pw_multi_aff_free(MMA.second); |
| 1558 | } |
| 1559 | delete MinMaxAccesses; |
| 1560 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
Johannes Doerfert | 80ef110 | 2014-11-07 08:31:31 +0000 | [diff] [blame] | 1563 | const ScopArrayInfo * |
| 1564 | Scop::getOrCreateScopArrayInfo(Value *BasePtr, Type *AccessType, |
| 1565 | const SmallVector<const SCEV *, 4> &Sizes) { |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 1566 | const ScopArrayInfo *&SAI = ScopArrayInfoMap[BasePtr]; |
Johannes Doerfert | 80ef110 | 2014-11-07 08:31:31 +0000 | [diff] [blame] | 1567 | if (!SAI) |
| 1568 | SAI = new ScopArrayInfo(BasePtr, AccessType, getIslCtx(), Sizes); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 1569 | return SAI; |
| 1570 | } |
| 1571 | |
| 1572 | const ScopArrayInfo *Scop::getScopArrayInfo(Value *BasePtr) { |
| 1573 | const SCEV *PtrSCEV = SE->getSCEV(BasePtr); |
| 1574 | const SCEVUnknown *PtrBaseSCEV = |
| 1575 | cast<SCEVUnknown>(SE->getPointerBase(PtrSCEV)); |
| 1576 | const ScopArrayInfo *SAI = ScopArrayInfoMap[PtrBaseSCEV->getValue()]; |
| 1577 | assert(SAI && "No ScopArrayInfo available for this base pointer"); |
| 1578 | return SAI; |
| 1579 | } |
| 1580 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 1581 | std::string Scop::getContextStr() const { return stringFromIslObj(Context); } |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1582 | std::string Scop::getAssumedContextStr() const { |
| 1583 | return stringFromIslObj(AssumedContext); |
| 1584 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1585 | |
| 1586 | std::string Scop::getNameStr() const { |
| 1587 | std::string ExitName, EntryName; |
| 1588 | raw_string_ostream ExitStr(ExitName); |
| 1589 | raw_string_ostream EntryStr(EntryName); |
| 1590 | |
Tobias Grosser | f240b48 | 2014-01-09 10:42:15 +0000 | [diff] [blame] | 1591 | R.getEntry()->printAsOperand(EntryStr, false); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1592 | EntryStr.str(); |
| 1593 | |
| 1594 | if (R.getExit()) { |
Tobias Grosser | f240b48 | 2014-01-09 10:42:15 +0000 | [diff] [blame] | 1595 | R.getExit()->printAsOperand(ExitStr, false); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1596 | ExitStr.str(); |
| 1597 | } else |
| 1598 | ExitName = "FunctionExit"; |
| 1599 | |
| 1600 | return EntryName + "---" + ExitName; |
| 1601 | } |
| 1602 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 1603 | __isl_give isl_set *Scop::getContext() const { return isl_set_copy(Context); } |
Tobias Grosser | 3748705 | 2011-10-06 00:03:42 +0000 | [diff] [blame] | 1604 | __isl_give isl_space *Scop::getParamSpace() const { |
| 1605 | return isl_set_get_space(this->Context); |
| 1606 | } |
| 1607 | |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 1608 | __isl_give isl_set *Scop::getAssumedContext() const { |
| 1609 | return isl_set_copy(AssumedContext); |
| 1610 | } |
| 1611 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1612 | void Scop::addAssumption(__isl_take isl_set *Set) { |
| 1613 | AssumedContext = isl_set_intersect(AssumedContext, Set); |
Tobias Grosser | 7b50bee | 2014-11-25 10:51:12 +0000 | [diff] [blame] | 1614 | AssumedContext = isl_set_coalesce(AssumedContext); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1615 | } |
| 1616 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1617 | void Scop::printContext(raw_ostream &OS) const { |
| 1618 | OS << "Context:\n"; |
| 1619 | |
| 1620 | if (!Context) { |
| 1621 | OS.indent(4) << "n/a\n\n"; |
| 1622 | return; |
| 1623 | } |
| 1624 | |
| 1625 | OS.indent(4) << getContextStr() << "\n"; |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 1626 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1627 | OS.indent(4) << "Assumed Context:\n"; |
| 1628 | if (!AssumedContext) { |
| 1629 | OS.indent(4) << "n/a\n\n"; |
| 1630 | return; |
| 1631 | } |
| 1632 | |
| 1633 | OS.indent(4) << getAssumedContextStr() << "\n"; |
| 1634 | |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1635 | for (const SCEV *Parameter : Parameters) { |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 1636 | int Dim = ParameterIds.find(Parameter)->second; |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 1637 | OS.indent(4) << "p" << Dim << ": " << *Parameter << "\n"; |
| 1638 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1641 | void Scop::printAliasAssumptions(raw_ostream &OS) const { |
| 1642 | OS.indent(4) << "Alias Groups (" << MinMaxAliasGroups.size() << "):\n"; |
| 1643 | if (MinMaxAliasGroups.empty()) { |
| 1644 | OS.indent(8) << "n/a\n"; |
| 1645 | return; |
| 1646 | } |
| 1647 | for (MinMaxVectorTy *MinMaxAccesses : MinMaxAliasGroups) { |
| 1648 | OS.indent(8) << "[["; |
| 1649 | for (MinMaxAccessTy &MinMacAccess : *MinMaxAccesses) |
| 1650 | OS << " <" << MinMacAccess.first << ", " << MinMacAccess.second << ">"; |
| 1651 | OS << " ]]\n"; |
| 1652 | } |
| 1653 | } |
| 1654 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1655 | void Scop::printStatements(raw_ostream &OS) const { |
| 1656 | OS << "Statements {\n"; |
| 1657 | |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1658 | for (ScopStmt *Stmt : *this) |
| 1659 | OS.indent(4) << *Stmt; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1660 | |
| 1661 | OS.indent(4) << "}\n"; |
| 1662 | } |
| 1663 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1664 | void Scop::print(raw_ostream &OS) const { |
Tobias Grosser | 4eb7ddb | 2014-03-18 18:51:11 +0000 | [diff] [blame] | 1665 | OS.indent(4) << "Function: " << getRegion().getEntry()->getParent()->getName() |
| 1666 | << "\n"; |
Tobias Grosser | 483fdd4 | 2014-03-18 18:05:38 +0000 | [diff] [blame] | 1667 | OS.indent(4) << "Region: " << getNameStr() << "\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1668 | printContext(OS.indent(4)); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1669 | printAliasAssumptions(OS); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1670 | printStatements(OS.indent(4)); |
| 1671 | } |
| 1672 | |
| 1673 | void Scop::dump() const { print(dbgs()); } |
| 1674 | |
Tobias Grosser | 9a38ab8 | 2011-11-08 15:41:03 +0000 | [diff] [blame] | 1675 | isl_ctx *Scop::getIslCtx() const { return IslCtx; } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1676 | |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 1677 | __isl_give isl_union_set *Scop::getDomains() { |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 1678 | isl_union_set *Domain = isl_union_set_empty(getParamSpace()); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 1679 | |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 1680 | for (ScopStmt *Stmt : *this) |
| 1681 | Domain = isl_union_set_add_set(Domain, Stmt->getDomain()); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 1682 | |
| 1683 | return Domain; |
| 1684 | } |
| 1685 | |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 1686 | __isl_give isl_union_map *Scop::getMustWrites() { |
| 1687 | isl_union_map *Write = isl_union_map_empty(this->getParamSpace()); |
| 1688 | |
| 1689 | for (ScopStmt *Stmt : *this) { |
| 1690 | for (MemoryAccess *MA : *Stmt) { |
| 1691 | if (!MA->isMustWrite()) |
| 1692 | continue; |
| 1693 | |
| 1694 | isl_set *Domain = Stmt->getDomain(); |
| 1695 | isl_map *AccessDomain = MA->getAccessRelation(); |
| 1696 | AccessDomain = isl_map_intersect_domain(AccessDomain, Domain); |
| 1697 | Write = isl_union_map_add_map(Write, AccessDomain); |
| 1698 | } |
| 1699 | } |
| 1700 | return isl_union_map_coalesce(Write); |
| 1701 | } |
| 1702 | |
| 1703 | __isl_give isl_union_map *Scop::getMayWrites() { |
| 1704 | isl_union_map *Write = isl_union_map_empty(this->getParamSpace()); |
| 1705 | |
| 1706 | for (ScopStmt *Stmt : *this) { |
| 1707 | for (MemoryAccess *MA : *Stmt) { |
| 1708 | if (!MA->isMayWrite()) |
| 1709 | continue; |
| 1710 | |
| 1711 | isl_set *Domain = Stmt->getDomain(); |
| 1712 | isl_map *AccessDomain = MA->getAccessRelation(); |
| 1713 | AccessDomain = isl_map_intersect_domain(AccessDomain, Domain); |
| 1714 | Write = isl_union_map_add_map(Write, AccessDomain); |
| 1715 | } |
| 1716 | } |
| 1717 | return isl_union_map_coalesce(Write); |
| 1718 | } |
| 1719 | |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1720 | __isl_give isl_union_map *Scop::getWrites() { |
| 1721 | isl_union_map *Write = isl_union_map_empty(this->getParamSpace()); |
| 1722 | |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 1723 | for (ScopStmt *Stmt : *this) { |
Johannes Doerfert | f675289 | 2014-06-13 18:01:45 +0000 | [diff] [blame] | 1724 | for (MemoryAccess *MA : *Stmt) { |
| 1725 | if (!MA->isWrite()) |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1726 | continue; |
| 1727 | |
| 1728 | isl_set *Domain = Stmt->getDomain(); |
Johannes Doerfert | f675289 | 2014-06-13 18:01:45 +0000 | [diff] [blame] | 1729 | isl_map *AccessDomain = MA->getAccessRelation(); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1730 | AccessDomain = isl_map_intersect_domain(AccessDomain, Domain); |
| 1731 | Write = isl_union_map_add_map(Write, AccessDomain); |
| 1732 | } |
| 1733 | } |
| 1734 | return isl_union_map_coalesce(Write); |
| 1735 | } |
| 1736 | |
| 1737 | __isl_give isl_union_map *Scop::getReads() { |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 1738 | isl_union_map *Read = isl_union_map_empty(getParamSpace()); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1739 | |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 1740 | for (ScopStmt *Stmt : *this) { |
Johannes Doerfert | f675289 | 2014-06-13 18:01:45 +0000 | [diff] [blame] | 1741 | for (MemoryAccess *MA : *Stmt) { |
| 1742 | if (!MA->isRead()) |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1743 | continue; |
| 1744 | |
| 1745 | isl_set *Domain = Stmt->getDomain(); |
Johannes Doerfert | f675289 | 2014-06-13 18:01:45 +0000 | [diff] [blame] | 1746 | isl_map *AccessDomain = MA->getAccessRelation(); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1747 | |
| 1748 | AccessDomain = isl_map_intersect_domain(AccessDomain, Domain); |
| 1749 | Read = isl_union_map_add_map(Read, AccessDomain); |
| 1750 | } |
| 1751 | } |
| 1752 | return isl_union_map_coalesce(Read); |
| 1753 | } |
| 1754 | |
| 1755 | __isl_give isl_union_map *Scop::getSchedule() { |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 1756 | isl_union_map *Schedule = isl_union_map_empty(getParamSpace()); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1757 | |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 1758 | for (ScopStmt *Stmt : *this) |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1759 | Schedule = isl_union_map_add_map(Schedule, Stmt->getScattering()); |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 1760 | |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1761 | return isl_union_map_coalesce(Schedule); |
| 1762 | } |
| 1763 | |
| 1764 | bool Scop::restrictDomains(__isl_take isl_union_set *Domain) { |
| 1765 | bool Changed = false; |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 1766 | for (ScopStmt *Stmt : *this) { |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1767 | isl_union_set *StmtDomain = isl_union_set_from_set(Stmt->getDomain()); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1768 | isl_union_set *NewStmtDomain = isl_union_set_intersect( |
| 1769 | isl_union_set_copy(StmtDomain), isl_union_set_copy(Domain)); |
| 1770 | |
| 1771 | if (isl_union_set_is_subset(StmtDomain, NewStmtDomain)) { |
| 1772 | isl_union_set_free(StmtDomain); |
| 1773 | isl_union_set_free(NewStmtDomain); |
| 1774 | continue; |
| 1775 | } |
| 1776 | |
| 1777 | Changed = true; |
| 1778 | |
| 1779 | isl_union_set_free(StmtDomain); |
| 1780 | NewStmtDomain = isl_union_set_coalesce(NewStmtDomain); |
| 1781 | |
| 1782 | if (isl_union_set_is_empty(NewStmtDomain)) { |
| 1783 | Stmt->restrictDomain(isl_set_empty(Stmt->getDomainSpace())); |
| 1784 | isl_union_set_free(NewStmtDomain); |
| 1785 | } else |
| 1786 | Stmt->restrictDomain(isl_set_from_union_set(NewStmtDomain)); |
| 1787 | } |
| 1788 | isl_union_set_free(Domain); |
| 1789 | return Changed; |
| 1790 | } |
| 1791 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1792 | ScalarEvolution *Scop::getSE() const { return SE; } |
| 1793 | |
| 1794 | bool Scop::isTrivialBB(BasicBlock *BB, TempScop &tempScop) { |
| 1795 | if (tempScop.getAccessFunctions(BB)) |
| 1796 | return false; |
| 1797 | |
| 1798 | return true; |
| 1799 | } |
| 1800 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 1801 | void Scop::buildScop(TempScop &tempScop, const Region &CurRegion, |
| 1802 | SmallVectorImpl<Loop *> &NestLoops, |
| 1803 | SmallVectorImpl<unsigned> &Scatter, LoopInfo &LI) { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1804 | Loop *L = castToLoop(CurRegion, LI); |
| 1805 | |
| 1806 | if (L) |
| 1807 | NestLoops.push_back(L); |
| 1808 | |
| 1809 | unsigned loopDepth = NestLoops.size(); |
| 1810 | assert(Scatter.size() > loopDepth && "Scatter not big enough!"); |
| 1811 | |
| 1812 | for (Region::const_element_iterator I = CurRegion.element_begin(), |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 1813 | E = CurRegion.element_end(); |
| 1814 | I != E; ++I) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1815 | if (I->isSubRegion()) |
| 1816 | buildScop(tempScop, *(I->getNodeAs<Region>()), NestLoops, Scatter, LI); |
| 1817 | else { |
| 1818 | BasicBlock *BB = I->getNodeAs<BasicBlock>(); |
| 1819 | |
| 1820 | if (isTrivialBB(BB, tempScop)) |
| 1821 | continue; |
| 1822 | |
Johannes Doerfert | 7c49421 | 2014-10-31 23:13:39 +0000 | [diff] [blame] | 1823 | ScopStmt *Stmt = |
| 1824 | new ScopStmt(*this, tempScop, CurRegion, *BB, NestLoops, Scatter); |
| 1825 | |
| 1826 | // Insert all statements into the statement map and the statement vector. |
| 1827 | StmtMap[BB] = Stmt; |
| 1828 | Stmts.push_back(Stmt); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1829 | |
| 1830 | // Increasing the Scattering function is OK for the moment, because |
| 1831 | // we are using a depth first iterator and the program is well structured. |
| 1832 | ++Scatter[loopDepth]; |
| 1833 | } |
| 1834 | |
| 1835 | if (!L) |
| 1836 | return; |
| 1837 | |
| 1838 | // Exiting a loop region. |
| 1839 | Scatter[loopDepth] = 0; |
| 1840 | NestLoops.pop_back(); |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 1841 | ++Scatter[loopDepth - 1]; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
Johannes Doerfert | 7c49421 | 2014-10-31 23:13:39 +0000 | [diff] [blame] | 1844 | ScopStmt *Scop::getStmtForBasicBlock(BasicBlock *BB) const { |
| 1845 | const auto &StmtMapIt = StmtMap.find(BB); |
| 1846 | if (StmtMapIt == StmtMap.end()) |
| 1847 | return nullptr; |
| 1848 | return StmtMapIt->second; |
| 1849 | } |
| 1850 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1851 | //===----------------------------------------------------------------------===// |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 1852 | ScopInfo::ScopInfo() : RegionPass(ID), scop(0) { |
| 1853 | ctx = isl_ctx_alloc(); |
Tobias Grosser | 4a8e356 | 2011-12-07 07:42:51 +0000 | [diff] [blame] | 1854 | isl_options_set_on_error(ctx, ISL_ON_ERROR_ABORT); |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | ScopInfo::~ScopInfo() { |
| 1858 | clear(); |
| 1859 | isl_ctx_free(ctx); |
| 1860 | } |
| 1861 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1862 | void ScopInfo::getAnalysisUsage(AnalysisUsage &AU) const { |
| 1863 | AU.addRequired<LoopInfo>(); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 1864 | AU.addRequired<RegionInfoPass>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1865 | AU.addRequired<ScalarEvolution>(); |
| 1866 | AU.addRequired<TempScopInfo>(); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1867 | AU.addRequired<AliasAnalysis>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1868 | AU.setPreservesAll(); |
| 1869 | } |
| 1870 | |
| 1871 | bool ScopInfo::runOnRegion(Region *R, RGPassManager &RGM) { |
| 1872 | LoopInfo &LI = getAnalysis<LoopInfo>(); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1873 | AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1874 | ScalarEvolution &SE = getAnalysis<ScalarEvolution>(); |
| 1875 | |
| 1876 | TempScop *tempScop = getAnalysis<TempScopInfo>().getTempScop(R); |
| 1877 | |
| 1878 | // This region is no Scop. |
| 1879 | if (!tempScop) { |
Tobias Grosser | c98a8fc | 2014-11-14 11:12:31 +0000 | [diff] [blame] | 1880 | scop = nullptr; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1881 | return false; |
| 1882 | } |
| 1883 | |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 1884 | scop = new Scop(*tempScop, LI, SE, ctx); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1885 | |
Johannes Doerfert | 21aa3dc | 2014-11-01 01:30:11 +0000 | [diff] [blame] | 1886 | if (!PollyUseRuntimeAliasChecks) { |
| 1887 | // Statistics. |
| 1888 | ++ScopFound; |
| 1889 | if (scop->getMaxLoopDepth() > 0) |
| 1890 | ++RichScopFound; |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 1891 | return false; |
Johannes Doerfert | 21aa3dc | 2014-11-01 01:30:11 +0000 | [diff] [blame] | 1892 | } |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1893 | |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 1894 | // If a problem occurs while building the alias groups we need to delete |
| 1895 | // this SCoP and pretend it wasn't valid in the first place. |
Johannes Doerfert | 21aa3dc | 2014-11-01 01:30:11 +0000 | [diff] [blame] | 1896 | if (scop->buildAliasGroups(AA)) { |
| 1897 | // Statistics. |
| 1898 | ++ScopFound; |
| 1899 | if (scop->getMaxLoopDepth() > 0) |
| 1900 | ++RichScopFound; |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 1901 | return false; |
Johannes Doerfert | 21aa3dc | 2014-11-01 01:30:11 +0000 | [diff] [blame] | 1902 | } |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 1903 | |
| 1904 | DEBUG(dbgs() |
| 1905 | << "\n\nNOTE: Run time checks for " << scop->getNameStr() |
| 1906 | << " could not be created as the number of parameters involved is too " |
| 1907 | "high. The SCoP will be " |
| 1908 | "dismissed.\nUse:\n\t--polly-rtc-max-parameters=X\nto adjust the " |
| 1909 | "maximal number of parameters but be advised that the compile time " |
| 1910 | "might increase exponentially.\n\n"); |
| 1911 | |
| 1912 | delete scop; |
| 1913 | scop = nullptr; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1914 | return false; |
| 1915 | } |
| 1916 | |
| 1917 | char ScopInfo::ID = 0; |
| 1918 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 1919 | Pass *polly::createScopInfoPass() { return new ScopInfo(); } |
| 1920 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 1921 | INITIALIZE_PASS_BEGIN(ScopInfo, "polly-scops", |
| 1922 | "Polly - Create polyhedral description of Scops", false, |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 1923 | false); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1924 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis); |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 1925 | INITIALIZE_PASS_DEPENDENCY(LoopInfo); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 1926 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 1927 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution); |
| 1928 | INITIALIZE_PASS_DEPENDENCY(TempScopInfo); |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 1929 | INITIALIZE_PASS_END(ScopInfo, "polly-scops", |
| 1930 | "Polly - Create polyhedral description of Scops", false, |
| 1931 | false) |