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