[PowerPC] Look through copies for compare elimination
[llvm-core.git] / utils / UpdateTestChecks / asm.py
blob7fb93fab56519095e67d0058032f135eb5eb0e9c
1 from __future__ import print_function
2 import re
3 import sys
5 from . import common
7 if sys.version_info[0] > 2:
8 class string:
9 expandtabs = str.expandtabs
10 else:
11 import string
13 # RegEx: this is where the magic happens.
15 ##### Assembly parser
17 ASM_FUNCTION_X86_RE = re.compile(
18 r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n(?:\s*\.?Lfunc_begin[^:\n]*:\n)?[^:]*?'
19 r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*'
20 r'^\s*(?:[^:\n]+?:\s*\n\s*\.size|\.cfi_endproc|\.globl|\.comm|\.(?:sub)?section|#+ -- End function)',
21 flags=(re.M | re.S))
23 ASM_FUNCTION_ARM_RE = re.compile(
24 r'^(?P<func>[0-9a-zA-Z_]+):\n' # f: (name of function)
25 r'\s+\.fnstart\n' # .fnstart
26 r'(?P<body>.*?)\n' # (body of the function)
27 r'.Lfunc_end[0-9]+:', # .Lfunc_end0: or # -- End function
28 flags=(re.M | re.S))
30 ASM_FUNCTION_AARCH64_RE = re.compile(
31 r'^_?(?P<func>[^:]+):[ \t]*\/\/[ \t]*@(?P=func)\n'
32 r'(?:[ \t]+.cfi_startproc\n)?' # drop optional cfi noise
33 r'(?P<body>.*?)\n'
34 # This list is incomplete
35 r'.Lfunc_end[0-9]+:\n',
36 flags=(re.M | re.S))
38 ASM_FUNCTION_AMDGPU_RE = re.compile(
39 r'^_?(?P<func>[^:]+):[ \t]*;+[ \t]*@(?P=func)\n[^:]*?'
40 r'(?P<body>.*?)\n' # (body of the function)
41 # This list is incomplete
42 r'.Lfunc_end[0-9]+:\n',
43 flags=(re.M | re.S))
45 ASM_FUNCTION_MIPS_RE = re.compile(
46 r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n[^:]*?' # f: (name of func)
47 r'(?:^[ \t]+\.(frame|f?mask|set).*?\n)+' # Mips+LLVM standard asm prologue
48 r'(?P<body>.*?)\n' # (body of the function)
49 r'(?:^[ \t]+\.(set|end).*?\n)+' # Mips+LLVM standard asm epilogue
50 r'(\$|\.L)func_end[0-9]+:\n', # $func_end0: (mips32 - O32) or
51 # .Lfunc_end0: (mips64 - NewABI)
52 flags=(re.M | re.S))
54 ASM_FUNCTION_PPC_RE = re.compile(
55 r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n'
56 r'.*?'
57 r'\.Lfunc_begin[0-9]+:\n'
58 r'(?:[ \t]+.cfi_startproc\n)?'
59 r'(?:\.Lfunc_[gl]ep[0-9]+:\n(?:[ \t]+.*?\n)*)*'
60 r'(?P<body>.*?)\n'
61 # This list is incomplete
62 r'(?:^[ \t]*(?:\.long[ \t]+[^\n]+|\.quad[ \t]+[^\n]+)\n)*'
63 r'.Lfunc_end[0-9]+:\n',
64 flags=(re.M | re.S))
66 ASM_FUNCTION_RISCV_RE = re.compile(
67 r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n[^:]*?'
68 r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*'
69 r'.Lfunc_end[0-9]+:\n',
70 flags=(re.M | re.S))
72 ASM_FUNCTION_LANAI_RE = re.compile(
73 r'^_?(?P<func>[^:]+):[ \t]*!+[ \t]*@(?P=func)\n'
74 r'(?:[ \t]+.cfi_startproc\n)?' # drop optional cfi noise
75 r'(?P<body>.*?)\s*'
76 r'.Lfunc_end[0-9]+:\n',
77 flags=(re.M | re.S))
79 ASM_FUNCTION_SPARC_RE = re.compile(
80 r'^_?(?P<func>[^:]+):[ \t]*!+[ \t]*@(?P=func)\n'
81 r'(?P<body>.*?)\s*'
82 r'.Lfunc_end[0-9]+:\n',
83 flags=(re.M | re.S))
85 ASM_FUNCTION_SYSTEMZ_RE = re.compile(
86 r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n'
87 r'[ \t]+.cfi_startproc\n'
88 r'(?P<body>.*?)\n'
89 r'.Lfunc_end[0-9]+:\n',
90 flags=(re.M | re.S))
92 ASM_FUNCTION_WASM32_RE = re.compile(
93 r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n'
94 r'(?P<body>.*?)\n'
95 r'.Lfunc_end[0-9]+:\n',
96 flags=(re.M | re.S))
99 SCRUB_LOOP_COMMENT_RE = re.compile(
100 r'# =>This Inner Loop Header:.*|# in Loop:.*', flags=re.M)
102 SCRUB_X86_SHUFFLES_RE = (
103 re.compile(
104 r'^(\s*\w+) [^#\n]+#+ ((?:[xyz]mm\d+|mem)( \{%k\d+\}( \{z\})?)? = .*)$',
105 flags=re.M))
106 SCRUB_X86_SPILL_RELOAD_RE = (
107 re.compile(
108 r'-?\d+\(%([er])[sb]p\)(.*(?:Spill|Reload))$',
109 flags=re.M))
110 SCRUB_X86_SP_RE = re.compile(r'\d+\(%(esp|rsp)\)')
111 SCRUB_X86_RIP_RE = re.compile(r'[.\w]+\(%rip\)')
112 SCRUB_X86_LCP_RE = re.compile(r'\.LCPI[0-9]+_[0-9]+')
113 SCRUB_X86_RET_RE = re.compile(r'ret[l|q]')
115 def scrub_asm_x86(asm, args):
116 # Scrub runs of whitespace out of the assembly, but leave the leading
117 # whitespace in place.
118 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
119 # Expand the tabs used for indentation.
120 asm = string.expandtabs(asm, 2)
121 # Detect shuffle asm comments and hide the operands in favor of the comments.
122 asm = SCRUB_X86_SHUFFLES_RE.sub(r'\1 {{.*#+}} \2', asm)
123 # Detect stack spills and reloads and hide their exact offset and whether
124 # they used the stack pointer or frame pointer.
125 asm = SCRUB_X86_SPILL_RELOAD_RE.sub(r'{{[-0-9]+}}(%\1{{[sb]}}p)\2', asm)
126 # Generically match the stack offset of a memory operand.
127 asm = SCRUB_X86_SP_RE.sub(r'{{[0-9]+}}(%\1)', asm)
128 if getattr(args, 'x86_scrub_rip', False):
129 # Generically match a RIP-relative memory operand.
130 asm = SCRUB_X86_RIP_RE.sub(r'{{.*}}(%rip)', asm)
131 # Generically match a LCP symbol.
132 asm = SCRUB_X86_LCP_RE.sub(r'{{\.LCPI.*}}', asm)
133 if getattr(args, 'extra_scrub', False):
134 # Avoid generating different checks for 32- and 64-bit because of 'retl' vs 'retq'.
135 asm = SCRUB_X86_RET_RE.sub(r'ret{{[l|q]}}', asm)
136 # Strip kill operands inserted into the asm.
137 asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm)
138 # Strip trailing whitespace.
139 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
140 return asm
142 def scrub_asm_amdgpu(asm, args):
143 # Scrub runs of whitespace out of the assembly, but leave the leading
144 # whitespace in place.
145 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
146 # Expand the tabs used for indentation.
147 asm = string.expandtabs(asm, 2)
148 # Strip trailing whitespace.
149 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
150 return asm
152 def scrub_asm_arm_eabi(asm, args):
153 # Scrub runs of whitespace out of the assembly, but leave the leading
154 # whitespace in place.
155 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
156 # Expand the tabs used for indentation.
157 asm = string.expandtabs(asm, 2)
158 # Strip kill operands inserted into the asm.
159 asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm)
160 # Strip trailing whitespace.
161 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
162 return asm
164 def scrub_asm_powerpc(asm, args):
165 # Scrub runs of whitespace out of the assembly, but leave the leading
166 # whitespace in place.
167 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
168 # Expand the tabs used for indentation.
169 asm = string.expandtabs(asm, 2)
170 # Stripe unimportant comments
171 asm = SCRUB_LOOP_COMMENT_RE.sub(r'', asm)
172 # Strip trailing whitespace.
173 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
174 return asm
176 def scrub_asm_mips(asm, args):
177 # Scrub runs of whitespace out of the assembly, but leave the leading
178 # whitespace in place.
179 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
180 # Expand the tabs used for indentation.
181 asm = string.expandtabs(asm, 2)
182 # Strip trailing whitespace.
183 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
184 return asm
186 def scrub_asm_riscv(asm, args):
187 # Scrub runs of whitespace out of the assembly, but leave the leading
188 # whitespace in place.
189 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
190 # Expand the tabs used for indentation.
191 asm = string.expandtabs(asm, 2)
192 # Strip trailing whitespace.
193 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
194 return asm
196 def scrub_asm_lanai(asm, args):
197 # Scrub runs of whitespace out of the assembly, but leave the leading
198 # whitespace in place.
199 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
200 # Expand the tabs used for indentation.
201 asm = string.expandtabs(asm, 2)
202 # Strip trailing whitespace.
203 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
204 return asm
206 def scrub_asm_sparc(asm, args):
207 # Scrub runs of whitespace out of the assembly, but leave the leading
208 # whitespace in place.
209 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
210 # Expand the tabs used for indentation.
211 asm = string.expandtabs(asm, 2)
212 # Strip trailing whitespace.
213 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
214 return asm
216 def scrub_asm_systemz(asm, args):
217 # Scrub runs of whitespace out of the assembly, but leave the leading
218 # whitespace in place.
219 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
220 # Expand the tabs used for indentation.
221 asm = string.expandtabs(asm, 2)
222 # Strip trailing whitespace.
223 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
224 return asm
226 def scrub_asm_wasm32(asm, args):
227 # Scrub runs of whitespace out of the assembly, but leave the leading
228 # whitespace in place.
229 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
230 # Expand the tabs used for indentation.
231 asm = string.expandtabs(asm, 2)
232 # Strip trailing whitespace.
233 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
234 return asm
236 def get_triple_from_march(march):
237 triples = {
238 'amdgcn': 'amdgcn',
239 'r600': 'r600',
240 'mips': 'mips',
241 'sparc': 'sparc',
243 for prefix, triple in triples.items():
244 if march.startswith(prefix):
245 return triple
246 print("Cannot find a triple. Assume 'x86'", file=sys.stderr)
247 return 'x86'
249 def build_function_body_dictionary_for_triple(args, raw_tool_output, triple, prefixes, func_dict):
250 target_handlers = {
251 'x86_64': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
252 'i686': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
253 'x86': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
254 'i386': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
255 'arm64-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_RE),
256 'aarch64': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_RE),
257 'r600': (scrub_asm_amdgpu, ASM_FUNCTION_AMDGPU_RE),
258 'amdgcn': (scrub_asm_amdgpu, ASM_FUNCTION_AMDGPU_RE),
259 'arm-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
260 'thumb-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
261 'thumbv6': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
262 'thumbv6-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
263 'thumbv6t2': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
264 'thumbv6t2-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
265 'thumbv6m': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
266 'thumbv6m-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
267 'thumbv7': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
268 'thumbv7-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
269 'thumbv7m': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
270 'thumbv7m-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
271 'thumbv8-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
272 'thumbv8m.base': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
273 'thumbv8m.main': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
274 'armv6': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
275 'armv7': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
276 'armv7-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
277 'armeb-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
278 'armv7eb-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
279 'armv7eb': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
280 'armv8a': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
281 'mips': (scrub_asm_mips, ASM_FUNCTION_MIPS_RE),
282 'ppc32': (scrub_asm_powerpc, ASM_FUNCTION_PPC_RE),
283 'powerpc64': (scrub_asm_powerpc, ASM_FUNCTION_PPC_RE),
284 'powerpc64le': (scrub_asm_powerpc, ASM_FUNCTION_PPC_RE),
285 'riscv32': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE),
286 'riscv64': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE),
287 'lanai': (scrub_asm_lanai, ASM_FUNCTION_LANAI_RE),
288 'sparc': (scrub_asm_sparc, ASM_FUNCTION_SPARC_RE),
289 'sparcv9': (scrub_asm_sparc, ASM_FUNCTION_SPARC_RE),
290 's390x': (scrub_asm_systemz, ASM_FUNCTION_SYSTEMZ_RE),
291 'wasm32': (scrub_asm_wasm32, ASM_FUNCTION_WASM32_RE),
293 handlers = None
294 for prefix, s in target_handlers.items():
295 if triple.startswith(prefix):
296 handlers = s
297 break
298 else:
299 raise KeyError('Triple %r is not supported' % (triple))
301 scrubber, function_re = handlers
302 common.build_function_body_dictionary(
303 function_re, scrubber, [args], raw_tool_output, prefixes,
304 func_dict, args.verbose)
306 ##### Generator of assembly CHECK lines
308 def add_asm_checks(output_lines, comment_marker, prefix_list, func_dict, func_name):
309 # Label format is based on ASM string.
310 check_label_format = '{} %s-LABEL: %s:'.format(comment_marker)
311 common.add_checks(output_lines, comment_marker, prefix_list, func_dict, func_name, check_label_format, True, False)