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