def _phase_0(code): instructions = code.instructions ''' Generate flow graph ''' flow_graph = cfg.generate(instructions) ''' Get reachability information ''' reach_ins, reach_outs = fr.analyse(flow_graph, code.argument_fields, []) ''' Cherry-pick dead instructions ''' instruction_list = [] for _, node in flow_graph.iteritems(): for instruction in node.basic_block: if isinstance(instruction, I.ADD): if is_reserved_or_argument_field(instruction.field.field, code.argument_fields): pass else: if get_add_instruction_count(code.instructions, instruction.field.field) < 2: pass elif not (instruction.field.field in reach_ins[instruction]): instruction_list.append(instruction) ''' Remove dead instructions from the code ''' for instruction in instruction_list: instructions.remove(instruction)
def _phase_2(code): instructions = code.instructions ''' Generate flow graph ''' flow_graph = cfg.generate(instructions) ''' Get reachability information ''' reach_ins, reach_outs = fr.analyse(flow_graph, code.argument_fields, [I.ADD]) ''' Cherry-pick dead instructions ''' instruction_list = [] for _, node in flow_graph.iteritems(): for instruction in node.basic_block: if isinstance(instruction, I.RMV): if is_reserved_or_argument_field(instruction.field.field, code.argument_fields): pass elif not (instruction.field.field in reach_ins[instruction]): instruction_list.append(instruction) ''' Remove dead instructions from the code ''' for instruction in instruction_list: instructions.remove(instruction)
def _phase_2(code): instructions = code.instructions """ Generate flow graph """ flow_graph = cfg.generate(instructions) """ Get reachability information """ reach_ins, reach_outs = fr.analyse(flow_graph, code.argument_fields, [I.ADD]) """ Cherry-pick dead instructions """ instruction_list = [] for _, node in flow_graph.iteritems(): for instruction in node.basic_block: if isinstance(instruction, I.RMV): if is_reserved_or_argument_field(instruction.field.field, code.argument_fields): pass elif not (instruction.field.field in reach_ins[instruction]): instruction_list.append(instruction) """ Remove dead instructions from the code """ for instruction in instruction_list: instructions.remove(instruction)
def cost_Code(code, state): argument_fields = code.argument_fields instructions = code.instructions ''' Generate control flow graph ''' flow_graph = cfg.generate(instructions) ''' Get reaching definitions information (for I.ADD only) ''' reach_def_ins, reach_def_outs = rd.analyse( flow_graph, [], [I.LD, I.OP, I.LDt, I.LKt, I.CRC, I.HSH, I.CNC, I.ATM, I.SEQ]) ''' Get field reachability information ''' reach_ins, reach_outs = fr.analyse( flow_graph, [], [I.LD, I.OP, I.LDt, I.LKt, I.CRC, I.HSH, I.CNC, I.ATM, I.SEQ]) instruction_dict = {} for _, node in flow_graph.iteritems(): for instruction in node.basic_block: if isinstance(instruction, I.ID): state.area += get_header_size(reach_ins[instruction], reach_def_ins[instruction], argument_fields) state.latency += 1 elif isinstance(instruction, I.DRP): state.area += get_header_size(reach_ins[instruction], reach_def_ins[instruction], argument_fields) state.latency += 1 elif isinstance(instruction, I.CTR): state.area += get_header_size(reach_ins[instruction], reach_def_ins[instruction], argument_fields) state.latency += 1 elif isinstance(instruction, I.ADD): pass elif isinstance(instruction, I.RMV): pass elif isinstance(instruction, I.LD): if isinstance(instruction.source, O.Value) or isinstance( instruction.source, O.Field): state.area += get_header_size(reach_ins[instruction], reach_def_ins[instruction], argument_fields) state.latency += 1 elif isinstance(instruction.source, O.Location): state.area += get_header_size(reach_ins[instruction], reach_def_ins[instruction], argument_fields) * 2 state.latency += 2 elif isinstance(instruction, I.ST): state.area += get_header_size(reach_ins[instruction], reach_def_ins[instruction], argument_fields) state.latency += 1 elif isinstance(instruction, I.OP): if not (instruction.operator == Op.Mul or instruction.operator == Op.Div): state.area += get_header_size(reach_ins[instruction], reach_def_ins[instruction], argument_fields) state.latency += 1 else: state.area += get_header_size(reach_ins[instruction], reach_def_ins[instruction], argument_fields) * 2 state.latency += 2 elif isinstance(instruction, I.PUSH): state.area += get_header_size(reach_ins[instruction], reach_def_ins[instruction], argument_fields) state.latency += 1 elif isinstance(instruction, I.POP): state.area += get_header_size(reach_ins[instruction], reach_def_ins[instruction], argument_fields) state.latency += 1 elif isinstance(instruction, I.BR): state.area += get_header_size(reach_ins[instruction], reach_def_ins[instruction], argument_fields) state.latency += 1 elif isinstance(instruction, I.JMP): state.area += get_header_size(reach_ins[instruction], reach_def_ins[instruction], argument_fields) state.latency += 1 elif isinstance(instruction, I.LBL): pass elif isinstance(instruction, I.LDt): _, _, table_type = state.tables[instruction.table_id] if table_type == syntax.TableTypeCollection.RAM: state.area += get_header_size(reach_ins[instruction], reach_def_ins[instruction], argument_fields) * 3 state.latency += 3 else: raise RuntimeError("invalid table type (%s)." % table_type) # Note: # 1) tables are not added in the "area" cost as they will incur a one time cost for the # whole program # 2) it only operates on RAM type elif isinstance(instruction, I.STt): _, _, table_type = state.tables[instruction.table_id] if table_type == syntax.TableTypeCollection.RAM: state.area += get_header_size(reach_ins[instruction], reach_def_ins[instruction], argument_fields) * 3 state.latency += 3 # elif table_type == syntax.TableTypeCollection.HSH: # latency += 2 elif table_type == syntax.TableTypeCollection.CAM: state.area += get_header_size(reach_ins[instruction], reach_def_ins[instruction], argument_fields) * 4 state.latency += 4 else: raise RuntimeError("invalid table type (%s)." % table_type) elif isinstance(instruction, I.INCt): _, _, table_type = state.tables[instruction.table_id] state.area += get_header_size(reach_ins[instruction], reach_def_ins[instruction], argument_fields) if table_type == syntax.TableTypeCollection.RAM: state.area += get_header_size(reach_ins[instruction], reach_def_ins[instruction], argument_fields) * 3 state.latency += 3 else: raise RuntimeError("invalid table type (%s)." % table_type) elif isinstance(instruction, I.LKt): _, _, table_type = state.tables[instruction.table_id] if table_type == syntax.TableTypeCollection.RAM: state.area += get_header_size(reach_ins[instruction], reach_def_ins[instruction], argument_fields) * 3 state.latency += 3 # elif table_type == syntax.TableTypeCollection.HSH: # latency += 2 elif table_type == syntax.TableTypeCollection.CAM: state.area += get_header_size(reach_ins[instruction], reach_def_ins[instruction], argument_fields) * 4 state.latency += 4 else: raise RuntimeError("invalid table type (%s)." % table_type) elif isinstance(instruction, I.CRC): state.area += get_header_size(reach_ins[instruction], reach_def_ins[instruction], argument_fields) state.latency += 1 elif isinstance(instruction, I.HSH): state.area += get_header_size(reach_ins[instruction], reach_def_ins[instruction], argument_fields) state.latency += 1 elif isinstance(instruction, I.CNC): for code in instruction.codes: cost_Code(code, state) elif isinstance(instruction, I.ATM): cost_Code(instruction.code, state) elif isinstance(instruction, I.SEQ): cost_Code(instruction.code, state)
def _transform(code, exclude_list): instructions = code.instructions ''' Generate flow graph ''' flow_graph = cfg.generate(instructions) ''' Get reaching definitions information (for I.ADD only) ''' reach_def_ins, reach_def_outs = rd.analyse(flow_graph, code.argument_fields, [I.LD, I.OP, I.LDt, I.LKt, I.CRC, I.HSH, I.CNC, I.ATM, I.SEQ]) ''' Get field reachability information ''' reach_ins, reach_outs = fr.analyse(flow_graph, code.argument_fields, exclude_list) ''' Cherry-pick dead instructions ''' instruction_dict = {} for _, node in flow_graph.iteritems(): for instruction in node.basic_block: if isinstance(instruction, I.LD): instruction_dict[instruction] = [] if isinstance(instruction.source, O.Field): if is_reserved_or_argument_field(instruction.source.field, code.argument_fields): pass elif not (instruction.source.field in reach_ins[instruction]): instruction_dict[instruction].append( I.ADD(O.Field(instruction.source.field), get_field_size(reach_def_ins[instruction], instruction.source.field))) if isinstance(instruction.destination, O.Field): if is_reserved_or_argument_field(instruction.destination.field, code.argument_fields): pass elif not (instruction.destination.field in reach_ins[instruction]): instruction_dict[instruction].append( I.ADD(O.Field(instruction.destination.field), get_field_size(reach_def_ins[instruction], instruction.destination.field))) if not instruction_dict[instruction]: del instruction_dict[instruction] elif isinstance(instruction, I.ST): instruction_dict[instruction] = [] if isinstance(instruction.source, O.Field): if is_reserved_or_argument_field(instruction.source.field, code.argument_fields): pass elif not (instruction.source.field in reach_ins[instruction]): instruction_dict[instruction].append( I.ADD(O.Field(instruction.source.field), get_field_size(reach_def_ins[instruction], instruction.source.field))) if isinstance(instruction.location, O.Location): if isinstance(instruction.location.location.offset, O.Field): if is_reserved_or_argument_field(instruction.location.location.offset.field, code.argument_fields): pass elif not (instruction.location.location.offset.field in reach_ins[instruction]): instruction_dict[instruction].append(I.ADD( O.Field(instruction.location.location.offset.field), get_field_size(reach_def_ins[instruction], instruction.location.location.offset.field))) if isinstance(instruction.location.location.length, O.Field): if is_reserved_or_argument_field(instruction.location.location.length.field, code.argument_fields): pass elif not (instruction.location.location.length.field in reach_ins[instruction]): instruction_dict[instruction].append(I.ADD( O.Field(instruction.location.location.length.field), get_field_size(reach_def_ins[instruction], instruction.location.location.length.field))) else: raise RuntimeError("invalid %s of location (%s). Should be %s." % (type(instruction.location), instruction.location, O.Location)) if not instruction_dict[instruction]: del instruction_dict[instruction] elif isinstance(instruction, I.OP): instruction_dict[instruction] = [] if isinstance(instruction.left_source, O.Field): if is_reserved_or_argument_field(instruction.left_source.field, code.argument_fields): pass elif not (instruction.left_source.field in reach_ins[instruction]): instruction_dict[instruction].append(I.ADD( O.Field(instruction.left_source.field), get_field_size(reach_def_ins[instruction], instruction.left_source.field))) if isinstance(instruction.right_source, O.Field): if is_reserved_or_argument_field(instruction.right_source.field, code.argument_fields): pass elif not (instruction.right_source.field in reach_ins[instruction]): instruction_dict[instruction].append(I.ADD( O.Field(instruction.right_source.field), get_field_size(reach_def_ins[instruction], instruction.right_source.field))) if isinstance(instruction.destination, O.Field): if is_reserved_or_argument_field(instruction.destination.field, code.argument_fields): pass elif not (instruction.destination.field in reach_ins[instruction]): instruction_dict[instruction].append(I.ADD( O.Field(instruction.destination.field), get_field_size(reach_def_ins[instruction], instruction.destination.field))) if not instruction_dict[instruction]: del instruction_dict[instruction] elif isinstance(instruction, I.PUSH): instruction_dict[instruction] = [] if isinstance(instruction.location, O.Location): if isinstance(instruction.location.location.offset, O.Field): if is_reserved_or_argument_field(instruction.location.location.offset.field, code.argument_fields): pass elif not (instruction.location.location.offset.field in reach_ins[instruction]): instruction_dict[instruction].append(I.ADD( O.Field(instruction.location.location.offset.field), get_field_size(reach_def_ins[instruction], instruction.location.location.offset.field))) if isinstance(instruction.location.location.length, O.Field): if is_reserved_or_argument_field(instruction.location.location.length.field, code.argument_fields): pass elif not (instruction.location.location.length.field in reach_ins[instruction]): instruction_dict[instruction].append(I.ADD( O.Field(instruction.location.location.length.field), get_field_size(reach_def_ins[instruction], instruction.location.location.length.field))) else: raise RuntimeError("invalid %s of location (%s). Should be %s." % (type(instruction.location), instruction.location, O.Location)) if not instruction_dict[instruction]: del instruction_dict[instruction] elif isinstance(instruction, I.POP): instruction_dict[instruction] = [] if isinstance(instruction.location, O.Location): if isinstance(instruction.location.location.offset, O.Field): if is_reserved_or_argument_field(instruction.location.location.offset.field, code.argument_fields): pass elif not (instruction.location.location.offset.field in reach_ins[instruction]): instruction_dict[instruction].append(I.ADD( O.Field(instruction.location.location.offset.field), get_field_size(reach_def_ins[instruction], instruction.location.location.offset.field))) if isinstance(instruction.location.location.length, O.Field): if is_reserved_or_argument_field(instruction.location.location.length.field, code.argument_fields): pass elif not (instruction.location.location.length.field in reach_ins[instruction]): instruction_dict[instruction].append(I.ADD( O.Field(instruction.location.location.length.field), get_field_size(reach_def_ins[instruction], instruction.location.location.length.field))) else: raise RuntimeError("invalid %s of location (%s). Should be %s." % (type(instruction.location), instruction.location, O.Location)) if not instruction_dict[instruction]: del instruction_dict[instruction] elif isinstance(instruction, I.BR): instruction_dict[instruction] = [] if isinstance(instruction.left_source, O.Field): if is_reserved_or_argument_field(instruction.left_source.field, code.argument_fields): pass elif not (instruction.left_source.field in reach_ins[instruction]): instruction_dict[instruction].append(I.ADD( O.Field(instruction.left_source.field), get_field_size(reach_def_ins[instruction], instruction.left_source.field))) if isinstance(instruction.right_source, O.Field): if is_reserved_or_argument_field(instruction.right_source.field, code.argument_fields): pass elif not (instruction.right_source.field in reach_ins[instruction]): instruction_dict[instruction].append(I.ADD( O.Field(instruction.right_source.field), get_field_size(reach_def_ins[instruction], instruction.right_source.field))) if not instruction_dict[instruction]: del instruction_dict[instruction] elif isinstance(instruction, I.LDt): instruction_dict[instruction] = [] if isinstance(instruction.index, O.Field): if is_reserved_or_argument_field(instruction.index.field, code.argument_fields): pass elif not (instruction.index.field in reach_ins[instruction]): instruction_dict[instruction].append(I.ADD( O.Field(instruction.index.field), get_field_size(reach_def_ins[instruction], instruction.index.field))) if isinstance(instruction.destinations, O.Operands__): for operand in instruction.destinations: if isinstance(operand, O.Field): if is_reserved_or_argument_field(operand.field, code.argument_fields): pass elif not (operand.field in reach_ins[instruction]): instruction_dict[instruction].append(I.ADD( O.Field(operand.field), get_field_size(reach_def_ins[instruction], operand.field))) else: raise RuntimeError("invalid %s of destinations (%s). Should be %s." % (type(instruction.destinations), instruction.destinations, O.Operands__)) if not instruction_dict[instruction]: del instruction_dict[instruction] elif isinstance(instruction, I.STt): instruction_dict[instruction] = [] if isinstance(instruction.index, O.Field): if is_reserved_or_argument_field(instruction.index.field, code.argument_fields): pass elif not (instruction.index.field in reach_ins[instruction]): instruction_dict[instruction].append(I.ADD( O.Field(instruction.index.field), get_field_size(reach_def_ins[instruction], instruction.index.field))) if isinstance(instruction.sources, O.Operands_): for operand in instruction.sources: if isinstance(operand, O.Field): if is_reserved_or_argument_field(operand.field, code.argument_fields): pass elif not (operand.field in reach_ins[instruction]): instruction_dict[instruction].append(I.ADD( O.Field(operand.field), get_field_size(reach_def_ins[instruction], operand.field))) elif isinstance(instruction.sources, O.OperandsMasks_): for operand, _ in instruction.sources: if isinstance(operand, O.Field): if is_reserved_or_argument_field(operand.field, code.argument_fields): pass elif not (operand.field in reach_ins[instruction]): instruction_dict[instruction].append(I.ADD( O.Field(operand.field), get_field_size(reach_def_ins[instruction], operand.field))) else: raise RuntimeError("invalid %s of sources (%s). Should be %s or %s." % (type(instruction.sources), instruction.sources, O.Operands_, O.OperandsMasks_)) if not instruction_dict[instruction]: del instruction_dict[instruction] elif isinstance(instruction, I.INCt): instruction_dict[instruction] = [] if isinstance(instruction.index, O.Field): if is_reserved_or_argument_field(instruction.index.field, code.argument_fields): pass elif not (instruction.index.field in reach_ins[instruction]): instruction_dict[instruction].append(I.ADD( O.Field(instruction.index.field), get_field_size(reach_def_ins[instruction], instruction.index.field))) if not instruction_dict[instruction]: del instruction_dict[instruction] elif isinstance(instruction, I.LKt): instruction_dict[instruction] = [] if isinstance(instruction.sources, O.Operands_): for operand in instruction.sources: if isinstance(operand, O.Field): if is_reserved_or_argument_field(operand.field, code.argument_fields): pass elif not (operand.field in reach_ins[instruction]): instruction_dict[instruction].append(I.ADD( O.Field(operand.field), get_field_size(reach_def_ins[instruction], operand.field))) else: raise RuntimeError("invalid %s of sources (%s). Should be %s." % (type(instruction.sources), instruction.sources, O.Operands_)) if isinstance(instruction.index, O.Field): if is_reserved_or_argument_field(instruction.index.field, code.argument_fields): pass elif not (instruction.index.field in reach_ins[instruction]): instruction_dict[instruction].append(I.ADD( O.Field(instruction.index.field), get_field_size(reach_def_ins[instruction], instruction.index.field))) if not instruction_dict[instruction]: del instruction_dict[instruction] elif isinstance(instruction, I.CRC): instruction_dict[instruction] = [] if isinstance(instruction.sources, O.Operands_): for operand in instruction.sources: if isinstance(operand, O.Field): if is_reserved_or_argument_field(operand.field, code.argument_fields): pass elif not (operand.field in reach_ins[instruction]): instruction_dict[instruction].append(I.ADD( O.Field(operand.field), get_field_size(reach_def_ins[instruction], operand.field))) else: raise RuntimeError("invalid %s of sources (%s). Should be %s." % (type(instruction.sources), instruction.sources, O.Operands_)) if isinstance(instruction.destination, O.Field): if is_reserved_or_argument_field(instruction.destination.field, code.argument_fields): pass elif not (instruction.destination.field in reach_ins[instruction]): instruction_dict[instruction].append(I.ADD( O.Field(instruction.destination.field), get_field_size(reach_def_ins[instruction], instruction.destination.field))) if not instruction_dict[instruction]: del instruction_dict[instruction] elif isinstance(instruction, I.HSH): instruction_dict[instruction] = [] if isinstance(instruction.sources, O.Operands_): for operand in instruction.sources: if isinstance(operand, O.Field): if is_reserved_or_argument_field(operand.field, code.argument_fields): pass elif not (operand.field in reach_ins[instruction]): instruction_dict[instruction].append(I.ADD( O.Field(operand.field), get_field_size(reach_def_ins[instruction], operand.field))) else: raise RuntimeError("invalid %s of sources (%s). Should be %s." % (type(instruction.sources), instruction.sources, O.Operands_)) if isinstance(instruction.destination, O.Field): if is_reserved_or_argument_field(instruction.destination.field, code.argument_fields): pass elif not (instruction.destination.field in reach_ins[instruction]): instruction_dict[instruction].append(I.ADD( O.Field(instruction.destination.field), get_field_size(reach_def_ins[instruction], instruction.destination.field))) if not instruction_dict[instruction]: del instruction_dict[instruction] elif isinstance(instruction, I.CNC): instruction_dict[instruction] = [] if isinstance(instruction.codes, I.Codes): for _code in instruction.codes: for field in _code.argument_fields: if is_reserved_or_argument_field(field, code.argument_fields): pass elif not (field in reach_ins[instruction]): instruction_dict[instruction].append(I.ADD( O.Field(field), get_field_size(reach_def_ins[instruction], field))) else: raise RuntimeError("invalid %s of codes (%s). Should be %s." % (type(instruction.codes), instruction.codes, I.Codes)) if not instruction_dict[instruction]: del instruction_dict[instruction] elif isinstance(instruction, I.ATM): instruction_dict[instruction] = [] if isinstance(instruction.code, I.Code): for field in instruction.code.argument_fields: if is_reserved_or_argument_field(field, code.argument_fields): pass elif not (field in reach_ins[instruction]): instruction_dict[instruction].append(I.ADD( O.Field(field), get_field_size(reach_def_ins[instruction], field))) else: raise RuntimeError("invalid %s of code (%s). Should be %s." % (type(instruction.code), instruction.code, I.Code)) if not instruction_dict[instruction]: del instruction_dict[instruction] elif isinstance(instruction, I.SEQ): instruction_dict[instruction] = [] if isinstance(instruction.code, I.Code): for field in instruction.code.argument_fields: if is_reserved_or_argument_field(field, code.argument_fields): pass elif not (field in reach_ins[instruction]): instruction_dict[instruction].append(I.ADD( O.Field(field), get_field_size(reach_def_ins[instruction], field))) else: raise RuntimeError("invalid %s of sources (%s). Should be %s." % (type(instruction.code), instruction.code, I.Code)) if not instruction_dict[instruction]: del instruction_dict[instruction] ''' Add new add instructions in the code ''' for instruction, _instructions in instruction_dict.iteritems(): index = instructions.index(instruction) for i in range(0, len(_instructions)): instructions.insert(index + i, _instructions[i])