2
0

Pass.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. //===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===//
  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. // This file implements the LLVM Pass infrastructure. It is primarily
  11. // responsible with ensuring that passes are executed and batched together
  12. // optimally.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/Pass.h"
  16. #include "llvm/IR/Function.h"
  17. #include "llvm/IR/IRPrintingPasses.h"
  18. #include "llvm/IR/LegacyPassNameParser.h"
  19. #include "llvm/PassRegistry.h"
  20. #include "llvm/Support/Debug.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. #include <algorithm> // HLSL Change
  23. using namespace llvm;
  24. #define DEBUG_TYPE "ir"
  25. //===----------------------------------------------------------------------===//
  26. // Pass Implementation
  27. //
  28. // Force out-of-line virtual method.
  29. Pass::~Pass() {
  30. delete Resolver;
  31. }
  32. // HLSL Change Starts
  33. void Pass::dumpConfig(llvm::raw_ostream &OS) {
  34. OS << '-' << this->getPassArgument();
  35. }
  36. const char *Pass::getPassArgument() const {
  37. AnalysisID AID = getPassID();
  38. const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
  39. if (PI)
  40. return PI->getPassArgument();
  41. return "Unnamed pass: implement Pass::getPassArgument()";
  42. }
  43. // HLSL Change Ends
  44. // Force out-of-line virtual method.
  45. ModulePass::~ModulePass() { }
  46. Pass *ModulePass::createPrinterPass(raw_ostream &O,
  47. const std::string &Banner) const {
  48. return createPrintModulePass(O, Banner);
  49. }
  50. PassManagerType ModulePass::getPotentialPassManagerType() const {
  51. return PMT_ModulePassManager;
  52. }
  53. bool Pass::mustPreserveAnalysisID(char &AID) const {
  54. return Resolver->getAnalysisIfAvailable(&AID, true) != nullptr;
  55. }
  56. // dumpPassStructure - Implement the -debug-pass=Structure option
  57. void Pass::dumpPassStructure(unsigned Offset) {
  58. dbgs().indent(Offset*2) << getPassName() << "\n";
  59. }
  60. /// getPassName - Return a nice clean name for a pass. This usually
  61. /// implemented in terms of the name that is registered by one of the
  62. /// Registration templates, but can be overloaded directly.
  63. ///
  64. const char *Pass::getPassName() const {
  65. AnalysisID AID = getPassID();
  66. const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
  67. if (PI)
  68. return PI->getPassName();
  69. return "Unnamed pass: implement Pass::getPassName()";
  70. }
  71. void Pass::preparePassManager(PMStack &) {
  72. // By default, don't do anything.
  73. }
  74. PassManagerType Pass::getPotentialPassManagerType() const {
  75. // Default implementation.
  76. return PMT_Unknown;
  77. }
  78. void Pass::getAnalysisUsage(AnalysisUsage &) const {
  79. // By default, no analysis results are used, all are invalidated.
  80. }
  81. void Pass::releaseMemory() {
  82. // By default, don't do anything.
  83. }
  84. void Pass::verifyAnalysis() const {
  85. // By default, don't do anything.
  86. }
  87. void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
  88. return this;
  89. }
  90. ImmutablePass *Pass::getAsImmutablePass() {
  91. return nullptr;
  92. }
  93. PMDataManager *Pass::getAsPMDataManager() {
  94. return nullptr;
  95. }
  96. void Pass::setResolver(AnalysisResolver *AR) {
  97. assert(!Resolver && "Resolver is already set");
  98. Resolver = AR;
  99. }
  100. // print - Print out the internal state of the pass. This is called by Analyze
  101. // to print out the contents of an analysis. Otherwise it is not necessary to
  102. // implement this method.
  103. //
  104. void Pass::print(raw_ostream &O,const Module*) const {
  105. O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
  106. }
  107. // dump - call print(cerr);
  108. void Pass::dump() const {
  109. print(dbgs(), nullptr);
  110. }
  111. //===----------------------------------------------------------------------===//
  112. // ImmutablePass Implementation
  113. //
  114. // Force out-of-line virtual method.
  115. ImmutablePass::~ImmutablePass() { }
  116. void ImmutablePass::initializePass() {
  117. // By default, don't do anything.
  118. }
  119. //===----------------------------------------------------------------------===//
  120. // FunctionPass Implementation
  121. //
  122. Pass *FunctionPass::createPrinterPass(raw_ostream &O,
  123. const std::string &Banner) const {
  124. return createPrintFunctionPass(O, Banner);
  125. }
  126. PassManagerType FunctionPass::getPotentialPassManagerType() const {
  127. return PMT_FunctionPassManager;
  128. }
  129. bool FunctionPass::skipOptnoneFunction(const Function &F) const {
  130. if (F.hasFnAttribute(Attribute::OptimizeNone)) {
  131. DEBUG(dbgs() << "Skipping pass '" << getPassName()
  132. << "' on function " << F.getName() << "\n");
  133. return true;
  134. }
  135. return false;
  136. }
  137. //===----------------------------------------------------------------------===//
  138. // BasicBlockPass Implementation
  139. //
  140. Pass *BasicBlockPass::createPrinterPass(raw_ostream &O,
  141. const std::string &Banner) const {
  142. return createPrintBasicBlockPass(O, Banner);
  143. }
  144. bool BasicBlockPass::doInitialization(Function &) {
  145. // By default, don't do anything.
  146. return false;
  147. }
  148. bool BasicBlockPass::doFinalization(Function &) {
  149. // By default, don't do anything.
  150. return false;
  151. }
  152. bool BasicBlockPass::skipOptnoneFunction(const BasicBlock &BB) const {
  153. const Function *F = BB.getParent();
  154. if (F && F->hasFnAttribute(Attribute::OptimizeNone)) {
  155. // Report this only once per function.
  156. if (&BB == &F->getEntryBlock())
  157. DEBUG(dbgs() << "Skipping pass '" << getPassName()
  158. << "' on function " << F->getName() << "\n");
  159. return true;
  160. }
  161. return false;
  162. }
  163. PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
  164. return PMT_BasicBlockPassManager;
  165. }
  166. const PassInfo *Pass::lookupPassInfo(const void *TI) {
  167. return PassRegistry::getPassRegistry()->getPassInfo(TI);
  168. }
  169. const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
  170. return PassRegistry::getPassRegistry()->getPassInfo(Arg);
  171. }
  172. Pass *Pass::createPass(AnalysisID ID) {
  173. const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
  174. if (!PI)
  175. return nullptr;
  176. return PI->createPass();
  177. }
  178. //===----------------------------------------------------------------------===//
  179. // Analysis Group Implementation Code
  180. //===----------------------------------------------------------------------===//
  181. // RegisterAGBase implementation
  182. //
  183. RegisterAGBase::RegisterAGBase(const char *Name, const void *InterfaceID,
  184. const void *PassID, bool isDefault)
  185. : PassInfo(Name, InterfaceID) {
  186. PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
  187. *this, isDefault);
  188. }
  189. //===----------------------------------------------------------------------===//
  190. // PassRegistrationListener implementation
  191. //
  192. // enumeratePasses - Iterate over the registered passes, calling the
  193. // passEnumerate callback on each PassInfo object.
  194. //
  195. void PassRegistrationListener::enumeratePasses() {
  196. PassRegistry::getPassRegistry()->enumerateWith(this);
  197. }
  198. PassNameParser::PassNameParser(cl::Option &O)
  199. : cl::parser<const PassInfo *>(O) {
  200. PassRegistry::getPassRegistry()->addRegistrationListener(this);
  201. }
  202. PassNameParser::~PassNameParser() {
  203. // This only gets called during static destruction, in which case the
  204. // PassRegistry will have already been destroyed by llvm_shutdown(). So
  205. // attempting to remove the registration listener is an error.
  206. }
  207. //===----------------------------------------------------------------------===//
  208. // AnalysisUsage Class Implementation
  209. //
  210. namespace {
  211. struct GetCFGOnlyPasses : public PassRegistrationListener {
  212. typedef AnalysisUsage::VectorType VectorType;
  213. VectorType &CFGOnlyList;
  214. GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
  215. void passEnumerate(const PassInfo *P) override {
  216. if (P->isCFGOnlyPass())
  217. CFGOnlyList.push_back(P->getTypeInfo());
  218. }
  219. };
  220. }
  221. // setPreservesCFG - This function should be called to by the pass, iff they do
  222. // not:
  223. //
  224. // 1. Add or remove basic blocks from the function
  225. // 2. Modify terminator instructions in any way.
  226. //
  227. // This function annotates the AnalysisUsage info object to say that analyses
  228. // that only depend on the CFG are preserved by this pass.
  229. //
  230. void AnalysisUsage::setPreservesCFG() {
  231. // Since this transformation doesn't modify the CFG, it preserves all analyses
  232. // that only depend on the CFG (like dominators, loop info, etc...)
  233. GetCFGOnlyPasses(Preserved).enumeratePasses();
  234. }
  235. AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
  236. const PassInfo *PI = Pass::lookupPassInfo(Arg);
  237. // If the pass exists, preserve it. Otherwise silently do nothing.
  238. if (PI) Preserved.push_back(PI->getTypeInfo());
  239. return *this;
  240. }
  241. AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
  242. Required.push_back(ID);
  243. return *this;
  244. }
  245. AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
  246. Required.push_back(&ID);
  247. return *this;
  248. }
  249. AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
  250. Required.push_back(&ID);
  251. RequiredTransitive.push_back(&ID);
  252. return *this;
  253. }
  254. // HLSL Changes Start
  255. bool llvm::GetPassOption(PassOptions &O, StringRef name, StringRef *pValue) {
  256. PassOption OpVal(name, StringRef());
  257. const PassOption *Op = std::lower_bound(O.begin(), O.end(), OpVal, PassOptionsCompare());
  258. if (Op != O.end() && Op->first == name) {
  259. *pValue = Op->second;
  260. return true;
  261. }
  262. return false;
  263. }
  264. bool llvm::GetPassOptionBool(PassOptions &O, llvm::StringRef name, bool *pValue, bool defaultValue) {
  265. StringRef val;
  266. if (GetPassOption(O, name, &val)) {
  267. *pValue = (val.startswith_lower("t") || val.equals("1"));
  268. return true;
  269. }
  270. *pValue = defaultValue;
  271. return false;
  272. }
  273. bool llvm::GetPassOptionUnsigned(PassOptions &O, llvm::StringRef name, unsigned *pValue, unsigned defaultValue) {
  274. StringRef val;
  275. if (GetPassOption(O, name, &val)) {
  276. val.getAsInteger<unsigned>(0, *pValue);
  277. return true;
  278. }
  279. *pValue = defaultValue;
  280. return false;
  281. }
  282. bool llvm::GetPassOptionInt(PassOptions &O, llvm::StringRef name, int *pValue, int defaultValue) {
  283. StringRef val;
  284. if (GetPassOption(O, name, &val)) {
  285. val.getAsInteger<int>(0, *pValue);
  286. return true;
  287. }
  288. *pValue = defaultValue;
  289. return false;
  290. }
  291. bool llvm::GetPassOptionUInt32(PassOptions &O, llvm::StringRef name, uint32_t *pValue, uint32_t defaultValue) {
  292. StringRef val;
  293. if (GetPassOption(O, name, &val)) {
  294. val.getAsInteger<uint32_t>(0, *pValue);
  295. return true;
  296. }
  297. *pValue = defaultValue;
  298. return false;
  299. }
  300. bool llvm::GetPassOptionUInt64(PassOptions &O, llvm::StringRef name, uint64_t *pValue, uint64_t defaultValue) {
  301. StringRef val;
  302. if (GetPassOption(O, name, &val)) {
  303. val.getAsInteger<uint64_t>(0, *pValue);
  304. return true;
  305. }
  306. *pValue = defaultValue;
  307. return false;
  308. }
  309. bool llvm::GetPassOptionFloat(PassOptions &O, llvm::StringRef name, float *pValue, float defaultValue) {
  310. StringRef val;
  311. if (GetPassOption(O, name, &val)) {
  312. std::string str;
  313. str.assign(val.begin(), val.end());
  314. *pValue = atof(str.c_str());
  315. return true;
  316. }
  317. *pValue = defaultValue;
  318. return false;
  319. }
  320. // HLSL Changes End