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

Function MergeNegateArithmetic

source/opt/folding_rules.cpp:407–433  ·  view source on GitHub ↗

Elides consecutive negate instructions.

Source from the content-addressed store, hash-verified

405
406// Elides consecutive negate instructions.
407FoldingRule MergeNegateArithmetic() {
408 return [](IRContext* context, Instruction* inst,
409 const std::vector<const analysis::Constant*>& constants) {
410 assert(inst->opcode() == spv::Op::OpFNegate ||
411 inst->opcode() == spv::Op::OpSNegate);
412 (void)constants;
413 const analysis::Type* type =
414 context->get_type_mgr()->GetType(inst->type_id());
415 if (HasFloatingPoint(type) && !inst->IsFloatingPointFoldingAllowed())
416 return false;
417
418 Instruction* op_inst =
419 context->get_def_use_mgr()->GetDef(inst->GetSingleWordInOperand(0u));
420 if (HasFloatingPoint(type) && !op_inst->IsFloatingPointFoldingAllowed())
421 return false;
422
423 if (op_inst->opcode() == inst->opcode()) {
424 // Elide negates.
425 inst->SetOpcode(spv::Op::OpCopyObject);
426 inst->SetInOperands(
427 {{SPV_OPERAND_TYPE_ID, {op_inst->GetSingleWordInOperand(0u)}}});
428 return true;
429 }
430
431 return false;
432 };
433}
434
435// Merges negate into a mul or div operation if that operation contains a
436// constant operand.

Callers 1

AddFoldingRulesMethod · 0.85

Calls 11

get_type_mgrMethod · 0.80
SetOpcodeMethod · 0.80
SetInOperandsMethod · 0.80
HasFloatingPointFunction · 0.70
opcodeMethod · 0.45
GetTypeMethod · 0.45
type_idMethod · 0.45
GetDefMethod · 0.45
get_def_use_mgrMethod · 0.45

Tested by

no test coverage detected