Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1 | //===----- ScopDetection.cpp - Detect Scops --------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Detect the maximal Scops of a function. |
| 11 | // |
| 12 | // A static control part (Scop) is a subgraph of the control flow graph (CFG) |
| 13 | // that only has statically known control flow and can therefore be described |
| 14 | // within the polyhedral model. |
| 15 | // |
| 16 | // Every Scop fullfills these restrictions: |
| 17 | // |
| 18 | // * It is a single entry single exit region |
| 19 | // |
| 20 | // * Only affine linear bounds in the loops |
| 21 | // |
| 22 | // Every natural loop in a Scop must have a number of loop iterations that can |
| 23 | // be described as an affine linear function in surrounding loop iterators or |
| 24 | // parameters. (A parameter is a scalar that does not change its value during |
| 25 | // execution of the Scop). |
| 26 | // |
| 27 | // * Only comparisons of affine linear expressions in conditions |
| 28 | // |
| 29 | // * All loops and conditions perfectly nested |
| 30 | // |
| 31 | // The control flow needs to be structured such that it could be written using |
| 32 | // just 'for' and 'if' statements, without the need for any 'goto', 'break' or |
| 33 | // 'continue'. |
| 34 | // |
| 35 | // * Side effect free functions call |
| 36 | // |
| 37 | // Only function calls and intrinsics that do not have side effects are allowed |
| 38 | // (readnone). |
| 39 | // |
| 40 | // The Scop detection finds the largest Scops by checking if the largest |
| 41 | // region is a Scop. If this is not the case, its canonical subregions are |
| 42 | // checked until a region is a Scop. It is now tried to extend this Scop by |
| 43 | // creating a larger non canonical region. |
| 44 | // |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 47 | #include "polly/CodeGen/BlockGenerators.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 48 | #include "polly/LinkAllPasses.h" |
Tobias Grosser | 637bd63 | 2013-05-07 07:31:10 +0000 | [diff] [blame] | 49 | #include "polly/Options.h" |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 50 | #include "polly/ScopDetectionDiagnostic.h" |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 51 | #include "polly/ScopDetection.h" |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame] | 52 | #include "polly/Support/SCEVValidator.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 53 | #include "polly/Support/ScopHelper.h" |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 54 | #include "polly/CodeGen/CodeGeneration.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 55 | #include "llvm/ADT/Statistic.h" |
| 56 | #include "llvm/Analysis/AliasAnalysis.h" |
Tobias Grosser | 6e9f25a | 2011-11-09 22:35:00 +0000 | [diff] [blame] | 57 | #include "llvm/Analysis/LoopInfo.h" |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 58 | #include "llvm/Analysis/PostDominators.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 59 | #include "llvm/Analysis/RegionIterator.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" |
Chandler Carruth | 6b96c24 | 2014-03-06 00:47:27 +0000 | [diff] [blame] | 62 | #include "llvm/IR/DebugInfo.h" |
Johannes Doerfert | 3f500fa | 2015-01-25 18:07:30 +0000 | [diff] [blame] | 63 | #include "llvm/IR/IntrinsicInst.h" |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 64 | #include "llvm/IR/DiagnosticInfo.h" |
| 65 | #include "llvm/IR/DiagnosticPrinter.h" |
Chandler Carruth | 6b96c24 | 2014-03-06 00:47:27 +0000 | [diff] [blame] | 66 | #include "llvm/IR/LLVMContext.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 67 | #include "llvm/Support/Debug.h" |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 68 | #include <set> |
| 69 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 70 | using namespace llvm; |
| 71 | using namespace polly; |
| 72 | |
Chandler Carruth | 95fef94 | 2014-04-22 03:30:19 +0000 | [diff] [blame] | 73 | #define DEBUG_TYPE "polly-detect" |
| 74 | |
Sebastian Pop | 8fe6d11 | 2013-05-30 17:47:32 +0000 | [diff] [blame] | 75 | static cl::opt<bool> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 76 | DetectScopsWithoutLoops("polly-detect-scops-in-functions-without-loops", |
| 77 | cl::desc("Detect scops in functions without loops"), |
| 78 | cl::Hidden, cl::init(false), cl::ZeroOrMore, |
| 79 | cl::cat(PollyCategory)); |
Sebastian Pop | 8fe6d11 | 2013-05-30 17:47:32 +0000 | [diff] [blame] | 80 | |
Sebastian Pop | 2c9ec2e | 2013-06-03 16:35:37 +0000 | [diff] [blame] | 81 | static cl::opt<bool> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 82 | DetectRegionsWithoutLoops("polly-detect-scops-in-regions-without-loops", |
| 83 | cl::desc("Detect scops in regions without loops"), |
| 84 | cl::Hidden, cl::init(false), cl::ZeroOrMore, |
| 85 | cl::cat(PollyCategory)); |
Sebastian Pop | 2c9ec2e | 2013-06-03 16:35:37 +0000 | [diff] [blame] | 86 | |
Tobias Grosser | d1e33e7 | 2015-02-19 05:31:07 +0000 | [diff] [blame] | 87 | static cl::opt<bool> DetectUnprofitable("polly-detect-unprofitable", |
| 88 | cl::desc("Detect unprofitable scops"), |
| 89 | cl::Hidden, cl::init(false), |
| 90 | cl::ZeroOrMore, cl::cat(PollyCategory)); |
| 91 | |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 92 | static cl::opt<std::string> OnlyFunction( |
| 93 | "polly-only-func", |
| 94 | cl::desc("Only run on functions that contain a certain string"), |
| 95 | cl::value_desc("string"), cl::ValueRequired, cl::init(""), |
| 96 | cl::cat(PollyCategory)); |
Tobias Grosser | 2ff8723 | 2011-10-23 11:17:06 +0000 | [diff] [blame] | 97 | |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 98 | static cl::opt<std::string> OnlyRegion( |
| 99 | "polly-only-region", |
| 100 | cl::desc("Only run on certain regions (The provided identifier must " |
| 101 | "appear in the name of the region's entry block"), |
| 102 | cl::value_desc("identifier"), cl::ValueRequired, cl::init(""), |
| 103 | cl::cat(PollyCategory)); |
Tobias Grosser | 4449e52 | 2014-01-27 14:24:53 +0000 | [diff] [blame] | 104 | |
Tobias Grosser | 60cd932 | 2011-11-10 12:47:26 +0000 | [diff] [blame] | 105 | static cl::opt<bool> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 106 | IgnoreAliasing("polly-ignore-aliasing", |
| 107 | cl::desc("Ignore possible aliasing of the array bases"), |
| 108 | cl::Hidden, cl::init(false), cl::ZeroOrMore, |
| 109 | cl::cat(PollyCategory)); |
Tobias Grosser | 2ff8723 | 2011-10-23 11:17:06 +0000 | [diff] [blame] | 110 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 111 | bool polly::PollyUseRuntimeAliasChecks; |
| 112 | static cl::opt<bool, true> XPollyUseRuntimeAliasChecks( |
| 113 | "polly-use-runtime-alias-checks", |
| 114 | cl::desc("Use runtime alias checks to resolve possible aliasing."), |
| 115 | cl::location(PollyUseRuntimeAliasChecks), cl::Hidden, cl::ZeroOrMore, |
| 116 | cl::init(true), cl::cat(PollyCategory)); |
| 117 | |
Tobias Grosser | 637bd63 | 2013-05-07 07:31:10 +0000 | [diff] [blame] | 118 | static cl::opt<bool> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 119 | ReportLevel("polly-report", |
| 120 | cl::desc("Print information about the activities of Polly"), |
| 121 | cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 122 | |
| 123 | static cl::opt<bool> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 124 | AllowNonAffine("polly-allow-nonaffine", |
| 125 | cl::desc("Allow non affine access functions in arrays"), |
| 126 | cl::Hidden, cl::init(false), cl::ZeroOrMore, |
| 127 | cl::cat(PollyCategory)); |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 128 | |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 129 | static cl::opt<bool> AllowNonAffineSubRegions( |
| 130 | "polly-allow-nonaffine-branches", |
| 131 | cl::desc("Allow non affine conditions for branches"), cl::Hidden, |
Johannes Doerfert | a36842f | 2015-02-26 11:09:24 +0000 | [diff] [blame] | 132 | cl::init(true), cl::ZeroOrMore, cl::cat(PollyCategory)); |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 133 | |
Tobias Grosser | bfbc369 | 2015-01-09 00:01:33 +0000 | [diff] [blame] | 134 | static cl::opt<bool> AllowUnsigned("polly-allow-unsigned", |
| 135 | cl::desc("Allow unsigned expressions"), |
| 136 | cl::Hidden, cl::init(false), cl::ZeroOrMore, |
| 137 | cl::cat(PollyCategory)); |
| 138 | |
Tobias Grosser | c7d3fc5 | 2013-07-25 03:02:29 +0000 | [diff] [blame] | 139 | static cl::opt<bool, true> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 140 | TrackFailures("polly-detect-track-failures", |
| 141 | cl::desc("Track failure strings in detecting scop regions"), |
| 142 | cl::location(PollyTrackFailures), cl::Hidden, cl::ZeroOrMore, |
Andreas Simbuerger | 3efe40b | 2014-08-17 10:09:03 +0000 | [diff] [blame] | 143 | cl::init(true), cl::cat(PollyCategory)); |
Tobias Grosser | c7d3fc5 | 2013-07-25 03:02:29 +0000 | [diff] [blame] | 144 | |
Andreas Simbuerger | 0447240 | 2014-05-24 09:25:10 +0000 | [diff] [blame] | 145 | static cl::opt<bool> KeepGoing("polly-detect-keep-going", |
| 146 | cl::desc("Do not fail on the first error."), |
| 147 | cl::Hidden, cl::ZeroOrMore, cl::init(false), |
| 148 | cl::cat(PollyCategory)); |
| 149 | |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 150 | static cl::opt<bool, true> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 151 | PollyDelinearizeX("polly-delinearize", |
| 152 | cl::desc("Delinearize array access functions"), |
| 153 | cl::location(PollyDelinearize), cl::Hidden, |
Tobias Grosser | 9d3c0b6 | 2015-03-08 12:57:31 +0000 | [diff] [blame] | 154 | cl::ZeroOrMore, cl::init(false), cl::cat(PollyCategory)); |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 155 | |
Tobias Grosser | a168993 | 2014-02-18 18:49:49 +0000 | [diff] [blame] | 156 | static cl::opt<bool> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 157 | VerifyScops("polly-detect-verify", |
| 158 | cl::desc("Verify the detected SCoPs after each transformation"), |
| 159 | cl::Hidden, cl::init(false), cl::ZeroOrMore, |
| 160 | cl::cat(PollyCategory)); |
Tobias Grosser | a168993 | 2014-02-18 18:49:49 +0000 | [diff] [blame] | 161 | |
Johannes Doerfert | 0ff23ec | 2015-02-06 20:13:15 +0000 | [diff] [blame] | 162 | static cl::opt<bool, true> XPollyModelPHINodes( |
| 163 | "polly-model-phi-nodes", |
| 164 | cl::desc("Allow PHI nodes in the input [Unsafe with code-generation!]."), |
| 165 | cl::location(PollyModelPHINodes), cl::Hidden, cl::ZeroOrMore, |
| 166 | cl::init(false), cl::cat(PollyCategory)); |
| 167 | |
| 168 | bool polly::PollyModelPHINodes = false; |
Tobias Grosser | c7d3fc5 | 2013-07-25 03:02:29 +0000 | [diff] [blame] | 169 | bool polly::PollyTrackFailures = false; |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 170 | bool polly::PollyDelinearize = false; |
Johannes Doerfert | 43e1ead | 2014-07-15 21:06:48 +0000 | [diff] [blame] | 171 | StringRef polly::PollySkipFnAttr = "polly.skip.fn"; |
Tobias Grosser | c7d3fc5 | 2013-07-25 03:02:29 +0000 | [diff] [blame] | 172 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 173 | //===----------------------------------------------------------------------===// |
| 174 | // Statistics. |
| 175 | |
| 176 | STATISTIC(ValidRegion, "Number of regions that a valid part of Scop"); |
| 177 | |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 178 | class DiagnosticScopFound : public DiagnosticInfo { |
| 179 | private: |
| 180 | static int PluginDiagnosticKind; |
| 181 | |
| 182 | Function &F; |
| 183 | std::string FileName; |
| 184 | unsigned EntryLine, ExitLine; |
| 185 | |
| 186 | public: |
Tobias Grosser | 1b12f46 | 2013-12-18 11:14:36 +0000 | [diff] [blame] | 187 | DiagnosticScopFound(Function &F, std::string FileName, unsigned EntryLine, |
| 188 | unsigned ExitLine) |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 189 | : DiagnosticInfo(PluginDiagnosticKind, DS_Note), F(F), FileName(FileName), |
Tobias Grosser | 1b12f46 | 2013-12-18 11:14:36 +0000 | [diff] [blame] | 190 | EntryLine(EntryLine), ExitLine(ExitLine) {} |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 191 | |
| 192 | virtual void print(DiagnosticPrinter &DP) const; |
| 193 | |
| 194 | static bool classof(const DiagnosticInfo *DI) { |
| 195 | return DI->getKind() == PluginDiagnosticKind; |
| 196 | } |
| 197 | }; |
| 198 | |
| 199 | int DiagnosticScopFound::PluginDiagnosticKind = 10; |
| 200 | |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 201 | void DiagnosticScopFound::print(DiagnosticPrinter &DP) const { |
Tobias Grosser | 1b12f46 | 2013-12-18 11:14:36 +0000 | [diff] [blame] | 202 | DP << "Polly detected an optimizable loop region (scop) in function '" << F |
| 203 | << "'\n"; |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 204 | |
| 205 | if (FileName.empty()) { |
| 206 | DP << "Scop location is unknown. Compile with debug info " |
| 207 | "(-g) to get more precise information. "; |
Tobias Grosser | 1b12f46 | 2013-12-18 11:14:36 +0000 | [diff] [blame] | 208 | return; |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | DP << FileName << ":" << EntryLine << ": Start of scop\n"; |
| 212 | DP << FileName << ":" << ExitLine << ": End of scop"; |
| 213 | } |
| 214 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 215 | //===----------------------------------------------------------------------===// |
| 216 | // ScopDetection. |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 217 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 218 | ScopDetection::ScopDetection() : FunctionPass(ID) { |
| 219 | if (!PollyUseRuntimeAliasChecks) |
| 220 | return; |
| 221 | |
Johannes Doerfert | 928229f | 2014-09-29 17:06:29 +0000 | [diff] [blame] | 222 | // Disable runtime alias checks if we ignore aliasing all together. |
| 223 | if (IgnoreAliasing) { |
| 224 | PollyUseRuntimeAliasChecks = false; |
| 225 | return; |
| 226 | } |
| 227 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 228 | if (AllowNonAffine) { |
| 229 | DEBUG(errs() << "WARNING: We disable runtime alias checks as non affine " |
| 230 | "accesses are enabled.\n"); |
| 231 | PollyUseRuntimeAliasChecks = false; |
| 232 | } |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 235 | template <class RR, typename... Args> |
| 236 | inline bool ScopDetection::invalid(DetectionContext &Context, bool Assert, |
| 237 | Args &&... Arguments) const { |
| 238 | |
| 239 | if (!Context.Verifying) { |
Andreas Simbuerger | 4870e09 | 2014-05-24 09:25:01 +0000 | [diff] [blame] | 240 | RejectLog &Log = Context.Log; |
| 241 | std::shared_ptr<RR> RejectReason = std::make_shared<RR>(Arguments...); |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 242 | |
Andreas Simbuerger | 8a00c9b | 2014-05-24 09:25:06 +0000 | [diff] [blame] | 243 | if (PollyTrackFailures) |
Andreas Simbuerger | 4870e09 | 2014-05-24 09:25:01 +0000 | [diff] [blame] | 244 | Log.report(RejectReason); |
Andreas Simbuerger | 4870e09 | 2014-05-24 09:25:01 +0000 | [diff] [blame] | 245 | |
| 246 | DEBUG(dbgs() << RejectReason->getMessage()); |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 247 | DEBUG(dbgs() << "\n"); |
| 248 | } else { |
| 249 | assert(!Assert && "Verification of detected scop failed"); |
| 250 | } |
| 251 | |
| 252 | return false; |
| 253 | } |
| 254 | |
Tobias Grosser | a168993 | 2014-02-18 18:49:49 +0000 | [diff] [blame] | 255 | bool ScopDetection::isMaxRegionInScop(const Region &R, bool Verify) const { |
| 256 | if (!ValidRegions.count(&R)) |
| 257 | return false; |
| 258 | |
Johannes Doerfert | 3f1c285 | 2015-02-19 18:11:50 +0000 | [diff] [blame] | 259 | if (Verify) { |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 260 | NonAffineSubRegionSetTy DummyNonAffineSubRegionSet; |
| 261 | DetectionContext Context(const_cast<Region &>(R), *AA, |
| 262 | DummyNonAffineSubRegionSet, false /*verifying*/); |
Johannes Doerfert | 3f1c285 | 2015-02-19 18:11:50 +0000 | [diff] [blame] | 263 | return isValidRegion(Context); |
| 264 | } |
Tobias Grosser | a168993 | 2014-02-18 18:49:49 +0000 | [diff] [blame] | 265 | |
| 266 | return true; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Tobias Grosser | 4f129a6 | 2011-10-08 00:30:55 +0000 | [diff] [blame] | 269 | std::string ScopDetection::regionIsInvalidBecause(const Region *R) const { |
Andreas Simbuerger | 8a00c9b | 2014-05-24 09:25:06 +0000 | [diff] [blame] | 270 | if (!RejectLogs.count(R)) |
Tobias Grosser | 4f129a6 | 2011-10-08 00:30:55 +0000 | [diff] [blame] | 271 | return ""; |
| 272 | |
Andreas Simbuerger | 8a00c9b | 2014-05-24 09:25:06 +0000 | [diff] [blame] | 273 | // Get the first error we found. Even in keep-going mode, this is the first |
| 274 | // reason that caused the candidate to be rejected. |
| 275 | RejectLog Errors = RejectLogs.at(R); |
Andreas Simbuerger | 83ed861 | 2014-06-12 07:25:08 +0000 | [diff] [blame] | 276 | |
| 277 | // This can happen when we marked a region invalid, but didn't track |
| 278 | // an error for it. |
| 279 | if (Errors.size() == 0) |
| 280 | return ""; |
| 281 | |
| 282 | RejectReasonPtr RR = *Errors.begin(); |
| 283 | return RR->getMessage(); |
Tobias Grosser | 4f129a6 | 2011-10-08 00:30:55 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 286 | static bool containsLoop(Region *R, LoopInfo *LI) { |
| 287 | for (BasicBlock *BB : R->blocks()) { |
| 288 | Loop *L = LI->getLoopFor(BB); |
| 289 | if (R->contains(L)) |
| 290 | return true; |
| 291 | } |
| 292 | return false; |
| 293 | } |
| 294 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 295 | bool ScopDetection::isValidCFG(BasicBlock &BB, |
| 296 | DetectionContext &Context) const { |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 297 | Region &CurRegion = Context.CurRegion; |
| 298 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 299 | TerminatorInst *TI = BB.getTerminator(); |
| 300 | |
| 301 | // Return instructions are only valid if the region is the top level region. |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 302 | if (isa<ReturnInst>(TI) && !CurRegion.getExit() && TI->getNumOperands() == 0) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 303 | return true; |
| 304 | |
| 305 | BranchInst *Br = dyn_cast<BranchInst>(TI); |
| 306 | |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 307 | if (!Br) |
| 308 | return invalid<ReportNonBranchTerminator>(Context, /*Assert=*/true, &BB); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 309 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 310 | if (Br->isUnconditional()) |
| 311 | return true; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 312 | |
| 313 | Value *Condition = Br->getCondition(); |
| 314 | |
| 315 | // UndefValue is not allowed as condition. |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 316 | if (isa<UndefValue>(Condition)) |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 317 | return invalid<ReportUndefCond>(Context, /*Assert=*/true, Br, &BB); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 318 | |
| 319 | // Only Constant and ICmpInst are allowed as condition. |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 320 | if (!(isa<Constant>(Condition) || isa<ICmpInst>(Condition))) { |
| 321 | if (AllowNonAffineSubRegions && !containsLoop(RI->getRegionFor(&BB), LI)) |
| 322 | Context.NonAffineSubRegionSet.insert(RI->getRegionFor(&BB)); |
| 323 | else |
| 324 | return invalid<ReportInvalidCond>(Context, /*Assert=*/true, Br, &BB); |
| 325 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 326 | |
| 327 | // Allow perfectly nested conditions. |
| 328 | assert(Br->getNumSuccessors() == 2 && "Unexpected number of successors"); |
| 329 | |
| 330 | if (ICmpInst *ICmp = dyn_cast<ICmpInst>(Condition)) { |
| 331 | // Unsigned comparisons are not allowed. They trigger overflow problems |
| 332 | // in the code generation. |
| 333 | // |
| 334 | // TODO: This is not sufficient and just hides bugs. However it does pretty |
| 335 | // well. |
Tobias Grosser | bfbc369 | 2015-01-09 00:01:33 +0000 | [diff] [blame] | 336 | if (ICmp->isUnsigned() && !AllowUnsigned) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 337 | return false; |
| 338 | |
| 339 | // Are both operands of the ICmp affine? |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 340 | if (isa<UndefValue>(ICmp->getOperand(0)) || |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 341 | isa<UndefValue>(ICmp->getOperand(1))) |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 342 | return invalid<ReportUndefOperand>(Context, /*Assert=*/true, &BB, ICmp); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 343 | |
Sebastian Pop | 9f57c5b | 2013-04-10 04:05:18 +0000 | [diff] [blame] | 344 | Loop *L = LI->getLoopFor(ICmp->getParent()); |
| 345 | const SCEV *LHS = SE->getSCEVAtScope(ICmp->getOperand(0), L); |
| 346 | const SCEV *RHS = SE->getSCEVAtScope(ICmp->getOperand(1), L); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 347 | |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 348 | if (!isAffineExpr(&CurRegion, LHS, *SE) || |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 349 | !isAffineExpr(&CurRegion, RHS, *SE)) { |
| 350 | if (AllowNonAffineSubRegions && !containsLoop(RI->getRegionFor(&BB), LI)) |
| 351 | Context.NonAffineSubRegionSet.insert(RI->getRegionFor(&BB)); |
| 352 | else |
| 353 | return invalid<ReportNonAffBranch>(Context, /*Assert=*/true, &BB, LHS, |
| 354 | RHS, ICmp); |
| 355 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | // Allow loop exit conditions. |
| 359 | Loop *L = LI->getLoopFor(&BB); |
| 360 | if (L && L->getExitingBlock() == &BB) |
| 361 | return true; |
| 362 | |
| 363 | // Allow perfectly nested conditions. |
| 364 | Region *R = RI->getRegionFor(&BB); |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 365 | if (R->getEntry() != &BB) |
| 366 | return invalid<ReportCondition>(Context, /*Assert=*/true, &BB); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 367 | |
| 368 | return true; |
| 369 | } |
| 370 | |
| 371 | bool ScopDetection::isValidCallInst(CallInst &CI) { |
Johannes Doerfert | 3f500fa | 2015-01-25 18:07:30 +0000 | [diff] [blame] | 372 | if (CI.doesNotReturn()) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 373 | return false; |
| 374 | |
| 375 | if (CI.doesNotAccessMemory()) |
| 376 | return true; |
| 377 | |
| 378 | Function *CalledFunction = CI.getCalledFunction(); |
| 379 | |
| 380 | // Indirect calls are not supported. |
| 381 | if (CalledFunction == 0) |
| 382 | return false; |
| 383 | |
Johannes Doerfert | 3f500fa | 2015-01-25 18:07:30 +0000 | [diff] [blame] | 384 | // Check if we can handle the intrinsic call. |
| 385 | if (auto *IT = dyn_cast<IntrinsicInst>(&CI)) { |
| 386 | switch (IT->getIntrinsicID()) { |
| 387 | // Lifetime markers are supported/ignored. |
| 388 | case llvm::Intrinsic::lifetime_start: |
| 389 | case llvm::Intrinsic::lifetime_end: |
| 390 | // Invariant markers are supported/ignored. |
| 391 | case llvm::Intrinsic::invariant_start: |
| 392 | case llvm::Intrinsic::invariant_end: |
| 393 | // Some misc annotations are supported/ignored. |
| 394 | case llvm::Intrinsic::var_annotation: |
| 395 | case llvm::Intrinsic::ptr_annotation: |
| 396 | case llvm::Intrinsic::annotation: |
| 397 | case llvm::Intrinsic::donothing: |
| 398 | case llvm::Intrinsic::assume: |
| 399 | case llvm::Intrinsic::expect: |
| 400 | return true; |
| 401 | default: |
| 402 | // Other intrinsics which may access the memory are not yet supported. |
| 403 | break; |
| 404 | } |
| 405 | } |
| 406 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 407 | return false; |
| 408 | } |
| 409 | |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 410 | bool ScopDetection::isInvariant(const Value &Val, const Region &Reg) const { |
| 411 | // A reference to function argument or constant value is invariant. |
| 412 | if (isa<Argument>(Val) || isa<Constant>(Val)) |
| 413 | return true; |
| 414 | |
| 415 | const Instruction *I = dyn_cast<Instruction>(&Val); |
| 416 | if (!I) |
| 417 | return false; |
| 418 | |
| 419 | if (!Reg.contains(I)) |
| 420 | return true; |
| 421 | |
| 422 | if (I->mayHaveSideEffects()) |
| 423 | return false; |
| 424 | |
| 425 | // When Val is a Phi node, it is likely not invariant. We do not check whether |
| 426 | // Phi nodes are actually invariant, we assume that Phi nodes are usually not |
| 427 | // invariant. Recursively checking the operators of Phi nodes would lead to |
| 428 | // infinite recursion. |
| 429 | if (isa<PHINode>(*I)) |
| 430 | return false; |
| 431 | |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 432 | for (const Use &Operand : I->operands()) |
Tobias Grosser | 1d19190 | 2014-03-03 13:13:55 +0000 | [diff] [blame] | 433 | if (!isInvariant(*Operand, Reg)) |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 434 | return false; |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 435 | |
| 436 | // When the instruction is a load instruction, check that no write to memory |
| 437 | // in the region aliases with the load. |
| 438 | if (const LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 439 | AliasAnalysis::Location Loc = AA->getLocation(LI); |
Johannes Doerfert | ca08c44 | 2015-02-21 16:18:28 +0000 | [diff] [blame] | 440 | |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 441 | // Check if any basic block in the region can modify the location pointed to |
| 442 | // by 'Loc'. If so, 'Val' is (likely) not invariant in the region. |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 443 | for (const BasicBlock *BB : Reg.blocks()) |
Tobias Grosser | 1d19190 | 2014-03-03 13:13:55 +0000 | [diff] [blame] | 444 | if (AA->canBasicBlockModify(*BB, Loc)) |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 445 | return false; |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | return true; |
| 449 | } |
| 450 | |
Sebastian Pop | 422e33f | 2014-06-03 18:16:31 +0000 | [diff] [blame] | 451 | MapInsnToMemAcc InsnToMemAcc; |
| 452 | |
Sebastian Pop | b57c099 | 2014-05-12 20:24:26 +0000 | [diff] [blame] | 453 | bool ScopDetection::hasAffineMemoryAccesses(DetectionContext &Context) const { |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 454 | Region &CurRegion = Context.CurRegion; |
| 455 | |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 456 | for (const SCEVUnknown *BasePointer : Context.NonAffineAccesses) { |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 457 | Value *BaseValue = BasePointer->getValue(); |
Sebastian Pop | 422e33f | 2014-06-03 18:16:31 +0000 | [diff] [blame] | 458 | ArrayShape *Shape = new ArrayShape(BasePointer); |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 459 | bool BasePtrHasNonAffine = false; |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 460 | |
| 461 | // First step: collect parametric terms in all array references. |
| 462 | SmallVector<const SCEV *, 4> Terms; |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 463 | for (const auto &Pair : Context.Accesses[BasePointer]) { |
| 464 | const SCEVAddRecExpr *AccessFunction = |
| 465 | dyn_cast<SCEVAddRecExpr>(Pair.second); |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 466 | |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 467 | if (AccessFunction) |
| 468 | AccessFunction->collectParametricTerms(*SE, Terms); |
| 469 | } |
Sebastian Pop | e8863b8 | 2014-05-12 19:02:02 +0000 | [diff] [blame] | 470 | |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 471 | // Second step: find array shape. |
Sebastian Pop | 422e33f | 2014-06-03 18:16:31 +0000 | [diff] [blame] | 472 | SE->findArrayDimensions(Terms, Shape->DelinearizedSizes, |
| 473 | Context.ElementSize[BasePointer]); |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 474 | |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 475 | // No array shape derived. |
| 476 | if (Shape->DelinearizedSizes.empty()) { |
| 477 | if (AllowNonAffine) |
| 478 | continue; |
Sebastian Pop | e8863b8 | 2014-05-12 19:02:02 +0000 | [diff] [blame] | 479 | |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 480 | for (const auto &Pair : Context.Accesses[BasePointer]) { |
| 481 | const Instruction *Insn = Pair.first; |
| 482 | const SCEV *AF = Pair.second; |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 483 | |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 484 | if (!isAffineExpr(&CurRegion, AF, *SE, BaseValue)) { |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 485 | invalid<ReportNonAffineAccess>(Context, /*Assert=*/true, AF, Insn, |
| 486 | BaseValue); |
| 487 | if (!KeepGoing) |
| 488 | return false; |
| 489 | } |
| 490 | } |
| 491 | continue; |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 492 | } |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 493 | |
| 494 | // Third step: compute the access functions for each subscript. |
| 495 | // |
| 496 | // We first store the resulting memory accesses in TempMemoryAccesses. Only |
| 497 | // if the access functions for all memory accesses have been successfully |
| 498 | // delinearized we continue. Otherwise, we either report a failure or, if |
| 499 | // non-affine accesses are allowed, we drop the information. In case the |
| 500 | // information is dropped the memory accesses need to be overapproximated |
| 501 | // when translated to a polyhedral representation. |
| 502 | MapInsnToMemAcc TempMemoryAccesses; |
| 503 | for (const auto &Pair : Context.Accesses[BasePointer]) { |
| 504 | const Instruction *Insn = Pair.first; |
| 505 | const SCEVAddRecExpr *AF = dyn_cast<SCEVAddRecExpr>(Pair.second); |
| 506 | bool IsNonAffine = false; |
| 507 | MemAcc *Acc = new MemAcc(Insn, Shape); |
| 508 | TempMemoryAccesses.insert({Insn, Acc}); |
| 509 | |
| 510 | if (!AF) { |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 511 | if (isAffineExpr(&CurRegion, Pair.second, *SE, BaseValue)) |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 512 | Acc->DelinearizedSubscripts.push_back(Pair.second); |
| 513 | else |
| 514 | IsNonAffine = true; |
| 515 | } else { |
| 516 | AF->computeAccessFunctions(*SE, Acc->DelinearizedSubscripts, |
| 517 | Shape->DelinearizedSizes); |
| 518 | if (Acc->DelinearizedSubscripts.size() == 0) |
| 519 | IsNonAffine = true; |
| 520 | for (const SCEV *S : Acc->DelinearizedSubscripts) |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 521 | if (!isAffineExpr(&CurRegion, S, *SE, BaseValue)) |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 522 | IsNonAffine = true; |
| 523 | } |
| 524 | |
| 525 | // (Possibly) report non affine access |
| 526 | if (IsNonAffine) { |
| 527 | BasePtrHasNonAffine = true; |
| 528 | if (!AllowNonAffine) |
Tobias Grosser | 021eaef | 2015-01-08 19:03:10 +0000 | [diff] [blame] | 529 | invalid<ReportNonAffineAccess>(Context, /*Assert=*/true, Pair.second, |
| 530 | Insn, BaseValue); |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 531 | if (!KeepGoing && !AllowNonAffine) |
| 532 | return false; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | if (!BasePtrHasNonAffine) |
| 537 | InsnToMemAcc.insert(TempMemoryAccesses.begin(), TempMemoryAccesses.end()); |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 538 | } |
Sebastian Pop | e8863b8 | 2014-05-12 19:02:02 +0000 | [diff] [blame] | 539 | return true; |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 542 | bool ScopDetection::isValidMemoryAccess(Instruction &Inst, |
| 543 | DetectionContext &Context) const { |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 544 | Region &CurRegion = Context.CurRegion; |
| 545 | |
Tobias Grosser | e5e171e | 2011-11-10 12:45:03 +0000 | [diff] [blame] | 546 | Value *Ptr = getPointerOperand(Inst); |
Sebastian Pop | 9f57c5b | 2013-04-10 04:05:18 +0000 | [diff] [blame] | 547 | Loop *L = LI->getLoopFor(Inst.getParent()); |
| 548 | const SCEV *AccessFunction = SE->getSCEVAtScope(Ptr, L); |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 549 | const SCEVUnknown *BasePointer; |
Tobias Grosser | e5e171e | 2011-11-10 12:45:03 +0000 | [diff] [blame] | 550 | Value *BaseValue; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 551 | |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 552 | BasePointer = dyn_cast<SCEVUnknown>(SE->getPointerBase(AccessFunction)); |
| 553 | |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 554 | if (!BasePointer) |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 555 | return invalid<ReportNoBasePtr>(Context, /*Assert=*/true, &Inst); |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 556 | |
| 557 | BaseValue = BasePointer->getValue(); |
| 558 | |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 559 | if (isa<UndefValue>(BaseValue)) |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 560 | return invalid<ReportUndefBasePtr>(Context, /*Assert=*/true, &Inst); |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 561 | |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 562 | // Check that the base address of the access is invariant in the current |
| 563 | // region. |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 564 | if (!isInvariant(*BaseValue, CurRegion)) |
Tobias Grosser | ab2227a | 2014-01-28 13:43:24 +0000 | [diff] [blame] | 565 | // Verification of this property is difficult as the independent blocks |
| 566 | // pass may introduce aliasing that we did not have when running the |
| 567 | // scop detection. |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 568 | return invalid<ReportVariantBasePtr>(Context, /*Assert=*/false, BaseValue, |
| 569 | &Inst); |
Tobias Grosser | 458fb78 | 2014-01-28 12:58:58 +0000 | [diff] [blame] | 570 | |
Tobias Grosser | b8710b5 | 2011-11-10 12:44:50 +0000 | [diff] [blame] | 571 | AccessFunction = SE->getMinusSCEV(AccessFunction, BasePointer); |
| 572 | |
Tobias Grosser | bcd4eff | 2014-09-13 14:47:40 +0000 | [diff] [blame] | 573 | const SCEV *Size = SE->getElementSize(&Inst); |
| 574 | if (Context.ElementSize.count(BasePointer)) { |
| 575 | if (Context.ElementSize[BasePointer] != Size) |
| 576 | return invalid<ReportDifferentArrayElementSize>(Context, /*Assert=*/true, |
| 577 | &Inst, BaseValue); |
| 578 | } else { |
| 579 | Context.ElementSize[BasePointer] = Size; |
| 580 | } |
| 581 | |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 582 | if (PollyDelinearize) { |
| 583 | Context.Accesses[BasePointer].push_back({&Inst, AccessFunction}); |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 584 | |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 585 | if (!isAffineExpr(&CurRegion, AccessFunction, *SE, BaseValue)) |
Tobias Grosser | 230acc4 | 2014-09-13 14:47:55 +0000 | [diff] [blame] | 586 | Context.NonAffineAccesses.insert(BasePointer); |
| 587 | } else if (!AllowNonAffine) { |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 588 | if (!isAffineExpr(&CurRegion, AccessFunction, *SE, BaseValue)) |
Sebastian Pop | cd3bb59 | 2014-04-10 16:08:11 +0000 | [diff] [blame] | 589 | return invalid<ReportNonAffineAccess>(Context, /*Assert=*/true, |
Andreas Simbuerger | d46b935 | 2014-08-17 10:09:11 +0000 | [diff] [blame] | 590 | AccessFunction, &Inst, BaseValue); |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 591 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 592 | |
| 593 | // FIXME: Alias Analysis thinks IntToPtrInst aliases with alloca instructions |
| 594 | // created by IndependentBlocks Pass. |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 595 | if (IntToPtrInst *Inst = dyn_cast<IntToPtrInst>(BaseValue)) |
| 596 | return invalid<ReportIntToPtr>(Context, /*Assert=*/true, Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 597 | |
Tobias Grosser | 1eedb67 | 2014-09-24 21:04:29 +0000 | [diff] [blame] | 598 | if (IgnoreAliasing) |
Sebastian Pop | 8c2d753 | 2013-07-03 22:50:36 +0000 | [diff] [blame] | 599 | return true; |
Tobias Grosser | fff5adc | 2011-11-10 13:21:43 +0000 | [diff] [blame] | 600 | |
Sebastian Pop | 8c2d753 | 2013-07-03 22:50:36 +0000 | [diff] [blame] | 601 | // Check if the base pointer of the memory access does alias with |
| 602 | // any other pointer. This cannot be handled at the moment. |
Benjamin Kramer | ae81abf | 2014-10-05 11:58:57 +0000 | [diff] [blame] | 603 | AAMDNodes AATags; |
| 604 | Inst.getAAMetadata(AATags); |
| 605 | AliasSet &AS = Context.AST.getAliasSetForPointer( |
| 606 | BaseValue, AliasAnalysis::UnknownSize, AATags); |
Tobias Grosser | 428b3e4 | 2013-02-04 15:46:25 +0000 | [diff] [blame] | 607 | |
Sebastian Pop | 8c2d753 | 2013-07-03 22:50:36 +0000 | [diff] [blame] | 608 | // INVALID triggers an assertion in verifying mode, if it detects that a |
| 609 | // SCoP was detected by SCoP detection and that this SCoP was invalidated by |
| 610 | // a pass that stated it would preserve the SCoPs. We disable this check as |
| 611 | // the independent blocks pass may create memory references which seem to |
| 612 | // alias, if -basicaa is not available. They actually do not, but as we can |
| 613 | // not proof this without -basicaa we would fail. We disable this check to |
| 614 | // not cause irrelevant verification failures. |
Tobias Grosser | 1eedb67 | 2014-09-24 21:04:29 +0000 | [diff] [blame] | 615 | if (!AS.isMustAlias()) { |
| 616 | if (PollyUseRuntimeAliasChecks) { |
| 617 | bool CanBuildRunTimeCheck = true; |
| 618 | // The run-time alias check places code that involves the base pointer at |
| 619 | // the beginning of the SCoP. This breaks if the base pointer is defined |
| 620 | // inside the scop. Hence, we can only create a run-time check if we are |
| 621 | // sure the base pointer is not an instruction defined inside the scop. |
| 622 | for (const auto &Ptr : AS) { |
| 623 | Instruction *Inst = dyn_cast<Instruction>(Ptr.getValue()); |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 624 | if (Inst && CurRegion.contains(Inst)) { |
Tobias Grosser | 1eedb67 | 2014-09-24 21:04:29 +0000 | [diff] [blame] | 625 | CanBuildRunTimeCheck = false; |
| 626 | break; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | if (CanBuildRunTimeCheck) |
| 631 | return true; |
| 632 | } |
Andreas Simbuerger | e2c9243 | 2014-06-26 10:19:57 +0000 | [diff] [blame] | 633 | return invalid<ReportAlias>(Context, /*Assert=*/false, &Inst, AS); |
Tobias Grosser | 1eedb67 | 2014-09-24 21:04:29 +0000 | [diff] [blame] | 634 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 635 | |
| 636 | return true; |
| 637 | } |
| 638 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 639 | bool ScopDetection::isValidInstruction(Instruction &Inst, |
| 640 | DetectionContext &Context) const { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 641 | if (PHINode *PN = dyn_cast<PHINode>(&Inst)) |
Johannes Doerfert | 0ff23ec | 2015-02-06 20:13:15 +0000 | [diff] [blame] | 642 | if (!PollyModelPHINodes && !canSynthesize(PN, LI, SE, &Context.CurRegion)) { |
Tobias Grosser | 683b8e4 | 2014-11-30 14:33:31 +0000 | [diff] [blame] | 643 | return invalid<ReportPhiNodeRefInRegion>(Context, /*Assert=*/true, &Inst); |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 644 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 645 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 646 | // We only check the call instruction but not invoke instruction. |
| 647 | if (CallInst *CI = dyn_cast<CallInst>(&Inst)) { |
| 648 | if (isValidCallInst(*CI)) |
| 649 | return true; |
| 650 | |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 651 | return invalid<ReportFuncCall>(Context, /*Assert=*/true, &Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | if (!Inst.mayWriteToMemory() && !Inst.mayReadFromMemory()) { |
Tobias Grosser | 5b1a7f2 | 2013-07-22 03:50:33 +0000 | [diff] [blame] | 655 | if (!isa<AllocaInst>(Inst)) |
| 656 | return true; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 657 | |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 658 | return invalid<ReportAlloca>(Context, /*Assert=*/true, &Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | // Check the access function. |
Tobias Grosser | d1e33e7 | 2015-02-19 05:31:07 +0000 | [diff] [blame] | 662 | if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst)) { |
| 663 | Context.hasStores |= isa<StoreInst>(Inst); |
| 664 | Context.hasLoads |= isa<LoadInst>(Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 665 | return isValidMemoryAccess(Inst, Context); |
Tobias Grosser | d1e33e7 | 2015-02-19 05:31:07 +0000 | [diff] [blame] | 666 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 667 | |
| 668 | // We do not know this instruction, therefore we assume it is invalid. |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 669 | return invalid<ReportUnknownInst>(Context, /*Assert=*/true, &Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 672 | bool ScopDetection::isValidLoop(Loop *L, DetectionContext &Context) const { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 673 | // Is the loop count affine? |
| 674 | const SCEV *LoopCount = SE->getBackedgeTakenCount(L); |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 675 | if (isAffineExpr(&Context.CurRegion, LoopCount, *SE)) |
| 676 | return true; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 677 | |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 678 | return invalid<ReportLoopBound>(Context, /*Assert=*/true, L, LoopCount); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | Region *ScopDetection::expandRegion(Region &R) { |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 682 | // Initial no valid region was found (greater than R) |
Tobias Grosser | 5a56cbf | 2014-04-16 07:33:47 +0000 | [diff] [blame] | 683 | Region *LastValidRegion = nullptr; |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 684 | Region *ExpandedRegion = R.getExpandedRegion(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 685 | |
| 686 | DEBUG(dbgs() << "\tExpanding " << R.getNameStr() << "\n"); |
| 687 | |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 688 | while (ExpandedRegion) { |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 689 | DetectionContext Context(*ExpandedRegion, *AA, |
| 690 | NonAffineSubRegionMap[ExpandedRegion], |
| 691 | false /* verifying */); |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 692 | DEBUG(dbgs() << "\t\tTrying " << ExpandedRegion->getNameStr() << "\n"); |
Andreas Simbuerger | b379edb | 2014-06-27 06:21:14 +0000 | [diff] [blame] | 693 | // Only expand when we did not collect errors. |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 694 | |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 695 | // Check the exit first (cheap) |
Andreas Simbuerger | b379edb | 2014-06-27 06:21:14 +0000 | [diff] [blame] | 696 | if (isValidExit(Context) && !Context.Log.hasErrors()) { |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 697 | // If the exit is valid check all blocks |
| 698 | // - if true, a valid region was found => store it + keep expanding |
| 699 | // - if false, .tbd. => stop (should this really end the loop?) |
Andreas Simbuerger | b379edb | 2014-06-27 06:21:14 +0000 | [diff] [blame] | 700 | if (!allBlocksValid(Context) || Context.Log.hasErrors()) |
| 701 | break; |
| 702 | |
| 703 | if (Context.Log.hasErrors()) |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 704 | break; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 705 | |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 706 | // Delete unnecessary regions (allocated by getExpandedRegion) |
| 707 | if (LastValidRegion) |
| 708 | delete LastValidRegion; |
| 709 | |
Tobias Grosser | d7e5864 | 2013-04-10 06:55:45 +0000 | [diff] [blame] | 710 | // Store this region, because it is the greatest valid (encountered so |
| 711 | // far). |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 712 | LastValidRegion = ExpandedRegion; |
| 713 | |
| 714 | // Create and test the next greater region (if any) |
| 715 | ExpandedRegion = ExpandedRegion->getExpandedRegion(); |
| 716 | |
| 717 | } else { |
| 718 | // Create and test the next greater region (if any) |
| 719 | Region *TmpRegion = ExpandedRegion->getExpandedRegion(); |
| 720 | |
| 721 | // Delete unnecessary regions (allocated by getExpandedRegion) |
| 722 | delete ExpandedRegion; |
| 723 | |
| 724 | ExpandedRegion = TmpRegion; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 725 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Tobias Grosser | 378a9f2 | 2013-11-16 19:34:11 +0000 | [diff] [blame] | 728 | DEBUG({ |
| 729 | if (LastValidRegion) |
| 730 | dbgs() << "\tto " << LastValidRegion->getNameStr() << "\n"; |
| 731 | else |
| 732 | dbgs() << "\tExpanding " << R.getNameStr() << " failed\n"; |
| 733 | }); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 734 | |
Hongbin Zheng | ed986ab | 2012-04-07 15:14:28 +0000 | [diff] [blame] | 735 | return LastValidRegion; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 736 | } |
Sebastian Pop | 2c9ec2e | 2013-06-03 16:35:37 +0000 | [diff] [blame] | 737 | static bool regionWithoutLoops(Region &R, LoopInfo *LI) { |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 738 | for (const BasicBlock *BB : R.blocks()) |
Tobias Grosser | 1d19190 | 2014-03-03 13:13:55 +0000 | [diff] [blame] | 739 | if (R.contains(LI->getLoopFor(BB))) |
Sebastian Pop | 2c9ec2e | 2013-06-03 16:35:37 +0000 | [diff] [blame] | 740 | return false; |
| 741 | |
| 742 | return true; |
| 743 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 744 | |
Tobias Grosser | 28a70c5 | 2014-01-29 19:05:30 +0000 | [diff] [blame] | 745 | // Remove all direct and indirect children of region R from the region set Regs, |
| 746 | // but do not recurse further if the first child has been found. |
| 747 | // |
| 748 | // Return the number of regions erased from Regs. |
David Peixotto | 8da2b93 | 2014-10-22 20:39:07 +0000 | [diff] [blame] | 749 | static unsigned eraseAllChildren(ScopDetection::RegionSet &Regs, |
David Blaikie | b035f6d | 2014-04-15 18:45:27 +0000 | [diff] [blame] | 750 | const Region &R) { |
Tobias Grosser | 28a70c5 | 2014-01-29 19:05:30 +0000 | [diff] [blame] | 751 | unsigned Count = 0; |
David Blaikie | b035f6d | 2014-04-15 18:45:27 +0000 | [diff] [blame] | 752 | for (auto &SubRegion : R) { |
David Peixotto | 8da2b93 | 2014-10-22 20:39:07 +0000 | [diff] [blame] | 753 | if (Regs.count(SubRegion.get())) { |
Tobias Grosser | 28a70c5 | 2014-01-29 19:05:30 +0000 | [diff] [blame] | 754 | ++Count; |
David Peixotto | 8da2b93 | 2014-10-22 20:39:07 +0000 | [diff] [blame] | 755 | Regs.remove(SubRegion.get()); |
Tobias Grosser | 28a70c5 | 2014-01-29 19:05:30 +0000 | [diff] [blame] | 756 | } else { |
David Blaikie | b035f6d | 2014-04-15 18:45:27 +0000 | [diff] [blame] | 757 | Count += eraseAllChildren(Regs, *SubRegion); |
Tobias Grosser | 28a70c5 | 2014-01-29 19:05:30 +0000 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | return Count; |
| 761 | } |
| 762 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 763 | void ScopDetection::findScops(Region &R) { |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 764 | DetectionContext Context(R, *AA, NonAffineSubRegionMap[&R], |
| 765 | false /*verifying*/); |
Johannes Doerfert | 6a4d81c | 2015-03-08 15:11:50 +0000 | [diff] [blame^] | 766 | |
| 767 | bool RegionIsValid = false; |
| 768 | if (!DetectRegionsWithoutLoops && regionWithoutLoops(R, LI)) |
| 769 | invalid<ReportUnprofitable>(Context, /*Assert=*/true, &R); |
| 770 | else |
| 771 | RegionIsValid = isValidRegion(Context); |
| 772 | |
Johannes Doerfert | 3f1c285 | 2015-02-19 18:11:50 +0000 | [diff] [blame] | 773 | bool HasErrors = !RegionIsValid || Context.Log.size() > 0; |
Andreas Simbuerger | 0447240 | 2014-05-24 09:25:10 +0000 | [diff] [blame] | 774 | |
Johannes Doerfert | 3f1c285 | 2015-02-19 18:11:50 +0000 | [diff] [blame] | 775 | if (PollyTrackFailures && HasErrors) |
| 776 | RejectLogs.insert(std::make_pair(&R, Context.Log)); |
| 777 | |
| 778 | if (!HasErrors) { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 779 | ++ValidRegion; |
| 780 | ValidRegions.insert(&R); |
| 781 | return; |
| 782 | } |
| 783 | |
David Blaikie | b035f6d | 2014-04-15 18:45:27 +0000 | [diff] [blame] | 784 | for (auto &SubRegion : R) |
Tobias Grosser | 00dc309 | 2014-03-02 12:02:46 +0000 | [diff] [blame] | 785 | findScops(*SubRegion); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 786 | |
| 787 | // Try to expand regions. |
| 788 | // |
| 789 | // As the region tree normally only contains canonical regions, non canonical |
| 790 | // regions that form a Scop are not found. Therefore, those non canonical |
| 791 | // regions are checked by expanding the canonical ones. |
| 792 | |
Tobias Grosser | 0d1eee3 | 2013-02-05 11:56:05 +0000 | [diff] [blame] | 793 | std::vector<Region *> ToExpand; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 794 | |
David Blaikie | b035f6d | 2014-04-15 18:45:27 +0000 | [diff] [blame] | 795 | for (auto &SubRegion : R) |
| 796 | ToExpand.push_back(SubRegion.get()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 797 | |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 798 | for (Region *CurrentRegion : ToExpand) { |
Andreas Simbuerger | b379edb | 2014-06-27 06:21:14 +0000 | [diff] [blame] | 799 | // Skip regions that had errors. |
| 800 | bool HadErrors = RejectLogs.hasErrors(CurrentRegion); |
| 801 | if (HadErrors) |
| 802 | continue; |
| 803 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 804 | // Skip invalid regions. Regions may become invalid, if they are element of |
| 805 | // an already expanded region. |
David Peixotto | 8da2b93 | 2014-10-22 20:39:07 +0000 | [diff] [blame] | 806 | if (!ValidRegions.count(CurrentRegion)) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 807 | continue; |
| 808 | |
| 809 | Region *ExpandedR = expandRegion(*CurrentRegion); |
| 810 | |
| 811 | if (!ExpandedR) |
| 812 | continue; |
| 813 | |
| 814 | R.addSubRegion(ExpandedR, true); |
| 815 | ValidRegions.insert(ExpandedR); |
David Peixotto | 8da2b93 | 2014-10-22 20:39:07 +0000 | [diff] [blame] | 816 | ValidRegions.remove(CurrentRegion); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 817 | |
Tobias Grosser | 28a70c5 | 2014-01-29 19:05:30 +0000 | [diff] [blame] | 818 | // Erase all (direct and indirect) children of ExpandedR from the valid |
| 819 | // regions and update the number of valid regions. |
David Blaikie | b035f6d | 2014-04-15 18:45:27 +0000 | [diff] [blame] | 820 | ValidRegion -= eraseAllChildren(ValidRegions, *ExpandedR); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 821 | } |
| 822 | } |
| 823 | |
| 824 | bool ScopDetection::allBlocksValid(DetectionContext &Context) const { |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 825 | Region &CurRegion = Context.CurRegion; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 826 | |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 827 | for (const BasicBlock *BB : CurRegion.blocks()) { |
Tobias Grosser | 1d19190 | 2014-03-03 13:13:55 +0000 | [diff] [blame] | 828 | Loop *L = LI->getLoopFor(BB); |
Andreas Simbuerger | 0447240 | 2014-05-24 09:25:10 +0000 | [diff] [blame] | 829 | if (L && L->getHeader() == BB && (!isValidLoop(L, Context) && !KeepGoing)) |
Sebastian Pop | b88ea5e | 2013-06-11 22:20:32 +0000 | [diff] [blame] | 830 | return false; |
| 831 | } |
| 832 | |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 833 | for (BasicBlock *BB : CurRegion.blocks()) |
Andreas Simbuerger | 0447240 | 2014-05-24 09:25:10 +0000 | [diff] [blame] | 834 | if (!isValidCFG(*BB, Context) && !KeepGoing) |
Sebastian Pop | 9e3d2dd | 2013-06-11 22:20:27 +0000 | [diff] [blame] | 835 | return false; |
| 836 | |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 837 | for (BasicBlock *BB : CurRegion.blocks()) |
Tobias Grosser | 1d19190 | 2014-03-03 13:13:55 +0000 | [diff] [blame] | 838 | for (BasicBlock::iterator I = BB->begin(), E = --BB->end(); I != E; ++I) |
Andreas Simbuerger | 0447240 | 2014-05-24 09:25:10 +0000 | [diff] [blame] | 839 | if (!isValidInstruction(*I, Context) && !KeepGoing) |
Sebastian Pop | 8ca899c | 2013-06-14 20:20:43 +0000 | [diff] [blame] | 840 | return false; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 841 | |
Sebastian Pop | e8863b8 | 2014-05-12 19:02:02 +0000 | [diff] [blame] | 842 | if (!hasAffineMemoryAccesses(Context)) |
Sebastian Pop | 46e1ecd | 2014-05-09 22:45:15 +0000 | [diff] [blame] | 843 | return false; |
| 844 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 845 | return true; |
| 846 | } |
| 847 | |
| 848 | bool ScopDetection::isValidExit(DetectionContext &Context) const { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 849 | |
| 850 | // PHI nodes are not allowed in the exit basic block. |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 851 | if (BasicBlock *Exit = Context.CurRegion.getExit()) { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 852 | BasicBlock::iterator I = Exit->begin(); |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 853 | if (I != Exit->end() && isa<PHINode>(*I)) |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 854 | return invalid<ReportPHIinExit>(Context, /*Assert=*/true, I); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | return true; |
| 858 | } |
| 859 | |
| 860 | bool ScopDetection::isValidRegion(DetectionContext &Context) const { |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 861 | Region &CurRegion = Context.CurRegion; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 862 | |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 863 | DEBUG(dbgs() << "Checking region: " << CurRegion.getNameStr() << "\n\t"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 864 | |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 865 | if (CurRegion.isTopLevelRegion()) { |
Tobias Grosser | f084edd | 2014-10-22 23:00:03 +0000 | [diff] [blame] | 866 | DEBUG(dbgs() << "Top level region is invalid\n"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 867 | return false; |
| 868 | } |
| 869 | |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 870 | if (!CurRegion.getEntry()->getName().count(OnlyRegion)) { |
Tobias Grosser | 4449e52 | 2014-01-27 14:24:53 +0000 | [diff] [blame] | 871 | DEBUG({ |
| 872 | dbgs() << "Region entry does not match -polly-region-only"; |
| 873 | dbgs() << "\n"; |
| 874 | }); |
| 875 | return false; |
| 876 | } |
| 877 | |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 878 | if (!CurRegion.getEnteringBlock()) { |
| 879 | BasicBlock *entry = CurRegion.getEntry(); |
Sebastian Pop | 9d63234 | 2013-06-11 22:20:40 +0000 | [diff] [blame] | 880 | Loop *L = LI->getLoopFor(entry); |
| 881 | |
| 882 | if (L) { |
Andreas Simbuerger | 01a37a0 | 2014-04-02 11:54:01 +0000 | [diff] [blame] | 883 | if (!L->isLoopSimplifyForm()) |
| 884 | return invalid<ReportSimpleLoop>(Context, /*Assert=*/true); |
Sebastian Pop | 9d63234 | 2013-06-11 22:20:40 +0000 | [diff] [blame] | 885 | |
| 886 | for (pred_iterator PI = pred_begin(entry), PE = pred_end(entry); PI != PE; |
| 887 | ++PI) { |
| 888 | // Region entering edges come from the same loop but outside the region |
| 889 | // are not allowed. |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 890 | if (L->contains(*PI) && !CurRegion.contains(*PI)) |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 891 | return invalid<ReportIndEdge>(Context, /*Assert=*/true, *PI); |
Sebastian Pop | 9d63234 | 2013-06-11 22:20:40 +0000 | [diff] [blame] | 892 | } |
| 893 | } |
Tobias Grosser | 8edce4e | 2013-04-16 08:04:42 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Tobias Grosser | d654c25 | 2012-04-10 18:12:19 +0000 | [diff] [blame] | 896 | // SCoP cannot contain the entry block of the function, because we need |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 897 | // to insert alloca instruction there when translate scalar to array. |
Johannes Doerfert | fb79a96 | 2015-02-23 14:18:28 +0000 | [diff] [blame] | 898 | if (CurRegion.getEntry() == |
| 899 | &(CurRegion.getEntry()->getParent()->getEntryBlock())) |
| 900 | return invalid<ReportEntry>(Context, /*Assert=*/true, CurRegion.getEntry()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 901 | |
Hongbin Zheng | 94868e6 | 2012-04-07 12:29:17 +0000 | [diff] [blame] | 902 | if (!isValidExit(Context)) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 903 | return false; |
| 904 | |
Hongbin Zheng | 94868e6 | 2012-04-07 12:29:17 +0000 | [diff] [blame] | 905 | if (!allBlocksValid(Context)) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 906 | return false; |
| 907 | |
Tobias Grosser | d1e33e7 | 2015-02-19 05:31:07 +0000 | [diff] [blame] | 908 | // We can probably not do a lot on scops that only write or only read |
| 909 | // data. |
| 910 | if (!DetectUnprofitable && (!Context.hasStores || !Context.hasLoads)) |
Johannes Doerfert | 6a4d81c | 2015-03-08 15:11:50 +0000 | [diff] [blame^] | 911 | invalid<ReportUnprofitable>(Context, /*Assert=*/true, &CurRegion); |
Tobias Grosser | d1e33e7 | 2015-02-19 05:31:07 +0000 | [diff] [blame] | 912 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 913 | DEBUG(dbgs() << "OK\n"); |
| 914 | return true; |
| 915 | } |
| 916 | |
Johannes Doerfert | 43e1ead | 2014-07-15 21:06:48 +0000 | [diff] [blame] | 917 | void ScopDetection::markFunctionAsInvalid(Function *F) const { |
| 918 | F->addFnAttr(PollySkipFnAttr); |
| 919 | } |
| 920 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 921 | bool ScopDetection::isValidFunction(llvm::Function &F) { |
Johannes Doerfert | 43e1ead | 2014-07-15 21:06:48 +0000 | [diff] [blame] | 922 | return !F.hasFnAttribute(PollySkipFnAttr); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 923 | } |
| 924 | |
Tobias Grosser | b2863ca | 2013-03-04 19:49:51 +0000 | [diff] [blame] | 925 | void ScopDetection::printLocations(llvm::Function &F) { |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 926 | for (const Region *R : *this) { |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 927 | unsigned LineEntry, LineExit; |
| 928 | std::string FileName; |
| 929 | |
Tobias Grosser | 00dc309 | 2014-03-02 12:02:46 +0000 | [diff] [blame] | 930 | getDebugLocation(R, LineEntry, LineExit, FileName); |
Tobias Grosser | 8519f89 | 2013-12-18 10:49:53 +0000 | [diff] [blame] | 931 | DiagnosticScopFound Diagnostic(F, FileName, LineEntry, LineExit); |
| 932 | F.getContext().diagnose(Diagnostic); |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 933 | } |
| 934 | } |
| 935 | |
Daniel Jasper | 8a1dea0 | 2014-10-27 19:45:31 +0000 | [diff] [blame] | 936 | void ScopDetection::emitMissedRemarksForValidRegions( |
| 937 | const Function &F, const RegionSet &ValidRegions) { |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 938 | for (const Region *R : ValidRegions) { |
| 939 | const Region *Parent = R->getParent(); |
| 940 | if (Parent && !Parent->isTopLevelRegion() && RejectLogs.count(Parent)) |
| 941 | emitRejectionRemarks(F, RejectLogs.at(Parent)); |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | void ScopDetection::emitMissedRemarksForLeaves(const Function &F, |
| 946 | const Region *R) { |
| 947 | for (const std::unique_ptr<Region> &Child : *R) { |
| 948 | bool IsValid = ValidRegions.count(Child.get()); |
| 949 | if (IsValid) |
| 950 | continue; |
| 951 | |
| 952 | bool IsLeaf = Child->begin() == Child->end(); |
| 953 | if (!IsLeaf) |
| 954 | emitMissedRemarksForLeaves(F, Child.get()); |
| 955 | else { |
| 956 | if (RejectLogs.count(Child.get())) { |
| 957 | emitRejectionRemarks(F, RejectLogs.at(Child.get())); |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | } |
| 962 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 963 | bool ScopDetection::runOnFunction(llvm::Function &F) { |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 964 | LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 965 | RI = &getAnalysis<RegionInfoPass>().getRegionInfo(); |
Sebastian Pop | 8fe6d11 | 2013-05-30 17:47:32 +0000 | [diff] [blame] | 966 | if (!DetectScopsWithoutLoops && LI->empty()) |
| 967 | return false; |
| 968 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 969 | AA = &getAnalysis<AliasAnalysis>(); |
| 970 | SE = &getAnalysis<ScalarEvolution>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 971 | Region *TopRegion = RI->getTopLevelRegion(); |
| 972 | |
Tobias Grosser | 2ff8723 | 2011-10-23 11:17:06 +0000 | [diff] [blame] | 973 | releaseMemory(); |
| 974 | |
Tobias Grosser | a3ab27e | 2014-05-07 11:23:32 +0000 | [diff] [blame] | 975 | if (OnlyFunction != "" && !F.getName().count(OnlyFunction)) |
Tobias Grosser | 2ff8723 | 2011-10-23 11:17:06 +0000 | [diff] [blame] | 976 | return false; |
| 977 | |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 978 | if (!isValidFunction(F)) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 979 | return false; |
| 980 | |
| 981 | findScops(*TopRegion); |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 982 | |
Andreas Simbuerger | 5569bf3 | 2014-06-26 10:06:40 +0000 | [diff] [blame] | 983 | // Only makes sense when we tracked errors. |
| 984 | if (PollyTrackFailures) { |
| 985 | emitMissedRemarksForValidRegions(F, ValidRegions); |
| 986 | emitMissedRemarksForLeaves(F, TopRegion); |
| 987 | } |
| 988 | |
| 989 | for (const Region *R : ValidRegions) |
| 990 | emitValidRemarks(F, R); |
| 991 | |
Johannes Doerfert | a05214f | 2014-10-15 23:24:28 +0000 | [diff] [blame] | 992 | if (ReportLevel) |
Tobias Grosser | b2863ca | 2013-03-04 19:49:51 +0000 | [diff] [blame] | 993 | printLocations(F); |
Tobias Grosser | 531891e | 2012-11-01 16:45:20 +0000 | [diff] [blame] | 994 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 995 | return false; |
| 996 | } |
| 997 | |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 998 | bool ScopDetection::isNonAffineSubRegion(const Region *SubR, |
| 999 | const Region *ScopR) const { |
| 1000 | return NonAffineSubRegionMap.lookup(ScopR).count(SubR); |
| 1001 | } |
| 1002 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1003 | void polly::ScopDetection::verifyRegion(const Region &R) const { |
| 1004 | assert(isMaxRegionInScop(R) && "Expect R is a valid region."); |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 1005 | NonAffineSubRegionSetTy DummyNonAffineSubRegionSet; |
| 1006 | DetectionContext Context(const_cast<Region &>(R), *AA, |
| 1007 | DummyNonAffineSubRegionSet, true /*verifying*/); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1008 | isValidRegion(Context); |
| 1009 | } |
| 1010 | |
| 1011 | void polly::ScopDetection::verifyAnalysis() const { |
Tobias Grosser | a168993 | 2014-02-18 18:49:49 +0000 | [diff] [blame] | 1012 | if (!VerifyScops) |
| 1013 | return; |
| 1014 | |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 1015 | for (const Region *R : ValidRegions) |
Tobias Grosser | 00dc309 | 2014-03-02 12:02:46 +0000 | [diff] [blame] | 1016 | verifyRegion(*R); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | void ScopDetection::getAnalysisUsage(AnalysisUsage &AU) const { |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 1020 | AU.addRequired<LoopInfoWrapperPass>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1021 | AU.addRequired<ScalarEvolution>(); |
| 1022 | // We also need AA and RegionInfo when we are verifying analysis. |
| 1023 | AU.addRequiredTransitive<AliasAnalysis>(); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 1024 | AU.addRequiredTransitive<RegionInfoPass>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1025 | AU.setPreservesAll(); |
| 1026 | } |
| 1027 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 1028 | void ScopDetection::print(raw_ostream &OS, const Module *) const { |
Tobias Grosser | 2610889 | 2014-04-02 20:18:19 +0000 | [diff] [blame] | 1029 | for (const Region *R : ValidRegions) |
Tobias Grosser | 00dc309 | 2014-03-02 12:02:46 +0000 | [diff] [blame] | 1030 | OS << "Valid Region for Scop: " << R->getNameStr() << '\n'; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1031 | |
| 1032 | OS << "\n"; |
| 1033 | } |
| 1034 | |
| 1035 | void ScopDetection::releaseMemory() { |
| 1036 | ValidRegions.clear(); |
Andreas Simbuerger | 4870e09 | 2014-05-24 09:25:01 +0000 | [diff] [blame] | 1037 | RejectLogs.clear(); |
Johannes Doerfert | ba65c16 | 2015-02-24 11:45:21 +0000 | [diff] [blame] | 1038 | NonAffineSubRegionMap.clear(); |
Andreas Simbuerger | 4870e09 | 2014-05-24 09:25:01 +0000 | [diff] [blame] | 1039 | |
Hongbin Zheng | 94c5df1 | 2011-05-06 02:38:20 +0000 | [diff] [blame] | 1040 | // Do not clear the invalid function set. |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | char ScopDetection::ID = 0; |
| 1044 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 1045 | Pass *polly::createScopDetectionPass() { return new ScopDetection(); } |
| 1046 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 1047 | INITIALIZE_PASS_BEGIN(ScopDetection, "polly-detect", |
| 1048 | "Polly - Detect static control parts (SCoPs)", false, |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 1049 | false); |
| 1050 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis); |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 1051 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 1052 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 1053 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution); |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 1054 | INITIALIZE_PASS_END(ScopDetection, "polly-detect", |
| 1055 | "Polly - Detect static control parts (SCoPs)", false, false) |