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