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 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Create a polyhedral description for a static control flow region. |
| 11 | // |
| 12 | // The pass creates a polyhedral description of the Scops detected by the Scop |
| 13 | // detection derived from their LLVM-IR code. |
| 14 | // |
Tobias Grosser | a5605d3 | 2014-10-29 19:58:28 +0000 | [diff] [blame] | 15 | // This representation is shared among several tools in the polyhedral |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 16 | // community, which are e.g. Cloog, Pluto, Loopo, Graphite. |
| 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Tobias Grosser | 5624d3c | 2015-12-21 12:38:56 +0000 | [diff] [blame] | 20 | #include "polly/ScopInfo.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 21 | #include "polly/LinkAllPasses.h" |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 22 | #include "polly/Options.h" |
Michael Kruse | 73fa33b | 2016-06-28 01:37:28 +0000 | [diff] [blame] | 23 | #include "polly/ScopBuilder.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 24 | #include "polly/ScopDetection.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 25 | #include "polly/Support/GICHelper.h" |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 26 | #include "polly/Support/ISLOStream.h" |
Michael Kruse | e330071 | 2018-05-09 16:23:56 +0000 | [diff] [blame] | 27 | #include "polly/Support/ISLTools.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 28 | #include "polly/Support/SCEVAffinator.h" |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 29 | #include "polly/Support/SCEVValidator.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 30 | #include "polly/Support/ScopHelper.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/APInt.h" |
| 32 | #include "llvm/ADT/ArrayRef.h" |
| 33 | #include "llvm/ADT/DenseMap.h" |
| 34 | #include "llvm/ADT/DenseSet.h" |
Tobias Grosser | c2bb0cb | 2015-09-25 09:49:19 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/PostOrderIterator.h" |
| 36 | #include "llvm/ADT/STLExtras.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/SetVector.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/SmallPtrSet.h" |
| 39 | #include "llvm/ADT/SmallSet.h" |
| 40 | #include "llvm/ADT/SmallVector.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 41 | #include "llvm/ADT/Statistic.h" |
Hongbin Zheng | 86a3774 | 2012-04-25 08:01:38 +0000 | [diff] [blame] | 42 | #include "llvm/ADT/StringExtras.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 43 | #include "llvm/ADT/StringMap.h" |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 44 | #include "llvm/Analysis/AliasAnalysis.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 45 | #include "llvm/Analysis/AliasSetTracker.h" |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 46 | #include "llvm/Analysis/AssumptionCache.h" |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 47 | #include "llvm/Analysis/Loads.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 48 | #include "llvm/Analysis/LoopInfo.h" |
Adam Nemet | e0f1541 | 2017-10-09 23:49:08 +0000 | [diff] [blame] | 49 | #include "llvm/Analysis/OptimizationRemarkEmitter.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 50 | #include "llvm/Analysis/RegionInfo.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 51 | #include "llvm/Analysis/RegionIterator.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 52 | #include "llvm/Analysis/ScalarEvolution.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 53 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 54 | #include "llvm/IR/Argument.h" |
| 55 | #include "llvm/IR/BasicBlock.h" |
| 56 | #include "llvm/IR/CFG.h" |
| 57 | #include "llvm/IR/ConstantRange.h" |
| 58 | #include "llvm/IR/Constants.h" |
| 59 | #include "llvm/IR/DataLayout.h" |
| 60 | #include "llvm/IR/DebugLoc.h" |
| 61 | #include "llvm/IR/DerivedTypes.h" |
Johannes Doerfert | 48fe86f | 2015-11-12 02:32:32 +0000 | [diff] [blame] | 62 | #include "llvm/IR/DiagnosticInfo.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 63 | #include "llvm/IR/Dominators.h" |
| 64 | #include "llvm/IR/Function.h" |
| 65 | #include "llvm/IR/InstrTypes.h" |
| 66 | #include "llvm/IR/Instruction.h" |
| 67 | #include "llvm/IR/Instructions.h" |
| 68 | #include "llvm/IR/IntrinsicInst.h" |
| 69 | #include "llvm/IR/Module.h" |
| 70 | #include "llvm/IR/PassManager.h" |
| 71 | #include "llvm/IR/Type.h" |
| 72 | #include "llvm/IR/Use.h" |
| 73 | #include "llvm/IR/User.h" |
| 74 | #include "llvm/IR/Value.h" |
| 75 | #include "llvm/Pass.h" |
| 76 | #include "llvm/Support/Casting.h" |
| 77 | #include "llvm/Support/CommandLine.h" |
| 78 | #include "llvm/Support/Compiler.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 79 | #include "llvm/Support/Debug.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 80 | #include "llvm/Support/ErrorHandling.h" |
| 81 | #include "llvm/Support/MathExtras.h" |
| 82 | #include "llvm/Support/raw_ostream.h" |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 83 | #include "isl/aff.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 84 | #include "isl/constraint.h" |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 85 | #include "isl/local_space.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 86 | #include "isl/map.h" |
Tobias Grosser | 4a8e356 | 2011-12-07 07:42:51 +0000 | [diff] [blame] | 87 | #include "isl/options.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 88 | #include "isl/printer.h" |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 89 | #include "isl/schedule.h" |
| 90 | #include "isl/schedule_node.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 91 | #include "isl/set.h" |
| 92 | #include "isl/union_map.h" |
Tobias Grosser | cd524dc | 2015-05-09 09:36:38 +0000 | [diff] [blame] | 93 | #include "isl/union_set.h" |
Tobias Grosser | edab135 | 2013-06-21 06:41:31 +0000 | [diff] [blame] | 94 | #include "isl/val.h" |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 95 | #include <algorithm> |
| 96 | #include <cassert> |
| 97 | #include <cstdlib> |
| 98 | #include <cstring> |
| 99 | #include <deque> |
| 100 | #include <iterator> |
| 101 | #include <memory> |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 102 | #include <string> |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 103 | #include <tuple> |
| 104 | #include <utility> |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 105 | #include <vector> |
| 106 | |
| 107 | using namespace llvm; |
| 108 | using namespace polly; |
| 109 | |
Chandler Carruth | 95fef94 | 2014-04-22 03:30:19 +0000 | [diff] [blame] | 110 | #define DEBUG_TYPE "polly-scops" |
| 111 | |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 112 | STATISTIC(AssumptionsAliasing, "Number of aliasing assumptions taken."); |
| 113 | STATISTIC(AssumptionsInbounds, "Number of inbounds assumptions taken."); |
| 114 | STATISTIC(AssumptionsWrapping, "Number of wrapping assumptions taken."); |
| 115 | STATISTIC(AssumptionsUnsigned, "Number of unsigned assumptions taken."); |
| 116 | STATISTIC(AssumptionsComplexity, "Number of too complex SCoPs."); |
| 117 | STATISTIC(AssumptionsUnprofitable, "Number of unprofitable SCoPs."); |
| 118 | STATISTIC(AssumptionsErrorBlock, "Number of error block assumptions taken."); |
| 119 | STATISTIC(AssumptionsInfiniteLoop, "Number of bounded loop assumptions taken."); |
| 120 | STATISTIC(AssumptionsInvariantLoad, |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 121 | "Number of invariant loads assumptions taken."); |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 122 | STATISTIC(AssumptionsDelinearization, |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 123 | "Number of delinearization assumptions taken."); |
| 124 | |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 125 | STATISTIC(NumScops, "Number of feasible SCoPs after ScopInfo"); |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 126 | STATISTIC(NumLoopsInScop, "Number of loops in scops"); |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 127 | STATISTIC(NumBoxedLoops, "Number of boxed loops in SCoPs after ScopInfo"); |
| 128 | STATISTIC(NumAffineLoops, "Number of affine loops in SCoPs after ScopInfo"); |
| 129 | |
Tobias Grosser | fcc3ad5 | 2018-04-18 20:03:36 +0000 | [diff] [blame] | 130 | STATISTIC(NumScopsDepthZero, "Number of scops with maximal loop depth 0"); |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 131 | STATISTIC(NumScopsDepthOne, "Number of scops with maximal loop depth 1"); |
| 132 | STATISTIC(NumScopsDepthTwo, "Number of scops with maximal loop depth 2"); |
| 133 | STATISTIC(NumScopsDepthThree, "Number of scops with maximal loop depth 3"); |
| 134 | STATISTIC(NumScopsDepthFour, "Number of scops with maximal loop depth 4"); |
| 135 | STATISTIC(NumScopsDepthFive, "Number of scops with maximal loop depth 5"); |
| 136 | STATISTIC(NumScopsDepthLarger, |
| 137 | "Number of scops with maximal loop depth 6 and larger"); |
| 138 | STATISTIC(MaxNumLoopsInScop, "Maximal number of loops in scops"); |
| 139 | |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 140 | STATISTIC(NumValueWrites, "Number of scalar value writes after ScopInfo"); |
| 141 | STATISTIC( |
| 142 | NumValueWritesInLoops, |
| 143 | "Number of scalar value writes nested in affine loops after ScopInfo"); |
| 144 | STATISTIC(NumPHIWrites, "Number of scalar phi writes after ScopInfo"); |
| 145 | STATISTIC(NumPHIWritesInLoops, |
| 146 | "Number of scalar phi writes nested in affine loops after ScopInfo"); |
| 147 | STATISTIC(NumSingletonWrites, "Number of singleton writes after ScopInfo"); |
| 148 | STATISTIC(NumSingletonWritesInLoops, |
| 149 | "Number of singleton writes nested in affine loops after ScopInfo"); |
| 150 | |
Tobias Grosser | 75dc40c | 2015-12-20 13:31:48 +0000 | [diff] [blame] | 151 | // The maximal number of basic sets we allow during domain construction to |
| 152 | // be created. More complex scops will result in very high compile time and |
| 153 | // are also unlikely to result in good code |
Tobias Grosser | 90411a9 | 2017-02-16 19:11:33 +0000 | [diff] [blame] | 154 | static int const MaxDisjunctsInDomain = 20; |
Tobias Grosser | 75dc40c | 2015-12-20 13:31:48 +0000 | [diff] [blame] | 155 | |
Tobias Grosser | c8a8276 | 2017-02-16 19:11:25 +0000 | [diff] [blame] | 156 | // The number of disjunct in the context after which we stop to add more |
| 157 | // disjuncts. This parameter is there to avoid exponential growth in the |
| 158 | // number of disjunct when adding non-convex sets to the context. |
| 159 | static int const MaxDisjunctsInContext = 4; |
| 160 | |
Tobias Grosser | 1eeedf4 | 2017-07-20 19:55:19 +0000 | [diff] [blame] | 161 | // The maximal number of dimensions we allow during invariant load construction. |
| 162 | // More complex access ranges will result in very high compile time and are also |
| 163 | // unlikely to result in good code. This value is very high and should only |
| 164 | // trigger for corner cases (e.g., the "dct_luma" function in h264, SPEC2006). |
| 165 | static int const MaxDimensionsInAccessRange = 9; |
| 166 | |
Tobias Grosser | 9771584 | 2017-05-19 04:01:52 +0000 | [diff] [blame] | 167 | static cl::opt<int> |
| 168 | OptComputeOut("polly-analysis-computeout", |
| 169 | cl::desc("Bound the scop analysis by a maximal amount of " |
| 170 | "computational steps (0 means no bound)"), |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 171 | cl::Hidden, cl::init(800000), cl::ZeroOrMore, |
Tobias Grosser | 9771584 | 2017-05-19 04:01:52 +0000 | [diff] [blame] | 172 | cl::cat(PollyCategory)); |
Tobias Grosser | 45e9fd1 | 2017-05-19 03:45:00 +0000 | [diff] [blame] | 173 | |
Johannes Doerfert | 2f70584 | 2016-04-12 16:09:44 +0000 | [diff] [blame] | 174 | static cl::opt<bool> PollyRemarksMinimal( |
| 175 | "polly-remarks-minimal", |
| 176 | cl::desc("Do not emit remarks about assumptions that are known"), |
| 177 | cl::Hidden, cl::ZeroOrMore, cl::init(false), cl::cat(PollyCategory)); |
| 178 | |
Tobias Grosser | 1b9d1bc | 2017-06-25 06:32:00 +0000 | [diff] [blame] | 179 | static cl::opt<int> RunTimeChecksMaxAccessDisjuncts( |
| 180 | "polly-rtc-max-array-disjuncts", |
| 181 | cl::desc("The maximal number of disjunts allowed in memory accesses to " |
| 182 | "to build RTCs."), |
| 183 | cl::Hidden, cl::ZeroOrMore, cl::init(8), cl::cat(PollyCategory)); |
| 184 | |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 185 | static cl::opt<unsigned> RunTimeChecksMaxParameters( |
| 186 | "polly-rtc-max-parameters", |
| 187 | cl::desc("The maximal number of parameters allowed in RTCs."), cl::Hidden, |
| 188 | cl::ZeroOrMore, cl::init(8), cl::cat(PollyCategory)); |
| 189 | |
Tobias Grosser | 7150072 | 2015-03-28 15:11:14 +0000 | [diff] [blame] | 190 | static cl::opt<unsigned> RunTimeChecksMaxArraysPerGroup( |
| 191 | "polly-rtc-max-arrays-per-group", |
| 192 | cl::desc("The maximal number of arrays to compare in each alias group."), |
| 193 | cl::Hidden, cl::ZeroOrMore, cl::init(20), cl::cat(PollyCategory)); |
Johannes Doerfert | 5210da5 | 2016-06-02 11:06:54 +0000 | [diff] [blame] | 194 | |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 195 | static cl::opt<std::string> UserContextStr( |
| 196 | "polly-context", cl::value_desc("isl parameter set"), |
| 197 | cl::desc("Provide additional constraints on the context parameters"), |
| 198 | cl::init(""), cl::cat(PollyCategory)); |
Tobias Grosser | 7150072 | 2015-03-28 15:11:14 +0000 | [diff] [blame] | 199 | |
Tobias Grosser | 2937b59 | 2016-04-29 11:43:20 +0000 | [diff] [blame] | 200 | static cl::opt<bool> |
| 201 | IslOnErrorAbort("polly-on-isl-error-abort", |
| 202 | cl::desc("Abort if an isl error is encountered"), |
| 203 | cl::init(true), cl::cat(PollyCategory)); |
| 204 | |
Tobias Grosser | d7c4975 | 2017-02-28 09:45:54 +0000 | [diff] [blame] | 205 | static cl::opt<bool> PollyPreciseInbounds( |
| 206 | "polly-precise-inbounds", |
| 207 | cl::desc("Take more precise inbounds assumptions (do not scale well)"), |
| 208 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
| 209 | |
Tobias Grosser | 8a6e605 | 2017-03-17 12:26:58 +0000 | [diff] [blame] | 210 | static cl::opt<bool> |
| 211 | PollyIgnoreInbounds("polly-ignore-inbounds", |
| 212 | cl::desc("Do not take inbounds assumptions at all"), |
| 213 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
| 214 | |
Tobias Grosser | 5842dee | 2017-03-17 13:00:53 +0000 | [diff] [blame] | 215 | static cl::opt<bool> PollyIgnoreParamBounds( |
| 216 | "polly-ignore-parameter-bounds", |
| 217 | cl::desc( |
| 218 | "Do not add parameter bounds and do no gist simplify sets accordingly"), |
| 219 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
| 220 | |
Siddharth Bhat | 7bc77e8 | 2017-08-21 11:57:04 +0000 | [diff] [blame] | 221 | static cl::opt<bool> PollyAllowDereferenceOfAllFunctionParams( |
| 222 | "polly-allow-dereference-of-all-function-parameters", |
| 223 | cl::desc( |
| 224 | "Treat all parameters to functions that are pointers as dereferencible." |
| 225 | " This is useful for invariant load hoisting, since we can generate" |
| 226 | " less runtime checks. This is only valid if all pointers to functions" |
| 227 | " are always initialized, so that Polly can choose to hoist" |
| 228 | " their loads. "), |
| 229 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
| 230 | |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 231 | static cl::opt<bool> PollyPreciseFoldAccesses( |
| 232 | "polly-precise-fold-accesses", |
Michael Kruse | 6e7854a | 2017-04-03 12:03:38 +0000 | [diff] [blame] | 233 | cl::desc("Fold memory accesses to model more possible delinearizations " |
| 234 | "(does not scale well)"), |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 235 | cl::Hidden, cl::init(false), cl::cat(PollyCategory)); |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 236 | |
Michael Kruse | 5ae08c0 | 2017-05-06 14:03:58 +0000 | [diff] [blame] | 237 | bool polly::UseInstructionNames; |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 238 | |
Michael Kruse | 5ae08c0 | 2017-05-06 14:03:58 +0000 | [diff] [blame] | 239 | static cl::opt<bool, true> XUseInstructionNames( |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 240 | "polly-use-llvm-names", |
Michael Kruse | 5ae08c0 | 2017-05-06 14:03:58 +0000 | [diff] [blame] | 241 | cl::desc("Use LLVM-IR names when deriving statement names"), |
| 242 | cl::location(UseInstructionNames), cl::Hidden, cl::init(false), |
| 243 | cl::ZeroOrMore, cl::cat(PollyCategory)); |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 244 | |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 245 | static cl::opt<bool> PollyPrintInstructions( |
| 246 | "polly-print-instructions", cl::desc("Output instructions per ScopStmt"), |
| 247 | cl::Hidden, cl::Optional, cl::init(false), cl::cat(PollyCategory)); |
| 248 | |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 249 | //===----------------------------------------------------------------------===// |
Michael Kruse | 7bf3944 | 2015-09-10 12:46:52 +0000 | [diff] [blame] | 250 | |
Michael Kruse | 046dde4 | 2015-08-10 13:01:57 +0000 | [diff] [blame] | 251 | // Create a sequence of two schedules. Either argument may be null and is |
| 252 | // interpreted as the empty schedule. Can also return null if both schedules are |
| 253 | // empty. |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 254 | static isl::schedule combineInSequence(isl::schedule Prev, isl::schedule Succ) { |
Michael Kruse | 046dde4 | 2015-08-10 13:01:57 +0000 | [diff] [blame] | 255 | if (!Prev) |
| 256 | return Succ; |
| 257 | if (!Succ) |
| 258 | return Prev; |
| 259 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 260 | return Prev.sequence(Succ); |
Michael Kruse | 046dde4 | 2015-08-10 13:01:57 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 263 | static isl::set addRangeBoundsToSet(isl::set S, const ConstantRange &Range, |
| 264 | int dim, isl::dim type) { |
| 265 | isl::val V; |
| 266 | isl::ctx Ctx = S.get_ctx(); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 267 | |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 268 | // The upper and lower bound for a parameter value is derived either from |
| 269 | // the data type of the parameter or from the - possibly more restrictive - |
| 270 | // range metadata. |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 271 | V = valFromAPInt(Ctx.get(), Range.getSignedMin(), true); |
| 272 | S = S.lower_bound_val(type, dim, V); |
| 273 | V = valFromAPInt(Ctx.get(), Range.getSignedMax(), true); |
| 274 | S = S.upper_bound_val(type, dim, V); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 275 | |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 276 | if (Range.isFullSet()) |
| 277 | return S; |
| 278 | |
Philip Pfaffe | 9375d57 | 2018-05-16 14:05:03 +0000 | [diff] [blame] | 279 | if (S.n_basic_set() > MaxDisjunctsInContext) |
Tobias Grosser | c8a8276 | 2017-02-16 19:11:25 +0000 | [diff] [blame] | 280 | return S; |
| 281 | |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 282 | // In case of signed wrapping, we can refine the set of valid values by |
| 283 | // excluding the part not covered by the wrapping range. |
| 284 | if (Range.isSignWrappedSet()) { |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 285 | V = valFromAPInt(Ctx.get(), Range.getLower(), true); |
| 286 | isl::set SLB = S.lower_bound_val(type, dim, V); |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 287 | |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 288 | V = valFromAPInt(Ctx.get(), Range.getUpper(), true); |
| 289 | V = V.sub_ui(1); |
| 290 | isl::set SUB = S.upper_bound_val(type, dim, V); |
| 291 | S = SLB.unite(SUB); |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 292 | } |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 293 | |
Tobias Grosser | 3281f60 | 2017-02-16 18:39:14 +0000 | [diff] [blame] | 294 | return S; |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 297 | static const ScopArrayInfo *identifyBasePtrOriginSAI(Scop *S, Value *BasePtr) { |
| 298 | LoadInst *BasePtrLI = dyn_cast<LoadInst>(BasePtr); |
| 299 | if (!BasePtrLI) |
| 300 | return nullptr; |
| 301 | |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 302 | if (!S->contains(BasePtrLI)) |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 303 | return nullptr; |
| 304 | |
| 305 | ScalarEvolution &SE = *S->getSE(); |
| 306 | |
| 307 | auto *OriginBaseSCEV = |
| 308 | SE.getPointerBase(SE.getSCEV(BasePtrLI->getPointerOperand())); |
| 309 | if (!OriginBaseSCEV) |
| 310 | return nullptr; |
| 311 | |
| 312 | auto *OriginBaseSCEVUnknown = dyn_cast<SCEVUnknown>(OriginBaseSCEV); |
| 313 | if (!OriginBaseSCEVUnknown) |
| 314 | return nullptr; |
| 315 | |
Tobias Grosser | 6abc75a | 2015-11-10 17:31:31 +0000 | [diff] [blame] | 316 | return S->getScopArrayInfo(OriginBaseSCEVUnknown->getValue(), |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 317 | MemoryKind::Array); |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Tobias Grosser | 27db02b | 2017-08-06 17:25:05 +0000 | [diff] [blame] | 320 | ScopArrayInfo::ScopArrayInfo(Value *BasePtr, Type *ElementType, isl::ctx Ctx, |
Hongbin Zheng | 6aded2a | 2017-01-15 16:47:26 +0000 | [diff] [blame] | 321 | ArrayRef<const SCEV *> Sizes, MemoryKind Kind, |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 322 | const DataLayout &DL, Scop *S, |
| 323 | const char *BaseName) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 324 | : BasePtr(BasePtr), ElementType(ElementType), Kind(Kind), DL(DL), S(*S) { |
Tobias Grosser | 9224522 | 2015-07-28 14:53:44 +0000 | [diff] [blame] | 325 | std::string BasePtrName = |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 326 | BaseName ? BaseName |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 327 | : getIslCompatibleName("MemRef", BasePtr, S->getNextArrayIdx(), |
| 328 | Kind == MemoryKind::PHI ? "__phi" : "", |
| 329 | UseInstructionNames); |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 330 | Id = isl::id::alloc(Ctx, BasePtrName, this); |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 331 | |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 332 | updateSizes(Sizes); |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 333 | |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 334 | if (!BasePtr || Kind != MemoryKind::Array) { |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 335 | BasePtrOriginSAI = nullptr; |
| 336 | return; |
| 337 | } |
| 338 | |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 339 | BasePtrOriginSAI = identifyBasePtrOriginSAI(S, BasePtr); |
| 340 | if (BasePtrOriginSAI) |
| 341 | const_cast<ScopArrayInfo *>(BasePtrOriginSAI)->addDerivedSAI(this); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 344 | ScopArrayInfo::~ScopArrayInfo() = default; |
| 345 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 346 | isl::space ScopArrayInfo::getSpace() const { |
| 347 | auto Space = isl::space(Id.get_ctx(), 0, getNumberOfDimensions()); |
| 348 | Space = Space.set_tuple_id(isl::dim::set, Id); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 349 | return Space; |
| 350 | } |
| 351 | |
Tobias Grosser | fe74a7a | 2016-09-17 19:22:18 +0000 | [diff] [blame] | 352 | bool ScopArrayInfo::isReadOnly() { |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 353 | isl::union_set WriteSet = S.getWrites().range(); |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 354 | isl::space Space = getSpace(); |
Tobias Grosser | 2ade986 | 2017-05-23 06:41:04 +0000 | [diff] [blame] | 355 | WriteSet = WriteSet.extract_set(Space); |
Tobias Grosser | fe74a7a | 2016-09-17 19:22:18 +0000 | [diff] [blame] | 356 | |
Tobias Grosser | 2ade986 | 2017-05-23 06:41:04 +0000 | [diff] [blame] | 357 | return bool(WriteSet.is_empty()); |
Tobias Grosser | fe74a7a | 2016-09-17 19:22:18 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 360 | bool ScopArrayInfo::isCompatibleWith(const ScopArrayInfo *Array) const { |
| 361 | if (Array->getElementType() != getElementType()) |
| 362 | return false; |
| 363 | |
| 364 | if (Array->getNumberOfDimensions() != getNumberOfDimensions()) |
| 365 | return false; |
| 366 | |
| 367 | for (unsigned i = 0; i < getNumberOfDimensions(); i++) |
| 368 | if (Array->getDimensionSize(i) != getDimensionSize(i)) |
| 369 | return false; |
| 370 | |
| 371 | return true; |
| 372 | } |
| 373 | |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 374 | void ScopArrayInfo::updateElementType(Type *NewElementType) { |
| 375 | if (NewElementType == ElementType) |
| 376 | return; |
| 377 | |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 378 | auto OldElementSize = DL.getTypeAllocSizeInBits(ElementType); |
| 379 | auto NewElementSize = DL.getTypeAllocSizeInBits(NewElementType); |
| 380 | |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 381 | if (NewElementSize == OldElementSize || NewElementSize == 0) |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 382 | return; |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 383 | |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 384 | if (NewElementSize % OldElementSize == 0 && NewElementSize < OldElementSize) { |
| 385 | ElementType = NewElementType; |
| 386 | } else { |
| 387 | auto GCD = GreatestCommonDivisor64(NewElementSize, OldElementSize); |
| 388 | ElementType = IntegerType::get(ElementType->getContext(), GCD); |
| 389 | } |
| 390 | } |
| 391 | |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 392 | /// Make the ScopArrayInfo model a Fortran Array |
| 393 | void ScopArrayInfo::applyAndSetFAD(Value *FAD) { |
| 394 | assert(FAD && "got invalid Fortran array descriptor"); |
| 395 | if (this->FAD) { |
| 396 | assert(this->FAD == FAD && |
| 397 | "receiving different array descriptors for same array"); |
| 398 | return; |
| 399 | } |
| 400 | |
| 401 | assert(DimensionSizesPw.size() > 0 && !DimensionSizesPw[0]); |
| 402 | assert(!this->FAD); |
| 403 | this->FAD = FAD; |
| 404 | |
Tobias Grosser | b1ed3d9 | 2017-05-23 07:07:05 +0000 | [diff] [blame] | 405 | isl::space Space(S.getIslCtx(), 1, 0); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 406 | |
| 407 | std::string param_name = getName(); |
| 408 | param_name += "_fortranarr_size"; |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 409 | isl::id IdPwAff = isl::id::alloc(S.getIslCtx(), param_name, this); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 410 | |
Tobias Grosser | b1ed3d9 | 2017-05-23 07:07:05 +0000 | [diff] [blame] | 411 | Space = Space.set_dim_id(isl::dim::param, 0, IdPwAff); |
| 412 | isl::pw_aff PwAff = |
| 413 | 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] | 414 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 415 | DimensionSizesPw[0] = PwAff; |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 418 | bool ScopArrayInfo::updateSizes(ArrayRef<const SCEV *> NewSizes, |
| 419 | bool CheckConsistency) { |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 420 | int SharedDims = std::min(NewSizes.size(), DimensionSizes.size()); |
| 421 | int ExtraDimsNew = NewSizes.size() - SharedDims; |
| 422 | int ExtraDimsOld = DimensionSizes.size() - SharedDims; |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 423 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 424 | if (CheckConsistency) { |
| 425 | for (int i = 0; i < SharedDims; i++) { |
| 426 | auto *NewSize = NewSizes[i + ExtraDimsNew]; |
| 427 | auto *KnownSize = DimensionSizes[i + ExtraDimsOld]; |
| 428 | if (NewSize && KnownSize && NewSize != KnownSize) |
| 429 | return false; |
| 430 | } |
Tobias Grosser | 8286b83 | 2015-11-02 11:29:32 +0000 | [diff] [blame] | 431 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 432 | if (DimensionSizes.size() >= NewSizes.size()) |
| 433 | return true; |
| 434 | } |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 435 | |
| 436 | DimensionSizes.clear(); |
| 437 | DimensionSizes.insert(DimensionSizes.begin(), NewSizes.begin(), |
| 438 | NewSizes.end()); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 439 | DimensionSizesPw.clear(); |
| 440 | for (const SCEV *Expr : DimensionSizes) { |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 441 | if (!Expr) { |
| 442 | DimensionSizesPw.push_back(nullptr); |
| 443 | continue; |
| 444 | } |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 445 | isl::pw_aff Size = S.getPwAffOnly(Expr); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 446 | DimensionSizesPw.push_back(Size); |
| 447 | } |
Tobias Grosser | 8286b83 | 2015-11-02 11:29:32 +0000 | [diff] [blame] | 448 | return true; |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 451 | std::string ScopArrayInfo::getName() const { return Id.get_name(); } |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 452 | |
| 453 | int ScopArrayInfo::getElemSizeInBytes() const { |
Johannes Doerfert | 55b3d8b | 2015-11-12 20:15:08 +0000 | [diff] [blame] | 454 | return DL.getTypeAllocSize(ElementType); |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 457 | isl::id ScopArrayInfo::getBasePtrId() const { return Id; } |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 458 | |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 459 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Michael Kruse | e186013 | 2017-07-21 15:54:13 +0000 | [diff] [blame] | 460 | LLVM_DUMP_METHOD void ScopArrayInfo::dump() const { print(errs()); } |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 461 | #endif |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 462 | |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 463 | void ScopArrayInfo::print(raw_ostream &OS, bool SizeAsPwAff) const { |
Tobias Grosser | 4ea2e07 | 2015-11-10 14:02:54 +0000 | [diff] [blame] | 464 | OS.indent(8) << *getElementType() << " " << getName(); |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 465 | unsigned u = 0; |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 466 | // If this is a Fortran array, then we can print the outermost dimension |
| 467 | // as a isl_pw_aff even though there is no SCEV information. |
| 468 | bool IsOutermostSizeKnown = SizeAsPwAff && FAD; |
| 469 | |
| 470 | if (!IsOutermostSizeKnown && getNumberOfDimensions() > 0 && |
| 471 | !getDimensionSize(0)) { |
Tobias Grosser | 4ea2e07 | 2015-11-10 14:02:54 +0000 | [diff] [blame] | 472 | OS << "[*]"; |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 473 | u++; |
| 474 | } |
| 475 | for (; u < getNumberOfDimensions(); u++) { |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 476 | OS << "["; |
| 477 | |
Tobias Grosser | 2625384 | 2015-11-10 14:24:21 +0000 | [diff] [blame] | 478 | if (SizeAsPwAff) { |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 479 | isl::pw_aff Size = getDimensionSizePw(u); |
Tobias Grosser | 2625384 | 2015-11-10 14:24:21 +0000 | [diff] [blame] | 480 | OS << " " << Size << " "; |
Tobias Grosser | 2625384 | 2015-11-10 14:24:21 +0000 | [diff] [blame] | 481 | } else { |
| 482 | OS << *getDimensionSize(u); |
| 483 | } |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 484 | |
| 485 | OS << "]"; |
| 486 | } |
| 487 | |
Tobias Grosser | 4ea2e07 | 2015-11-10 14:02:54 +0000 | [diff] [blame] | 488 | OS << ";"; |
| 489 | |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 490 | if (BasePtrOriginSAI) |
| 491 | OS << " [BasePtrOrigin: " << BasePtrOriginSAI->getName() << "]"; |
| 492 | |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 493 | OS << " // Element size " << getElemSizeInBytes() << "\n"; |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | const ScopArrayInfo * |
Tobias Grosser | 206e9e3 | 2017-07-24 16:22:27 +0000 | [diff] [blame] | 497 | ScopArrayInfo::getFromAccessFunction(isl::pw_multi_aff PMA) { |
| 498 | isl::id Id = PMA.get_tuple_id(isl::dim::out); |
| 499 | assert(!Id.is_null() && "Output dimension didn't have an ID"); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 500 | return getFromId(Id); |
| 501 | } |
| 502 | |
Tobias Grosser | 206e9e3 | 2017-07-24 16:22:27 +0000 | [diff] [blame] | 503 | const ScopArrayInfo *ScopArrayInfo::getFromId(isl::id Id) { |
| 504 | void *User = Id.get_user(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 505 | const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 506 | return SAI; |
| 507 | } |
| 508 | |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 509 | void MemoryAccess::wrapConstantDimensions() { |
| 510 | auto *SAI = getScopArrayInfo(); |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 511 | isl::space ArraySpace = SAI->getSpace(); |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 512 | isl::ctx Ctx = ArraySpace.get_ctx(); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 513 | unsigned DimsArray = SAI->getNumberOfDimensions(); |
| 514 | |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 515 | isl::multi_aff DivModAff = isl::multi_aff::identity( |
| 516 | ArraySpace.map_from_domain_and_range(ArraySpace)); |
| 517 | isl::local_space LArraySpace = isl::local_space(ArraySpace); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 518 | |
| 519 | // Begin with last dimension, to iteratively carry into higher dimensions. |
| 520 | for (int i = DimsArray - 1; i > 0; i--) { |
| 521 | auto *DimSize = SAI->getDimensionSize(i); |
| 522 | auto *DimSizeCst = dyn_cast<SCEVConstant>(DimSize); |
| 523 | |
| 524 | // This transformation is not applicable to dimensions with dynamic size. |
| 525 | if (!DimSizeCst) |
| 526 | continue; |
| 527 | |
Tobias Grosser | ca2cfd0 | 2017-02-17 04:48:52 +0000 | [diff] [blame] | 528 | // This transformation is not applicable to dimensions of size zero. |
| 529 | if (DimSize->isZero()) |
| 530 | continue; |
| 531 | |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 532 | isl::val DimSizeVal = |
| 533 | valFromAPInt(Ctx.get(), DimSizeCst->getAPInt(), false); |
| 534 | isl::aff Var = isl::aff::var_on_domain(LArraySpace, isl::dim::set, i); |
| 535 | isl::aff PrevVar = |
| 536 | isl::aff::var_on_domain(LArraySpace, isl::dim::set, i - 1); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 537 | |
| 538 | // Compute: index % size |
| 539 | // Modulo must apply in the divide of the previous iteration, if any. |
Tobias Grosser | cb0224a | 2017-08-06 15:56:45 +0000 | [diff] [blame] | 540 | isl::aff Modulo = Var.mod(DimSizeVal); |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 541 | Modulo = Modulo.pullback(DivModAff); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 542 | |
| 543 | // Compute: floor(index / size) |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 544 | isl::aff Divide = Var.div(isl::aff(LArraySpace, DimSizeVal)); |
| 545 | Divide = Divide.floor(); |
| 546 | Divide = Divide.add(PrevVar); |
| 547 | Divide = Divide.pullback(DivModAff); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 548 | |
| 549 | // Apply Modulo and Divide. |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 550 | DivModAff = DivModAff.set_aff(i, Modulo); |
| 551 | DivModAff = DivModAff.set_aff(i - 1, Divide); |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | // Apply all modulo/divides on the accesses. |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 555 | isl::map Relation = AccessRelation; |
Tobias Grosser | 3137f2c | 2017-05-21 20:23:23 +0000 | [diff] [blame] | 556 | Relation = Relation.apply_range(isl::map::from_multi_aff(DivModAff)); |
| 557 | Relation = Relation.detect_equalities(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 558 | AccessRelation = Relation; |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 561 | void MemoryAccess::updateDimensionality() { |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 562 | auto *SAI = getScopArrayInfo(); |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 563 | isl::space ArraySpace = SAI->getSpace(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 564 | isl::space AccessSpace = AccessRelation.get_space().range(); |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 565 | isl::ctx Ctx = ArraySpace.get_ctx(); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 566 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 567 | auto DimsArray = ArraySpace.dim(isl::dim::set); |
| 568 | auto DimsAccess = AccessSpace.dim(isl::dim::set); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 569 | auto DimsMissing = DimsArray - DimsAccess; |
| 570 | |
Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 571 | auto *BB = getStatement()->getEntryBlock(); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 572 | auto &DL = BB->getModule()->getDataLayout(); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 573 | unsigned ArrayElemSize = SAI->getElemSizeInBytes(); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 574 | unsigned ElemBytes = DL.getTypeAllocSize(getElementType()); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 575 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 576 | isl::map Map = isl::map::from_domain_and_range( |
| 577 | isl::set::universe(AccessSpace), isl::set::universe(ArraySpace)); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 578 | |
| 579 | for (unsigned i = 0; i < DimsMissing; i++) |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 580 | Map = Map.fix_si(isl::dim::out, i, 0); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 581 | |
| 582 | for (unsigned i = DimsMissing; i < DimsArray; i++) |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 583 | Map = Map.equate(isl::dim::in, i - DimsMissing, isl::dim::out, i); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 584 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 585 | AccessRelation = AccessRelation.apply_range(Map); |
Roman Gareev | 10595a1 | 2016-01-08 14:01:59 +0000 | [diff] [blame] | 586 | |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 587 | // For the non delinearized arrays, divide the access function of the last |
| 588 | // subscript by the size of the elements in the array. |
| 589 | // |
| 590 | // A stride one array access in C expressed as A[i] is expressed in |
| 591 | // LLVM-IR as something like A[i * elementsize]. This hides the fact that |
| 592 | // two subsequent values of 'i' index two values that are stored next to |
| 593 | // each other in memory. By this division we make this characteristic |
| 594 | // obvious again. If the base pointer was accessed with offsets not divisible |
Tobias Grosser | 2219d15 | 2016-08-03 05:28:09 +0000 | [diff] [blame] | 595 | // by the accesses element size, we will have chosen a smaller ArrayElemSize |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 596 | // that divides the offsets of all accesses to this base pointer. |
| 597 | if (DimsAccess == 1) { |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 598 | isl::val V = isl::val(Ctx, ArrayElemSize); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 599 | AccessRelation = AccessRelation.floordiv_val(V); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Michael Kruse | 3b425ff | 2016-04-11 14:34:08 +0000 | [diff] [blame] | 602 | // We currently do this only if we added at least one dimension, which means |
| 603 | // some dimension's indices have not been specified, an indicator that some |
| 604 | // index values have been added together. |
| 605 | // TODO: Investigate general usefulness; Effect on unit tests is to make index |
| 606 | // expressions more complicated. |
| 607 | if (DimsMissing) |
| 608 | wrapConstantDimensions(); |
| 609 | |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 610 | if (!isAffine()) |
| 611 | computeBoundsOnAccessRelation(ArrayElemSize); |
| 612 | |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 613 | // Introduce multi-element accesses in case the type loaded by this memory |
| 614 | // access is larger than the canonical element type of the array. |
| 615 | // |
| 616 | // An access ((float *)A)[i] to an array char *A is modeled as |
| 617 | // {[i] -> A[o] : 4 i <= o <= 4 i + 3 |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 618 | if (ElemBytes > ArrayElemSize) { |
| 619 | assert(ElemBytes % ArrayElemSize == 0 && |
| 620 | "Loaded element size should be multiple of canonical element size"); |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 621 | isl::map Map = isl::map::from_domain_and_range( |
| 622 | isl::set::universe(ArraySpace), isl::set::universe(ArraySpace)); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 623 | for (unsigned i = 0; i < DimsArray - 1; i++) |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 624 | Map = Map.equate(isl::dim::in, i, isl::dim::out, i); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 625 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 626 | isl::constraint C; |
| 627 | isl::local_space LS; |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 628 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 629 | LS = isl::local_space(Map.get_space()); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 630 | int Num = ElemBytes / getScopArrayInfo()->getElemSizeInBytes(); |
| 631 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 632 | C = isl::constraint::alloc_inequality(LS); |
| 633 | C = C.set_constant_val(isl::val(Ctx, Num - 1)); |
| 634 | C = C.set_coefficient_si(isl::dim::in, DimsArray - 1, 1); |
| 635 | C = C.set_coefficient_si(isl::dim::out, DimsArray - 1, -1); |
| 636 | Map = Map.add_constraint(C); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 637 | |
Tobias Grosser | 7be8245 | 2017-05-21 20:38:33 +0000 | [diff] [blame] | 638 | C = isl::constraint::alloc_inequality(LS); |
| 639 | C = C.set_coefficient_si(isl::dim::in, DimsArray - 1, -1); |
| 640 | C = C.set_coefficient_si(isl::dim::out, DimsArray - 1, 1); |
| 641 | C = C.set_constant_val(isl::val(Ctx, 0)); |
| 642 | Map = Map.add_constraint(C); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 643 | AccessRelation = AccessRelation.apply_range(Map); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 644 | } |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 647 | const std::string |
| 648 | MemoryAccess::getReductionOperatorStr(MemoryAccess::ReductionType RT) { |
| 649 | switch (RT) { |
| 650 | case MemoryAccess::RT_NONE: |
| 651 | llvm_unreachable("Requested a reduction operator string for a memory " |
| 652 | "access which isn't a reduction"); |
| 653 | case MemoryAccess::RT_ADD: |
| 654 | return "+"; |
| 655 | case MemoryAccess::RT_MUL: |
| 656 | return "*"; |
| 657 | case MemoryAccess::RT_BOR: |
| 658 | return "|"; |
| 659 | case MemoryAccess::RT_BXOR: |
| 660 | return "^"; |
| 661 | case MemoryAccess::RT_BAND: |
| 662 | return "&"; |
| 663 | } |
| 664 | llvm_unreachable("Unknown reduction type"); |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 667 | const ScopArrayInfo *MemoryAccess::getOriginalScopArrayInfo() const { |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 668 | isl::id ArrayId = getArrayId(); |
| 669 | void *User = ArrayId.get_user(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 670 | const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 671 | return SAI; |
| 672 | } |
| 673 | |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 674 | const ScopArrayInfo *MemoryAccess::getLatestScopArrayInfo() const { |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 675 | isl::id ArrayId = getLatestArrayId(); |
| 676 | void *User = ArrayId.get_user(); |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 677 | const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 678 | return SAI; |
| 679 | } |
| 680 | |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 681 | isl::id MemoryAccess::getOriginalArrayId() const { |
| 682 | return AccessRelation.get_tuple_id(isl::dim::out); |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 685 | isl::id MemoryAccess::getLatestArrayId() const { |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 686 | if (!hasNewAccessRelation()) |
| 687 | return getOriginalArrayId(); |
Tobias Grosser | 1959dbd | 2017-07-23 04:08:59 +0000 | [diff] [blame] | 688 | return NewAccessRelation.get_tuple_id(isl::dim::out); |
Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Tobias Grosser | 6a87036 | 2017-07-23 04:08:45 +0000 | [diff] [blame] | 691 | isl::map MemoryAccess::getAddressFunction() const { |
| 692 | return getAccessRelation().lexmin(); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Tobias Grosser | 3b19613 | 2017-07-23 04:08:52 +0000 | [diff] [blame] | 695 | isl::pw_multi_aff |
| 696 | MemoryAccess::applyScheduleToAccessRelation(isl::union_map USchedule) const { |
| 697 | isl::map Schedule, ScheduledAccRel; |
| 698 | isl::union_set UDomain; |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 699 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 700 | UDomain = getStatement()->getDomain(); |
Tobias Grosser | 3b19613 | 2017-07-23 04:08:52 +0000 | [diff] [blame] | 701 | USchedule = USchedule.intersect_domain(UDomain); |
| 702 | Schedule = isl::map::from_union_map(USchedule); |
| 703 | ScheduledAccRel = getAddressFunction().apply_domain(Schedule); |
| 704 | return isl::pw_multi_aff::from_map(ScheduledAccRel); |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Tobias Grosser | 22da5f0 | 2017-07-23 04:08:27 +0000 | [diff] [blame] | 707 | isl::map MemoryAccess::getOriginalAccessRelation() const { |
| 708 | return AccessRelation; |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 711 | std::string MemoryAccess::getOriginalAccessRelationStr() const { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 712 | return AccessRelation.to_str(); |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Tobias Grosser | 22da5f0 | 2017-07-23 04:08:27 +0000 | [diff] [blame] | 715 | isl::space MemoryAccess::getOriginalAccessRelationSpace() const { |
| 716 | return AccessRelation.get_space(); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 719 | isl::map MemoryAccess::getNewAccessRelation() const { |
| 720 | return NewAccessRelation; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Tobias Grosser | 6f73008 | 2015-09-05 07:46:47 +0000 | [diff] [blame] | 723 | std::string MemoryAccess::getNewAccessRelationStr() const { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 724 | return NewAccessRelation.to_str(); |
Tobias Grosser | 6f73008 | 2015-09-05 07:46:47 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Tobias Grosser | 6a4c12f | 2017-07-11 10:10:13 +0000 | [diff] [blame] | 727 | std::string MemoryAccess::getAccessRelationStr() const { |
Tobias Grosser | 2b7479b | 2017-08-06 11:41:10 +0000 | [diff] [blame] | 728 | return getAccessRelation().to_str(); |
Tobias Grosser | 6a4c12f | 2017-07-11 10:10:13 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Tobias Grosser | b6e7a85 | 2017-07-23 04:08:17 +0000 | [diff] [blame] | 731 | isl::basic_map MemoryAccess::createBasicAccessMap(ScopStmt *Statement) { |
| 732 | isl::space Space = isl::space(Statement->getIslCtx(), 0, 1); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 733 | Space = Space.align_params(Statement->getDomainSpace()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 734 | |
Tobias Grosser | b6e7a85 | 2017-07-23 04:08:17 +0000 | [diff] [blame] | 735 | return isl::basic_map::from_domain_and_range( |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 736 | isl::basic_set::universe(Statement->getDomainSpace()), |
Tobias Grosser | b6e7a85 | 2017-07-23 04:08:17 +0000 | [diff] [blame] | 737 | isl::basic_set::universe(Space)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 740 | // Formalize no out-of-bound access assumption |
| 741 | // |
| 742 | // When delinearizing array accesses we optimistically assume that the |
| 743 | // delinearized accesses do not access out of bound locations (the subscript |
| 744 | // expression of each array evaluates for each statement instance that is |
| 745 | // executed to a value that is larger than zero and strictly smaller than the |
| 746 | // size of the corresponding dimension). The only exception is the outermost |
Tobias Grosser | f57d63f | 2014-08-03 21:07:30 +0000 | [diff] [blame] | 747 | // dimension for which we do not need to assume any upper bound. At this point |
| 748 | // we formalize this assumption to ensure that at code generation time the |
| 749 | // relevant run-time checks can be generated. |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 750 | // |
| 751 | // To find the set of constraints necessary to avoid out of bound accesses, we |
| 752 | // first build the set of data locations that are not within array bounds. We |
| 753 | // then apply the reverse access relation to obtain the set of iterations that |
| 754 | // may contain invalid accesses and reduce this set of iterations to the ones |
| 755 | // that are actually executed by intersecting them with the domain of the |
| 756 | // statement. If we now project out all loop dimensions, we obtain a set of |
| 757 | // parameters that may cause statement instances to be executed that may |
| 758 | // possibly yield out of bound memory accesses. The complement of these |
| 759 | // constraints is the set of constraints that needs to be assumed to ensure such |
| 760 | // statement instances are never executed. |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 761 | void MemoryAccess::assumeNoOutOfBound() { |
Tobias Grosser | 8a6e605 | 2017-03-17 12:26:58 +0000 | [diff] [blame] | 762 | if (PollyIgnoreInbounds) |
| 763 | return; |
Johannes Doerfert | adeab37 | 2016-02-07 13:57:32 +0000 | [diff] [blame] | 764 | auto *SAI = getScopArrayInfo(); |
Tobias Grosser | 22da5f0 | 2017-07-23 04:08:27 +0000 | [diff] [blame] | 765 | isl::space Space = getOriginalAccessRelationSpace().range(); |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 766 | isl::set Outside = isl::set::empty(Space); |
| 767 | for (int i = 1, Size = Space.dim(isl::dim::set); i < Size; ++i) { |
| 768 | isl::local_space LS(Space); |
| 769 | isl::pw_aff Var = isl::pw_aff::var_on_domain(LS, isl::dim::set, i); |
| 770 | isl::pw_aff Zero = isl::pw_aff(LS); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 771 | |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 772 | isl::set DimOutside = Var.lt_set(Zero); |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 773 | isl::pw_aff SizeE = SAI->getDimensionSizePw(i); |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 774 | SizeE = SizeE.add_dims(isl::dim::in, Space.dim(isl::dim::set)); |
| 775 | SizeE = SizeE.set_tuple_id(isl::dim::in, Space.get_tuple_id(isl::dim::set)); |
| 776 | DimOutside = DimOutside.unite(SizeE.le_set(Var)); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 777 | |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 778 | Outside = Outside.unite(DimOutside); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 781 | Outside = Outside.apply(getAccessRelation().reverse()); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 782 | Outside = Outside.intersect(Statement->getDomain()); |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 783 | Outside = Outside.params(); |
Tobias Grosser | f54bb77 | 2015-06-26 12:09:28 +0000 | [diff] [blame] | 784 | |
| 785 | // Remove divs to avoid the construction of overly complicated assumptions. |
| 786 | // Doing so increases the set of parameter combinations that are assumed to |
| 787 | // not appear. This is always save, but may make the resulting run-time check |
| 788 | // bail out more often than strictly necessary. |
Tobias Grosser | 1e2edaf | 2017-05-23 07:07:07 +0000 | [diff] [blame] | 789 | Outside = Outside.remove_divs(); |
| 790 | Outside = Outside.complement(); |
Michael Kruse | 7071e8b | 2016-04-11 13:24:29 +0000 | [diff] [blame] | 791 | const auto &Loc = getAccessInstruction() |
| 792 | ? getAccessInstruction()->getDebugLoc() |
| 793 | : DebugLoc(); |
Tobias Grosser | d7c4975 | 2017-02-28 09:45:54 +0000 | [diff] [blame] | 794 | if (!PollyPreciseInbounds) |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 795 | Outside = Outside.gist_params(Statement->getDomain().params()); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 796 | Statement->getParent()->recordAssumption(INBOUNDS, Outside, Loc, |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 797 | AS_ASSUMPTION); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 800 | void MemoryAccess::buildMemIntrinsicAccessRelation() { |
Johannes Doerfert | c976546 | 2016-11-17 22:11:56 +0000 | [diff] [blame] | 801 | assert(isMemoryIntrinsic()); |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 802 | assert(Subscripts.size() == 2 && Sizes.size() == 1); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 803 | |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 804 | isl::pw_aff SubscriptPWA = getPwAff(Subscripts[0]); |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 805 | isl::map SubscriptMap = isl::map::from_pw_aff(SubscriptPWA); |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 806 | |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 807 | isl::map LengthMap; |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 808 | if (Subscripts[1] == nullptr) { |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 809 | LengthMap = isl::map::universe(SubscriptMap.get_space()); |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 810 | } else { |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 811 | isl::pw_aff LengthPWA = getPwAff(Subscripts[1]); |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 812 | LengthMap = isl::map::from_pw_aff(LengthPWA); |
| 813 | isl::space RangeSpace = LengthMap.get_space().range(); |
| 814 | LengthMap = LengthMap.apply_range(isl::map::lex_gt(RangeSpace)); |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 815 | } |
Tobias Grosser | 53fc355 | 2017-05-23 07:07:09 +0000 | [diff] [blame] | 816 | LengthMap = LengthMap.lower_bound_si(isl::dim::out, 0, 0); |
| 817 | LengthMap = LengthMap.align_params(SubscriptMap.get_space()); |
| 818 | SubscriptMap = SubscriptMap.align_params(LengthMap.get_space()); |
| 819 | LengthMap = LengthMap.sum(SubscriptMap); |
| 820 | AccessRelation = |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 821 | LengthMap.set_tuple_id(isl::dim::in, getStatement()->getDomainId()); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 824 | void MemoryAccess::computeBoundsOnAccessRelation(unsigned ElementSize) { |
| 825 | ScalarEvolution *SE = Statement->getParent()->getSE(); |
| 826 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 827 | auto MAI = MemAccInst(getAccessInstruction()); |
Hongbin Zheng | 8efb22e | 2016-02-27 01:49:58 +0000 | [diff] [blame] | 828 | if (isa<MemIntrinsic>(MAI)) |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 829 | return; |
| 830 | |
| 831 | Value *Ptr = MAI.getPointerOperand(); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 832 | if (!Ptr || !SE->isSCEVable(Ptr->getType())) |
| 833 | return; |
| 834 | |
| 835 | auto *PtrSCEV = SE->getSCEV(Ptr); |
| 836 | if (isa<SCEVCouldNotCompute>(PtrSCEV)) |
| 837 | return; |
| 838 | |
| 839 | auto *BasePtrSCEV = SE->getPointerBase(PtrSCEV); |
| 840 | if (BasePtrSCEV && !isa<SCEVCouldNotCompute>(BasePtrSCEV)) |
| 841 | PtrSCEV = SE->getMinusSCEV(PtrSCEV, BasePtrSCEV); |
| 842 | |
| 843 | const ConstantRange &Range = SE->getSignedRange(PtrSCEV); |
| 844 | if (Range.isFullSet()) |
| 845 | return; |
| 846 | |
Michael Kruse | 960c0d0 | 2017-05-18 21:55:36 +0000 | [diff] [blame] | 847 | if (Range.isWrappedSet() || Range.isSignWrappedSet()) |
Tobias Grosser | b3a8588 | 2017-02-12 08:11:12 +0000 | [diff] [blame] | 848 | return; |
| 849 | |
Johannes Doerfert | e4bd53b | 2015-03-08 19:49:50 +0000 | [diff] [blame] | 850 | bool isWrapping = Range.isSignWrappedSet(); |
Tobias Grosser | b3a8588 | 2017-02-12 08:11:12 +0000 | [diff] [blame] | 851 | |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 852 | unsigned BW = Range.getBitWidth(); |
Johannes Doerfert | e708790 | 2016-02-07 13:59:03 +0000 | [diff] [blame] | 853 | const auto One = APInt(BW, 1); |
Johannes Doerfert | e4bd53b | 2015-03-08 19:49:50 +0000 | [diff] [blame] | 854 | const auto LB = isWrapping ? Range.getLower() : Range.getSignedMin(); |
Johannes Doerfert | e708790 | 2016-02-07 13:59:03 +0000 | [diff] [blame] | 855 | const auto UB = isWrapping ? (Range.getUpper() - One) : Range.getSignedMax(); |
Johannes Doerfert | e4bd53b | 2015-03-08 19:49:50 +0000 | [diff] [blame] | 856 | |
| 857 | auto Min = LB.sdiv(APInt(BW, ElementSize)); |
Johannes Doerfert | e708790 | 2016-02-07 13:59:03 +0000 | [diff] [blame] | 858 | auto Max = UB.sdiv(APInt(BW, ElementSize)) + One; |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 859 | |
Tobias Grosser | b3a8588 | 2017-02-12 08:11:12 +0000 | [diff] [blame] | 860 | assert(Min.sle(Max) && "Minimum expected to be less or equal than max"); |
| 861 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 862 | isl::map Relation = AccessRelation; |
Tobias Grosser | 99ea1d0 | 2017-05-21 20:23:20 +0000 | [diff] [blame] | 863 | isl::set AccessRange = Relation.range(); |
| 864 | AccessRange = addRangeBoundsToSet(AccessRange, ConstantRange(Min, Max), 0, |
| 865 | isl::dim::set); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 866 | AccessRelation = Relation.intersect_range(AccessRange); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 869 | void MemoryAccess::foldAccessRelation() { |
| 870 | if (Sizes.size() < 2 || isa<SCEVConstant>(Sizes[1])) |
| 871 | return; |
| 872 | |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 873 | int Size = Subscripts.size(); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 874 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 875 | isl::map NewAccessRelation = AccessRelation; |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 876 | |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 877 | for (int i = Size - 2; i >= 0; --i) { |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 878 | isl::space Space; |
| 879 | isl::map MapOne, MapTwo; |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 880 | isl::pw_aff DimSize = getPwAff(Sizes[i + 1]); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 881 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 882 | isl::space SpaceSize = DimSize.get_space(); |
Tobias Grosser | d3d3d6b | 2018-04-29 00:28:26 +0000 | [diff] [blame] | 883 | isl::id ParamId = SpaceSize.get_dim_id(isl::dim::param, 0); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 884 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 885 | Space = AccessRelation.get_space(); |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 886 | Space = Space.range().map_from_set(); |
| 887 | Space = Space.align_params(SpaceSize); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 888 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 889 | int ParamLocation = Space.find_dim_by_id(isl::dim::param, ParamId); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 890 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 891 | MapOne = isl::map::universe(Space); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 892 | for (int j = 0; j < Size; ++j) |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 893 | MapOne = MapOne.equate(isl::dim::in, j, isl::dim::out, j); |
| 894 | MapOne = MapOne.lower_bound_si(isl::dim::in, i + 1, 0); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 895 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 896 | MapTwo = isl::map::universe(Space); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 897 | for (int j = 0; j < Size; ++j) |
| 898 | if (j < i || j > i + 1) |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 899 | MapTwo = MapTwo.equate(isl::dim::in, j, isl::dim::out, j); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 900 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 901 | isl::local_space LS(Space); |
| 902 | isl::constraint C; |
| 903 | C = isl::constraint::alloc_equality(LS); |
| 904 | C = C.set_constant_si(-1); |
| 905 | C = C.set_coefficient_si(isl::dim::in, i, 1); |
| 906 | C = C.set_coefficient_si(isl::dim::out, i, -1); |
| 907 | MapTwo = MapTwo.add_constraint(C); |
| 908 | C = isl::constraint::alloc_equality(LS); |
| 909 | C = C.set_coefficient_si(isl::dim::in, i + 1, 1); |
| 910 | C = C.set_coefficient_si(isl::dim::out, i + 1, -1); |
| 911 | C = C.set_coefficient_si(isl::dim::param, ParamLocation, 1); |
| 912 | MapTwo = MapTwo.add_constraint(C); |
| 913 | MapTwo = MapTwo.upper_bound_si(isl::dim::in, i + 1, -1); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 914 | |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 915 | MapOne = MapOne.unite(MapTwo); |
| 916 | NewAccessRelation = NewAccessRelation.apply_range(MapOne); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 917 | } |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 918 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 919 | isl::id BaseAddrId = getScopArrayInfo()->getBasePtrId(); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 920 | isl::space Space = Statement->getDomainSpace(); |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 921 | NewAccessRelation = NewAccessRelation.set_tuple_id( |
| 922 | isl::dim::in, Space.get_tuple_id(isl::dim::set)); |
| 923 | NewAccessRelation = NewAccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 924 | NewAccessRelation = NewAccessRelation.gist_domain(Statement->getDomain()); |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 925 | |
| 926 | // Access dimension folding might in certain cases increase the number of |
| 927 | // disjuncts in the memory access, which can possibly complicate the generated |
| 928 | // run-time checks and can lead to costly compilation. |
Tobias Grosser | a32de13 | 2017-05-23 07:22:56 +0000 | [diff] [blame] | 929 | if (!PollyPreciseFoldAccesses && |
| 930 | isl_map_n_basic_map(NewAccessRelation.get()) > |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 931 | isl_map_n_basic_map(AccessRelation.get())) { |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 932 | } else { |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 933 | AccessRelation = NewAccessRelation; |
Tobias Grosser | c2f1510 | 2017-03-01 21:11:27 +0000 | [diff] [blame] | 934 | } |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 935 | } |
| 936 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 937 | /// Check if @p Expr is divisible by @p Size. |
Johannes Doerfert | a4b77c0 | 2015-11-12 20:15:32 +0000 | [diff] [blame] | 938 | static bool isDivisible(const SCEV *Expr, unsigned Size, ScalarEvolution &SE) { |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 939 | assert(Size != 0); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 940 | if (Size == 1) |
| 941 | return true; |
Johannes Doerfert | a4b77c0 | 2015-11-12 20:15:32 +0000 | [diff] [blame] | 942 | |
| 943 | // Only one factor needs to be divisible. |
| 944 | if (auto *MulExpr = dyn_cast<SCEVMulExpr>(Expr)) { |
| 945 | for (auto *FactorExpr : MulExpr->operands()) |
| 946 | if (isDivisible(FactorExpr, Size, SE)) |
| 947 | return true; |
| 948 | return false; |
| 949 | } |
| 950 | |
| 951 | // For other n-ary expressions (Add, AddRec, Max,...) all operands need |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 952 | // to be divisible. |
Johannes Doerfert | a4b77c0 | 2015-11-12 20:15:32 +0000 | [diff] [blame] | 953 | if (auto *NAryExpr = dyn_cast<SCEVNAryExpr>(Expr)) { |
| 954 | for (auto *OpExpr : NAryExpr->operands()) |
| 955 | if (!isDivisible(OpExpr, Size, SE)) |
| 956 | return false; |
| 957 | return true; |
| 958 | } |
| 959 | |
| 960 | auto *SizeSCEV = SE.getConstant(Expr->getType(), Size); |
| 961 | auto *UDivSCEV = SE.getUDivExpr(Expr, SizeSCEV); |
| 962 | auto *MulSCEV = SE.getMulExpr(UDivSCEV, SizeSCEV); |
| 963 | return MulSCEV == Expr; |
| 964 | } |
| 965 | |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 966 | void MemoryAccess::buildAccessRelation(const ScopArrayInfo *SAI) { |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 967 | assert(AccessRelation.is_null() && "AccessRelation already built"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 968 | |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 969 | // Initialize the invalid domain which describes all iterations for which the |
| 970 | // access relation is not modeled correctly. |
Tobias Grosser | 2332fa3 | 2017-08-06 15:36:48 +0000 | [diff] [blame] | 971 | isl::set StmtInvalidDomain = getStatement()->getInvalidDomain(); |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 972 | InvalidDomain = isl::set::empty(StmtInvalidDomain.get_space()); |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 973 | |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 974 | isl::ctx Ctx = Id.get_ctx(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 975 | isl::id BaseAddrId = SAI->getBasePtrId(); |
Tobias Grosser | 5683df4 | 2011-11-09 22:34:34 +0000 | [diff] [blame] | 976 | |
Eli Friedman | b9c6f01 | 2016-11-01 20:53:11 +0000 | [diff] [blame] | 977 | if (getAccessInstruction() && isa<MemIntrinsic>(getAccessInstruction())) { |
| 978 | buildMemIntrinsicAccessRelation(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 979 | AccessRelation = AccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); |
Eli Friedman | b9c6f01 | 2016-11-01 20:53:11 +0000 | [diff] [blame] | 980 | return; |
| 981 | } |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 982 | |
Eli Friedman | b9c6f01 | 2016-11-01 20:53:11 +0000 | [diff] [blame] | 983 | if (!isAffine()) { |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 984 | // We overapproximate non-affine accesses with a possible access to the |
| 985 | // whole array. For read accesses it does not make a difference, if an |
| 986 | // access must or may happen. However, for write accesses it is important to |
| 987 | // differentiate between writes that must happen and writes that may happen. |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 988 | if (AccessRelation.is_null()) |
| 989 | AccessRelation = createBasicAccessMap(Statement); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 990 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 991 | AccessRelation = AccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 992 | return; |
| 993 | } |
| 994 | |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 995 | isl::space Space = isl::space(Ctx, 0, Statement->getNumIterators(), 0); |
| 996 | AccessRelation = isl::map::universe(Space); |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 997 | |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 998 | for (int i = 0, Size = Subscripts.size(); i < Size; ++i) { |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 999 | isl::pw_aff Affine = getPwAff(Subscripts[i]); |
| 1000 | isl::map SubscriptMap = isl::map::from_pw_aff(Affine); |
| 1001 | AccessRelation = AccessRelation.flat_range_product(SubscriptMap); |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1004 | Space = Statement->getDomainSpace(); |
Tobias Grosser | 0c4c2ee | 2017-07-23 04:08:22 +0000 | [diff] [blame] | 1005 | AccessRelation = AccessRelation.set_tuple_id( |
| 1006 | isl::dim::in, Space.get_tuple_id(isl::dim::set)); |
| 1007 | AccessRelation = AccessRelation.set_tuple_id(isl::dim::out, BaseAddrId); |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 1008 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1009 | AccessRelation = AccessRelation.gist_domain(Statement->getDomain()); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1010 | } |
Tobias Grosser | 30b8a09 | 2011-08-18 07:51:37 +0000 | [diff] [blame] | 1011 | |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1012 | MemoryAccess::MemoryAccess(ScopStmt *Stmt, Instruction *AccessInst, |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1013 | AccessType AccType, Value *BaseAddress, |
| 1014 | Type *ElementType, bool Affine, |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 1015 | ArrayRef<const SCEV *> Subscripts, |
| 1016 | ArrayRef<const SCEV *> Sizes, Value *AccessValue, |
Tobias Grosser | 72684bb | 2017-05-03 08:02:32 +0000 | [diff] [blame] | 1017 | MemoryKind Kind) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1018 | : Kind(Kind), AccType(AccType), Statement(Stmt), InvalidDomain(nullptr), |
| 1019 | BaseAddr(BaseAddress), ElementType(ElementType), |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 1020 | Sizes(Sizes.begin(), Sizes.end()), AccessInstruction(AccessInst), |
| 1021 | AccessValue(AccessValue), IsAffine(Affine), |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 1022 | Subscripts(Subscripts.begin(), Subscripts.end()), AccessRelation(nullptr), |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 1023 | NewAccessRelation(nullptr), FAD(nullptr) { |
Hongbin Zheng | 86f43ea | 2016-02-20 03:40:15 +0000 | [diff] [blame] | 1024 | static const std::string TypeStrings[] = {"", "_Read", "_Write", "_MayWrite"}; |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 1025 | const std::string Access = TypeStrings[AccType] + utostr(Stmt->size()); |
Tobias Grosser | f1bfd75 | 2015-11-05 20:15:37 +0000 | [diff] [blame] | 1026 | |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 1027 | std::string IdName = Stmt->getBaseName() + Access; |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1028 | Id = isl::id::alloc(Stmt->getParent()->getIslCtx(), IdName, this); |
Tobias Grosser | f1bfd75 | 2015-11-05 20:15:37 +0000 | [diff] [blame] | 1029 | } |
Michael Kruse | e2bccbb | 2015-09-18 19:59:43 +0000 | [diff] [blame] | 1030 | |
Tobias Grosser | 1f6ba7e | 2017-07-24 16:22:32 +0000 | [diff] [blame] | 1031 | MemoryAccess::MemoryAccess(ScopStmt *Stmt, AccessType AccType, isl::map AccRel) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1032 | : Kind(MemoryKind::Array), AccType(AccType), Statement(Stmt), |
| 1033 | InvalidDomain(nullptr), AccessRelation(nullptr), |
| 1034 | NewAccessRelation(AccRel), FAD(nullptr) { |
Tobias Grosser | 206e9e3 | 2017-07-24 16:22:27 +0000 | [diff] [blame] | 1035 | isl::id ArrayInfoId = NewAccessRelation.get_tuple_id(isl::dim::out); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1036 | auto *SAI = ScopArrayInfo::getFromId(ArrayInfoId); |
| 1037 | Sizes.push_back(nullptr); |
| 1038 | for (unsigned i = 1; i < SAI->getNumberOfDimensions(); i++) |
| 1039 | Sizes.push_back(SAI->getDimensionSize(i)); |
| 1040 | ElementType = SAI->getElementType(); |
| 1041 | BaseAddr = SAI->getBasePtr(); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1042 | static const std::string TypeStrings[] = {"", "_Read", "_Write", "_MayWrite"}; |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 1043 | const std::string Access = TypeStrings[AccType] + utostr(Stmt->size()); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1044 | |
Tobias Grosser | 8133128 | 2017-05-03 07:57:35 +0000 | [diff] [blame] | 1045 | std::string IdName = Stmt->getBaseName() + Access; |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1046 | Id = isl::id::alloc(Stmt->getParent()->getIslCtx(), IdName, this); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1049 | MemoryAccess::~MemoryAccess() = default; |
| 1050 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1051 | void MemoryAccess::realignParams() { |
Tobias Grosser | 8ea1fc1 | 2017-08-06 19:52:38 +0000 | [diff] [blame] | 1052 | isl::set Ctx = Statement->getParent()->getContext(); |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 1053 | InvalidDomain = InvalidDomain.gist_params(Ctx); |
| 1054 | AccessRelation = AccessRelation.gist_params(Ctx); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 1057 | const std::string MemoryAccess::getReductionOperatorStr() const { |
| 1058 | return MemoryAccess::getReductionOperatorStr(getReductionType()); |
| 1059 | } |
| 1060 | |
Tobias Grosser | fe46c3f | 2017-07-23 04:08:11 +0000 | [diff] [blame] | 1061 | isl::id MemoryAccess::getId() const { return Id; } |
Tobias Grosser | 6f48e0f | 2015-05-15 09:58:32 +0000 | [diff] [blame] | 1062 | |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1063 | raw_ostream &polly::operator<<(raw_ostream &OS, |
| 1064 | MemoryAccess::ReductionType RT) { |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 1065 | if (RT == MemoryAccess::RT_NONE) |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1066 | OS << "NONE"; |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 1067 | else |
| 1068 | OS << MemoryAccess::getReductionOperatorStr(RT); |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1069 | return OS; |
| 1070 | } |
| 1071 | |
Siddharth Bhat | 0fe7231 | 2017-05-15 08:41:30 +0000 | [diff] [blame] | 1072 | void MemoryAccess::setFortranArrayDescriptor(Value *FAD) { this->FAD = FAD; } |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 1073 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1074 | void MemoryAccess::print(raw_ostream &OS) const { |
Johannes Doerfert | 4c7ce47 | 2014-10-08 10:11:33 +0000 | [diff] [blame] | 1075 | switch (AccType) { |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 1076 | case READ: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 1077 | OS.indent(12) << "ReadAccess :=\t"; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 1078 | break; |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 1079 | case MUST_WRITE: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 1080 | OS.indent(12) << "MustWriteAccess :=\t"; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 1081 | break; |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 1082 | case MAY_WRITE: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 1083 | OS.indent(12) << "MayWriteAccess :=\t"; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 1084 | break; |
| 1085 | } |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 1086 | |
Johannes Doerfert | 0ff23ec | 2015-02-06 20:13:15 +0000 | [diff] [blame] | 1087 | OS << "[Reduction Type: " << getReductionType() << "] "; |
Siddharth Bhat | f2dbba8 | 2017-05-10 13:11:20 +0000 | [diff] [blame] | 1088 | |
| 1089 | if (FAD) { |
| 1090 | OS << "[Fortran array descriptor: " << FAD->getName(); |
| 1091 | OS << "] "; |
| 1092 | }; |
| 1093 | |
Tobias Grosser | a535dff | 2015-12-13 19:59:01 +0000 | [diff] [blame] | 1094 | OS << "[Scalar: " << isScalarKind() << "]\n"; |
Michael Kruse | b8d2644 | 2015-12-13 19:35:26 +0000 | [diff] [blame] | 1095 | OS.indent(16) << getOriginalAccessRelationStr() << ";\n"; |
Tobias Grosser | 6f73008 | 2015-09-05 07:46:47 +0000 | [diff] [blame] | 1096 | if (hasNewAccessRelation()) |
| 1097 | OS.indent(11) << "new: " << getNewAccessRelationStr() << ";\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 1100 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Michael Kruse | e186013 | 2017-07-21 15:54:13 +0000 | [diff] [blame] | 1101 | LLVM_DUMP_METHOD void MemoryAccess::dump() const { print(errs()); } |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 1102 | #endif |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1103 | |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 1104 | isl::pw_aff MemoryAccess::getPwAff(const SCEV *E) { |
Johannes Doerfert | 97f0dcd | 2016-04-12 13:26:45 +0000 | [diff] [blame] | 1105 | auto *Stmt = getStatement(); |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 1106 | PWACtx PWAC = Stmt->getParent()->getPwAff(E, Stmt->getEntryBlock()); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1107 | isl::set StmtDom = getStatement()->getDomain(); |
Tobias Grosser | cdf471b | 2017-07-24 16:36:34 +0000 | [diff] [blame] | 1108 | StmtDom = StmtDom.reset_tuple_id(); |
Philip Pfaffe | d98dbee | 2017-12-06 21:02:22 +0000 | [diff] [blame] | 1109 | isl::set NewInvalidDom = StmtDom.intersect(PWAC.second); |
Tobias Grosser | b739cb4 | 2017-07-24 20:30:34 +0000 | [diff] [blame] | 1110 | InvalidDomain = InvalidDomain.unite(NewInvalidDom); |
Philip Pfaffe | d98dbee | 2017-12-06 21:02:22 +0000 | [diff] [blame] | 1111 | return PWAC.first; |
Johannes Doerfert | 97f0dcd | 2016-04-12 13:26:45 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1114 | // Create a map in the size of the provided set domain, that maps from the |
| 1115 | // one element of the provided set domain to another element of the provided |
| 1116 | // set domain. |
| 1117 | // The mapping is limited to all points that are equal in all but the last |
| 1118 | // dimension and for which the last dimension of the input is strict smaller |
| 1119 | // than the last dimension of the output. |
| 1120 | // |
| 1121 | // getEqualAndLarger(set[i0, i1, ..., iX]): |
| 1122 | // |
| 1123 | // set[i0, i1, ..., iX] -> set[o0, o1, ..., oX] |
| 1124 | // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1), iX < oX |
| 1125 | // |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1126 | static isl::map getEqualAndLarger(isl::space SetDomain) { |
| 1127 | isl::space Space = SetDomain.map_from_set(); |
| 1128 | isl::map Map = isl::map::universe(Space); |
| 1129 | unsigned lastDimension = Map.dim(isl::dim::in) - 1; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1130 | |
| 1131 | // Set all but the last dimension to be equal for the input and output |
| 1132 | // |
| 1133 | // input[i0, i1, ..., iX] -> output[o0, o1, ..., oX] |
| 1134 | // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1) |
Sebastian Pop | 4040876 | 2013-10-04 17:14:53 +0000 | [diff] [blame] | 1135 | for (unsigned i = 0; i < lastDimension; ++i) |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1136 | Map = Map.equate(isl::dim::in, i, isl::dim::out, i); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1137 | |
| 1138 | // Set the last dimension of the input to be strict smaller than the |
| 1139 | // last dimension of the output. |
| 1140 | // |
| 1141 | // input[?,?,?,...,iX] -> output[?,?,?,...,oX] : iX < oX |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1142 | Map = Map.order_lt(isl::dim::in, lastDimension, isl::dim::out, lastDimension); |
Tobias Grosser | c327932c | 2012-02-01 14:23:36 +0000 | [diff] [blame] | 1143 | return Map; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1146 | isl::set MemoryAccess::getStride(isl::map Schedule) const { |
| 1147 | isl::map AccessRelation = getAccessRelation(); |
| 1148 | isl::space Space = Schedule.get_space().range(); |
| 1149 | isl::map NextScatt = getEqualAndLarger(Space); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1150 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1151 | Schedule = Schedule.reverse(); |
| 1152 | NextScatt = NextScatt.lexmin(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1153 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1154 | NextScatt = NextScatt.apply_range(Schedule); |
| 1155 | NextScatt = NextScatt.apply_range(AccessRelation); |
| 1156 | NextScatt = NextScatt.apply_domain(Schedule); |
| 1157 | NextScatt = NextScatt.apply_domain(AccessRelation); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1158 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1159 | isl::set Deltas = NextScatt.deltas(); |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1160 | return Deltas; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1163 | bool MemoryAccess::isStrideX(isl::map Schedule, int StrideWidth) const { |
| 1164 | isl::set Stride, StrideX; |
Tobias Grosser | 28dd486 | 2012-01-24 16:42:16 +0000 | [diff] [blame] | 1165 | bool IsStrideX; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1166 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1167 | Stride = getStride(Schedule); |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1168 | StrideX = isl::set::universe(Stride.get_space()); |
| 1169 | for (unsigned i = 0; i < StrideX.dim(isl::dim::set) - 1; i++) |
| 1170 | StrideX = StrideX.fix_si(isl::dim::set, i, 0); |
| 1171 | StrideX = StrideX.fix_si(isl::dim::set, StrideX.dim(isl::dim::set) - 1, |
| 1172 | StrideWidth); |
| 1173 | IsStrideX = Stride.is_subset(StrideX); |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 1174 | |
Tobias Grosser | 28dd486 | 2012-01-24 16:42:16 +0000 | [diff] [blame] | 1175 | return IsStrideX; |
| 1176 | } |
| 1177 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1178 | bool MemoryAccess::isStrideZero(isl::map Schedule) const { |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1179 | return isStrideX(Schedule, 0); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
Tobias Grosser | d7065e5 | 2017-07-24 20:50:22 +0000 | [diff] [blame] | 1182 | bool MemoryAccess::isStrideOne(isl::map Schedule) const { |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 1183 | return isStrideX(Schedule, 1); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
Tobias Grosser | 6d58804 | 2017-08-02 19:27:16 +0000 | [diff] [blame] | 1186 | void MemoryAccess::setAccessRelation(isl::map NewAccess) { |
| 1187 | AccessRelation = NewAccess; |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1190 | void MemoryAccess::setNewAccessRelation(isl::map NewAccess) { |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1191 | assert(NewAccess); |
| 1192 | |
| 1193 | #ifndef NDEBUG |
| 1194 | // Check domain space compatibility. |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1195 | isl::space NewSpace = NewAccess.get_space(); |
| 1196 | isl::space NewDomainSpace = NewSpace.domain(); |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1197 | isl::space OriginalDomainSpace = getStatement()->getDomainSpace(); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1198 | assert(OriginalDomainSpace.has_equal_tuples(NewDomainSpace)); |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1199 | |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1200 | // Reads must be executed unconditionally. Writes might be executed in a |
| 1201 | // subdomain only. |
| 1202 | if (isRead()) { |
| 1203 | // Check whether there is an access for every statement instance. |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1204 | isl::set StmtDomain = getStatement()->getDomain(); |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 1205 | StmtDomain = |
| 1206 | StmtDomain.intersect_params(getStatement()->getParent()->getContext()); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1207 | isl::set NewDomain = NewAccess.domain(); |
| 1208 | assert(StmtDomain.is_subset(NewDomain) && |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1209 | "Partial READ accesses not supported"); |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1210 | } |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1211 | |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1212 | isl::space NewAccessSpace = NewAccess.get_space(); |
| 1213 | assert(NewAccessSpace.has_tuple_id(isl::dim::set) && |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1214 | "Must specify the array that is accessed"); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1215 | isl::id NewArrayId = NewAccessSpace.get_tuple_id(isl::dim::set); |
| 1216 | auto *SAI = static_cast<ScopArrayInfo *>(NewArrayId.get_user()); |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1217 | assert(SAI && "Must set a ScopArrayInfo"); |
Tobias Grosser | e1ff0cf | 2017-01-17 12:00:42 +0000 | [diff] [blame] | 1218 | |
| 1219 | if (SAI->isArrayKind() && SAI->getBasePtrOriginSAI()) { |
| 1220 | InvariantEquivClassTy *EqClass = |
| 1221 | getStatement()->getParent()->lookupInvariantEquivClass( |
| 1222 | SAI->getBasePtr()); |
| 1223 | assert(EqClass && |
| 1224 | "Access functions to indirect arrays must have an invariant and " |
| 1225 | "hoisted base pointer"); |
| 1226 | } |
| 1227 | |
| 1228 | // Check whether access dimensions correspond to number of dimensions of the |
| 1229 | // accesses array. |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1230 | auto Dims = SAI->getNumberOfDimensions(); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1231 | assert(NewAccessSpace.dim(isl::dim::set) == Dims && |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1232 | "Access dims must match array dims"); |
Michael Kruse | 772ce72 | 2016-09-01 19:16:58 +0000 | [diff] [blame] | 1233 | #endif |
| 1234 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1235 | NewAccess = NewAccess.gist_domain(getStatement()->getDomain()); |
Tobias Grosser | 7b45af1 | 2017-08-02 19:27:25 +0000 | [diff] [blame] | 1236 | NewAccessRelation = NewAccess; |
Raghesh Aloor | 3cb6628 | 2011-07-12 17:14:03 +0000 | [diff] [blame] | 1237 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1238 | |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1239 | bool MemoryAccess::isLatestPartialAccess() const { |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1240 | isl::set StmtDom = getStatement()->getDomain(); |
Tobias Grosser | 1515f6b | 2017-07-23 04:08:38 +0000 | [diff] [blame] | 1241 | isl::set AccDom = getLatestAccessRelation().domain(); |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1242 | |
Tobias Grosser | d3d3d6b | 2018-04-29 00:28:26 +0000 | [diff] [blame] | 1243 | return !StmtDom.is_subset(AccDom); |
Michael Kruse | 706f79a | 2017-05-21 22:46:57 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1246 | //===----------------------------------------------------------------------===// |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 1247 | |
Tobias Grosser | 6ad1640 | 2017-08-06 17:45:28 +0000 | [diff] [blame] | 1248 | isl::map ScopStmt::getSchedule() const { |
Tobias Grosser | 1e09c13 | 2017-08-14 06:49:06 +0000 | [diff] [blame] | 1249 | isl::set Domain = getDomain(); |
| 1250 | if (Domain.is_empty()) |
| 1251 | return isl::map::from_aff(isl::aff(isl::local_space(getDomainSpace()))); |
| 1252 | auto Schedule = getParent()->getSchedule(); |
| 1253 | if (!Schedule) |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1254 | return nullptr; |
Tobias Grosser | 1e09c13 | 2017-08-14 06:49:06 +0000 | [diff] [blame] | 1255 | Schedule = Schedule.intersect_domain(isl::union_set(Domain)); |
| 1256 | if (Schedule.is_empty()) |
| 1257 | return isl::map::from_aff(isl::aff(isl::local_space(getDomainSpace()))); |
| 1258 | isl::map M = M.from_union_map(Schedule); |
| 1259 | M = M.coalesce(); |
| 1260 | M = M.gist_domain(Domain); |
| 1261 | M = M.coalesce(); |
| 1262 | return M; |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1263 | } |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 1264 | |
Tobias Grosser | a9b5bba | 2017-08-06 16:11:53 +0000 | [diff] [blame] | 1265 | void ScopStmt::restrictDomain(isl::set NewDomain) { |
| 1266 | assert(NewDomain.is_subset(Domain) && |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1267 | "New domain is not a subset of old domain!"); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1268 | Domain = NewDomain; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
Michael Kruse | 70af4f5 | 2017-08-07 18:40:29 +0000 | [diff] [blame] | 1271 | void ScopStmt::addAccess(MemoryAccess *Access, bool Prepend) { |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1272 | Instruction *AccessInst = Access->getAccessInstruction(); |
| 1273 | |
Michael Kruse | 58fa3bb | 2015-12-22 23:25:11 +0000 | [diff] [blame] | 1274 | if (Access->isArrayKind()) { |
| 1275 | MemoryAccessList &MAL = InstructionToAccess[AccessInst]; |
| 1276 | MAL.emplace_front(Access); |
Michael Kruse | 436db62 | 2016-01-26 13:33:10 +0000 | [diff] [blame] | 1277 | } else if (Access->isValueKind() && Access->isWrite()) { |
| 1278 | Instruction *AccessVal = cast<Instruction>(Access->getAccessValue()); |
Michael Kruse | 436db62 | 2016-01-26 13:33:10 +0000 | [diff] [blame] | 1279 | assert(!ValueWrites.lookup(AccessVal)); |
| 1280 | |
| 1281 | ValueWrites[AccessVal] = Access; |
Michael Kruse | ad28e5a | 2016-01-26 13:33:15 +0000 | [diff] [blame] | 1282 | } else if (Access->isValueKind() && Access->isRead()) { |
| 1283 | Value *AccessVal = Access->getAccessValue(); |
| 1284 | assert(!ValueReads.lookup(AccessVal)); |
| 1285 | |
| 1286 | ValueReads[AccessVal] = Access; |
Michael Kruse | ee6a4fc | 2016-01-26 13:33:27 +0000 | [diff] [blame] | 1287 | } else if (Access->isAnyPHIKind() && Access->isWrite()) { |
Tobias Grosser | 5db171a | 2017-02-10 10:09:44 +0000 | [diff] [blame] | 1288 | PHINode *PHI = cast<PHINode>(Access->getAccessValue()); |
Michael Kruse | ee6a4fc | 2016-01-26 13:33:27 +0000 | [diff] [blame] | 1289 | assert(!PHIWrites.lookup(PHI)); |
| 1290 | |
| 1291 | PHIWrites[PHI] = Access; |
Michael Kruse | 3562f27 | 2017-07-20 16:47:57 +0000 | [diff] [blame] | 1292 | } else if (Access->isAnyPHIKind() && Access->isRead()) { |
| 1293 | PHINode *PHI = cast<PHINode>(Access->getAccessValue()); |
| 1294 | assert(!PHIReads.lookup(PHI)); |
| 1295 | |
| 1296 | PHIReads[PHI] = Access; |
Michael Kruse | 58fa3bb | 2015-12-22 23:25:11 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
Michael Kruse | 70af4f5 | 2017-08-07 18:40:29 +0000 | [diff] [blame] | 1299 | if (Prepend) { |
| 1300 | MemAccs.insert(MemAccs.begin(), Access); |
| 1301 | return; |
| 1302 | } |
Michael Kruse | 58fa3bb | 2015-12-22 23:25:11 +0000 | [diff] [blame] | 1303 | MemAccs.push_back(Access); |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1306 | void ScopStmt::realignParams() { |
Johannes Doerfert | f675289 | 2014-06-13 18:01:45 +0000 | [diff] [blame] | 1307 | for (MemoryAccess *MA : *this) |
| 1308 | MA->realignParams(); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1309 | |
Tobias Grosser | 8ea1fc1 | 2017-08-06 19:52:38 +0000 | [diff] [blame] | 1310 | isl::set Ctx = Parent.getContext(); |
Tobias Grosser | 2332fa3 | 2017-08-06 15:36:48 +0000 | [diff] [blame] | 1311 | InvalidDomain = InvalidDomain.gist_params(Ctx); |
Tobias Grosser | a9b5bba | 2017-08-06 16:11:53 +0000 | [diff] [blame] | 1312 | Domain = Domain.gist_params(Ctx); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
Tobias Grosser | 78a8494 | 2018-06-01 19:12:00 +0000 | [diff] [blame] | 1315 | /// Add @p BSet to set @p BoundedParts if @p BSet is bounded. |
| 1316 | static isl::set collectBoundedParts(isl::set S) { |
| 1317 | isl::set BoundedParts = isl::set::empty(S.get_space()); |
| 1318 | S.foreach_basic_set([&BoundedParts](isl::basic_set BSet) -> isl::stat { |
| 1319 | if (BSet.is_bounded()) { |
| 1320 | BoundedParts = BoundedParts.unite(isl::set(BSet)); |
| 1321 | } |
| 1322 | return isl::stat::ok; |
| 1323 | }); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1324 | return BoundedParts; |
| 1325 | } |
| 1326 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1327 | /// 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] | 1328 | /// |
| 1329 | /// @returns A separation of @p S into first an unbounded then a bounded subset, |
| 1330 | /// both with regards to the dimension @p Dim. |
Tobias Grosser | 78a8494 | 2018-06-01 19:12:00 +0000 | [diff] [blame] | 1331 | static std::pair<isl::set, isl::set> partitionSetParts(isl::set S, |
| 1332 | unsigned Dim) { |
| 1333 | for (unsigned u = 0, e = S.n_dim(); u < e; u++) |
| 1334 | S = S.lower_bound_si(isl::dim::set, u, 0); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1335 | |
Tobias Grosser | 78a8494 | 2018-06-01 19:12:00 +0000 | [diff] [blame] | 1336 | unsigned NumDimsS = S.n_dim(); |
| 1337 | isl::set OnlyDimS = S; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1338 | |
| 1339 | // Remove dimensions that are greater than Dim as they are not interesting. |
| 1340 | assert(NumDimsS >= Dim + 1); |
Tobias Grosser | 78a8494 | 2018-06-01 19:12:00 +0000 | [diff] [blame] | 1341 | OnlyDimS = OnlyDimS.project_out(isl::dim::set, Dim + 1, NumDimsS - Dim - 1); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1342 | |
| 1343 | // Create artificial parametric upper bounds for dimensions smaller than Dim |
| 1344 | // as we are not interested in them. |
Tobias Grosser | 78a8494 | 2018-06-01 19:12:00 +0000 | [diff] [blame] | 1345 | OnlyDimS = OnlyDimS.insert_dims(isl::dim::param, 0, Dim); |
| 1346 | |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1347 | for (unsigned u = 0; u < Dim; u++) { |
Tobias Grosser | 78a8494 | 2018-06-01 19:12:00 +0000 | [diff] [blame] | 1348 | isl::constraint C = isl::constraint::alloc_inequality( |
| 1349 | isl::local_space(OnlyDimS.get_space())); |
| 1350 | C = C.set_coefficient_si(isl::dim::param, u, 1); |
| 1351 | C = C.set_coefficient_si(isl::dim::set, u, -1); |
| 1352 | OnlyDimS = OnlyDimS.add_constraint(C); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | // Collect all bounded parts of OnlyDimS. |
Tobias Grosser | 78a8494 | 2018-06-01 19:12:00 +0000 | [diff] [blame] | 1356 | isl::set BoundedParts = collectBoundedParts(OnlyDimS); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1357 | |
| 1358 | // Create the dimensions greater than Dim again. |
Tobias Grosser | 78a8494 | 2018-06-01 19:12:00 +0000 | [diff] [blame] | 1359 | BoundedParts = |
| 1360 | BoundedParts.insert_dims(isl::dim::set, Dim + 1, NumDimsS - Dim - 1); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1361 | |
| 1362 | // Remove the artificial upper bound parameters again. |
Tobias Grosser | 78a8494 | 2018-06-01 19:12:00 +0000 | [diff] [blame] | 1363 | BoundedParts = BoundedParts.remove_dims(isl::dim::param, 0, Dim); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1364 | |
Tobias Grosser | 78a8494 | 2018-06-01 19:12:00 +0000 | [diff] [blame] | 1365 | isl::set UnboundedParts = S.subtract(BoundedParts); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 1366 | return std::make_pair(UnboundedParts, BoundedParts); |
| 1367 | } |
| 1368 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1369 | /// Create the conditions under which @p L @p Pred @p R is true. |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1370 | static __isl_give isl_set *buildConditionSet(ICmpInst::Predicate Pred, |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1371 | __isl_take isl_pw_aff *L, |
| 1372 | __isl_take isl_pw_aff *R) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1373 | switch (Pred) { |
| 1374 | case ICmpInst::ICMP_EQ: |
| 1375 | return isl_pw_aff_eq_set(L, R); |
| 1376 | case ICmpInst::ICMP_NE: |
| 1377 | return isl_pw_aff_ne_set(L, R); |
| 1378 | case ICmpInst::ICMP_SLT: |
| 1379 | return isl_pw_aff_lt_set(L, R); |
| 1380 | case ICmpInst::ICMP_SLE: |
| 1381 | return isl_pw_aff_le_set(L, R); |
| 1382 | case ICmpInst::ICMP_SGT: |
| 1383 | return isl_pw_aff_gt_set(L, R); |
| 1384 | case ICmpInst::ICMP_SGE: |
| 1385 | return isl_pw_aff_ge_set(L, R); |
| 1386 | case ICmpInst::ICMP_ULT: |
| 1387 | return isl_pw_aff_lt_set(L, R); |
| 1388 | case ICmpInst::ICMP_UGT: |
| 1389 | return isl_pw_aff_gt_set(L, R); |
| 1390 | case ICmpInst::ICMP_ULE: |
| 1391 | return isl_pw_aff_le_set(L, R); |
| 1392 | case ICmpInst::ICMP_UGE: |
| 1393 | return isl_pw_aff_ge_set(L, R); |
| 1394 | default: |
| 1395 | llvm_unreachable("Non integer predicate not supported"); |
| 1396 | } |
| 1397 | } |
| 1398 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1399 | /// Compute the isl representation for the SCEV @p E in this BB. |
| 1400 | /// |
| 1401 | /// @param S The Scop in which @p BB resides in. |
| 1402 | /// @param BB The BB for which isl representation is to be |
| 1403 | /// computed. |
| 1404 | /// @param InvalidDomainMap A map of BB to their invalid domains. |
| 1405 | /// @param E The SCEV that should be translated. |
| 1406 | /// @param NonNegative Flag to indicate the @p E has to be non-negative. |
| 1407 | /// |
| 1408 | /// Note that this function will also adjust the invalid context accordingly. |
| 1409 | |
| 1410 | __isl_give isl_pw_aff * |
| 1411 | getPwAff(Scop &S, BasicBlock *BB, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 1412 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, const SCEV *E, |
| 1413 | bool NonNegative = false) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1414 | PWACtx PWAC = S.getPwAff(E, BB, NonNegative); |
Philip Pfaffe | d98dbee | 2017-12-06 21:02:22 +0000 | [diff] [blame] | 1415 | InvalidDomainMap[BB] = InvalidDomainMap[BB].unite(PWAC.second); |
Tobias Grosser | 8dae41a | 2018-04-29 00:57:38 +0000 | [diff] [blame] | 1416 | return PWAC.first.release(); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1419 | /// 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] | 1420 | /// |
| 1421 | /// This will fill @p ConditionSets with the conditions under which control |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1422 | /// will be moved from @p SI to its successors. Hence, @p ConditionSets will |
| 1423 | /// have as many elements as @p SI has successors. |
Tobias Grosser | ee45759 | 2017-09-24 09:25:30 +0000 | [diff] [blame] | 1424 | bool buildConditionSets(Scop &S, BasicBlock *BB, SwitchInst *SI, Loop *L, |
| 1425 | __isl_keep isl_set *Domain, |
| 1426 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, |
| 1427 | SmallVectorImpl<__isl_give isl_set *> &ConditionSets) { |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1428 | Value *Condition = getConditionFromTerminator(SI); |
| 1429 | assert(Condition && "No condition for switch"); |
| 1430 | |
| 1431 | ScalarEvolution &SE = *S.getSE(); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1432 | isl_pw_aff *LHS, *RHS; |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1433 | LHS = getPwAff(S, BB, InvalidDomainMap, SE.getSCEVAtScope(Condition, L)); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1434 | |
| 1435 | unsigned NumSuccessors = SI->getNumSuccessors(); |
| 1436 | ConditionSets.resize(NumSuccessors); |
| 1437 | for (auto &Case : SI->cases()) { |
| 1438 | unsigned Idx = Case.getSuccessorIndex(); |
| 1439 | ConstantInt *CaseValue = Case.getCaseValue(); |
| 1440 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1441 | RHS = getPwAff(S, BB, InvalidDomainMap, SE.getSCEV(CaseValue)); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1442 | isl_set *CaseConditionSet = |
Tobias Grosser | b948630 | 2018-03-03 19:27:54 +0000 | [diff] [blame] | 1443 | buildConditionSet(ICmpInst::ICMP_EQ, isl_pw_aff_copy(LHS), RHS); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1444 | ConditionSets[Idx] = isl_set_coalesce( |
| 1445 | isl_set_intersect(CaseConditionSet, isl_set_copy(Domain))); |
| 1446 | } |
| 1447 | |
| 1448 | assert(ConditionSets[0] == nullptr && "Default condition set was set"); |
| 1449 | isl_set *ConditionSetUnion = isl_set_copy(ConditionSets[1]); |
| 1450 | for (unsigned u = 2; u < NumSuccessors; u++) |
| 1451 | ConditionSetUnion = |
| 1452 | isl_set_union(ConditionSetUnion, isl_set_copy(ConditionSets[u])); |
Tobias Grosser | b948630 | 2018-03-03 19:27:54 +0000 | [diff] [blame] | 1453 | ConditionSets[0] = isl_set_subtract(isl_set_copy(Domain), ConditionSetUnion); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1454 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1455 | isl_pw_aff_free(LHS); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1456 | |
| 1457 | return true; |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
Michael Kruse | 0865585 | 2017-07-20 12:37:02 +0000 | [diff] [blame] | 1460 | /// Build condition sets for unsigned ICmpInst(s). |
| 1461 | /// Special handling is required for unsigned operands to ensure that if |
| 1462 | /// MSB (aka the Sign bit) is set for an operands in an unsigned ICmpInst |
| 1463 | /// it should wrap around. |
| 1464 | /// |
| 1465 | /// @param IsStrictUpperBound holds information on the predicate relation |
| 1466 | /// between TestVal and UpperBound, i.e, |
| 1467 | /// TestVal < UpperBound OR TestVal <= UpperBound |
Tobias Grosser | ee45759 | 2017-09-24 09:25:30 +0000 | [diff] [blame] | 1468 | __isl_give isl_set * |
Michael Kruse | 0865585 | 2017-07-20 12:37:02 +0000 | [diff] [blame] | 1469 | buildUnsignedConditionSets(Scop &S, BasicBlock *BB, Value *Condition, |
| 1470 | __isl_keep isl_set *Domain, const SCEV *SCEV_TestVal, |
| 1471 | const SCEV *SCEV_UpperBound, |
| 1472 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, |
| 1473 | bool IsStrictUpperBound) { |
Michael Kruse | 0865585 | 2017-07-20 12:37:02 +0000 | [diff] [blame] | 1474 | // Do not take NonNeg assumption on TestVal |
| 1475 | // as it might have MSB (Sign bit) set. |
| 1476 | isl_pw_aff *TestVal = getPwAff(S, BB, InvalidDomainMap, SCEV_TestVal, false); |
| 1477 | // Take NonNeg assumption on UpperBound. |
| 1478 | isl_pw_aff *UpperBound = |
| 1479 | getPwAff(S, BB, InvalidDomainMap, SCEV_UpperBound, true); |
| 1480 | |
| 1481 | // 0 <= TestVal |
| 1482 | isl_set *First = |
| 1483 | isl_pw_aff_le_set(isl_pw_aff_zero_on_domain(isl_local_space_from_space( |
| 1484 | isl_pw_aff_get_domain_space(TestVal))), |
| 1485 | isl_pw_aff_copy(TestVal)); |
| 1486 | |
| 1487 | isl_set *Second; |
| 1488 | if (IsStrictUpperBound) |
| 1489 | // TestVal < UpperBound |
| 1490 | Second = isl_pw_aff_lt_set(TestVal, UpperBound); |
| 1491 | else |
| 1492 | // TestVal <= UpperBound |
| 1493 | Second = isl_pw_aff_le_set(TestVal, UpperBound); |
| 1494 | |
| 1495 | isl_set *ConsequenceCondSet = isl_set_intersect(First, Second); |
Michael Kruse | 0865585 | 2017-07-20 12:37:02 +0000 | [diff] [blame] | 1496 | return ConsequenceCondSet; |
| 1497 | } |
| 1498 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1499 | /// Build the conditions sets for the branch condition @p Condition in |
| 1500 | /// the @p Domain. |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1501 | /// |
| 1502 | /// This will fill @p ConditionSets with the conditions under which control |
| 1503 | /// 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] | 1504 | /// have as many elements as @p TI has successors. If @p TI is nullptr the |
| 1505 | /// context under which @p Condition is true/false will be returned as the |
| 1506 | /// new elements of @p ConditionSets. |
Tobias Grosser | ee45759 | 2017-09-24 09:25:30 +0000 | [diff] [blame] | 1507 | bool buildConditionSets(Scop &S, BasicBlock *BB, Value *Condition, |
| 1508 | TerminatorInst *TI, Loop *L, __isl_keep isl_set *Domain, |
| 1509 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, |
| 1510 | SmallVectorImpl<__isl_give isl_set *> &ConditionSets) { |
Tobias Grosser | 5e531df | 2017-09-25 20:27:15 +0000 | [diff] [blame] | 1511 | ScalarEvolution &SE = *S.getSE(); |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1512 | isl_set *ConsequenceCondSet = nullptr; |
Tobias Grosser | 0a62b2d | 2017-09-25 16:37:15 +0000 | [diff] [blame] | 1513 | |
Tobias Grosser | 5e531df | 2017-09-25 20:27:15 +0000 | [diff] [blame] | 1514 | if (auto Load = dyn_cast<LoadInst>(Condition)) { |
| 1515 | const SCEV *LHSSCEV = SE.getSCEVAtScope(Load, L); |
| 1516 | const SCEV *RHSSCEV = SE.getZero(LHSSCEV->getType()); |
| 1517 | bool NonNeg = false; |
| 1518 | isl_pw_aff *LHS = getPwAff(S, BB, InvalidDomainMap, LHSSCEV, NonNeg); |
| 1519 | isl_pw_aff *RHS = getPwAff(S, BB, InvalidDomainMap, RHSSCEV, NonNeg); |
Tobias Grosser | b948630 | 2018-03-03 19:27:54 +0000 | [diff] [blame] | 1520 | ConsequenceCondSet = buildConditionSet(ICmpInst::ICMP_SLE, LHS, RHS); |
Tobias Grosser | 5e531df | 2017-09-25 20:27:15 +0000 | [diff] [blame] | 1521 | } else if (auto *PHI = dyn_cast<PHINode>(Condition)) { |
Tobias Grosser | 0a62b2d | 2017-09-25 16:37:15 +0000 | [diff] [blame] | 1522 | auto *Unique = dyn_cast<ConstantInt>( |
| 1523 | getUniqueNonErrorValue(PHI, &S.getRegion(), *S.getLI(), *S.getDT())); |
| 1524 | |
| 1525 | if (Unique->isZero()) |
| 1526 | ConsequenceCondSet = isl_set_empty(isl_set_get_space(Domain)); |
| 1527 | else |
| 1528 | ConsequenceCondSet = isl_set_universe(isl_set_get_space(Domain)); |
| 1529 | } else if (auto *CCond = dyn_cast<ConstantInt>(Condition)) { |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1530 | if (CCond->isZero()) |
| 1531 | ConsequenceCondSet = isl_set_empty(isl_set_get_space(Domain)); |
| 1532 | else |
| 1533 | ConsequenceCondSet = isl_set_universe(isl_set_get_space(Domain)); |
| 1534 | } else if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Condition)) { |
| 1535 | auto Opcode = BinOp->getOpcode(); |
| 1536 | assert(Opcode == Instruction::And || Opcode == Instruction::Or); |
| 1537 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1538 | bool Valid = buildConditionSets(S, BB, BinOp->getOperand(0), TI, L, Domain, |
| 1539 | InvalidDomainMap, ConditionSets) && |
| 1540 | buildConditionSets(S, BB, BinOp->getOperand(1), TI, L, Domain, |
| 1541 | InvalidDomainMap, ConditionSets); |
Johannes Doerfert | ede4eca | 2016-05-10 14:01:21 +0000 | [diff] [blame] | 1542 | if (!Valid) { |
| 1543 | while (!ConditionSets.empty()) |
| 1544 | isl_set_free(ConditionSets.pop_back_val()); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1545 | return false; |
Johannes Doerfert | ede4eca | 2016-05-10 14:01:21 +0000 | [diff] [blame] | 1546 | } |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1547 | |
| 1548 | isl_set_free(ConditionSets.pop_back_val()); |
| 1549 | isl_set *ConsCondPart0 = ConditionSets.pop_back_val(); |
| 1550 | isl_set_free(ConditionSets.pop_back_val()); |
| 1551 | isl_set *ConsCondPart1 = ConditionSets.pop_back_val(); |
| 1552 | |
| 1553 | if (Opcode == Instruction::And) |
| 1554 | ConsequenceCondSet = isl_set_intersect(ConsCondPart0, ConsCondPart1); |
| 1555 | else |
| 1556 | ConsequenceCondSet = isl_set_union(ConsCondPart0, ConsCondPart1); |
| 1557 | } else { |
| 1558 | auto *ICond = dyn_cast<ICmpInst>(Condition); |
| 1559 | assert(ICond && |
| 1560 | "Condition of exiting branch was neither constant nor ICmp!"); |
| 1561 | |
Tobias Grosser | ee45759 | 2017-09-24 09:25:30 +0000 | [diff] [blame] | 1562 | LoopInfo &LI = *S.getLI(); |
| 1563 | DominatorTree &DT = *S.getDT(); |
| 1564 | Region &R = S.getRegion(); |
| 1565 | |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1566 | isl_pw_aff *LHS, *RHS; |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 1567 | // For unsigned comparisons we assumed the signed bit of neither operand |
| 1568 | // to be set. The comparison is equal to a signed comparison under this |
| 1569 | // assumption. |
| 1570 | bool NonNeg = ICond->isUnsigned(); |
Michael Kruse | 0865585 | 2017-07-20 12:37:02 +0000 | [diff] [blame] | 1571 | const SCEV *LeftOperand = SE.getSCEVAtScope(ICond->getOperand(0), L), |
| 1572 | *RightOperand = SE.getSCEVAtScope(ICond->getOperand(1), L); |
| 1573 | |
Tobias Grosser | ee45759 | 2017-09-24 09:25:30 +0000 | [diff] [blame] | 1574 | LeftOperand = tryForwardThroughPHI(LeftOperand, R, SE, LI, DT); |
| 1575 | RightOperand = tryForwardThroughPHI(RightOperand, R, SE, LI, DT); |
| 1576 | |
Michael Kruse | 0865585 | 2017-07-20 12:37:02 +0000 | [diff] [blame] | 1577 | switch (ICond->getPredicate()) { |
| 1578 | case ICmpInst::ICMP_ULT: |
| 1579 | ConsequenceCondSet = |
| 1580 | buildUnsignedConditionSets(S, BB, Condition, Domain, LeftOperand, |
| 1581 | RightOperand, InvalidDomainMap, true); |
| 1582 | break; |
| 1583 | case ICmpInst::ICMP_ULE: |
| 1584 | ConsequenceCondSet = |
| 1585 | buildUnsignedConditionSets(S, BB, Condition, Domain, LeftOperand, |
| 1586 | RightOperand, InvalidDomainMap, false); |
| 1587 | break; |
| 1588 | case ICmpInst::ICMP_UGT: |
| 1589 | ConsequenceCondSet = |
| 1590 | buildUnsignedConditionSets(S, BB, Condition, Domain, RightOperand, |
| 1591 | LeftOperand, InvalidDomainMap, true); |
| 1592 | break; |
| 1593 | case ICmpInst::ICMP_UGE: |
| 1594 | ConsequenceCondSet = |
| 1595 | buildUnsignedConditionSets(S, BB, Condition, Domain, RightOperand, |
| 1596 | LeftOperand, InvalidDomainMap, false); |
| 1597 | break; |
| 1598 | default: |
| 1599 | LHS = getPwAff(S, BB, InvalidDomainMap, LeftOperand, NonNeg); |
| 1600 | RHS = getPwAff(S, BB, InvalidDomainMap, RightOperand, NonNeg); |
Tobias Grosser | b948630 | 2018-03-03 19:27:54 +0000 | [diff] [blame] | 1601 | ConsequenceCondSet = buildConditionSet(ICond->getPredicate(), LHS, RHS); |
Michael Kruse | 0865585 | 2017-07-20 12:37:02 +0000 | [diff] [blame] | 1602 | break; |
| 1603 | } |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
Johannes Doerfert | 2af10e2 | 2015-11-12 03:25:01 +0000 | [diff] [blame] | 1606 | // If no terminator was given we are only looking for parameter constraints |
| 1607 | // under which @p Condition is true/false. |
| 1608 | if (!TI) |
| 1609 | ConsequenceCondSet = isl_set_params(ConsequenceCondSet); |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1610 | assert(ConsequenceCondSet); |
Johannes Doerfert | 1519491 | 2016-04-04 07:59:41 +0000 | [diff] [blame] | 1611 | ConsequenceCondSet = isl_set_coalesce( |
| 1612 | isl_set_intersect(ConsequenceCondSet, isl_set_copy(Domain))); |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1613 | |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1614 | isl_set *AlternativeCondSet = nullptr; |
Michael Kruse | f7a4a94 | 2016-05-02 12:25:36 +0000 | [diff] [blame] | 1615 | bool TooComplex = |
Tobias Grosser | 90411a9 | 2017-02-16 19:11:33 +0000 | [diff] [blame] | 1616 | isl_set_n_basic_set(ConsequenceCondSet) >= MaxDisjunctsInDomain; |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1617 | |
Michael Kruse | f7a4a94 | 2016-05-02 12:25:36 +0000 | [diff] [blame] | 1618 | if (!TooComplex) { |
Johannes Doerfert | 1519491 | 2016-04-04 07:59:41 +0000 | [diff] [blame] | 1619 | AlternativeCondSet = isl_set_subtract(isl_set_copy(Domain), |
| 1620 | isl_set_copy(ConsequenceCondSet)); |
Michael Kruse | f7a4a94 | 2016-05-02 12:25:36 +0000 | [diff] [blame] | 1621 | TooComplex = |
Tobias Grosser | 90411a9 | 2017-02-16 19:11:33 +0000 | [diff] [blame] | 1622 | isl_set_n_basic_set(AlternativeCondSet) >= MaxDisjunctsInDomain; |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1623 | } |
| 1624 | |
Michael Kruse | f7a4a94 | 2016-05-02 12:25:36 +0000 | [diff] [blame] | 1625 | if (TooComplex) { |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 1626 | S.invalidate(COMPLEXITY, TI ? TI->getDebugLoc() : DebugLoc(), |
| 1627 | TI ? TI->getParent() : nullptr /* BasicBlock */); |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1628 | isl_set_free(AlternativeCondSet); |
Johannes Doerfert | b288579 | 2016-04-26 09:20:41 +0000 | [diff] [blame] | 1629 | isl_set_free(ConsequenceCondSet); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1630 | return false; |
Johannes Doerfert | 1519491 | 2016-04-04 07:59:41 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | ConditionSets.push_back(ConsequenceCondSet); |
| 1634 | ConditionSets.push_back(isl_set_coalesce(AlternativeCondSet)); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1635 | |
| 1636 | return true; |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1639 | /// 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] | 1640 | /// |
| 1641 | /// This will fill @p ConditionSets with the conditions under which control |
| 1642 | /// will be moved from @p TI to its successors. Hence, @p ConditionSets will |
| 1643 | /// have as many elements as @p TI has successors. |
Tobias Grosser | ee45759 | 2017-09-24 09:25:30 +0000 | [diff] [blame] | 1644 | bool buildConditionSets(Scop &S, BasicBlock *BB, TerminatorInst *TI, Loop *L, |
| 1645 | __isl_keep isl_set *Domain, |
| 1646 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, |
| 1647 | SmallVectorImpl<__isl_give isl_set *> &ConditionSets) { |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1648 | if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1649 | return buildConditionSets(S, BB, SI, L, Domain, InvalidDomainMap, |
| 1650 | ConditionSets); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1651 | |
| 1652 | assert(isa<BranchInst>(TI) && "Terminator was neither branch nor switch."); |
| 1653 | |
| 1654 | if (TI->getNumSuccessors() == 1) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1655 | ConditionSets.push_back(isl_set_copy(Domain)); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 1656 | return true; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1659 | Value *Condition = getConditionFromTerminator(TI); |
| 1660 | assert(Condition && "No condition for Terminator"); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1661 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 1662 | return buildConditionSets(S, BB, Condition, TI, L, Domain, InvalidDomainMap, |
| 1663 | ConditionSets); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 1666 | ScopStmt::ScopStmt(Scop &parent, Region &R, StringRef Name, |
| 1667 | Loop *SurroundingLoop, |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 1668 | std::vector<Instruction *> EntryBlockInstructions) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1669 | : Parent(parent), InvalidDomain(nullptr), Domain(nullptr), R(&R), |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 1670 | Build(nullptr), BaseName(Name), SurroundingLoop(SurroundingLoop), |
| 1671 | Instructions(EntryBlockInstructions) {} |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 1672 | |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 1673 | ScopStmt::ScopStmt(Scop &parent, BasicBlock &bb, StringRef Name, |
| 1674 | Loop *SurroundingLoop, |
| 1675 | std::vector<Instruction *> Instructions) |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 1676 | : Parent(parent), InvalidDomain(nullptr), Domain(nullptr), BB(&bb), |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 1677 | Build(nullptr), BaseName(Name), SurroundingLoop(SurroundingLoop), |
| 1678 | Instructions(Instructions) {} |
Michael Kruse | cac948e | 2015-10-02 13:53:07 +0000 | [diff] [blame] | 1679 | |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 1680 | ScopStmt::ScopStmt(Scop &parent, isl::map SourceRel, isl::map TargetRel, |
| 1681 | isl::set NewDomain) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1682 | : Parent(parent), InvalidDomain(nullptr), Domain(NewDomain), |
| 1683 | Build(nullptr) { |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1684 | BaseName = getIslCompatibleName("CopyStmt_", "", |
| 1685 | std::to_string(parent.getCopyStmtsNum())); |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 1686 | isl::id Id = isl::id::alloc(getIslCtx(), getBaseName(), this); |
| 1687 | Domain = Domain.set_tuple_id(Id); |
| 1688 | TargetRel = TargetRel.set_tuple_id(isl::dim::in, Id); |
| 1689 | auto *Access = |
| 1690 | new MemoryAccess(this, MemoryAccess::AccessType::MUST_WRITE, TargetRel); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1691 | parent.addAccessFunction(Access); |
| 1692 | addAccess(Access); |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 1693 | SourceRel = SourceRel.set_tuple_id(isl::dim::in, Id); |
| 1694 | Access = new MemoryAccess(this, MemoryAccess::AccessType::READ, SourceRel); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1695 | parent.addAccessFunction(Access); |
| 1696 | addAccess(Access); |
| 1697 | } |
| 1698 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1699 | ScopStmt::~ScopStmt() = default; |
| 1700 | |
Tobias Grosser | a9b5bba | 2017-08-06 16:11:53 +0000 | [diff] [blame] | 1701 | std::string ScopStmt::getDomainStr() const { return Domain.to_str(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1702 | |
Tobias Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 1703 | std::string ScopStmt::getScheduleStr() const { |
Tobias Grosser | 6ad1640 | 2017-08-06 17:45:28 +0000 | [diff] [blame] | 1704 | auto *S = getSchedule().release(); |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 1705 | if (!S) |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1706 | return {}; |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1707 | auto Str = stringFromIslObj(S); |
| 1708 | isl_map_free(S); |
| 1709 | return Str; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
Tobias Grosser | 2332fa3 | 2017-08-06 15:36:48 +0000 | [diff] [blame] | 1712 | void ScopStmt::setInvalidDomain(isl::set ID) { InvalidDomain = ID; } |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 1713 | |
Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 1714 | BasicBlock *ScopStmt::getEntryBlock() const { |
| 1715 | if (isBlockStmt()) |
| 1716 | return getBasicBlock(); |
| 1717 | return getRegion()->getEntry(); |
| 1718 | } |
| 1719 | |
Tobias Grosser | f567e1a | 2015-02-19 22:16:12 +0000 | [diff] [blame] | 1720 | unsigned ScopStmt::getNumIterators() const { return NestLoops.size(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1721 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1722 | const char *ScopStmt::getBaseName() const { return BaseName.c_str(); } |
| 1723 | |
Johannes Doerfert | 2b92a0e | 2016-05-10 14:00:57 +0000 | [diff] [blame] | 1724 | Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const { |
Sebastian Pop | 860e021 | 2013-02-15 21:26:44 +0000 | [diff] [blame] | 1725 | return NestLoops[Dimension]; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 1728 | isl::ctx ScopStmt::getIslCtx() const { return Parent.getIslCtx(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1729 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1730 | isl::set ScopStmt::getDomain() const { return Domain; } |
Tobias Grosser | d5a7bfc | 2011-05-06 19:52:19 +0000 | [diff] [blame] | 1731 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1732 | isl::space ScopStmt::getDomainSpace() const { return Domain.get_space(); } |
Tobias Grosser | 78d8a3d | 2012-01-17 20:34:23 +0000 | [diff] [blame] | 1733 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 1734 | isl::id ScopStmt::getDomainId() const { return Domain.get_tuple_id(); } |
Tobias Grosser | cd95b77 | 2012-08-30 11:49:38 +0000 | [diff] [blame] | 1735 | |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1736 | void ScopStmt::printInstructions(raw_ostream &OS) const { |
| 1737 | OS << "Instructions {\n"; |
| 1738 | |
| 1739 | for (Instruction *Inst : Instructions) |
| 1740 | OS.indent(16) << *Inst << "\n"; |
| 1741 | |
Michael Kruse | e52ebd1 | 2017-07-22 16:44:39 +0000 | [diff] [blame] | 1742 | OS.indent(12) << "}\n"; |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 1745 | void ScopStmt::print(raw_ostream &OS, bool PrintInstructions) const { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1746 | OS << "\t" << getBaseName() << "\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1747 | OS.indent(12) << "Domain :=\n"; |
| 1748 | |
| 1749 | if (Domain) { |
| 1750 | OS.indent(16) << getDomainStr() << ";\n"; |
| 1751 | } else |
| 1752 | OS.indent(16) << "n/a\n"; |
| 1753 | |
Tobias Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 1754 | OS.indent(12) << "Schedule :=\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1755 | |
| 1756 | if (Domain) { |
Tobias Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 1757 | OS.indent(16) << getScheduleStr() << ";\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1758 | } else |
| 1759 | OS.indent(16) << "n/a\n"; |
| 1760 | |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1761 | for (MemoryAccess *Access : MemAccs) |
| 1762 | Access->print(OS); |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1763 | |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 1764 | if (PrintInstructions) |
Tobias Grosser | d5fcbef | 2017-05-27 04:40:18 +0000 | [diff] [blame] | 1765 | printInstructions(OS.indent(12)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 1768 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Michael Kruse | e186013 | 2017-07-21 15:54:13 +0000 | [diff] [blame] | 1769 | LLVM_DUMP_METHOD void ScopStmt::dump() const { print(dbgs(), true); } |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 1770 | #endif |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1771 | |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1772 | void ScopStmt::removeAccessData(MemoryAccess *MA) { |
| 1773 | if (MA->isRead() && MA->isOriginalValueKind()) { |
| 1774 | bool Found = ValueReads.erase(MA->getAccessValue()); |
| 1775 | (void)Found; |
| 1776 | assert(Found && "Expected access data not found"); |
| 1777 | } |
| 1778 | if (MA->isWrite() && MA->isOriginalValueKind()) { |
| 1779 | bool Found = ValueWrites.erase(cast<Instruction>(MA->getAccessValue())); |
| 1780 | (void)Found; |
| 1781 | assert(Found && "Expected access data not found"); |
| 1782 | } |
| 1783 | if (MA->isWrite() && MA->isOriginalAnyPHIKind()) { |
| 1784 | bool Found = PHIWrites.erase(cast<PHINode>(MA->getAccessInstruction())); |
| 1785 | (void)Found; |
| 1786 | assert(Found && "Expected access data not found"); |
| 1787 | } |
Michael Kruse | 3562f27 | 2017-07-20 16:47:57 +0000 | [diff] [blame] | 1788 | if (MA->isRead() && MA->isOriginalAnyPHIKind()) { |
| 1789 | bool Found = PHIReads.erase(cast<PHINode>(MA->getAccessInstruction())); |
| 1790 | (void)Found; |
| 1791 | assert(Found && "Expected access data not found"); |
| 1792 | } |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 1795 | void ScopStmt::removeMemoryAccess(MemoryAccess *MA) { |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1796 | // Remove the memory accesses from this statement together with all scalar |
| 1797 | // accesses that were caused by it. MemoryKind::Value READs have no access |
| 1798 | // instruction, hence would not be removed by this function. However, it is |
| 1799 | // only used for invariant LoadInst accesses, its arguments are always affine, |
| 1800 | // hence synthesizable, and therefore there are no MemoryKind::Value READ |
| 1801 | // accesses to be removed. |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 1802 | auto Predicate = [&](MemoryAccess *Acc) { |
| 1803 | return Acc->getAccessInstruction() == MA->getAccessInstruction(); |
| 1804 | }; |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1805 | for (auto *MA : MemAccs) { |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 1806 | if (Predicate(MA)) { |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1807 | removeAccessData(MA); |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 1808 | Parent.removeAccessData(MA); |
| 1809 | } |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1810 | } |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 1811 | MemAccs.erase(std::remove_if(MemAccs.begin(), MemAccs.end(), Predicate), |
| 1812 | MemAccs.end()); |
| 1813 | InstructionToAccess.erase(MA->getAccessInstruction()); |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
Michael Kruse | 192e7f7 | 2018-04-09 23:13:05 +0000 | [diff] [blame] | 1816 | void ScopStmt::removeSingleMemoryAccess(MemoryAccess *MA, bool AfterHoisting) { |
| 1817 | if (AfterHoisting) { |
| 1818 | auto MAIt = std::find(MemAccs.begin(), MemAccs.end(), MA); |
| 1819 | assert(MAIt != MemAccs.end()); |
| 1820 | MemAccs.erase(MAIt); |
Michael Kruse | 0446d81 | 2017-03-10 16:05:24 +0000 | [diff] [blame] | 1821 | |
Michael Kruse | 192e7f7 | 2018-04-09 23:13:05 +0000 | [diff] [blame] | 1822 | removeAccessData(MA); |
| 1823 | Parent.removeAccessData(MA); |
| 1824 | } |
Michael Kruse | e60eca7 | 2017-05-11 22:56:12 +0000 | [diff] [blame] | 1825 | |
Michael Kruse | 0446d81 | 2017-03-10 16:05:24 +0000 | [diff] [blame] | 1826 | auto It = InstructionToAccess.find(MA->getAccessInstruction()); |
| 1827 | if (It != InstructionToAccess.end()) { |
| 1828 | It->second.remove(MA); |
| 1829 | if (It->second.empty()) |
| 1830 | InstructionToAccess.erase(MA->getAccessInstruction()); |
| 1831 | } |
| 1832 | } |
| 1833 | |
Michael Kruse | 07e8c36 | 2017-07-24 12:43:27 +0000 | [diff] [blame] | 1834 | MemoryAccess *ScopStmt::ensureValueRead(Value *V) { |
| 1835 | MemoryAccess *Access = lookupInputAccessOf(V); |
| 1836 | if (Access) |
| 1837 | return Access; |
| 1838 | |
| 1839 | ScopArrayInfo *SAI = |
| 1840 | Parent.getOrCreateScopArrayInfo(V, V->getType(), {}, MemoryKind::Value); |
| 1841 | Access = new MemoryAccess(this, nullptr, MemoryAccess::READ, V, V->getType(), |
| 1842 | true, {}, {}, V, MemoryKind::Value); |
| 1843 | Parent.addAccessFunction(Access); |
| 1844 | Access->buildAccessRelation(SAI); |
| 1845 | addAccess(Access); |
| 1846 | Parent.addAccessData(Access); |
| 1847 | return Access; |
| 1848 | } |
| 1849 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1850 | raw_ostream &polly::operator<<(raw_ostream &OS, const ScopStmt &S) { |
| 1851 | S.print(OS, PollyPrintInstructions); |
| 1852 | return OS; |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 1853 | } |
| 1854 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1855 | //===----------------------------------------------------------------------===// |
| 1856 | /// Scop class implement |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 1857 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 1858 | void Scop::setContext(isl::set NewContext) { |
| 1859 | Context = NewContext.align_params(Context.get_space()); |
Tobias Grosser | ff9b54d | 2011-11-15 11:38:44 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1862 | namespace { |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1863 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1864 | /// Remap parameter values but keep AddRecs valid wrt. invariant loads. |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 1865 | struct SCEVSensitiveParameterRewriter |
Tobias Grosser | 278f9e7 | 2016-11-26 17:58:40 +0000 | [diff] [blame] | 1866 | : public SCEVRewriteVisitor<SCEVSensitiveParameterRewriter> { |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1867 | const ValueToValueMap &VMap; |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 1868 | |
| 1869 | public: |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1870 | SCEVSensitiveParameterRewriter(const ValueToValueMap &VMap, |
| 1871 | ScalarEvolution &SE) |
Tobias Grosser | 278f9e7 | 2016-11-26 17:58:40 +0000 | [diff] [blame] | 1872 | : SCEVRewriteVisitor(SE), VMap(VMap) {} |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 1873 | |
| 1874 | static const SCEV *rewrite(const SCEV *E, ScalarEvolution &SE, |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1875 | const ValueToValueMap &VMap) { |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 1876 | SCEVSensitiveParameterRewriter SSPR(VMap, SE); |
| 1877 | return SSPR.visit(E); |
| 1878 | } |
| 1879 | |
Johannes Doerfert | d6fc070 | 2015-11-03 16:47:58 +0000 | [diff] [blame] | 1880 | const SCEV *visitAddRecExpr(const SCEVAddRecExpr *E) { |
| 1881 | auto *Start = visit(E->getStart()); |
| 1882 | auto *AddRec = SE.getAddRecExpr(SE.getConstant(E->getType(), 0), |
| 1883 | visit(E->getStepRecurrence(SE)), |
| 1884 | E->getLoop(), SCEV::FlagAnyWrap); |
| 1885 | return SE.getAddExpr(Start, AddRec); |
| 1886 | } |
| 1887 | |
| 1888 | const SCEV *visitUnknown(const SCEVUnknown *E) { |
| 1889 | if (auto *NewValue = VMap.lookup(E->getValue())) |
| 1890 | return SE.getUnknown(NewValue); |
| 1891 | return E; |
| 1892 | } |
| 1893 | }; |
| 1894 | |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1895 | /// Check whether we should remap a SCEV expression. |
| 1896 | struct SCEVFindInsideScop : public SCEVTraversal<SCEVFindInsideScop> { |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1897 | const ValueToValueMap &VMap; |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1898 | bool FoundInside = false; |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1899 | const Scop *S; |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1900 | |
| 1901 | public: |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1902 | SCEVFindInsideScop(const ValueToValueMap &VMap, ScalarEvolution &SE, |
| 1903 | const Scop *S) |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1904 | : SCEVTraversal(*this), VMap(VMap), S(S) {} |
| 1905 | |
| 1906 | static bool hasVariant(const SCEV *E, ScalarEvolution &SE, |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1907 | const ValueToValueMap &VMap, const Scop *S) { |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1908 | SCEVFindInsideScop SFIS(VMap, SE, S); |
| 1909 | SFIS.visitAll(E); |
| 1910 | return SFIS.FoundInside; |
| 1911 | } |
| 1912 | |
| 1913 | bool follow(const SCEV *E) { |
| 1914 | if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(E)) { |
| 1915 | FoundInside |= S->getRegion().contains(AddRec->getLoop()); |
| 1916 | } else if (auto *Unknown = dyn_cast<SCEVUnknown>(E)) { |
| 1917 | if (Instruction *I = dyn_cast<Instruction>(Unknown->getValue())) |
| 1918 | FoundInside |= S->getRegion().contains(I) && !VMap.count(I); |
| 1919 | } |
| 1920 | return !FoundInside; |
| 1921 | } |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1922 | |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1923 | bool isDone() { return FoundInside; } |
| 1924 | }; |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 1925 | } // end anonymous namespace |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1926 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 1927 | const SCEV *Scop::getRepresentingInvariantLoadSCEV(const SCEV *E) const { |
Eli Friedman | 5e589ea | 2017-06-20 22:53:02 +0000 | [diff] [blame] | 1928 | // Check whether it makes sense to rewrite the SCEV. (ScalarEvolution |
| 1929 | // doesn't like addition between an AddRec and an expression that |
| 1930 | // doesn't have a dominance relationship with it.) |
| 1931 | if (SCEVFindInsideScop::hasVariant(E, *SE, InvEquivClassVMap, this)) |
| 1932 | return E; |
| 1933 | |
| 1934 | // Rewrite SCEV. |
| 1935 | return SCEVSensitiveParameterRewriter::rewrite(E, *SE, InvEquivClassVMap); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 1938 | // This table of function names is used to translate parameter names in more |
| 1939 | // human-readable names. This makes it easier to interpret Polly analysis |
| 1940 | // results. |
| 1941 | StringMap<std::string> KnownNames = { |
| 1942 | {"_Z13get_global_idj", "global_id"}, |
| 1943 | {"_Z12get_local_idj", "local_id"}, |
| 1944 | {"_Z15get_global_sizej", "global_size"}, |
| 1945 | {"_Z14get_local_sizej", "local_size"}, |
| 1946 | {"_Z12get_work_dimv", "work_dim"}, |
| 1947 | {"_Z17get_global_offsetj", "global_offset"}, |
| 1948 | {"_Z12get_group_idj", "group_id"}, |
| 1949 | {"_Z14get_num_groupsj", "num_groups"}, |
| 1950 | }; |
| 1951 | |
| 1952 | static std::string getCallParamName(CallInst *Call) { |
| 1953 | std::string Result; |
| 1954 | raw_string_ostream OS(Result); |
| 1955 | std::string Name = Call->getCalledFunction()->getName(); |
| 1956 | |
| 1957 | auto Iterator = KnownNames.find(Name); |
| 1958 | if (Iterator != KnownNames.end()) |
Tobias Grosser | dff902f | 2017-06-01 12:46:51 +0000 | [diff] [blame] | 1959 | Name = "__" + Iterator->getValue(); |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 1960 | OS << Name; |
| 1961 | for (auto &Operand : Call->arg_operands()) { |
| 1962 | ConstantInt *Op = cast<ConstantInt>(&Operand); |
| 1963 | OS << "_" << Op->getValue(); |
| 1964 | } |
| 1965 | OS.flush(); |
| 1966 | return Result; |
| 1967 | } |
| 1968 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 1969 | void Scop::createParameterId(const SCEV *Parameter) { |
| 1970 | assert(Parameters.count(Parameter)); |
| 1971 | assert(!ParameterIds.count(Parameter)); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 1972 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 1973 | std::string ParameterName = "p_" + std::to_string(getNumParams() - 1); |
Tobias Grosser | b39c96a | 2015-11-17 11:54:51 +0000 | [diff] [blame] | 1974 | |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 1975 | if (const SCEVUnknown *ValueParameter = dyn_cast<SCEVUnknown>(Parameter)) { |
| 1976 | Value *Val = ValueParameter->getValue(); |
| 1977 | CallInst *Call = dyn_cast<CallInst>(Val); |
Tobias Grosser | 8f99c16 | 2011-11-15 11:38:55 +0000 | [diff] [blame] | 1978 | |
Tobias Grosser | f5e7e60 | 2017-05-27 15:18:46 +0000 | [diff] [blame] | 1979 | if (Call && isConstCall(Call)) { |
| 1980 | ParameterName = getCallParamName(Call); |
| 1981 | } else if (UseInstructionNames) { |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 1982 | // If this parameter references a specific Value and this value has a name |
| 1983 | // we use this name as it is likely to be unique and more useful than just |
| 1984 | // a number. |
| 1985 | if (Val->hasName()) |
| 1986 | ParameterName = Val->getName(); |
| 1987 | else if (LoadInst *LI = dyn_cast<LoadInst>(Val)) { |
| 1988 | auto *LoadOrigin = LI->getPointerOperand()->stripInBoundsOffsets(); |
| 1989 | if (LoadOrigin->hasName()) { |
| 1990 | ParameterName += "_loaded_from_"; |
| 1991 | ParameterName += |
| 1992 | LI->getPointerOperand()->stripInBoundsOffsets()->getName(); |
| 1993 | } |
Tobias Grosser | b39c96a | 2015-11-17 11:54:51 +0000 | [diff] [blame] | 1994 | } |
| 1995 | } |
Tobias Grosser | 8f99c16 | 2011-11-15 11:38:55 +0000 | [diff] [blame] | 1996 | |
Tobias Grosser | e2ccc3f | 2017-05-03 20:08:52 +0000 | [diff] [blame] | 1997 | ParameterName = getIslCompatibleName("", ParameterName, ""); |
| 1998 | } |
Tobias Grosser | 2ea7c6e | 2016-07-01 13:40:28 +0000 | [diff] [blame] | 1999 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 2000 | isl::id Id = isl::id::alloc(getIslCtx(), ParameterName, |
Tobias Grosser | 6e78cc6 | 2017-08-13 17:54:51 +0000 | [diff] [blame] | 2001 | const_cast<void *>((const void *)Parameter)); |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2002 | ParameterIds[Parameter] = Id; |
| 2003 | } |
| 2004 | |
| 2005 | void Scop::addParams(const ParameterSetTy &NewParameters) { |
| 2006 | for (const SCEV *Parameter : NewParameters) { |
| 2007 | // Normalize the SCEV to get the representing element for an invariant load. |
| 2008 | Parameter = extractConstantFactor(Parameter, *SE).second; |
| 2009 | Parameter = getRepresentingInvariantLoadSCEV(Parameter); |
| 2010 | |
| 2011 | if (Parameters.insert(Parameter)) |
| 2012 | createParameterId(Parameter); |
| 2013 | } |
| 2014 | } |
| 2015 | |
Tobias Grosser | 9a63570 | 2017-08-06 19:31:27 +0000 | [diff] [blame] | 2016 | isl::id Scop::getIdForParam(const SCEV *Parameter) const { |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2017 | // Normalize the SCEV to get the representing element for an invariant load. |
| 2018 | Parameter = getRepresentingInvariantLoadSCEV(Parameter); |
Tobias Grosser | 6e78cc6 | 2017-08-13 17:54:51 +0000 | [diff] [blame] | 2019 | return ParameterIds.lookup(Parameter); |
Tobias Grosser | 76c2e32 | 2011-11-07 12:58:59 +0000 | [diff] [blame] | 2020 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2021 | |
Tobias Grosser | 232fdad | 2017-08-06 20:19:26 +0000 | [diff] [blame] | 2022 | isl::set Scop::addNonEmptyDomainConstraints(isl::set C) const { |
Tobias Grosser | 31df6f3 | 2017-08-06 21:42:25 +0000 | [diff] [blame] | 2023 | isl_set *DomainContext = isl_union_set_params(getDomains().release()); |
Tobias Grosser | 232fdad | 2017-08-06 20:19:26 +0000 | [diff] [blame] | 2024 | return isl::manage(isl_set_intersect_params(C.release(), DomainContext)); |
Johannes Doerfert | 5d5b306 | 2015-08-20 18:06:30 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
Johannes Doerfert | e0b0807 | 2016-05-23 12:43:44 +0000 | [diff] [blame] | 2027 | bool Scop::isDominatedBy(const DominatorTree &DT, BasicBlock *BB) const { |
| 2028 | return DT.dominates(BB, getEntry()); |
| 2029 | } |
| 2030 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2031 | void Scop::addUserAssumptions( |
| 2032 | AssumptionCache &AC, DominatorTree &DT, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2033 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2034 | for (auto &Assumption : AC.assumptions()) { |
| 2035 | auto *CI = dyn_cast_or_null<CallInst>(Assumption); |
| 2036 | if (!CI || CI->getNumArgOperands() != 1) |
Johannes Doerfert | 2af10e2 | 2015-11-12 03:25:01 +0000 | [diff] [blame] | 2037 | continue; |
Johannes Doerfert | 2b92a0e | 2016-05-10 14:00:57 +0000 | [diff] [blame] | 2038 | |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2039 | bool InScop = contains(CI); |
| 2040 | if (!InScop && !isDominatedBy(DT, CI->getParent())) |
| 2041 | continue; |
Johannes Doerfert | 2af10e2 | 2015-11-12 03:25:01 +0000 | [diff] [blame] | 2042 | |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2043 | auto *L = LI.getLoopFor(CI->getParent()); |
| 2044 | auto *Val = CI->getArgOperand(0); |
| 2045 | ParameterSetTy DetectedParams; |
| 2046 | if (!isAffineConstraint(Val, &R, L, *SE, DetectedParams)) { |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 2047 | ORE.emit( |
| 2048 | OptimizationRemarkAnalysis(DEBUG_TYPE, "IgnoreUserAssumption", CI) |
| 2049 | << "Non-affine user assumption ignored."); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2050 | continue; |
Michael Kruse | 7037fde | 2016-12-15 09:25:14 +0000 | [diff] [blame] | 2051 | } |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2052 | |
| 2053 | // Collect all newly introduced parameters. |
| 2054 | ParameterSetTy NewParams; |
| 2055 | for (auto *Param : DetectedParams) { |
| 2056 | Param = extractConstantFactor(Param, *SE).second; |
| 2057 | Param = getRepresentingInvariantLoadSCEV(Param); |
| 2058 | if (Parameters.count(Param)) |
| 2059 | continue; |
| 2060 | NewParams.insert(Param); |
| 2061 | } |
| 2062 | |
| 2063 | SmallVector<isl_set *, 2> ConditionSets; |
| 2064 | auto *TI = InScop ? CI->getParent()->getTerminator() : nullptr; |
Michael Kruse | 1df1aac | 2017-07-26 13:25:28 +0000 | [diff] [blame] | 2065 | BasicBlock *BB = InScop ? CI->getParent() : getRegion().getEntry(); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2066 | auto *Dom = InScop ? DomainMap[BB].copy() : Context.copy(); |
Michael Kruse | 1df1aac | 2017-07-26 13:25:28 +0000 | [diff] [blame] | 2067 | assert(Dom && "Cannot propagate a nullptr."); |
| 2068 | bool Valid = buildConditionSets(*this, BB, Val, TI, L, Dom, |
| 2069 | InvalidDomainMap, ConditionSets); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 2070 | isl_set_free(Dom); |
| 2071 | |
| 2072 | if (!Valid) |
| 2073 | continue; |
| 2074 | |
| 2075 | isl_set *AssumptionCtx = nullptr; |
| 2076 | if (InScop) { |
| 2077 | AssumptionCtx = isl_set_complement(isl_set_params(ConditionSets[1])); |
| 2078 | isl_set_free(ConditionSets[0]); |
| 2079 | } else { |
| 2080 | AssumptionCtx = isl_set_complement(ConditionSets[1]); |
| 2081 | AssumptionCtx = isl_set_intersect(AssumptionCtx, ConditionSets[0]); |
| 2082 | } |
| 2083 | |
| 2084 | // Project out newly introduced parameters as they are not otherwise useful. |
| 2085 | if (!NewParams.empty()) { |
| 2086 | for (unsigned u = 0; u < isl_set_n_param(AssumptionCtx); u++) { |
| 2087 | auto *Id = isl_set_get_dim_id(AssumptionCtx, isl_dim_param, u); |
| 2088 | auto *Param = static_cast<const SCEV *>(isl_id_get_user(Id)); |
| 2089 | isl_id_free(Id); |
| 2090 | |
| 2091 | if (!NewParams.count(Param)) |
| 2092 | continue; |
| 2093 | |
| 2094 | AssumptionCtx = |
| 2095 | isl_set_project_out(AssumptionCtx, isl_dim_param, u--, 1); |
| 2096 | } |
| 2097 | } |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 2098 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "UserAssumption", CI) |
| 2099 | << "Use user assumption: " << stringFromIslObj(AssumptionCtx)); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2100 | Context = Context.intersect(isl::manage(AssumptionCtx)); |
Johannes Doerfert | 2af10e2 | 2015-11-12 03:25:01 +0000 | [diff] [blame] | 2101 | } |
| 2102 | } |
| 2103 | |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2104 | void Scop::addUserContext() { |
| 2105 | if (UserContextStr.empty()) |
| 2106 | return; |
| 2107 | |
Tobias Grosser | 9b9c701 | 2018-05-28 07:45:25 +0000 | [diff] [blame] | 2108 | isl::set UserContext = isl::set(getIslCtx(), UserContextStr.c_str()); |
| 2109 | isl::space Space = getParamSpace(); |
| 2110 | if (Space.dim(isl::dim::param) != UserContext.dim(isl::dim::param)) { |
| 2111 | std::string SpaceStr = Space.to_str(); |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2112 | errs() << "Error: the context provided in -polly-context has not the same " |
| 2113 | << "number of dimensions than the computed context. Due to this " |
| 2114 | << "mismatch, the -polly-context option is ignored. Please provide " |
| 2115 | << "the context in the parameter space: " << SpaceStr << ".\n"; |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2116 | return; |
| 2117 | } |
| 2118 | |
Tobias Grosser | 9b9c701 | 2018-05-28 07:45:25 +0000 | [diff] [blame] | 2119 | for (unsigned i = 0; i < Space.dim(isl::dim::param); i++) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2120 | std::string NameContext = Context.get_dim_name(isl::dim::param, i); |
Tobias Grosser | 9b9c701 | 2018-05-28 07:45:25 +0000 | [diff] [blame] | 2121 | std::string NameUserContext = UserContext.get_dim_name(isl::dim::param, i); |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2122 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2123 | if (NameContext != NameUserContext) { |
Tobias Grosser | 9b9c701 | 2018-05-28 07:45:25 +0000 | [diff] [blame] | 2124 | std::string SpaceStr = Space.to_str(); |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2125 | errs() << "Error: the name of dimension " << i |
| 2126 | << " provided in -polly-context " |
| 2127 | << "is '" << NameUserContext << "', but the name in the computed " |
| 2128 | << "context is '" << NameContext |
| 2129 | << "'. Due to this name mismatch, " |
| 2130 | << "the -polly-context option is ignored. Please provide " |
| 2131 | << "the context in the parameter space: " << SpaceStr << ".\n"; |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2132 | return; |
| 2133 | } |
| 2134 | |
Tobias Grosser | 9b9c701 | 2018-05-28 07:45:25 +0000 | [diff] [blame] | 2135 | UserContext = UserContext.set_dim_id(isl::dim::param, i, |
| 2136 | Space.get_dim_id(isl::dim::param, i)); |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2137 | } |
| 2138 | |
Tobias Grosser | 9b9c701 | 2018-05-28 07:45:25 +0000 | [diff] [blame] | 2139 | Context = Context.intersect(UserContext); |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2142 | void Scop::buildInvariantEquivalenceClasses() { |
Johannes Doerfert | 96e5471 | 2016-02-07 17:30:13 +0000 | [diff] [blame] | 2143 | DenseMap<std::pair<const SCEV *, Type *>, LoadInst *> EquivClasses; |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 2144 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2145 | const InvariantLoadsSetTy &RIL = getRequiredInvariantLoads(); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2146 | for (LoadInst *LInst : RIL) { |
| 2147 | const SCEV *PointerSCEV = SE->getSCEV(LInst->getPointerOperand()); |
| 2148 | |
Johannes Doerfert | 96e5471 | 2016-02-07 17:30:13 +0000 | [diff] [blame] | 2149 | Type *Ty = LInst->getType(); |
| 2150 | LoadInst *&ClassRep = EquivClasses[std::make_pair(PointerSCEV, Ty)]; |
Johannes Doerfert | fc4bfc4 | 2015-11-11 04:30:07 +0000 | [diff] [blame] | 2151 | if (ClassRep) { |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 2152 | InvEquivClassVMap[LInst] = ClassRep; |
Johannes Doerfert | fc4bfc4 | 2015-11-11 04:30:07 +0000 | [diff] [blame] | 2153 | continue; |
| 2154 | } |
| 2155 | |
| 2156 | ClassRep = LInst; |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 2157 | InvariantEquivClasses.emplace_back( |
| 2158 | InvariantEquivClassTy{PointerSCEV, MemoryAccessList(), nullptr, Ty}); |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 2159 | } |
| 2160 | } |
| 2161 | |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2162 | void Scop::buildContext() { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2163 | isl::space Space = isl::space::params_alloc(getIslCtx(), 0); |
| 2164 | Context = isl::set::universe(Space); |
| 2165 | InvalidContext = isl::set::empty(Space); |
| 2166 | AssumedContext = isl::set::universe(Space); |
Tobias Grosser | 0e27e24 | 2011-10-06 00:03:48 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 2169 | void Scop::addParameterBounds() { |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 2170 | unsigned PDim = 0; |
| 2171 | for (auto *Parameter : Parameters) { |
| 2172 | ConstantRange SRange = SE->getSignedRange(Parameter); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2173 | Context = addRangeBoundsToSet(Context, SRange, PDim++, isl::dim::param); |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 2174 | } |
| 2175 | } |
| 2176 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2177 | static std::vector<isl::id> getFortranArrayIds(Scop::array_range Arrays) { |
| 2178 | std::vector<isl::id> OutermostSizeIds; |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2179 | for (auto Array : Arrays) { |
| 2180 | // To check if an array is a Fortran array, we check if it has a isl_pw_aff |
| 2181 | // for its outermost dimension. Fortran arrays will have this since the |
| 2182 | // outermost dimension size can be picked up from their runtime description. |
| 2183 | // TODO: actually need to check if it has a FAD, but for now this works. |
| 2184 | if (Array->getNumberOfDimensions() > 0) { |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2185 | isl::pw_aff PwAff = Array->getDimensionSizePw(0); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2186 | if (!PwAff) |
| 2187 | continue; |
| 2188 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2189 | isl::id Id = |
| 2190 | isl::manage(isl_pw_aff_get_dim_id(PwAff.get(), isl_dim_param, 0)); |
| 2191 | assert(!Id.is_null() && |
| 2192 | "Invalid Id for PwAff expression in Fortran array"); |
| 2193 | Id.dump(); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2194 | OutermostSizeIds.push_back(Id); |
| 2195 | } |
| 2196 | } |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2197 | return OutermostSizeIds; |
| 2198 | } |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2199 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2200 | // The FORTRAN array size parameters are known to be non-negative. |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2201 | static isl::set boundFortranArrayParams(isl::set Context, |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2202 | Scop::array_range Arrays) { |
| 2203 | std::vector<isl::id> OutermostSizeIds; |
| 2204 | OutermostSizeIds = getFortranArrayIds(Arrays); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2205 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2206 | for (isl::id Id : OutermostSizeIds) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2207 | int dim = Context.find_dim_by_id(isl::dim::param, Id); |
| 2208 | Context = Context.lower_bound_si(isl::dim::param, dim, 0); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2209 | } |
| 2210 | |
| 2211 | return Context; |
| 2212 | } |
| 2213 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 2214 | void Scop::realignParams() { |
Tobias Grosser | 5842dee | 2017-03-17 13:00:53 +0000 | [diff] [blame] | 2215 | if (PollyIgnoreParamBounds) |
| 2216 | return; |
| 2217 | |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2218 | // Add all parameters into a common model. |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2219 | isl::space Space = getFullParamSpace(); |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2220 | |
| 2221 | // Align the parameters of all data structures to the model. |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2222 | Context = Context.align_params(Space); |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 2223 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 2224 | // Bound the size of the fortran array dimensions. |
| 2225 | Context = boundFortranArrayParams(Context, arrays()); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 2226 | |
Johannes Doerfert | a60ad84 | 2016-05-10 12:18:22 +0000 | [diff] [blame] | 2227 | // As all parameters are known add bounds to them. |
| 2228 | addParameterBounds(); |
| 2229 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 2230 | for (ScopStmt &Stmt : *this) |
| 2231 | Stmt.realignParams(); |
Johannes Doerfert | 06445ded | 2016-06-02 15:07:41 +0000 | [diff] [blame] | 2232 | // Simplify the schedule according to the context too. |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2233 | Schedule = Schedule.gist_domain_params(getContext()); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 2234 | } |
| 2235 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2236 | static isl::set simplifyAssumptionContext(isl::set AssumptionContext, |
| 2237 | const Scop &S) { |
Tobias Grosser | cdbe5c9 | 2017-01-06 17:30:34 +0000 | [diff] [blame] | 2238 | // If we have modeled all blocks in the SCoP that have side effects we can |
| 2239 | // simplify the context with the constraints that are needed for anything to |
| 2240 | // be executed at all. However, if we have error blocks in the SCoP we already |
| 2241 | // assumed some parameter combinations cannot occur and removed them from the |
Johannes Doerfert | f85ad04 | 2015-11-08 20:16:39 +0000 | [diff] [blame] | 2242 | // domains, thus we cannot use the remaining domain to simplify the |
| 2243 | // assumptions. |
| 2244 | if (!S.hasErrorBlock()) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2245 | auto DomainParameters = S.getDomains().params(); |
| 2246 | AssumptionContext = AssumptionContext.gist_params(DomainParameters); |
Johannes Doerfert | f85ad04 | 2015-11-08 20:16:39 +0000 | [diff] [blame] | 2247 | } |
| 2248 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2249 | AssumptionContext = AssumptionContext.gist_params(S.getContext()); |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 2250 | return AssumptionContext; |
| 2251 | } |
| 2252 | |
| 2253 | void Scop::simplifyContexts() { |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2254 | // The parameter constraints of the iteration domains give us a set of |
| 2255 | // constraints that need to hold for all cases where at least a single |
| 2256 | // statement iteration is executed in the whole scop. We now simplify the |
| 2257 | // assumed context under the assumption that such constraints hold and at |
| 2258 | // least a single statement iteration is executed. For cases where no |
| 2259 | // statement instances are executed, the assumptions we have taken about |
| 2260 | // the executed code do not matter and can be changed. |
| 2261 | // |
| 2262 | // WARNING: This only holds if the assumptions we have taken do not reduce |
| 2263 | // the set of statement instances that are executed. Otherwise we |
| 2264 | // may run into a case where the iteration domains suggest that |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 2265 | // for a certain set of parameter constraints no code is executed, |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2266 | // but in the original program some computation would have been |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 2267 | // performed. In such a case, modifying the run-time conditions and |
| 2268 | // possibly influencing the run-time check may cause certain scops |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2269 | // to not be executed. |
| 2270 | // |
| 2271 | // Example: |
| 2272 | // |
| 2273 | // When delinearizing the following code: |
| 2274 | // |
| 2275 | // for (long i = 0; i < 100; i++) |
| 2276 | // for (long j = 0; j < m; j++) |
| 2277 | // A[i+p][j] = 1.0; |
| 2278 | // |
| 2279 | // 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] | 2280 | // 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] | 2281 | // 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] | 2282 | AssumedContext = simplifyAssumptionContext(AssumedContext, *this); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2283 | InvalidContext = InvalidContext.align_params(getParamSpace()); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 2284 | } |
| 2285 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2286 | /// Add the minimal/maximal access in @p Set to @p User. |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2287 | static isl::stat |
| 2288 | buildMinMaxAccess(isl::set Set, Scop::MinMaxVectorTy &MinMaxAccesses, Scop &S) { |
| 2289 | isl::pw_multi_aff MinPMA, MaxPMA; |
| 2290 | isl::pw_aff LastDimAff; |
| 2291 | isl::aff OneAff; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2292 | unsigned Pos; |
| 2293 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2294 | Set = Set.remove_divs(); |
Michael Kruse | e330071 | 2018-05-09 16:23:56 +0000 | [diff] [blame] | 2295 | polly::simplify(Set); |
Johannes Doerfert | 6296d95 | 2016-04-22 11:38:19 +0000 | [diff] [blame] | 2296 | |
Philip Pfaffe | 9375d57 | 2018-05-16 14:05:03 +0000 | [diff] [blame] | 2297 | if (Set.n_basic_set() > RunTimeChecksMaxAccessDisjuncts) |
Michael Kruse | e330071 | 2018-05-09 16:23:56 +0000 | [diff] [blame] | 2298 | Set = Set.simple_hull(); |
Johannes Doerfert | 6296d95 | 2016-04-22 11:38:19 +0000 | [diff] [blame] | 2299 | |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 2300 | // Restrict the number of parameters involved in the access as the lexmin/ |
| 2301 | // lexmax computation will take too long if this number is high. |
| 2302 | // |
| 2303 | // Experiments with a simple test case using an i7 4800MQ: |
| 2304 | // |
| 2305 | // #Parameters involved | Time (in sec) |
| 2306 | // 6 | 0.01 |
| 2307 | // 7 | 0.04 |
| 2308 | // 8 | 0.12 |
| 2309 | // 9 | 0.40 |
| 2310 | // 10 | 1.54 |
| 2311 | // 11 | 6.78 |
| 2312 | // 12 | 30.38 |
| 2313 | // |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2314 | if (isl_set_n_param(Set.get()) > RunTimeChecksMaxParameters) { |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 2315 | unsigned InvolvedParams = 0; |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2316 | for (unsigned u = 0, e = isl_set_n_param(Set.get()); u < e; u++) |
| 2317 | if (Set.involves_dims(isl::dim::param, u, 1)) |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 2318 | InvolvedParams++; |
| 2319 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2320 | if (InvolvedParams > RunTimeChecksMaxParameters) |
| 2321 | return isl::stat::error; |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 2322 | } |
| 2323 | |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 2324 | MinPMA = Set.lexmin_pw_multi_aff(); |
| 2325 | MaxPMA = Set.lexmax_pw_multi_aff(); |
Tobias Grosser | 45e9fd1 | 2017-05-19 03:45:00 +0000 | [diff] [blame] | 2326 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2327 | MinPMA = MinPMA.coalesce(); |
| 2328 | MaxPMA = MaxPMA.coalesce(); |
Johannes Doerfert | 219b20e | 2014-10-07 14:37:59 +0000 | [diff] [blame] | 2329 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2330 | // Adjust the last dimension of the maximal access by one as we want to |
| 2331 | // enclose the accessed memory region by MinPMA and MaxPMA. The pointer |
| 2332 | // we test during code generation might now point after the end of the |
| 2333 | // allocated array but we will never dereference it anyway. |
Michael Kruse | e330071 | 2018-05-09 16:23:56 +0000 | [diff] [blame] | 2334 | assert((!MaxPMA || MaxPMA.dim(isl::dim::out)) && |
| 2335 | "Assumed at least one output dimension"); |
| 2336 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2337 | Pos = MaxPMA.dim(isl::dim::out) - 1; |
| 2338 | LastDimAff = MaxPMA.get_pw_aff(Pos); |
| 2339 | OneAff = isl::aff(isl::local_space(LastDimAff.get_domain_space())); |
| 2340 | OneAff = OneAff.add_constant_si(1); |
| 2341 | LastDimAff = LastDimAff.add(OneAff); |
| 2342 | MaxPMA = MaxPMA.set_pw_aff(Pos, LastDimAff); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2343 | |
Michael Kruse | e330071 | 2018-05-09 16:23:56 +0000 | [diff] [blame] | 2344 | if (!MinPMA || !MaxPMA) |
| 2345 | return isl::stat::error; |
| 2346 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2347 | MinMaxAccesses.push_back(std::make_pair(MinPMA, MaxPMA)); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2348 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2349 | return isl::stat::ok; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2350 | } |
| 2351 | |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 2352 | static __isl_give isl_set *getAccessDomain(MemoryAccess *MA) { |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 2353 | isl_set *Domain = MA->getStatement()->getDomain().release(); |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 2354 | Domain = isl_set_project_out(Domain, isl_dim_set, 0, isl_set_n_dim(Domain)); |
| 2355 | return isl_set_reset_tuple_id(Domain); |
| 2356 | } |
| 2357 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2358 | /// Wrapper function to calculate minimal/maximal accesses to each array. |
Tobias Grosser | e952223 | 2017-01-16 15:49:04 +0000 | [diff] [blame] | 2359 | static bool calculateMinMaxAccess(Scop::AliasGroupTy AliasGroup, Scop &S, |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 2360 | Scop::MinMaxVectorTy &MinMaxAccesses) { |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2361 | MinMaxAccesses.reserve(AliasGroup.size()); |
Tobias Grosser | e952223 | 2017-01-16 15:49:04 +0000 | [diff] [blame] | 2362 | |
Tobias Grosser | 31df6f3 | 2017-08-06 21:42:25 +0000 | [diff] [blame] | 2363 | isl::union_set Domains = S.getDomains(); |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 2364 | isl::union_map Accesses = isl::union_map::empty(S.getParamSpace()); |
Tobias Grosser | e952223 | 2017-01-16 15:49:04 +0000 | [diff] [blame] | 2365 | |
| 2366 | for (MemoryAccess *MA : AliasGroup) |
Tobias Grosser | d3d3d6b | 2018-04-29 00:28:26 +0000 | [diff] [blame] | 2367 | Accesses = Accesses.add_map(MA->getAccessRelation()); |
Tobias Grosser | e952223 | 2017-01-16 15:49:04 +0000 | [diff] [blame] | 2368 | |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2369 | Accesses = Accesses.intersect_domain(Domains); |
| 2370 | isl::union_set Locations = Accesses.range(); |
Tobias Grosser | 8f23fb8 | 2017-06-23 08:05:20 +0000 | [diff] [blame] | 2371 | |
| 2372 | auto Lambda = [&MinMaxAccesses, &S](isl::set Set) -> isl::stat { |
| 2373 | return buildMinMaxAccess(Set, MinMaxAccesses, S); |
| 2374 | }; |
| 2375 | return Locations.foreach_set(Lambda) == isl::stat::ok; |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 2376 | } |
| 2377 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2378 | /// Helper to treat non-affine regions and basic blocks the same. |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2379 | /// |
| 2380 | ///{ |
| 2381 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2382 | /// Return the block that is the representing block for @p RN. |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2383 | static inline BasicBlock *getRegionNodeBasicBlock(RegionNode *RN) { |
| 2384 | return RN->isSubRegion() ? RN->getNodeAs<Region>()->getEntry() |
| 2385 | : RN->getNodeAs<BasicBlock>(); |
| 2386 | } |
| 2387 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2388 | /// Return the @p idx'th block that is executed after @p RN. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2389 | static inline BasicBlock * |
| 2390 | getRegionNodeSuccessor(RegionNode *RN, TerminatorInst *TI, unsigned idx) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2391 | if (RN->isSubRegion()) { |
| 2392 | assert(idx == 0); |
| 2393 | return RN->getNodeAs<Region>()->getExit(); |
| 2394 | } |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2395 | return TI->getSuccessor(idx); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2396 | } |
| 2397 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2398 | /// Return the smallest loop surrounding @p RN. |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2399 | static inline Loop *getRegionNodeLoop(RegionNode *RN, LoopInfo &LI) { |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 2400 | if (!RN->isSubRegion()) { |
| 2401 | BasicBlock *BB = RN->getNodeAs<BasicBlock>(); |
| 2402 | Loop *L = LI.getLoopFor(BB); |
| 2403 | |
| 2404 | // Unreachable statements are not considered to belong to a LLVM loop, as |
| 2405 | // they are not part of an actual loop in the control flow graph. |
| 2406 | // Nevertheless, we handle certain unreachable statements that are common |
| 2407 | // when modeling run-time bounds checks as being part of the loop to be |
| 2408 | // able to model them and to later eliminate the run-time bounds checks. |
| 2409 | // |
| 2410 | // Specifically, for basic blocks that terminate in an unreachable and |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 2411 | // where the immediate predecessor is part of a loop, we assume these |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 2412 | // basic blocks belong to the loop the predecessor belongs to. This |
| 2413 | // allows us to model the following code. |
| 2414 | // |
| 2415 | // for (i = 0; i < N; i++) { |
| 2416 | // if (i > 1024) |
| 2417 | // abort(); <- this abort might be translated to an |
| 2418 | // unreachable |
| 2419 | // |
| 2420 | // A[i] = ... |
| 2421 | // } |
| 2422 | if (!L && isa<UnreachableInst>(BB->getTerminator()) && BB->getPrevNode()) |
| 2423 | L = LI.getLoopFor(BB->getPrevNode()); |
| 2424 | return L; |
| 2425 | } |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2426 | |
| 2427 | Region *NonAffineSubRegion = RN->getNodeAs<Region>(); |
| 2428 | Loop *L = LI.getLoopFor(NonAffineSubRegion->getEntry()); |
| 2429 | while (L && NonAffineSubRegion->contains(L)) |
| 2430 | L = L->getParentLoop(); |
| 2431 | return L; |
| 2432 | } |
| 2433 | |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 2434 | /// Get the number of blocks in @p L. |
| 2435 | /// |
| 2436 | /// The number of blocks in a loop are the number of basic blocks actually |
| 2437 | /// belonging to the loop, as well as all single basic blocks that the loop |
| 2438 | /// exits to and which terminate in an unreachable instruction. We do not |
| 2439 | /// allow such basic blocks in the exit of a scop, hence they belong to the |
| 2440 | /// scop and represent run-time conditions which we want to model and |
| 2441 | /// subsequently speculate away. |
| 2442 | /// |
| 2443 | /// @see getRegionNodeLoop for additional details. |
Reid Kleckner | df2b283 | 2017-06-19 17:44:02 +0000 | [diff] [blame] | 2444 | unsigned getNumBlocksInLoop(Loop *L) { |
| 2445 | unsigned NumBlocks = L->getNumBlocks(); |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 2446 | SmallVector<BasicBlock *, 4> ExitBlocks; |
Tobias Grosser | ce69e7b | 2017-03-07 16:17:55 +0000 | [diff] [blame] | 2447 | L->getExitBlocks(ExitBlocks); |
| 2448 | |
| 2449 | for (auto ExitBlock : ExitBlocks) { |
| 2450 | if (isa<UnreachableInst>(ExitBlock->getTerminator())) |
| 2451 | NumBlocks++; |
| 2452 | } |
| 2453 | return NumBlocks; |
| 2454 | } |
| 2455 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2456 | static inline unsigned getNumBlocksInRegionNode(RegionNode *RN) { |
| 2457 | if (!RN->isSubRegion()) |
| 2458 | return 1; |
| 2459 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2460 | Region *R = RN->getNodeAs<Region>(); |
Tobias Grosser | 0dd4a9a | 2016-02-01 01:55:08 +0000 | [diff] [blame] | 2461 | return std::distance(R->block_begin(), R->block_end()); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 2464 | static bool containsErrorBlock(RegionNode *RN, const Region &R, LoopInfo &LI, |
| 2465 | const DominatorTree &DT) { |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2466 | if (!RN->isSubRegion()) |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 2467 | return isErrorBlock(*RN->getNodeAs<BasicBlock>(), R, LI, DT); |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2468 | for (BasicBlock *BB : RN->getNodeAs<Region>()->blocks()) |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 2469 | if (isErrorBlock(*BB, R, LI, DT)) |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2470 | return true; |
| 2471 | return false; |
| 2472 | } |
| 2473 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2474 | ///} |
| 2475 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 2476 | isl::set Scop::getDomainConditions(const ScopStmt *Stmt) const { |
Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 2477 | return getDomainConditions(Stmt->getEntryBlock()); |
Johannes Doerfert | cef616f | 2015-09-15 22:49:04 +0000 | [diff] [blame] | 2478 | } |
| 2479 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 2480 | isl::set Scop::getDomainConditions(BasicBlock *BB) const { |
Johannes Doerfert | 41cda15 | 2016-04-08 10:32:26 +0000 | [diff] [blame] | 2481 | auto DIt = DomainMap.find(BB); |
| 2482 | if (DIt != DomainMap.end()) |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 2483 | return DIt->getSecond(); |
Johannes Doerfert | 41cda15 | 2016-04-08 10:32:26 +0000 | [diff] [blame] | 2484 | |
| 2485 | auto &RI = *R.getRegionInfo(); |
| 2486 | auto *BBR = RI.getRegionFor(BB); |
| 2487 | while (BBR->getEntry() == BB) |
| 2488 | BBR = BBR->getParent(); |
| 2489 | return getDomainConditions(BBR->getEntry()); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2490 | } |
| 2491 | |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2492 | bool Scop::buildDomains(Region *R, DominatorTree &DT, LoopInfo &LI, |
| 2493 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2494 | bool IsOnlyNonAffineRegion = isNonAffineSubRegion(R); |
Johannes Doerfert | f08bd00 | 2015-08-31 13:56:32 +0000 | [diff] [blame] | 2495 | auto *EntryBB = R->getEntry(); |
Johannes Doerfert | 432658d | 2016-01-26 11:01:41 +0000 | [diff] [blame] | 2496 | auto *L = IsOnlyNonAffineRegion ? nullptr : LI.getLoopFor(EntryBB); |
| 2497 | int LD = getRelativeLoopDepth(L); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2498 | 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] | 2499 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2500 | while (LD-- >= 0) { |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 2501 | L = L->getParentLoop(); |
| 2502 | } |
| 2503 | |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2504 | InvalidDomainMap[EntryBB] = isl::manage(isl_set_empty(isl_set_get_space(S))); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2505 | DomainMap[EntryBB] = isl::manage(S); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2506 | |
Johannes Doerfert | 432658d | 2016-01-26 11:01:41 +0000 | [diff] [blame] | 2507 | if (IsOnlyNonAffineRegion) |
Johannes Doerfert | 2640454 | 2016-05-10 12:19:47 +0000 | [diff] [blame] | 2508 | return !containsErrorBlock(R->getNode(), *R, LI, DT); |
Johannes Doerfert | 40fa56f | 2015-09-14 11:15:07 +0000 | [diff] [blame] | 2509 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2510 | if (!buildDomainsWithBranchConstraints(R, DT, LI, InvalidDomainMap)) |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2511 | return false; |
| 2512 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2513 | if (!propagateDomainConstraints(R, DT, LI, InvalidDomainMap)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2514 | return false; |
Tobias Grosser | 9737c7b | 2015-11-22 11:06:51 +0000 | [diff] [blame] | 2515 | |
| 2516 | // Error blocks and blocks dominated by them have been assumed to never be |
| 2517 | // executed. Representing them in the Scop does not add any value. In fact, |
| 2518 | // it is likely to cause issues during construction of the ScopStmts. The |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2519 | // contents of error blocks have not been verified to be expressible and |
Tobias Grosser | 9737c7b | 2015-11-22 11:06:51 +0000 | [diff] [blame] | 2520 | // will cause problems when building up a ScopStmt for them. |
| 2521 | // Furthermore, basic blocks dominated by error blocks may reference |
| 2522 | // 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] | 2523 | // can themselves not be constructed properly. To this end we will replace |
| 2524 | // the domains of error blocks and those only reachable via error blocks |
| 2525 | // 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] | 2526 | // parameter combination it would be reached via an error block in its |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2527 | // InvalidDomain. This information is needed during load hoisting. |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2528 | if (!propagateInvalidStmtDomains(R, DT, LI, InvalidDomainMap)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2529 | return false; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2530 | |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2531 | return true; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2534 | /// Adjust the dimensions of @p Dom that was constructed for @p OldL |
Johannes Doerfert | a07f0ac | 2016-04-04 07:50:40 +0000 | [diff] [blame] | 2535 | /// to be compatible to domains constructed for loop @p NewL. |
| 2536 | /// |
| 2537 | /// This function assumes @p NewL and @p OldL are equal or there is a CFG |
| 2538 | /// edge from @p OldL to @p NewL. |
| 2539 | static __isl_give isl_set *adjustDomainDimensions(Scop &S, |
| 2540 | __isl_take isl_set *Dom, |
| 2541 | Loop *OldL, Loop *NewL) { |
Johannes Doerfert | a07f0ac | 2016-04-04 07:50:40 +0000 | [diff] [blame] | 2542 | // If the loops are the same there is nothing to do. |
| 2543 | if (NewL == OldL) |
| 2544 | return Dom; |
| 2545 | |
| 2546 | int OldDepth = S.getRelativeLoopDepth(OldL); |
| 2547 | int NewDepth = S.getRelativeLoopDepth(NewL); |
| 2548 | // If both loops are non-affine loops there is nothing to do. |
| 2549 | if (OldDepth == -1 && NewDepth == -1) |
| 2550 | return Dom; |
| 2551 | |
| 2552 | // Distinguish three cases: |
| 2553 | // 1) The depth is the same but the loops are not. |
| 2554 | // => One loop was left one was entered. |
| 2555 | // 2) The depth increased from OldL to NewL. |
| 2556 | // => One loop was entered, none was left. |
| 2557 | // 3) The depth decreased from OldL to NewL. |
| 2558 | // => Loops were left were difference of the depths defines how many. |
| 2559 | if (OldDepth == NewDepth) { |
| 2560 | assert(OldL->getParentLoop() == NewL->getParentLoop()); |
| 2561 | Dom = isl_set_project_out(Dom, isl_dim_set, NewDepth, 1); |
| 2562 | Dom = isl_set_add_dims(Dom, isl_dim_set, 1); |
Johannes Doerfert | a07f0ac | 2016-04-04 07:50:40 +0000 | [diff] [blame] | 2563 | } else if (OldDepth < NewDepth) { |
| 2564 | assert(OldDepth + 1 == NewDepth); |
| 2565 | auto &R = S.getRegion(); |
| 2566 | (void)R; |
| 2567 | assert(NewL->getParentLoop() == OldL || |
| 2568 | ((!OldL || !R.contains(OldL)) && R.contains(NewL))); |
| 2569 | Dom = isl_set_add_dims(Dom, isl_dim_set, 1); |
Johannes Doerfert | a07f0ac | 2016-04-04 07:50:40 +0000 | [diff] [blame] | 2570 | } else { |
| 2571 | assert(OldDepth > NewDepth); |
| 2572 | int Diff = OldDepth - NewDepth; |
| 2573 | int NumDim = isl_set_n_dim(Dom); |
| 2574 | assert(NumDim >= Diff); |
| 2575 | Dom = isl_set_project_out(Dom, isl_dim_set, NumDim - Diff, Diff); |
| 2576 | } |
| 2577 | |
| 2578 | return Dom; |
| 2579 | } |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2580 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2581 | bool Scop::propagateInvalidStmtDomains( |
| 2582 | Region *R, DominatorTree &DT, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2583 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2584 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 2585 | for (auto *RN : RTraversal) { |
| 2586 | |
| 2587 | // Recurse for affine subregions but go on for basic blocks and non-affine |
| 2588 | // subregions. |
| 2589 | if (RN->isSubRegion()) { |
| 2590 | Region *SubRegion = RN->getNodeAs<Region>(); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2591 | if (!isNonAffineSubRegion(SubRegion)) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2592 | propagateInvalidStmtDomains(SubRegion, DT, LI, InvalidDomainMap); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2593 | continue; |
| 2594 | } |
| 2595 | } |
| 2596 | |
| 2597 | bool ContainsErrorBlock = containsErrorBlock(RN, getRegion(), LI, DT); |
| 2598 | BasicBlock *BB = getRegionNodeBasicBlock(RN); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2599 | isl::set &Domain = DomainMap[BB]; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2600 | assert(Domain && "Cannot propagate a nullptr"); |
| 2601 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2602 | isl::set InvalidDomain = InvalidDomainMap[BB]; |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2603 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2604 | bool IsInvalidBlock = ContainsErrorBlock || Domain.is_subset(InvalidDomain); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2605 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2606 | if (!IsInvalidBlock) { |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2607 | InvalidDomain = InvalidDomain.intersect(Domain); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2608 | } else { |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2609 | InvalidDomain = Domain; |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2610 | isl::set DomPar = Domain.params(); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 2611 | recordAssumption(ERRORBLOCK, DomPar, BB->getTerminator()->getDebugLoc(), |
| 2612 | AS_RESTRICTION); |
Johannes Doerfert | 14b1cf3 | 2016-05-10 12:42:26 +0000 | [diff] [blame] | 2613 | Domain = nullptr; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2616 | if (InvalidDomain.is_empty()) { |
| 2617 | InvalidDomainMap[BB] = InvalidDomain; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2618 | continue; |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 2619 | } |
| 2620 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2621 | auto *BBLoop = getRegionNodeLoop(RN, LI); |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2622 | auto *TI = BB->getTerminator(); |
| 2623 | unsigned NumSuccs = RN->isSubRegion() ? 1 : TI->getNumSuccessors(); |
| 2624 | for (unsigned u = 0; u < NumSuccs; u++) { |
| 2625 | auto *SuccBB = getRegionNodeSuccessor(RN, TI, u); |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 2626 | |
| 2627 | // Skip successors outside the SCoP. |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2628 | if (!contains(SuccBB)) |
Johannes Doerfert | 7c01357 | 2016-04-12 09:57:34 +0000 | [diff] [blame] | 2629 | continue; |
| 2630 | |
Johannes Doerfert | e4459a2 | 2016-04-25 13:34:50 +0000 | [diff] [blame] | 2631 | // Skip backedges. |
| 2632 | if (DT.dominates(SuccBB, BB)) |
| 2633 | continue; |
| 2634 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2635 | Loop *SuccBBLoop = getFirstNonBoxedLoopFor(SuccBB, LI, getBoxedLoops()); |
| 2636 | |
Philip Pfaffe | 9375d57 | 2018-05-16 14:05:03 +0000 | [diff] [blame] | 2637 | auto AdjustedInvalidDomain = isl::manage(adjustDomainDimensions( |
| 2638 | *this, InvalidDomain.copy(), BBLoop, SuccBBLoop)); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2639 | |
Philip Pfaffe | 9375d57 | 2018-05-16 14:05:03 +0000 | [diff] [blame] | 2640 | isl::set SuccInvalidDomain = InvalidDomainMap[SuccBB]; |
| 2641 | SuccInvalidDomain = SuccInvalidDomain.unite(AdjustedInvalidDomain); |
| 2642 | SuccInvalidDomain = SuccInvalidDomain.coalesce(); |
| 2643 | unsigned NumConjucts = SuccInvalidDomain.n_basic_set(); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2644 | |
Philip Pfaffe | 9375d57 | 2018-05-16 14:05:03 +0000 | [diff] [blame] | 2645 | InvalidDomainMap[SuccBB] = SuccInvalidDomain; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2646 | |
Michael Kruse | bc15012 | 2016-05-02 12:25:18 +0000 | [diff] [blame] | 2647 | // Check if the maximal number of domain disjunctions was reached. |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2648 | // In case this happens we will bail. |
Tobias Grosser | 90411a9 | 2017-02-16 19:11:33 +0000 | [diff] [blame] | 2649 | if (NumConjucts < MaxDisjunctsInDomain) |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2650 | continue; |
| 2651 | |
Tobias Grosser | f44f005 | 2017-07-09 15:47:17 +0000 | [diff] [blame] | 2652 | InvalidDomainMap.erase(BB); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 2653 | invalidate(COMPLEXITY, TI->getDebugLoc(), TI->getParent()); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2654 | return false; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2655 | } |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2656 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2657 | InvalidDomainMap[BB] = InvalidDomain; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2658 | } |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2659 | |
| 2660 | return true; |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 2661 | } |
| 2662 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2663 | void Scop::propagateDomainConstraintsToRegionExit( |
| 2664 | BasicBlock *BB, Loop *BBLoop, |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2665 | SmallPtrSetImpl<BasicBlock *> &FinishedExitBlocks, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2666 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2667 | // Check if the block @p BB is the entry of a region. If so we propagate it's |
| 2668 | // domain to the exit block of the region. Otherwise we are done. |
| 2669 | auto *RI = R.getRegionInfo(); |
| 2670 | auto *BBReg = RI ? RI->getRegionFor(BB) : nullptr; |
| 2671 | auto *ExitBB = BBReg ? BBReg->getExit() : nullptr; |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 2672 | if (!BBReg || BBReg->getEntry() != BB || !contains(ExitBB)) |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2673 | return; |
| 2674 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2675 | // 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] | 2676 | // that would prevent the exit block from being executed. |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2677 | auto *L = BBLoop; |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 2678 | while (L && contains(L)) { |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2679 | SmallVector<BasicBlock *, 4> LatchBBs; |
| 2680 | BBLoop->getLoopLatches(LatchBBs); |
| 2681 | for (auto *LatchBB : LatchBBs) |
| 2682 | if (BB != LatchBB && BBReg->contains(LatchBB)) |
| 2683 | return; |
| 2684 | L = L->getParentLoop(); |
| 2685 | } |
| 2686 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2687 | isl::set Domain = DomainMap[BB]; |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2688 | assert(Domain && "Cannot propagate a nullptr"); |
| 2689 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2690 | Loop *ExitBBLoop = getFirstNonBoxedLoopFor(ExitBB, LI, getBoxedLoops()); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2691 | |
| 2692 | // Since the dimensions of @p BB and @p ExitBB might be different we have to |
| 2693 | // adjust the domain before we can propagate it. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2694 | isl::set AdjustedDomain = isl::manage( |
| 2695 | adjustDomainDimensions(*this, Domain.copy(), BBLoop, ExitBBLoop)); |
| 2696 | isl::set &ExitDomain = DomainMap[ExitBB]; |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2697 | |
| 2698 | // If the exit domain is not yet created we set it otherwise we "add" the |
| 2699 | // current domain. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2700 | ExitDomain = ExitDomain ? AdjustedDomain.unite(ExitDomain) : AdjustedDomain; |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2701 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2702 | // Initialize the invalid domain. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2703 | InvalidDomainMap[ExitBB] = ExitDomain.empty(ExitDomain.get_space()); |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2704 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2705 | FinishedExitBlocks.insert(ExitBB); |
| 2706 | } |
| 2707 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2708 | bool Scop::buildDomainsWithBranchConstraints( |
| 2709 | Region *R, DominatorTree &DT, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2710 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2711 | // To create the domain for each block in R we iterate over all blocks and |
| 2712 | // subregions in R and propagate the conditions under which the current region |
| 2713 | // element is executed. To this end we iterate in reverse post order over R as |
| 2714 | // it ensures that we first visit all predecessors of a region node (either a |
| 2715 | // basic block or a subregion) before we visit the region node itself. |
| 2716 | // Initially, only the domain for the SCoP region entry block is set and from |
| 2717 | // there we propagate the current domain to all successors, however we add the |
| 2718 | // condition that the successor is actually executed next. |
| 2719 | // As we are only interested in non-loop carried constraints here we can |
| 2720 | // simply skip loop back edges. |
| 2721 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2722 | SmallPtrSet<BasicBlock *, 8> FinishedExitBlocks; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2723 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 2724 | for (auto *RN : RTraversal) { |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2725 | // Recurse for affine subregions but go on for basic blocks and non-affine |
| 2726 | // subregions. |
| 2727 | if (RN->isSubRegion()) { |
| 2728 | Region *SubRegion = RN->getNodeAs<Region>(); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2729 | if (!isNonAffineSubRegion(SubRegion)) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2730 | if (!buildDomainsWithBranchConstraints(SubRegion, DT, LI, |
| 2731 | InvalidDomainMap)) |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2732 | return false; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2733 | continue; |
| 2734 | } |
| 2735 | } |
| 2736 | |
Tobias Grosser | b76cd3c | 2015-11-11 08:42:20 +0000 | [diff] [blame] | 2737 | if (containsErrorBlock(RN, getRegion(), LI, DT)) |
Johannes Doerfert | f85ad04 | 2015-11-08 20:16:39 +0000 | [diff] [blame] | 2738 | HasErrorBlock = true; |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2739 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2740 | BasicBlock *BB = getRegionNodeBasicBlock(RN); |
Johannes Doerfert | 90db75e | 2015-09-10 17:51:27 +0000 | [diff] [blame] | 2741 | TerminatorInst *TI = BB->getTerminator(); |
| 2742 | |
Tobias Grosser | b76cd3c | 2015-11-11 08:42:20 +0000 | [diff] [blame] | 2743 | if (isa<UnreachableInst>(TI)) |
| 2744 | continue; |
| 2745 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2746 | isl::set Domain = DomainMap.lookup(BB); |
Tobias Grosser | 4fb9e51 | 2016-02-27 06:59:30 +0000 | [diff] [blame] | 2747 | if (!Domain) |
Johannes Doerfert | 90db75e | 2015-09-10 17:51:27 +0000 | [diff] [blame] | 2748 | continue; |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2749 | MaxLoopDepth = std::max(MaxLoopDepth, isl_set_n_dim(Domain.get())); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2750 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2751 | auto *BBLoop = getRegionNodeLoop(RN, LI); |
| 2752 | // Propagate the domain from BB directly to blocks that have a superset |
| 2753 | // 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] | 2754 | propagateDomainConstraintsToRegionExit(BB, BBLoop, FinishedExitBlocks, LI, |
| 2755 | InvalidDomainMap); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2756 | |
| 2757 | // If all successors of BB have been set a domain through the propagation |
| 2758 | // above we do not need to build condition sets but can just skip this |
| 2759 | // block. However, it is important to note that this is a local property |
| 2760 | // with regards to the region @p R. To this end FinishedExitBlocks is a |
| 2761 | // local variable. |
| 2762 | auto IsFinishedRegionExit = [&FinishedExitBlocks](BasicBlock *SuccBB) { |
| 2763 | return FinishedExitBlocks.count(SuccBB); |
| 2764 | }; |
| 2765 | if (std::all_of(succ_begin(BB), succ_end(BB), IsFinishedRegionExit)) |
| 2766 | continue; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2767 | |
| 2768 | // Build the condition sets for the successor nodes of the current region |
| 2769 | // node. If it is a non-affine subregion we will always execute the single |
| 2770 | // exit node, hence the single entry node domain is the condition set. For |
| 2771 | // basic blocks we use the helper function buildConditionSets. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2772 | SmallVector<isl_set *, 8> ConditionSets; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2773 | if (RN->isSubRegion()) |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2774 | ConditionSets.push_back(Domain.copy()); |
| 2775 | else if (!buildConditionSets(*this, BB, TI, BBLoop, Domain.get(), |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2776 | InvalidDomainMap, ConditionSets)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2777 | return false; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2778 | |
| 2779 | // Now iterate over the successors and set their initial domain based on |
| 2780 | // their condition set. We skip back edges here and have to be careful when |
| 2781 | // we leave a loop not to keep constraints over a dimension that doesn't |
| 2782 | // exist anymore. |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2783 | assert(RN->isSubRegion() || TI->getNumSuccessors() == ConditionSets.size()); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2784 | for (unsigned u = 0, e = ConditionSets.size(); u < e; u++) { |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2785 | isl::set CondSet = isl::manage(ConditionSets[u]); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2786 | BasicBlock *SuccBB = getRegionNodeSuccessor(RN, TI, u); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2787 | |
Johannes Doerfert | 535de03 | 2016-04-19 14:49:05 +0000 | [diff] [blame] | 2788 | // Skip blocks outside the region. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2789 | if (!contains(SuccBB)) |
Johannes Doerfert | 535de03 | 2016-04-19 14:49:05 +0000 | [diff] [blame] | 2790 | continue; |
Johannes Doerfert | 535de03 | 2016-04-19 14:49:05 +0000 | [diff] [blame] | 2791 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2792 | // If we propagate the domain of some block to "SuccBB" we do not have to |
| 2793 | // adjust the domain. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2794 | if (FinishedExitBlocks.count(SuccBB)) |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2795 | continue; |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2796 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2797 | // Skip back edges. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2798 | if (DT.dominates(SuccBB, BB)) |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2799 | continue; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2800 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2801 | Loop *SuccBBLoop = getFirstNonBoxedLoopFor(SuccBB, LI, getBoxedLoops()); |
| 2802 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2803 | CondSet = isl::manage( |
| 2804 | adjustDomainDimensions(*this, CondSet.copy(), BBLoop, SuccBBLoop)); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2805 | |
| 2806 | // Set the domain for the successor or merge it with an existing domain in |
| 2807 | // case there are multiple paths (without loop back edges) to the |
| 2808 | // successor block. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2809 | isl::set &SuccDomain = DomainMap[SuccBB]; |
Tobias Grosser | 5a8c052 | 2016-03-22 22:05:32 +0000 | [diff] [blame] | 2810 | |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2811 | if (SuccDomain) { |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2812 | SuccDomain = SuccDomain.unite(CondSet).coalesce(); |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2813 | } else { |
| 2814 | // Initialize the invalid domain. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2815 | InvalidDomainMap[SuccBB] = CondSet.empty(CondSet.get_space()); |
Johannes Doerfert | a351951 | 2016-04-23 13:02:23 +0000 | [diff] [blame] | 2816 | SuccDomain = CondSet; |
| 2817 | } |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2818 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2819 | SuccDomain = SuccDomain.detect_equalities(); |
Tobias Grosser | 6d459c5 | 2017-05-23 04:26:28 +0000 | [diff] [blame] | 2820 | |
Michael Kruse | bc15012 | 2016-05-02 12:25:18 +0000 | [diff] [blame] | 2821 | // Check if the maximal number of domain disjunctions was reached. |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2822 | // In case this happens we will clean up and bail. |
Philip Pfaffe | 9375d57 | 2018-05-16 14:05:03 +0000 | [diff] [blame] | 2823 | if (SuccDomain.n_basic_set() < MaxDisjunctsInDomain) |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2824 | continue; |
| 2825 | |
| 2826 | invalidate(COMPLEXITY, DebugLoc()); |
| 2827 | while (++u < ConditionSets.size()) |
| 2828 | isl_set_free(ConditionSets[u]); |
| 2829 | return false; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2830 | } |
| 2831 | } |
Johannes Doerfert | 5fb9b21 | 2016-03-29 20:02:05 +0000 | [diff] [blame] | 2832 | |
| 2833 | return true; |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 2834 | } |
| 2835 | |
Tobias Grosser | 2f3041f | 2017-08-06 17:31:38 +0000 | [diff] [blame] | 2836 | isl::set Scop::getPredecessorDomainConstraints(BasicBlock *BB, isl::set Domain, |
| 2837 | DominatorTree &DT, |
| 2838 | LoopInfo &LI) { |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2839 | // If @p BB is the ScopEntry we are done |
| 2840 | if (R.getEntry() == BB) |
Tobias Grosser | 2f3041f | 2017-08-06 17:31:38 +0000 | [diff] [blame] | 2841 | return isl::set::universe(Domain.get_space()); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2842 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2843 | // The region info of this function. |
| 2844 | auto &RI = *R.getRegionInfo(); |
| 2845 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2846 | Loop *BBLoop = getFirstNonBoxedLoopFor(BB, LI, getBoxedLoops()); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2847 | |
| 2848 | // A domain to collect all predecessor domains, thus all conditions under |
| 2849 | // 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] | 2850 | isl::set PredDom = isl::set::empty(Domain.get_space()); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2851 | |
| 2852 | // Set of regions of which the entry block domain has been propagated to BB. |
| 2853 | // all predecessors inside any of the regions can be skipped. |
| 2854 | SmallSet<Region *, 8> PropagatedRegions; |
| 2855 | |
| 2856 | for (auto *PredBB : predecessors(BB)) { |
| 2857 | // Skip backedges. |
| 2858 | if (DT.dominates(BB, PredBB)) |
| 2859 | continue; |
| 2860 | |
| 2861 | // If the predecessor is in a region we used for propagation we can skip it. |
| 2862 | auto PredBBInRegion = [PredBB](Region *PR) { return PR->contains(PredBB); }; |
| 2863 | if (std::any_of(PropagatedRegions.begin(), PropagatedRegions.end(), |
| 2864 | PredBBInRegion)) { |
| 2865 | continue; |
| 2866 | } |
| 2867 | |
| 2868 | // Check if there is a valid region we can use for propagation, thus look |
| 2869 | // for a region that contains the predecessor and has @p BB as exit block. |
| 2870 | auto *PredR = RI.getRegionFor(PredBB); |
| 2871 | while (PredR->getExit() != BB && !PredR->contains(BB)) |
| 2872 | PredR->getParent(); |
| 2873 | |
| 2874 | // If a valid region for propagation was found use the entry of that region |
| 2875 | // for propagation, otherwise the PredBB directly. |
| 2876 | if (PredR->getExit() == BB) { |
| 2877 | PredBB = PredR->getEntry(); |
| 2878 | PropagatedRegions.insert(PredR); |
| 2879 | } |
| 2880 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 2881 | auto *PredBBDom = getDomainConditions(PredBB).release(); |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2882 | Loop *PredBBLoop = getFirstNonBoxedLoopFor(PredBB, LI, getBoxedLoops()); |
| 2883 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2884 | PredBBDom = adjustDomainDimensions(*this, PredBBDom, PredBBLoop, BBLoop); |
| 2885 | |
Tobias Grosser | 2f3041f | 2017-08-06 17:31:38 +0000 | [diff] [blame] | 2886 | PredDom = PredDom.unite(isl::manage(PredBBDom)); |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2887 | } |
| 2888 | |
| 2889 | return PredDom; |
| 2890 | } |
| 2891 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2892 | bool Scop::propagateDomainConstraints( |
| 2893 | Region *R, DominatorTree &DT, LoopInfo &LI, |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2894 | DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2895 | // Iterate over the region R and propagate the domain constrains from the |
| 2896 | // predecessors to the current node. In contrast to the |
| 2897 | // buildDomainsWithBranchConstraints function, this one will pull the domain |
| 2898 | // information from the predecessors instead of pushing it to the successors. |
| 2899 | // Additionally, we assume the domains to be already present in the domain |
| 2900 | // map here. However, we iterate again in reverse post order so we know all |
| 2901 | // predecessors have been visited before a block or non-affine subregion is |
| 2902 | // visited. |
| 2903 | |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2904 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 2905 | for (auto *RN : RTraversal) { |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2906 | // Recurse for affine subregions but go on for basic blocks and non-affine |
| 2907 | // subregions. |
| 2908 | if (RN->isSubRegion()) { |
| 2909 | Region *SubRegion = RN->getNodeAs<Region>(); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 2910 | if (!isNonAffineSubRegion(SubRegion)) { |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2911 | if (!propagateDomainConstraints(SubRegion, DT, LI, InvalidDomainMap)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2912 | return false; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2913 | continue; |
| 2914 | } |
| 2915 | } |
| 2916 | |
| 2917 | BasicBlock *BB = getRegionNodeBasicBlock(RN); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2918 | isl::set &Domain = DomainMap[BB]; |
Johannes Doerfert | a49c557 | 2016-04-05 16:18:53 +0000 | [diff] [blame] | 2919 | assert(Domain); |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2920 | |
Tobias Grosser | 6deba4e | 2016-03-30 18:18:31 +0000 | [diff] [blame] | 2921 | // Under the union of all predecessor conditions we can reach this block. |
Tobias Grosser | 2f3041f | 2017-08-06 17:31:38 +0000 | [diff] [blame] | 2922 | isl::set PredDom = getPredecessorDomainConstraints(BB, Domain, DT, LI); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2923 | Domain = Domain.intersect(PredDom).coalesce(); |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 2924 | Domain = Domain.align_params(getParamSpace()); |
Tobias Grosser | 6deba4e | 2016-03-30 18:18:31 +0000 | [diff] [blame] | 2925 | |
Johannes Doerfert | 642594a | 2016-04-04 07:57:39 +0000 | [diff] [blame] | 2926 | Loop *BBLoop = getRegionNodeLoop(RN, LI); |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 2927 | if (BBLoop && BBLoop->getHeader() == BB && contains(BBLoop)) |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2928 | if (!addLoopBoundsToHeaderDomain(BBLoop, LI, InvalidDomainMap)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2929 | return false; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2930 | } |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2931 | |
| 2932 | return true; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2933 | } |
| 2934 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2935 | /// Create a map to map from a given iteration to a subsequent iteration. |
| 2936 | /// |
| 2937 | /// This map maps from SetSpace -> SetSpace where the dimensions @p Dim |
| 2938 | /// is incremented by one and all other dimensions are equal, e.g., |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2939 | /// [i0, i1, i2, i3] -> [i0, i1, i2 + 1, i3] |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 2940 | /// |
| 2941 | /// if @p Dim is 2 and @p SetSpace has 4 dimensions. |
Tobias Grosser | 10da5a0 | 2018-05-23 18:41:40 +0000 | [diff] [blame] | 2942 | static isl::map createNextIterationMap(isl::space SetSpace, unsigned Dim) { |
| 2943 | isl::space MapSpace = SetSpace.map_from_set(); |
| 2944 | isl::map NextIterationMap = isl::map::universe(MapSpace); |
| 2945 | for (unsigned u = 0; u < NextIterationMap.dim(isl::dim::in); u++) |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2946 | if (u != Dim) |
| 2947 | NextIterationMap = |
Tobias Grosser | 10da5a0 | 2018-05-23 18:41:40 +0000 | [diff] [blame] | 2948 | NextIterationMap.equate(isl::dim::in, u, isl::dim::out, u); |
| 2949 | isl::constraint C = |
| 2950 | isl::constraint::alloc_equality(isl::local_space(MapSpace)); |
| 2951 | C = C.set_constant_si(1); |
| 2952 | C = C.set_coefficient_si(isl::dim::in, Dim, 1); |
| 2953 | C = C.set_coefficient_si(isl::dim::out, Dim, -1); |
| 2954 | NextIterationMap = NextIterationMap.add_constraint(C); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2955 | return NextIterationMap; |
| 2956 | } |
| 2957 | |
Michael Kruse | 476f855 | 2017-06-29 12:47:41 +0000 | [diff] [blame] | 2958 | bool Scop::addLoopBoundsToHeaderDomain( |
Tobias Grosser | 13acbb9 | 2017-07-15 09:01:31 +0000 | [diff] [blame] | 2959 | Loop *L, LoopInfo &LI, DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) { |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2960 | int LoopDepth = getRelativeLoopDepth(L); |
| 2961 | assert(LoopDepth >= 0 && "Loop in region should have at least depth one"); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2962 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2963 | BasicBlock *HeaderBB = L->getHeader(); |
| 2964 | assert(DomainMap.count(HeaderBB)); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2965 | isl::set &HeaderBBDom = DomainMap[HeaderBB]; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2966 | |
Tobias Grosser | 10da5a0 | 2018-05-23 18:41:40 +0000 | [diff] [blame] | 2967 | isl::map NextIterationMap = |
| 2968 | createNextIterationMap(HeaderBBDom.get_space(), LoopDepth); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2969 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2970 | isl::set UnionBackedgeCondition = HeaderBBDom.empty(HeaderBBDom.get_space()); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2971 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 2972 | SmallVector<BasicBlock *, 4> LatchBlocks; |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2973 | L->getLoopLatches(LatchBlocks); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2974 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2975 | for (BasicBlock *LatchBB : LatchBlocks) { |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2976 | // If the latch is only reachable via error statements we skip it. |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2977 | isl::set LatchBBDom = DomainMap.lookup(LatchBB); |
Johannes Doerfert | f567380 | 2015-10-01 23:48:18 +0000 | [diff] [blame] | 2978 | if (!LatchBBDom) |
| 2979 | continue; |
| 2980 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2981 | isl::set BackedgeCondition = nullptr; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2982 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2983 | TerminatorInst *TI = LatchBB->getTerminator(); |
| 2984 | BranchInst *BI = dyn_cast<BranchInst>(TI); |
Tobias Grosser | bbaeda3 | 2016-11-10 05:20:29 +0000 | [diff] [blame] | 2985 | assert(BI && "Only branch instructions allowed in loop latches"); |
| 2986 | |
| 2987 | if (BI->isUnconditional()) |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2988 | BackedgeCondition = LatchBBDom; |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2989 | else { |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 2990 | SmallVector<isl_set *, 8> ConditionSets; |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2991 | int idx = BI->getSuccessor(0) != HeaderBB; |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2992 | if (!buildConditionSets(*this, LatchBB, TI, L, LatchBBDom.get(), |
| 2993 | InvalidDomainMap, ConditionSets)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 2994 | return false; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2995 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 2996 | // Free the non back edge condition set as we do not need it. |
| 2997 | isl_set_free(ConditionSets[1 - idx]); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 2998 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 2999 | BackedgeCondition = isl::manage(ConditionSets[idx]); |
Johannes Doerfert | 06c57b5 | 2015-09-20 15:00:20 +0000 | [diff] [blame] | 3000 | } |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3001 | |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3002 | int LatchLoopDepth = getRelativeLoopDepth(LI.getLoopFor(LatchBB)); |
| 3003 | assert(LatchLoopDepth >= LoopDepth); |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3004 | BackedgeCondition = BackedgeCondition.project_out( |
| 3005 | isl::dim::set, LoopDepth + 1, LatchLoopDepth - LoopDepth); |
| 3006 | UnionBackedgeCondition = UnionBackedgeCondition.unite(BackedgeCondition); |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3007 | } |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3008 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3009 | isl::map ForwardMap = ForwardMap.lex_le(HeaderBBDom.get_space()); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3010 | for (int i = 0; i < LoopDepth; i++) |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3011 | ForwardMap = ForwardMap.equate(isl::dim::in, i, isl::dim::out, i); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3012 | |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3013 | isl::set UnionBackedgeConditionComplement = |
| 3014 | UnionBackedgeCondition.complement(); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3015 | UnionBackedgeConditionComplement = |
Tobias Grosser | 325204a3 | 2017-07-15 12:41:32 +0000 | [diff] [blame] | 3016 | UnionBackedgeConditionComplement.lower_bound_si(isl::dim::set, LoopDepth, |
| 3017 | 0); |
| 3018 | UnionBackedgeConditionComplement = |
| 3019 | UnionBackedgeConditionComplement.apply(ForwardMap); |
| 3020 | HeaderBBDom = HeaderBBDom.subtract(UnionBackedgeConditionComplement); |
| 3021 | HeaderBBDom = HeaderBBDom.apply(NextIterationMap); |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3022 | |
Tobias Grosser | 78a8494 | 2018-06-01 19:12:00 +0000 | [diff] [blame] | 3023 | auto Parts = partitionSetParts(HeaderBBDom, LoopDepth); |
| 3024 | HeaderBBDom = Parts.second; |
Johannes Doerfert | f2cc86e | 2015-09-20 16:15:32 +0000 | [diff] [blame] | 3025 | |
Johannes Doerfert | 6a72a2a | 2015-09-20 16:59:23 +0000 | [diff] [blame] | 3026 | // Check if there is a <nsw> tagged AddRec for this loop and if so do not add |
| 3027 | // the bounded assumptions to the context as they are already implied by the |
| 3028 | // <nsw> tag. |
Tobias Grosser | 78a8494 | 2018-06-01 19:12:00 +0000 | [diff] [blame] | 3029 | if (Affinator.hasNSWAddRecForLoop(L)) |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3030 | return true; |
Johannes Doerfert | 6a72a2a | 2015-09-20 16:59:23 +0000 | [diff] [blame] | 3031 | |
Tobias Grosser | 78a8494 | 2018-06-01 19:12:00 +0000 | [diff] [blame] | 3032 | isl::set UnboundedCtx = Parts.first.params(); |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 3033 | recordAssumption(INFINITELOOP, UnboundedCtx, |
| 3034 | HeaderBB->getTerminator()->getDebugLoc(), AS_RESTRICTION); |
Johannes Doerfert | 297c720 | 2016-05-10 13:06:42 +0000 | [diff] [blame] | 3035 | return true; |
Johannes Doerfert | 5b9ff8b | 2015-09-10 13:00:06 +0000 | [diff] [blame] | 3036 | } |
| 3037 | |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3038 | MemoryAccess *Scop::lookupBasePtrAccess(MemoryAccess *MA) { |
Tobias Grosser | be372d5 | 2017-02-09 10:11:58 +0000 | [diff] [blame] | 3039 | Value *PointerBase = MA->getOriginalBaseAddr(); |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3040 | |
Tobias Grosser | e0e0e4d | 2017-02-09 09:34:46 +0000 | [diff] [blame] | 3041 | auto *PointerBaseInst = dyn_cast<Instruction>(PointerBase); |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3042 | if (!PointerBaseInst) |
| 3043 | return nullptr; |
| 3044 | |
| 3045 | auto *BasePtrStmt = getStmtFor(PointerBaseInst); |
| 3046 | if (!BasePtrStmt) |
| 3047 | return nullptr; |
| 3048 | |
| 3049 | return BasePtrStmt->getArrayAccessOrNULLFor(PointerBaseInst); |
| 3050 | } |
| 3051 | |
| 3052 | bool Scop::hasNonHoistableBasePtrInScop(MemoryAccess *MA, |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3053 | isl::union_map Writes) { |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3054 | if (auto *BasePtrMA = lookupBasePtrAccess(MA)) { |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3055 | return getNonHoistableCtx(BasePtrMA, Writes).is_null(); |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3056 | } |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3057 | |
Tobias Grosser | be372d5 | 2017-02-09 10:11:58 +0000 | [diff] [blame] | 3058 | Value *BaseAddr = MA->getOriginalBaseAddr(); |
Tobias Grosser | e0e0e4d | 2017-02-09 09:34:46 +0000 | [diff] [blame] | 3059 | if (auto *BasePtrInst = dyn_cast<Instruction>(BaseAddr)) |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3060 | if (!isa<LoadInst>(BasePtrInst)) |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 3061 | return contains(BasePtrInst); |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3062 | |
| 3063 | return false; |
| 3064 | } |
| 3065 | |
Johannes Doerfert | 5210da5 | 2016-06-02 11:06:54 +0000 | [diff] [blame] | 3066 | bool Scop::buildAliasChecks(AliasAnalysis &AA) { |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3067 | if (!PollyUseRuntimeAliasChecks) |
Johannes Doerfert | 5210da5 | 2016-06-02 11:06:54 +0000 | [diff] [blame] | 3068 | return true; |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3069 | |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 3070 | if (buildAliasGroups(AA)) { |
| 3071 | // Aliasing assumptions do not go through addAssumption but we still want to |
| 3072 | // collect statistics so we do it here explicitly. |
| 3073 | if (MinMaxAliasGroups.size()) |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 3074 | AssumptionsAliasing++; |
Johannes Doerfert | 5210da5 | 2016-06-02 11:06:54 +0000 | [diff] [blame] | 3075 | return true; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 3076 | } |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3077 | |
| 3078 | // If a problem occurs while building the alias groups we need to delete |
| 3079 | // this SCoP and pretend it wasn't valid in the first place. To this end |
| 3080 | // we make the assumed context infeasible. |
Tobias Grosser | 8d4f626 | 2015-12-12 09:52:26 +0000 | [diff] [blame] | 3081 | invalidate(ALIASING, DebugLoc()); |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3082 | |
Nicola Zaghen | 349506a | 2018-05-15 13:37:17 +0000 | [diff] [blame] | 3083 | LLVM_DEBUG( |
| 3084 | dbgs() << "\n\nNOTE: Run time checks for " << getNameStr() |
| 3085 | << " could not be created as the number of parameters involved " |
| 3086 | "is too high. The SCoP will be " |
| 3087 | "dismissed.\nUse:\n\t--polly-rtc-max-parameters=X\nto adjust " |
| 3088 | "the maximal number of parameters but be advised that the " |
| 3089 | "compile time might increase exponentially.\n\n"); |
Johannes Doerfert | 5210da5 | 2016-06-02 11:06:54 +0000 | [diff] [blame] | 3090 | return false; |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 3091 | } |
| 3092 | |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3093 | std::tuple<Scop::AliasGroupVectorTy, DenseSet<const ScopArrayInfo *>> |
Tobias Grosser | 9edcf07 | 2017-01-16 14:07:57 +0000 | [diff] [blame] | 3094 | Scop::buildAliasGroupsForAccesses(AliasAnalysis &AA) { |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3095 | AliasSetTracker AST(AA); |
| 3096 | |
| 3097 | DenseMap<Value *, MemoryAccess *> PtrToAcc; |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3098 | DenseSet<const ScopArrayInfo *> HasWriteAccess; |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 3099 | for (ScopStmt &Stmt : *this) { |
Johannes Doerfert | f1ee262 | 2014-10-06 17:43:00 +0000 | [diff] [blame] | 3100 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 3101 | isl_set *StmtDomain = Stmt.getDomain().release(); |
Johannes Doerfert | f1ee262 | 2014-10-06 17:43:00 +0000 | [diff] [blame] | 3102 | bool StmtDomainEmpty = isl_set_is_empty(StmtDomain); |
| 3103 | isl_set_free(StmtDomain); |
Tobias Grosser | 9edcf07 | 2017-01-16 14:07:57 +0000 | [diff] [blame] | 3104 | |
| 3105 | // Statements with an empty domain will never be executed. |
Johannes Doerfert | f1ee262 | 2014-10-06 17:43:00 +0000 | [diff] [blame] | 3106 | if (StmtDomainEmpty) |
| 3107 | continue; |
| 3108 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 3109 | for (MemoryAccess *MA : Stmt) { |
Tobias Grosser | a535dff | 2015-12-13 19:59:01 +0000 | [diff] [blame] | 3110 | if (MA->isScalarKind()) |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3111 | continue; |
Johannes Doerfert | 1377173 | 2014-10-01 12:40:46 +0000 | [diff] [blame] | 3112 | if (!MA->isRead()) |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3113 | HasWriteAccess.insert(MA->getScopArrayInfo()); |
Michael Kruse | 70131d3 | 2016-01-27 17:09:17 +0000 | [diff] [blame] | 3114 | MemAccInst Acc(MA->getAccessInstruction()); |
Hongbin Zheng | 8efb22e | 2016-02-27 01:49:58 +0000 | [diff] [blame] | 3115 | if (MA->isRead() && isa<MemTransferInst>(Acc)) |
Michael Kruse | 426e6f7 | 2016-10-25 13:37:43 +0000 | [diff] [blame] | 3116 | PtrToAcc[cast<MemTransferInst>(Acc)->getRawSource()] = MA; |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 3117 | else |
| 3118 | PtrToAcc[Acc.getPointerOperand()] = MA; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3119 | AST.add(Acc); |
| 3120 | } |
| 3121 | } |
| 3122 | |
Tobias Grosser | 9edcf07 | 2017-01-16 14:07:57 +0000 | [diff] [blame] | 3123 | AliasGroupVectorTy AliasGroups; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3124 | for (AliasSet &AS : AST) { |
Johannes Doerfert | 74f6869 | 2014-10-08 02:23:48 +0000 | [diff] [blame] | 3125 | if (AS.isMustAlias() || AS.isForwardingAliasSet()) |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3126 | continue; |
| 3127 | AliasGroupTy AG; |
Johannes Doerfert | a90943d | 2016-02-21 16:37:25 +0000 | [diff] [blame] | 3128 | for (auto &PR : AS) |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3129 | AG.push_back(PtrToAcc[PR.getValue()]); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 3130 | if (AG.size() < 2) |
| 3131 | continue; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3132 | AliasGroups.push_back(std::move(AG)); |
| 3133 | } |
| 3134 | |
Tobias Grosser | 9edcf07 | 2017-01-16 14:07:57 +0000 | [diff] [blame] | 3135 | return std::make_tuple(AliasGroups, HasWriteAccess); |
| 3136 | } |
| 3137 | |
Tobias Grosser | e39f912 | 2017-01-16 14:08:00 +0000 | [diff] [blame] | 3138 | void Scop::splitAliasGroupsByDomain(AliasGroupVectorTy &AliasGroups) { |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 3139 | for (unsigned u = 0; u < AliasGroups.size(); u++) { |
| 3140 | AliasGroupTy NewAG; |
| 3141 | AliasGroupTy &AG = AliasGroups[u]; |
| 3142 | AliasGroupTy::iterator AGI = AG.begin(); |
| 3143 | isl_set *AGDomain = getAccessDomain(*AGI); |
| 3144 | while (AGI != AG.end()) { |
| 3145 | MemoryAccess *MA = *AGI; |
| 3146 | isl_set *MADomain = getAccessDomain(MA); |
| 3147 | if (isl_set_is_disjoint(AGDomain, MADomain)) { |
| 3148 | NewAG.push_back(MA); |
| 3149 | AGI = AG.erase(AGI); |
| 3150 | isl_set_free(MADomain); |
| 3151 | } else { |
| 3152 | AGDomain = isl_set_union(AGDomain, MADomain); |
| 3153 | AGI++; |
| 3154 | } |
| 3155 | } |
| 3156 | if (NewAG.size() > 1) |
| 3157 | AliasGroups.push_back(std::move(NewAG)); |
| 3158 | isl_set_free(AGDomain); |
| 3159 | } |
Tobias Grosser | e39f912 | 2017-01-16 14:08:00 +0000 | [diff] [blame] | 3160 | } |
| 3161 | |
| 3162 | bool Scop::buildAliasGroups(AliasAnalysis &AA) { |
| 3163 | // To create sound alias checks we perform the following steps: |
| 3164 | // o) We partition each group into read only and non read only accesses. |
| 3165 | // o) For each group with more than one base pointer we then compute minimal |
| 3166 | // and maximal accesses to each array of a group in read only and non |
| 3167 | // read only partitions separately. |
| 3168 | AliasGroupVectorTy AliasGroups; |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3169 | DenseSet<const ScopArrayInfo *> HasWriteAccess; |
Tobias Grosser | e39f912 | 2017-01-16 14:08:00 +0000 | [diff] [blame] | 3170 | |
| 3171 | std::tie(AliasGroups, HasWriteAccess) = buildAliasGroupsForAccesses(AA); |
| 3172 | |
| 3173 | splitAliasGroupsByDomain(AliasGroups); |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 3174 | |
Johannes Doerfert | 1377173 | 2014-10-01 12:40:46 +0000 | [diff] [blame] | 3175 | for (AliasGroupTy &AG : AliasGroups) { |
Tobias Grosser | 78a7a6c | 2017-06-23 08:05:31 +0000 | [diff] [blame] | 3176 | if (!hasFeasibleRuntimeContext()) |
| 3177 | return false; |
| 3178 | |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 3179 | { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3180 | IslMaxOperationsGuard MaxOpGuard(getIslCtx().get(), OptComputeOut); |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 3181 | bool Valid = buildAliasGroup(AG, HasWriteAccess); |
| 3182 | if (!Valid) |
| 3183 | return false; |
| 3184 | } |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3185 | if (isl_ctx_last_error(getIslCtx().get()) == isl_error_quota) { |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 3186 | invalidate(COMPLEXITY, DebugLoc()); |
Tobias Grosser | 50d4e2e | 2015-03-28 14:50:32 +0000 | [diff] [blame] | 3187 | return false; |
Tobias Grosser | 57a1d36 | 2017-06-23 08:05:27 +0000 | [diff] [blame] | 3188 | } |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3189 | } |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 3190 | |
Tobias Grosser | 50d4e2e | 2015-03-28 14:50:32 +0000 | [diff] [blame] | 3191 | return true; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 3192 | } |
| 3193 | |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3194 | bool Scop::buildAliasGroup(Scop::AliasGroupTy &AliasGroup, |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3195 | DenseSet<const ScopArrayInfo *> HasWriteAccess) { |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3196 | AliasGroupTy ReadOnlyAccesses; |
| 3197 | AliasGroupTy ReadWriteAccesses; |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3198 | SmallPtrSet<const ScopArrayInfo *, 4> ReadWriteArrays; |
Tobias Grosser | 079d511 | 2017-02-18 20:51:29 +0000 | [diff] [blame] | 3199 | SmallPtrSet<const ScopArrayInfo *, 4> ReadOnlyArrays; |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3200 | |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3201 | if (AliasGroup.size() < 2) |
| 3202 | return true; |
| 3203 | |
| 3204 | for (MemoryAccess *Access : AliasGroup) { |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 3205 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "PossibleAlias", |
| 3206 | Access->getAccessInstruction()) |
| 3207 | << "Possibly aliasing pointer, use restrict keyword."); |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3208 | const ScopArrayInfo *Array = Access->getScopArrayInfo(); |
| 3209 | if (HasWriteAccess.count(Array)) { |
| 3210 | ReadWriteArrays.insert(Array); |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3211 | ReadWriteAccesses.push_back(Access); |
| 3212 | } else { |
Tobias Grosser | 079d511 | 2017-02-18 20:51:29 +0000 | [diff] [blame] | 3213 | ReadOnlyArrays.insert(Array); |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3214 | ReadOnlyAccesses.push_back(Access); |
| 3215 | } |
| 3216 | } |
| 3217 | |
Tobias Grosser | f3c145f | 2017-01-16 15:49:09 +0000 | [diff] [blame] | 3218 | // If there are no read-only pointers, and less than two read-write pointers, |
| 3219 | // no alias check is needed. |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3220 | if (ReadOnlyAccesses.empty() && ReadWriteArrays.size() <= 1) |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3221 | return true; |
| 3222 | |
Tobias Grosser | f3c145f | 2017-01-16 15:49:09 +0000 | [diff] [blame] | 3223 | // If there is no read-write pointer, no alias check is needed. |
Tobias Grosser | 889830b | 2017-02-09 23:12:22 +0000 | [diff] [blame] | 3224 | if (ReadWriteArrays.empty()) |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3225 | return true; |
| 3226 | |
Tobias Grosser | f3c145f | 2017-01-16 15:49:09 +0000 | [diff] [blame] | 3227 | // For non-affine accesses, no alias check can be generated as we cannot |
| 3228 | // compute a sufficiently tight lower and upper bound: bail out. |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3229 | for (MemoryAccess *MA : AliasGroup) { |
| 3230 | if (!MA->isAffine()) { |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 3231 | invalidate(ALIASING, MA->getAccessInstruction()->getDebugLoc(), |
| 3232 | MA->getAccessInstruction()->getParent()); |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3233 | return false; |
| 3234 | } |
Tobias Grosser | 0032d87 | 2017-01-16 15:49:14 +0000 | [diff] [blame] | 3235 | } |
| 3236 | |
| 3237 | // Ensure that for all memory accesses for which we generate alias checks, |
| 3238 | // their base pointers are available. |
| 3239 | for (MemoryAccess *MA : AliasGroup) { |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3240 | if (MemoryAccess *BasePtrMA = lookupBasePtrAccess(MA)) |
| 3241 | addRequiredInvariantLoad( |
| 3242 | cast<LoadInst>(BasePtrMA->getAccessInstruction())); |
| 3243 | } |
| 3244 | |
| 3245 | MinMaxAliasGroups.emplace_back(); |
| 3246 | MinMaxVectorPairTy &pair = MinMaxAliasGroups.back(); |
| 3247 | MinMaxVectorTy &MinMaxAccessesReadWrite = pair.first; |
| 3248 | MinMaxVectorTy &MinMaxAccessesReadOnly = pair.second; |
| 3249 | |
| 3250 | bool Valid; |
| 3251 | |
| 3252 | Valid = |
| 3253 | calculateMinMaxAccess(ReadWriteAccesses, *this, MinMaxAccessesReadWrite); |
| 3254 | |
| 3255 | if (!Valid) |
| 3256 | return false; |
| 3257 | |
| 3258 | // Bail out if the number of values we need to compare is too large. |
| 3259 | // This is important as the number of comparisons grows quadratically with |
| 3260 | // the number of values we need to compare. |
Tobias Grosser | 079d511 | 2017-02-18 20:51:29 +0000 | [diff] [blame] | 3261 | if (MinMaxAccessesReadWrite.size() + ReadOnlyArrays.size() > |
Tobias Grosser | 77f3257 | 2017-01-16 15:49:07 +0000 | [diff] [blame] | 3262 | RunTimeChecksMaxArraysPerGroup) |
| 3263 | return false; |
| 3264 | |
| 3265 | Valid = |
| 3266 | calculateMinMaxAccess(ReadOnlyAccesses, *this, MinMaxAccessesReadOnly); |
| 3267 | |
| 3268 | if (!Valid) |
| 3269 | return false; |
| 3270 | |
| 3271 | return true; |
| 3272 | } |
| 3273 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 3274 | /// 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] | 3275 | static Loop *getLoopSurroundingScop(Scop &S, LoopInfo &LI) { |
Johannes Doerfert | dec27df | 2015-11-21 16:56:13 +0000 | [diff] [blame] | 3276 | // Start with the smallest loop containing the entry and expand that |
| 3277 | // loop until it contains all blocks in the region. If there is a loop |
| 3278 | // containing all blocks in the region check if it is itself contained |
| 3279 | // and if so take the parent loop as it will be the smallest containing |
| 3280 | // the region but not contained by it. |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 3281 | Loop *L = LI.getLoopFor(S.getEntry()); |
Johannes Doerfert | dec27df | 2015-11-21 16:56:13 +0000 | [diff] [blame] | 3282 | while (L) { |
| 3283 | bool AllContained = true; |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 3284 | for (auto *BB : S.blocks()) |
Johannes Doerfert | dec27df | 2015-11-21 16:56:13 +0000 | [diff] [blame] | 3285 | AllContained &= L->contains(BB); |
| 3286 | if (AllContained) |
| 3287 | break; |
| 3288 | L = L->getParentLoop(); |
| 3289 | } |
| 3290 | |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 3291 | return L ? (S.contains(L) ? L->getParentLoop() : L) : nullptr; |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 3292 | } |
| 3293 | |
Singapuram Sanjay Srivallabh | 1abd9ff | 2017-07-12 16:46:19 +0000 | [diff] [blame] | 3294 | int Scop::NextScopID = 0; |
| 3295 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 3296 | std::string Scop::CurrentFunc; |
Singapuram Sanjay Srivallabh | 1abd9ff | 2017-07-12 16:46:19 +0000 | [diff] [blame] | 3297 | |
| 3298 | int Scop::getNextID(std::string ParentFunc) { |
| 3299 | if (ParentFunc != CurrentFunc) { |
| 3300 | CurrentFunc = ParentFunc; |
| 3301 | NextScopID = 0; |
| 3302 | } |
| 3303 | return NextScopID++; |
| 3304 | } |
| 3305 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 3306 | Scop::Scop(Region &R, ScalarEvolution &ScalarEvolution, LoopInfo &LI, |
Tobias Grosser | ee45759 | 2017-09-24 09:25:30 +0000 | [diff] [blame] | 3307 | DominatorTree &DT, ScopDetection::DetectionContext &DC, |
| 3308 | OptimizationRemarkEmitter &ORE) |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3309 | : IslCtx(isl_ctx_alloc(), isl_ctx_free), SE(&ScalarEvolution), DT(&DT), |
Philip Pfaffe | d477bb9 | 2018-05-15 14:53:25 +0000 | [diff] [blame] | 3310 | R(R), name(None), HasSingleExitEdge(R.getExitingBlock()), DC(DC), |
| 3311 | ORE(ORE), Affinator(this, LI), |
Singapuram Sanjay Srivallabh | 1abd9ff | 2017-07-12 16:46:19 +0000 | [diff] [blame] | 3312 | ID(getNextID((*R.getEntry()->getParent()).getName().str())) { |
Tobias Grosser | 2937b59 | 2016-04-29 11:43:20 +0000 | [diff] [blame] | 3313 | if (IslOnErrorAbort) |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3314 | isl_options_set_on_error(getIslCtx().get(), ISL_ON_ERROR_ABORT); |
Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 3315 | buildContext(); |
| 3316 | } |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 3317 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3318 | Scop::~Scop() = default; |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 3319 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3320 | void Scop::foldSizeConstantsToRight() { |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 3321 | isl_union_set *Accessed = isl_union_map_range(getAccesses().release()); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3322 | |
| 3323 | for (auto Array : arrays()) { |
| 3324 | if (Array->getNumberOfDimensions() <= 1) |
| 3325 | continue; |
| 3326 | |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 3327 | isl_space *Space = Array->getSpace().release(); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3328 | |
| 3329 | Space = isl_space_align_params(Space, isl_union_set_get_space(Accessed)); |
| 3330 | |
| 3331 | if (!isl_union_set_contains(Accessed, Space)) { |
| 3332 | isl_space_free(Space); |
| 3333 | continue; |
| 3334 | } |
| 3335 | |
| 3336 | isl_set *Elements = isl_union_set_extract_set(Accessed, Space); |
| 3337 | |
| 3338 | isl_map *Transform = |
Tobias Grosser | 77eef90 | 2017-07-21 23:07:56 +0000 | [diff] [blame] | 3339 | isl_map_universe(isl_space_map_from_set(Array->getSpace().release())); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3340 | |
| 3341 | std::vector<int> Int; |
| 3342 | |
| 3343 | int Dims = isl_set_dim(Elements, isl_dim_set); |
| 3344 | for (int i = 0; i < Dims; i++) { |
| 3345 | isl_set *DimOnly = |
| 3346 | isl_set_project_out(isl_set_copy(Elements), isl_dim_set, 0, i); |
| 3347 | DimOnly = isl_set_project_out(DimOnly, isl_dim_set, 1, Dims - i - 1); |
| 3348 | DimOnly = isl_set_lower_bound_si(DimOnly, isl_dim_set, 0, 0); |
| 3349 | |
| 3350 | isl_basic_set *DimHull = isl_set_affine_hull(DimOnly); |
| 3351 | |
| 3352 | if (i == Dims - 1) { |
| 3353 | Int.push_back(1); |
| 3354 | Transform = isl_map_equate(Transform, isl_dim_in, i, isl_dim_out, i); |
| 3355 | isl_basic_set_free(DimHull); |
| 3356 | continue; |
| 3357 | } |
| 3358 | |
| 3359 | if (isl_basic_set_dim(DimHull, isl_dim_div) == 1) { |
| 3360 | isl_aff *Diff = isl_basic_set_get_div(DimHull, 0); |
| 3361 | isl_val *Val = isl_aff_get_denominator_val(Diff); |
| 3362 | isl_aff_free(Diff); |
| 3363 | |
| 3364 | int ValInt = 1; |
| 3365 | |
Eli Friedman | a75d53c | 2018-01-17 21:59:02 +0000 | [diff] [blame] | 3366 | if (isl_val_is_int(Val)) { |
| 3367 | auto ValAPInt = APIntFromVal(Val); |
| 3368 | if (ValAPInt.isSignedIntN(32)) |
| 3369 | ValInt = ValAPInt.getSExtValue(); |
| 3370 | } else { |
| 3371 | isl_val_free(Val); |
| 3372 | } |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3373 | |
| 3374 | Int.push_back(ValInt); |
| 3375 | |
| 3376 | isl_constraint *C = isl_constraint_alloc_equality( |
| 3377 | isl_local_space_from_space(isl_map_get_space(Transform))); |
| 3378 | C = isl_constraint_set_coefficient_si(C, isl_dim_out, i, ValInt); |
| 3379 | C = isl_constraint_set_coefficient_si(C, isl_dim_in, i, -1); |
| 3380 | Transform = isl_map_add_constraint(Transform, C); |
| 3381 | isl_basic_set_free(DimHull); |
| 3382 | continue; |
| 3383 | } |
| 3384 | |
| 3385 | isl_basic_set *ZeroSet = isl_basic_set_copy(DimHull); |
| 3386 | ZeroSet = isl_basic_set_fix_si(ZeroSet, isl_dim_set, 0, 0); |
| 3387 | |
| 3388 | int ValInt = 1; |
| 3389 | if (isl_basic_set_is_equal(ZeroSet, DimHull)) { |
| 3390 | ValInt = 0; |
| 3391 | } |
| 3392 | |
| 3393 | Int.push_back(ValInt); |
| 3394 | Transform = isl_map_equate(Transform, isl_dim_in, i, isl_dim_out, i); |
| 3395 | isl_basic_set_free(DimHull); |
| 3396 | isl_basic_set_free(ZeroSet); |
| 3397 | } |
| 3398 | |
| 3399 | isl_set *MappedElements = isl_map_domain(isl_map_copy(Transform)); |
| 3400 | |
| 3401 | if (!isl_set_is_subset(Elements, MappedElements)) { |
| 3402 | isl_set_free(Elements); |
| 3403 | isl_set_free(MappedElements); |
| 3404 | isl_map_free(Transform); |
| 3405 | continue; |
| 3406 | } |
| 3407 | |
| 3408 | isl_set_free(MappedElements); |
| 3409 | |
| 3410 | bool CanFold = true; |
| 3411 | |
| 3412 | if (Int[0] <= 1) |
| 3413 | CanFold = false; |
| 3414 | |
| 3415 | unsigned NumDims = Array->getNumberOfDimensions(); |
| 3416 | for (unsigned i = 1; i < NumDims - 1; i++) |
| 3417 | if (Int[0] != Int[i] && Int[i]) |
| 3418 | CanFold = false; |
| 3419 | |
| 3420 | if (!CanFold) { |
| 3421 | isl_set_free(Elements); |
| 3422 | isl_map_free(Transform); |
| 3423 | continue; |
| 3424 | } |
| 3425 | |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3426 | for (auto &Access : AccessFunctions) |
| 3427 | if (Access->getScopArrayInfo() == Array) |
Tobias Grosser | 6d58804 | 2017-08-02 19:27:16 +0000 | [diff] [blame] | 3428 | Access->setAccessRelation(Access->getAccessRelation().apply_range( |
Tobias Grosser | 718d04c | 2018-02-20 07:26:58 +0000 | [diff] [blame] | 3429 | isl::manage_copy(Transform))); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3430 | |
| 3431 | isl_map_free(Transform); |
| 3432 | |
| 3433 | std::vector<const SCEV *> Sizes; |
| 3434 | for (unsigned i = 0; i < NumDims; i++) { |
| 3435 | auto Size = Array->getDimensionSize(i); |
| 3436 | |
| 3437 | if (i == NumDims - 1) |
| 3438 | Size = SE->getMulExpr(Size, SE->getConstant(Size->getType(), Int[0])); |
| 3439 | Sizes.push_back(Size); |
| 3440 | } |
| 3441 | |
| 3442 | Array->updateSizes(Sizes, false /* CheckConsistency */); |
| 3443 | |
| 3444 | isl_set_free(Elements); |
| 3445 | } |
| 3446 | isl_union_set_free(Accessed); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3447 | } |
| 3448 | |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 3449 | void Scop::markFortranArrays() { |
| 3450 | for (ScopStmt &Stmt : Stmts) { |
| 3451 | for (MemoryAccess *MemAcc : Stmt) { |
| 3452 | Value *FAD = MemAcc->getFortranArrayDescriptor(); |
| 3453 | if (!FAD) |
| 3454 | continue; |
| 3455 | |
| 3456 | // TODO: const_cast-ing to edit |
| 3457 | ScopArrayInfo *SAI = |
| 3458 | const_cast<ScopArrayInfo *>(MemAcc->getLatestScopArrayInfo()); |
| 3459 | assert(SAI && "memory access into a Fortran array does not " |
| 3460 | "have an associated ScopArrayInfo"); |
| 3461 | SAI->applyAndSetFAD(FAD); |
| 3462 | } |
| 3463 | } |
| 3464 | } |
| 3465 | |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 3466 | void Scop::finalizeAccesses() { |
| 3467 | updateAccessDimensionality(); |
Tobias Grosser | bedef00 | 2016-12-02 08:10:56 +0000 | [diff] [blame] | 3468 | foldSizeConstantsToRight(); |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 3469 | foldAccessRelations(); |
| 3470 | assumeNoOutOfBounds(); |
Siddharth Bhat | b7f68b8 | 2017-05-19 15:07:45 +0000 | [diff] [blame] | 3471 | markFortranArrays(); |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 3472 | } |
| 3473 | |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 3474 | void Scop::updateAccessDimensionality() { |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3475 | // Check all array accesses for each base pointer and find a (virtual) element |
| 3476 | // size for the base pointer that divides all access functions. |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3477 | for (ScopStmt &Stmt : *this) |
| 3478 | for (MemoryAccess *Access : Stmt) { |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3479 | if (!Access->isArrayKind()) |
| 3480 | continue; |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3481 | ScopArrayInfo *Array = |
Tobias Grosser | e24b7b9 | 2017-02-09 23:24:57 +0000 | [diff] [blame] | 3482 | const_cast<ScopArrayInfo *>(Access->getScopArrayInfo()); |
| 3483 | |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3484 | if (Array->getNumberOfDimensions() != 1) |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3485 | continue; |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3486 | unsigned DivisibleSize = Array->getElemSizeInBytes(); |
| 3487 | const SCEV *Subscript = Access->getSubscript(0); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3488 | while (!isDivisible(Subscript, DivisibleSize, *SE)) |
| 3489 | DivisibleSize /= 2; |
| 3490 | auto *Ty = IntegerType::get(SE->getContext(), DivisibleSize * 8); |
Tobias Grosser | 9c7d181 | 2017-02-09 23:24:54 +0000 | [diff] [blame] | 3491 | Array->updateElementType(Ty); |
Johannes Doerfert | 4d9bb8d | 2016-02-18 16:50:12 +0000 | [diff] [blame] | 3492 | } |
| 3493 | |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 3494 | for (auto &Stmt : *this) |
| 3495 | for (auto &Access : Stmt) |
| 3496 | Access->updateDimensionality(); |
| 3497 | } |
| 3498 | |
Tobias Grosser | 491b799 | 2016-12-02 05:21:22 +0000 | [diff] [blame] | 3499 | void Scop::foldAccessRelations() { |
| 3500 | for (auto &Stmt : *this) |
| 3501 | for (auto &Access : Stmt) |
| 3502 | Access->foldAccessRelation(); |
| 3503 | } |
| 3504 | |
| 3505 | void Scop::assumeNoOutOfBounds() { |
| 3506 | for (auto &Stmt : *this) |
| 3507 | for (auto &Access : Stmt) |
| 3508 | Access->assumeNoOutOfBound(); |
| 3509 | } |
| 3510 | |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3511 | void Scop::removeFromStmtMap(ScopStmt &Stmt) { |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 3512 | for (Instruction *Inst : Stmt.getInstructions()) |
| 3513 | InstStmtMap.erase(Inst); |
| 3514 | |
| 3515 | if (Stmt.isRegionStmt()) { |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 3516 | for (BasicBlock *BB : Stmt.getRegion()->blocks()) { |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3517 | StmtMap.erase(BB); |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 3518 | // Skip entry basic block, as its instructions are already deleted as |
| 3519 | // part of the statement's instruction list. |
| 3520 | if (BB == Stmt.getEntryBlock()) |
| 3521 | continue; |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 3522 | for (Instruction &Inst : *BB) |
| 3523 | InstStmtMap.erase(&Inst); |
| 3524 | } |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 3525 | } else { |
Michael Kruse | 0c6c555 | 2017-09-01 11:36:52 +0000 | [diff] [blame] | 3526 | auto StmtMapIt = StmtMap.find(Stmt.getBasicBlock()); |
| 3527 | if (StmtMapIt != StmtMap.end()) |
| 3528 | StmtMapIt->second.erase(std::remove(StmtMapIt->second.begin(), |
| 3529 | StmtMapIt->second.end(), &Stmt), |
| 3530 | StmtMapIt->second.end()); |
| 3531 | for (Instruction *Inst : Stmt.getInstructions()) |
| 3532 | InstStmtMap.erase(Inst); |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 3533 | } |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3534 | } |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 3535 | |
Michael Kruse | 192e7f7 | 2018-04-09 23:13:05 +0000 | [diff] [blame] | 3536 | void Scop::removeStmts(std::function<bool(ScopStmt &)> ShouldDelete, |
| 3537 | bool AfterHoisting) { |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3538 | for (auto StmtIt = Stmts.begin(), StmtEnd = Stmts.end(); StmtIt != StmtEnd;) { |
| 3539 | if (!ShouldDelete(*StmtIt)) { |
| 3540 | StmtIt++; |
| 3541 | continue; |
| 3542 | } |
| 3543 | |
Michael Kruse | 192e7f7 | 2018-04-09 23:13:05 +0000 | [diff] [blame] | 3544 | // Start with removing all of the statement's accesses including erasing it |
| 3545 | // from all maps that are pointing to them. |
Michael Kruse | db6f71e | 2018-04-10 01:20:41 +0000 | [diff] [blame] | 3546 | // Make a temporary copy because removing MAs invalidates the iterator. |
| 3547 | SmallVector<MemoryAccess *, 16> MAList(StmtIt->begin(), StmtIt->end()); |
| 3548 | for (MemoryAccess *MA : MAList) |
Michael Kruse | 192e7f7 | 2018-04-09 23:13:05 +0000 | [diff] [blame] | 3549 | StmtIt->removeSingleMemoryAccess(MA, AfterHoisting); |
| 3550 | |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3551 | removeFromStmtMap(*StmtIt); |
| 3552 | StmtIt = Stmts.erase(StmtIt); |
| 3553 | } |
| 3554 | } |
| 3555 | |
| 3556 | void Scop::removeStmtNotInDomainMap() { |
| 3557 | auto ShouldDelete = [this](ScopStmt &Stmt) -> bool { |
Tobias Grosser | 199ec4a | 2017-07-19 16:31:10 +0000 | [diff] [blame] | 3558 | return !this->DomainMap.lookup(Stmt.getEntryBlock()); |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3559 | }; |
Michael Kruse | 192e7f7 | 2018-04-09 23:13:05 +0000 | [diff] [blame] | 3560 | removeStmts(ShouldDelete, false); |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3561 | } |
| 3562 | |
| 3563 | void Scop::simplifySCoP(bool AfterHoisting) { |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3564 | auto ShouldDelete = [AfterHoisting](ScopStmt &Stmt) -> bool { |
Michael Kruse | 5369ea5 | 2018-04-20 18:55:44 +0000 | [diff] [blame] | 3565 | // Never delete statements that contain calls to debug functions. |
| 3566 | if (hasDebugCall(&Stmt)) |
| 3567 | return false; |
| 3568 | |
Johannes Doerfert | 2640454 | 2016-05-10 12:19:47 +0000 | [diff] [blame] | 3569 | bool RemoveStmt = Stmt.isEmpty(); |
Johannes Doerfert | f17a78e | 2015-10-04 15:00:05 +0000 | [diff] [blame] | 3570 | |
Tobias Grosser | 3012a0b | 2017-07-16 22:44:17 +0000 | [diff] [blame] | 3571 | // Remove read only statements only after invariant load hoisting. |
Johannes Doerfert | 2640454 | 2016-05-10 12:19:47 +0000 | [diff] [blame] | 3572 | if (!RemoveStmt && AfterHoisting) { |
Johannes Doerfert | eca9e89 | 2015-11-03 16:54:49 +0000 | [diff] [blame] | 3573 | bool OnlyRead = true; |
| 3574 | for (MemoryAccess *MA : Stmt) { |
| 3575 | if (MA->isRead()) |
| 3576 | continue; |
| 3577 | |
| 3578 | OnlyRead = false; |
| 3579 | break; |
| 3580 | } |
| 3581 | |
| 3582 | RemoveStmt = OnlyRead; |
| 3583 | } |
Tobias Grosser | 21cbcf0 | 2017-07-16 23:55:38 +0000 | [diff] [blame] | 3584 | return RemoveStmt; |
| 3585 | }; |
Johannes Doerfert | eca9e89 | 2015-11-03 16:54:49 +0000 | [diff] [blame] | 3586 | |
Michael Kruse | 192e7f7 | 2018-04-09 23:13:05 +0000 | [diff] [blame] | 3587 | removeStmts(ShouldDelete, AfterHoisting); |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 3588 | } |
| 3589 | |
Johannes Doerfert | 8ab2803 | 2016-04-27 12:49:11 +0000 | [diff] [blame] | 3590 | InvariantEquivClassTy *Scop::lookupInvariantEquivClass(Value *Val) { |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3591 | LoadInst *LInst = dyn_cast<LoadInst>(Val); |
| 3592 | if (!LInst) |
| 3593 | return nullptr; |
| 3594 | |
| 3595 | if (Value *Rep = InvEquivClassVMap.lookup(LInst)) |
| 3596 | LInst = cast<LoadInst>(Rep); |
| 3597 | |
Johannes Doerfert | 96e5471 | 2016-02-07 17:30:13 +0000 | [diff] [blame] | 3598 | Type *Ty = LInst->getType(); |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3599 | const SCEV *PointerSCEV = SE->getSCEV(LInst->getPointerOperand()); |
Johannes Doerfert | 549768c | 2016-03-24 13:22:16 +0000 | [diff] [blame] | 3600 | for (auto &IAClass : InvariantEquivClasses) { |
Tobias Grosser | faef9a7 | 2016-07-11 12:27:04 +0000 | [diff] [blame] | 3601 | if (PointerSCEV != IAClass.IdentifyingPointer || Ty != IAClass.AccessType) |
Johannes Doerfert | 549768c | 2016-03-24 13:22:16 +0000 | [diff] [blame] | 3602 | continue; |
| 3603 | |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 3604 | auto &MAs = IAClass.InvariantAccesses; |
Johannes Doerfert | 549768c | 2016-03-24 13:22:16 +0000 | [diff] [blame] | 3605 | for (auto *MA : MAs) |
| 3606 | if (MA->getAccessInstruction() == Val) |
| 3607 | return &IAClass; |
| 3608 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3609 | |
| 3610 | return nullptr; |
| 3611 | } |
| 3612 | |
Siddharth Bhat | 7bc77e8 | 2017-08-21 11:57:04 +0000 | [diff] [blame] | 3613 | bool isAParameter(llvm::Value *maybeParam, const Function &F) { |
| 3614 | for (const llvm::Argument &Arg : F.args()) |
| 3615 | if (&Arg == maybeParam) |
| 3616 | return true; |
| 3617 | |
| 3618 | return false; |
Michael Kruse | 594386e | 2017-08-23 12:34:37 +0000 | [diff] [blame] | 3619 | } |
Siddharth Bhat | 7bc77e8 | 2017-08-21 11:57:04 +0000 | [diff] [blame] | 3620 | |
Tobias Grosser | 305d316 | 2017-08-07 00:10:11 +0000 | [diff] [blame] | 3621 | bool Scop::canAlwaysBeHoisted(MemoryAccess *MA, bool StmtInvalidCtxIsEmpty, |
| 3622 | bool MAInvalidCtxIsEmpty, |
| 3623 | bool NonHoistableCtxIsEmpty) { |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3624 | LoadInst *LInst = cast<LoadInst>(MA->getAccessInstruction()); |
| 3625 | const DataLayout &DL = LInst->getParent()->getModule()->getDataLayout(); |
Siddharth Bhat | 7bc77e8 | 2017-08-21 11:57:04 +0000 | [diff] [blame] | 3626 | if (PollyAllowDereferenceOfAllFunctionParams && |
| 3627 | isAParameter(LInst->getPointerOperand(), getFunction())) |
| 3628 | return true; |
| 3629 | |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3630 | // TODO: We can provide more information for better but more expensive |
| 3631 | // results. |
| 3632 | if (!isDereferenceableAndAlignedPointer(LInst->getPointerOperand(), |
| 3633 | LInst->getAlignment(), DL)) |
| 3634 | return false; |
| 3635 | |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3636 | // If the location might be overwritten we do not hoist it unconditionally. |
| 3637 | // |
Siddharth Bhat | 83fe6b5 | 2017-08-08 12:26:32 +0000 | [diff] [blame] | 3638 | // TODO: This is probably too conservative. |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3639 | if (!NonHoistableCtxIsEmpty) |
| 3640 | return false; |
| 3641 | |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 3642 | // If a dereferenceable load is in a statement that is modeled precisely we |
| 3643 | // can hoist it. |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 3644 | if (StmtInvalidCtxIsEmpty && MAInvalidCtxIsEmpty) |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3645 | return true; |
| 3646 | |
| 3647 | // Even if the statement is not modeled precisely we can hoist the load if it |
Tobias Grosser | cdbe5c9 | 2017-01-06 17:30:34 +0000 | [diff] [blame] | 3648 | // does not involve any parameters that might have been specialized by the |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3649 | // statement domain. |
| 3650 | for (unsigned u = 0, e = MA->getNumSubscripts(); u < e; u++) |
| 3651 | if (!isa<SCEVConstant>(MA->getSubscript(u))) |
| 3652 | return false; |
| 3653 | return true; |
| 3654 | } |
| 3655 | |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3656 | void Scop::addInvariantLoads(ScopStmt &Stmt, InvariantAccessesTy &InvMAs) { |
Johannes Doerfert | 5d03f84 | 2016-04-22 11:38:44 +0000 | [diff] [blame] | 3657 | if (InvMAs.empty()) |
| 3658 | return; |
| 3659 | |
Tobias Grosser | 2332fa3 | 2017-08-06 15:36:48 +0000 | [diff] [blame] | 3660 | isl::set StmtInvalidCtx = Stmt.getInvalidContext(); |
| 3661 | bool StmtInvalidCtxIsEmpty = StmtInvalidCtx.is_empty(); |
Johannes Doerfert | d77089e | 2016-04-22 11:41:14 +0000 | [diff] [blame] | 3662 | |
Johannes Doerfert | 3ef78d6 | 2016-04-08 10:30:09 +0000 | [diff] [blame] | 3663 | // Get the context under which the statement is executed but remove the error |
| 3664 | // context under which this statement is reached. |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3665 | isl::set DomainCtx = Stmt.getDomain().params(); |
| 3666 | DomainCtx = DomainCtx.subtract(StmtInvalidCtx); |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3667 | |
Philip Pfaffe | 9375d57 | 2018-05-16 14:05:03 +0000 | [diff] [blame] | 3668 | if (DomainCtx.n_basic_set() >= MaxDisjunctsInDomain) { |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3669 | auto *AccInst = InvMAs.front().MA->getAccessInstruction(); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 3670 | invalidate(COMPLEXITY, AccInst->getDebugLoc(), AccInst->getParent()); |
Johannes Doerfert | d77089e | 2016-04-22 11:41:14 +0000 | [diff] [blame] | 3671 | return; |
| 3672 | } |
| 3673 | |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3674 | // Project out all parameters that relate to loads in the statement. Otherwise |
| 3675 | // we could have cyclic dependences on the constraints under which the |
| 3676 | // hoisted loads are executed and we could not determine an order in which to |
| 3677 | // pre-load them. This happens because not only lower bounds are part of the |
| 3678 | // domain but also upper bounds. |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3679 | for (auto &InvMA : InvMAs) { |
| 3680 | auto *MA = InvMA.MA; |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3681 | Instruction *AccInst = MA->getAccessInstruction(); |
| 3682 | if (SE->isSCEVable(AccInst->getType())) { |
Johannes Doerfert | 44483c5 | 2015-11-07 19:45:27 +0000 | [diff] [blame] | 3683 | SetVector<Value *> Values; |
| 3684 | for (const SCEV *Parameter : Parameters) { |
| 3685 | Values.clear(); |
Johannes Doerfert | 7b81103 | 2016-04-08 10:25:58 +0000 | [diff] [blame] | 3686 | findValues(Parameter, *SE, Values); |
Johannes Doerfert | 44483c5 | 2015-11-07 19:45:27 +0000 | [diff] [blame] | 3687 | if (!Values.count(AccInst)) |
| 3688 | continue; |
| 3689 | |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3690 | if (isl::id ParamId = getIdForParam(Parameter)) { |
| 3691 | int Dim = DomainCtx.find_dim_by_id(isl::dim::param, ParamId); |
Tobias Grosser | b58ed8d | 2017-03-17 09:02:53 +0000 | [diff] [blame] | 3692 | if (Dim >= 0) |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3693 | DomainCtx = DomainCtx.eliminate(isl::dim::param, Dim, 1); |
Johannes Doerfert | 44483c5 | 2015-11-07 19:45:27 +0000 | [diff] [blame] | 3694 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3695 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3696 | } |
| 3697 | } |
| 3698 | |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3699 | for (auto &InvMA : InvMAs) { |
| 3700 | auto *MA = InvMA.MA; |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3701 | isl::set NHCtx = InvMA.NonHoistableCtx; |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3702 | |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3703 | // Check for another invariant access that accesses the same location as |
| 3704 | // MA and if found consolidate them. Otherwise create a new equivalence |
| 3705 | // class at the end of InvariantEquivClasses. |
| 3706 | LoadInst *LInst = cast<LoadInst>(MA->getAccessInstruction()); |
Johannes Doerfert | 96e5471 | 2016-02-07 17:30:13 +0000 | [diff] [blame] | 3707 | Type *Ty = LInst->getType(); |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3708 | const SCEV *PointerSCEV = SE->getSCEV(LInst->getPointerOperand()); |
| 3709 | |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3710 | isl::set MAInvalidCtx = MA->getInvalidContext(); |
| 3711 | bool NonHoistableCtxIsEmpty = NHCtx.is_empty(); |
| 3712 | bool MAInvalidCtxIsEmpty = MAInvalidCtx.is_empty(); |
Johannes Doerfert | 85676e3 | 2016-04-23 14:32:34 +0000 | [diff] [blame] | 3713 | |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3714 | isl::set MACtx; |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3715 | // Check if we know that this pointer can be speculatively accessed. |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3716 | if (canAlwaysBeHoisted(MA, StmtInvalidCtxIsEmpty, MAInvalidCtxIsEmpty, |
| 3717 | NonHoistableCtxIsEmpty)) { |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3718 | MACtx = isl::set::universe(DomainCtx.get_space()); |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3719 | } else { |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3720 | MACtx = DomainCtx; |
| 3721 | MACtx = MACtx.subtract(MAInvalidCtx.unite(NHCtx)); |
| 3722 | MACtx = MACtx.gist_params(getContext()); |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3723 | } |
| 3724 | |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3725 | bool Consolidated = false; |
| 3726 | for (auto &IAClass : InvariantEquivClasses) { |
Tobias Grosser | faef9a7 | 2016-07-11 12:27:04 +0000 | [diff] [blame] | 3727 | if (PointerSCEV != IAClass.IdentifyingPointer || Ty != IAClass.AccessType) |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3728 | continue; |
| 3729 | |
Johannes Doerfert | df88023 | 2016-03-03 12:26:58 +0000 | [diff] [blame] | 3730 | // If the pointer and the type is equal check if the access function wrt. |
| 3731 | // to the domain is equal too. It can happen that the domain fixes |
| 3732 | // parameter values and these can be different for distinct part of the |
Johannes Doerfert | ac37c56 | 2016-03-03 12:30:19 +0000 | [diff] [blame] | 3733 | // SCoP. If this happens we cannot consolidate the loads but need to |
Johannes Doerfert | df88023 | 2016-03-03 12:26:58 +0000 | [diff] [blame] | 3734 | // create a new invariant load equivalence class. |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 3735 | auto &MAs = IAClass.InvariantAccesses; |
Johannes Doerfert | df88023 | 2016-03-03 12:26:58 +0000 | [diff] [blame] | 3736 | if (!MAs.empty()) { |
| 3737 | auto *LastMA = MAs.front(); |
| 3738 | |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3739 | isl::set AR = MA->getAccessRelation().range(); |
| 3740 | isl::set LastAR = LastMA->getAccessRelation().range(); |
| 3741 | bool SameAR = AR.is_equal(LastAR); |
Johannes Doerfert | df88023 | 2016-03-03 12:26:58 +0000 | [diff] [blame] | 3742 | |
| 3743 | if (!SameAR) |
| 3744 | continue; |
| 3745 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3746 | |
| 3747 | // Add MA to the list of accesses that are in this class. |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3748 | MAs.push_front(MA); |
| 3749 | |
Johannes Doerfert | df88023 | 2016-03-03 12:26:58 +0000 | [diff] [blame] | 3750 | Consolidated = true; |
| 3751 | |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3752 | // Unify the execution context of the class and this statement. |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3753 | isl::set IAClassDomainCtx = IAClass.ExecutionContext; |
Johannes Doerfert | fc4bfc4 | 2015-11-11 04:30:07 +0000 | [diff] [blame] | 3754 | if (IAClassDomainCtx) |
Tobias Grosser | e69b272 | 2017-08-06 23:50:25 +0000 | [diff] [blame] | 3755 | IAClassDomainCtx = IAClassDomainCtx.unite(MACtx).coalesce(); |
Johannes Doerfert | fc4bfc4 | 2015-11-11 04:30:07 +0000 | [diff] [blame] | 3756 | else |
Johannes Doerfert | 1dc12af | 2016-04-23 12:59:18 +0000 | [diff] [blame] | 3757 | IAClassDomainCtx = MACtx; |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3758 | IAClass.ExecutionContext = IAClassDomainCtx; |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3759 | break; |
| 3760 | } |
| 3761 | |
| 3762 | if (Consolidated) |
| 3763 | continue; |
| 3764 | |
| 3765 | // If we did not consolidate MA, thus did not find an equivalence class |
| 3766 | // for it, we create a new one. |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3767 | InvariantEquivClasses.emplace_back( |
| 3768 | InvariantEquivClassTy{PointerSCEV, MemoryAccessList{MA}, MACtx, Ty}); |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3769 | } |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 3770 | } |
| 3771 | |
Tobias Grosser | 1eeedf4 | 2017-07-20 19:55:19 +0000 | [diff] [blame] | 3772 | /// Check if an access range is too complex. |
| 3773 | /// |
| 3774 | /// An access range is too complex, if it contains either many disjuncts or |
| 3775 | /// very complex expressions. As a simple heuristic, we assume if a set to |
| 3776 | /// be too complex if the sum of existentially quantified dimensions and |
| 3777 | /// set dimensions is larger than a threshold. This reliably detects both |
| 3778 | /// sets with many disjuncts as well as sets with many divisions as they |
| 3779 | /// arise in h264. |
| 3780 | /// |
| 3781 | /// @param AccessRange The range to check for complexity. |
| 3782 | /// |
| 3783 | /// @returns True if the access range is too complex. |
| 3784 | static bool isAccessRangeTooComplex(isl::set AccessRange) { |
| 3785 | unsigned NumTotalDims = 0; |
| 3786 | |
| 3787 | auto CountDimensions = [&NumTotalDims](isl::basic_set BSet) -> isl::stat { |
| 3788 | NumTotalDims += BSet.dim(isl::dim::div); |
| 3789 | NumTotalDims += BSet.dim(isl::dim::set); |
| 3790 | return isl::stat::ok; |
| 3791 | }; |
| 3792 | |
| 3793 | AccessRange.foreach_basic_set(CountDimensions); |
| 3794 | |
| 3795 | if (NumTotalDims > MaxDimensionsInAccessRange) |
| 3796 | return true; |
| 3797 | |
| 3798 | return false; |
| 3799 | } |
| 3800 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3801 | isl::set Scop::getNonHoistableCtx(MemoryAccess *Access, isl::union_map Writes) { |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 3802 | // TODO: Loads that are not loop carried, hence are in a statement with |
| 3803 | // zero iterators, are by construction invariant, though we |
| 3804 | // currently "hoist" them anyway. This is necessary because we allow |
| 3805 | // them to be treated as parameters (e.g., in conditions) and our code |
| 3806 | // generation would otherwise use the old value. |
| 3807 | |
| 3808 | auto &Stmt = *Access->getStatement(); |
Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 3809 | BasicBlock *BB = Stmt.getEntryBlock(); |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 3810 | |
Johannes Doerfert | c976546 | 2016-11-17 22:11:56 +0000 | [diff] [blame] | 3811 | if (Access->isScalarKind() || Access->isWrite() || !Access->isAffine() || |
| 3812 | Access->isMemoryIntrinsic()) |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3813 | return nullptr; |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 3814 | |
| 3815 | // Skip accesses that have an invariant base pointer which is defined but |
| 3816 | // not loaded inside the SCoP. This can happened e.g., if a readnone call |
| 3817 | // returns a pointer that is used as a base address. However, as we want |
| 3818 | // to hoist indirect pointers, we allow the base pointer to be defined in |
| 3819 | // the region if it is also a memory access. Each ScopArrayInfo object |
| 3820 | // that has a base pointer origin has a base pointer that is loaded and |
| 3821 | // that it is invariant, thus it will be hoisted too. However, if there is |
| 3822 | // no base pointer origin we check that the base pointer is defined |
| 3823 | // outside the region. |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3824 | auto *LI = cast<LoadInst>(Access->getAccessInstruction()); |
Johannes Doerfert | 764b7e6 | 2016-05-23 09:26:46 +0000 | [diff] [blame] | 3825 | if (hasNonHoistableBasePtrInScop(Access, Writes)) |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3826 | return nullptr; |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 3827 | |
Tobias Grosser | d3d3d6b | 2018-04-29 00:28:26 +0000 | [diff] [blame] | 3828 | isl::map AccessRelation = Access->getAccessRelation(); |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3829 | assert(!AccessRelation.is_empty()); |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 3830 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3831 | if (AccessRelation.involves_dims(isl::dim::in, 0, Stmt.getNumIterators())) |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3832 | return nullptr; |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 3833 | |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 3834 | AccessRelation = AccessRelation.intersect_domain(Stmt.getDomain()); |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3835 | isl::set SafeToLoad; |
Tobias Grosser | c96c1d8 | 2017-04-27 20:08:16 +0000 | [diff] [blame] | 3836 | |
| 3837 | auto &DL = getFunction().getParent()->getDataLayout(); |
| 3838 | if (isSafeToLoadUnconditionally(LI->getPointerOperand(), LI->getAlignment(), |
| 3839 | DL)) { |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3840 | SafeToLoad = isl::set::universe(AccessRelation.get_space().range()); |
Tobias Grosser | c96c1d8 | 2017-04-27 20:08:16 +0000 | [diff] [blame] | 3841 | } else if (BB != LI->getParent()) { |
| 3842 | // Skip accesses in non-affine subregions as they might not be executed |
| 3843 | // under the same condition as the entry of the non-affine subregion. |
Tobias Grosser | c96c1d8 | 2017-04-27 20:08:16 +0000 | [diff] [blame] | 3844 | return nullptr; |
| 3845 | } else { |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3846 | SafeToLoad = AccessRelation.range(); |
Tobias Grosser | c96c1d8 | 2017-04-27 20:08:16 +0000 | [diff] [blame] | 3847 | } |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 3848 | |
Tobias Grosser | 1eeedf4 | 2017-07-20 19:55:19 +0000 | [diff] [blame] | 3849 | if (isAccessRangeTooComplex(AccessRelation.range())) |
| 3850 | return nullptr; |
| 3851 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3852 | isl::union_map Written = Writes.intersect_range(SafeToLoad); |
| 3853 | isl::set WrittenCtx = Written.params(); |
| 3854 | bool IsWritten = !WrittenCtx.is_empty(); |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 3855 | |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3856 | if (!IsWritten) |
| 3857 | return WrittenCtx; |
| 3858 | |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3859 | WrittenCtx = WrittenCtx.remove_divs(); |
Philip Pfaffe | 9375d57 | 2018-05-16 14:05:03 +0000 | [diff] [blame] | 3860 | bool TooComplex = WrittenCtx.n_basic_set() >= MaxDisjunctsInDomain; |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3861 | if (TooComplex || !isRequiredInvariantLoad(LI)) |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3862 | return nullptr; |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3863 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 3864 | addAssumption(INVARIANTLOAD, WrittenCtx, LI->getDebugLoc(), AS_RESTRICTION, |
| 3865 | LI->getParent()); |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3866 | return WrittenCtx; |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 3867 | } |
| 3868 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 3869 | void Scop::verifyInvariantLoads() { |
| 3870 | auto &RIL = getRequiredInvariantLoads(); |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 3871 | for (LoadInst *LI : RIL) { |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 3872 | assert(LI && contains(LI)); |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 3873 | // If there exists a statement in the scop which has a memory access for |
| 3874 | // @p LI, then mark this scop as infeasible for optimization. |
| 3875 | for (ScopStmt &Stmt : Stmts) |
| 3876 | if (Stmt.getArrayAccessOrNULLFor(LI)) { |
| 3877 | invalidate(INVARIANTLOAD, LI->getDebugLoc(), LI->getParent()); |
| 3878 | return; |
| 3879 | } |
Tobias Grosser | 29f38ab | 2015-12-13 21:00:40 +0000 | [diff] [blame] | 3880 | } |
| 3881 | } |
| 3882 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 3883 | void Scop::hoistInvariantLoads() { |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 3884 | if (!PollyInvariantLoadHoisting) |
| 3885 | return; |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 3886 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 3887 | isl::union_map Writes = getWrites(); |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 3888 | for (ScopStmt &Stmt : *this) { |
Johannes Doerfert | 25227fe | 2016-05-23 10:40:54 +0000 | [diff] [blame] | 3889 | InvariantAccessesTy InvariantAccesses; |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 3890 | |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 3891 | for (MemoryAccess *Access : Stmt) |
Tobias Grosser | 4071cb5 | 2017-06-06 23:13:02 +0000 | [diff] [blame] | 3892 | if (isl::set NHCtx = getNonHoistableCtx(Access, Writes)) |
Tobias Grosser | d16f927 | 2017-08-06 17:25:14 +0000 | [diff] [blame] | 3893 | InvariantAccesses.push_back({Access, NHCtx}); |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 3894 | |
| 3895 | // Transfer the memory access from the statement to the SCoP. |
Michael Kruse | 1007182 | 2016-05-23 14:45:58 +0000 | [diff] [blame] | 3896 | for (auto InvMA : InvariantAccesses) |
| 3897 | Stmt.removeMemoryAccess(InvMA.MA); |
Tobias Grosser | 0865e775 | 2016-02-29 07:29:42 +0000 | [diff] [blame] | 3898 | addInvariantLoads(Stmt, InvariantAccesses); |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 3899 | } |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 3900 | } |
| 3901 | |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 3902 | /// Find the canonical scop array info object for a set of invariant load |
| 3903 | /// hoisted loads. The canonical array is the one that corresponds to the |
| 3904 | /// first load in the list of accesses which is used as base pointer of a |
| 3905 | /// scop array. |
| 3906 | static const ScopArrayInfo *findCanonicalArray(Scop *S, |
| 3907 | MemoryAccessList &Accesses) { |
| 3908 | for (MemoryAccess *Access : Accesses) { |
| 3909 | const ScopArrayInfo *CanonicalArray = S->getScopArrayInfoOrNull( |
| 3910 | Access->getAccessInstruction(), MemoryKind::Array); |
| 3911 | if (CanonicalArray) |
| 3912 | return CanonicalArray; |
| 3913 | } |
| 3914 | return nullptr; |
| 3915 | } |
| 3916 | |
| 3917 | /// Check if @p Array severs as base array in an invariant load. |
| 3918 | static bool isUsedForIndirectHoistedLoad(Scop *S, const ScopArrayInfo *Array) { |
| 3919 | for (InvariantEquivClassTy &EqClass2 : S->getInvariantAccesses()) |
| 3920 | for (MemoryAccess *Access2 : EqClass2.InvariantAccesses) |
| 3921 | if (Access2->getScopArrayInfo() == Array) |
| 3922 | return true; |
| 3923 | return false; |
| 3924 | } |
| 3925 | |
| 3926 | /// Replace the base pointer arrays in all memory accesses referencing @p Old, |
| 3927 | /// with a reference to @p New. |
| 3928 | static void replaceBasePtrArrays(Scop *S, const ScopArrayInfo *Old, |
| 3929 | const ScopArrayInfo *New) { |
| 3930 | for (ScopStmt &Stmt : *S) |
| 3931 | for (MemoryAccess *Access : Stmt) { |
| 3932 | if (Access->getLatestScopArrayInfo() != Old) |
| 3933 | continue; |
| 3934 | |
Tobias Grosser | 6d58804 | 2017-08-02 19:27:16 +0000 | [diff] [blame] | 3935 | isl::id Id = New->getBasePtrId(); |
| 3936 | isl::map Map = Access->getAccessRelation(); |
| 3937 | Map = Map.set_tuple_id(isl::dim::out, Id); |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 3938 | Access->setAccessRelation(Map); |
| 3939 | } |
| 3940 | } |
| 3941 | |
| 3942 | void Scop::canonicalizeDynamicBasePtrs() { |
| 3943 | for (InvariantEquivClassTy &EqClass : InvariantEquivClasses) { |
| 3944 | MemoryAccessList &BasePtrAccesses = EqClass.InvariantAccesses; |
| 3945 | |
| 3946 | const ScopArrayInfo *CanonicalBasePtrSAI = |
| 3947 | findCanonicalArray(this, BasePtrAccesses); |
| 3948 | |
| 3949 | if (!CanonicalBasePtrSAI) |
| 3950 | continue; |
| 3951 | |
| 3952 | for (MemoryAccess *BasePtrAccess : BasePtrAccesses) { |
| 3953 | const ScopArrayInfo *BasePtrSAI = getScopArrayInfoOrNull( |
| 3954 | BasePtrAccess->getAccessInstruction(), MemoryKind::Array); |
| 3955 | if (!BasePtrSAI || BasePtrSAI == CanonicalBasePtrSAI || |
| 3956 | !BasePtrSAI->isCompatibleWith(CanonicalBasePtrSAI)) |
| 3957 | continue; |
| 3958 | |
| 3959 | // we currently do not canonicalize arrays where some accesses are |
| 3960 | // hoisted as invariant loads. If we would, we need to update the access |
| 3961 | // function of the invariant loads as well. However, as this is not a |
| 3962 | // very common situation, we leave this for now to avoid further |
| 3963 | // complexity increases. |
| 3964 | if (isUsedForIndirectHoistedLoad(this, BasePtrSAI)) |
| 3965 | continue; |
| 3966 | |
| 3967 | replaceBasePtrArrays(this, BasePtrSAI, CanonicalBasePtrSAI); |
| 3968 | } |
| 3969 | } |
| 3970 | } |
| 3971 | |
Michael Kruse | b738ffa | 2017-06-28 13:02:43 +0000 | [diff] [blame] | 3972 | ScopArrayInfo *Scop::getOrCreateScopArrayInfo(Value *BasePtr, Type *ElementType, |
| 3973 | ArrayRef<const SCEV *> Sizes, |
| 3974 | MemoryKind Kind, |
| 3975 | const char *BaseName) { |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 3976 | assert((BasePtr || BaseName) && |
| 3977 | "BasePtr and BaseName can not be nullptr at the same time."); |
| 3978 | assert(!(BasePtr && BaseName) && "BaseName is redundant."); |
| 3979 | auto &SAI = BasePtr ? ScopArrayInfoMap[std::make_pair(BasePtr, Kind)] |
| 3980 | : ScopArrayNameMap[BaseName]; |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 3981 | if (!SAI) { |
Johannes Doerfert | 3f52e35 | 2016-05-23 12:38:05 +0000 | [diff] [blame] | 3982 | auto &DL = getFunction().getParent()->getDataLayout(); |
Tobias Grosser | cc77950 | 2016-02-02 13:22:54 +0000 | [diff] [blame] | 3983 | SAI.reset(new ScopArrayInfo(BasePtr, ElementType, getIslCtx(), Sizes, Kind, |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 3984 | DL, this, BaseName)); |
| 3985 | ScopArrayInfoSet.insert(SAI.get()); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 3986 | } else { |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 3987 | SAI->updateElementType(ElementType); |
Tobias Grosser | 8286b83 | 2015-11-02 11:29:32 +0000 | [diff] [blame] | 3988 | // In case of mismatching array sizes, we bail out by setting the run-time |
| 3989 | // context to false. |
Johannes Doerfert | 3ff2221 | 2016-02-14 22:31:39 +0000 | [diff] [blame] | 3990 | if (!SAI->updateSizes(Sizes)) |
Tobias Grosser | 8d4f626 | 2015-12-12 09:52:26 +0000 | [diff] [blame] | 3991 | invalidate(DELINEARIZATION, DebugLoc()); |
Tobias Grosser | 99c70dd | 2015-09-26 08:55:54 +0000 | [diff] [blame] | 3992 | } |
Tobias Grosser | ab67144 | 2015-05-23 05:58:27 +0000 | [diff] [blame] | 3993 | return SAI.get(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 3994 | } |
| 3995 | |
Michael Kruse | b738ffa | 2017-06-28 13:02:43 +0000 | [diff] [blame] | 3996 | ScopArrayInfo *Scop::createScopArrayInfo(Type *ElementType, |
| 3997 | const std::string &BaseName, |
| 3998 | const std::vector<unsigned> &Sizes) { |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 3999 | auto *DimSizeType = Type::getInt64Ty(getSE()->getContext()); |
| 4000 | std::vector<const SCEV *> SCEVSizes; |
| 4001 | |
| 4002 | for (auto size : Sizes) |
Roman Gareev | f5aff70 | 2016-09-12 17:08:31 +0000 | [diff] [blame] | 4003 | if (size) |
| 4004 | SCEVSizes.push_back(getSE()->getConstant(DimSizeType, size, false)); |
| 4005 | else |
| 4006 | SCEVSizes.push_back(nullptr); |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4007 | |
Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 4008 | auto *SAI = getOrCreateScopArrayInfo(nullptr, ElementType, SCEVSizes, |
| 4009 | MemoryKind::Array, BaseName.c_str()); |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4010 | return SAI; |
| 4011 | } |
| 4012 | |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 4013 | const ScopArrayInfo *Scop::getScopArrayInfoOrNull(Value *BasePtr, |
| 4014 | MemoryKind Kind) { |
Tobias Grosser | 6abc75a | 2015-11-10 17:31:31 +0000 | [diff] [blame] | 4015 | auto *SAI = ScopArrayInfoMap[std::make_pair(BasePtr, Kind)].get(); |
Tobias Grosser | f3adab4 | 2017-05-10 10:59:58 +0000 | [diff] [blame] | 4016 | return SAI; |
| 4017 | } |
| 4018 | |
| 4019 | const ScopArrayInfo *Scop::getScopArrayInfo(Value *BasePtr, MemoryKind Kind) { |
| 4020 | auto *SAI = getScopArrayInfoOrNull(BasePtr, Kind); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 4021 | assert(SAI && "No ScopArrayInfo available for this base pointer"); |
| 4022 | return SAI; |
| 4023 | } |
| 4024 | |
Tobias Grosser | 8ea1fc1 | 2017-08-06 19:52:38 +0000 | [diff] [blame] | 4025 | std::string Scop::getContextStr() const { return getContext().to_str(); } |
Johannes Doerfert | b92e218 | 2016-02-21 16:37:58 +0000 | [diff] [blame] | 4026 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4027 | std::string Scop::getAssumedContextStr() const { |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4028 | assert(AssumedContext && "Assumed context not yet built"); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4029 | return AssumedContext.to_str(); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4030 | } |
Johannes Doerfert | b92e218 | 2016-02-21 16:37:58 +0000 | [diff] [blame] | 4031 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4032 | std::string Scop::getInvalidContextStr() const { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4033 | return InvalidContext.to_str(); |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 4034 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4035 | |
| 4036 | std::string Scop::getNameStr() const { |
| 4037 | std::string ExitName, EntryName; |
Siddharth Bhat | 07bee29 | 2017-06-02 08:01:22 +0000 | [diff] [blame] | 4038 | std::tie(EntryName, ExitName) = getEntryExitStr(); |
| 4039 | return EntryName + "---" + ExitName; |
| 4040 | } |
| 4041 | |
| 4042 | std::pair<std::string, std::string> Scop::getEntryExitStr() const { |
| 4043 | std::string ExitName, EntryName; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4044 | raw_string_ostream ExitStr(ExitName); |
| 4045 | raw_string_ostream EntryStr(EntryName); |
| 4046 | |
Tobias Grosser | f240b48 | 2014-01-09 10:42:15 +0000 | [diff] [blame] | 4047 | R.getEntry()->printAsOperand(EntryStr, false); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4048 | EntryStr.str(); |
| 4049 | |
| 4050 | if (R.getExit()) { |
Tobias Grosser | f240b48 | 2014-01-09 10:42:15 +0000 | [diff] [blame] | 4051 | R.getExit()->printAsOperand(ExitStr, false); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4052 | ExitStr.str(); |
| 4053 | } else |
| 4054 | ExitName = "FunctionExit"; |
| 4055 | |
Siddharth Bhat | 07bee29 | 2017-06-02 08:01:22 +0000 | [diff] [blame] | 4056 | return std::make_pair(EntryName, ExitName); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4057 | } |
| 4058 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4059 | isl::set Scop::getContext() const { return Context; } |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 4060 | isl::space Scop::getParamSpace() const { return getContext().get_space(); } |
Tobias Grosser | 3748705 | 2011-10-06 00:03:42 +0000 | [diff] [blame] | 4061 | |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 4062 | isl::space Scop::getFullParamSpace() const { |
| 4063 | std::vector<isl::id> FortranIDs; |
| 4064 | FortranIDs = getFortranArrayIds(arrays()); |
| 4065 | |
| 4066 | isl::space Space = isl::space::params_alloc( |
| 4067 | getIslCtx(), ParameterIds.size() + FortranIDs.size()); |
| 4068 | |
| 4069 | unsigned PDim = 0; |
| 4070 | for (const SCEV *Parameter : Parameters) { |
Tobias Grosser | 9a63570 | 2017-08-06 19:31:27 +0000 | [diff] [blame] | 4071 | isl::id Id = getIdForParam(Parameter); |
Tobias Grosser | b5563c6 | 2017-08-03 13:51:15 +0000 | [diff] [blame] | 4072 | Space = Space.set_dim_id(isl::dim::param, PDim++, Id); |
| 4073 | } |
| 4074 | |
| 4075 | for (isl::id Id : FortranIDs) |
| 4076 | Space = Space.set_dim_id(isl::dim::param, PDim++, Id); |
| 4077 | |
| 4078 | return Space; |
| 4079 | } |
| 4080 | |
Tobias Grosser | e127033 | 2017-08-06 21:42:09 +0000 | [diff] [blame] | 4081 | isl::set Scop::getAssumedContext() const { |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4082 | assert(AssumedContext && "Assumed context not yet built"); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4083 | return AssumedContext; |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 4084 | } |
| 4085 | |
Michael Kruse | f3091bf | 2017-03-17 13:09:52 +0000 | [diff] [blame] | 4086 | bool Scop::isProfitable(bool ScalarsAreUnprofitable) const { |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4087 | if (PollyProcessUnprofitable) |
| 4088 | return true; |
| 4089 | |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4090 | if (isEmpty()) |
| 4091 | return false; |
| 4092 | |
| 4093 | unsigned OptimizableStmtsOrLoops = 0; |
| 4094 | for (auto &Stmt : *this) { |
| 4095 | if (Stmt.getNumIterators() == 0) |
| 4096 | continue; |
| 4097 | |
| 4098 | bool ContainsArrayAccs = false; |
| 4099 | bool ContainsScalarAccs = false; |
| 4100 | for (auto *MA : Stmt) { |
| 4101 | if (MA->isRead()) |
| 4102 | continue; |
Michael Kruse | f3091bf | 2017-03-17 13:09:52 +0000 | [diff] [blame] | 4103 | ContainsArrayAccs |= MA->isLatestArrayKind(); |
| 4104 | ContainsScalarAccs |= MA->isLatestScalarKind(); |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4105 | } |
| 4106 | |
Michael Kruse | f3091bf | 2017-03-17 13:09:52 +0000 | [diff] [blame] | 4107 | if (!ScalarsAreUnprofitable || (ContainsArrayAccs && !ContainsScalarAccs)) |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4108 | OptimizableStmtsOrLoops += Stmt.getNumIterators(); |
| 4109 | } |
| 4110 | |
| 4111 | return OptimizableStmtsOrLoops > 1; |
| 4112 | } |
| 4113 | |
Johannes Doerfert | 5d5b306 | 2015-08-20 18:06:30 +0000 | [diff] [blame] | 4114 | bool Scop::hasFeasibleRuntimeContext() const { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4115 | auto PositiveContext = getAssumedContext(); |
| 4116 | auto NegativeContext = getInvalidContext(); |
| 4117 | PositiveContext = addNonEmptyDomainConstraints(PositiveContext); |
| 4118 | // addNonEmptyDomainConstraints returns null if ScopStmts have a null domain |
| 4119 | if (!PositiveContext) |
Johannes Doerfert | 94341c9 | 2016-04-23 13:00:27 +0000 | [diff] [blame] | 4120 | return false; |
Johannes Doerfert | 94341c9 | 2016-04-23 13:00:27 +0000 | [diff] [blame] | 4121 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4122 | bool IsFeasible = !(PositiveContext.is_empty() || |
| 4123 | PositiveContext.is_subset(NegativeContext)); |
| 4124 | if (!IsFeasible) |
| 4125 | return false; |
| 4126 | |
| 4127 | auto DomainContext = getDomains().params(); |
| 4128 | IsFeasible = !DomainContext.is_subset(NegativeContext); |
| 4129 | IsFeasible &= !Context.is_subset(NegativeContext); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4130 | |
Johannes Doerfert | 43788c5 | 2015-08-20 05:58:56 +0000 | [diff] [blame] | 4131 | return IsFeasible; |
| 4132 | } |
| 4133 | |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 4134 | static std::string toString(AssumptionKind Kind) { |
| 4135 | switch (Kind) { |
| 4136 | case ALIASING: |
| 4137 | return "No-aliasing"; |
| 4138 | case INBOUNDS: |
| 4139 | return "Inbounds"; |
| 4140 | case WRAPPING: |
| 4141 | return "No-overflows"; |
Johannes Doerfert | c359628 | 2016-04-25 14:01:36 +0000 | [diff] [blame] | 4142 | case UNSIGNED: |
| 4143 | return "Signed-unsigned"; |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 4144 | case COMPLEXITY: |
| 4145 | return "Low complexity"; |
Johannes Doerfert | 27d12d3 | 2016-05-10 16:38:09 +0000 | [diff] [blame] | 4146 | case PROFITABLE: |
| 4147 | return "Profitable"; |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 4148 | case ERRORBLOCK: |
| 4149 | return "No-error"; |
| 4150 | case INFINITELOOP: |
| 4151 | return "Finite loop"; |
| 4152 | case INVARIANTLOAD: |
| 4153 | return "Invariant load"; |
| 4154 | case DELINEARIZATION: |
| 4155 | return "Delinearization"; |
| 4156 | } |
| 4157 | llvm_unreachable("Unknown AssumptionKind!"); |
| 4158 | } |
| 4159 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4160 | bool Scop::isEffectiveAssumption(isl::set Set, AssumptionSign Sign) { |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 4161 | if (Sign == AS_ASSUMPTION) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4162 | if (Context.is_subset(Set)) |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 4163 | return false; |
| 4164 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4165 | if (AssumedContext.is_subset(Set)) |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 4166 | return false; |
| 4167 | } else { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4168 | if (Set.is_disjoint(Context)) |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 4169 | return false; |
| 4170 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4171 | if (Set.is_subset(InvalidContext)) |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 4172 | return false; |
| 4173 | } |
| 4174 | return true; |
| 4175 | } |
| 4176 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4177 | bool Scop::trackAssumption(AssumptionKind Kind, isl::set Set, DebugLoc Loc, |
| 4178 | AssumptionSign Sign, BasicBlock *BB) { |
Johannes Doerfert | 1a6b0f7 | 2016-06-06 12:16:10 +0000 | [diff] [blame] | 4179 | if (PollyRemarksMinimal && !isEffectiveAssumption(Set, Sign)) |
| 4180 | return false; |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 4181 | |
Johannes Doerfert | b3265a3 | 2016-11-17 22:08:40 +0000 | [diff] [blame] | 4182 | // Do never emit trivial assumptions as they only clutter the output. |
| 4183 | if (!PollyRemarksMinimal) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4184 | isl::set Univ; |
Johannes Doerfert | b3265a3 | 2016-11-17 22:08:40 +0000 | [diff] [blame] | 4185 | if (Sign == AS_ASSUMPTION) |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4186 | Univ = isl::set::universe(Set.get_space()); |
Johannes Doerfert | b3265a3 | 2016-11-17 22:08:40 +0000 | [diff] [blame] | 4187 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4188 | bool IsTrivial = (Sign == AS_RESTRICTION && Set.is_empty()) || |
| 4189 | (Sign == AS_ASSUMPTION && Univ.is_equal(Set)); |
Johannes Doerfert | b3265a3 | 2016-11-17 22:08:40 +0000 | [diff] [blame] | 4190 | |
| 4191 | if (IsTrivial) |
| 4192 | return false; |
| 4193 | } |
| 4194 | |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4195 | switch (Kind) { |
| 4196 | case ALIASING: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4197 | AssumptionsAliasing++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4198 | break; |
| 4199 | case INBOUNDS: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4200 | AssumptionsInbounds++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4201 | break; |
| 4202 | case WRAPPING: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4203 | AssumptionsWrapping++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4204 | break; |
| 4205 | case UNSIGNED: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4206 | AssumptionsUnsigned++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4207 | break; |
| 4208 | case COMPLEXITY: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4209 | AssumptionsComplexity++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4210 | break; |
| 4211 | case PROFITABLE: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4212 | AssumptionsUnprofitable++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4213 | break; |
| 4214 | case ERRORBLOCK: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4215 | AssumptionsErrorBlock++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4216 | break; |
| 4217 | case INFINITELOOP: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4218 | AssumptionsInfiniteLoop++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4219 | break; |
| 4220 | case INVARIANTLOAD: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4221 | AssumptionsInvariantLoad++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4222 | break; |
| 4223 | case DELINEARIZATION: |
Johannes Doerfert | 81aa6e8 | 2016-11-18 14:37:08 +0000 | [diff] [blame] | 4224 | AssumptionsDelinearization++; |
Johannes Doerfert | cd19532 | 2016-11-17 21:41:08 +0000 | [diff] [blame] | 4225 | break; |
| 4226 | } |
| 4227 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4228 | auto Suffix = Sign == AS_ASSUMPTION ? " assumption:\t" : " restriction:\t"; |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4229 | std::string Msg = toString(Kind) + Suffix + Set.to_str(); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4230 | if (BB) |
| 4231 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "AssumpRestrict", Loc, BB) |
| 4232 | << Msg); |
| 4233 | else |
| 4234 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "AssumpRestrict", Loc, |
| 4235 | R.getEntry()) |
| 4236 | << Msg); |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4237 | return true; |
Johannes Doerfert | d84493e | 2015-11-12 02:33:38 +0000 | [diff] [blame] | 4238 | } |
| 4239 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4240 | void Scop::addAssumption(AssumptionKind Kind, isl::set Set, DebugLoc Loc, |
| 4241 | AssumptionSign Sign, BasicBlock *BB) { |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 4242 | // Simplify the assumptions/restrictions first. |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4243 | Set = Set.gist_params(getContext()); |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 4244 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4245 | if (!trackAssumption(Kind, Set, Loc, Sign, BB)) |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4246 | return; |
Tobias Grosser | 20a4c0c | 2015-11-11 16:22:36 +0000 | [diff] [blame] | 4247 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4248 | if (Sign == AS_ASSUMPTION) |
| 4249 | AssumedContext = AssumedContext.intersect(Set).coalesce(); |
| 4250 | else |
| 4251 | InvalidContext = InvalidContext.unite(Set).coalesce(); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4252 | } |
| 4253 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4254 | void Scop::recordAssumption(AssumptionKind Kind, isl::set Set, DebugLoc Loc, |
| 4255 | AssumptionSign Sign, BasicBlock *BB) { |
| 4256 | assert((Set.is_params() || BB) && |
Tobias Grosser | f67433a | 2016-11-10 11:44:10 +0000 | [diff] [blame] | 4257 | "Assumptions without a basic block must be parameter sets"); |
Johannes Doerfert | 615e0b8 | 2016-04-12 13:28:39 +0000 | [diff] [blame] | 4258 | RecordedAssumptions.push_back({Kind, Sign, Set, Loc, BB}); |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 4259 | } |
| 4260 | |
| 4261 | void Scop::addRecordedAssumptions() { |
| 4262 | while (!RecordedAssumptions.empty()) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4263 | Assumption AS = RecordedAssumptions.pop_back_val(); |
Johannes Doerfert | 615e0b8 | 2016-04-12 13:28:39 +0000 | [diff] [blame] | 4264 | |
Johannes Doerfert | 8475d1c | 2016-04-28 14:32:58 +0000 | [diff] [blame] | 4265 | if (!AS.BB) { |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4266 | addAssumption(AS.Kind, AS.Set, AS.Loc, AS.Sign, nullptr /* BasicBlock */); |
Johannes Doerfert | 8475d1c | 2016-04-28 14:32:58 +0000 | [diff] [blame] | 4267 | continue; |
| 4268 | } |
Johannes Doerfert | 615e0b8 | 2016-04-12 13:28:39 +0000 | [diff] [blame] | 4269 | |
Johannes Doerfert | 14b1cf3 | 2016-05-10 12:42:26 +0000 | [diff] [blame] | 4270 | // If the domain was deleted the assumptions are void. |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 4271 | isl_set *Dom = getDomainConditions(AS.BB).release(); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4272 | if (!Dom) |
Johannes Doerfert | 14b1cf3 | 2016-05-10 12:42:26 +0000 | [diff] [blame] | 4273 | continue; |
Johannes Doerfert | 14b1cf3 | 2016-05-10 12:42:26 +0000 | [diff] [blame] | 4274 | |
Johannes Doerfert | 8475d1c | 2016-04-28 14:32:58 +0000 | [diff] [blame] | 4275 | // If a basic block was given use its domain to simplify the assumption. |
| 4276 | // In case of restrictions we know they only have to hold on the domain, |
| 4277 | // thus we can intersect them with the domain of the block. However, for |
| 4278 | // assumptions the domain has to imply them, thus: |
| 4279 | // _ _____ |
| 4280 | // Dom => S <==> A v B <==> A - B |
| 4281 | // |
Tobias Grosser | cdbe5c9 | 2017-01-06 17:30:34 +0000 | [diff] [blame] | 4282 | // To avoid the complement we will register A - B as a restriction not an |
Johannes Doerfert | 8475d1c | 2016-04-28 14:32:58 +0000 | [diff] [blame] | 4283 | // assumption. |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4284 | isl_set *S = AS.Set.copy(); |
Johannes Doerfert | 8475d1c | 2016-04-28 14:32:58 +0000 | [diff] [blame] | 4285 | if (AS.Sign == AS_RESTRICTION) |
| 4286 | S = isl_set_params(isl_set_intersect(S, Dom)); |
| 4287 | else /* (AS.Sign == AS_ASSUMPTION) */ |
| 4288 | S = isl_set_params(isl_set_subtract(Dom, S)); |
| 4289 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4290 | addAssumption(AS.Kind, isl::manage(S), AS.Loc, AS_RESTRICTION, AS.BB); |
Johannes Doerfert | 3bf6e412 | 2016-04-12 13:27:35 +0000 | [diff] [blame] | 4291 | } |
| 4292 | } |
| 4293 | |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4294 | void Scop::invalidate(AssumptionKind Kind, DebugLoc Loc, BasicBlock *BB) { |
Nicola Zaghen | 349506a | 2018-05-15 13:37:17 +0000 | [diff] [blame] | 4295 | LLVM_DEBUG(dbgs() << "Invalidate SCoP because of reason " << Kind << "\n"); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4296 | addAssumption(Kind, isl::set::empty(getParamSpace()), Loc, AS_ASSUMPTION, BB); |
Tobias Grosser | 8d4f626 | 2015-12-12 09:52:26 +0000 | [diff] [blame] | 4297 | } |
| 4298 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4299 | isl::set Scop::getInvalidContext() const { return InvalidContext; } |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 4300 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4301 | void Scop::printContext(raw_ostream &OS) const { |
| 4302 | OS << "Context:\n"; |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4303 | OS.indent(4) << Context << "\n"; |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 4304 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4305 | OS.indent(4) << "Assumed Context:\n"; |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4306 | OS.indent(4) << AssumedContext << "\n"; |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 4307 | |
Johannes Doerfert | 066dbf3 | 2016-03-01 13:06:28 +0000 | [diff] [blame] | 4308 | OS.indent(4) << "Invalid Context:\n"; |
| 4309 | OS.indent(4) << InvalidContext << "\n"; |
Johannes Doerfert | 883f8c1 | 2015-09-15 22:52:53 +0000 | [diff] [blame] | 4310 | |
Johannes Doerfert | 4e3bb7b | 2016-04-25 16:15:13 +0000 | [diff] [blame] | 4311 | unsigned Dim = 0; |
| 4312 | for (const SCEV *Parameter : Parameters) |
| 4313 | OS.indent(4) << "p" << Dim++ << ": " << *Parameter << "\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4314 | } |
| 4315 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 4316 | void Scop::printAliasAssumptions(raw_ostream &OS) const { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4317 | int noOfGroups = 0; |
| 4318 | for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) { |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4319 | if (Pair.second.size() == 0) |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4320 | noOfGroups += 1; |
| 4321 | else |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4322 | noOfGroups += Pair.second.size(); |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4323 | } |
| 4324 | |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4325 | OS.indent(4) << "Alias Groups (" << noOfGroups << "):\n"; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 4326 | if (MinMaxAliasGroups.empty()) { |
| 4327 | OS.indent(8) << "n/a\n"; |
| 4328 | return; |
| 4329 | } |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4330 | |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4331 | for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4332 | |
| 4333 | // If the group has no read only accesses print the write accesses. |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4334 | if (Pair.second.empty()) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4335 | OS.indent(8) << "[["; |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4336 | for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4337 | OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second |
| 4338 | << ">"; |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4339 | } |
| 4340 | OS << " ]]\n"; |
| 4341 | } |
| 4342 | |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4343 | for (const MinMaxAccessTy &MMAReadOnly : Pair.second) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4344 | OS.indent(8) << "[["; |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4345 | OS << " <" << MMAReadOnly.first << ", " << MMAReadOnly.second << ">"; |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 4346 | for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 4347 | OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second |
| 4348 | << ">"; |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 4349 | } |
| 4350 | OS << " ]]\n"; |
| 4351 | } |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 4352 | } |
| 4353 | } |
| 4354 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 4355 | void Scop::printStatements(raw_ostream &OS, bool PrintInstructions) const { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4356 | OS << "Statements {\n"; |
| 4357 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 4358 | for (const ScopStmt &Stmt : *this) { |
| 4359 | OS.indent(4); |
| 4360 | Stmt.print(OS, PrintInstructions); |
| 4361 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4362 | |
| 4363 | OS.indent(4) << "}\n"; |
| 4364 | } |
| 4365 | |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 4366 | void Scop::printArrayInfo(raw_ostream &OS) const { |
| 4367 | OS << "Arrays {\n"; |
| 4368 | |
Tobias Grosser | ab67144 | 2015-05-23 05:58:27 +0000 | [diff] [blame] | 4369 | for (auto &Array : arrays()) |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4370 | Array->print(OS); |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 4371 | |
| 4372 | OS.indent(4) << "}\n"; |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 4373 | |
| 4374 | OS.indent(4) << "Arrays (Bounds as pw_affs) {\n"; |
| 4375 | |
| 4376 | for (auto &Array : arrays()) |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4377 | Array->print(OS, /* SizeAsPwAff */ true); |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 4378 | |
| 4379 | OS.indent(4) << "}\n"; |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 4380 | } |
| 4381 | |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 4382 | void Scop::print(raw_ostream &OS, bool PrintInstructions) const { |
Johannes Doerfert | 3f52e35 | 2016-05-23 12:38:05 +0000 | [diff] [blame] | 4383 | OS.indent(4) << "Function: " << getFunction().getName() << "\n"; |
Tobias Grosser | 483fdd4 | 2014-03-18 18:05:38 +0000 | [diff] [blame] | 4384 | OS.indent(4) << "Region: " << getNameStr() << "\n"; |
David Peixotto | dc0a11c | 2015-01-13 18:31:55 +0000 | [diff] [blame] | 4385 | OS.indent(4) << "Max Loop Depth: " << getMaxLoopDepth() << "\n"; |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 4386 | OS.indent(4) << "Invariant Accesses: {\n"; |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 4387 | for (const auto &IAClass : InvariantEquivClasses) { |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 4388 | const auto &MAs = IAClass.InvariantAccesses; |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 4389 | if (MAs.empty()) { |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 4390 | OS.indent(12) << "Class Pointer: " << *IAClass.IdentifyingPointer << "\n"; |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 4391 | } else { |
Johannes Doerfert | af3e301 | 2015-10-18 12:39:19 +0000 | [diff] [blame] | 4392 | MAs.front()->print(OS); |
Tobias Grosser | 4e2d9c4 | 2016-07-11 12:15:10 +0000 | [diff] [blame] | 4393 | OS.indent(12) << "Execution Context: " << IAClass.ExecutionContext |
| 4394 | << "\n"; |
Johannes Doerfert | 697fdf8 | 2015-10-09 17:12:26 +0000 | [diff] [blame] | 4395 | } |
Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 4396 | } |
| 4397 | OS.indent(4) << "}\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4398 | printContext(OS.indent(4)); |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 4399 | printArrayInfo(OS.indent(4)); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 4400 | printAliasAssumptions(OS); |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 4401 | printStatements(OS.indent(4), PrintInstructions); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4402 | } |
| 4403 | |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 4404 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Michael Kruse | e186013 | 2017-07-21 15:54:13 +0000 | [diff] [blame] | 4405 | LLVM_DUMP_METHOD void Scop::dump() const { print(dbgs(), true); } |
Michael Kruse | 5d51846 | 2017-07-21 15:54:07 +0000 | [diff] [blame] | 4406 | #endif |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4407 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4408 | isl::ctx Scop::getIslCtx() const { return IslCtx.get(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4409 | |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 4410 | __isl_give PWACtx Scop::getPwAff(const SCEV *E, BasicBlock *BB, |
| 4411 | bool NonNegative) { |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 4412 | // First try to use the SCEVAffinator to generate a piecewise defined |
| 4413 | // affine function from @p E in the context of @p BB. If that tasks becomes to |
| 4414 | // complex the affinator might return a nullptr. In such a case we invalidate |
| 4415 | // 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] | 4416 | // handling code to all users of this function. |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 4417 | auto PWAC = Affinator.getPwAff(E, BB); |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 4418 | if (PWAC.first) { |
Johannes Doerfert | 56b3776 | 2016-05-10 11:45:46 +0000 | [diff] [blame] | 4419 | // TODO: We could use a heuristic and either use: |
| 4420 | // SCEVAffinator::takeNonNegativeAssumption |
| 4421 | // or |
| 4422 | // SCEVAffinator::interpretAsUnsigned |
| 4423 | // to deal with unsigned or "NonNegative" SCEVs. |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 4424 | if (NonNegative) |
| 4425 | Affinator.takeNonNegativeAssumption(PWAC); |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 4426 | return PWAC; |
Johannes Doerfert | 3e48ee2 | 2016-04-29 10:44:41 +0000 | [diff] [blame] | 4427 | } |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 4428 | |
| 4429 | auto DL = BB ? BB->getTerminator()->getDebugLoc() : DebugLoc(); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 4430 | invalidate(COMPLEXITY, DL, BB); |
Johannes Doerfert | 6462d8c | 2016-03-26 16:17:00 +0000 | [diff] [blame] | 4431 | return Affinator.getPwAff(SE->getZero(E->getType()), BB); |
Johannes Doerfert | 574182d | 2015-08-12 10:19:50 +0000 | [diff] [blame] | 4432 | } |
| 4433 | |
Tobias Grosser | 31df6f3 | 2017-08-06 21:42:25 +0000 | [diff] [blame] | 4434 | isl::union_set Scop::getDomains() const { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4435 | isl_space *EmptySpace = isl_space_params_alloc(getIslCtx().get(), 0); |
Tobias Grosser | 941cb7d | 2017-03-17 09:02:50 +0000 | [diff] [blame] | 4436 | isl_union_set *Domain = isl_union_set_empty(EmptySpace); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 4437 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4438 | for (const ScopStmt &Stmt : *this) |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 4439 | Domain = isl_union_set_add_set(Domain, Stmt.getDomain().release()); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 4440 | |
Tobias Grosser | 31df6f3 | 2017-08-06 21:42:25 +0000 | [diff] [blame] | 4441 | return isl::manage(Domain); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 4442 | } |
| 4443 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 4444 | isl::pw_aff Scop::getPwAffOnly(const SCEV *E, BasicBlock *BB) { |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 4445 | PWACtx PWAC = getPwAff(E, BB); |
Philip Pfaffe | d98dbee | 2017-12-06 21:02:22 +0000 | [diff] [blame] | 4446 | return PWAC.first; |
Johannes Doerfert | ac9c32e | 2016-04-23 14:31:17 +0000 | [diff] [blame] | 4447 | } |
| 4448 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4449 | isl::union_map |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4450 | Scop::getAccessesOfType(std::function<bool(MemoryAccess &)> Predicate) { |
Tobias Grosser | b65ccc4 | 2017-08-06 20:11:59 +0000 | [diff] [blame] | 4451 | isl::union_map Accesses = isl::union_map::empty(getParamSpace()); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4452 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 4453 | for (ScopStmt &Stmt : *this) { |
| 4454 | for (MemoryAccess *MA : Stmt) { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4455 | if (!Predicate(*MA)) |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4456 | continue; |
| 4457 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4458 | isl::set Domain = Stmt.getDomain(); |
| 4459 | isl::map AccessDomain = MA->getAccessRelation(); |
| 4460 | AccessDomain = AccessDomain.intersect_domain(Domain); |
| 4461 | Accesses = Accesses.add_map(AccessDomain); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4462 | } |
| 4463 | } |
Tobias Grosser | 206e9e3 | 2017-07-24 16:22:27 +0000 | [diff] [blame] | 4464 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4465 | return Accesses.coalesce(); |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4466 | } |
| 4467 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4468 | isl::union_map Scop::getMustWrites() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4469 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isMustWrite(); }); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4470 | } |
| 4471 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4472 | isl::union_map Scop::getMayWrites() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4473 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isMayWrite(); }); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 4474 | } |
| 4475 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4476 | isl::union_map Scop::getWrites() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4477 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isWrite(); }); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4478 | } |
| 4479 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4480 | isl::union_map Scop::getReads() { |
Tobias Grosser | e5a3514 | 2015-11-12 14:07:09 +0000 | [diff] [blame] | 4481 | return getAccessesOfType([](MemoryAccess &MA) { return MA.isRead(); }); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4482 | } |
| 4483 | |
Tobias Grosser | 5ab39ff | 2017-08-06 19:22:27 +0000 | [diff] [blame] | 4484 | isl::union_map Scop::getAccesses() { |
Tobias Grosser | 2ac2338 | 2015-11-12 14:07:13 +0000 | [diff] [blame] | 4485 | return getAccessesOfType([](MemoryAccess &MA) { return true; }); |
| 4486 | } |
| 4487 | |
Tobias Grosser | fa03cb7 | 2017-08-17 22:04:53 +0000 | [diff] [blame] | 4488 | isl::union_map Scop::getAccesses(ScopArrayInfo *Array) { |
| 4489 | return getAccessesOfType( |
| 4490 | [Array](MemoryAccess &MA) { return MA.getScopArrayInfo() == Array; }); |
| 4491 | } |
| 4492 | |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 4493 | // Check whether @p Node is an extension node. |
| 4494 | // |
| 4495 | // @return true if @p Node is an extension node. |
| 4496 | isl_bool isNotExtNode(__isl_keep isl_schedule_node *Node, void *User) { |
| 4497 | if (isl_schedule_node_get_type(Node) == isl_schedule_node_extension) |
| 4498 | return isl_bool_error; |
| 4499 | else |
| 4500 | return isl_bool_true; |
| 4501 | } |
| 4502 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4503 | bool Scop::containsExtensionNode(isl::schedule Schedule) { |
| 4504 | return isl_schedule_foreach_schedule_node_top_down( |
Tobias Grosser | d3d3d6b | 2018-04-29 00:28:26 +0000 | [diff] [blame] | 4505 | Schedule.get(), isNotExtNode, nullptr) == isl_stat_error; |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 4506 | } |
| 4507 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 4508 | isl::union_map Scop::getSchedule() const { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4509 | auto Tree = getScheduleTree(); |
| 4510 | if (containsExtensionNode(Tree)) |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 4511 | return nullptr; |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4512 | |
| 4513 | return Tree.get_map(); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4514 | } |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4515 | |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 4516 | isl::schedule Scop::getScheduleTree() const { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4517 | return Schedule.intersect_domain(getDomains()); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4518 | } |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 4519 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4520 | void Scop::setSchedule(isl::union_map NewSchedule) { |
| 4521 | auto S = isl::schedule::from_domain(getDomains()); |
| 4522 | Schedule = S.insert_partial_schedule( |
| 4523 | isl::multi_union_pw_aff::from_union_map(NewSchedule)); |
Michael Kruse | 2dab88e | 2018-06-06 21:37:35 +0000 | [diff] [blame^] | 4524 | ScheduleModified = true; |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4525 | } |
| 4526 | |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4527 | void Scop::setScheduleTree(isl::schedule NewSchedule) { |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4528 | Schedule = NewSchedule; |
Michael Kruse | 2dab88e | 2018-06-06 21:37:35 +0000 | [diff] [blame^] | 4529 | ScheduleModified = true; |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4530 | } |
| 4531 | |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 4532 | bool Scop::restrictDomains(isl::union_set Domain) { |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4533 | bool Changed = false; |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 4534 | for (ScopStmt &Stmt : *this) { |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 4535 | isl::union_set StmtDomain = isl::union_set(Stmt.getDomain()); |
| 4536 | isl::union_set NewStmtDomain = StmtDomain.intersect(Domain); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4537 | |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 4538 | if (StmtDomain.is_subset(NewStmtDomain)) |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4539 | continue; |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4540 | |
| 4541 | Changed = true; |
| 4542 | |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 4543 | NewStmtDomain = NewStmtDomain.coalesce(); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4544 | |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 4545 | if (NewStmtDomain.is_empty()) |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 4546 | Stmt.restrictDomain(isl::set::empty(Stmt.getDomainSpace())); |
Tobias Grosser | 990cbb4 | 2017-08-14 06:49:01 +0000 | [diff] [blame] | 4547 | else |
| 4548 | Stmt.restrictDomain(isl::set(NewStmtDomain)); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4549 | } |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 4550 | return Changed; |
| 4551 | } |
| 4552 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4553 | ScalarEvolution *Scop::getSE() const { return SE; } |
| 4554 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 4555 | // Create an isl_multi_union_aff that defines an identity mapping from the |
| 4556 | // elements of USet to their N-th dimension. |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4557 | // |
Tobias Grosser | cbf7ae8 | 2015-12-21 22:45:53 +0000 | [diff] [blame] | 4558 | // # Example: |
| 4559 | // |
| 4560 | // Domain: { A[i,j]; B[i,j,k] } |
| 4561 | // N: 1 |
| 4562 | // |
| 4563 | // Resulting Mapping: { {A[i,j] -> [(j)]; B[i,j,k] -> [(j)] } |
| 4564 | // |
| 4565 | // @param USet A union set describing the elements for which to generate a |
| 4566 | // mapping. |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4567 | // @param N The dimension to map to. |
Tobias Grosser | cbf7ae8 | 2015-12-21 22:45:53 +0000 | [diff] [blame] | 4568 | // @returns A mapping from USet to its N-th dimension. |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4569 | 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] | 4570 | assert(N >= 0); |
Tobias Grosser | c900633d | 2015-12-21 23:01:53 +0000 | [diff] [blame] | 4571 | assert(USet); |
Siddharth Bhat | 8bb436e | 2017-05-29 11:34:29 +0000 | [diff] [blame] | 4572 | assert(!USet.is_empty()); |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 4573 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4574 | auto Result = isl::union_pw_multi_aff::empty(USet.get_space()); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4575 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4576 | auto Lambda = [&Result, N](isl::set S) -> isl::stat { |
| 4577 | int Dim = S.dim(isl::dim::set); |
| 4578 | auto PMA = isl::pw_multi_aff::project_out_map(S.get_space(), isl::dim::set, |
| 4579 | N, Dim - N); |
| 4580 | if (N > 1) |
| 4581 | PMA = PMA.drop_dims(isl::dim::out, 0, N - 1); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4582 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4583 | Result = Result.add_pw_multi_aff(PMA); |
| 4584 | return isl::stat::ok; |
| 4585 | }; |
Tobias Grosser | cbf7ae8 | 2015-12-21 22:45:53 +0000 | [diff] [blame] | 4586 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4587 | isl::stat Res = USet.foreach_set(Lambda); |
Sumanth Gundapaneni | 4b1472f | 2016-01-20 15:41:30 +0000 | [diff] [blame] | 4588 | (void)Res; |
| 4589 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4590 | assert(Res == isl::stat::ok); |
Tobias Grosser | cbf7ae8 | 2015-12-21 22:45:53 +0000 | [diff] [blame] | 4591 | |
Tobias Grosser | 9932086 | 2017-05-26 17:22:03 +0000 | [diff] [blame] | 4592 | return isl::multi_union_pw_aff(isl::union_pw_multi_aff(Result)); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4593 | } |
| 4594 | |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 4595 | void Scop::addScopStmt(BasicBlock *BB, StringRef Name, Loop *SurroundingLoop, |
| 4596 | std::vector<Instruction *> Instructions) { |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 4597 | assert(BB && "Unexpected nullptr!"); |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 4598 | Stmts.emplace_back(*this, *BB, Name, SurroundingLoop, Instructions); |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 4599 | auto *Stmt = &Stmts.back(); |
Michael Kruse | 4dfa732 | 2017-07-18 15:41:49 +0000 | [diff] [blame] | 4600 | StmtMap[BB].push_back(Stmt); |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 4601 | for (Instruction *Inst : Instructions) { |
| 4602 | assert(!InstStmtMap.count(Inst) && |
| 4603 | "Unexpected statement corresponding to the instruction."); |
| 4604 | InstStmtMap[Inst] = Stmt; |
| 4605 | } |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 4606 | } |
| 4607 | |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 4608 | void Scop::addScopStmt(Region *R, StringRef Name, Loop *SurroundingLoop, |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 4609 | std::vector<Instruction *> Instructions) { |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 4610 | assert(R && "Unexpected nullptr!"); |
Michael Kruse | d6e2208 | 2018-01-18 15:15:38 +0000 | [diff] [blame] | 4611 | Stmts.emplace_back(*this, *R, Name, SurroundingLoop, Instructions); |
Hongbin Zheng | a8fb73f | 2016-11-21 20:09:40 +0000 | [diff] [blame] | 4612 | auto *Stmt = &Stmts.back(); |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 4613 | |
| 4614 | for (Instruction *Inst : Instructions) { |
| 4615 | assert(!InstStmtMap.count(Inst) && |
| 4616 | "Unexpected statement corresponding to the instruction."); |
| 4617 | InstStmtMap[Inst] = Stmt; |
| 4618 | } |
| 4619 | |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 4620 | for (BasicBlock *BB : R->blocks()) { |
Michael Kruse | 4dfa732 | 2017-07-18 15:41:49 +0000 | [diff] [blame] | 4621 | StmtMap[BB].push_back(Stmt); |
Tobias Grosser | bd15d13 | 2017-08-31 03:15:56 +0000 | [diff] [blame] | 4622 | if (BB == R->getEntry()) |
| 4623 | continue; |
Michael Kruse | cd3b9fe | 2017-08-09 16:45:37 +0000 | [diff] [blame] | 4624 | for (Instruction &Inst : *BB) { |
| 4625 | assert(!InstStmtMap.count(&Inst) && |
| 4626 | "Unexpected statement corresponding to the instruction."); |
| 4627 | InstStmtMap[&Inst] = Stmt; |
| 4628 | } |
| 4629 | } |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4630 | } |
| 4631 | |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 4632 | ScopStmt *Scop::addScopStmt(isl::map SourceRel, isl::map TargetRel, |
| 4633 | isl::set Domain) { |
Tobias Grosser | eba86a1 | 2016-11-09 04:24:49 +0000 | [diff] [blame] | 4634 | #ifndef NDEBUG |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 4635 | isl::set SourceDomain = SourceRel.domain(); |
| 4636 | isl::set TargetDomain = TargetRel.domain(); |
| 4637 | assert(Domain.is_subset(TargetDomain) && |
Tobias Grosser | 744740a | 2016-11-05 21:02:43 +0000 | [diff] [blame] | 4638 | "Target access not defined for complete statement domain"); |
Tobias Grosser | 85048ef | 2017-08-06 17:24:59 +0000 | [diff] [blame] | 4639 | assert(Domain.is_subset(SourceDomain) && |
Tobias Grosser | 744740a | 2016-11-05 21:02:43 +0000 | [diff] [blame] | 4640 | "Source access not defined for complete statement domain"); |
Tobias Grosser | eba86a1 | 2016-11-09 04:24:49 +0000 | [diff] [blame] | 4641 | #endif |
Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 4642 | Stmts.emplace_back(*this, SourceRel, TargetRel, Domain); |
| 4643 | CopyStmtsNum++; |
| 4644 | return &(Stmts.back()); |
| 4645 | } |
| 4646 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4647 | void Scop::buildSchedule(LoopInfo &LI) { |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 4648 | Loop *L = getLoopSurroundingScop(*this, LI); |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4649 | LoopStackTy LoopStack({LoopStackElementTy(L, nullptr, 0)}); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4650 | buildSchedule(getRegion().getNode(), LoopStack, LI); |
Tobias Grosser | 151ae32 | 2016-04-03 19:36:52 +0000 | [diff] [blame] | 4651 | assert(LoopStack.size() == 1 && LoopStack.back().L == L); |
| 4652 | Schedule = LoopStack[0].Schedule; |
Johannes Doerfert | f9711ef | 2016-01-06 12:59:23 +0000 | [diff] [blame] | 4653 | } |
| 4654 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4655 | /// To generate a schedule for the elements in a Region we traverse the Region |
| 4656 | /// in reverse-post-order and add the contained RegionNodes in traversal order |
| 4657 | /// to the schedule of the loop that is currently at the top of the LoopStack. |
| 4658 | /// For loop-free codes, this results in a correct sequential ordering. |
| 4659 | /// |
| 4660 | /// Example: |
| 4661 | /// bb1(0) |
| 4662 | /// / \. |
| 4663 | /// bb2(1) bb3(2) |
| 4664 | /// \ / \. |
| 4665 | /// bb4(3) bb5(4) |
| 4666 | /// \ / |
| 4667 | /// bb6(5) |
| 4668 | /// |
| 4669 | /// Including loops requires additional processing. Whenever a loop header is |
| 4670 | /// encountered, the corresponding loop is added to the @p LoopStack. Starting |
| 4671 | /// from an empty schedule, we first process all RegionNodes that are within |
| 4672 | /// this loop and complete the sequential schedule at this loop-level before |
| 4673 | /// processing about any other nodes. To implement this |
| 4674 | /// loop-nodes-first-processing, the reverse post-order traversal is |
| 4675 | /// insufficient. Hence, we additionally check if the traversal yields |
| 4676 | /// sub-regions or blocks that are outside the last loop on the @p LoopStack. |
| 4677 | /// These region-nodes are then queue and only traverse after the all nodes |
| 4678 | /// within the current loop have been processed. |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4679 | void Scop::buildSchedule(Region *R, LoopStackTy &LoopStack, LoopInfo &LI) { |
Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 4680 | Loop *OuterScopLoop = getLoopSurroundingScop(*this, LI); |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4681 | |
| 4682 | ReversePostOrderTraversal<Region *> RTraversal(R); |
| 4683 | std::deque<RegionNode *> WorkList(RTraversal.begin(), RTraversal.end()); |
| 4684 | std::deque<RegionNode *> DelayList; |
| 4685 | bool LastRNWaiting = false; |
| 4686 | |
| 4687 | // Iterate over the region @p R in reverse post-order but queue |
| 4688 | // sub-regions/blocks iff they are not part of the last encountered but not |
| 4689 | // completely traversed loop. The variable LastRNWaiting is a flag to indicate |
| 4690 | // that we queued the last sub-region/block from the reverse post-order |
| 4691 | // iterator. If it is set we have to explore the next sub-region/block from |
| 4692 | // the iterator (if any) to guarantee progress. If it is not set we first try |
| 4693 | // the next queued sub-region/blocks. |
| 4694 | while (!WorkList.empty() || !DelayList.empty()) { |
| 4695 | RegionNode *RN; |
| 4696 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 4697 | if ((LastRNWaiting && !WorkList.empty()) || DelayList.empty()) { |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4698 | RN = WorkList.front(); |
| 4699 | WorkList.pop_front(); |
| 4700 | LastRNWaiting = false; |
| 4701 | } else { |
| 4702 | RN = DelayList.front(); |
| 4703 | DelayList.pop_front(); |
| 4704 | } |
| 4705 | |
| 4706 | Loop *L = getRegionNodeLoop(RN, LI); |
Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 4707 | if (!contains(L)) |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4708 | L = OuterScopLoop; |
| 4709 | |
Tobias Grosser | 151ae32 | 2016-04-03 19:36:52 +0000 | [diff] [blame] | 4710 | Loop *LastLoop = LoopStack.back().L; |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4711 | if (LastLoop != L) { |
Johannes Doerfert | d5edbd6 | 2016-04-03 23:09:06 +0000 | [diff] [blame] | 4712 | if (LastLoop && !LastLoop->contains(L)) { |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4713 | LastRNWaiting = true; |
| 4714 | DelayList.push_back(RN); |
| 4715 | continue; |
| 4716 | } |
| 4717 | LoopStack.push_back({L, nullptr, 0}); |
| 4718 | } |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4719 | buildSchedule(RN, LoopStack, LI); |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4720 | } |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4721 | } |
| 4722 | |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4723 | void Scop::buildSchedule(RegionNode *RN, LoopStackTy &LoopStack, LoopInfo &LI) { |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4724 | if (RN->isSubRegion()) { |
| 4725 | auto *LocalRegion = RN->getNodeAs<Region>(); |
Johannes Doerfert | ffd222f | 2016-05-19 12:34:57 +0000 | [diff] [blame] | 4726 | if (!isNonAffineSubRegion(LocalRegion)) { |
| 4727 | buildSchedule(LocalRegion, LoopStack, LI); |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4728 | return; |
| 4729 | } |
| 4730 | } |
Michael Kruse | 046dde4 | 2015-08-10 13:01:57 +0000 | [diff] [blame] | 4731 | |
Philip Pfaffe | 8dd0f47 | 2017-11-16 16:35:19 +0000 | [diff] [blame] | 4732 | assert(LoopStack.rbegin() != LoopStack.rend()); |
| 4733 | auto LoopData = LoopStack.rbegin(); |
| 4734 | LoopData->NumBlocksProcessed += getNumBlocksInRegionNode(RN); |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4735 | |
Michael Kruse | 1ce6791 | 2017-07-20 17:18:58 +0000 | [diff] [blame] | 4736 | for (auto *Stmt : getStmtListFor(RN)) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4737 | isl::union_set UDomain{Stmt->getDomain()}; |
| 4738 | auto StmtSchedule = isl::schedule::from_domain(UDomain); |
Philip Pfaffe | 8dd0f47 | 2017-11-16 16:35:19 +0000 | [diff] [blame] | 4739 | LoopData->Schedule = combineInSequence(LoopData->Schedule, StmtSchedule); |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4740 | } |
| 4741 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4742 | // Check if we just processed the last node in this loop. If we did, finalize |
| 4743 | // the loop by: |
| 4744 | // |
| 4745 | // - adding new schedule dimensions |
| 4746 | // - folding the resulting schedule into the parent loop schedule |
| 4747 | // - dropping the loop schedule from the LoopStack. |
| 4748 | // |
| 4749 | // Then continue to check surrounding loops, which might also have been |
| 4750 | // completed by this node. |
Philip Pfaffe | 8dd0f47 | 2017-11-16 16:35:19 +0000 | [diff] [blame] | 4751 | size_t Dimension = LoopStack.size(); |
| 4752 | while (LoopData->L && |
| 4753 | LoopData->NumBlocksProcessed == getNumBlocksInLoop(LoopData->L)) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4754 | isl::schedule Schedule = LoopData->Schedule; |
Philip Pfaffe | 8dd0f47 | 2017-11-16 16:35:19 +0000 | [diff] [blame] | 4755 | auto NumBlocksProcessed = LoopData->NumBlocksProcessed; |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4756 | |
Philip Pfaffe | 8dd0f47 | 2017-11-16 16:35:19 +0000 | [diff] [blame] | 4757 | assert(std::next(LoopData) != LoopStack.rend()); |
| 4758 | ++LoopData; |
| 4759 | --Dimension; |
Tobias Grosser | 8362c26 | 2016-01-06 15:30:06 +0000 | [diff] [blame] | 4760 | |
Tobias Grosser | c2fd8b4 | 2016-02-01 11:54:13 +0000 | [diff] [blame] | 4761 | if (Schedule) { |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4762 | isl::union_set Domain = Schedule.get_domain(); |
Philip Pfaffe | 8dd0f47 | 2017-11-16 16:35:19 +0000 | [diff] [blame] | 4763 | isl::multi_union_pw_aff MUPA = mapToDimension(Domain, Dimension); |
Philip Pfaffe | 00fd43b | 2017-11-19 22:13:34 +0000 | [diff] [blame] | 4764 | Schedule = Schedule.insert_partial_schedule(MUPA); |
Philip Pfaffe | 8dd0f47 | 2017-11-16 16:35:19 +0000 | [diff] [blame] | 4765 | LoopData->Schedule = combineInSequence(LoopData->Schedule, Schedule); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4766 | } |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 4767 | |
Philip Pfaffe | 8dd0f47 | 2017-11-16 16:35:19 +0000 | [diff] [blame] | 4768 | LoopData->NumBlocksProcessed += NumBlocksProcessed; |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 4769 | } |
Philip Pfaffe | 8dd0f47 | 2017-11-16 16:35:19 +0000 | [diff] [blame] | 4770 | // Now pop all loops processed up there from the LoopStack |
| 4771 | LoopStack.erase(LoopStack.begin() + Dimension, LoopStack.end()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 4772 | } |
| 4773 | |
Michael Kruse | 6eba4b1 | 2017-07-20 17:08:50 +0000 | [diff] [blame] | 4774 | ArrayRef<ScopStmt *> Scop::getStmtListFor(BasicBlock *BB) const { |
| 4775 | auto StmtMapIt = StmtMap.find(BB); |
| 4776 | if (StmtMapIt == StmtMap.end()) |
| 4777 | return {}; |
Michael Kruse | 6eba4b1 | 2017-07-20 17:08:50 +0000 | [diff] [blame] | 4778 | return StmtMapIt->second; |
| 4779 | } |
| 4780 | |
Michael Kruse | a230f22 | 2018-01-23 23:56:36 +0000 | [diff] [blame] | 4781 | ScopStmt *Scop::getIncomingStmtFor(const Use &U) const { |
| 4782 | auto *PHI = cast<PHINode>(U.getUser()); |
| 4783 | BasicBlock *IncomingBB = PHI->getIncomingBlock(U); |
| 4784 | |
| 4785 | // If the value is a non-synthesizable from the incoming block, use the |
| 4786 | // statement that contains it as user statement. |
| 4787 | if (auto *IncomingInst = dyn_cast<Instruction>(U.get())) { |
| 4788 | if (IncomingInst->getParent() == IncomingBB) { |
| 4789 | if (ScopStmt *IncomingStmt = getStmtFor(IncomingInst)) |
| 4790 | return IncomingStmt; |
| 4791 | } |
| 4792 | } |
| 4793 | |
| 4794 | // Otherwise, use the epilogue/last statement. |
| 4795 | return getLastStmtFor(IncomingBB); |
| 4796 | } |
| 4797 | |
Michael Kruse | 6eba4b1 | 2017-07-20 17:08:50 +0000 | [diff] [blame] | 4798 | ScopStmt *Scop::getLastStmtFor(BasicBlock *BB) const { |
| 4799 | ArrayRef<ScopStmt *> StmtList = getStmtListFor(BB); |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 4800 | if (!StmtList.empty()) |
Michael Kruse | 6eba4b1 | 2017-07-20 17:08:50 +0000 | [diff] [blame] | 4801 | return StmtList.back(); |
| 4802 | return nullptr; |
| 4803 | } |
| 4804 | |
Michael Kruse | 1ce6791 | 2017-07-20 17:18:58 +0000 | [diff] [blame] | 4805 | ArrayRef<ScopStmt *> Scop::getStmtListFor(RegionNode *RN) const { |
Michael Kruse | 6f7721f | 2016-02-24 22:08:19 +0000 | [diff] [blame] | 4806 | if (RN->isSubRegion()) |
Michael Kruse | 1ce6791 | 2017-07-20 17:18:58 +0000 | [diff] [blame] | 4807 | return getStmtListFor(RN->getNodeAs<Region>()); |
| 4808 | return getStmtListFor(RN->getNodeAs<BasicBlock>()); |
Michael Kruse | 6f7721f | 2016-02-24 22:08:19 +0000 | [diff] [blame] | 4809 | } |
| 4810 | |
Michael Kruse | 1ce6791 | 2017-07-20 17:18:58 +0000 | [diff] [blame] | 4811 | ArrayRef<ScopStmt *> Scop::getStmtListFor(Region *R) const { |
| 4812 | return getStmtListFor(R->getEntry()); |
Michael Kruse | a902ba6 | 2015-12-13 19:21:45 +0000 | [diff] [blame] | 4813 | } |
| 4814 | |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 4815 | int Scop::getRelativeLoopDepth(const Loop *L) const { |
Philip Pfaffe | 1a0128f | 2017-05-24 18:39:39 +0000 | [diff] [blame] | 4816 | if (!L || !R.contains(L)) |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 4817 | return -1; |
Philip Pfaffe | 1a0128f | 2017-05-24 18:39:39 +0000 | [diff] [blame] | 4818 | // outermostLoopInRegion always returns nullptr for top level regions |
| 4819 | if (R.isTopLevelRegion()) { |
| 4820 | // LoopInfo's depths start at 1, we start at 0 |
| 4821 | return L->getLoopDepth() - 1; |
| 4822 | } else { |
| 4823 | Loop *OuterLoop = R.outermostLoopInRegion(const_cast<Loop *>(L)); |
| 4824 | assert(OuterLoop); |
| 4825 | return L->getLoopDepth() - OuterLoop->getLoopDepth(); |
| 4826 | } |
Johannes Doerfert | d020b77 | 2015-08-27 06:53:52 +0000 | [diff] [blame] | 4827 | } |
| 4828 | |
Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 4829 | ScopArrayInfo *Scop::getArrayInfoByName(const std::string BaseName) { |
| 4830 | for (auto &SAI : arrays()) { |
| 4831 | if (SAI->getName() == BaseName) |
| 4832 | return SAI; |
| 4833 | } |
| 4834 | return nullptr; |
| 4835 | } |
| 4836 | |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 4837 | void Scop::addAccessData(MemoryAccess *Access) { |
| 4838 | const ScopArrayInfo *SAI = Access->getOriginalScopArrayInfo(); |
| 4839 | assert(SAI && "can only use after access relations have been constructed"); |
| 4840 | |
| 4841 | if (Access->isOriginalValueKind() && Access->isRead()) |
| 4842 | ValueUseAccs[SAI].push_back(Access); |
| 4843 | else if (Access->isOriginalAnyPHIKind() && Access->isWrite()) |
| 4844 | PHIIncomingAccs[SAI].push_back(Access); |
| 4845 | } |
| 4846 | |
| 4847 | void Scop::removeAccessData(MemoryAccess *Access) { |
Michael Kruse | 6d7a789 | 2017-09-21 14:23:11 +0000 | [diff] [blame] | 4848 | if (Access->isOriginalValueKind() && Access->isWrite()) { |
| 4849 | ValueDefAccs.erase(Access->getAccessValue()); |
| 4850 | } else if (Access->isOriginalValueKind() && Access->isRead()) { |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 4851 | auto &Uses = ValueUseAccs[Access->getScopArrayInfo()]; |
Michael Kruse | 7de6166 | 2018-04-09 23:13:01 +0000 | [diff] [blame] | 4852 | auto NewEnd = std::remove(Uses.begin(), Uses.end(), Access); |
| 4853 | Uses.erase(NewEnd, Uses.end()); |
Michael Kruse | 6d7a789 | 2017-09-21 14:23:11 +0000 | [diff] [blame] | 4854 | } else if (Access->isOriginalPHIKind() && Access->isRead()) { |
| 4855 | PHINode *PHI = cast<PHINode>(Access->getAccessInstruction()); |
| 4856 | PHIReadAccs.erase(PHI); |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 4857 | } else if (Access->isOriginalAnyPHIKind() && Access->isWrite()) { |
| 4858 | auto &Incomings = PHIIncomingAccs[Access->getScopArrayInfo()]; |
Michael Kruse | 7de6166 | 2018-04-09 23:13:01 +0000 | [diff] [blame] | 4859 | auto NewEnd = std::remove(Incomings.begin(), Incomings.end(), Access); |
| 4860 | Incomings.erase(NewEnd, Incomings.end()); |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 4861 | } |
| 4862 | } |
| 4863 | |
| 4864 | MemoryAccess *Scop::getValueDef(const ScopArrayInfo *SAI) const { |
| 4865 | assert(SAI->isValueKind()); |
| 4866 | |
| 4867 | Instruction *Val = dyn_cast<Instruction>(SAI->getBasePtr()); |
| 4868 | if (!Val) |
| 4869 | return nullptr; |
| 4870 | |
Michael Kruse | 6d7a789 | 2017-09-21 14:23:11 +0000 | [diff] [blame] | 4871 | return ValueDefAccs.lookup(Val); |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 4872 | } |
| 4873 | |
| 4874 | ArrayRef<MemoryAccess *> Scop::getValueUses(const ScopArrayInfo *SAI) const { |
| 4875 | assert(SAI->isValueKind()); |
| 4876 | auto It = ValueUseAccs.find(SAI); |
| 4877 | if (It == ValueUseAccs.end()) |
| 4878 | return {}; |
| 4879 | return It->second; |
| 4880 | } |
| 4881 | |
| 4882 | MemoryAccess *Scop::getPHIRead(const ScopArrayInfo *SAI) const { |
| 4883 | assert(SAI->isPHIKind() || SAI->isExitPHIKind()); |
| 4884 | |
| 4885 | if (SAI->isExitPHIKind()) |
| 4886 | return nullptr; |
| 4887 | |
| 4888 | PHINode *PHI = cast<PHINode>(SAI->getBasePtr()); |
Michael Kruse | 6d7a789 | 2017-09-21 14:23:11 +0000 | [diff] [blame] | 4889 | return PHIReadAccs.lookup(PHI); |
Michael Kruse | 8b80580 | 2017-07-19 17:11:25 +0000 | [diff] [blame] | 4890 | } |
| 4891 | |
| 4892 | ArrayRef<MemoryAccess *> Scop::getPHIIncomings(const ScopArrayInfo *SAI) const { |
| 4893 | assert(SAI->isPHIKind() || SAI->isExitPHIKind()); |
| 4894 | auto It = PHIIncomingAccs.find(SAI); |
| 4895 | if (It == PHIIncomingAccs.end()) |
| 4896 | return {}; |
| 4897 | return It->second; |
| 4898 | } |
| 4899 | |
Michael Kruse | a508a4e | 2017-07-27 14:39:52 +0000 | [diff] [blame] | 4900 | bool Scop::isEscaping(Instruction *Inst) { |
| 4901 | assert(contains(Inst) && "The concept of escaping makes only sense for " |
| 4902 | "values defined inside the SCoP"); |
| 4903 | |
| 4904 | for (Use &Use : Inst->uses()) { |
| 4905 | BasicBlock *UserBB = getUseBlock(Use); |
| 4906 | if (!contains(UserBB)) |
| 4907 | return true; |
| 4908 | |
| 4909 | // When the SCoP region exit needs to be simplified, PHIs in the region exit |
| 4910 | // move to a new basic block such that its incoming blocks are not in the |
| 4911 | // SCoP anymore. |
| 4912 | if (hasSingleExitEdge() && isa<PHINode>(Use.getUser()) && |
| 4913 | isExit(cast<PHINode>(Use.getUser())->getParent())) |
| 4914 | return true; |
| 4915 | } |
| 4916 | return false; |
| 4917 | } |
| 4918 | |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 4919 | Scop::ScopStatistics Scop::getStatistics() const { |
| 4920 | ScopStatistics Result; |
| 4921 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS) |
| 4922 | auto LoopStat = ScopDetection::countBeneficialLoops(&R, *SE, *getLI(), 0); |
| 4923 | |
| 4924 | int NumTotalLoops = LoopStat.NumLoops; |
| 4925 | Result.NumBoxedLoops = getBoxedLoops().size(); |
| 4926 | Result.NumAffineLoops = NumTotalLoops - Result.NumBoxedLoops; |
| 4927 | |
| 4928 | for (const ScopStmt &Stmt : *this) { |
| 4929 | isl::set Domain = Stmt.getDomain().intersect_params(getContext()); |
| 4930 | bool IsInLoop = Stmt.getNumIterators() >= 1; |
| 4931 | for (MemoryAccess *MA : Stmt) { |
| 4932 | if (!MA->isWrite()) |
| 4933 | continue; |
| 4934 | |
| 4935 | if (MA->isLatestValueKind()) { |
| 4936 | Result.NumValueWrites += 1; |
| 4937 | if (IsInLoop) |
| 4938 | Result.NumValueWritesInLoops += 1; |
| 4939 | } |
| 4940 | |
| 4941 | if (MA->isLatestAnyPHIKind()) { |
| 4942 | Result.NumPHIWrites += 1; |
| 4943 | if (IsInLoop) |
| 4944 | Result.NumPHIWritesInLoops += 1; |
| 4945 | } |
| 4946 | |
| 4947 | isl::set AccSet = |
| 4948 | MA->getAccessRelation().intersect_domain(Domain).range(); |
| 4949 | if (AccSet.is_singleton()) { |
| 4950 | Result.NumSingletonWrites += 1; |
| 4951 | if (IsInLoop) |
| 4952 | Result.NumSingletonWritesInLoops += 1; |
| 4953 | } |
| 4954 | } |
| 4955 | } |
| 4956 | #endif |
| 4957 | return Result; |
| 4958 | } |
| 4959 | |
Eugene Zelenko | 0c4c2ce | 2017-08-22 21:25:51 +0000 | [diff] [blame] | 4960 | raw_ostream &polly::operator<<(raw_ostream &OS, const Scop &scop) { |
| 4961 | scop.print(OS, PollyPrintInstructions); |
| 4962 | return OS; |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 4963 | } |
| 4964 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 4965 | //===----------------------------------------------------------------------===// |
| 4966 | void ScopInfoRegionPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 4967 | AU.addRequired<LoopInfoWrapperPass>(); |
| 4968 | AU.addRequired<RegionInfoPass>(); |
| 4969 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 4970 | AU.addRequiredTransitive<ScalarEvolutionWrapperPass>(); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 4971 | AU.addRequiredTransitive<ScopDetectionWrapperPass>(); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 4972 | AU.addRequired<AAResultsWrapperPass>(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 4973 | AU.addRequired<AssumptionCacheTracker>(); |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 4974 | AU.addRequired<OptimizationRemarkEmitterWrapperPass>(); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 4975 | AU.setPreservesAll(); |
| 4976 | } |
| 4977 | |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 4978 | void updateLoopCountStatistic(ScopDetection::LoopStats Stats, |
| 4979 | Scop::ScopStatistics ScopStats) { |
| 4980 | assert(Stats.NumLoops == ScopStats.NumAffineLoops + ScopStats.NumBoxedLoops); |
| 4981 | |
| 4982 | NumScops++; |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 4983 | NumLoopsInScop += Stats.NumLoops; |
| 4984 | MaxNumLoopsInScop = |
| 4985 | std::max(MaxNumLoopsInScop.getValue(), (unsigned)Stats.NumLoops); |
| 4986 | |
Tobias Grosser | fcc3ad5 | 2018-04-18 20:03:36 +0000 | [diff] [blame] | 4987 | if (Stats.MaxDepth == 0) |
| 4988 | NumScopsDepthZero++; |
| 4989 | else if (Stats.MaxDepth == 1) |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 4990 | NumScopsDepthOne++; |
| 4991 | else if (Stats.MaxDepth == 2) |
| 4992 | NumScopsDepthTwo++; |
| 4993 | else if (Stats.MaxDepth == 3) |
| 4994 | NumScopsDepthThree++; |
| 4995 | else if (Stats.MaxDepth == 4) |
| 4996 | NumScopsDepthFour++; |
| 4997 | else if (Stats.MaxDepth == 5) |
| 4998 | NumScopsDepthFive++; |
| 4999 | else |
| 5000 | NumScopsDepthLarger++; |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 5001 | |
| 5002 | NumAffineLoops += ScopStats.NumAffineLoops; |
| 5003 | NumBoxedLoops += ScopStats.NumBoxedLoops; |
| 5004 | |
| 5005 | NumValueWrites += ScopStats.NumValueWrites; |
| 5006 | NumValueWritesInLoops += ScopStats.NumValueWritesInLoops; |
| 5007 | NumPHIWrites += ScopStats.NumPHIWrites; |
| 5008 | NumPHIWritesInLoops += ScopStats.NumPHIWritesInLoops; |
| 5009 | NumSingletonWrites += ScopStats.NumSingletonWrites; |
| 5010 | NumSingletonWritesInLoops += ScopStats.NumSingletonWritesInLoops; |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 5011 | } |
| 5012 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5013 | bool ScopInfoRegionPass::runOnRegion(Region *R, RGPassManager &RGM) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5014 | auto &SD = getAnalysis<ScopDetectionWrapperPass>().getSD(); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5015 | |
| 5016 | if (!SD.isMaxRegionInScop(*R)) |
| 5017 | return false; |
| 5018 | |
| 5019 | Function *F = R->getEntry()->getParent(); |
| 5020 | auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
| 5021 | auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 5022 | auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 5023 | auto const &DL = F->getParent()->getDataLayout(); |
| 5024 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5025 | auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(*F); |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 5026 | auto &ORE = getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE(); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5027 | |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 5028 | ScopBuilder SB(R, AC, AA, DL, DT, LI, SD, SE, ORE); |
Johannes Doerfert | b7e9713 | 2016-06-27 09:25:40 +0000 | [diff] [blame] | 5029 | S = SB.getScop(); // take ownership of scop object |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 5030 | |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 5031 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS) |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 5032 | if (S) { |
| 5033 | ScopDetection::LoopStats Stats = |
| 5034 | ScopDetection::countBeneficialLoops(&S->getRegion(), SE, LI, 0); |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 5035 | updateLoopCountStatistic(Stats, S->getStatistics()); |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 5036 | } |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 5037 | #endif |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 5038 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 5039 | return false; |
| 5040 | } |
| 5041 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5042 | void ScopInfoRegionPass::print(raw_ostream &OS, const Module *) const { |
Johannes Doerfert | b7e9713 | 2016-06-27 09:25:40 +0000 | [diff] [blame] | 5043 | if (S) |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 5044 | S->print(OS, PollyPrintInstructions); |
Johannes Doerfert | b7e9713 | 2016-06-27 09:25:40 +0000 | [diff] [blame] | 5045 | else |
| 5046 | OS << "Invalid Scop!\n"; |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5047 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 5048 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5049 | char ScopInfoRegionPass::ID = 0; |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 5050 | |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5051 | Pass *polly::createScopInfoRegionPassPass() { return new ScopInfoRegionPass(); } |
| 5052 | |
| 5053 | INITIALIZE_PASS_BEGIN(ScopInfoRegionPass, "polly-scops", |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 5054 | "Polly - Create polyhedral description of Scops", false, |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 5055 | false); |
Chandler Carruth | 66ef16b | 2015-09-09 22:13:56 +0000 | [diff] [blame] | 5056 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5057 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker); |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 5058 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 5059 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); |
Tobias Grosser | c5bcf24 | 2015-08-17 10:57:08 +0000 | [diff] [blame] | 5060 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5061 | INITIALIZE_PASS_DEPENDENCY(ScopDetectionWrapperPass); |
Johannes Doerfert | 96425c2 | 2015-08-30 21:13:53 +0000 | [diff] [blame] | 5062 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); |
Johannes Doerfert | 99191c7 | 2016-05-31 09:41:04 +0000 | [diff] [blame] | 5063 | INITIALIZE_PASS_END(ScopInfoRegionPass, "polly-scops", |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 5064 | "Polly - Create polyhedral description of Scops", false, |
| 5065 | false) |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5066 | |
| 5067 | //===----------------------------------------------------------------------===// |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5068 | ScopInfo::ScopInfo(const DataLayout &DL, ScopDetection &SD, ScalarEvolution &SE, |
| 5069 | LoopInfo &LI, AliasAnalysis &AA, DominatorTree &DT, |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 5070 | AssumptionCache &AC, OptimizationRemarkEmitter &ORE) |
| 5071 | : 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] | 5072 | recompute(); |
| 5073 | } |
| 5074 | |
| 5075 | void ScopInfo::recompute() { |
| 5076 | RegionToScopMap.clear(); |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 5077 | /// Create polyhedral description of scops for all the valid regions of a |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5078 | /// function. |
| 5079 | for (auto &It : SD) { |
| 5080 | Region *R = const_cast<Region *>(It); |
| 5081 | if (!SD.isMaxRegionInScop(*R)) |
| 5082 | continue; |
| 5083 | |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 5084 | ScopBuilder SB(R, AC, AA, DL, DT, LI, SD, SE, ORE); |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5085 | std::unique_ptr<Scop> S = SB.getScop(); |
| 5086 | if (!S) |
| 5087 | continue; |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 5088 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS) |
Philip Pfaffe | ead67db | 2017-08-02 11:14:41 +0000 | [diff] [blame] | 5089 | ScopDetection::LoopStats Stats = |
| 5090 | ScopDetection::countBeneficialLoops(&S->getRegion(), SE, LI, 0); |
Michael Kruse | 06ed529 | 2017-08-23 13:50:30 +0000 | [diff] [blame] | 5091 | updateLoopCountStatistic(Stats, S->getStatistics()); |
| 5092 | #endif |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5093 | bool Inserted = RegionToScopMap.insert({R, std::move(S)}).second; |
| 5094 | assert(Inserted && "Building Scop for the same region twice!"); |
| 5095 | (void)Inserted; |
| 5096 | } |
| 5097 | } |
| 5098 | |
Philip Pfaffe | f43e7c2 | 2017-08-10 07:43:46 +0000 | [diff] [blame] | 5099 | bool ScopInfo::invalidate(Function &F, const PreservedAnalyses &PA, |
| 5100 | FunctionAnalysisManager::Invalidator &Inv) { |
| 5101 | // Check whether the analysis, all analyses on functions have been preserved |
| 5102 | // or anything we're holding references to is being invalidated |
| 5103 | auto PAC = PA.getChecker<ScopInfoAnalysis>(); |
| 5104 | return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) || |
| 5105 | Inv.invalidate<ScopAnalysis>(F, PA) || |
| 5106 | Inv.invalidate<ScalarEvolutionAnalysis>(F, PA) || |
| 5107 | Inv.invalidate<LoopAnalysis>(F, PA) || |
| 5108 | Inv.invalidate<AAManager>(F, PA) || |
| 5109 | Inv.invalidate<DominatorTreeAnalysis>(F, PA) || |
| 5110 | Inv.invalidate<AssumptionAnalysis>(F, PA); |
| 5111 | } |
| 5112 | |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5113 | AnalysisKey ScopInfoAnalysis::Key; |
| 5114 | |
| 5115 | ScopInfoAnalysis::Result ScopInfoAnalysis::run(Function &F, |
| 5116 | FunctionAnalysisManager &FAM) { |
| 5117 | auto &SD = FAM.getResult<ScopAnalysis>(F); |
| 5118 | auto &SE = FAM.getResult<ScalarEvolutionAnalysis>(F); |
| 5119 | auto &LI = FAM.getResult<LoopAnalysis>(F); |
| 5120 | auto &AA = FAM.getResult<AAManager>(F); |
| 5121 | auto &DT = FAM.getResult<DominatorTreeAnalysis>(F); |
| 5122 | auto &AC = FAM.getResult<AssumptionAnalysis>(F); |
| 5123 | auto &DL = F.getParent()->getDataLayout(); |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 5124 | auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(F); |
| 5125 | return {DL, SD, SE, LI, AA, DT, AC, ORE}; |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5126 | } |
| 5127 | |
| 5128 | PreservedAnalyses ScopInfoPrinterPass::run(Function &F, |
| 5129 | FunctionAnalysisManager &FAM) { |
| 5130 | auto &SI = FAM.getResult<ScopInfoAnalysis>(F); |
Philip Pfaffe | 96d2143 | 2017-08-04 11:28:51 +0000 | [diff] [blame] | 5131 | // Since the legacy PM processes Scops in bottom up, we print them in reverse |
| 5132 | // order here to keep the output persistent |
| 5133 | for (auto &It : reverse(SI)) { |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5134 | if (It.second) |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 5135 | It.second->print(Stream, PollyPrintInstructions); |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5136 | else |
| 5137 | Stream << "Invalid Scop!\n"; |
| 5138 | } |
| 5139 | return PreservedAnalyses::all(); |
| 5140 | } |
| 5141 | |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5142 | void ScopInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 5143 | AU.addRequired<LoopInfoWrapperPass>(); |
| 5144 | AU.addRequired<RegionInfoPass>(); |
| 5145 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 5146 | AU.addRequiredTransitive<ScalarEvolutionWrapperPass>(); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5147 | AU.addRequiredTransitive<ScopDetectionWrapperPass>(); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5148 | AU.addRequired<AAResultsWrapperPass>(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5149 | AU.addRequired<AssumptionCacheTracker>(); |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 5150 | AU.addRequired<OptimizationRemarkEmitterWrapperPass>(); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5151 | AU.setPreservesAll(); |
| 5152 | } |
| 5153 | |
| 5154 | bool ScopInfoWrapperPass::runOnFunction(Function &F) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5155 | auto &SD = getAnalysis<ScopDetectionWrapperPass>().getSD(); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5156 | auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
| 5157 | auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 5158 | auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 5159 | auto const &DL = F.getParent()->getDataLayout(); |
| 5160 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5161 | auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 5162 | auto &ORE = getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE(); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5163 | |
Michael Kruse | a4f447c | 2017-08-28 14:07:33 +0000 | [diff] [blame] | 5164 | Result.reset(new ScopInfo{DL, SD, SE, LI, AA, DT, AC, ORE}); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5165 | return false; |
| 5166 | } |
| 5167 | |
| 5168 | void ScopInfoWrapperPass::print(raw_ostream &OS, const Module *) const { |
Philip Pfaffe | 838e088 | 2017-05-15 12:55:14 +0000 | [diff] [blame] | 5169 | for (auto &It : *Result) { |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5170 | if (It.second) |
Michael Kruse | cd4c977 | 2017-07-21 15:35:53 +0000 | [diff] [blame] | 5171 | It.second->print(OS, PollyPrintInstructions); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5172 | else |
| 5173 | OS << "Invalid Scop!\n"; |
| 5174 | } |
| 5175 | } |
| 5176 | |
| 5177 | char ScopInfoWrapperPass::ID = 0; |
| 5178 | |
| 5179 | Pass *polly::createScopInfoWrapperPassPass() { |
| 5180 | return new ScopInfoWrapperPass(); |
| 5181 | } |
| 5182 | |
| 5183 | INITIALIZE_PASS_BEGIN( |
| 5184 | ScopInfoWrapperPass, "polly-function-scops", |
| 5185 | "Polly - Create polyhedral description of all Scops of a function", false, |
| 5186 | false); |
| 5187 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass); |
Michael Kruse | 89b1f94 | 2017-03-17 13:56:53 +0000 | [diff] [blame] | 5188 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5189 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); |
| 5190 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); |
| 5191 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 5192 | INITIALIZE_PASS_DEPENDENCY(ScopDetectionWrapperPass); |
Johannes Doerfert | 4ba65a5 | 2016-06-27 09:32:30 +0000 | [diff] [blame] | 5193 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); |
| 5194 | INITIALIZE_PASS_END( |
| 5195 | ScopInfoWrapperPass, "polly-function-scops", |
| 5196 | "Polly - Create polyhedral description of all Scops of a function", false, |
| 5197 | false) |