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