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