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