blob: 32516ad87b3ebf4b639d92135783d5c2661ec871 [file] [log] [blame]
Chris Lattner9d3787b2009-08-15 15:38:11 +00001
2=pod
3
4=head1 NAME
5
6FileCheck - Flexible pattern matching file verifier
7
8=head1 SYNOPSIS
9
10B<FileCheck> I<match-filename> [I<--check-prefix=XXX>] [I<--strict-whitespace>]
11
12=head1 DESCRIPTION
13
14B<FileCheck> reads two files (one from standard input, and one specified on the
15command line) and uses one to verify the other. This behavior is particularly
16useful for the testsuite, which wants to verify that the output of some tool
17(e.g. llc) contains the expected information (for example, a movsd from esp or
18whatever is interesting). This is similar to using grep, but it is optimized
19for matching multiple different inputs in one file in a specific order.
20
21The I<match-filename> file specifies the file that contains the patterns to
22match. The file to verify is always read from standard input.
23
Chris Lattner9d3787b2009-08-15 15:38:11 +000024=head1 OPTIONS
25
26=over
27
28=item B<--help>
29
30Print a summary of command line options.
31
32=item B<--check-prefix> I<prefix>
33
34FileCheck searches the contents of I<match-filename> for patterns to match. By
35default, these patterns are prefixed with "CHECK:". If you'd like to use a
36different prefix (e.g. because the same input file is checking multiple
37different tool or options), the B<--check-prefix> argument allows you to specify
38a specific prefix to match.
39
40=item B<--strict-whitespace>
41
42By default, FileCheck canonicalizes input horizontal whitespace (spaces and
43tabs) which causes it to ignore these differences (a space will match a tab).
44The --strict-whitespace argument disables this behavior.
45
46=item B<-version>
47
48Show the version number of this program.
49
50=back
51
52=head1 EXIT STATUS
53
54If B<FileCheck> verifies that the file matches the expected contents, it exits
55with 0. Otherwise, if not, or if an error occurs, it will exit with a non-zero
56value.
57
Chris Lattner704ac902009-10-17 04:47:42 +000058=head1 TUTORIAL
59
60FileCheck is typically used from LLVM regression tests, being invoked on the RUN
61line of the test. A simple example of using FileCheck from a RUN line looks
62like this:
63
64 ; RUN: llvm-as < %s | llc -march=x86-64 | FileCheck %s
65
66This syntax says to pipe the current file ("%s") into llvm-as, pipe that into
67llc, then pipe the output of llc into FileCheck. This means that FileCheck will
68be verifying its standard input (the llc output) against the filename argument
69specified (the original .ll file specified by "%s"). To see how this works,
70lets look at the rest of the .ll file (after the RUN line):
71
72 define void @sub1(i32* %p, i32 %v) {
73 entry:
74 ; <b>CHECK: sub1:</b>
75 ; <b>CHECK: subl</b>
76 %0 = tail call i32 @llvm.atomic.load.sub.i32.p0i32(i32* %p, i32 %v)
77 ret void
78 }
79
80 define void @inc4(i64* %p) {
81 entry:
82 ; <b>CHECK: inc4:</b>
83 ; <b>CHECK: incq</b>
84 %0 = tail call i64 @llvm.atomic.load.add.i64.p0i64(i64* %p, i64 1)
85 ret void
86 }
87
88Here you can see some "CHECK:" lines specified in comments. Now you can see
89how the file is piped into llvm-as, then llc, and the machine code output is
90what we are verifying. FileCheck checks the machine code output to verify that
91it matches what the "CHECK:" lines specify.
92
93The syntax of the CHECK: lines is very simple: they are fixed strings that
94must occur in order. FileCheck defaults to ignoring horizontal whitespace
95differences (e.g. a space is allowed to match a tab) but otherwise, the contents
96of the CHECK: line is required to match some thing in the test file exactly.
97
98One nice thing about FileCheck (compared to grep) is that it allows merging
99test cases together into logical groups. For example, because the test above
100is checking for the "sub1:" and "inc4:" labels, it will not match unless there
101is a "subl" in between those labels. If it existed somewhere else in the file,
102that would not count: "grep subl" matches if subl exists anywhere in the
103file.
104
105
106
107=head2 The FileCheck -check-prefix option
108
109The FileCheck -check-prefix option allows multiple test configurations to be
110driven from one .ll file. This is useful in many circumstances, for example,
111testing different architectural variants with llc. Here's a simple example:
112
113 ; RUN: llvm-as < %s | llc -mtriple=i686-apple-darwin9 -mattr=sse41 \
114 ; RUN: | <b>FileCheck %s -check-prefix=X32</b>
115 ; RUN: llvm-as < %s | llc -mtriple=x86_64-apple-darwin9 -mattr=sse41 \
116 ; RUN: | <b>FileCheck %s -check-prefix=X64</b>
117
118 define <4 x i32> @pinsrd_1(i32 %s, <4 x i32> %tmp) nounwind {
119 %tmp1 = insertelement <4 x i32>; %tmp, i32 %s, i32 1
120 ret <4 x i32> %tmp1
121 ; <b>X32:</b> pinsrd_1:
122 ; <b>X32:</b> pinsrd $1, 4(%esp), %xmm0
123
124 ; <b>X64:</b> pinsrd_1:
125 ; <b>X64:</b> pinsrd $1, %edi, %xmm0
126 }
127
128In this case, we're testing that we get the expected code generation with
129both 32-bit and 64-bit code generation.
130
131
132
133=head2 The "CHECK-NEXT:" directive
134
135Sometimes you want to match lines and would like to verify that matches
136happen on exactly consequtive lines with no other lines in between them. In
137this case, you can use CHECK: and CHECK-NEXT: directives to specify this. If
138you specified a custom check prefix, just use "<PREFIX>-NEXT:". For
139example, something like this works as you'd expect:
140
141 define void @t2(<2 x double>* %r, <2 x double&gt;* %A, double %B) {
142 %tmp3 = load <2 x double&gt;* %A, align 16
143 %tmp7 = insertelement <2 x double&gt; undef, double %B, i32 0
144 %tmp9 = shufflevector <2 x double&gt; %tmp3,
145 <2 x double&gt; %tmp7,
146 <2 x i32&gt; < i32 0, i32 2 &gt;
147 store <2 x double&gt; %tmp9, <2 x double&gt;* %r, align 16
148 ret void
149
150 ; <b>CHECK:</b> t2:
151 ; <b>CHECK:</b> movl 8(%esp), %eax
152 ; <b>CHECK-NEXT:</b> movapd (%eax), %xmm0
153 ; <b>CHECK-NEXT:</b> movhpd 12(%esp), %xmm0
154 ; <b>CHECK-NEXT:</b> movl 4(%esp), %eax
155 ; <b>CHECK-NEXT:</b> movapd %xmm0, (%eax)
156 ; <b>CHECK-NEXT:</b> ret
157 }
158
159CHECK-NEXT: directives reject the input unless there is exactly one newline
160between it an the previous directive. A CHECK-NEXT cannot be the first
161directive in a file.
162
163
164
165=head2 The "CHECK-NOT:" directive
166
167The CHECK-NOT: directive is used to verify that a string doesn't occur
168between two matches (or the first match and the beginning of the file). For
169example, to verify that a load is removed by a transformation, a test like this
170can be used:
171
172 define i8 @coerce_offset0(i32 %V, i32* %P) {
173 store i32 %V, i32* %P
174
175 %P2 = bitcast i32* %P to i8*
176 %P3 = getelementptr i8* %P2, i32 2
177
178 %A = load i8* %P3
179 ret i8 %A
180 ; <b>CHECK:</b> @coerce_offset0
181 ; <b>CHECK-NOT:</b> load
182 ; <b>CHECK:</b> ret i8
183 }
184
185
186
187=head2 FileCheck Pattern Matching Syntax
188
189The CHECK: and CHECK-NOT: directives both take a pattern to match. For most
190uses of FileCheck, fixed string matching is perfectly sufficient. For some
191things, a more flexible form of matching is desired. To support this, FileCheck
192allows you to specify regular expressions in matching strings, surrounded by
193double braces: B<{{yourregex}}>. Because we want to use fixed string
194matching for a majority of what we do, FileCheck has been designed to support
195mixing and matching fixed string matching with regular expressions. This allows
196you to write things like this:
197
198 ; CHECK: movhpd <b>{{[0-9]+}}</b>(%esp), <b>{{%xmm[0-7]}}</b>
199
200In this case, any offset from the ESP register will be allowed, and any xmm
201register will be allowed.
202
203Because regular expressions are enclosed with double braces, they are
204visually distinct, and you don't need to use escape characters within the double
205braces like you would in C. In the rare case that you want to match double
206braces explicitly from the input, you can use something ugly like
207B<{{[{][{]}}> as your pattern.
208
209
210
211=head2 FileCheck Variables
212
213It is often useful to match a pattern and then verify that it occurs again
214later in the file. For codegen tests, this can be useful to allow any register,
215but verify that that register is used consistently later. To do this, FileCheck
216allows named variables to be defined and substituted into patterns. Here is a
217simple example:
218
219 ; CHECK: test5:
220 ; CHECK: notw <b>[[REGISTER:%[a-z]+]]</b>
221 ; CHECK: andw {{.*}}<b>[[REGISTER]]</b>
222
223The first check line matches a regex (<tt>%[a-z]+</tt>) and captures it into
224the variables "REGISTER". The second line verifies that whatever is in REGISTER
225occurs later in the file after an "andw". FileCheck variable references are
226always contained in <tt>[[ ]]</tt> pairs, are named, and their names can be
Daniel Dunbar964ac012009-11-22 22:07:50 +0000227formed with the regex "<tt>[a-zA-Z_][a-zA-Z0-9_]*</tt>". If a colon follows the
Chris Lattner704ac902009-10-17 04:47:42 +0000228name, then it is a definition of the variable, if not, it is a use.
229
230FileCheck variables can be defined multiple times, and uses always get the
231latest value. Note that variables are all read at the start of a "CHECK" line
232and are all defined at the end. This means that if you have something like
233"<tt>CHECK: [[XYZ:.*]]x[[XYZ]]<tt>" that the check line will read the previous
234value of the XYZ variable and define a new one after the match is performed. If
235you need to do something like this you can probably take advantage of the fact
236that FileCheck is not actually line-oriented when it matches, this allows you to
237define two separate CHECK lines that match on the same line.
238
239
240
Chris Lattner9d3787b2009-08-15 15:38:11 +0000241=head1 AUTHORS
242
243Maintained by The LLVM Team (L<http://llvm.org>).
244
245=cut