[Frontend] Remove unused includes (NFC) (#116927)
[llvm-project.git] / llvm / test / Bindings / OCaml / core.ml
blob923a3541e239c39cbad3d86dbff9109d870665b1
1 (* RUN: rm -rf %t && mkdir -p %t && cp %s %t/core.ml && cp %S/Utils/Testsuite.ml %t/Testsuite.ml
2 * RUN: %ocamlc -g -w +A -package llvm.analysis -package llvm.bitwriter -I %t/ -linkpkg %t/Testsuite.ml %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 -I %t/ -linkpkg %t/Testsuite.ml %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
20 open Testsuite
21 let context = global_context ()
22 let i1_type = Llvm.i1_type context
23 let i8_type = Llvm.i8_type context
24 let i16_type = Llvm.i16_type context
25 let i32_type = Llvm.i32_type context
26 let i64_type = Llvm.i64_type context
27 let void_type = Llvm.void_type context
28 let float_type = Llvm.float_type context
29 let double_type = Llvm.double_type context
30 let fp128_type = Llvm.fp128_type context
32 (*===-- Fixture -----------------------------------------------------------===*)
34 let filename = Sys.argv.(1)
35 let m = create_module context filename
37 (*===-- Modules ----------------------------------------------------------===*)
39 let test_modules () =
40 insist (module_context m = context)
42 (*===-- Contained types --------------------------------------------------===*)
44 let test_contained_types () =
45 let ar = struct_type context [| i32_type; i8_type |] in
46 insist (i32_type = (Array.get (subtypes ar)) 0);
47 insist (i8_type = (Array.get (subtypes ar)) 1);
48 insist ([| i32_type; i8_type |] = struct_element_types ar)
50 (*===-- Pointer types ----------------------------------------------------===*)
51 let test_pointer_types () =
52 insist (0 = address_space (pointer_type context));
53 insist (0 = address_space (qualified_pointer_type context 0));
54 insist (1 = address_space (qualified_pointer_type context 1))
56 (*===-- Conversion --------------------------------------------------------===*)
58 let test_conversion () =
59 insist ("i32" = (string_of_lltype i32_type));
60 let c = const_int i32_type 42 in
61 insist ("i32 42" = (string_of_llvalue c))
64 (*===-- Target ------------------------------------------------------------===*)
66 let test_target () =
67 begin group "triple";
68 let trip = "i686-apple-darwin8" in
69 set_target_triple trip m;
70 insist (trip = target_triple m)
71 end;
73 begin group "layout";
74 let layout = "e-m:o-p:32:32-p270:32:32-p271:32:32-p272:64:64-i128:128-f64:32:64-f80:128-n8:16:32-S128" in
75 set_data_layout layout m;
76 insist (layout = data_layout m)
77 end
78 (* CHECK: target datalayout = "e-m:o-p:32:32-p270:32:32-p271:32:32-p272:64:64-i128:128-f64:32:64-f80:128-n8:16:32-S128"
79 * CHECK: target triple = "i686-apple-darwin8"
83 (*===-- Constants ---------------------------------------------------------===*)
85 let test_constants () =
86 (* CHECK: const_int{{.*}}i32{{.*}}-1
88 group "int";
89 let c = const_int i32_type (-1) in
90 ignore (define_global "const_int" c m);
91 insist (i32_type = type_of c);
92 insist (is_constant c);
93 insist (Some (-1L) = int64_of_const c);
95 (* CHECK: const_sext_int{{.*}}i64{{.*}}-1
97 group "sext int";
98 let c = const_int i64_type (-1) in
99 ignore (define_global "const_sext_int" c m);
100 insist (i64_type = type_of c);
101 insist (Some (-1L) = int64_of_const c);
103 (* CHECK: const_zext_int64{{.*}}i64{{.*}}4294967295
105 group "zext int64";
106 let c = const_of_int64 i64_type (Int64.of_string "4294967295") false in
107 ignore (define_global "const_zext_int64" c m);
108 insist (i64_type = type_of c);
109 insist (Some 4294967295L = int64_of_const c);
111 (* CHECK: const_int_string{{.*}}i32{{.*}}-1
113 group "int string";
114 let c = const_int_of_string i32_type "-1" 10 in
115 ignore (define_global "const_int_string" c m);
116 insist (i32_type = type_of c);
117 insist (None = (string_of_const c));
118 insist (None = float_of_const c);
119 insist (Some (-1L) = int64_of_const c);
121 (* CHECK: const_int64{{.*}}i64{{.*}}9223372036854775807
123 group "max int64";
124 let c = const_of_int64 i64_type 9223372036854775807L true in
125 ignore (define_global "const_int64" c m) ;
126 insist (i64_type = type_of c);
127 insist (Some 9223372036854775807L = int64_of_const c);
129 if Sys.word_size = 64; then begin
130 group "long int";
131 let c = const_int i64_type (1 lsl 61) in
132 insist (c = const_of_int64 i64_type (Int64.of_int (1 lsl 61)) false)
133 end;
135 (* CHECK: @const_string = global {{.*}}c"cruel\00world"
137 group "string";
138 let c = const_string context "cruel\000world" in
139 ignore (define_global "const_string" c m);
140 insist ((array_type i8_type 11) = type_of c);
141 insist ((Some "cruel\000world") = (string_of_const c));
143 (* CHECK: const_stringz{{.*}}"hi\00again\00"
145 group "stringz";
146 let c = const_stringz context "hi\000again" in
147 ignore (define_global "const_stringz" c m);
148 insist ((array_type i8_type 9) = type_of c);
150 (* CHECK: const_single{{.*}}2.75
151 * CHECK: const_double{{.*}}3.1459
152 * CHECK: const_double_string{{.*}}2
153 * CHECK: const_fake_fp128{{.*}}0xL00000000000000004000000000000000
154 * CHECK: const_fp128_string{{.*}}0xLF3CB1CCF26FBC178452FB4EC7F91973F
156 begin group "real";
157 let cs = const_float float_type 2.75 in
158 ignore (define_global "const_single" cs m);
159 insist (float_type = type_of cs);
160 insist (float_of_const cs = Some 2.75);
162 let cd = const_float double_type 3.1459 in
163 ignore (define_global "const_double" cd m);
164 insist (double_type = type_of cd);
165 insist (float_of_const cd = Some 3.1459);
167 let cd = const_float_of_string double_type "2" in
168 ignore (define_global "const_double_string" cd m);
169 insist (double_type = type_of cd);
170 insist (float_of_const cd = Some 2.);
172 let cd = const_float fp128_type 2. in
173 ignore (define_global "const_fake_fp128" cd m);
174 insist (fp128_type = type_of cd);
175 insist (float_of_const cd = Some 2.);
177 let cd = const_float_of_string fp128_type "1e400" in
178 ignore (define_global "const_fp128_string" cd m);
179 insist (fp128_type = type_of cd);
180 insist (float_of_const cd = None);
181 end;
183 let one = const_int i16_type 1 in
184 let two = const_int i16_type 2 in
185 let three = const_int i32_type 3 in
186 let four = const_int i32_type 4 in
188 (* CHECK: const_array{{.*}}[i32 3, i32 4]
190 group "array";
191 let c = const_array i32_type [| three; four |] in
192 ignore (define_global "const_array" c m);
193 insist ((array_type i32_type 2) = (type_of c));
194 insist (element_type (type_of c) = i32_type);
195 insist (Some three = (aggregate_element c 0));
196 insist (Some four = (aggregate_element c 1));
197 insist (None = (aggregate_element c 2));
199 (* CHECK: const_vector{{.*}}<i16 1, i16 2{{.*}}>
201 group "vector";
202 let c = const_vector [| one; two; one; two;
203 one; two; one; two |] in
204 ignore (define_global "const_vector" c m);
205 insist ((vector_type i16_type 8) = (type_of c));
206 insist (element_type (type_of c) = i16_type);
208 (* CHECK: const_structure{{.*.}}i16 1, i16 2, i32 3, i32 4
210 group "structure";
211 let c = const_struct context [| one; two; three; four |] in
212 ignore (define_global "const_structure" c m);
213 insist ((struct_type context [| i16_type; i16_type; i32_type; i32_type |])
214 = (type_of c));
216 (* CHECK: const_null{{.*}}zeroinit
218 group "null";
219 let c = const_null (packed_struct_type context [| i1_type; i8_type; i64_type;
220 double_type |]) in
221 ignore (define_global "const_null" c m);
223 (* CHECK: const_all_ones{{.*}}-1
225 group "all ones";
226 let c = const_all_ones i64_type in
227 ignore (define_global "const_all_ones" c m);
229 group "pointer null"; begin
230 (* CHECK: const_pointer_null = global ptr null
232 let c = const_pointer_null (pointer_type context) in
233 ignore (define_global "const_pointer_null" c m);
234 end;
236 (* CHECK: const_undef{{.*}}undef
238 group "undef";
239 let c = undef i1_type in
240 ignore (define_global "const_undef" c m);
241 insist (i1_type = type_of c);
242 insist (is_undef c);
244 (* CHECK: const_poison{{.*}}poison
246 group "poison";
247 let c = poison i1_type in
248 ignore (define_global "const_poison" c m);
249 insist (i1_type = type_of c);
250 insist (is_poison c);
252 group "constant arithmetic";
253 (* CHECK: @const_neg = global i64 sub
254 * CHECK: @const_nsw_neg = global i64 sub nsw
255 * CHECK: @const_nuw_neg = global i64 sub
256 * CHECK: @const_not = global i64 xor
257 * CHECK: @const_add = global i64 add
258 * CHECK: @const_nsw_add = global i64 add nsw
259 * CHECK: @const_nuw_add = global i64 add nuw
260 * CHECK: @const_sub = global i64 sub
261 * CHECK: @const_nsw_sub = global i64 sub nsw
262 * CHECK: @const_nuw_sub = global i64 sub nuw
263 * CHECK: @const_mul = global i64 mul
264 * CHECK: @const_nsw_mul = global i64 mul nsw
265 * CHECK: @const_nuw_mul = global i64 mul nuw
266 * CHECK: @const_xor = global i64 xor
268 let void_ptr = pointer_type context in
269 let five = const_int i64_type 5 in
270 let foldbomb_gv = define_global "FoldBomb" (const_null i8_type) m in
271 let foldbomb = const_ptrtoint foldbomb_gv i64_type in
272 ignore (define_global "const_neg" (const_neg foldbomb) m);
273 ignore (define_global "const_nsw_neg" (const_nsw_neg foldbomb) m);
274 ignore (define_global "const_nuw_neg" (const_nuw_neg foldbomb) m);
275 ignore (define_global "const_not" (const_not foldbomb) m);
276 ignore (define_global "const_add" (const_add foldbomb five) m);
277 ignore (define_global "const_nsw_add" (const_nsw_add foldbomb five) m);
278 ignore (define_global "const_nuw_add" (const_nuw_add foldbomb five) m);
279 ignore (define_global "const_sub" (const_sub foldbomb five) m);
280 ignore (define_global "const_nsw_sub" (const_nsw_sub foldbomb five) m);
281 ignore (define_global "const_nuw_sub" (const_nuw_sub foldbomb five) m);
282 ignore (define_global "const_mul" (const_mul foldbomb five) m);
283 ignore (define_global "const_nsw_mul" (const_nsw_mul foldbomb five) m);
284 ignore (define_global "const_nuw_mul" (const_nuw_mul foldbomb five) m);
285 ignore (define_global "const_xor" (const_xor foldbomb five) m);
287 group "constant casts";
288 (* CHECK: const_trunc{{.*}}trunc
289 * CHECK: const_ptrtoint{{.*}}ptrtoint
290 * CHECK: const_inttoptr{{.*}}inttoptr
291 * CHECK: const_bitcast{{.*}}bitcast
293 let i128_type = integer_type context 128 in
294 ignore (define_global "const_trunc" (const_trunc (const_add foldbomb five)
295 i8_type) m);
296 ignore (define_global "const_ptrtoint" (const_ptrtoint
297 (const_gep i8_type (const_null (pointer_type context))
298 [| const_int i32_type 1 |])
299 i32_type) m);
300 ignore (define_global "const_inttoptr" (const_inttoptr (const_add foldbomb five)
301 void_ptr) m);
302 ignore (define_global "const_bitcast" (const_bitcast foldbomb double_type) m);
304 group "misc constants";
305 (* CHECK: const_size_of{{.*}}getelementptr{{.*}}null
306 * CHECK: const_gep{{.*}}getelementptr
307 * CHECK: const_extractelement{{.*}}extractelement
308 * CHECK: const_insertelement{{.*}}insertelement
309 * CHECK: const_shufflevector = global <4 x i32> <i32 0, i32 1, i32 1, i32 0>
311 ignore (define_global "const_size_of" (size_of (pointer_type context)) m);
312 ignore (define_global "const_gep" (const_gep i8_type foldbomb_gv [| five |])
314 let zero = const_int i32_type 0 in
315 let one = const_int i32_type 1 in
316 ignore (define_global "const_extractelement" (const_extractelement
317 (const_vector [| zero; one; zero; one |])
318 (const_trunc foldbomb i32_type)) m);
319 ignore (define_global "const_insertelement" (const_insertelement
320 (const_vector [| zero; one; zero; one |])
321 zero (const_trunc foldbomb i32_type)) m);
322 ignore (define_global "const_shufflevector" (const_shufflevector
323 (const_vector [| zero; one |])
324 (const_vector [| one; zero |])
325 (const_vector [| const_int i32_type 0; const_int i32_type 1;
326 const_int i32_type 2; const_int i32_type 3 |])) m);
328 group "asm"; begin
329 let ft = function_type void_type [| i32_type; i32_type; i32_type |] in
330 ignore (const_inline_asm
333 "{cx},{ax},{di},~{dirflag},~{fpsr},~{flags},~{edi},~{ecx}"
334 true
335 false)
336 end;
338 group "recursive struct"; begin
339 let nsty = named_struct_type context "rec" in
340 let pty = pointer_type context in
341 struct_set_body nsty [| i32_type; pty |] false;
342 let elts = [| const_int i32_type 4; const_pointer_null pty |] in
343 let grec_init = const_named_struct nsty elts in
344 ignore (define_global "grec" grec_init m);
345 ignore (string_of_lltype nsty);
349 (*===-- Attributes --------------------------------------------------------===*)
351 let test_attributes () =
352 group "enum attrs";
353 let nonnull_kind = enum_attr_kind "nonnull" in
354 let dereferenceable_kind = enum_attr_kind "dereferenceable" in
355 insist (nonnull_kind = (enum_attr_kind "nonnull"));
356 insist (nonnull_kind <> dereferenceable_kind);
358 let nonnull =
359 create_enum_attr context "nonnull" 0L in
360 let dereferenceable_4 =
361 create_enum_attr context "dereferenceable" 4L in
362 let dereferenceable_8 =
363 create_enum_attr context "dereferenceable" 8L in
364 insist (nonnull <> dereferenceable_4);
365 insist (dereferenceable_4 <> dereferenceable_8);
366 insist (nonnull = (create_enum_attr context "nonnull" 0L));
367 insist ((repr_of_attr nonnull) =
368 AttrRepr.Enum(nonnull_kind, 0L));
369 insist ((repr_of_attr dereferenceable_4) =
370 AttrRepr.Enum(dereferenceable_kind, 4L));
371 insist ((attr_of_repr context (repr_of_attr nonnull)) =
372 nonnull);
373 insist ((attr_of_repr context (repr_of_attr dereferenceable_4)) =
374 dereferenceable_4);
376 group "string attrs";
377 let foo_bar = create_string_attr context "foo" "bar" in
378 let foo_baz = create_string_attr context "foo" "baz" in
379 insist (foo_bar <> foo_baz);
380 insist (foo_bar = (create_string_attr context "foo" "bar"));
381 insist ((repr_of_attr foo_bar) = AttrRepr.String("foo", "bar"));
382 insist ((attr_of_repr context (repr_of_attr foo_bar)) = foo_bar);
385 (*===-- Global Values -----------------------------------------------------===*)
387 let test_global_values () =
388 let (++) x f = f x; x in
389 let zero32 = const_null i32_type in
391 (* CHECK: GVal01
393 group "naming";
394 let g = define_global "TEMPORARY" zero32 m in
395 insist ("TEMPORARY" = value_name g);
396 set_value_name "GVal01" g;
397 insist ("GVal01" = value_name g);
399 (* CHECK: GVal02{{.*}}linkonce
401 group "linkage";
402 let g = define_global "GVal02" zero32 m ++
403 set_linkage Linkage.Link_once in
404 insist (Linkage.Link_once = linkage g);
406 (* CHECK: GVal03{{.*}}Hanalei
408 group "section";
409 let g = define_global "GVal03" zero32 m ++
410 set_section "Hanalei" in
411 insist ("Hanalei" = section g);
413 (* CHECK: GVal04{{.*}}hidden
415 group "visibility";
416 let g = define_global "GVal04" zero32 m ++
417 set_visibility Visibility.Hidden in
418 insist (Visibility.Hidden = visibility g);
420 (* CHECK: GVal05{{.*}}align 128
422 group "alignment";
423 let g = define_global "GVal05" zero32 m ++
424 set_alignment 128 in
425 insist (128 = alignment g);
427 (* CHECK: GVal06{{.*}}dllexport
429 group "dll_storage_class";
430 let g = define_global "GVal06" zero32 m ++
431 set_dll_storage_class DLLStorageClass.DLLExport in
432 insist (DLLStorageClass.DLLExport = dll_storage_class g)
435 (*===-- Global Variables --------------------------------------------------===*)
437 let test_global_variables () =
438 let (++) x f = f x; x in
439 let forty_two32 = const_int i32_type 42 in
441 group "declarations"; begin
442 (* CHECK: @GVar01 = external global i32
443 * CHECK: @QGVar01 = external addrspace(3) global i32
445 insist (None == lookup_global "GVar01" m);
446 let g = declare_global i32_type "GVar01" m in
447 insist (is_declaration g);
448 insist (pointer_type context ==
449 type_of (declare_global float_type "GVar01" m));
450 insist (g == declare_global i32_type "GVar01" m);
451 insist (match lookup_global "GVar01" m with Some x -> x = g
452 | None -> false);
454 insist (None == lookup_global "QGVar01" m);
455 let g = declare_qualified_global i32_type "QGVar01" 3 m in
456 insist (is_declaration g);
457 insist (qualified_pointer_type context 3 ==
458 type_of (declare_qualified_global float_type "QGVar01" 3 m));
459 insist (g == declare_qualified_global i32_type "QGVar01" 3 m);
460 insist (match lookup_global "QGVar01" m with Some x -> x = g
461 | None -> false);
462 end;
464 group "definitions"; begin
465 (* CHECK: @GVar02 = global i32 42
466 * CHECK: @GVar03 = global i32 42
467 * CHECK: @QGVar02 = addrspace(3) global i32 42
468 * CHECK: @QGVar03 = addrspace(3) global i32 42
470 let g = define_global "GVar02" forty_two32 m in
471 let g2 = declare_global i32_type "GVar03" m ++
472 set_initializer forty_two32 in
473 insist (not (is_declaration g));
474 insist (not (is_declaration g2));
475 insist ((global_initializer g) = (global_initializer g2));
477 let g = define_qualified_global "QGVar02" forty_two32 3 m in
478 let g2 = declare_qualified_global i32_type "QGVar03" 3 m ++
479 set_initializer forty_two32 in
480 insist (not (is_declaration g));
481 insist (not (is_declaration g2));
482 insist ((global_initializer g) = (global_initializer g2));
483 end;
485 (* CHECK: GVar04{{.*}}thread_local
487 group "threadlocal";
488 let g = define_global "GVar04" forty_two32 m ++
489 set_thread_local true in
490 insist (is_thread_local g);
492 (* CHECK: GVar05{{.*}}thread_local(initialexec)
494 group "threadlocal_mode";
495 let g = define_global "GVar05" forty_two32 m ++
496 set_thread_local_mode ThreadLocalMode.InitialExec in
497 insist ((thread_local_mode g) = ThreadLocalMode.InitialExec);
499 (* CHECK: GVar06{{.*}}externally_initialized
501 group "externally_initialized";
502 let g = define_global "GVar06" forty_two32 m ++
503 set_externally_initialized true in
504 insist (is_externally_initialized g);
506 (* CHECK-NOWHERE-NOT: GVar07
508 group "delete";
509 let g = define_global "GVar07" forty_two32 m in
510 delete_global g;
512 (* CHECK: ConstGlobalVar{{.*}}constant
514 group "constant";
515 let g = define_global "ConstGlobalVar" forty_two32 m in
516 insist (not (is_global_constant g));
517 set_global_constant true g;
518 insist (is_global_constant g);
520 begin group "iteration";
521 let m = create_module context "temp" in
523 insist (get_module_identifier m = "temp");
524 set_module_identifer m "temp2";
525 insist (get_module_identifier m = "temp2");
527 insist (At_end m = global_begin m);
528 insist (At_start m = global_end m);
530 let g1 = declare_global i32_type "One" m in
531 let g2 = declare_global i32_type "Two" m in
533 insist (Before g1 = global_begin m);
534 insist (Before g2 = global_succ g1);
535 insist (At_end m = global_succ g2);
537 insist (After g2 = global_end m);
538 insist (After g1 = global_pred g2);
539 insist (At_start m = global_pred g1);
541 let lf s x = s ^ "->" ^ value_name x in
542 insist ("->One->Two" = fold_left_globals lf "" m);
544 let rf x s = value_name x ^ "<-" ^ s in
545 insist ("One<-Two<-" = fold_right_globals rf m "");
547 dispose_module m
550 (* String globals built below are emitted here.
551 * CHECK: build_global_string{{.*}}stringval
555 (*===-- Uses --------------------------------------------------------------===*)
557 let test_uses () =
558 let ty = function_type i32_type [| i32_type; i32_type |] in
559 let fn = define_function "use_function" ty m in
560 let b = builder_at_end context (entry_block fn) in
562 let p1 = param fn 0 in
563 let p2 = param fn 1 in
564 let v1 = build_add p1 p2 "v1" b in
565 let v2 = build_add p1 v1 "v2" b in
566 let _ = build_add v1 v2 "v3" b in
568 let lf s u = value_name (user u) ^ "->" ^ s in
569 insist ("v2->v3->" = fold_left_uses lf "" v1);
570 let rf u s = value_name (user u) ^ "<-" ^ s in
571 insist ("v3<-v2<-" = fold_right_uses rf v1 "");
573 let lf s u = value_name (used_value u) ^ "->" ^ s in
574 insist ("v1->v1->" = fold_left_uses lf "" v1);
576 let rf u s = value_name (used_value u) ^ "<-" ^ s in
577 insist ("v1<-v1<-" = fold_right_uses rf v1 "");
579 ignore (build_unreachable b)
582 (*===-- Users -------------------------------------------------------------===*)
584 let test_users () =
585 let ty = function_type i32_type [| i32_type; i32_type |] in
586 let fn = define_function "user_function" ty m in
587 let b = builder_at_end context (entry_block fn) in
589 let p1 = param fn 0 in
590 let p2 = param fn 1 in
591 let a3 = build_alloca i32_type "user_alloca" b in
592 let p3 = build_load i32_type a3 "user_load" b in
593 let i = build_add p1 p2 "sum" b in
595 insist ((num_operands i) = 2);
596 insist ((operand i 0) = p1);
597 insist ((operand i 1) = p2);
599 set_operand i 1 p3;
600 insist ((operand i 1) != p2);
601 insist ((operand i 1) = p3);
603 ignore (build_unreachable b)
606 (*===-- Aliases -----------------------------------------------------------===*)
608 let test_aliases () =
609 (* CHECK: @alias = alias i32, ptr @aliasee
611 let forty_two32 = const_int i32_type 42 in
612 let v = define_global "aliasee" forty_two32 m in
613 ignore (add_alias m i32_type 0 v "alias")
616 (*===-- Functions ---------------------------------------------------------===*)
618 let test_functions () =
619 let ty = function_type i32_type [| i32_type; i64_type |] in
620 let ty2 = function_type i8_type [| i8_type; i64_type |] in
622 (* CHECK: declare i32 @Fn1(i32, i64)
624 begin group "declare";
625 insist (None = lookup_function "Fn1" m);
626 let fn = declare_function "Fn1" ty m in
627 insist (pointer_type context = type_of fn);
628 insist (is_declaration fn);
629 insist (0 = Array.length (basic_blocks fn));
630 insist (pointer_type context == type_of (declare_function "Fn1" ty2 m));
631 insist (fn == declare_function "Fn1" ty m);
632 insist (None <> lookup_function "Fn1" m);
633 insist (match lookup_function "Fn1" m with Some x -> x = fn
634 | None -> false);
635 insist (m == global_parent fn)
636 end;
638 (* CHECK-NOWHERE-NOT: Fn2
640 group "delete";
641 let fn = declare_function "Fn2" ty m in
642 delete_function fn;
644 (* CHECK: define{{.*}}Fn3
646 group "define";
647 let fn = define_function "Fn3" ty m in
648 insist (not (is_declaration fn));
649 insist (1 = Array.length (basic_blocks fn));
650 ignore (build_unreachable (builder_at_end context (entry_block fn)));
652 (* CHECK: define{{.*}}Fn4{{.*}}Param1{{.*}}Param2
654 group "params";
655 let fn = define_function "Fn4" ty m in
656 let params = params fn in
657 insist (2 = Array.length params);
658 insist (params.(0) = param fn 0);
659 insist (params.(1) = param fn 1);
660 insist (i32_type = type_of params.(0));
661 insist (i64_type = type_of params.(1));
662 set_value_name "Param1" params.(0);
663 set_value_name "Param2" params.(1);
664 ignore (build_unreachable (builder_at_end context (entry_block fn)));
666 (* CHECK: fastcc{{.*}}Fn5
668 group "callconv";
669 let fn = define_function "Fn5" ty m in
670 insist (CallConv.c = function_call_conv fn);
671 set_function_call_conv CallConv.fast fn;
672 insist (CallConv.fast = function_call_conv fn);
673 ignore (build_unreachable (builder_at_end context (entry_block fn)));
675 begin group "gc";
676 (* CHECK: Fn6{{.*}}gc{{.*}}shadowstack
678 let fn = define_function "Fn6" ty m in
679 insist (None = gc fn);
680 set_gc (Some "ocaml") fn;
681 insist (Some "ocaml" = gc fn);
682 set_gc None fn;
683 insist (None = gc fn);
684 set_gc (Some "shadowstack") fn;
685 ignore (build_unreachable (builder_at_end context (entry_block fn)));
686 end;
688 begin group "iteration";
689 let m = create_module context "temp" in
691 insist (At_end m = function_begin m);
692 insist (At_start m = function_end m);
694 let f1 = define_function "One" ty m in
695 let f2 = define_function "Two" ty m in
697 insist (Before f1 = function_begin m);
698 insist (Before f2 = function_succ f1);
699 insist (At_end m = function_succ f2);
701 insist (After f2 = function_end m);
702 insist (After f1 = function_pred f2);
703 insist (At_start m = function_pred f1);
705 let lf s x = s ^ "->" ^ value_name x in
706 insist ("->One->Two" = fold_left_functions lf "" m);
708 let rf x s = value_name x ^ "<-" ^ s in
709 insist ("One<-Two<-" = fold_right_functions rf m "");
711 dispose_module m
715 (*===-- Params ------------------------------------------------------------===*)
717 let test_params () =
718 begin group "iteration";
719 let m = create_module context "temp" in
721 let vf = define_function "void" (function_type void_type [| |]) m in
723 insist (At_end vf = param_begin vf);
724 insist (At_start vf = param_end vf);
726 let ty = function_type void_type [| i32_type; i32_type |] in
727 let f = define_function "f" ty m in
728 let p1 = param f 0 in
729 let p2 = param f 1 in
730 set_value_name "One" p1;
731 set_value_name "Two" p2;
733 insist (Before p1 = param_begin f);
734 insist (Before p2 = param_succ p1);
735 insist (At_end f = param_succ p2);
737 insist (After p2 = param_end f);
738 insist (After p1 = param_pred p2);
739 insist (At_start f = param_pred p1);
741 let lf s x = s ^ "->" ^ value_name x in
742 insist ("->One->Two" = fold_left_params lf "" f);
744 let rf x s = value_name x ^ "<-" ^ s in
745 insist ("One<-Two<-" = fold_right_params rf f "");
747 dispose_module m
751 (*===-- Basic Blocks ------------------------------------------------------===*)
753 let test_basic_blocks () =
754 let ty = function_type void_type [| |] in
756 (* CHECK: Bb1
758 group "entry";
759 let fn = declare_function "X" ty m in
760 let bb = append_block context "Bb1" fn in
761 insist (bb = entry_block fn);
762 ignore (build_unreachable (builder_at_end context bb));
764 (* CHECK-NOWHERE-NOT: Bb2
766 group "delete";
767 let fn = declare_function "X2" ty m in
768 let bb = append_block context "Bb2" fn in
769 delete_block bb;
771 group "insert";
772 let fn = declare_function "X3" ty m in
773 let bbb = append_block context "b" fn in
774 let bba = insert_block context "a" bbb in
775 insist ([| bba; bbb |] = basic_blocks fn);
776 ignore (build_unreachable (builder_at_end context bba));
777 ignore (build_unreachable (builder_at_end context bbb));
779 (* CHECK: Bb3
781 group "name/value";
782 let fn = define_function "X4" ty m in
783 let bb = entry_block fn in
784 ignore (build_unreachable (builder_at_end context bb));
785 let bbv = value_of_block bb in
786 set_value_name "Bb3" bbv;
787 insist ("Bb3" = value_name bbv);
789 group "casts";
790 let fn = define_function "X5" ty m in
791 let bb = entry_block fn in
792 ignore (build_unreachable (builder_at_end context bb));
793 insist (bb = block_of_value (value_of_block bb));
794 insist (value_is_block (value_of_block bb));
795 insist (not (value_is_block (const_null i32_type)));
797 begin group "iteration";
798 let m = create_module context "temp" in
799 let f = declare_function "Temp" (function_type i32_type [| |]) m in
801 insist (At_end f = block_begin f);
802 insist (At_start f = block_end f);
804 let b1 = append_block context "One" f in
805 let b2 = append_block context "Two" f in
807 insist (Before b1 = block_begin f);
808 insist (Before b2 = block_succ b1);
809 insist (At_end f = block_succ b2);
811 insist (After b2 = block_end f);
812 insist (After b1 = block_pred b2);
813 insist (At_start f = block_pred b1);
815 let lf s x = s ^ "->" ^ value_name (value_of_block x) in
816 insist ("->One->Two" = fold_left_blocks lf "" f);
818 let rf x s = value_name (value_of_block x) ^ "<-" ^ s in
819 insist ("One<-Two<-" = fold_right_blocks rf f "");
821 dispose_module m
825 (*===-- Instructions ------------------------------------------------------===*)
827 let test_instructions () =
828 begin group "iteration";
829 let m = create_module context "temp" in
830 let fty = function_type void_type [| i32_type; i32_type |] in
831 let f = define_function "f" fty m in
832 let bb = entry_block f in
833 let b = builder_at context (At_end bb) in
835 insist (At_end bb = instr_begin bb);
836 insist (At_start bb = instr_end bb);
838 let i1 = build_add (param f 0) (param f 1) "One" b in
839 let i2 = build_sub (param f 0) (param f 1) "Two" b in
841 insist (Before i1 = instr_begin bb);
842 insist (Before i2 = instr_succ i1);
843 insist (At_end bb = instr_succ i2);
845 insist (After i2 = instr_end bb);
846 insist (After i1 = instr_pred i2);
847 insist (At_start bb = instr_pred i1);
849 let lf s x = s ^ "->" ^ value_name x in
850 insist ("->One->Two" = fold_left_instrs lf "" bb);
852 let rf x s = value_name x ^ "<-" ^ s in
853 insist ("One<-Two<-" = fold_right_instrs rf bb "");
855 dispose_module m
856 end;
858 group "clone instr";
859 begin
860 (* CHECK: %clone = add i32 %0, 2
862 let fty = function_type void_type [| i32_type |] in
863 let fn = define_function "BuilderParent" fty m in
864 let bb = entry_block fn in
865 let b = builder_at_end context bb in
866 let p = param fn 0 in
867 let sum = build_add p p "sum" b in
868 let y = const_int i32_type 2 in
869 let clone = instr_clone sum in
870 set_operand clone 0 p;
871 set_operand clone 1 y;
872 insert_into_builder clone "clone" b;
873 ignore (build_ret_void b)
877 (*===-- Builder -----------------------------------------------------------===*)
879 let test_builder () =
880 let (++) x f = f x; x in
882 begin group "parent";
883 insist (try
884 ignore (insertion_block (builder context));
885 false
886 with Not_found ->
887 true);
889 let fty = function_type void_type [| i32_type |] in
890 let fn = define_function "BuilderParent" fty m in
891 let bb = entry_block fn in
892 let b = builder_at_end context bb in
893 let p = param fn 0 in
894 let sum = build_add p p "sum" b in
895 ignore (build_ret_void b);
897 insist (fn = block_parent bb);
898 insist (fn = param_parent p);
899 insist (bb = instr_parent sum);
900 insist (bb = insertion_block b)
901 end;
903 group "ret void";
904 begin
905 (* CHECK: ret void
907 let fty = function_type void_type [| |] in
908 let fn = declare_function "X6" fty m in
909 let b = builder_at_end context (append_block context "Bb01" fn) in
910 ignore (build_ret_void b)
911 end;
913 group "ret aggregate";
914 begin
915 (* CHECK: ret { i8, i64 } { i8 4, i64 5 }
917 let sty = struct_type context [| i8_type; i64_type |] in
918 let fty = function_type sty [| |] in
919 let fn = declare_function "XA6" fty m in
920 let b = builder_at_end context (append_block context "Bb01" fn) in
921 let agg = [| const_int i8_type 4; const_int i64_type 5 |] in
922 ignore (build_aggregate_ret agg b)
923 end;
925 (* The rest of the tests will use one big function. *)
926 let fty = function_type i32_type [| i32_type; i32_type |] in
927 let fn = define_function "X7" fty m in
928 let atentry = builder_at_end context (entry_block fn) in
929 let p1 = param fn 0 ++ set_value_name "P1" in
930 let p2 = param fn 1 ++ set_value_name "P2" in
931 let f1 = build_uitofp p1 float_type "F1" atentry in
932 let f2 = build_uitofp p2 float_type "F2" atentry in
934 let bb00 = append_block context "Bb00" fn in
935 ignore (build_unreachable (builder_at_end context bb00));
937 group "function attribute";
938 begin
939 let signext = create_enum_attr context "signext" 0L in
940 let zeroext = create_enum_attr context "zeroext" 0L in
941 let noalias = create_enum_attr context "noalias" 0L in
942 let nounwind = create_enum_attr context "nounwind" 0L in
943 let no_sse = create_string_attr context "no-sse" "" in
945 add_function_attr fn signext (AttrIndex.Param 0);
946 add_function_attr fn noalias (AttrIndex.Param 1);
947 insist ((function_attrs fn (AttrIndex.Param 1)) = [|noalias|]);
948 remove_enum_function_attr fn (enum_attr_kind "noalias") (AttrIndex.Param 1);
949 add_function_attr fn no_sse (AttrIndex.Param 1);
950 insist ((function_attrs fn (AttrIndex.Param 1)) = [|no_sse|]);
951 remove_string_function_attr fn "no-sse" (AttrIndex.Param 1);
952 insist ((function_attrs fn (AttrIndex.Param 1)) = [||]);
953 add_function_attr fn nounwind AttrIndex.Function;
954 add_function_attr fn zeroext AttrIndex.Return;
956 (* CHECK: define zeroext i32 @X7(i32 signext %P1, i32 %P2)
958 end;
960 group "casts"; begin
961 let void_ptr = pointer_type context in
963 (* CHECK-DAG: %build_trunc = trunc i32 %P1 to i8
964 * CHECK-DAG: %build_trunc2 = trunc i32 %P1 to i8
965 * CHECK-DAG: %build_trunc3 = trunc i32 %P1 to i8
966 * CHECK-DAG: %build_zext = zext i8 %build_trunc to i32
967 * CHECK-DAG: %build_zext2 = zext i8 %build_trunc to i32
968 * CHECK-DAG: %build_sext = sext i32 %build_zext to i64
969 * CHECK-DAG: %build_sext2 = sext i32 %build_zext to i64
970 * CHECK-DAG: %build_sext3 = sext i32 %build_zext to i64
971 * CHECK-DAG: %build_uitofp = uitofp i64 %build_sext to float
972 * CHECK-DAG: %build_sitofp = sitofp i32 %build_zext to double
973 * CHECK-DAG: %build_fptoui = fptoui float %build_uitofp to i32
974 * CHECK-DAG: %build_fptosi = fptosi double %build_sitofp to i64
975 * CHECK-DAG: %build_fptrunc = fptrunc double %build_sitofp to float
976 * CHECK-DAG: %build_fptrunc2 = fptrunc double %build_sitofp to float
977 * CHECK-DAG: %build_fpext = fpext float %build_fptrunc to double
978 * CHECK-DAG: %build_fpext2 = fpext float %build_fptrunc to double
979 * CHECK-DAG: %build_inttoptr = inttoptr i32 %P1 to ptr
980 * CHECK-DAG: %build_ptrtoint = ptrtoint ptr %build_inttoptr to i64
981 * CHECK-DAG: %build_ptrtoint2 = ptrtoint ptr %build_inttoptr to i64
982 * CHECK-DAG: %build_bitcast = bitcast i64 %build_ptrtoint to double
983 * CHECK-DAG: %build_bitcast2 = bitcast i64 %build_ptrtoint to double
984 * CHECK-DAG: %build_bitcast3 = bitcast i64 %build_ptrtoint to double
985 * CHECK-DAG: %build_bitcast4 = bitcast i64 %build_ptrtoint to double
987 let inst28 = build_trunc p1 i8_type "build_trunc" atentry in
988 let inst29 = build_zext inst28 i32_type "build_zext" atentry in
989 let inst30 = build_sext inst29 i64_type "build_sext" atentry in
990 let inst31 = build_uitofp inst30 float_type "build_uitofp" atentry in
991 let inst32 = build_sitofp inst29 double_type "build_sitofp" atentry in
992 ignore(build_fptoui inst31 i32_type "build_fptoui" atentry);
993 ignore(build_fptosi inst32 i64_type "build_fptosi" atentry);
994 let inst35 = build_fptrunc inst32 float_type "build_fptrunc" atentry in
995 ignore(build_fpext inst35 double_type "build_fpext" atentry);
996 let inst37 = build_inttoptr p1 void_ptr "build_inttoptr" atentry in
997 let inst38 = build_ptrtoint inst37 i64_type "build_ptrtoint" atentry in
998 ignore(build_bitcast inst38 double_type "build_bitcast" atentry);
999 ignore(build_zext_or_bitcast inst38 double_type "build_bitcast2" atentry);
1000 ignore(build_sext_or_bitcast inst38 double_type "build_bitcast3" atentry);
1001 ignore(build_trunc_or_bitcast inst38 double_type "build_bitcast4" atentry);
1002 ignore(build_pointercast inst37 (pointer_type context) "build_pointercast" atentry);
1004 ignore(build_zext_or_bitcast inst28 i32_type "build_zext2" atentry);
1005 ignore(build_sext_or_bitcast inst29 i64_type "build_sext2" atentry);
1006 ignore(build_trunc_or_bitcast p1 i8_type "build_trunc2" atentry);
1007 ignore(build_pointercast inst37 i64_type "build_ptrtoint2" atentry);
1008 ignore(build_intcast inst29 i64_type "build_sext3" atentry);
1009 ignore(build_intcast p1 i8_type "build_trunc3" atentry);
1010 ignore(build_fpcast inst35 double_type "build_fpext2" atentry);
1011 ignore(build_fpcast inst32 float_type "build_fptrunc2" atentry);
1012 end;
1014 group "comparisons"; begin
1015 (* CHECK: %build_icmp_ne = icmp ne i32 %P1, %P2
1016 * CHECK: %build_icmp_sle = icmp sle i32 %P2, %P1
1017 * CHECK: %build_fcmp_false = fcmp false float %F1, %F2
1018 * CHECK: %build_fcmp_true = fcmp true float %F2, %F1
1019 * CHECK: %build_is_null{{.*}}= icmp eq{{.*}}%X0,{{.*}}null
1020 * CHECK: %build_is_not_null = icmp ne ptr %X1, null
1021 * CHECK: %build_ptrdiff
1023 let c = build_icmp Icmp.Ne p1 p2 "build_icmp_ne" atentry in
1024 insist (Some Icmp.Ne = icmp_predicate c);
1025 insist (None = fcmp_predicate c);
1027 let c = build_icmp Icmp.Sle p2 p1 "build_icmp_sle" atentry in
1028 insist (Some Icmp.Sle = icmp_predicate c);
1029 insist (None = fcmp_predicate c);
1031 let c = build_fcmp Fcmp.False f1 f2 "build_fcmp_false" atentry in
1032 (* insist (Some Fcmp.False = fcmp_predicate c); *)
1033 insist (None = icmp_predicate c);
1035 let c = build_fcmp Fcmp.True f2 f1 "build_fcmp_true" atentry in
1036 (* insist (Some Fcmp.True = fcmp_predicate c); *)
1037 insist (None = icmp_predicate c);
1039 let g0 = declare_global (pointer_type context) "g0" m in
1040 let g1 = declare_global (pointer_type context) "g1" m in
1041 let p0 = build_load (pointer_type context) g0 "X0" atentry in
1042 let p1 = build_load (pointer_type context) g1 "X1" atentry in
1043 ignore (build_is_null p0 "build_is_null" atentry);
1044 ignore (build_is_not_null p1 "build_is_not_null" atentry);
1045 ignore (build_ptrdiff i8_type p1 p0 "build_ptrdiff" atentry);
1046 end;
1048 group "miscellaneous"; begin
1049 (* CHECK: %build_call = tail call cc63 zeroext i32 @{{.*}}(i32 signext %P2, i32 %P1)
1050 * CHECK: %build_select = select i1 %build_icmp, i32 %P1, i32 %P2
1051 * CHECK: %build_va_arg = va_arg ptr null, i32
1052 * CHECK: %build_extractelement = extractelement <4 x i32> %Vec1, i32 %P2
1053 * CHECK: %build_insertelement = insertelement <4 x i32> %Vec1, i32 %P1, i32 %P2
1054 * CHECK: %build_shufflevector = shufflevector <4 x i32> %Vec1, <4 x i32> %Vec2, <4 x i32> <i32 1, i32 1, i32 0, i32 0>
1055 * CHECK: %build_insertvalue0 = insertvalue{{.*}}%bl, i32 1, 0
1056 * CHECK: %build_extractvalue = extractvalue{{.*}}%build_insertvalue1, 1
1058 let ci = build_call fty fn [| p2; p1 |] "build_call" atentry in
1059 insist (CallConv.c = instruction_call_conv ci);
1060 set_instruction_call_conv 63 ci;
1061 insist (63 = instruction_call_conv ci);
1062 insist (not (is_tail_call ci));
1063 set_tail_call true ci;
1064 insist (is_tail_call ci);
1066 let signext = create_enum_attr context "signext" 0L in
1067 let zeroext = create_enum_attr context "zeroext" 0L in
1068 let noalias = create_enum_attr context "noalias" 0L in
1069 let noreturn = create_enum_attr context "noreturn" 0L in
1070 let no_sse = create_string_attr context "no-sse" "" in
1072 add_call_site_attr ci signext (AttrIndex.Param 0);
1073 add_call_site_attr ci noalias (AttrIndex.Param 1);
1074 insist ((call_site_attrs ci (AttrIndex.Param 1)) = [|noalias|]);
1075 remove_enum_call_site_attr ci (enum_attr_kind "noalias") (AttrIndex.Param 1);
1076 add_call_site_attr ci no_sse (AttrIndex.Param 1);
1077 insist ((call_site_attrs ci (AttrIndex.Param 1)) = [|no_sse|]);
1078 remove_string_call_site_attr ci "no-sse" (AttrIndex.Param 1);
1079 insist ((call_site_attrs ci (AttrIndex.Param 1)) = [||]);
1080 add_call_site_attr ci noreturn AttrIndex.Function;
1081 add_call_site_attr ci zeroext AttrIndex.Return;
1083 let inst46 = build_icmp Icmp.Eq p1 p2 "build_icmp" atentry in
1084 ignore (build_select inst46 p1 p2 "build_select" atentry);
1085 ignore (build_va_arg
1086 (const_null (pointer_type context))
1087 i32_type "build_va_arg" atentry);
1089 (* Set up some vector vregs. *)
1090 let one = const_int i32_type 1 in
1091 let zero = const_int i32_type 0 in
1092 let t1 = const_vector [| one; zero; one; zero |] in
1093 let t2 = const_vector [| zero; one; zero; one |] in
1094 let t3 = const_vector [| one; one; zero; zero |] in
1095 let vec1 = build_insertelement t1 p1 p2 "Vec1" atentry in
1096 let vec2 = build_insertelement t2 p1 p2 "Vec2" atentry in
1097 let sty = struct_type context [| i32_type; i8_type |] in
1099 ignore (build_extractelement vec1 p2 "build_extractelement" atentry);
1100 ignore (build_insertelement vec1 p1 p2 "build_insertelement" atentry);
1101 ignore (build_shufflevector vec1 vec2 t3 "build_shufflevector" atentry);
1103 let p = build_alloca sty "ba" atentry in
1104 let agg = build_load sty p "bl" atentry in
1105 let agg0 = build_insertvalue agg (const_int i32_type 1) 0
1106 "build_insertvalue0" atentry in
1107 let agg1 = build_insertvalue agg0 (const_int i8_type 2) 1
1108 "build_insertvalue1" atentry in
1109 ignore (build_extractvalue agg1 1 "build_extractvalue" atentry)
1110 end;
1112 group "metadata"; begin
1113 (* CHECK: %metadata = add i32 %P1, %P2, !test !1
1114 * !1 is metadata emitted at EOF.
1116 let i = build_add p1 p2 "metadata" atentry in
1117 insist ((has_metadata i) = false);
1119 let m1 = const_int i32_type 1 in
1120 let m2 = mdstring context "metadata test" in
1121 let md = mdnode context [| m1; m2 |] in
1123 let kind = mdkind_id context "test" in
1124 set_metadata i kind md;
1126 insist ((has_metadata i) = true);
1127 insist ((metadata i kind) = Some md);
1128 insist ((get_mdnode_operands md) = [| m1; m2 |]);
1130 clear_metadata i kind;
1132 insist ((has_metadata i) = false);
1133 insist ((metadata i kind) = None);
1135 set_metadata i kind md
1136 end;
1138 group "named metadata"; begin
1139 (* !llvm.module.flags is emitted at EOF. *)
1140 let n1 = const_int i32_type 1 in
1141 let n2 = mdstring context "Debug Info Version" in
1142 let n3 = const_int i32_type 3 in
1143 let md = mdnode context [| n1; n2; n3 |] in
1144 add_named_metadata_operand m "llvm.module.flags" md;
1146 insist ((get_named_metadata m "llvm.module.flags") = [| md |])
1147 end;
1149 group "ret"; begin
1150 (* CHECK: ret{{.*}}P1
1152 let ret = build_ret p1 atentry in
1153 position_before_dbg_records ret atentry
1154 end;
1156 (* see test/Feature/exception.ll *)
1157 let bblpad = append_block context "Bblpad" fn in
1158 let rt = struct_type context [| pointer_type context; i32_type |] in
1159 let ft = var_arg_function_type i32_type [||] in
1160 let personality = declare_function "__gxx_personality_v0" ft m in
1161 let ztic = declare_global (pointer_type context) "_ZTIc" m in
1162 let ztid = declare_global (pointer_type context) "_ZTId" m in
1163 let ztipkc = declare_global (pointer_type context) "_ZTIPKc" m in
1164 begin
1165 set_global_constant true ztic;
1166 set_global_constant true ztid;
1167 set_global_constant true ztipkc;
1168 let lp = build_landingpad rt personality 0 "lpad"
1169 (builder_at_end context bblpad) in begin
1170 set_cleanup lp true;
1171 add_clause lp ztic;
1172 insist((pointer_type context) = type_of ztid);
1173 let ety = pointer_type context in
1174 add_clause lp (const_array ety [| ztipkc; ztid |]);
1175 ignore (build_resume lp (builder_at_end context bblpad));
1176 end;
1177 (* CHECK: landingpad
1178 * CHECK: cleanup
1179 * CHECK: catch{{.*}}ptr{{.*}}@_ZTIc
1180 * CHECK: filter{{.*}}@_ZTIPKc{{.*}}@_ZTId
1181 * CHECK: resume
1182 * *)
1183 end;
1185 group "br"; begin
1186 (* CHECK: br{{.*}}Bb02
1188 let bb02 = append_block context "Bb02" fn in
1189 let b = builder_at_end context bb02 in
1190 let br = build_br bb02 b in
1191 insist (successors br = [| bb02 |]) ;
1192 insist (is_conditional br = false) ;
1193 insist (get_branch br = Some (`Unconditional bb02)) ;
1194 end;
1196 group "cond_br"; begin
1197 (* CHECK: br{{.*}}build_br{{.*}}Bb03{{.*}}Bb00
1199 let bb03 = append_block context "Bb03" fn in
1200 let b = builder_at_end context bb03 in
1201 let cond = build_trunc p1 i1_type "build_br" b in
1202 let br = build_cond_br cond bb03 bb00 b in
1203 insist (num_successors br = 2) ;
1204 insist (successor br 0 = bb03) ;
1205 insist (successor br 1 = bb00) ;
1206 insist (is_conditional br = true) ;
1207 insist (get_branch br = Some (`Conditional (cond, bb03, bb00))) ;
1208 end;
1210 group "switch"; begin
1211 (* CHECK: switch{{.*}}P1{{.*}}SwiBlock3
1212 * CHECK: 2,{{.*}}SwiBlock2
1214 let bb1 = append_block context "SwiBlock1" fn in
1215 let bb2 = append_block context "SwiBlock2" fn in
1216 ignore (build_unreachable (builder_at_end context bb2));
1217 let bb3 = append_block context "SwiBlock3" fn in
1218 ignore (build_unreachable (builder_at_end context bb3));
1219 let si = build_switch p1 bb3 1 (builder_at_end context bb1) in begin
1220 ignore (add_case si (const_int i32_type 2) bb2);
1221 insist (switch_default_dest si = bb3);
1222 end;
1223 insist (num_successors si = 2) ;
1224 insist (get_branch si = None) ;
1225 end;
1227 group "malloc/free"; begin
1228 (* CHECK: call{{.*}}@malloc(i32 ptrtoint
1229 * CHECK: call{{.*}}@free(ptr
1230 * CHECK: call{{.*}}@malloc(i32 %
1232 let bb1 = append_block context "MallocBlock1" fn in
1233 let m1 = (build_malloc (pointer_type context) "m1"
1234 (builder_at_end context bb1)) in
1235 ignore (build_free m1 (builder_at_end context bb1));
1236 ignore (build_array_malloc i32_type p1 "m2" (builder_at_end context bb1));
1237 ignore (build_unreachable (builder_at_end context bb1));
1238 end;
1240 group "indirectbr"; begin
1241 (* CHECK: indirectbr ptr blockaddress(@X7, %IBRBlock2), [label %IBRBlock2, label %IBRBlock3]
1243 let bb1 = append_block context "IBRBlock1" fn in
1245 let bb2 = append_block context "IBRBlock2" fn in
1246 ignore (build_unreachable (builder_at_end context bb2));
1248 let bb3 = append_block context "IBRBlock3" fn in
1249 ignore (build_unreachable (builder_at_end context bb3));
1251 let addr = block_address fn bb2 in
1252 let ibr = build_indirect_br addr 2 (builder_at_end context bb1) in
1253 ignore (add_destination ibr bb2);
1254 ignore (add_destination ibr bb3)
1255 end;
1257 group "invoke"; begin
1258 (* CHECK: build_invoke{{.*}}invoke{{.*}}P1{{.*}}P2
1259 * CHECK: to{{.*}}Bb04{{.*}}unwind{{.*}}Bblpad
1261 let bb04 = append_block context "Bb04" fn in
1262 let b = builder_at_end context bb04 in
1263 ignore (build_invoke fty fn [| p1; p2 |] bb04 bblpad "build_invoke" b)
1264 end;
1266 group "unreachable"; begin
1267 (* CHECK: unreachable
1269 let bb06 = append_block context "Bb06" fn in
1270 let b = builder_at_end context bb06 in
1271 ignore (build_unreachable b)
1272 end;
1274 group "arithmetic"; begin
1275 let bb07 = append_block context "Bb07" fn in
1276 let b = builder_at_end context bb07 in
1278 (* CHECK: %build_add = add i32 %P1, %P2
1279 * CHECK: %build_nsw_add = add nsw i32 %P1, %P2
1280 * CHECK: %build_nuw_add = add nuw i32 %P1, %P2
1281 * CHECK: %build_fadd = fadd float %F1, %F2
1282 * CHECK: %build_sub = sub i32 %P1, %P2
1283 * CHECK: %build_nsw_sub = sub nsw i32 %P1, %P2
1284 * CHECK: %build_nuw_sub = sub nuw i32 %P1, %P2
1285 * CHECK: %build_fsub = fsub float %F1, %F2
1286 * CHECK: %build_mul = mul i32 %P1, %P2
1287 * CHECK: %build_nsw_mul = mul nsw i32 %P1, %P2
1288 * CHECK: %build_nuw_mul = mul nuw i32 %P1, %P2
1289 * CHECK: %build_fmul = fmul float %F1, %F2
1290 * CHECK: %build_udiv = udiv i32 %P1, %P2
1291 * CHECK: %build_sdiv = sdiv i32 %P1, %P2
1292 * CHECK: %build_exact_sdiv = sdiv exact i32 %P1, %P2
1293 * CHECK: %build_fdiv = fdiv float %F1, %F2
1294 * CHECK: %build_urem = urem i32 %P1, %P2
1295 * CHECK: %build_srem = srem i32 %P1, %P2
1296 * CHECK: %build_frem = frem float %F1, %F2
1297 * CHECK: %build_shl = shl i32 %P1, %P2
1298 * CHECK: %build_lshl = lshr i32 %P1, %P2
1299 * CHECK: %build_ashl = ashr i32 %P1, %P2
1300 * CHECK: %build_and = and i32 %P1, %P2
1301 * CHECK: %build_or = or i32 %P1, %P2
1302 * CHECK: %build_xor = xor i32 %P1, %P2
1303 * CHECK: %build_neg = sub i32 0, %P1
1304 * CHECK: %build_nsw_neg = sub nsw i32 0, %P1
1305 * CHECK: %build_nuw_neg = sub nuw i32 0, %P1
1306 * CHECK: %build_fneg = fneg float %F1
1307 * CHECK: %build_not = xor i32 %P1, -1
1308 * CHECK: %build_freeze = freeze i32 %P1
1310 ignore (build_add p1 p2 "build_add" b);
1311 ignore (build_nsw_add p1 p2 "build_nsw_add" b);
1312 ignore (build_nuw_add p1 p2 "build_nuw_add" b);
1313 ignore (build_fadd f1 f2 "build_fadd" b);
1314 ignore (build_sub p1 p2 "build_sub" b);
1315 ignore (build_nsw_sub p1 p2 "build_nsw_sub" b);
1316 ignore (build_nuw_sub p1 p2 "build_nuw_sub" b);
1317 ignore (build_fsub f1 f2 "build_fsub" b);
1318 ignore (build_mul p1 p2 "build_mul" b);
1319 ignore (build_nsw_mul p1 p2 "build_nsw_mul" b);
1320 ignore (build_nuw_mul p1 p2 "build_nuw_mul" b);
1321 ignore (build_fmul f1 f2 "build_fmul" b);
1322 ignore (build_udiv p1 p2 "build_udiv" b);
1323 ignore (build_sdiv p1 p2 "build_sdiv" b);
1324 ignore (build_exact_sdiv p1 p2 "build_exact_sdiv" b);
1325 ignore (build_fdiv f1 f2 "build_fdiv" b);
1326 ignore (build_urem p1 p2 "build_urem" b);
1327 ignore (build_srem p1 p2 "build_srem" b);
1328 ignore (build_frem f1 f2 "build_frem" b);
1329 ignore (build_shl p1 p2 "build_shl" b);
1330 ignore (build_lshr p1 p2 "build_lshl" b);
1331 ignore (build_ashr p1 p2 "build_ashl" b);
1332 ignore (build_and p1 p2 "build_and" b);
1333 ignore (build_or p1 p2 "build_or" b);
1334 ignore (build_xor p1 p2 "build_xor" b);
1335 ignore (build_neg p1 "build_neg" b);
1336 ignore (build_nsw_neg p1 "build_nsw_neg" b);
1337 ignore (build_nuw_neg p1 "build_nuw_neg" b);
1338 ignore (build_fneg f1 "build_fneg" b);
1339 ignore (build_not p1 "build_not" b);
1340 ignore (build_freeze p1 "build_freeze" b);
1341 ignore (build_unreachable b)
1342 end;
1344 group "memory"; begin
1345 let bb08 = append_block context "Bb08" fn in
1346 let b = builder_at_end context bb08 in
1348 (* CHECK: %build_alloca = alloca i32
1349 * CHECK: %build_array_alloca = alloca i32, i32 %P2
1350 * CHECK: %build_load = load volatile i32, ptr %build_array_alloca, align 4
1351 * CHECK: store volatile i32 %P2, ptr %build_alloca, align 4
1352 * CHECK: %build_gep = getelementptr i32, ptr %build_array_alloca, i32 %P2
1353 * CHECK: %build_in_bounds_gep = getelementptr inbounds i32, ptr %build_array_alloca, i32 %P2
1354 * CHECK: %build_struct_gep = getelementptr inbounds{{.*}}%build_alloca2, i32 0, i32 1
1355 * CHECK: %build_atomicrmw = atomicrmw xchg ptr %p, i8 42 seq_cst
1357 let alloca = build_alloca i32_type "build_alloca" b in
1358 let array_alloca = build_array_alloca i32_type p2 "build_array_alloca" b in
1360 let load = build_load i32_type array_alloca "build_load" b in
1361 ignore(set_alignment 4 load);
1362 ignore(set_volatile true load);
1363 insist(true = is_volatile load);
1364 insist(4 = alignment load);
1366 let store = build_store p2 alloca b in
1367 ignore(set_volatile true store);
1368 ignore(set_alignment 4 store);
1369 insist(true = is_volatile store);
1370 insist(4 = alignment store);
1371 ignore(build_gep i32_type array_alloca [| p2 |] "build_gep" b);
1372 ignore(build_in_bounds_gep i32_type array_alloca [| p2 |]
1373 "build_in_bounds_gep" b);
1375 let sty = struct_type context [| i32_type; i8_type |] in
1376 let alloca2 = build_alloca sty "build_alloca2" b in
1377 ignore(build_struct_gep sty alloca2 1 "build_struct_gep" b);
1379 let p = build_alloca i8_type "p" b in
1380 ignore(build_atomicrmw AtomicRMWBinOp.Xchg p (const_int i8_type 42)
1381 AtomicOrdering.SequentiallyConsistent false "build_atomicrmw"
1384 ignore(build_unreachable b)
1385 end;
1387 group "string"; begin
1388 let bb09 = append_block context "Bb09" fn in
1389 let b = builder_at_end context bb09 in
1390 let p = build_alloca (pointer_type context) "p" b in
1391 (* build_global_string is emitted above.
1392 * CHECK: store{{.*}}build_global_string1{{.*}}p
1393 * *)
1394 ignore (build_global_string "stringval" "build_global_string" b);
1395 let g = build_global_stringptr "stringval" "build_global_string1" b in
1396 ignore (build_store g p b);
1397 ignore(build_unreachable b);
1398 end;
1400 group "phi"; begin
1401 (* CHECK: PhiNode{{.*}}P1{{.*}}PhiBlock1{{.*}}P2{{.*}}PhiBlock2
1403 let b1 = append_block context "PhiBlock1" fn in
1404 let b2 = append_block context "PhiBlock2" fn in
1406 let jb = append_block context "PhiJoinBlock" fn in
1407 ignore (build_br jb (builder_at_end context b1));
1408 ignore (build_br jb (builder_at_end context b2));
1409 let at_jb = builder_at_end context jb in
1411 let phi = build_phi [(p1, b1)] "PhiNode" at_jb in
1412 insist ([(p1, b1)] = incoming phi);
1414 add_incoming (p2, b2) phi;
1415 insist ([(p1, b1); (p2, b2)] = incoming phi);
1417 (* CHECK: %PhiEmptyNode = phi i8
1419 let phi_empty = build_empty_phi i8_type "PhiEmptyNode" at_jb in
1420 insist ([] = incoming phi_empty);
1422 (* can't emit an empty phi to bitcode *)
1423 add_incoming (const_int i8_type 1, b1) phi_empty;
1424 add_incoming (const_int i8_type 2, b2) phi_empty;
1426 ignore (build_unreachable at_jb);
1429 (* End-of-file checks for things like metdata and attributes.
1430 * CHECK: !llvm.module.flags = !{!0}
1431 * CHECK: !0 = !{i32 1, !"Debug Info Version", i32 3}
1432 * CHECK: !1 = !{i32 1, !"metadata test"}
1436 (*===-- Memory Buffer -----------------------------------------------------===*)
1438 let test_memory_buffer () =
1439 group "memory buffer";
1440 let buf = MemoryBuffer.of_string "foobar" in
1441 insist ((MemoryBuffer.as_string buf) = "foobar")
1444 (*===-- Writer ------------------------------------------------------------===*)
1446 let test_writer () =
1447 group "valid";
1448 insist (match Llvm_analysis.verify_module m with
1449 | None -> true
1450 | Some msg -> prerr_string msg; false);
1452 group "writer";
1453 insist (write_bitcode_file m filename);
1455 dispose_module m
1458 (*===-- Driver ------------------------------------------------------------===*)
1460 let _ =
1461 suite "modules" test_modules;
1462 suite "contained types" test_contained_types;
1463 suite "pointer types" test_pointer_types;
1464 suite "conversion" test_conversion;
1465 suite "target" test_target;
1466 suite "constants" test_constants;
1467 suite "attributes" test_attributes;
1468 suite "global values" test_global_values;
1469 suite "global variables" test_global_variables;
1470 suite "uses" test_uses;
1471 suite "users" test_users;
1472 suite "aliases" test_aliases;
1473 suite "functions" test_functions;
1474 suite "params" test_params;
1475 suite "basic blocks" test_basic_blocks;
1476 suite "instructions" test_instructions;
1477 suite "builder" test_builder;
1478 suite "memory buffer" test_memory_buffer;
1479 suite "writer" test_writer; (* Keep this last; it disposes m. *)
1480 exit !exit_status