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