xref: /openbsd-src/gnu/llvm/llvm/utils/schedcover.py (revision 73471bf04ceb096474c7f0fa83b1b65c70a787a1)
1#!/usr/bin/env python
2
3# This creates a CSV file from the output of the debug output of subtarget:
4#   llvm-tblgen --gen-subtarget --debug-only=subtarget-emitter
5# With thanks to Dave Estes for mentioning the idea at 2014 LLVM Developers' Meeting
6
7import os;
8import sys;
9import re;
10import operator;
11
12table = {}
13models = set()
14filt = None
15
16def add(instr, model, resource=None):
17    global table, models
18
19    entry = table.setdefault(instr, dict())
20    entry[model] = resource
21    models.add(model)
22
23def filter_model(m):
24    global filt
25    if m and filt:
26        return filt.search(m) != None
27    else:
28        return True
29
30
31def display():
32    global table, models
33
34    # remove default and itinerary so we can control their sort order to make
35    # them first
36    models.discard("default")
37    models.discard("itinerary")
38
39    ordered_table  = sorted(table.items(), key=operator.itemgetter(0))
40    ordered_models = ["itinerary", "default"]
41    ordered_models.extend(sorted(models))
42    ordered_models = [m for m in ordered_models if filter_model(m)]
43
44    # print header
45    sys.stdout.write("instruction")
46    for model in ordered_models:
47        sys.stdout.write(", {}".format(model))
48    sys.stdout.write(os.linesep)
49
50    for (instr, mapping) in ordered_table:
51        sys.stdout.write(instr)
52        for model in ordered_models:
53            if model in mapping and mapping[model] is not None:
54                sys.stdout.write(", {}".format(mapping[model]))
55            else:
56                sys.stdout.write(", ")
57        sys.stdout.write(os.linesep)
58
59
60def machineModelCover(path):
61    # The interesting bits
62    re_sched_default  = re.compile("SchedRW machine model for ([^ ]*) (.*)\n");
63    re_sched_no_default = re.compile("No machine model for ([^ ]*)\n");
64    re_sched_spec = re.compile("InstRW on ([^ ]*) for ([^ ]*) (.*)\n");
65    re_sched_no_spec = re.compile("No machine model for ([^ ]*) on processor (.*)\n");
66    re_sched_itin = re.compile("Itinerary for ([^ ]*): ([^ ]*)\n")
67
68    # scan the file
69    with open(path, 'r') as f:
70        for line in f.readlines():
71            match = re_sched_default.match(line)
72            if match: add(match.group(1), "default", match.group(2))
73            match = re_sched_no_default.match(line)
74            if match: add(match.group(1), "default")
75            match = re_sched_spec.match(line)
76            if match: add(match.group(2), match.group(1), match.group(3))
77            match = re_sched_no_spec.match(line)
78            if match: add(match.group(1), match.group(2))
79            match = re_sched_itin.match(line)
80            if match: add(match.group(1), "itinerary", match.group(2))
81
82    display()
83
84if len(sys.argv) > 2:
85    filt = re.compile(sys.argv[2], re.IGNORECASE)
86machineModelCover(sys.argv[1])
87