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