MCPcopy Create free account
hub / github.com/KhronosGroup/SPIRV-Tools / ForEachIntegerConstantPair

Function ForEachIntegerConstantPair

source/opt/folding_rules.cpp:3863–3913  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3861// The constants |input1| and |input2| must be integers or a vector of integers.
3862template <typename Callback>
3863void ForEachIntegerConstantPair(analysis::ConstantManager* const_mgr,
3864 const analysis::Constant* input1,
3865 const analysis::Constant* input2,
3866 Callback&& callback) {
3867 assert(input1 && input2);
3868
3869 auto Dispatch = [&callback](const analysis::Constant* lhs,
3870 const analysis::Constant* rhs) {
3871 assert(lhs->type()->AsInteger());
3872 const analysis::Integer* type = lhs->type()->AsInteger();
3873 uint32_t width = type->AsInteger()->width();
3874 assert(width == 32 || width == 64);
3875 if (width == 32) {
3876 callback(lhs->GetU32(), rhs->GetU32());
3877 } else {
3878 callback(lhs->GetU64(), rhs->GetU64());
3879 }
3880 };
3881
3882 const analysis::Type* type = input1->type();
3883 if (const analysis::Vector* vector_type = type->AsVector()) {
3884 const analysis::Type* ele_type = vector_type->element_type();
3885 assert(ele_type->AsInteger());
3886 for (uint32_t i = 0; i != vector_type->element_count(); ++i) {
3887 const analysis::Constant* input1_comp = nullptr;
3888 if (const analysis::VectorConstant* input1_vector =
3889 input1->AsVectorConstant()) {
3890 input1_comp = input1_vector->GetComponents()[i];
3891 } else {
3892 assert(input1->AsNullConstant());
3893 input1_comp = const_mgr->GetConstant(ele_type, {});
3894 }
3895
3896 const analysis::Constant* input2_comp = nullptr;
3897 if (const analysis::VectorConstant* input2_vector =
3898 input2->AsVectorConstant()) {
3899 input2_comp = input2_vector->GetComponents()[i];
3900 } else {
3901 assert(input2->AsNullConstant());
3902 input2_comp = const_mgr->GetConstant(ele_type, {});
3903 }
3904
3905 assert(ele_type->AsInteger());
3906 Dispatch(input1_comp, input2_comp);
3907 }
3908
3909 } else {
3910 assert(type->AsInteger());
3911 Dispatch(input1, input2);
3912 }
3913}
3914
3915// Folds redundant xor and or ops that are part of an and.
3916// Cases handled:

Callers 2

RedundantAndOrXorFunction · 0.85
RedundantAndAddSubFunction · 0.85

Calls 11

AsIntegerMethod · 0.80
GetU32Method · 0.80
GetU64Method · 0.80
AsVectorMethod · 0.80
typeMethod · 0.45
widthMethod · 0.45
element_typeMethod · 0.45
element_countMethod · 0.45
AsVectorConstantMethod · 0.45
AsNullConstantMethod · 0.45
GetConstantMethod · 0.45

Tested by

no test coverage detected