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