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