1 (* RUN: cp %s %T/vmcore.ml
2 * RUN: %ocamlcomp -g -warn-error A -package llvm.analysis -package llvm.bitwriter -linkpkg %T/vmcore.ml -o %t
4 * RUN: llvm-dis < %t.bc > %t.ll
5 * RUN: FileCheck %s < %t.ll
6 * Do a second pass for things that shouldn't be anywhere.
7 * RUN: FileCheck -check-prefix=CHECK-NOWHERE %s < %t.ll
11 (* Note: It takes several seconds for ocamlopt to link an executable with
12 libLLVMCore.a, so it's better to write a big test than a bunch of
19 (* Tiny unit test framework - really just to help find which line is busted *)
20 let exit_status = ref 0
21 let suite_name = ref ""
22 let group_name = ref ""
24 let print_checkpoints = false
25 let context = global_context ()
26 let i1_type = Llvm.i1_type context
27 let i8_type = Llvm.i8_type context
28 let i16_type = Llvm.i16_type context
29 let i32_type = Llvm.i32_type context
30 let i64_type = Llvm.i64_type context
31 let void_type = Llvm.void_type context
32 let float_type = Llvm.float_type context
33 let double_type = Llvm.double_type context
34 let fp128_type = Llvm.fp128_type context
37 group_name := !suite_name ^ "/" ^ name;
39 if print_checkpoints then
40 prerr_endline (" " ^ name ^ "...")
46 match print_checkpoints, cond with
49 prerr_endline ("FAILED: " ^ !suite_name ^ "/" ^ !group_name ^ " #" ^ (string_of_int !case_num))
51 prerr_endline (" " ^ (string_of_int !case_num))
53 prerr_endline (" " ^ (string_of_int !case_num) ^ " FAIL")
57 if print_checkpoints then
58 prerr_endline (name ^ ":");
62 (*===-- Fixture -----------------------------------------------------------===*)
64 let filename = Sys.argv.(1)
65 let m = create_module context filename
68 (*===-- Conversion --------------------------------------------------------===*)
70 let test_conversion () =
71 insist ("i32" = (string_of_lltype i32_type));
72 let c = const_int i32_type 42 in
73 insist ("i32 42" = (string_of_llvalue c))
76 (*===-- Target ------------------------------------------------------------===*)
80 let trip = "i686-apple-darwin8" in
81 set_target_triple trip m;
82 insist (trip = target_triple m)
87 set_data_layout layout m;
88 insist (layout = data_layout m)
90 (* CHECK: target datalayout = "e"
91 * CHECK: target triple = "i686-apple-darwin8"
95 (*===-- Constants ---------------------------------------------------------===*)
97 let test_constants () =
98 (* CHECK: const_int{{.*}}i32{{.*}}-1
101 let c = const_int i32_type (-1) in
102 ignore (define_global "const_int" c m);
103 insist (i32_type = type_of c);
104 insist (is_constant c);
105 insist (Some (-1L) = int64_of_const c);
107 (* CHECK: const_sext_int{{.*}}i64{{.*}}-1
110 let c = const_int i64_type (-1) in
111 ignore (define_global "const_sext_int" c m);
112 insist (i64_type = type_of c);
113 insist (Some (-1L) = int64_of_const c);
115 (* CHECK: const_zext_int64{{.*}}i64{{.*}}4294967295
118 let c = const_of_int64 i64_type (Int64.of_string "4294967295") false in
119 ignore (define_global "const_zext_int64" c m);
120 insist (i64_type = type_of c);
121 insist (Some 4294967295L = int64_of_const c);
123 (* CHECK: const_int_string{{.*}}i32{{.*}}-1
126 let c = const_int_of_string i32_type "-1" 10 in
127 ignore (define_global "const_int_string" c m);
128 insist (i32_type = type_of c);
129 insist (None = (string_of_const c));
130 insist (None = float_of_const c);
131 insist (Some (-1L) = int64_of_const c);
133 (* CHECK: const_int64{{.*}}i64{{.*}}9223372036854775807
136 let c = const_of_int64 i64_type 9223372036854775807L true in
137 ignore (define_global "const_int64" c m) ;
138 insist (i64_type = type_of c);
139 insist (Some 9223372036854775807L = int64_of_const c);
141 if Sys.word_size = 64; then begin
143 let c = const_int i64_type (1 lsl 61) in
144 insist (c = const_of_int64 i64_type (Int64.of_int (1 lsl 61)) false)
147 (* CHECK: @const_string = global {{.*}}c"cruel\00world"
150 let c = const_string context "cruel\000world" in
151 ignore (define_global "const_string" c m);
152 insist ((array_type i8_type 11) = type_of c);
153 insist ((Some "cruel\000world") = (string_of_const c));
155 (* CHECK: const_stringz{{.*}}"hi\00again\00"
158 let c = const_stringz context "hi\000again" in
159 ignore (define_global "const_stringz" c m);
160 insist ((array_type i8_type 9) = type_of c);
162 (* CHECK: const_single{{.*}}2.75
163 * CHECK: const_double{{.*}}3.1459
164 * CHECK: const_double_string{{.*}}2
165 * CHECK: const_fake_fp128{{.*}}0xL00000000000000004000000000000000
166 * CHECK: const_fp128_string{{.*}}0xLF3CB1CCF26FBC178452FB4EC7F91973F
169 let cs = const_float float_type 2.75 in
170 ignore (define_global "const_single" cs m);
171 insist (float_type = type_of cs);
172 insist (float_of_const cs = Some 2.75);
174 let cd = const_float double_type 3.1459 in
175 ignore (define_global "const_double" cd m);
176 insist (double_type = type_of cd);
177 insist (float_of_const cd = Some 3.1459);
179 let cd = const_float_of_string double_type "2" in
180 ignore (define_global "const_double_string" cd m);
181 insist (double_type = type_of cd);
182 insist (float_of_const cd = Some 2.);
184 let cd = const_float fp128_type 2. in
185 ignore (define_global "const_fake_fp128" cd m);
186 insist (fp128_type = type_of cd);
187 insist (float_of_const cd = Some 2.);
189 let cd = const_float_of_string fp128_type "1e400" in
190 ignore (define_global "const_fp128_string" cd m);
191 insist (fp128_type = type_of cd);
192 insist (float_of_const cd = None);
195 let one = const_int i16_type 1 in
196 let two = const_int i16_type 2 in
197 let three = const_int i32_type 3 in
198 let four = const_int i32_type 4 in
200 (* CHECK: const_array{{.*}}[i32 3, i32 4]
203 let c = const_array i32_type [| three; four |] in
204 ignore (define_global "const_array" c m);
205 insist ((array_type i32_type 2) = (type_of c));
206 insist (three = (const_element c 0));
207 insist (four = (const_element c 1));
209 (* CHECK: const_vector{{.*}}<i16 1, i16 2{{.*}}>
212 let c = const_vector [| one; two; one; two;
213 one; two; one; two |] in
214 ignore (define_global "const_vector" c m);
215 insist ((vector_type i16_type 8) = (type_of c));
217 (* CHECK: const_structure{{.*.}}i16 1, i16 2, i32 3, i32 4
220 let c = const_struct context [| one; two; three; four |] in
221 ignore (define_global "const_structure" c m);
222 insist ((struct_type context [| i16_type; i16_type; i32_type; i32_type |])
225 (* CHECK: const_null{{.*}}zeroinit
228 let c = const_null (packed_struct_type context [| i1_type; i8_type; i64_type;
230 ignore (define_global "const_null" c m);
232 (* CHECK: const_all_ones{{.*}}-1
235 let c = const_all_ones i64_type in
236 ignore (define_global "const_all_ones" c m);
238 group "pointer null"; begin
239 (* CHECK: const_pointer_null = global i64* null
241 let c = const_pointer_null (pointer_type i64_type) in
242 ignore (define_global "const_pointer_null" c m);
245 (* CHECK: const_undef{{.*}}undef
248 let c = undef i1_type in
249 ignore (define_global "const_undef" c m);
250 insist (i1_type = type_of c);
253 group "constant arithmetic";
254 (* CHECK: @const_neg = global i64 sub
255 * CHECK: @const_nsw_neg = global i64 sub nsw
256 * CHECK: @const_nuw_neg = global i64 sub nuw
257 * CHECK: @const_fneg = global double fsub
258 * CHECK: @const_not = global i64 xor
259 * CHECK: @const_add = global i64 add
260 * CHECK: @const_nsw_add = global i64 add nsw
261 * CHECK: @const_nuw_add = global i64 add nuw
262 * CHECK: @const_fadd = global double fadd
263 * CHECK: @const_sub = global i64 sub
264 * CHECK: @const_nsw_sub = global i64 sub nsw
265 * CHECK: @const_nuw_sub = global i64 sub nuw
266 * CHECK: @const_fsub = global double fsub
267 * CHECK: @const_mul = global i64 mul
268 * CHECK: @const_nsw_mul = global i64 mul nsw
269 * CHECK: @const_nuw_mul = global i64 mul nuw
270 * CHECK: @const_fmul = global double fmul
271 * CHECK: @const_udiv = global i64 udiv
272 * CHECK: @const_sdiv = global i64 sdiv
273 * CHECK: @const_exact_sdiv = global i64 sdiv exact
274 * CHECK: @const_fdiv = global double fdiv
275 * CHECK: @const_urem = global i64 urem
276 * CHECK: @const_srem = global i64 srem
277 * CHECK: @const_frem = global double frem
278 * CHECK: @const_and = global i64 and
279 * CHECK: @const_or = global i64 or
280 * CHECK: @const_xor = global i64 xor
281 * CHECK: @const_icmp = global i1 icmp sle
282 * CHECK: @const_fcmp = global i1 fcmp ole
284 let void_ptr = pointer_type i8_type in
285 let five = const_int i64_type 5 in
286 let ffive = const_uitofp five double_type in
287 let foldbomb_gv = define_global "FoldBomb" (const_null i8_type) m in
288 let foldbomb = const_ptrtoint foldbomb_gv i64_type in
289 let ffoldbomb = const_uitofp foldbomb double_type in
290 ignore (define_global "const_neg" (const_neg foldbomb) m);
291 ignore (define_global "const_nsw_neg" (const_nsw_neg foldbomb) m);
292 ignore (define_global "const_nuw_neg" (const_nuw_neg foldbomb) m);
293 ignore (define_global "const_fneg" (const_fneg ffoldbomb) m);
294 ignore (define_global "const_not" (const_not foldbomb) m);
295 ignore (define_global "const_add" (const_add foldbomb five) m);
296 ignore (define_global "const_nsw_add" (const_nsw_add foldbomb five) m);
297 ignore (define_global "const_nuw_add" (const_nuw_add foldbomb five) m);
298 ignore (define_global "const_fadd" (const_fadd ffoldbomb ffive) m);
299 ignore (define_global "const_sub" (const_sub foldbomb five) m);
300 ignore (define_global "const_nsw_sub" (const_nsw_sub foldbomb five) m);
301 ignore (define_global "const_nuw_sub" (const_nuw_sub foldbomb five) m);
302 ignore (define_global "const_fsub" (const_fsub ffoldbomb ffive) m);
303 ignore (define_global "const_mul" (const_mul foldbomb five) m);
304 ignore (define_global "const_nsw_mul" (const_nsw_mul foldbomb five) m);
305 ignore (define_global "const_nuw_mul" (const_nuw_mul foldbomb five) m);
306 ignore (define_global "const_fmul" (const_fmul ffoldbomb ffive) m);
307 ignore (define_global "const_udiv" (const_udiv foldbomb five) m);
308 ignore (define_global "const_sdiv" (const_sdiv foldbomb five) m);
309 ignore (define_global "const_exact_sdiv" (const_exact_sdiv foldbomb five) m);
310 ignore (define_global "const_fdiv" (const_fdiv ffoldbomb ffive) m);
311 ignore (define_global "const_urem" (const_urem foldbomb five) m);
312 ignore (define_global "const_srem" (const_srem foldbomb five) m);
313 ignore (define_global "const_frem" (const_frem ffoldbomb ffive) m);
314 ignore (define_global "const_and" (const_and foldbomb five) m);
315 ignore (define_global "const_or" (const_or foldbomb five) m);
316 ignore (define_global "const_xor" (const_xor foldbomb five) m);
317 ignore (define_global "const_icmp" (const_icmp Icmp.Sle foldbomb five) m);
318 ignore (define_global "const_fcmp" (const_fcmp Fcmp.Ole ffoldbomb ffive) m);
320 group "constant casts";
321 (* CHECK: const_trunc{{.*}}trunc
322 * CHECK: const_sext{{.*}}sext
323 * CHECK: const_zext{{.*}}zext
324 * CHECK: const_fptrunc{{.*}}fptrunc
325 * CHECK: const_fpext{{.*}}fpext
326 * CHECK: const_uitofp{{.*}}uitofp
327 * CHECK: const_sitofp{{.*}}sitofp
328 * CHECK: const_fptoui{{.*}}fptoui
329 * CHECK: const_fptosi{{.*}}fptosi
330 * CHECK: const_ptrtoint{{.*}}ptrtoint
331 * CHECK: const_inttoptr{{.*}}inttoptr
332 * CHECK: const_bitcast{{.*}}bitcast
333 * CHECK: const_intcast{{.*}}zext
335 let i128_type = integer_type context 128 in
336 ignore (define_global "const_trunc" (const_trunc (const_add foldbomb five)
338 ignore (define_global "const_sext" (const_sext foldbomb i128_type) m);
339 ignore (define_global "const_zext" (const_zext foldbomb i128_type) m);
340 ignore (define_global "const_fptrunc" (const_fptrunc ffoldbomb float_type) m);
341 ignore (define_global "const_fpext" (const_fpext ffoldbomb fp128_type) m);
342 ignore (define_global "const_uitofp" (const_uitofp foldbomb double_type) m);
343 ignore (define_global "const_sitofp" (const_sitofp foldbomb double_type) m);
344 ignore (define_global "const_fptoui" (const_fptoui ffoldbomb i32_type) m);
345 ignore (define_global "const_fptosi" (const_fptosi ffoldbomb i32_type) m);
346 ignore (define_global "const_ptrtoint" (const_ptrtoint
347 (const_gep (const_null (pointer_type i8_type))
348 [| const_int i32_type 1 |])
350 ignore (define_global "const_inttoptr" (const_inttoptr (const_add foldbomb five)
352 ignore (define_global "const_bitcast" (const_bitcast ffoldbomb i64_type) m);
353 ignore (define_global "const_intcast"
354 (const_intcast foldbomb i128_type ~is_signed:false) m);
356 group "misc constants";
357 (* CHECK: const_size_of{{.*}}getelementptr{{.*}}null
358 * CHECK: const_gep{{.*}}getelementptr
359 * CHECK: const_select{{.*}}select
360 * CHECK: const_extractelement{{.*}}extractelement
361 * CHECK: const_insertelement{{.*}}insertelement
362 * CHECK: const_shufflevector = global <4 x i32> <i32 0, i32 1, i32 1, i32 0>
364 ignore (define_global "const_size_of" (size_of (pointer_type i8_type)) m);
365 ignore (define_global "const_gep" (const_gep foldbomb_gv [| five |]) m);
366 ignore (define_global "const_select" (const_select
367 (const_icmp Icmp.Sle foldbomb five)
368 (const_int i8_type (-1))
369 (const_int i8_type 0)) m);
370 let zero = const_int i32_type 0 in
371 let one = const_int i32_type 1 in
372 ignore (define_global "const_extractelement" (const_extractelement
373 (const_vector [| zero; one; zero; one |])
374 (const_trunc foldbomb i32_type)) m);
375 ignore (define_global "const_insertelement" (const_insertelement
376 (const_vector [| zero; one; zero; one |])
377 zero (const_trunc foldbomb i32_type)) m);
378 ignore (define_global "const_shufflevector" (const_shufflevector
379 (const_vector [| zero; one |])
380 (const_vector [| one; zero |])
381 (const_vector [| const_int i32_type 0; const_int i32_type 1;
382 const_int i32_type 2; const_int i32_type 3 |])) m);
385 let ft = function_type void_type [| i32_type; i32_type; i32_type |] in
386 ignore (const_inline_asm
389 "{cx},{ax},{di},~{dirflag},~{fpsr},~{flags},~{edi},~{ecx}"
394 group "recursive struct"; begin
395 let nsty = named_struct_type context "rec" in
396 let pty = pointer_type nsty in
397 struct_set_body nsty [| i32_type; pty |] false;
398 let elts = [| const_int i32_type 4; const_pointer_null pty |] in
399 let grec_init = const_named_struct nsty elts in
400 ignore (define_global "grec" grec_init m);
401 ignore (string_of_lltype nsty);
405 (*===-- Global Values -----------------------------------------------------===*)
407 let test_global_values () =
408 let (++) x f = f x; x in
409 let zero32 = const_null i32_type in
414 let g = define_global "TEMPORARY" zero32 m in
415 insist ("TEMPORARY" = value_name g);
416 set_value_name "GVal01" g;
417 insist ("GVal01" = value_name g);
419 (* CHECK: GVal02{{.*}}linkonce
422 let g = define_global "GVal02" zero32 m ++
423 set_linkage Linkage.Link_once in
424 insist (Linkage.Link_once = linkage g);
426 (* CHECK: GVal03{{.*}}Hanalei
429 let g = define_global "GVal03" zero32 m ++
430 set_section "Hanalei" in
431 insist ("Hanalei" = section g);
433 (* CHECK: GVal04{{.*}}hidden
436 let g = define_global "GVal04" zero32 m ++
437 set_visibility Visibility.Hidden in
438 insist (Visibility.Hidden = visibility g);
440 (* CHECK: GVal05{{.*}}align 128
443 let g = define_global "GVal05" zero32 m ++
445 insist (128 = alignment g);
447 (* CHECK: GVal06{{.*}}dllexport
449 group "dll_storage_class";
450 let g = define_global "GVal06" zero32 m ++
451 set_dll_storage_class DLLStorageClass.DLLExport in
452 insist (DLLStorageClass.DLLExport = dll_storage_class g)
455 (*===-- Global Variables --------------------------------------------------===*)
457 let test_global_variables () =
458 let (++) x f = f x; x in
459 let forty_two32 = const_int i32_type 42 in
461 group "declarations"; begin
462 (* CHECK: @GVar01 = external global i32
463 * CHECK: @QGVar01 = external addrspace(3) global i32
465 insist (None == lookup_global "GVar01" m);
466 let g = declare_global i32_type "GVar01" m in
467 insist (is_declaration g);
468 insist (pointer_type float_type ==
469 type_of (declare_global float_type "GVar01" m));
470 insist (g == declare_global i32_type "GVar01" m);
471 insist (match lookup_global "GVar01" m with Some x -> x = g
474 insist (None == lookup_global "QGVar01" m);
475 let g = declare_qualified_global i32_type "QGVar01" 3 m in
476 insist (is_declaration g);
477 insist (qualified_pointer_type float_type 3 ==
478 type_of (declare_qualified_global float_type "QGVar01" 3 m));
479 insist (g == declare_qualified_global i32_type "QGVar01" 3 m);
480 insist (match lookup_global "QGVar01" m with Some x -> x = g
484 group "definitions"; begin
485 (* CHECK: @GVar02 = global i32 42
486 * CHECK: @GVar03 = global i32 42
487 * CHECK: @QGVar02 = addrspace(3) global i32 42
488 * CHECK: @QGVar03 = addrspace(3) global i32 42
490 let g = define_global "GVar02" forty_two32 m in
491 let g2 = declare_global i32_type "GVar03" m ++
492 set_initializer forty_two32 in
493 insist (not (is_declaration g));
494 insist (not (is_declaration g2));
495 insist ((global_initializer g) == (global_initializer g2));
497 let g = define_qualified_global "QGVar02" forty_two32 3 m in
498 let g2 = declare_qualified_global i32_type "QGVar03" 3 m ++
499 set_initializer forty_two32 in
500 insist (not (is_declaration g));
501 insist (not (is_declaration g2));
502 insist ((global_initializer g) == (global_initializer g2));
505 (* CHECK: GVar04{{.*}}thread_local
508 let g = define_global "GVar04" forty_two32 m ++
509 set_thread_local true in
510 insist (is_thread_local g);
512 (* CHECK: GVar05{{.*}}thread_local(initialexec)
514 group "threadlocal_mode";
515 let g = define_global "GVar05" forty_two32 m ++
516 set_thread_local_mode ThreadLocalMode.InitialExec in
517 insist ((thread_local_mode g) = ThreadLocalMode.InitialExec);
519 (* CHECK: GVar06{{.*}}externally_initialized
521 group "externally_initialized";
522 let g = define_global "GVar06" forty_two32 m ++
523 set_externally_initialized true in
524 insist (is_externally_initialized g);
526 (* CHECK-NOWHERE-NOT: GVar07
529 let g = define_global "GVar07" forty_two32 m in
532 (* CHECK: ConstGlobalVar{{.*}}constant
535 let g = define_global "ConstGlobalVar" forty_two32 m in
536 insist (not (is_global_constant g));
537 set_global_constant true g;
538 insist (is_global_constant g);
540 begin group "iteration";
541 let m = create_module context "temp" in
543 insist (At_end m = global_begin m);
544 insist (At_start m = global_end m);
546 let g1 = declare_global i32_type "One" m in
547 let g2 = declare_global i32_type "Two" m in
549 insist (Before g1 = global_begin m);
550 insist (Before g2 = global_succ g1);
551 insist (At_end m = global_succ g2);
553 insist (After g2 = global_end m);
554 insist (After g1 = global_pred g2);
555 insist (At_start m = global_pred g1);
557 let lf s x = s ^ "->" ^ value_name x in
558 insist ("->One->Two" = fold_left_globals lf "" m);
560 let rf x s = value_name x ^ "<-" ^ s in
561 insist ("One<-Two<-" = fold_right_globals rf m "");
566 (* String globals built below are emitted here.
567 * CHECK: build_global_string{{.*}}stringval
571 (*===-- Uses --------------------------------------------------------------===*)
574 let ty = function_type i32_type [| i32_type; i32_type |] in
575 let fn = define_function "use_function" ty m in
576 let b = builder_at_end context (entry_block fn) in
578 let p1 = param fn 0 in
579 let p2 = param fn 1 in
580 let v1 = build_add p1 p2 "v1" b in
581 let v2 = build_add p1 v1 "v2" b in
582 let _ = build_add v1 v2 "v3" b in
584 let lf s u = value_name (user u) ^ "->" ^ s in
585 insist ("v2->v3->" = fold_left_uses lf "" v1);
586 let rf u s = value_name (user u) ^ "<-" ^ s in
587 insist ("v3<-v2<-" = fold_right_uses rf v1 "");
589 let lf s u = value_name (used_value u) ^ "->" ^ s in
590 insist ("v1->v1->" = fold_left_uses lf "" v1);
592 let rf u s = value_name (used_value u) ^ "<-" ^ s in
593 insist ("v1<-v1<-" = fold_right_uses rf v1 "");
595 ignore (build_unreachable b)
598 (*===-- Users -------------------------------------------------------------===*)
601 let ty = function_type i32_type [| i32_type; i32_type |] in
602 let fn = define_function "user_function" ty m in
603 let b = builder_at_end context (entry_block fn) in
605 let p1 = param fn 0 in
606 let p2 = param fn 1 in
607 let a3 = build_alloca i32_type "user_alloca" b in
608 let p3 = build_load a3 "user_load" b in
609 let i = build_add p1 p2 "sum" b in
611 insist ((num_operands i) = 2);
612 insist ((operand i 0) = p1);
613 insist ((operand i 1) = p2);
616 insist ((operand i 1) != p2);
617 insist ((operand i 1) = p3);
619 ignore (build_unreachable b)
622 (*===-- Aliases -----------------------------------------------------------===*)
624 let test_aliases () =
625 (* CHECK: @alias = alias i32* @aliasee
627 let forty_two32 = const_int i32_type 42 in
628 let v = define_global "aliasee" forty_two32 m in
629 ignore (add_alias m (pointer_type i32_type) v "alias")
632 (*===-- Functions ---------------------------------------------------------===*)
634 let test_functions () =
635 let ty = function_type i32_type [| i32_type; i64_type |] in
636 let ty2 = function_type i8_type [| i8_type; i64_type |] in
638 (* CHECK: declare i32 @Fn1(i32, i64)
640 begin group "declare";
641 insist (None = lookup_function "Fn1" m);
642 let fn = declare_function "Fn1" ty m in
643 insist (pointer_type ty = type_of fn);
644 insist (is_declaration fn);
645 insist (0 = Array.length (basic_blocks fn));
646 insist (pointer_type ty2 == type_of (declare_function "Fn1" ty2 m));
647 insist (fn == declare_function "Fn1" ty m);
648 insist (None <> lookup_function "Fn1" m);
649 insist (match lookup_function "Fn1" m with Some x -> x = fn
651 insist (m == global_parent fn)
654 (* CHECK-NOWHERE-NOT: Fn2
657 let fn = declare_function "Fn2" ty m in
660 (* CHECK: define{{.*}}Fn3
663 let fn = define_function "Fn3" ty m in
664 insist (not (is_declaration fn));
665 insist (1 = Array.length (basic_blocks fn));
666 ignore (build_unreachable (builder_at_end context (entry_block fn)));
668 (* CHECK: define{{.*}}Fn4{{.*}}Param1{{.*}}Param2
671 let fn = define_function "Fn4" ty m in
672 let params = params fn in
673 insist (2 = Array.length params);
674 insist (params.(0) = param fn 0);
675 insist (params.(1) = param fn 1);
676 insist (i32_type = type_of params.(0));
677 insist (i64_type = type_of params.(1));
678 set_value_name "Param1" params.(0);
679 set_value_name "Param2" params.(1);
680 ignore (build_unreachable (builder_at_end context (entry_block fn)));
682 (* CHECK: fastcc{{.*}}Fn5
685 let fn = define_function "Fn5" ty m in
686 insist (CallConv.c = function_call_conv fn);
687 set_function_call_conv CallConv.fast fn;
688 insist (CallConv.fast = function_call_conv fn);
689 ignore (build_unreachable (builder_at_end context (entry_block fn)));
692 (* CHECK: Fn6{{.*}}gc{{.*}}shadowstack
694 let fn = define_function "Fn6" ty m in
695 insist (None = gc fn);
696 set_gc (Some "ocaml") fn;
697 insist (Some "ocaml" = gc fn);
699 insist (None = gc fn);
700 set_gc (Some "shadowstack") fn;
701 ignore (build_unreachable (builder_at_end context (entry_block fn)));
704 begin group "iteration";
705 let m = create_module context "temp" in
707 insist (At_end m = function_begin m);
708 insist (At_start m = function_end m);
710 let f1 = define_function "One" ty m in
711 let f2 = define_function "Two" ty m in
713 insist (Before f1 = function_begin m);
714 insist (Before f2 = function_succ f1);
715 insist (At_end m = function_succ f2);
717 insist (After f2 = function_end m);
718 insist (After f1 = function_pred f2);
719 insist (At_start m = function_pred f1);
721 let lf s x = s ^ "->" ^ value_name x in
722 insist ("->One->Two" = fold_left_functions lf "" m);
724 let rf x s = value_name x ^ "<-" ^ s in
725 insist ("One<-Two<-" = fold_right_functions rf m "");
731 (*===-- Params ------------------------------------------------------------===*)
734 begin group "iteration";
735 let m = create_module context "temp" in
737 let vf = define_function "void" (function_type void_type [| |]) m in
739 insist (At_end vf = param_begin vf);
740 insist (At_start vf = param_end vf);
742 let ty = function_type void_type [| i32_type; i32_type |] in
743 let f = define_function "f" ty m in
744 let p1 = param f 0 in
745 let p2 = param f 1 in
746 set_value_name "One" p1;
747 set_value_name "Two" p2;
748 add_param_attr p1 Attribute.Sext;
749 add_param_attr p2 Attribute.Noalias;
750 remove_param_attr p2 Attribute.Noalias;
751 add_function_attr f Attribute.Nounwind;
752 add_function_attr f Attribute.Noreturn;
753 remove_function_attr f Attribute.Noreturn;
755 insist (Before p1 = param_begin f);
756 insist (Before p2 = param_succ p1);
757 insist (At_end f = param_succ p2);
759 insist (After p2 = param_end f);
760 insist (After p1 = param_pred p2);
761 insist (At_start f = param_pred p1);
763 let lf s x = s ^ "->" ^ value_name x in
764 insist ("->One->Two" = fold_left_params lf "" f);
766 let rf x s = value_name x ^ "<-" ^ s in
767 insist ("One<-Two<-" = fold_right_params rf f "");
773 (*===-- Basic Blocks ------------------------------------------------------===*)
775 let test_basic_blocks () =
776 let ty = function_type void_type [| |] in
781 let fn = declare_function "X" ty m in
782 let bb = append_block context "Bb1" fn in
783 insist (bb = entry_block fn);
784 ignore (build_unreachable (builder_at_end context bb));
786 (* CHECK-NOWHERE-NOT: Bb2
789 let fn = declare_function "X2" ty m in
790 let bb = append_block context "Bb2" fn in
794 let fn = declare_function "X3" ty m in
795 let bbb = append_block context "b" fn in
796 let bba = insert_block context "a" bbb in
797 insist ([| bba; bbb |] = basic_blocks fn);
798 ignore (build_unreachable (builder_at_end context bba));
799 ignore (build_unreachable (builder_at_end context bbb));
804 let fn = define_function "X4" ty m in
805 let bb = entry_block fn in
806 ignore (build_unreachable (builder_at_end context bb));
807 let bbv = value_of_block bb in
808 set_value_name "Bb3" bbv;
809 insist ("Bb3" = value_name bbv);
812 let fn = define_function "X5" ty m in
813 let bb = entry_block fn in
814 ignore (build_unreachable (builder_at_end context bb));
815 insist (bb = block_of_value (value_of_block bb));
816 insist (value_is_block (value_of_block bb));
817 insist (not (value_is_block (const_null i32_type)));
819 begin group "iteration";
820 let m = create_module context "temp" in
821 let f = declare_function "Temp" (function_type i32_type [| |]) m in
823 insist (At_end f = block_begin f);
824 insist (At_start f = block_end f);
826 let b1 = append_block context "One" f in
827 let b2 = append_block context "Two" f in
829 insist (Before b1 = block_begin f);
830 insist (Before b2 = block_succ b1);
831 insist (At_end f = block_succ b2);
833 insist (After b2 = block_end f);
834 insist (After b1 = block_pred b2);
835 insist (At_start f = block_pred b1);
837 let lf s x = s ^ "->" ^ value_name (value_of_block x) in
838 insist ("->One->Two" = fold_left_blocks lf "" f);
840 let rf x s = value_name (value_of_block x) ^ "<-" ^ s in
841 insist ("One<-Two<-" = fold_right_blocks rf f "");
847 (*===-- Instructions ------------------------------------------------------===*)
849 let test_instructions () =
850 begin group "iteration";
851 let m = create_module context "temp" in
852 let fty = function_type void_type [| i32_type; i32_type |] in
853 let f = define_function "f" fty m in
854 let bb = entry_block f in
855 let b = builder_at context (At_end bb) in
857 insist (At_end bb = instr_begin bb);
858 insist (At_start bb = instr_end bb);
860 let i1 = build_add (param f 0) (param f 1) "One" b in
861 let i2 = build_sub (param f 0) (param f 1) "Two" b in
863 insist (Before i1 = instr_begin bb);
864 insist (Before i2 = instr_succ i1);
865 insist (At_end bb = instr_succ i2);
867 insist (After i2 = instr_end bb);
868 insist (After i1 = instr_pred i2);
869 insist (At_start bb = instr_pred i1);
871 let lf s x = s ^ "->" ^ value_name x in
872 insist ("->One->Two" = fold_left_instrs lf "" bb);
874 let rf x s = value_name x ^ "<-" ^ s in
875 insist ("One<-Two<-" = fold_right_instrs rf bb "");
882 (* CHECK: %clone = add i32 %0, 2
884 let fty = function_type void_type [| i32_type |] in
885 let fn = define_function "BuilderParent" fty m in
886 let bb = entry_block fn in
887 let b = builder_at_end context bb in
888 let p = param fn 0 in
889 let sum = build_add p p "sum" b in
890 let y = const_int i32_type 2 in
891 let clone = instr_clone sum in
892 set_operand clone 0 p;
893 set_operand clone 1 y;
894 insert_into_builder clone "clone" b;
895 ignore (build_ret_void b)
899 (*===-- Builder -----------------------------------------------------------===*)
901 let test_builder () =
902 let (++) x f = f x; x in
904 begin group "parent";
906 ignore (insertion_block (builder context));
911 let fty = function_type void_type [| i32_type |] in
912 let fn = define_function "BuilderParent" fty m in
913 let bb = entry_block fn in
914 let b = builder_at_end context bb in
915 let p = param fn 0 in
916 let sum = build_add p p "sum" b in
917 ignore (build_ret_void b);
919 insist (fn = block_parent bb);
920 insist (fn = param_parent p);
921 insist (bb = instr_parent sum);
922 insist (bb = insertion_block b)
929 let fty = function_type void_type [| |] in
930 let fn = declare_function "X6" fty m in
931 let b = builder_at_end context (append_block context "Bb01" fn) in
932 ignore (build_ret_void b)
935 group "ret aggregate";
937 (* CHECK: ret { i8, i64 } { i8 4, i64 5 }
939 let sty = struct_type context [| i8_type; i64_type |] in
940 let fty = function_type sty [| |] in
941 let fn = declare_function "XA6" fty m in
942 let b = builder_at_end context (append_block context "Bb01" fn) in
943 let agg = [| const_int i8_type 4; const_int i64_type 5 |] in
944 ignore (build_aggregate_ret agg b)
947 (* The rest of the tests will use one big function. *)
948 let fty = function_type i32_type [| i32_type; i32_type |] in
949 let fn = define_function "X7" fty m in
950 let atentry = builder_at_end context (entry_block fn) in
951 let p1 = param fn 0 ++ set_value_name "P1" in
952 let p2 = param fn 1 ++ set_value_name "P2" in
953 let f1 = build_uitofp p1 float_type "F1" atentry in
954 let f2 = build_uitofp p2 float_type "F2" atentry in
956 let bb00 = append_block context "Bb00" fn in
957 ignore (build_unreachable (builder_at_end context bb00));
959 group "function attribute";
961 ignore (add_function_attr fn Attribute.UWTable);
963 * #0 is uwtable, defined at EOF.
965 insist ([Attribute.UWTable] = function_attr fn);
969 let void_ptr = pointer_type i8_type in
971 (* CHECK-DAG: %build_trunc = trunc i32 %P1 to i8
972 * CHECK-DAG: %build_trunc2 = trunc i32 %P1 to i8
973 * CHECK-DAG: %build_trunc3 = trunc i32 %P1 to i8
974 * CHECK-DAG: %build_zext = zext i8 %build_trunc to i32
975 * CHECK-DAG: %build_zext2 = zext i8 %build_trunc to i32
976 * CHECK-DAG: %build_sext = sext i32 %build_zext to i64
977 * CHECK-DAG: %build_sext2 = sext i32 %build_zext to i64
978 * CHECK-DAG: %build_sext3 = sext i32 %build_zext to i64
979 * CHECK-DAG: %build_uitofp = uitofp i64 %build_sext to float
980 * CHECK-DAG: %build_sitofp = sitofp i32 %build_zext to double
981 * CHECK-DAG: %build_fptoui = fptoui float %build_uitofp to i32
982 * CHECK-DAG: %build_fptosi = fptosi double %build_sitofp to i64
983 * CHECK-DAG: %build_fptrunc = fptrunc double %build_sitofp to float
984 * CHECK-DAG: %build_fptrunc2 = fptrunc double %build_sitofp to float
985 * CHECK-DAG: %build_fpext = fpext float %build_fptrunc to double
986 * CHECK-DAG: %build_fpext2 = fpext float %build_fptrunc to double
987 * CHECK-DAG: %build_inttoptr = inttoptr i32 %P1 to i8*
988 * CHECK-DAG: %build_ptrtoint = ptrtoint i8* %build_inttoptr to i64
989 * CHECK-DAG: %build_ptrtoint2 = ptrtoint i8* %build_inttoptr to i64
990 * CHECK-DAG: %build_bitcast = bitcast i64 %build_ptrtoint to double
991 * CHECK-DAG: %build_bitcast2 = bitcast i64 %build_ptrtoint to double
992 * CHECK-DAG: %build_bitcast3 = bitcast i64 %build_ptrtoint to double
993 * CHECK-DAG: %build_bitcast4 = bitcast i64 %build_ptrtoint to double
994 * CHECK-DAG: %build_pointercast = bitcast i8* %build_inttoptr to i16*
996 let inst28 = build_trunc p1 i8_type "build_trunc" atentry in
997 let inst29 = build_zext inst28 i32_type "build_zext" atentry in
998 let inst30 = build_sext inst29 i64_type "build_sext" atentry in
999 let inst31 = build_uitofp inst30 float_type "build_uitofp" atentry in
1000 let inst32 = build_sitofp inst29 double_type "build_sitofp" atentry in
1001 ignore(build_fptoui inst31 i32_type "build_fptoui" atentry);
1002 ignore(build_fptosi inst32 i64_type "build_fptosi" atentry);
1003 let inst35 = build_fptrunc inst32 float_type "build_fptrunc" atentry in
1004 ignore(build_fpext inst35 double_type "build_fpext" atentry);
1005 let inst37 = build_inttoptr p1 void_ptr "build_inttoptr" atentry in
1006 let inst38 = build_ptrtoint inst37 i64_type "build_ptrtoint" atentry in
1007 ignore(build_bitcast inst38 double_type "build_bitcast" atentry);
1008 ignore(build_zext_or_bitcast inst38 double_type "build_bitcast2" atentry);
1009 ignore(build_sext_or_bitcast inst38 double_type "build_bitcast3" atentry);
1010 ignore(build_trunc_or_bitcast inst38 double_type "build_bitcast4" atentry);
1011 ignore(build_pointercast inst37 (pointer_type i16_type) "build_pointercast" atentry);
1013 ignore(build_zext_or_bitcast inst28 i32_type "build_zext2" atentry);
1014 ignore(build_sext_or_bitcast inst29 i64_type "build_sext2" atentry);
1015 ignore(build_trunc_or_bitcast p1 i8_type "build_trunc2" atentry);
1016 ignore(build_pointercast inst37 i64_type "build_ptrtoint2" atentry);
1017 ignore(build_intcast inst29 i64_type "build_sext3" atentry);
1018 ignore(build_intcast p1 i8_type "build_trunc3" atentry);
1019 ignore(build_fpcast inst35 double_type "build_fpext2" atentry);
1020 ignore(build_fpcast inst32 float_type "build_fptrunc2" atentry);
1023 group "comparisons"; begin
1024 (* CHECK: %build_icmp_ne = icmp ne i32 %P1, %P2
1025 * CHECK: %build_icmp_sle = icmp sle i32 %P2, %P1
1026 * CHECK: %build_fcmp_false = fcmp false float %F1, %F2
1027 * CHECK: %build_fcmp_true = fcmp true float %F2, %F1
1028 * CHECK: %build_is_null{{.*}}= icmp eq{{.*}}%X0,{{.*}}null
1029 * CHECK: %build_is_not_null = icmp ne i8* %X1, null
1030 * CHECK: %build_ptrdiff
1032 let c = build_icmp Icmp.Ne p1 p2 "build_icmp_ne" atentry in
1033 insist (Some Icmp.Ne = icmp_predicate c);
1034 insist (None = fcmp_predicate c);
1036 let c = build_icmp Icmp.Sle p2 p1 "build_icmp_sle" atentry in
1037 insist (Some Icmp.Sle = icmp_predicate c);
1038 insist (None = fcmp_predicate c);
1040 let c = build_fcmp Fcmp.False f1 f2 "build_fcmp_false" atentry in
1041 (* insist (Some Fcmp.False = fcmp_predicate c); *)
1042 insist (None = icmp_predicate c);
1044 let c = build_fcmp Fcmp.True f2 f1 "build_fcmp_true" atentry in
1045 (* insist (Some Fcmp.True = fcmp_predicate c); *)
1046 insist (None = icmp_predicate c);
1048 let g0 = declare_global (pointer_type i8_type) "g0" m in
1049 let g1 = declare_global (pointer_type i8_type) "g1" m in
1050 let p0 = build_load g0 "X0" atentry in
1051 let p1 = build_load g1 "X1" atentry in
1052 ignore (build_is_null p0 "build_is_null" atentry);
1053 ignore (build_is_not_null p1 "build_is_not_null" atentry);
1054 ignore (build_ptrdiff p1 p0 "build_ptrdiff" atentry);
1057 group "miscellaneous"; begin
1058 (* CHECK: %build_call = tail call cc63 i32 @{{.*}}(i32 signext %P2, i32 %P1)
1059 * CHECK: %build_select = select i1 %build_icmp, i32 %P1, i32 %P2
1060 * CHECK: %build_va_arg = va_arg i8** null, i32
1061 * CHECK: %build_extractelement = extractelement <4 x i32> %Vec1, i32 %P2
1062 * CHECK: %build_insertelement = insertelement <4 x i32> %Vec1, i32 %P1, i32 %P2
1063 * CHECK: %build_shufflevector = shufflevector <4 x i32> %Vec1, <4 x i32> %Vec2, <4 x i32> <i32 1, i32 1, i32 0, i32 0>
1064 * CHECK: %build_insertvalue0 = insertvalue{{.*}}%bl, i32 1, 0
1065 * CHECK: %build_extractvalue = extractvalue{{.*}}%build_insertvalue1, 1
1067 let ci = build_call fn [| p2; p1 |] "build_call" atentry in
1068 insist (CallConv.c = instruction_call_conv ci);
1069 set_instruction_call_conv 63 ci;
1070 insist (63 = instruction_call_conv ci);
1071 insist (not (is_tail_call ci));
1072 set_tail_call true ci;
1073 insist (is_tail_call ci);
1074 add_instruction_param_attr ci 1 Attribute.Sext;
1075 add_instruction_param_attr ci 2 Attribute.Noalias;
1076 remove_instruction_param_attr ci 2 Attribute.Noalias;
1078 let inst46 = build_icmp Icmp.Eq p1 p2 "build_icmp" atentry in
1079 ignore (build_select inst46 p1 p2 "build_select" atentry);
1080 ignore (build_va_arg
1081 (const_null (pointer_type (pointer_type i8_type)))
1082 i32_type "build_va_arg" atentry);
1084 (* Set up some vector vregs. *)
1085 let one = const_int i32_type 1 in
1086 let zero = const_int i32_type 0 in
1087 let t1 = const_vector [| one; zero; one; zero |] in
1088 let t2 = const_vector [| zero; one; zero; one |] in
1089 let t3 = const_vector [| one; one; zero; zero |] in
1090 let vec1 = build_insertelement t1 p1 p2 "Vec1" atentry in
1091 let vec2 = build_insertelement t2 p1 p2 "Vec2" atentry in
1092 let sty = struct_type context [| i32_type; i8_type |] in
1094 ignore (build_extractelement vec1 p2 "build_extractelement" atentry);
1095 ignore (build_insertelement vec1 p1 p2 "build_insertelement" atentry);
1096 ignore (build_shufflevector vec1 vec2 t3 "build_shufflevector" atentry);
1098 let p = build_alloca sty "ba" atentry in
1099 let agg = build_load p "bl" atentry in
1100 let agg0 = build_insertvalue agg (const_int i32_type 1) 0
1101 "build_insertvalue0" atentry in
1102 let agg1 = build_insertvalue agg0 (const_int i8_type 2) 1
1103 "build_insertvalue1" atentry in
1104 ignore (build_extractvalue agg1 1 "build_extractvalue" atentry)
1107 group "metadata"; begin
1108 (* CHECK: %metadata = add i32 %P1, %P2, !test !1
1109 * !1 is metadata emitted at EOF.
1111 let i = build_add p1 p2 "metadata" atentry in
1112 insist ((has_metadata i) = false);
1114 let m1 = const_int i32_type 1 in
1115 let m2 = mdstring context "metadata test" in
1116 let md = mdnode context [| m1; m2 |] in
1118 let kind = mdkind_id context "test" in
1119 set_metadata i kind md;
1121 insist ((has_metadata i) = true);
1122 insist ((metadata i kind) = Some md);
1124 clear_metadata i kind;
1126 insist ((has_metadata i) = false);
1127 insist ((metadata i kind) = None);
1129 set_metadata i kind md
1132 group "named metadata"; begin
1133 (* !llvm.module.flags is emitted at EOF. *)
1134 let n1 = const_int i32_type 1 in
1135 let n2 = mdstring context "Debug Info Version" in
1136 let n3 = const_int i32_type 2 in
1137 let md = mdnode context [| n1; n2; n3 |] in
1138 add_named_metadata_operand m "llvm.module.flags" md;
1140 insist ((get_named_metadata m "llvm.module.flags") = [| md |])
1144 (* CHECK: %dbg = add i32 %P1, %P2, !dbg !2
1145 * !2 is metadata emitted at EOF.
1147 insist ((current_debug_location atentry) = None);
1149 let m_line = const_int i32_type 2 in
1150 let m_col = const_int i32_type 3 in
1151 let m_scope = mdnode context [| |] in
1152 let m_inlined = mdnode context [| |] in
1153 let md = mdnode context [| m_line; m_col; m_scope; m_inlined |] in
1154 set_current_debug_location atentry md;
1156 insist ((current_debug_location atentry) = Some md);
1158 let i = build_add p1 p2 "dbg" atentry in
1159 insist ((has_metadata i) = true);
1161 clear_current_debug_location atentry
1165 (* CHECK: ret{{.*}}P1
1167 let ret = build_ret p1 atentry in
1168 position_before ret atentry
1171 (* see test/Feature/exception.ll *)
1172 let bblpad = append_block context "Bblpad" fn in
1173 let rt = struct_type context [| pointer_type i8_type; i32_type |] in
1174 let ft = var_arg_function_type i32_type [||] in
1175 let personality = declare_function "__gxx_personality_v0" ft m in
1176 let ztic = declare_global (pointer_type i8_type) "_ZTIc" m in
1177 let ztid = declare_global (pointer_type i8_type) "_ZTId" m in
1178 let ztipkc = declare_global (pointer_type i8_type) "_ZTIPKc" m in
1180 set_global_constant true ztic;
1181 set_global_constant true ztid;
1182 set_global_constant true ztipkc;
1183 let lp = build_landingpad rt personality 0 "lpad"
1184 (builder_at_end context bblpad) in begin
1185 set_cleanup lp true;
1187 insist((pointer_type (pointer_type i8_type)) = type_of ztid);
1188 let ety = pointer_type (pointer_type i8_type) in
1189 add_clause lp (const_array ety [| ztipkc; ztid |]);
1190 ignore (build_resume lp (builder_at_end context bblpad));
1192 (* CHECK: landingpad{{.*}}personality{{.*}}__gxx_personality_v0
1194 * CHECK: catch{{.*}}i8**{{.*}}@_ZTIc
1195 * CHECK: filter{{.*}}@_ZTIPKc{{.*}}@_ZTId
1201 (* CHECK: br{{.*}}Bb02
1203 let bb02 = append_block context "Bb02" fn in
1204 let b = builder_at_end context bb02 in
1205 let br = build_br bb02 b in
1206 insist (successors br = [| bb02 |]) ;
1207 insist (is_conditional br = false) ;
1208 insist (get_branch br = Some (`Unconditional bb02)) ;
1211 group "cond_br"; begin
1212 (* CHECK: br{{.*}}build_br{{.*}}Bb03{{.*}}Bb00
1214 let bb03 = append_block context "Bb03" fn in
1215 let b = builder_at_end context bb03 in
1216 let cond = build_trunc p1 i1_type "build_br" b in
1217 let br = build_cond_br cond bb03 bb00 b in
1218 insist (num_successors br = 2) ;
1219 insist (successor br 0 = bb03) ;
1220 insist (successor br 1 = bb00) ;
1221 insist (is_conditional br = true) ;
1222 insist (get_branch br = Some (`Conditional (cond, bb03, bb00))) ;
1225 group "switch"; begin
1226 (* CHECK: switch{{.*}}P1{{.*}}SwiBlock3
1227 * CHECK: 2,{{.*}}SwiBlock2
1229 let bb1 = append_block context "SwiBlock1" fn in
1230 let bb2 = append_block context "SwiBlock2" fn in
1231 ignore (build_unreachable (builder_at_end context bb2));
1232 let bb3 = append_block context "SwiBlock3" fn in
1233 ignore (build_unreachable (builder_at_end context bb3));
1234 let si = build_switch p1 bb3 1 (builder_at_end context bb1) in begin
1235 ignore (add_case si (const_int i32_type 2) bb2);
1236 insist (switch_default_dest si = bb3);
1238 insist (num_successors si = 2) ;
1239 insist (get_branch si = None) ;
1242 group "malloc/free"; begin
1243 (* CHECK: call{{.*}}@malloc(i32 ptrtoint
1244 * CHECK: call{{.*}}@free(i8*
1245 * CHECK: call{{.*}}@malloc(i32 %
1247 let bb1 = append_block context "MallocBlock1" fn in
1248 let m1 = (build_malloc (pointer_type i32_type) "m1"
1249 (builder_at_end context bb1)) in
1250 ignore (build_free m1 (builder_at_end context bb1));
1251 ignore (build_array_malloc i32_type p1 "m2" (builder_at_end context bb1));
1252 ignore (build_unreachable (builder_at_end context bb1));
1255 group "indirectbr"; begin
1256 (* CHECK: indirectbr i8* blockaddress(@X7, %IBRBlock2), [label %IBRBlock2, label %IBRBlock3]
1258 let bb1 = append_block context "IBRBlock1" fn in
1260 let bb2 = append_block context "IBRBlock2" fn in
1261 ignore (build_unreachable (builder_at_end context bb2));
1263 let bb3 = append_block context "IBRBlock3" fn in
1264 ignore (build_unreachable (builder_at_end context bb3));
1266 let addr = block_address fn bb2 in
1267 let ibr = build_indirect_br addr 2 (builder_at_end context bb1) in
1268 ignore (add_destination ibr bb2);
1269 ignore (add_destination ibr bb3)
1272 group "invoke"; begin
1273 (* CHECK: build_invoke{{.*}}invoke{{.*}}P1{{.*}}P2
1274 * CHECK: to{{.*}}Bb04{{.*}}unwind{{.*}}Bblpad
1276 let bb04 = append_block context "Bb04" fn in
1277 let b = builder_at_end context bb04 in
1278 ignore (build_invoke fn [| p1; p2 |] bb04 bblpad "build_invoke" b)
1281 group "unreachable"; begin
1282 (* CHECK: unreachable
1284 let bb06 = append_block context "Bb06" fn in
1285 let b = builder_at_end context bb06 in
1286 ignore (build_unreachable b)
1289 group "arithmetic"; begin
1290 let bb07 = append_block context "Bb07" fn in
1291 let b = builder_at_end context bb07 in
1293 (* CHECK: %build_add = add i32 %P1, %P2
1294 * CHECK: %build_nsw_add = add nsw i32 %P1, %P2
1295 * CHECK: %build_nuw_add = add nuw i32 %P1, %P2
1296 * CHECK: %build_fadd = fadd float %F1, %F2
1297 * CHECK: %build_sub = sub i32 %P1, %P2
1298 * CHECK: %build_nsw_sub = sub nsw i32 %P1, %P2
1299 * CHECK: %build_nuw_sub = sub nuw i32 %P1, %P2
1300 * CHECK: %build_fsub = fsub float %F1, %F2
1301 * CHECK: %build_mul = mul i32 %P1, %P2
1302 * CHECK: %build_nsw_mul = mul nsw i32 %P1, %P2
1303 * CHECK: %build_nuw_mul = mul nuw i32 %P1, %P2
1304 * CHECK: %build_fmul = fmul float %F1, %F2
1305 * CHECK: %build_udiv = udiv i32 %P1, %P2
1306 * CHECK: %build_sdiv = sdiv i32 %P1, %P2
1307 * CHECK: %build_exact_sdiv = sdiv exact i32 %P1, %P2
1308 * CHECK: %build_fdiv = fdiv float %F1, %F2
1309 * CHECK: %build_urem = urem i32 %P1, %P2
1310 * CHECK: %build_srem = srem i32 %P1, %P2
1311 * CHECK: %build_frem = frem float %F1, %F2
1312 * CHECK: %build_shl = shl i32 %P1, %P2
1313 * CHECK: %build_lshl = lshr i32 %P1, %P2
1314 * CHECK: %build_ashl = ashr i32 %P1, %P2
1315 * CHECK: %build_and = and i32 %P1, %P2
1316 * CHECK: %build_or = or i32 %P1, %P2
1317 * CHECK: %build_xor = xor i32 %P1, %P2
1318 * CHECK: %build_neg = sub i32 0, %P1
1319 * CHECK: %build_nsw_neg = sub nsw i32 0, %P1
1320 * CHECK: %build_nuw_neg = sub nuw i32 0, %P1
1321 * CHECK: %build_fneg = fsub float {{.*}}0{{.*}}, %F1
1322 * CHECK: %build_not = xor i32 %P1, -1
1324 ignore (build_add p1 p2 "build_add" b);
1325 ignore (build_nsw_add p1 p2 "build_nsw_add" b);
1326 ignore (build_nuw_add p1 p2 "build_nuw_add" b);
1327 ignore (build_fadd f1 f2 "build_fadd" b);
1328 ignore (build_sub p1 p2 "build_sub" b);
1329 ignore (build_nsw_sub p1 p2 "build_nsw_sub" b);
1330 ignore (build_nuw_sub p1 p2 "build_nuw_sub" b);
1331 ignore (build_fsub f1 f2 "build_fsub" b);
1332 ignore (build_mul p1 p2 "build_mul" b);
1333 ignore (build_nsw_mul p1 p2 "build_nsw_mul" b);
1334 ignore (build_nuw_mul p1 p2 "build_nuw_mul" b);
1335 ignore (build_fmul f1 f2 "build_fmul" b);
1336 ignore (build_udiv p1 p2 "build_udiv" b);
1337 ignore (build_sdiv p1 p2 "build_sdiv" b);
1338 ignore (build_exact_sdiv p1 p2 "build_exact_sdiv" b);
1339 ignore (build_fdiv f1 f2 "build_fdiv" b);
1340 ignore (build_urem p1 p2 "build_urem" b);
1341 ignore (build_srem p1 p2 "build_srem" b);
1342 ignore (build_frem f1 f2 "build_frem" b);
1343 ignore (build_shl p1 p2 "build_shl" b);
1344 ignore (build_lshr p1 p2 "build_lshl" b);
1345 ignore (build_ashr p1 p2 "build_ashl" b);
1346 ignore (build_and p1 p2 "build_and" b);
1347 ignore (build_or p1 p2 "build_or" b);
1348 ignore (build_xor p1 p2 "build_xor" b);
1349 ignore (build_neg p1 "build_neg" b);
1350 ignore (build_nsw_neg p1 "build_nsw_neg" b);
1351 ignore (build_nuw_neg p1 "build_nuw_neg" b);
1352 ignore (build_fneg f1 "build_fneg" b);
1353 ignore (build_not p1 "build_not" b);
1354 ignore (build_unreachable b)
1357 group "memory"; begin
1358 let bb08 = append_block context "Bb08" fn in
1359 let b = builder_at_end context bb08 in
1361 (* CHECK: %build_alloca = alloca i32
1362 * CHECK: %build_array_alloca = alloca i32, i32 %P2
1363 * CHECK: %build_load = load volatile i32* %build_array_alloca, align 4
1364 * CHECK: store volatile i32 %P2, i32* %build_alloca, align 4
1365 * CHECK: %build_gep = getelementptr i32* %build_array_alloca, i32 %P2
1366 * CHECK: %build_in_bounds_gep = getelementptr inbounds i32* %build_array_alloca, i32 %P2
1367 * CHECK: %build_struct_gep = getelementptr inbounds{{.*}}%build_alloca2, i32 0, i32 1
1368 * CHECK: %build_atomicrmw = atomicrmw xchg i8* %p, i8 42 seq_cst
1370 let alloca = build_alloca i32_type "build_alloca" b in
1371 let array_alloca = build_array_alloca i32_type p2 "build_array_alloca" b in
1373 let load = build_load array_alloca "build_load" b in
1374 ignore(set_alignment 4 load);
1375 ignore(set_volatile true load);
1376 insist(true = is_volatile load);
1377 insist(4 = alignment load);
1379 let store = build_store p2 alloca b in
1380 ignore(set_volatile true store);
1381 ignore(set_alignment 4 store);
1382 insist(true = is_volatile store);
1383 insist(4 = alignment store);
1384 ignore(build_gep array_alloca [| p2 |] "build_gep" b);
1385 ignore(build_in_bounds_gep array_alloca [| p2 |] "build_in_bounds_gep" b);
1387 let sty = struct_type context [| i32_type; i8_type |] in
1388 let alloca2 = build_alloca sty "build_alloca2" b in
1389 ignore(build_struct_gep alloca2 1 "build_struct_gep" b);
1391 let p = build_alloca i8_type "p" b in
1392 ignore(build_atomicrmw AtomicRMWBinOp.Xchg p (const_int i8_type 42)
1393 AtomicOrdering.SequentiallyConsistent false "build_atomicrmw"
1396 ignore(build_unreachable b)
1399 group "string"; begin
1400 let bb09 = append_block context "Bb09" fn in
1401 let b = builder_at_end context bb09 in
1402 let p = build_alloca (pointer_type i8_type) "p" b in
1403 (* build_global_string is emitted above.
1404 * CHECK: store{{.*}}build_global_string1{{.*}}p
1406 ignore (build_global_string "stringval" "build_global_string" b);
1407 let g = build_global_stringptr "stringval" "build_global_string1" b in
1408 ignore (build_store g p b);
1409 ignore(build_unreachable b);
1413 (* CHECK: PhiNode{{.*}}P1{{.*}}PhiBlock1{{.*}}P2{{.*}}PhiBlock2
1415 let b1 = append_block context "PhiBlock1" fn in
1416 let b2 = append_block context "PhiBlock2" fn in
1418 let jb = append_block context "PhiJoinBlock" fn in
1419 ignore (build_br jb (builder_at_end context b1));
1420 ignore (build_br jb (builder_at_end context b2));
1421 let at_jb = builder_at_end context jb in
1423 let phi = build_phi [(p1, b1)] "PhiNode" at_jb in
1424 insist ([(p1, b1)] = incoming phi);
1426 add_incoming (p2, b2) phi;
1427 insist ([(p1, b1); (p2, b2)] = incoming phi);
1429 ignore (build_unreachable at_jb);
1432 (* End-of-file checks for things like metdata and attributes.
1433 * CHECK: attributes #0 = {{.*}}uwtable{{.*}}
1434 * CHECK: !llvm.module.flags = !{!0}
1435 * CHECK: !0 = metadata !{i32 1, metadata !"Debug Info Version", i32 2}
1436 * CHECK: !1 = metadata !{i32 1, metadata !"metadata test"}
1437 * CHECK: !2 = metadata !{i32 2, i32 3, metadata !3, metadata !3}
1440 (*===-- Pass Managers -----------------------------------------------------===*)
1442 let test_pass_manager () =
1443 let (++) x f = ignore (f x); x in
1445 begin group "module pass manager";
1446 ignore (PassManager.create ()
1447 ++ PassManager.run_module m
1448 ++ PassManager.dispose)
1451 begin group "function pass manager";
1452 let fty = function_type void_type [| |] in
1453 let fn = define_function "FunctionPassManager" fty m in
1454 ignore (build_ret_void (builder_at_end context (entry_block fn)));
1456 ignore (PassManager.create_function m
1457 ++ PassManager.initialize
1458 ++ PassManager.run_function fn
1459 ++ PassManager.finalize
1460 ++ PassManager.dispose)
1464 (*===-- Memory Buffer -----------------------------------------------------===*)
1466 let test_memory_buffer () =
1467 group "memory buffer";
1468 let buf = MemoryBuffer.of_string "foobar" in
1469 insist ((MemoryBuffer.as_string buf) = "foobar")
1472 (*===-- Writer ------------------------------------------------------------===*)
1474 let test_writer () =
1476 insist (match Llvm_analysis.verify_module m with
1478 | Some msg -> prerr_string msg; false);
1481 insist (write_bitcode_file m filename);
1486 (*===-- Driver ------------------------------------------------------------===*)
1489 suite "conversion" test_conversion;
1490 suite "target" test_target;
1491 suite "constants" test_constants;
1492 suite "global values" test_global_values;
1493 suite "global variables" test_global_variables;
1494 suite "uses" test_uses;
1495 suite "users" test_users;
1496 suite "aliases" test_aliases;
1497 suite "functions" test_functions;
1498 suite "params" test_params;
1499 suite "basic blocks" test_basic_blocks;
1500 suite "instructions" test_instructions;
1501 suite "builder" test_builder;
1502 suite "pass manager" test_pass_manager;
1503 suite "memory buffer" test_memory_buffer;
1504 suite "writer" test_writer; (* Keep this last; it disposes m. *)