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