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