if_conversion.h 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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_IF_CONVERSION_H_
  15. #define SOURCE_OPT_IF_CONVERSION_H_
  16. #include "source/opt/basic_block.h"
  17. #include "source/opt/ir_builder.h"
  18. #include "source/opt/pass.h"
  19. #include "source/opt/types.h"
  20. namespace spvtools {
  21. namespace opt {
  22. // See optimizer.hpp for documentation.
  23. class IfConversion : public Pass {
  24. public:
  25. const char* name() const override { return "if-conversion"; }
  26. Status Process() override;
  27. IRContext::Analysis GetPreservedAnalyses() override {
  28. return IRContext::kAnalysisDefUse | IRContext::kAnalysisDominatorAnalysis |
  29. IRContext::kAnalysisInstrToBlockMapping | IRContext::kAnalysisCFG |
  30. IRContext::kAnalysisNameMap | IRContext::kAnalysisConstants |
  31. IRContext::kAnalysisTypes;
  32. }
  33. private:
  34. // Returns true if |id| is a valid type for use with OpSelect. OpSelect only
  35. // allows scalars, vectors and pointers as valid inputs.
  36. bool CheckType(uint32_t id);
  37. // Returns the basic block containing |id|.
  38. BasicBlock* GetBlock(uint32_t id);
  39. // Returns the basic block for the |predecessor|'th index predecessor of
  40. // |phi|.
  41. BasicBlock* GetIncomingBlock(Instruction* phi, uint32_t predecessor);
  42. // Returns the instruction defining the |predecessor|'th index of |phi|.
  43. Instruction* GetIncomingValue(Instruction* phi, uint32_t predecessor);
  44. // Returns the id of a OpCompositeConstruct boolean vector. The composite has
  45. // the same number of elements as |vec_data_ty| and each member is |cond|.
  46. // |where| indicates the location in |block| to insert the composite
  47. // construct. If necessary, this function will also construct the necessary
  48. // type instructions for the boolean vector.
  49. uint32_t SplatCondition(analysis::Vector* vec_data_ty, uint32_t cond,
  50. InstructionBuilder* builder);
  51. // Returns true if none of |phi|'s users are in |block|.
  52. bool CheckPhiUsers(Instruction* phi, BasicBlock* block);
  53. // Returns |false| if |block| is not appropriate to transform. Only
  54. // transforms blocks with two predecessors. Neither incoming block can be
  55. // dominated by |block|. Both predecessors must share a common dominator that
  56. // is terminated by a conditional branch.
  57. bool CheckBlock(BasicBlock* block, DominatorAnalysis* dominators,
  58. BasicBlock** common);
  59. // Moves |inst| to |target_block| if it does not already dominate the block.
  60. // Any instructions that |inst| depends on are move if necessary. It is
  61. // assumed that |inst| can be hoisted to |target_block| as defined by
  62. // |CanHoistInstruction|. |dominators| is the dominator analysis for the
  63. // function that contains |target_block|.
  64. void HoistInstruction(Instruction* inst, BasicBlock* target_block,
  65. DominatorAnalysis* dominators);
  66. // Returns true if it is legal to move |inst| and the instructions it depends
  67. // on to |target_block| if they do not already dominate |target_block|.
  68. bool CanHoistInstruction(Instruction* inst, BasicBlock* target_block,
  69. DominatorAnalysis* dominators);
  70. };
  71. } // namespace opt
  72. } // namespace spvtools
  73. #endif // SOURCE_OPT_IF_CONVERSION_H_