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