ScopeNestInfo.h 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // ScopeNestInfo.h //
  4. // Copyright (C) Microsoft Corporation. All rights reserved. //
  5. // This file is distributed under the University of Illinois Open Source //
  6. // License. See LICENSE.TXT for details. //
  7. // //
  8. // Implementation of ScopeNestInfo class and related transformation pass. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. // Pass to read the scope nest annotations in a cfg and provide a high-level
  12. // view of the scope nesting structure.
  13. //
  14. // The pass follows the same usage patter as the LLVM LoopInfo pass. We have
  15. // a ScopeNestInfo class that contains the results of the scope info
  16. // analysis. The ScopeNestInfoWrapperPass class is the pass implementation
  17. // that runs the analysis and saves the results so it can be queried by
  18. // a later pass.
  19. //
  20. // This pass requires the the -scopenestedcfg pass has been run prior to
  21. // running this pass because we rely on the cfg annotations added by the
  22. // scopenestedcfg pass.
  23. //
  24. // This pass is itself a thin wrapper around the ScopeNestIterator pass. The
  25. // iterator does the heavy lifting and we just cache the results of the
  26. // iteration here. We keep the iterator separate so that it can be easily
  27. // run outside the llvm pass infrastructure.
  28. //
  29. ///////////////////////////////////////////////////////////////////////////////
  30. #pragma once
  31. #include "llvm/Pass.h"
  32. #include "DxilConvPasses/ScopeNest.h"
  33. namespace llvm {
  34. class Function;
  35. class PassRegistry;
  36. class FunctionPass;
  37. llvm::FunctionPass *createScopeNestInfoWrapperPass();
  38. void initializeScopeNestInfoWrapperPassPass(llvm::PassRegistry&);
  39. // Class to hold the results of the scope nest analysis.
  40. //
  41. // Provides an iterator to examine the sequence of ScopeNestElements.
  42. // We could provide a higher-level view of the scope nesting if needed,
  43. // but that would probably build on the stream of elements anyway.
  44. //
  45. // This class is modeled after llvm LoopInfo.
  46. class ScopeNestInfo {
  47. public:
  48. typedef std::vector<ScopeNestEvent>::const_iterator elements_iterator;
  49. typedef iterator_range<elements_iterator> elements_iterator_range;
  50. elements_iterator elements_begin() { return m_scopeElements.begin(); }
  51. elements_iterator elements_end() { return m_scopeElements.end(); }
  52. elements_iterator_range elements(){ return elements_iterator_range(elements_begin(), elements_end()); }
  53. void Analyze(Function &F);
  54. void print(raw_ostream &O) const;
  55. void releaseMemory();
  56. private:
  57. std::vector<ScopeNestEvent> m_scopeElements;
  58. raw_ostream &indent(raw_ostream &O, int level, StringRef str) const;
  59. };
  60. // The legacy pass manager's analysis pass to read scope nest annotation information.
  61. //
  62. // This class is modeled after the llvm LoopInfoWrapperPass.
  63. class ScopeNestInfoWrapperPass : public FunctionPass {
  64. ScopeNestInfo SI;
  65. public:
  66. static char ID; // Pass identification, replacement for typeid
  67. ScopeNestInfoWrapperPass() : FunctionPass(ID) {
  68. initializeScopeNestInfoWrapperPassPass(*PassRegistry::getPassRegistry());
  69. }
  70. ScopeNestInfo &getScopeNestedInfo() { return SI; }
  71. const ScopeNestInfo &getScopeNestedInfo() const { return SI; }
  72. // Read the scope nest annotation information for a given function.
  73. bool runOnFunction(Function &F) override;
  74. void releaseMemory() override;
  75. void print(raw_ostream &O, const Module *M = nullptr) const override;
  76. void getAnalysisUsage(AnalysisUsage &AU) const override;
  77. };
  78. }