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