<commit_before>"""Change instructions having only constant operands to a constant.

This pass tends to leave dead instructions, so dead_code_elim should
be run after."""
import ir


def optimize_OpCompositeConstruct(module, inst):
    new_inst = ir.Instruction(module, 'OpConstantComposite', module.new_id(),
                              inst.type_id, inst.operands[:])
    module.add_global_inst(new_inst)
    return new_inst


def optimize_OpCompositeExtract(inst):
    result_inst = inst.operands[0].inst
    for index in inst.operands[1:]:
        result_inst = result_inst.operands[index].inst
    return result_inst


def optimize_OpVectorShuffle(module, inst):
    vec1_inst = inst.operands[0].inst
    vec1_len = len(vec1_inst.operands)
    vec2_inst = inst.operands[1].inst
    components = []
    for component in inst.operands[2:]:
        if component == 0xffffffff:
            # Undefined component, so we may choose any value, e.g. the
            # first element from vector 1.
            components.append(vec1_inst.operands[0])
        elif component < vec1_len:
            components.append(vec1_inst.operands[component])
        else:
            components.append(vec2_inst.operands[component - vec1_len])
    new_inst = ir.Instruction(module, 'OpConstantComposite', module.new_id(),
                              inst.type_id, components)
    module.add_global_inst(new_inst)
    return new_inst


def optimize_inst(module, inst):
    """Simplify one instruction"""
    for operand in inst.operands:
        if isinstance(operand, ir.Id):
            if operand.inst.op_name not in ir.CONSTANT_INSTRUCTIONS:
                return inst

    if inst.op_name == 'OpCompositeConstruct':
        inst = optimize_OpCompositeConstruct(module, inst)
    elif inst.op_name == 'OpCompositeExtract':
        inst = optimize_OpCompositeExtract(inst)
    elif inst.op_name == 'OpVectorShuffle':
        inst = optimize_OpVectorShuffle(module, inst)
<commit_msg>Do not duplicate constant vectors.<commit_after>"""Change instructions having only constant operands to a constant.

This pass tends to leave dead instructions, so dead_code_elim should
be run after."""
import ir


def get_or_create_const_composite(module, type_id, operands):
    """Get an OpConstantComposite instruction with given type/operands.

    An existing instruction is returned, or a new one is created if there
    is no such instruction already."""
    for inst in module.global_instructions.type_insts:
        if (inst.op_name == 'OpConstantComposite' and
                inst.type_id == type_id and
                inst.operands == operands):
            return inst
    new_inst = ir.Instruction(module, 'OpConstantComposite', module.new_id(),
                              type_id, operands[:])
    module.add_global_inst(new_inst)
    return new_inst


def optimize_OpCompositeConstruct(module, inst):
    return get_or_create_const_composite(module, inst.type_id, inst.operands)


def optimize_OpCompositeExtract(inst):
    result_inst = inst.operands[0].inst
    for index in inst.operands[1:]:
        result_inst = result_inst.operands[index].inst
    return result_inst


def optimize_OpVectorShuffle(module, inst):
    vec1_inst = inst.operands[0].inst
    vec1_len = len(vec1_inst.operands)
    vec2_inst = inst.operands[1].inst
    components = []
    for component in inst.operands[2:]:
        if component == 0xffffffff:
            # Undefined component, so we may choose any value, e.g. the
            # first element from vector 1.
            components.append(vec1_inst.operands[0])
        elif component < vec1_len:
            components.append(vec1_inst.operands[component])
        else:
            components.append(vec2_inst.operands[component - vec1_len])
    return get_or_create_const_composite(module, inst.type_id, components)


def optimize_inst(module, inst):
    """Simplify one instruction"""
    for operand in inst.operands:
        if isinstance(operand, ir.Id):
            if operand.inst.op_name not in ir.CONSTANT_INSTRUCTIONS:
                return inst

    if inst.op_name == 'OpCompositeConstruct':
        inst = optimize_OpCompositeConstruct(module, inst)
    elif inst.op_name == 'OpCompositeExtract':
        inst = optimize_OpCompositeExtract(inst)
    elif inst.op_name == 'OpVectorShuffle':
        inst = optimize_OpVectorShuffle(module, inst)

{'fraction_non_alphanumeric': 0.08765938069216758, 'fraction_numerical': 0.007741347905282332, 'mean_word_length': 3.0789229340761373, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7265992', 'n_tokens_mistral': 1307, 'n_tokens_neox': 1275, 'n_words': 315}