8a8eddfde45a5a0dd27bf4646738d9d172c3d1e8
[oota-llvm.git] / docs / CommandGuide / FileCheck.pod
1
2 =pod
3
4 =head1 NAME
5
6 FileCheck - Flexible pattern matching file verifier
7
8 =head1 SYNOPSIS
9
10 B<FileCheck> I<match-filename> [I<--check-prefix=XXX>] [I<--strict-whitespace>]
11
12 =head1 DESCRIPTION
13
14 B<FileCheck> reads two files (one from standard input, and one specified on the
15 command line) and uses one to verify the other.  This behavior is particularly
16 useful 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
18 whatever is interesting).  This is similar to using grep, but it is optimized
19 for matching multiple different inputs in one file in a specific order.
20
21 The I<match-filename> file specifies the file that contains the patterns to
22 match.  The file to verify is always read from standard input.
23
24 =head1 OPTIONS
25
26 =over
27
28 =item B<-help>
29
30 Print a summary of command line options.
31
32 =item B<--check-prefix> I<prefix>
33
34 FileCheck searches the contents of I<match-filename> for patterns to match.  By
35 default, these patterns are prefixed with "CHECK:".  If you'd like to use a
36 different prefix (e.g. because the same input file is checking multiple
37 different tool or options), the B<--check-prefix> argument allows you to specify
38 a specific prefix to match.
39
40 =item B<--strict-whitespace>
41
42 By default, FileCheck canonicalizes input horizontal whitespace (spaces and
43 tabs) which causes it to ignore these differences (a space will match a tab).
44 The --strict-whitespace argument disables this behavior.
45
46 =item B<-version>
47
48 Show the version number of this program.
49
50 =back
51
52 =head1 EXIT STATUS
53
54 If B<FileCheck> verifies that the file matches the expected contents, it exits
55 with 0.  Otherwise, if not, or if an error occurs, it will exit with a non-zero
56 value.
57
58 =head1 TUTORIAL
59
60 FileCheck is typically used from LLVM regression tests, being invoked on the RUN
61 line of the test.  A simple example of using FileCheck from a RUN line looks
62 like this:
63
64   ; RUN: llvm-as < %s | llc -march=x86-64 | FileCheck %s
65
66 This syntax says to pipe the current file ("%s") into llvm-as, pipe that into
67 llc, then pipe the output of llc into FileCheck.  This means that FileCheck will
68 be verifying its standard input (the llc output) against the filename argument
69 specified (the original .ll file specified by "%s").  To see how this works,
70 lets 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
88 Here you can see some "CHECK:" lines specified in comments.  Now you can see
89 how the file is piped into llvm-as, then llc, and the machine code output is
90 what we are verifying.  FileCheck checks the machine code output to verify that
91 it matches what the "CHECK:" lines specify.
92
93 The syntax of the CHECK: lines is very simple: they are fixed strings that
94 must occur in order.  FileCheck defaults to ignoring horizontal whitespace
95 differences (e.g. a space is allowed to match a tab) but otherwise, the contents
96 of the CHECK: line is required to match some thing in the test file exactly.
97
98 One nice thing about FileCheck (compared to grep) is that it allows merging
99 test cases together into logical groups.  For example, because the test above
100 is checking for the "sub1:" and "inc4:" labels, it will not match unless there
101 is a "subl" in between those labels.  If it existed somewhere else in the file,
102 that would not count: "grep subl" matches if subl exists anywhere in the
103 file.
104
105
106
107 =head2 The FileCheck -check-prefix option
108
109 The FileCheck -check-prefix option allows multiple test configurations to be
110 driven from one .ll file.  This is useful in many circumstances, for example,
111 testing 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
128 In this case, we're testing that we get the expected code generation with
129 both 32-bit and 64-bit code generation.
130
131
132
133 =head2 The "CHECK-NEXT:" directive
134
135 Sometimes you want to match lines and would like to verify that matches
136 happen on exactly consequtive lines with no other lines in between them.  In
137 this case, you can use CHECK: and CHECK-NEXT: directives to specify this.  If
138 you specified a custom check prefix, just use "<PREFIX>-NEXT:".  For
139 example, 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
159 CHECK-NEXT: directives reject the input unless there is exactly one newline
160 between it an the previous directive.  A CHECK-NEXT cannot be the first
161 directive in a file.
162
163
164
165 =head2 The "CHECK-NOT:" directive
166
167 The CHECK-NOT: directive is used to verify that a string doesn't occur
168 between two matches (or before the first match, or after the last match).  For
169 example, to verify that a load is removed by a transformation, a test like this
170 can 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
189 The CHECK: and CHECK-NOT: directives both take a pattern to match.  For most
190 uses of FileCheck, fixed string matching is perfectly sufficient.  For some
191 things, a more flexible form of matching is desired.  To support this, FileCheck
192 allows you to specify regular expressions in matching strings, surrounded by
193 double braces: B<{{yourregex}}>.  Because we want to use fixed string
194 matching for a majority of what we do, FileCheck has been designed to support
195 mixing and matching fixed string matching with regular expressions.  This allows
196 you to write things like this:
197
198   ; CHECK: movhpd       <b>{{[0-9]+}}</b>(%esp), <b>{{%xmm[0-7]}}</b>
199
200 In this case, any offset from the ESP register will be allowed, and any xmm
201 register will be allowed.
202
203 Because regular expressions are enclosed with double braces, they are
204 visually distinct, and you don't need to use escape characters within the double
205 braces like you would in C.  In the rare case that you want to match double
206 braces explicitly from the input, you can use something ugly like
207 B<{{[{][{]}}> as your pattern.
208
209
210
211 =head2 FileCheck Variables
212
213 It is often useful to match a pattern and then verify that it occurs again
214 later in the file.  For codegen tests, this can be useful to allow any register,
215 but verify that that register is used consistently later.  To do this, FileCheck
216 allows named variables to be defined and substituted into patterns.  Here is a
217 simple example:
218
219   ; CHECK: test5:
220   ; CHECK:    notw      <b>[[REGISTER:%[a-z]+]]</b>
221   ; CHECK:    andw      {{.*}}<b>[[REGISTER]]</b>
222
223 The first check line matches a regex (<tt>%[a-z]+</tt>) and captures it into
224 the variables "REGISTER".  The second line verifies that whatever is in REGISTER
225 occurs later in the file after an "andw".  FileCheck variable references are
226 always contained in <tt>[[ ]]</tt> pairs, are named, and their names can be
227 formed with the regex "<tt>[a-zA-Z_][a-zA-Z0-9_]*</tt>".  If a colon follows the
228 name, then it is a definition of the variable, if not, it is a use.
229
230 FileCheck variables can be defined multiple times, and uses always get the
231 latest value.  Note that variables are all read at the start of a "CHECK" line
232 and 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
234 value of the XYZ variable and define a new one after the match is performed.  If
235 you need to do something like this you can probably take advantage of the fact
236 that FileCheck is not actually line-oriented when it matches, this allows you to
237 define two separate CHECK lines that match on the same line.
238
239
240
241 =head1 AUTHORS
242
243 Maintained by The LLVM Team (L<http://llvm.org>).
244
245 =cut