[InstCombine] Signed saturation patterns
[llvm-core.git] / test / Bindings / OCaml / core.ml
blobf649dbdb8fa20c283cbdcc2e852a7dd8648555e6
1 (* RUN: rm -rf %t && mkdir -p %t && cp %s %t/core.ml
2 * RUN: %ocamlc -g -w +A -package llvm.analysis -package llvm.bitwriter -linkpkg %t/core.ml -o %t/executable
3 * RUN: %t/executable %t/bitcode.bc
4 * RUN: %ocamlopt -g -w +A -package llvm.analysis -package llvm.bitwriter -linkpkg %t/core.ml -o %t/executable
5 * RUN: %t/executable %t/bitcode.bc
6 * RUN: llvm-dis < %t/bitcode.bc > %t/dis.ll
7 * RUN: FileCheck %s < %t/dis.ll
8 * Do a second pass for things that shouldn't be anywhere.
9 * RUN: FileCheck -check-prefix=CHECK-NOWHERE %s < %t/dis.ll
10 * XFAIL: vg_leak
13 (* Note: It takes several seconds for ocamlopt to link an executable with
14 libLLVMCore.a, so it's better to write a big test than a bunch of
15 little ones. *)
17 open Llvm
18 open Llvm_bitwriter
21 (* Tiny unit test framework - really just to help find which line is busted *)
22 let exit_status = ref 0
23 let suite_name = ref ""
24 let group_name = ref ""
25 let case_num = ref 0
26 let print_checkpoints = false
27 let context = global_context ()
28 let i1_type = Llvm.i1_type context
29 let i8_type = Llvm.i8_type context
30 let i16_type = Llvm.i16_type context
31 let i32_type = Llvm.i32_type context
32 let i64_type = Llvm.i64_type context
33 let void_type = Llvm.void_type context
34 let float_type = Llvm.float_type context
35 let double_type = Llvm.double_type context
36 let fp128_type = Llvm.fp128_type context
38 let group name =
39 group_name := !suite_name ^ "/" ^ name;
40 case_num := 0;
41 if print_checkpoints then
42 prerr_endline (" " ^ name ^ "...")
44 let insist cond =
45 incr case_num;
46 if not cond then
47 exit_status := 10;
48 match print_checkpoints, cond with
49 | false, true -> ()
50 | false, false ->
51 prerr_endline ("FAILED: " ^ !suite_name ^ "/" ^ !group_name ^ " #" ^ (string_of_int !case_num))
52 | true, true ->
53 prerr_endline (" " ^ (string_of_int !case_num))
54 | true, false ->
55 prerr_endline (" " ^ (string_of_int !case_num) ^ " FAIL")
57 let suite name f =
58 suite_name := name;
59 if print_checkpoints then
60 prerr_endline (name ^ ":");
61 f ()
64 (*===-- Fixture -----------------------------------------------------------===*)
66 let filename = Sys.argv.(1)
67 let m = create_module context filename
69 (*===-- Contained types --------------------------------------------------===*)
71 let test_contained_types () =
72 let pointer_i32 = pointer_type i32_type in
73 insist (i32_type = (Array.get (subtypes pointer_i32) 0));
75 let ar = struct_type context [| i32_type; i8_type |] in
76 insist (i32_type = (Array.get (subtypes ar)) 0);
77 insist (i8_type = (Array.get (subtypes ar)) 1)
80 (*===-- Conversion --------------------------------------------------------===*)
82 let test_conversion () =
83 insist ("i32" = (string_of_lltype i32_type));
84 let c = const_int i32_type 42 in
85 insist ("i32 42" = (string_of_llvalue c))
88 (*===-- Target ------------------------------------------------------------===*)
90 let test_target () =
91 begin group "triple";
92 let trip = "i686-apple-darwin8" in
93 set_target_triple trip m;
94 insist (trip = target_triple m)
95 end;
97 begin group "layout";
98 let layout = "e" in
99 set_data_layout layout m;
100 insist (layout = data_layout m)
102 (* CHECK: target datalayout = "e"
103 * CHECK: target triple = "i686-apple-darwin8"
107 (*===-- Constants ---------------------------------------------------------===*)
109 let test_constants () =
110 (* CHECK: const_int{{.*}}i32{{.*}}-1
112 group "int";
113 let c = const_int i32_type (-1) in
114 ignore (define_global "const_int" c m);
115 insist (i32_type = type_of c);
116 insist (is_constant c);
117 insist (Some (-1L) = int64_of_const c);
119 (* CHECK: const_sext_int{{.*}}i64{{.*}}-1
121 group "sext int";
122 let c = const_int i64_type (-1) in
123 ignore (define_global "const_sext_int" c m);
124 insist (i64_type = type_of c);
125 insist (Some (-1L) = int64_of_const c);
127 (* CHECK: const_zext_int64{{.*}}i64{{.*}}4294967295
129 group "zext int64";
130 let c = const_of_int64 i64_type (Int64.of_string "4294967295") false in
131 ignore (define_global "const_zext_int64" c m);
132 insist (i64_type = type_of c);
133 insist (Some 4294967295L = int64_of_const c);
135 (* CHECK: const_int_string{{.*}}i32{{.*}}-1
137 group "int string";
138 let c = const_int_of_string i32_type "-1" 10 in
139 ignore (define_global "const_int_string" c m);
140 insist (i32_type = type_of c);
141 insist (None = (string_of_const c));
142 insist (None = float_of_const c);
143 insist (Some (-1L) = int64_of_const c);
145 (* CHECK: const_int64{{.*}}i64{{.*}}9223372036854775807
147 group "max int64";
148 let c = const_of_int64 i64_type 9223372036854775807L true in
149 ignore (define_global "const_int64" c m) ;
150 insist (i64_type = type_of c);
151 insist (Some 9223372036854775807L = int64_of_const c);
153 if Sys.word_size = 64; then begin
154 group "long int";
155 let c = const_int i64_type (1 lsl 61) in
156 insist (c = const_of_int64 i64_type (Int64.of_int (1 lsl 61)) false)
157 end;
159 (* CHECK: @const_string = global {{.*}}c"cruel\00world"
161 group "string";
162 let c = const_string context "cruel\000world" in
163 ignore (define_global "const_string" c m);
164 insist ((array_type i8_type 11) = type_of c);
165 insist ((Some "cruel\000world") = (string_of_const c));
167 (* CHECK: const_stringz{{.*}}"hi\00again\00"
169 group "stringz";
170 let c = const_stringz context "hi\000again" in
171 ignore (define_global "const_stringz" c m);
172 insist ((array_type i8_type 9) = type_of c);
174 (* CHECK: const_single{{.*}}2.75
175 * CHECK: const_double{{.*}}3.1459
176 * CHECK: const_double_string{{.*}}2
177 * CHECK: const_fake_fp128{{.*}}0xL00000000000000004000000000000000
178 * CHECK: const_fp128_string{{.*}}0xLF3CB1CCF26FBC178452FB4EC7F91973F
180 begin group "real";
181 let cs = const_float float_type 2.75 in
182 ignore (define_global "const_single" cs m);
183 insist (float_type = type_of cs);
184 insist (float_of_const cs = Some 2.75);
186 let cd = const_float double_type 3.1459 in
187 ignore (define_global "const_double" cd m);
188 insist (double_type = type_of cd);
189 insist (float_of_const cd = Some 3.1459);
191 let cd = const_float_of_string double_type "2" in
192 ignore (define_global "const_double_string" cd m);
193 insist (double_type = type_of cd);
194 insist (float_of_const cd = Some 2.);
196 let cd = const_float fp128_type 2. in
197 ignore (define_global "const_fake_fp128" cd m);
198 insist (fp128_type = type_of cd);
199 insist (float_of_const cd = Some 2.);
201 let cd = const_float_of_string fp128_type "1e400" in
202 ignore (define_global "const_fp128_string" cd m);
203 insist (fp128_type = type_of cd);
204 insist (float_of_const cd = None);
205 end;
207 let one = const_int i16_type 1 in
208 let two = const_int i16_type 2 in
209 let three = const_int i32_type 3 in
210 let four = const_int i32_type 4 in
212 (* CHECK: const_array{{.*}}[i32 3, i32 4]
214 group "array";
215 let c = const_array i32_type [| three; four |] in
216 ignore (define_global "const_array" c m);
217 insist ((array_type i32_type 2) = (type_of c));
218 insist (three = (const_element c 0));
219 insist (four = (const_element c 1));
221 (* CHECK: const_vector{{.*}}<i16 1, i16 2{{.*}}>
223 group "vector";
224 let c = const_vector [| one; two; one; two;
225 one; two; one; two |] in
226 ignore (define_global "const_vector" c m);
227 insist ((vector_type i16_type 8) = (type_of c));
229 (* CHECK: const_structure{{.*.}}i16 1, i16 2, i32 3, i32 4
231 group "structure";
232 let c = const_struct context [| one; two; three; four |] in
233 ignore (define_global "const_structure" c m);
234 insist ((struct_type context [| i16_type; i16_type; i32_type; i32_type |])
235 = (type_of c));
237 (* CHECK: const_null{{.*}}zeroinit
239 group "null";
240 let c = const_null (packed_struct_type context [| i1_type; i8_type; i64_type;
241 double_type |]) in
242 ignore (define_global "const_null" c m);
244 (* CHECK: const_all_ones{{.*}}-1
246 group "all ones";
247 let c = const_all_ones i64_type in
248 ignore (define_global "const_all_ones" c m);
250 group "pointer null"; begin
251 (* CHECK: const_pointer_null = global i64* null
253 let c = const_pointer_null (pointer_type i64_type) in
254 ignore (define_global "const_pointer_null" c m);
255 end;
257 (* CHECK: const_undef{{.*}}undef
259 group "undef";
260 let c = undef i1_type in
261 ignore (define_global "const_undef" c m);
262 insist (i1_type = type_of c);
263 insist (is_undef c);
265 group "constant arithmetic";
266 (* CHECK: @const_neg = global i64 sub
267 * CHECK: @const_nsw_neg = global i64 sub nsw
268 * CHECK: @const_nuw_neg = global i64 sub nuw
269 * CHECK: @const_fneg = global double fneg
270 * CHECK: @const_not = global i64 xor
271 * CHECK: @const_add = global i64 add
272 * CHECK: @const_nsw_add = global i64 add nsw
273 * CHECK: @const_nuw_add = global i64 add nuw
274 * CHECK: @const_fadd = global double fadd
275 * CHECK: @const_sub = global i64 sub
276 * CHECK: @const_nsw_sub = global i64 sub nsw
277 * CHECK: @const_nuw_sub = global i64 sub nuw
278 * CHECK: @const_fsub = global double fsub
279 * CHECK: @const_mul = global i64 mul
280 * CHECK: @const_nsw_mul = global i64 mul nsw
281 * CHECK: @const_nuw_mul = global i64 mul nuw
282 * CHECK: @const_fmul = global double fmul
283 * CHECK: @const_udiv = global i64 udiv
284 * CHECK: @const_sdiv = global i64 sdiv
285 * CHECK: @const_exact_sdiv = global i64 sdiv exact
286 * CHECK: @const_fdiv = global double fdiv
287 * CHECK: @const_urem = global i64 urem
288 * CHECK: @const_srem = global i64 srem
289 * CHECK: @const_frem = global double frem
290 * CHECK: @const_and = global i64 and
291 * CHECK: @const_or = global i64 or
292 * CHECK: @const_xor = global i64 xor
293 * CHECK: @const_icmp = global i1 icmp sle
294 * CHECK: @const_fcmp = global i1 fcmp ole
296 let void_ptr = pointer_type i8_type in
297 let five = const_int i64_type 5 in
298 let ffive = const_uitofp five double_type in
299 let foldbomb_gv = define_global "FoldBomb" (const_null i8_type) m in
300 let foldbomb = const_ptrtoint foldbomb_gv i64_type in
301 let ffoldbomb = const_uitofp foldbomb double_type in
302 ignore (define_global "const_neg" (const_neg foldbomb) m);
303 ignore (define_global "const_nsw_neg" (const_nsw_neg foldbomb) m);
304 ignore (define_global "const_nuw_neg" (const_nuw_neg foldbomb) m);
305 ignore (define_global "const_fneg" (const_fneg ffoldbomb) m);
306 ignore (define_global "const_not" (const_not foldbomb) m);
307 ignore (define_global "const_add" (const_add foldbomb five) m);
308 ignore (define_global "const_nsw_add" (const_nsw_add foldbomb five) m);
309 ignore (define_global "const_nuw_add" (const_nuw_add foldbomb five) m);
310 ignore (define_global "const_fadd" (const_fadd ffoldbomb ffive) m);
311 ignore (define_global "const_sub" (const_sub foldbomb five) m);
312 ignore (define_global "const_nsw_sub" (const_nsw_sub foldbomb five) m);
313 ignore (define_global "const_nuw_sub" (const_nuw_sub foldbomb five) m);
314 ignore (define_global "const_fsub" (const_fsub ffoldbomb ffive) m);
315 ignore (define_global "const_mul" (const_mul foldbomb five) m);
316 ignore (define_global "const_nsw_mul" (const_nsw_mul foldbomb five) m);
317 ignore (define_global "const_nuw_mul" (const_nuw_mul foldbomb five) m);
318 ignore (define_global "const_fmul" (const_fmul ffoldbomb ffive) m);
319 ignore (define_global "const_udiv" (const_udiv foldbomb five) m);
320 ignore (define_global "const_sdiv" (const_sdiv foldbomb five) m);
321 ignore (define_global "const_exact_sdiv" (const_exact_sdiv foldbomb five) m);
322 ignore (define_global "const_fdiv" (const_fdiv ffoldbomb ffive) m);
323 ignore (define_global "const_urem" (const_urem foldbomb five) m);
324 ignore (define_global "const_srem" (const_srem foldbomb five) m);
325 ignore (define_global "const_frem" (const_frem ffoldbomb ffive) m);
326 ignore (define_global "const_and" (const_and foldbomb five) m);
327 ignore (define_global "const_or" (const_or foldbomb five) m);
328 ignore (define_global "const_xor" (const_xor foldbomb five) m);
329 ignore (define_global "const_icmp" (const_icmp Icmp.Sle foldbomb five) m);
330 ignore (define_global "const_fcmp" (const_fcmp Fcmp.Ole ffoldbomb ffive) m);
332 group "constant casts";
333 (* CHECK: const_trunc{{.*}}trunc
334 * CHECK: const_sext{{.*}}sext
335 * CHECK: const_zext{{.*}}zext
336 * CHECK: const_fptrunc{{.*}}fptrunc
337 * CHECK: const_fpext{{.*}}fpext
338 * CHECK: const_uitofp{{.*}}uitofp
339 * CHECK: const_sitofp{{.*}}sitofp
340 * CHECK: const_fptoui{{.*}}fptoui
341 * CHECK: const_fptosi{{.*}}fptosi
342 * CHECK: const_ptrtoint{{.*}}ptrtoint
343 * CHECK: const_inttoptr{{.*}}inttoptr
344 * CHECK: const_bitcast{{.*}}bitcast
345 * CHECK: const_intcast{{.*}}zext
347 let i128_type = integer_type context 128 in
348 ignore (define_global "const_trunc" (const_trunc (const_add foldbomb five)
349 i8_type) m);
350 ignore (define_global "const_sext" (const_sext foldbomb i128_type) m);
351 ignore (define_global "const_zext" (const_zext foldbomb i128_type) m);
352 ignore (define_global "const_fptrunc" (const_fptrunc ffoldbomb float_type) m);
353 ignore (define_global "const_fpext" (const_fpext ffoldbomb fp128_type) m);
354 ignore (define_global "const_uitofp" (const_uitofp foldbomb double_type) m);
355 ignore (define_global "const_sitofp" (const_sitofp foldbomb double_type) m);
356 ignore (define_global "const_fptoui" (const_fptoui ffoldbomb i32_type) m);
357 ignore (define_global "const_fptosi" (const_fptosi ffoldbomb i32_type) m);
358 ignore (define_global "const_ptrtoint" (const_ptrtoint
359 (const_gep (const_null (pointer_type i8_type))
360 [| const_int i32_type 1 |])
361 i32_type) m);
362 ignore (define_global "const_inttoptr" (const_inttoptr (const_add foldbomb five)
363 void_ptr) m);
364 ignore (define_global "const_bitcast" (const_bitcast ffoldbomb i64_type) m);
365 ignore (define_global "const_intcast"
366 (const_intcast foldbomb i128_type ~is_signed:false) m);
368 group "misc constants";
369 (* CHECK: const_size_of{{.*}}getelementptr{{.*}}null
370 * CHECK: const_gep{{.*}}getelementptr
371 * CHECK: const_select{{.*}}select
372 * CHECK: const_extractelement{{.*}}extractelement
373 * CHECK: const_insertelement{{.*}}insertelement
374 * CHECK: const_shufflevector = global <4 x i32> <i32 0, i32 1, i32 1, i32 0>
376 ignore (define_global "const_size_of" (size_of (pointer_type i8_type)) m);
377 ignore (define_global "const_gep" (const_gep foldbomb_gv [| five |]) m);
378 ignore (define_global "const_select" (const_select
379 (const_icmp Icmp.Sle foldbomb five)
380 (const_int i8_type (-1))
381 (const_int i8_type 0)) m);
382 let zero = const_int i32_type 0 in
383 let one = const_int i32_type 1 in
384 ignore (define_global "const_extractelement" (const_extractelement
385 (const_vector [| zero; one; zero; one |])
386 (const_trunc foldbomb i32_type)) m);
387 ignore (define_global "const_insertelement" (const_insertelement
388 (const_vector [| zero; one; zero; one |])
389 zero (const_trunc foldbomb i32_type)) m);
390 ignore (define_global "const_shufflevector" (const_shufflevector
391 (const_vector [| zero; one |])
392 (const_vector [| one; zero |])
393 (const_vector [| const_int i32_type 0; const_int i32_type 1;
394 const_int i32_type 2; const_int i32_type 3 |])) m);
396 group "asm"; begin
397 let ft = function_type void_type [| i32_type; i32_type; i32_type |] in
398 ignore (const_inline_asm
401 "{cx},{ax},{di},~{dirflag},~{fpsr},~{flags},~{edi},~{ecx}"
402 true
403 false)
404 end;
406 group "recursive struct"; begin
407 let nsty = named_struct_type context "rec" in
408 let pty = pointer_type nsty in
409 struct_set_body nsty [| i32_type; pty |] false;
410 let elts = [| const_int i32_type 4; const_pointer_null pty |] in
411 let grec_init = const_named_struct nsty elts in
412 ignore (define_global "grec" grec_init m);
413 ignore (string_of_lltype nsty);
417 (*===-- Attributes --------------------------------------------------------===*)
419 let test_attributes () =
420 group "enum attrs";
421 let nonnull_kind = enum_attr_kind "nonnull" in
422 let dereferenceable_kind = enum_attr_kind "dereferenceable" in
423 insist (nonnull_kind = (enum_attr_kind "nonnull"));
424 insist (nonnull_kind <> dereferenceable_kind);
426 let nonnull =
427 create_enum_attr context "nonnull" 0L in
428 let dereferenceable_4 =
429 create_enum_attr context "dereferenceable" 4L in
430 let dereferenceable_8 =
431 create_enum_attr context "dereferenceable" 8L in
432 insist (nonnull <> dereferenceable_4);
433 insist (dereferenceable_4 <> dereferenceable_8);
434 insist (nonnull = (create_enum_attr context "nonnull" 0L));
435 insist ((repr_of_attr nonnull) =
436 AttrRepr.Enum(nonnull_kind, 0L));
437 insist ((repr_of_attr dereferenceable_4) =
438 AttrRepr.Enum(dereferenceable_kind, 4L));
439 insist ((attr_of_repr context (repr_of_attr nonnull)) =
440 nonnull);
441 insist ((attr_of_repr context (repr_of_attr dereferenceable_4)) =
442 dereferenceable_4);
444 group "string attrs";
445 let foo_bar = create_string_attr context "foo" "bar" in
446 let foo_baz = create_string_attr context "foo" "baz" in
447 insist (foo_bar <> foo_baz);
448 insist (foo_bar = (create_string_attr context "foo" "bar"));
449 insist ((repr_of_attr foo_bar) = AttrRepr.String("foo", "bar"));
450 insist ((attr_of_repr context (repr_of_attr foo_bar)) = foo_bar);
453 (*===-- Global Values -----------------------------------------------------===*)
455 let test_global_values () =
456 let (++) x f = f x; x in
457 let zero32 = const_null i32_type in
459 (* CHECK: GVal01
461 group "naming";
462 let g = define_global "TEMPORARY" zero32 m in
463 insist ("TEMPORARY" = value_name g);
464 set_value_name "GVal01" g;
465 insist ("GVal01" = value_name g);
467 (* CHECK: GVal02{{.*}}linkonce
469 group "linkage";
470 let g = define_global "GVal02" zero32 m ++
471 set_linkage Linkage.Link_once in
472 insist (Linkage.Link_once = linkage g);
474 (* CHECK: GVal03{{.*}}Hanalei
476 group "section";
477 let g = define_global "GVal03" zero32 m ++
478 set_section "Hanalei" in
479 insist ("Hanalei" = section g);
481 (* CHECK: GVal04{{.*}}hidden
483 group "visibility";
484 let g = define_global "GVal04" zero32 m ++
485 set_visibility Visibility.Hidden in
486 insist (Visibility.Hidden = visibility g);
488 (* CHECK: GVal05{{.*}}align 128
490 group "alignment";
491 let g = define_global "GVal05" zero32 m ++
492 set_alignment 128 in
493 insist (128 = alignment g);
495 (* CHECK: GVal06{{.*}}dllexport
497 group "dll_storage_class";
498 let g = define_global "GVal06" zero32 m ++
499 set_dll_storage_class DLLStorageClass.DLLExport in
500 insist (DLLStorageClass.DLLExport = dll_storage_class g)
503 (*===-- Global Variables --------------------------------------------------===*)
505 let test_global_variables () =
506 let (++) x f = f x; x in
507 let forty_two32 = const_int i32_type 42 in
509 group "declarations"; begin
510 (* CHECK: @GVar01 = external global i32
511 * CHECK: @QGVar01 = external addrspace(3) global i32
513 insist (None == lookup_global "GVar01" m);
514 let g = declare_global i32_type "GVar01" m in
515 insist (is_declaration g);
516 insist (pointer_type float_type ==
517 type_of (declare_global float_type "GVar01" m));
518 insist (g == declare_global i32_type "GVar01" m);
519 insist (match lookup_global "GVar01" m with Some x -> x = g
520 | None -> false);
522 insist (None == lookup_global "QGVar01" m);
523 let g = declare_qualified_global i32_type "QGVar01" 3 m in
524 insist (is_declaration g);
525 insist (qualified_pointer_type float_type 3 ==
526 type_of (declare_qualified_global float_type "QGVar01" 3 m));
527 insist (g == declare_qualified_global i32_type "QGVar01" 3 m);
528 insist (match lookup_global "QGVar01" m with Some x -> x = g
529 | None -> false);
530 end;
532 group "definitions"; begin
533 (* CHECK: @GVar02 = global i32 42
534 * CHECK: @GVar03 = global i32 42
535 * CHECK: @QGVar02 = addrspace(3) global i32 42
536 * CHECK: @QGVar03 = addrspace(3) global i32 42
538 let g = define_global "GVar02" forty_two32 m in
539 let g2 = declare_global i32_type "GVar03" m ++
540 set_initializer forty_two32 in
541 insist (not (is_declaration g));
542 insist (not (is_declaration g2));
543 insist ((global_initializer g) == (global_initializer g2));
545 let g = define_qualified_global "QGVar02" forty_two32 3 m in
546 let g2 = declare_qualified_global i32_type "QGVar03" 3 m ++
547 set_initializer forty_two32 in
548 insist (not (is_declaration g));
549 insist (not (is_declaration g2));
550 insist ((global_initializer g) == (global_initializer g2));
551 end;
553 (* CHECK: GVar04{{.*}}thread_local
555 group "threadlocal";
556 let g = define_global "GVar04" forty_two32 m ++
557 set_thread_local true in
558 insist (is_thread_local g);
560 (* CHECK: GVar05{{.*}}thread_local(initialexec)
562 group "threadlocal_mode";
563 let g = define_global "GVar05" forty_two32 m ++
564 set_thread_local_mode ThreadLocalMode.InitialExec in
565 insist ((thread_local_mode g) = ThreadLocalMode.InitialExec);
567 (* CHECK: GVar06{{.*}}externally_initialized
569 group "externally_initialized";
570 let g = define_global "GVar06" forty_two32 m ++
571 set_externally_initialized true in
572 insist (is_externally_initialized g);
574 (* CHECK-NOWHERE-NOT: GVar07
576 group "delete";
577 let g = define_global "GVar07" forty_two32 m in
578 delete_global g;
580 (* CHECK: ConstGlobalVar{{.*}}constant
582 group "constant";
583 let g = define_global "ConstGlobalVar" forty_two32 m in
584 insist (not (is_global_constant g));
585 set_global_constant true g;
586 insist (is_global_constant g);
588 begin group "iteration";
589 let m = create_module context "temp" in
591 insist (At_end m = global_begin m);
592 insist (At_start m = global_end m);
594 let g1 = declare_global i32_type "One" m in
595 let g2 = declare_global i32_type "Two" m in
597 insist (Before g1 = global_begin m);
598 insist (Before g2 = global_succ g1);
599 insist (At_end m = global_succ g2);
601 insist (After g2 = global_end m);
602 insist (After g1 = global_pred g2);
603 insist (At_start m = global_pred g1);
605 let lf s x = s ^ "->" ^ value_name x in
606 insist ("->One->Two" = fold_left_globals lf "" m);
608 let rf x s = value_name x ^ "<-" ^ s in
609 insist ("One<-Two<-" = fold_right_globals rf m "");
611 dispose_module m
614 (* String globals built below are emitted here.
615 * CHECK: build_global_string{{.*}}stringval
619 (*===-- Uses --------------------------------------------------------------===*)
621 let test_uses () =
622 let ty = function_type i32_type [| i32_type; i32_type |] in
623 let fn = define_function "use_function" ty m in
624 let b = builder_at_end context (entry_block fn) in
626 let p1 = param fn 0 in
627 let p2 = param fn 1 in
628 let v1 = build_add p1 p2 "v1" b in
629 let v2 = build_add p1 v1 "v2" b in
630 let _ = build_add v1 v2 "v3" b in
632 let lf s u = value_name (user u) ^ "->" ^ s in
633 insist ("v2->v3->" = fold_left_uses lf "" v1);
634 let rf u s = value_name (user u) ^ "<-" ^ s in
635 insist ("v3<-v2<-" = fold_right_uses rf v1 "");
637 let lf s u = value_name (used_value u) ^ "->" ^ s in
638 insist ("v1->v1->" = fold_left_uses lf "" v1);
640 let rf u s = value_name (used_value u) ^ "<-" ^ s in
641 insist ("v1<-v1<-" = fold_right_uses rf v1 "");
643 ignore (build_unreachable b)
646 (*===-- Users -------------------------------------------------------------===*)
648 let test_users () =
649 let ty = function_type i32_type [| i32_type; i32_type |] in
650 let fn = define_function "user_function" ty m in
651 let b = builder_at_end context (entry_block fn) in
653 let p1 = param fn 0 in
654 let p2 = param fn 1 in
655 let a3 = build_alloca i32_type "user_alloca" b in
656 let p3 = build_load a3 "user_load" b in
657 let i = build_add p1 p2 "sum" b in
659 insist ((num_operands i) = 2);
660 insist ((operand i 0) = p1);
661 insist ((operand i 1) = p2);
663 set_operand i 1 p3;
664 insist ((operand i 1) != p2);
665 insist ((operand i 1) = p3);
667 ignore (build_unreachable b)
670 (*===-- Aliases -----------------------------------------------------------===*)
672 let test_aliases () =
673 (* CHECK: @alias = alias i32, i32* @aliasee
675 let forty_two32 = const_int i32_type 42 in
676 let v = define_global "aliasee" forty_two32 m in
677 ignore (add_alias m (pointer_type i32_type) v "alias")
680 (*===-- Functions ---------------------------------------------------------===*)
682 let test_functions () =
683 let ty = function_type i32_type [| i32_type; i64_type |] in
684 let ty2 = function_type i8_type [| i8_type; i64_type |] in
686 (* CHECK: declare i32 @Fn1(i32, i64)
688 begin group "declare";
689 insist (None = lookup_function "Fn1" m);
690 let fn = declare_function "Fn1" ty m in
691 insist (pointer_type ty = type_of fn);
692 insist (is_declaration fn);
693 insist (0 = Array.length (basic_blocks fn));
694 insist (pointer_type ty2 == type_of (declare_function "Fn1" ty2 m));
695 insist (fn == declare_function "Fn1" ty m);
696 insist (None <> lookup_function "Fn1" m);
697 insist (match lookup_function "Fn1" m with Some x -> x = fn
698 | None -> false);
699 insist (m == global_parent fn)
700 end;
702 (* CHECK-NOWHERE-NOT: Fn2
704 group "delete";
705 let fn = declare_function "Fn2" ty m in
706 delete_function fn;
708 (* CHECK: define{{.*}}Fn3
710 group "define";
711 let fn = define_function "Fn3" ty m in
712 insist (not (is_declaration fn));
713 insist (1 = Array.length (basic_blocks fn));
714 ignore (build_unreachable (builder_at_end context (entry_block fn)));
716 (* CHECK: define{{.*}}Fn4{{.*}}Param1{{.*}}Param2
718 group "params";
719 let fn = define_function "Fn4" ty m in
720 let params = params fn in
721 insist (2 = Array.length params);
722 insist (params.(0) = param fn 0);
723 insist (params.(1) = param fn 1);
724 insist (i32_type = type_of params.(0));
725 insist (i64_type = type_of params.(1));
726 set_value_name "Param1" params.(0);
727 set_value_name "Param2" params.(1);
728 ignore (build_unreachable (builder_at_end context (entry_block fn)));
730 (* CHECK: fastcc{{.*}}Fn5
732 group "callconv";
733 let fn = define_function "Fn5" ty m in
734 insist (CallConv.c = function_call_conv fn);
735 set_function_call_conv CallConv.fast fn;
736 insist (CallConv.fast = function_call_conv fn);
737 ignore (build_unreachable (builder_at_end context (entry_block fn)));
739 begin group "gc";
740 (* CHECK: Fn6{{.*}}gc{{.*}}shadowstack
742 let fn = define_function "Fn6" ty m in
743 insist (None = gc fn);
744 set_gc (Some "ocaml") fn;
745 insist (Some "ocaml" = gc fn);
746 set_gc None fn;
747 insist (None = gc fn);
748 set_gc (Some "shadowstack") fn;
749 ignore (build_unreachable (builder_at_end context (entry_block fn)));
750 end;
752 begin group "iteration";
753 let m = create_module context "temp" in
755 insist (At_end m = function_begin m);
756 insist (At_start m = function_end m);
758 let f1 = define_function "One" ty m in
759 let f2 = define_function "Two" ty m in
761 insist (Before f1 = function_begin m);
762 insist (Before f2 = function_succ f1);
763 insist (At_end m = function_succ f2);
765 insist (After f2 = function_end m);
766 insist (After f1 = function_pred f2);
767 insist (At_start m = function_pred f1);
769 let lf s x = s ^ "->" ^ value_name x in
770 insist ("->One->Two" = fold_left_functions lf "" m);
772 let rf x s = value_name x ^ "<-" ^ s in
773 insist ("One<-Two<-" = fold_right_functions rf m "");
775 dispose_module m
779 (*===-- Params ------------------------------------------------------------===*)
781 let test_params () =
782 begin group "iteration";
783 let m = create_module context "temp" in
785 let vf = define_function "void" (function_type void_type [| |]) m in
787 insist (At_end vf = param_begin vf);
788 insist (At_start vf = param_end vf);
790 let ty = function_type void_type [| i32_type; i32_type |] in
791 let f = define_function "f" ty m in
792 let p1 = param f 0 in
793 let p2 = param f 1 in
794 set_value_name "One" p1;
795 set_value_name "Two" p2;
797 insist (Before p1 = param_begin f);
798 insist (Before p2 = param_succ p1);
799 insist (At_end f = param_succ p2);
801 insist (After p2 = param_end f);
802 insist (After p1 = param_pred p2);
803 insist (At_start f = param_pred p1);
805 let lf s x = s ^ "->" ^ value_name x in
806 insist ("->One->Two" = fold_left_params lf "" f);
808 let rf x s = value_name x ^ "<-" ^ s in
809 insist ("One<-Two<-" = fold_right_params rf f "");
811 dispose_module m
815 (*===-- Basic Blocks ------------------------------------------------------===*)
817 let test_basic_blocks () =
818 let ty = function_type void_type [| |] in
820 (* CHECK: Bb1
822 group "entry";
823 let fn = declare_function "X" ty m in
824 let bb = append_block context "Bb1" fn in
825 insist (bb = entry_block fn);
826 ignore (build_unreachable (builder_at_end context bb));
828 (* CHECK-NOWHERE-NOT: Bb2
830 group "delete";
831 let fn = declare_function "X2" ty m in
832 let bb = append_block context "Bb2" fn in
833 delete_block bb;
835 group "insert";
836 let fn = declare_function "X3" ty m in
837 let bbb = append_block context "b" fn in
838 let bba = insert_block context "a" bbb in
839 insist ([| bba; bbb |] = basic_blocks fn);
840 ignore (build_unreachable (builder_at_end context bba));
841 ignore (build_unreachable (builder_at_end context bbb));
843 (* CHECK: Bb3
845 group "name/value";
846 let fn = define_function "X4" ty m in
847 let bb = entry_block fn in
848 ignore (build_unreachable (builder_at_end context bb));
849 let bbv = value_of_block bb in
850 set_value_name "Bb3" bbv;
851 insist ("Bb3" = value_name bbv);
853 group "casts";
854 let fn = define_function "X5" ty m in
855 let bb = entry_block fn in
856 ignore (build_unreachable (builder_at_end context bb));
857 insist (bb = block_of_value (value_of_block bb));
858 insist (value_is_block (value_of_block bb));
859 insist (not (value_is_block (const_null i32_type)));
861 begin group "iteration";
862 let m = create_module context "temp" in
863 let f = declare_function "Temp" (function_type i32_type [| |]) m in
865 insist (At_end f = block_begin f);
866 insist (At_start f = block_end f);
868 let b1 = append_block context "One" f in
869 let b2 = append_block context "Two" f in
871 insist (Before b1 = block_begin f);
872 insist (Before b2 = block_succ b1);
873 insist (At_end f = block_succ b2);
875 insist (After b2 = block_end f);
876 insist (After b1 = block_pred b2);
877 insist (At_start f = block_pred b1);
879 let lf s x = s ^ "->" ^ value_name (value_of_block x) in
880 insist ("->One->Two" = fold_left_blocks lf "" f);
882 let rf x s = value_name (value_of_block x) ^ "<-" ^ s in
883 insist ("One<-Two<-" = fold_right_blocks rf f "");
885 dispose_module m
889 (*===-- Instructions ------------------------------------------------------===*)
891 let test_instructions () =
892 begin group "iteration";
893 let m = create_module context "temp" in
894 let fty = function_type void_type [| i32_type; i32_type |] in
895 let f = define_function "f" fty m in
896 let bb = entry_block f in
897 let b = builder_at context (At_end bb) in
899 insist (At_end bb = instr_begin bb);
900 insist (At_start bb = instr_end bb);
902 let i1 = build_add (param f 0) (param f 1) "One" b in
903 let i2 = build_sub (param f 0) (param f 1) "Two" b in
905 insist (Before i1 = instr_begin bb);
906 insist (Before i2 = instr_succ i1);
907 insist (At_end bb = instr_succ i2);
909 insist (After i2 = instr_end bb);
910 insist (After i1 = instr_pred i2);
911 insist (At_start bb = instr_pred i1);
913 let lf s x = s ^ "->" ^ value_name x in
914 insist ("->One->Two" = fold_left_instrs lf "" bb);
916 let rf x s = value_name x ^ "<-" ^ s in
917 insist ("One<-Two<-" = fold_right_instrs rf bb "");
919 dispose_module m
920 end;
922 group "clone instr";
923 begin
924 (* CHECK: %clone = add i32 %0, 2
926 let fty = function_type void_type [| i32_type |] in
927 let fn = define_function "BuilderParent" fty m in
928 let bb = entry_block fn in
929 let b = builder_at_end context bb in
930 let p = param fn 0 in
931 let sum = build_add p p "sum" b in
932 let y = const_int i32_type 2 in
933 let clone = instr_clone sum in
934 set_operand clone 0 p;
935 set_operand clone 1 y;
936 insert_into_builder clone "clone" b;
937 ignore (build_ret_void b)
941 (*===-- Builder -----------------------------------------------------------===*)
943 let test_builder () =
944 let (++) x f = f x; x in
946 begin group "parent";
947 insist (try
948 ignore (insertion_block (builder context));
949 false
950 with Not_found ->
951 true);
953 let fty = function_type void_type [| i32_type |] in
954 let fn = define_function "BuilderParent" fty m in
955 let bb = entry_block fn in
956 let b = builder_at_end context bb in
957 let p = param fn 0 in
958 let sum = build_add p p "sum" b in
959 ignore (build_ret_void b);
961 insist (fn = block_parent bb);
962 insist (fn = param_parent p);
963 insist (bb = instr_parent sum);
964 insist (bb = insertion_block b)
965 end;
967 group "ret void";
968 begin
969 (* CHECK: ret void
971 let fty = function_type void_type [| |] in
972 let fn = declare_function "X6" fty m in
973 let b = builder_at_end context (append_block context "Bb01" fn) in
974 ignore (build_ret_void b)
975 end;
977 group "ret aggregate";
978 begin
979 (* CHECK: ret { i8, i64 } { i8 4, i64 5 }
981 let sty = struct_type context [| i8_type; i64_type |] in
982 let fty = function_type sty [| |] in
983 let fn = declare_function "XA6" fty m in
984 let b = builder_at_end context (append_block context "Bb01" fn) in
985 let agg = [| const_int i8_type 4; const_int i64_type 5 |] in
986 ignore (build_aggregate_ret agg b)
987 end;
989 (* The rest of the tests will use one big function. *)
990 let fty = function_type i32_type [| i32_type; i32_type |] in
991 let fn = define_function "X7" fty m in
992 let atentry = builder_at_end context (entry_block fn) in
993 let p1 = param fn 0 ++ set_value_name "P1" in
994 let p2 = param fn 1 ++ set_value_name "P2" in
995 let f1 = build_uitofp p1 float_type "F1" atentry in
996 let f2 = build_uitofp p2 float_type "F2" atentry in
998 let bb00 = append_block context "Bb00" fn in
999 ignore (build_unreachable (builder_at_end context bb00));
1001 group "function attribute";
1002 begin
1003 let signext = create_enum_attr context "signext" 0L in
1004 let zeroext = create_enum_attr context "zeroext" 0L in
1005 let noalias = create_enum_attr context "noalias" 0L in
1006 let nounwind = create_enum_attr context "nounwind" 0L in
1007 let no_sse = create_string_attr context "no-sse" "" in
1009 add_function_attr fn signext (AttrIndex.Param 0);
1010 add_function_attr fn noalias (AttrIndex.Param 1);
1011 insist ((function_attrs fn (AttrIndex.Param 1)) = [|noalias|]);
1012 remove_enum_function_attr fn (enum_attr_kind "noalias") (AttrIndex.Param 1);
1013 add_function_attr fn no_sse (AttrIndex.Param 1);
1014 insist ((function_attrs fn (AttrIndex.Param 1)) = [|no_sse|]);
1015 remove_string_function_attr fn "no-sse" (AttrIndex.Param 1);
1016 insist ((function_attrs fn (AttrIndex.Param 1)) = [||]);
1017 add_function_attr fn nounwind AttrIndex.Function;
1018 add_function_attr fn zeroext AttrIndex.Return;
1020 (* CHECK: define zeroext i32 @X7(i32 signext %P1, i32 %P2)
1022 end;
1024 group "casts"; begin
1025 let void_ptr = pointer_type i8_type in
1027 (* CHECK-DAG: %build_trunc = trunc i32 %P1 to i8
1028 * CHECK-DAG: %build_trunc2 = trunc i32 %P1 to i8
1029 * CHECK-DAG: %build_trunc3 = trunc i32 %P1 to i8
1030 * CHECK-DAG: %build_zext = zext i8 %build_trunc to i32
1031 * CHECK-DAG: %build_zext2 = zext i8 %build_trunc to i32
1032 * CHECK-DAG: %build_sext = sext i32 %build_zext to i64
1033 * CHECK-DAG: %build_sext2 = sext i32 %build_zext to i64
1034 * CHECK-DAG: %build_sext3 = sext i32 %build_zext to i64
1035 * CHECK-DAG: %build_uitofp = uitofp i64 %build_sext to float
1036 * CHECK-DAG: %build_sitofp = sitofp i32 %build_zext to double
1037 * CHECK-DAG: %build_fptoui = fptoui float %build_uitofp to i32
1038 * CHECK-DAG: %build_fptosi = fptosi double %build_sitofp to i64
1039 * CHECK-DAG: %build_fptrunc = fptrunc double %build_sitofp to float
1040 * CHECK-DAG: %build_fptrunc2 = fptrunc double %build_sitofp to float
1041 * CHECK-DAG: %build_fpext = fpext float %build_fptrunc to double
1042 * CHECK-DAG: %build_fpext2 = fpext float %build_fptrunc to double
1043 * CHECK-DAG: %build_inttoptr = inttoptr i32 %P1 to i8*
1044 * CHECK-DAG: %build_ptrtoint = ptrtoint i8* %build_inttoptr to i64
1045 * CHECK-DAG: %build_ptrtoint2 = ptrtoint i8* %build_inttoptr to i64
1046 * CHECK-DAG: %build_bitcast = bitcast i64 %build_ptrtoint to double
1047 * CHECK-DAG: %build_bitcast2 = bitcast i64 %build_ptrtoint to double
1048 * CHECK-DAG: %build_bitcast3 = bitcast i64 %build_ptrtoint to double
1049 * CHECK-DAG: %build_bitcast4 = bitcast i64 %build_ptrtoint to double
1050 * CHECK-DAG: %build_pointercast = bitcast i8* %build_inttoptr to i16*
1052 let inst28 = build_trunc p1 i8_type "build_trunc" atentry in
1053 let inst29 = build_zext inst28 i32_type "build_zext" atentry in
1054 let inst30 = build_sext inst29 i64_type "build_sext" atentry in
1055 let inst31 = build_uitofp inst30 float_type "build_uitofp" atentry in
1056 let inst32 = build_sitofp inst29 double_type "build_sitofp" atentry in
1057 ignore(build_fptoui inst31 i32_type "build_fptoui" atentry);
1058 ignore(build_fptosi inst32 i64_type "build_fptosi" atentry);
1059 let inst35 = build_fptrunc inst32 float_type "build_fptrunc" atentry in
1060 ignore(build_fpext inst35 double_type "build_fpext" atentry);
1061 let inst37 = build_inttoptr p1 void_ptr "build_inttoptr" atentry in
1062 let inst38 = build_ptrtoint inst37 i64_type "build_ptrtoint" atentry in
1063 ignore(build_bitcast inst38 double_type "build_bitcast" atentry);
1064 ignore(build_zext_or_bitcast inst38 double_type "build_bitcast2" atentry);
1065 ignore(build_sext_or_bitcast inst38 double_type "build_bitcast3" atentry);
1066 ignore(build_trunc_or_bitcast inst38 double_type "build_bitcast4" atentry);
1067 ignore(build_pointercast inst37 (pointer_type i16_type) "build_pointercast" atentry);
1069 ignore(build_zext_or_bitcast inst28 i32_type "build_zext2" atentry);
1070 ignore(build_sext_or_bitcast inst29 i64_type "build_sext2" atentry);
1071 ignore(build_trunc_or_bitcast p1 i8_type "build_trunc2" atentry);
1072 ignore(build_pointercast inst37 i64_type "build_ptrtoint2" atentry);
1073 ignore(build_intcast inst29 i64_type "build_sext3" atentry);
1074 ignore(build_intcast p1 i8_type "build_trunc3" atentry);
1075 ignore(build_fpcast inst35 double_type "build_fpext2" atentry);
1076 ignore(build_fpcast inst32 float_type "build_fptrunc2" atentry);
1077 end;
1079 group "comparisons"; begin
1080 (* CHECK: %build_icmp_ne = icmp ne i32 %P1, %P2
1081 * CHECK: %build_icmp_sle = icmp sle i32 %P2, %P1
1082 * CHECK: %build_fcmp_false = fcmp false float %F1, %F2
1083 * CHECK: %build_fcmp_true = fcmp true float %F2, %F1
1084 * CHECK: %build_is_null{{.*}}= icmp eq{{.*}}%X0,{{.*}}null
1085 * CHECK: %build_is_not_null = icmp ne i8* %X1, null
1086 * CHECK: %build_ptrdiff
1088 let c = build_icmp Icmp.Ne p1 p2 "build_icmp_ne" atentry in
1089 insist (Some Icmp.Ne = icmp_predicate c);
1090 insist (None = fcmp_predicate c);
1092 let c = build_icmp Icmp.Sle p2 p1 "build_icmp_sle" atentry in
1093 insist (Some Icmp.Sle = icmp_predicate c);
1094 insist (None = fcmp_predicate c);
1096 let c = build_fcmp Fcmp.False f1 f2 "build_fcmp_false" atentry in
1097 (* insist (Some Fcmp.False = fcmp_predicate c); *)
1098 insist (None = icmp_predicate c);
1100 let c = build_fcmp Fcmp.True f2 f1 "build_fcmp_true" atentry in
1101 (* insist (Some Fcmp.True = fcmp_predicate c); *)
1102 insist (None = icmp_predicate c);
1104 let g0 = declare_global (pointer_type i8_type) "g0" m in
1105 let g1 = declare_global (pointer_type i8_type) "g1" m in
1106 let p0 = build_load g0 "X0" atentry in
1107 let p1 = build_load g1 "X1" atentry in
1108 ignore (build_is_null p0 "build_is_null" atentry);
1109 ignore (build_is_not_null p1 "build_is_not_null" atentry);
1110 ignore (build_ptrdiff p1 p0 "build_ptrdiff" atentry);
1111 end;
1113 group "miscellaneous"; begin
1114 (* CHECK: %build_call = tail call cc63 zeroext i32 @{{.*}}(i32 signext %P2, i32 %P1)
1115 * CHECK: %build_select = select i1 %build_icmp, i32 %P1, i32 %P2
1116 * CHECK: %build_va_arg = va_arg i8** null, i32
1117 * CHECK: %build_extractelement = extractelement <4 x i32> %Vec1, i32 %P2
1118 * CHECK: %build_insertelement = insertelement <4 x i32> %Vec1, i32 %P1, i32 %P2
1119 * CHECK: %build_shufflevector = shufflevector <4 x i32> %Vec1, <4 x i32> %Vec2, <4 x i32> <i32 1, i32 1, i32 0, i32 0>
1120 * CHECK: %build_insertvalue0 = insertvalue{{.*}}%bl, i32 1, 0
1121 * CHECK: %build_extractvalue = extractvalue{{.*}}%build_insertvalue1, 1
1123 let ci = build_call fn [| p2; p1 |] "build_call" atentry in
1124 insist (CallConv.c = instruction_call_conv ci);
1125 set_instruction_call_conv 63 ci;
1126 insist (63 = instruction_call_conv ci);
1127 insist (not (is_tail_call ci));
1128 set_tail_call true ci;
1129 insist (is_tail_call ci);
1131 let signext = create_enum_attr context "signext" 0L in
1132 let zeroext = create_enum_attr context "zeroext" 0L in
1133 let noalias = create_enum_attr context "noalias" 0L in
1134 let noreturn = create_enum_attr context "noreturn" 0L in
1135 let no_sse = create_string_attr context "no-sse" "" in
1137 add_call_site_attr ci signext (AttrIndex.Param 0);
1138 add_call_site_attr ci noalias (AttrIndex.Param 1);
1139 insist ((call_site_attrs ci (AttrIndex.Param 1)) = [|noalias|]);
1140 remove_enum_call_site_attr ci (enum_attr_kind "noalias") (AttrIndex.Param 1);
1141 add_call_site_attr ci no_sse (AttrIndex.Param 1);
1142 insist ((call_site_attrs ci (AttrIndex.Param 1)) = [|no_sse|]);
1143 remove_string_call_site_attr ci "no-sse" (AttrIndex.Param 1);
1144 insist ((call_site_attrs ci (AttrIndex.Param 1)) = [||]);
1145 add_call_site_attr ci noreturn AttrIndex.Function;
1146 add_call_site_attr ci zeroext AttrIndex.Return;
1148 let inst46 = build_icmp Icmp.Eq p1 p2 "build_icmp" atentry in
1149 ignore (build_select inst46 p1 p2 "build_select" atentry);
1150 ignore (build_va_arg
1151 (const_null (pointer_type (pointer_type i8_type)))
1152 i32_type "build_va_arg" atentry);
1154 (* Set up some vector vregs. *)
1155 let one = const_int i32_type 1 in
1156 let zero = const_int i32_type 0 in
1157 let t1 = const_vector [| one; zero; one; zero |] in
1158 let t2 = const_vector [| zero; one; zero; one |] in
1159 let t3 = const_vector [| one; one; zero; zero |] in
1160 let vec1 = build_insertelement t1 p1 p2 "Vec1" atentry in
1161 let vec2 = build_insertelement t2 p1 p2 "Vec2" atentry in
1162 let sty = struct_type context [| i32_type; i8_type |] in
1164 ignore (build_extractelement vec1 p2 "build_extractelement" atentry);
1165 ignore (build_insertelement vec1 p1 p2 "build_insertelement" atentry);
1166 ignore (build_shufflevector vec1 vec2 t3 "build_shufflevector" atentry);
1168 let p = build_alloca sty "ba" atentry in
1169 let agg = build_load p "bl" atentry in
1170 let agg0 = build_insertvalue agg (const_int i32_type 1) 0
1171 "build_insertvalue0" atentry in
1172 let agg1 = build_insertvalue agg0 (const_int i8_type 2) 1
1173 "build_insertvalue1" atentry in
1174 ignore (build_extractvalue agg1 1 "build_extractvalue" atentry)
1175 end;
1177 group "metadata"; begin
1178 (* CHECK: %metadata = add i32 %P1, %P2, !test !1
1179 * !1 is metadata emitted at EOF.
1181 let i = build_add p1 p2 "metadata" atentry in
1182 insist ((has_metadata i) = false);
1184 let m1 = const_int i32_type 1 in
1185 let m2 = mdstring context "metadata test" in
1186 let md = mdnode context [| m1; m2 |] in
1188 let kind = mdkind_id context "test" in
1189 set_metadata i kind md;
1191 insist ((has_metadata i) = true);
1192 insist ((metadata i kind) = Some md);
1193 insist ((get_mdnode_operands md) = [| m1; m2 |]);
1195 clear_metadata i kind;
1197 insist ((has_metadata i) = false);
1198 insist ((metadata i kind) = None);
1200 set_metadata i kind md
1201 end;
1203 group "named metadata"; begin
1204 (* !llvm.module.flags is emitted at EOF. *)
1205 let n1 = const_int i32_type 1 in
1206 let n2 = mdstring context "Debug Info Version" in
1207 let n3 = const_int i32_type 3 in
1208 let md = mdnode context [| n1; n2; n3 |] in
1209 add_named_metadata_operand m "llvm.module.flags" md;
1211 insist ((get_named_metadata m "llvm.module.flags") = [| md |])
1212 end;
1214 group "ret"; begin
1215 (* CHECK: ret{{.*}}P1
1217 let ret = build_ret p1 atentry in
1218 position_before ret atentry
1219 end;
1221 (* see test/Feature/exception.ll *)
1222 let bblpad = append_block context "Bblpad" fn in
1223 let rt = struct_type context [| pointer_type i8_type; i32_type |] in
1224 let ft = var_arg_function_type i32_type [||] in
1225 let personality = declare_function "__gxx_personality_v0" ft m in
1226 let ztic = declare_global (pointer_type i8_type) "_ZTIc" m in
1227 let ztid = declare_global (pointer_type i8_type) "_ZTId" m in
1228 let ztipkc = declare_global (pointer_type i8_type) "_ZTIPKc" m in
1229 begin
1230 set_global_constant true ztic;
1231 set_global_constant true ztid;
1232 set_global_constant true ztipkc;
1233 let lp = build_landingpad rt personality 0 "lpad"
1234 (builder_at_end context bblpad) in begin
1235 set_cleanup lp true;
1236 add_clause lp ztic;
1237 insist((pointer_type (pointer_type i8_type)) = type_of ztid);
1238 let ety = pointer_type (pointer_type i8_type) in
1239 add_clause lp (const_array ety [| ztipkc; ztid |]);
1240 ignore (build_resume lp (builder_at_end context bblpad));
1241 end;
1242 (* CHECK: landingpad
1243 * CHECK: cleanup
1244 * CHECK: catch{{.*}}i8**{{.*}}@_ZTIc
1245 * CHECK: filter{{.*}}@_ZTIPKc{{.*}}@_ZTId
1246 * CHECK: resume
1247 * *)
1248 end;
1250 group "br"; begin
1251 (* CHECK: br{{.*}}Bb02
1253 let bb02 = append_block context "Bb02" fn in
1254 let b = builder_at_end context bb02 in
1255 let br = build_br bb02 b in
1256 insist (successors br = [| bb02 |]) ;
1257 insist (is_conditional br = false) ;
1258 insist (get_branch br = Some (`Unconditional bb02)) ;
1259 end;
1261 group "cond_br"; begin
1262 (* CHECK: br{{.*}}build_br{{.*}}Bb03{{.*}}Bb00
1264 let bb03 = append_block context "Bb03" fn in
1265 let b = builder_at_end context bb03 in
1266 let cond = build_trunc p1 i1_type "build_br" b in
1267 let br = build_cond_br cond bb03 bb00 b in
1268 insist (num_successors br = 2) ;
1269 insist (successor br 0 = bb03) ;
1270 insist (successor br 1 = bb00) ;
1271 insist (is_conditional br = true) ;
1272 insist (get_branch br = Some (`Conditional (cond, bb03, bb00))) ;
1273 end;
1275 group "switch"; begin
1276 (* CHECK: switch{{.*}}P1{{.*}}SwiBlock3
1277 * CHECK: 2,{{.*}}SwiBlock2
1279 let bb1 = append_block context "SwiBlock1" fn in
1280 let bb2 = append_block context "SwiBlock2" fn in
1281 ignore (build_unreachable (builder_at_end context bb2));
1282 let bb3 = append_block context "SwiBlock3" fn in
1283 ignore (build_unreachable (builder_at_end context bb3));
1284 let si = build_switch p1 bb3 1 (builder_at_end context bb1) in begin
1285 ignore (add_case si (const_int i32_type 2) bb2);
1286 insist (switch_default_dest si = bb3);
1287 end;
1288 insist (num_successors si = 2) ;
1289 insist (get_branch si = None) ;
1290 end;
1292 group "malloc/free"; begin
1293 (* CHECK: call{{.*}}@malloc(i32 ptrtoint
1294 * CHECK: call{{.*}}@free(i8*
1295 * CHECK: call{{.*}}@malloc(i32 %
1297 let bb1 = append_block context "MallocBlock1" fn in
1298 let m1 = (build_malloc (pointer_type i32_type) "m1"
1299 (builder_at_end context bb1)) in
1300 ignore (build_free m1 (builder_at_end context bb1));
1301 ignore (build_array_malloc i32_type p1 "m2" (builder_at_end context bb1));
1302 ignore (build_unreachable (builder_at_end context bb1));
1303 end;
1305 group "indirectbr"; begin
1306 (* CHECK: indirectbr i8* blockaddress(@X7, %IBRBlock2), [label %IBRBlock2, label %IBRBlock3]
1308 let bb1 = append_block context "IBRBlock1" fn in
1310 let bb2 = append_block context "IBRBlock2" fn in
1311 ignore (build_unreachable (builder_at_end context bb2));
1313 let bb3 = append_block context "IBRBlock3" fn in
1314 ignore (build_unreachable (builder_at_end context bb3));
1316 let addr = block_address fn bb2 in
1317 let ibr = build_indirect_br addr 2 (builder_at_end context bb1) in
1318 ignore (add_destination ibr bb2);
1319 ignore (add_destination ibr bb3)
1320 end;
1322 group "invoke"; begin
1323 (* CHECK: build_invoke{{.*}}invoke{{.*}}P1{{.*}}P2
1324 * CHECK: to{{.*}}Bb04{{.*}}unwind{{.*}}Bblpad
1326 let bb04 = append_block context "Bb04" fn in
1327 let b = builder_at_end context bb04 in
1328 ignore (build_invoke fn [| p1; p2 |] bb04 bblpad "build_invoke" b)
1329 end;
1331 group "unreachable"; begin
1332 (* CHECK: unreachable
1334 let bb06 = append_block context "Bb06" fn in
1335 let b = builder_at_end context bb06 in
1336 ignore (build_unreachable b)
1337 end;
1339 group "arithmetic"; begin
1340 let bb07 = append_block context "Bb07" fn in
1341 let b = builder_at_end context bb07 in
1343 (* CHECK: %build_add = add i32 %P1, %P2
1344 * CHECK: %build_nsw_add = add nsw i32 %P1, %P2
1345 * CHECK: %build_nuw_add = add nuw i32 %P1, %P2
1346 * CHECK: %build_fadd = fadd float %F1, %F2
1347 * CHECK: %build_sub = sub i32 %P1, %P2
1348 * CHECK: %build_nsw_sub = sub nsw i32 %P1, %P2
1349 * CHECK: %build_nuw_sub = sub nuw i32 %P1, %P2
1350 * CHECK: %build_fsub = fsub float %F1, %F2
1351 * CHECK: %build_mul = mul i32 %P1, %P2
1352 * CHECK: %build_nsw_mul = mul nsw i32 %P1, %P2
1353 * CHECK: %build_nuw_mul = mul nuw i32 %P1, %P2
1354 * CHECK: %build_fmul = fmul float %F1, %F2
1355 * CHECK: %build_udiv = udiv i32 %P1, %P2
1356 * CHECK: %build_sdiv = sdiv i32 %P1, %P2
1357 * CHECK: %build_exact_sdiv = sdiv exact i32 %P1, %P2
1358 * CHECK: %build_fdiv = fdiv float %F1, %F2
1359 * CHECK: %build_urem = urem i32 %P1, %P2
1360 * CHECK: %build_srem = srem i32 %P1, %P2
1361 * CHECK: %build_frem = frem float %F1, %F2
1362 * CHECK: %build_shl = shl i32 %P1, %P2
1363 * CHECK: %build_lshl = lshr i32 %P1, %P2
1364 * CHECK: %build_ashl = ashr i32 %P1, %P2
1365 * CHECK: %build_and = and i32 %P1, %P2
1366 * CHECK: %build_or = or i32 %P1, %P2
1367 * CHECK: %build_xor = xor i32 %P1, %P2
1368 * CHECK: %build_neg = sub i32 0, %P1
1369 * CHECK: %build_nsw_neg = sub nsw i32 0, %P1
1370 * CHECK: %build_nuw_neg = sub nuw i32 0, %P1
1371 * CHECK: %build_fneg = fneg float %F1
1372 * CHECK: %build_not = xor i32 %P1, -1
1374 ignore (build_add p1 p2 "build_add" b);
1375 ignore (build_nsw_add p1 p2 "build_nsw_add" b);
1376 ignore (build_nuw_add p1 p2 "build_nuw_add" b);
1377 ignore (build_fadd f1 f2 "build_fadd" b);
1378 ignore (build_sub p1 p2 "build_sub" b);
1379 ignore (build_nsw_sub p1 p2 "build_nsw_sub" b);
1380 ignore (build_nuw_sub p1 p2 "build_nuw_sub" b);
1381 ignore (build_fsub f1 f2 "build_fsub" b);
1382 ignore (build_mul p1 p2 "build_mul" b);
1383 ignore (build_nsw_mul p1 p2 "build_nsw_mul" b);
1384 ignore (build_nuw_mul p1 p2 "build_nuw_mul" b);
1385 ignore (build_fmul f1 f2 "build_fmul" b);
1386 ignore (build_udiv p1 p2 "build_udiv" b);
1387 ignore (build_sdiv p1 p2 "build_sdiv" b);
1388 ignore (build_exact_sdiv p1 p2 "build_exact_sdiv" b);
1389 ignore (build_fdiv f1 f2 "build_fdiv" b);
1390 ignore (build_urem p1 p2 "build_urem" b);
1391 ignore (build_srem p1 p2 "build_srem" b);
1392 ignore (build_frem f1 f2 "build_frem" b);
1393 ignore (build_shl p1 p2 "build_shl" b);
1394 ignore (build_lshr p1 p2 "build_lshl" b);
1395 ignore (build_ashr p1 p2 "build_ashl" b);
1396 ignore (build_and p1 p2 "build_and" b);
1397 ignore (build_or p1 p2 "build_or" b);
1398 ignore (build_xor p1 p2 "build_xor" b);
1399 ignore (build_neg p1 "build_neg" b);
1400 ignore (build_nsw_neg p1 "build_nsw_neg" b);
1401 ignore (build_nuw_neg p1 "build_nuw_neg" b);
1402 ignore (build_fneg f1 "build_fneg" b);
1403 ignore (build_not p1 "build_not" b);
1404 ignore (build_unreachable b)
1405 end;
1407 group "memory"; begin
1408 let bb08 = append_block context "Bb08" fn in
1409 let b = builder_at_end context bb08 in
1411 (* CHECK: %build_alloca = alloca i32
1412 * CHECK: %build_array_alloca = alloca i32, i32 %P2
1413 * CHECK: %build_load = load volatile i32, i32* %build_array_alloca, align 4
1414 * CHECK: store volatile i32 %P2, i32* %build_alloca, align 4
1415 * CHECK: %build_gep = getelementptr i32, i32* %build_array_alloca, i32 %P2
1416 * CHECK: %build_in_bounds_gep = getelementptr inbounds i32, i32* %build_array_alloca, i32 %P2
1417 * CHECK: %build_struct_gep = getelementptr inbounds{{.*}}%build_alloca2, i32 0, i32 1
1418 * CHECK: %build_atomicrmw = atomicrmw xchg i8* %p, i8 42 seq_cst
1420 let alloca = build_alloca i32_type "build_alloca" b in
1421 let array_alloca = build_array_alloca i32_type p2 "build_array_alloca" b in
1423 let load = build_load array_alloca "build_load" b in
1424 ignore(set_alignment 4 load);
1425 ignore(set_volatile true load);
1426 insist(true = is_volatile load);
1427 insist(4 = alignment load);
1429 let store = build_store p2 alloca b in
1430 ignore(set_volatile true store);
1431 ignore(set_alignment 4 store);
1432 insist(true = is_volatile store);
1433 insist(4 = alignment store);
1434 ignore(build_gep array_alloca [| p2 |] "build_gep" b);
1435 ignore(build_in_bounds_gep array_alloca [| p2 |] "build_in_bounds_gep" b);
1437 let sty = struct_type context [| i32_type; i8_type |] in
1438 let alloca2 = build_alloca sty "build_alloca2" b in
1439 ignore(build_struct_gep alloca2 1 "build_struct_gep" b);
1441 let p = build_alloca i8_type "p" b in
1442 ignore(build_atomicrmw AtomicRMWBinOp.Xchg p (const_int i8_type 42)
1443 AtomicOrdering.SequentiallyConsistent false "build_atomicrmw"
1446 ignore(build_unreachable b)
1447 end;
1449 group "string"; begin
1450 let bb09 = append_block context "Bb09" fn in
1451 let b = builder_at_end context bb09 in
1452 let p = build_alloca (pointer_type i8_type) "p" b in
1453 (* build_global_string is emitted above.
1454 * CHECK: store{{.*}}build_global_string1{{.*}}p
1455 * *)
1456 ignore (build_global_string "stringval" "build_global_string" b);
1457 let g = build_global_stringptr "stringval" "build_global_string1" b in
1458 ignore (build_store g p b);
1459 ignore(build_unreachable b);
1460 end;
1462 group "phi"; begin
1463 (* CHECK: PhiNode{{.*}}P1{{.*}}PhiBlock1{{.*}}P2{{.*}}PhiBlock2
1465 let b1 = append_block context "PhiBlock1" fn in
1466 let b2 = append_block context "PhiBlock2" fn in
1468 let jb = append_block context "PhiJoinBlock" fn in
1469 ignore (build_br jb (builder_at_end context b1));
1470 ignore (build_br jb (builder_at_end context b2));
1471 let at_jb = builder_at_end context jb in
1473 let phi = build_phi [(p1, b1)] "PhiNode" at_jb in
1474 insist ([(p1, b1)] = incoming phi);
1476 add_incoming (p2, b2) phi;
1477 insist ([(p1, b1); (p2, b2)] = incoming phi);
1479 (* CHECK: %PhiEmptyNode = phi i8
1481 let phi_empty = build_empty_phi i8_type "PhiEmptyNode" at_jb in
1482 insist ([] = incoming phi_empty);
1484 (* can't emit an empty phi to bitcode *)
1485 add_incoming (const_int i8_type 1, b1) phi_empty;
1486 add_incoming (const_int i8_type 2, b2) phi_empty;
1488 ignore (build_unreachable at_jb);
1491 (* End-of-file checks for things like metdata and attributes.
1492 * CHECK: !llvm.module.flags = !{!0}
1493 * CHECK: !0 = !{i32 1, !"Debug Info Version", i32 3}
1494 * CHECK: !1 = !{i32 1, !"metadata test"}
1497 (*===-- Pass Managers -----------------------------------------------------===*)
1499 let test_pass_manager () =
1500 let (++) x f = ignore (f x); x in
1502 begin group "module pass manager";
1503 ignore (PassManager.create ()
1504 ++ PassManager.run_module m
1505 ++ PassManager.dispose)
1506 end;
1508 begin group "function pass manager";
1509 let fty = function_type void_type [| |] in
1510 let fn = define_function "FunctionPassManager" fty m in
1511 ignore (build_ret_void (builder_at_end context (entry_block fn)));
1513 ignore (PassManager.create_function m
1514 ++ PassManager.initialize
1515 ++ PassManager.run_function fn
1516 ++ PassManager.finalize
1517 ++ PassManager.dispose)
1521 (*===-- Memory Buffer -----------------------------------------------------===*)
1523 let test_memory_buffer () =
1524 group "memory buffer";
1525 let buf = MemoryBuffer.of_string "foobar" in
1526 insist ((MemoryBuffer.as_string buf) = "foobar")
1529 (*===-- Writer ------------------------------------------------------------===*)
1531 let test_writer () =
1532 group "valid";
1533 insist (match Llvm_analysis.verify_module m with
1534 | None -> true
1535 | Some msg -> prerr_string msg; false);
1537 group "writer";
1538 insist (write_bitcode_file m filename);
1540 dispose_module m
1543 (*===-- Driver ------------------------------------------------------------===*)
1545 let _ =
1546 suite "contained types" test_contained_types;
1547 suite "conversion" test_conversion;
1548 suite "target" test_target;
1549 suite "constants" test_constants;
1550 suite "attributes" test_attributes;
1551 suite "global values" test_global_values;
1552 suite "global variables" test_global_variables;
1553 suite "uses" test_uses;
1554 suite "users" test_users;
1555 suite "aliases" test_aliases;
1556 suite "functions" test_functions;
1557 suite "params" test_params;
1558 suite "basic blocks" test_basic_blocks;
1559 suite "instructions" test_instructions;
1560 suite "builder" test_builder;
1561 suite "pass manager" test_pass_manager;
1562 suite "memory buffer" test_memory_buffer;
1563 suite "writer" test_writer; (* Keep this last; it disposes m. *)
1564 exit !exit_status