Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1 | //===- ScopInfo.cpp -------------------------------------------------------===// |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Create a polyhedral description for a static control flow region. |
| 10 | // |
| 11 | // The pass creates a polyhedral description of the Scops detected by the Scop |
| 12 | // detection derived from their LLVM-IR code. |
| 13 | // |
Tobias Grosser | a5605d3 | 2014-10-29 19:58:28 +0000 | [diff] [blame] | 14 | // This representation is shared among several tools in the polyhedral |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 15 | // community, which are e.g. Cloog, Pluto, Loopo, Graphite. |
| 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Tobias Grosser | 5624d3c | 2015-12-21 12:38:56 +0000 | [diff] [blame] | 19 | #include "polly/ScopInfo.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 20 | #include "polly/LinkAllPasses.h" |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 21 | #include "polly/Options.h" |
Michael Kruse | 73fa33b | 2016-06-28 01:37:28 +0000 | [diff] [blame] | 22 | #include "polly/ScopBuilder.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 23 | #include "polly/ScopDetection.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 24 | #include "polly/Support/GICHelper.h" |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 25 | #include "polly/Support/ISLOStream.h" |
Michael Kruse | e330071 | 2018-05-09 16:23:56 +0000 | [diff] [blame] | 26 | #include "polly/Support/ISLTools.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 27 | #include "polly/Support/SCEVAffinator.h" |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 28 | #include "polly/Support/SCEVValidator.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 29 | #include "polly/Support/ScopHelper.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/APInt.h" |
| 31 | #include "llvm/ADT/ArrayRef.h" |
Tobias Grosser | c2bb0cb | 2015-09-25 09:49:19 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/PostOrderIterator.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallPtrSet.h" |
| 34 | #include "llvm/ADT/SmallSet.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/Statistic.h" |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 36 | #include "llvm/Analysis/AliasAnalysis.h" |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 37 | #include "llvm/Analysis/AssumptionCache.h" |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 38 | #include "llvm/Analysis/Loads.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 39 | #include "llvm/Analysis/LoopInfo.h" |
Adam Nemet | e0f1541 | 2017-10-09 23:49:08 +0000 | [diff] [blame] | 40 | #include "llvm/Analysis/OptimizationRemarkEmitter.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 41 | #include "llvm/Analysis/RegionInfo.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 42 | #include "llvm/Analysis/RegionIterator.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 43 | #include "llvm/Analysis/ScalarEvolution.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 44 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 45 | #include "llvm/IR/BasicBlock.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 46 | #include "llvm/IR/ConstantRange.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 47 | #include "llvm/IR/DataLayout.h" |
| 48 | #include "llvm/IR/DebugLoc.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 49 | #include "llvm/IR/Dominators.h" |
| 50 | #include "llvm/IR/Function.h" |
| 51 | #include "llvm/IR/InstrTypes.h" |
| 52 | #include "llvm/IR/Instruction.h" |
| 53 | #include "llvm/IR/Instructions.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 54 | #include "llvm/IR/Module.h" |
| 55 | #include "llvm/IR/PassManager.h" |
| 56 | #include "llvm/IR/Type.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 57 | #include "llvm/IR/Value.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 58 | #include "llvm/Support/Compiler.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 59 | #include "llvm/Support/Debug.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 60 | #include "llvm/Support/ErrorHandling.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 61 | #include "llvm/Support/raw_ostream.h" |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 62 | #include "isl/aff.h" |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 63 | #include "isl/local_space.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 64 | #include "isl/map.h" |
Tobias Grosser | 4a8e356 | 2011-12-07 07:42:51 +0000 | [diff] [blame] | 65 | #include "isl/options.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 66 | #include "isl/set.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 67 | #include <cassert> |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 68 | |
| 69 | using namespace llvm; |
| 70 | using namespace polly; |
| 71 | |
Chandler Carruth | 95fef94 | 2014-04-22 03:30:19 +0000 | [diff] [blame] | 72 | #define DEBUG_TYPE "polly-scops" |
| 73 | |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 74 | STATISTIC(AssumptionsAliasing, "Number of aliasing assumptions taken."); |
| 75 | STATISTIC(AssumptionsInbounds, "Number of inbounds assumptions taken."); |
| 76 | STATISTIC(AssumptionsWrapping, "Number of wrapping assumptions taken."); |
| 77 | STATISTIC(AssumptionsUnsigned, "Number of unsigned assumptions taken."); |
| 78 | STATISTIC(AssumptionsComplexity, "Number of too complex SCoPs."); |
| 79 | STATISTIC(AssumptionsUnprofitable, "Number of unprofitable SCoPs."); |
| 80 | STATISTIC(AssumptionsErrorBlock, "Number of error block assumptions taken."); |
| 81 | STATISTIC(AssumptionsInfiniteLoop, "Number of bounded loop assumptions taken."); |
| 82 | STATISTIC(AssumptionsInvariantLoad, |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 83 | "Number of invariant loads assumptions taken."); |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 84 | STATISTIC(AssumptionsDelinearization, |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 85 | "Number of delinearization assumptions taken."); |
| 86 | |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 87 | STATISTIC(NumScops, "Number of feasible SCoPs after ScopInfo"); |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 88 | STATISTIC(NumLoopsInScop, "Number of loops in scops"); |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 89 | STATISTIC(NumBoxedLoops, "Number of boxed loops in SCoPs after ScopInfo"); |
| 90 | STATISTIC(NumAffineLoops, "Number of affine loops in SCoPs after ScopInfo"); |
| 91 | |
Tobias Grosser | fcc3ad5 | 2018-04-18 20:03:36 +0000 | [diff] [blame] | 92 | STATISTIC(NumScopsDepthZero, "Number of scops with maximal loop depth 0"); |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 93 | STATISTIC(NumScopsDepthOne, "Number of scops with maximal loop depth 1"); |
| 94 | STATISTIC(NumScopsDepthTwo, "Number of scops with maximal loop depth 2"); |
| 95 | STATISTIC(NumScopsDepthThree, "Number of scops with maximal loop depth 3"); |
| 96 | STATISTIC(NumScopsDepthFour, "Number of scops with maximal loop depth 4"); |
| 97 | STATISTIC(NumScopsDepthFive, "Number of scops with maximal loop depth 5"); |
| 98 | STATISTIC(NumScopsDepthLarger, |
| 99 | "Number of scops with maximal loop depth 6 and larger"); |
| 100 | STATISTIC(MaxNumLoopsInScop, "Maximal number of loops in scops"); |
| 101 | |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 102 | STATISTIC(NumValueWrites, "Number of scalar value writes after ScopInfo"); |
| 103 | STATISTIC( |
| 104 | NumValueWritesInLoops, |
| 105 | "Number of scalar value writes nested in affine loops after ScopInfo"); |
| 106 | STATISTIC(NumPHIWrites, "Number of scalar phi writes after ScopInfo"); |
| 107 | STATISTIC(NumPHIWritesInLoops, |
| 108 | "Number of scalar phi writes nested in affine loops after ScopInfo"); |
| 109 | STATISTIC(NumSingletonWrites, "Number of singleton writes after ScopInfo"); |
| 110 | STATISTIC(NumSingletonWritesInLoops, |
| 111 | "Number of singleton writes nested in affine loops after ScopInfo"); |
| 112 | |
Michael Kruse | bb824c6 | 2019-06-12 22:40:08 +0000 | [diff] [blame] | 113 | int const polly::MaxDisjunctsInDomain = 20; |
Tobias Grosser | 75dc40c | 2015-12-20 13:31:48 +0000 | [diff] [blame] | 114 | |
Tobias Grosser | c8a8276 | 2017-02-16 19:11:25 +0000 | [diff] [blame] | 115 | // The number of disjunct in the context after which we stop to add more |
| 116 | // disjuncts. This parameter is there to avoid exponential growth in the |
| 117 | // number of disjunct when adding non-convex sets to the context. |
| 118 | static int const MaxDisjunctsInContext = 4; |
| 119 | |
Johannes Doerfert | 2f70584 | 2016-04-12 16:09:44 +0000 | [diff] [blame] | 120 | static cl::opt<bool> PollyRemarksMinimal( |
| 121 | "polly-remarks-minimal", |
| 122 | cl::desc("Do not emit remarks about assumptions that are known"), |
| 123 | cl::Hidden, cl::ZeroOrMore, cl::init(false), cl::cat(PollyCategory)); |
| 124 | |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 125 | static cl::opt<std::string> UserContextStr( |
| 126 | "polly-context", cl::value_desc("isl parameter set"), |
| 127 | cl::desc("Provide additional constraints on the context parameters"), |
| 128 | cl::init(""), cl::cat(PollyCategory)); |
Tobias Grosser | 7150072 | 2015-03-28 15:11:14 +0000 | [diff] [blame] | 129 | |
Tobias Grosser | 2937b59 | 2016-04-29 11:43:20 +0000 | [diff] [blame] | 130 | static cl::opt<bool> |
| 131 | IslOnErrorAbort("polly-on-isl-error-abort", |
| 132 | cl::desc("Abort if an isl error is encountered"), |
| 133 | cl::init(true), cl::cat(PollyCategory)); |
| 134 | |
Tobias Grosser | d7c4975 | 2017-02-28 09:45:54 +0000 | [diff] [blame] | 135 | static cl::opt<bool> PollyPreciseInbounds( |
| 136 | "polly-precise-inbounds", |
| 137 | cl::desc("Take more precise inbounds assumptions (do not scale well)"), |
| 138 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
| 139 | |
Tobias Grosser | 8a6e605 | 2017-03-17 12:26:58 +0000 | [diff] [blame] | 140 | static cl::opt<bool> |
| 141 | PollyIgnoreInbounds("polly-ignore-inbounds", |
| 142 | cl::desc("Do not take inbounds assumptions at all"), |
| 143 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
| 144 | |
Tobias Grosser | 5842dee | 2017-03-17 13:00:53 +0000 | [diff] [blame] | 145 | static cl::opt<bool> PollyIgnoreParamBounds( |
| 146 | "polly-ignore-parameter-bounds", |
| 147 | cl::desc( |
| 148 | "Do not add parameter bounds and do no gist simplify sets accordingly"), |
| 149 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
| 150 | |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 151 | static cl::opt<bool> PollyPreciseFoldAccesses( |
| 152 | "polly-precise-fold-accesses", |
Michael Kruse | 6e7854a | 2017-04-03 12:03:38 +0000 | [diff] [blame] | 153 | cl::desc("Fold memory accesses to model more possible delinearizations " |
| 154 | "(does not scale well)"), |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 155 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 156 | |
Michael Kruse | 5ae08c0 | 2017-05-06 14:03:58 +0000 | [diff] [blame] | 157 | bool polly::UseInstructionNames; |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 158 | |
Michael Kruse | 5ae08c0 | 2017-05-06 14:03:58 +0000 | [diff] [blame] | 159 | static cl::opt<bool, true> XUseInstructionNames( |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 160 | "polly-use-llvm-names", |
Michael Kruse | 5ae08c0 | 2017-05-06 14:03:58 +0000 | [diff] [blame] | 161 | cl::desc("Use LLVM-IR names when deriving statement names"), |
| 162 | cl::location(UseInstructionNames), cl::Hidden, cl::init(false), |
| 163 | cl::ZeroOrMore, cl::cat(PollyCategory)); |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 164 | |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 165 | static cl::opt<bool> PollyPrintInstructions( |
| 166 | "polly-print-instructions", cl::desc("Output instructions per ScopStmt"), |
| 167 | cl::Hidden, cl::Optional, cl::init(false), cl::cat(PollyCategory)); |
| 168 | |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 169 | //===----------------------------------------------------------------------===// |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 170 | |
Michael Kruse | 046dde4 | 2015-08-10 13:01:57 +0000 | [diff] [blame] | 171 | // Create a sequence of two schedules. Either argument may be null and is |
| 172 | // interpreted as the empty schedule. Can also return null if both schedules are |
| 173 | // empty. |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 174 | static isl::schedule combineInSequence(isl::schedule Prev, isl::schedule Succ) { |
Michael Kruse | 046dde4 | 2015-08-10 13:01:57 +0000 | [diff] [blame] | 175 | if (!Prev) |
| 176 | return Succ; |
| 177 | if (!Succ) |
| 178 | return Prev; |
| 179 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 180 | return Prev.sequence(Succ); |
Michael Kruse | 046dde4 | 2015-08-10 13:01:57 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 183 | static isl::set addRangeBoundsToSet(isl::set S, const ConstantRange &Range, |
| 184 | int dim, isl::dim type) { |
| 185 | isl::val V; |
| 186 | isl::ctx Ctx = S.get_ctx(); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 187 | |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 188 | // The upper and lower bound for a parameter value is derived either from |
| 189 | // the data type of the parameter or from the - possibly more restrictive - |
| 190 | // range metadata. |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 191 | V = valFromAPInt(Ctx.get(), Range.getSignedMin(), true); |
| 192 | S = S.lower_bound_val(type, dim, V); |
| 193 | V = valFromAPInt(Ctx.get(), Range.getSignedMax(), true); |
| 194 | S = S.upper_bound_val(type, dim, V); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 195 | |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 196 | if (Range.isFullSet()) |
| 197 | return S; |
| 198 | |
Philip Pfaffe | 9375d57 | 2018-05-16 14:05:03 +0000 | [diff] [blame] | 199 | if (S.n_basic_set() > MaxDisjunctsInContext) |
Tobias Grosser | c8a8276 | 2017-02-16 19:11:25 +0000 | [diff] [blame] | 200 | return S; |
| 201 | |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 202 | // In case of signed wrapping, we can refine the set of valid values by |
| 203 | // excluding the part not covered by the wrapping range. |
| 204 | if (Range.isSignWrappedSet()) { |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 205 | V = valFromAPInt(Ctx.get(), Range.getLower(), true); |
| 206 | isl::set SLB = S.lower_bound_val(type, dim, V); |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 207 | |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 208 | V = valFromAPInt(Ctx.get(), Range.getUpper(), true); |
| 209 | V = V.sub_ui(1); |
| 210 | isl::set SUB = S.upper_bound_val(type, dim, V); |
| 211 | S = SLB.unite(SUB); |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 212 | } |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 213 | |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 214 | return S; |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 217 | static const ScopArrayInfo *identifyBasePtrOriginSAI(Scop *S, Value *BasePtr) { |
| 218 | LoadInst *BasePtrLI = dyn_cast<LoadInst>(BasePtr); |
| 219 | if (!BasePtrLI) |
| 220 | return nullptr; |
| 221 | |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 222 | if (!S->contains(BasePtrLI)) |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 223 | return nullptr; |
| 224 | |
| 225 | ScalarEvolution &SE = *S->getSE(); |
| 226 | |
| 227 | auto *OriginBaseSCEV = |
| 228 | SE.getPointerBase(SE.getSCEV(BasePtrLI->getPointerOperand())); |
| 229 | if (!OriginBaseSCEV) |
| 230 | return nullptr; |
| 231 | |
| 232 | auto *OriginBaseSCEVUnknown = dyn_cast<SCEVUnknown>(OriginBaseSCEV); |
| 233 | if (!OriginBaseSCEVUnknown) |
| 234 | return nullptr; |
| 235 | |
Tobias Grosser | 6abc75a | 2015-11-10 17:31:31 +0000 | [diff] [blame] | 236 | return S->getScopArrayInfo(OriginBaseSCEVUnknown->getValue(), |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 237 | MemoryKind::Array); |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Tobias Grosser | 27db02b | 2017-08-06 17:25:05 +0000 | [diff] [blame] | 240 | ScopArrayInfo::ScopArrayInfo(Value *BasePtr, Type *ElementType, isl::ctx Ctx, |
Hongbin Zheng | 6aded2a | 2017-01-15 16:47:26 +0000 | [diff] [blame] | 241 | ArrayRef<const SCEV *> Sizes, MemoryKind Kind, |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 242 | const DataLayout &DL, Scop *S, |
| 243 | const char *BaseName) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 244 | : BasePtr(BasePtr), ElementType(ElementType), Kind(Kind), DL(DL), S(*S) { |
Tobias Grosser | 9224522 | 2015-07-28 14:53:44 +0000 | [diff] [blame] | 245 | std::string BasePtrName = |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 246 | BaseName ? BaseName |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 247 | : getIslCompatibleName("MemRef", BasePtr, S->getNextArrayIdx(), |
| 248 | Kind == MemoryKind::PHI ? "__phi" : "", |
| 249 | UseInstructionNames); |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 250 | Id = isl::id::alloc(Ctx, BasePtrName, this); |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 251 | |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 252 | updateSizes(Sizes); |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 253 | |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 254 | if (!BasePtr || Kind != MemoryKind::Array) { |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 255 | BasePtrOriginSAI = nullptr; |
| 256 | return; |
| 257 | } |
| 258 | |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 259 | BasePtrOriginSAI = identifyBasePtrOriginSAI(S, BasePtr); |
| 260 | if (BasePtrOriginSAI) |
| 261 | const_cast<ScopArrayInfo *>(BasePtrOriginSAI)->addDerivedSAI(this); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 264 | ScopArrayInfo::~ScopArrayInfo() = default; |
| 265 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 266 | isl::space ScopArrayInfo::getSpace() const { |
| 267 | auto Space = isl::space(Id.get_ctx(), 0, getNumberOfDimensions()); |
| 268 | Space = Space.set_tuple_id(isl::dim::set, Id); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 269 | return Space; |
| 270 | } |
| 271 | |
Tobias Grosser | fe74a7a | 2016-09-17 19:22:18 +0000 | [diff] [blame] | 272 | bool ScopArrayInfo::isReadOnly() { |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 273 | isl::union_set WriteSet = S.getWrites().range(); |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 274 | isl::space Space = getSpace(); |
Tobias Grosser | 2ade986 | 2017-05-23 06:41:04 +0000 | [diff] [blame] | 275 | WriteSet = WriteSet.extract_set(Space); |
Tobias Grosser | fe74a7a | 2016-09-17 19:22:18 +0000 | [diff] [blame] | 276 | |
Tobias Grosser | 2ade986 | 2017-05-23 06:41:04 +0000 | [diff] [blame] | 277 | return bool(WriteSet.is_empty()); |
Tobias Grosser | fe74a7a | 2016-09-17 19:22:18 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 280 | bool ScopArrayInfo::isCompatibleWith(const ScopArrayInfo *Array) const { |
| 281 | if (Array->getElementType() != getElementType()) |
| 282 | return false; |
| 283 | |
| 284 | if (Array->getNumberOfDimensions() != getNumberOfDimensions()) |
| 285 | return false; |
| 286 | |
| 287 | for (unsigned i = 0; i < getNumberOfDimensions(); i++) |
| 288 | if (Array->getDimensionSize(i) != getDimensionSize(i)) |
| 289 | return false; |
| 290 | |
| 291 | return true; |
| 292 | } |
| 293 | |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 294 | void ScopArrayInfo::updateElementType(Type *NewElementType) { |
| 295 | if (NewElementType == ElementType) |
| 296 | return; |
| 297 | |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 298 | auto OldElementSize = DL.getTypeAllocSizeInBits(ElementType); |
| 299 | auto NewElementSize = DL.getTypeAllocSizeInBits(NewElementType); |
| 300 | |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 301 | if (NewElementSize == OldElementSize || NewElementSize == 0) |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 302 | return; |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 303 | |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 304 | if (NewElementSize % OldElementSize == 0 && NewElementSize < OldElementSize) { |
| 305 | ElementType = NewElementType; |
| 306 | } else { |
| 307 | auto GCD = GreatestCommonDivisor64(NewElementSize, OldElementSize); |
| 308 | ElementType = IntegerType::get(ElementType->getContext(), GCD); |
| 309 | } |
| 310 | } |
| 311 | |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 312 | /// Make the ScopArrayInfo model a Fortran Array |
| 313 | void ScopArrayInfo::applyAndSetFAD(Value *FAD) { |
| 314 | assert(FAD && "got invalid Fortran array descriptor"); |
| 315 | if (this->FAD) { |
| 316 | assert(this->FAD == FAD && |
| 317 | "receiving different array descriptors for same array"); |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | assert(DimensionSizesPw.size() > 0 && !DimensionSizesPw[0]); |
| 322 | assert(!this->FAD); |
| 323 | this->FAD = FAD; |
| 324 | |
Tobias Grosser | b1ed3d9 | 2017-05-23 07:07:05 +0000 | [diff] [blame] | 325 | isl::space Space(S.getIslCtx(), 1, 0); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 326 | |
| 327 | std::string param_name = getName(); |
| 328 | param_name += "_fortranarr_size"; |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 329 | isl::id IdPwAff = isl::id::alloc(S.getIslCtx(), param_name, this); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 330 | |
Tobias Grosser | b1ed3d9 | 2017-05-23 07:07:05 +0000 | [diff] [blame] | 331 | Space = Space.set_dim_id(isl::dim::param, 0, IdPwAff); |
| 332 | isl::pw_aff PwAff = |
| 333 | isl::aff::var_on_domain(isl::local_space(Space), isl::dim::param, 0); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 334 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 335 | DimensionSizesPw[0] = PwAff; |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 338 | bool ScopArrayInfo::updateSizes(ArrayRef<const SCEV *> NewSizes, |
| 339 | bool CheckConsistency) { |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 340 | int SharedDims = std::min(NewSizes.size(), DimensionSizes.size()); |
| 341 | int ExtraDimsNew = NewSizes.size() - SharedDims; |
| 342 | int ExtraDimsOld = DimensionSizes.size() - SharedDims; |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 343 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 344 | if (CheckConsistency) { |
| 345 | for (int i = 0; i < SharedDims; i++) { |
| 346 | auto *NewSize = NewSizes[i + ExtraDimsNew]; |
| 347 | auto *KnownSize = DimensionSizes[i + ExtraDimsOld]; |
| 348 | if (NewSize && KnownSize && NewSize != KnownSize) |
| 349 | return false; |
| 350 | } |
Tobias Grosser | 8286b83 | 2015-11-02 11:29:32 +0000 | [diff] [blame] | 351 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 352 | if (DimensionSizes.size() >= NewSizes.size()) |
| 353 | return true; |
| 354 | } |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 355 | |
| 356 | DimensionSizes.clear(); |
| 357 | DimensionSizes.insert(DimensionSizes.begin(), NewSizes.begin(), |
| 358 | NewSizes.end()); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 359 | DimensionSizesPw.clear(); |
| 360 | for (const SCEV *Expr : DimensionSizes) { |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 361 | if (!Expr) { |
| 362 | DimensionSizesPw.push_back(nullptr); |
| 363 | continue; |
| 364 | } |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 365 | isl::pw_aff Size = S.getPwAffOnly(Expr); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 366 | DimensionSizesPw.push_back(Size); |
| 367 | } |
Tobias Grosser | 8286b83 | 2015-11-02 11:29:32 +0000 | [diff] [blame] | 368 | return true; |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 371 | std::string ScopArrayInfo::getName() const { return Id.get_name(); } |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 372 | |
| 373 | int ScopArrayInfo::getElemSizeInBytes() const { |
Johannes Doerfert | 55b3d8b | 2015-11-12 20:15:08 +0000 | [diff] [blame] | 374 | return DL.getTypeAllocSize(ElementType); |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 377 | isl::id ScopArrayInfo::getBasePtrId() const { return Id; } |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 378 | |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 379 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Michael Kruse | e186013 | 2017-07-21 15:54:13 +0000 | [diff] [blame] | 380 | LLVM_DUMP_METHOD void ScopArrayInfo::dump() const { print(errs()); } |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 381 | #endif |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 382 | |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 383 | void ScopArrayInfo::print(raw_ostream &OS, bool SizeAsPwAff) const { |
Tobias Grosser | 4ea2e07 | 2015-11-10 14:02:54 +0000 | [diff] [blame] | 384 | OS.indent(8) << *getElementType() << " " << getName(); |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 385 | unsigned u = 0; |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 386 | // If this is a Fortran array, then we can print the outermost dimension |
| 387 | // as a isl_pw_aff even though there is no SCEV information. |
| 388 | bool IsOutermostSizeKnown = SizeAsPwAff && FAD; |
| 389 | |
| 390 | if (!IsOutermostSizeKnown && getNumberOfDimensions() > 0 && |
| 391 | !getDimensionSize(0)) { |
Tobias Grosser | 4ea2e07 | 2015-11-10 14:02:54 +0000 | [diff] [blame] | 392 | OS << "[*]"; |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 393 | u++; |
| 394 | } |
| 395 | for (; u < getNumberOfDimensions(); u++) { |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 396 | OS << "["; |
| 397 | |
Tobias Grosser | 2625384 | 2015-11-10 14:24:21 +0000 | [diff] [blame] | 398 | if (SizeAsPwAff) { |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 399 | isl::pw_aff Size = getDimensionSizePw(u); |
Tobias Grosser | 2625384 | 2015-11-10 14:24:21 +0000 | [diff] [blame] | 400 | OS << " " << Size << " "; |
Tobias Grosser | 2625384 | 2015-11-10 14:24:21 +0000 | [diff] [blame] | 401 | } else { |
| 402 | OS << *getDimensionSize(u); |
| 403 | } |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 404 | |
| 405 | OS << "]"; |
| 406 | } |
| 407 | |
Tobias Grosser | 4ea2e07 | 2015-11-10 14:02:54 +0000 | [diff] [blame] | 408 | OS << ";"; |
| 409 | |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 410 | if (BasePtrOriginSAI) |
| 411 | OS << " [BasePtrOrigin: " << BasePtrOriginSAI->getName() << "]"; |
| 412 | |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 413 | OS << " // Element size " << getElemSizeInBytes() << "\n"; |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | const ScopArrayInfo * |
Tobias Grosser | 206e9e3 | 2017-07-24 16:22:27 +0000 | [diff] [blame] | 417 | ScopArrayInfo::getFromAccessFunction(isl::pw_multi_aff PMA) { |
| 418 | isl::id Id = PMA.get_tuple_id(isl::dim::out); |
| 419 | assert(!Id.is_null() && "Output dimension didn't have an ID"); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 420 | return getFromId(Id); |
| 421 | } |
| 422 | |
Tobias Grosser | 206e9e3 | 2017-07-24 16:22:27 +0000 | [diff] [blame] | 423 | const ScopArrayInfo *ScopArrayInfo::getFromId(isl::id Id) { |
| 424 | void *User = Id.get_user(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 425 | const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 426 | return SAI; |
| 427 | } |
| 428 | |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 429 | void MemoryAccess::wrapConstantDimensions() { |
| 430 | auto *SAI = getScopArrayInfo(); |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 431 | isl::space ArraySpace = SAI->getSpace(); |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 432 | isl::ctx Ctx = ArraySpace.get_ctx(); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 433 | unsigned DimsArray = SAI->getNumberOfDimensions(); |
| 434 | |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 435 | isl::multi_aff DivModAff = isl::multi_aff::identity( |
| 436 | ArraySpace.map_from_domain_and_range(ArraySpace)); |
| 437 | isl::local_space LArraySpace = isl::local_space(ArraySpace); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 438 | |
| 439 | // Begin with last dimension, to iteratively carry into higher dimensions. |
| 440 | for (int i = DimsArray - 1; i > 0; i--) { |
| 441 | auto *DimSize = SAI->getDimensionSize(i); |
| 442 | auto *DimSizeCst = dyn_cast<SCEVConstant>(DimSize); |
| 443 | |
| 444 | // This transformation is not applicable to dimensions with dynamic size. |
| 445 | if (!DimSizeCst) |
| 446 | continue; |
| 447 | |
Tobias Grosser | ca2cfd0 | 2017-02-17 04:48:52 +0000 | [diff] [blame] | 448 | // This transformation is not applicable to dimensions of size zero. |
| 449 | if (DimSize->isZero()) |
| 450 | continue; |
| 451 | |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 452 | isl::val DimSizeVal = |
| 453 | valFromAPInt(Ctx.get(), DimSizeCst->getAPInt(), false); |
| 454 | isl::aff Var = isl::aff::var_on_domain(LArraySpace, isl::dim::set, i); |
| 455 | isl::aff PrevVar = |
| 456 | isl::aff::var_on_domain(LArraySpace, isl::dim::set, i - 1); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 457 | |
| 458 | // Compute: index % size |
| 459 | // Modulo must apply in the divide of the previous iteration, if any. |
Tobias Grosser | cb0224a | 2017-08-06 15:56:45 +0000 | [diff] [blame] | 460 | isl::aff Modulo = Var.mod(DimSizeVal); |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 461 | Modulo = Modulo.pullback(DivModAff); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 462 | |
| 463 | // Compute: floor(index / size) |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 464 | isl::aff Divide = Var.div(isl::aff(LArraySpace, DimSizeVal)); |
| 465 | Divide = Divide.floor(); |
| 466 | Divide = Divide.add(PrevVar); |
| 467 | Divide = Divide.pullback(DivModAff); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 468 | |
| 469 | // Apply Modulo and Divide. |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 470 | DivModAff = DivModAff.set_aff(i, Modulo); |
| 471 | DivModAff = DivModAff.set_aff(i - 1, Divide); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | // Apply all modulo/divides on the accesses. |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 475 | isl::map Relation = AccessRelation; |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 476 | Relation = Relation.apply_range(isl::map::from_multi_aff(DivModAff)); |
| 477 | Relation = Relation.detect_equalities(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 478 | AccessRelation = Relation; |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 481 | void MemoryAccess::updateDimensionality() { |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 482 | auto *SAI = getScopArrayInfo(); |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 483 | isl::space ArraySpace = SAI->getSpace(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 484 | isl::space AccessSpace = AccessRelation.get_space().range(); |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 485 | isl::ctx Ctx = ArraySpace.get_ctx(); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 486 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 487 | auto DimsArray = ArraySpace.dim(isl::dim::set); |
| 488 | auto DimsAccess = AccessSpace.dim(isl::dim::set); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 489 | auto DimsMissing = DimsArray - DimsAccess; |
| 490 | |
Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 491 | auto *BB = getStatement()->getEntryBlock(); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 492 | auto &DL = BB->getModule()->getDataLayout(); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 493 | unsigned ArrayElemSize = SAI->getElemSizeInBytes(); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 494 | unsigned ElemBytes = DL.getTypeAllocSize(getElementType()); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 495 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 496 | isl::map Map = isl::map::from_domain_and_range( |
| 497 | isl::set::universe(AccessSpace), isl::set::universe(ArraySpace)); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 498 | |
| 499 | for (unsigned i = 0; i < DimsMissing; i++) |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 500 | Map = Map.fix_si(isl::dim::out, i, 0); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 501 | |
| 502 | for (unsigned i = DimsMissing; i < DimsArray; i++) |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 503 | Map = Map.equate(isl::dim::in, i - DimsMissing, isl::dim::out, i); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 504 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 505 | AccessRelation = AccessRelation.apply_range(Map); |
Roman Gareev | 10595a1 | 2016-01-08 14:01:59 +0000 | [diff] [blame] | 506 | |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 507 | // For the non delinearized arrays, divide the access function of the last |
| 508 | // subscript by the size of the elements in the array. |
| 509 | // |
| 510 | // A stride one array access in C expressed as A[i] is expressed in |
| 511 | // LLVM-IR as something like A[i * elementsize]. This hides the fact that |
| 512 | // two subsequent values of 'i' index two values that are stored next to |
| 513 | // each other in memory. By this division we make this characteristic |
| 514 | // obvious again. If the base pointer was accessed with offsets not divisible |
Tobias Grosser | 2219d15 | 2016-08-03 05:28:09 +0000 | [diff] [blame] | 515 | // by the accesses element size, we will have chosen a smaller ArrayElemSize |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 516 | // that divides the offsets of all accesses to this base pointer. |
| 517 | if (DimsAccess == 1) { |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 518 | isl::val V = isl::val(Ctx, ArrayElemSize); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 519 | AccessRelation = AccessRelation.floordiv_val(V); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 522 | // We currently do this only if we added at least one dimension, which means |
| 523 | // some dimension's indices have not been specified, an indicator that some |
| 524 | // index values have been added together. |
| 525 | // TODO: Investigate general usefulness; Effect on unit tests is to make index |
| 526 | // expressions more complicated. |
| 527 | if (DimsMissing) |
| 528 | wrapConstantDimensions(); |
| 529 | |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 530 | if (!isAffine()) |
| 531 | computeBoundsOnAccessRelation(ArrayElemSize); |
| 532 | |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 533 | // Introduce multi-element accesses in case the type loaded by this memory |
| 534 | // access is larger than the canonical element type of the array. |
| 535 | // |
| 536 | // An access ((float *)A)[i] to an array char *A is modeled as |
| 537 | // {[i] -> A[o] : 4 i <= o <= 4 i + 3 |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 538 | if (ElemBytes > ArrayElemSize) { |
| 539 | assert(ElemBytes % ArrayElemSize == 0 && |
| 540 | "Loaded element size should be multiple of canonical element size"); |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 541 | isl::map Map = isl::map::from_domain_and_range( |
| 542 | isl::set::universe(ArraySpace), isl::set::universe(ArraySpace)); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 543 | for (unsigned i = 0; i < DimsArray - 1; i++) |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 544 | Map = Map.equate(isl::dim::in, i, isl::dim::out, i); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 545 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 546 | isl::constraint C; |
| 547 | isl::local_space LS; |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 548 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 549 | LS = isl::local_space(Map.get_space()); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 550 | int Num = ElemBytes / getScopArrayInfo()->getElemSizeInBytes(); |
| 551 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 552 | C = isl::constraint::alloc_inequality(LS); |
| 553 | C = C.set_constant_val(isl::val(Ctx, Num - 1)); |
| 554 | C = C.set_coefficient_si(isl::dim::in, DimsArray - 1, 1); |
| 555 | C = C.set_coefficient_si(isl::dim::out, DimsArray - 1, -1); |
| 556 | Map = Map.add_constraint(C); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 557 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 558 | C = isl::constraint::alloc_inequality(LS); |
| 559 | C = C.set_coefficient_si(isl::dim::in, DimsArray - 1, -1); |
| 560 | C = C.set_coefficient_si(isl::dim::out, DimsArray - 1, 1); |
| 561 | C = C.set_constant_val(isl::val(Ctx, 0)); |
| 562 | Map = Map.add_constraint(C); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 563 | AccessRelation = AccessRelation.apply_range(Map); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 564 | } |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 567 | const std::string |
| 568 | MemoryAccess::getReductionOperatorStr(MemoryAccess::ReductionType RT) { |
| 569 | switch (RT) { |
| 570 | case MemoryAccess::RT_NONE: |
| 571 | llvm_unreachable("Requested a reduction operator string for a memory " |
| 572 | "access which isn't a reduction"); |
| 573 | case MemoryAccess::RT_ADD: |
| 574 | return "+"; |
| 575 | case MemoryAccess::RT_MUL: |
| 576 | return "*"; |
| 577 | case MemoryAccess::RT_BOR: |
| 578 | return "|"; |
| 579 | case MemoryAccess::RT_BXOR: |
| 580 | return "^"; |
| 581 | case MemoryAccess::RT_BAND: |
| 582 | return "&"; |
| 583 | } |
| 584 | llvm_unreachable("Unknown reduction type"); |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 587 | const ScopArrayInfo *MemoryAccess::getOriginalScopArrayInfo() const { |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 588 | isl::id ArrayId = getArrayId(); |
| 589 | void *User = ArrayId.get_user(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 590 | const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 591 | return SAI; |
| 592 | } |
| 593 | |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 594 | const ScopArrayInfo *MemoryAccess::getLatestScopArrayInfo() const { |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 595 | isl::id ArrayId = getLatestArrayId(); |
| 596 | void *User = ArrayId.get_user(); |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 597 | const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 598 | return SAI; |
| 599 | } |
| 600 | |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 601 | isl::id MemoryAccess::getOriginalArrayId() const { |
| 602 | return AccessRelation.get_tuple_id(isl::dim::out); |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 605 | isl::id MemoryAccess::getLatestArrayId() const { |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 606 | if (!hasNewAccessRelation()) |
| 607 | return getOriginalArrayId(); |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 608 | return NewAccessRelation.get_tuple_id(isl::dim::out); |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Tobias Grosser | 6a87036 | 2017-07-23 04:08:45 +0000 | [diff] [blame] | 611 | isl::map MemoryAccess::getAddressFunction() const { |
| 612 | return getAccessRelation().lexmin(); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Tobias Grosser | 3b19613 | 2017-07-23 04:08:52 +0000 | [diff] [blame] | 615 | isl::pw_multi_aff |
| 616 | MemoryAccess::applyScheduleToAccessRelation(isl::union_map USchedule) const { |
| 617 | isl::map Schedule, ScheduledAccRel; |
| 618 | isl::union_set UDomain; |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 619 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 620 | UDomain = getStatement()->getDomain(); |
Tobias Grosser | 3b19613 | 2017-07-23 04:08:52 +0000 | [diff] [blame] | 621 | USchedule = USchedule.intersect_domain(UDomain); |
| 622 | Schedule = isl::map::from_union_map(USchedule); |
| 623 | ScheduledAccRel = getAddressFunction().apply_domain(Schedule); |
| 624 | return isl::pw_multi_aff::from_map(ScheduledAccRel); |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Tobias Grosser | 22da5f0 | 2017-07-23 04:08:27 +0000 | [diff] [blame] | 627 | isl::map MemoryAccess::getOriginalAccessRelation() const { |
| 628 | return AccessRelation; |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 631 | std::string MemoryAccess::getOriginalAccessRelationStr() const { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 632 | return AccessRelation.to_str(); |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Tobias Grosser | 22da5f0 | 2017-07-23 04:08:27 +0000 | [diff] [blame] | 635 | isl::space MemoryAccess::getOriginalAccessRelationSpace() const { |
| 636 | return AccessRelation.get_space(); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 639 | isl::map MemoryAccess::getNewAccessRelation() const { |
| 640 | return NewAccessRelation; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Tobias Grosser | 6f73008 | 2015-09-05 07:46:47 +0000 | [diff] [blame] | 643 | std::string MemoryAccess::getNewAccessRelationStr() const { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 644 | return NewAccessRelation.to_str(); |
Tobias Grosser | 6f73008 | 2015-09-05 07:46:47 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Tobias Grosser | 6a4c12f | 2017-07-11 10:10:13 +0000 | [diff] [blame] | 647 | std::string MemoryAccess::getAccessRelationStr() const { |
Tobias Grosser | 2b7479b | 2017-08-06 11:41:10 +0000 | [diff] [blame] | 648 | return getAccessRelation().to_str(); |
Tobias Grosser | 6a4c12f | 2017-07-11 10:10:13 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Tobias Grosser | b6e7a85 | 2017-07-23 04:08:17 +0000 | [diff] [blame] | 651 | isl::basic_map MemoryAccess::createBasicAccessMap(ScopStmt *Statement) { |
| 652 | isl::space Space = isl::space(Statement->getIslCtx(), 0, 1); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 653 | Space = Space.align_params(Statement->getDomainSpace()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 654 | |
Tobias Grosser | b6e7a85 | 2017-07-23 04:08:17 +0000 | [diff] [blame] | 655 | return isl::basic_map::from_domain_and_range( |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 656 | isl::basic_set::universe(Statement->getDomainSpace()), |
Tobias Grosser | b6e7a85 | 2017-07-23 04:08:17 +0000 | [diff] [blame] | 657 | isl::basic_set::universe(Space)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 660 | // Formalize no out-of-bound access assumption |
| 661 | // |
| 662 | // When delinearizing array accesses we optimistically assume that the |
| 663 | // delinearized accesses do not access out of bound locations (the subscript |
| 664 | // expression of each array evaluates for each statement instance that is |
| 665 | // executed to a value that is larger than zero and strictly smaller than the |
| 666 | // size of the corresponding dimension). The only exception is the outermost |
Tobias Grosser | f57d63f | 2014-08-03 21:07:30 +0000 | [diff] [blame] | 667 | // dimension for which we do not need to assume any upper bound. At this point |
| 668 | // we formalize this assumption to ensure that at code generation time the |
| 669 | // relevant run-time checks can be generated. |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 670 | // |
| 671 | // To find the set of constraints necessary to avoid out of bound accesses, we |
| 672 | // first build the set of data locations that are not within array bounds. We |
| 673 | // then apply the reverse access relation to obtain the set of iterations that |
| 674 | // may contain invalid accesses and reduce this set of iterations to the ones |
| 675 | // that are actually executed by intersecting them with the domain of the |
| 676 | // statement. If we now project out all loop dimensions, we obtain a set of |
| 677 | // parameters that may cause statement instances to be executed that may |
| 678 | // possibly yield out of bound memory accesses. The complement of these |
| 679 | // constraints is the set of constraints that needs to be assumed to ensure such |
| 680 | // statement instances are never executed. |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 681 | void MemoryAccess::assumeNoOutOfBound() { |
Tobias Grosser | 8a6e605 | 2017-03-17 12:26:58 +0000 | [diff] [blame] | 682 | if (PollyIgnoreInbounds) |
| 683 | return; |
Johannes Doerfert | adeab37 | 2016-02-07 13:57:32 +0000 | [diff] [blame] | 684 | auto *SAI = getScopArrayInfo(); |
Tobias Grosser | 22da5f0 | 2017-07-23 04:08:27 +0000 | [diff] [blame] | 685 | isl::space Space = getOriginalAccessRelationSpace().range(); |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 686 | isl::set Outside = isl::set::empty(Space); |
| 687 | for (int i = 1, Size = Space.dim(isl::dim::set); i < Size; ++i) { |
| 688 | isl::local_space LS(Space); |
| 689 | isl::pw_aff Var = isl::pw_aff::var_on_domain(LS, isl::dim::set, i); |
| 690 | isl::pw_aff Zero = isl::pw_aff(LS); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 691 | |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 692 | isl::set DimOutside = Var.lt_set(Zero); |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 693 | isl::pw_aff SizeE = SAI->getDimensionSizePw(i); |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 694 | SizeE = SizeE.add_dims(isl::dim::in, Space.dim(isl::dim::set)); |
| 695 | SizeE = SizeE.set_tuple_id(isl::dim::in, Space.get_tuple_id(isl::dim::set)); |
| 696 | DimOutside = DimOutside.unite(SizeE.le_set(Var)); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 697 | |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 698 | Outside = Outside.unite(DimOutside); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 701 | Outside = Outside.apply(getAccessRelation().reverse()); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 702 | Outside = Outside.intersect(Statement->getDomain()); |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 703 | Outside = Outside.params(); |
Tobias Grosser | f54bb77 | 2015-06-26 12:09:28 +0000 | [diff] [blame] | 704 | |
| 705 | // Remove divs to avoid the construction of overly complicated assumptions. |
| 706 | // Doing so increases the set of parameter combinations that are assumed to |
| 707 | // not appear. This is always save, but may make the resulting run-time check |
| 708 | // bail out more often than strictly necessary. |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 709 | Outside = Outside.remove_divs(); |
| 710 | Outside = Outside.complement(); |
Michael Kruse | 7071e8b | 2016-04-11 13:24:29 +0000 | [diff] [blame] | 711 | const auto &Loc = getAccessInstruction() |
| 712 | ? getAccessInstruction()->getDebugLoc() |
| 713 | : DebugLoc(); |
Tobias Grosser | d7c4975 | 2017-02-28 09:45:54 +0000 | [diff] [blame] | 714 | if (!PollyPreciseInbounds) |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 715 | Outside = Outside.gist_params(Statement->getDomain().params()); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 716 | Statement->getParent()->recordAssumption(INBOUNDS, Outside, Loc, |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 717 | AS_ASSUMPTION); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 720 | void MemoryAccess::buildMemIntrinsicAccessRelation() { |
Johannes Doerfert | c976546 | 2016-11-17 22:11:56 +0000 | [diff] [blame] | 721 | assert(isMemoryIntrinsic()); |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 722 | assert(Subscripts.size() == 2 && Sizes.size() == 1); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 723 | |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 724 | isl::pw_aff SubscriptPWA = getPwAff(Subscripts[0]); |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 725 | isl::map SubscriptMap = isl::map::from_pw_aff(SubscriptPWA); |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 726 | |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 727 | isl::map LengthMap; |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 728 | if (Subscripts[1] == nullptr) { |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 729 | LengthMap = isl::map::universe(SubscriptMap.get_space()); |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 730 | } else { |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 731 | isl::pw_aff LengthPWA = getPwAff(Subscripts[1]); |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 732 | LengthMap = isl::map::from_pw_aff(LengthPWA); |
| 733 | isl::space RangeSpace = LengthMap.get_space().range(); |
| 734 | LengthMap = LengthMap.apply_range(isl::map::lex_gt(RangeSpace)); |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 735 | } |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 736 | LengthMap = LengthMap.lower_bound_si(isl::dim::out, 0, 0); |
| 737 | LengthMap = LengthMap.align_params(SubscriptMap.get_space()); |
| 738 | SubscriptMap = SubscriptMap.align_params(LengthMap.get_space()); |
| 739 | LengthMap = LengthMap.sum(SubscriptMap); |
| 740 | AccessRelation = |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 741 | LengthMap.set_tuple_id(isl::dim::in, getStatement()->getDomainId()); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 744 | void MemoryAccess::computeBoundsOnAccessRelation(unsigned ElementSize) { |
| 745 | ScalarEvolution *SE = Statement->getParent()->getSE(); |
| 746 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 747 | auto MAI = MemAccInst(getAccessInstruction()); |
Hongbin Zheng | 8efb22e | 2016-02-27 01:49:58 +0000 | [diff] [blame] | 748 | if (isa<MemIntrinsic>(MAI)) |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 749 | return; |
| 750 | |
| 751 | Value *Ptr = MAI.getPointerOperand(); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 752 | if (!Ptr || !SE->isSCEVable(Ptr->getType())) |
| 753 | return; |
| 754 | |
| 755 | auto *PtrSCEV = SE->getSCEV(Ptr); |
| 756 | if (isa<SCEVCouldNotCompute>(PtrSCEV)) |
| 757 | return; |
| 758 | |
| 759 | auto *BasePtrSCEV = SE->getPointerBase(PtrSCEV); |
| 760 | if (BasePtrSCEV && !isa<SCEVCouldNotCompute>(BasePtrSCEV)) |
| 761 | PtrSCEV = SE->getMinusSCEV(PtrSCEV, BasePtrSCEV); |
| 762 | |
| 763 | const ConstantRange &Range = SE->getSignedRange(PtrSCEV); |
| 764 | if (Range.isFullSet()) |
| 765 | return; |
| 766 | |
Nikita Popov | 6d855ea | 2019-03-27 18:19:33 +0000 | [diff] [blame] | 767 | if (Range.isUpperWrapped() || Range.isSignWrappedSet()) |
Tobias Grosser | b3a8588 | 2017-02-12 08:11:12 +0000 | [diff] [blame] | 768 | return; |
| 769 | |
Johannes Doerfert | e4bd53b | 2015-03-08 19:49:50 +0000 | [diff] [blame] | 770 | bool isWrapping = Range.isSignWrappedSet(); |
Tobias Grosser | b3a8588 | 2017-02-12 08:11:12 +0000 | [diff] [blame] | 771 | |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 772 | unsigned BW = Range.getBitWidth(); |
Johannes Doerfert | e708790 | 2016-02-07 13:59:03 +0000 | [diff] [blame] | 773 | const auto One = APInt(BW, 1); |
Johannes Doerfert | e4bd53b | 2015-03-08 19:49:50 +0000 | [diff] [blame] | 774 | const auto LB = isWrapping ? Range.getLower() : Range.getSignedMin(); |
Johannes Doerfert | e708790 | 2016-02-07 13:59:03 +0000 | [diff] [blame] | 775 | const auto UB = isWrapping ? (Range.getUpper() - One) : Range.getSignedMax(); |
Johannes Doerfert | e4bd53b | 2015-03-08 19:49:50 +0000 | [diff] [blame] | 776 | |
| 777 | auto Min = LB.sdiv(APInt(BW, ElementSize)); |
Johannes Doerfert | e708790 | 2016-02-07 13:59:03 +0000 | [diff] [blame] | 778 | auto Max = UB.sdiv(APInt(BW, ElementSize)) + One; |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 779 | |
Tobias Grosser | b3a8588 | 2017-02-12 08:11:12 +0000 | [diff] [blame] | 780 | assert(Min.sle(Max) && "Minimum expected to be less or equal than max"); |
| 781 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 782 | isl::map Relation = AccessRelation; |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 783 | isl::set AccessRange = Relation.range(); |
| 784 | AccessRange = addRangeBoundsToSet(AccessRange, ConstantRange(Min, Max), 0, |
| 785 | isl::dim::set); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 786 | AccessRelation = Relation.intersect_range(AccessRange); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 789 | void MemoryAccess::foldAccessRelation() { |
| 790 | if (Sizes.size() < 2 || isa<SCEVConstant>(Sizes[1])) |
| 791 | return; |
| 792 | |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 793 | int Size = Subscripts.size(); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 794 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 795 | isl::map NewAccessRelation = AccessRelation; |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 796 | |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 797 | for (int i = Size - 2; i >= 0; --i) { |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 798 | isl::space Space; |
| 799 | isl::map MapOne, MapTwo; |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 800 | isl::pw_aff DimSize = getPwAff(Sizes[i + 1]); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 801 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 802 | isl::space SpaceSize = DimSize.get_space(); |
Tobias Grosser | d3d3d6b | 2018-04-29 00:28:26 +0000 | [diff] [blame] | 803 | isl::id ParamId = SpaceSize.get_dim_id(isl::dim::param, 0); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 804 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 805 | Space = AccessRelation.get_space(); |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 806 | Space = Space.range().map_from_set(); |
| 807 | Space = Space.align_params(SpaceSize); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 808 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 809 | int ParamLocation = Space.find_dim_by_id(isl::dim::param, ParamId); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 810 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 811 | MapOne = isl::map::universe(Space); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 812 | for (int j = 0; j < Size; ++j) |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 813 | MapOne = MapOne.equate(isl::dim::in, j, isl::dim::out, j); |
| 814 | MapOne = MapOne.lower_bound_si(isl::dim::in, i + 1, 0); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 815 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 816 | MapTwo = isl::map::universe(Space); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 817 | for (int j = 0; j < Size; ++j) |
| 818 | if (j < i || j > i + 1) |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 819 | MapTwo = MapTwo.equate(isl::dim::in, j, isl::dim::out, j); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 820 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 821 | isl::local_space LS(Space); |
| 822 | isl::constraint C; |
| 823 | C = isl::constraint::alloc_equality(LS); |
| 824 | C = C.set_constant_si(-1); |
| 825 | C = C.set_coefficient_si(isl::dim::in, i, 1); |
| 826 | C = C.set_coefficient_si(isl::dim::out, i, -1); |
| 827 | MapTwo = MapTwo.add_constraint(C); |
| 828 | C = isl::constraint::alloc_equality(LS); |
| 829 | C = C.set_coefficient_si(isl::dim::in, i + 1, 1); |
| 830 | C = C.set_coefficient_si(isl::dim::out, i + 1, -1); |
| 831 | C = C.set_coefficient_si(isl::dim::param, ParamLocation, 1); |
| 832 | MapTwo = MapTwo.add_constraint(C); |
| 833 | MapTwo = MapTwo.upper_bound_si(isl::dim::in, i + 1, -1); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 834 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 835 | MapOne = MapOne.unite(MapTwo); |
| 836 | NewAccessRelation = NewAccessRelation.apply_range(MapOne); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 837 | } |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 838 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 839 | isl::id BaseAddrId = getScopArrayInfo()->getBasePtrId(); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 840 | isl::space Space = Statement->getDomainSpace(); |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 841 | NewAccessRelation = NewAccessRelation.set_tuple_id( |
| 842 | isl::dim::in, Space.get_tuple_id(isl::dim::set)); |
| 843 | NewAccessRelation = NewAccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 844 | NewAccessRelation = NewAccessRelation.gist_domain(Statement->getDomain()); |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 845 | |
| 846 | // Access dimension folding might in certain cases increase the number of |
| 847 | // disjuncts in the memory access, which can possibly complicate the generated |
| 848 | // run-time checks and can lead to costly compilation. |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 849 | if (!PollyPreciseFoldAccesses && |
Tobias Grosser | 6ec6e1d | 2018-06-19 08:13:53 +0000 | [diff] [blame] | 850 | NewAccessRelation.n_basic_map() > AccessRelation.n_basic_map()) { |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 851 | } else { |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 852 | AccessRelation = NewAccessRelation; |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 853 | } |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 856 | /// Check if @p Expr is divisible by @p Size. |
Johannes Doerfert | a4b77c0 | 2015-11-12 20:15:32 +0000 | [diff] [blame] | 857 | static bool isDivisible(const SCEV *Expr, unsigned Size, ScalarEvolution &SE) { |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 858 | assert(Size != 0); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 859 | if (Size == 1) |
| 860 | return true; |
Johannes Doerfert | a4b77c0 | 2015-11-12 20:15:32 +0000 | [diff] [blame] | 861 | |
| 862 | // Only one factor needs to be divisible. |
| 863 | if (auto *MulExpr = dyn_cast<SCEVMulExpr>(Expr)) { |
| 864 | for (auto *FactorExpr : MulExpr->operands()) |
| 865 | if (isDivisible(FactorExpr, Size, SE)) |
| 866 | return true; |
| 867 | return false; |
| 868 | } |
| 869 | |
| 870 | // For other n-ary expressions (Add, AddRec, Max,...) all operands need |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 871 | // to be divisible. |
Johannes Doerfert | a4b77c0 | 2015-11-12 20:15:32 +0000 | [diff] [blame] | 872 | if (auto *NAryExpr = dyn_cast<SCEVNAryExpr>(Expr)) { |
| 873 | for (auto *OpExpr : NAryExpr->operands()) |
| 874 | if (!isDivisible(OpExpr, Size, SE)) |
| 875 | return false; |
| 876 | return true; |
| 877 | } |
| 878 | |
| 879 | auto *SizeSCEV = SE.getConstant(Expr->getType(), Size); |
| 880 | auto *UDivSCEV = SE.getUDivExpr(Expr, SizeSCEV); |
| 881 | auto *MulSCEV = SE.getMulExpr(UDivSCEV, SizeSCEV); |
| 882 | return MulSCEV == Expr; |
| 883 | } |
| 884 | |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 885 | void MemoryAccess::buildAccessRelation(const ScopArrayInfo *SAI) { |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 886 | assert(AccessRelation.is_null() && "AccessRelation already built"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 887 | |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 888 | // Initialize the invalid domain which describes all iterations for which the |
| 889 | // access relation is not modeled correctly. |
Tobias Grosser | 2332fa3 | 2017-08-06 15:36:48 +0000 | [diff] [blame] | 890 | isl::set StmtInvalidDomain = getStatement()->getInvalidDomain(); |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 891 | InvalidDomain = isl::set::empty(StmtInvalidDomain.get_space()); |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 892 | |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 893 | isl::ctx Ctx = Id.get_ctx(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 894 | isl::id BaseAddrId = SAI->getBasePtrId(); |
Tobias Grosser | 5683df4 | 2011-11-09 22:34:34 +0000 | [diff] [blame] | 895 | |
Eli Friedman | b9c6f01 | 2016-11-01 20:53:11 +0000 | [diff] [blame] | 896 | if (getAccessInstruction() && isa<MemIntrinsic>(getAccessInstruction())) { |
| 897 | buildMemIntrinsicAccessRelation(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 898 | AccessRelation = AccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); |
Eli Friedman | b9c6f01 | 2016-11-01 20:53:11 +0000 | [diff] [blame] | 899 | return; |
| 900 | } |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 901 | |
Eli Friedman | b9c6f01 | 2016-11-01 20:53:11 +0000 | [diff] [blame] | 902 | if (!isAffine()) { |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 903 | // We overapproximate non-affine accesses with a possible access to the |
| 904 | // whole array. For read accesses it does not make a difference, if an |
| 905 | // access must or may happen. However, for write accesses it is important to |
| 906 | // differentiate between writes that must happen and writes that may happen. |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 907 | if (AccessRelation.is_null()) |
| 908 | AccessRelation = createBasicAccessMap(Statement); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 909 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 910 | AccessRelation = AccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 911 | return; |
| 912 | } |
| 913 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 914 | isl::space Space = isl::space(Ctx, 0, Statement->getNumIterators(), 0); |
| 915 | AccessRelation = isl::map::universe(Space); |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 916 | |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 917 | for (int i = 0, Size = Subscripts.size(); i < Size; ++i) { |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 918 | isl::pw_aff Affine = getPwAff(Subscripts[i]); |
| 919 | isl::map SubscriptMap = isl::map::from_pw_aff(Affine); |
| 920 | AccessRelation = AccessRelation.flat_range_product(SubscriptMap); |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 921 | } |
| 922 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 923 | Space = Statement->getDomainSpace(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 924 | AccessRelation = AccessRelation.set_tuple_id( |
| 925 | isl::dim::in, Space.get_tuple_id(isl::dim::set)); |
| 926 | AccessRelation = AccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 927 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 928 | AccessRelation = AccessRelation.gist_domain(Statement->getDomain()); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 929 | } |
Tobias Grosser | 30b8a09 | 2011-08-18 07:51:37 +0000 | [diff] [blame] | 930 | |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 931 | MemoryAccess::MemoryAccess(ScopStmt *Stmt, Instruction *AccessInst, |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 932 | AccessType AccType, Value *BaseAddress, |
| 933 | Type *ElementType, bool Affine, |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 934 | ArrayRef<const SCEV *> Subscripts, |
| 935 | ArrayRef<const SCEV *> Sizes, Value *AccessValue, |
Tobias Grosser | 72684bb | 2017-05-03 08:02:32 +0000 | [diff] [blame] | 936 | MemoryKind Kind) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 937 | : Kind(Kind), AccType(AccType), Statement(Stmt), InvalidDomain(nullptr), |
| 938 | BaseAddr(BaseAddress), ElementType(ElementType), |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 939 | Sizes(Sizes.begin(), Sizes.end()), AccessInstruction(AccessInst), |
| 940 | AccessValue(AccessValue), IsAffine(Affine), |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 941 | Subscripts(Subscripts.begin(), Subscripts.end()), AccessRelation(nullptr), |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 942 | NewAccessRelation(nullptr), FAD(nullptr) { |
Hongbin Zheng | 86f43ea | 2016-02-20 03:40:15 +0000 | [diff] [blame] | 943 | static const std::string TypeStrings[] = {"", "_Read", "_Write", "_MayWrite"}; |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 944 | const std::string Access = TypeStrings[AccType] + utostr(Stmt->size()); |
Tobias Grosser | f1bfd75 | 2015-11-05 20:15:37 +0000 | [diff] [blame] | 945 | |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 946 | std::string IdName = Stmt->getBaseName() + Access; |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 947 | Id = isl::id::alloc(Stmt->getParent()->getIslCtx(), IdName, this); |
Tobias Grosser | f1bfd75 | 2015-11-05 20:15:37 +0000 | [diff] [blame] | 948 | } |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 949 | |
Tobias Grosser | 1f6ba7e | 2017-07-24 16:22:32 +0000 | [diff] [blame] | 950 | MemoryAccess::MemoryAccess(ScopStmt *Stmt, AccessType AccType, isl::map AccRel) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 951 | : Kind(MemoryKind::Array), AccType(AccType), Statement(Stmt), |
| 952 | InvalidDomain(nullptr), AccessRelation(nullptr), |
| 953 | NewAccessRelation(AccRel), FAD(nullptr) { |
Tobias Grosser | 206e9e3 | 2017-07-24 16:22:27 +0000 | [diff] [blame] | 954 | isl::id ArrayInfoId = NewAccessRelation.get_tuple_id(isl::dim::out); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 955 | auto *SAI = ScopArrayInfo::getFromId(ArrayInfoId); |
| 956 | Sizes.push_back(nullptr); |
| 957 | for (unsigned i = 1; i < SAI->getNumberOfDimensions(); i++) |
| 958 | Sizes.push_back(SAI->getDimensionSize(i)); |
| 959 | ElementType = SAI->getElementType(); |
| 960 | BaseAddr = SAI->getBasePtr(); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 961 | static const std::string TypeStrings[] = {"", "_Read", "_Write", "_MayWrite"}; |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 962 | const std::string Access = TypeStrings[AccType] + utostr(Stmt->size()); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 963 | |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 964 | std::string IdName = Stmt->getBaseName() + Access; |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 965 | Id = isl::id::alloc(Stmt->getParent()->getIslCtx(), IdName, this); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 966 | } |
| 967 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 968 | MemoryAccess::~MemoryAccess() = default; |
| 969 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 970 | void MemoryAccess::realignParams() { |
Tobias Grosser | 8ea1fc1 | 2017-08-06 19:52:38 +0000 | [diff] [blame] | 971 | isl::set Ctx = Statement->getParent()->getContext(); |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 972 | InvalidDomain = InvalidDomain.gist_params(Ctx); |
| 973 | AccessRelation = AccessRelation.gist_params(Ctx); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 974 | } |
| 975 | |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 976 | const std::string MemoryAccess::getReductionOperatorStr() const { |
| 977 | return MemoryAccess::getReductionOperatorStr(getReductionType()); |
| 978 | } |
| 979 | |
Tobias Grosser | fe46c3f | 2017-07-23 04:08:11 +0000 | [diff] [blame] | 980 | isl::id MemoryAccess::getId() const { return Id; } |
Tobias Grosser | 6f48e0f | 2015-05-15 09:58:32 +0000 | [diff] [blame] | 981 | |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 982 | raw_ostream &polly::operator<<(raw_ostream &OS, |
| 983 | MemoryAccess::ReductionType RT) { |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 984 | if (RT == MemoryAccess::RT_NONE) |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 985 | OS << "NONE"; |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 986 | else |
| 987 | OS << MemoryAccess::getReductionOperatorStr(RT); |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 988 | return OS; |
| 989 | } |
| 990 | |
Siddharth Bhat | 0fe7231 | 2017-05-15 08:41:30 +0000 | [diff] [blame] | 991 | void MemoryAccess::setFortranArrayDescriptor(Value *FAD) { this->FAD = FAD; } |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 992 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 993 | void MemoryAccess::print(raw_ostream &OS) const { |
Johannes Doerfert | 4c7ce47 | 2014-10-08 10:11:33 +0000 | [diff] [blame] | 994 | switch (AccType) { |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 995 | case READ: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 996 | OS.indent(12) << "ReadAccess :=\t"; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 997 | break; |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 998 | case MUST_WRITE: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 999 | OS.indent(12) << "MustWriteAccess :=\t"; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 1000 | break; |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 1001 | case MAY_WRITE: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 1002 | OS.indent(12) << "MayWriteAccess :=\t"; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 1003 | break; |
| 1004 | } |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 1005 | |
Johannes Doerfert | 0ff23ec | 2015-02-06 20:13:15 +0000 | [diff] [blame] | 1006 | OS << "[Reduction Type: " << getReductionType() << "] "; |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 1007 | |
| 1008 | if (FAD) { |
| 1009 | OS << "[Fortran array descriptor: " << FAD->getName(); |
| 1010 | OS << "] "; |
| 1011 | }; |
| 1012 | |
Tobias Grosser | a535dff | 2015-12-13 19:59:01 +0000 | [diff] [blame] | 1013 | OS << "[Scalar: " << isScalarKind() << "]\n"; |
Michael Kruse | b8d2644 | 2015-12-13 19:35:26 +0000 | [diff] [blame] | 1014 | OS.indent(16) << getOriginalAccessRelationStr() << ";\n"; |
Tobias Grosser | 6f73008 | 2015-09-05 07:46:47 +0000 | [diff] [blame] | 1015 | if (hasNewAccessRelation()) |
| 1016 | OS.indent(11) << "new: " << getNewAccessRelationStr() << ";\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 1019 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Michael Kruse | e186013 | 2017-07-21 15:54:13 +0000 | [diff] [blame] | 1020 | LLVM_DUMP_METHOD void MemoryAccess::dump() const { print(errs()); } |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 1021 | #endif |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1022 | |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 1023 | isl::pw_aff MemoryAccess::getPwAff(const SCEV *E) { |
Johannes Doerfert | 97f0dcd | 2016-04-12 13:26:45 +0000 | [diff] [blame] | 1024 | auto *Stmt = getStatement(); |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 1025 | PWACtx PWAC = Stmt->getParent()->getPwAff(E, Stmt->getEntryBlock()); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1026 | isl::set StmtDom = getStatement()->getDomain(); |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 1027 | StmtDom = StmtDom.reset_tuple_id(); |
Philip Pfaffe | d98dbee | 2017-12-06 21:02:22 +0000 | [diff] [blame] | 1028 | isl::set NewInvalidDom = StmtDom.intersect(PWAC.second); |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 1029 | InvalidDomain = InvalidDomain.unite(NewInvalidDom); |
Philip Pfaffe | d98dbee | 2017-12-06 21:02:22 +0000 | [diff] [blame] | 1030 | return PWAC.first; |
Johannes Doerfert | 97f0dcd | 2016-04-12 13:26:45 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1033 | // Create a map in the size of the provided set domain, that maps from the |
| 1034 | // one element of the provided set domain to another element of the provided |
| 1035 | // set domain. |
| 1036 | // The mapping is limited to all points that are equal in all but the last |
| 1037 | // dimension and for which the last dimension of the input is strict smaller |
| 1038 | // than the last dimension of the output. |
| 1039 | // |
| 1040 | // getEqualAndLarger(set[i0, i1, ..., iX]): |
| 1041 | // |
| 1042 | // set[i0, i1, ..., iX] -> set[o0, o1, ..., oX] |
| 1043 | // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1), iX < oX |
| 1044 | // |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1045 | static isl::map getEqualAndLarger(isl::space SetDomain) { |
| 1046 | isl::space Space = SetDomain.map_from_set(); |
| 1047 | isl::map Map = isl::map::universe(Space); |
| 1048 | unsigned lastDimension = Map.dim(isl::dim::in) - 1; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1049 | |
| 1050 | // Set all but the last dimension to be equal for the input and output |
| 1051 | // |
| 1052 | // input[i0, i1, ..., iX] -> output[o0, o1, ..., oX] |
| 1053 | // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1) |
Sebastian Pop | 4040876 | 2013-10-04 17:14:53 +0000 | [diff] [blame] | 1054 | for (unsigned i = 0; i < lastDimension; ++i) |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1055 | Map = Map.equate(isl::dim::in, i, isl::dim::out, i); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1056 | |
| 1057 | // Set the last dimension of the input to be strict smaller than the |
| 1058 | // last dimension of the output. |
| 1059 | // |
| 1060 | // input[?,?,?,...,iX] -> output[?,?,?,...,oX] : iX < oX |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1061 | Map = Map.order_lt(isl::dim::in, lastDimension, isl::dim::out, lastDimension); |
Tobias Grosser | c327932c | 2012-02-01 14:23:36 +0000 | [diff] [blame] | 1062 | return Map; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1065 | isl::set MemoryAccess::getStride(isl::map Schedule) const { |
| 1066 | isl::map AccessRelation = getAccessRelation(); |
| 1067 | isl::space Space = Schedule.get_space().range(); |
| 1068 | isl::map NextScatt = getEqualAndLarger(Space); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1069 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1070 | Schedule = Schedule.reverse(); |
| 1071 | NextScatt = NextScatt.lexmin(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1072 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1073 | NextScatt = NextScatt.apply_range(Schedule); |
| 1074 | NextScatt = NextScatt.apply_range(AccessRelation); |
| 1075 | NextScatt = NextScatt.apply_domain(Schedule); |
| 1076 | NextScatt = NextScatt.apply_domain(AccessRelation); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1077 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1078 | isl::set Deltas = NextScatt.deltas(); |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1079 | return Deltas; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1082 | bool MemoryAccess::isStrideX(isl::map Schedule, int StrideWidth) const { |
| 1083 | isl::set Stride, StrideX; |
Tobias Grosser | 28dd486 | 2012-01-24 16:42:16 +0000 | [diff] [blame] | 1084 | bool IsStrideX; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1085 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1086 | Stride = getStride(Schedule); |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1087 | StrideX = isl::set::universe(Stride.get_space()); |
| 1088 | for (unsigned i = 0; i < StrideX.dim(isl::dim::set) - 1; i++) |
| 1089 | StrideX = StrideX.fix_si(isl::dim::set, i, 0); |
| 1090 | StrideX = StrideX.fix_si(isl::dim::set, StrideX.dim(isl::dim::set) - 1, |
| 1091 | StrideWidth); |
| 1092 | IsStrideX = Stride.is_subset(StrideX); |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 1093 | |
Tobias Grosser | 28dd486 | 2012-01-24 16:42:16 +0000 | [diff] [blame] | 1094 | return IsStrideX; |
| 1095 | } |
| 1096 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1097 | bool MemoryAccess::isStrideZero(isl::map Schedule) const { |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1098 | return isStrideX(Schedule, 0); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1101 | bool MemoryAccess::isStrideOne(isl::map Schedule) const { |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1102 | return isStrideX(Schedule, 1); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
Tobias Grosser | 6d58804 | 2017-08-02 19:27:16 +0000 | [diff] [blame] | 1105 | void MemoryAccess::setAccessRelation(isl::map NewAccess) { |
| 1106 | AccessRelation = NewAccess; |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1109 | void MemoryAccess::setNewAccessRelation(isl::map NewAccess) { |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1110 | assert(NewAccess); |
| 1111 | |
| 1112 | #ifndef NDEBUG |
| 1113 | // Check domain space compatibility. |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1114 | isl::space NewSpace = NewAccess.get_space(); |
| 1115 | isl::space NewDomainSpace = NewSpace.domain(); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1116 | isl::space OriginalDomainSpace = getStatement()->getDomainSpace(); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1117 | assert(OriginalDomainSpace.has_equal_tuples(NewDomainSpace)); |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1118 | |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1119 | // Reads must be executed unconditionally. Writes might be executed in a |
| 1120 | // subdomain only. |
| 1121 | if (isRead()) { |
| 1122 | // Check whether there is an access for every statement instance. |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1123 | isl::set StmtDomain = getStatement()->getDomain(); |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 1124 | StmtDomain = |
| 1125 | StmtDomain.intersect_params(getStatement()->getParent()->getContext()); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1126 | isl::set NewDomain = NewAccess.domain(); |
| 1127 | assert(StmtDomain.is_subset(NewDomain) && |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1128 | "Partial READ accesses not supported"); |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1129 | } |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1130 | |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1131 | isl::space NewAccessSpace = NewAccess.get_space(); |
| 1132 | assert(NewAccessSpace.has_tuple_id(isl::dim::set) && |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1133 | "Must specify the array that is accessed"); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1134 | isl::id NewArrayId = NewAccessSpace.get_tuple_id(isl::dim::set); |
| 1135 | auto *SAI = static_cast<ScopArrayInfo *>(NewArrayId.get_user()); |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1136 | assert(SAI && "Must set a ScopArrayInfo"); |
Tobias Grosser | e1ff0cf | 2017-01-17 12:00:42 +0000 | [diff] [blame] | 1137 | |
| 1138 | if (SAI->isArrayKind() && SAI->getBasePtrOriginSAI()) { |
| 1139 | InvariantEquivClassTy *EqClass = |
| 1140 | getStatement()->getParent()->lookupInvariantEquivClass( |
| 1141 | SAI->getBasePtr()); |
| 1142 | assert(EqClass && |
| 1143 | "Access functions to indirect arrays must have an invariant and " |
| 1144 | "hoisted base pointer"); |
| 1145 | } |
| 1146 | |
| 1147 | // Check whether access dimensions correspond to number of dimensions of the |
| 1148 | // accesses array. |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1149 | auto Dims = SAI->getNumberOfDimensions(); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1150 | assert(NewAccessSpace.dim(isl::dim::set) == Dims && |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1151 | "Access dims must match array dims"); |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1152 | #endif |
| 1153 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1154 | NewAccess = NewAccess.gist_domain(getStatement()->getDomain()); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1155 | NewAccessRelation = NewAccess; |
Raghesh Aloor | 3cb6628 | 2011-07-12 17:14:03 +0000 | [diff] [blame] | 1156 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1157 | |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1158 | bool MemoryAccess::isLatestPartialAccess() const { |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1159 | isl::set StmtDom = getStatement()->getDomain(); |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 1160 | isl::set AccDom = getLatestAccessRelation().domain(); |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1161 | |
Tobias Grosser | d3d3d6b | 2018-04-29 00:28:26 +0000 | [diff] [blame] | 1162 | return !StmtDom.is_subset(AccDom); |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1165 | //===----------------------------------------------------------------------===// |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 1166 | |
Tobias Grosser | 6ad1640 | 2017-08-06 17:45:28 +0000 | [diff] [blame] | 1167 | isl::map ScopStmt::getSchedule() const { |
Tobias Grosser | 1e09c13 | 2017-08-14 06:49:06 +0000 | [diff] [blame] | 1168 | isl::set Domain = getDomain(); |
| 1169 | if (Domain.is_empty()) |
| 1170 | return isl::map::from_aff(isl::aff(isl::local_space(getDomainSpace()))); |
| 1171 | auto Schedule = getParent()->getSchedule(); |
| 1172 | if (!Schedule) |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1173 | return nullptr; |
Tobias Grosser | 1e09c13 | 2017-08-14 06:49:06 +0000 | [diff] [blame] | 1174 | Schedule = Schedule.intersect_domain(isl::union_set(Domain)); |
| 1175 | if (Schedule.is_empty()) |
| 1176 | return isl::map::from_aff(isl::aff(isl::local_space(getDomainSpace()))); |
| 1177 | isl::map M = M.from_union_map(Schedule); |
| 1178 | M = M.coalesce(); |
| 1179 | M = M.gist_domain(Domain); |
| 1180 | M = M.coalesce(); |
| 1181 | return M; |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1182 | } |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 1183 | |
Tobias Grosser | a9b5bba | 2017-08-06 16:11:53 +0000 | [diff] [blame] | 1184 | void ScopStmt::restrictDomain(isl::set NewDomain) { |
| 1185 | assert(NewDomain.is_subset(Domain) && |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1186 | "New domain is not a subset of old domain!"); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1187 | Domain = NewDomain; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
Michael Kruse | 70af4f5 | 2017-08-07 18:40:29 +0000 | [diff] [blame] | 1190 | void ScopStmt::addAccess(MemoryAccess *Access, bool Prepend) { |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1191 | Instruction *AccessInst = Access->getAccessInstruction(); |
| 1192 | |
Michael Kruse | 58fa3bb | 2015-12-22 23:25:11 +0000 | [diff] [blame] | 1193 | if (Access->isArrayKind()) { |
| 1194 | MemoryAccessList &MAL = InstructionToAccess[AccessInst]; |
| 1195 | MAL.emplace_front(Access); |
Michael Kruse | 436db62 | 2016-01-26 13:33:10 +0000 | [diff] [blame] | 1196 | } else if (Access->isValueKind() && Access->isWrite()) { |
| 1197 | Instruction *AccessVal = cast<Instruction>(Access->getAccessValue()); |
Michael Kruse | 436db62 | 2016-01-26 13:33:10 +0000 | [diff] [blame] | 1198 | assert(!ValueWrites.lookup(AccessVal)); |
| 1199 | |
| 1200 | ValueWrites[AccessVal] = Access; |
Michael Kruse | ad28e5a | 2016-01-26 13:33:15 +0000 | [diff] [blame] | 1201 | } else if (Access->isValueKind() && Access->isRead()) { |
| 1202 | Value *AccessVal = Access->getAccessValue(); |
| 1203 | assert(!ValueReads.lookup(AccessVal)); |
| 1204 | |
| 1205 | ValueReads[AccessVal] = Access; |
Michael Kruse | ee6a4fc | 2016-01-26 13:33:27 +0000 | [diff] [blame] | 1206 | } else if (Access->isAnyPHIKind() && Access->isWrite()) { |
Tobias Grosser | 5db171a | 2017-02-10 10:09:44 +0000 | [diff] [blame] | 1207 | PHINode *PHI = cast<PHINode>(Access->getAccessValue()); |
Michael Kruse | ee6a4fc | 2016-01-26 13:33:27 +0000 | [diff] [blame] | 1208 | assert(!PHIWrites.lookup(PHI)); |
| 1209 | |
| 1210 | PHIWrites[PHI] = Access; |
Michael Kruse | 3562f27 | 2017-07-20 16:47:57 +0000 | [diff] [blame] | 1211 | } else if (Access->isAnyPHIKind() && Access->isRead()) { |
| 1212 | PHINode *PHI = cast<PHINode>(Access->getAccessValue()); |
| 1213 | assert(!PHIReads.lookup(PHI)); |
| 1214 | |
| 1215 | PHIReads[PHI] = Access; |
Michael Kruse | 58fa3bb | 2015-12-22 23:25:11 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Michael Kruse | 70af4f5 | 2017-08-07 18:40:29 +0000 | [diff] [blame] | 1218 | if (Prepend) { |
| 1219 | MemAccs.insert(MemAccs.begin(), Access); |
| 1220 | return; |
| 1221 | } |
Michael Kruse | 58fa3bb | 2015-12-22 23:25:11 +0000 | [diff] [blame] | 1222 | MemAccs.push_back(Access); |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1225 | void ScopStmt::realignParams() { |
Johannes Doerfert | f675289 | 2014-06-13 18:01:45 +0000 | [diff] [blame] | 1226 | for (MemoryAccess *MA : *this) |
| 1227 | MA->realignParams(); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1228 | |
Tobias Grosser | 8ea1fc1 | 2017-08-06 19:52:38 +0000 | [diff] [blame] | 1229 | isl::set Ctx = Parent.getContext(); |
Tobias Grosser | 2332fa3 | 2017-08-06 15:36:48 +0000 | [diff] [blame] | 1230 | InvalidDomain = InvalidDomain.gist_params(Ctx); |
Tobias Grosser | a9b5bba | 2017-08-06 16:11:53 +0000 | [diff] [blame] | 1231 | Domain = Domain.gist_params(Ctx); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
Tobias Grosser | 78a8494 | 2018-06-01 19:12:00 +0000 | [diff] [blame] | 1234 | /// Add @p BSet to set @p BoundedParts if @p BSet is bounded. |
| 1235 | static isl::set collectBoundedParts(isl::set S) { |
| 1236 | isl::set BoundedParts = isl::set::empty(S.get_space()); |
Tobias Grosser | 31e29a4 | 2018-07-16 19:04:16 +0000 | [diff] [blame] | 1237 | for (isl::basic_set BSet : S.get_basic_set_list()) |
| 1238 | if (BSet.is_bounded()) |
Tobias Grosser | 78a8494 | 2018-06-01 19:12:00 +0000 | [diff] [blame] | 1239 | BoundedParts = BoundedParts.unite(isl::set(BSet)); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1240 | return BoundedParts; |
| 1241 | } |
| 1242 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1243 | /// Compute the (un)bounded parts of @p S wrt. to dimension @p Dim. |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1244 | /// |
| 1245 | /// @returns A separation of @p S into first an unbounded then a bounded subset, |
| 1246 | /// both with regards to the dimension @p Dim. |
Tobias Grosser | 78a8494 | 2018-06-01 19:12:00 +0000 | [diff] [blame] | 1247 | static std::pair<isl::set, isl::set> partitionSetParts(isl::set S, |
| 1248 | unsigned Dim) { |
| 1249 | for (unsigned u = 0, e = S.n_dim(); u < e; u++) |
| 1250 | S = S.lower_bound_si(isl::dim::set, u, 0); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1251 | |
Tobias Grosser | 78a8494 | 2018-06-01 19:12:00 +0000 | [diff] [blame] | 1252 | unsigned NumDimsS = S.n_dim(); |
| 1253 | isl::set OnlyDimS = S; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1254 | |
| 1255 | // Remove dimensions that are greater than Dim as they are not interesting. |
| 1256 | assert(NumDimsS >= Dim + 1); |
Tobias Grosser | 78a8494 | 2018-06-01 19:12:00 +0000 | [diff] [blame] | 1257 | OnlyDimS = OnlyDimS.project_out(isl::dim::set, Dim + 1, NumDimsS - Dim - 1); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1258 | |
| 1259 | // Create artificial parametric upper bounds for dimensions smaller than Dim |
| 1260 | // as we are not interested in them. |
Tobias Grosser | 78a8494 | 2018-06-01 19:12:00 +0000 | [diff] [blame] | 1261 | OnlyDimS = OnlyDimS.insert_dims(isl::dim::param, 0, Dim); |
| 1262 | |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1263 | for (unsigned u = 0; u < Dim; u++) { |
Tobias Grosser | 78a8494 | 2018-06-01 19:12:00 +0000 | [diff] [blame] | 1264 | isl::constraint C = isl::constraint::alloc_inequality( |
| 1265 | isl::local_space(OnlyDimS.get_space())); |
| 1266 | C = C.set_coefficient_si(isl::dim::param, u, 1); |
| 1267 | C = C.set_coefficient_si(isl::dim::set, u, -1); |
| 1268 | OnlyDimS = OnlyDimS.add_constraint(C); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | // Collect all bounded parts of OnlyDimS. |
Tobias Grosser | 78a8494 | 2018-06-01 19:12:00 +0000 | [diff] [blame] | 1272 | isl::set BoundedParts = collectBoundedParts(OnlyDimS); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1273 | |
| 1274 | // Create the dimensions greater than Dim again. |
Tobias Grosser | 78a8494 | 2018-06-01 19:12:00 +0000 | [diff] [blame] | 1275 | BoundedParts = |
| 1276 | BoundedParts.insert_dims(isl::dim::set, Dim + 1, NumDimsS - Dim - 1); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1277 | |
| 1278 | // Remove the artificial upper bound parameters again. |
Tobias Grosser | 78a8494 | 2018-06-01 19:12:00 +0000 | [diff] [blame] | 1279 | BoundedParts = BoundedParts.remove_dims(isl::dim::param, 0, Dim); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1280 | |
Tobias Grosser | 78a8494 | 2018-06-01 19:12:00 +0000 | [diff] [blame] | 1281 | isl::set UnboundedParts = S.subtract(BoundedParts); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1282 | return std::make_pair(UnboundedParts, BoundedParts); |
| 1283 | } |
| 1284 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1285 | /// Create the conditions under which @p L @p Pred @p R is true. |
Tobias Grosser | fd5c856 | 2018-06-18 12:35:36 +0000 | [diff] [blame] | 1286 | static isl::set buildConditionSet(ICmpInst::Predicate Pred, isl::pw_aff L, |
| 1287 | isl::pw_aff R) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1288 | switch (Pred) { |
| 1289 | case ICmpInst::ICMP_EQ: |
Tobias Grosser | fd5c856 | 2018-06-18 12:35:36 +0000 | [diff] [blame] | 1290 | return L.eq_set(R); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1291 | case ICmpInst::ICMP_NE: |
Tobias Grosser | fd5c856 | 2018-06-18 12:35:36 +0000 | [diff] [blame] | 1292 | return L.ne_set(R); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1293 | case ICmpInst::ICMP_SLT: |
Tobias Grosser | fd5c856 | 2018-06-18 12:35:36 +0000 | [diff] [blame] | 1294 | return L.lt_set(R); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1295 | case ICmpInst::ICMP_SLE: |
Tobias Grosser | fd5c856 | 2018-06-18 12:35:36 +0000 | [diff] [blame] | 1296 | return L.le_set(R); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1297 | case ICmpInst::ICMP_SGT: |
Tobias Grosser | fd5c856 | 2018-06-18 12:35:36 +0000 | [diff] [blame] | 1298 | return L.gt_set(R); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1299 | case ICmpInst::ICMP_SGE: |
Tobias Grosser | fd5c856 | 2018-06-18 12:35:36 +0000 | [diff] [blame] | 1300 | return L.ge_set(R); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1301 | case ICmpInst::ICMP_ULT: |
Tobias Grosser | fd5c856 | 2018-06-18 12:35:36 +0000 | [diff] [blame] | 1302 | return L.lt_set(R); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1303 | case ICmpInst::ICMP_UGT: |
Tobias Grosser | fd5c856 | 2018-06-18 12:35:36 +0000 | [diff] [blame] | 1304 | return L.gt_set(R); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1305 | case ICmpInst::ICMP_ULE: |
Tobias Grosser | fd5c856 | 2018-06-18 12:35:36 +0000 | [diff] [blame] | 1306 | return L.le_set(R); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1307 | case ICmpInst::ICMP_UGE: |
Tobias Grosser | fd5c856 | 2018-06-18 12:35:36 +0000 | [diff] [blame] | 1308 | return L.ge_set(R); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1309 | default: |
| 1310 | llvm_unreachable("Non integer predicate not supported"); |
| 1311 | } |
| 1312 | } |
| 1313 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1314 | /// Compute the isl representation for the SCEV @p E in this BB. |
| 1315 | /// |
| 1316 | /// @param S The Scop in which @p BB resides in. |
| 1317 | /// @param BB The BB for which isl representation is to be |
| 1318 | /// computed. |
| 1319 | /// @param InvalidDomainMap A map of BB to their invalid domains. |
| 1320 | /// @param E The SCEV that should be translated. |
| 1321 | /// @param NonNegative Flag to indicate the @p E has to be non-negative. |
| 1322 | /// |
| 1323 | /// Note that this function will also adjust the invalid context accordingly. |
| 1324 | |
| 1325 | __isl_give isl_pw_aff * |
| 1326 | getPwAff(Scop &S, BasicBlock *BB, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 1327 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, const SCEV *E, |
| 1328 | bool NonNegative = false) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1329 | PWACtx PWAC = S.getPwAff(E, BB, NonNegative); |
Philip Pfaffe | d98dbee | 2017-12-06 21:02:22 +0000 | [diff] [blame] | 1330 | InvalidDomainMap[BB] = InvalidDomainMap[BB].unite(PWAC.second); |
Tobias Grosser | 8dae41a | 2018-04-29 00:57:38 +0000 | [diff] [blame] | 1331 | return PWAC.first.release(); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1334 | /// 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] | 1335 | /// |
| 1336 | /// This will fill @p ConditionSets with the conditions under which control |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1337 | /// will be moved from @p SI to its successors. Hence, @p ConditionSets will |
| 1338 | /// have as many elements as @p SI has successors. |
Tobias Grosser | ee45759 | 2017-09-24 09:25:30 +0000 | [diff] [blame] | 1339 | bool buildConditionSets(Scop &S, BasicBlock *BB, SwitchInst *SI, Loop *L, |
| 1340 | __isl_keep isl_set *Domain, |
| 1341 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, |
| 1342 | SmallVectorImpl<__isl_give isl_set *> &ConditionSets) { |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1343 | Value *Condition = getConditionFromTerminator(SI); |
| 1344 | assert(Condition && "No condition for switch"); |
| 1345 | |
| 1346 | ScalarEvolution &SE = *S.getSE(); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1347 | isl_pw_aff *LHS, *RHS; |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1348 | LHS = getPwAff(S, BB, InvalidDomainMap, SE.getSCEVAtScope(Condition, L)); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1349 | |
| 1350 | unsigned NumSuccessors = SI->getNumSuccessors(); |
| 1351 | ConditionSets.resize(NumSuccessors); |
| 1352 | for (auto &Case : SI->cases()) { |
| 1353 | unsigned Idx = Case.getSuccessorIndex(); |
| 1354 | ConstantInt *CaseValue = Case.getCaseValue(); |
| 1355 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1356 | RHS = getPwAff(S, BB, InvalidDomainMap, SE.getSCEV(CaseValue)); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1357 | isl_set *CaseConditionSet = |
Tobias Grosser | fd5c856 | 2018-06-18 12:35:36 +0000 | [diff] [blame] | 1358 | buildConditionSet(ICmpInst::ICMP_EQ, isl::manage_copy(LHS), |
| 1359 | isl::manage(RHS)) |
| 1360 | .release(); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1361 | ConditionSets[Idx] = isl_set_coalesce( |
| 1362 | isl_set_intersect(CaseConditionSet, isl_set_copy(Domain))); |
| 1363 | } |
| 1364 | |
| 1365 | assert(ConditionSets[0] == nullptr && "Default condition set was set"); |
| 1366 | isl_set *ConditionSetUnion = isl_set_copy(ConditionSets[1]); |
| 1367 | for (unsigned u = 2; u < NumSuccessors; u++) |
| 1368 | ConditionSetUnion = |
| 1369 | isl_set_union(ConditionSetUnion, isl_set_copy(ConditionSets[u])); |
Tobias Grosser | b948630 | 2018-03-03 19:27:54 +0000 | [diff] [blame] | 1370 | ConditionSets[0] = isl_set_subtract(isl_set_copy(Domain), ConditionSetUnion); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1371 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1372 | isl_pw_aff_free(LHS); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1373 | |
| 1374 | return true; |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
Michael Kruse | 0865585 | 2017-07-20 12:37:02 +0000 | [diff] [blame] | 1377 | /// Build condition sets for unsigned ICmpInst(s). |
| 1378 | /// Special handling is required for unsigned operands to ensure that if |
| 1379 | /// MSB (aka the Sign bit) is set for an operands in an unsigned ICmpInst |
| 1380 | /// it should wrap around. |
| 1381 | /// |
| 1382 | /// @param IsStrictUpperBound holds information on the predicate relation |
| 1383 | /// between TestVal and UpperBound, i.e, |
| 1384 | /// TestVal < UpperBound OR TestVal <= UpperBound |
Tobias Grosser | ee45759 | 2017-09-24 09:25:30 +0000 | [diff] [blame] | 1385 | __isl_give isl_set * |
Michael Kruse | 0865585 | 2017-07-20 12:37:02 +0000 | [diff] [blame] | 1386 | buildUnsignedConditionSets(Scop &S, BasicBlock *BB, Value *Condition, |
| 1387 | __isl_keep isl_set *Domain, const SCEV *SCEV_TestVal, |
| 1388 | const SCEV *SCEV_UpperBound, |
| 1389 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, |
| 1390 | bool IsStrictUpperBound) { |
Michael Kruse | 0865585 | 2017-07-20 12:37:02 +0000 | [diff] [blame] | 1391 | // Do not take NonNeg assumption on TestVal |
| 1392 | // as it might have MSB (Sign bit) set. |
| 1393 | isl_pw_aff *TestVal = getPwAff(S, BB, InvalidDomainMap, SCEV_TestVal, false); |
| 1394 | // Take NonNeg assumption on UpperBound. |
| 1395 | isl_pw_aff *UpperBound = |
| 1396 | getPwAff(S, BB, InvalidDomainMap, SCEV_UpperBound, true); |
| 1397 | |
| 1398 | // 0 <= TestVal |
| 1399 | isl_set *First = |
| 1400 | isl_pw_aff_le_set(isl_pw_aff_zero_on_domain(isl_local_space_from_space( |
| 1401 | isl_pw_aff_get_domain_space(TestVal))), |
| 1402 | isl_pw_aff_copy(TestVal)); |
| 1403 | |
| 1404 | isl_set *Second; |
| 1405 | if (IsStrictUpperBound) |
| 1406 | // TestVal < UpperBound |
| 1407 | Second = isl_pw_aff_lt_set(TestVal, UpperBound); |
| 1408 | else |
| 1409 | // TestVal <= UpperBound |
| 1410 | Second = isl_pw_aff_le_set(TestVal, UpperBound); |
| 1411 | |
| 1412 | isl_set *ConsequenceCondSet = isl_set_intersect(First, Second); |
Michael Kruse | 0865585 | 2017-07-20 12:37:02 +0000 | [diff] [blame] | 1413 | return ConsequenceCondSet; |
| 1414 | } |
| 1415 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1416 | /// Build the conditions sets for the branch condition @p Condition in |
| 1417 | /// the @p Domain. |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1418 | /// |
| 1419 | /// This will fill @p ConditionSets with the conditions under which control |
| 1420 | /// 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] | 1421 | /// have as many elements as @p TI has successors. If @p TI is nullptr the |
| 1422 | /// context under which @p Condition is true/false will be returned as the |
| 1423 | /// new elements of @p ConditionSets. |
Tobias Grosser | ee45759 | 2017-09-24 09:25:30 +0000 | [diff] [blame] | 1424 | bool buildConditionSets(Scop &S, BasicBlock *BB, Value *Condition, |
Chandler Carruth | e303c87 | 2018-10-15 10:42:50 +0000 | [diff] [blame] | 1425 | Instruction *TI, Loop *L, __isl_keep isl_set *Domain, |
Tobias Grosser | ee45759 | 2017-09-24 09:25:30 +0000 | [diff] [blame] | 1426 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, |
| 1427 | SmallVectorImpl<__isl_give isl_set *> &ConditionSets) { |
Tobias Grosser | 5e531df | 2017-09-25 20:27:15 +0000 | [diff] [blame] | 1428 | ScalarEvolution &SE = *S.getSE(); |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1429 | isl_set *ConsequenceCondSet = nullptr; |
Tobias Grosser | 0a62b2d | 2017-09-25 16:37:15 +0000 | [diff] [blame] | 1430 | |
Tobias Grosser | 5e531df | 2017-09-25 20:27:15 +0000 | [diff] [blame] | 1431 | if (auto Load = dyn_cast<LoadInst>(Condition)) { |
| 1432 | const SCEV *LHSSCEV = SE.getSCEVAtScope(Load, L); |
| 1433 | const SCEV *RHSSCEV = SE.getZero(LHSSCEV->getType()); |
| 1434 | bool NonNeg = false; |
| 1435 | isl_pw_aff *LHS = getPwAff(S, BB, InvalidDomainMap, LHSSCEV, NonNeg); |
| 1436 | isl_pw_aff *RHS = getPwAff(S, BB, InvalidDomainMap, RHSSCEV, NonNeg); |
Tobias Grosser | fd5c856 | 2018-06-18 12:35:36 +0000 | [diff] [blame] | 1437 | ConsequenceCondSet = buildConditionSet(ICmpInst::ICMP_SLE, isl::manage(LHS), |
| 1438 | isl::manage(RHS)) |
| 1439 | .release(); |
Tobias Grosser | 5e531df | 2017-09-25 20:27:15 +0000 | [diff] [blame] | 1440 | } else if (auto *PHI = dyn_cast<PHINode>(Condition)) { |
Tobias Grosser | 0a62b2d | 2017-09-25 16:37:15 +0000 | [diff] [blame] | 1441 | auto *Unique = dyn_cast<ConstantInt>( |
| 1442 | getUniqueNonErrorValue(PHI, &S.getRegion(), *S.getLI(), *S.getDT())); |
| 1443 | |
| 1444 | if (Unique->isZero()) |
| 1445 | ConsequenceCondSet = isl_set_empty(isl_set_get_space(Domain)); |
| 1446 | else |
| 1447 | ConsequenceCondSet = isl_set_universe(isl_set_get_space(Domain)); |
| 1448 | } else if (auto *CCond = dyn_cast<ConstantInt>(Condition)) { |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1449 | if (CCond->isZero()) |
| 1450 | ConsequenceCondSet = isl_set_empty(isl_set_get_space(Domain)); |
| 1451 | else |
| 1452 | ConsequenceCondSet = isl_set_universe(isl_set_get_space(Domain)); |
| 1453 | } else if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Condition)) { |
| 1454 | auto Opcode = BinOp->getOpcode(); |
| 1455 | assert(Opcode == Instruction::And || Opcode == Instruction::Or); |
| 1456 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1457 | bool Valid = buildConditionSets(S, BB, BinOp->getOperand(0), TI, L, Domain, |
| 1458 | InvalidDomainMap, ConditionSets) && |
| 1459 | buildConditionSets(S, BB, BinOp->getOperand(1), TI, L, Domain, |
| 1460 | InvalidDomainMap, ConditionSets); |
Johannes Doerfert | ede4eca | 2016-05-10 14:01:21 +0000 | [diff] [blame] | 1461 | if (!Valid) { |
| 1462 | while (!ConditionSets.empty()) |
| 1463 | isl_set_free(ConditionSets.pop_back_val()); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1464 | return false; |
Johannes Doerfert | ede4eca | 2016-05-10 14:01:21 +0000 | [diff] [blame] | 1465 | } |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1466 | |
| 1467 | isl_set_free(ConditionSets.pop_back_val()); |
| 1468 | isl_set *ConsCondPart0 = ConditionSets.pop_back_val(); |
| 1469 | isl_set_free(ConditionSets.pop_back_val()); |
| 1470 | isl_set *ConsCondPart1 = ConditionSets.pop_back_val(); |
| 1471 | |
| 1472 | if (Opcode == Instruction::And) |
| 1473 | ConsequenceCondSet = isl_set_intersect(ConsCondPart0, ConsCondPart1); |
| 1474 | else |
| 1475 | ConsequenceCondSet = isl_set_union(ConsCondPart0, ConsCondPart1); |
| 1476 | } else { |
| 1477 | auto *ICond = dyn_cast<ICmpInst>(Condition); |
| 1478 | assert(ICond && |
| 1479 | "Condition of exiting branch was neither constant nor ICmp!"); |
| 1480 | |
Tobias Grosser | ee45759 | 2017-09-24 09:25:30 +0000 | [diff] [blame] | 1481 | LoopInfo &LI = *S.getLI(); |
| 1482 | DominatorTree &DT = *S.getDT(); |
| 1483 | Region &R = S.getRegion(); |
| 1484 | |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1485 | isl_pw_aff *LHS, *RHS; |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 1486 | // For unsigned comparisons we assumed the signed bit of neither operand |
| 1487 | // to be set. The comparison is equal to a signed comparison under this |
| 1488 | // assumption. |
| 1489 | bool NonNeg = ICond->isUnsigned(); |
Michael Kruse | 0865585 | 2017-07-20 12:37:02 +0000 | [diff] [blame] | 1490 | const SCEV *LeftOperand = SE.getSCEVAtScope(ICond->getOperand(0), L), |
| 1491 | *RightOperand = SE.getSCEVAtScope(ICond->getOperand(1), L); |
| 1492 | |
Tobias Grosser | ee45759 | 2017-09-24 09:25:30 +0000 | [diff] [blame] | 1493 | LeftOperand = tryForwardThroughPHI(LeftOperand, R, SE, LI, DT); |
| 1494 | RightOperand = tryForwardThroughPHI(RightOperand, R, SE, LI, DT); |
| 1495 | |
Michael Kruse | 0865585 | 2017-07-20 12:37:02 +0000 | [diff] [blame] | 1496 | switch (ICond->getPredicate()) { |
| 1497 | case ICmpInst::ICMP_ULT: |
| 1498 | ConsequenceCondSet = |
| 1499 | buildUnsignedConditionSets(S, BB, Condition, Domain, LeftOperand, |
| 1500 | RightOperand, InvalidDomainMap, true); |
| 1501 | break; |
| 1502 | case ICmpInst::ICMP_ULE: |
| 1503 | ConsequenceCondSet = |
| 1504 | buildUnsignedConditionSets(S, BB, Condition, Domain, LeftOperand, |
| 1505 | RightOperand, InvalidDomainMap, false); |
| 1506 | break; |
| 1507 | case ICmpInst::ICMP_UGT: |
| 1508 | ConsequenceCondSet = |
| 1509 | buildUnsignedConditionSets(S, BB, Condition, Domain, RightOperand, |
| 1510 | LeftOperand, InvalidDomainMap, true); |
| 1511 | break; |
| 1512 | case ICmpInst::ICMP_UGE: |
| 1513 | ConsequenceCondSet = |
| 1514 | buildUnsignedConditionSets(S, BB, Condition, Domain, RightOperand, |
| 1515 | LeftOperand, InvalidDomainMap, false); |
| 1516 | break; |
| 1517 | default: |
| 1518 | LHS = getPwAff(S, BB, InvalidDomainMap, LeftOperand, NonNeg); |
| 1519 | RHS = getPwAff(S, BB, InvalidDomainMap, RightOperand, NonNeg); |
Tobias Grosser | fd5c856 | 2018-06-18 12:35:36 +0000 | [diff] [blame] | 1520 | ConsequenceCondSet = buildConditionSet(ICond->getPredicate(), |
| 1521 | isl::manage(LHS), isl::manage(RHS)) |
| 1522 | .release(); |
Michael Kruse | 0865585 | 2017-07-20 12:37:02 +0000 | [diff] [blame] | 1523 | break; |
| 1524 | } |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
Johannes Doerfert | 2af10e2 | 2015-11-12 03:25:01 +0000 | [diff] [blame] | 1527 | // If no terminator was given we are only looking for parameter constraints |
| 1528 | // under which @p Condition is true/false. |
| 1529 | if (!TI) |
| 1530 | ConsequenceCondSet = isl_set_params(ConsequenceCondSet); |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1531 | assert(ConsequenceCondSet); |
Johannes Doerfert | 1519491 | 2016-04-04 07:59:41 +0000 | [diff] [blame] | 1532 | ConsequenceCondSet = isl_set_coalesce( |
| 1533 | isl_set_intersect(ConsequenceCondSet, isl_set_copy(Domain))); |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1534 | |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1535 | isl_set *AlternativeCondSet = nullptr; |
Michael Kruse | f7a4a94 | 2016-05-02 12:25:36 +0000 | [diff] [blame] | 1536 | bool TooComplex = |
Tobias Grosser | 90411a9 | 2017-02-16 19:11:33 +0000 | [diff] [blame] | 1537 | isl_set_n_basic_set(ConsequenceCondSet) >= MaxDisjunctsInDomain; |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1538 | |
Michael Kruse | f7a4a94 | 2016-05-02 12:25:36 +0000 | [diff] [blame] | 1539 | if (!TooComplex) { |
Johannes Doerfert | 1519491 | 2016-04-04 07:59:41 +0000 | [diff] [blame] | 1540 | AlternativeCondSet = isl_set_subtract(isl_set_copy(Domain), |
| 1541 | isl_set_copy(ConsequenceCondSet)); |
Michael Kruse | f7a4a94 | 2016-05-02 12:25:36 +0000 | [diff] [blame] | 1542 | TooComplex = |
Tobias Grosser | 90411a9 | 2017-02-16 19:11:33 +0000 | [diff] [blame] | 1543 | isl_set_n_basic_set(AlternativeCondSet) >= MaxDisjunctsInDomain; |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
Michael Kruse | f7a4a94 | 2016-05-02 12:25:36 +0000 | [diff] [blame] | 1546 | if (TooComplex) { |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 1547 | S.invalidate(COMPLEXITY, TI ? TI->getDebugLoc() : DebugLoc(), |
| 1548 | TI ? TI->getParent() : nullptr /* BasicBlock */); |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1549 | isl_set_free(AlternativeCondSet); |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1550 | isl_set_free(ConsequenceCondSet); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1551 | return false; |
Johannes Doerfert | 1519491 | 2016-04-04 07:59:41 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | ConditionSets.push_back(ConsequenceCondSet); |
| 1555 | ConditionSets.push_back(isl_set_coalesce(AlternativeCondSet)); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1556 | |
| 1557 | return true; |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1560 | /// Build the conditions sets for the terminator @p TI in the @p Domain. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1561 | /// |
| 1562 | /// This will fill @p ConditionSets with the conditions under which control |
| 1563 | /// will be moved from @p TI to its successors. Hence, @p ConditionSets will |
| 1564 | /// have as many elements as @p TI has successors. |
Chandler Carruth | e303c87 | 2018-10-15 10:42:50 +0000 | [diff] [blame] | 1565 | bool buildConditionSets(Scop &S, BasicBlock *BB, Instruction *TI, Loop *L, |
Tobias Grosser | ee45759 | 2017-09-24 09:25:30 +0000 | [diff] [blame] | 1566 | __isl_keep isl_set *Domain, |
| 1567 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, |
| 1568 | SmallVectorImpl<__isl_give isl_set *> &ConditionSets) { |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1569 | if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1570 | return buildConditionSets(S, BB, SI, L, Domain, InvalidDomainMap, |
| 1571 | ConditionSets); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1572 | |
| 1573 | assert(isa<BranchInst>(TI) && "Terminator was neither branch nor switch."); |
| 1574 | |
| 1575 | if (TI->getNumSuccessors() == 1) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1576 | ConditionSets.push_back(isl_set_copy(Domain)); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1577 | return true; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1580 | Value *Condition = getConditionFromTerminator(TI); |
| 1581 | assert(Condition && "No condition for Terminator"); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1582 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1583 | return buildConditionSets(S, BB, Condition, TI, L, Domain, InvalidDomainMap, |
| 1584 | ConditionSets); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1585 | } |
| 1586 | |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 1587 | ScopStmt::ScopStmt(Scop &parent, Region &R, StringRef Name, |
| 1588 | Loop *SurroundingLoop, |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 1589 | std::vector<Instruction *> EntryBlockInstructions) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1590 | : Parent(parent), InvalidDomain(nullptr), Domain(nullptr), R(&R), |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 1591 | Build(nullptr), BaseName(Name), SurroundingLoop(SurroundingLoop), |
| 1592 | Instructions(EntryBlockInstructions) {} |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 1593 | |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 1594 | ScopStmt::ScopStmt(Scop &parent, BasicBlock &bb, StringRef Name, |
| 1595 | Loop *SurroundingLoop, |
| 1596 | std::vector<Instruction *> Instructions) |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 1597 | : Parent(parent), InvalidDomain(nullptr), Domain(nullptr), BB(&bb), |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 1598 | Build(nullptr), BaseName(Name), SurroundingLoop(SurroundingLoop), |
| 1599 | Instructions(Instructions) {} |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1600 | |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 1601 | ScopStmt::ScopStmt(Scop &parent, isl::map SourceRel, isl::map TargetRel, |
| 1602 | isl::set NewDomain) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1603 | : Parent(parent), InvalidDomain(nullptr), Domain(NewDomain), |
| 1604 | Build(nullptr) { |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1605 | BaseName = getIslCompatibleName("CopyStmt_", "", |
| 1606 | std::to_string(parent.getCopyStmtsNum())); |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 1607 | isl::id Id = isl::id::alloc(getIslCtx(), getBaseName(), this); |
| 1608 | Domain = Domain.set_tuple_id(Id); |
| 1609 | TargetRel = TargetRel.set_tuple_id(isl::dim::in, Id); |
| 1610 | auto *Access = |
| 1611 | new MemoryAccess(this, MemoryAccess::AccessType::MUST_WRITE, TargetRel); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1612 | parent.addAccessFunction(Access); |
| 1613 | addAccess(Access); |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 1614 | SourceRel = SourceRel.set_tuple_id(isl::dim::in, Id); |
| 1615 | Access = new MemoryAccess(this, MemoryAccess::AccessType::READ, SourceRel); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1616 | parent.addAccessFunction(Access); |
| 1617 | addAccess(Access); |
| 1618 | } |
| 1619 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1620 | ScopStmt::~ScopStmt() = default; |
| 1621 | |
Tobias Grosser | a9b5bba | 2017-08-06 16:11:53 +0000 | [diff] [blame] | 1622 | std::string ScopStmt::getDomainStr() const { return Domain.to_str(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1623 | |
Tobias Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 1624 | std::string ScopStmt::getScheduleStr() const { |
Tobias Grosser | 6ad1640 | 2017-08-06 17:45:28 +0000 | [diff] [blame] | 1625 | auto *S = getSchedule().release(); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1626 | if (!S) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1627 | return {}; |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1628 | auto Str = stringFromIslObj(S); |
| 1629 | isl_map_free(S); |
| 1630 | return Str; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
Tobias Grosser | 2332fa3 | 2017-08-06 15:36:48 +0000 | [diff] [blame] | 1633 | void ScopStmt::setInvalidDomain(isl::set ID) { InvalidDomain = ID; } |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 1634 | |
Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 1635 | BasicBlock *ScopStmt::getEntryBlock() const { |
| 1636 | if (isBlockStmt()) |
| 1637 | return getBasicBlock(); |
| 1638 | return getRegion()->getEntry(); |
| 1639 | } |
| 1640 | |
Tobias Grosser | f567e1a | 2015-02-19 22:16:12 +0000 | [diff] [blame] | 1641 | unsigned ScopStmt::getNumIterators() const { return NestLoops.size(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1642 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1643 | const char *ScopStmt::getBaseName() const { return BaseName.c_str(); } |
| 1644 | |
Johannes Doerfert | 2b92a0e | 2016-05-10 14:00:57 +0000 | [diff] [blame] | 1645 | Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const { |
Sebastian Pop | 860e021 | 2013-02-15 21:26:44 +0000 | [diff] [blame] | 1646 | return NestLoops[Dimension]; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 1649 | isl::ctx ScopStmt::getIslCtx() const { return Parent.getIslCtx(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1650 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1651 | isl::set ScopStmt::getDomain() const { return Domain; } |
Tobias Grosser | d5a7bfc | 2011-05-06 19:52:19 +0000 | [diff] [blame] | 1652 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1653 | isl::space ScopStmt::getDomainSpace() const { return Domain.get_space(); } |
Tobias Grosser | 78d8a3d | 2012-01-17 20:34:23 +0000 | [diff] [blame] | 1654 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1655 | isl::id ScopStmt::getDomainId() const { return Domain.get_tuple_id(); } |
Tobias Grosser | cd95b77 | 2012-08-30 11:49:38 +0000 | [diff] [blame] | 1656 | |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1657 | void ScopStmt::printInstructions(raw_ostream &OS) const { |
| 1658 | OS << "Instructions {\n"; |
| 1659 | |
| 1660 | for (Instruction *Inst : Instructions) |
| 1661 | OS.indent(16) << *Inst << "\n"; |
| 1662 | |
Michael Kruse | e52ebd1 | 2017-07-22 16:44:39 +0000 | [diff] [blame] | 1663 | OS.indent(12) << "}\n"; |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 1666 | void ScopStmt::print(raw_ostream &OS, bool PrintInstructions) const { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1667 | OS << "\t" << getBaseName() << "\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1668 | OS.indent(12) << "Domain :=\n"; |
| 1669 | |
| 1670 | if (Domain) { |
| 1671 | OS.indent(16) << getDomainStr() << ";\n"; |
| 1672 | } else |
| 1673 | OS.indent(16) << "n/a\n"; |
| 1674 | |
Tobias Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 1675 | OS.indent(12) << "Schedule :=\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1676 | |
| 1677 | if (Domain) { |
Tobias Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 1678 | OS.indent(16) << getScheduleStr() << ";\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1679 | } else |
| 1680 | OS.indent(16) << "n/a\n"; |
| 1681 | |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1682 | for (MemoryAccess *Access : MemAccs) |
| 1683 | Access->print(OS); |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1684 | |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 1685 | if (PrintInstructions) |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1686 | printInstructions(OS.indent(12)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 1689 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Michael Kruse | e186013 | 2017-07-21 15:54:13 +0000 | [diff] [blame] | 1690 | LLVM_DUMP_METHOD void ScopStmt::dump() const { print(dbgs(), true); } |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 1691 | #endif |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1692 | |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1693 | void ScopStmt::removeAccessData(MemoryAccess *MA) { |
| 1694 | if (MA->isRead() && MA->isOriginalValueKind()) { |
| 1695 | bool Found = ValueReads.erase(MA->getAccessValue()); |
| 1696 | (void)Found; |
| 1697 | assert(Found && "Expected access data not found"); |
| 1698 | } |
| 1699 | if (MA->isWrite() && MA->isOriginalValueKind()) { |
| 1700 | bool Found = ValueWrites.erase(cast<Instruction>(MA->getAccessValue())); |
| 1701 | (void)Found; |
| 1702 | assert(Found && "Expected access data not found"); |
| 1703 | } |
| 1704 | if (MA->isWrite() && MA->isOriginalAnyPHIKind()) { |
| 1705 | bool Found = PHIWrites.erase(cast<PHINode>(MA->getAccessInstruction())); |
| 1706 | (void)Found; |
| 1707 | assert(Found && "Expected access data not found"); |
| 1708 | } |
Michael Kruse | 3562f27 | 2017-07-20 16:47:57 +0000 | [diff] [blame] | 1709 | if (MA->isRead() && MA->isOriginalAnyPHIKind()) { |
| 1710 | bool Found = PHIReads.erase(cast<PHINode>(MA->getAccessInstruction())); |
| 1711 | (void)Found; |
| 1712 | assert(Found && "Expected access data not found"); |
| 1713 | } |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 1716 | void ScopStmt::removeMemoryAccess(MemoryAccess *MA) { |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1717 | // Remove the memory accesses from this statement together with all scalar |
| 1718 | // accesses that were caused by it. MemoryKind::Value READs have no access |
| 1719 | // instruction, hence would not be removed by this function. However, it is |
| 1720 | // only used for invariant LoadInst accesses, its arguments are always affine, |
| 1721 | // hence synthesizable, and therefore there are no MemoryKind::Value READ |
| 1722 | // accesses to be removed. |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 1723 | auto Predicate = [&](MemoryAccess *Acc) { |
| 1724 | return Acc->getAccessInstruction() == MA->getAccessInstruction(); |
| 1725 | }; |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1726 | for (auto *MA : MemAccs) { |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 1727 | if (Predicate(MA)) { |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1728 | removeAccessData(MA); |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 1729 | Parent.removeAccessData(MA); |
| 1730 | } |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1731 | } |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 1732 | MemAccs.erase(std::remove_if(MemAccs.begin(), MemAccs.end(), Predicate), |
| 1733 | MemAccs.end()); |
| 1734 | InstructionToAccess.erase(MA->getAccessInstruction()); |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
Michael Kruse | 192e7f7 | 2018-04-09 23:13:05 +0000 | [diff] [blame] | 1737 | void ScopStmt::removeSingleMemoryAccess(MemoryAccess *MA, bool AfterHoisting) { |
| 1738 | if (AfterHoisting) { |
| 1739 | auto MAIt = std::find(MemAccs.begin(), MemAccs.end(), MA); |
| 1740 | assert(MAIt != MemAccs.end()); |
| 1741 | MemAccs.erase(MAIt); |
Michael Kruse | 0446d81 | 2017-03-10 16:05:24 +0000 | [diff] [blame] | 1742 | |
Michael Kruse | 192e7f7 | 2018-04-09 23:13:05 +0000 | [diff] [blame] | 1743 | removeAccessData(MA); |
| 1744 | Parent.removeAccessData(MA); |
| 1745 | } |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1746 | |
Michael Kruse | 0446d81 | 2017-03-10 16:05:24 +0000 | [diff] [blame] | 1747 | auto It = InstructionToAccess.find(MA->getAccessInstruction()); |
| 1748 | if (It != InstructionToAccess.end()) { |
| 1749 | It->second.remove(MA); |
| 1750 | if (It->second.empty()) |
| 1751 | InstructionToAccess.erase(MA->getAccessInstruction()); |
| 1752 | } |
| 1753 | } |
| 1754 | |
Michael Kruse | 07e8c36 | 2017-07-24 12:43:27 +0000 | [diff] [blame] | 1755 | MemoryAccess *ScopStmt::ensureValueRead(Value *V) { |
| 1756 | MemoryAccess *Access = lookupInputAccessOf(V); |
| 1757 | if (Access) |
| 1758 | return Access; |
| 1759 | |
| 1760 | ScopArrayInfo *SAI = |
| 1761 | Parent.getOrCreateScopArrayInfo(V, V->getType(), {}, MemoryKind::Value); |
| 1762 | Access = new MemoryAccess(this, nullptr, MemoryAccess::READ, V, V->getType(), |
| 1763 | true, {}, {}, V, MemoryKind::Value); |
| 1764 | Parent.addAccessFunction(Access); |
| 1765 | Access->buildAccessRelation(SAI); |
| 1766 | addAccess(Access); |
| 1767 | Parent.addAccessData(Access); |
| 1768 | return Access; |
| 1769 | } |
| 1770 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1771 | raw_ostream &polly::operator<<(raw_ostream &OS, const ScopStmt &S) { |
| 1772 | S.print(OS, PollyPrintInstructions); |
| 1773 | return OS; |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1776 | //===----------------------------------------------------------------------===// |
| 1777 | /// Scop class implement |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 1778 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 1779 | void Scop::setContext(isl::set NewContext) { |
| 1780 | Context = NewContext.align_params(Context.get_space()); |
Tobias Grosser | ff9b54d | 2011-11-15 11:38:44 +0000 | [diff] [blame] | 1781 | } |
| 1782 | |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1783 | namespace { |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1784 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1785 | /// Remap parameter values but keep AddRecs valid wrt. invariant loads. |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 1786 | struct SCEVSensitiveParameterRewriter |
Tobias Grosser | 278f9e7 | 2016-11-26 17:58:40 +0000 | [diff] [blame] | 1787 | : public SCEVRewriteVisitor<SCEVSensitiveParameterRewriter> { |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1788 | const ValueToValueMap &VMap; |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 1789 | |
| 1790 | public: |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1791 | SCEVSensitiveParameterRewriter(const ValueToValueMap &VMap, |
| 1792 | ScalarEvolution &SE) |
Tobias Grosser | 278f9e7 | 2016-11-26 17:58:40 +0000 | [diff] [blame] | 1793 | : SCEVRewriteVisitor(SE), VMap(VMap) {} |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 1794 | |
| 1795 | static const SCEV *rewrite(const SCEV *E, ScalarEvolution &SE, |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1796 | const ValueToValueMap &VMap) { |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 1797 | SCEVSensitiveParameterRewriter SSPR(VMap, SE); |
| 1798 | return SSPR.visit(E); |
| 1799 | } |
| 1800 | |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 1801 | const SCEV *visitAddRecExpr(const SCEVAddRecExpr *E) { |
| 1802 | auto *Start = visit(E->getStart()); |
| 1803 | auto *AddRec = SE.getAddRecExpr(SE.getConstant(E->getType(), 0), |
| 1804 | visit(E->getStepRecurrence(SE)), |
| 1805 | E->getLoop(), SCEV::FlagAnyWrap); |
| 1806 | return SE.getAddExpr(Start, AddRec); |
| 1807 | } |
| 1808 | |
| 1809 | const SCEV *visitUnknown(const SCEVUnknown *E) { |
| 1810 | if (auto *NewValue = VMap.lookup(E->getValue())) |
| 1811 | return SE.getUnknown(NewValue); |
| 1812 | return E; |
| 1813 | } |
| 1814 | }; |
| 1815 | |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1816 | /// Check whether we should remap a SCEV expression. |
| 1817 | struct SCEVFindInsideScop : public SCEVTraversal<SCEVFindInsideScop> { |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1818 | const ValueToValueMap &VMap; |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1819 | bool FoundInside = false; |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1820 | const Scop *S; |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1821 | |
| 1822 | public: |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1823 | SCEVFindInsideScop(const ValueToValueMap &VMap, ScalarEvolution &SE, |
| 1824 | const Scop *S) |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1825 | : SCEVTraversal(*this), VMap(VMap), S(S) {} |
| 1826 | |
| 1827 | static bool hasVariant(const SCEV *E, ScalarEvolution &SE, |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1828 | const ValueToValueMap &VMap, const Scop *S) { |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1829 | SCEVFindInsideScop SFIS(VMap, SE, S); |
| 1830 | SFIS.visitAll(E); |
| 1831 | return SFIS.FoundInside; |
| 1832 | } |
| 1833 | |
| 1834 | bool follow(const SCEV *E) { |
| 1835 | if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(E)) { |
| 1836 | FoundInside |= S->getRegion().contains(AddRec->getLoop()); |
| 1837 | } else if (auto *Unknown = dyn_cast<SCEVUnknown>(E)) { |
| 1838 | if (Instruction *I = dyn_cast<Instruction>(Unknown->getValue())) |
| 1839 | FoundInside |= S->getRegion().contains(I) && !VMap.count(I); |
| 1840 | } |
| 1841 | return !FoundInside; |
| 1842 | } |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1843 | |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1844 | bool isDone() { return FoundInside; } |
| 1845 | }; |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1846 | } // end anonymous namespace |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1847 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1848 | const SCEV *Scop::getRepresentingInvariantLoadSCEV(const SCEV *E) const { |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1849 | // Check whether it makes sense to rewrite the SCEV. (ScalarEvolution |
| 1850 | // doesn't like addition between an AddRec and an expression that |
| 1851 | // doesn't have a dominance relationship with it.) |
| 1852 | if (SCEVFindInsideScop::hasVariant(E, *SE, InvEquivClassVMap, this)) |
| 1853 | return E; |
| 1854 | |
| 1855 | // Rewrite SCEV. |
| 1856 | return SCEVSensitiveParameterRewriter::rewrite(E, *SE, InvEquivClassVMap); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 1859 | // This table of function names is used to translate parameter names in more |
| 1860 | // human-readable names. This makes it easier to interpret Polly analysis |
| 1861 | // results. |
| 1862 | StringMap<std::string> KnownNames = { |
| 1863 | {"_Z13get_global_idj", "global_id"}, |
| 1864 | {"_Z12get_local_idj", "local_id"}, |
| 1865 | {"_Z15get_global_sizej", "global_size"}, |
| 1866 | {"_Z14get_local_sizej", "local_size"}, |
| 1867 | {"_Z12get_work_dimv", "work_dim"}, |
| 1868 | {"_Z17get_global_offsetj", "global_offset"}, |
| 1869 | {"_Z12get_group_idj", "group_id"}, |
| 1870 | {"_Z14get_num_groupsj", "num_groups"}, |
| 1871 | }; |
| 1872 | |
| 1873 | static std::string getCallParamName(CallInst *Call) { |
| 1874 | std::string Result; |
| 1875 | raw_string_ostream OS(Result); |
| 1876 | std::string Name = Call->getCalledFunction()->getName(); |
| 1877 | |
| 1878 | auto Iterator = KnownNames.find(Name); |
| 1879 | if (Iterator != KnownNames.end()) |
Tobias Grosser | dff902f | 2017-06-01 12:46:51 +0000 | [diff] [blame] | 1880 | Name = "__" + Iterator->getValue(); |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 1881 | OS << Name; |
| 1882 | for (auto &Operand : Call->arg_operands()) { |
| 1883 | ConstantInt *Op = cast<ConstantInt>(&Operand); |
| 1884 | OS << "_" << Op->getValue(); |
| 1885 | } |
| 1886 | OS.flush(); |
| 1887 | return Result; |
| 1888 | } |
| 1889 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 1890 | void Scop::createParameterId(const SCEV *Parameter) { |
| 1891 | assert(Parameters.count(Parameter)); |
| 1892 | assert(!ParameterIds.count(Parameter)); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 1893 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 1894 | std::string ParameterName = "p_" + std::to_string(getNumParams() - 1); |
Tobias Grosser | b39c96a | 2015-11-17 11:54:51 +0000 | [diff] [blame] | 1895 | |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 1896 | if (const SCEVUnknown *ValueParameter = dyn_cast<SCEVUnknown>(Parameter)) { |
| 1897 | Value *Val = ValueParameter->getValue(); |
| 1898 | CallInst *Call = dyn_cast<CallInst>(Val); |
Tobias Grosser | 8f99c16 | 2011-11-15 11:38:55 +0000 | [diff] [blame] | 1899 | |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 1900 | if (Call && isConstCall(Call)) { |
| 1901 | ParameterName = getCallParamName(Call); |
| 1902 | } else if (UseInstructionNames) { |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 1903 | // If this parameter references a specific Value and this value has a name |
| 1904 | // we use this name as it is likely to be unique and more useful than just |
| 1905 | // a number. |
| 1906 | if (Val->hasName()) |
| 1907 | ParameterName = Val->getName(); |
| 1908 | else if (LoadInst *LI = dyn_cast<LoadInst>(Val)) { |
| 1909 | auto *LoadOrigin = LI->getPointerOperand()->stripInBoundsOffsets(); |
| 1910 | if (LoadOrigin->hasName()) { |
| 1911 | ParameterName += "_loaded_from_"; |
| 1912 | ParameterName += |
| 1913 | LI->getPointerOperand()->stripInBoundsOffsets()->getName(); |
| 1914 | } |
Tobias Grosser | b39c96a | 2015-11-17 11:54:51 +0000 | [diff] [blame] | 1915 | } |
| 1916 | } |
Tobias Grosser | 8f99c16 | 2011-11-15 11:38:55 +0000 | [diff] [blame] | 1917 | |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 1918 | ParameterName = getIslCompatibleName("", ParameterName, ""); |
| 1919 | } |
Tobias Grosser | 2ea7c6e | 2016-07-01 13:40:28 +0000 | [diff] [blame] | 1920 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1921 | isl::id Id = isl::id::alloc(getIslCtx(), ParameterName, |
Tobias Grosser | 6e78cc6 | 2017-08-13 17:54:51 +0000 | [diff] [blame] | 1922 | const_cast<void *>((const void *)Parameter)); |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 1923 | ParameterIds[Parameter] = Id; |
| 1924 | } |
| 1925 | |
| 1926 | void Scop::addParams(const ParameterSetTy &NewParameters) { |
| 1927 | for (const SCEV *Parameter : NewParameters) { |
| 1928 | // Normalize the SCEV to get the representing element for an invariant load. |
| 1929 | Parameter = extractConstantFactor(Parameter, *SE).second; |
| 1930 | Parameter = getRepresentingInvariantLoadSCEV(Parameter); |
| 1931 | |
| 1932 | if (Parameters.insert(Parameter)) |
| 1933 | createParameterId(Parameter); |
| 1934 | } |
| 1935 | } |
| 1936 | |
Tobias Grosser | 9a63570 | 2017-08-06 19:31:27 +0000 | [diff] [blame] | 1937 | isl::id Scop::getIdForParam(const SCEV *Parameter) const { |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 1938 | // Normalize the SCEV to get the representing element for an invariant load. |
| 1939 | Parameter = getRepresentingInvariantLoadSCEV(Parameter); |
Tobias Grosser | 6e78cc6 | 2017-08-13 17:54:51 +0000 | [diff] [blame] | 1940 | return ParameterIds.lookup(Parameter); |
Tobias Grosser | 76c2e32 | 2011-11-07 12:58:59 +0000 | [diff] [blame] | 1941 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1942 | |
Johannes Doerfert | e0b0807 | 2016-05-23 12:43:44 +0000 | [diff] [blame] | 1943 | bool Scop::isDominatedBy(const DominatorTree &DT, BasicBlock *BB) const { |
| 1944 | return DT.dominates(BB, getEntry()); |
| 1945 | } |
| 1946 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1947 | void Scop::addUserAssumptions( |
| 1948 | AssumptionCache &AC, DominatorTree &DT, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 1949 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 1950 | for (auto &Assumption : AC.assumptions()) { |
| 1951 | auto *CI = dyn_cast_or_null<CallInst>(Assumption); |
| 1952 | if (!CI || CI->getNumArgOperands() != 1) |
Johannes Doerfert | 2af10e2 | 2015-11-12 03:25:01 +0000 | [diff] [blame] | 1953 | continue; |
Johannes Doerfert | 2b92a0e | 2016-05-10 14:00:57 +0000 | [diff] [blame] | 1954 | |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 1955 | bool InScop = contains(CI); |
| 1956 | if (!InScop && !isDominatedBy(DT, CI->getParent())) |
| 1957 | continue; |
Johannes Doerfert | 2af10e2 | 2015-11-12 03:25:01 +0000 | [diff] [blame] | 1958 | |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 1959 | auto *L = LI.getLoopFor(CI->getParent()); |
| 1960 | auto *Val = CI->getArgOperand(0); |
| 1961 | ParameterSetTy DetectedParams; |
| 1962 | if (!isAffineConstraint(Val, &R, L, *SE, DetectedParams)) { |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 1963 | ORE.emit( |
| 1964 | OptimizationRemarkAnalysis(DEBUG_TYPE, "IgnoreUserAssumption", CI) |
| 1965 | << "Non-affine user assumption ignored."); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 1966 | continue; |
Michael Kruse | 7037fde | 2016-12-15 09:25:14 +0000 | [diff] [blame] | 1967 | } |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 1968 | |
| 1969 | // Collect all newly introduced parameters. |
| 1970 | ParameterSetTy NewParams; |
| 1971 | for (auto *Param : DetectedParams) { |
| 1972 | Param = extractConstantFactor(Param, *SE).second; |
| 1973 | Param = getRepresentingInvariantLoadSCEV(Param); |
| 1974 | if (Parameters.count(Param)) |
| 1975 | continue; |
| 1976 | NewParams.insert(Param); |
| 1977 | } |
| 1978 | |
| 1979 | SmallVector<isl_set *, 2> ConditionSets; |
| 1980 | auto *TI = InScop ? CI->getParent()->getTerminator() : nullptr; |
Michael Kruse | 1df1aac | 2017-07-26 13:25:28 +0000 | [diff] [blame] | 1981 | BasicBlock *BB = InScop ? CI->getParent() : getRegion().getEntry(); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 1982 | auto *Dom = InScop ? DomainMap[BB].copy() : Context.copy(); |
Michael Kruse | 1df1aac | 2017-07-26 13:25:28 +0000 | [diff] [blame] | 1983 | assert(Dom && "Cannot propagate a nullptr."); |
| 1984 | bool Valid = buildConditionSets(*this, BB, Val, TI, L, Dom, |
| 1985 | InvalidDomainMap, ConditionSets); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 1986 | isl_set_free(Dom); |
| 1987 | |
| 1988 | if (!Valid) |
| 1989 | continue; |
| 1990 | |
| 1991 | isl_set *AssumptionCtx = nullptr; |
| 1992 | if (InScop) { |
| 1993 | AssumptionCtx = isl_set_complement(isl_set_params(ConditionSets[1])); |
| 1994 | isl_set_free(ConditionSets[0]); |
| 1995 | } else { |
| 1996 | AssumptionCtx = isl_set_complement(ConditionSets[1]); |
| 1997 | AssumptionCtx = isl_set_intersect(AssumptionCtx, ConditionSets[0]); |
| 1998 | } |
| 1999 | |
| 2000 | // Project out newly introduced parameters as they are not otherwise useful. |
| 2001 | if (!NewParams.empty()) { |
| 2002 | for (unsigned u = 0; u < isl_set_n_param(AssumptionCtx); u++) { |
| 2003 | auto *Id = isl_set_get_dim_id(AssumptionCtx, isl_dim_param, u); |
| 2004 | auto *Param = static_cast<const SCEV *>(isl_id_get_user(Id)); |
| 2005 | isl_id_free(Id); |
| 2006 | |
| 2007 | if (!NewParams.count(Param)) |
| 2008 | continue; |
| 2009 | |
| 2010 | AssumptionCtx = |
| 2011 | isl_set_project_out(AssumptionCtx, isl_dim_param, u--, 1); |
| 2012 | } |
| 2013 | } |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 2014 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "UserAssumption", CI) |
| 2015 | << "Use user assumption: " << stringFromIslObj(AssumptionCtx)); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2016 | Context = Context.intersect(isl::manage(AssumptionCtx)); |
Johannes Doerfert | 2af10e2 | 2015-11-12 03:25:01 +0000 | [diff] [blame] | 2017 | } |
| 2018 | } |
| 2019 | |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2020 | void Scop::addUserContext() { |
| 2021 | if (UserContextStr.empty()) |
| 2022 | return; |
| 2023 | |
Tobias Grosser | 9b9c701 | 2018-05-28 07:45:25 +0000 | [diff] [blame] | 2024 | isl::set UserContext = isl::set(getIslCtx(), UserContextStr.c_str()); |
| 2025 | isl::space Space = getParamSpace(); |
| 2026 | if (Space.dim(isl::dim::param) != UserContext.dim(isl::dim::param)) { |
| 2027 | std::string SpaceStr = Space.to_str(); |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2028 | errs() << "Error: the context provided in -polly-context has not the same " |
| 2029 | << "number of dimensions than the computed context. Due to this " |
| 2030 | << "mismatch, the -polly-context option is ignored. Please provide " |
| 2031 | << "the context in the parameter space: " << SpaceStr << ".\n"; |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2032 | return; |
| 2033 | } |
| 2034 | |
Tobias Grosser | 9b9c701 | 2018-05-28 07:45:25 +0000 | [diff] [blame] | 2035 | for (unsigned i = 0; i < Space.dim(isl::dim::param); i++) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2036 | std::string NameContext = Context.get_dim_name(isl::dim::param, i); |
Tobias Grosser | 9b9c701 | 2018-05-28 07:45:25 +0000 | [diff] [blame] | 2037 | std::string NameUserContext = UserContext.get_dim_name(isl::dim::param, i); |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2038 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2039 | if (NameContext != NameUserContext) { |
Tobias Grosser | 9b9c701 | 2018-05-28 07:45:25 +0000 | [diff] [blame] | 2040 | std::string SpaceStr = Space.to_str(); |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2041 | errs() << "Error: the name of dimension " << i |
| 2042 | << " provided in -polly-context " |
| 2043 | << "is '" << NameUserContext << "', but the name in the computed " |
| 2044 | << "context is '" << NameContext |
| 2045 | << "'. Due to this name mismatch, " |
| 2046 | << "the -polly-context option is ignored. Please provide " |
| 2047 | << "the context in the parameter space: " << SpaceStr << ".\n"; |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2048 | return; |
| 2049 | } |
| 2050 | |
Tobias Grosser | 9b9c701 | 2018-05-28 07:45:25 +0000 | [diff] [blame] | 2051 | UserContext = UserContext.set_dim_id(isl::dim::param, i, |
| 2052 | Space.get_dim_id(isl::dim::param, i)); |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
Tobias Grosser | 9b9c701 | 2018-05-28 07:45:25 +0000 | [diff] [blame] | 2055 | Context = Context.intersect(UserContext); |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2058 | void Scop::buildContext() { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2059 | isl::space Space = isl::space::params_alloc(getIslCtx(), 0); |
| 2060 | Context = isl::set::universe(Space); |
| 2061 | InvalidContext = isl::set::empty(Space); |
| 2062 | AssumedContext = isl::set::universe(Space); |
Tobias Grosser | 0e27e24 | 2011-10-06 00:03:48 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 2065 | void Scop::addParameterBounds() { |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2066 | unsigned PDim = 0; |
| 2067 | for (auto *Parameter : Parameters) { |
| 2068 | ConstantRange SRange = SE->getSignedRange(Parameter); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2069 | Context = addRangeBoundsToSet(Context, SRange, PDim++, isl::dim::param); |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 2070 | } |
| 2071 | } |
| 2072 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2073 | static std::vector<isl::id> getFortranArrayIds(Scop::array_range Arrays) { |
| 2074 | std::vector<isl::id> OutermostSizeIds; |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2075 | for (auto Array : Arrays) { |
| 2076 | // To check if an array is a Fortran array, we check if it has a isl_pw_aff |
| 2077 | // for its outermost dimension. Fortran arrays will have this since the |
| 2078 | // outermost dimension size can be picked up from their runtime description. |
| 2079 | // TODO: actually need to check if it has a FAD, but for now this works. |
| 2080 | if (Array->getNumberOfDimensions() > 0) { |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2081 | isl::pw_aff PwAff = Array->getDimensionSizePw(0); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2082 | if (!PwAff) |
| 2083 | continue; |
| 2084 | |
Tobias Grosser | 9b29af9 | 2018-06-18 12:49:47 +0000 | [diff] [blame] | 2085 | isl::id Id = PwAff.get_dim_id(isl::dim::param, 0); |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2086 | assert(!Id.is_null() && |
| 2087 | "Invalid Id for PwAff expression in Fortran array"); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2088 | OutermostSizeIds.push_back(Id); |
| 2089 | } |
| 2090 | } |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2091 | return OutermostSizeIds; |
| 2092 | } |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2093 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2094 | // The FORTRAN array size parameters are known to be non-negative. |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2095 | static isl::set boundFortranArrayParams(isl::set Context, |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2096 | Scop::array_range Arrays) { |
| 2097 | std::vector<isl::id> OutermostSizeIds; |
| 2098 | OutermostSizeIds = getFortranArrayIds(Arrays); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2099 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2100 | for (isl::id Id : OutermostSizeIds) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2101 | int dim = Context.find_dim_by_id(isl::dim::param, Id); |
| 2102 | Context = Context.lower_bound_si(isl::dim::param, dim, 0); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2103 | } |
| 2104 | |
| 2105 | return Context; |
| 2106 | } |
| 2107 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 2108 | void Scop::realignParams() { |
Tobias Grosser | 5842dee | 2017-03-17 13:00:53 +0000 | [diff] [blame] | 2109 | if (PollyIgnoreParamBounds) |
| 2110 | return; |
| 2111 | |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2112 | // Add all parameters into a common model. |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2113 | isl::space Space = getFullParamSpace(); |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2114 | |
| 2115 | // Align the parameters of all data structures to the model. |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2116 | Context = Context.align_params(Space); |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2117 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2118 | // Bound the size of the fortran array dimensions. |
| 2119 | Context = boundFortranArrayParams(Context, arrays()); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2120 | |
Johannes Doerfert | a60ad84 | 2016-05-10 12:18:22 +0000 | [diff] [blame] | 2121 | // As all parameters are known add bounds to them. |
| 2122 | addParameterBounds(); |
| 2123 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 2124 | for (ScopStmt &Stmt : *this) |
| 2125 | Stmt.realignParams(); |
Johannes Doerfert | 06445ded | 2016-06-02 15:07:41 +0000 | [diff] [blame] | 2126 | // Simplify the schedule according to the context too. |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2127 | Schedule = Schedule.gist_domain_params(getContext()); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 2128 | } |
| 2129 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2130 | static isl::set simplifyAssumptionContext(isl::set AssumptionContext, |
| 2131 | const Scop &S) { |
Tobias Grosser | cdbe5c9 | 2017-01-06 17:30:34 +0000 | [diff] [blame] | 2132 | // If we have modeled all blocks in the SCoP that have side effects we can |
| 2133 | // simplify the context with the constraints that are needed for anything to |
| 2134 | // be executed at all. However, if we have error blocks in the SCoP we already |
| 2135 | // assumed some parameter combinations cannot occur and removed them from the |
Johannes Doerfert | f85ad04 | 2015-11-08 20:16:39 +0000 | [diff] [blame] | 2136 | // domains, thus we cannot use the remaining domain to simplify the |
| 2137 | // assumptions. |
| 2138 | if (!S.hasErrorBlock()) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2139 | auto DomainParameters = S.getDomains().params(); |
| 2140 | AssumptionContext = AssumptionContext.gist_params(DomainParameters); |
Johannes Doerfert | f85ad04 | 2015-11-08 20:16:39 +0000 | [diff] [blame] | 2141 | } |
| 2142 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2143 | AssumptionContext = AssumptionContext.gist_params(S.getContext()); |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 2144 | return AssumptionContext; |
| 2145 | } |
| 2146 | |
| 2147 | void Scop::simplifyContexts() { |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2148 | // The parameter constraints of the iteration domains give us a set of |
| 2149 | // constraints that need to hold for all cases where at least a single |
| 2150 | // statement iteration is executed in the whole scop. We now simplify the |
| 2151 | // assumed context under the assumption that such constraints hold and at |
| 2152 | // least a single statement iteration is executed. For cases where no |
| 2153 | // statement instances are executed, the assumptions we have taken about |
| 2154 | // the executed code do not matter and can be changed. |
| 2155 | // |
| 2156 | // WARNING: This only holds if the assumptions we have taken do not reduce |
| 2157 | // the set of statement instances that are executed. Otherwise we |
| 2158 | // may run into a case where the iteration domains suggest that |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 2159 | // for a certain set of parameter constraints no code is executed, |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2160 | // but in the original program some computation would have been |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 2161 | // performed. In such a case, modifying the run-time conditions and |
| 2162 | // possibly influencing the run-time check may cause certain scops |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2163 | // to not be executed. |
| 2164 | // |
| 2165 | // Example: |
| 2166 | // |
| 2167 | // When delinearizing the following code: |
| 2168 | // |
| 2169 | // for (long i = 0; i < 100; i++) |
| 2170 | // for (long j = 0; j < m; j++) |
| 2171 | // A[i+p][j] = 1.0; |
| 2172 | // |
| 2173 | // 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] | 2174 | // 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] | 2175 | // 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] | 2176 | AssumedContext = simplifyAssumptionContext(AssumedContext, *this); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2177 | InvalidContext = InvalidContext.align_params(getParamSpace()); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2178 | } |
| 2179 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2180 | /// Helper to treat non-affine regions and basic blocks the same. |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2181 | /// |
| 2182 | ///{ |
| 2183 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2184 | /// Return the block that is the representing block for @p RN. |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2185 | static inline BasicBlock *getRegionNodeBasicBlock(RegionNode *RN) { |
| 2186 | return RN->isSubRegion() ? RN->getNodeAs<Region>()->getEntry() |
| 2187 | : RN->getNodeAs<BasicBlock>(); |
| 2188 | } |
| 2189 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2190 | /// Return the @p idx'th block that is executed after @p RN. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2191 | static inline BasicBlock * |
Chandler Carruth | e303c87 | 2018-10-15 10:42:50 +0000 | [diff] [blame] | 2192 | getRegionNodeSuccessor(RegionNode *RN, Instruction *TI, unsigned idx) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2193 | if (RN->isSubRegion()) { |
| 2194 | assert(idx == 0); |
| 2195 | return RN->getNodeAs<Region>()->getExit(); |
| 2196 | } |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2197 | return TI->getSuccessor(idx); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2198 | } |
| 2199 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2200 | /// Return the smallest loop surrounding @p RN. |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2201 | static inline Loop *getRegionNodeLoop(RegionNode *RN, LoopInfo &LI) { |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 2202 | if (!RN->isSubRegion()) { |
| 2203 | BasicBlock *BB = RN->getNodeAs<BasicBlock>(); |
| 2204 | Loop *L = LI.getLoopFor(BB); |
| 2205 | |
| 2206 | // Unreachable statements are not considered to belong to a LLVM loop, as |
| 2207 | // they are not part of an actual loop in the control flow graph. |
| 2208 | // Nevertheless, we handle certain unreachable statements that are common |
| 2209 | // when modeling run-time bounds checks as being part of the loop to be |
| 2210 | // able to model them and to later eliminate the run-time bounds checks. |
| 2211 | // |
| 2212 | // Specifically, for basic blocks that terminate in an unreachable and |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 2213 | // where the immediate predecessor is part of a loop, we assume these |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 2214 | // basic blocks belong to the loop the predecessor belongs to. This |
| 2215 | // allows us to model the following code. |
| 2216 | // |
| 2217 | // for (i = 0; i < N; i++) { |
| 2218 | // if (i > 1024) |
| 2219 | // abort(); <- this abort might be translated to an |
| 2220 | // unreachable |
| 2221 | // |
| 2222 | // A[i] = ... |
| 2223 | // } |
| 2224 | if (!L && isa<UnreachableInst>(BB->getTerminator()) && BB->getPrevNode()) |
| 2225 | L = LI.getLoopFor(BB->getPrevNode()); |
| 2226 | return L; |
| 2227 | } |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2228 | |
| 2229 | Region *NonAffineSubRegion = RN->getNodeAs<Region>(); |
| 2230 | Loop *L = LI.getLoopFor(NonAffineSubRegion->getEntry()); |
| 2231 | while (L && NonAffineSubRegion->contains(L)) |
| 2232 | L = L->getParentLoop(); |
| 2233 | return L; |
| 2234 | } |
| 2235 | |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 2236 | /// Get the number of blocks in @p L. |
| 2237 | /// |
| 2238 | /// The number of blocks in a loop are the number of basic blocks actually |
| 2239 | /// belonging to the loop, as well as all single basic blocks that the loop |
| 2240 | /// exits to and which terminate in an unreachable instruction. We do not |
| 2241 | /// allow such basic blocks in the exit of a scop, hence they belong to the |
| 2242 | /// scop and represent run-time conditions which we want to model and |
| 2243 | /// subsequently speculate away. |
| 2244 | /// |
| 2245 | /// @see getRegionNodeLoop for additional details. |
Reid Kleckner | df2b283 | 2017-06-19 17:44:02 +0000 | [diff] [blame] | 2246 | unsigned getNumBlocksInLoop(Loop *L) { |
| 2247 | unsigned NumBlocks = L->getNumBlocks(); |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 2248 | SmallVector<BasicBlock *, 4> ExitBlocks; |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 2249 | L->getExitBlocks(ExitBlocks); |
| 2250 | |
| 2251 | for (auto ExitBlock : ExitBlocks) { |
| 2252 | if (isa<UnreachableInst>(ExitBlock->getTerminator())) |
| 2253 | NumBlocks++; |
| 2254 | } |
| 2255 | return NumBlocks; |
| 2256 | } |
| 2257 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2258 | static inline unsigned getNumBlocksInRegionNode(RegionNode *RN) { |
| 2259 | if (!RN->isSubRegion()) |
| 2260 | return 1; |
| 2261 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2262 | Region *R = RN->getNodeAs<Region>(); |
Tobias Grosser | 0dd4a9a | 2016-02-01 01:55:08 +0000 | [diff] [blame] | 2263 | return std::distance(R->block_begin(), R->block_end()); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2264 | } |
| 2265 | |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 2266 | static bool containsErrorBlock(RegionNode *RN, const Region &R, LoopInfo &LI, |
| 2267 | const DominatorTree &DT) { |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2268 | if (!RN->isSubRegion()) |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 2269 | return isErrorBlock(*RN->getNodeAs<BasicBlock>(), R, LI, DT); |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2270 | for (BasicBlock *BB : RN->getNodeAs<Region>()->blocks()) |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 2271 | if (isErrorBlock(*BB, R, LI, DT)) |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2272 | return true; |
| 2273 | return false; |
| 2274 | } |
| 2275 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2276 | ///} |
| 2277 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 2278 | isl::set Scop::getDomainConditions(const ScopStmt *Stmt) const { |
Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 2279 | return getDomainConditions(Stmt->getEntryBlock()); |
Johannes Doerfert | cef616f | 2015-09-15 22:49:04 +0000 | [diff] [blame] | 2280 | } |
| 2281 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 2282 | isl::set Scop::getDomainConditions(BasicBlock *BB) const { |
Johannes Doerfert | 41cda15 | 2016-04-08 10:32:26 +0000 | [diff] [blame] | 2283 | auto DIt = DomainMap.find(BB); |
| 2284 | if (DIt != DomainMap.end()) |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 2285 | return DIt->getSecond(); |
Johannes Doerfert | 41cda15 | 2016-04-08 10:32:26 +0000 | [diff] [blame] | 2286 | |
| 2287 | auto &RI = *R.getRegionInfo(); |
| 2288 | auto *BBR = RI.getRegionFor(BB); |
| 2289 | while (BBR->getEntry() == BB) |
| 2290 | BBR = BBR->getParent(); |
| 2291 | return getDomainConditions(BBR->getEntry()); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2292 | } |
| 2293 | |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2294 | bool Scop::buildDomains(Region *R, DominatorTree &DT, LoopInfo &LI, |
| 2295 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2296 | bool IsOnlyNonAffineRegion = isNonAffineSubRegion(R); |
Johannes Doerfert | f08bd00 | 2015-08-31 13:56:32 +0000 | [diff] [blame] | 2297 | auto *EntryBB = R->getEntry(); |
Johannes Doerfert | 432658d | 2016-01-26 11:01:41 +0000 | [diff] [blame] | 2298 | auto *L = IsOnlyNonAffineRegion ? nullptr : LI.getLoopFor(EntryBB); |
| 2299 | int LD = getRelativeLoopDepth(L); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2300 | auto *S = isl_set_universe(isl_space_set_alloc(getIslCtx().get(), 0, LD + 1)); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2301 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2302 | while (LD-- >= 0) { |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2303 | L = L->getParentLoop(); |
| 2304 | } |
| 2305 | |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2306 | InvalidDomainMap[EntryBB] = isl::manage(isl_set_empty(isl_set_get_space(S))); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2307 | DomainMap[EntryBB] = isl::manage(S); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2308 | |
Johannes Doerfert | 432658d | 2016-01-26 11:01:41 +0000 | [diff] [blame] | 2309 | if (IsOnlyNonAffineRegion) |
Johannes Doerfert | 2640454 | 2016-05-10 12:19:47 +0000 | [diff] [blame] | 2310 | return !containsErrorBlock(R->getNode(), *R, LI, DT); |
Johannes Doerfert | 40fa56f | 2015-09-14 11:15:07 +0000 | [diff] [blame] | 2311 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2312 | if (!buildDomainsWithBranchConstraints(R, DT, LI, InvalidDomainMap)) |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2313 | return false; |
| 2314 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2315 | if (!propagateDomainConstraints(R, DT, LI, InvalidDomainMap)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2316 | return false; |
Tobias Grosser | 9737c7b | 2015-11-22 11:06:51 +0000 | [diff] [blame] | 2317 | |
| 2318 | // Error blocks and blocks dominated by them have been assumed to never be |
| 2319 | // executed. Representing them in the Scop does not add any value. In fact, |
| 2320 | // it is likely to cause issues during construction of the ScopStmts. The |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2321 | // contents of error blocks have not been verified to be expressible and |
Tobias Grosser | 9737c7b | 2015-11-22 11:06:51 +0000 | [diff] [blame] | 2322 | // will cause problems when building up a ScopStmt for them. |
| 2323 | // Furthermore, basic blocks dominated by error blocks may reference |
| 2324 | // 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] | 2325 | // can themselves not be constructed properly. To this end we will replace |
| 2326 | // the domains of error blocks and those only reachable via error blocks |
| 2327 | // 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] | 2328 | // parameter combination it would be reached via an error block in its |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2329 | // InvalidDomain. This information is needed during load hoisting. |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2330 | if (!propagateInvalidStmtDomains(R, DT, LI, InvalidDomainMap)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2331 | return false; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2332 | |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2333 | return true; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2334 | } |
| 2335 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2336 | /// Adjust the dimensions of @p Dom that was constructed for @p OldL |
Johannes Doerfert | a07f0ac | 2016-04-04 07:50:40 +0000 | [diff] [blame] | 2337 | /// to be compatible to domains constructed for loop @p NewL. |
| 2338 | /// |
| 2339 | /// This function assumes @p NewL and @p OldL are equal or there is a CFG |
| 2340 | /// edge from @p OldL to @p NewL. |
Tobias Grosser | 67dc08b | 2018-06-18 13:01:52 +0000 | [diff] [blame] | 2341 | static isl::set adjustDomainDimensions(Scop &S, isl::set Dom, Loop *OldL, |
| 2342 | Loop *NewL) { |
Johannes Doerfert | a07f0ac | 2016-04-04 07:50:40 +0000 | [diff] [blame] | 2343 | // If the loops are the same there is nothing to do. |
| 2344 | if (NewL == OldL) |
| 2345 | return Dom; |
| 2346 | |
| 2347 | int OldDepth = S.getRelativeLoopDepth(OldL); |
| 2348 | int NewDepth = S.getRelativeLoopDepth(NewL); |
| 2349 | // If both loops are non-affine loops there is nothing to do. |
| 2350 | if (OldDepth == -1 && NewDepth == -1) |
| 2351 | return Dom; |
| 2352 | |
| 2353 | // Distinguish three cases: |
| 2354 | // 1) The depth is the same but the loops are not. |
| 2355 | // => One loop was left one was entered. |
| 2356 | // 2) The depth increased from OldL to NewL. |
| 2357 | // => One loop was entered, none was left. |
| 2358 | // 3) The depth decreased from OldL to NewL. |
| 2359 | // => Loops were left were difference of the depths defines how many. |
| 2360 | if (OldDepth == NewDepth) { |
| 2361 | assert(OldL->getParentLoop() == NewL->getParentLoop()); |
Tobias Grosser | 9b29af9 | 2018-06-18 12:49:47 +0000 | [diff] [blame] | 2362 | Dom = Dom.project_out(isl::dim::set, NewDepth, 1); |
| 2363 | Dom = Dom.add_dims(isl::dim::set, 1); |
Johannes Doerfert | a07f0ac | 2016-04-04 07:50:40 +0000 | [diff] [blame] | 2364 | } else if (OldDepth < NewDepth) { |
| 2365 | assert(OldDepth + 1 == NewDepth); |
| 2366 | auto &R = S.getRegion(); |
| 2367 | (void)R; |
| 2368 | assert(NewL->getParentLoop() == OldL || |
| 2369 | ((!OldL || !R.contains(OldL)) && R.contains(NewL))); |
Tobias Grosser | 9b29af9 | 2018-06-18 12:49:47 +0000 | [diff] [blame] | 2370 | Dom = Dom.add_dims(isl::dim::set, 1); |
Johannes Doerfert | a07f0ac | 2016-04-04 07:50:40 +0000 | [diff] [blame] | 2371 | } else { |
| 2372 | assert(OldDepth > NewDepth); |
| 2373 | int Diff = OldDepth - NewDepth; |
Tobias Grosser | 9b29af9 | 2018-06-18 12:49:47 +0000 | [diff] [blame] | 2374 | int NumDim = Dom.n_dim(); |
Johannes Doerfert | a07f0ac | 2016-04-04 07:50:40 +0000 | [diff] [blame] | 2375 | assert(NumDim >= Diff); |
Tobias Grosser | 9b29af9 | 2018-06-18 12:49:47 +0000 | [diff] [blame] | 2376 | Dom = Dom.project_out(isl::dim::set, NumDim - Diff, Diff); |
Johannes Doerfert | a07f0ac | 2016-04-04 07:50:40 +0000 | [diff] [blame] | 2377 | } |
| 2378 | |
| 2379 | return Dom; |
| 2380 | } |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2381 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2382 | bool Scop::propagateInvalidStmtDomains( |
| 2383 | Region *R, DominatorTree &DT, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2384 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2385 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 2386 | for (auto *RN : RTraversal) { |
| 2387 | |
| 2388 | // Recurse for affine subregions but go on for basic blocks and non-affine |
| 2389 | // subregions. |
| 2390 | if (RN->isSubRegion()) { |
| 2391 | Region *SubRegion = RN->getNodeAs<Region>(); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2392 | if (!isNonAffineSubRegion(SubRegion)) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2393 | propagateInvalidStmtDomains(SubRegion, DT, LI, InvalidDomainMap); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2394 | continue; |
| 2395 | } |
| 2396 | } |
| 2397 | |
| 2398 | bool ContainsErrorBlock = containsErrorBlock(RN, getRegion(), LI, DT); |
| 2399 | BasicBlock *BB = getRegionNodeBasicBlock(RN); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2400 | isl::set &Domain = DomainMap[BB]; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2401 | assert(Domain && "Cannot propagate a nullptr"); |
| 2402 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2403 | isl::set InvalidDomain = InvalidDomainMap[BB]; |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2404 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2405 | bool IsInvalidBlock = ContainsErrorBlock || Domain.is_subset(InvalidDomain); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2406 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2407 | if (!IsInvalidBlock) { |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2408 | InvalidDomain = InvalidDomain.intersect(Domain); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2409 | } else { |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2410 | InvalidDomain = Domain; |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2411 | isl::set DomPar = Domain.params(); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2412 | recordAssumption(ERRORBLOCK, DomPar, BB->getTerminator()->getDebugLoc(), |
| 2413 | AS_RESTRICTION); |
Michael Kruse | 842bdd0 | 2018-08-01 22:28:32 +0000 | [diff] [blame] | 2414 | Domain = isl::set::empty(Domain.get_space()); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2417 | if (InvalidDomain.is_empty()) { |
| 2418 | InvalidDomainMap[BB] = InvalidDomain; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2419 | continue; |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 2420 | } |
| 2421 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2422 | auto *BBLoop = getRegionNodeLoop(RN, LI); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2423 | auto *TI = BB->getTerminator(); |
| 2424 | unsigned NumSuccs = RN->isSubRegion() ? 1 : TI->getNumSuccessors(); |
| 2425 | for (unsigned u = 0; u < NumSuccs; u++) { |
| 2426 | auto *SuccBB = getRegionNodeSuccessor(RN, TI, u); |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 2427 | |
| 2428 | // Skip successors outside the SCoP. |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2429 | if (!contains(SuccBB)) |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 2430 | continue; |
| 2431 | |
Johannes Doerfert | e4459a2 | 2016-04-25 13:34:50 +0000 | [diff] [blame] | 2432 | // Skip backedges. |
| 2433 | if (DT.dominates(SuccBB, BB)) |
| 2434 | continue; |
| 2435 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2436 | Loop *SuccBBLoop = getFirstNonBoxedLoopFor(SuccBB, LI, getBoxedLoops()); |
| 2437 | |
Tobias Grosser | 67dc08b | 2018-06-18 13:01:52 +0000 | [diff] [blame] | 2438 | auto AdjustedInvalidDomain = |
| 2439 | adjustDomainDimensions(*this, InvalidDomain, BBLoop, SuccBBLoop); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2440 | |
Philip Pfaffe | 9375d57 | 2018-05-16 14:05:03 +0000 | [diff] [blame] | 2441 | isl::set SuccInvalidDomain = InvalidDomainMap[SuccBB]; |
| 2442 | SuccInvalidDomain = SuccInvalidDomain.unite(AdjustedInvalidDomain); |
| 2443 | SuccInvalidDomain = SuccInvalidDomain.coalesce(); |
| 2444 | unsigned NumConjucts = SuccInvalidDomain.n_basic_set(); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2445 | |
Philip Pfaffe | 9375d57 | 2018-05-16 14:05:03 +0000 | [diff] [blame] | 2446 | InvalidDomainMap[SuccBB] = SuccInvalidDomain; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2447 | |
Michael Kruse | bc15012 | 2016-05-02 12:25:18 +0000 | [diff] [blame] | 2448 | // Check if the maximal number of domain disjunctions was reached. |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2449 | // In case this happens we will bail. |
Tobias Grosser | 90411a9 | 2017-02-16 19:11:33 +0000 | [diff] [blame] | 2450 | if (NumConjucts < MaxDisjunctsInDomain) |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2451 | continue; |
| 2452 | |
Tobias Grosser | f44f005 | 2017-07-09 15:47:17 +0000 | [diff] [blame] | 2453 | InvalidDomainMap.erase(BB); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 2454 | invalidate(COMPLEXITY, TI->getDebugLoc(), TI->getParent()); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2455 | return false; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2456 | } |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2457 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2458 | InvalidDomainMap[BB] = InvalidDomain; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2459 | } |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2460 | |
| 2461 | return true; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2464 | void Scop::propagateDomainConstraintsToRegionExit( |
| 2465 | BasicBlock *BB, Loop *BBLoop, |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2466 | SmallPtrSetImpl<BasicBlock *> &FinishedExitBlocks, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2467 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2468 | // Check if the block @p BB is the entry of a region. If so we propagate it's |
| 2469 | // domain to the exit block of the region. Otherwise we are done. |
| 2470 | auto *RI = R.getRegionInfo(); |
| 2471 | auto *BBReg = RI ? RI->getRegionFor(BB) : nullptr; |
| 2472 | auto *ExitBB = BBReg ? BBReg->getExit() : nullptr; |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 2473 | if (!BBReg || BBReg->getEntry() != BB || !contains(ExitBB)) |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2474 | return; |
| 2475 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2476 | // Do not propagate the domain if there is a loop backedge inside the region |
Tobias Grosser | cdbe5c9 | 2017-01-06 17:30:34 +0000 | [diff] [blame] | 2477 | // that would prevent the exit block from being executed. |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2478 | auto *L = BBLoop; |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 2479 | while (L && contains(L)) { |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2480 | SmallVector<BasicBlock *, 4> LatchBBs; |
| 2481 | BBLoop->getLoopLatches(LatchBBs); |
| 2482 | for (auto *LatchBB : LatchBBs) |
| 2483 | if (BB != LatchBB && BBReg->contains(LatchBB)) |
| 2484 | return; |
| 2485 | L = L->getParentLoop(); |
| 2486 | } |
| 2487 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2488 | isl::set Domain = DomainMap[BB]; |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2489 | assert(Domain && "Cannot propagate a nullptr"); |
| 2490 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2491 | Loop *ExitBBLoop = getFirstNonBoxedLoopFor(ExitBB, LI, getBoxedLoops()); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2492 | |
| 2493 | // Since the dimensions of @p BB and @p ExitBB might be different we have to |
| 2494 | // adjust the domain before we can propagate it. |
Tobias Grosser | 67dc08b | 2018-06-18 13:01:52 +0000 | [diff] [blame] | 2495 | isl::set AdjustedDomain = |
| 2496 | adjustDomainDimensions(*this, Domain, BBLoop, ExitBBLoop); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2497 | isl::set &ExitDomain = DomainMap[ExitBB]; |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2498 | |
| 2499 | // If the exit domain is not yet created we set it otherwise we "add" the |
| 2500 | // current domain. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2501 | ExitDomain = ExitDomain ? AdjustedDomain.unite(ExitDomain) : AdjustedDomain; |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2502 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2503 | // Initialize the invalid domain. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2504 | InvalidDomainMap[ExitBB] = ExitDomain.empty(ExitDomain.get_space()); |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2505 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2506 | FinishedExitBlocks.insert(ExitBB); |
| 2507 | } |
| 2508 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2509 | bool Scop::buildDomainsWithBranchConstraints( |
| 2510 | Region *R, DominatorTree &DT, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2511 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2512 | // To create the domain for each block in R we iterate over all blocks and |
| 2513 | // subregions in R and propagate the conditions under which the current region |
| 2514 | // element is executed. To this end we iterate in reverse post order over R as |
| 2515 | // it ensures that we first visit all predecessors of a region node (either a |
| 2516 | // basic block or a subregion) before we visit the region node itself. |
| 2517 | // Initially, only the domain for the SCoP region entry block is set and from |
| 2518 | // there we propagate the current domain to all successors, however we add the |
| 2519 | // condition that the successor is actually executed next. |
| 2520 | // As we are only interested in non-loop carried constraints here we can |
| 2521 | // simply skip loop back edges. |
| 2522 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2523 | SmallPtrSet<BasicBlock *, 8> FinishedExitBlocks; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2524 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 2525 | for (auto *RN : RTraversal) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2526 | // Recurse for affine subregions but go on for basic blocks and non-affine |
| 2527 | // subregions. |
| 2528 | if (RN->isSubRegion()) { |
| 2529 | Region *SubRegion = RN->getNodeAs<Region>(); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2530 | if (!isNonAffineSubRegion(SubRegion)) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2531 | if (!buildDomainsWithBranchConstraints(SubRegion, DT, LI, |
| 2532 | InvalidDomainMap)) |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2533 | return false; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2534 | continue; |
| 2535 | } |
| 2536 | } |
| 2537 | |
Tobias Grosser | b76cd3c | 2015-11-11 08:42:20 +0000 | [diff] [blame] | 2538 | if (containsErrorBlock(RN, getRegion(), LI, DT)) |
Johannes Doerfert | f85ad04 | 2015-11-08 20:16:39 +0000 | [diff] [blame] | 2539 | HasErrorBlock = true; |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2540 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2541 | BasicBlock *BB = getRegionNodeBasicBlock(RN); |
Chandler Carruth | e303c87 | 2018-10-15 10:42:50 +0000 | [diff] [blame] | 2542 | Instruction *TI = BB->getTerminator(); |
Johannes Doerfert | 90db75e | 2015-09-10 17:51:27 +0000 | [diff] [blame] | 2543 | |
Tobias Grosser | b76cd3c | 2015-11-11 08:42:20 +0000 | [diff] [blame] | 2544 | if (isa<UnreachableInst>(TI)) |
| 2545 | continue; |
| 2546 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2547 | isl::set Domain = DomainMap.lookup(BB); |
Tobias Grosser | 4fb9e51 | 2016-02-27 06:59:30 +0000 | [diff] [blame] | 2548 | if (!Domain) |
Johannes Doerfert | 90db75e | 2015-09-10 17:51:27 +0000 | [diff] [blame] | 2549 | continue; |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2550 | MaxLoopDepth = std::max(MaxLoopDepth, isl_set_n_dim(Domain.get())); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2551 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2552 | auto *BBLoop = getRegionNodeLoop(RN, LI); |
| 2553 | // Propagate the domain from BB directly to blocks that have a superset |
| 2554 | // domain, at the moment only region exit nodes of regions that start in BB. |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2555 | propagateDomainConstraintsToRegionExit(BB, BBLoop, FinishedExitBlocks, LI, |
| 2556 | InvalidDomainMap); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2557 | |
| 2558 | // If all successors of BB have been set a domain through the propagation |
| 2559 | // above we do not need to build condition sets but can just skip this |
| 2560 | // block. However, it is important to note that this is a local property |
| 2561 | // with regards to the region @p R. To this end FinishedExitBlocks is a |
| 2562 | // local variable. |
| 2563 | auto IsFinishedRegionExit = [&FinishedExitBlocks](BasicBlock *SuccBB) { |
| 2564 | return FinishedExitBlocks.count(SuccBB); |
| 2565 | }; |
| 2566 | if (std::all_of(succ_begin(BB), succ_end(BB), IsFinishedRegionExit)) |
| 2567 | continue; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2568 | |
| 2569 | // Build the condition sets for the successor nodes of the current region |
| 2570 | // node. If it is a non-affine subregion we will always execute the single |
| 2571 | // exit node, hence the single entry node domain is the condition set. For |
| 2572 | // basic blocks we use the helper function buildConditionSets. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2573 | SmallVector<isl_set *, 8> ConditionSets; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2574 | if (RN->isSubRegion()) |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2575 | ConditionSets.push_back(Domain.copy()); |
| 2576 | else if (!buildConditionSets(*this, BB, TI, BBLoop, Domain.get(), |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2577 | InvalidDomainMap, ConditionSets)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2578 | return false; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2579 | |
| 2580 | // Now iterate over the successors and set their initial domain based on |
| 2581 | // their condition set. We skip back edges here and have to be careful when |
| 2582 | // we leave a loop not to keep constraints over a dimension that doesn't |
| 2583 | // exist anymore. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2584 | assert(RN->isSubRegion() || TI->getNumSuccessors() == ConditionSets.size()); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2585 | for (unsigned u = 0, e = ConditionSets.size(); u < e; u++) { |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2586 | isl::set CondSet = isl::manage(ConditionSets[u]); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2587 | BasicBlock *SuccBB = getRegionNodeSuccessor(RN, TI, u); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2588 | |
Johannes Doerfert | 535de03 | 2016-04-19 14:49:05 +0000 | [diff] [blame] | 2589 | // Skip blocks outside the region. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2590 | if (!contains(SuccBB)) |
Johannes Doerfert | 535de03 | 2016-04-19 14:49:05 +0000 | [diff] [blame] | 2591 | continue; |
Johannes Doerfert | 535de03 | 2016-04-19 14:49:05 +0000 | [diff] [blame] | 2592 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2593 | // If we propagate the domain of some block to "SuccBB" we do not have to |
| 2594 | // adjust the domain. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2595 | if (FinishedExitBlocks.count(SuccBB)) |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2596 | continue; |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2597 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2598 | // Skip back edges. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2599 | if (DT.dominates(SuccBB, BB)) |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2600 | continue; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2601 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2602 | Loop *SuccBBLoop = getFirstNonBoxedLoopFor(SuccBB, LI, getBoxedLoops()); |
| 2603 | |
Tobias Grosser | 9b29af9 | 2018-06-18 12:49:47 +0000 | [diff] [blame] | 2604 | CondSet = adjustDomainDimensions(*this, CondSet, BBLoop, SuccBBLoop); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2605 | |
| 2606 | // Set the domain for the successor or merge it with an existing domain in |
| 2607 | // case there are multiple paths (without loop back edges) to the |
| 2608 | // successor block. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2609 | isl::set &SuccDomain = DomainMap[SuccBB]; |
Tobias Grosser | 5a8c052 | 2016-03-22 22:05:32 +0000 | [diff] [blame] | 2610 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2611 | if (SuccDomain) { |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2612 | SuccDomain = SuccDomain.unite(CondSet).coalesce(); |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2613 | } else { |
| 2614 | // Initialize the invalid domain. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2615 | InvalidDomainMap[SuccBB] = CondSet.empty(CondSet.get_space()); |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2616 | SuccDomain = CondSet; |
| 2617 | } |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2618 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2619 | SuccDomain = SuccDomain.detect_equalities(); |
Tobias Grosser | 6d459c5 | 2017-05-23 04:26:28 +0000 | [diff] [blame] | 2620 | |
Michael Kruse | bc15012 | 2016-05-02 12:25:18 +0000 | [diff] [blame] | 2621 | // Check if the maximal number of domain disjunctions was reached. |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2622 | // In case this happens we will clean up and bail. |
Philip Pfaffe | 9375d57 | 2018-05-16 14:05:03 +0000 | [diff] [blame] | 2623 | if (SuccDomain.n_basic_set() < MaxDisjunctsInDomain) |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2624 | continue; |
| 2625 | |
| 2626 | invalidate(COMPLEXITY, DebugLoc()); |
| 2627 | while (++u < ConditionSets.size()) |
| 2628 | isl_set_free(ConditionSets[u]); |
| 2629 | return false; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2630 | } |
| 2631 | } |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2632 | |
| 2633 | return true; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2634 | } |
| 2635 | |
Tobias Grosser | 2f3041f | 2017-08-06 17:31:38 +0000 | [diff] [blame] | 2636 | isl::set Scop::getPredecessorDomainConstraints(BasicBlock *BB, isl::set Domain, |
| 2637 | DominatorTree &DT, |
| 2638 | LoopInfo &LI) { |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2639 | // If @p BB is the ScopEntry we are done |
| 2640 | if (R.getEntry() == BB) |
Tobias Grosser | 2f3041f | 2017-08-06 17:31:38 +0000 | [diff] [blame] | 2641 | return isl::set::universe(Domain.get_space()); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2642 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2643 | // The region info of this function. |
| 2644 | auto &RI = *R.getRegionInfo(); |
| 2645 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2646 | Loop *BBLoop = getFirstNonBoxedLoopFor(BB, LI, getBoxedLoops()); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2647 | |
| 2648 | // A domain to collect all predecessor domains, thus all conditions under |
| 2649 | // which the block is executed. To this end we start with the empty domain. |
Tobias Grosser | 2f3041f | 2017-08-06 17:31:38 +0000 | [diff] [blame] | 2650 | isl::set PredDom = isl::set::empty(Domain.get_space()); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2651 | |
| 2652 | // Set of regions of which the entry block domain has been propagated to BB. |
| 2653 | // all predecessors inside any of the regions can be skipped. |
| 2654 | SmallSet<Region *, 8> PropagatedRegions; |
| 2655 | |
| 2656 | for (auto *PredBB : predecessors(BB)) { |
| 2657 | // Skip backedges. |
| 2658 | if (DT.dominates(BB, PredBB)) |
| 2659 | continue; |
| 2660 | |
| 2661 | // If the predecessor is in a region we used for propagation we can skip it. |
| 2662 | auto PredBBInRegion = [PredBB](Region *PR) { return PR->contains(PredBB); }; |
| 2663 | if (std::any_of(PropagatedRegions.begin(), PropagatedRegions.end(), |
| 2664 | PredBBInRegion)) { |
| 2665 | continue; |
| 2666 | } |
| 2667 | |
| 2668 | // Check if there is a valid region we can use for propagation, thus look |
| 2669 | // for a region that contains the predecessor and has @p BB as exit block. |
| 2670 | auto *PredR = RI.getRegionFor(PredBB); |
| 2671 | while (PredR->getExit() != BB && !PredR->contains(BB)) |
| 2672 | PredR->getParent(); |
| 2673 | |
| 2674 | // If a valid region for propagation was found use the entry of that region |
| 2675 | // for propagation, otherwise the PredBB directly. |
| 2676 | if (PredR->getExit() == BB) { |
| 2677 | PredBB = PredR->getEntry(); |
| 2678 | PropagatedRegions.insert(PredR); |
| 2679 | } |
| 2680 | |
Tobias Grosser | 9b29af9 | 2018-06-18 12:49:47 +0000 | [diff] [blame] | 2681 | isl::set PredBBDom = getDomainConditions(PredBB); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2682 | Loop *PredBBLoop = getFirstNonBoxedLoopFor(PredBB, LI, getBoxedLoops()); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2683 | PredBBDom = adjustDomainDimensions(*this, PredBBDom, PredBBLoop, BBLoop); |
Tobias Grosser | 9b29af9 | 2018-06-18 12:49:47 +0000 | [diff] [blame] | 2684 | PredDom = PredDom.unite(PredBBDom); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2685 | } |
| 2686 | |
| 2687 | return PredDom; |
| 2688 | } |
| 2689 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2690 | bool Scop::propagateDomainConstraints( |
| 2691 | Region *R, DominatorTree &DT, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2692 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2693 | // Iterate over the region R and propagate the domain constrains from the |
| 2694 | // predecessors to the current node. In contrast to the |
| 2695 | // buildDomainsWithBranchConstraints function, this one will pull the domain |
| 2696 | // information from the predecessors instead of pushing it to the successors. |
| 2697 | // Additionally, we assume the domains to be already present in the domain |
| 2698 | // map here. However, we iterate again in reverse post order so we know all |
| 2699 | // predecessors have been visited before a block or non-affine subregion is |
| 2700 | // visited. |
| 2701 | |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2702 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 2703 | for (auto *RN : RTraversal) { |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2704 | // Recurse for affine subregions but go on for basic blocks and non-affine |
| 2705 | // subregions. |
| 2706 | if (RN->isSubRegion()) { |
| 2707 | Region *SubRegion = RN->getNodeAs<Region>(); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2708 | if (!isNonAffineSubRegion(SubRegion)) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2709 | if (!propagateDomainConstraints(SubRegion, DT, LI, InvalidDomainMap)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2710 | return false; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2711 | continue; |
| 2712 | } |
| 2713 | } |
| 2714 | |
| 2715 | BasicBlock *BB = getRegionNodeBasicBlock(RN); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2716 | isl::set &Domain = DomainMap[BB]; |
Johannes Doerfert | a49c557 | 2016-04-05 16:18:53 +0000 | [diff] [blame] | 2717 | assert(Domain); |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2718 | |
Tobias Grosser | 6deba4e | 2016-03-30 18:18:31 +0000 | [diff] [blame] | 2719 | // Under the union of all predecessor conditions we can reach this block. |
Tobias Grosser | 2f3041f | 2017-08-06 17:31:38 +0000 | [diff] [blame] | 2720 | isl::set PredDom = getPredecessorDomainConstraints(BB, Domain, DT, LI); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2721 | Domain = Domain.intersect(PredDom).coalesce(); |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 2722 | Domain = Domain.align_params(getParamSpace()); |
Tobias Grosser | 6deba4e | 2016-03-30 18:18:31 +0000 | [diff] [blame] | 2723 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2724 | Loop *BBLoop = getRegionNodeLoop(RN, LI); |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 2725 | if (BBLoop && BBLoop->getHeader() == BB && contains(BBLoop)) |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2726 | if (!addLoopBoundsToHeaderDomain(BBLoop, LI, InvalidDomainMap)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2727 | return false; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2728 | } |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2729 | |
| 2730 | return true; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2731 | } |
| 2732 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2733 | /// Create a map to map from a given iteration to a subsequent iteration. |
| 2734 | /// |
| 2735 | /// This map maps from SetSpace -> SetSpace where the dimensions @p Dim |
| 2736 | /// is incremented by one and all other dimensions are equal, e.g., |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2737 | /// [i0, i1, i2, i3] -> [i0, i1, i2 + 1, i3] |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2738 | /// |
| 2739 | /// if @p Dim is 2 and @p SetSpace has 4 dimensions. |
Tobias Grosser | 10da5a0 | 2018-05-23 18:41:40 +0000 | [diff] [blame] | 2740 | static isl::map createNextIterationMap(isl::space SetSpace, unsigned Dim) { |
| 2741 | isl::space MapSpace = SetSpace.map_from_set(); |
| 2742 | isl::map NextIterationMap = isl::map::universe(MapSpace); |
| 2743 | for (unsigned u = 0; u < NextIterationMap.dim(isl::dim::in); u++) |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2744 | if (u != Dim) |
| 2745 | NextIterationMap = |
Tobias Grosser | 10da5a0 | 2018-05-23 18:41:40 +0000 | [diff] [blame] | 2746 | NextIterationMap.equate(isl::dim::in, u, isl::dim::out, u); |
| 2747 | isl::constraint C = |
| 2748 | isl::constraint::alloc_equality(isl::local_space(MapSpace)); |
| 2749 | C = C.set_constant_si(1); |
| 2750 | C = C.set_coefficient_si(isl::dim::in, Dim, 1); |
| 2751 | C = C.set_coefficient_si(isl::dim::out, Dim, -1); |
| 2752 | NextIterationMap = NextIterationMap.add_constraint(C); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2753 | return NextIterationMap; |
| 2754 | } |
| 2755 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2756 | bool Scop::addLoopBoundsToHeaderDomain( |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2757 | Loop *L, LoopInfo &LI, DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2758 | int LoopDepth = getRelativeLoopDepth(L); |
| 2759 | assert(LoopDepth >= 0 && "Loop in region should have at least depth one"); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2760 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2761 | BasicBlock *HeaderBB = L->getHeader(); |
| 2762 | assert(DomainMap.count(HeaderBB)); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2763 | isl::set &HeaderBBDom = DomainMap[HeaderBB]; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2764 | |
Tobias Grosser | 10da5a0 | 2018-05-23 18:41:40 +0000 | [diff] [blame] | 2765 | isl::map NextIterationMap = |
| 2766 | createNextIterationMap(HeaderBBDom.get_space(), LoopDepth); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2767 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2768 | isl::set UnionBackedgeCondition = HeaderBBDom.empty(HeaderBBDom.get_space()); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2769 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 2770 | SmallVector<BasicBlock *, 4> LatchBlocks; |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2771 | L->getLoopLatches(LatchBlocks); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2772 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2773 | for (BasicBlock *LatchBB : LatchBlocks) { |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2774 | // If the latch is only reachable via error statements we skip it. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2775 | isl::set LatchBBDom = DomainMap.lookup(LatchBB); |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2776 | if (!LatchBBDom) |
| 2777 | continue; |
| 2778 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2779 | isl::set BackedgeCondition = nullptr; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2780 | |
Chandler Carruth | e303c87 | 2018-10-15 10:42:50 +0000 | [diff] [blame] | 2781 | Instruction *TI = LatchBB->getTerminator(); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2782 | BranchInst *BI = dyn_cast<BranchInst>(TI); |
Tobias Grosser | bbaeda3 | 2016-11-10 05:20:29 +0000 | [diff] [blame] | 2783 | assert(BI && "Only branch instructions allowed in loop latches"); |
| 2784 | |
| 2785 | if (BI->isUnconditional()) |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2786 | BackedgeCondition = LatchBBDom; |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2787 | else { |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2788 | SmallVector<isl_set *, 8> ConditionSets; |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2789 | int idx = BI->getSuccessor(0) != HeaderBB; |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2790 | if (!buildConditionSets(*this, LatchBB, TI, L, LatchBBDom.get(), |
| 2791 | InvalidDomainMap, ConditionSets)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2792 | return false; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2793 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2794 | // Free the non back edge condition set as we do not need it. |
| 2795 | isl_set_free(ConditionSets[1 - idx]); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2796 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2797 | BackedgeCondition = isl::manage(ConditionSets[idx]); |
Johannes Doerfert | 06c57b5 | 2015-09-20 15:00:20 +0000 | [diff] [blame] | 2798 | } |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2799 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2800 | int LatchLoopDepth = getRelativeLoopDepth(LI.getLoopFor(LatchBB)); |
| 2801 | assert(LatchLoopDepth >= LoopDepth); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2802 | BackedgeCondition = BackedgeCondition.project_out( |
| 2803 | isl::dim::set, LoopDepth + 1, LatchLoopDepth - LoopDepth); |
| 2804 | UnionBackedgeCondition = UnionBackedgeCondition.unite(BackedgeCondition); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2805 | } |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2806 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2807 | isl::map ForwardMap = ForwardMap.lex_le(HeaderBBDom.get_space()); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2808 | for (int i = 0; i < LoopDepth; i++) |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2809 | ForwardMap = ForwardMap.equate(isl::dim::in, i, isl::dim::out, i); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2810 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2811 | isl::set UnionBackedgeConditionComplement = |
| 2812 | UnionBackedgeCondition.complement(); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2813 | UnionBackedgeConditionComplement = |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2814 | UnionBackedgeConditionComplement.lower_bound_si(isl::dim::set, LoopDepth, |
| 2815 | 0); |
| 2816 | UnionBackedgeConditionComplement = |
| 2817 | UnionBackedgeConditionComplement.apply(ForwardMap); |
| 2818 | HeaderBBDom = HeaderBBDom.subtract(UnionBackedgeConditionComplement); |
| 2819 | HeaderBBDom = HeaderBBDom.apply(NextIterationMap); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2820 | |
Tobias Grosser | 78a8494 | 2018-06-01 19:12:00 +0000 | [diff] [blame] | 2821 | auto Parts = partitionSetParts(HeaderBBDom, LoopDepth); |
| 2822 | HeaderBBDom = Parts.second; |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2823 | |
Johannes Doerfert | 6a72a2a | 2015-09-20 16:59:23 +0000 | [diff] [blame] | 2824 | // Check if there is a <nsw> tagged AddRec for this loop and if so do not add |
| 2825 | // the bounded assumptions to the context as they are already implied by the |
| 2826 | // <nsw> tag. |
Tobias Grosser | 78a8494 | 2018-06-01 19:12:00 +0000 | [diff] [blame] | 2827 | if (Affinator.hasNSWAddRecForLoop(L)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2828 | return true; |
Johannes Doerfert | 6a72a2a | 2015-09-20 16:59:23 +0000 | [diff] [blame] | 2829 | |
Tobias Grosser | 78a8494 | 2018-06-01 19:12:00 +0000 | [diff] [blame] | 2830 | isl::set UnboundedCtx = Parts.first.params(); |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 2831 | recordAssumption(INFINITELOOP, UnboundedCtx, |
| 2832 | HeaderBB->getTerminator()->getDebugLoc(), AS_RESTRICTION); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2833 | return true; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2834 | } |
| 2835 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2836 | /// Get the smallest loop that contains @p S but is not in @p S. |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 2837 | static Loop *getLoopSurroundingScop(Scop &S, LoopInfo &LI) { |
Johannes Doerfert | dec27df | 2015-11-21 16:56:13 +0000 | [diff] [blame] | 2838 | // Start with the smallest loop containing the entry and expand that |
| 2839 | // loop until it contains all blocks in the region. If there is a loop |
| 2840 | // containing all blocks in the region check if it is itself contained |
| 2841 | // and if so take the parent loop as it will be the smallest containing |
| 2842 | // the region but not contained by it. |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 2843 | Loop *L = LI.getLoopFor(S.getEntry()); |
Johannes Doerfert | dec27df | 2015-11-21 16:56:13 +0000 | [diff] [blame] | 2844 | while (L) { |
| 2845 | bool AllContained = true; |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 2846 | for (auto *BB : S.blocks()) |
Johannes Doerfert | dec27df | 2015-11-21 16:56:13 +0000 | [diff] [blame] | 2847 | AllContained &= L->contains(BB); |
| 2848 | if (AllContained) |
| 2849 | break; |
| 2850 | L = L->getParentLoop(); |
| 2851 | } |
| 2852 | |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 2853 | return L ? (S.contains(L) ? L->getParentLoop() : L) : nullptr; |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2854 | } |
| 2855 | |
Singapuram Sanjay Srivallabh | 1abd9ff | 2017-07-12 16:46:19 +0000 | [diff] [blame] | 2856 | int Scop::NextScopID = 0; |
| 2857 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 2858 | std::string Scop::CurrentFunc; |
Singapuram Sanjay Srivallabh | 1abd9ff | 2017-07-12 16:46:19 +0000 | [diff] [blame] | 2859 | |
| 2860 | int Scop::getNextID(std::string ParentFunc) { |
| 2861 | if (ParentFunc != CurrentFunc) { |
| 2862 | CurrentFunc = ParentFunc; |
| 2863 | NextScopID = 0; |
| 2864 | } |
| 2865 | return NextScopID++; |
| 2866 | } |
| 2867 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2868 | Scop::Scop(Region &R, ScalarEvolution &ScalarEvolution, LoopInfo &LI, |
Tobias Grosser | ee45759 | 2017-09-24 09:25:30 +0000 | [diff] [blame] | 2869 | DominatorTree &DT, ScopDetection::DetectionContext &DC, |
| 2870 | OptimizationRemarkEmitter &ORE) |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2871 | : IslCtx(isl_ctx_alloc(), isl_ctx_free), SE(&ScalarEvolution), DT(&DT), |
Philip Pfaffe | d477bb9 | 2018-05-15 14:53:25 +0000 | [diff] [blame] | 2872 | R(R), name(None), HasSingleExitEdge(R.getExitingBlock()), DC(DC), |
| 2873 | ORE(ORE), Affinator(this, LI), |
Singapuram Sanjay Srivallabh | 1abd9ff | 2017-07-12 16:46:19 +0000 | [diff] [blame] | 2874 | ID(getNextID((*R.getEntry()->getParent()).getName().str())) { |
Tobias Grosser | 2937b59 | 2016-04-29 11:43:20 +0000 | [diff] [blame] | 2875 | if (IslOnErrorAbort) |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2876 | isl_options_set_on_error(getIslCtx().get(), ISL_ON_ERROR_ABORT); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 2877 | buildContext(); |
| 2878 | } |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 2879 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2880 | Scop::~Scop() = default; |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 2881 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 2882 | void Scop::foldSizeConstantsToRight() { |
Tobias Grosser | 4aab4ec | 2018-07-05 15:23:28 +0000 | [diff] [blame] | 2883 | isl::union_set Accessed = getAccesses().range(); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 2884 | |
| 2885 | for (auto Array : arrays()) { |
| 2886 | if (Array->getNumberOfDimensions() <= 1) |
| 2887 | continue; |
| 2888 | |
Tobias Grosser | 4aab4ec | 2018-07-05 15:23:28 +0000 | [diff] [blame] | 2889 | isl::space Space = Array->getSpace(); |
| 2890 | Space = Space.align_params(Accessed.get_space()); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 2891 | |
Tobias Grosser | 4aab4ec | 2018-07-05 15:23:28 +0000 | [diff] [blame] | 2892 | if (!Accessed.contains(Space)) |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 2893 | continue; |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 2894 | |
Tobias Grosser | 4aab4ec | 2018-07-05 15:23:28 +0000 | [diff] [blame] | 2895 | isl::set Elements = Accessed.extract_set(Space); |
| 2896 | isl::map Transform = isl::map::universe(Array->getSpace().map_from_set()); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 2897 | |
| 2898 | std::vector<int> Int; |
Tobias Grosser | 4aab4ec | 2018-07-05 15:23:28 +0000 | [diff] [blame] | 2899 | int Dims = Elements.dim(isl::dim::set); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 2900 | for (int i = 0; i < Dims; i++) { |
Tobias Grosser | 4aab4ec | 2018-07-05 15:23:28 +0000 | [diff] [blame] | 2901 | isl::set DimOnly = isl::set(Elements).project_out(isl::dim::set, 0, i); |
| 2902 | DimOnly = DimOnly.project_out(isl::dim::set, 1, Dims - i - 1); |
| 2903 | DimOnly = DimOnly.lower_bound_si(isl::dim::set, 0, 0); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 2904 | |
Tobias Grosser | 4aab4ec | 2018-07-05 15:23:28 +0000 | [diff] [blame] | 2905 | isl::basic_set DimHull = DimOnly.affine_hull(); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 2906 | |
| 2907 | if (i == Dims - 1) { |
| 2908 | Int.push_back(1); |
Tobias Grosser | 4aab4ec | 2018-07-05 15:23:28 +0000 | [diff] [blame] | 2909 | Transform = Transform.equate(isl::dim::in, i, isl::dim::out, i); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 2910 | continue; |
| 2911 | } |
| 2912 | |
Tobias Grosser | 4aab4ec | 2018-07-05 15:23:28 +0000 | [diff] [blame] | 2913 | if (DimHull.dim(isl::dim::div) == 1) { |
| 2914 | isl::aff Diff = DimHull.get_div(0); |
| 2915 | isl::val Val = Diff.get_denominator_val(); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 2916 | |
| 2917 | int ValInt = 1; |
Tobias Grosser | 4aab4ec | 2018-07-05 15:23:28 +0000 | [diff] [blame] | 2918 | if (Val.is_int()) { |
Eli Friedman | a75d53c | 2018-01-17 21:59:02 +0000 | [diff] [blame] | 2919 | auto ValAPInt = APIntFromVal(Val); |
| 2920 | if (ValAPInt.isSignedIntN(32)) |
| 2921 | ValInt = ValAPInt.getSExtValue(); |
| 2922 | } else { |
Eli Friedman | a75d53c | 2018-01-17 21:59:02 +0000 | [diff] [blame] | 2923 | } |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 2924 | |
| 2925 | Int.push_back(ValInt); |
Tobias Grosser | 4aab4ec | 2018-07-05 15:23:28 +0000 | [diff] [blame] | 2926 | isl::constraint C = isl::constraint::alloc_equality( |
| 2927 | isl::local_space(Transform.get_space())); |
| 2928 | C = C.set_coefficient_si(isl::dim::out, i, ValInt); |
| 2929 | C = C.set_coefficient_si(isl::dim::in, i, -1); |
| 2930 | Transform = Transform.add_constraint(C); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 2931 | continue; |
| 2932 | } |
| 2933 | |
Tobias Grosser | 4aab4ec | 2018-07-05 15:23:28 +0000 | [diff] [blame] | 2934 | isl::basic_set ZeroSet = isl::basic_set(DimHull); |
| 2935 | ZeroSet = ZeroSet.fix_si(isl::dim::set, 0, 0); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 2936 | |
| 2937 | int ValInt = 1; |
Tobias Grosser | 4aab4ec | 2018-07-05 15:23:28 +0000 | [diff] [blame] | 2938 | if (ZeroSet.is_equal(DimHull)) { |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 2939 | ValInt = 0; |
| 2940 | } |
| 2941 | |
| 2942 | Int.push_back(ValInt); |
Tobias Grosser | 4aab4ec | 2018-07-05 15:23:28 +0000 | [diff] [blame] | 2943 | Transform = Transform.equate(isl::dim::in, i, isl::dim::out, i); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 2944 | } |
| 2945 | |
Tobias Grosser | 4aab4ec | 2018-07-05 15:23:28 +0000 | [diff] [blame] | 2946 | isl::set MappedElements = isl::map(Transform).domain(); |
| 2947 | if (!Elements.is_subset(MappedElements)) |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 2948 | continue; |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 2949 | |
| 2950 | bool CanFold = true; |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 2951 | if (Int[0] <= 1) |
| 2952 | CanFold = false; |
| 2953 | |
| 2954 | unsigned NumDims = Array->getNumberOfDimensions(); |
| 2955 | for (unsigned i = 1; i < NumDims - 1; i++) |
| 2956 | if (Int[0] != Int[i] && Int[i]) |
| 2957 | CanFold = false; |
| 2958 | |
Tobias Grosser | 4aab4ec | 2018-07-05 15:23:28 +0000 | [diff] [blame] | 2959 | if (!CanFold) |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 2960 | continue; |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 2961 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 2962 | for (auto &Access : AccessFunctions) |
| 2963 | if (Access->getScopArrayInfo() == Array) |
Tobias Grosser | 4aab4ec | 2018-07-05 15:23:28 +0000 | [diff] [blame] | 2964 | Access->setAccessRelation( |
| 2965 | Access->getAccessRelation().apply_range(Transform)); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 2966 | |
| 2967 | std::vector<const SCEV *> Sizes; |
| 2968 | for (unsigned i = 0; i < NumDims; i++) { |
| 2969 | auto Size = Array->getDimensionSize(i); |
| 2970 | |
| 2971 | if (i == NumDims - 1) |
| 2972 | Size = SE->getMulExpr(Size, SE->getConstant(Size->getType(), Int[0])); |
| 2973 | Sizes.push_back(Size); |
| 2974 | } |
| 2975 | |
| 2976 | Array->updateSizes(Sizes, false /* CheckConsistency */); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 2977 | } |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 2978 | } |
| 2979 | |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2980 | void Scop::markFortranArrays() { |
| 2981 | for (ScopStmt &Stmt : Stmts) { |
| 2982 | for (MemoryAccess *MemAcc : Stmt) { |
| 2983 | Value *FAD = MemAcc->getFortranArrayDescriptor(); |
| 2984 | if (!FAD) |
| 2985 | continue; |
| 2986 | |
| 2987 | // TODO: const_cast-ing to edit |
| 2988 | ScopArrayInfo *SAI = |
| 2989 | const_cast<ScopArrayInfo *>(MemAcc->getLatestScopArrayInfo()); |
| 2990 | assert(SAI && "memory access into a Fortran array does not " |
| 2991 | "have an associated ScopArrayInfo"); |
| 2992 | SAI->applyAndSetFAD(FAD); |
| 2993 | } |
| 2994 | } |
| 2995 | } |
| 2996 | |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 2997 | void Scop::finalizeAccesses() { |
| 2998 | updateAccessDimensionality(); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 2999 | foldSizeConstantsToRight(); |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 3000 | foldAccessRelations(); |
| 3001 | assumeNoOutOfBounds(); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 3002 | markFortranArrays(); |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 3003 | } |
| 3004 | |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 3005 | void Scop::updateAccessDimensionality() { |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3006 | // Check all array accesses for each base pointer and find a (virtual) element |
| 3007 | // size for the base pointer that divides all access functions. |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3008 | for (ScopStmt &Stmt : *this) |
| 3009 | for (MemoryAccess *Access : Stmt) { |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3010 | if (!Access->isArrayKind()) |
| 3011 | continue; |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3012 | ScopArrayInfo *Array = |
Tobias Grosser | e24b7b9 | 2017-02-09 23:24:57 +0000 | [diff] [blame] | 3013 | const_cast<ScopArrayInfo *>(Access->getScopArrayInfo()); |
| 3014 | |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3015 | if (Array->getNumberOfDimensions() != 1) |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3016 | continue; |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3017 | unsigned DivisibleSize = Array->getElemSizeInBytes(); |
| 3018 | const SCEV *Subscript = Access->getSubscript(0); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3019 | while (!isDivisible(Subscript, DivisibleSize, *SE)) |
| 3020 | DivisibleSize /= 2; |
| 3021 | auto *Ty = IntegerType::get(SE->getContext(), DivisibleSize * 8); |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3022 | Array->updateElementType(Ty); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3023 | } |
| 3024 | |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 3025 | for (auto &Stmt : *this) |
| 3026 | for (auto &Access : Stmt) |
| 3027 | Access->updateDimensionality(); |
| 3028 | } |
| 3029 | |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 3030 | void Scop::foldAccessRelations() { |
| 3031 | for (auto &Stmt : *this) |
| 3032 | for (auto &Access : Stmt) |
| 3033 | Access->foldAccessRelation(); |
| 3034 | } |
| 3035 | |
| 3036 | void Scop::assumeNoOutOfBounds() { |
| 3037 | for (auto &Stmt : *this) |
| 3038 | for (auto &Access : Stmt) |
| 3039 | Access->assumeNoOutOfBound(); |
| 3040 | } |
| 3041 | |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3042 | void Scop::removeFromStmtMap(ScopStmt &Stmt) { |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 3043 | for (Instruction *Inst : Stmt.getInstructions()) |
| 3044 | InstStmtMap.erase(Inst); |
| 3045 | |
| 3046 | if (Stmt.isRegionStmt()) { |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 3047 | for (BasicBlock *BB : Stmt.getRegion()->blocks()) { |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3048 | StmtMap.erase(BB); |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 3049 | // Skip entry basic block, as its instructions are already deleted as |
| 3050 | // part of the statement's instruction list. |
| 3051 | if (BB == Stmt.getEntryBlock()) |
| 3052 | continue; |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 3053 | for (Instruction &Inst : *BB) |
| 3054 | InstStmtMap.erase(&Inst); |
| 3055 | } |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 3056 | } else { |
Michael Kruse | 0c6c555 | 2017-09-01 11:36:52 +0000 | [diff] [blame] | 3057 | auto StmtMapIt = StmtMap.find(Stmt.getBasicBlock()); |
| 3058 | if (StmtMapIt != StmtMap.end()) |
| 3059 | StmtMapIt->second.erase(std::remove(StmtMapIt->second.begin(), |
| 3060 | StmtMapIt->second.end(), &Stmt), |
| 3061 | StmtMapIt->second.end()); |
| 3062 | for (Instruction *Inst : Stmt.getInstructions()) |
| 3063 | InstStmtMap.erase(Inst); |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 3064 | } |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3065 | } |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 3066 | |
Michael Kruse | 192e7f7 | 2018-04-09 23:13:05 +0000 | [diff] [blame] | 3067 | void Scop::removeStmts(std::function<bool(ScopStmt &)> ShouldDelete, |
| 3068 | bool AfterHoisting) { |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3069 | for (auto StmtIt = Stmts.begin(), StmtEnd = Stmts.end(); StmtIt != StmtEnd;) { |
| 3070 | if (!ShouldDelete(*StmtIt)) { |
| 3071 | StmtIt++; |
| 3072 | continue; |
| 3073 | } |
| 3074 | |
Michael Kruse | 192e7f7 | 2018-04-09 23:13:05 +0000 | [diff] [blame] | 3075 | // Start with removing all of the statement's accesses including erasing it |
| 3076 | // from all maps that are pointing to them. |
Michael Kruse | db6f71e | 2018-04-10 01:20:41 +0000 | [diff] [blame] | 3077 | // Make a temporary copy because removing MAs invalidates the iterator. |
| 3078 | SmallVector<MemoryAccess *, 16> MAList(StmtIt->begin(), StmtIt->end()); |
| 3079 | for (MemoryAccess *MA : MAList) |
Michael Kruse | 192e7f7 | 2018-04-09 23:13:05 +0000 | [diff] [blame] | 3080 | StmtIt->removeSingleMemoryAccess(MA, AfterHoisting); |
| 3081 | |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3082 | removeFromStmtMap(*StmtIt); |
| 3083 | StmtIt = Stmts.erase(StmtIt); |
| 3084 | } |
| 3085 | } |
| 3086 | |
| 3087 | void Scop::removeStmtNotInDomainMap() { |
| 3088 | auto ShouldDelete = [this](ScopStmt &Stmt) -> bool { |
Michael Kruse | 842bdd0 | 2018-08-01 22:28:32 +0000 | [diff] [blame] | 3089 | isl::set Domain = DomainMap.lookup(Stmt.getEntryBlock()); |
| 3090 | if (!Domain) |
| 3091 | return true; |
| 3092 | return Domain.is_empty(); |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3093 | }; |
Michael Kruse | 192e7f7 | 2018-04-09 23:13:05 +0000 | [diff] [blame] | 3094 | removeStmts(ShouldDelete, false); |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3095 | } |
| 3096 | |
| 3097 | void Scop::simplifySCoP(bool AfterHoisting) { |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3098 | auto ShouldDelete = [AfterHoisting](ScopStmt &Stmt) -> bool { |
Michael Kruse | 5369ea5 | 2018-04-20 18:55:44 +0000 | [diff] [blame] | 3099 | // Never delete statements that contain calls to debug functions. |
| 3100 | if (hasDebugCall(&Stmt)) |
| 3101 | return false; |
| 3102 | |
Johannes Doerfert | 2640454 | 2016-05-10 12:19:47 +0000 | [diff] [blame] | 3103 | bool RemoveStmt = Stmt.isEmpty(); |
Johannes Doerfert | f17a78e | 2015-10-04 15:00:05 +0000 | [diff] [blame] | 3104 | |
Tobias Grosser | 3012a0b | 2017-07-16 22:44:17 +0000 | [diff] [blame] | 3105 | // Remove read only statements only after invariant load hoisting. |
Johannes Doerfert | 2640454 | 2016-05-10 12:19:47 +0000 | [diff] [blame] | 3106 | if (!RemoveStmt && AfterHoisting) { |
Johannes Doerfert | eca9e89 | 2015-11-03 16:54:49 +0000 | [diff] [blame] | 3107 | bool OnlyRead = true; |
| 3108 | for (MemoryAccess *MA : Stmt) { |
| 3109 | if (MA->isRead()) |
| 3110 | continue; |
| 3111 | |
| 3112 | OnlyRead = false; |
| 3113 | break; |
| 3114 | } |
| 3115 | |
| 3116 | RemoveStmt = OnlyRead; |
| 3117 | } |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3118 | return RemoveStmt; |
| 3119 | }; |
Johannes Doerfert | eca9e89 | 2015-11-03 16:54:49 +0000 | [diff] [blame] | 3120 | |
Michael Kruse | 192e7f7 | 2018-04-09 23:13:05 +0000 | [diff] [blame] | 3121 | removeStmts(ShouldDelete, AfterHoisting); |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 3122 | } |
| 3123 | |
Johannes Doerfert | 8ab2803 | 2016-04-27 12:49:11 +0000 | [diff] [blame] | 3124 | InvariantEquivClassTy *Scop::lookupInvariantEquivClass(Value *Val) { |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3125 | LoadInst *LInst = dyn_cast<LoadInst>(Val); |
| 3126 | if (!LInst) |
| 3127 | return nullptr; |
| 3128 | |
| 3129 | if (Value *Rep = InvEquivClassVMap.lookup(LInst)) |
| 3130 | LInst = cast<LoadInst>(Rep); |
| 3131 | |
Johannes Doerfert | 96e5471 | 2016-02-07 17:30:13 +0000 | [diff] [blame] | 3132 | Type *Ty = LInst->getType(); |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3133 | const SCEV *PointerSCEV = SE->getSCEV(LInst->getPointerOperand()); |
Johannes Doerfert | 549768c | 2016-03-24 13:22:16 +0000 | [diff] [blame] | 3134 | for (auto &IAClass : InvariantEquivClasses) { |
Tobias Grosser | faef9a7 | 2016-07-11 12:27:04 +0000 | [diff] [blame] | 3135 | if (PointerSCEV != IAClass.IdentifyingPointer || Ty != IAClass.AccessType) |
Johannes Doerfert | 549768c | 2016-03-24 13:22:16 +0000 | [diff] [blame] | 3136 | continue; |
| 3137 | |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 3138 | auto &MAs = IAClass.InvariantAccesses; |
Johannes Doerfert | 549768c | 2016-03-24 13:22:16 +0000 | [diff] [blame] | 3139 | for (auto *MA : MAs) |
| 3140 | if (MA->getAccessInstruction() == Val) |
| 3141 | return &IAClass; |
| 3142 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3143 | |
| 3144 | return nullptr; |
| 3145 | } |
| 3146 | |
Michael Kruse | b738ffa | 2017-06-28 13:02:43 +0000 | [diff] [blame] | 3147 | ScopArrayInfo *Scop::getOrCreateScopArrayInfo(Value *BasePtr, Type *ElementType, |
| 3148 | ArrayRef<const SCEV *> Sizes, |
| 3149 | MemoryKind Kind, |
| 3150 | const char *BaseName) { |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 3151 | assert((BasePtr || BaseName) && |
| 3152 | "BasePtr and BaseName can not be nullptr at the same time."); |
| 3153 | assert(!(BasePtr && BaseName) && "BaseName is redundant."); |
| 3154 | auto &SAI = BasePtr ? ScopArrayInfoMap[std::make_pair(BasePtr, Kind)] |
| 3155 | : ScopArrayNameMap[BaseName]; |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 3156 | if (!SAI) { |
Johannes Doerfert | 3f52e35 | 2016-05-23 12:38:05 +0000 | [diff] [blame] | 3157 | auto &DL = getFunction().getParent()->getDataLayout(); |
Tobias Grosser | cc77950 | 2016-02-02 13:22:54 +0000 | [diff] [blame] | 3158 | SAI.reset(new ScopArrayInfo(BasePtr, ElementType, getIslCtx(), Sizes, Kind, |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 3159 | DL, this, BaseName)); |
| 3160 | ScopArrayInfoSet.insert(SAI.get()); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 3161 | } else { |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 3162 | SAI->updateElementType(ElementType); |
Tobias Grosser | 8286b83 | 2015-11-02 11:29:32 +0000 | [diff] [blame] | 3163 | // In case of mismatching array sizes, we bail out by setting the run-time |
| 3164 | // context to false. |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 3165 | if (!SAI->updateSizes(Sizes)) |
Tobias Grosser | 8d4f626 | 2015-12-12 09:52:26 +0000 | [diff] [blame] | 3166 | invalidate(DELINEARIZATION, DebugLoc()); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 3167 | } |
Tobias Grosser | ab67144 | 2015-05-23 05:58:27 +0000 | [diff] [blame] | 3168 | return SAI.get(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 3169 | } |
| 3170 | |
Michael Kruse | b738ffa | 2017-06-28 13:02:43 +0000 | [diff] [blame] | 3171 | ScopArrayInfo *Scop::createScopArrayInfo(Type *ElementType, |
| 3172 | const std::string &BaseName, |
| 3173 | const std::vector<unsigned> &Sizes) { |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 3174 | auto *DimSizeType = Type::getInt64Ty(getSE()->getContext()); |
| 3175 | std::vector<const SCEV *> SCEVSizes; |
| 3176 | |
| 3177 | for (auto size : Sizes) |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 3178 | if (size) |
| 3179 | SCEVSizes.push_back(getSE()->getConstant(DimSizeType, size, false)); |
| 3180 | else |
| 3181 | SCEVSizes.push_back(nullptr); |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 3182 | |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 3183 | auto *SAI = getOrCreateScopArrayInfo(nullptr, ElementType, SCEVSizes, |
| 3184 | MemoryKind::Array, BaseName.c_str()); |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 3185 | return SAI; |
| 3186 | } |
| 3187 | |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 3188 | const ScopArrayInfo *Scop::getScopArrayInfoOrNull(Value *BasePtr, |
| 3189 | MemoryKind Kind) { |
Tobias Grosser | 6abc75a | 2015-11-10 17:31:31 +0000 | [diff] [blame] | 3190 | auto *SAI = ScopArrayInfoMap[std::make_pair(BasePtr, Kind)].get(); |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 3191 | return SAI; |
| 3192 | } |
| 3193 | |
| 3194 | const ScopArrayInfo *Scop::getScopArrayInfo(Value *BasePtr, MemoryKind Kind) { |
| 3195 | auto *SAI = getScopArrayInfoOrNull(BasePtr, Kind); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 3196 | assert(SAI && "No ScopArrayInfo available for this base pointer"); |
| 3197 | return SAI; |
| 3198 | } |
| 3199 | |
Tobias Grosser | 8ea1fc1 | 2017-08-06 19:52:38 +0000 | [diff] [blame] | 3200 | std::string Scop::getContextStr() const { return getContext().to_str(); } |
Johannes Doerfert | b92e218 | 2016-02-21 16:37:58 +0000 | [diff] [blame] | 3201 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 3202 | std::string Scop::getAssumedContextStr() const { |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 3203 | assert(AssumedContext && "Assumed context not yet built"); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3204 | return AssumedContext.to_str(); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 3205 | } |
Johannes Doerfert | b92e218 | 2016-02-21 16:37:58 +0000 | [diff] [blame] | 3206 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 3207 | std::string Scop::getInvalidContextStr() const { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3208 | return InvalidContext.to_str(); |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 3209 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3210 | |
| 3211 | std::string Scop::getNameStr() const { |
| 3212 | std::string ExitName, EntryName; |
Siddharth Bhat | 07bee29 | 2017-06-02 08:01:22 +0000 | [diff] [blame] | 3213 | std::tie(EntryName, ExitName) = getEntryExitStr(); |
| 3214 | return EntryName + "---" + ExitName; |
| 3215 | } |
| 3216 | |
| 3217 | std::pair<std::string, std::string> Scop::getEntryExitStr() const { |
| 3218 | std::string ExitName, EntryName; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3219 | raw_string_ostream ExitStr(ExitName); |
| 3220 | raw_string_ostream EntryStr(EntryName); |
| 3221 | |
Tobias Grosser | f240b48 | 2014-01-09 10:42:15 +0000 | [diff] [blame] | 3222 | R.getEntry()->printAsOperand(EntryStr, false); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3223 | EntryStr.str(); |
| 3224 | |
| 3225 | if (R.getExit()) { |
Tobias Grosser | f240b48 | 2014-01-09 10:42:15 +0000 | [diff] [blame] | 3226 | R.getExit()->printAsOperand(ExitStr, false); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3227 | ExitStr.str(); |
| 3228 | } else |
| 3229 | ExitName = "FunctionExit"; |
| 3230 | |
Siddharth Bhat | 07bee29 | 2017-06-02 08:01:22 +0000 | [diff] [blame] | 3231 | return std::make_pair(EntryName, ExitName); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3232 | } |
| 3233 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3234 | isl::set Scop::getContext() const { return Context; } |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 3235 | isl::space Scop::getParamSpace() const { return getContext().get_space(); } |
Tobias Grosser | 3748705 | 2011-10-06 00:03:42 +0000 | [diff] [blame] | 3236 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 3237 | isl::space Scop::getFullParamSpace() const { |
| 3238 | std::vector<isl::id> FortranIDs; |
| 3239 | FortranIDs = getFortranArrayIds(arrays()); |
| 3240 | |
| 3241 | isl::space Space = isl::space::params_alloc( |
| 3242 | getIslCtx(), ParameterIds.size() + FortranIDs.size()); |
| 3243 | |
| 3244 | unsigned PDim = 0; |
| 3245 | for (const SCEV *Parameter : Parameters) { |
Tobias Grosser | 9a63570 | 2017-08-06 19:31:27 +0000 | [diff] [blame] | 3246 | isl::id Id = getIdForParam(Parameter); |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 3247 | Space = Space.set_dim_id(isl::dim::param, PDim++, Id); |
| 3248 | } |
| 3249 | |
| 3250 | for (isl::id Id : FortranIDs) |
| 3251 | Space = Space.set_dim_id(isl::dim::param, PDim++, Id); |
| 3252 | |
| 3253 | return Space; |
| 3254 | } |
| 3255 | |
Tobias Grosser | e127033 | 2017-08-06 21:42:09 +0000 | [diff] [blame] | 3256 | isl::set Scop::getAssumedContext() const { |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 3257 | assert(AssumedContext && "Assumed context not yet built"); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3258 | return AssumedContext; |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 3259 | } |
| 3260 | |
Michael Kruse | f3091bf | 2017-03-17 13:09:52 +0000 | [diff] [blame] | 3261 | bool Scop::isProfitable(bool ScalarsAreUnprofitable) const { |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 3262 | if (PollyProcessUnprofitable) |
| 3263 | return true; |
| 3264 | |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 3265 | if (isEmpty()) |
| 3266 | return false; |
| 3267 | |
| 3268 | unsigned OptimizableStmtsOrLoops = 0; |
| 3269 | for (auto &Stmt : *this) { |
| 3270 | if (Stmt.getNumIterators() == 0) |
| 3271 | continue; |
| 3272 | |
| 3273 | bool ContainsArrayAccs = false; |
| 3274 | bool ContainsScalarAccs = false; |
| 3275 | for (auto *MA : Stmt) { |
| 3276 | if (MA->isRead()) |
| 3277 | continue; |
Michael Kruse | f3091bf | 2017-03-17 13:09:52 +0000 | [diff] [blame] | 3278 | ContainsArrayAccs |= MA->isLatestArrayKind(); |
| 3279 | ContainsScalarAccs |= MA->isLatestScalarKind(); |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 3280 | } |
| 3281 | |
Michael Kruse | f3091bf | 2017-03-17 13:09:52 +0000 | [diff] [blame] | 3282 | if (!ScalarsAreUnprofitable || (ContainsArrayAccs && !ContainsScalarAccs)) |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 3283 | OptimizableStmtsOrLoops += Stmt.getNumIterators(); |
| 3284 | } |
| 3285 | |
| 3286 | return OptimizableStmtsOrLoops > 1; |
| 3287 | } |
| 3288 | |
Johannes Doerfert | 5d5b306 | 2015-08-20 18:06:30 +0000 | [diff] [blame] | 3289 | bool Scop::hasFeasibleRuntimeContext() const { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3290 | auto PositiveContext = getAssumedContext(); |
| 3291 | auto NegativeContext = getInvalidContext(); |
| 3292 | PositiveContext = addNonEmptyDomainConstraints(PositiveContext); |
| 3293 | // addNonEmptyDomainConstraints returns null if ScopStmts have a null domain |
| 3294 | if (!PositiveContext) |
Johannes Doerfert | 94341c9 | 2016-04-23 13:00:27 +0000 | [diff] [blame] | 3295 | return false; |
Johannes Doerfert | 94341c9 | 2016-04-23 13:00:27 +0000 | [diff] [blame] | 3296 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3297 | bool IsFeasible = !(PositiveContext.is_empty() || |
| 3298 | PositiveContext.is_subset(NegativeContext)); |
| 3299 | if (!IsFeasible) |
| 3300 | return false; |
| 3301 | |
| 3302 | auto DomainContext = getDomains().params(); |
| 3303 | IsFeasible = !DomainContext.is_subset(NegativeContext); |
Dominik Adamski | 588fc9e | 2019-07-16 21:10:45 +0000 | [diff] [blame^] | 3304 | IsFeasible &= !getContext().is_subset(NegativeContext); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 3305 | |
Johannes Doerfert | 43788c5 | 2015-08-20 05:58:56 +0000 | [diff] [blame] | 3306 | return IsFeasible; |
| 3307 | } |
| 3308 | |
Dominik Adamski | 588fc9e | 2019-07-16 21:10:45 +0000 | [diff] [blame^] | 3309 | isl::set Scop::addNonEmptyDomainConstraints(isl::set C) const { |
| 3310 | isl::set DomainContext = getDomains().params(); |
| 3311 | return C.intersect_params(DomainContext); |
| 3312 | } |
| 3313 | |
| 3314 | MemoryAccess *Scop::lookupBasePtrAccess(MemoryAccess *MA) { |
| 3315 | Value *PointerBase = MA->getOriginalBaseAddr(); |
| 3316 | |
| 3317 | auto *PointerBaseInst = dyn_cast<Instruction>(PointerBase); |
| 3318 | if (!PointerBaseInst) |
| 3319 | return nullptr; |
| 3320 | |
| 3321 | auto *BasePtrStmt = getStmtFor(PointerBaseInst); |
| 3322 | if (!BasePtrStmt) |
| 3323 | return nullptr; |
| 3324 | |
| 3325 | return BasePtrStmt->getArrayAccessOrNULLFor(PointerBaseInst); |
| 3326 | } |
| 3327 | |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 3328 | static std::string toString(AssumptionKind Kind) { |
| 3329 | switch (Kind) { |
| 3330 | case ALIASING: |
| 3331 | return "No-aliasing"; |
| 3332 | case INBOUNDS: |
| 3333 | return "Inbounds"; |
| 3334 | case WRAPPING: |
| 3335 | return "No-overflows"; |
Johannes Doerfert | c359628 | 2016-04-25 14:01:36 +0000 | [diff] [blame] | 3336 | case UNSIGNED: |
| 3337 | return "Signed-unsigned"; |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 3338 | case COMPLEXITY: |
| 3339 | return "Low complexity"; |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 3340 | case PROFITABLE: |
| 3341 | return "Profitable"; |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 3342 | case ERRORBLOCK: |
| 3343 | return "No-error"; |
| 3344 | case INFINITELOOP: |
| 3345 | return "Finite loop"; |
| 3346 | case INVARIANTLOAD: |
| 3347 | return "Invariant load"; |
| 3348 | case DELINEARIZATION: |
| 3349 | return "Delinearization"; |
| 3350 | } |
| 3351 | llvm_unreachable("Unknown AssumptionKind!"); |
| 3352 | } |
| 3353 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3354 | bool Scop::isEffectiveAssumption(isl::set Set, AssumptionSign Sign) { |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 3355 | if (Sign == AS_ASSUMPTION) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3356 | if (Context.is_subset(Set)) |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 3357 | return false; |
| 3358 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3359 | if (AssumedContext.is_subset(Set)) |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 3360 | return false; |
| 3361 | } else { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3362 | if (Set.is_disjoint(Context)) |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 3363 | return false; |
| 3364 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3365 | if (Set.is_subset(InvalidContext)) |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 3366 | return false; |
| 3367 | } |
| 3368 | return true; |
| 3369 | } |
| 3370 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3371 | bool Scop::trackAssumption(AssumptionKind Kind, isl::set Set, DebugLoc Loc, |
| 3372 | AssumptionSign Sign, BasicBlock *BB) { |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 3373 | if (PollyRemarksMinimal && !isEffectiveAssumption(Set, Sign)) |
| 3374 | return false; |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 3375 | |
Johannes Doerfert | b3265a3 | 2016-11-17 22:08:40 +0000 | [diff] [blame] | 3376 | // Do never emit trivial assumptions as they only clutter the output. |
| 3377 | if (!PollyRemarksMinimal) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3378 | isl::set Univ; |
Johannes Doerfert | b3265a3 | 2016-11-17 22:08:40 +0000 | [diff] [blame] | 3379 | if (Sign == AS_ASSUMPTION) |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3380 | Univ = isl::set::universe(Set.get_space()); |
Johannes Doerfert | b3265a3 | 2016-11-17 22:08:40 +0000 | [diff] [blame] | 3381 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3382 | bool IsTrivial = (Sign == AS_RESTRICTION && Set.is_empty()) || |
| 3383 | (Sign == AS_ASSUMPTION && Univ.is_equal(Set)); |
Johannes Doerfert | b3265a3 | 2016-11-17 22:08:40 +0000 | [diff] [blame] | 3384 | |
| 3385 | if (IsTrivial) |
| 3386 | return false; |
| 3387 | } |
| 3388 | |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 3389 | switch (Kind) { |
| 3390 | case ALIASING: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 3391 | AssumptionsAliasing++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 3392 | break; |
| 3393 | case INBOUNDS: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 3394 | AssumptionsInbounds++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 3395 | break; |
| 3396 | case WRAPPING: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 3397 | AssumptionsWrapping++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 3398 | break; |
| 3399 | case UNSIGNED: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 3400 | AssumptionsUnsigned++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 3401 | break; |
| 3402 | case COMPLEXITY: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 3403 | AssumptionsComplexity++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 3404 | break; |
| 3405 | case PROFITABLE: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 3406 | AssumptionsUnprofitable++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 3407 | break; |
| 3408 | case ERRORBLOCK: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 3409 | AssumptionsErrorBlock++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 3410 | break; |
| 3411 | case INFINITELOOP: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 3412 | AssumptionsInfiniteLoop++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 3413 | break; |
| 3414 | case INVARIANTLOAD: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 3415 | AssumptionsInvariantLoad++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 3416 | break; |
| 3417 | case DELINEARIZATION: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 3418 | AssumptionsDelinearization++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 3419 | break; |
| 3420 | } |
| 3421 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 3422 | auto Suffix = Sign == AS_ASSUMPTION ? " assumption:\t" : " restriction:\t"; |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3423 | std::string Msg = toString(Kind) + Suffix + Set.to_str(); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 3424 | if (BB) |
| 3425 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "AssumpRestrict", Loc, BB) |
| 3426 | << Msg); |
| 3427 | else |
| 3428 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "AssumpRestrict", Loc, |
| 3429 | R.getEntry()) |
| 3430 | << Msg); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 3431 | return true; |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 3432 | } |
| 3433 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3434 | void Scop::addAssumption(AssumptionKind Kind, isl::set Set, DebugLoc Loc, |
| 3435 | AssumptionSign Sign, BasicBlock *BB) { |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 3436 | // Simplify the assumptions/restrictions first. |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3437 | Set = Set.gist_params(getContext()); |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 3438 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3439 | if (!trackAssumption(Kind, Set, Loc, Sign, BB)) |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 3440 | return; |
Tobias Grosser | 20a4c0c | 2015-11-11 16:22:36 +0000 | [diff] [blame] | 3441 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3442 | if (Sign == AS_ASSUMPTION) |
| 3443 | AssumedContext = AssumedContext.intersect(Set).coalesce(); |
| 3444 | else |
| 3445 | InvalidContext = InvalidContext.unite(Set).coalesce(); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 3446 | } |
| 3447 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3448 | void Scop::recordAssumption(AssumptionKind Kind, isl::set Set, DebugLoc Loc, |
| 3449 | AssumptionSign Sign, BasicBlock *BB) { |
| 3450 | assert((Set.is_params() || BB) && |
Tobias Grosser | f67433a | 2016-11-10 11:44:10 +0000 | [diff] [blame] | 3451 | "Assumptions without a basic block must be parameter sets"); |
Johannes Doerfert | 615e0b8 | 2016-04-12 13:28:39 +0000 | [diff] [blame] | 3452 | RecordedAssumptions.push_back({Kind, Sign, Set, Loc, BB}); |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 3453 | } |
| 3454 | |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 3455 | void Scop::invalidate(AssumptionKind Kind, DebugLoc Loc, BasicBlock *BB) { |
Nicola Zaghen | 349506a | 2018-05-15 13:37:17 +0000 | [diff] [blame] | 3456 | LLVM_DEBUG(dbgs() << "Invalidate SCoP because of reason " << Kind << "\n"); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3457 | addAssumption(Kind, isl::set::empty(getParamSpace()), Loc, AS_ASSUMPTION, BB); |
Tobias Grosser | 8d4f626 | 2015-12-12 09:52:26 +0000 | [diff] [blame] | 3458 | } |
| 3459 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3460 | isl::set Scop::getInvalidContext() const { return InvalidContext; } |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 3461 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3462 | void Scop::printContext(raw_ostream &OS) const { |
| 3463 | OS << "Context:\n"; |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 3464 | OS.indent(4) << Context << "\n"; |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 3465 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 3466 | OS.indent(4) << "Assumed Context:\n"; |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 3467 | OS.indent(4) << AssumedContext << "\n"; |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 3468 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 3469 | OS.indent(4) << "Invalid Context:\n"; |
| 3470 | OS.indent(4) << InvalidContext << "\n"; |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 3471 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 3472 | unsigned Dim = 0; |
| 3473 | for (const SCEV *Parameter : Parameters) |
| 3474 | OS.indent(4) << "p" << Dim++ << ": " << *Parameter << "\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3475 | } |
| 3476 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3477 | void Scop::printAliasAssumptions(raw_ostream &OS) const { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 3478 | int noOfGroups = 0; |
| 3479 | for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) { |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 3480 | if (Pair.second.size() == 0) |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 3481 | noOfGroups += 1; |
| 3482 | else |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 3483 | noOfGroups += Pair.second.size(); |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 3484 | } |
| 3485 | |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 3486 | OS.indent(4) << "Alias Groups (" << noOfGroups << "):\n"; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3487 | if (MinMaxAliasGroups.empty()) { |
| 3488 | OS.indent(8) << "n/a\n"; |
| 3489 | return; |
| 3490 | } |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 3491 | |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 3492 | for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 3493 | |
| 3494 | // If the group has no read only accesses print the write accesses. |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 3495 | if (Pair.second.empty()) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 3496 | OS.indent(8) << "[["; |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 3497 | for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 3498 | OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second |
| 3499 | << ">"; |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 3500 | } |
| 3501 | OS << " ]]\n"; |
| 3502 | } |
| 3503 | |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 3504 | for (const MinMaxAccessTy &MMAReadOnly : Pair.second) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 3505 | OS.indent(8) << "[["; |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 3506 | OS << " <" << MMAReadOnly.first << ", " << MMAReadOnly.second << ">"; |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 3507 | for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 3508 | OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second |
| 3509 | << ">"; |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 3510 | } |
| 3511 | OS << " ]]\n"; |
| 3512 | } |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3513 | } |
| 3514 | } |
| 3515 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 3516 | void Scop::printStatements(raw_ostream &OS, bool PrintInstructions) const { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3517 | OS << "Statements {\n"; |
| 3518 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 3519 | for (const ScopStmt &Stmt : *this) { |
| 3520 | OS.indent(4); |
| 3521 | Stmt.print(OS, PrintInstructions); |
| 3522 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3523 | |
| 3524 | OS.indent(4) << "}\n"; |
| 3525 | } |
| 3526 | |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 3527 | void Scop::printArrayInfo(raw_ostream &OS) const { |
| 3528 | OS << "Arrays {\n"; |
| 3529 | |
Tobias Grosser | ab67144 | 2015-05-23 05:58:27 +0000 | [diff] [blame] | 3530 | for (auto &Array : arrays()) |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 3531 | Array->print(OS); |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 3532 | |
| 3533 | OS.indent(4) << "}\n"; |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 3534 | |
| 3535 | OS.indent(4) << "Arrays (Bounds as pw_affs) {\n"; |
| 3536 | |
| 3537 | for (auto &Array : arrays()) |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 3538 | Array->print(OS, /* SizeAsPwAff */ true); |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 3539 | |
| 3540 | OS.indent(4) << "}\n"; |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 3541 | } |
| 3542 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 3543 | void Scop::print(raw_ostream &OS, bool PrintInstructions) const { |
Johannes Doerfert | 3f52e35 | 2016-05-23 12:38:05 +0000 | [diff] [blame] | 3544 | OS.indent(4) << "Function: " << getFunction().getName() << "\n"; |
Tobias Grosser | 483fdd4 | 2014-03-18 18:05:38 +0000 | [diff] [blame] | 3545 | OS.indent(4) << "Region: " << getNameStr() << "\n"; |
David Peixotto | dc0a11c | 2015-01-13 18:31:55 +0000 | [diff] [blame] | 3546 | OS.indent(4) << "Max Loop Depth: " << getMaxLoopDepth() << "\n"; |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 3547 | OS.indent(4) << "Invariant Accesses: {\n"; |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 3548 | for (const auto &IAClass : InvariantEquivClasses) { |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 3549 | const auto &MAs = IAClass.InvariantAccesses; |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3550 | if (MAs.empty()) { |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 3551 | OS.indent(12) << "Class Pointer: " << *IAClass.IdentifyingPointer << "\n"; |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 3552 | } else { |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3553 | MAs.front()->print(OS); |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 3554 | OS.indent(12) << "Execution Context: " << IAClass.ExecutionContext |
| 3555 | << "\n"; |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 3556 | } |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 3557 | } |
| 3558 | OS.indent(4) << "}\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3559 | printContext(OS.indent(4)); |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 3560 | printArrayInfo(OS.indent(4)); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3561 | printAliasAssumptions(OS); |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 3562 | printStatements(OS.indent(4), PrintInstructions); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3563 | } |
| 3564 | |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 3565 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Michael Kruse | e186013 | 2017-07-21 15:54:13 +0000 | [diff] [blame] | 3566 | LLVM_DUMP_METHOD void Scop::dump() const { print(dbgs(), true); } |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 3567 | #endif |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3568 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3569 | isl::ctx Scop::getIslCtx() const { return IslCtx.get(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3570 | |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 3571 | __isl_give PWACtx Scop::getPwAff(const SCEV *E, BasicBlock *BB, |
| 3572 | bool NonNegative) { |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 3573 | // First try to use the SCEVAffinator to generate a piecewise defined |
| 3574 | // affine function from @p E in the context of @p BB. If that tasks becomes to |
| 3575 | // complex the affinator might return a nullptr. In such a case we invalidate |
| 3576 | // the SCoP and return a dummy value. This way we do not need to add error |
Tobias Grosser | cdbe5c9 | 2017-01-06 17:30:34 +0000 | [diff] [blame] | 3577 | // handling code to all users of this function. |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 3578 | auto PWAC = Affinator.getPwAff(E, BB); |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 3579 | if (PWAC.first) { |
Johannes Doerfert | 56b3776 | 2016-05-10 11:45:46 +0000 | [diff] [blame] | 3580 | // TODO: We could use a heuristic and either use: |
| 3581 | // SCEVAffinator::takeNonNegativeAssumption |
| 3582 | // or |
| 3583 | // SCEVAffinator::interpretAsUnsigned |
| 3584 | // to deal with unsigned or "NonNegative" SCEVs. |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 3585 | if (NonNegative) |
| 3586 | Affinator.takeNonNegativeAssumption(PWAC); |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 3587 | return PWAC; |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 3588 | } |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 3589 | |
| 3590 | auto DL = BB ? BB->getTerminator()->getDebugLoc() : DebugLoc(); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 3591 | invalidate(COMPLEXITY, DL, BB); |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 3592 | return Affinator.getPwAff(SE->getZero(E->getType()), BB); |
Johannes Doerfert | 574182d | 2015-08-12 10:19:50 +0000 | [diff] [blame] | 3593 | } |
| 3594 | |
Tobias Grosser | 31df6f3 | 2017-08-06 21:42:25 +0000 | [diff] [blame] | 3595 | isl::union_set Scop::getDomains() const { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3596 | isl_space *EmptySpace = isl_space_params_alloc(getIslCtx().get(), 0); |
Tobias Grosser | 941cb7d | 2017-03-17 09:02:50 +0000 | [diff] [blame] | 3597 | isl_union_set *Domain = isl_union_set_empty(EmptySpace); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 3598 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 3599 | for (const ScopStmt &Stmt : *this) |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 3600 | Domain = isl_union_set_add_set(Domain, Stmt.getDomain().release()); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 3601 | |
Tobias Grosser | 31df6f3 | 2017-08-06 21:42:25 +0000 | [diff] [blame] | 3602 | return isl::manage(Domain); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 3603 | } |
| 3604 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 3605 | isl::pw_aff Scop::getPwAffOnly(const SCEV *E, BasicBlock *BB) { |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 3606 | PWACtx PWAC = getPwAff(E, BB); |
Philip Pfaffe | d98dbee | 2017-12-06 21:02:22 +0000 | [diff] [blame] | 3607 | return PWAC.first; |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 3608 | } |
| 3609 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 3610 | isl::union_map |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 3611 | Scop::getAccessesOfType(std::function<bool(MemoryAccess &)> Predicate) { |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 3612 | isl::union_map Accesses = isl::union_map::empty(getParamSpace()); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 3613 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 3614 | for (ScopStmt &Stmt : *this) { |
| 3615 | for (MemoryAccess *MA : Stmt) { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 3616 | if (!Predicate(*MA)) |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 3617 | continue; |
| 3618 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 3619 | isl::set Domain = Stmt.getDomain(); |
| 3620 | isl::map AccessDomain = MA->getAccessRelation(); |
| 3621 | AccessDomain = AccessDomain.intersect_domain(Domain); |
| 3622 | Accesses = Accesses.add_map(AccessDomain); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 3623 | } |
| 3624 | } |
Tobias Grosser | 206e9e3 | 2017-07-24 16:22:27 +0000 | [diff] [blame] | 3625 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 3626 | return Accesses.coalesce(); |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 3627 | } |
| 3628 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 3629 | isl::union_map Scop::getMustWrites() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 3630 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isMustWrite(); }); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 3631 | } |
| 3632 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 3633 | isl::union_map Scop::getMayWrites() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 3634 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isMayWrite(); }); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 3635 | } |
| 3636 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 3637 | isl::union_map Scop::getWrites() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 3638 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isWrite(); }); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 3639 | } |
| 3640 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 3641 | isl::union_map Scop::getReads() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 3642 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isRead(); }); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 3643 | } |
| 3644 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 3645 | isl::union_map Scop::getAccesses() { |
Tobias Grosser | 2ac2338 | 2015-11-12 14:07:13 +0000 | [diff] [blame] | 3646 | return getAccessesOfType([](MemoryAccess &MA) { return true; }); |
| 3647 | } |
| 3648 | |
Tobias Grosser | fa03cb7 | 2017-08-17 22:04:53 +0000 | [diff] [blame] | 3649 | isl::union_map Scop::getAccesses(ScopArrayInfo *Array) { |
| 3650 | return getAccessesOfType( |
| 3651 | [Array](MemoryAccess &MA) { return MA.getScopArrayInfo() == Array; }); |
| 3652 | } |
| 3653 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 3654 | isl::union_map Scop::getSchedule() const { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3655 | auto Tree = getScheduleTree(); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3656 | return Tree.get_map(); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 3657 | } |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 3658 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 3659 | isl::schedule Scop::getScheduleTree() const { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3660 | return Schedule.intersect_domain(getDomains()); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 3661 | } |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 3662 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3663 | void Scop::setSchedule(isl::union_map NewSchedule) { |
| 3664 | auto S = isl::schedule::from_domain(getDomains()); |
| 3665 | Schedule = S.insert_partial_schedule( |
| 3666 | isl::multi_union_pw_aff::from_union_map(NewSchedule)); |
Michael Kruse | 2dab88e | 2018-06-06 21:37:35 +0000 | [diff] [blame] | 3667 | ScheduleModified = true; |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 3668 | } |
| 3669 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3670 | void Scop::setScheduleTree(isl::schedule NewSchedule) { |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 3671 | Schedule = NewSchedule; |
Michael Kruse | 2dab88e | 2018-06-06 21:37:35 +0000 | [diff] [blame] | 3672 | ScheduleModified = true; |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 3673 | } |
| 3674 | |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 3675 | bool Scop::restrictDomains(isl::union_set Domain) { |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 3676 | bool Changed = false; |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 3677 | for (ScopStmt &Stmt : *this) { |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 3678 | isl::union_set StmtDomain = isl::union_set(Stmt.getDomain()); |
| 3679 | isl::union_set NewStmtDomain = StmtDomain.intersect(Domain); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 3680 | |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 3681 | if (StmtDomain.is_subset(NewStmtDomain)) |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 3682 | continue; |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 3683 | |
| 3684 | Changed = true; |
| 3685 | |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 3686 | NewStmtDomain = NewStmtDomain.coalesce(); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 3687 | |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 3688 | if (NewStmtDomain.is_empty()) |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 3689 | Stmt.restrictDomain(isl::set::empty(Stmt.getDomainSpace())); |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 3690 | else |
| 3691 | Stmt.restrictDomain(isl::set(NewStmtDomain)); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 3692 | } |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 3693 | return Changed; |
| 3694 | } |
| 3695 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3696 | ScalarEvolution *Scop::getSE() const { return SE; } |
| 3697 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 3698 | // Create an isl_multi_union_aff that defines an identity mapping from the |
| 3699 | // elements of USet to their N-th dimension. |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 3700 | // |
Tobias Grosser | cbf7ae8 | 2015-12-21 22:45:53 +0000 | [diff] [blame] | 3701 | // # Example: |
| 3702 | // |
| 3703 | // Domain: { A[i,j]; B[i,j,k] } |
| 3704 | // N: 1 |
| 3705 | // |
| 3706 | // Resulting Mapping: { {A[i,j] -> [(j)]; B[i,j,k] -> [(j)] } |
| 3707 | // |
| 3708 | // @param USet A union set describing the elements for which to generate a |
| 3709 | // mapping. |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 3710 | // @param N The dimension to map to. |
Tobias Grosser | cbf7ae8 | 2015-12-21 22:45:53 +0000 | [diff] [blame] | 3711 | // @returns A mapping from USet to its N-th dimension. |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 3712 | static isl::multi_union_pw_aff mapToDimension(isl::union_set USet, int N) { |
Tobias Grosser | cbf7ae8 | 2015-12-21 22:45:53 +0000 | [diff] [blame] | 3713 | assert(N >= 0); |
Tobias Grosser | c900633d | 2015-12-21 23:01:53 +0000 | [diff] [blame] | 3714 | assert(USet); |
Siddharth Bhat | 8bb436e | 2017-05-29 11:34:29 +0000 | [diff] [blame] | 3715 | assert(!USet.is_empty()); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 3716 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 3717 | auto Result = isl::union_pw_multi_aff::empty(USet.get_space()); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 3718 | |
Tobias Grosser | 31e29a4 | 2018-07-16 19:04:16 +0000 | [diff] [blame] | 3719 | for (isl::set S : USet.get_set_list()) { |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 3720 | int Dim = S.dim(isl::dim::set); |
| 3721 | auto PMA = isl::pw_multi_aff::project_out_map(S.get_space(), isl::dim::set, |
| 3722 | N, Dim - N); |
| 3723 | if (N > 1) |
| 3724 | PMA = PMA.drop_dims(isl::dim::out, 0, N - 1); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 3725 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 3726 | Result = Result.add_pw_multi_aff(PMA); |
Tobias Grosser | 31e29a4 | 2018-07-16 19:04:16 +0000 | [diff] [blame] | 3727 | } |
Tobias Grosser | cbf7ae8 | 2015-12-21 22:45:53 +0000 | [diff] [blame] | 3728 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 3729 | return isl::multi_union_pw_aff(isl::union_pw_multi_aff(Result)); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 3730 | } |
| 3731 | |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 3732 | void Scop::addScopStmt(BasicBlock *BB, StringRef Name, Loop *SurroundingLoop, |
| 3733 | std::vector<Instruction *> Instructions) { |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 3734 | assert(BB && "Unexpected nullptr!"); |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 3735 | Stmts.emplace_back(*this, *BB, Name, SurroundingLoop, Instructions); |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 3736 | auto *Stmt = &Stmts.back(); |
Michael Kruse | 4dfa732 | 2017-07-18 15:41:49 +0000 | [diff] [blame] | 3737 | StmtMap[BB].push_back(Stmt); |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 3738 | for (Instruction *Inst : Instructions) { |
| 3739 | assert(!InstStmtMap.count(Inst) && |
| 3740 | "Unexpected statement corresponding to the instruction."); |
| 3741 | InstStmtMap[Inst] = Stmt; |
| 3742 | } |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 3743 | } |
| 3744 | |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 3745 | void Scop::addScopStmt(Region *R, StringRef Name, Loop *SurroundingLoop, |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 3746 | std::vector<Instruction *> Instructions) { |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 3747 | assert(R && "Unexpected nullptr!"); |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 3748 | Stmts.emplace_back(*this, *R, Name, SurroundingLoop, Instructions); |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 3749 | auto *Stmt = &Stmts.back(); |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 3750 | |
| 3751 | for (Instruction *Inst : Instructions) { |
| 3752 | assert(!InstStmtMap.count(Inst) && |
| 3753 | "Unexpected statement corresponding to the instruction."); |
| 3754 | InstStmtMap[Inst] = Stmt; |
| 3755 | } |
| 3756 | |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 3757 | for (BasicBlock *BB : R->blocks()) { |
Michael Kruse | 4dfa732 | 2017-07-18 15:41:49 +0000 | [diff] [blame] | 3758 | StmtMap[BB].push_back(Stmt); |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 3759 | if (BB == R->getEntry()) |
| 3760 | continue; |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 3761 | for (Instruction &Inst : *BB) { |
| 3762 | assert(!InstStmtMap.count(&Inst) && |
| 3763 | "Unexpected statement corresponding to the instruction."); |
| 3764 | InstStmtMap[&Inst] = Stmt; |
| 3765 | } |
| 3766 | } |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 3767 | } |
| 3768 | |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 3769 | ScopStmt *Scop::addScopStmt(isl::map SourceRel, isl::map TargetRel, |
| 3770 | isl::set Domain) { |
Tobias Grosser | eba86a1 | 2016-11-09 04:24:49 +0000 | [diff] [blame] | 3771 | #ifndef NDEBUG |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 3772 | isl::set SourceDomain = SourceRel.domain(); |
| 3773 | isl::set TargetDomain = TargetRel.domain(); |
| 3774 | assert(Domain.is_subset(TargetDomain) && |
Tobias Grosser | 744740a | 2016-11-05 21:02:43 +0000 | [diff] [blame] | 3775 | "Target access not defined for complete statement domain"); |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 3776 | assert(Domain.is_subset(SourceDomain) && |
Tobias Grosser | 744740a | 2016-11-05 21:02:43 +0000 | [diff] [blame] | 3777 | "Source access not defined for complete statement domain"); |
Tobias Grosser | eba86a1 | 2016-11-09 04:24:49 +0000 | [diff] [blame] | 3778 | #endif |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 3779 | Stmts.emplace_back(*this, SourceRel, TargetRel, Domain); |
| 3780 | CopyStmtsNum++; |
| 3781 | return &(Stmts.back()); |
| 3782 | } |
| 3783 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 3784 | void Scop::buildSchedule(LoopInfo &LI) { |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 3785 | Loop *L = getLoopSurroundingScop(*this, LI); |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 3786 | LoopStackTy LoopStack({LoopStackElementTy(L, nullptr, 0)}); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 3787 | buildSchedule(getRegion().getNode(), LoopStack, LI); |
Tobias Grosser | 151ae32 | 2016-04-03 19:36:52 +0000 | [diff] [blame] | 3788 | assert(LoopStack.size() == 1 && LoopStack.back().L == L); |
| 3789 | Schedule = LoopStack[0].Schedule; |
Johannes Doerfert | f9711ef | 2016-01-06 12:59:23 +0000 | [diff] [blame] | 3790 | } |
| 3791 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 3792 | /// To generate a schedule for the elements in a Region we traverse the Region |
| 3793 | /// in reverse-post-order and add the contained RegionNodes in traversal order |
| 3794 | /// to the schedule of the loop that is currently at the top of the LoopStack. |
| 3795 | /// For loop-free codes, this results in a correct sequential ordering. |
| 3796 | /// |
| 3797 | /// Example: |
| 3798 | /// bb1(0) |
| 3799 | /// / \. |
| 3800 | /// bb2(1) bb3(2) |
| 3801 | /// \ / \. |
| 3802 | /// bb4(3) bb5(4) |
| 3803 | /// \ / |
| 3804 | /// bb6(5) |
| 3805 | /// |
| 3806 | /// Including loops requires additional processing. Whenever a loop header is |
| 3807 | /// encountered, the corresponding loop is added to the @p LoopStack. Starting |
| 3808 | /// from an empty schedule, we first process all RegionNodes that are within |
| 3809 | /// this loop and complete the sequential schedule at this loop-level before |
| 3810 | /// processing about any other nodes. To implement this |
| 3811 | /// loop-nodes-first-processing, the reverse post-order traversal is |
| 3812 | /// insufficient. Hence, we additionally check if the traversal yields |
| 3813 | /// sub-regions or blocks that are outside the last loop on the @p LoopStack. |
| 3814 | /// These region-nodes are then queue and only traverse after the all nodes |
| 3815 | /// within the current loop have been processed. |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 3816 | void Scop::buildSchedule(Region *R, LoopStackTy &LoopStack, LoopInfo &LI) { |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 3817 | Loop *OuterScopLoop = getLoopSurroundingScop(*this, LI); |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 3818 | |
| 3819 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 3820 | std::deque<RegionNode *> WorkList(RTraversal.begin(), RTraversal.end()); |
| 3821 | std::deque<RegionNode *> DelayList; |
| 3822 | bool LastRNWaiting = false; |
| 3823 | |
| 3824 | // Iterate over the region @p R in reverse post-order but queue |
| 3825 | // sub-regions/blocks iff they are not part of the last encountered but not |
| 3826 | // completely traversed loop. The variable LastRNWaiting is a flag to indicate |
| 3827 | // that we queued the last sub-region/block from the reverse post-order |
| 3828 | // iterator. If it is set we have to explore the next sub-region/block from |
| 3829 | // the iterator (if any) to guarantee progress. If it is not set we first try |
| 3830 | // the next queued sub-region/blocks. |
| 3831 | while (!WorkList.empty() || !DelayList.empty()) { |
| 3832 | RegionNode *RN; |
| 3833 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 3834 | if ((LastRNWaiting && !WorkList.empty()) || DelayList.empty()) { |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 3835 | RN = WorkList.front(); |
| 3836 | WorkList.pop_front(); |
| 3837 | LastRNWaiting = false; |
| 3838 | } else { |
| 3839 | RN = DelayList.front(); |
| 3840 | DelayList.pop_front(); |
| 3841 | } |
| 3842 | |
| 3843 | Loop *L = getRegionNodeLoop(RN, LI); |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 3844 | if (!contains(L)) |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 3845 | L = OuterScopLoop; |
| 3846 | |
Tobias Grosser | 151ae32 | 2016-04-03 19:36:52 +0000 | [diff] [blame] | 3847 | Loop *LastLoop = LoopStack.back().L; |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 3848 | if (LastLoop != L) { |
Johannes Doerfert | d5edbd6 | 2016-04-03 23:09:06 +0000 | [diff] [blame] | 3849 | if (LastLoop && !LastLoop->contains(L)) { |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 3850 | LastRNWaiting = true; |
| 3851 | DelayList.push_back(RN); |
| 3852 | continue; |
| 3853 | } |
| 3854 | LoopStack.push_back({L, nullptr, 0}); |
| 3855 | } |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 3856 | buildSchedule(RN, LoopStack, LI); |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 3857 | } |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 3858 | } |
| 3859 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 3860 | void Scop::buildSchedule(RegionNode *RN, LoopStackTy &LoopStack, LoopInfo &LI) { |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 3861 | if (RN->isSubRegion()) { |
| 3862 | auto *LocalRegion = RN->getNodeAs<Region>(); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 3863 | if (!isNonAffineSubRegion(LocalRegion)) { |
| 3864 | buildSchedule(LocalRegion, LoopStack, LI); |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 3865 | return; |
| 3866 | } |
| 3867 | } |
Michael Kruse | 046dde4 | 2015-08-10 13:01:57 +0000 | [diff] [blame] | 3868 | |
Philip Pfaffe | 8dd0f47 | 2017-11-16 16:35:19 +0000 | [diff] [blame] | 3869 | assert(LoopStack.rbegin() != LoopStack.rend()); |
| 3870 | auto LoopData = LoopStack.rbegin(); |
| 3871 | LoopData->NumBlocksProcessed += getNumBlocksInRegionNode(RN); |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 3872 | |
Michael Kruse | 1ce6791 | 2017-07-20 17:18:58 +0000 | [diff] [blame] | 3873 | for (auto *Stmt : getStmtListFor(RN)) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3874 | isl::union_set UDomain{Stmt->getDomain()}; |
| 3875 | auto StmtSchedule = isl::schedule::from_domain(UDomain); |
Philip Pfaffe | 8dd0f47 | 2017-11-16 16:35:19 +0000 | [diff] [blame] | 3876 | LoopData->Schedule = combineInSequence(LoopData->Schedule, StmtSchedule); |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 3877 | } |
| 3878 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 3879 | // Check if we just processed the last node in this loop. If we did, finalize |
| 3880 | // the loop by: |
| 3881 | // |
| 3882 | // - adding new schedule dimensions |
| 3883 | // - folding the resulting schedule into the parent loop schedule |
| 3884 | // - dropping the loop schedule from the LoopStack. |
| 3885 | // |
| 3886 | // Then continue to check surrounding loops, which might also have been |
| 3887 | // completed by this node. |
Philip Pfaffe | 8dd0f47 | 2017-11-16 16:35:19 +0000 | [diff] [blame] | 3888 | size_t Dimension = LoopStack.size(); |
| 3889 | while (LoopData->L && |
| 3890 | LoopData->NumBlocksProcessed == getNumBlocksInLoop(LoopData->L)) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3891 | isl::schedule Schedule = LoopData->Schedule; |
Philip Pfaffe | 8dd0f47 | 2017-11-16 16:35:19 +0000 | [diff] [blame] | 3892 | auto NumBlocksProcessed = LoopData->NumBlocksProcessed; |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 3893 | |
Philip Pfaffe | 8dd0f47 | 2017-11-16 16:35:19 +0000 | [diff] [blame] | 3894 | assert(std::next(LoopData) != LoopStack.rend()); |
| 3895 | ++LoopData; |
| 3896 | --Dimension; |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 3897 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 3898 | if (Schedule) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3899 | isl::union_set Domain = Schedule.get_domain(); |
Philip Pfaffe | 8dd0f47 | 2017-11-16 16:35:19 +0000 | [diff] [blame] | 3900 | isl::multi_union_pw_aff MUPA = mapToDimension(Domain, Dimension); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3901 | Schedule = Schedule.insert_partial_schedule(MUPA); |
Philip Pfaffe | 8dd0f47 | 2017-11-16 16:35:19 +0000 | [diff] [blame] | 3902 | LoopData->Schedule = combineInSequence(LoopData->Schedule, Schedule); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3903 | } |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 3904 | |
Philip Pfaffe | 8dd0f47 | 2017-11-16 16:35:19 +0000 | [diff] [blame] | 3905 | LoopData->NumBlocksProcessed += NumBlocksProcessed; |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 3906 | } |
Philip Pfaffe | 8dd0f47 | 2017-11-16 16:35:19 +0000 | [diff] [blame] | 3907 | // Now pop all loops processed up there from the LoopStack |
| 3908 | LoopStack.erase(LoopStack.begin() + Dimension, LoopStack.end()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 3909 | } |
| 3910 | |
Michael Kruse | 6eba4b1 | 2017-07-20 17:08:50 +0000 | [diff] [blame] | 3911 | ArrayRef<ScopStmt *> Scop::getStmtListFor(BasicBlock *BB) const { |
| 3912 | auto StmtMapIt = StmtMap.find(BB); |
| 3913 | if (StmtMapIt == StmtMap.end()) |
| 3914 | return {}; |
Michael Kruse | 6eba4b1 | 2017-07-20 17:08:50 +0000 | [diff] [blame] | 3915 | return StmtMapIt->second; |
| 3916 | } |
| 3917 | |
Michael Kruse | a230f22 | 2018-01-23 23:56:36 +0000 | [diff] [blame] | 3918 | ScopStmt *Scop::getIncomingStmtFor(const Use &U) const { |
| 3919 | auto *PHI = cast<PHINode>(U.getUser()); |
| 3920 | BasicBlock *IncomingBB = PHI->getIncomingBlock(U); |
| 3921 | |
| 3922 | // If the value is a non-synthesizable from the incoming block, use the |
| 3923 | // statement that contains it as user statement. |
| 3924 | if (auto *IncomingInst = dyn_cast<Instruction>(U.get())) { |
| 3925 | if (IncomingInst->getParent() == IncomingBB) { |
| 3926 | if (ScopStmt *IncomingStmt = getStmtFor(IncomingInst)) |
| 3927 | return IncomingStmt; |
| 3928 | } |
| 3929 | } |
| 3930 | |
| 3931 | // Otherwise, use the epilogue/last statement. |
| 3932 | return getLastStmtFor(IncomingBB); |
| 3933 | } |
| 3934 | |
Michael Kruse | 6eba4b1 | 2017-07-20 17:08:50 +0000 | [diff] [blame] | 3935 | ScopStmt *Scop::getLastStmtFor(BasicBlock *BB) const { |
| 3936 | ArrayRef<ScopStmt *> StmtList = getStmtListFor(BB); |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 3937 | if (!StmtList.empty()) |
Michael Kruse | 6eba4b1 | 2017-07-20 17:08:50 +0000 | [diff] [blame] | 3938 | return StmtList.back(); |
| 3939 | return nullptr; |
| 3940 | } |
| 3941 | |
Michael Kruse | 1ce6791 | 2017-07-20 17:18:58 +0000 | [diff] [blame] | 3942 | ArrayRef<ScopStmt *> Scop::getStmtListFor(RegionNode *RN) const { |
Michael Kruse | 6f7721f | 2016-02-24 22:08:19 +0000 | [diff] [blame] | 3943 | if (RN->isSubRegion()) |
Michael Kruse | 1ce6791 | 2017-07-20 17:18:58 +0000 | [diff] [blame] | 3944 | return getStmtListFor(RN->getNodeAs<Region>()); |
| 3945 | return getStmtListFor(RN->getNodeAs<BasicBlock>()); |
Michael Kruse | 6f7721f | 2016-02-24 22:08:19 +0000 | [diff] [blame] | 3946 | } |
| 3947 | |
Michael Kruse | 1ce6791 | 2017-07-20 17:18:58 +0000 | [diff] [blame] | 3948 | ArrayRef<ScopStmt *> Scop::getStmtListFor(Region *R) const { |
| 3949 | return getStmtListFor(R->getEntry()); |
Michael Kruse | a902ba6 | 2015-12-13 19:21:45 +0000 | [diff] [blame] | 3950 | } |
| 3951 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3952 | int Scop::getRelativeLoopDepth(const Loop *L) const { |
Philip Pfaffe | 1a0128f | 2017-05-24 18:39:39 +0000 | [diff] [blame] | 3953 | if (!L || !R.contains(L)) |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 3954 | return -1; |
Philip Pfaffe | 1a0128f | 2017-05-24 18:39:39 +0000 | [diff] [blame] | 3955 | // outermostLoopInRegion always returns nullptr for top level regions |
| 3956 | if (R.isTopLevelRegion()) { |
| 3957 | // LoopInfo's depths start at 1, we start at 0 |
| 3958 | return L->getLoopDepth() - 1; |
| 3959 | } else { |
| 3960 | Loop *OuterLoop = R.outermostLoopInRegion(const_cast<Loop *>(L)); |
| 3961 | assert(OuterLoop); |
| 3962 | return L->getLoopDepth() - OuterLoop->getLoopDepth(); |
| 3963 | } |
Johannes Doerfert | d020b77 | 2015-08-27 06:53:52 +0000 | [diff] [blame] | 3964 | } |
| 3965 | |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 3966 | ScopArrayInfo *Scop::getArrayInfoByName(const std::string BaseName) { |
| 3967 | for (auto &SAI : arrays()) { |
| 3968 | if (SAI->getName() == BaseName) |
| 3969 | return SAI; |
| 3970 | } |
| 3971 | return nullptr; |
| 3972 | } |
| 3973 | |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 3974 | void Scop::addAccessData(MemoryAccess *Access) { |
| 3975 | const ScopArrayInfo *SAI = Access->getOriginalScopArrayInfo(); |
| 3976 | assert(SAI && "can only use after access relations have been constructed"); |
| 3977 | |
| 3978 | if (Access->isOriginalValueKind() && Access->isRead()) |
| 3979 | ValueUseAccs[SAI].push_back(Access); |
| 3980 | else if (Access->isOriginalAnyPHIKind() && Access->isWrite()) |
| 3981 | PHIIncomingAccs[SAI].push_back(Access); |
| 3982 | } |
| 3983 | |
| 3984 | void Scop::removeAccessData(MemoryAccess *Access) { |
Michael Kruse | 6d7a789 | 2017-09-21 14:23:11 +0000 | [diff] [blame] | 3985 | if (Access->isOriginalValueKind() && Access->isWrite()) { |
| 3986 | ValueDefAccs.erase(Access->getAccessValue()); |
| 3987 | } else if (Access->isOriginalValueKind() && Access->isRead()) { |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 3988 | auto &Uses = ValueUseAccs[Access->getScopArrayInfo()]; |
Michael Kruse | 7de6166 | 2018-04-09 23:13:01 +0000 | [diff] [blame] | 3989 | auto NewEnd = std::remove(Uses.begin(), Uses.end(), Access); |
| 3990 | Uses.erase(NewEnd, Uses.end()); |
Michael Kruse | 6d7a789 | 2017-09-21 14:23:11 +0000 | [diff] [blame] | 3991 | } else if (Access->isOriginalPHIKind() && Access->isRead()) { |
| 3992 | PHINode *PHI = cast<PHINode>(Access->getAccessInstruction()); |
| 3993 | PHIReadAccs.erase(PHI); |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 3994 | } else if (Access->isOriginalAnyPHIKind() && Access->isWrite()) { |
| 3995 | auto &Incomings = PHIIncomingAccs[Access->getScopArrayInfo()]; |
Michael Kruse | 7de6166 | 2018-04-09 23:13:01 +0000 | [diff] [blame] | 3996 | auto NewEnd = std::remove(Incomings.begin(), Incomings.end(), Access); |
| 3997 | Incomings.erase(NewEnd, Incomings.end()); |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 3998 | } |
| 3999 | } |
| 4000 | |
| 4001 | MemoryAccess *Scop::getValueDef(const ScopArrayInfo *SAI) const { |
| 4002 | assert(SAI->isValueKind()); |
| 4003 | |
| 4004 | Instruction *Val = dyn_cast<Instruction>(SAI->getBasePtr()); |
| 4005 | if (!Val) |
| 4006 | return nullptr; |
| 4007 | |
Michael Kruse | 6d7a789 | 2017-09-21 14:23:11 +0000 | [diff] [blame] | 4008 | return ValueDefAccs.lookup(Val); |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 4009 | } |
| 4010 | |
| 4011 | ArrayRef<MemoryAccess *> Scop::getValueUses(const ScopArrayInfo *SAI) const { |
| 4012 | assert(SAI->isValueKind()); |
| 4013 | auto It = ValueUseAccs.find(SAI); |
| 4014 | if (It == ValueUseAccs.end()) |
| 4015 | return {}; |
| 4016 | return It->second; |
| 4017 | } |
| 4018 | |
| 4019 | MemoryAccess *Scop::getPHIRead(const ScopArrayInfo *SAI) const { |
| 4020 | assert(SAI->isPHIKind() || SAI->isExitPHIKind()); |
| 4021 | |
| 4022 | if (SAI->isExitPHIKind()) |
| 4023 | return nullptr; |
| 4024 | |
| 4025 | PHINode *PHI = cast<PHINode>(SAI->getBasePtr()); |
Michael Kruse | 6d7a789 | 2017-09-21 14:23:11 +0000 | [diff] [blame] | 4026 | return PHIReadAccs.lookup(PHI); |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 4027 | } |
| 4028 | |
| 4029 | ArrayRef<MemoryAccess *> Scop::getPHIIncomings(const ScopArrayInfo *SAI) const { |
| 4030 | assert(SAI->isPHIKind() || SAI->isExitPHIKind()); |
| 4031 | auto It = PHIIncomingAccs.find(SAI); |
| 4032 | if (It == PHIIncomingAccs.end()) |
| 4033 | return {}; |
| 4034 | return It->second; |
| 4035 | } |
| 4036 | |
Michael Kruse | a508a4e | 2017-07-27 14:39:52 +0000 | [diff] [blame] | 4037 | bool Scop::isEscaping(Instruction *Inst) { |
| 4038 | assert(contains(Inst) && "The concept of escaping makes only sense for " |
| 4039 | "values defined inside the SCoP"); |
| 4040 | |
| 4041 | for (Use &Use : Inst->uses()) { |
| 4042 | BasicBlock *UserBB = getUseBlock(Use); |
| 4043 | if (!contains(UserBB)) |
| 4044 | return true; |
| 4045 | |
| 4046 | // When the SCoP region exit needs to be simplified, PHIs in the region exit |
| 4047 | // move to a new basic block such that its incoming blocks are not in the |
| 4048 | // SCoP anymore. |
| 4049 | if (hasSingleExitEdge() && isa<PHINode>(Use.getUser()) && |
| 4050 | isExit(cast<PHINode>(Use.getUser())->getParent())) |
| 4051 | return true; |
| 4052 | } |
| 4053 | return false; |
| 4054 | } |
| 4055 | |
Dominik Adamski | 588fc9e | 2019-07-16 21:10:45 +0000 | [diff] [blame^] | 4056 | void Scop::incrementNumberOfAliasingAssumptions(unsigned step) { |
| 4057 | AssumptionsAliasing += step; |
| 4058 | } |
| 4059 | |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 4060 | Scop::ScopStatistics Scop::getStatistics() const { |
| 4061 | ScopStatistics Result; |
| 4062 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS) |
| 4063 | auto LoopStat = ScopDetection::countBeneficialLoops(&R, *SE, *getLI(), 0); |
| 4064 | |
| 4065 | int NumTotalLoops = LoopStat.NumLoops; |
| 4066 | Result.NumBoxedLoops = getBoxedLoops().size(); |
| 4067 | Result.NumAffineLoops = NumTotalLoops - Result.NumBoxedLoops; |
| 4068 | |
| 4069 | for (const ScopStmt &Stmt : *this) { |
| 4070 | isl::set Domain = Stmt.getDomain().intersect_params(getContext()); |
| 4071 | bool IsInLoop = Stmt.getNumIterators() >= 1; |
| 4072 | for (MemoryAccess *MA : Stmt) { |
| 4073 | if (!MA->isWrite()) |
| 4074 | continue; |
| 4075 | |
| 4076 | if (MA->isLatestValueKind()) { |
| 4077 | Result.NumValueWrites += 1; |
| 4078 | if (IsInLoop) |
| 4079 | Result.NumValueWritesInLoops += 1; |
| 4080 | } |
| 4081 | |
| 4082 | if (MA->isLatestAnyPHIKind()) { |
| 4083 | Result.NumPHIWrites += 1; |
| 4084 | if (IsInLoop) |
| 4085 | Result.NumPHIWritesInLoops += 1; |
| 4086 | } |
| 4087 | |
| 4088 | isl::set AccSet = |
| 4089 | MA->getAccessRelation().intersect_domain(Domain).range(); |
| 4090 | if (AccSet.is_singleton()) { |
| 4091 | Result.NumSingletonWrites += 1; |
| 4092 | if (IsInLoop) |
| 4093 | Result.NumSingletonWritesInLoops += 1; |
| 4094 | } |
| 4095 | } |
| 4096 | } |
| 4097 | #endif |
| 4098 | return Result; |
| 4099 | } |
| 4100 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 4101 | raw_ostream &polly::operator<<(raw_ostream &OS, const Scop &scop) { |
| 4102 | scop.print(OS, PollyPrintInstructions); |
| 4103 | return OS; |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 4104 | } |
| 4105 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 4106 | //===----------------------------------------------------------------------===// |
| 4107 | void ScopInfoRegionPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 4108 | AU.addRequired<LoopInfoWrapperPass>(); |
| 4109 | AU.addRequired<RegionInfoPass>(); |
| 4110 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 4111 | AU.addRequiredTransitive<ScalarEvolutionWrapperPass>(); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 4112 | AU.addRequiredTransitive<ScopDetectionWrapperPass>(); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 4113 | AU.addRequired<AAResultsWrapperPass>(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 4114 | AU.addRequired<AssumptionCacheTracker>(); |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 4115 | AU.addRequired<OptimizationRemarkEmitterWrapperPass>(); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 4116 | AU.setPreservesAll(); |
| 4117 | } |
| 4118 | |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 4119 | void updateLoopCountStatistic(ScopDetection::LoopStats Stats, |
| 4120 | Scop::ScopStatistics ScopStats) { |
| 4121 | assert(Stats.NumLoops == ScopStats.NumAffineLoops + ScopStats.NumBoxedLoops); |
| 4122 | |
| 4123 | NumScops++; |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 4124 | NumLoopsInScop += Stats.NumLoops; |
| 4125 | MaxNumLoopsInScop = |
| 4126 | std::max(MaxNumLoopsInScop.getValue(), (unsigned)Stats.NumLoops); |
| 4127 | |
Tobias Grosser | fcc3ad5 | 2018-04-18 20:03:36 +0000 | [diff] [blame] | 4128 | if (Stats.MaxDepth == 0) |
| 4129 | NumScopsDepthZero++; |
| 4130 | else if (Stats.MaxDepth == 1) |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 4131 | NumScopsDepthOne++; |
| 4132 | else if (Stats.MaxDepth == 2) |
| 4133 | NumScopsDepthTwo++; |
| 4134 | else if (Stats.MaxDepth == 3) |
| 4135 | NumScopsDepthThree++; |
| 4136 | else if (Stats.MaxDepth == 4) |
| 4137 | NumScopsDepthFour++; |
| 4138 | else if (Stats.MaxDepth == 5) |
| 4139 | NumScopsDepthFive++; |
| 4140 | else |
| 4141 | NumScopsDepthLarger++; |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 4142 | |
| 4143 | NumAffineLoops += ScopStats.NumAffineLoops; |
| 4144 | NumBoxedLoops += ScopStats.NumBoxedLoops; |
| 4145 | |
| 4146 | NumValueWrites += ScopStats.NumValueWrites; |
| 4147 | NumValueWritesInLoops += ScopStats.NumValueWritesInLoops; |
| 4148 | NumPHIWrites += ScopStats.NumPHIWrites; |
| 4149 | NumPHIWritesInLoops += ScopStats.NumPHIWritesInLoops; |
| 4150 | NumSingletonWrites += ScopStats.NumSingletonWrites; |
| 4151 | NumSingletonWritesInLoops += ScopStats.NumSingletonWritesInLoops; |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 4152 | } |
| 4153 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 4154 | bool ScopInfoRegionPass::runOnRegion(Region *R, RGPassManager &RGM) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 4155 | auto &SD = getAnalysis<ScopDetectionWrapperPass>().getSD(); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 4156 | |
| 4157 | if (!SD.isMaxRegionInScop(*R)) |
| 4158 | return false; |
| 4159 | |
| 4160 | Function *F = R->getEntry()->getParent(); |
| 4161 | auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
| 4162 | auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 4163 | auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 4164 | auto const &DL = F->getParent()->getDataLayout(); |
| 4165 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 4166 | auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(*F); |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 4167 | auto &ORE = getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE(); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 4168 | |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 4169 | ScopBuilder SB(R, AC, AA, DL, DT, LI, SD, SE, ORE); |
Johannes Doerfert | b7e9713 | 2016-06-27 09:25:40 +0000 | [diff] [blame] | 4170 | S = SB.getScop(); // take ownership of scop object |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 4171 | |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 4172 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS) |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 4173 | if (S) { |
| 4174 | ScopDetection::LoopStats Stats = |
| 4175 | ScopDetection::countBeneficialLoops(&S->getRegion(), SE, LI, 0); |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 4176 | updateLoopCountStatistic(Stats, S->getStatistics()); |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 4177 | } |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 4178 | #endif |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 4179 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4180 | return false; |
| 4181 | } |
| 4182 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 4183 | void ScopInfoRegionPass::print(raw_ostream &OS, const Module *) const { |
Johannes Doerfert | b7e9713 | 2016-06-27 09:25:40 +0000 | [diff] [blame] | 4184 | if (S) |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 4185 | S->print(OS, PollyPrintInstructions); |
Johannes Doerfert | b7e9713 | 2016-06-27 09:25:40 +0000 | [diff] [blame] | 4186 | else |
| 4187 | OS << "Invalid Scop!\n"; |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 4188 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4189 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 4190 | char ScopInfoRegionPass::ID = 0; |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 4191 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 4192 | Pass *polly::createScopInfoRegionPassPass() { return new ScopInfoRegionPass(); } |
| 4193 | |
| 4194 | INITIALIZE_PASS_BEGIN(ScopInfoRegionPass, "polly-scops", |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 4195 | "Polly - Create polyhedral description of Scops", false, |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 4196 | false); |
Chandler Carruth | 66ef16b | 2015-09-09 22:13:56 +0000 | [diff] [blame] | 4197 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 4198 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker); |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 4199 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 4200 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); |
Tobias Grosser | c5bcf24 | 2015-08-17 10:57:08 +0000 | [diff] [blame] | 4201 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 4202 | INITIALIZE_PASS_DEPENDENCY(ScopDetectionWrapperPass); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 4203 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 4204 | INITIALIZE_PASS_END(ScopInfoRegionPass, "polly-scops", |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 4205 | "Polly - Create polyhedral description of Scops", false, |
| 4206 | false) |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 4207 | |
| 4208 | //===----------------------------------------------------------------------===// |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 4209 | ScopInfo::ScopInfo(const DataLayout &DL, ScopDetection &SD, ScalarEvolution &SE, |
| 4210 | LoopInfo &LI, AliasAnalysis &AA, DominatorTree &DT, |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 4211 | AssumptionCache &AC, OptimizationRemarkEmitter &ORE) |
| 4212 | : DL(DL), SD(SD), SE(SE), LI(LI), AA(AA), DT(DT), AC(AC), ORE(ORE) { |
Philip Pfaffe | f43e7c2 | 2017-08-10 07:43:46 +0000 | [diff] [blame] | 4213 | recompute(); |
| 4214 | } |
| 4215 | |
| 4216 | void ScopInfo::recompute() { |
| 4217 | RegionToScopMap.clear(); |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 4218 | /// Create polyhedral description of scops for all the valid regions of a |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 4219 | /// function. |
| 4220 | for (auto &It : SD) { |
| 4221 | Region *R = const_cast<Region *>(It); |
| 4222 | if (!SD.isMaxRegionInScop(*R)) |
| 4223 | continue; |
| 4224 | |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 4225 | ScopBuilder SB(R, AC, AA, DL, DT, LI, SD, SE, ORE); |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 4226 | std::unique_ptr<Scop> S = SB.getScop(); |
| 4227 | if (!S) |
| 4228 | continue; |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 4229 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS) |
Philip Pfaffe | ead67db | 2017-08-02 11:14:41 +0000 | [diff] [blame] | 4230 | ScopDetection::LoopStats Stats = |
| 4231 | ScopDetection::countBeneficialLoops(&S->getRegion(), SE, LI, 0); |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 4232 | updateLoopCountStatistic(Stats, S->getStatistics()); |
| 4233 | #endif |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 4234 | bool Inserted = RegionToScopMap.insert({R, std::move(S)}).second; |
| 4235 | assert(Inserted && "Building Scop for the same region twice!"); |
| 4236 | (void)Inserted; |
| 4237 | } |
| 4238 | } |
| 4239 | |
Philip Pfaffe | f43e7c2 | 2017-08-10 07:43:46 +0000 | [diff] [blame] | 4240 | bool ScopInfo::invalidate(Function &F, const PreservedAnalyses &PA, |
| 4241 | FunctionAnalysisManager::Invalidator &Inv) { |
| 4242 | // Check whether the analysis, all analyses on functions have been preserved |
| 4243 | // or anything we're holding references to is being invalidated |
| 4244 | auto PAC = PA.getChecker<ScopInfoAnalysis>(); |
| 4245 | return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) || |
| 4246 | Inv.invalidate<ScopAnalysis>(F, PA) || |
| 4247 | Inv.invalidate<ScalarEvolutionAnalysis>(F, PA) || |
| 4248 | Inv.invalidate<LoopAnalysis>(F, PA) || |
| 4249 | Inv.invalidate<AAManager>(F, PA) || |
| 4250 | Inv.invalidate<DominatorTreeAnalysis>(F, PA) || |
| 4251 | Inv.invalidate<AssumptionAnalysis>(F, PA); |
| 4252 | } |
| 4253 | |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 4254 | AnalysisKey ScopInfoAnalysis::Key; |
| 4255 | |
| 4256 | ScopInfoAnalysis::Result ScopInfoAnalysis::run(Function &F, |
| 4257 | FunctionAnalysisManager &FAM) { |
| 4258 | auto &SD = FAM.getResult<ScopAnalysis>(F); |
| 4259 | auto &SE = FAM.getResult<ScalarEvolutionAnalysis>(F); |
| 4260 | auto &LI = FAM.getResult<LoopAnalysis>(F); |
| 4261 | auto &AA = FAM.getResult<AAManager>(F); |
| 4262 | auto &DT = FAM.getResult<DominatorTreeAnalysis>(F); |
| 4263 | auto &AC = FAM.getResult<AssumptionAnalysis>(F); |
| 4264 | auto &DL = F.getParent()->getDataLayout(); |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 4265 | auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(F); |
| 4266 | return {DL, SD, SE, LI, AA, DT, AC, ORE}; |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 4267 | } |
| 4268 | |
| 4269 | PreservedAnalyses ScopInfoPrinterPass::run(Function &F, |
| 4270 | FunctionAnalysisManager &FAM) { |
| 4271 | auto &SI = FAM.getResult<ScopInfoAnalysis>(F); |
Philip Pfaffe | 96d2143 | 2017-08-04 11:28:51 +0000 | [diff] [blame] | 4272 | // Since the legacy PM processes Scops in bottom up, we print them in reverse |
| 4273 | // order here to keep the output persistent |
| 4274 | for (auto &It : reverse(SI)) { |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 4275 | if (It.second) |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 4276 | It.second->print(Stream, PollyPrintInstructions); |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 4277 | else |
| 4278 | Stream << "Invalid Scop!\n"; |
| 4279 | } |
| 4280 | return PreservedAnalyses::all(); |
| 4281 | } |
| 4282 | |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 4283 | void ScopInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 4284 | AU.addRequired<LoopInfoWrapperPass>(); |
| 4285 | AU.addRequired<RegionInfoPass>(); |
| 4286 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 4287 | AU.addRequiredTransitive<ScalarEvolutionWrapperPass>(); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 4288 | AU.addRequiredTransitive<ScopDetectionWrapperPass>(); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 4289 | AU.addRequired<AAResultsWrapperPass>(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 4290 | AU.addRequired<AssumptionCacheTracker>(); |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 4291 | AU.addRequired<OptimizationRemarkEmitterWrapperPass>(); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 4292 | AU.setPreservesAll(); |
| 4293 | } |
| 4294 | |
| 4295 | bool ScopInfoWrapperPass::runOnFunction(Function &F) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 4296 | auto &SD = getAnalysis<ScopDetectionWrapperPass>().getSD(); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 4297 | auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
| 4298 | auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 4299 | auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 4300 | auto const &DL = F.getParent()->getDataLayout(); |
| 4301 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 4302 | auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 4303 | auto &ORE = getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE(); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 4304 | |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 4305 | Result.reset(new ScopInfo{DL, SD, SE, LI, AA, DT, AC, ORE}); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 4306 | return false; |
| 4307 | } |
| 4308 | |
| 4309 | void ScopInfoWrapperPass::print(raw_ostream &OS, const Module *) const { |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 4310 | for (auto &It : *Result) { |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 4311 | if (It.second) |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 4312 | It.second->print(OS, PollyPrintInstructions); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 4313 | else |
| 4314 | OS << "Invalid Scop!\n"; |
| 4315 | } |
| 4316 | } |
| 4317 | |
| 4318 | char ScopInfoWrapperPass::ID = 0; |
| 4319 | |
| 4320 | Pass *polly::createScopInfoWrapperPassPass() { |
| 4321 | return new ScopInfoWrapperPass(); |
| 4322 | } |
| 4323 | |
| 4324 | INITIALIZE_PASS_BEGIN( |
| 4325 | ScopInfoWrapperPass, "polly-function-scops", |
| 4326 | "Polly - Create polyhedral description of all Scops of a function", false, |
| 4327 | false); |
| 4328 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 4329 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 4330 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); |
| 4331 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); |
| 4332 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 4333 | INITIALIZE_PASS_DEPENDENCY(ScopDetectionWrapperPass); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 4334 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); |
| 4335 | INITIALIZE_PASS_END( |
| 4336 | ScopInfoWrapperPass, "polly-function-scops", |
| 4337 | "Polly - Create polyhedral description of all Scops of a function", false, |
| 4338 | false) |