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