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

Function MergeGenericAddSubArithmetic

source/opt/folding_rules.cpp:1534–1557  ·  view source on GitHub ↗

Folds addition of a subtraction where the subtrahend is equal to the other addend. Return a copy of the minuend. Accepts generic (const and non-const) operands. Cases: (a - b) + b = a b + (a - b) = a

Source from the content-addressed store, hash-verified

1532// (a - b) + b = a
1533// b + (a - b) = a
1534FoldingRule MergeGenericAddSubArithmetic() {
1535 return [](IRContext* context, Instruction* inst,
1536 const std::vector<const analysis::Constant*>&) {
1537 assert(inst->opcode() == spv::Op::OpFAdd ||
1538 inst->opcode() == spv::Op::OpIAdd);
1539 const analysis::Type* type =
1540 context->get_type_mgr()->GetType(inst->type_id());
1541
1542 if (type->IsCooperativeMatrix()) {
1543 return false;
1544 }
1545
1546 bool uses_float = HasFloatingPoint(type);
1547 if (uses_float && !inst->IsFloatingPointFoldingAllowed()) return false;
1548
1549 uint32_t width = ElementWidth(type);
1550 if (width != 32 && width != 64) return false;
1551
1552 uint32_t add_op0 = inst->GetSingleWordInOperand(0);
1553 uint32_t add_op1 = inst->GetSingleWordInOperand(1);
1554 if (MergeGenericAddendSub(add_op0, add_op1, inst)) return true;
1555 return MergeGenericAddendSub(add_op1, add_op0, inst);
1556 };
1557}
1558
1559// Helper function for FactorAddSubMuls.
1560// If |factor0_0| is the same as |factor1_0|, generate:

Callers 1

AddFoldingRulesMethod · 0.85

Calls 10

ElementWidthFunction · 0.85
MergeGenericAddendSubFunction · 0.85
get_type_mgrMethod · 0.80
IsCooperativeMatrixMethod · 0.80
HasFloatingPointFunction · 0.70
opcodeMethod · 0.45
GetTypeMethod · 0.45
type_idMethod · 0.45

Tested by

no test coverage detected