combine_access_chains.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_COMBINE_ACCESS_CHAINS_H_
  15. #define SOURCE_OPT_COMBINE_ACCESS_CHAINS_H_
  16. #include <vector>
  17. #include "source/opt/pass.h"
  18. namespace spvtools {
  19. namespace opt {
  20. // See optimizer.hpp for documentation.
  21. class CombineAccessChains : public Pass {
  22. public:
  23. const char* name() const override { return "combine-access-chains"; }
  24. Status Process() override;
  25. IRContext::Analysis GetPreservedAnalyses() override {
  26. return IRContext::kAnalysisDefUse |
  27. IRContext::kAnalysisInstrToBlockMapping |
  28. IRContext::kAnalysisDecorations | IRContext::kAnalysisCombinators |
  29. IRContext::kAnalysisCFG | IRContext::kAnalysisDominatorAnalysis |
  30. IRContext::kAnalysisNameMap | IRContext::kAnalysisConstants |
  31. IRContext::kAnalysisTypes;
  32. }
  33. private:
  34. // Combine access chains in |function|. Blocks are processed in reverse
  35. // post-order. Returns true if the function is modified.
  36. bool ProcessFunction(Function& function);
  37. // Combines an access chain (normal, in bounds or pointer) |inst| if its base
  38. // pointer is another access chain. Returns true if the access chain was
  39. // modified.
  40. bool CombineAccessChain(Instruction* inst);
  41. // Returns the value of |constant_inst| as a uint32_t.
  42. uint32_t GetConstantValue(const analysis::Constant* constant_inst);
  43. // Returns the array stride of |inst|'s type.
  44. uint32_t GetArrayStride(const Instruction* inst);
  45. // Returns the type by resolving the index operands |inst|. |inst| must be an
  46. // access chain instruction.
  47. const analysis::Type* GetIndexedType(Instruction* inst);
  48. // Populates |new_operands| with the operands for the combined access chain.
  49. // Returns false if the access chains cannot be combined.
  50. bool CreateNewInputOperands(Instruction* ptr_input, Instruction* inst,
  51. std::vector<Operand>* new_operands);
  52. // Combines the last index of |ptr_input| with the element operand of |inst|.
  53. // Adds the combined operand to |new_operands|.
  54. bool CombineIndices(Instruction* ptr_input, Instruction* inst,
  55. std::vector<Operand>* new_operands);
  56. // Returns the opcode to use for the combined access chain.
  57. spv::Op UpdateOpcode(spv::Op base_opcode, spv::Op input_opcode);
  58. // Returns true if |opcode| is a pointer access chain.
  59. bool IsPtrAccessChain(spv::Op opcode);
  60. // Returns true if |inst| (an access chain) has 64-bit indices.
  61. bool Has64BitIndices(Instruction* inst);
  62. };
  63. } // namespace opt
  64. } // namespace spvtools
  65. #endif // SOURCE_OPT_COMBINE_ACCESS_CHAINS_H_