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