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