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