[TableGen] Flip reversed comments.
[oota-llvm.git] / utils / update_llc_test_checks.py
1 #!/usr/bin/env python2.7
2
3 """A test case update script.
4
5 This script is a utility to update LLVM X86 'llc' based test cases with new
6 FileCheck patterns. It can either update all of the tests in the file or
7 a single test function.
8 """
9
10 import argparse
11 import itertools
12 import string
13 import subprocess
14 import sys
15 import tempfile
16 import re
17
18
19 def llc(args, cmd_args, ir):
20   with open(ir) as ir_file:
21     stdout = subprocess.check_output(args.llc_binary + ' ' + cmd_args,
22                                      shell=True, stdin=ir_file)
23   return stdout
24
25
26 ASM_SCRUB_WHITESPACE_RE = re.compile(r'(?!^(|  \w))[ \t]+', flags=re.M)
27 ASM_SCRUB_TRAILING_WHITESPACE_RE = re.compile(r'[ \t]+$', flags=re.M)
28 ASM_SCRUB_SHUFFLES_RE = (
29     re.compile(
30         r'^(\s*\w+) [^#\n]+#+ ((?:[xyz]mm\d+|mem) = .*)$',
31         flags=re.M))
32 ASM_SCRUB_SP_RE = re.compile(r'\d+\(%(esp|rsp)\)')
33 ASM_SCRUB_RIP_RE = re.compile(r'[.\w]+\(%rip\)')
34 ASM_SCRUB_KILL_COMMENT_RE = re.compile(r'^ *#+ +kill:.*\n')
35
36
37 def scrub_asm(asm):
38   # Scrub runs of whitespace out of the assembly, but leave the leading
39   # whitespace in place.
40   asm = ASM_SCRUB_WHITESPACE_RE.sub(r' ', asm)
41   # Expand the tabs used for indentation.
42   asm = string.expandtabs(asm, 2)
43   # Detect shuffle asm comments and hide the operands in favor of the comments.
44   asm = ASM_SCRUB_SHUFFLES_RE.sub(r'\1 {{.*#+}} \2', asm)
45   # Generically match the stack offset of a memory operand.
46   asm = ASM_SCRUB_SP_RE.sub(r'{{[0-9]+}}(%\1)', asm)
47   # Generically match a RIP-relative memory operand.
48   asm = ASM_SCRUB_RIP_RE.sub(r'{{.*}}(%rip)', asm)
49   # Strip kill operands inserted into the asm.
50   asm = ASM_SCRUB_KILL_COMMENT_RE.sub('', asm)
51   # Strip trailing whitespace.
52   asm = ASM_SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
53   return asm
54
55
56 def main():
57   parser = argparse.ArgumentParser(description=__doc__)
58   parser.add_argument('-v', '--verbose', action='store_true',
59                       help='Show verbose output')
60   parser.add_argument('--llc-binary', default='llc',
61                       help='The "llc" binary to use to generate the test case')
62   parser.add_argument(
63       '--function', help='The function in the test file to update')
64   parser.add_argument('tests', nargs='+')
65   args = parser.parse_args()
66
67   run_line_re = re.compile('^\s*;\s*RUN:\s*(.*)$')
68   ir_function_re = re.compile('^\s*define\s+(?:internal\s+)?[^@]*@(\w+)\s*\(')
69   asm_function_re = re.compile(
70       r'^_?(?P<f>[^:]+):[ \t]*#+[ \t]*@(?P=f)\n[^:]*?'
71       r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*'
72       r'^\s*(?:[^:\n]+?:\s*\n\s*\.size|\.cfi_endproc|\.globl|\.comm|\.(?:sub)?section)',
73       flags=(re.M | re.S))
74   check_prefix_re = re.compile('--check-prefix=(\S+)')
75   check_re = re.compile(r'^\s*;\s*([^:]+?)(?:-NEXT|-NOT|-DAG|-LABEL)?:')
76   autogenerated_note = ('; NOTE: Assertions have been autogenerated by '
77                         'utils/update_llc_test_checks.py')
78
79   for test in args.tests:
80     if args.verbose:
81       print >>sys.stderr, 'Scanning for RUN lines in test file: %s' % (test,)
82     with open(test) as f:
83       test_lines = [l.rstrip() for l in f]
84
85     run_lines = [m.group(1)
86                  for m in [run_line_re.match(l) for l in test_lines] if m]
87     if args.verbose:
88       print >>sys.stderr, 'Found %d RUN lines:' % (len(run_lines),)
89       for l in run_lines:
90         print >>sys.stderr, '  RUN: ' + l
91
92     checks = []
93     for l in run_lines:
94       (llc_cmd, filecheck_cmd) = tuple([cmd.strip() for cmd in l.split('|', 1)])
95       if not llc_cmd.startswith('llc '):
96         print >>sys.stderr, 'WARNING: Skipping non-llc RUN line: ' + l
97         continue
98
99       if not filecheck_cmd.startswith('FileCheck '):
100         print >>sys.stderr, 'WARNING: Skipping non-FileChecked RUN line: ' + l
101         continue
102
103       llc_cmd_args = llc_cmd[len('llc'):].strip()
104       llc_cmd_args = llc_cmd_args.replace('< %s', '').replace('%s', '').strip()
105
106       check_prefixes = [m.group(1)
107                         for m in check_prefix_re.finditer(filecheck_cmd)]
108       if not check_prefixes:
109         check_prefixes = ['CHECK']
110
111       # FIXME: We should use multiple check prefixes to common check lines. For
112       # now, we just ignore all but the last.
113       checks.append((check_prefixes, llc_cmd_args))
114
115     asm = {}
116     for prefixes, _ in checks:
117       for prefix in prefixes:
118         asm.update({prefix: dict()})
119     for prefixes, llc_args in checks:
120       if args.verbose:
121         print >>sys.stderr, 'Extracted LLC cmd: llc ' + llc_args
122         print >>sys.stderr, 'Extracted FileCheck prefixes: ' + str(prefixes)
123       raw_asm = llc(args, llc_args, test)
124       # Build up a dictionary of all the function bodies.
125       for m in asm_function_re.finditer(raw_asm):
126         if not m:
127           continue
128         f = m.group('f')
129         f_asm = scrub_asm(m.group('body'))
130         if f.startswith('stress'):
131           # We only use the last line of the asm for stress tests.
132           f_asm = '\n'.join(f_asm.splitlines()[-1:])
133         if args.verbose:
134           print >>sys.stderr, 'Processing asm for function: ' + f
135           for l in f_asm.splitlines():
136             print >>sys.stderr, '  ' + l
137         for prefix in prefixes:
138           if f in asm[prefix] and asm[prefix][f] != f_asm:
139             if prefix == prefixes[-1]:
140               print >>sys.stderr, ('WARNING: Found conflicting asm under the '
141                                    'same prefix: %r!' % (prefix,))
142             else:
143               asm[prefix][f] = None
144               continue
145
146           asm[prefix][f] = f_asm
147
148     is_in_function = False
149     is_in_function_start = False
150     prefix_set = set([prefix for prefixes, _ in checks for prefix in prefixes])
151     if args.verbose:
152       print >>sys.stderr, 'Rewriting FileCheck prefixes: %s' % (prefix_set,)
153     fixed_lines = []
154     fixed_lines.append(autogenerated_note)
155
156     for l in test_lines:
157       if is_in_function_start:
158         if l.lstrip().startswith(';'):
159           m = check_re.match(l)
160           if not m or m.group(1) not in prefix_set:
161             fixed_lines.append(l)
162             continue
163
164         # Print out the various check lines here
165         printed_prefixes = []
166         for prefixes, _ in checks:
167           for prefix in prefixes:
168             if prefix in printed_prefixes:
169               break
170             if not asm[prefix][name]:
171               continue
172             if len(printed_prefixes) != 0:
173               fixed_lines.append(';')
174             printed_prefixes.append(prefix)
175             fixed_lines.append('; %s-LABEL: %s:' % (prefix, name))
176             asm_lines = asm[prefix][name].splitlines()
177             fixed_lines.append('; %s:       %s' % (prefix, asm_lines[0]))
178             for asm_line in asm_lines[1:]:
179               fixed_lines.append('; %s-NEXT:  %s' % (prefix, asm_line))
180             break
181         is_in_function_start = False
182
183       if is_in_function:
184         # Skip any blank comment lines in the IR.
185         if l.strip() == ';':
186           continue
187         # And skip any CHECK lines. We'll build our own.
188         m = check_re.match(l)
189         if m and m.group(1) in prefix_set:
190           continue
191         # Collect the remaining lines in the function body and look for the end
192         # of the function.
193         fixed_lines.append(l)
194         if l.strip() == '}':
195           is_in_function = False
196         continue
197
198       if l == autogenerated_note:
199         continue
200       fixed_lines.append(l)
201
202       m = ir_function_re.match(l)
203       if not m:
204         continue
205       name = m.group(1)
206       if args.function is not None and name != args.function:
207         # When filtering on a specific function, skip all others.
208         continue
209       is_in_function = is_in_function_start = True
210
211     if args.verbose:
212       print>>sys.stderr, 'Writing %d fixed lines to %s...' % (
213           len(fixed_lines), test)
214     with open(test, 'w') as f:
215       f.writelines([l + '\n' for l in fixed_lines])
216
217
218 if __name__ == '__main__':
219   main()