Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 1 | //===- ScopDetection.cpp - Detect Scops -----------------------------------===// |
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 | // Detect the maximal Scops of a function. |
| 11 | // |
| 12 | // A static control part (Scop) is a subgraph of the control flow graph (CFG) |
| 13 | // that only has statically known control flow and can therefore be described |
| 14 | // within the polyhedral model. |
| 15 | // |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 16 | // Every Scop fulfills these restrictions: |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 17 | // |
| 18 | // * It is a single entry single exit region |
| 19 | // |
| 20 | // * Only affine linear bounds in the loops |
| 21 | // |
| 22 | // Every natural loop in a Scop must have a number of loop iterations that can |
| 23 | // be described as an affine linear function in surrounding loop iterators or |
| 24 | // parameters. (A parameter is a scalar that does not change its value during |
| 25 | // execution of the Scop). |
| 26 | // |
| 27 | // * Only comparisons of affine linear expressions in conditions |
| 28 | // |
| 29 | // * All loops and conditions perfectly nested |
| 30 | // |
| 31 | // The control flow needs to be structured such that it could be written using |
| 32 | // just 'for' and 'if' statements, without the need for any 'goto', 'break' or |
| 33 | // 'continue'. |
| 34 | // |
| 35 | // * Side effect free functions call |
| 36 | // |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 37 | // Function calls and intrinsics that do not have side effects (readnone) |
| 38 | // or memory intrinsics (memset, memcpy, memmove) are allowed. |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 39 | // |
| 40 | // The Scop detection finds the largest Scops by checking if the largest |
| 41 | // region is a Scop. If this is not the case, its canonical subregions are |
| 42 | // checked until a region is a Scop. It is now tried to extend this Scop by |
| 43 | // creating a larger non canonical region. |
| 44 | // |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | |
Tobias Grosser | 5624d3c | 2015-12-21 12:38:56 +0000 | [diff] [blame] | 47 | #include "polly/ScopDetection.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 48 | #include "polly/LinkAllPasses.h" |
Tobias Grosser | 637bd63 | 2013-05-07 07:31:10 +0000 | [diff] [blame] | 49 | #include "polly/Options.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 50 | #include "polly/ScopDetectionDiagnostic.h" |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 51 | #include "polly/Support/SCEVValidator.h" |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 52 | #include "polly/Support/ScopHelper.h" |
Tobias Grosser | a63b7ce | 2015-05-03 05:21:36 +0000 | [diff] [blame] | 53 | #include "polly/Support/ScopLocation.h" |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 54 | #include "llvm/ADT/DenseMap.h" |
| 55 | #include "llvm/ADT/SetVector.h" |
| 56 | #include "llvm/ADT/SmallPtrSet.h" |
| 57 | #include "llvm/ADT/SmallVector.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 58 | #include "llvm/ADT/Statistic.h" |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 59 | #include "llvm/ADT/StringRef.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 60 | #include "llvm/Analysis/AliasAnalysis.h" |
Tobias Grosser | 8bd7f3c | 2017-03-09 11:36:00 +0000 | [diff] [blame] | 61 | #include "llvm/Analysis/Loads.h" |
Tobias Grosser | 6e9f25a | 2011-11-09 22:35:00 +0000 | [diff] [blame] | 62 | #include "llvm/Analysis/LoopInfo.h" |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 63 | #include "llvm/Analysis/MemoryLocation.h" |
| 64 | #include "llvm/Analysis/OptimizationDiagnosticInfo.h" |
| 65 | #include "llvm/Analysis/RegionInfo.h" |
Tobias Grosser | 6e9f25a | 2011-11-09 22:35:00 +0000 | [diff] [blame] | 66 | #include "llvm/Analysis/ScalarEvolution.h" |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 67 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 68 | #include "llvm/IR/BasicBlock.h" |
| 69 | #include "llvm/IR/Constants.h" |
| 70 | #include "llvm/IR/DebugLoc.h" |
| 71 | #include "llvm/IR/DerivedTypes.h" |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 72 | #include "llvm/IR/DiagnosticInfo.h" |
| 73 | #include "llvm/IR/DiagnosticPrinter.h" |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 74 | #include "llvm/IR/Dominators.h" |
| 75 | #include "llvm/IR/Function.h" |
| 76 | #include "llvm/IR/InstrTypes.h" |
| 77 | #include "llvm/IR/Instruction.h" |
| 78 | #include "llvm/IR/Instructions.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 79 | #include "llvm/IR/IntrinsicInst.h" |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 80 | #include "llvm/IR/Intrinsics.h" |
Chandler Carruth | 6b96c24 | 2014-03-06 00:47:27 +0000 | [diff] [blame] | 81 | #include "llvm/IR/LLVMContext.h" |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 82 | #include "llvm/IR/Metadata.h" |
| 83 | #include "llvm/IR/Module.h" |
| 84 | #include "llvm/IR/PassManager.h" |
| 85 | #include "llvm/IR/Type.h" |
| 86 | #include "llvm/IR/Value.h" |
| 87 | #include "llvm/Pass.h" |
| 88 | #include "llvm/Support/Casting.h" |
| 89 | #include "llvm/Support/CommandLine.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 90 | #include "llvm/Support/Debug.h" |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 91 | #include "llvm/Support/ErrorHandling.h" |
Siddharth Bhat | e2699b5 | 2017-07-24 12:40:52 +0000 | [diff] [blame] | 92 | #include "llvm/Support/Regex.h" |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 93 | #include "llvm/Support/raw_ostream.h" |
| 94 | #include <algorithm> |
| 95 | #include <cassert> |
| 96 | #include <memory> |
Tobias Grosser | 1c3a6d7 | 2016-01-22 09:44:37 +0000 | [diff] [blame] | 97 | #include <stack> |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 98 | #include <string> |
| 99 | #include <utility> |
| 100 | #include <vector> |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 101 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 102 | using namespace llvm; |
| 103 | using namespace polly; |
| 104 | |
Chandler Carruth | 95fef94 | 2014-04-22 03:30:19 +0000 | [diff] [blame] | 105 | #define DEBUG_TYPE "polly-detect" |
| 106 | |
Tobias Grosser | c1a269b | 2015-12-21 21:00:43 +0000 | [diff] [blame] | 107 | // This option is set to a very high value, as analyzing such loops increases |
| 108 | // compile time on several cases. For experiments that enable this option, |
| 109 | // a value of around 40 has been working to avoid run-time regressions with |
| 110 | // Polly while still exposing interesting optimization opportunities. |
| 111 | static cl::opt<int> ProfitabilityMinPerLoopInstructions( |
| 112 | "polly-detect-profitability-min-per-loop-insts", |
| 113 | cl::desc("The minimal number of per-loop instructions before a single loop " |
| 114 | "region is considered profitable"), |
| 115 | cl::Hidden, cl::ValueRequired, cl::init(100000000), cl::cat(PollyCategory)); |
| 116 | |
Tobias Grosser | 575aca8 | 2015-10-06 16:10:29 +0000 | [diff] [blame] | 117 | bool polly::PollyProcessUnprofitable; |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 118 | |
Tobias Grosser | 575aca8 | 2015-10-06 16:10:29 +0000 | [diff] [blame] | 119 | static cl::opt<bool, true> XPollyProcessUnprofitable( |
| 120 | "polly-process-unprofitable", |
| 121 | cl::desc( |
| 122 | "Process scops that are unlikely to benefit from Polly optimizations."), |
| 123 | cl::location(PollyProcessUnprofitable), cl::init(false), cl::ZeroOrMore, |
| 124 | cl::cat(PollyCategory)); |
Tobias Grosser | d1e33e7 | 2015-02-19 05:31:07 +0000 | [diff] [blame] | 125 | |
Siddharth Bhat | 286c916 | 2017-06-09 08:23:40 +0000 | [diff] [blame] | 126 | static cl::list<std::string> OnlyFunctions( |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 127 | "polly-only-func", |
Siddharth Bhat | e2699b5 | 2017-07-24 12:40:52 +0000 | [diff] [blame] | 128 | cl::desc("Only run on functions that match a regex. " |
| 129 | "Multiple regexes can be comma separated. " |
| 130 | "Scop detection will run on all functions that match " |
| 131 | "ANY of the regexes provided."), |
Siddharth Bhat | 286c916 | 2017-06-09 08:23:40 +0000 | [diff] [blame] | 132 | cl::ZeroOrMore, cl::CommaSeparated, cl::cat(PollyCategory)); |
Tobias Grosser | 2ff8723 | 2011-10-23 11:17:06 +0000 | [diff] [blame] | 133 | |
Siddharth Bhat | 0a1177b | 2017-07-28 11:47:24 +0000 | [diff] [blame] | 134 | static cl::list<std::string> IgnoredFunctions( |
| 135 | "polly-ignore-func", |
| 136 | cl::desc("Ignore functions that match a regex. " |
| 137 | "Multiple regexes can be comma separated. " |
| 138 | "Scop detection will ignore all functions that match " |
| 139 | "ANY of the regexes provided."), |
| 140 | cl::ZeroOrMore, cl::CommaSeparated, cl::cat(PollyCategory)); |
| 141 | |
Siddharth Bhat | b46847c | 2017-08-17 21:57:23 +0000 | [diff] [blame] | 142 | bool polly::PollyAllowFullFunction; |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 143 | |
Siddharth Bhat | b46847c | 2017-08-17 21:57:23 +0000 | [diff] [blame] | 144 | static cl::opt<bool, true> |
| 145 | XAllowFullFunction("polly-detect-full-functions", |
| 146 | cl::desc("Allow the detection of full functions"), |
| 147 | cl::location(polly::PollyAllowFullFunction), |
| 148 | cl::init(false), cl::cat(PollyCategory)); |
Tobias Grosser | d8945ba | 2017-05-19 12:13:02 +0000 | [diff] [blame] | 149 | |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 150 | static cl::opt<std::string> OnlyRegion( |
| 151 | "polly-only-region", |
| 152 | cl::desc("Only run on certain regions (The provided identifier must " |
| 153 | "appear in the name of the region's entry block"), |
| 154 | cl::value_desc("identifier"), cl::ValueRequired, cl::init(""), |
| 155 | cl::cat(PollyCategory)); |
Tobias Grosser | 4449e52 | 2014-01-27 14:24:53 +0000 | [diff] [blame] | 156 | |
Tobias Grosser | 60cd932 | 2011-11-10 12:47:26 +0000 | [diff] [blame] | 157 | static cl::opt<bool> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 158 | IgnoreAliasing("polly-ignore-aliasing", |
| 159 | cl::desc("Ignore possible aliasing of the array bases"), |
| 160 | cl::Hidden, cl::init(false), cl::ZeroOrMore, |
| 161 | cl::cat(PollyCategory)); |
Tobias Grosser | 2ff8723 | 2011-10-23 11:17:06 +0000 | [diff] [blame] | 162 | |
Johannes Doerfert | bda8143 | 2016-12-02 17:55:41 +0000 | [diff] [blame] | 163 | bool polly::PollyAllowUnsignedOperations; |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 164 | |
Johannes Doerfert | bda8143 | 2016-12-02 17:55:41 +0000 | [diff] [blame] | 165 | static cl::opt<bool, true> XPollyAllowUnsignedOperations( |
| 166 | "polly-allow-unsigned-operations", |
| 167 | cl::desc("Allow unsigned operations such as comparisons or zero-extends."), |
| 168 | cl::location(PollyAllowUnsignedOperations), cl::Hidden, cl::ZeroOrMore, |
| 169 | cl::init(true), cl::cat(PollyCategory)); |
| 170 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 171 | bool polly::PollyUseRuntimeAliasChecks; |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 172 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 173 | static cl::opt<bool, true> XPollyUseRuntimeAliasChecks( |
| 174 | "polly-use-runtime-alias-checks", |
| 175 | cl::desc("Use runtime alias checks to resolve possible aliasing."), |
| 176 | cl::location(PollyUseRuntimeAliasChecks), cl::Hidden, cl::ZeroOrMore, |
| 177 | cl::init(true), cl::cat(PollyCategory)); |
| 178 | |
Tobias Grosser | 637bd63 | 2013-05-07 07:31:10 +0000 | [diff] [blame] | 179 | static cl::opt<bool> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 180 | ReportLevel("polly-report", |
| 181 | cl::desc("Print information about the activities of Polly"), |
| 182 | cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 183 | |
Tobias Grosser | 8ebdc2d | 2016-02-07 08:48:57 +0000 | [diff] [blame] | 184 | static cl::opt<bool> AllowDifferentTypes( |
| 185 | "polly-allow-differing-element-types", |
| 186 | cl::desc("Allow different element types for array accesses"), cl::Hidden, |
Tobias Grosser | a2ee003 | 2016-02-16 14:37:24 +0000 | [diff] [blame] | 187 | cl::init(true), cl::ZeroOrMore, cl::cat(PollyCategory)); |
Tobias Grosser | 8ebdc2d | 2016-02-07 08:48:57 +0000 | [diff] [blame] | 188 | |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 189 | static cl::opt<bool> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 190 | AllowNonAffine("polly-allow-nonaffine", |
| 191 | cl::desc("Allow non affine access functions in arrays"), |
| 192 | cl::Hidden, cl::init(false), cl::ZeroOrMore, |
| 193 | cl::cat(PollyCategory)); |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 194 | |
Tobias Grosser | 898a636 | 2016-03-23 06:40:15 +0000 | [diff] [blame] | 195 | static cl::opt<bool> |
| 196 | AllowModrefCall("polly-allow-modref-calls", |
| 197 | cl::desc("Allow functions with known modref behavior"), |
| 198 | cl::Hidden, cl::init(false), cl::ZeroOrMore, |
| 199 | cl::cat(PollyCategory)); |
| 200 | |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 201 | static cl::opt<bool> AllowNonAffineSubRegions( |
| 202 | "polly-allow-nonaffine-branches", |
| 203 | cl::desc("Allow non affine conditions for branches"), cl::Hidden, |
Johannes Doerfert | a36842f | 2015-02-26 11:09:24 +0000 | [diff] [blame] | 204 | cl::init(true), cl::ZeroOrMore, cl::cat(PollyCategory)); |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 205 | |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 206 | static cl::opt<bool> |
| 207 | AllowNonAffineSubLoops("polly-allow-nonaffine-loops", |
| 208 | cl::desc("Allow non affine conditions for loops"), |
| 209 | cl::Hidden, cl::init(false), cl::ZeroOrMore, |
| 210 | cl::cat(PollyCategory)); |
| 211 | |
Tobias Grosser | c7d3fc5 | 2013-07-25 03:02:29 +0000 | [diff] [blame] | 212 | static cl::opt<bool, true> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 213 | TrackFailures("polly-detect-track-failures", |
| 214 | cl::desc("Track failure strings in detecting scop regions"), |
| 215 | cl::location(PollyTrackFailures), cl::Hidden, cl::ZeroOrMore, |
Andreas Simbuerger | 3efe40b | 2014-08-17 10:09:03 +0000 | [diff] [blame] | 216 | cl::init(true), cl::cat(PollyCategory)); |
Tobias Grosser | c7d3fc5 | 2013-07-25 03:02:29 +0000 | [diff] [blame] | 217 | |
Andreas Simbuerger | 0447240 | 2014-05-24 09:25:10 +0000 | [diff] [blame] | 218 | static cl::opt<bool> KeepGoing("polly-detect-keep-going", |
| 219 | cl::desc("Do not fail on the first error."), |
| 220 | cl::Hidden, cl::ZeroOrMore, cl::init(false), |
| 221 | cl::cat(PollyCategory)); |
| 222 | |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 223 | static cl::opt<bool, true> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 224 | PollyDelinearizeX("polly-delinearize", |
| 225 | cl::desc("Delinearize array access functions"), |
| 226 | cl::location(PollyDelinearize), cl::Hidden, |
Tobias Grosser | 6973cb6 | 2015-03-08 15:21:18 +0000 | [diff] [blame] | 227 | cl::ZeroOrMore, cl::init(true), cl::cat(PollyCategory)); |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 228 | |
Tobias Grosser | a168993 | 2014-02-18 18:49:49 +0000 | [diff] [blame] | 229 | static cl::opt<bool> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 230 | VerifyScops("polly-detect-verify", |
| 231 | cl::desc("Verify the detected SCoPs after each transformation"), |
| 232 | cl::Hidden, cl::init(false), cl::ZeroOrMore, |
| 233 | cl::cat(PollyCategory)); |
Tobias Grosser | a168993 | 2014-02-18 18:49:49 +0000 | [diff] [blame] | 234 | |
Tobias Grosser | 8fa3e4c | 2016-02-26 16:43:35 +0000 | [diff] [blame] | 235 | bool polly::PollyInvariantLoadHoisting; |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 236 | |
Tobias Grosser | 8fa3e4c | 2016-02-26 16:43:35 +0000 | [diff] [blame] | 237 | static cl::opt<bool, true> XPollyInvariantLoadHoisting( |
| 238 | "polly-invariant-load-hoisting", cl::desc("Hoist invariant loads."), |
| 239 | cl::location(PollyInvariantLoadHoisting), cl::Hidden, cl::ZeroOrMore, |
Tobias Grosser | 74814e1 | 2016-08-15 16:43:36 +0000 | [diff] [blame] | 240 | cl::init(false), cl::cat(PollyCategory)); |
Tobias Grosser | 8fa3e4c | 2016-02-26 16:43:35 +0000 | [diff] [blame] | 241 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 242 | /// The minimal trip count under which loops are considered unprofitable. |
Johannes Doerfert | e526de5 | 2015-09-21 19:10:11 +0000 | [diff] [blame] | 243 | static const unsigned MIN_LOOP_TRIP_COUNT = 8; |
| 244 | |
Tobias Grosser | c7d3fc5 | 2013-07-25 03:02:29 +0000 | [diff] [blame] | 245 | bool polly::PollyTrackFailures = false; |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 246 | bool polly::PollyDelinearize = false; |
Johannes Doerfert | 43e1ead | 2014-07-15 21:06:48 +0000 | [diff] [blame] | 247 | StringRef polly::PollySkipFnAttr = "polly.skip.fn"; |
Tobias Grosser | c7d3fc5 | 2013-07-25 03:02:29 +0000 | [diff] [blame] | 248 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 249 | //===----------------------------------------------------------------------===// |
| 250 | // Statistics. |
| 251 | |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 252 | STATISTIC(NumScopRegions, "Number of scops"); |
| 253 | STATISTIC(NumLoopsInScop, "Number of loops in scops"); |
| 254 | STATISTIC(NumScopsDepthOne, "Number of scops with maximal loop depth 1"); |
| 255 | STATISTIC(NumScopsDepthTwo, "Number of scops with maximal loop depth 2"); |
| 256 | STATISTIC(NumScopsDepthThree, "Number of scops with maximal loop depth 3"); |
| 257 | STATISTIC(NumScopsDepthFour, "Number of scops with maximal loop depth 4"); |
| 258 | STATISTIC(NumScopsDepthFive, "Number of scops with maximal loop depth 5"); |
| 259 | STATISTIC(NumScopsDepthLarger, |
| 260 | "Number of scops with maximal loop depth 6 and larger"); |
| 261 | STATISTIC(NumProfScopRegions, "Number of scops (profitable scops only)"); |
| 262 | STATISTIC(NumLoopsInProfScop, |
| 263 | "Number of loops in scops (profitable scops only)"); |
| 264 | STATISTIC(NumLoopsOverall, "Number of total loops"); |
| 265 | STATISTIC(NumProfScopsDepthOne, |
| 266 | "Number of scops with maximal loop depth 1 (profitable scops only)"); |
| 267 | STATISTIC(NumProfScopsDepthTwo, |
| 268 | "Number of scops with maximal loop depth 2 (profitable scops only)"); |
| 269 | STATISTIC(NumProfScopsDepthThree, |
| 270 | "Number of scops with maximal loop depth 3 (profitable scops only)"); |
| 271 | STATISTIC(NumProfScopsDepthFour, |
| 272 | "Number of scops with maximal loop depth 4 (profitable scops only)"); |
| 273 | STATISTIC(NumProfScopsDepthFive, |
| 274 | "Number of scops with maximal loop depth 5 (profitable scops only)"); |
Tobias Grosser | 21a059a | 2017-01-16 14:08:10 +0000 | [diff] [blame] | 275 | STATISTIC(NumProfScopsDepthLarger, |
| 276 | "Number of scops with maximal loop depth 6 and larger " |
| 277 | "(profitable scops only)"); |
Tobias Grosser | 9fe37df | 2017-02-12 10:52:57 +0000 | [diff] [blame] | 278 | STATISTIC(MaxNumLoopsInScop, "Maximal number of loops in scops"); |
| 279 | STATISTIC(MaxNumLoopsInProfScop, |
| 280 | "Maximal number of loops in scops (profitable scops only)"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 281 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 282 | static void updateLoopCountStatistic(ScopDetection::LoopStats Stats, |
| 283 | bool OnlyProfitable); |
| 284 | |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 285 | namespace { |
| 286 | |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 287 | class DiagnosticScopFound : public DiagnosticInfo { |
| 288 | private: |
| 289 | static int PluginDiagnosticKind; |
| 290 | |
| 291 | Function &F; |
| 292 | std::string FileName; |
| 293 | unsigned EntryLine, ExitLine; |
| 294 | |
| 295 | public: |
Tobias Grosser | 1b12f46 | 2013-12-18 11:14:36 +0000 | [diff] [blame] | 296 | DiagnosticScopFound(Function &F, std::string FileName, unsigned EntryLine, |
| 297 | unsigned ExitLine) |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 298 | : DiagnosticInfo(PluginDiagnosticKind, DS_Note), F(F), FileName(FileName), |
Tobias Grosser | 1b12f46 | 2013-12-18 11:14:36 +0000 | [diff] [blame] | 299 | EntryLine(EntryLine), ExitLine(ExitLine) {} |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 300 | |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 301 | void print(DiagnosticPrinter &DP) const override; |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 302 | |
| 303 | static bool classof(const DiagnosticInfo *DI) { |
| 304 | return DI->getKind() == PluginDiagnosticKind; |
| 305 | } |
| 306 | }; |
| 307 | |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 308 | } // namespace |
| 309 | |
Tobias Grosser | db6db50 | 2016-04-01 07:15:19 +0000 | [diff] [blame] | 310 | int DiagnosticScopFound::PluginDiagnosticKind = |
| 311 | getNextAvailablePluginDiagnosticKind(); |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 312 | |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 313 | void DiagnosticScopFound::print(DiagnosticPrinter &DP) const { |
Tobias Grosser | 1b12f46 | 2013-12-18 11:14:36 +0000 | [diff] [blame] | 314 | DP << "Polly detected an optimizable loop region (scop) in function '" << F |
| 315 | << "'\n"; |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 316 | |
| 317 | if (FileName.empty()) { |
| 318 | DP << "Scop location is unknown. Compile with debug info " |
| 319 | "(-g) to get more precise information. "; |
Tobias Grosser | 1b12f46 | 2013-12-18 11:14:36 +0000 | [diff] [blame] | 320 | return; |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | DP << FileName << ":" << EntryLine << ": Start of scop\n"; |
| 324 | DP << FileName << ":" << ExitLine << ": End of scop"; |
| 325 | } |
| 326 | |
Siddharth Bhat | 0a1177b | 2017-07-28 11:47:24 +0000 | [diff] [blame] | 327 | /// Check if a string matches any regex in a list of regexes. |
| 328 | /// @param Str the input string to match against. |
| 329 | /// @param RegexList a list of strings that are regular expressions. |
| 330 | static bool doesStringMatchAnyRegex(StringRef Str, |
| 331 | const cl::list<std::string> &RegexList) { |
| 332 | for (auto RegexStr : RegexList) { |
Siddharth Bhat | e2699b5 | 2017-07-24 12:40:52 +0000 | [diff] [blame] | 333 | Regex R(RegexStr); |
| 334 | |
| 335 | std::string Err; |
| 336 | if (!R.isValid(Err)) |
Siddharth Bhat | 0a1177b | 2017-07-28 11:47:24 +0000 | [diff] [blame] | 337 | report_fatal_error("invalid regex given as input to polly: " + Err, true); |
Siddharth Bhat | e2699b5 | 2017-07-24 12:40:52 +0000 | [diff] [blame] | 338 | |
Siddharth Bhat | 0a1177b | 2017-07-28 11:47:24 +0000 | [diff] [blame] | 339 | if (R.match(Str)) |
Siddharth Bhat | 286c916 | 2017-06-09 08:23:40 +0000 | [diff] [blame] | 340 | return true; |
Siddharth Bhat | e2699b5 | 2017-07-24 12:40:52 +0000 | [diff] [blame] | 341 | } |
Siddharth Bhat | 286c916 | 2017-06-09 08:23:40 +0000 | [diff] [blame] | 342 | return false; |
| 343 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 344 | //===----------------------------------------------------------------------===// |
| 345 | // ScopDetection. |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 346 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 347 | ScopDetection::ScopDetection(Function &F, const DominatorTree &DT, |
| 348 | ScalarEvolution &SE, LoopInfo &LI, RegionInfo &RI, |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 349 | AliasAnalysis &AA, OptimizationRemarkEmitter &ORE) |
| 350 | : DT(DT), SE(SE), LI(LI), RI(RI), AA(AA), ORE(ORE) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 351 | if (!PollyProcessUnprofitable && LI.empty()) |
| 352 | return; |
| 353 | |
| 354 | Region *TopRegion = RI.getTopLevelRegion(); |
| 355 | |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 356 | if (!OnlyFunctions.empty() && |
Siddharth Bhat | 0a1177b | 2017-07-28 11:47:24 +0000 | [diff] [blame] | 357 | !doesStringMatchAnyRegex(F.getName(), OnlyFunctions)) |
| 358 | return; |
| 359 | |
| 360 | if (doesStringMatchAnyRegex(F.getName(), IgnoredFunctions)) |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 361 | return; |
| 362 | |
| 363 | if (!isValidFunction(F)) |
| 364 | return; |
| 365 | |
| 366 | findScops(*TopRegion); |
| 367 | |
| 368 | NumScopRegions += ValidRegions.size(); |
| 369 | |
| 370 | // Prune non-profitable regions. |
| 371 | for (auto &DIt : DetectionContextMap) { |
| 372 | auto &DC = DIt.getSecond(); |
| 373 | if (DC.Log.hasErrors()) |
| 374 | continue; |
| 375 | if (!ValidRegions.count(&DC.CurRegion)) |
| 376 | continue; |
| 377 | LoopStats Stats = countBeneficialLoops(&DC.CurRegion, SE, LI, 0); |
| 378 | updateLoopCountStatistic(Stats, false /* OnlyProfitable */); |
| 379 | if (isProfitableRegion(DC)) { |
| 380 | updateLoopCountStatistic(Stats, true /* OnlyProfitable */); |
| 381 | continue; |
| 382 | } |
| 383 | |
| 384 | ValidRegions.remove(&DC.CurRegion); |
| 385 | } |
| 386 | |
| 387 | NumProfScopRegions += ValidRegions.size(); |
| 388 | NumLoopsOverall += countBeneficialLoops(TopRegion, SE, LI, 0).NumLoops; |
| 389 | |
| 390 | // Only makes sense when we tracked errors. |
| 391 | if (PollyTrackFailures) |
| 392 | emitMissedRemarks(F); |
| 393 | |
| 394 | if (ReportLevel) |
| 395 | printLocations(F); |
| 396 | |
| 397 | assert(ValidRegions.size() <= DetectionContextMap.size() && |
| 398 | "Cached more results than valid regions"); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 401 | template <class RR, typename... Args> |
| 402 | inline bool ScopDetection::invalid(DetectionContext &Context, bool Assert, |
| 403 | Args &&... Arguments) const { |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 404 | if (!Context.Verifying) { |
Andreas Simbuerger | 4870e09 | 2014-05-24 09:25:01 +0000 | [diff] [blame] | 405 | RejectLog &Log = Context.Log; |
| 406 | std::shared_ptr<RR> RejectReason = std::make_shared<RR>(Arguments...); |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 407 | |
Andreas Simbuerger | 8a00c9b | 2014-05-24 09:25:06 +0000 | [diff] [blame] | 408 | if (PollyTrackFailures) |
Andreas Simbuerger | 4870e09 | 2014-05-24 09:25:01 +0000 | [diff] [blame] | 409 | Log.report(RejectReason); |
Andreas Simbuerger | 4870e09 | 2014-05-24 09:25:01 +0000 | [diff] [blame] | 410 | |
| 411 | DEBUG(dbgs() << RejectReason->getMessage()); |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 412 | DEBUG(dbgs() << "\n"); |
| 413 | } else { |
| 414 | assert(!Assert && "Verification of detected scop failed"); |
| 415 | } |
| 416 | |
| 417 | return false; |
| 418 | } |
| 419 | |
Tobias Grosser | a168993 | 2014-02-18 18:49:49 +0000 | [diff] [blame] | 420 | bool ScopDetection::isMaxRegionInScop(const Region &R, bool Verify) const { |
| 421 | if (!ValidRegions.count(&R)) |
| 422 | return false; |
| 423 | |
Johannes Doerfert | 3f1c285 | 2015-02-19 18:11:50 +0000 | [diff] [blame] | 424 | if (Verify) { |
Johannes Doerfert | 6c7639b | 2016-05-12 18:50:01 +0000 | [diff] [blame] | 425 | DetectionContextMap.erase(getBBPairForRegion(&R)); |
| 426 | const auto &It = DetectionContextMap.insert(std::make_pair( |
| 427 | getBBPairForRegion(&R), |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 428 | DetectionContext(const_cast<Region &>(R), AA, false /*verifying*/))); |
Tobias Grosser | 907090c | 2015-10-25 10:55:35 +0000 | [diff] [blame] | 429 | DetectionContext &Context = It.first->second; |
Johannes Doerfert | 3f1c285 | 2015-02-19 18:11:50 +0000 | [diff] [blame] | 430 | return isValidRegion(Context); |
| 431 | } |
Tobias Grosser | a168993 | 2014-02-18 18:49:49 +0000 | [diff] [blame] | 432 | |
| 433 | return true; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Tobias Grosser | 4f129a6 | 2011-10-08 00:30:55 +0000 | [diff] [blame] | 436 | std::string ScopDetection::regionIsInvalidBecause(const Region *R) const { |
Andreas Simbuerger | 8a00c9b | 2014-05-24 09:25:06 +0000 | [diff] [blame] | 437 | // Get the first error we found. Even in keep-going mode, this is the first |
| 438 | // reason that caused the candidate to be rejected. |
Johannes Doerfert | 6c7639b | 2016-05-12 18:50:01 +0000 | [diff] [blame] | 439 | auto *Log = lookupRejectionLog(R); |
Andreas Simbuerger | 83ed861 | 2014-06-12 07:25:08 +0000 | [diff] [blame] | 440 | |
| 441 | // This can happen when we marked a region invalid, but didn't track |
| 442 | // an error for it. |
Johannes Doerfert | 6c7639b | 2016-05-12 18:50:01 +0000 | [diff] [blame] | 443 | if (!Log || !Log->hasErrors()) |
Andreas Simbuerger | 83ed861 | 2014-06-12 07:25:08 +0000 | [diff] [blame] | 444 | return ""; |
| 445 | |
Johannes Doerfert | 6c7639b | 2016-05-12 18:50:01 +0000 | [diff] [blame] | 446 | RejectReasonPtr RR = *Log->begin(); |
Andreas Simbuerger | 83ed861 | 2014-06-12 07:25:08 +0000 | [diff] [blame] | 447 | return RR->getMessage(); |
Tobias Grosser | 4f129a6 | 2011-10-08 00:30:55 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 450 | bool ScopDetection::addOverApproximatedRegion(Region *AR, |
| 451 | DetectionContext &Context) const { |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 452 | // If we already know about Ar we can exit. |
| 453 | if (!Context.NonAffineSubRegionSet.insert(AR)) |
| 454 | return true; |
| 455 | |
| 456 | // All loops in the region have to be overapproximated too if there |
| 457 | // are accesses that depend on the iteration count. |
Michael Kruse | 41f046a | 2016-06-27 19:00:49 +0000 | [diff] [blame] | 458 | |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 459 | for (BasicBlock *BB : AR->blocks()) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 460 | Loop *L = LI.getLoopFor(BB); |
Tobias Grosser | 349d1c3 | 2016-09-20 17:05:22 +0000 | [diff] [blame] | 461 | if (AR->contains(L)) |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 462 | Context.BoxedLoopsSet.insert(L); |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 463 | } |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 464 | |
| 465 | return (AllowNonAffineSubLoops || Context.BoxedLoopsSet.empty()); |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 468 | bool ScopDetection::onlyValidRequiredInvariantLoads( |
| 469 | InvariantLoadsSetTy &RequiredILS, DetectionContext &Context) const { |
| 470 | Region &CurRegion = Context.CurRegion; |
Tobias Grosser | 7b5a4df | 2017-04-11 04:59:13 +0000 | [diff] [blame] | 471 | const DataLayout &DL = CurRegion.getEntry()->getModule()->getDataLayout(); |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 472 | |
Tobias Grosser | 8fa3e4c | 2016-02-26 16:43:35 +0000 | [diff] [blame] | 473 | if (!PollyInvariantLoadHoisting && !RequiredILS.empty()) |
| 474 | return false; |
| 475 | |
Tobias Grosser | 1c787e0 | 2017-03-02 12:15:37 +0000 | [diff] [blame] | 476 | for (LoadInst *Load : RequiredILS) { |
Tobias Grosser | 3f25a7e | 2017-05-04 10:16:20 +0000 | [diff] [blame] | 477 | // If we already know a load has been accepted as required invariant, we |
| 478 | // already run the validation below once and consequently don't need to |
| 479 | // run it again. Hence, we return early. For certain test cases (e.g., |
| 480 | // COSMO this avoids us spending 50% of scop-detection time in this |
| 481 | // very function (and its children). |
| 482 | if (Context.RequiredILS.count(Load)) |
| 483 | continue; |
| 484 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 485 | if (!isHoistableLoad(Load, CurRegion, LI, SE, DT)) |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 486 | return false; |
| 487 | |
Tobias Grosser | 8bd7f3c | 2017-03-09 11:36:00 +0000 | [diff] [blame] | 488 | for (auto NonAffineRegion : Context.NonAffineSubRegionSet) { |
Tobias Grosser | 8bd7f3c | 2017-03-09 11:36:00 +0000 | [diff] [blame] | 489 | if (isSafeToLoadUnconditionally(Load->getPointerOperand(), |
| 490 | Load->getAlignment(), DL)) |
| 491 | continue; |
| 492 | |
Tobias Grosser | 1c787e0 | 2017-03-02 12:15:37 +0000 | [diff] [blame] | 493 | if (NonAffineRegion->contains(Load) && |
| 494 | Load->getParent() != NonAffineRegion->getEntry()) |
| 495 | return false; |
Tobias Grosser | 8bd7f3c | 2017-03-09 11:36:00 +0000 | [diff] [blame] | 496 | } |
Tobias Grosser | 1c787e0 | 2017-03-02 12:15:37 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 499 | Context.RequiredILS.insert(RequiredILS.begin(), RequiredILS.end()); |
| 500 | |
| 501 | return true; |
| 502 | } |
| 503 | |
Johannes Doerfert | a94ae1a | 2016-12-02 17:49:52 +0000 | [diff] [blame] | 504 | bool ScopDetection::involvesMultiplePtrs(const SCEV *S0, const SCEV *S1, |
| 505 | Loop *Scope) const { |
| 506 | SetVector<Value *> Values; |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 507 | findValues(S0, SE, Values); |
Johannes Doerfert | a94ae1a | 2016-12-02 17:49:52 +0000 | [diff] [blame] | 508 | if (S1) |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 509 | findValues(S1, SE, Values); |
Johannes Doerfert | a94ae1a | 2016-12-02 17:49:52 +0000 | [diff] [blame] | 510 | |
| 511 | SmallPtrSet<Value *, 8> PtrVals; |
| 512 | for (auto *V : Values) { |
| 513 | if (auto *P2I = dyn_cast<PtrToIntInst>(V)) |
| 514 | V = P2I->getOperand(0); |
| 515 | |
| 516 | if (!V->getType()->isPointerTy()) |
| 517 | continue; |
| 518 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 519 | auto *PtrSCEV = SE.getSCEVAtScope(V, Scope); |
Johannes Doerfert | a94ae1a | 2016-12-02 17:49:52 +0000 | [diff] [blame] | 520 | if (isa<SCEVConstant>(PtrSCEV)) |
| 521 | continue; |
| 522 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 523 | auto *BasePtr = dyn_cast<SCEVUnknown>(SE.getPointerBase(PtrSCEV)); |
Johannes Doerfert | a94ae1a | 2016-12-02 17:49:52 +0000 | [diff] [blame] | 524 | if (!BasePtr) |
| 525 | return true; |
| 526 | |
| 527 | auto *BasePtrVal = BasePtr->getValue(); |
| 528 | if (PtrVals.insert(BasePtrVal).second) { |
| 529 | for (auto *PtrVal : PtrVals) |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 530 | if (PtrVal != BasePtrVal && !AA.isNoAlias(PtrVal, BasePtrVal)) |
Johannes Doerfert | a94ae1a | 2016-12-02 17:49:52 +0000 | [diff] [blame] | 531 | return true; |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | return false; |
| 536 | } |
| 537 | |
Michael Kruse | 09eb445 | 2016-03-03 22:10:47 +0000 | [diff] [blame] | 538 | bool ScopDetection::isAffine(const SCEV *S, Loop *Scope, |
Johannes Doerfert | ec8a217 | 2016-04-25 13:32:36 +0000 | [diff] [blame] | 539 | DetectionContext &Context) const { |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 540 | InvariantLoadsSetTy AccessILS; |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 541 | if (!isAffineExpr(&Context.CurRegion, Scope, S, SE, &AccessILS)) |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 542 | return false; |
| 543 | |
| 544 | if (!onlyValidRequiredInvariantLoads(AccessILS, Context)) |
| 545 | return false; |
| 546 | |
| 547 | return true; |
| 548 | } |
| 549 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 550 | bool ScopDetection::isValidSwitch(BasicBlock &BB, SwitchInst *SI, |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 551 | Value *Condition, bool IsLoopBranch, |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 552 | DetectionContext &Context) const { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 553 | Loop *L = LI.getLoopFor(&BB); |
| 554 | const SCEV *ConditionSCEV = SE.getSCEVAtScope(Condition, L); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 555 | |
Tobias Grosser | bbaeda3 | 2016-11-10 05:20:29 +0000 | [diff] [blame] | 556 | if (IsLoopBranch && L->isLoopLatch(&BB)) |
| 557 | return false; |
| 558 | |
Johannes Doerfert | a94ae1a | 2016-12-02 17:49:52 +0000 | [diff] [blame] | 559 | // Check for invalid usage of different pointers in one expression. |
| 560 | if (involvesMultiplePtrs(ConditionSCEV, nullptr, L)) |
| 561 | return false; |
| 562 | |
Michael Kruse | 09eb445 | 2016-03-03 22:10:47 +0000 | [diff] [blame] | 563 | if (isAffine(ConditionSCEV, L, Context)) |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 564 | return true; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 565 | |
Tobias Grosser | bbaeda3 | 2016-11-10 05:20:29 +0000 | [diff] [blame] | 566 | if (AllowNonAffineSubRegions && |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 567 | addOverApproximatedRegion(RI.getRegionFor(&BB), Context)) |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 568 | return true; |
| 569 | |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 570 | return invalid<ReportNonAffBranch>(Context, /*Assert=*/true, &BB, |
| 571 | ConditionSCEV, ConditionSCEV, SI); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | bool ScopDetection::isValidBranch(BasicBlock &BB, BranchInst *BI, |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 575 | Value *Condition, bool IsLoopBranch, |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 576 | DetectionContext &Context) const { |
Tobias Grosser | bbaeda3 | 2016-11-10 05:20:29 +0000 | [diff] [blame] | 577 | // Constant integer conditions are always affine. |
| 578 | if (isa<ConstantInt>(Condition)) |
| 579 | return true; |
| 580 | |
Johannes Doerfert | 9b1f9c8 | 2015-10-11 13:21:03 +0000 | [diff] [blame] | 581 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Condition)) { |
| 582 | auto Opcode = BinOp->getOpcode(); |
| 583 | if (Opcode == Instruction::And || Opcode == Instruction::Or) { |
| 584 | Value *Op0 = BinOp->getOperand(0); |
| 585 | Value *Op1 = BinOp->getOperand(1); |
| 586 | return isValidBranch(BB, BI, Op0, IsLoopBranch, Context) && |
| 587 | isValidBranch(BB, BI, Op1, IsLoopBranch, Context); |
| 588 | } |
| 589 | } |
| 590 | |
Tobias Grosser | 0a62b2d | 2017-09-25 16:37:15 +0000 | [diff] [blame] | 591 | if (auto PHI = dyn_cast<PHINode>(Condition)) { |
| 592 | auto *Unique = dyn_cast_or_null<ConstantInt>( |
| 593 | getUniqueNonErrorValue(PHI, &Context.CurRegion, LI, DT)); |
| 594 | if (Unique && (Unique->isZero() || Unique->isOne())) |
| 595 | return true; |
| 596 | } |
| 597 | |
Tobias Grosser | 5e531df | 2017-09-25 20:27:15 +0000 | [diff] [blame] | 598 | if (auto Load = dyn_cast<LoadInst>(Condition)) |
Michael Kruse | c013399 | 2017-10-01 22:19:28 +0000 | [diff] [blame^] | 599 | if (!IsLoopBranch && Context.CurRegion.contains(Load)) { |
Tobias Grosser | 5e531df | 2017-09-25 20:27:15 +0000 | [diff] [blame] | 600 | Context.RequiredILS.insert(Load); |
| 601 | return true; |
| 602 | } |
| 603 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 604 | // Non constant conditions of branches need to be ICmpInst. |
| 605 | if (!isa<ICmpInst>(Condition)) { |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 606 | if (!IsLoopBranch && AllowNonAffineSubRegions && |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 607 | addOverApproximatedRegion(RI.getRegionFor(&BB), Context)) |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 608 | return true; |
| 609 | return invalid<ReportInvalidCond>(Context, /*Assert=*/true, BI, &BB); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 610 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 611 | |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 612 | ICmpInst *ICmp = cast<ICmpInst>(Condition); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 613 | |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 614 | // Are both operands of the ICmp affine? |
| 615 | if (isa<UndefValue>(ICmp->getOperand(0)) || |
| 616 | isa<UndefValue>(ICmp->getOperand(1))) |
| 617 | return invalid<ReportUndefOperand>(Context, /*Assert=*/true, &BB, ICmp); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 618 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 619 | Loop *L = LI.getLoopFor(&BB); |
| 620 | const SCEV *LHS = SE.getSCEVAtScope(ICmp->getOperand(0), L); |
| 621 | const SCEV *RHS = SE.getSCEVAtScope(ICmp->getOperand(1), L); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 622 | |
Tobias Grosser | ee45759 | 2017-09-24 09:25:30 +0000 | [diff] [blame] | 623 | LHS = tryForwardThroughPHI(LHS, Context.CurRegion, SE, LI, DT); |
| 624 | RHS = tryForwardThroughPHI(RHS, Context.CurRegion, SE, LI, DT); |
| 625 | |
Johannes Doerfert | bda8143 | 2016-12-02 17:55:41 +0000 | [diff] [blame] | 626 | // If unsigned operations are not allowed try to approximate the region. |
| 627 | if (ICmp->isUnsigned() && !PollyAllowUnsignedOperations) |
| 628 | return !IsLoopBranch && AllowNonAffineSubRegions && |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 629 | addOverApproximatedRegion(RI.getRegionFor(&BB), Context); |
Johannes Doerfert | bda8143 | 2016-12-02 17:55:41 +0000 | [diff] [blame] | 630 | |
Johannes Doerfert | a94ae1a | 2016-12-02 17:49:52 +0000 | [diff] [blame] | 631 | // Check for invalid usage of different pointers in one expression. |
| 632 | if (ICmp->isEquality() && involvesMultiplePtrs(LHS, nullptr, L) && |
| 633 | involvesMultiplePtrs(RHS, nullptr, L)) |
| 634 | return false; |
| 635 | |
| 636 | // Check for invalid usage of different pointers in a relational comparison. |
| 637 | if (ICmp->isRelational() && involvesMultiplePtrs(LHS, RHS, L)) |
| 638 | return false; |
| 639 | |
Michael Kruse | 09eb445 | 2016-03-03 22:10:47 +0000 | [diff] [blame] | 640 | if (isAffine(LHS, L, Context) && isAffine(RHS, L, Context)) |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 641 | return true; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 642 | |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 643 | if (!IsLoopBranch && AllowNonAffineSubRegions && |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 644 | addOverApproximatedRegion(RI.getRegionFor(&BB), Context)) |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 645 | return true; |
| 646 | |
| 647 | if (IsLoopBranch) |
| 648 | return false; |
| 649 | |
| 650 | return invalid<ReportNonAffBranch>(Context, /*Assert=*/true, &BB, LHS, RHS, |
| 651 | ICmp); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 654 | bool ScopDetection::isValidCFG(BasicBlock &BB, bool IsLoopBranch, |
Tobias Grosser | b76cd3c | 2015-11-11 08:42:20 +0000 | [diff] [blame] | 655 | bool AllowUnreachable, |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 656 | DetectionContext &Context) const { |
| 657 | Region &CurRegion = Context.CurRegion; |
| 658 | |
| 659 | TerminatorInst *TI = BB.getTerminator(); |
| 660 | |
Tobias Grosser | b76cd3c | 2015-11-11 08:42:20 +0000 | [diff] [blame] | 661 | if (AllowUnreachable && isa<UnreachableInst>(TI)) |
| 662 | return true; |
| 663 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 664 | // Return instructions are only valid if the region is the top level region. |
Philip Pfaffe | 1a0128f | 2017-05-24 18:39:39 +0000 | [diff] [blame] | 665 | if (isa<ReturnInst>(TI) && CurRegion.isTopLevelRegion()) |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 666 | return true; |
| 667 | |
| 668 | Value *Condition = getConditionFromTerminator(TI); |
| 669 | |
| 670 | if (!Condition) |
| 671 | return invalid<ReportInvalidTerminator>(Context, /*Assert=*/true, &BB); |
| 672 | |
| 673 | // UndefValue is not allowed as condition. |
| 674 | if (isa<UndefValue>(Condition)) |
| 675 | return invalid<ReportUndefCond>(Context, /*Assert=*/true, TI, &BB); |
| 676 | |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 677 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 678 | return isValidBranch(BB, BI, Condition, IsLoopBranch, Context); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 679 | |
| 680 | SwitchInst *SI = dyn_cast<SwitchInst>(TI); |
| 681 | assert(SI && "Terminator was neither branch nor switch"); |
| 682 | |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 683 | return isValidSwitch(BB, SI, Condition, IsLoopBranch, Context); |
Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 686 | bool ScopDetection::isValidCallInst(CallInst &CI, |
| 687 | DetectionContext &Context) const { |
Johannes Doerfert | 3f500fa | 2015-01-25 18:07:30 +0000 | [diff] [blame] | 688 | if (CI.doesNotReturn()) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 689 | return false; |
| 690 | |
| 691 | if (CI.doesNotAccessMemory()) |
| 692 | return true; |
| 693 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 694 | if (auto *II = dyn_cast<IntrinsicInst>(&CI)) |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 695 | if (isValidIntrinsicInst(*II, Context)) |
| 696 | return true; |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 697 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 698 | Function *CalledFunction = CI.getCalledFunction(); |
| 699 | |
| 700 | // Indirect calls are not supported. |
Tobias Grosser | 8dd653d | 2016-06-22 16:22:00 +0000 | [diff] [blame] | 701 | if (CalledFunction == nullptr) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 702 | return false; |
| 703 | |
Tobias Grosser | 898a636 | 2016-03-23 06:40:15 +0000 | [diff] [blame] | 704 | if (AllowModrefCall) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 705 | switch (AA.getModRefBehavior(CalledFunction)) { |
Tobias Grosser | b94e9b3 | 2016-11-21 09:04:45 +0000 | [diff] [blame] | 706 | case FMRB_UnknownModRefBehavior: |
Tobias Grosser | 898a636 | 2016-03-23 06:40:15 +0000 | [diff] [blame] | 707 | return false; |
Tobias Grosser | b94e9b3 | 2016-11-21 09:04:45 +0000 | [diff] [blame] | 708 | case FMRB_DoesNotAccessMemory: |
| 709 | case FMRB_OnlyReadsMemory: |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 710 | // Implicitly disable delinearization since we have an unknown |
| 711 | // accesses with an unknown access function. |
| 712 | Context.HasUnknownAccess = true; |
Tobias Grosser | 898a636 | 2016-03-23 06:40:15 +0000 | [diff] [blame] | 713 | Context.AST.add(&CI); |
| 714 | return true; |
Tobias Grosser | b94e9b3 | 2016-11-21 09:04:45 +0000 | [diff] [blame] | 715 | case FMRB_OnlyReadsArgumentPointees: |
| 716 | case FMRB_OnlyAccessesArgumentPointees: |
Tobias Grosser | 898a636 | 2016-03-23 06:40:15 +0000 | [diff] [blame] | 717 | for (const auto &Arg : CI.arg_operands()) { |
| 718 | if (!Arg->getType()->isPointerTy()) |
| 719 | continue; |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 720 | |
Tobias Grosser | 898a636 | 2016-03-23 06:40:15 +0000 | [diff] [blame] | 721 | // Bail if a pointer argument has a base address not known to |
| 722 | // ScalarEvolution. Note that a zero pointer is acceptable. |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 723 | auto *ArgSCEV = SE.getSCEVAtScope(Arg, LI.getLoopFor(CI.getParent())); |
Tobias Grosser | 898a636 | 2016-03-23 06:40:15 +0000 | [diff] [blame] | 724 | if (ArgSCEV->isZero()) |
| 725 | continue; |
| 726 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 727 | auto *BP = dyn_cast<SCEVUnknown>(SE.getPointerBase(ArgSCEV)); |
Tobias Grosser | 898a636 | 2016-03-23 06:40:15 +0000 | [diff] [blame] | 728 | if (!BP) |
| 729 | return false; |
| 730 | |
| 731 | // Implicitly disable delinearization since we have an unknown |
| 732 | // accesses with an unknown access function. |
| 733 | Context.HasUnknownAccess = true; |
| 734 | } |
| 735 | |
| 736 | Context.AST.add(&CI); |
| 737 | return true; |
Weiming Zhao | 7614e17 | 2016-07-11 18:27:52 +0000 | [diff] [blame] | 738 | case FMRB_DoesNotReadMemory: |
Tobias Grosser | 70d2709 | 2016-11-13 19:27:24 +0000 | [diff] [blame] | 739 | case FMRB_OnlyAccessesInaccessibleMem: |
| 740 | case FMRB_OnlyAccessesInaccessibleOrArgMem: |
Weiming Zhao | 7614e17 | 2016-07-11 18:27:52 +0000 | [diff] [blame] | 741 | return false; |
Tobias Grosser | 898a636 | 2016-03-23 06:40:15 +0000 | [diff] [blame] | 742 | } |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 745 | return false; |
| 746 | } |
| 747 | |
| 748 | bool ScopDetection::isValidIntrinsicInst(IntrinsicInst &II, |
| 749 | DetectionContext &Context) const { |
| 750 | if (isIgnoredIntrinsic(&II)) |
Tobias Grosser | 9c0ffe3 | 2015-08-30 16:57:15 +0000 | [diff] [blame] | 751 | return true; |
Johannes Doerfert | 3f500fa | 2015-01-25 18:07:30 +0000 | [diff] [blame] | 752 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 753 | // The closest loop surrounding the call instruction. |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 754 | Loop *L = LI.getLoopFor(II.getParent()); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 755 | |
| 756 | // The access function and base pointer for memory intrinsics. |
| 757 | const SCEV *AF; |
| 758 | const SCEVUnknown *BP; |
| 759 | |
| 760 | switch (II.getIntrinsicID()) { |
| 761 | // Memory intrinsics that can be represented are supported. |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 762 | case Intrinsic::memmove: |
| 763 | case Intrinsic::memcpy: |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 764 | AF = SE.getSCEVAtScope(cast<MemTransferInst>(II).getSource(), L); |
Johannes Doerfert | 733ea34 | 2016-03-24 13:50:04 +0000 | [diff] [blame] | 765 | if (!AF->isZero()) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 766 | BP = dyn_cast<SCEVUnknown>(SE.getPointerBase(AF)); |
Johannes Doerfert | 733ea34 | 2016-03-24 13:50:04 +0000 | [diff] [blame] | 767 | // Bail if the source pointer is not valid. |
| 768 | if (!isValidAccess(&II, AF, BP, Context)) |
| 769 | return false; |
| 770 | } |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 771 | // Fall through |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 772 | case Intrinsic::memset: |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 773 | AF = SE.getSCEVAtScope(cast<MemIntrinsic>(II).getDest(), L); |
Johannes Doerfert | 733ea34 | 2016-03-24 13:50:04 +0000 | [diff] [blame] | 774 | if (!AF->isZero()) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 775 | BP = dyn_cast<SCEVUnknown>(SE.getPointerBase(AF)); |
Johannes Doerfert | 733ea34 | 2016-03-24 13:50:04 +0000 | [diff] [blame] | 776 | // Bail if the destination pointer is not valid. |
| 777 | if (!isValidAccess(&II, AF, BP, Context)) |
| 778 | return false; |
| 779 | } |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 780 | |
| 781 | // Bail if the length is not affine. |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 782 | if (!isAffine(SE.getSCEVAtScope(cast<MemIntrinsic>(II).getLength(), L), L, |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 783 | Context)) |
| 784 | return false; |
| 785 | |
| 786 | return true; |
| 787 | default: |
| 788 | break; |
| 789 | } |
| 790 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 791 | return false; |
| 792 | } |
| 793 | |
Michael Kruse | 5a4ec5c | 2017-03-07 20:28:43 +0000 | [diff] [blame] | 794 | bool ScopDetection::isInvariant(Value &Val, const Region &Reg, |
| 795 | DetectionContext &Ctx) const { |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 796 | // A reference to function argument or constant value is invariant. |
| 797 | if (isa<Argument>(Val) || isa<Constant>(Val)) |
| 798 | return true; |
| 799 | |
Michael Kruse | 5a4ec5c | 2017-03-07 20:28:43 +0000 | [diff] [blame] | 800 | Instruction *I = dyn_cast<Instruction>(&Val); |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 801 | if (!I) |
| 802 | return false; |
| 803 | |
| 804 | if (!Reg.contains(I)) |
| 805 | return true; |
| 806 | |
Michael Kruse | 5a4ec5c | 2017-03-07 20:28:43 +0000 | [diff] [blame] | 807 | // Loads within the SCoP may read arbitrary values, need to hoist them. If it |
| 808 | // is not hoistable, it will be rejected later, but here we assume it is and |
| 809 | // that makes the value invariant. |
| 810 | if (auto LI = dyn_cast<LoadInst>(I)) { |
| 811 | Ctx.RequiredILS.insert(LI); |
| 812 | return true; |
| 813 | } |
| 814 | |
Michael Kruse | 6744efa8d | 2017-03-08 15:14:46 +0000 | [diff] [blame] | 815 | return false; |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 818 | namespace { |
| 819 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 820 | /// Remove smax of smax(0, size) expressions from a SCEV expression and |
Tobias Grosser | 2f8e43d | 2015-11-24 17:06:38 +0000 | [diff] [blame] | 821 | /// register the '...' components. |
| 822 | /// |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 823 | /// Array access expressions as they are generated by GFortran contain smax(0, |
Tobias Grosser | 2f8e43d | 2015-11-24 17:06:38 +0000 | [diff] [blame] | 824 | /// size) expressions that confuse the 'normal' delinearization algorithm. |
| 825 | /// However, if we extract such expressions before the normal delinearization |
| 826 | /// takes place they can actually help to identify array size expressions in |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 827 | /// Fortran accesses. For the subsequently following delinearization the smax(0, |
Tobias Grosser | 2f8e43d | 2015-11-24 17:06:38 +0000 | [diff] [blame] | 828 | /// size) component can be replaced by just 'size'. This is correct as we will |
| 829 | /// always add and verify the assumption that for all subscript expressions |
| 830 | /// 'exp' the inequality 0 <= exp < size holds. Hence, we will also verify |
| 831 | /// that 0 <= size, which means smax(0, size) == size. |
Tobias Grosser | ebb626e | 2016-10-29 06:19:34 +0000 | [diff] [blame] | 832 | class SCEVRemoveMax : public SCEVRewriteVisitor<SCEVRemoveMax> { |
Tobias Grosser | 2f8e43d | 2015-11-24 17:06:38 +0000 | [diff] [blame] | 833 | public: |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 834 | SCEVRemoveMax(ScalarEvolution &SE, std::vector<const SCEV *> *Terms) |
| 835 | : SCEVRewriteVisitor(SE), Terms(Terms) {} |
| 836 | |
Tobias Grosser | ebb626e | 2016-10-29 06:19:34 +0000 | [diff] [blame] | 837 | static const SCEV *rewrite(const SCEV *Scev, ScalarEvolution &SE, |
| 838 | std::vector<const SCEV *> *Terms = nullptr) { |
| 839 | SCEVRemoveMax Rewriter(SE, Terms); |
| 840 | return Rewriter.visit(Scev); |
Tobias Grosser | 2f8e43d | 2015-11-24 17:06:38 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Tobias Grosser | 2f8e43d | 2015-11-24 17:06:38 +0000 | [diff] [blame] | 843 | const SCEV *visitSMaxExpr(const SCEVSMaxExpr *Expr) { |
Michael Kruse | 8fc2896 | 2015-12-20 14:42:32 +0000 | [diff] [blame] | 844 | if ((Expr->getNumOperands() == 2) && Expr->getOperand(0)->isZero()) { |
Tobias Grosser | 2f8e43d | 2015-11-24 17:06:38 +0000 | [diff] [blame] | 845 | auto Res = visit(Expr->getOperand(1)); |
| 846 | if (Terms) |
| 847 | (*Terms).push_back(Res); |
| 848 | return Res; |
| 849 | } |
| 850 | |
| 851 | return Expr; |
| 852 | } |
| 853 | |
Tobias Grosser | 2f8e43d | 2015-11-24 17:06:38 +0000 | [diff] [blame] | 854 | private: |
Tobias Grosser | 2f8e43d | 2015-11-24 17:06:38 +0000 | [diff] [blame] | 855 | std::vector<const SCEV *> *Terms; |
| 856 | }; |
| 857 | |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 858 | } // namespace |
| 859 | |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 860 | SmallVector<const SCEV *, 4> |
| 861 | ScopDetection::getDelinearizationTerms(DetectionContext &Context, |
| 862 | const SCEVUnknown *BasePointer) const { |
| 863 | SmallVector<const SCEV *, 4> Terms; |
| 864 | for (const auto &Pair : Context.Accesses[BasePointer]) { |
Tobias Grosser | 2f8e43d | 2015-11-24 17:06:38 +0000 | [diff] [blame] | 865 | std::vector<const SCEV *> MaxTerms; |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 866 | SCEVRemoveMax::rewrite(Pair.second, SE, &MaxTerms); |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 867 | if (!MaxTerms.empty()) { |
Tobias Grosser | 2f8e43d | 2015-11-24 17:06:38 +0000 | [diff] [blame] | 868 | Terms.insert(Terms.begin(), MaxTerms.begin(), MaxTerms.end()); |
| 869 | continue; |
| 870 | } |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 871 | // In case the outermost expression is a plain add, we check if any of its |
| 872 | // terms has the form 4 * %inst * %param * %param ..., aka a term that |
| 873 | // contains a product between a parameter and an instruction that is |
| 874 | // inside the scop. Such instructions, if allowed at all, are instructions |
| 875 | // SCEV can not represent, but Polly is still looking through. As a |
| 876 | // result, these instructions can depend on induction variables and are |
| 877 | // most likely no array sizes. However, terms that are multiplied with |
| 878 | // them are likely candidates for array sizes. |
| 879 | if (auto *AF = dyn_cast<SCEVAddExpr>(Pair.second)) { |
| 880 | for (auto Op : AF->operands()) { |
| 881 | if (auto *AF2 = dyn_cast<SCEVAddRecExpr>(Op)) |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 882 | SE.collectParametricTerms(AF2, Terms); |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 883 | if (auto *AF2 = dyn_cast<SCEVMulExpr>(Op)) { |
| 884 | SmallVector<const SCEV *, 0> Operands; |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 885 | |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 886 | for (auto *MulOp : AF2->operands()) { |
| 887 | if (auto *Const = dyn_cast<SCEVConstant>(MulOp)) |
| 888 | Operands.push_back(Const); |
| 889 | if (auto *Unknown = dyn_cast<SCEVUnknown>(MulOp)) { |
| 890 | if (auto *Inst = dyn_cast<Instruction>(Unknown->getValue())) { |
| 891 | if (!Context.CurRegion.contains(Inst)) |
Tobias Grosser | 1b13dde | 2015-06-29 14:44:22 +0000 | [diff] [blame] | 892 | Operands.push_back(MulOp); |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 893 | |
| 894 | } else { |
| 895 | Operands.push_back(MulOp); |
Tobias Grosser | 1b13dde | 2015-06-29 14:44:22 +0000 | [diff] [blame] | 896 | } |
| 897 | } |
Tobias Grosser | 1b13dde | 2015-06-29 14:44:22 +0000 | [diff] [blame] | 898 | } |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 899 | if (Operands.size()) |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 900 | Terms.push_back(SE.getMulExpr(Operands)); |
Tobias Grosser | 1b13dde | 2015-06-29 14:44:22 +0000 | [diff] [blame] | 901 | } |
| 902 | } |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 903 | } |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 904 | if (Terms.empty()) |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 905 | SE.collectParametricTerms(Pair.second, Terms); |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 906 | } |
| 907 | return Terms; |
| 908 | } |
Sebastian Pop | e8863b8 | 2014-05-12 19:02:02 +0000 | [diff] [blame] | 909 | |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 910 | bool ScopDetection::hasValidArraySizes(DetectionContext &Context, |
| 911 | SmallVectorImpl<const SCEV *> &Sizes, |
Michael Kruse | c7e0d9c | 2016-03-01 21:44:06 +0000 | [diff] [blame] | 912 | const SCEVUnknown *BasePointer, |
| 913 | Loop *Scope) const { |
Tobias Grosser | 1e55db3 | 2017-05-27 15:18:53 +0000 | [diff] [blame] | 914 | // If no sizes were found, all sizes are trivially valid. We allow this case |
| 915 | // to make it possible to pass known-affine accesses to the delinearization to |
| 916 | // try to recover some interesting multi-dimensional accesses, but to still |
| 917 | // allow the already known to be affine access in case the delinearization |
| 918 | // fails. In such situations, the delinearization will just return a Sizes |
| 919 | // array of size zero. |
| 920 | if (Sizes.size() == 0) |
| 921 | return true; |
| 922 | |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 923 | Value *BaseValue = BasePointer->getValue(); |
| 924 | Region &CurRegion = Context.CurRegion; |
| 925 | for (const SCEV *DelinearizedSize : Sizes) { |
Johannes Doerfert | ec8a217 | 2016-04-25 13:32:36 +0000 | [diff] [blame] | 926 | if (!isAffine(DelinearizedSize, Scope, Context)) { |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 927 | Sizes.clear(); |
| 928 | break; |
Tobias Grosser | 5528dcd | 2015-10-25 08:40:38 +0000 | [diff] [blame] | 929 | } |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 930 | if (auto *Unknown = dyn_cast<SCEVUnknown>(DelinearizedSize)) { |
| 931 | auto *V = dyn_cast<Value>(Unknown->getValue()); |
| 932 | if (auto *Load = dyn_cast<LoadInst>(V)) { |
| 933 | if (Context.CurRegion.contains(Load) && |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 934 | isHoistableLoad(Load, CurRegion, LI, SE, DT)) |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 935 | Context.RequiredILS.insert(Load); |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 936 | continue; |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 937 | } |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 938 | } |
Siddharth Bhat | a1b2086 | 2017-07-13 12:18:56 +0000 | [diff] [blame] | 939 | if (hasScalarDepsInsideRegion(DelinearizedSize, &CurRegion, Scope, false, |
| 940 | Context.RequiredILS)) |
Tobias Grosser | bfaf1ae | 2015-12-21 09:09:39 +0000 | [diff] [blame] | 941 | return invalid<ReportNonAffineAccess>( |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 942 | Context, /*Assert=*/true, DelinearizedSize, |
| 943 | Context.Accesses[BasePointer].front().first, BaseValue); |
| 944 | } |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 945 | |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 946 | // No array shape derived. |
| 947 | if (Sizes.empty()) { |
| 948 | if (AllowNonAffine) |
| 949 | return true; |
| 950 | |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 951 | for (const auto &Pair : Context.Accesses[BasePointer]) { |
| 952 | const Instruction *Insn = Pair.first; |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 953 | const SCEV *AF = Pair.second; |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 954 | |
Johannes Doerfert | ec8a217 | 2016-04-25 13:32:36 +0000 | [diff] [blame] | 955 | if (!isAffine(AF, Scope, Context)) { |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 956 | invalid<ReportNonAffineAccess>(Context, /*Assert=*/true, AF, Insn, |
| 957 | BaseValue); |
| 958 | if (!KeepGoing) |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 959 | return false; |
| 960 | } |
| 961 | } |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 962 | return false; |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 963 | } |
Sebastian Pop | e8863b8 | 2014-05-12 19:02:02 +0000 | [diff] [blame] | 964 | return true; |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 967 | // We first store the resulting memory accesses in TempMemoryAccesses. Only |
| 968 | // if the access functions for all memory accesses have been successfully |
| 969 | // delinearized we continue. Otherwise, we either report a failure or, if |
| 970 | // non-affine accesses are allowed, we drop the information. In case the |
| 971 | // information is dropped the memory accesses need to be overapproximated |
| 972 | // when translated to a polyhedral representation. |
| 973 | bool ScopDetection::computeAccessFunctions( |
| 974 | DetectionContext &Context, const SCEVUnknown *BasePointer, |
| 975 | std::shared_ptr<ArrayShape> Shape) const { |
| 976 | Value *BaseValue = BasePointer->getValue(); |
| 977 | bool BasePtrHasNonAffine = false; |
| 978 | MapInsnToMemAcc TempMemoryAccesses; |
| 979 | for (const auto &Pair : Context.Accesses[BasePointer]) { |
| 980 | const Instruction *Insn = Pair.first; |
| 981 | auto *AF = Pair.second; |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 982 | AF = SCEVRemoveMax::rewrite(AF, SE); |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 983 | bool IsNonAffine = false; |
| 984 | TempMemoryAccesses.insert(std::make_pair(Insn, MemAcc(Insn, Shape))); |
| 985 | MemAcc *Acc = &TempMemoryAccesses.find(Insn)->second; |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 986 | auto *Scope = LI.getLoopFor(Insn->getParent()); |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 987 | |
| 988 | if (!AF) { |
Johannes Doerfert | ec8a217 | 2016-04-25 13:32:36 +0000 | [diff] [blame] | 989 | if (isAffine(Pair.second, Scope, Context)) |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 990 | Acc->DelinearizedSubscripts.push_back(Pair.second); |
| 991 | else |
| 992 | IsNonAffine = true; |
| 993 | } else { |
Tobias Grosser | 1e55db3 | 2017-05-27 15:18:53 +0000 | [diff] [blame] | 994 | if (Shape->DelinearizedSizes.size() == 0) { |
| 995 | Acc->DelinearizedSubscripts.push_back(AF); |
| 996 | } else { |
| 997 | SE.computeAccessFunctions(AF, Acc->DelinearizedSubscripts, |
| 998 | Shape->DelinearizedSizes); |
| 999 | if (Acc->DelinearizedSubscripts.size() == 0) |
| 1000 | IsNonAffine = true; |
| 1001 | } |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 1002 | for (const SCEV *S : Acc->DelinearizedSubscripts) |
Johannes Doerfert | ec8a217 | 2016-04-25 13:32:36 +0000 | [diff] [blame] | 1003 | if (!isAffine(S, Scope, Context)) |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 1004 | IsNonAffine = true; |
| 1005 | } |
| 1006 | |
| 1007 | // (Possibly) report non affine access |
| 1008 | if (IsNonAffine) { |
| 1009 | BasePtrHasNonAffine = true; |
| 1010 | if (!AllowNonAffine) |
| 1011 | invalid<ReportNonAffineAccess>(Context, /*Assert=*/true, Pair.second, |
| 1012 | Insn, BaseValue); |
| 1013 | if (!KeepGoing && !AllowNonAffine) |
| 1014 | return false; |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | if (!BasePtrHasNonAffine) |
Hongbin Zheng | 2262320 | 2016-02-15 00:20:58 +0000 | [diff] [blame] | 1019 | Context.InsnToMemAcc.insert(TempMemoryAccesses.begin(), |
| 1020 | TempMemoryAccesses.end()); |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 1021 | |
| 1022 | return true; |
| 1023 | } |
| 1024 | |
Michael Kruse | c7e0d9c | 2016-03-01 21:44:06 +0000 | [diff] [blame] | 1025 | bool ScopDetection::hasBaseAffineAccesses(DetectionContext &Context, |
| 1026 | const SCEVUnknown *BasePointer, |
| 1027 | Loop *Scope) const { |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 1028 | auto Shape = std::shared_ptr<ArrayShape>(new ArrayShape(BasePointer)); |
| 1029 | |
| 1030 | auto Terms = getDelinearizationTerms(Context, BasePointer); |
| 1031 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1032 | SE.findArrayDimensions(Terms, Shape->DelinearizedSizes, |
| 1033 | Context.ElementSize[BasePointer]); |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 1034 | |
Michael Kruse | c7e0d9c | 2016-03-01 21:44:06 +0000 | [diff] [blame] | 1035 | if (!hasValidArraySizes(Context, Shape->DelinearizedSizes, BasePointer, |
| 1036 | Scope)) |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 1037 | return false; |
| 1038 | |
| 1039 | return computeAccessFunctions(Context, BasePointer, Shape); |
| 1040 | } |
| 1041 | |
| 1042 | bool ScopDetection::hasAffineMemoryAccesses(DetectionContext &Context) const { |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 1043 | // TODO: If we have an unknown access and other non-affine accesses we do |
| 1044 | // not try to delinearize them for now. |
| 1045 | if (Context.HasUnknownAccess && !Context.NonAffineAccesses.empty()) |
| 1046 | return AllowNonAffine; |
| 1047 | |
Michael Kruse | c7e0d9c | 2016-03-01 21:44:06 +0000 | [diff] [blame] | 1048 | for (auto &Pair : Context.NonAffineAccesses) { |
| 1049 | auto *BasePointer = Pair.first; |
| 1050 | auto *Scope = Pair.second; |
| 1051 | if (!hasBaseAffineAccesses(Context, BasePointer, Scope)) { |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 1052 | if (KeepGoing) |
| 1053 | continue; |
| 1054 | else |
| 1055 | return false; |
| 1056 | } |
Michael Kruse | c7e0d9c | 2016-03-01 21:44:06 +0000 | [diff] [blame] | 1057 | } |
Tobias Grosser | d68ba42 | 2015-11-24 05:00:36 +0000 | [diff] [blame] | 1058 | return true; |
| 1059 | } |
| 1060 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1061 | bool ScopDetection::isValidAccess(Instruction *Inst, const SCEV *AF, |
| 1062 | const SCEVUnknown *BP, |
| 1063 | DetectionContext &Context) const { |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 1064 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1065 | if (!BP) |
Michael Kruse | 70131d3 | 2016-01-27 17:09:17 +0000 | [diff] [blame] | 1066 | return invalid<ReportNoBasePtr>(Context, /*Assert=*/true, Inst); |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 1067 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1068 | auto *BV = BP->getValue(); |
| 1069 | if (isa<UndefValue>(BV)) |
Michael Kruse | 70131d3 | 2016-01-27 17:09:17 +0000 | [diff] [blame] | 1070 | return invalid<ReportUndefBasePtr>(Context, /*Assert=*/true, Inst); |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 1071 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1072 | // FIXME: Think about allowing IntToPtrInst |
| 1073 | if (IntToPtrInst *Inst = dyn_cast<IntToPtrInst>(BV)) |
| 1074 | return invalid<ReportIntToPtr>(Context, /*Assert=*/true, Inst); |
| 1075 | |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 1076 | // Check that the base address of the access is invariant in the current |
| 1077 | // region. |
Michael Kruse | 5a4ec5c | 2017-03-07 20:28:43 +0000 | [diff] [blame] | 1078 | if (!isInvariant(*BV, Context.CurRegion, Context)) |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1079 | return invalid<ReportVariantBasePtr>(Context, /*Assert=*/true, BV, Inst); |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 1080 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1081 | AF = SE.getMinusSCEV(AF, BP); |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 1082 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1083 | const SCEV *Size; |
| 1084 | if (!isa<MemIntrinsic>(Inst)) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1085 | Size = SE.getElementSize(Inst); |
Tobias Grosser | 8ebdc2d | 2016-02-07 08:48:57 +0000 | [diff] [blame] | 1086 | } else { |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1087 | auto *SizeTy = |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1088 | SE.getEffectiveSCEVType(PointerType::getInt8PtrTy(SE.getContext())); |
| 1089 | Size = SE.getConstant(SizeTy, 8); |
Tobias Grosser | 8ebdc2d | 2016-02-07 08:48:57 +0000 | [diff] [blame] | 1090 | } |
Tobias Grosser | bcd4eff | 2014-09-13 14:47:40 +0000 | [diff] [blame] | 1091 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1092 | if (Context.ElementSize[BP]) { |
| 1093 | if (!AllowDifferentTypes && Context.ElementSize[BP] != Size) |
| 1094 | return invalid<ReportDifferentArrayElementSize>(Context, /*Assert=*/true, |
| 1095 | Inst, BV); |
| 1096 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1097 | Context.ElementSize[BP] = SE.getSMinExpr(Size, Context.ElementSize[BP]); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1098 | } else { |
| 1099 | Context.ElementSize[BP] = Size; |
| 1100 | } |
| 1101 | |
| 1102 | bool IsVariantInNonAffineLoop = false; |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 1103 | SetVector<const Loop *> Loops; |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1104 | findLoops(AF, Loops); |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 1105 | for (const Loop *L : Loops) |
| 1106 | if (Context.BoxedLoopsSet.count(L)) |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1107 | IsVariantInNonAffineLoop = true; |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 1108 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1109 | auto *Scope = LI.getLoopFor(Inst->getParent()); |
Johannes Doerfert | ec8a217 | 2016-04-25 13:32:36 +0000 | [diff] [blame] | 1110 | bool IsAffine = !IsVariantInNonAffineLoop && isAffine(AF, Scope, Context); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1111 | // Do not try to delinearize memory intrinsics and force them to be affine. |
| 1112 | if (isa<MemIntrinsic>(Inst) && !IsAffine) { |
| 1113 | return invalid<ReportNonAffineAccess>(Context, /*Assert=*/true, AF, Inst, |
| 1114 | BV); |
| 1115 | } else if (PollyDelinearize && !IsVariantInNonAffineLoop) { |
| 1116 | Context.Accesses[BP].push_back({Inst, AF}); |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 1117 | |
Tobias Grosser | 1e55db3 | 2017-05-27 15:18:53 +0000 | [diff] [blame] | 1118 | if (!IsAffine || hasIVParams(AF)) |
Michael Kruse | c7e0d9c | 2016-03-01 21:44:06 +0000 | [diff] [blame] | 1119 | Context.NonAffineAccesses.insert( |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1120 | std::make_pair(BP, LI.getLoopFor(Inst->getParent()))); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1121 | } else if (!AllowNonAffine && !IsAffine) { |
| 1122 | return invalid<ReportNonAffineAccess>(Context, /*Assert=*/true, AF, Inst, |
| 1123 | BV); |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 1124 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1125 | |
Tobias Grosser | 1eedb67 | 2014-09-24 21:04:29 +0000 | [diff] [blame] | 1126 | if (IgnoreAliasing) |
Sebastian Pop | 8c2d753 | 2013-07-03 22:50:36 +0000 | [diff] [blame] | 1127 | return true; |
Tobias Grosser | fff5adc | 2011-11-10 13:21:43 +0000 | [diff] [blame] | 1128 | |
Sebastian Pop | 8c2d753 | 2013-07-03 22:50:36 +0000 | [diff] [blame] | 1129 | // Check if the base pointer of the memory access does alias with |
| 1130 | // any other pointer. This cannot be handled at the moment. |
Benjamin Kramer | ae81abf | 2014-10-05 11:58:57 +0000 | [diff] [blame] | 1131 | AAMDNodes AATags; |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1132 | Inst->getAAMetadata(AATags); |
Benjamin Kramer | ae81abf | 2014-10-05 11:58:57 +0000 | [diff] [blame] | 1133 | AliasSet &AS = Context.AST.getAliasSetForPointer( |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1134 | BP->getValue(), MemoryLocation::UnknownSize, AATags); |
Tobias Grosser | 428b3e4 | 2013-02-04 15:46:25 +0000 | [diff] [blame] | 1135 | |
Tobias Grosser | 1eedb67 | 2014-09-24 21:04:29 +0000 | [diff] [blame] | 1136 | if (!AS.isMustAlias()) { |
| 1137 | if (PollyUseRuntimeAliasChecks) { |
| 1138 | bool CanBuildRunTimeCheck = true; |
| 1139 | // The run-time alias check places code that involves the base pointer at |
| 1140 | // the beginning of the SCoP. This breaks if the base pointer is defined |
| 1141 | // inside the scop. Hence, we can only create a run-time check if we are |
| 1142 | // sure the base pointer is not an instruction defined inside the scop. |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 1143 | // However, we can ignore loads that will be hoisted. |
Tobias Grosser | 1eedb67 | 2014-09-24 21:04:29 +0000 | [diff] [blame] | 1144 | for (const auto &Ptr : AS) { |
| 1145 | Instruction *Inst = dyn_cast<Instruction>(Ptr.getValue()); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1146 | if (Inst && Context.CurRegion.contains(Inst)) { |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 1147 | auto *Load = dyn_cast<LoadInst>(Inst); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1148 | if (Load && isHoistableLoad(Load, Context.CurRegion, LI, SE, DT)) { |
Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 1149 | Context.RequiredILS.insert(Load); |
| 1150 | continue; |
| 1151 | } |
| 1152 | |
Tobias Grosser | 1eedb67 | 2014-09-24 21:04:29 +0000 | [diff] [blame] | 1153 | CanBuildRunTimeCheck = false; |
| 1154 | break; |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | if (CanBuildRunTimeCheck) |
| 1159 | return true; |
| 1160 | } |
Michael Kruse | 70131d3 | 2016-01-27 17:09:17 +0000 | [diff] [blame] | 1161 | return invalid<ReportAlias>(Context, /*Assert=*/true, Inst, AS); |
Tobias Grosser | 1eedb67 | 2014-09-24 21:04:29 +0000 | [diff] [blame] | 1162 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1163 | |
| 1164 | return true; |
| 1165 | } |
| 1166 | |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1167 | bool ScopDetection::isValidMemoryAccess(MemAccInst Inst, |
| 1168 | DetectionContext &Context) const { |
| 1169 | Value *Ptr = Inst.getPointerOperand(); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1170 | Loop *L = LI.getLoopFor(Inst->getParent()); |
| 1171 | const SCEV *AccessFunction = SE.getSCEVAtScope(Ptr, L); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1172 | const SCEVUnknown *BasePointer; |
| 1173 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1174 | BasePointer = dyn_cast<SCEVUnknown>(SE.getPointerBase(AccessFunction)); |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1175 | |
| 1176 | return isValidAccess(Inst, AccessFunction, BasePointer, Context); |
| 1177 | } |
| 1178 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1179 | bool ScopDetection::isValidInstruction(Instruction &Inst, |
| 1180 | DetectionContext &Context) const { |
Tobias Grosser | b12b006 | 2015-11-11 12:44:18 +0000 | [diff] [blame] | 1181 | for (auto &Op : Inst.operands()) { |
| 1182 | auto *OpInst = dyn_cast<Instruction>(&Op); |
| 1183 | |
| 1184 | if (!OpInst) |
| 1185 | continue; |
| 1186 | |
Tobias Grosser | 1f93d0f | 2017-09-26 15:00:10 +0000 | [diff] [blame] | 1187 | if (isErrorBlock(*OpInst->getParent(), Context.CurRegion, LI, DT)) { |
| 1188 | auto *PHI = dyn_cast<PHINode>(OpInst); |
| 1189 | if (PHI) { |
| 1190 | for (User *U : PHI->users()) { |
| 1191 | if (!isa<TerminatorInst>(U)) |
| 1192 | return false; |
| 1193 | } |
| 1194 | } else { |
| 1195 | return false; |
| 1196 | } |
| 1197 | } |
Tobias Grosser | b12b006 | 2015-11-11 12:44:18 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
Johannes Doerfert | 81c41b9 | 2016-04-09 21:55:58 +0000 | [diff] [blame] | 1200 | if (isa<LandingPadInst>(&Inst) || isa<ResumeInst>(&Inst)) |
| 1201 | return false; |
| 1202 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1203 | // We only check the call instruction but not invoke instruction. |
| 1204 | if (CallInst *CI = dyn_cast<CallInst>(&Inst)) { |
Johannes Doerfert | cea6193 | 2016-02-21 19:13:19 +0000 | [diff] [blame] | 1205 | if (isValidCallInst(*CI, Context)) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1206 | return true; |
| 1207 | |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 1208 | return invalid<ReportFuncCall>(Context, /*Assert=*/true, &Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
Tobias Grosser | 1f0236d | 2016-11-21 09:07:30 +0000 | [diff] [blame] | 1211 | if (!Inst.mayReadOrWriteMemory()) { |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 1212 | if (!isa<AllocaInst>(Inst)) |
| 1213 | return true; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1214 | |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 1215 | return invalid<ReportAlloca>(Context, /*Assert=*/true, &Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | // Check the access function. |
Michael Kruse | 70131d3 | 2016-01-27 17:09:17 +0000 | [diff] [blame] | 1219 | if (auto MemInst = MemAccInst::dyn_cast(Inst)) { |
Hongbin Zheng | 8efb22e | 2016-02-27 01:49:58 +0000 | [diff] [blame] | 1220 | Context.hasStores |= isa<StoreInst>(MemInst); |
| 1221 | Context.hasLoads |= isa<LoadInst>(MemInst); |
Michael Kruse | 70131d3 | 2016-01-27 17:09:17 +0000 | [diff] [blame] | 1222 | if (!MemInst.isSimple()) |
| 1223 | return invalid<ReportNonSimpleMemoryAccess>(Context, /*Assert=*/true, |
| 1224 | &Inst); |
Tobias Grosser | bf45e74 | 2015-10-25 13:48:40 +0000 | [diff] [blame] | 1225 | |
Michael Kruse | 70131d3 | 2016-01-27 17:09:17 +0000 | [diff] [blame] | 1226 | return isValidMemoryAccess(MemInst, Context); |
Tobias Grosser | d1e33e7 | 2015-02-19 05:31:07 +0000 | [diff] [blame] | 1227 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1228 | |
| 1229 | // We do not know this instruction, therefore we assume it is invalid. |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 1230 | return invalid<ReportUnknownInst>(Context, /*Assert=*/true, &Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
Tobias Grosser | 6d0970f | 2017-08-24 19:47:15 +0000 | [diff] [blame] | 1233 | /// Check whether @p L has exiting blocks. |
| 1234 | /// |
| 1235 | /// @param L The loop of interest |
| 1236 | /// |
| 1237 | /// @return True if the loop has exiting blocks, false otherwise. |
| 1238 | static bool hasExitingBlocks(Loop *L) { |
| 1239 | SmallVector<BasicBlock *, 4> ExitingBlocks; |
| 1240 | L->getExitingBlocks(ExitingBlocks); |
| 1241 | return !ExitingBlocks.empty(); |
| 1242 | } |
| 1243 | |
Johannes Doerfert | d020b77 | 2015-08-27 06:53:52 +0000 | [diff] [blame] | 1244 | bool ScopDetection::canUseISLTripCount(Loop *L, |
| 1245 | DetectionContext &Context) const { |
Johannes Doerfert | 30ffb6f | 2015-10-04 14:53:18 +0000 | [diff] [blame] | 1246 | // Ensure the loop has valid exiting blocks as well as latches, otherwise we |
| 1247 | // need to overapproximate it as a boxed loop. |
| 1248 | SmallVector<BasicBlock *, 4> LoopControlBlocks; |
Tobias Grosser | 151ae32 | 2016-04-03 19:36:52 +0000 | [diff] [blame] | 1249 | L->getExitingBlocks(LoopControlBlocks); |
Johannes Doerfert | d5edbd6 | 2016-04-03 23:09:06 +0000 | [diff] [blame] | 1250 | L->getLoopLatches(LoopControlBlocks); |
Johannes Doerfert | 30ffb6f | 2015-10-04 14:53:18 +0000 | [diff] [blame] | 1251 | for (BasicBlock *ControlBB : LoopControlBlocks) { |
Tobias Grosser | b76cd3c | 2015-11-11 08:42:20 +0000 | [diff] [blame] | 1252 | if (!isValidCFG(*ControlBB, true, false, Context)) |
Johannes Doerfert | d020b77 | 2015-08-27 06:53:52 +0000 | [diff] [blame] | 1253 | return false; |
| 1254 | } |
| 1255 | |
Johannes Doerfert | d020b77 | 2015-08-27 06:53:52 +0000 | [diff] [blame] | 1256 | // We can use ISL to compute the trip count of L. |
| 1257 | return true; |
| 1258 | } |
| 1259 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1260 | bool ScopDetection::isValidLoop(Loop *L, DetectionContext &Context) const { |
Tobias Grosser | 349d1c3 | 2016-09-20 17:05:22 +0000 | [diff] [blame] | 1261 | // Loops that contain part but not all of the blocks of a region cannot be |
| 1262 | // handled by the schedule generation. Such loop constructs can happen |
| 1263 | // because a region can contain BBs that have no path to the exit block |
Tobias Grosser | 6d0970f | 2017-08-24 19:47:15 +0000 | [diff] [blame] | 1264 | // (Infinite loops, UnreachableInst), but such blocks are never part of a |
| 1265 | // loop. |
| 1266 | // |
| 1267 | // _______________ |
| 1268 | // | Loop Header | <-----------. |
| 1269 | // --------------- | |
| 1270 | // | | |
| 1271 | // _______________ ______________ |
| 1272 | // | RegionEntry |-----> | RegionExit |-----> |
| 1273 | // --------------- -------------- |
| 1274 | // | |
| 1275 | // _______________ |
| 1276 | // | EndlessLoop | <--. |
| 1277 | // --------------- | |
| 1278 | // | | |
| 1279 | // \------------/ |
| 1280 | // |
| 1281 | // In the example above, the loop (LoopHeader,RegionEntry,RegionExit) is |
| 1282 | // neither entirely contained in the region RegionEntry->RegionExit |
| 1283 | // (containing RegionEntry,EndlessLoop) nor is the region entirely contained |
| 1284 | // in the loop. |
| 1285 | // The block EndlessLoop is contained in the region because Region::contains |
| 1286 | // tests whether it is not dominated by RegionExit. This is probably to not |
| 1287 | // having to query the PostdominatorTree. Instead of an endless loop, a dead |
| 1288 | // end can also be formed by an UnreachableInst. This case is already caught |
| 1289 | // by isErrorBlock(). We hence only have to reject endless loops here. |
| 1290 | if (!hasExitingBlocks(L)) |
| 1291 | return invalid<ReportLoopHasNoExit>(Context, /*Assert=*/true, L); |
Tobias Grosser | 349d1c3 | 2016-09-20 17:05:22 +0000 | [diff] [blame] | 1292 | |
Johannes Doerfert | f61df69 | 2015-10-04 14:56:08 +0000 | [diff] [blame] | 1293 | if (canUseISLTripCount(L, Context)) |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 1294 | return true; |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 1295 | |
Johannes Doerfert | b68cffb | 2015-09-10 15:27:46 +0000 | [diff] [blame] | 1296 | if (AllowNonAffineSubLoops && AllowNonAffineSubRegions) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1297 | Region *R = RI.getRegionFor(L->getHeader()); |
Johannes Doerfert | 757a32b | 2015-10-04 14:54:27 +0000 | [diff] [blame] | 1298 | while (R != &Context.CurRegion && !R->contains(L)) |
| 1299 | R = R->getParent(); |
| 1300 | |
| 1301 | if (addOverApproximatedRegion(R, Context)) |
| 1302 | return true; |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 1303 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1304 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1305 | const SCEV *LoopCount = SE.getBackedgeTakenCount(L); |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 1306 | return invalid<ReportLoopBound>(Context, /*Assert=*/true, L, LoopCount); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1309 | /// Return the number of loops in @p L (incl. @p L) that have a trip |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1310 | /// count that is not known to be less than @MinProfitableTrips. |
| 1311 | ScopDetection::LoopStats |
| 1312 | ScopDetection::countBeneficialSubLoops(Loop *L, ScalarEvolution &SE, |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 1313 | unsigned MinProfitableTrips) { |
Johannes Doerfert | 7175bdf | 2015-09-20 14:56:54 +0000 | [diff] [blame] | 1314 | auto *TripCount = SE.getBackedgeTakenCount(L); |
| 1315 | |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1316 | int NumLoops = 1; |
| 1317 | int MaxLoopDepth = 1; |
Michael Kruse | 7fac28fa | 2017-08-23 13:29:59 +0000 | [diff] [blame] | 1318 | if (MinProfitableTrips > 0) |
| 1319 | if (auto *TripCountC = dyn_cast<SCEVConstant>(TripCount)) |
| 1320 | if (TripCountC->getType()->getScalarSizeInBits() <= 64) |
| 1321 | if (TripCountC->getValue()->getZExtValue() <= MinProfitableTrips) |
| 1322 | NumLoops -= 1; |
Johannes Doerfert | 7175bdf | 2015-09-20 14:56:54 +0000 | [diff] [blame] | 1323 | |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1324 | for (auto &SubLoop : *L) { |
| 1325 | LoopStats Stats = countBeneficialSubLoops(SubLoop, SE, MinProfitableTrips); |
| 1326 | NumLoops += Stats.NumLoops; |
Tobias Grosser | 65ce936 | 2017-02-17 08:08:54 +0000 | [diff] [blame] | 1327 | MaxLoopDepth = std::max(MaxLoopDepth, Stats.MaxDepth + 1); |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1328 | } |
Johannes Doerfert | 7175bdf | 2015-09-20 14:56:54 +0000 | [diff] [blame] | 1329 | |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1330 | return {NumLoops, MaxLoopDepth}; |
Johannes Doerfert | 7175bdf | 2015-09-20 14:56:54 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1333 | ScopDetection::LoopStats |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 1334 | ScopDetection::countBeneficialLoops(Region *R, ScalarEvolution &SE, |
| 1335 | LoopInfo &LI, unsigned MinProfitableTrips) { |
Johannes Doerfert | f61df69 | 2015-10-04 14:56:08 +0000 | [diff] [blame] | 1336 | int LoopNum = 0; |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1337 | int MaxLoopDepth = 0; |
Tobias Grosser | ed21a1f | 2015-08-27 16:55:18 +0000 | [diff] [blame] | 1338 | |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 1339 | auto L = LI.getLoopFor(R->getEntry()); |
Tobias Grosser | 93ab558 | 2017-08-27 21:39:25 +0000 | [diff] [blame] | 1340 | |
| 1341 | // If L is fully contained in R, move to first loop surrounding R. Otherwise, |
| 1342 | // L is either nullptr or already surrounding R. |
| 1343 | if (L && R->contains(L)) { |
| 1344 | L = R->outermostLoopInRegion(L); |
| 1345 | L = L->getParentLoop(); |
| 1346 | } |
Tobias Grosser | ed21a1f | 2015-08-27 16:55:18 +0000 | [diff] [blame] | 1347 | |
Tobias Grosser | 050e0cb | 2015-08-31 12:08:11 +0000 | [diff] [blame] | 1348 | auto SubLoops = |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 1349 | L ? L->getSubLoopsVector() : std::vector<Loop *>(LI.begin(), LI.end()); |
Tobias Grosser | 050e0cb | 2015-08-31 12:08:11 +0000 | [diff] [blame] | 1350 | |
| 1351 | for (auto &SubLoop : SubLoops) |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1352 | if (R->contains(SubLoop)) { |
| 1353 | LoopStats Stats = |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 1354 | countBeneficialSubLoops(SubLoop, SE, MinProfitableTrips); |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1355 | LoopNum += Stats.NumLoops; |
| 1356 | MaxLoopDepth = std::max(MaxLoopDepth, Stats.MaxDepth); |
| 1357 | } |
Tobias Grosser | 050e0cb | 2015-08-31 12:08:11 +0000 | [diff] [blame] | 1358 | |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1359 | return {LoopNum, MaxLoopDepth}; |
Tobias Grosser | ed21a1f | 2015-08-27 16:55:18 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1362 | Region *ScopDetection::expandRegion(Region &R) { |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 1363 | // Initial no valid region was found (greater than R) |
Tobias Grosser | d5d93ec | 2015-06-04 17:59:54 +0000 | [diff] [blame] | 1364 | std::unique_ptr<Region> LastValidRegion; |
| 1365 | auto ExpandedRegion = std::unique_ptr<Region>(R.getExpandedRegion()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1366 | |
| 1367 | DEBUG(dbgs() << "\tExpanding " << R.getNameStr() << "\n"); |
| 1368 | |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 1369 | while (ExpandedRegion) { |
Johannes Doerfert | c06b7d6 | 2015-10-07 20:46:06 +0000 | [diff] [blame] | 1370 | const auto &It = DetectionContextMap.insert(std::make_pair( |
Johannes Doerfert | 6c7639b | 2016-05-12 18:50:01 +0000 | [diff] [blame] | 1371 | getBBPairForRegion(ExpandedRegion.get()), |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1372 | DetectionContext(*ExpandedRegion, AA, false /*verifying*/))); |
Johannes Doerfert | c06b7d6 | 2015-10-07 20:46:06 +0000 | [diff] [blame] | 1373 | DetectionContext &Context = It.first->second; |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 1374 | DEBUG(dbgs() << "\t\tTrying " << ExpandedRegion->getNameStr() << "\n"); |
Andreas Simbuerger | b379edb | 2014-06-27 06:21:14 +0000 | [diff] [blame] | 1375 | // Only expand when we did not collect errors. |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1376 | |
Johannes Doerfert | 717b866 | 2015-09-08 21:44:27 +0000 | [diff] [blame] | 1377 | if (!Context.Log.hasErrors()) { |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 1378 | // If the exit is valid check all blocks |
| 1379 | // - if true, a valid region was found => store it + keep expanding |
| 1380 | // - if false, .tbd. => stop (should this really end the loop?) |
Johannes Doerfert | e46925f | 2015-10-01 10:59:14 +0000 | [diff] [blame] | 1381 | if (!allBlocksValid(Context) || Context.Log.hasErrors()) { |
| 1382 | removeCachedResults(*ExpandedRegion); |
Michael Kruse | a6cc0d3 | 2016-08-08 22:39:32 +0000 | [diff] [blame] | 1383 | DetectionContextMap.erase(It.first); |
Andreas Simbuerger | b379edb | 2014-06-27 06:21:14 +0000 | [diff] [blame] | 1384 | break; |
Johannes Doerfert | e46925f | 2015-10-01 10:59:14 +0000 | [diff] [blame] | 1385 | } |
Andreas Simbuerger | b379edb | 2014-06-27 06:21:14 +0000 | [diff] [blame] | 1386 | |
Tobias Grosser | d7e5864 | 2013-04-10 06:55:45 +0000 | [diff] [blame] | 1387 | // Store this region, because it is the greatest valid (encountered so |
| 1388 | // far). |
Michael Kruse | a6cc0d3 | 2016-08-08 22:39:32 +0000 | [diff] [blame] | 1389 | if (LastValidRegion) { |
| 1390 | removeCachedResults(*LastValidRegion); |
| 1391 | DetectionContextMap.erase(getBBPairForRegion(LastValidRegion.get())); |
| 1392 | } |
Tobias Grosser | d5d93ec | 2015-06-04 17:59:54 +0000 | [diff] [blame] | 1393 | LastValidRegion = std::move(ExpandedRegion); |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 1394 | |
| 1395 | // Create and test the next greater region (if any) |
Tobias Grosser | d5d93ec | 2015-06-04 17:59:54 +0000 | [diff] [blame] | 1396 | ExpandedRegion = |
| 1397 | std::unique_ptr<Region>(LastValidRegion->getExpandedRegion()); |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 1398 | |
| 1399 | } else { |
| 1400 | // Create and test the next greater region (if any) |
Johannes Doerfert | c06b7d6 | 2015-10-07 20:46:06 +0000 | [diff] [blame] | 1401 | removeCachedResults(*ExpandedRegion); |
Michael Kruse | a6cc0d3 | 2016-08-08 22:39:32 +0000 | [diff] [blame] | 1402 | DetectionContextMap.erase(It.first); |
Tobias Grosser | d5d93ec | 2015-06-04 17:59:54 +0000 | [diff] [blame] | 1403 | ExpandedRegion = |
| 1404 | std::unique_ptr<Region>(ExpandedRegion->getExpandedRegion()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1405 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
Tobias Grosser | 378a9f2 | 2013-11-16 19:34:11 +0000 | [diff] [blame] | 1408 | DEBUG({ |
| 1409 | if (LastValidRegion) |
| 1410 | dbgs() << "\tto " << LastValidRegion->getNameStr() << "\n"; |
| 1411 | else |
| 1412 | dbgs() << "\tExpanding " << R.getNameStr() << " failed\n"; |
| 1413 | }); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1414 | |
Tobias Grosser | d5d93ec | 2015-06-04 17:59:54 +0000 | [diff] [blame] | 1415 | return LastValidRegion.release(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1416 | } |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 1417 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1418 | static bool regionWithoutLoops(Region &R, LoopInfo &LI) { |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 1419 | for (const BasicBlock *BB : R.blocks()) |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1420 | if (R.contains(LI.getLoopFor(BB))) |
Sebastian Pop | 2c9ec2e | 2013-06-03 16:35:37 +0000 | [diff] [blame] | 1421 | return false; |
| 1422 | |
| 1423 | return true; |
| 1424 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1425 | |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1426 | void ScopDetection::removeCachedResultsRecursively(const Region &R) { |
David Blaikie | b035f6d | 2014-04-15 18:45:27 +0000 | [diff] [blame] | 1427 | for (auto &SubRegion : R) { |
Johannes Doerfert | e46925f | 2015-10-01 10:59:14 +0000 | [diff] [blame] | 1428 | if (ValidRegions.count(SubRegion.get())) { |
| 1429 | removeCachedResults(*SubRegion.get()); |
Johannes Doerfert | e46925f | 2015-10-01 10:59:14 +0000 | [diff] [blame] | 1430 | } else |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1431 | removeCachedResultsRecursively(*SubRegion); |
Tobias Grosser | 28a70c5 | 2014-01-29 19:05:30 +0000 | [diff] [blame] | 1432 | } |
Tobias Grosser | 28a70c5 | 2014-01-29 19:05:30 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
Johannes Doerfert | e46925f | 2015-10-01 10:59:14 +0000 | [diff] [blame] | 1435 | void ScopDetection::removeCachedResults(const Region &R) { |
| 1436 | ValidRegions.remove(&R); |
Johannes Doerfert | e46925f | 2015-10-01 10:59:14 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1439 | void ScopDetection::findScops(Region &R) { |
Johannes Doerfert | 6c7639b | 2016-05-12 18:50:01 +0000 | [diff] [blame] | 1440 | const auto &It = DetectionContextMap.insert(std::make_pair( |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1441 | getBBPairForRegion(&R), DetectionContext(R, AA, false /*verifying*/))); |
Johannes Doerfert | c06b7d6 | 2015-10-07 20:46:06 +0000 | [diff] [blame] | 1442 | DetectionContext &Context = It.first->second; |
Johannes Doerfert | 6a4d81c | 2015-03-08 15:11:50 +0000 | [diff] [blame] | 1443 | |
| 1444 | bool RegionIsValid = false; |
Michael Kruse | 0b56681 | 2016-02-29 16:54:18 +0000 | [diff] [blame] | 1445 | if (!PollyProcessUnprofitable && regionWithoutLoops(R, LI)) |
Johannes Doerfert | 6a4d81c | 2015-03-08 15:11:50 +0000 | [diff] [blame] | 1446 | invalid<ReportUnprofitable>(Context, /*Assert=*/true, &R); |
Michael Kruse | 0b56681 | 2016-02-29 16:54:18 +0000 | [diff] [blame] | 1447 | else |
Johannes Doerfert | 6a4d81c | 2015-03-08 15:11:50 +0000 | [diff] [blame] | 1448 | RegionIsValid = isValidRegion(Context); |
| 1449 | |
Johannes Doerfert | 3f1c285 | 2015-02-19 18:11:50 +0000 | [diff] [blame] | 1450 | bool HasErrors = !RegionIsValid || Context.Log.size() > 0; |
Andreas Simbuerger | 0447240 | 2014-05-24 09:25:10 +0000 | [diff] [blame] | 1451 | |
Johannes Doerfert | e46925f | 2015-10-01 10:59:14 +0000 | [diff] [blame] | 1452 | if (HasErrors) { |
| 1453 | removeCachedResults(R); |
| 1454 | } else { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1455 | ValidRegions.insert(&R); |
| 1456 | return; |
| 1457 | } |
| 1458 | |
David Blaikie | b035f6d | 2014-04-15 18:45:27 +0000 | [diff] [blame] | 1459 | for (auto &SubRegion : R) |
Tobias Grosser | 00dc309 | 2014-03-02 12:02:46 +0000 | [diff] [blame] | 1460 | findScops(*SubRegion); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1461 | |
| 1462 | // Try to expand regions. |
| 1463 | // |
| 1464 | // As the region tree normally only contains canonical regions, non canonical |
| 1465 | // regions that form a Scop are not found. Therefore, those non canonical |
| 1466 | // regions are checked by expanding the canonical ones. |
| 1467 | |
Tobias Grosser | 0d1eee3 | 2013-02-05 11:56:05 +0000 | [diff] [blame] | 1468 | std::vector<Region *> ToExpand; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1469 | |
David Blaikie | b035f6d | 2014-04-15 18:45:27 +0000 | [diff] [blame] | 1470 | for (auto &SubRegion : R) |
| 1471 | ToExpand.push_back(SubRegion.get()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1472 | |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 1473 | for (Region *CurrentRegion : ToExpand) { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1474 | // Skip invalid regions. Regions may become invalid, if they are element of |
| 1475 | // an already expanded region. |
David Peixotto | 8da2b93 | 2014-10-22 20:39:07 +0000 | [diff] [blame] | 1476 | if (!ValidRegions.count(CurrentRegion)) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1477 | continue; |
| 1478 | |
Johannes Doerfert | 6c7639b | 2016-05-12 18:50:01 +0000 | [diff] [blame] | 1479 | // Skip regions that had errors. |
| 1480 | bool HadErrors = lookupRejectionLog(CurrentRegion)->hasErrors(); |
| 1481 | if (HadErrors) |
| 1482 | continue; |
| 1483 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1484 | Region *ExpandedR = expandRegion(*CurrentRegion); |
| 1485 | |
| 1486 | if (!ExpandedR) |
| 1487 | continue; |
| 1488 | |
| 1489 | R.addSubRegion(ExpandedR, true); |
| 1490 | ValidRegions.insert(ExpandedR); |
Johannes Doerfert | e46925f | 2015-10-01 10:59:14 +0000 | [diff] [blame] | 1491 | removeCachedResults(*CurrentRegion); |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1492 | removeCachedResultsRecursively(*ExpandedR); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1493 | } |
| 1494 | } |
| 1495 | |
| 1496 | bool ScopDetection::allBlocksValid(DetectionContext &Context) const { |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 1497 | Region &CurRegion = Context.CurRegion; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1498 | |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 1499 | for (const BasicBlock *BB : CurRegion.blocks()) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1500 | Loop *L = LI.getLoopFor(BB); |
Tobias Grosser | a3aa423 | 2017-07-15 22:42:17 +0000 | [diff] [blame] | 1501 | if (L && L->getHeader() == BB) { |
| 1502 | if (CurRegion.contains(L)) { |
| 1503 | if (!isValidLoop(L, Context) && !KeepGoing) |
| 1504 | return false; |
| 1505 | } else { |
| 1506 | SmallVector<BasicBlock *, 1> Latches; |
| 1507 | L->getLoopLatches(Latches); |
| 1508 | for (BasicBlock *Latch : Latches) |
| 1509 | if (CurRegion.contains(Latch)) |
| 1510 | return invalid<ReportLoopOnlySomeLatches>(Context, /*Assert=*/true, |
| 1511 | L); |
| 1512 | } |
| 1513 | } |
Sebastian Pop | b88ea5e | 2013-06-11 22:20:32 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
Johannes Doerfert | 90db75e | 2015-09-10 17:51:27 +0000 | [diff] [blame] | 1516 | for (BasicBlock *BB : CurRegion.blocks()) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1517 | bool IsErrorBlock = isErrorBlock(*BB, CurRegion, LI, DT); |
Tobias Grosser | b76cd3c | 2015-11-11 08:42:20 +0000 | [diff] [blame] | 1518 | |
| 1519 | // Also check exception blocks (and possibly register them as non-affine |
| 1520 | // regions). Even though exception blocks are not modeled, we use them |
| 1521 | // to forward-propagate domain constraints during ScopInfo construction. |
| 1522 | if (!isValidCFG(*BB, false, IsErrorBlock, Context) && !KeepGoing) |
| 1523 | return false; |
| 1524 | |
| 1525 | if (IsErrorBlock) |
Johannes Doerfert | 90db75e | 2015-09-10 17:51:27 +0000 | [diff] [blame] | 1526 | continue; |
| 1527 | |
Tobias Grosser | 1d19190 | 2014-03-03 13:13:55 +0000 | [diff] [blame] | 1528 | for (BasicBlock::iterator I = BB->begin(), E = --BB->end(); I != E; ++I) |
Andreas Simbuerger | 0447240 | 2014-05-24 09:25:10 +0000 | [diff] [blame] | 1529 | if (!isValidInstruction(*I, Context) && !KeepGoing) |
Sebastian Pop | 8ca899c | 2013-06-14 20:20:43 +0000 | [diff] [blame] | 1530 | return false; |
Johannes Doerfert | 90db75e | 2015-09-10 17:51:27 +0000 | [diff] [blame] | 1531 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1532 | |
Sebastian Pop | e8863b8 | 2014-05-12 19:02:02 +0000 | [diff] [blame] | 1533 | if (!hasAffineMemoryAccesses(Context)) |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 1534 | return false; |
| 1535 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1536 | return true; |
| 1537 | } |
| 1538 | |
Tobias Grosser | c1a269b | 2015-12-21 21:00:43 +0000 | [diff] [blame] | 1539 | bool ScopDetection::hasSufficientCompute(DetectionContext &Context, |
| 1540 | int NumLoops) const { |
| 1541 | int InstCount = 0; |
| 1542 | |
Tobias Grosser | b316dc1 | 2016-09-08 14:08:05 +0000 | [diff] [blame] | 1543 | if (NumLoops == 0) |
| 1544 | return false; |
| 1545 | |
Tobias Grosser | c1a269b | 2015-12-21 21:00:43 +0000 | [diff] [blame] | 1546 | for (auto *BB : Context.CurRegion.blocks()) |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1547 | if (Context.CurRegion.contains(LI.getLoopFor(BB))) |
Tobias Grosser | c6424ae | 2015-12-22 17:38:59 +0000 | [diff] [blame] | 1548 | InstCount += BB->size(); |
Tobias Grosser | c1a269b | 2015-12-21 21:00:43 +0000 | [diff] [blame] | 1549 | |
| 1550 | InstCount = InstCount / NumLoops; |
| 1551 | |
| 1552 | return InstCount >= ProfitabilityMinPerLoopInstructions; |
| 1553 | } |
| 1554 | |
Johannes Doerfert | bf9473b | 2016-05-10 14:42:30 +0000 | [diff] [blame] | 1555 | bool ScopDetection::hasPossiblyDistributableLoop( |
| 1556 | DetectionContext &Context) const { |
| 1557 | for (auto *BB : Context.CurRegion.blocks()) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1558 | auto *L = LI.getLoopFor(BB); |
Johannes Doerfert | bf9473b | 2016-05-10 14:42:30 +0000 | [diff] [blame] | 1559 | if (!Context.CurRegion.contains(L)) |
| 1560 | continue; |
| 1561 | if (Context.BoxedLoopsSet.count(L)) |
| 1562 | continue; |
| 1563 | unsigned StmtsWithStoresInLoops = 0; |
| 1564 | for (auto *LBB : L->blocks()) { |
| 1565 | bool MemStore = false; |
| 1566 | for (auto &I : *LBB) |
| 1567 | MemStore |= isa<StoreInst>(&I); |
| 1568 | StmtsWithStoresInLoops += MemStore; |
| 1569 | } |
| 1570 | return (StmtsWithStoresInLoops > 1); |
| 1571 | } |
| 1572 | return false; |
| 1573 | } |
| 1574 | |
Tobias Grosser | 97fc5bb | 2015-12-21 12:14:48 +0000 | [diff] [blame] | 1575 | bool ScopDetection::isProfitableRegion(DetectionContext &Context) const { |
| 1576 | Region &CurRegion = Context.CurRegion; |
| 1577 | |
| 1578 | if (PollyProcessUnprofitable) |
| 1579 | return true; |
| 1580 | |
| 1581 | // We can probably not do a lot on scops that only write or only read |
| 1582 | // data. |
| 1583 | if (!Context.hasStores || !Context.hasLoads) |
| 1584 | return invalid<ReportUnprofitable>(Context, /*Assert=*/true, &CurRegion); |
| 1585 | |
Tobias Grosser | cd01a36 | 2017-02-17 08:12:36 +0000 | [diff] [blame] | 1586 | int NumLoops = |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1587 | countBeneficialLoops(&CurRegion, SE, LI, MIN_LOOP_TRIP_COUNT).NumLoops; |
Tobias Grosser | 97fc5bb | 2015-12-21 12:14:48 +0000 | [diff] [blame] | 1588 | int NumAffineLoops = NumLoops - Context.BoxedLoopsSet.size(); |
Tobias Grosser | 97fc5bb | 2015-12-21 12:14:48 +0000 | [diff] [blame] | 1589 | |
Tobias Grosser | c1a269b | 2015-12-21 21:00:43 +0000 | [diff] [blame] | 1590 | // Scops with at least two loops may allow either loop fusion or tiling and |
| 1591 | // are consequently interesting to look at. |
| 1592 | if (NumAffineLoops >= 2) |
| 1593 | return true; |
| 1594 | |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 +0000 | [diff] [blame] | 1595 | // A loop with multiple non-trivial blocks might be amendable to distribution. |
Johannes Doerfert | bf9473b | 2016-05-10 14:42:30 +0000 | [diff] [blame] | 1596 | if (NumAffineLoops == 1 && hasPossiblyDistributableLoop(Context)) |
| 1597 | return true; |
| 1598 | |
Tobias Grosser | c1a269b | 2015-12-21 21:00:43 +0000 | [diff] [blame] | 1599 | // Scops that contain a loop with a non-trivial amount of computation per |
| 1600 | // loop-iteration are interesting as we may be able to parallelize such |
| 1601 | // loops. Individual loops that have only a small amount of computation |
| 1602 | // per-iteration are performance-wise very fragile as any change to the |
| 1603 | // loop induction variables may affect performance. To not cause spurious |
| 1604 | // performance regressions, we do not consider such loops. |
| 1605 | if (NumAffineLoops == 1 && hasSufficientCompute(Context, NumLoops)) |
| 1606 | return true; |
| 1607 | |
| 1608 | return invalid<ReportUnprofitable>(Context, /*Assert=*/true, &CurRegion); |
Tobias Grosser | 97fc5bb | 2015-12-21 12:14:48 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1611 | bool ScopDetection::isValidRegion(DetectionContext &Context) const { |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 1612 | Region &CurRegion = Context.CurRegion; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1613 | |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 1614 | DEBUG(dbgs() << "Checking region: " << CurRegion.getNameStr() << "\n\t"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1615 | |
Siddharth Bhat | b46847c | 2017-08-17 21:57:23 +0000 | [diff] [blame] | 1616 | if (!PollyAllowFullFunction && CurRegion.isTopLevelRegion()) { |
Tobias Grosser | f084edd | 2014-10-22 23:00:03 +0000 | [diff] [blame] | 1617 | DEBUG(dbgs() << "Top level region is invalid\n"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1618 | return false; |
| 1619 | } |
| 1620 | |
Tobias Grosser | 134a572 | 2017-03-07 15:50:43 +0000 | [diff] [blame] | 1621 | DebugLoc DbgLoc; |
Philip Pfaffe | 1a0128f | 2017-05-24 18:39:39 +0000 | [diff] [blame] | 1622 | if (CurRegion.getExit() && |
| 1623 | isa<UnreachableInst>(CurRegion.getExit()->getTerminator())) { |
Tobias Grosser | 134a572 | 2017-03-07 15:50:43 +0000 | [diff] [blame] | 1624 | DEBUG(dbgs() << "Unreachable in exit\n"); |
| 1625 | return invalid<ReportUnreachableInExit>(Context, /*Assert=*/true, |
| 1626 | CurRegion.getExit(), DbgLoc); |
| 1627 | } |
| 1628 | |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 1629 | if (!CurRegion.getEntry()->getName().count(OnlyRegion)) { |
Tobias Grosser | 4449e52 | 2014-01-27 14:24:53 +0000 | [diff] [blame] | 1630 | DEBUG({ |
| 1631 | dbgs() << "Region entry does not match -polly-region-only"; |
| 1632 | dbgs() << "\n"; |
| 1633 | }); |
| 1634 | return false; |
| 1635 | } |
| 1636 | |
Tobias Grosser | d654c25 | 2012-04-10 18:12:19 +0000 | [diff] [blame] | 1637 | // SCoP cannot contain the entry block of the function, because we need |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1638 | // to insert alloca instruction there when translate scalar to array. |
Siddharth Bhat | b46847c | 2017-08-17 21:57:23 +0000 | [diff] [blame] | 1639 | if (!PollyAllowFullFunction && |
Tobias Grosser | d8945ba | 2017-05-19 12:13:02 +0000 | [diff] [blame] | 1640 | CurRegion.getEntry() == |
| 1641 | &(CurRegion.getEntry()->getParent()->getEntryBlock())) |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 1642 | return invalid<ReportEntry>(Context, /*Assert=*/true, CurRegion.getEntry()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1643 | |
Hongbin Zheng | 94868e6 | 2012-04-07 12:29:17 +0000 | [diff] [blame] | 1644 | if (!allBlocksValid(Context)) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1645 | return false; |
| 1646 | |
Tobias Grosser | 1c3a6d7 | 2016-01-22 09:44:37 +0000 | [diff] [blame] | 1647 | if (!isReducibleRegion(CurRegion, DbgLoc)) |
| 1648 | return invalid<ReportIrreducibleRegion>(Context, /*Assert=*/true, |
| 1649 | &CurRegion, DbgLoc); |
| 1650 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1651 | DEBUG(dbgs() << "OK\n"); |
| 1652 | return true; |
| 1653 | } |
| 1654 | |
Tobias Grosser | 629109b | 2016-08-03 12:00:07 +0000 | [diff] [blame] | 1655 | void ScopDetection::markFunctionAsInvalid(Function *F) { |
Johannes Doerfert | 43e1ead | 2014-07-15 21:06:48 +0000 | [diff] [blame] | 1656 | F->addFnAttr(PollySkipFnAttr); |
| 1657 | } |
| 1658 | |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 1659 | bool ScopDetection::isValidFunction(Function &F) { |
Johannes Doerfert | 43e1ead | 2014-07-15 21:06:48 +0000 | [diff] [blame] | 1660 | return !F.hasFnAttribute(PollySkipFnAttr); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 1663 | void ScopDetection::printLocations(Function &F) { |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 1664 | for (const Region *R : *this) { |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 1665 | unsigned LineEntry, LineExit; |
| 1666 | std::string FileName; |
| 1667 | |
Tobias Grosser | 00dc309 | 2014-03-02 12:02:46 +0000 | [diff] [blame] | 1668 | getDebugLocation(R, LineEntry, LineExit, FileName); |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 1669 | DiagnosticScopFound Diagnostic(F, FileName, LineEntry, LineExit); |
| 1670 | F.getContext().diagnose(Diagnostic); |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 1671 | } |
| 1672 | } |
| 1673 | |
Johannes Doerfert | 6c7639b | 2016-05-12 18:50:01 +0000 | [diff] [blame] | 1674 | void ScopDetection::emitMissedRemarks(const Function &F) { |
| 1675 | for (auto &DIt : DetectionContextMap) { |
| 1676 | auto &DC = DIt.getSecond(); |
| 1677 | if (DC.Log.hasErrors()) |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 1678 | emitRejectionRemarks(DIt.getFirst(), DC.Log, ORE); |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 1679 | } |
| 1680 | } |
| 1681 | |
Tobias Grosser | 1c3a6d7 | 2016-01-22 09:44:37 +0000 | [diff] [blame] | 1682 | bool ScopDetection::isReducibleRegion(Region &R, DebugLoc &DbgLoc) const { |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 1683 | /// Enum for coloring BBs in Region. |
Tobias Grosser | ef6ae70 | 2016-06-11 09:00:37 +0000 | [diff] [blame] | 1684 | /// |
| 1685 | /// WHITE - Unvisited BB in DFS walk. |
| 1686 | /// GREY - BBs which are currently on the DFS stack for processing. |
| 1687 | /// BLACK - Visited and completely processed BB. |
| 1688 | enum Color { WHITE, GREY, BLACK }; |
| 1689 | |
Tobias Grosser | 1c3a6d7 | 2016-01-22 09:44:37 +0000 | [diff] [blame] | 1690 | BasicBlock *REntry = R.getEntry(); |
| 1691 | BasicBlock *RExit = R.getExit(); |
| 1692 | // Map to match the color of a BasicBlock during the DFS walk. |
| 1693 | DenseMap<const BasicBlock *, Color> BBColorMap; |
| 1694 | // Stack keeping track of current BB and index of next child to be processed. |
| 1695 | std::stack<std::pair<BasicBlock *, unsigned>> DFSStack; |
| 1696 | |
| 1697 | unsigned AdjacentBlockIndex = 0; |
| 1698 | BasicBlock *CurrBB, *SuccBB; |
| 1699 | CurrBB = REntry; |
| 1700 | |
| 1701 | // Initialize the map for all BB with WHITE color. |
| 1702 | for (auto *BB : R.blocks()) |
Johannes Doerfert | 469db6a | 2016-05-19 12:36:43 +0000 | [diff] [blame] | 1703 | BBColorMap[BB] = WHITE; |
Tobias Grosser | 1c3a6d7 | 2016-01-22 09:44:37 +0000 | [diff] [blame] | 1704 | |
| 1705 | // Process the entry block of the Region. |
Johannes Doerfert | 469db6a | 2016-05-19 12:36:43 +0000 | [diff] [blame] | 1706 | BBColorMap[CurrBB] = GREY; |
Tobias Grosser | 1c3a6d7 | 2016-01-22 09:44:37 +0000 | [diff] [blame] | 1707 | DFSStack.push(std::make_pair(CurrBB, 0)); |
| 1708 | |
| 1709 | while (!DFSStack.empty()) { |
| 1710 | // Get next BB on stack to be processed. |
| 1711 | CurrBB = DFSStack.top().first; |
| 1712 | AdjacentBlockIndex = DFSStack.top().second; |
| 1713 | DFSStack.pop(); |
| 1714 | |
| 1715 | // Loop to iterate over the successors of current BB. |
| 1716 | const TerminatorInst *TInst = CurrBB->getTerminator(); |
| 1717 | unsigned NSucc = TInst->getNumSuccessors(); |
| 1718 | for (unsigned I = AdjacentBlockIndex; I < NSucc; |
| 1719 | ++I, ++AdjacentBlockIndex) { |
| 1720 | SuccBB = TInst->getSuccessor(I); |
| 1721 | |
| 1722 | // Checks for region exit block and self-loops in BB. |
| 1723 | if (SuccBB == RExit || SuccBB == CurrBB) |
| 1724 | continue; |
| 1725 | |
| 1726 | // WHITE indicates an unvisited BB in DFS walk. |
Johannes Doerfert | 469db6a | 2016-05-19 12:36:43 +0000 | [diff] [blame] | 1727 | if (BBColorMap[SuccBB] == WHITE) { |
Tobias Grosser | 1c3a6d7 | 2016-01-22 09:44:37 +0000 | [diff] [blame] | 1728 | // Push the current BB and the index of the next child to be visited. |
| 1729 | DFSStack.push(std::make_pair(CurrBB, I + 1)); |
| 1730 | // Push the next BB to be processed. |
| 1731 | DFSStack.push(std::make_pair(SuccBB, 0)); |
| 1732 | // First time the BB is being processed. |
Johannes Doerfert | 469db6a | 2016-05-19 12:36:43 +0000 | [diff] [blame] | 1733 | BBColorMap[SuccBB] = GREY; |
Tobias Grosser | 1c3a6d7 | 2016-01-22 09:44:37 +0000 | [diff] [blame] | 1734 | break; |
Johannes Doerfert | 469db6a | 2016-05-19 12:36:43 +0000 | [diff] [blame] | 1735 | } else if (BBColorMap[SuccBB] == GREY) { |
Tobias Grosser | 1c3a6d7 | 2016-01-22 09:44:37 +0000 | [diff] [blame] | 1736 | // GREY indicates a loop in the control flow. |
| 1737 | // If the destination dominates the source, it is a natural loop |
| 1738 | // else, an irreducible control flow in the region is detected. |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1739 | if (!DT.dominates(SuccBB, CurrBB)) { |
Tobias Grosser | 1c3a6d7 | 2016-01-22 09:44:37 +0000 | [diff] [blame] | 1740 | // Get debug info of instruction which causes irregular control flow. |
| 1741 | DbgLoc = TInst->getDebugLoc(); |
| 1742 | return false; |
| 1743 | } |
| 1744 | } |
| 1745 | } |
| 1746 | |
| 1747 | // If all children of current BB have been processed, |
| 1748 | // then mark that BB as fully processed. |
| 1749 | if (AdjacentBlockIndex == NSucc) |
Johannes Doerfert | 469db6a | 2016-05-19 12:36:43 +0000 | [diff] [blame] | 1750 | BBColorMap[CurrBB] = BLACK; |
Tobias Grosser | 1c3a6d7 | 2016-01-22 09:44:37 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
| 1753 | return true; |
| 1754 | } |
| 1755 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1756 | static void updateLoopCountStatistic(ScopDetection::LoopStats Stats, |
| 1757 | bool OnlyProfitable) { |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1758 | if (!OnlyProfitable) { |
| 1759 | NumLoopsInScop += Stats.NumLoops; |
Tobias Grosser | 9fe37df | 2017-02-12 10:52:57 +0000 | [diff] [blame] | 1760 | MaxNumLoopsInScop = |
| 1761 | std::max(MaxNumLoopsInScop.getValue(), (unsigned)Stats.NumLoops); |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1762 | if (Stats.MaxDepth == 1) |
| 1763 | NumScopsDepthOne++; |
| 1764 | else if (Stats.MaxDepth == 2) |
| 1765 | NumScopsDepthTwo++; |
| 1766 | else if (Stats.MaxDepth == 3) |
| 1767 | NumScopsDepthThree++; |
| 1768 | else if (Stats.MaxDepth == 4) |
| 1769 | NumScopsDepthFour++; |
| 1770 | else if (Stats.MaxDepth == 5) |
| 1771 | NumScopsDepthFive++; |
| 1772 | else |
| 1773 | NumScopsDepthLarger++; |
| 1774 | } else { |
| 1775 | NumLoopsInProfScop += Stats.NumLoops; |
Tobias Grosser | 9fe37df | 2017-02-12 10:52:57 +0000 | [diff] [blame] | 1776 | MaxNumLoopsInProfScop = |
| 1777 | std::max(MaxNumLoopsInProfScop.getValue(), (unsigned)Stats.NumLoops); |
Tobias Grosser | b45ae56 | 2016-11-26 07:37:46 +0000 | [diff] [blame] | 1778 | if (Stats.MaxDepth == 1) |
| 1779 | NumProfScopsDepthOne++; |
| 1780 | else if (Stats.MaxDepth == 2) |
| 1781 | NumProfScopsDepthTwo++; |
| 1782 | else if (Stats.MaxDepth == 3) |
| 1783 | NumProfScopsDepthThree++; |
| 1784 | else if (Stats.MaxDepth == 4) |
| 1785 | NumProfScopsDepthFour++; |
| 1786 | else if (Stats.MaxDepth == 5) |
| 1787 | NumProfScopsDepthFive++; |
| 1788 | else |
| 1789 | NumProfScopsDepthLarger++; |
| 1790 | } |
| 1791 | } |
| 1792 | |
Johannes Doerfert | 1dafea4 | 2016-05-23 09:07:08 +0000 | [diff] [blame] | 1793 | ScopDetection::DetectionContext * |
Johannes Doerfert | c06b7d6 | 2015-10-07 20:46:06 +0000 | [diff] [blame] | 1794 | ScopDetection::getDetectionContext(const Region *R) const { |
Johannes Doerfert | 6c7639b | 2016-05-12 18:50:01 +0000 | [diff] [blame] | 1795 | auto DCMIt = DetectionContextMap.find(getBBPairForRegion(R)); |
Johannes Doerfert | c06b7d6 | 2015-10-07 20:46:06 +0000 | [diff] [blame] | 1796 | if (DCMIt == DetectionContextMap.end()) |
| 1797 | return nullptr; |
| 1798 | return &DCMIt->second; |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
Johannes Doerfert | 6c7639b | 2016-05-12 18:50:01 +0000 | [diff] [blame] | 1801 | const RejectLog *ScopDetection::lookupRejectionLog(const Region *R) const { |
| 1802 | const DetectionContext *DC = getDetectionContext(R); |
| 1803 | return DC ? &DC->Log : nullptr; |
| 1804 | } |
| 1805 | |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 1806 | void ScopDetection::verifyRegion(const Region &R) const { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1807 | assert(isMaxRegionInScop(R) && "Expect R is a valid region."); |
Johannes Doerfert | f3e98f4 | 2015-04-12 22:52:20 +0000 | [diff] [blame] | 1808 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1809 | DetectionContext Context(const_cast<Region &>(R), AA, true /*verifying*/); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1810 | isValidRegion(Context); |
| 1811 | } |
| 1812 | |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 1813 | void ScopDetection::verifyAnalysis() const { |
Tobias Grosser | a168993 | 2014-02-18 18:49:49 +0000 | [diff] [blame] | 1814 | if (!VerifyScops) |
| 1815 | return; |
| 1816 | |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 1817 | for (const Region *R : ValidRegions) |
Tobias Grosser | 00dc309 | 2014-03-02 12:02:46 +0000 | [diff] [blame] | 1818 | verifyRegion(*R); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 1821 | bool ScopDetectionWrapperPass::runOnFunction(Function &F) { |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1822 | auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 1823 | auto &RI = getAnalysis<RegionInfoPass>().getRegionInfo(); |
| 1824 | auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 1825 | auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
| 1826 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 1827 | auto &ORE = getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE(); |
| 1828 | Result.reset(new ScopDetection(F, DT, SE, LI, RI, AA, ORE)); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1829 | return false; |
| 1830 | } |
| 1831 | |
| 1832 | void ScopDetectionWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 1833 | AU.addRequired<LoopInfoWrapperPass>(); |
Michael Kruse | 6a19d59 | 2016-10-17 13:29:20 +0000 | [diff] [blame] | 1834 | AU.addRequiredTransitive<ScalarEvolutionWrapperPass>(); |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 1835 | AU.addRequired<DominatorTreeWrapperPass>(); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 1836 | AU.addRequired<OptimizationRemarkEmitterWrapperPass>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1837 | // We also need AA and RegionInfo when we are verifying analysis. |
Chandler Carruth | 66ef16b | 2015-09-09 22:13:56 +0000 | [diff] [blame] | 1838 | AU.addRequiredTransitive<AAResultsWrapperPass>(); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 1839 | AU.addRequiredTransitive<RegionInfoPass>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1840 | AU.setPreservesAll(); |
| 1841 | } |
| 1842 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1843 | void ScopDetectionWrapperPass::print(raw_ostream &OS, const Module *) const { |
| 1844 | for (const Region *R : Result->ValidRegions) |
Tobias Grosser | 00dc309 | 2014-03-02 12:02:46 +0000 | [diff] [blame] | 1845 | OS << "Valid Region for Scop: " << R->getNameStr() << '\n'; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1846 | |
| 1847 | OS << "\n"; |
| 1848 | } |
| 1849 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1850 | ScopDetectionWrapperPass::ScopDetectionWrapperPass() : FunctionPass(ID) { |
| 1851 | // Disable runtime alias checks if we ignore aliasing all together. |
| 1852 | if (IgnoreAliasing) |
| 1853 | PollyUseRuntimeAliasChecks = false; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1854 | } |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 1855 | |
Philip Pfaffe | f5a4394 | 2017-08-02 11:08:01 +0000 | [diff] [blame] | 1856 | ScopAnalysis::ScopAnalysis() { |
| 1857 | // Disable runtime alias checks if we ignore aliasing all together. |
| 1858 | if (IgnoreAliasing) |
| 1859 | PollyUseRuntimeAliasChecks = false; |
| 1860 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1861 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1862 | void ScopDetectionWrapperPass::releaseMemory() { Result.reset(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1863 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1864 | char ScopDetectionWrapperPass::ID; |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 1865 | |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1866 | AnalysisKey ScopAnalysis::Key; |
| 1867 | |
| 1868 | ScopDetection ScopAnalysis::run(Function &F, FunctionAnalysisManager &FAM) { |
| 1869 | auto &LI = FAM.getResult<LoopAnalysis>(F); |
| 1870 | auto &RI = FAM.getResult<RegionInfoAnalysis>(F); |
| 1871 | auto &AA = FAM.getResult<AAManager>(F); |
| 1872 | auto &SE = FAM.getResult<ScalarEvolutionAnalysis>(F); |
| 1873 | auto &DT = FAM.getResult<DominatorTreeAnalysis>(F); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 1874 | auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(F); |
| 1875 | return {F, DT, SE, LI, RI, AA, ORE}; |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
| 1878 | PreservedAnalyses ScopAnalysisPrinterPass::run(Function &F, |
| 1879 | FunctionAnalysisManager &FAM) { |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 1880 | OS << "Detected Scops in Function " << F.getName() << "\n"; |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1881 | auto &SD = FAM.getResult<ScopAnalysis>(F); |
| 1882 | for (const Region *R : SD.ValidRegions) |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 1883 | OS << "Valid Region for Scop: " << R->getNameStr() << '\n'; |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1884 | |
Eugene Zelenko | a32707d | 2017-08-25 21:35:27 +0000 | [diff] [blame] | 1885 | OS << "\n"; |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1886 | return PreservedAnalyses::all(); |
| 1887 | } |
| 1888 | |
| 1889 | Pass *polly::createScopDetectionWrapperPassPass() { |
| 1890 | return new ScopDetectionWrapperPass(); |
| 1891 | } |
| 1892 | |
| 1893 | INITIALIZE_PASS_BEGIN(ScopDetectionWrapperPass, "polly-detect", |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 1894 | "Polly - Detect static control parts (SCoPs)", false, |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 1895 | false); |
Chandler Carruth | 66ef16b | 2015-09-09 22:13:56 +0000 | [diff] [blame] | 1896 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass); |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 1897 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 1898 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); |
Johannes Doerfert | 08d90a3 | 2015-10-07 20:32:43 +0000 | [diff] [blame] | 1899 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); |
Tobias Grosser | c5bcf24 | 2015-08-17 10:57:08 +0000 | [diff] [blame] | 1900 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass); |
Eli Friedman | e737fc1 | 2017-07-17 23:58:33 +0000 | [diff] [blame] | 1901 | INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass); |
Philip Pfaffe | 5cc87e3 | 2017-05-12 14:37:29 +0000 | [diff] [blame] | 1902 | INITIALIZE_PASS_END(ScopDetectionWrapperPass, "polly-detect", |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 1903 | "Polly - Detect static control parts (SCoPs)", false, false) |