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