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 | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 1387 | void ScopStmt::removeMemoryAccesses(MemoryAccessList &InvMAs) { |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 1388 | |
| 1389 | // Remove all memory accesses in @p InvMAs from this statement together |
| 1390 | // with all scalar accesses that were caused by them. The tricky iteration |
| 1391 | // order uses is needed because the MemAccs is a vector and the order in |
| 1392 | // which the accesses of each memory access list (MAL) are stored in this |
| 1393 | // vector is reversed. |
| 1394 | for (MemoryAccess *MA : InvMAs) { |
| 1395 | auto &MAL = *lookupAccessesFor(MA->getAccessInstruction()); |
| 1396 | MAL.reverse(); |
| 1397 | |
| 1398 | auto MALIt = MAL.begin(); |
| 1399 | auto MALEnd = MAL.end(); |
| 1400 | auto MemAccsIt = MemAccs.begin(); |
| 1401 | while (MALIt != MALEnd) { |
| 1402 | while (*MemAccsIt != *MALIt) |
| 1403 | MemAccsIt++; |
| 1404 | |
| 1405 | MALIt++; |
| 1406 | MemAccs.erase(MemAccsIt); |
| 1407 | } |
| 1408 | |
| 1409 | InstructionToAccess.erase(MA->getAccessInstruction()); |
| 1410 | delete &MAL; |
| 1411 | } |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1414 | //===----------------------------------------------------------------------===// |
| 1415 | /// Scop class implement |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 1416 | |
Tobias Grosser | 7ffe4e8 | 2011-11-17 12:56:10 +0000 | [diff] [blame] | 1417 | void Scop::setContext(__isl_take isl_set *NewContext) { |
Tobias Grosser | ff9b54d | 2011-11-15 11:38:44 +0000 | [diff] [blame] | 1418 | NewContext = isl_set_align_params(NewContext, isl_set_get_space(Context)); |
| 1419 | isl_set_free(Context); |
| 1420 | Context = NewContext; |
| 1421 | } |
| 1422 | |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 1423 | const SCEV *Scop::getRepresentingInvariantLoadSCEV(const SCEV *S) { |
| 1424 | return SCEVParameterRewriter::rewrite(S, *SE, InvEquivClassVMap); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 1427 | void Scop::addParams(std::vector<const SCEV *> NewParameters) { |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1428 | for (const SCEV *Parameter : NewParameters) { |
Johannes Doerfert | be40996 | 2015-03-29 20:45:09 +0000 | [diff] [blame] | 1429 | Parameter = extractConstantFactor(Parameter, *SE).second; |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 1430 | |
| 1431 | // Normalize the SCEV to get the representing element for an invariant load. |
| 1432 | Parameter = getRepresentingInvariantLoadSCEV(Parameter); |
| 1433 | |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 1434 | if (ParameterIds.find(Parameter) != ParameterIds.end()) |
| 1435 | continue; |
| 1436 | |
| 1437 | int dimension = Parameters.size(); |
| 1438 | |
| 1439 | Parameters.push_back(Parameter); |
| 1440 | ParameterIds[Parameter] = dimension; |
| 1441 | } |
| 1442 | } |
| 1443 | |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 1444 | __isl_give isl_id *Scop::getIdForParam(const SCEV *Parameter) { |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 1445 | // Normalize the SCEV to get the representing element for an invariant load. |
| 1446 | Parameter = getRepresentingInvariantLoadSCEV(Parameter); |
| 1447 | |
Tobias Grosser | 9a38ab8 | 2011-11-08 15:41:03 +0000 | [diff] [blame] | 1448 | ParamIdType::const_iterator IdIter = ParameterIds.find(Parameter); |
Tobias Grosser | 76c2e32 | 2011-11-07 12:58:59 +0000 | [diff] [blame] | 1449 | |
Tobias Grosser | 9a38ab8 | 2011-11-08 15:41:03 +0000 | [diff] [blame] | 1450 | if (IdIter == ParameterIds.end()) |
Tobias Grosser | 5a56cbf | 2014-04-16 07:33:47 +0000 | [diff] [blame] | 1451 | return nullptr; |
Tobias Grosser | 76c2e32 | 2011-11-07 12:58:59 +0000 | [diff] [blame] | 1452 | |
Tobias Grosser | 8f99c16 | 2011-11-15 11:38:55 +0000 | [diff] [blame] | 1453 | std::string ParameterName; |
| 1454 | |
| 1455 | if (const SCEVUnknown *ValueParameter = dyn_cast<SCEVUnknown>(Parameter)) { |
| 1456 | Value *Val = ValueParameter->getValue(); |
Tobias Grosser | 29ee0b1 | 2011-11-17 14:52:36 +0000 | [diff] [blame] | 1457 | ParameterName = Val->getName(); |
Tobias Grosser | 8f99c16 | 2011-11-15 11:38:55 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | if (ParameterName == "" || ParameterName.substr(0, 2) == "p_") |
Hongbin Zheng | 86a3774 | 2012-04-25 08:01:38 +0000 | [diff] [blame] | 1461 | ParameterName = "p_" + utostr_32(IdIter->second); |
Tobias Grosser | 8f99c16 | 2011-11-15 11:38:55 +0000 | [diff] [blame] | 1462 | |
Tobias Grosser | 20532b8 | 2014-04-11 17:56:49 +0000 | [diff] [blame] | 1463 | return isl_id_alloc(getIslCtx(), ParameterName.c_str(), |
| 1464 | const_cast<void *>((const void *)Parameter)); |
Tobias Grosser | 76c2e32 | 2011-11-07 12:58:59 +0000 | [diff] [blame] | 1465 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1466 | |
Johannes Doerfert | 5d5b306 | 2015-08-20 18:06:30 +0000 | [diff] [blame] | 1467 | isl_set *Scop::addNonEmptyDomainConstraints(isl_set *C) const { |
| 1468 | isl_set *DomainContext = isl_union_set_params(getDomains()); |
| 1469 | return isl_set_intersect_params(C, DomainContext); |
| 1470 | } |
| 1471 | |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 1472 | void Scop::buildBoundaryContext() { |
| 1473 | BoundaryContext = Affinator.getWrappingContext(); |
| 1474 | BoundaryContext = isl_set_complement(BoundaryContext); |
| 1475 | BoundaryContext = isl_set_gist_params(BoundaryContext, getContext()); |
| 1476 | } |
| 1477 | |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 1478 | void Scop::addUserContext() { |
| 1479 | if (UserContextStr.empty()) |
| 1480 | return; |
| 1481 | |
| 1482 | isl_set *UserContext = isl_set_read_from_str(IslCtx, UserContextStr.c_str()); |
| 1483 | isl_space *Space = getParamSpace(); |
| 1484 | if (isl_space_dim(Space, isl_dim_param) != |
| 1485 | isl_set_dim(UserContext, isl_dim_param)) { |
| 1486 | auto SpaceStr = isl_space_to_str(Space); |
| 1487 | errs() << "Error: the context provided in -polly-context has not the same " |
| 1488 | << "number of dimensions than the computed context. Due to this " |
| 1489 | << "mismatch, the -polly-context option is ignored. Please provide " |
| 1490 | << "the context in the parameter space: " << SpaceStr << ".\n"; |
| 1491 | free(SpaceStr); |
| 1492 | isl_set_free(UserContext); |
| 1493 | isl_space_free(Space); |
| 1494 | return; |
| 1495 | } |
| 1496 | |
| 1497 | for (unsigned i = 0; i < isl_space_dim(Space, isl_dim_param); i++) { |
| 1498 | auto NameContext = isl_set_get_dim_name(Context, isl_dim_param, i); |
| 1499 | auto NameUserContext = isl_set_get_dim_name(UserContext, isl_dim_param, i); |
| 1500 | |
| 1501 | if (strcmp(NameContext, NameUserContext) != 0) { |
| 1502 | auto SpaceStr = isl_space_to_str(Space); |
| 1503 | errs() << "Error: the name of dimension " << i |
| 1504 | << " provided in -polly-context " |
| 1505 | << "is '" << NameUserContext << "', but the name in the computed " |
| 1506 | << "context is '" << NameContext |
| 1507 | << "'. Due to this name mismatch, " |
| 1508 | << "the -polly-context option is ignored. Please provide " |
| 1509 | << "the context in the parameter space: " << SpaceStr << ".\n"; |
| 1510 | free(SpaceStr); |
| 1511 | isl_set_free(UserContext); |
| 1512 | isl_space_free(Space); |
| 1513 | return; |
| 1514 | } |
| 1515 | |
| 1516 | UserContext = |
| 1517 | isl_set_set_dim_id(UserContext, isl_dim_param, i, |
| 1518 | isl_space_get_dim_id(Space, isl_dim_param, i)); |
| 1519 | } |
| 1520 | |
| 1521 | Context = isl_set_intersect(Context, UserContext); |
| 1522 | isl_space_free(Space); |
| 1523 | } |
| 1524 | |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 1525 | void Scop::buildInvariantEquivalenceClasses() { |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 1526 | DenseMap<const SCEV *, LoadInst *> EquivClasses; |
| 1527 | |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 1528 | const InvariantLoadsSetTy &RIL = *SD.getRequiredInvariantLoads(&getRegion()); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 1529 | for (LoadInst *LInst : RIL) { |
| 1530 | const SCEV *PointerSCEV = SE->getSCEV(LInst->getPointerOperand()); |
| 1531 | |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 1532 | LoadInst *&ClassRep = EquivClasses[PointerSCEV]; |
| 1533 | if (!ClassRep) |
| 1534 | ClassRep = LInst; |
| 1535 | else |
| 1536 | InvEquivClassVMap[LInst] = ClassRep; |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 1537 | } |
| 1538 | } |
| 1539 | |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 1540 | void Scop::buildContext() { |
| 1541 | isl_space *Space = isl_space_params_alloc(IslCtx, 0); |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 1542 | Context = isl_set_universe(isl_space_copy(Space)); |
| 1543 | AssumedContext = isl_set_universe(Space); |
Tobias Grosser | 0e27e24 | 2011-10-06 00:03:48 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 1546 | void Scop::addParameterBounds() { |
Johannes Doerfert | 4f8ac3d | 2015-02-23 16:15:51 +0000 | [diff] [blame] | 1547 | for (const auto &ParamID : ParameterIds) { |
Johannes Doerfert | 4f8ac3d | 2015-02-23 16:15:51 +0000 | [diff] [blame] | 1548 | int dim = ParamID.second; |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 1549 | |
Johannes Doerfert | 4f8ac3d | 2015-02-23 16:15:51 +0000 | [diff] [blame] | 1550 | ConstantRange SRange = SE->getSignedRange(ParamID.first); |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 1551 | |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 1552 | Context = addRangeBoundsToSet(Context, SRange, dim, isl_dim_param); |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 1553 | } |
| 1554 | } |
| 1555 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1556 | void Scop::realignParams() { |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 1557 | // Add all parameters into a common model. |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 1558 | isl_space *Space = isl_space_params_alloc(IslCtx, ParameterIds.size()); |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 1559 | |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1560 | for (const auto &ParamID : ParameterIds) { |
| 1561 | const SCEV *Parameter = ParamID.first; |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 1562 | isl_id *id = getIdForParam(Parameter); |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1563 | 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] | 1564 | } |
| 1565 | |
| 1566 | // Align the parameters of all data structures to the model. |
| 1567 | Context = isl_set_align_params(Context, Space); |
| 1568 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 1569 | for (ScopStmt &Stmt : *this) |
| 1570 | Stmt.realignParams(); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 1573 | static __isl_give isl_set * |
| 1574 | simplifyAssumptionContext(__isl_take isl_set *AssumptionContext, |
| 1575 | const Scop &S) { |
| 1576 | isl_set *DomainParameters = isl_union_set_params(S.getDomains()); |
| 1577 | AssumptionContext = isl_set_gist_params(AssumptionContext, DomainParameters); |
| 1578 | AssumptionContext = isl_set_gist_params(AssumptionContext, S.getContext()); |
| 1579 | return AssumptionContext; |
| 1580 | } |
| 1581 | |
| 1582 | void Scop::simplifyContexts() { |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1583 | // The parameter constraints of the iteration domains give us a set of |
| 1584 | // constraints that need to hold for all cases where at least a single |
| 1585 | // statement iteration is executed in the whole scop. We now simplify the |
| 1586 | // assumed context under the assumption that such constraints hold and at |
| 1587 | // least a single statement iteration is executed. For cases where no |
| 1588 | // statement instances are executed, the assumptions we have taken about |
| 1589 | // the executed code do not matter and can be changed. |
| 1590 | // |
| 1591 | // WARNING: This only holds if the assumptions we have taken do not reduce |
| 1592 | // the set of statement instances that are executed. Otherwise we |
| 1593 | // may run into a case where the iteration domains suggest that |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1594 | // for a certain set of parameter constraints no code is executed, |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1595 | // but in the original program some computation would have been |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1596 | // performed. In such a case, modifying the run-time conditions and |
| 1597 | // possibly influencing the run-time check may cause certain scops |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1598 | // to not be executed. |
| 1599 | // |
| 1600 | // Example: |
| 1601 | // |
| 1602 | // When delinearizing the following code: |
| 1603 | // |
| 1604 | // for (long i = 0; i < 100; i++) |
| 1605 | // for (long j = 0; j < m; j++) |
| 1606 | // A[i+p][j] = 1.0; |
| 1607 | // |
| 1608 | // 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] | 1609 | // 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] | 1610 | // 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] | 1611 | AssumedContext = simplifyAssumptionContext(AssumedContext, *this); |
| 1612 | BoundaryContext = simplifyAssumptionContext(BoundaryContext, *this); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1613 | } |
| 1614 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1615 | /// @brief Add the minimal/maximal access in @p Set to @p User. |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 1616 | static isl_stat buildMinMaxAccess(__isl_take isl_set *Set, void *User) { |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1617 | Scop::MinMaxVectorTy *MinMaxAccesses = (Scop::MinMaxVectorTy *)User; |
| 1618 | isl_pw_multi_aff *MinPMA, *MaxPMA; |
| 1619 | isl_pw_aff *LastDimAff; |
| 1620 | isl_aff *OneAff; |
| 1621 | unsigned Pos; |
| 1622 | |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 1623 | // Restrict the number of parameters involved in the access as the lexmin/ |
| 1624 | // lexmax computation will take too long if this number is high. |
| 1625 | // |
| 1626 | // Experiments with a simple test case using an i7 4800MQ: |
| 1627 | // |
| 1628 | // #Parameters involved | Time (in sec) |
| 1629 | // 6 | 0.01 |
| 1630 | // 7 | 0.04 |
| 1631 | // 8 | 0.12 |
| 1632 | // 9 | 0.40 |
| 1633 | // 10 | 1.54 |
| 1634 | // 11 | 6.78 |
| 1635 | // 12 | 30.38 |
| 1636 | // |
| 1637 | if (isl_set_n_param(Set) > RunTimeChecksMaxParameters) { |
| 1638 | unsigned InvolvedParams = 0; |
| 1639 | for (unsigned u = 0, e = isl_set_n_param(Set); u < e; u++) |
| 1640 | if (isl_set_involves_dims(Set, isl_dim_param, u, 1)) |
| 1641 | InvolvedParams++; |
| 1642 | |
| 1643 | if (InvolvedParams > RunTimeChecksMaxParameters) { |
| 1644 | isl_set_free(Set); |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 1645 | return isl_stat_error; |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 1646 | } |
| 1647 | } |
| 1648 | |
Johannes Doerfert | b6755bb | 2015-02-14 12:00:06 +0000 | [diff] [blame] | 1649 | Set = isl_set_remove_divs(Set); |
| 1650 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1651 | MinPMA = isl_set_lexmin_pw_multi_aff(isl_set_copy(Set)); |
| 1652 | MaxPMA = isl_set_lexmax_pw_multi_aff(isl_set_copy(Set)); |
| 1653 | |
Johannes Doerfert | 219b20e | 2014-10-07 14:37:59 +0000 | [diff] [blame] | 1654 | MinPMA = isl_pw_multi_aff_coalesce(MinPMA); |
| 1655 | MaxPMA = isl_pw_multi_aff_coalesce(MaxPMA); |
| 1656 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1657 | // Adjust the last dimension of the maximal access by one as we want to |
| 1658 | // enclose the accessed memory region by MinPMA and MaxPMA. The pointer |
| 1659 | // we test during code generation might now point after the end of the |
| 1660 | // allocated array but we will never dereference it anyway. |
| 1661 | assert(isl_pw_multi_aff_dim(MaxPMA, isl_dim_out) && |
| 1662 | "Assumed at least one output dimension"); |
| 1663 | Pos = isl_pw_multi_aff_dim(MaxPMA, isl_dim_out) - 1; |
| 1664 | LastDimAff = isl_pw_multi_aff_get_pw_aff(MaxPMA, Pos); |
| 1665 | OneAff = isl_aff_zero_on_domain( |
| 1666 | isl_local_space_from_space(isl_pw_aff_get_domain_space(LastDimAff))); |
| 1667 | OneAff = isl_aff_add_constant_si(OneAff, 1); |
| 1668 | LastDimAff = isl_pw_aff_add(LastDimAff, isl_pw_aff_from_aff(OneAff)); |
| 1669 | MaxPMA = isl_pw_multi_aff_set_pw_aff(MaxPMA, Pos, LastDimAff); |
| 1670 | |
| 1671 | MinMaxAccesses->push_back(std::make_pair(MinPMA, MaxPMA)); |
| 1672 | |
| 1673 | isl_set_free(Set); |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 1674 | return isl_stat_ok; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 1677 | static __isl_give isl_set *getAccessDomain(MemoryAccess *MA) { |
| 1678 | isl_set *Domain = MA->getStatement()->getDomain(); |
| 1679 | Domain = isl_set_project_out(Domain, isl_dim_set, 0, isl_set_n_dim(Domain)); |
| 1680 | return isl_set_reset_tuple_id(Domain); |
| 1681 | } |
| 1682 | |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 1683 | /// @brief Wrapper function to calculate minimal/maximal accesses to each array. |
| 1684 | static bool calculateMinMaxAccess(__isl_take isl_union_map *Accesses, |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 1685 | __isl_take isl_union_set *Domains, |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 1686 | Scop::MinMaxVectorTy &MinMaxAccesses) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 1687 | |
| 1688 | Accesses = isl_union_map_intersect_domain(Accesses, Domains); |
| 1689 | isl_union_set *Locations = isl_union_map_range(Accesses); |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 1690 | Locations = isl_union_set_coalesce(Locations); |
| 1691 | Locations = isl_union_set_detect_equalities(Locations); |
| 1692 | bool Valid = (0 == isl_union_set_foreach_set(Locations, buildMinMaxAccess, |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 1693 | &MinMaxAccesses)); |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 1694 | isl_union_set_free(Locations); |
| 1695 | return Valid; |
| 1696 | } |
| 1697 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1698 | /// @brief Helper to treat non-affine regions and basic blocks the same. |
| 1699 | /// |
| 1700 | ///{ |
| 1701 | |
| 1702 | /// @brief Return the block that is the representing block for @p RN. |
| 1703 | static inline BasicBlock *getRegionNodeBasicBlock(RegionNode *RN) { |
| 1704 | return RN->isSubRegion() ? RN->getNodeAs<Region>()->getEntry() |
| 1705 | : RN->getNodeAs<BasicBlock>(); |
| 1706 | } |
| 1707 | |
| 1708 | /// @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] | 1709 | static inline BasicBlock * |
| 1710 | getRegionNodeSuccessor(RegionNode *RN, TerminatorInst *TI, unsigned idx) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1711 | if (RN->isSubRegion()) { |
| 1712 | assert(idx == 0); |
| 1713 | return RN->getNodeAs<Region>()->getExit(); |
| 1714 | } |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1715 | return TI->getSuccessor(idx); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | /// @brief Return the smallest loop surrounding @p RN. |
| 1719 | static inline Loop *getRegionNodeLoop(RegionNode *RN, LoopInfo &LI) { |
| 1720 | if (!RN->isSubRegion()) |
| 1721 | return LI.getLoopFor(RN->getNodeAs<BasicBlock>()); |
| 1722 | |
| 1723 | Region *NonAffineSubRegion = RN->getNodeAs<Region>(); |
| 1724 | Loop *L = LI.getLoopFor(NonAffineSubRegion->getEntry()); |
| 1725 | while (L && NonAffineSubRegion->contains(L)) |
| 1726 | L = L->getParentLoop(); |
| 1727 | return L; |
| 1728 | } |
| 1729 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 1730 | static inline unsigned getNumBlocksInRegionNode(RegionNode *RN) { |
| 1731 | if (!RN->isSubRegion()) |
| 1732 | return 1; |
| 1733 | |
| 1734 | unsigned NumBlocks = 0; |
| 1735 | Region *R = RN->getNodeAs<Region>(); |
| 1736 | for (auto BB : R->blocks()) { |
| 1737 | (void)BB; |
| 1738 | NumBlocks++; |
| 1739 | } |
| 1740 | return NumBlocks; |
| 1741 | } |
| 1742 | |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 1743 | static bool containsErrorBlock(RegionNode *RN, const Region &R, LoopInfo &LI, |
| 1744 | const DominatorTree &DT) { |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 1745 | if (!RN->isSubRegion()) |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 1746 | return isErrorBlock(*RN->getNodeAs<BasicBlock>(), R, LI, DT); |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 1747 | for (BasicBlock *BB : RN->getNodeAs<Region>()->blocks()) |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 1748 | if (isErrorBlock(*BB, R, LI, DT)) |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 1749 | return true; |
| 1750 | return false; |
| 1751 | } |
| 1752 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1753 | ///} |
| 1754 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 1755 | static inline __isl_give isl_set *addDomainDimId(__isl_take isl_set *Domain, |
| 1756 | unsigned Dim, Loop *L) { |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 1757 | Domain = isl_set_lower_bound_si(Domain, isl_dim_set, Dim, -1); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 1758 | isl_id *DimId = |
| 1759 | isl_id_alloc(isl_set_get_ctx(Domain), nullptr, static_cast<void *>(L)); |
| 1760 | return isl_set_set_dim_id(Domain, isl_dim_set, Dim, DimId); |
| 1761 | } |
| 1762 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1763 | isl_set *Scop::getDomainConditions(ScopStmt *Stmt) { |
| 1764 | BasicBlock *BB = Stmt->isBlockStmt() ? Stmt->getBasicBlock() |
| 1765 | : Stmt->getRegion()->getEntry(); |
Johannes Doerfert | cef616f | 2015-09-15 22:49:04 +0000 | [diff] [blame] | 1766 | return getDomainConditions(BB); |
| 1767 | } |
| 1768 | |
| 1769 | isl_set *Scop::getDomainConditions(BasicBlock *BB) { |
| 1770 | assert(DomainMap.count(BB) && "Requested BB did not have a domain"); |
Johannes Doerfert | f08bd00 | 2015-08-31 13:56:32 +0000 | [diff] [blame] | 1771 | return isl_set_copy(DomainMap[BB]); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
Johannes Doerfert | d8dd863 | 2015-10-07 20:31:36 +0000 | [diff] [blame] | 1774 | void Scop::buildDomains(Region *R) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1775 | |
Johannes Doerfert | f08bd00 | 2015-08-31 13:56:32 +0000 | [diff] [blame] | 1776 | auto *EntryBB = R->getEntry(); |
| 1777 | int LD = getRelativeLoopDepth(LI.getLoopFor(EntryBB)); |
| 1778 | 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] | 1779 | |
| 1780 | Loop *L = LI.getLoopFor(EntryBB); |
| 1781 | while (LD-- >= 0) { |
| 1782 | S = addDomainDimId(S, LD + 1, L); |
| 1783 | L = L->getParentLoop(); |
| 1784 | } |
| 1785 | |
Johannes Doerfert | f08bd00 | 2015-08-31 13:56:32 +0000 | [diff] [blame] | 1786 | DomainMap[EntryBB] = S; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1787 | |
Johannes Doerfert | 40fa56f | 2015-09-14 11:15:07 +0000 | [diff] [blame] | 1788 | if (SD.isNonAffineSubRegion(R, R)) |
| 1789 | return; |
| 1790 | |
Johannes Doerfert | d8dd863 | 2015-10-07 20:31:36 +0000 | [diff] [blame] | 1791 | buildDomainsWithBranchConstraints(R); |
| 1792 | propagateDomainConstraints(R); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
Johannes Doerfert | d8dd863 | 2015-10-07 20:31:36 +0000 | [diff] [blame] | 1795 | void Scop::buildDomainsWithBranchConstraints(Region *R) { |
Johannes Doerfert | f08bd00 | 2015-08-31 13:56:32 +0000 | [diff] [blame] | 1796 | RegionInfo &RI = *R->getRegionInfo(); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1797 | |
| 1798 | // To create the domain for each block in R we iterate over all blocks and |
| 1799 | // subregions in R and propagate the conditions under which the current region |
| 1800 | // element is executed. To this end we iterate in reverse post order over R as |
| 1801 | // it ensures that we first visit all predecessors of a region node (either a |
| 1802 | // basic block or a subregion) before we visit the region node itself. |
| 1803 | // Initially, only the domain for the SCoP region entry block is set and from |
| 1804 | // there we propagate the current domain to all successors, however we add the |
| 1805 | // condition that the successor is actually executed next. |
| 1806 | // As we are only interested in non-loop carried constraints here we can |
| 1807 | // simply skip loop back edges. |
| 1808 | |
| 1809 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 1810 | for (auto *RN : RTraversal) { |
| 1811 | |
| 1812 | // Recurse for affine subregions but go on for basic blocks and non-affine |
| 1813 | // subregions. |
| 1814 | if (RN->isSubRegion()) { |
| 1815 | Region *SubRegion = RN->getNodeAs<Region>(); |
| 1816 | if (!SD.isNonAffineSubRegion(SubRegion, &getRegion())) { |
Johannes Doerfert | d8dd863 | 2015-10-07 20:31:36 +0000 | [diff] [blame] | 1817 | buildDomainsWithBranchConstraints(SubRegion); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1818 | continue; |
| 1819 | } |
| 1820 | } |
| 1821 | |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 1822 | // Error blocks are assumed not to be executed. Therefor they are not |
| 1823 | // checked properly in the ScopDetection. Any attempt to generate control |
| 1824 | // conditions from them might result in a crash. However, this is only true |
| 1825 | // for the first step of the domain generation (this function) where we |
| 1826 | // push the control conditions of a block to the successors. In the second |
| 1827 | // step (propagateDomainConstraints) we only receive domain constraints from |
| 1828 | // the predecessors and can therefor look at the domain of a error block. |
| 1829 | // That allows us to generate the assumptions needed for them not to be |
| 1830 | // executed at runtime. |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 1831 | if (containsErrorBlock(RN, getRegion(), LI, DT)) |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 1832 | continue; |
| 1833 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1834 | BasicBlock *BB = getRegionNodeBasicBlock(RN); |
Johannes Doerfert | 90db75e | 2015-09-10 17:51:27 +0000 | [diff] [blame] | 1835 | TerminatorInst *TI = BB->getTerminator(); |
| 1836 | |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 1837 | isl_set *Domain = DomainMap.lookup(BB); |
| 1838 | if (!Domain) { |
| 1839 | DEBUG(dbgs() << "\tSkip: " << BB->getName() |
| 1840 | << ", it is only reachable from error blocks.\n"); |
Johannes Doerfert | 90db75e | 2015-09-10 17:51:27 +0000 | [diff] [blame] | 1841 | continue; |
| 1842 | } |
| 1843 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1844 | DEBUG(dbgs() << "\tVisit: " << BB->getName() << " : " << Domain << "\n"); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1845 | |
| 1846 | Loop *BBLoop = getRegionNodeLoop(RN, LI); |
| 1847 | int BBLoopDepth = getRelativeLoopDepth(BBLoop); |
| 1848 | |
| 1849 | // Build the condition sets for the successor nodes of the current region |
| 1850 | // node. If it is a non-affine subregion we will always execute the single |
| 1851 | // exit node, hence the single entry node domain is the condition set. For |
| 1852 | // basic blocks we use the helper function buildConditionSets. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1853 | SmallVector<isl_set *, 8> ConditionSets; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1854 | if (RN->isSubRegion()) |
| 1855 | ConditionSets.push_back(isl_set_copy(Domain)); |
| 1856 | else |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1857 | buildConditionSets(*this, TI, BBLoop, Domain, ConditionSets); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1858 | |
| 1859 | // Now iterate over the successors and set their initial domain based on |
| 1860 | // their condition set. We skip back edges here and have to be careful when |
| 1861 | // we leave a loop not to keep constraints over a dimension that doesn't |
| 1862 | // exist anymore. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1863 | assert(RN->isSubRegion() || TI->getNumSuccessors() == ConditionSets.size()); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1864 | for (unsigned u = 0, e = ConditionSets.size(); u < e; u++) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1865 | isl_set *CondSet = ConditionSets[u]; |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1866 | BasicBlock *SuccBB = getRegionNodeSuccessor(RN, TI, u); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1867 | |
| 1868 | // Skip back edges. |
| 1869 | if (DT.dominates(SuccBB, BB)) { |
| 1870 | isl_set_free(CondSet); |
| 1871 | continue; |
| 1872 | } |
| 1873 | |
Johannes Doerfert | f08bd00 | 2015-08-31 13:56:32 +0000 | [diff] [blame] | 1874 | // Do not adjust the number of dimensions if we enter a boxed loop or are |
| 1875 | // 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] | 1876 | Loop *SuccBBLoop = LI.getLoopFor(SuccBB); |
Johannes Doerfert | f08bd00 | 2015-08-31 13:56:32 +0000 | [diff] [blame] | 1877 | Region *SuccRegion = RI.getRegionFor(SuccBB); |
Johannes Doerfert | 634909c | 2015-10-04 14:57:41 +0000 | [diff] [blame] | 1878 | if (SD.isNonAffineSubRegion(SuccRegion, &getRegion())) |
| 1879 | while (SuccBBLoop && SuccRegion->contains(SuccBBLoop)) |
| 1880 | SuccBBLoop = SuccBBLoop->getParentLoop(); |
| 1881 | |
| 1882 | if (BBLoop != SuccBBLoop) { |
Johannes Doerfert | f08bd00 | 2015-08-31 13:56:32 +0000 | [diff] [blame] | 1883 | |
| 1884 | // Check if the edge to SuccBB is a loop entry or exit edge. If so |
| 1885 | // adjust the dimensionality accordingly. Lastly, if we leave a loop |
| 1886 | // and enter a new one we need to drop the old constraints. |
| 1887 | int SuccBBLoopDepth = getRelativeLoopDepth(SuccBBLoop); |
Johannes Doerfert | f4fa987 | 2015-09-10 15:53:59 +0000 | [diff] [blame] | 1888 | unsigned LoopDepthDiff = std::abs(BBLoopDepth - SuccBBLoopDepth); |
Tobias Grosser | 2df884f | 2015-09-01 18:17:41 +0000 | [diff] [blame] | 1889 | if (BBLoopDepth > SuccBBLoopDepth) { |
Johannes Doerfert | f4fa987 | 2015-09-10 15:53:59 +0000 | [diff] [blame] | 1890 | CondSet = isl_set_project_out(CondSet, isl_dim_set, |
| 1891 | isl_set_n_dim(CondSet) - LoopDepthDiff, |
| 1892 | LoopDepthDiff); |
Tobias Grosser | 2df884f | 2015-09-01 18:17:41 +0000 | [diff] [blame] | 1893 | } else if (SuccBBLoopDepth > BBLoopDepth) { |
Johannes Doerfert | f4fa987 | 2015-09-10 15:53:59 +0000 | [diff] [blame] | 1894 | assert(LoopDepthDiff == 1); |
Johannes Doerfert | f08bd00 | 2015-08-31 13:56:32 +0000 | [diff] [blame] | 1895 | CondSet = isl_set_add_dims(CondSet, isl_dim_set, 1); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 1896 | CondSet = addDomainDimId(CondSet, SuccBBLoopDepth, SuccBBLoop); |
Tobias Grosser | 2df884f | 2015-09-01 18:17:41 +0000 | [diff] [blame] | 1897 | } else if (BBLoopDepth >= 0) { |
Johannes Doerfert | f4fa987 | 2015-09-10 15:53:59 +0000 | [diff] [blame] | 1898 | assert(LoopDepthDiff <= 1); |
Tobias Grosser | 2df884f | 2015-09-01 18:17:41 +0000 | [diff] [blame] | 1899 | CondSet = isl_set_project_out(CondSet, isl_dim_set, BBLoopDepth, 1); |
| 1900 | CondSet = isl_set_add_dims(CondSet, isl_dim_set, 1); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 1901 | CondSet = addDomainDimId(CondSet, SuccBBLoopDepth, SuccBBLoop); |
Tobias Grosser | 2df884f | 2015-09-01 18:17:41 +0000 | [diff] [blame] | 1902 | } |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
| 1905 | // Set the domain for the successor or merge it with an existing domain in |
| 1906 | // case there are multiple paths (without loop back edges) to the |
| 1907 | // successor block. |
| 1908 | isl_set *&SuccDomain = DomainMap[SuccBB]; |
| 1909 | if (!SuccDomain) |
| 1910 | SuccDomain = CondSet; |
| 1911 | else |
| 1912 | SuccDomain = isl_set_union(SuccDomain, CondSet); |
| 1913 | |
| 1914 | SuccDomain = isl_set_coalesce(SuccDomain); |
Johannes Doerfert | 634909c | 2015-10-04 14:57:41 +0000 | [diff] [blame] | 1915 | DEBUG(dbgs() << "\tSet SuccBB: " << SuccBB->getName() << " : " |
| 1916 | << SuccDomain << "\n"); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1917 | } |
| 1918 | } |
| 1919 | } |
| 1920 | |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1921 | /// @brief Return the domain for @p BB wrt @p DomainMap. |
| 1922 | /// |
| 1923 | /// This helper function will lookup @p BB in @p DomainMap but also handle the |
| 1924 | /// case where @p BB is contained in a non-affine subregion using the region |
| 1925 | /// tree obtained by @p RI. |
| 1926 | static __isl_give isl_set * |
| 1927 | getDomainForBlock(BasicBlock *BB, DenseMap<BasicBlock *, isl_set *> &DomainMap, |
| 1928 | RegionInfo &RI) { |
| 1929 | auto DIt = DomainMap.find(BB); |
| 1930 | if (DIt != DomainMap.end()) |
| 1931 | return isl_set_copy(DIt->getSecond()); |
| 1932 | |
| 1933 | Region *R = RI.getRegionFor(BB); |
| 1934 | while (R->getEntry() == BB) |
| 1935 | R = R->getParent(); |
| 1936 | return getDomainForBlock(R->getEntry(), DomainMap, RI); |
| 1937 | } |
| 1938 | |
Johannes Doerfert | d8dd863 | 2015-10-07 20:31:36 +0000 | [diff] [blame] | 1939 | void Scop::propagateDomainConstraints(Region *R) { |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1940 | // Iterate over the region R and propagate the domain constrains from the |
| 1941 | // predecessors to the current node. In contrast to the |
| 1942 | // buildDomainsWithBranchConstraints function, this one will pull the domain |
| 1943 | // information from the predecessors instead of pushing it to the successors. |
| 1944 | // Additionally, we assume the domains to be already present in the domain |
| 1945 | // map here. However, we iterate again in reverse post order so we know all |
| 1946 | // predecessors have been visited before a block or non-affine subregion is |
| 1947 | // visited. |
| 1948 | |
| 1949 | // The set of boxed loops (loops in non-affine subregions) for this SCoP. |
| 1950 | auto &BoxedLoops = *SD.getBoxedLoops(&getRegion()); |
| 1951 | |
| 1952 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 1953 | for (auto *RN : RTraversal) { |
| 1954 | |
| 1955 | // Recurse for affine subregions but go on for basic blocks and non-affine |
| 1956 | // subregions. |
| 1957 | if (RN->isSubRegion()) { |
| 1958 | Region *SubRegion = RN->getNodeAs<Region>(); |
| 1959 | if (!SD.isNonAffineSubRegion(SubRegion, &getRegion())) { |
Johannes Doerfert | d8dd863 | 2015-10-07 20:31:36 +0000 | [diff] [blame] | 1960 | propagateDomainConstraints(SubRegion); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1961 | continue; |
| 1962 | } |
| 1963 | } |
| 1964 | |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 1965 | // Get the domain for the current block and check if it was initialized or |
| 1966 | // not. The only way it was not is if this block is only reachable via error |
| 1967 | // blocks, thus will not be executed under the assumptions we make. Such |
| 1968 | // blocks have to be skipped as their predecessors might not have domains |
| 1969 | // either. It would not benefit us to compute the domain anyway, only the |
| 1970 | // domains of the error blocks that are reachable from non-error blocks |
| 1971 | // are needed to generate assumptions. |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1972 | BasicBlock *BB = getRegionNodeBasicBlock(RN); |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 1973 | isl_set *&Domain = DomainMap[BB]; |
| 1974 | if (!Domain) { |
| 1975 | DEBUG(dbgs() << "\tSkip: " << BB->getName() |
| 1976 | << ", it is only reachable from error blocks.\n"); |
| 1977 | DomainMap.erase(BB); |
| 1978 | continue; |
| 1979 | } |
| 1980 | DEBUG(dbgs() << "\tVisit: " << BB->getName() << " : " << Domain << "\n"); |
| 1981 | |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1982 | Loop *BBLoop = getRegionNodeLoop(RN, LI); |
| 1983 | int BBLoopDepth = getRelativeLoopDepth(BBLoop); |
| 1984 | |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1985 | isl_set *PredDom = isl_set_empty(isl_set_get_space(Domain)); |
| 1986 | for (auto *PredBB : predecessors(BB)) { |
| 1987 | |
| 1988 | // Skip backedges |
| 1989 | if (DT.dominates(BB, PredBB)) |
| 1990 | continue; |
| 1991 | |
| 1992 | isl_set *PredBBDom = nullptr; |
| 1993 | |
| 1994 | // Handle the SCoP entry block with its outside predecessors. |
| 1995 | if (!getRegion().contains(PredBB)) |
| 1996 | PredBBDom = isl_set_universe(isl_set_get_space(PredDom)); |
| 1997 | |
| 1998 | if (!PredBBDom) { |
| 1999 | // Determine the loop depth of the predecessor and adjust its domain to |
| 2000 | // the domain of the current block. This can mean we have to: |
| 2001 | // o) Drop a dimension if this block is the exit of a loop, not the |
| 2002 | // header of a new loop and the predecessor was part of the loop. |
| 2003 | // o) Add an unconstrainted new dimension if this block is the header |
| 2004 | // of a loop and the predecessor is not part of it. |
| 2005 | // o) Drop the information about the innermost loop dimension when the |
| 2006 | // predecessor and the current block are surrounded by different |
| 2007 | // loops in the same depth. |
| 2008 | PredBBDom = getDomainForBlock(PredBB, DomainMap, *R->getRegionInfo()); |
| 2009 | Loop *PredBBLoop = LI.getLoopFor(PredBB); |
| 2010 | while (BoxedLoops.count(PredBBLoop)) |
| 2011 | PredBBLoop = PredBBLoop->getParentLoop(); |
| 2012 | |
| 2013 | int PredBBLoopDepth = getRelativeLoopDepth(PredBBLoop); |
Johannes Doerfert | f4fa987 | 2015-09-10 15:53:59 +0000 | [diff] [blame] | 2014 | unsigned LoopDepthDiff = std::abs(BBLoopDepth - PredBBLoopDepth); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2015 | if (BBLoopDepth < PredBBLoopDepth) |
Johannes Doerfert | f4fa987 | 2015-09-10 15:53:59 +0000 | [diff] [blame] | 2016 | PredBBDom = isl_set_project_out( |
| 2017 | PredBBDom, isl_dim_set, isl_set_n_dim(PredBBDom) - LoopDepthDiff, |
| 2018 | LoopDepthDiff); |
| 2019 | else if (PredBBLoopDepth < BBLoopDepth) { |
| 2020 | assert(LoopDepthDiff == 1); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2021 | PredBBDom = isl_set_add_dims(PredBBDom, isl_dim_set, 1); |
Johannes Doerfert | f4fa987 | 2015-09-10 15:53:59 +0000 | [diff] [blame] | 2022 | } else if (BBLoop != PredBBLoop && BBLoopDepth >= 0) { |
| 2023 | assert(LoopDepthDiff <= 1); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2024 | PredBBDom = isl_set_drop_constraints_involving_dims( |
| 2025 | PredBBDom, isl_dim_set, BBLoopDepth, 1); |
Johannes Doerfert | f4fa987 | 2015-09-10 15:53:59 +0000 | [diff] [blame] | 2026 | } |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2027 | } |
| 2028 | |
| 2029 | PredDom = isl_set_union(PredDom, PredBBDom); |
| 2030 | } |
| 2031 | |
| 2032 | // Under the union of all predecessor conditions we can reach this block. |
Johannes Doerfert | b20f151 | 2015-09-15 22:11:49 +0000 | [diff] [blame] | 2033 | Domain = isl_set_coalesce(isl_set_intersect(Domain, PredDom)); |
Johannes Doerfert | 90db75e | 2015-09-10 17:51:27 +0000 | [diff] [blame] | 2034 | |
Johannes Doerfert | f32f5f2 | 2015-09-28 01:30:37 +0000 | [diff] [blame] | 2035 | if (BBLoop && BBLoop->getHeader() == BB && getRegion().contains(BBLoop)) |
Johannes Doerfert | d8dd863 | 2015-10-07 20:31:36 +0000 | [diff] [blame] | 2036 | addLoopBoundsToHeaderDomain(BBLoop); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2037 | |
Johannes Doerfert | 90db75e | 2015-09-10 17:51:27 +0000 | [diff] [blame] | 2038 | // Add assumptions for error blocks. |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 2039 | if (containsErrorBlock(RN, getRegion(), LI, DT)) { |
Johannes Doerfert | 90db75e | 2015-09-10 17:51:27 +0000 | [diff] [blame] | 2040 | IsOptimized = true; |
| 2041 | isl_set *DomPar = isl_set_params(isl_set_copy(Domain)); |
| 2042 | addAssumption(isl_set_complement(DomPar)); |
| 2043 | } |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2044 | } |
| 2045 | } |
| 2046 | |
| 2047 | /// @brief Create a map from SetSpace -> SetSpace where the dimensions @p Dim |
| 2048 | /// is incremented by one and all other dimensions are equal, e.g., |
| 2049 | /// [i0, i1, i2, i3] -> [i0, i1, i2 + 1, i3] |
| 2050 | /// if @p Dim is 2 and @p SetSpace has 4 dimensions. |
| 2051 | static __isl_give isl_map * |
| 2052 | createNextIterationMap(__isl_take isl_space *SetSpace, unsigned Dim) { |
| 2053 | auto *MapSpace = isl_space_map_from_set(SetSpace); |
| 2054 | auto *NextIterationMap = isl_map_universe(isl_space_copy(MapSpace)); |
| 2055 | for (unsigned u = 0; u < isl_map_n_in(NextIterationMap); u++) |
| 2056 | if (u != Dim) |
| 2057 | NextIterationMap = |
| 2058 | isl_map_equate(NextIterationMap, isl_dim_in, u, isl_dim_out, u); |
| 2059 | auto *C = isl_constraint_alloc_equality(isl_local_space_from_space(MapSpace)); |
| 2060 | C = isl_constraint_set_constant_si(C, 1); |
| 2061 | C = isl_constraint_set_coefficient_si(C, isl_dim_in, Dim, 1); |
| 2062 | C = isl_constraint_set_coefficient_si(C, isl_dim_out, Dim, -1); |
| 2063 | NextIterationMap = isl_map_add_constraint(NextIterationMap, C); |
| 2064 | return NextIterationMap; |
| 2065 | } |
| 2066 | |
Johannes Doerfert | d8dd863 | 2015-10-07 20:31:36 +0000 | [diff] [blame] | 2067 | void Scop::addLoopBoundsToHeaderDomain(Loop *L) { |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2068 | int LoopDepth = getRelativeLoopDepth(L); |
| 2069 | assert(LoopDepth >= 0 && "Loop in region should have at least depth one"); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2070 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2071 | BasicBlock *HeaderBB = L->getHeader(); |
| 2072 | assert(DomainMap.count(HeaderBB)); |
| 2073 | isl_set *&HeaderBBDom = DomainMap[HeaderBB]; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2074 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2075 | isl_map *NextIterationMap = |
| 2076 | createNextIterationMap(isl_set_get_space(HeaderBBDom), LoopDepth); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2077 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2078 | isl_set *UnionBackedgeCondition = |
| 2079 | isl_set_empty(isl_set_get_space(HeaderBBDom)); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2080 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2081 | SmallVector<llvm::BasicBlock *, 4> LatchBlocks; |
| 2082 | L->getLoopLatches(LatchBlocks); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2083 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2084 | for (BasicBlock *LatchBB : LatchBlocks) { |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2085 | |
| 2086 | // If the latch is only reachable via error statements we skip it. |
| 2087 | isl_set *LatchBBDom = DomainMap.lookup(LatchBB); |
| 2088 | if (!LatchBBDom) |
| 2089 | continue; |
| 2090 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2091 | isl_set *BackedgeCondition = nullptr; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2092 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2093 | TerminatorInst *TI = LatchBB->getTerminator(); |
| 2094 | BranchInst *BI = dyn_cast<BranchInst>(TI); |
| 2095 | if (BI && BI->isUnconditional()) |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2096 | BackedgeCondition = isl_set_copy(LatchBBDom); |
| 2097 | else { |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2098 | SmallVector<isl_set *, 8> ConditionSets; |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2099 | int idx = BI->getSuccessor(0) != HeaderBB; |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2100 | buildConditionSets(*this, TI, L, LatchBBDom, ConditionSets); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2101 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2102 | // Free the non back edge condition set as we do not need it. |
| 2103 | isl_set_free(ConditionSets[1 - idx]); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2104 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2105 | BackedgeCondition = ConditionSets[idx]; |
Johannes Doerfert | 06c57b5 | 2015-09-20 15:00:20 +0000 | [diff] [blame] | 2106 | } |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2107 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2108 | int LatchLoopDepth = getRelativeLoopDepth(LI.getLoopFor(LatchBB)); |
| 2109 | assert(LatchLoopDepth >= LoopDepth); |
| 2110 | BackedgeCondition = |
| 2111 | isl_set_project_out(BackedgeCondition, isl_dim_set, LoopDepth + 1, |
| 2112 | LatchLoopDepth - LoopDepth); |
| 2113 | UnionBackedgeCondition = |
| 2114 | isl_set_union(UnionBackedgeCondition, BackedgeCondition); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2115 | } |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2116 | |
| 2117 | isl_map *ForwardMap = isl_map_lex_le(isl_set_get_space(HeaderBBDom)); |
| 2118 | for (int i = 0; i < LoopDepth; i++) |
| 2119 | ForwardMap = isl_map_equate(ForwardMap, isl_dim_in, i, isl_dim_out, i); |
| 2120 | |
| 2121 | isl_set *UnionBackedgeConditionComplement = |
| 2122 | isl_set_complement(UnionBackedgeCondition); |
| 2123 | UnionBackedgeConditionComplement = isl_set_lower_bound_si( |
| 2124 | UnionBackedgeConditionComplement, isl_dim_set, LoopDepth, 0); |
| 2125 | UnionBackedgeConditionComplement = |
| 2126 | isl_set_apply(UnionBackedgeConditionComplement, ForwardMap); |
| 2127 | HeaderBBDom = isl_set_subtract(HeaderBBDom, UnionBackedgeConditionComplement); |
| 2128 | HeaderBBDom = isl_set_apply(HeaderBBDom, NextIterationMap); |
| 2129 | |
| 2130 | auto Parts = partitionSetParts(HeaderBBDom, LoopDepth); |
| 2131 | HeaderBBDom = Parts.second; |
| 2132 | |
Johannes Doerfert | 6a72a2a | 2015-09-20 16:59:23 +0000 | [diff] [blame] | 2133 | // Check if there is a <nsw> tagged AddRec for this loop and if so do not add |
| 2134 | // the bounded assumptions to the context as they are already implied by the |
| 2135 | // <nsw> tag. |
| 2136 | if (Affinator.hasNSWAddRecForLoop(L)) { |
| 2137 | isl_set_free(Parts.first); |
| 2138 | return; |
| 2139 | } |
| 2140 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2141 | isl_set *UnboundedCtx = isl_set_params(Parts.first); |
| 2142 | isl_set *BoundedCtx = isl_set_complement(UnboundedCtx); |
Johannes Doerfert | 707a406 | 2015-09-20 16:38:19 +0000 | [diff] [blame] | 2143 | addAssumption(BoundedCtx); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2144 | } |
| 2145 | |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 2146 | void Scop::buildAliasChecks(AliasAnalysis &AA) { |
| 2147 | if (!PollyUseRuntimeAliasChecks) |
| 2148 | return; |
| 2149 | |
| 2150 | if (buildAliasGroups(AA)) |
| 2151 | return; |
| 2152 | |
| 2153 | // If a problem occurs while building the alias groups we need to delete |
| 2154 | // this SCoP and pretend it wasn't valid in the first place. To this end |
| 2155 | // we make the assumed context infeasible. |
| 2156 | addAssumption(isl_set_empty(getParamSpace())); |
| 2157 | |
| 2158 | DEBUG(dbgs() << "\n\nNOTE: Run time checks for " << getNameStr() |
| 2159 | << " could not be created as the number of parameters involved " |
| 2160 | "is too high. The SCoP will be " |
| 2161 | "dismissed.\nUse:\n\t--polly-rtc-max-parameters=X\nto adjust " |
| 2162 | "the maximal number of parameters but be advised that the " |
| 2163 | "compile time might increase exponentially.\n\n"); |
| 2164 | } |
| 2165 | |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 2166 | bool Scop::buildAliasGroups(AliasAnalysis &AA) { |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2167 | // To create sound alias checks we perform the following steps: |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 2168 | // 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] | 2169 | // for all memory accesses inside the SCoP. |
| 2170 | // 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] | 2171 | // memory accesses we know, thus obtain groups of memory accesses which |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2172 | // might alias. |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 2173 | // 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] | 2174 | // accesses. That means two minimal/maximal accesses are only in a group |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 2175 | // if their access domains intersect, otherwise they are in different |
| 2176 | // ones. |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2177 | // 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] | 2178 | // 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] | 2179 | // and maximal accesses to each array of a group in read only and non |
| 2180 | // read only partitions separately. |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2181 | using AliasGroupTy = SmallVector<MemoryAccess *, 4>; |
| 2182 | |
| 2183 | AliasSetTracker AST(AA); |
| 2184 | |
| 2185 | DenseMap<Value *, MemoryAccess *> PtrToAcc; |
Johannes Doerfert | 1377173 | 2014-10-01 12:40:46 +0000 | [diff] [blame] | 2186 | DenseSet<Value *> HasWriteAccess; |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 2187 | for (ScopStmt &Stmt : *this) { |
Johannes Doerfert | f1ee262 | 2014-10-06 17:43:00 +0000 | [diff] [blame] | 2188 | |
| 2189 | // Skip statements with an empty domain as they will never be executed. |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 2190 | isl_set *StmtDomain = Stmt.getDomain(); |
Johannes Doerfert | f1ee262 | 2014-10-06 17:43:00 +0000 | [diff] [blame] | 2191 | bool StmtDomainEmpty = isl_set_is_empty(StmtDomain); |
| 2192 | isl_set_free(StmtDomain); |
| 2193 | if (StmtDomainEmpty) |
| 2194 | continue; |
| 2195 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 2196 | for (MemoryAccess *MA : Stmt) { |
Michael Kruse | 8d0b734 | 2015-09-25 21:21:00 +0000 | [diff] [blame] | 2197 | if (MA->isImplicit()) |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2198 | continue; |
Johannes Doerfert | 1377173 | 2014-10-01 12:40:46 +0000 | [diff] [blame] | 2199 | if (!MA->isRead()) |
| 2200 | HasWriteAccess.insert(MA->getBaseAddr()); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2201 | Instruction *Acc = MA->getAccessInstruction(); |
| 2202 | PtrToAcc[getPointerOperand(*Acc)] = MA; |
| 2203 | AST.add(Acc); |
| 2204 | } |
| 2205 | } |
| 2206 | |
| 2207 | SmallVector<AliasGroupTy, 4> AliasGroups; |
| 2208 | for (AliasSet &AS : AST) { |
Johannes Doerfert | 74f6869 | 2014-10-08 02:23:48 +0000 | [diff] [blame] | 2209 | if (AS.isMustAlias() || AS.isForwardingAliasSet()) |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2210 | continue; |
| 2211 | AliasGroupTy AG; |
| 2212 | for (auto PR : AS) |
| 2213 | AG.push_back(PtrToAcc[PR.getValue()]); |
| 2214 | assert(AG.size() > 1 && |
| 2215 | "Alias groups should contain at least two accesses"); |
| 2216 | AliasGroups.push_back(std::move(AG)); |
| 2217 | } |
| 2218 | |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 2219 | // Split the alias groups based on their domain. |
| 2220 | for (unsigned u = 0; u < AliasGroups.size(); u++) { |
| 2221 | AliasGroupTy NewAG; |
| 2222 | AliasGroupTy &AG = AliasGroups[u]; |
| 2223 | AliasGroupTy::iterator AGI = AG.begin(); |
| 2224 | isl_set *AGDomain = getAccessDomain(*AGI); |
| 2225 | while (AGI != AG.end()) { |
| 2226 | MemoryAccess *MA = *AGI; |
| 2227 | isl_set *MADomain = getAccessDomain(MA); |
| 2228 | if (isl_set_is_disjoint(AGDomain, MADomain)) { |
| 2229 | NewAG.push_back(MA); |
| 2230 | AGI = AG.erase(AGI); |
| 2231 | isl_set_free(MADomain); |
| 2232 | } else { |
| 2233 | AGDomain = isl_set_union(AGDomain, MADomain); |
| 2234 | AGI++; |
| 2235 | } |
| 2236 | } |
| 2237 | if (NewAG.size() > 1) |
| 2238 | AliasGroups.push_back(std::move(NewAG)); |
| 2239 | isl_set_free(AGDomain); |
| 2240 | } |
| 2241 | |
Tobias Grosser | f4c24b2 | 2015-04-05 13:11:54 +0000 | [diff] [blame] | 2242 | MapVector<const Value *, SmallPtrSet<MemoryAccess *, 8>> ReadOnlyPairs; |
Johannes Doerfert | 1377173 | 2014-10-01 12:40:46 +0000 | [diff] [blame] | 2243 | SmallPtrSet<const Value *, 4> NonReadOnlyBaseValues; |
| 2244 | for (AliasGroupTy &AG : AliasGroups) { |
| 2245 | NonReadOnlyBaseValues.clear(); |
| 2246 | ReadOnlyPairs.clear(); |
| 2247 | |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 2248 | if (AG.size() < 2) { |
| 2249 | AG.clear(); |
| 2250 | continue; |
| 2251 | } |
| 2252 | |
Johannes Doerfert | 1377173 | 2014-10-01 12:40:46 +0000 | [diff] [blame] | 2253 | for (auto II = AG.begin(); II != AG.end();) { |
| 2254 | Value *BaseAddr = (*II)->getBaseAddr(); |
| 2255 | if (HasWriteAccess.count(BaseAddr)) { |
| 2256 | NonReadOnlyBaseValues.insert(BaseAddr); |
| 2257 | II++; |
| 2258 | } else { |
| 2259 | ReadOnlyPairs[BaseAddr].insert(*II); |
| 2260 | II = AG.erase(II); |
| 2261 | } |
| 2262 | } |
| 2263 | |
| 2264 | // If we don't have read only pointers check if there are at least two |
| 2265 | // non read only pointers, otherwise clear the alias group. |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 2266 | if (ReadOnlyPairs.empty() && NonReadOnlyBaseValues.size() <= 1) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2267 | AG.clear(); |
Johannes Doerfert | 1377173 | 2014-10-01 12:40:46 +0000 | [diff] [blame] | 2268 | continue; |
| 2269 | } |
| 2270 | |
| 2271 | // If we don't have non read only pointers clear the alias group. |
| 2272 | if (NonReadOnlyBaseValues.empty()) { |
| 2273 | AG.clear(); |
| 2274 | continue; |
| 2275 | } |
| 2276 | |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2277 | // Calculate minimal and maximal accesses for non read only accesses. |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 2278 | MinMaxAliasGroups.emplace_back(); |
| 2279 | MinMaxVectorPairTy &pair = MinMaxAliasGroups.back(); |
| 2280 | MinMaxVectorTy &MinMaxAccessesNonReadOnly = pair.first; |
| 2281 | MinMaxVectorTy &MinMaxAccessesReadOnly = pair.second; |
| 2282 | MinMaxAccessesNonReadOnly.reserve(AG.size()); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2283 | |
| 2284 | isl_union_map *Accesses = isl_union_map_empty(getParamSpace()); |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2285 | |
| 2286 | // AG contains only non read only accesses. |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2287 | for (MemoryAccess *MA : AG) |
| 2288 | Accesses = isl_union_map_add_map(Accesses, MA->getAccessRelation()); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2289 | |
Tobias Grosser | daaed0e | 2015-08-20 21:29:26 +0000 | [diff] [blame] | 2290 | bool Valid = calculateMinMaxAccess(Accesses, getDomains(), |
| 2291 | MinMaxAccessesNonReadOnly); |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2292 | |
| 2293 | // Bail out if the number of values we need to compare is too large. |
| 2294 | // This is important as the number of comparisions grows quadratically with |
| 2295 | // the number of values we need to compare. |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 2296 | if (!Valid || (MinMaxAccessesNonReadOnly.size() + !ReadOnlyPairs.empty() > |
| 2297 | RunTimeChecksMaxArraysPerGroup)) |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2298 | return false; |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2299 | |
| 2300 | // Calculate minimal and maximal accesses for read only accesses. |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 2301 | MinMaxAccessesReadOnly.reserve(ReadOnlyPairs.size()); |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2302 | Accesses = isl_union_map_empty(getParamSpace()); |
| 2303 | |
| 2304 | for (const auto &ReadOnlyPair : ReadOnlyPairs) |
| 2305 | for (MemoryAccess *MA : ReadOnlyPair.second) |
| 2306 | Accesses = isl_union_map_add_map(Accesses, MA->getAccessRelation()); |
| 2307 | |
Tobias Grosser | daaed0e | 2015-08-20 21:29:26 +0000 | [diff] [blame] | 2308 | Valid = |
| 2309 | calculateMinMaxAccess(Accesses, getDomains(), MinMaxAccessesReadOnly); |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 2310 | |
| 2311 | if (!Valid) |
Tobias Grosser | 50d4e2e | 2015-03-28 14:50:32 +0000 | [diff] [blame] | 2312 | return false; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2313 | } |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 2314 | |
Tobias Grosser | 50d4e2e | 2015-03-28 14:50:32 +0000 | [diff] [blame] | 2315 | return true; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2318 | static Loop *getLoopSurroundingRegion(Region &R, LoopInfo &LI) { |
| 2319 | Loop *L = LI.getLoopFor(R.getEntry()); |
| 2320 | return L ? (R.contains(L) ? L->getParentLoop() : L) : nullptr; |
| 2321 | } |
| 2322 | |
Johannes Doerfert | f8206cf | 2015-04-12 22:58:40 +0000 | [diff] [blame] | 2323 | static unsigned getMaxLoopDepthInRegion(const Region &R, LoopInfo &LI, |
| 2324 | ScopDetection &SD) { |
| 2325 | |
| 2326 | const ScopDetection::BoxedLoopsSetTy *BoxedLoops = SD.getBoxedLoops(&R); |
| 2327 | |
Johannes Doerfert | e3da05a | 2014-11-01 00:12:13 +0000 | [diff] [blame] | 2328 | unsigned MinLD = INT_MAX, MaxLD = 0; |
| 2329 | for (BasicBlock *BB : R.blocks()) { |
| 2330 | if (Loop *L = LI.getLoopFor(BB)) { |
David Peixotto | dc0a11c | 2015-01-13 18:31:55 +0000 | [diff] [blame] | 2331 | if (!R.contains(L)) |
| 2332 | continue; |
Johannes Doerfert | f8206cf | 2015-04-12 22:58:40 +0000 | [diff] [blame] | 2333 | if (BoxedLoops && BoxedLoops->count(L)) |
| 2334 | continue; |
Johannes Doerfert | e3da05a | 2014-11-01 00:12:13 +0000 | [diff] [blame] | 2335 | unsigned LD = L->getLoopDepth(); |
| 2336 | MinLD = std::min(MinLD, LD); |
| 2337 | MaxLD = std::max(MaxLD, LD); |
| 2338 | } |
| 2339 | } |
| 2340 | |
| 2341 | // Handle the case that there is no loop in the SCoP first. |
| 2342 | if (MaxLD == 0) |
| 2343 | return 1; |
| 2344 | |
| 2345 | assert(MinLD >= 1 && "Minimal loop depth should be at least one"); |
| 2346 | assert(MaxLD >= MinLD && |
| 2347 | "Maximal loop depth was smaller than mininaml loop depth?"); |
| 2348 | return MaxLD - MinLD + 1; |
| 2349 | } |
| 2350 | |
Johannes Doerfert | 478a7de | 2015-10-02 13:09:31 +0000 | [diff] [blame] | 2351 | Scop::Scop(Region &R, AccFuncMapType &AccFuncMap, ScopDetection &SD, |
Johannes Doerfert | d8dd863 | 2015-10-07 20:31:36 +0000 | [diff] [blame] | 2352 | ScalarEvolution &ScalarEvolution, DominatorTree &DT, LoopInfo &LI, |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2353 | isl_ctx *Context, unsigned MaxLoopDepth) |
Johannes Doerfert | d8dd863 | 2015-10-07 20:31:36 +0000 | [diff] [blame] | 2354 | : LI(LI), DT(DT), SE(&ScalarEvolution), SD(SD), R(R), |
| 2355 | AccFuncMap(AccFuncMap), IsOptimized(false), |
| 2356 | HasSingleExitEdge(R.getExitingBlock()), MaxLoopDepth(MaxLoopDepth), |
| 2357 | IslCtx(Context), Context(nullptr), Affinator(this), |
| 2358 | AssumedContext(nullptr), BoundaryContext(nullptr), Schedule(nullptr) {} |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 2359 | |
Johannes Doerfert | d8dd863 | 2015-10-07 20:31:36 +0000 | [diff] [blame] | 2360 | void Scop::init(AliasAnalysis &AA) { |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2361 | buildContext(); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2362 | buildInvariantEquivalenceClasses(); |
| 2363 | |
Johannes Doerfert | d8dd863 | 2015-10-07 20:31:36 +0000 | [diff] [blame] | 2364 | buildDomains(&R); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2365 | |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 2366 | // Remove empty and ignored statements. |
Michael Kruse | afe0670 | 2015-10-02 16:33:27 +0000 | [diff] [blame] | 2367 | // 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] | 2368 | simplifySCoP(true); |
Michael Kruse | afe0670 | 2015-10-02 16:33:27 +0000 | [diff] [blame] | 2369 | if (Stmts.empty()) |
| 2370 | return; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2371 | |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 2372 | // The ScopStmts now have enough information to initialize themselves. |
| 2373 | for (ScopStmt &Stmt : Stmts) |
| 2374 | Stmt.init(); |
| 2375 | |
| 2376 | DenseMap<Loop *, std::pair<isl_schedule *, unsigned>> LoopSchedules; |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2377 | Loop *L = getLoopSurroundingRegion(R, LI); |
| 2378 | LoopSchedules[L]; |
Johannes Doerfert | d8dd863 | 2015-10-07 20:31:36 +0000 | [diff] [blame] | 2379 | buildSchedule(&R, LoopSchedules); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 2380 | updateAccessDimensionality(); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2381 | Schedule = LoopSchedules[L].first; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2382 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 2383 | realignParams(); |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 2384 | addParameterBounds(); |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2385 | addUserContext(); |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 2386 | buildBoundaryContext(); |
| 2387 | simplifyContexts(); |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 2388 | buildAliasChecks(AA); |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 2389 | |
| 2390 | hoistInvariantLoads(); |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 2391 | simplifySCoP(false); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
| 2394 | Scop::~Scop() { |
| 2395 | isl_set_free(Context); |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 2396 | isl_set_free(AssumedContext); |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 2397 | isl_set_free(BoundaryContext); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 2398 | isl_schedule_free(Schedule); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2399 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2400 | for (auto It : DomainMap) |
| 2401 | isl_set_free(It.second); |
| 2402 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2403 | // Free the alias groups |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2404 | for (MinMaxVectorPairTy &MinMaxAccessPair : MinMaxAliasGroups) { |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 2405 | for (MinMaxAccessTy &MMA : MinMaxAccessPair.first) { |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2406 | isl_pw_multi_aff_free(MMA.first); |
| 2407 | isl_pw_multi_aff_free(MMA.second); |
| 2408 | } |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 2409 | for (MinMaxAccessTy &MMA : MinMaxAccessPair.second) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2410 | isl_pw_multi_aff_free(MMA.first); |
| 2411 | isl_pw_multi_aff_free(MMA.second); |
| 2412 | } |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2413 | } |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 2414 | |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2415 | for (const auto &IAClass : InvariantEquivClasses) |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 2416 | isl_set_free(std::get<2>(IAClass)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2417 | } |
| 2418 | |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 2419 | void Scop::updateAccessDimensionality() { |
| 2420 | for (auto &Stmt : *this) |
| 2421 | for (auto &Access : Stmt) |
| 2422 | Access->updateDimensionality(); |
| 2423 | } |
| 2424 | |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 2425 | void Scop::simplifySCoP(bool RemoveIgnoredStmts) { |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 2426 | for (auto StmtIt = Stmts.begin(), StmtEnd = Stmts.end(); StmtIt != StmtEnd;) { |
| 2427 | ScopStmt &Stmt = *StmtIt; |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 2428 | RegionNode *RN = Stmt.isRegionStmt() |
| 2429 | ? Stmt.getRegion()->getNode() |
| 2430 | : getRegion().getBBNode(Stmt.getBasicBlock()); |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 2431 | |
Johannes Doerfert | f17a78e | 2015-10-04 15:00:05 +0000 | [diff] [blame] | 2432 | if (StmtIt->isEmpty() || |
| 2433 | isl_set_is_empty(DomainMap[getRegionNodeBasicBlock(RN)]) || |
| 2434 | (RemoveIgnoredStmts && isIgnored(RN))) { |
| 2435 | |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 2436 | // Remove the statement because it is unnecessary. |
| 2437 | if (Stmt.isRegionStmt()) |
| 2438 | for (BasicBlock *BB : Stmt.getRegion()->blocks()) |
| 2439 | StmtMap.erase(BB); |
| 2440 | else |
| 2441 | StmtMap.erase(Stmt.getBasicBlock()); |
| 2442 | |
| 2443 | StmtIt = Stmts.erase(StmtIt); |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 2444 | continue; |
| 2445 | } |
| 2446 | |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 2447 | StmtIt++; |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 2448 | } |
| 2449 | } |
| 2450 | |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 2451 | const InvariantEquivClassTy *Scop::lookupInvariantEquivClass(Value *Val) const { |
| 2452 | LoadInst *LInst = dyn_cast<LoadInst>(Val); |
| 2453 | if (!LInst) |
| 2454 | return nullptr; |
| 2455 | |
| 2456 | if (Value *Rep = InvEquivClassVMap.lookup(LInst)) |
| 2457 | LInst = cast<LoadInst>(Rep); |
| 2458 | |
| 2459 | const SCEV *PointerSCEV = SE->getSCEV(LInst->getPointerOperand()); |
| 2460 | for (auto &IAClass : InvariantEquivClasses) |
| 2461 | if (PointerSCEV == std::get<0>(IAClass)) |
| 2462 | return &IAClass; |
| 2463 | |
| 2464 | return nullptr; |
| 2465 | } |
| 2466 | |
| 2467 | void Scop::addInvariantLoads(ScopStmt &Stmt, MemoryAccessList &InvMAs) { |
| 2468 | |
| 2469 | // Get the context under which the statement is executed. |
| 2470 | isl_set *DomainCtx = isl_set_params(Stmt.getDomain()); |
| 2471 | DomainCtx = isl_set_remove_redundancies(DomainCtx); |
| 2472 | DomainCtx = isl_set_detect_equalities(DomainCtx); |
| 2473 | DomainCtx = isl_set_coalesce(DomainCtx); |
| 2474 | |
| 2475 | // Project out all parameters that relate to loads in the statement. Otherwise |
| 2476 | // we could have cyclic dependences on the constraints under which the |
| 2477 | // hoisted loads are executed and we could not determine an order in which to |
| 2478 | // pre-load them. This happens because not only lower bounds are part of the |
| 2479 | // domain but also upper bounds. |
| 2480 | for (MemoryAccess *MA : InvMAs) { |
| 2481 | Instruction *AccInst = MA->getAccessInstruction(); |
| 2482 | if (SE->isSCEVable(AccInst->getType())) { |
| 2483 | isl_id *ParamId = getIdForParam(SE->getSCEV(AccInst)); |
| 2484 | if (ParamId) { |
| 2485 | int Dim = isl_set_find_dim_by_id(DomainCtx, isl_dim_param, ParamId); |
| 2486 | DomainCtx = isl_set_eliminate(DomainCtx, isl_dim_param, Dim, 1); |
| 2487 | } |
| 2488 | isl_id_free(ParamId); |
| 2489 | } |
| 2490 | } |
| 2491 | |
| 2492 | for (MemoryAccess *MA : InvMAs) { |
| 2493 | // Check for another invariant access that accesses the same location as |
| 2494 | // MA and if found consolidate them. Otherwise create a new equivalence |
| 2495 | // class at the end of InvariantEquivClasses. |
| 2496 | LoadInst *LInst = cast<LoadInst>(MA->getAccessInstruction()); |
| 2497 | const SCEV *PointerSCEV = SE->getSCEV(LInst->getPointerOperand()); |
| 2498 | |
| 2499 | bool Consolidated = false; |
| 2500 | for (auto &IAClass : InvariantEquivClasses) { |
| 2501 | if (PointerSCEV != std::get<0>(IAClass)) |
| 2502 | continue; |
| 2503 | |
| 2504 | Consolidated = true; |
| 2505 | |
| 2506 | // Add MA to the list of accesses that are in this class. |
| 2507 | auto &MAs = std::get<1>(IAClass); |
| 2508 | MAs.push_front(MA); |
| 2509 | |
| 2510 | // Unify the execution context of the class and this statement. |
| 2511 | isl_set *&IAClassDomainCtx = std::get<2>(IAClass); |
| 2512 | IAClassDomainCtx = isl_set_coalesce( |
| 2513 | isl_set_union(IAClassDomainCtx, isl_set_copy(DomainCtx))); |
| 2514 | break; |
| 2515 | } |
| 2516 | |
| 2517 | if (Consolidated) |
| 2518 | continue; |
| 2519 | |
| 2520 | // If we did not consolidate MA, thus did not find an equivalence class |
| 2521 | // for it, we create a new one. |
| 2522 | InvariantEquivClasses.emplace_back(PointerSCEV, MemoryAccessList{MA}, |
| 2523 | isl_set_copy(DomainCtx)); |
| 2524 | } |
| 2525 | |
| 2526 | isl_set_free(DomainCtx); |
| 2527 | } |
| 2528 | |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 2529 | void Scop::hoistInvariantLoads() { |
| 2530 | isl_union_map *Writes = getWrites(); |
| 2531 | for (ScopStmt &Stmt : *this) { |
| 2532 | |
| 2533 | // TODO: Loads that are not loop carried, hence are in a statement with |
| 2534 | // zero iterators, are by construction invariant, though we |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 2535 | // currently "hoist" them anyway. This is necessary because we allow |
| 2536 | // them to be treated as parameters (e.g., in conditions) and our code |
| 2537 | // generation would otherwise use the old value. |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 2538 | |
Johannes Doerfert | 8930f48 | 2015-10-02 14:51:00 +0000 | [diff] [blame] | 2539 | BasicBlock *BB = Stmt.isBlockStmt() ? Stmt.getBasicBlock() |
| 2540 | : Stmt.getRegion()->getEntry(); |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 2541 | isl_set *Domain = Stmt.getDomain(); |
| 2542 | MemoryAccessList InvMAs; |
| 2543 | |
| 2544 | for (MemoryAccess *MA : Stmt) { |
| 2545 | if (MA->isImplicit() || MA->isWrite() || !MA->isAffine()) |
| 2546 | continue; |
| 2547 | |
Johannes Doerfert | bc7cff4 | 2015-10-18 19:49:25 +0000 | [diff] [blame] | 2548 | // Skip accesses that have an invariant base pointer which is defined but |
| 2549 | // not loaded inside the SCoP. This can happened e.g., if a readnone call |
| 2550 | // returns a pointer that is used as a base address. However, as we want |
| 2551 | // to hoist indirect pointers, we allow the base pointer to be defined in |
Johannes Doerfert | 654c328 | 2015-10-21 22:14:57 +0000 | [diff] [blame] | 2552 | // the region if it is also a memory access. Each ScopArrayInfo object |
| 2553 | // that has a base pointer origin has a base pointer that is loaded and |
| 2554 | // that it is invariant, thus it will be hoisted too. However, if there is |
| 2555 | // no bease pointer origin we check that the base pointer is defined |
| 2556 | // outside the region. |
Johannes Doerfert | bc7cff4 | 2015-10-18 19:49:25 +0000 | [diff] [blame] | 2557 | const ScopArrayInfo *SAI = MA->getScopArrayInfo(); |
Johannes Doerfert | 654c328 | 2015-10-21 22:14:57 +0000 | [diff] [blame] | 2558 | while (auto *BasePtrOriginSAI = SAI->getBasePtrOriginSAI()) |
| 2559 | SAI = BasePtrOriginSAI; |
| 2560 | |
| 2561 | if (auto *BasePtrInst = dyn_cast<Instruction>(SAI->getBasePtr())) |
| 2562 | if (R.contains(BasePtrInst)) |
| 2563 | continue; |
Johannes Doerfert | bc7cff4 | 2015-10-18 19:49:25 +0000 | [diff] [blame] | 2564 | |
Johannes Doerfert | 8930f48 | 2015-10-02 14:51:00 +0000 | [diff] [blame] | 2565 | // Skip accesses in non-affine subregions as they might not be executed |
| 2566 | // under the same condition as the entry of the non-affine subregion. |
| 2567 | if (BB != MA->getAccessInstruction()->getParent()) |
| 2568 | continue; |
| 2569 | |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 2570 | isl_map *AccessRelation = MA->getAccessRelation(); |
Johannes Doerfert | b864c2c | 2015-10-18 19:50:18 +0000 | [diff] [blame] | 2571 | |
| 2572 | // Skip accesses that have an empty access relation. These can be caused |
| 2573 | // by multiple offsets with a type cast in-between that cause the overall |
| 2574 | // byte offset to be not divisible by the new types sizes. |
| 2575 | if (isl_map_is_empty(AccessRelation)) { |
| 2576 | isl_map_free(AccessRelation); |
| 2577 | continue; |
| 2578 | } |
| 2579 | |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 2580 | if (isl_map_involves_dims(AccessRelation, isl_dim_in, 0, |
| 2581 | Stmt.getNumIterators())) { |
| 2582 | isl_map_free(AccessRelation); |
| 2583 | continue; |
| 2584 | } |
| 2585 | |
| 2586 | AccessRelation = |
| 2587 | isl_map_intersect_domain(AccessRelation, isl_set_copy(Domain)); |
| 2588 | isl_set *AccessRange = isl_map_range(AccessRelation); |
| 2589 | |
| 2590 | isl_union_map *Written = isl_union_map_intersect_range( |
| 2591 | isl_union_map_copy(Writes), isl_union_set_from_set(AccessRange)); |
| 2592 | bool IsWritten = !isl_union_map_is_empty(Written); |
| 2593 | isl_union_map_free(Written); |
| 2594 | |
| 2595 | if (IsWritten) |
| 2596 | continue; |
| 2597 | |
| 2598 | InvMAs.push_front(MA); |
| 2599 | } |
| 2600 | |
| 2601 | // We inserted invariant accesses always in the front but need them to be |
| 2602 | // sorted in a "natural order". The statements are already sorted in reverse |
| 2603 | // post order and that suffices for the accesses too. The reason we require |
| 2604 | // an order in the first place is the dependences between invariant loads |
| 2605 | // that can be caused by indirect loads. |
| 2606 | InvMAs.reverse(); |
| 2607 | |
| 2608 | // Transfer the memory access from the statement to the SCoP. |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 2609 | Stmt.removeMemoryAccesses(InvMAs); |
| 2610 | addInvariantLoads(Stmt, InvMAs); |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 2611 | |
| 2612 | isl_set_free(Domain); |
| 2613 | } |
| 2614 | isl_union_map_free(Writes); |
| 2615 | |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2616 | if (!InvariantEquivClasses.empty()) |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 2617 | IsOptimized = true; |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 2618 | |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2619 | auto &ScopRIL = *SD.getRequiredInvariantLoads(&getRegion()); |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 2620 | // Check required invariant loads that were tagged during SCoP detection. |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2621 | for (LoadInst *LI : ScopRIL) { |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 2622 | assert(LI && getRegion().contains(LI)); |
| 2623 | ScopStmt *Stmt = getStmtForBasicBlock(LI->getParent()); |
| 2624 | if (Stmt && Stmt->lookupAccessesFor(LI) != nullptr) { |
| 2625 | DEBUG(dbgs() << "\n\nWARNING: Load (" << *LI |
| 2626 | << ") is required to be invariant but was not marked as " |
| 2627 | "such. SCoP for " |
| 2628 | << getRegion() << " will be dropped\n\n"); |
| 2629 | addAssumption(isl_set_empty(getParamSpace())); |
| 2630 | return; |
| 2631 | } |
| 2632 | } |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 2633 | } |
| 2634 | |
Johannes Doerfert | 80ef110 | 2014-11-07 08:31:31 +0000 | [diff] [blame] | 2635 | const ScopArrayInfo * |
| 2636 | Scop::getOrCreateScopArrayInfo(Value *BasePtr, Type *AccessType, |
Michael Kruse | 2846877 | 2015-09-14 15:45:33 +0000 | [diff] [blame] | 2637 | ArrayRef<const SCEV *> Sizes, bool IsPHI) { |
Tobias Grosser | 9224522 | 2015-07-28 14:53:44 +0000 | [diff] [blame] | 2638 | auto &SAI = ScopArrayInfoMap[std::make_pair(BasePtr, IsPHI)]; |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 2639 | if (!SAI) { |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 2640 | SAI.reset(new ScopArrayInfo(BasePtr, AccessType, getIslCtx(), Sizes, IsPHI, |
| 2641 | this)); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 2642 | } else { |
| 2643 | if (Sizes.size() > SAI->getNumberOfDimensions()) |
| 2644 | SAI->updateSizes(Sizes); |
| 2645 | } |
Tobias Grosser | ab67144 | 2015-05-23 05:58:27 +0000 | [diff] [blame] | 2646 | return SAI.get(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 2647 | } |
| 2648 | |
Tobias Grosser | 9224522 | 2015-07-28 14:53:44 +0000 | [diff] [blame] | 2649 | const ScopArrayInfo *Scop::getScopArrayInfo(Value *BasePtr, bool IsPHI) { |
| 2650 | auto *SAI = ScopArrayInfoMap[std::make_pair(BasePtr, IsPHI)].get(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 2651 | assert(SAI && "No ScopArrayInfo available for this base pointer"); |
| 2652 | return SAI; |
| 2653 | } |
| 2654 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 2655 | std::string Scop::getContextStr() const { return stringFromIslObj(Context); } |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2656 | std::string Scop::getAssumedContextStr() const { |
| 2657 | return stringFromIslObj(AssumedContext); |
| 2658 | } |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 2659 | std::string Scop::getBoundaryContextStr() const { |
| 2660 | return stringFromIslObj(BoundaryContext); |
| 2661 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2662 | |
| 2663 | std::string Scop::getNameStr() const { |
| 2664 | std::string ExitName, EntryName; |
| 2665 | raw_string_ostream ExitStr(ExitName); |
| 2666 | raw_string_ostream EntryStr(EntryName); |
| 2667 | |
Tobias Grosser | f240b48 | 2014-01-09 10:42:15 +0000 | [diff] [blame] | 2668 | R.getEntry()->printAsOperand(EntryStr, false); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2669 | EntryStr.str(); |
| 2670 | |
| 2671 | if (R.getExit()) { |
Tobias Grosser | f240b48 | 2014-01-09 10:42:15 +0000 | [diff] [blame] | 2672 | R.getExit()->printAsOperand(ExitStr, false); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2673 | ExitStr.str(); |
| 2674 | } else |
| 2675 | ExitName = "FunctionExit"; |
| 2676 | |
| 2677 | return EntryName + "---" + ExitName; |
| 2678 | } |
| 2679 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 2680 | __isl_give isl_set *Scop::getContext() const { return isl_set_copy(Context); } |
Tobias Grosser | 3748705 | 2011-10-06 00:03:42 +0000 | [diff] [blame] | 2681 | __isl_give isl_space *Scop::getParamSpace() const { |
Tobias Grosser | eeb9f3c | 2015-05-26 21:37:31 +0000 | [diff] [blame] | 2682 | return isl_set_get_space(Context); |
Tobias Grosser | 3748705 | 2011-10-06 00:03:42 +0000 | [diff] [blame] | 2683 | } |
| 2684 | |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 2685 | __isl_give isl_set *Scop::getAssumedContext() const { |
| 2686 | return isl_set_copy(AssumedContext); |
| 2687 | } |
| 2688 | |
Johannes Doerfert | 43788c5 | 2015-08-20 05:58:56 +0000 | [diff] [blame] | 2689 | __isl_give isl_set *Scop::getRuntimeCheckContext() const { |
| 2690 | isl_set *RuntimeCheckContext = getAssumedContext(); |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 2691 | RuntimeCheckContext = |
| 2692 | isl_set_intersect(RuntimeCheckContext, getBoundaryContext()); |
| 2693 | RuntimeCheckContext = simplifyAssumptionContext(RuntimeCheckContext, *this); |
Johannes Doerfert | 43788c5 | 2015-08-20 05:58:56 +0000 | [diff] [blame] | 2694 | return RuntimeCheckContext; |
| 2695 | } |
| 2696 | |
Johannes Doerfert | 5d5b306 | 2015-08-20 18:06:30 +0000 | [diff] [blame] | 2697 | bool Scop::hasFeasibleRuntimeContext() const { |
Johannes Doerfert | 43788c5 | 2015-08-20 05:58:56 +0000 | [diff] [blame] | 2698 | isl_set *RuntimeCheckContext = getRuntimeCheckContext(); |
Johannes Doerfert | 5d5b306 | 2015-08-20 18:06:30 +0000 | [diff] [blame] | 2699 | RuntimeCheckContext = addNonEmptyDomainConstraints(RuntimeCheckContext); |
Johannes Doerfert | 43788c5 | 2015-08-20 05:58:56 +0000 | [diff] [blame] | 2700 | bool IsFeasible = !isl_set_is_empty(RuntimeCheckContext); |
| 2701 | isl_set_free(RuntimeCheckContext); |
| 2702 | return IsFeasible; |
| 2703 | } |
| 2704 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2705 | void Scop::addAssumption(__isl_take isl_set *Set) { |
| 2706 | AssumedContext = isl_set_intersect(AssumedContext, Set); |
Tobias Grosser | 7b50bee | 2014-11-25 10:51:12 +0000 | [diff] [blame] | 2707 | AssumedContext = isl_set_coalesce(AssumedContext); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2708 | } |
| 2709 | |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 2710 | __isl_give isl_set *Scop::getBoundaryContext() const { |
| 2711 | return isl_set_copy(BoundaryContext); |
| 2712 | } |
| 2713 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2714 | void Scop::printContext(raw_ostream &OS) const { |
| 2715 | OS << "Context:\n"; |
| 2716 | |
| 2717 | if (!Context) { |
| 2718 | OS.indent(4) << "n/a\n\n"; |
| 2719 | return; |
| 2720 | } |
| 2721 | |
| 2722 | OS.indent(4) << getContextStr() << "\n"; |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 2723 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2724 | OS.indent(4) << "Assumed Context:\n"; |
| 2725 | if (!AssumedContext) { |
| 2726 | OS.indent(4) << "n/a\n\n"; |
| 2727 | return; |
| 2728 | } |
| 2729 | |
| 2730 | OS.indent(4) << getAssumedContextStr() << "\n"; |
| 2731 | |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 2732 | OS.indent(4) << "Boundary Context:\n"; |
| 2733 | if (!BoundaryContext) { |
| 2734 | OS.indent(4) << "n/a\n\n"; |
| 2735 | return; |
| 2736 | } |
| 2737 | |
| 2738 | OS.indent(4) << getBoundaryContextStr() << "\n"; |
| 2739 | |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 2740 | for (const SCEV *Parameter : Parameters) { |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 2741 | int Dim = ParameterIds.find(Parameter)->second; |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 2742 | OS.indent(4) << "p" << Dim << ": " << *Parameter << "\n"; |
| 2743 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2744 | } |
| 2745 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2746 | void Scop::printAliasAssumptions(raw_ostream &OS) const { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 2747 | int noOfGroups = 0; |
| 2748 | for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) { |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 2749 | if (Pair.second.size() == 0) |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2750 | noOfGroups += 1; |
| 2751 | else |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 2752 | noOfGroups += Pair.second.size(); |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2753 | } |
| 2754 | |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 2755 | OS.indent(4) << "Alias Groups (" << noOfGroups << "):\n"; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2756 | if (MinMaxAliasGroups.empty()) { |
| 2757 | OS.indent(8) << "n/a\n"; |
| 2758 | return; |
| 2759 | } |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2760 | |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 2761 | for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2762 | |
| 2763 | // If the group has no read only accesses print the write accesses. |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 2764 | if (Pair.second.empty()) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2765 | OS.indent(8) << "[["; |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 2766 | for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 2767 | OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second |
| 2768 | << ">"; |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2769 | } |
| 2770 | OS << " ]]\n"; |
| 2771 | } |
| 2772 | |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 2773 | for (const MinMaxAccessTy &MMAReadOnly : Pair.second) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2774 | OS.indent(8) << "[["; |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 2775 | OS << " <" << MMAReadOnly.first << ", " << MMAReadOnly.second << ">"; |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 2776 | for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 2777 | OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second |
| 2778 | << ">"; |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2779 | } |
| 2780 | OS << " ]]\n"; |
| 2781 | } |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2782 | } |
| 2783 | } |
| 2784 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2785 | void Scop::printStatements(raw_ostream &OS) const { |
| 2786 | OS << "Statements {\n"; |
| 2787 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 2788 | for (const ScopStmt &Stmt : *this) |
| 2789 | OS.indent(4) << Stmt; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2790 | |
| 2791 | OS.indent(4) << "}\n"; |
| 2792 | } |
| 2793 | |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 2794 | void Scop::printArrayInfo(raw_ostream &OS) const { |
| 2795 | OS << "Arrays {\n"; |
| 2796 | |
Tobias Grosser | ab67144 | 2015-05-23 05:58:27 +0000 | [diff] [blame] | 2797 | for (auto &Array : arrays()) |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 2798 | Array.second->print(OS); |
| 2799 | |
| 2800 | OS.indent(4) << "}\n"; |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 2801 | |
| 2802 | OS.indent(4) << "Arrays (Bounds as pw_affs) {\n"; |
| 2803 | |
| 2804 | for (auto &Array : arrays()) |
| 2805 | Array.second->print(OS, /* SizeAsPwAff */ true); |
| 2806 | |
| 2807 | OS.indent(4) << "}\n"; |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 2808 | } |
| 2809 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2810 | void Scop::print(raw_ostream &OS) const { |
Tobias Grosser | 4eb7ddb | 2014-03-18 18:51:11 +0000 | [diff] [blame] | 2811 | OS.indent(4) << "Function: " << getRegion().getEntry()->getParent()->getName() |
| 2812 | << "\n"; |
Tobias Grosser | 483fdd4 | 2014-03-18 18:05:38 +0000 | [diff] [blame] | 2813 | OS.indent(4) << "Region: " << getNameStr() << "\n"; |
David Peixotto | dc0a11c | 2015-01-13 18:31:55 +0000 | [diff] [blame] | 2814 | OS.indent(4) << "Max Loop Depth: " << getMaxLoopDepth() << "\n"; |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 2815 | OS.indent(4) << "Invariant Accesses: {\n"; |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2816 | for (const auto &IAClass : InvariantEquivClasses) { |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 2817 | const auto &MAs = std::get<1>(IAClass); |
| 2818 | if (MAs.empty()) { |
| 2819 | OS.indent(12) << "Class Pointer: " << *std::get<0>(IAClass) << "\n"; |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2820 | } else { |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 2821 | MAs.front()->print(OS); |
| 2822 | OS.indent(12) << "Execution Context: " << std::get<2>(IAClass) << "\n"; |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2823 | } |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 2824 | } |
| 2825 | OS.indent(4) << "}\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2826 | printContext(OS.indent(4)); |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 2827 | printArrayInfo(OS.indent(4)); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2828 | printAliasAssumptions(OS); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2829 | printStatements(OS.indent(4)); |
| 2830 | } |
| 2831 | |
| 2832 | void Scop::dump() const { print(dbgs()); } |
| 2833 | |
Tobias Grosser | 9a38ab8 | 2011-11-08 15:41:03 +0000 | [diff] [blame] | 2834 | isl_ctx *Scop::getIslCtx() const { return IslCtx; } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2835 | |
Johannes Doerfert | cef616f | 2015-09-15 22:49:04 +0000 | [diff] [blame] | 2836 | __isl_give isl_pw_aff *Scop::getPwAff(const SCEV *E, BasicBlock *BB) { |
| 2837 | return Affinator.getPwAff(E, BB); |
Johannes Doerfert | 574182d | 2015-08-12 10:19:50 +0000 | [diff] [blame] | 2838 | } |
| 2839 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 2840 | __isl_give isl_union_set *Scop::getDomains() const { |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 2841 | isl_union_set *Domain = isl_union_set_empty(getParamSpace()); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 2842 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 2843 | for (const ScopStmt &Stmt : *this) |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 2844 | Domain = isl_union_set_add_set(Domain, Stmt.getDomain()); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 2845 | |
| 2846 | return Domain; |
| 2847 | } |
| 2848 | |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 2849 | __isl_give isl_union_map *Scop::getMustWrites() { |
Tobias Grosser | eeb9f3c | 2015-05-26 21:37:31 +0000 | [diff] [blame] | 2850 | isl_union_map *Write = isl_union_map_empty(getParamSpace()); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 2851 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 2852 | for (ScopStmt &Stmt : *this) { |
| 2853 | for (MemoryAccess *MA : Stmt) { |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 2854 | if (!MA->isMustWrite()) |
| 2855 | continue; |
| 2856 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 2857 | isl_set *Domain = Stmt.getDomain(); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 2858 | isl_map *AccessDomain = MA->getAccessRelation(); |
| 2859 | AccessDomain = isl_map_intersect_domain(AccessDomain, Domain); |
| 2860 | Write = isl_union_map_add_map(Write, AccessDomain); |
| 2861 | } |
| 2862 | } |
| 2863 | return isl_union_map_coalesce(Write); |
| 2864 | } |
| 2865 | |
| 2866 | __isl_give isl_union_map *Scop::getMayWrites() { |
Tobias Grosser | eeb9f3c | 2015-05-26 21:37:31 +0000 | [diff] [blame] | 2867 | isl_union_map *Write = isl_union_map_empty(getParamSpace()); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 2868 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 2869 | for (ScopStmt &Stmt : *this) { |
| 2870 | for (MemoryAccess *MA : Stmt) { |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 2871 | if (!MA->isMayWrite()) |
| 2872 | continue; |
| 2873 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 2874 | isl_set *Domain = Stmt.getDomain(); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 2875 | isl_map *AccessDomain = MA->getAccessRelation(); |
| 2876 | AccessDomain = isl_map_intersect_domain(AccessDomain, Domain); |
| 2877 | Write = isl_union_map_add_map(Write, AccessDomain); |
| 2878 | } |
| 2879 | } |
| 2880 | return isl_union_map_coalesce(Write); |
| 2881 | } |
| 2882 | |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 2883 | __isl_give isl_union_map *Scop::getWrites() { |
Tobias Grosser | eeb9f3c | 2015-05-26 21:37:31 +0000 | [diff] [blame] | 2884 | isl_union_map *Write = isl_union_map_empty(getParamSpace()); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 2885 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 2886 | for (ScopStmt &Stmt : *this) { |
| 2887 | for (MemoryAccess *MA : Stmt) { |
Johannes Doerfert | f675289 | 2014-06-13 18:01:45 +0000 | [diff] [blame] | 2888 | if (!MA->isWrite()) |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 2889 | continue; |
| 2890 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 2891 | isl_set *Domain = Stmt.getDomain(); |
Johannes Doerfert | f675289 | 2014-06-13 18:01:45 +0000 | [diff] [blame] | 2892 | isl_map *AccessDomain = MA->getAccessRelation(); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 2893 | AccessDomain = isl_map_intersect_domain(AccessDomain, Domain); |
| 2894 | Write = isl_union_map_add_map(Write, AccessDomain); |
| 2895 | } |
| 2896 | } |
| 2897 | return isl_union_map_coalesce(Write); |
| 2898 | } |
| 2899 | |
| 2900 | __isl_give isl_union_map *Scop::getReads() { |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 2901 | isl_union_map *Read = isl_union_map_empty(getParamSpace()); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 2902 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 2903 | for (ScopStmt &Stmt : *this) { |
| 2904 | for (MemoryAccess *MA : Stmt) { |
Johannes Doerfert | f675289 | 2014-06-13 18:01:45 +0000 | [diff] [blame] | 2905 | if (!MA->isRead()) |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 2906 | continue; |
| 2907 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 2908 | isl_set *Domain = Stmt.getDomain(); |
Johannes Doerfert | f675289 | 2014-06-13 18:01:45 +0000 | [diff] [blame] | 2909 | isl_map *AccessDomain = MA->getAccessRelation(); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 2910 | |
| 2911 | AccessDomain = isl_map_intersect_domain(AccessDomain, Domain); |
| 2912 | Read = isl_union_map_add_map(Read, AccessDomain); |
| 2913 | } |
| 2914 | } |
| 2915 | return isl_union_map_coalesce(Read); |
| 2916 | } |
| 2917 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 2918 | __isl_give isl_union_map *Scop::getSchedule() const { |
| 2919 | auto Tree = getScheduleTree(); |
| 2920 | auto S = isl_schedule_get_map(Tree); |
| 2921 | isl_schedule_free(Tree); |
| 2922 | return S; |
| 2923 | } |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 2924 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 2925 | __isl_give isl_schedule *Scop::getScheduleTree() const { |
| 2926 | return isl_schedule_intersect_domain(isl_schedule_copy(Schedule), |
| 2927 | getDomains()); |
| 2928 | } |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 2929 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 2930 | void Scop::setSchedule(__isl_take isl_union_map *NewSchedule) { |
| 2931 | auto *S = isl_schedule_from_domain(getDomains()); |
| 2932 | S = isl_schedule_insert_partial_schedule( |
| 2933 | S, isl_multi_union_pw_aff_from_union_map(NewSchedule)); |
| 2934 | isl_schedule_free(Schedule); |
| 2935 | Schedule = S; |
| 2936 | } |
| 2937 | |
| 2938 | void Scop::setScheduleTree(__isl_take isl_schedule *NewSchedule) { |
| 2939 | isl_schedule_free(Schedule); |
| 2940 | Schedule = NewSchedule; |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 2941 | } |
| 2942 | |
| 2943 | bool Scop::restrictDomains(__isl_take isl_union_set *Domain) { |
| 2944 | bool Changed = false; |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 2945 | for (ScopStmt &Stmt : *this) { |
| 2946 | isl_union_set *StmtDomain = isl_union_set_from_set(Stmt.getDomain()); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 2947 | isl_union_set *NewStmtDomain = isl_union_set_intersect( |
| 2948 | isl_union_set_copy(StmtDomain), isl_union_set_copy(Domain)); |
| 2949 | |
| 2950 | if (isl_union_set_is_subset(StmtDomain, NewStmtDomain)) { |
| 2951 | isl_union_set_free(StmtDomain); |
| 2952 | isl_union_set_free(NewStmtDomain); |
| 2953 | continue; |
| 2954 | } |
| 2955 | |
| 2956 | Changed = true; |
| 2957 | |
| 2958 | isl_union_set_free(StmtDomain); |
| 2959 | NewStmtDomain = isl_union_set_coalesce(NewStmtDomain); |
| 2960 | |
| 2961 | if (isl_union_set_is_empty(NewStmtDomain)) { |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 2962 | Stmt.restrictDomain(isl_set_empty(Stmt.getDomainSpace())); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 2963 | isl_union_set_free(NewStmtDomain); |
| 2964 | } else |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 2965 | Stmt.restrictDomain(isl_set_from_union_set(NewStmtDomain)); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 2966 | } |
| 2967 | isl_union_set_free(Domain); |
| 2968 | return Changed; |
| 2969 | } |
| 2970 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2971 | ScalarEvolution *Scop::getSE() const { return SE; } |
| 2972 | |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2973 | bool Scop::isIgnored(RegionNode *RN) { |
| 2974 | BasicBlock *BB = getRegionNodeBasicBlock(RN); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2975 | |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2976 | // Check if there are accesses contained. |
| 2977 | bool ContainsAccesses = false; |
| 2978 | if (!RN->isSubRegion()) |
| 2979 | ContainsAccesses = getAccessFunctions(BB); |
| 2980 | else |
| 2981 | for (BasicBlock *RBB : RN->getNodeAs<Region>()->blocks()) |
| 2982 | ContainsAccesses |= (getAccessFunctions(RBB) != nullptr); |
| 2983 | if (!ContainsAccesses) |
| 2984 | return true; |
| 2985 | |
| 2986 | // Check for reachability via non-error blocks. |
| 2987 | if (!DomainMap.count(BB)) |
| 2988 | return true; |
| 2989 | |
| 2990 | // Check if error blocks are contained. |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 2991 | if (containsErrorBlock(RN, getRegion(), LI, DT)) |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2992 | return true; |
| 2993 | |
| 2994 | return false; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2995 | } |
| 2996 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 2997 | struct MapToDimensionDataTy { |
| 2998 | int N; |
| 2999 | isl_union_pw_multi_aff *Res; |
| 3000 | }; |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 3001 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 3002 | // @brief Create a function that maps the elements of 'Set' to its N-th |
| 3003 | // dimension. |
| 3004 | // |
| 3005 | // The result is added to 'User->Res'. |
| 3006 | // |
| 3007 | // @param Set The input set. |
| 3008 | // @param N The dimension to map to. |
| 3009 | // |
| 3010 | // @returns Zero if no error occurred, non-zero otherwise. |
| 3011 | static isl_stat mapToDimension_AddSet(__isl_take isl_set *Set, void *User) { |
| 3012 | struct MapToDimensionDataTy *Data = (struct MapToDimensionDataTy *)User; |
| 3013 | int Dim; |
| 3014 | isl_space *Space; |
| 3015 | isl_pw_multi_aff *PMA; |
| 3016 | |
| 3017 | Dim = isl_set_dim(Set, isl_dim_set); |
| 3018 | Space = isl_set_get_space(Set); |
| 3019 | PMA = isl_pw_multi_aff_project_out_map(Space, isl_dim_set, Data->N, |
| 3020 | Dim - Data->N); |
| 3021 | if (Data->N > 1) |
| 3022 | PMA = isl_pw_multi_aff_drop_dims(PMA, isl_dim_out, 0, Data->N - 1); |
| 3023 | Data->Res = isl_union_pw_multi_aff_add_pw_multi_aff(Data->Res, PMA); |
| 3024 | |
| 3025 | isl_set_free(Set); |
| 3026 | |
| 3027 | return isl_stat_ok; |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 3028 | } |
| 3029 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 3030 | // @brief Create a function that maps the elements of Domain to their Nth |
| 3031 | // dimension. |
| 3032 | // |
| 3033 | // @param Domain The set of elements to map. |
| 3034 | // @param N The dimension to map to. |
| 3035 | static __isl_give isl_multi_union_pw_aff * |
| 3036 | mapToDimension(__isl_take isl_union_set *Domain, int N) { |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 3037 | if (N <= 0 || isl_union_set_is_empty(Domain)) { |
| 3038 | isl_union_set_free(Domain); |
| 3039 | return nullptr; |
| 3040 | } |
| 3041 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 3042 | struct MapToDimensionDataTy Data; |
| 3043 | isl_space *Space; |
| 3044 | |
| 3045 | Space = isl_union_set_get_space(Domain); |
| 3046 | Data.N = N; |
| 3047 | Data.Res = isl_union_pw_multi_aff_empty(Space); |
| 3048 | if (isl_union_set_foreach_set(Domain, &mapToDimension_AddSet, &Data) < 0) |
| 3049 | Data.Res = isl_union_pw_multi_aff_free(Data.Res); |
| 3050 | |
| 3051 | isl_union_set_free(Domain); |
| 3052 | return isl_multi_union_pw_aff_from_union_pw_multi_aff(Data.Res); |
| 3053 | } |
| 3054 | |
Michael Kruse | 9d08009 | 2015-09-11 21:41:48 +0000 | [diff] [blame] | 3055 | ScopStmt *Scop::addScopStmt(BasicBlock *BB, Region *R) { |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 3056 | ScopStmt *Stmt; |
| 3057 | if (BB) { |
Michael Kruse | 9d08009 | 2015-09-11 21:41:48 +0000 | [diff] [blame] | 3058 | Stmts.emplace_back(*this, *BB); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 3059 | Stmt = &Stmts.back(); |
| 3060 | StmtMap[BB] = Stmt; |
| 3061 | } else { |
| 3062 | assert(R && "Either basic block or a region expected."); |
Michael Kruse | 9d08009 | 2015-09-11 21:41:48 +0000 | [diff] [blame] | 3063 | Stmts.emplace_back(*this, *R); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 3064 | Stmt = &Stmts.back(); |
| 3065 | for (BasicBlock *BB : R->blocks()) |
| 3066 | StmtMap[BB] = Stmt; |
| 3067 | } |
| 3068 | return Stmt; |
| 3069 | } |
| 3070 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 3071 | void Scop::buildSchedule( |
Johannes Doerfert | d8dd863 | 2015-10-07 20:31:36 +0000 | [diff] [blame] | 3072 | Region *R, |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 3073 | DenseMap<Loop *, std::pair<isl_schedule *, unsigned>> &LoopSchedules) { |
Michael Kruse | 046dde4 | 2015-08-10 13:01:57 +0000 | [diff] [blame] | 3074 | |
Johannes Doerfert | 40fa56f | 2015-09-14 11:15:07 +0000 | [diff] [blame] | 3075 | if (SD.isNonAffineSubRegion(R, &getRegion())) { |
Johannes Doerfert | c6987c1 | 2015-09-26 13:41:43 +0000 | [diff] [blame] | 3076 | Loop *L = getLoopSurroundingRegion(*R, LI); |
| 3077 | auto &LSchedulePair = LoopSchedules[L]; |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 3078 | ScopStmt *Stmt = getStmtForBasicBlock(R->getEntry()); |
Michael Kruse | afe0670 | 2015-10-02 16:33:27 +0000 | [diff] [blame] | 3079 | isl_set *Domain = Stmt->getDomain(); |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 3080 | auto *UDomain = isl_union_set_from_set(Domain); |
| 3081 | auto *StmtSchedule = isl_schedule_from_domain(UDomain); |
Johannes Doerfert | 40fa56f | 2015-09-14 11:15:07 +0000 | [diff] [blame] | 3082 | LSchedulePair.first = StmtSchedule; |
| 3083 | return; |
| 3084 | } |
| 3085 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 3086 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 3087 | for (auto *RN : RTraversal) { |
Michael Kruse | 046dde4 | 2015-08-10 13:01:57 +0000 | [diff] [blame] | 3088 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 3089 | if (RN->isSubRegion()) { |
| 3090 | Region *SubRegion = RN->getNodeAs<Region>(); |
| 3091 | if (!SD.isNonAffineSubRegion(SubRegion, &getRegion())) { |
Johannes Doerfert | d8dd863 | 2015-10-07 20:31:36 +0000 | [diff] [blame] | 3092 | buildSchedule(SubRegion, LoopSchedules); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 3093 | continue; |
| 3094 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3095 | } |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 3096 | |
| 3097 | Loop *L = getRegionNodeLoop(RN, LI); |
Johannes Doerfert | 30c2265 | 2015-10-18 21:17:11 +0000 | [diff] [blame] | 3098 | if (!getRegion().contains(L)) |
| 3099 | L = getLoopSurroundingRegion(getRegion(), LI); |
| 3100 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 3101 | int LD = getRelativeLoopDepth(L); |
| 3102 | auto &LSchedulePair = LoopSchedules[L]; |
| 3103 | LSchedulePair.second += getNumBlocksInRegionNode(RN); |
| 3104 | |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 3105 | BasicBlock *BB = getRegionNodeBasicBlock(RN); |
| 3106 | ScopStmt *Stmt = getStmtForBasicBlock(BB); |
| 3107 | if (Stmt) { |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 3108 | auto *UDomain = isl_union_set_from_set(Stmt->getDomain()); |
| 3109 | auto *StmtSchedule = isl_schedule_from_domain(UDomain); |
| 3110 | LSchedulePair.first = |
| 3111 | combineInSequence(LSchedulePair.first, StmtSchedule); |
| 3112 | } |
| 3113 | |
| 3114 | unsigned NumVisited = LSchedulePair.second; |
| 3115 | while (L && NumVisited == L->getNumBlocks()) { |
| 3116 | auto *LDomain = isl_schedule_get_domain(LSchedulePair.first); |
| 3117 | if (auto *MUPA = mapToDimension(LDomain, LD + 1)) |
| 3118 | LSchedulePair.first = |
| 3119 | isl_schedule_insert_partial_schedule(LSchedulePair.first, MUPA); |
| 3120 | |
| 3121 | auto *PL = L->getParentLoop(); |
| 3122 | assert(LoopSchedules.count(PL)); |
| 3123 | auto &PSchedulePair = LoopSchedules[PL]; |
| 3124 | PSchedulePair.first = |
| 3125 | combineInSequence(PSchedulePair.first, LSchedulePair.first); |
| 3126 | PSchedulePair.second += NumVisited; |
| 3127 | |
| 3128 | L = PL; |
| 3129 | NumVisited = PSchedulePair.second; |
| 3130 | } |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 3131 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3132 | } |
| 3133 | |
Johannes Doerfert | 7c49421 | 2014-10-31 23:13:39 +0000 | [diff] [blame] | 3134 | ScopStmt *Scop::getStmtForBasicBlock(BasicBlock *BB) const { |
Tobias Grosser | 57411e3 | 2015-05-27 06:51:34 +0000 | [diff] [blame] | 3135 | auto StmtMapIt = StmtMap.find(BB); |
Johannes Doerfert | 7c49421 | 2014-10-31 23:13:39 +0000 | [diff] [blame] | 3136 | if (StmtMapIt == StmtMap.end()) |
| 3137 | return nullptr; |
| 3138 | return StmtMapIt->second; |
| 3139 | } |
| 3140 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3141 | int Scop::getRelativeLoopDepth(const Loop *L) const { |
| 3142 | Loop *OuterLoop = |
| 3143 | L ? R.outermostLoopInRegion(const_cast<Loop *>(L)) : nullptr; |
| 3144 | if (!OuterLoop) |
| 3145 | return -1; |
Johannes Doerfert | d020b77 | 2015-08-27 06:53:52 +0000 | [diff] [blame] | 3146 | return L->getLoopDepth() - OuterLoop->getLoopDepth(); |
| 3147 | } |
| 3148 | |
Michael Kruse | d868b5d | 2015-09-10 15:25:24 +0000 | [diff] [blame] | 3149 | void ScopInfo::buildPHIAccesses(PHINode *PHI, Region &R, |
Michael Kruse | d868b5d | 2015-09-10 15:25:24 +0000 | [diff] [blame] | 3150 | Region *NonAffineSubRegion, bool IsExitBlock) { |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3151 | |
| 3152 | // PHI nodes that are in the exit block of the region, hence if IsExitBlock is |
| 3153 | // true, are not modeled as ordinary PHI nodes as they are not part of the |
| 3154 | // region. However, we model the operands in the predecessor blocks that are |
| 3155 | // part of the region as regular scalar accesses. |
| 3156 | |
| 3157 | // If we can synthesize a PHI we can skip it, however only if it is in |
| 3158 | // the region. If it is not it can only be in the exit block of the region. |
| 3159 | // In this case we model the operands but not the PHI itself. |
| 3160 | if (!IsExitBlock && canSynthesize(PHI, LI, SE, &R)) |
| 3161 | return; |
| 3162 | |
| 3163 | // PHI nodes are modeled as if they had been demoted prior to the SCoP |
| 3164 | // detection. Hence, the PHI is a load of a new memory location in which the |
| 3165 | // incoming value was written at the end of the incoming basic block. |
| 3166 | bool OnlyNonAffineSubRegionOperands = true; |
| 3167 | for (unsigned u = 0; u < PHI->getNumIncomingValues(); u++) { |
| 3168 | Value *Op = PHI->getIncomingValue(u); |
| 3169 | BasicBlock *OpBB = PHI->getIncomingBlock(u); |
| 3170 | |
| 3171 | // Do not build scalar dependences inside a non-affine subregion. |
| 3172 | if (NonAffineSubRegion && NonAffineSubRegion->contains(OpBB)) |
| 3173 | continue; |
| 3174 | |
| 3175 | OnlyNonAffineSubRegionOperands = false; |
| 3176 | |
| 3177 | if (!R.contains(OpBB)) |
| 3178 | continue; |
| 3179 | |
| 3180 | Instruction *OpI = dyn_cast<Instruction>(Op); |
| 3181 | if (OpI) { |
| 3182 | BasicBlock *OpIBB = OpI->getParent(); |
| 3183 | // As we pretend there is a use (or more precise a write) of OpI in OpBB |
| 3184 | // we have to insert a scalar dependence from the definition of OpI to |
| 3185 | // OpBB if the definition is not in OpBB. |
Michael Kruse | 668af71 | 2015-10-15 14:45:48 +0000 | [diff] [blame] | 3186 | if (scop->getStmtForBasicBlock(OpIBB) != |
| 3187 | scop->getStmtForBasicBlock(OpBB)) { |
Michael Kruse | 33d6c0b | 2015-09-25 18:53:27 +0000 | [diff] [blame] | 3188 | addScalarReadAccess(OpI, PHI, OpBB); |
| 3189 | addScalarWriteAccess(OpI); |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3190 | } |
Tobias Grosser | da95a4a | 2015-09-24 20:59:59 +0000 | [diff] [blame] | 3191 | } else if (ModelReadOnlyScalars && !isa<Constant>(Op)) { |
Michael Kruse | 33d6c0b | 2015-09-25 18:53:27 +0000 | [diff] [blame] | 3192 | addScalarReadAccess(Op, PHI, OpBB); |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3193 | } |
| 3194 | |
Michael Kruse | 33d6c0b | 2015-09-25 18:53:27 +0000 | [diff] [blame] | 3195 | addPHIWriteAccess(PHI, OpBB, Op, IsExitBlock); |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3196 | } |
| 3197 | |
Michael Kruse | 33d6c0b | 2015-09-25 18:53:27 +0000 | [diff] [blame] | 3198 | if (!OnlyNonAffineSubRegionOperands && !IsExitBlock) { |
| 3199 | addPHIReadAccess(PHI); |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3200 | } |
| 3201 | } |
| 3202 | |
Michael Kruse | d868b5d | 2015-09-10 15:25:24 +0000 | [diff] [blame] | 3203 | bool ScopInfo::buildScalarDependences(Instruction *Inst, Region *R, |
| 3204 | Region *NonAffineSubRegion) { |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3205 | bool canSynthesizeInst = canSynthesize(Inst, LI, SE, R); |
| 3206 | if (isIgnoredIntrinsic(Inst)) |
| 3207 | return false; |
| 3208 | |
| 3209 | bool AnyCrossStmtUse = false; |
| 3210 | BasicBlock *ParentBB = Inst->getParent(); |
| 3211 | |
| 3212 | for (User *U : Inst->users()) { |
| 3213 | Instruction *UI = dyn_cast<Instruction>(U); |
| 3214 | |
| 3215 | // Ignore the strange user |
| 3216 | if (UI == 0) |
| 3217 | continue; |
| 3218 | |
| 3219 | BasicBlock *UseParent = UI->getParent(); |
| 3220 | |
Tobias Grosser | baffa09 | 2015-10-24 20:55:27 +0000 | [diff] [blame^] | 3221 | // Ignore basic block local uses. A value that is defined in a scop, but |
| 3222 | // used in a PHI node in the same basic block does not count as basic block |
| 3223 | // local, as for such cases a control flow edge is passed between definition |
| 3224 | // and use. |
| 3225 | if (UseParent == ParentBB && !isa<PHINode>(UI)) |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3226 | continue; |
| 3227 | |
| 3228 | // Do not build scalar dependences inside a non-affine subregion. |
| 3229 | if (NonAffineSubRegion && NonAffineSubRegion->contains(UseParent)) |
| 3230 | continue; |
| 3231 | |
Michael Kruse | 01cb379 | 2015-10-17 21:07:08 +0000 | [diff] [blame] | 3232 | // Check for PHI nodes in the region exit and skip them, if they will be |
Tobias Grosser | 05d7fa7 | 2015-10-17 21:46:28 +0000 | [diff] [blame] | 3233 | // modeled as PHI nodes. |
Michael Kruse | 01cb379 | 2015-10-17 21:07:08 +0000 | [diff] [blame] | 3234 | // |
| 3235 | // PHI nodes in the region exit that have more than two incoming edges need |
Tobias Grosser | 05d7fa7 | 2015-10-17 21:46:28 +0000 | [diff] [blame] | 3236 | // to be modeled as PHI-Nodes to correctly model the fact that depending on |
| 3237 | // the control flow a different value will be assigned to the PHI node. In |
| 3238 | // case this is the case, there is no need to create an additional normal |
| 3239 | // scalar dependence. Hence, bail out before we register an "out-of-region" |
| 3240 | // use for this definition. |
Michael Kruse | 01cb379 | 2015-10-17 21:07:08 +0000 | [diff] [blame] | 3241 | if (isa<PHINode>(UI) && UI->getParent() == R->getExit() && |
| 3242 | !R->getExitingBlock()) |
| 3243 | continue; |
| 3244 | |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3245 | // Check whether or not the use is in the SCoP. |
Tobias Grosser | c73d8b0 | 2015-10-23 22:36:22 +0000 | [diff] [blame] | 3246 | if (!R->contains(UseParent)) { |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3247 | AnyCrossStmtUse = true; |
| 3248 | continue; |
| 3249 | } |
| 3250 | |
Tobias Grosser | baffa09 | 2015-10-24 20:55:27 +0000 | [diff] [blame^] | 3251 | // Uses by PHI nodes in the entry node count as external uses in case the |
| 3252 | // use is through an incoming block that is itself not contained in the |
| 3253 | // region. |
| 3254 | if (R->getEntry() == UseParent) { |
| 3255 | if (auto *PHI = dyn_cast<PHINode>(UI)) { |
| 3256 | bool ExternalUse = false; |
| 3257 | for (unsigned i = 0; i < PHI->getNumIncomingValues(); i++) { |
| 3258 | if (PHI->getIncomingValue(i) == Inst && |
| 3259 | !R->contains(PHI->getIncomingBlock(i))) { |
| 3260 | ExternalUse = true; |
| 3261 | break; |
| 3262 | } |
| 3263 | } |
| 3264 | |
| 3265 | if (ExternalUse) { |
| 3266 | AnyCrossStmtUse = true; |
| 3267 | continue; |
| 3268 | } |
| 3269 | } |
| 3270 | } |
| 3271 | |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3272 | // If the instruction can be synthesized and the user is in the region |
| 3273 | // we do not need to add scalar dependences. |
| 3274 | if (canSynthesizeInst) |
| 3275 | continue; |
| 3276 | |
| 3277 | // No need to translate these scalar dependences into polyhedral form, |
| 3278 | // because synthesizable scalars can be generated by the code generator. |
| 3279 | if (canSynthesize(UI, LI, SE, R)) |
| 3280 | continue; |
| 3281 | |
| 3282 | // Skip PHI nodes in the region as they handle their operands on their own. |
| 3283 | if (isa<PHINode>(UI)) |
| 3284 | continue; |
| 3285 | |
| 3286 | // Now U is used in another statement. |
| 3287 | AnyCrossStmtUse = true; |
| 3288 | |
| 3289 | // 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] | 3290 | // Use the def instruction as base address of the MemoryAccess, so that it |
| 3291 | // will become the name of the scalar access in the polyhedral form. |
Michael Kruse | 33d6c0b | 2015-09-25 18:53:27 +0000 | [diff] [blame] | 3292 | addScalarReadAccess(Inst, UI); |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3293 | } |
| 3294 | |
Tobias Grosser | da95a4a | 2015-09-24 20:59:59 +0000 | [diff] [blame] | 3295 | if (ModelReadOnlyScalars && !isa<PHINode>(Inst)) { |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3296 | for (Value *Op : Inst->operands()) { |
| 3297 | if (canSynthesize(Op, LI, SE, R)) |
| 3298 | continue; |
| 3299 | |
| 3300 | if (Instruction *OpInst = dyn_cast<Instruction>(Op)) |
| 3301 | if (R->contains(OpInst)) |
| 3302 | continue; |
| 3303 | |
| 3304 | if (isa<Constant>(Op)) |
| 3305 | continue; |
| 3306 | |
Michael Kruse | 33d6c0b | 2015-09-25 18:53:27 +0000 | [diff] [blame] | 3307 | addScalarReadAccess(Op, Inst); |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3308 | } |
| 3309 | } |
| 3310 | |
| 3311 | return AnyCrossStmtUse; |
| 3312 | } |
| 3313 | |
| 3314 | extern MapInsnToMemAcc InsnToMemAcc; |
| 3315 | |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 3316 | void ScopInfo::buildMemoryAccess( |
| 3317 | Instruction *Inst, Loop *L, Region *R, |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 3318 | const ScopDetection::BoxedLoopsSetTy *BoxedLoops, |
| 3319 | const InvariantLoadsSetTy &ScopRIL) { |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3320 | unsigned Size; |
| 3321 | Type *SizeType; |
| 3322 | Value *Val; |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 3323 | enum MemoryAccess::AccessType Type; |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3324 | |
| 3325 | if (LoadInst *Load = dyn_cast<LoadInst>(Inst)) { |
| 3326 | SizeType = Load->getType(); |
| 3327 | Size = TD->getTypeStoreSize(SizeType); |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 3328 | Type = MemoryAccess::READ; |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3329 | Val = Load; |
| 3330 | } else { |
| 3331 | StoreInst *Store = cast<StoreInst>(Inst); |
| 3332 | SizeType = Store->getValueOperand()->getType(); |
| 3333 | Size = TD->getTypeStoreSize(SizeType); |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 3334 | Type = MemoryAccess::MUST_WRITE; |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3335 | Val = Store->getValueOperand(); |
| 3336 | } |
| 3337 | |
Tobias Grosser | 5fd8c09 | 2015-09-17 17:28:15 +0000 | [diff] [blame] | 3338 | auto Address = getPointerOperand(*Inst); |
| 3339 | |
| 3340 | const SCEV *AccessFunction = SE->getSCEVAtScope(Address, L); |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3341 | const SCEVUnknown *BasePointer = |
| 3342 | dyn_cast<SCEVUnknown>(SE->getPointerBase(AccessFunction)); |
| 3343 | |
| 3344 | assert(BasePointer && "Could not find base pointer"); |
| 3345 | AccessFunction = SE->getMinusSCEV(AccessFunction, BasePointer); |
| 3346 | |
Tobias Grosser | 6f36d9a | 2015-09-17 20:16:21 +0000 | [diff] [blame] | 3347 | if (isa<GetElementPtrInst>(Address) || isa<BitCastInst>(Address)) { |
| 3348 | auto NewAddress = Address; |
| 3349 | if (auto *BitCast = dyn_cast<BitCastInst>(Address)) { |
| 3350 | auto Src = BitCast->getOperand(0); |
| 3351 | auto SrcTy = Src->getType(); |
| 3352 | auto DstTy = BitCast->getType(); |
| 3353 | if (SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits()) |
| 3354 | NewAddress = Src; |
| 3355 | } |
Tobias Grosser | 5fd8c09 | 2015-09-17 17:28:15 +0000 | [diff] [blame] | 3356 | |
Tobias Grosser | 6f36d9a | 2015-09-17 20:16:21 +0000 | [diff] [blame] | 3357 | if (auto *GEP = dyn_cast<GetElementPtrInst>(NewAddress)) { |
| 3358 | std::vector<const SCEV *> Subscripts; |
| 3359 | std::vector<int> Sizes; |
| 3360 | std::tie(Subscripts, Sizes) = getIndexExpressionsFromGEP(GEP, *SE); |
| 3361 | auto BasePtr = GEP->getOperand(0); |
Tobias Grosser | 5fd8c09 | 2015-09-17 17:28:15 +0000 | [diff] [blame] | 3362 | |
Tobias Grosser | 6f36d9a | 2015-09-17 20:16:21 +0000 | [diff] [blame] | 3363 | std::vector<const SCEV *> SizesSCEV; |
Tobias Grosser | 5fd8c09 | 2015-09-17 17:28:15 +0000 | [diff] [blame] | 3364 | |
Tobias Grosser | 6f36d9a | 2015-09-17 20:16:21 +0000 | [diff] [blame] | 3365 | bool AllAffineSubcripts = true; |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 3366 | for (auto Subscript : Subscripts) { |
| 3367 | InvariantLoadsSetTy AccessILS; |
| 3368 | AllAffineSubcripts = |
| 3369 | isAffineExpr(R, Subscript, *SE, nullptr, &AccessILS); |
| 3370 | |
| 3371 | for (LoadInst *LInst : AccessILS) |
| 3372 | if (!ScopRIL.count(LInst)) |
| 3373 | AllAffineSubcripts = false; |
| 3374 | |
| 3375 | if (!AllAffineSubcripts) |
Tobias Grosser | 6f36d9a | 2015-09-17 20:16:21 +0000 | [diff] [blame] | 3376 | break; |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 3377 | } |
Tobias Grosser | 6f36d9a | 2015-09-17 20:16:21 +0000 | [diff] [blame] | 3378 | |
| 3379 | if (AllAffineSubcripts && Sizes.size() > 0) { |
| 3380 | for (auto V : Sizes) |
| 3381 | SizesSCEV.push_back(SE->getSCEV(ConstantInt::get( |
| 3382 | IntegerType::getInt64Ty(BasePtr->getContext()), V))); |
Tobias Grosser | 5fd8c09 | 2015-09-17 17:28:15 +0000 | [diff] [blame] | 3383 | SizesSCEV.push_back(SE->getSCEV(ConstantInt::get( |
Tobias Grosser | 6f36d9a | 2015-09-17 20:16:21 +0000 | [diff] [blame] | 3384 | IntegerType::getInt64Ty(BasePtr->getContext()), Size))); |
Tobias Grosser | 5fd8c09 | 2015-09-17 17:28:15 +0000 | [diff] [blame] | 3385 | |
Michael Kruse | 33d6c0b | 2015-09-25 18:53:27 +0000 | [diff] [blame] | 3386 | addExplicitAccess(Inst, Type, BasePointer->getValue(), Size, true, |
| 3387 | Subscripts, SizesSCEV, Val); |
Tobias Grosser | b1c3942 | 2015-09-21 16:19:25 +0000 | [diff] [blame] | 3388 | return; |
Tobias Grosser | 6f36d9a | 2015-09-17 20:16:21 +0000 | [diff] [blame] | 3389 | } |
Tobias Grosser | 5fd8c09 | 2015-09-17 17:28:15 +0000 | [diff] [blame] | 3390 | } |
| 3391 | } |
| 3392 | |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3393 | auto AccItr = InsnToMemAcc.find(Inst); |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 3394 | if (PollyDelinearize && AccItr != InsnToMemAcc.end()) { |
Michael Kruse | 33d6c0b | 2015-09-25 18:53:27 +0000 | [diff] [blame] | 3395 | addExplicitAccess(Inst, Type, BasePointer->getValue(), Size, true, |
| 3396 | AccItr->second.DelinearizedSubscripts, |
| 3397 | AccItr->second.Shape->DelinearizedSizes, Val); |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 3398 | return; |
| 3399 | } |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3400 | |
| 3401 | // Check if the access depends on a loop contained in a non-affine subregion. |
| 3402 | bool isVariantInNonAffineLoop = false; |
| 3403 | if (BoxedLoops) { |
| 3404 | SetVector<const Loop *> Loops; |
| 3405 | findLoops(AccessFunction, Loops); |
| 3406 | for (const Loop *L : Loops) |
| 3407 | if (BoxedLoops->count(L)) |
| 3408 | isVariantInNonAffineLoop = true; |
| 3409 | } |
| 3410 | |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 3411 | InvariantLoadsSetTy AccessILS; |
| 3412 | bool IsAffine = |
| 3413 | !isVariantInNonAffineLoop && |
| 3414 | isAffineExpr(R, AccessFunction, *SE, BasePointer->getValue(), &AccessILS); |
| 3415 | |
| 3416 | for (LoadInst *LInst : AccessILS) |
| 3417 | if (!ScopRIL.count(LInst)) |
| 3418 | IsAffine = false; |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3419 | |
Michael Kruse | caac2b6 | 2015-09-26 15:51:44 +0000 | [diff] [blame] | 3420 | // 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] | 3421 | // elements as probably intended here. |
Tobias Grosser | a43b6e9 | 2015-09-27 17:54:50 +0000 | [diff] [blame] | 3422 | const SCEV *SizeSCEV = |
| 3423 | SE->getConstant(TD->getIntPtrType(Inst->getContext()), Size); |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3424 | |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 3425 | if (!IsAffine && Type == MemoryAccess::MUST_WRITE) |
| 3426 | Type = MemoryAccess::MAY_WRITE; |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3427 | |
Michael Kruse | 33d6c0b | 2015-09-25 18:53:27 +0000 | [diff] [blame] | 3428 | addExplicitAccess(Inst, Type, BasePointer->getValue(), Size, IsAffine, |
| 3429 | ArrayRef<const SCEV *>(AccessFunction), |
| 3430 | ArrayRef<const SCEV *>(SizeSCEV), Val); |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3431 | } |
| 3432 | |
Michael Kruse | d868b5d | 2015-09-10 15:25:24 +0000 | [diff] [blame] | 3433 | void ScopInfo::buildAccessFunctions(Region &R, Region &SR) { |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3434 | |
| 3435 | if (SD->isNonAffineSubRegion(&SR, &R)) { |
| 3436 | for (BasicBlock *BB : SR.blocks()) |
| 3437 | buildAccessFunctions(R, *BB, &SR); |
| 3438 | return; |
| 3439 | } |
| 3440 | |
| 3441 | for (auto I = SR.element_begin(), E = SR.element_end(); I != E; ++I) |
| 3442 | if (I->isSubRegion()) |
| 3443 | buildAccessFunctions(R, *I->getNodeAs<Region>()); |
| 3444 | else |
| 3445 | buildAccessFunctions(R, *I->getNodeAs<BasicBlock>()); |
| 3446 | } |
| 3447 | |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 3448 | void ScopInfo::buildStmts(Region &SR) { |
| 3449 | Region *R = getRegion(); |
| 3450 | |
| 3451 | if (SD->isNonAffineSubRegion(&SR, R)) { |
| 3452 | scop->addScopStmt(nullptr, &SR); |
| 3453 | return; |
| 3454 | } |
| 3455 | |
| 3456 | for (auto I = SR.element_begin(), E = SR.element_end(); I != E; ++I) |
| 3457 | if (I->isSubRegion()) |
| 3458 | buildStmts(*I->getNodeAs<Region>()); |
| 3459 | else |
| 3460 | scop->addScopStmt(I->getNodeAs<BasicBlock>(), nullptr); |
| 3461 | } |
| 3462 | |
Michael Kruse | d868b5d | 2015-09-10 15:25:24 +0000 | [diff] [blame] | 3463 | void ScopInfo::buildAccessFunctions(Region &R, BasicBlock &BB, |
| 3464 | Region *NonAffineSubRegion, |
| 3465 | bool IsExitBlock) { |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3466 | Loop *L = LI->getLoopFor(&BB); |
| 3467 | |
| 3468 | // The set of loops contained in non-affine subregions that are part of R. |
| 3469 | const ScopDetection::BoxedLoopsSetTy *BoxedLoops = SD->getBoxedLoops(&R); |
| 3470 | |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 3471 | // The set of loads that are required to be invariant. |
| 3472 | auto &ScopRIL = *SD->getRequiredInvariantLoads(&R); |
| 3473 | |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3474 | for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I) { |
| 3475 | Instruction *Inst = I; |
| 3476 | |
| 3477 | PHINode *PHI = dyn_cast<PHINode>(Inst); |
| 3478 | if (PHI) |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 3479 | buildPHIAccesses(PHI, R, NonAffineSubRegion, IsExitBlock); |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3480 | |
| 3481 | // For the exit block we stop modeling after the last PHI node. |
| 3482 | if (!PHI && IsExitBlock) |
| 3483 | break; |
| 3484 | |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 3485 | // TODO: At this point we only know that elements of ScopRIL have to be |
| 3486 | // invariant and will be hoisted for the SCoP to be processed. Though, |
| 3487 | // there might be other invariant accesses that will be hoisted and |
| 3488 | // that would allow to make a non-affine access affine. |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3489 | if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst)) |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 3490 | buildMemoryAccess(Inst, L, &R, BoxedLoops, ScopRIL); |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3491 | |
| 3492 | if (isIgnoredIntrinsic(Inst)) |
| 3493 | continue; |
| 3494 | |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 3495 | // Do not build scalar dependences for required invariant loads as we will |
| 3496 | // hoist them later on anyway or drop the SCoP if we cannot. |
| 3497 | if (ScopRIL.count(dyn_cast<LoadInst>(Inst))) |
| 3498 | continue; |
| 3499 | |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3500 | if (buildScalarDependences(Inst, &R, NonAffineSubRegion)) { |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 3501 | if (!isa<StoreInst>(Inst)) |
Michael Kruse | 33d6c0b | 2015-09-25 18:53:27 +0000 | [diff] [blame] | 3502 | addScalarWriteAccess(Inst); |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3503 | } |
| 3504 | } |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 3505 | } |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3506 | |
Michael Kruse | 2d0ece9 | 2015-09-24 11:41:21 +0000 | [diff] [blame] | 3507 | void ScopInfo::addMemoryAccess(BasicBlock *BB, Instruction *Inst, |
| 3508 | MemoryAccess::AccessType Type, |
| 3509 | Value *BaseAddress, unsigned ElemBytes, |
| 3510 | bool Affine, Value *AccessValue, |
| 3511 | ArrayRef<const SCEV *> Subscripts, |
Michael Kruse | 8d0b734 | 2015-09-25 21:21:00 +0000 | [diff] [blame] | 3512 | ArrayRef<const SCEV *> Sizes, |
| 3513 | MemoryAccess::AccessOrigin Origin) { |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 3514 | ScopStmt *Stmt = scop->getStmtForBasicBlock(BB); |
| 3515 | |
| 3516 | // Do not create a memory access for anything not in the SCoP. It would be |
| 3517 | // ignored anyway. |
| 3518 | if (!Stmt) |
| 3519 | return; |
| 3520 | |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 3521 | AccFuncSetType &AccList = AccFuncMap[BB]; |
| 3522 | size_t Identifier = AccList.size(); |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3523 | |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 3524 | Value *BaseAddr = BaseAddress; |
| 3525 | std::string BaseName = getIslCompatibleName("MemRef_", BaseAddr, ""); |
| 3526 | |
| 3527 | std::string IdName = "__polly_array_ref_" + std::to_string(Identifier); |
| 3528 | isl_id *Id = isl_id_alloc(ctx, IdName.c_str(), nullptr); |
| 3529 | |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 3530 | bool isApproximated = |
| 3531 | Stmt->isRegionStmt() && (Stmt->getRegion()->getEntry() != BB); |
| 3532 | if (isApproximated && Type == MemoryAccess::MUST_WRITE) |
| 3533 | Type = MemoryAccess::MAY_WRITE; |
| 3534 | |
| 3535 | AccList.emplace_back(Stmt, Inst, Id, Type, BaseAddress, ElemBytes, Affine, |
Michael Kruse | 8d0b734 | 2015-09-25 21:21:00 +0000 | [diff] [blame] | 3536 | Subscripts, Sizes, AccessValue, Origin, BaseName); |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 3537 | Stmt->addAccess(&AccList.back()); |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3538 | } |
| 3539 | |
Michael Kruse | 33d6c0b | 2015-09-25 18:53:27 +0000 | [diff] [blame] | 3540 | void ScopInfo::addExplicitAccess( |
| 3541 | Instruction *MemAccInst, MemoryAccess::AccessType Type, Value *BaseAddress, |
| 3542 | unsigned ElemBytes, bool IsAffine, ArrayRef<const SCEV *> Subscripts, |
| 3543 | ArrayRef<const SCEV *> Sizes, Value *AccessValue) { |
| 3544 | assert(isa<LoadInst>(MemAccInst) || isa<StoreInst>(MemAccInst)); |
| 3545 | assert(isa<LoadInst>(MemAccInst) == (Type == MemoryAccess::READ)); |
| 3546 | addMemoryAccess(MemAccInst->getParent(), MemAccInst, Type, BaseAddress, |
Michael Kruse | 8d0b734 | 2015-09-25 21:21:00 +0000 | [diff] [blame] | 3547 | ElemBytes, IsAffine, AccessValue, Subscripts, Sizes, |
| 3548 | MemoryAccess::EXPLICIT); |
Michael Kruse | 33d6c0b | 2015-09-25 18:53:27 +0000 | [diff] [blame] | 3549 | } |
| 3550 | void ScopInfo::addScalarWriteAccess(Instruction *Value) { |
| 3551 | addMemoryAccess(Value->getParent(), Value, MemoryAccess::MUST_WRITE, Value, 1, |
| 3552 | true, Value, ArrayRef<const SCEV *>(), |
Michael Kruse | 8d0b734 | 2015-09-25 21:21:00 +0000 | [diff] [blame] | 3553 | ArrayRef<const SCEV *>(), MemoryAccess::SCALAR); |
Michael Kruse | 33d6c0b | 2015-09-25 18:53:27 +0000 | [diff] [blame] | 3554 | } |
| 3555 | void ScopInfo::addScalarReadAccess(Value *Value, Instruction *User) { |
| 3556 | assert(!isa<PHINode>(User)); |
| 3557 | addMemoryAccess(User->getParent(), User, MemoryAccess::READ, Value, 1, true, |
| 3558 | Value, ArrayRef<const SCEV *>(), ArrayRef<const SCEV *>(), |
Michael Kruse | 8d0b734 | 2015-09-25 21:21:00 +0000 | [diff] [blame] | 3559 | MemoryAccess::SCALAR); |
Michael Kruse | 33d6c0b | 2015-09-25 18:53:27 +0000 | [diff] [blame] | 3560 | } |
| 3561 | void ScopInfo::addScalarReadAccess(Value *Value, PHINode *User, |
| 3562 | BasicBlock *UserBB) { |
| 3563 | addMemoryAccess(UserBB, User, MemoryAccess::READ, Value, 1, true, Value, |
Michael Kruse | 8d0b734 | 2015-09-25 21:21:00 +0000 | [diff] [blame] | 3564 | ArrayRef<const SCEV *>(), ArrayRef<const SCEV *>(), |
| 3565 | MemoryAccess::SCALAR); |
Michael Kruse | 33d6c0b | 2015-09-25 18:53:27 +0000 | [diff] [blame] | 3566 | } |
| 3567 | void ScopInfo::addPHIWriteAccess(PHINode *PHI, BasicBlock *IncomingBlock, |
| 3568 | Value *IncomingValue, bool IsExitBlock) { |
| 3569 | addMemoryAccess(IncomingBlock, IncomingBlock->getTerminator(), |
| 3570 | MemoryAccess::MUST_WRITE, PHI, 1, true, IncomingValue, |
| 3571 | ArrayRef<const SCEV *>(), ArrayRef<const SCEV *>(), |
Michael Kruse | 8d0b734 | 2015-09-25 21:21:00 +0000 | [diff] [blame] | 3572 | IsExitBlock ? MemoryAccess::SCALAR : MemoryAccess::PHI); |
Michael Kruse | 33d6c0b | 2015-09-25 18:53:27 +0000 | [diff] [blame] | 3573 | } |
| 3574 | void ScopInfo::addPHIReadAccess(PHINode *PHI) { |
| 3575 | addMemoryAccess(PHI->getParent(), PHI, MemoryAccess::READ, PHI, 1, true, PHI, |
Michael Kruse | 8d0b734 | 2015-09-25 21:21:00 +0000 | [diff] [blame] | 3576 | ArrayRef<const SCEV *>(), ArrayRef<const SCEV *>(), |
| 3577 | MemoryAccess::PHI); |
Michael Kruse | 33d6c0b | 2015-09-25 18:53:27 +0000 | [diff] [blame] | 3578 | } |
| 3579 | |
Michael Kruse | 76e924d | 2015-09-30 09:16:07 +0000 | [diff] [blame] | 3580 | void ScopInfo::buildScop(Region &R, DominatorTree &DT) { |
Michael Kruse | 9d08009 | 2015-09-11 21:41:48 +0000 | [diff] [blame] | 3581 | unsigned MaxLoopDepth = getMaxLoopDepthInRegion(R, *LI, *SD); |
Johannes Doerfert | d8dd863 | 2015-10-07 20:31:36 +0000 | [diff] [blame] | 3582 | scop = new Scop(R, AccFuncMap, *SD, *SE, DT, *LI, ctx, MaxLoopDepth); |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3583 | |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 3584 | buildStmts(R); |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3585 | buildAccessFunctions(R, R); |
| 3586 | |
| 3587 | // In case the region does not have an exiting block we will later (during |
| 3588 | // code generation) split the exit block. This will move potential PHI nodes |
| 3589 | // from the current exit block into the new region exiting block. Hence, PHI |
| 3590 | // nodes that are at this point not part of the region will be. |
| 3591 | // To handle these PHI nodes later we will now model their operands as scalar |
| 3592 | // accesses. Note that we do not model anything in the exit block if we have |
| 3593 | // an exiting block in the region, as there will not be any splitting later. |
| 3594 | if (!R.getExitingBlock()) |
| 3595 | buildAccessFunctions(R, *R.getExit(), nullptr, /* IsExitBlock */ true); |
| 3596 | |
Johannes Doerfert | d8dd863 | 2015-10-07 20:31:36 +0000 | [diff] [blame] | 3597 | scop->init(*AA); |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3598 | } |
| 3599 | |
Michael Kruse | d868b5d | 2015-09-10 15:25:24 +0000 | [diff] [blame] | 3600 | void ScopInfo::print(raw_ostream &OS, const Module *) const { |
Michael Kruse | 9d08009 | 2015-09-11 21:41:48 +0000 | [diff] [blame] | 3601 | if (!scop) { |
Michael Kruse | d868b5d | 2015-09-10 15:25:24 +0000 | [diff] [blame] | 3602 | OS << "Invalid Scop!\n"; |
Michael Kruse | 9d08009 | 2015-09-11 21:41:48 +0000 | [diff] [blame] | 3603 | return; |
| 3604 | } |
| 3605 | |
Michael Kruse | 9d08009 | 2015-09-11 21:41:48 +0000 | [diff] [blame] | 3606 | scop->print(OS); |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3607 | } |
| 3608 | |
Michael Kruse | d868b5d | 2015-09-10 15:25:24 +0000 | [diff] [blame] | 3609 | void ScopInfo::clear() { |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3610 | AccFuncMap.clear(); |
Michael Kruse | d868b5d | 2015-09-10 15:25:24 +0000 | [diff] [blame] | 3611 | if (scop) { |
| 3612 | delete scop; |
| 3613 | scop = 0; |
| 3614 | } |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 3615 | } |
| 3616 | |
| 3617 | //===----------------------------------------------------------------------===// |
Michael Kruse | 9d08009 | 2015-09-11 21:41:48 +0000 | [diff] [blame] | 3618 | ScopInfo::ScopInfo() : RegionPass(ID), scop(0) { |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 3619 | ctx = isl_ctx_alloc(); |
Tobias Grosser | 4a8e356 | 2011-12-07 07:42:51 +0000 | [diff] [blame] | 3620 | isl_options_set_on_error(ctx, ISL_ON_ERROR_ABORT); |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 3621 | } |
| 3622 | |
| 3623 | ScopInfo::~ScopInfo() { |
| 3624 | clear(); |
| 3625 | isl_ctx_free(ctx); |
| 3626 | } |
| 3627 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3628 | void ScopInfo::getAnalysisUsage(AnalysisUsage &AU) const { |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 3629 | AU.addRequired<LoopInfoWrapperPass>(); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 3630 | AU.addRequired<RegionInfoPass>(); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3631 | AU.addRequired<DominatorTreeWrapperPass>(); |
Michael Kruse | d868b5d | 2015-09-10 15:25:24 +0000 | [diff] [blame] | 3632 | AU.addRequiredTransitive<ScalarEvolutionWrapperPass>(); |
| 3633 | AU.addRequiredTransitive<ScopDetection>(); |
Chandler Carruth | 66ef16b | 2015-09-09 22:13:56 +0000 | [diff] [blame] | 3634 | AU.addRequired<AAResultsWrapperPass>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3635 | AU.setPreservesAll(); |
| 3636 | } |
| 3637 | |
| 3638 | bool ScopInfo::runOnRegion(Region *R, RGPassManager &RGM) { |
Michael Kruse | d868b5d | 2015-09-10 15:25:24 +0000 | [diff] [blame] | 3639 | SD = &getAnalysis<ScopDetection>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3640 | |
Michael Kruse | d868b5d | 2015-09-10 15:25:24 +0000 | [diff] [blame] | 3641 | if (!SD->isMaxRegionInScop(*R)) |
| 3642 | return false; |
| 3643 | |
| 3644 | Function *F = R->getEntry()->getParent(); |
| 3645 | SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
| 3646 | LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 3647 | AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 3648 | TD = &F->getParent()->getDataLayout(); |
| 3649 | DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Michael Kruse | d868b5d | 2015-09-10 15:25:24 +0000 | [diff] [blame] | 3650 | |
Michael Kruse | 76e924d | 2015-09-30 09:16:07 +0000 | [diff] [blame] | 3651 | buildScop(*R, DT); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3652 | |
Tobias Grosser | d6a50b3 | 2015-05-30 06:26:21 +0000 | [diff] [blame] | 3653 | DEBUG(scop->print(dbgs())); |
| 3654 | |
Michael Kruse | afe0670 | 2015-10-02 16:33:27 +0000 | [diff] [blame] | 3655 | if (scop->isEmpty() || !scop->hasFeasibleRuntimeContext()) { |
Johannes Doerfert | 43788c5 | 2015-08-20 05:58:56 +0000 | [diff] [blame] | 3656 | delete scop; |
| 3657 | scop = nullptr; |
| 3658 | return false; |
| 3659 | } |
| 3660 | |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3661 | // Statistics. |
| 3662 | ++ScopFound; |
| 3663 | if (scop->getMaxLoopDepth() > 0) |
| 3664 | ++RichScopFound; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3665 | return false; |
| 3666 | } |
| 3667 | |
| 3668 | char ScopInfo::ID = 0; |
| 3669 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 3670 | Pass *polly::createScopInfoPass() { return new ScopInfo(); } |
| 3671 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 3672 | INITIALIZE_PASS_BEGIN(ScopInfo, "polly-scops", |
| 3673 | "Polly - Create polyhedral description of Scops", false, |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 3674 | false); |
Chandler Carruth | 66ef16b | 2015-09-09 22:13:56 +0000 | [diff] [blame] | 3675 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass); |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 3676 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 3677 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); |
Tobias Grosser | c5bcf24 | 2015-08-17 10:57:08 +0000 | [diff] [blame] | 3678 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass); |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 3679 | INITIALIZE_PASS_DEPENDENCY(ScopDetection); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3680 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 3681 | INITIALIZE_PASS_END(ScopInfo, "polly-scops", |
| 3682 | "Polly - Create polyhedral description of Scops", false, |
| 3683 | false) |