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