strength_reduction_pass.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright (c) 2017 Google Inc.
  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_STRENGTH_REDUCTION_PASS_H_
  15. #define SOURCE_OPT_STRENGTH_REDUCTION_PASS_H_
  16. #include "source/opt/def_use_manager.h"
  17. #include "source/opt/ir_context.h"
  18. #include "source/opt/module.h"
  19. #include "source/opt/pass.h"
  20. namespace spvtools {
  21. namespace opt {
  22. // See optimizer.hpp for documentation.
  23. class StrengthReductionPass : public Pass {
  24. public:
  25. const char* name() const override { return "strength-reduction"; }
  26. Status Process() override;
  27. private:
  28. // Replaces multiple by power of 2 with an equivalent bit shift.
  29. // Returns true if something changed.
  30. bool ReplaceMultiplyByPowerOf2(BasicBlock::iterator*);
  31. // Scan the types and constants in the module looking for the integer
  32. // types that we are
  33. // interested in. The shift operation needs a small unsigned integer. We
  34. // need to find
  35. // them or create them. We do not want duplicates.
  36. void FindIntTypesAndConstants();
  37. // Get the id for the given constant. If it does not exist, it will be
  38. // created. The parameter must be between 0 and 32 inclusive.
  39. uint32_t GetConstantId(uint32_t);
  40. // Replaces certain instructions in function bodies with presumably cheaper
  41. // ones. Returns true if something changed.
  42. bool ScanFunctions();
  43. // Type ids for the types of interest, or 0 if they do not exist.
  44. uint32_t int32_type_id_;
  45. uint32_t uint32_type_id_;
  46. // constant_ids[i] is the id for unsigned integer constant i.
  47. // We set the limit at 32 because a bit shift of a 32-bit integer does not
  48. // need a value larger than 32.
  49. uint32_t constant_ids_[33];
  50. };
  51. } // namespace opt
  52. } // namespace spvtools
  53. #endif // SOURCE_OPT_STRENGTH_REDUCTION_PASS_H_