009cd1406f6c270243dd5b193cd549061eb22862
[libreriscv.git] / openpower / sv / estimate-compression.py
1 #! /bin/env python3
2 # see https://bugs.libre-soc.org/show_bug.cgi?id=532
3
4 # Estimate ppc code compression with Libre-SOC encoding attempt v2.
5
6
7 # This script is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3, or (at your option)
10 # any later version.
11
12 # This script is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with this script; see the file COPYING3. If not see
19 # <http://www.gnu.org/licenses/>.
20
21 # Skeleton originally by Alexandre Oliva <oliva@gnu.org>.
22
23
24 # Feed this script the output of objdump -M raw --no-show-raw-insn ppc-prog
25
26 # It will look for insns that can be represented in compressed mode,
27 # according to the encoding rules in the copcond dictionary below.
28 # Nothing is assumed as to the actual bit-encoding of the insns, this
29 # is just to experiment with insn selection and get a quick feedback
30 # loop for the encoding options in compressed mode.
31
32 # In this script, the computations of encoding modes and transitions
33 # are tuned for the simpler model that uses 1-byte nops for
34 # transitions in and out of compressed mode, placing compressed-mode
35 # insns at odd addresses. At (visible) entry points, mode is forced
36 # to return to uncompressed mode.
37
38 # The entire code stream is printed, without any attempt to modify the
39 # addresses that go along with or in them; we only insert markers for
40 # the transition points, and for the compressed instructions.
41
42 # The really useful information is printed at the end: a summary of
43 # transition and compressed-insn counts, and the achieved compression
44 # rate.
45
46 import sys
47 import re
48
49 insn = re.compile('\s+(?P<addr>[0-9a-f]+):\s+(?P<opcode>[^ ]+) *(?P<operands>.*)')
50
51 opkind = re.compile('(?P<reg>(?P<regkind>[cf]?r)(?P<regnum>[0-9]+))|(?P<immediate>-?[0-9]+)|(?P<branch>[0-9a-f]+)(?: <.*>)?|(?P<offset>-?[0-9]+)\((?P<basereg>r[0-9]+)\)')
52
53 def mapop(op):
54 match = opkind.fullmatch(op)
55
56 if match is None:
57 op = ('other', op)
58 elif match['reg'] is not None:
59 op = (match['regkind'], int(match['regnum']))
60 elif match['immediate'] is not None:
61 op = ('imm', int (op).bit_length ())
62 elif match['branch'] is not None:
63 op = ('pcoff', (int (match['branch'], 16)
64 - int (addr, 16)).bit_length ())
65 elif match['offset'] is not None:
66 op = ('ofst', mapop(match['offset']), mapop(match['basereg']))
67 else:
68 raise "unrecognized operand kind"
69
70 return op
71
72 def opclass(mop):
73 return mop[0]
74 def regno(mop):
75 if mop[0] in { 'r', 'fr', 'cr' }:
76 return mop[1]
77 else:
78 raise "operand is not a register"
79
80 def immbits(mop):
81 if mop[0] is 'imm':
82 return mop[1]
83 else:
84 raise "operand is not an immediate"
85
86 # Following are predicates to be used in copcond, to tell the mode in
87 # which opcode with ops as operands is to be represented
88
89 # Any occurrence of the opcode can be compressed.
90 def anyops(opcode, ops):
91 return 1
92
93 # Compress iff first and second operands are the same.
94 def same01(opcode, ops):
95 if ops[0] == ops[1]:
96 return 1
97 else:
98 return 0
99
100 # Registers representable in a made-up 3-bit mapping.
101 cregs2 = { 1, 2, 3, 4, 5, 6, 7, 31 }
102
103 # Return true iff mop is a regular register present in cregs2
104 def bin2regs3(mop):
105 return opclass(mop) is 'r' and regno(mop) in cregs2
106
107 # Return true iff mop is an immediate of at most 8 bits.
108 def imm8(mop):
109 return opclass(mop) is 'imm' and immbits(mop) <= 8
110
111 # Compress binary opcodes iff the first two operands (output and first
112 # input operand) are registers representable in 3 bits in compressed
113 # mode, and the immediate operand can be represented in 8 bits.
114 def bin2regs3imm8(opcode, ops):
115 if bin2regs3(ops[0]) and bin2regs3(ops[1]) and imm8(ops[2]):
116 return 1
117 else:
118 return 0
119
120 # Map opcodes that might be compressed to a function that returns the
121 # mode (index into mode_list below) in which the insn is to be
122 # represented. Those not mentioned in copcond are assumed
123 # Uncomopressed.
124 copcond = {
125 # Pretending anything goes, just for demonstration purposes.
126 'mr': anyops,
127 'ld': anyops,
128 'std': anyops,
129 # Output and first input operand must coincide for these.
130 'add': same01,
131 'sub': same01,
132 # Limiting register and operand range:
133 'addi': bin2regs3imm8
134 # Anything else is uncompressed.
135 }
136
137 enter_compressed = 0
138 leave_compressed = 0
139 count_compressed = 0
140 count_uncompressed = 0
141 current_mode = 0
142 mode_list = ['Uncompressed', 'Compressed'] # for documentation purposes only
143
144 for line in sys.stdin:
145 if line[-1] is '\n':
146 line = line[:-1]
147
148 match = insn.fullmatch(line)
149 if match is None:
150 print(line)
151 # Switch to uncompressed mode at function boundaries
152 if current_mode is not 0:
153 print('<leave compressed mode>')
154 current_mode = 0
155 leave_compressed += 1
156 continue
157
158 addr = match['addr']
159 opcode = match['opcode']
160 operands = match['operands']
161
162 if opcode in copcond:
163 this_mode = copcond[opcode](opcode,
164 [mapop(op) for op in operands.split(',')])
165 else:
166 this_mode = 0
167
168 if this_mode is 1:
169 if current_mode is not 1:
170 print('\t\tcin.nop')
171 current_mode = 1
172 enter_compressed += 1
173 print(line + ' (c)')
174 count_compressed += 1
175 else:
176 if current_mode is not 0:
177 print('\t\tcout.nop')
178 current_mode = 0
179 leave_compressed += 1
180 print(line)
181 count_uncompressed += 1
182
183 transition_bytes = 1 * enter_compressed + 1 * leave_compressed
184 compressed_bytes = 2 * count_compressed
185 uncompressed_bytes = 4 * count_uncompressed
186 total_bytes = transition_bytes + compressed_bytes + uncompressed_bytes
187 original_bytes = 2 * compressed_bytes + uncompressed_bytes
188
189 print()
190 print('Summary')
191 print('Compressed instructions: %i' % count_compressed)
192 print('Uncompressed instructions: %i' % count_uncompressed)
193 print('Transitions into compressed mode: %i' % enter_compressed)
194 print('Transitions out of compressed mode: %i' % leave_compressed)
195 print('Compressed size estimate: %i' % total_bytes)
196 print('Original size: %i' % original_bytes)
197 print('Compressed/original ratio: %f' % (total_bytes / original_bytes))