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