licm_pass.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright (c) 2018 Google LLC.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef SOURCE_OPT_LICM_PASS_H_
  15. #define SOURCE_OPT_LICM_PASS_H_
  16. #include <queue>
  17. #include <vector>
  18. #include "source/opt/basic_block.h"
  19. #include "source/opt/instruction.h"
  20. #include "source/opt/loop_descriptor.h"
  21. #include "source/opt/pass.h"
  22. namespace spvtools {
  23. namespace opt {
  24. class LICMPass : public Pass {
  25. public:
  26. LICMPass() {}
  27. const char* name() const override { return "loop-invariant-code-motion"; }
  28. Status Process() override;
  29. private:
  30. // Searches the IRContext for functions and processes each, moving invariants
  31. // outside loops within the function where possible.
  32. // Returns the status depending on whether or not there was a failure or
  33. // change.
  34. Pass::Status ProcessIRContext();
  35. // Checks the function for loops, calling ProcessLoop on each one found.
  36. // Returns the status depending on whether or not there was a failure or
  37. // change.
  38. Pass::Status ProcessFunction(Function* f);
  39. // Checks for invariants in the loop and attempts to move them to the loops
  40. // preheader. Works from inner loop to outer when nested loops are found.
  41. // Returns the status depending on whether or not there was a failure or
  42. // change.
  43. Pass::Status ProcessLoop(Loop* loop, Function* f);
  44. // Analyses each instruction in |bb|, hoisting invariants to |pre_header_bb|.
  45. // Each child of |bb| wrt to |dom_tree| is pushed to |loop_bbs|
  46. // Returns the status depending on whether or not there was a failure or
  47. // change.
  48. Pass::Status AnalyseAndHoistFromBB(Loop* loop, Function* f, BasicBlock* bb,
  49. std::vector<BasicBlock*>* loop_bbs);
  50. // Returns true if |bb| is immediately contained in |loop|
  51. bool IsImmediatelyContainedInLoop(Loop* loop, Function* f, BasicBlock* bb);
  52. // Move the instruction to the preheader of |loop|.
  53. // This method will update the instruction to block mapping for the context
  54. bool HoistInstruction(Loop* loop, Instruction* inst);
  55. };
  56. } // namespace opt
  57. } // namespace spvtools
  58. #endif // SOURCE_OPT_LICM_PASS_H_