Skip to content

Instantly share code, notes, and snippets.

@kohnakagawa
Created July 1, 2020 12:27
Show Gist options
  • Save kohnakagawa/75c6fab3a08dc53b10d47df8a7b83dc5 to your computer and use it in GitHub Desktop.
Save kohnakagawa/75c6fab3a08dc53b10d47df8a7b83dc5 to your computer and use it in GitHub Desktop.

RISC-V instructions used in real-world binaries

  • target: 856 binaries in /bin
name frequency (%) cumulative (%)
ld 14.101200487156945 14.101200487156945
mv 10.010240605850349 24.111441093007294
addi 9.590532965163105 33.7019740581704
sd 8.400851481380371 42.10282553955077
li 8.125153393115912 50.22797893266669
jal 7.438872769547847 57.66685170221454
auipc 4.532127806046617 62.198979508261154
j 4.015768635289742 66.21474814355089
beqz 3.152443028634279 69.36719117218517
lw 2.5248689806228177 71.89206015280799
lbu 2.1917389047239393 74.08379905753192
add 1.9912981522204478 76.07509720975237
lui 1.9702957756804564 78.04539298543283
bnez 1.9151769290900005 79.96056991452282
slli 1.8403066904016967 81.80087660492453
beq 1.4300734866026883 83.23095009152722
sw 1.351695770780374 84.5826458623076
bne 1.2768774597481594 85.85952332205576
sb 1.2157397257473888 87.07526304780315
ret 1.1996138281837543 88.2748768759869
jalr 1.0494343260783947 89.3243112020653
addiw 1.0396908095176973 90.364002011583
or 0.9887544995904088 91.35275651117341
andi 0.8893272002680223 92.24208371144144
sext.w 0.7243199920012526 92.9664037034427
srli 0.6562145106925417 93.62261821413523
bltu 0.5048029069290707 94.1274211210643
bgeu 0.47227259073732414 94.59969371180162
nop 0.3855817292447637 94.98527544104638
addw 0.38516630799605184 95.37044174904244
and 0.3162205427979008 95.68666229184035
sub 0.30403642640102113 95.99069871824138
bltz 0.2792622137505661 96.26996093199195
srliw 0.27517881170356845 96.54513974369551
slliw 0.24515046439610944 96.79029020809162
bge 0.23518507512303197 97.02547528321465
xor 0.22665949813242184 97.25213478134707
subw 0.22046594496980806 97.47260072631687
blt 0.20866420494958368 97.68126493126645
lhu 0.2025603450111236 97.88382527627758
bgez 0.17207409019087994 98.05589936646845
ori 0.13585691041681527 98.19175627688527
blez 0.13491277121519732 98.32666904810047
fld 0.13308586186006657 98.45975490996054
sh 0.12211496433726597 98.58186987429781
jr 0.08760195582212173 98.66947183011993
seqz 0.081568906323783 98.75104073644371
sraiw 0.07992610411296777 98.83096684055668
not 0.07841548139037904 98.90938232194706
lwu 0.07510155279270002 98.98448387473977
snez 0.07374199234237018 99.05822586708214
import sys
from typing import List
from collections import defaultdict
def show_frequently_used_insn(hist_sorted):
upper_bound = 99.0
cumu = 0
result = list()
for insn, value in hist_sorted:
cumu += value
result.append((insn, value))
if cumu >= upper_bound:
break
cumu = 0
for insn, value in result:
cumu += value
print(f"|{insn}|{value}|{cumu}|")
def _main() -> None:
with open("concat.asm") as fin:
asm_lines: List[str] = [l for l in fin.readlines() if (not l.strip().endswith(":"))]
hist = defaultdict(int)
n_ops = 0
for asm_line in asm_lines:
op_and_oprands = asm_line.split()
if len(op_and_oprands) < 3:
continue
op_name = op_and_oprands[2]
hist[op_name] += 1
n_ops += 1
for k, v in hist.items():
hist[k] = (v / n_ops) * 100.0
hist_sorted = sorted(hist.items(), key=lambda k_v: k_v[1], reverse=True)
show_frequently_used_insn(hist_sorted)
if __name__ == "__main__":
_main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment