blob: 79608f159f39ff28a5d7f97f72223eca004d2622 [file] [log] [blame]
Chandler Carruth06a5dd62015-01-12 04:43:18 +00001#!/usr/bin/env python2.7
2
3"""A test case update script.
4
5This script is a utility to update LLVM X86 'llc' based test cases with new
6FileCheck patterns. It can either update all of the tests in the file or
7a single test function.
8"""
9
10import argparse
Sanjay Patel506fd0d2016-03-24 17:30:38 +000011import os # Used to advertise this file's name ("autogenerated_note").
Chandler Carruth06a5dd62015-01-12 04:43:18 +000012import string
13import subprocess
14import sys
Chandler Carruth06a5dd62015-01-12 04:43:18 +000015import re
16
Sanjay Patelbf623012016-03-23 21:40:53 +000017# Invoke the tool that is being tested.
Chandler Carruth06a5dd62015-01-12 04:43:18 +000018def llc(args, cmd_args, ir):
19 with open(ir) as ir_file:
20 stdout = subprocess.check_output(args.llc_binary + ' ' + cmd_args,
21 shell=True, stdin=ir_file)
Simon Pilgrim6b6dcc42016-01-27 21:13:18 +000022 # Fix line endings to unix CR style.
23 stdout = stdout.replace('\r\n', '\n')
Chandler Carruth06a5dd62015-01-12 04:43:18 +000024 return stdout
25
26
Sanjay Patelbf623012016-03-23 21:40:53 +000027# RegEx: this is where the magic happens.
28
Eli Friedman1a9a8872016-12-19 23:09:51 +000029ASM_FUNCTION_X86_RE = re.compile(
30 r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n[^:]*?'
31 r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*'
Zvi Rackoverd635eeb2017-09-06 23:04:28 +000032 r'^\s*(?:[^:\n]+?:\s*\n\s*\.size|\.cfi_endproc|\.globl|\.comm|\.(?:sub)?section|#+ -- End function)',
Eli Friedman1a9a8872016-12-19 23:09:51 +000033 flags=(re.M | re.S))
Eli Friedman1a9a8872016-12-19 23:09:51 +000034
35ASM_FUNCTION_ARM_RE = re.compile(
36 r'^(?P<func>[0-9a-zA-Z_]+):\n' # f: (name of function)
37 r'\s+\.fnstart\n' # .fnstart
38 r'(?P<body>.*?)\n' # (body of the function)
Chandler Carruth5c69dac2017-08-25 02:32:48 +000039 r'.Lfunc_end[0-9]+:', # .Lfunc_end0: or # -- End function
Eli Friedman1a9a8872016-12-19 23:09:51 +000040 flags=(re.M | re.S))
Sanjay Patelbf623012016-03-23 21:40:53 +000041
Sanjay Patelb2f62a9e2017-08-25 19:33:18 +000042ASM_FUNCTION_AARCH64_RE = re.compile(
43 r'^_?(?P<func>[^:]+):[ \t]*\/\/[ \t]*@(?P=func)\n'
44 r'[ \t]+.cfi_startproc\n'
45 r'(?P<body>.*?)\n'
46 # This list is incomplete
47 r'.Lfunc_end[0-9]+:\n',
48 flags=(re.M | re.S))
Chandler Carruth06a5dd62015-01-12 04:43:18 +000049
Simon Dardis9d685652017-11-26 19:22:44 +000050ASM_FUNCTION_MIPS_RE = re.compile(
51 r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n[^:]*?' # f: (name of func)
52 r'(?:^[ \t]+\.(frame|f?mask|set).*?\n)+' # Mips+LLVM standard asm prologue
53 r'(?P<body>.*?)\n' # (body of the function)
54 r'(?:^[ \t]+\.(set|end).*?\n)+' # Mips+LLVM standard asm epilogue
55 r'(\$|\.L)func_end[0-9]+:\n', # $func_end0: (mips32 - O32) or
56 # .Lfunc_end0: (mips64 - NewABI)
57 flags=(re.M | re.S))
58
Tim Shen53ddc1d2016-12-22 20:59:39 +000059ASM_FUNCTION_PPC_RE = re.compile(
60 r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n'
61 r'\.Lfunc_begin[0-9]+:\n'
62 r'[ \t]+.cfi_startproc\n'
63 r'(?:\.Lfunc_[gl]ep[0-9]+:\n(?:[ \t]+.*?\n)*)*'
64 r'(?P<body>.*?)\n'
65 # This list is incomplete
66 r'(?:^[ \t]*(?:\.long[ \t]+[^\n]+|\.quad[ \t]+[^\n]+)\n)*'
67 r'.Lfunc_end[0-9]+:\n',
68 flags=(re.M | re.S))
69
Alex Bradbury86f971c2017-11-08 14:24:42 +000070ASM_FUNCTION_RISCV_RE = re.compile(
71 r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n[^:]*?'
72 r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*'
73 r'.Lfunc_end[0-9]+:\n',
74 flags=(re.M | re.S))
75
Jonas Paulssonf20386d2017-03-17 07:11:42 +000076ASM_FUNCTION_SYSTEMZ_RE = re.compile(
77 r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n'
78 r'[ \t]+.cfi_startproc\n'
79 r'(?P<body>.*?)\n'
80 r'.Lfunc_end[0-9]+:\n',
81 flags=(re.M | re.S))
82
Chandler Carruth06a5dd62015-01-12 04:43:18 +000083
Sanjay Patelb2f62a9e2017-08-25 19:33:18 +000084SCRUB_WHITESPACE_RE = re.compile(r'(?!^(| \w))[ \t]+', flags=re.M)
85SCRUB_TRAILING_WHITESPACE_RE = re.compile(r'[ \t]+$', flags=re.M)
86SCRUB_KILL_COMMENT_RE = re.compile(r'^ *#+ +kill:.*\n')
87SCRUB_LOOP_COMMENT_RE = re.compile(
88 r'# =>This Inner Loop Header:.*|# in Loop:.*', flags=re.M)
89
90SCRUB_X86_SHUFFLES_RE = (
91 re.compile(
92 r'^(\s*\w+) [^#\n]+#+ ((?:[xyz]mm\d+|mem)( \{%k\d+\}( \{z\})?)? = .*)$',
93 flags=re.M))
94SCRUB_X86_SP_RE = re.compile(r'\d+\(%(esp|rsp)\)')
95SCRUB_X86_RIP_RE = re.compile(r'[.\w]+\(%rip\)')
96SCRUB_X86_LCP_RE = re.compile(r'\.LCPI[0-9]+_[0-9]+')
Sanjay Patelf1735a52017-10-20 21:55:23 +000097SCRUB_X86_RET_RE = re.compile(r'ret[l|q]')
Sanjay Patelb2f62a9e2017-08-25 19:33:18 +000098
99RUN_LINE_RE = re.compile('^\s*;\s*RUN:\s*(.*)$')
100TRIPLE_ARG_RE = re.compile(r'-mtriple=([^ ]+)')
101TRIPLE_IR_RE = re.compile(r'^target\s+triple\s*=\s*"([^"]+)"$')
102IR_FUNCTION_RE = re.compile('^\s*define\s+(?:internal\s+)?[^@]*@(\w+)\s*\(')
103CHECK_PREFIX_RE = re.compile('--?check-prefix(?:es)?=(\S+)')
104CHECK_RE = re.compile(r'^\s*;\s*([^:]+?)(?:-NEXT|-NOT|-DAG|-LABEL)?:')
105
Sanjay Patel9db5da22017-10-24 14:32:52 +0000106def scrub_asm_x86(asm, args):
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000107 # Scrub runs of whitespace out of the assembly, but leave the leading
108 # whitespace in place.
Sanjay Patelbf623012016-03-23 21:40:53 +0000109 asm = SCRUB_WHITESPACE_RE.sub(r' ', asm)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000110 # Expand the tabs used for indentation.
111 asm = string.expandtabs(asm, 2)
112 # Detect shuffle asm comments and hide the operands in favor of the comments.
Sanjay Patelbf623012016-03-23 21:40:53 +0000113 asm = SCRUB_X86_SHUFFLES_RE.sub(r'\1 {{.*#+}} \2', asm)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000114 # Generically match the stack offset of a memory operand.
Sanjay Patelbf623012016-03-23 21:40:53 +0000115 asm = SCRUB_X86_SP_RE.sub(r'{{[0-9]+}}(%\1)', asm)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000116 # Generically match a RIP-relative memory operand.
Sanjay Patelbf623012016-03-23 21:40:53 +0000117 asm = SCRUB_X86_RIP_RE.sub(r'{{.*}}(%rip)', asm)
Simon Pilgrim2b7c02a2016-06-11 20:39:21 +0000118 # Generically match a LCP symbol.
119 asm = SCRUB_X86_LCP_RE.sub(r'{{\.LCPI.*}}', asm)
Sanjay Patel9db5da22017-10-24 14:32:52 +0000120 if args.x86_extra_scrub:
121 # Avoid generating different checks for 32- and 64-bit because of 'retl' vs 'retq'.
122 asm = SCRUB_X86_RET_RE.sub(r'ret{{[l|q]}}', asm)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000123 # Strip kill operands inserted into the asm.
Sanjay Patelbf623012016-03-23 21:40:53 +0000124 asm = SCRUB_KILL_COMMENT_RE.sub('', asm)
Chandler Carruthe3750952015-02-04 10:46:48 +0000125 # Strip trailing whitespace.
Sanjay Patelbf623012016-03-23 21:40:53 +0000126 asm = SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000127 return asm
128
Sanjay Patel9db5da22017-10-24 14:32:52 +0000129def scrub_asm_arm_eabi(asm, args):
Eli Friedman1a9a8872016-12-19 23:09:51 +0000130 # Scrub runs of whitespace out of the assembly, but leave the leading
131 # whitespace in place.
132 asm = SCRUB_WHITESPACE_RE.sub(r' ', asm)
133 # Expand the tabs used for indentation.
134 asm = string.expandtabs(asm, 2)
135 # Strip kill operands inserted into the asm.
136 asm = SCRUB_KILL_COMMENT_RE.sub('', asm)
137 # Strip trailing whitespace.
138 asm = SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
139 return asm
140
Sanjay Patel9db5da22017-10-24 14:32:52 +0000141def scrub_asm_powerpc64(asm, args):
Tim Shen53ddc1d2016-12-22 20:59:39 +0000142 # Scrub runs of whitespace out of the assembly, but leave the leading
143 # whitespace in place.
144 asm = SCRUB_WHITESPACE_RE.sub(r' ', asm)
145 # Expand the tabs used for indentation.
146 asm = string.expandtabs(asm, 2)
Tim Shence26a452017-03-23 16:02:47 +0000147 # Stripe unimportant comments
148 asm = SCRUB_LOOP_COMMENT_RE.sub(r'', asm)
Tim Shen53ddc1d2016-12-22 20:59:39 +0000149 # Strip trailing whitespace.
150 asm = SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
151 return asm
152
Simon Dardis9d685652017-11-26 19:22:44 +0000153def scrub_asm_mips(asm, args):
154 # Scrub runs of whitespace out of the assembly, but leave the leading
155 # whitespace in place.
156 asm = SCRUB_WHITESPACE_RE.sub(r' ', asm)
157 # Expand the tabs used for indentation.
158 asm = string.expandtabs(asm, 2)
159 # Strip trailing whitespace.
160 asm = SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
161 return asm
162
Alex Bradbury2af11912017-11-09 20:01:25 +0000163def scrub_asm_riscv(asm, args):
Alex Bradbury86f971c2017-11-08 14:24:42 +0000164 # Scrub runs of whitespace out of the assembly, but leave the leading
165 # whitespace in place.
166 asm = SCRUB_WHITESPACE_RE.sub(r' ', asm)
167 # Expand the tabs used for indentation.
168 asm = string.expandtabs(asm, 2)
169 # Strip trailing whitespace.
170 asm = SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
171 return asm
172
Sanjay Patel9db5da22017-10-24 14:32:52 +0000173def scrub_asm_systemz(asm, args):
Jonas Paulssonf20386d2017-03-17 07:11:42 +0000174 # Scrub runs of whitespace out of the assembly, but leave the leading
175 # whitespace in place.
176 asm = SCRUB_WHITESPACE_RE.sub(r' ', asm)
177 # Expand the tabs used for indentation.
178 asm = string.expandtabs(asm, 2)
179 # Strip trailing whitespace.
180 asm = SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
181 return asm
182
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000183
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000184# Build up a dictionary of all the function bodies.
Tim Shen53ddc1d2016-12-22 20:59:39 +0000185def build_function_body_dictionary(raw_tool_output, triple, prefixes, func_dict,
Sanjay Patel9db5da22017-10-24 14:32:52 +0000186 args):
Tim Shen53ddc1d2016-12-22 20:59:39 +0000187 target_handlers = {
188 'x86_64': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
189 'i686': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
190 'x86': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
191 'i386': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
Sanjay Patelb2f62a9e2017-08-25 19:33:18 +0000192 'aarch64': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_RE),
Tim Shen53ddc1d2016-12-22 20:59:39 +0000193 'arm-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
Sanjay Patel588e4152017-02-24 21:47:44 +0000194 'thumb-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
195 'thumbv8-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
Eli Friedman7e0ce822017-02-24 03:04:11 +0000196 'armeb-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
Simon Dardis9d685652017-11-26 19:22:44 +0000197 'mips': (scrub_asm_mips, ASM_FUNCTION_MIPS_RE),
Fangrui Songdc168722017-10-22 18:43:23 +0000198 'powerpc64': (scrub_asm_powerpc64, ASM_FUNCTION_PPC_RE),
199 'powerpc64le': (scrub_asm_powerpc64, ASM_FUNCTION_PPC_RE),
Alex Bradbury86f971c2017-11-08 14:24:42 +0000200 'riscv32': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE),
201 'riscv64': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE),
Jonas Paulssonf20386d2017-03-17 07:11:42 +0000202 's390x': (scrub_asm_systemz, ASM_FUNCTION_SYSTEMZ_RE),
Tim Shen53ddc1d2016-12-22 20:59:39 +0000203 }
204 handlers = None
205 for prefix, s in target_handlers.items():
206 if triple.startswith(prefix):
207 handlers = s
208 break
209 else:
210 raise KeyError('Triple %r is not supported' % (triple))
211
212 scrubber, function_re = handlers
Eli Friedman1a9a8872016-12-19 23:09:51 +0000213 for m in function_re.finditer(raw_tool_output):
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000214 if not m:
215 continue
216 func = m.group('func')
Sanjay Patel9db5da22017-10-24 14:32:52 +0000217 scrubbed_body = scrubber(m.group('body'), args)
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000218 if func.startswith('stress'):
219 # We only use the last line of the function body for stress tests.
220 scrubbed_body = '\n'.join(scrubbed_body.splitlines()[-1:])
Sanjay Patel9db5da22017-10-24 14:32:52 +0000221 if args.verbose:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000222 print >>sys.stderr, 'Processing function: ' + func
223 for l in scrubbed_body.splitlines():
224 print >>sys.stderr, ' ' + l
225 for prefix in prefixes:
226 if func in func_dict[prefix] and func_dict[prefix][func] != scrubbed_body:
227 if prefix == prefixes[-1]:
228 print >>sys.stderr, ('WARNING: Found conflicting asm under the '
229 'same prefix: %r!' % (prefix,))
230 else:
231 func_dict[prefix][func] = None
232 continue
233
234 func_dict[prefix][func] = scrubbed_body
235
236
Tim Shen53ddc1d2016-12-22 20:59:39 +0000237def add_checks(output_lines, run_list, func_dict, func_name):
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000238 printed_prefixes = []
Tim Shen53ddc1d2016-12-22 20:59:39 +0000239 for p in run_list:
240 checkprefixes = p[0]
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000241 for checkprefix in checkprefixes:
242 if checkprefix in printed_prefixes:
243 break
244 if not func_dict[checkprefix][func_name]:
245 continue
246 # Add some space between different check prefixes.
247 if len(printed_prefixes) != 0:
248 output_lines.append(';')
249 printed_prefixes.append(checkprefix)
250 output_lines.append('; %s-LABEL: %s:' % (checkprefix, func_name))
251 func_body = func_dict[checkprefix][func_name].splitlines()
252 output_lines.append('; %s: %s' % (checkprefix, func_body[0]))
253 for func_line in func_body[1:]:
254 output_lines.append('; %s-NEXT: %s' % (checkprefix, func_line))
255 # Add space between different check prefixes and the first line of code.
256 # output_lines.append(';')
257 break
258 return output_lines
259
260
261def should_add_line_to_output(input_line, prefix_set):
262 # Skip any blank comment lines in the IR.
263 if input_line.strip() == ';':
264 return False
265 # Skip any blank lines in the IR.
266 #if input_line.strip() == '':
267 # return False
268 # And skip any CHECK lines. We're building our own.
269 m = CHECK_RE.match(input_line)
270 if m and m.group(1) in prefix_set:
271 return False
272
273 return True
274
275
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000276def main():
277 parser = argparse.ArgumentParser(description=__doc__)
278 parser.add_argument('-v', '--verbose', action='store_true',
279 help='Show verbose output')
280 parser.add_argument('--llc-binary', default='llc',
281 help='The "llc" binary to use to generate the test case')
282 parser.add_argument(
283 '--function', help='The function in the test file to update')
Sanjay Patel9db5da22017-10-24 14:32:52 +0000284 parser.add_argument(
285 '--x86_extra_scrub', action='store_true',
286 help='Use more regex for x86 matching to reduce diffs between various subtargets')
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000287 parser.add_argument('tests', nargs='+')
288 args = parser.parse_args()
289
James Y Knight7c905062015-11-23 21:33:58 +0000290 autogenerated_note = ('; NOTE: Assertions have been autogenerated by '
Simon Pilgrim2b7c02a2016-06-11 20:39:21 +0000291 'utils/' + os.path.basename(__file__))
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000292
293 for test in args.tests:
294 if args.verbose:
295 print >>sys.stderr, 'Scanning for RUN lines in test file: %s' % (test,)
296 with open(test) as f:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000297 input_lines = [l.rstrip() for l in f]
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000298
Tim Shen53ddc1d2016-12-22 20:59:39 +0000299 triple_in_ir = None
300 for l in input_lines:
301 m = TRIPLE_IR_RE.match(l)
302 if m:
303 triple_in_ir = m.groups()[0]
304 break
305
Bryant Wong291264b2016-12-29 19:32:34 +0000306 raw_lines = [m.group(1)
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000307 for m in [RUN_LINE_RE.match(l) for l in input_lines] if m]
Bryant Wong291264b2016-12-29 19:32:34 +0000308 run_lines = [raw_lines[0]] if len(raw_lines) > 0 else []
309 for l in raw_lines[1:]:
Bryant Wong507256b2016-12-29 20:05:51 +0000310 if run_lines[-1].endswith("\\"):
311 run_lines[-1] = run_lines[-1].rstrip("\\") + " " + l
312 else:
313 run_lines.append(l)
Bryant Wong291264b2016-12-29 19:32:34 +0000314
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000315 if args.verbose:
316 print >>sys.stderr, 'Found %d RUN lines:' % (len(run_lines),)
317 for l in run_lines:
318 print >>sys.stderr, ' RUN: ' + l
319
Tim Shen53ddc1d2016-12-22 20:59:39 +0000320 run_list = []
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000321 for l in run_lines:
Zvi Rackover35a5acf2016-11-07 17:47:21 +0000322 commands = [cmd.strip() for cmd in l.split('|', 1)]
323 llc_cmd = commands[0]
Tim Shen53ddc1d2016-12-22 20:59:39 +0000324
325 triple_in_cmd = None
326 m = TRIPLE_ARG_RE.search(llc_cmd)
327 if m:
328 triple_in_cmd = m.groups()[0]
329
Zvi Rackover35a5acf2016-11-07 17:47:21 +0000330 filecheck_cmd = ''
331 if len(commands) > 1:
332 filecheck_cmd = commands[1]
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000333 if not llc_cmd.startswith('llc '):
334 print >>sys.stderr, 'WARNING: Skipping non-llc RUN line: ' + l
335 continue
336
337 if not filecheck_cmd.startswith('FileCheck '):
338 print >>sys.stderr, 'WARNING: Skipping non-FileChecked RUN line: ' + l
339 continue
340
341 llc_cmd_args = llc_cmd[len('llc'):].strip()
342 llc_cmd_args = llc_cmd_args.replace('< %s', '').replace('%s', '').strip()
343
Nikolai Bozhenov33ee40e2017-01-14 09:39:35 +0000344 check_prefixes = [item for m in CHECK_PREFIX_RE.finditer(filecheck_cmd)
345 for item in m.group(1).split(',')]
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000346 if not check_prefixes:
347 check_prefixes = ['CHECK']
348
349 # FIXME: We should use multiple check prefixes to common check lines. For
350 # now, we just ignore all but the last.
Tim Shen53ddc1d2016-12-22 20:59:39 +0000351 run_list.append((check_prefixes, llc_cmd_args, triple_in_cmd))
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000352
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000353 func_dict = {}
Tim Shen53ddc1d2016-12-22 20:59:39 +0000354 for p in run_list:
355 prefixes = p[0]
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000356 for prefix in prefixes:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000357 func_dict.update({prefix: dict()})
Tim Shen53ddc1d2016-12-22 20:59:39 +0000358 for prefixes, llc_args, triple_in_cmd in run_list:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000359 if args.verbose:
360 print >>sys.stderr, 'Extracted LLC cmd: llc ' + llc_args
361 print >>sys.stderr, 'Extracted FileCheck prefixes: ' + str(prefixes)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000362
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000363 raw_tool_output = llc(args, llc_args, test)
Tim Shen53ddc1d2016-12-22 20:59:39 +0000364 if not (triple_in_cmd or triple_in_ir):
365 print >>sys.stderr, "Cannot find a triple. Assume 'x86'"
366
367 build_function_body_dictionary(raw_tool_output,
Sanjay Patel9db5da22017-10-24 14:32:52 +0000368 triple_in_cmd or triple_in_ir or 'x86', prefixes, func_dict, args)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000369
370 is_in_function = False
371 is_in_function_start = False
Zvi Rackover18082ab2016-11-07 18:08:19 +0000372 func_name = None
Tim Shen53ddc1d2016-12-22 20:59:39 +0000373 prefix_set = set([prefix for p in run_list for prefix in p[0]])
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000374 if args.verbose:
375 print >>sys.stderr, 'Rewriting FileCheck prefixes: %s' % (prefix_set,)
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000376 output_lines = []
377 output_lines.append(autogenerated_note)
James Y Knight7c905062015-11-23 21:33:58 +0000378
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000379 for input_line in input_lines:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000380 if is_in_function_start:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000381 if input_line == '':
382 continue
383 if input_line.lstrip().startswith(';'):
384 m = CHECK_RE.match(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000385 if not m or m.group(1) not in prefix_set:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000386 output_lines.append(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000387 continue
388
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000389 # Print out the various check lines here.
Tim Shen53ddc1d2016-12-22 20:59:39 +0000390 output_lines = add_checks(output_lines, run_list, func_dict, func_name)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000391 is_in_function_start = False
392
393 if is_in_function:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000394 if should_add_line_to_output(input_line, prefix_set) == True:
395 # This input line of the function body will go as-is into the output.
396 output_lines.append(input_line)
397 else:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000398 continue
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000399 if input_line.strip() == '}':
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000400 is_in_function = False
401 continue
402
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000403 if input_line == autogenerated_note:
James Y Knight7c905062015-11-23 21:33:58 +0000404 continue
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000405
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000406 # If it's outside a function, it just gets copied to the output.
407 output_lines.append(input_line)
408
409 m = IR_FUNCTION_RE.match(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000410 if not m:
411 continue
Zvi Rackover18082ab2016-11-07 18:08:19 +0000412 func_name = m.group(1)
413 if args.function is not None and func_name != args.function:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000414 # When filtering on a specific function, skip all others.
415 continue
416 is_in_function = is_in_function_start = True
417
418 if args.verbose:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000419 print>>sys.stderr, 'Writing %d lines to %s...' % (len(output_lines), test)
420
Simon Pilgrim6b6dcc42016-01-27 21:13:18 +0000421 with open(test, 'wb') as f:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000422 f.writelines([l + '\n' for l in output_lines])
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000423
424
425if __name__ == '__main__':
426 main()