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