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