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