1 (* RUN: rm -rf %t.builddir
2 * RUN: mkdir -p %t.builddir
3 * RUN: cp %s %t.builddir
4 * RUN: %ocamlopt -warn-error A llvm.cmxa llvm_analysis.cmxa llvm_bitwriter.cmxa %t.builddir/vmcore.ml -o %t
6 * RUN: llvm-dis < %t.bc > %t.ll
7 * RUN: FileCheck %s < %t.ll
8 * Do a second pass for things that shouldn't be anywhere.
9 * RUN: FileCheck -check-prefix=CHECK-NOWHERE %s < %t.ll
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
21 (* Tiny unit test framework - really just to help find which line is busted *)
22 let exit_status = ref 0
23 let suite_name = ref ""
24 let group_name = ref ""
26 let print_checkpoints = false
27 let context = global_context ()
28 let i1_type = Llvm.i1_type context
29 let i8_type = Llvm.i8_type context
30 let i16_type = Llvm.i16_type context
31 let i32_type = Llvm.i32_type context
32 let i64_type = Llvm.i64_type context
33 let void_type = Llvm.void_type context
34 let float_type = Llvm.float_type context
35 let double_type = Llvm.double_type context
36 let fp128_type = Llvm.fp128_type context
39 group_name := !suite_name ^ "/" ^ name;
41 if print_checkpoints then
42 prerr_endline (" " ^ name ^ "...")
48 match print_checkpoints, cond with
51 prerr_endline ("FAILED: " ^ !suite_name ^ "/" ^ !group_name ^ " #" ^ (string_of_int !case_num))
53 prerr_endline (" " ^ (string_of_int !case_num))
55 prerr_endline (" " ^ (string_of_int !case_num) ^ " FAIL")
59 if print_checkpoints then
60 prerr_endline (name ^ ":");
64 (*===-- Fixture -----------------------------------------------------------===*)
66 let filename = Sys.argv.(1)
67 let m = create_module context filename
70 (*===-- Conversion --------------------------------------------------------===*)
72 let test_conversion () =
73 insist ("i32" = (string_of_lltype i32_type));
74 let c = const_int i32_type 42 in
75 insist ("i32 42" = (string_of_llvalue c))
78 (*===-- Target ------------------------------------------------------------===*)
82 let trip = "i686-apple-darwin8" in
83 set_target_triple trip m;
84 insist (trip = target_triple m)
89 set_data_layout layout m;
90 insist (layout = data_layout m)
92 (* CHECK: target datalayout = "e"
93 * CHECK: target triple = "i686-apple-darwin8"
97 (*===-- Constants ---------------------------------------------------------===*)
99 let test_constants () =
100 (* CHECK: const_int{{.*}}i32{{.*}}-1
103 let c = const_int i32_type (-1) in
104 ignore (define_global "const_int" c m);
105 insist (i32_type = type_of c);
106 insist (is_constant c);
108 (* CHECK: const_sext_int{{.*}}i64{{.*}}-1
111 let c = const_int i64_type (-1) in
112 ignore (define_global "const_sext_int" c m);
113 insist (i64_type = type_of 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);
122 (* CHECK: const_int_string{{.*}}i32{{.*}}-1
125 let c = const_int_of_string i32_type "-1" 10 in
126 ignore (define_global "const_int_string" c m);
127 insist (i32_type = type_of c);
128 insist (None = (string_of_const c));
130 if Sys.word_size = 64; then begin
132 let c = const_int i64_type (1 lsl 61) in
133 insist (c = const_of_int64 i64_type (Int64.of_int (1 lsl 61)) false)
136 (* CHECK: @const_string = global {{.*}}c"cruel\00world"
139 let c = const_string context "cruel\000world" in
140 ignore (define_global "const_string" c m);
141 insist ((array_type i8_type 11) = type_of c);
142 insist ((Some "cruel\000world") = (string_of_const c));
144 (* CHECK: const_stringz{{.*}}"hi\00again\00"
147 let c = const_stringz context "hi\000again" in
148 ignore (define_global "const_stringz" c m);
149 insist ((array_type i8_type 9) = type_of c);
151 (* CHECK: const_single{{.*}}2.75
152 * CHECK: const_double{{.*}}3.1459
153 * CHECK: const_double_string{{.*}}1.25
156 let cs = const_float float_type 2.75 in
157 ignore (define_global "const_single" cs m);
158 insist (float_type = type_of cs);
160 let cd = const_float double_type 3.1459 in
161 ignore (define_global "const_double" cd m);
162 insist (double_type = type_of cd);
164 let cd = const_float_of_string double_type "1.25" in
165 ignore (define_global "const_double_string" cd m);
166 insist (double_type = type_of cd)
169 let one = const_int i16_type 1 in
170 let two = const_int i16_type 2 in
171 let three = const_int i32_type 3 in
172 let four = const_int i32_type 4 in
174 (* CHECK: const_array{{.*}}[i32 3, i32 4]
177 let c = const_array i32_type [| three; four |] in
178 ignore (define_global "const_array" c m);
179 insist ((array_type i32_type 2) = (type_of c));
180 insist (three = (const_element c 0));
181 insist (four = (const_element c 1));
183 (* CHECK: const_vector{{.*}}<i16 1, i16 2{{.*}}>
186 let c = const_vector [| one; two; one; two;
187 one; two; one; two |] in
188 ignore (define_global "const_vector" c m);
189 insist ((vector_type i16_type 8) = (type_of c));
191 (* CHECK: const_structure{{.*.}}i16 1, i16 2, i32 3, i32 4
194 let c = const_struct context [| one; two; three; four |] in
195 ignore (define_global "const_structure" c m);
196 insist ((struct_type context [| i16_type; i16_type; i32_type; i32_type |])
199 (* CHECK: const_null{{.*}}zeroinit
202 let c = const_null (packed_struct_type context [| i1_type; i8_type; i64_type;
204 ignore (define_global "const_null" c m);
206 (* CHECK: const_all_ones{{.*}}-1
209 let c = const_all_ones i64_type in
210 ignore (define_global "const_all_ones" c m);
212 group "pointer null"; begin
213 (* CHECK: const_pointer_null = global i64* null
215 let c = const_pointer_null (pointer_type i64_type) in
216 ignore (define_global "const_pointer_null" c m);
219 (* CHECK: const_undef{{.*}}undef
222 let c = undef i1_type in
223 ignore (define_global "const_undef" c m);
224 insist (i1_type = type_of c);
227 group "constant arithmetic";
228 (* CHECK: @const_neg = global i64 sub
229 * CHECK: @const_nsw_neg = global i64 sub nsw
230 * CHECK: @const_nuw_neg = global i64 sub nuw
231 * CHECK: @const_fneg = global double fsub
232 * CHECK: @const_not = global i64 xor
233 * CHECK: @const_add = global i64 add
234 * CHECK: @const_nsw_add = global i64 add nsw
235 * CHECK: @const_nuw_add = global i64 add nuw
236 * CHECK: @const_fadd = global double fadd
237 * CHECK: @const_sub = global i64 sub
238 * CHECK: @const_nsw_sub = global i64 sub nsw
239 * CHECK: @const_nuw_sub = global i64 sub nuw
240 * CHECK: @const_fsub = global double fsub
241 * CHECK: @const_mul = global i64 mul
242 * CHECK: @const_nsw_mul = global i64 mul nsw
243 * CHECK: @const_nuw_mul = global i64 mul nuw
244 * CHECK: @const_fmul = global double fmul
245 * CHECK: @const_udiv = global i64 udiv
246 * CHECK: @const_sdiv = global i64 sdiv
247 * CHECK: @const_exact_sdiv = global i64 sdiv exact
248 * CHECK: @const_fdiv = global double fdiv
249 * CHECK: @const_urem = global i64 urem
250 * CHECK: @const_srem = global i64 srem
251 * CHECK: @const_frem = global double frem
252 * CHECK: @const_and = global i64 and
253 * CHECK: @const_or = global i64 or
254 * CHECK: @const_xor = global i64 xor
255 * CHECK: @const_icmp = global i1 icmp sle
256 * CHECK: @const_fcmp = global i1 fcmp ole
258 let void_ptr = pointer_type i8_type in
259 let five = const_int i64_type 5 in
260 let ffive = const_uitofp five double_type in
261 let foldbomb_gv = define_global "FoldBomb" (const_null i8_type) m in
262 let foldbomb = const_ptrtoint foldbomb_gv i64_type in
263 let ffoldbomb = const_uitofp foldbomb double_type in
264 ignore (define_global "const_neg" (const_neg foldbomb) m);
265 ignore (define_global "const_nsw_neg" (const_nsw_neg foldbomb) m);
266 ignore (define_global "const_nuw_neg" (const_nuw_neg foldbomb) m);
267 ignore (define_global "const_fneg" (const_fneg ffoldbomb) m);
268 ignore (define_global "const_not" (const_not foldbomb) m);
269 ignore (define_global "const_add" (const_add foldbomb five) m);
270 ignore (define_global "const_nsw_add" (const_nsw_add foldbomb five) m);
271 ignore (define_global "const_nuw_add" (const_nuw_add foldbomb five) m);
272 ignore (define_global "const_fadd" (const_fadd ffoldbomb ffive) m);
273 ignore (define_global "const_sub" (const_sub foldbomb five) m);
274 ignore (define_global "const_nsw_sub" (const_nsw_sub foldbomb five) m);
275 ignore (define_global "const_nuw_sub" (const_nuw_sub foldbomb five) m);
276 ignore (define_global "const_fsub" (const_fsub ffoldbomb ffive) m);
277 ignore (define_global "const_mul" (const_mul foldbomb five) m);
278 ignore (define_global "const_nsw_mul" (const_nsw_mul foldbomb five) m);
279 ignore (define_global "const_nuw_mul" (const_nuw_mul foldbomb five) m);
280 ignore (define_global "const_fmul" (const_fmul ffoldbomb ffive) m);
281 ignore (define_global "const_udiv" (const_udiv foldbomb five) m);
282 ignore (define_global "const_sdiv" (const_sdiv foldbomb five) m);
283 ignore (define_global "const_exact_sdiv" (const_exact_sdiv foldbomb five) m);
284 ignore (define_global "const_fdiv" (const_fdiv ffoldbomb ffive) m);
285 ignore (define_global "const_urem" (const_urem foldbomb five) m);
286 ignore (define_global "const_srem" (const_srem foldbomb five) m);
287 ignore (define_global "const_frem" (const_frem ffoldbomb ffive) m);
288 ignore (define_global "const_and" (const_and foldbomb five) m);
289 ignore (define_global "const_or" (const_or foldbomb five) m);
290 ignore (define_global "const_xor" (const_xor foldbomb five) m);
291 ignore (define_global "const_icmp" (const_icmp Icmp.Sle foldbomb five) m);
292 ignore (define_global "const_fcmp" (const_fcmp Fcmp.Ole ffoldbomb ffive) m);
294 group "constant casts";
295 (* CHECK: const_trunc{{.*}}trunc
296 * CHECK: const_sext{{.*}}sext
297 * CHECK: const_zext{{.*}}zext
298 * CHECK: const_fptrunc{{.*}}fptrunc
299 * CHECK: const_fpext{{.*}}fpext
300 * CHECK: const_uitofp{{.*}}uitofp
301 * CHECK: const_sitofp{{.*}}sitofp
302 * CHECK: const_fptoui{{.*}}fptoui
303 * CHECK: const_fptosi{{.*}}fptosi
304 * CHECK: const_ptrtoint{{.*}}ptrtoint
305 * CHECK: const_inttoptr{{.*}}inttoptr
306 * CHECK: const_bitcast{{.*}}bitcast
307 * CHECK: const_intcast{{.*}}zext
309 let i128_type = integer_type context 128 in
310 ignore (define_global "const_trunc" (const_trunc (const_add foldbomb five)
312 ignore (define_global "const_sext" (const_sext foldbomb i128_type) m);
313 ignore (define_global "const_zext" (const_zext foldbomb i128_type) m);
314 ignore (define_global "const_fptrunc" (const_fptrunc ffoldbomb float_type) m);
315 ignore (define_global "const_fpext" (const_fpext ffoldbomb fp128_type) m);
316 ignore (define_global "const_uitofp" (const_uitofp foldbomb double_type) m);
317 ignore (define_global "const_sitofp" (const_sitofp foldbomb double_type) m);
318 ignore (define_global "const_fptoui" (const_fptoui ffoldbomb i32_type) m);
319 ignore (define_global "const_fptosi" (const_fptosi ffoldbomb i32_type) m);
320 ignore (define_global "const_ptrtoint" (const_ptrtoint
321 (const_gep (const_null (pointer_type i8_type))
322 [| const_int i32_type 1 |])
324 ignore (define_global "const_inttoptr" (const_inttoptr (const_add foldbomb five)
326 ignore (define_global "const_bitcast" (const_bitcast ffoldbomb i64_type) m);
327 ignore (define_global "const_intcast"
328 (const_intcast foldbomb i128_type ~is_signed:false) m);
330 group "misc constants";
331 (* CHECK: const_size_of{{.*}}getelementptr{{.*}}null
332 * CHECK: const_gep{{.*}}getelementptr
333 * CHECK: const_select{{.*}}select
334 * CHECK: const_extractelement{{.*}}extractelement
335 * CHECK: const_insertelement{{.*}}insertelement
336 * CHECK: const_shufflevector = global <4 x i32> <i32 0, i32 1, i32 1, i32 0>
338 ignore (define_global "const_size_of" (size_of (pointer_type i8_type)) m);
339 ignore (define_global "const_gep" (const_gep foldbomb_gv [| five |]) m);
340 ignore (define_global "const_select" (const_select
341 (const_icmp Icmp.Sle foldbomb five)
342 (const_int i8_type (-1))
343 (const_int i8_type 0)) m);
344 let zero = const_int i32_type 0 in
345 let one = const_int i32_type 1 in
346 ignore (define_global "const_extractelement" (const_extractelement
347 (const_vector [| zero; one; zero; one |])
348 (const_trunc foldbomb i32_type)) m);
349 ignore (define_global "const_insertelement" (const_insertelement
350 (const_vector [| zero; one; zero; one |])
351 zero (const_trunc foldbomb i32_type)) m);
352 ignore (define_global "const_shufflevector" (const_shufflevector
353 (const_vector [| zero; one |])
354 (const_vector [| one; zero |])
355 (const_vector [| const_int i32_type 0; const_int i32_type 1;
356 const_int i32_type 2; const_int i32_type 3 |])) m);
359 let ft = function_type void_type [| i32_type; i32_type; i32_type |] in
360 ignore (const_inline_asm
363 "{cx},{ax},{di},~{dirflag},~{fpsr},~{flags},~{edi},~{ecx}"
368 group "recursive struct"; begin
369 let nsty = named_struct_type context "rec" in
370 let pty = pointer_type nsty in
371 struct_set_body nsty [| i32_type; pty |] false;
372 let elts = [| const_int i32_type 4; const_pointer_null pty |] in
373 let grec_init = const_named_struct nsty elts in
374 ignore (define_global "grec" grec_init m);
375 ignore (string_of_lltype nsty);
379 (*===-- Global Values -----------------------------------------------------===*)
381 let test_global_values () =
382 let (++) x f = f x; x in
383 let zero32 = const_null i32_type in
388 let g = define_global "TEMPORARY" zero32 m in
389 insist ("TEMPORARY" = value_name g);
390 set_value_name "GVal01" g;
391 insist ("GVal01" = value_name g);
393 (* CHECK: GVal02{{.*}}linkonce
396 let g = define_global "GVal02" zero32 m ++
397 set_linkage Linkage.Link_once in
398 insist (Linkage.Link_once = linkage g);
400 (* CHECK: GVal03{{.*}}Hanalei
403 let g = define_global "GVal03" zero32 m ++
404 set_section "Hanalei" in
405 insist ("Hanalei" = section g);
407 (* CHECK: GVal04{{.*}}hidden
410 let g = define_global "GVal04" zero32 m ++
411 set_visibility Visibility.Hidden in
412 insist (Visibility.Hidden = visibility g);
414 (* CHECK: GVal05{{.*}}align 128
417 let g = define_global "GVal05" zero32 m ++
419 insist (128 = alignment g)
422 (*===-- Global Variables --------------------------------------------------===*)
424 let test_global_variables () =
425 let (++) x f = f x; x in
426 let forty_two32 = const_int i32_type 42 in
428 group "declarations"; begin
429 (* CHECK: @GVar01 = external global i32
430 * CHECK: @QGVar01 = external addrspace(3) global i32
432 insist (None == lookup_global "GVar01" m);
433 let g = declare_global i32_type "GVar01" m in
434 insist (is_declaration g);
435 insist (pointer_type float_type ==
436 type_of (declare_global float_type "GVar01" m));
437 insist (g == declare_global i32_type "GVar01" m);
438 insist (match lookup_global "GVar01" m with Some x -> x = g
441 insist (None == lookup_global "QGVar01" m);
442 let g = declare_qualified_global i32_type "QGVar01" 3 m in
443 insist (is_declaration g);
444 insist (qualified_pointer_type float_type 3 ==
445 type_of (declare_qualified_global float_type "QGVar01" 3 m));
446 insist (g == declare_qualified_global i32_type "QGVar01" 3 m);
447 insist (match lookup_global "QGVar01" m with Some x -> x = g
451 group "definitions"; begin
452 (* CHECK: @GVar02 = global i32 42
453 * CHECK: @GVar03 = global i32 42
454 * CHECK: @QGVar02 = addrspace(3) global i32 42
455 * CHECK: @QGVar03 = addrspace(3) global i32 42
457 let g = define_global "GVar02" forty_two32 m in
458 let g2 = declare_global i32_type "GVar03" m ++
459 set_initializer forty_two32 in
460 insist (not (is_declaration g));
461 insist (not (is_declaration g2));
462 insist ((global_initializer g) == (global_initializer g2));
464 let g = define_qualified_global "QGVar02" forty_two32 3 m in
465 let g2 = declare_qualified_global i32_type "QGVar03" 3 m ++
466 set_initializer forty_two32 in
467 insist (not (is_declaration g));
468 insist (not (is_declaration g2));
469 insist ((global_initializer g) == (global_initializer g2));
472 (* CHECK: GVar04{{.*}}thread_local
475 let g = define_global "GVar04" forty_two32 m ++
476 set_thread_local true in
477 insist (is_thread_local g);
479 (* CHECK: GVar05{{.*}}thread_local(initialexec)
481 group "threadlocal_mode";
482 let g = define_global "GVar05" forty_two32 m ++
483 set_thread_local_mode ThreadLocalMode.InitialExec in
484 insist ((thread_local_mode g) = ThreadLocalMode.InitialExec);
486 (* CHECK: GVar06{{.*}}externally_initialized
488 group "externally_initialized";
489 let g = define_global "GVar06" forty_two32 m ++
490 set_externally_initialized true in
491 insist (is_externally_initialized g);
493 (* CHECK-NOWHERE-NOT: GVar07
496 let g = define_global "GVar07" forty_two32 m in
499 (* CHECK: ConstGlobalVar{{.*}}constant
502 let g = define_global "ConstGlobalVar" forty_two32 m in
503 insist (not (is_global_constant g));
504 set_global_constant true g;
505 insist (is_global_constant g);
507 begin group "iteration";
508 let m = create_module context "temp" in
510 insist (At_end m = global_begin m);
511 insist (At_start m = global_end m);
513 let g1 = declare_global i32_type "One" m in
514 let g2 = declare_global i32_type "Two" m in
516 insist (Before g1 = global_begin m);
517 insist (Before g2 = global_succ g1);
518 insist (At_end m = global_succ g2);
520 insist (After g2 = global_end m);
521 insist (After g1 = global_pred g2);
522 insist (At_start m = global_pred g1);
524 let lf s x = s ^ "->" ^ value_name x in
525 insist ("->One->Two" = fold_left_globals lf "" m);
527 let rf x s = value_name x ^ "<-" ^ s in
528 insist ("One<-Two<-" = fold_right_globals rf m "");
533 (* String globals built below are emitted here.
534 * CHECK: build_global_string{{.*}}stringval
538 (*===-- Uses --------------------------------------------------------------===*)
541 let ty = function_type i32_type [| i32_type; i32_type |] in
542 let fn = define_function "use_function" ty m in
543 let b = builder_at_end context (entry_block fn) in
545 let p1 = param fn 0 in
546 let p2 = param fn 1 in
547 let v1 = build_add p1 p2 "v1" b in
548 let v2 = build_add p1 v1 "v2" b in
549 let _ = build_add v1 v2 "v3" b in
551 let lf s u = value_name (user u) ^ "->" ^ s in
552 insist ("v2->v3->" = fold_left_uses lf "" v1);
553 let rf u s = value_name (user u) ^ "<-" ^ s in
554 insist ("v3<-v2<-" = fold_right_uses rf v1 "");
556 let lf s u = value_name (used_value u) ^ "->" ^ s in
557 insist ("v1->v1->" = fold_left_uses lf "" v1);
559 let rf u s = value_name (used_value u) ^ "<-" ^ s in
560 insist ("v1<-v1<-" = fold_right_uses rf v1 "");
562 ignore (build_unreachable b)
565 (*===-- Users -------------------------------------------------------------===*)
568 let ty = function_type i32_type [| i32_type; i32_type |] in
569 let fn = define_function "user_function" ty m in
570 let b = builder_at_end context (entry_block fn) in
572 let p1 = param fn 0 in
573 let p2 = param fn 1 in
574 let a3 = build_alloca i32_type "user_alloca" b in
575 let p3 = build_load a3 "user_load" b in
576 let i = build_add p1 p2 "sum" b in
578 insist ((num_operands i) = 2);
579 insist ((operand i 0) = p1);
580 insist ((operand i 1) = p2);
583 insist ((operand i 1) != p2);
584 insist ((operand i 1) = p3);
586 ignore (build_unreachable b)
589 (*===-- Aliases -----------------------------------------------------------===*)
591 let test_aliases () =
592 (* CHECK: @alias = alias i32* @aliasee
594 let forty_two32 = const_int i32_type 42 in
595 let v = define_global "aliasee" forty_two32 m in
596 ignore (add_alias m (pointer_type i32_type) v "alias")
599 (*===-- Functions ---------------------------------------------------------===*)
601 let test_functions () =
602 let ty = function_type i32_type [| i32_type; i64_type |] in
603 let ty2 = function_type i8_type [| i8_type; i64_type |] in
605 (* CHECK: declare i32 @Fn1(i32, i64)
607 begin group "declare";
608 insist (None = lookup_function "Fn1" m);
609 let fn = declare_function "Fn1" ty m in
610 insist (pointer_type ty = type_of fn);
611 insist (is_declaration fn);
612 insist (0 = Array.length (basic_blocks fn));
613 insist (pointer_type ty2 == type_of (declare_function "Fn1" ty2 m));
614 insist (fn == declare_function "Fn1" ty m);
615 insist (None <> lookup_function "Fn1" m);
616 insist (match lookup_function "Fn1" m with Some x -> x = fn
618 insist (m == global_parent fn)
621 (* CHECK-NOWHERE-NOT: Fn2
624 let fn = declare_function "Fn2" ty m in
627 (* CHECK: define{{.*}}Fn3
630 let fn = define_function "Fn3" ty m in
631 insist (not (is_declaration fn));
632 insist (1 = Array.length (basic_blocks fn));
633 ignore (build_unreachable (builder_at_end context (entry_block fn)));
635 (* CHECK: define{{.*}}Fn4{{.*}}Param1{{.*}}Param2
638 let fn = define_function "Fn4" ty m in
639 let params = params fn in
640 insist (2 = Array.length params);
641 insist (params.(0) = param fn 0);
642 insist (params.(1) = param fn 1);
643 insist (i32_type = type_of params.(0));
644 insist (i64_type = type_of params.(1));
645 set_value_name "Param1" params.(0);
646 set_value_name "Param2" params.(1);
647 ignore (build_unreachable (builder_at_end context (entry_block fn)));
649 (* CHECK: fastcc{{.*}}Fn5
652 let fn = define_function "Fn5" ty m in
653 insist (CallConv.c = function_call_conv fn);
654 set_function_call_conv CallConv.fast fn;
655 insist (CallConv.fast = function_call_conv fn);
656 ignore (build_unreachable (builder_at_end context (entry_block fn)));
659 (* CHECK: Fn6{{.*}}gc{{.*}}shadowstack
661 let fn = define_function "Fn6" ty m in
662 insist (None = gc fn);
663 set_gc (Some "ocaml") fn;
664 insist (Some "ocaml" = gc fn);
666 insist (None = gc fn);
667 set_gc (Some "shadowstack") fn;
668 ignore (build_unreachable (builder_at_end context (entry_block fn)));
671 begin group "iteration";
672 let m = create_module context "temp" in
674 insist (At_end m = function_begin m);
675 insist (At_start m = function_end m);
677 let f1 = define_function "One" ty m in
678 let f2 = define_function "Two" ty m in
680 insist (Before f1 = function_begin m);
681 insist (Before f2 = function_succ f1);
682 insist (At_end m = function_succ f2);
684 insist (After f2 = function_end m);
685 insist (After f1 = function_pred f2);
686 insist (At_start m = function_pred f1);
688 let lf s x = s ^ "->" ^ value_name x in
689 insist ("->One->Two" = fold_left_functions lf "" m);
691 let rf x s = value_name x ^ "<-" ^ s in
692 insist ("One<-Two<-" = fold_right_functions rf m "");
698 (*===-- Params ------------------------------------------------------------===*)
701 begin group "iteration";
702 let m = create_module context "temp" in
704 let vf = define_function "void" (function_type void_type [| |]) m in
706 insist (At_end vf = param_begin vf);
707 insist (At_start vf = param_end vf);
709 let ty = function_type void_type [| i32_type; i32_type |] in
710 let f = define_function "f" ty m in
711 let p1 = param f 0 in
712 let p2 = param f 1 in
713 set_value_name "One" p1;
714 set_value_name "Two" p2;
715 add_param_attr p1 Attribute.Sext;
716 add_param_attr p2 Attribute.Noalias;
717 remove_param_attr p2 Attribute.Noalias;
718 add_function_attr f Attribute.Nounwind;
719 add_function_attr f Attribute.Noreturn;
720 remove_function_attr f Attribute.Noreturn;
722 insist (Before p1 = param_begin f);
723 insist (Before p2 = param_succ p1);
724 insist (At_end f = param_succ p2);
726 insist (After p2 = param_end f);
727 insist (After p1 = param_pred p2);
728 insist (At_start f = param_pred p1);
730 let lf s x = s ^ "->" ^ value_name x in
731 insist ("->One->Two" = fold_left_params lf "" f);
733 let rf x s = value_name x ^ "<-" ^ s in
734 insist ("One<-Two<-" = fold_right_params rf f "");
740 (*===-- Basic Blocks ------------------------------------------------------===*)
742 let test_basic_blocks () =
743 let ty = function_type void_type [| |] in
748 let fn = declare_function "X" ty m in
749 let bb = append_block context "Bb1" fn in
750 insist (bb = entry_block fn);
751 ignore (build_unreachable (builder_at_end context bb));
753 (* CHECK-NOWHERE-NOT: Bb2
756 let fn = declare_function "X2" ty m in
757 let bb = append_block context "Bb2" fn in
761 let fn = declare_function "X3" ty m in
762 let bbb = append_block context "b" fn in
763 let bba = insert_block context "a" bbb in
764 insist ([| bba; bbb |] = basic_blocks fn);
765 ignore (build_unreachable (builder_at_end context bba));
766 ignore (build_unreachable (builder_at_end context bbb));
771 let fn = define_function "X4" ty m in
772 let bb = entry_block fn in
773 ignore (build_unreachable (builder_at_end context bb));
774 let bbv = value_of_block bb in
775 set_value_name "Bb3" bbv;
776 insist ("Bb3" = value_name bbv);
779 let fn = define_function "X5" ty m in
780 let bb = entry_block fn in
781 ignore (build_unreachable (builder_at_end context bb));
782 insist (bb = block_of_value (value_of_block bb));
783 insist (value_is_block (value_of_block bb));
784 insist (not (value_is_block (const_null i32_type)));
786 begin group "iteration";
787 let m = create_module context "temp" in
788 let f = declare_function "Temp" (function_type i32_type [| |]) m in
790 insist (At_end f = block_begin f);
791 insist (At_start f = block_end f);
793 let b1 = append_block context "One" f in
794 let b2 = append_block context "Two" f in
796 insist (Before b1 = block_begin f);
797 insist (Before b2 = block_succ b1);
798 insist (At_end f = block_succ b2);
800 insist (After b2 = block_end f);
801 insist (After b1 = block_pred b2);
802 insist (At_start f = block_pred b1);
804 let lf s x = s ^ "->" ^ value_name (value_of_block x) in
805 insist ("->One->Two" = fold_left_blocks lf "" f);
807 let rf x s = value_name (value_of_block x) ^ "<-" ^ s in
808 insist ("One<-Two<-" = fold_right_blocks rf f "");
814 (*===-- Instructions ------------------------------------------------------===*)
816 let test_instructions () =
817 begin group "iteration";
818 let m = create_module context "temp" in
819 let fty = function_type void_type [| i32_type; i32_type |] in
820 let f = define_function "f" fty m in
821 let bb = entry_block f in
822 let b = builder_at context (At_end bb) in
824 insist (At_end bb = instr_begin bb);
825 insist (At_start bb = instr_end bb);
827 let i1 = build_add (param f 0) (param f 1) "One" b in
828 let i2 = build_sub (param f 0) (param f 1) "Two" b in
830 insist (Before i1 = instr_begin bb);
831 insist (Before i2 = instr_succ i1);
832 insist (At_end bb = instr_succ i2);
834 insist (After i2 = instr_end bb);
835 insist (After i1 = instr_pred i2);
836 insist (At_start bb = instr_pred i1);
838 let lf s x = s ^ "->" ^ value_name x in
839 insist ("->One->Two" = fold_left_instrs lf "" bb);
841 let rf x s = value_name x ^ "<-" ^ s in
842 insist ("One<-Two<-" = fold_right_instrs rf bb "");
849 (* CHECK: %clone = add i32 %0, 2
851 let fty = function_type void_type [| i32_type |] in
852 let fn = define_function "BuilderParent" fty m in
853 let bb = entry_block fn in
854 let b = builder_at_end context bb in
855 let p = param fn 0 in
856 let sum = build_add p p "sum" b in
857 let y = const_int i32_type 2 in
858 let clone = instr_clone sum in
859 set_operand clone 0 p;
860 set_operand clone 1 y;
861 insert_into_builder clone "clone" b;
862 ignore (build_ret_void b)
866 (*===-- Builder -----------------------------------------------------------===*)
868 let test_builder () =
869 let (++) x f = f x; x in
871 begin group "parent";
873 ignore (insertion_block (builder context));
878 let fty = function_type void_type [| i32_type |] in
879 let fn = define_function "BuilderParent" fty m in
880 let bb = entry_block fn in
881 let b = builder_at_end context bb in
882 let p = param fn 0 in
883 let sum = build_add p p "sum" b in
884 ignore (build_ret_void b);
886 insist (fn = block_parent bb);
887 insist (fn = param_parent p);
888 insist (bb = instr_parent sum);
889 insist (bb = insertion_block b)
896 let fty = function_type void_type [| |] in
897 let fn = declare_function "X6" fty m in
898 let b = builder_at_end context (append_block context "Bb01" fn) in
899 ignore (build_ret_void b)
902 group "ret aggregate";
904 (* CHECK: ret { i8, i64 } { i8 4, i64 5 }
906 let sty = struct_type context [| i8_type; i64_type |] in
907 let fty = function_type sty [| |] in
908 let fn = declare_function "XA6" fty m in
909 let b = builder_at_end context (append_block context "Bb01" fn) in
910 let agg = [| const_int i8_type 4; const_int i64_type 5 |] in
911 ignore (build_aggregate_ret agg b)
914 (* The rest of the tests will use one big function. *)
915 let fty = function_type i32_type [| i32_type; i32_type |] in
916 let fn = define_function "X7" fty m in
917 let atentry = builder_at_end context (entry_block fn) in
918 let p1 = param fn 0 ++ set_value_name "P1" in
919 let p2 = param fn 1 ++ set_value_name "P2" in
920 let f1 = build_uitofp p1 float_type "F1" atentry in
921 let f2 = build_uitofp p2 float_type "F2" atentry in
923 let bb00 = append_block context "Bb00" fn in
924 ignore (build_unreachable (builder_at_end context bb00));
926 group "function attribute";
928 ignore (add_function_attr fn Attribute.UWTable);
930 * #0 is uwtable, defined at EOF.
932 insist ([Attribute.UWTable] = function_attr fn);
936 let void_ptr = pointer_type i8_type in
938 (* CHECK-DAG: %build_trunc = trunc i32 %P1 to i8
939 * CHECK-DAG: %build_trunc2 = trunc i32 %P1 to i8
940 * CHECK-DAG: %build_trunc3 = trunc i32 %P1 to i8
941 * CHECK-DAG: %build_zext = zext i8 %build_trunc to i32
942 * CHECK-DAG: %build_zext2 = zext i8 %build_trunc to i32
943 * CHECK-DAG: %build_sext = sext i32 %build_zext to i64
944 * CHECK-DAG: %build_sext2 = sext i32 %build_zext to i64
945 * CHECK-DAG: %build_sext3 = sext i32 %build_zext to i64
946 * CHECK-DAG: %build_uitofp = uitofp i64 %build_sext to float
947 * CHECK-DAG: %build_sitofp = sitofp i32 %build_zext to double
948 * CHECK-DAG: %build_fptoui = fptoui float %build_uitofp to i32
949 * CHECK-DAG: %build_fptosi = fptosi double %build_sitofp to i64
950 * CHECK-DAG: %build_fptrunc = fptrunc double %build_sitofp to float
951 * CHECK-DAG: %build_fptrunc2 = fptrunc double %build_sitofp to float
952 * CHECK-DAG: %build_fpext = fpext float %build_fptrunc to double
953 * CHECK-DAG: %build_fpext2 = fpext float %build_fptrunc to double
954 * CHECK-DAG: %build_inttoptr = inttoptr i32 %P1 to i8*
955 * CHECK-DAG: %build_ptrtoint = ptrtoint i8* %build_inttoptr to i64
956 * CHECK-DAG: %build_ptrtoint2 = ptrtoint i8* %build_inttoptr to i64
957 * CHECK-DAG: %build_bitcast = bitcast i64 %build_ptrtoint to double
958 * CHECK-DAG: %build_bitcast2 = bitcast i64 %build_ptrtoint to double
959 * CHECK-DAG: %build_bitcast3 = bitcast i64 %build_ptrtoint to double
960 * CHECK-DAG: %build_bitcast4 = bitcast i64 %build_ptrtoint to double
961 * CHECK-DAG: %build_pointercast = bitcast i8* %build_inttoptr to i16*
963 let inst28 = build_trunc p1 i8_type "build_trunc" atentry in
964 let inst29 = build_zext inst28 i32_type "build_zext" atentry in
965 let inst30 = build_sext inst29 i64_type "build_sext" atentry in
966 let inst31 = build_uitofp inst30 float_type "build_uitofp" atentry in
967 let inst32 = build_sitofp inst29 double_type "build_sitofp" atentry in
968 ignore(build_fptoui inst31 i32_type "build_fptoui" atentry);
969 ignore(build_fptosi inst32 i64_type "build_fptosi" atentry);
970 let inst35 = build_fptrunc inst32 float_type "build_fptrunc" atentry in
971 ignore(build_fpext inst35 double_type "build_fpext" atentry);
972 let inst37 = build_inttoptr p1 void_ptr "build_inttoptr" atentry in
973 let inst38 = build_ptrtoint inst37 i64_type "build_ptrtoint" atentry in
974 ignore(build_bitcast inst38 double_type "build_bitcast" atentry);
975 ignore(build_zext_or_bitcast inst38 double_type "build_bitcast2" atentry);
976 ignore(build_sext_or_bitcast inst38 double_type "build_bitcast3" atentry);
977 ignore(build_trunc_or_bitcast inst38 double_type "build_bitcast4" atentry);
978 ignore(build_pointercast inst37 (pointer_type i16_type) "build_pointercast" atentry);
980 ignore(build_zext_or_bitcast inst28 i32_type "build_zext2" atentry);
981 ignore(build_sext_or_bitcast inst29 i64_type "build_sext2" atentry);
982 ignore(build_trunc_or_bitcast p1 i8_type "build_trunc2" atentry);
983 ignore(build_pointercast inst37 i64_type "build_ptrtoint2" atentry);
984 ignore(build_intcast inst29 i64_type "build_sext3" atentry);
985 ignore(build_intcast p1 i8_type "build_trunc3" atentry);
986 ignore(build_fpcast inst35 double_type "build_fpext2" atentry);
987 ignore(build_fpcast inst32 float_type "build_fptrunc2" atentry);
990 group "comparisons"; begin
991 (* CHECK: %build_icmp_ne = icmp ne i32 %P1, %P2
992 * CHECK: %build_icmp_sle = icmp sle i32 %P2, %P1
993 * CHECK: %build_fcmp_false = fcmp false float %F1, %F2
994 * CHECK: %build_fcmp_true = fcmp true float %F2, %F1
995 * CHECK: %build_is_null{{.*}}= icmp eq{{.*}}%X0,{{.*}}null
996 * CHECK: %build_is_not_null = icmp ne i8* %X1, null
997 * CHECK: %build_ptrdiff
999 ignore (build_icmp Icmp.Ne p1 p2 "build_icmp_ne" atentry);
1000 ignore (build_icmp Icmp.Sle p2 p1 "build_icmp_sle" atentry);
1001 ignore (build_fcmp Fcmp.False f1 f2 "build_fcmp_false" atentry);
1002 ignore (build_fcmp Fcmp.True f2 f1 "build_fcmp_true" atentry);
1003 let g0 = declare_global (pointer_type i8_type) "g0" m in
1004 let g1 = declare_global (pointer_type i8_type) "g1" m in
1005 let p0 = build_load g0 "X0" atentry in
1006 let p1 = build_load g1 "X1" atentry in
1007 ignore (build_is_null p0 "build_is_null" atentry);
1008 ignore (build_is_not_null p1 "build_is_not_null" atentry);
1009 ignore (build_ptrdiff p1 p0 "build_ptrdiff" atentry);
1012 group "miscellaneous"; begin
1013 (* CHECK: %build_call = tail call cc63 i32 @{{.*}}(i32 signext %P2, i32 %P1)
1014 * CHECK: %build_select = select i1 %build_icmp, i32 %P1, i32 %P2
1015 * CHECK: %build_va_arg = va_arg i8** null, i32
1016 * CHECK: %build_extractelement = extractelement <4 x i32> %Vec1, i32 %P2
1017 * CHECK: %build_insertelement = insertelement <4 x i32> %Vec1, i32 %P1, i32 %P2
1018 * CHECK: %build_shufflevector = shufflevector <4 x i32> %Vec1, <4 x i32> %Vec2, <4 x i32> <i32 1, i32 1, i32 0, i32 0>
1019 * CHECK: %build_insertvalue0 = insertvalue{{.*}}%bl, i32 1, 0
1020 * CHECK: %build_extractvalue = extractvalue{{.*}}%build_insertvalue1, 1
1022 let ci = build_call fn [| p2; p1 |] "build_call" atentry in
1023 insist (CallConv.c = instruction_call_conv ci);
1024 set_instruction_call_conv 63 ci;
1025 insist (63 = instruction_call_conv ci);
1026 insist (not (is_tail_call ci));
1027 set_tail_call true ci;
1028 insist (is_tail_call ci);
1029 add_instruction_param_attr ci 1 Attribute.Sext;
1030 add_instruction_param_attr ci 2 Attribute.Noalias;
1031 remove_instruction_param_attr ci 2 Attribute.Noalias;
1033 let inst46 = build_icmp Icmp.Eq p1 p2 "build_icmp" atentry in
1034 ignore (build_select inst46 p1 p2 "build_select" atentry);
1035 ignore (build_va_arg
1036 (const_null (pointer_type (pointer_type i8_type)))
1037 i32_type "build_va_arg" atentry);
1039 (* Set up some vector vregs. *)
1040 let one = const_int i32_type 1 in
1041 let zero = const_int i32_type 0 in
1042 let t1 = const_vector [| one; zero; one; zero |] in
1043 let t2 = const_vector [| zero; one; zero; one |] in
1044 let t3 = const_vector [| one; one; zero; zero |] in
1045 let vec1 = build_insertelement t1 p1 p2 "Vec1" atentry in
1046 let vec2 = build_insertelement t2 p1 p2 "Vec2" atentry in
1047 let sty = struct_type context [| i32_type; i8_type |] in
1049 ignore (build_extractelement vec1 p2 "build_extractelement" atentry);
1050 ignore (build_insertelement vec1 p1 p2 "build_insertelement" atentry);
1051 ignore (build_shufflevector vec1 vec2 t3 "build_shufflevector" atentry);
1053 let p = build_alloca sty "ba" atentry in
1054 let agg = build_load p "bl" atentry in
1055 let agg0 = build_insertvalue agg (const_int i32_type 1) 0
1056 "build_insertvalue0" atentry in
1057 let agg1 = build_insertvalue agg0 (const_int i8_type 2) 1
1058 "build_insertvalue1" atentry in
1059 ignore (build_extractvalue agg1 1 "build_extractvalue" atentry)
1062 group "metadata"; begin
1063 (* CHECK: %metadata = add i32 %P1, %P2, !test !1
1064 * !1 is metadata emitted at EOF.
1066 let i = build_add p1 p2 "metadata" atentry in
1067 insist ((has_metadata i) = false);
1069 let m1 = const_int i32_type 1 in
1070 let m2 = mdstring context "metadata test" in
1071 let md = mdnode context [| m1; m2 |] in
1073 let kind = mdkind_id context "test" in
1074 set_metadata i kind md;
1076 insist ((has_metadata i) = true);
1077 insist ((metadata i kind) = Some md);
1079 clear_metadata i kind;
1081 insist ((has_metadata i) = false);
1082 insist ((metadata i kind) = None);
1084 set_metadata i kind md
1087 group "named metadata"; begin
1088 (* !llvm.module.flags is emitted at EOF. *)
1089 let n1 = const_int i32_type 1 in
1090 let n2 = mdstring context "Debug Info Version" in
1091 let n3 = const_int i32_type 2 in
1092 let md = mdnode context [| n1; n2; n3 |] in
1093 add_named_metadata_operand m "llvm.module.flags" md;
1095 insist ((get_named_metadata m "llvm.module.flags") = [| md |])
1099 (* CHECK: %dbg = add i32 %P1, %P2, !dbg !2
1100 * !2 is metadata emitted at EOF.
1102 insist ((current_debug_location atentry) = None);
1104 let m_line = const_int i32_type 2 in
1105 let m_col = const_int i32_type 3 in
1106 let m_scope = mdnode context [| |] in
1107 let m_inlined = mdnode context [| |] in
1108 let md = mdnode context [| m_line; m_col; m_scope; m_inlined |] in
1109 set_current_debug_location atentry md;
1111 insist ((current_debug_location atentry) = Some md);
1113 let i = build_add p1 p2 "dbg" atentry in
1114 insist ((has_metadata i) = true);
1116 clear_current_debug_location atentry
1120 (* CHECK: ret{{.*}}P1
1122 let ret = build_ret p1 atentry in
1123 position_before ret atentry
1126 (* see test/Feature/exception.ll *)
1127 let bblpad = append_block context "Bblpad" fn in
1128 let rt = struct_type context [| pointer_type i8_type; i32_type |] in
1129 let ft = var_arg_function_type i32_type [||] in
1130 let personality = declare_function "__gxx_personality_v0" ft m in
1131 let ztic = declare_global (pointer_type i8_type) "_ZTIc" m in
1132 let ztid = declare_global (pointer_type i8_type) "_ZTId" m in
1133 let ztipkc = declare_global (pointer_type i8_type) "_ZTIPKc" m in
1135 set_global_constant true ztic;
1136 set_global_constant true ztid;
1137 set_global_constant true ztipkc;
1138 let lp = build_landingpad rt personality 0 "lpad"
1139 (builder_at_end context bblpad) in begin
1140 set_cleanup lp true;
1142 insist((pointer_type (pointer_type i8_type)) = type_of ztid);
1143 let ety = pointer_type (pointer_type i8_type) in
1144 add_clause lp (const_array ety [| ztipkc; ztid |]);
1145 ignore (build_resume lp (builder_at_end context bblpad));
1147 (* CHECK: landingpad{{.*}}personality{{.*}}__gxx_personality_v0
1149 * CHECK: catch{{.*}}i8**{{.*}}@_ZTIc
1150 * CHECK: filter{{.*}}@_ZTIPKc{{.*}}@_ZTId
1156 (* CHECK: br{{.*}}Bb02
1158 let bb02 = append_block context "Bb02" fn in
1159 let b = builder_at_end context bb02 in
1160 ignore (build_br bb02 b)
1163 group "cond_br"; begin
1164 (* CHECK: br{{.*}}build_br{{.*}}Bb03{{.*}}Bb00
1166 let bb03 = append_block context "Bb03" fn in
1167 let b = builder_at_end context bb03 in
1168 let cond = build_trunc p1 i1_type "build_br" b in
1169 ignore (build_cond_br cond bb03 bb00 b)
1172 group "switch"; begin
1173 (* CHECK: switch{{.*}}P1{{.*}}SwiBlock3
1174 * CHECK: 2,{{.*}}SwiBlock2
1176 let bb1 = append_block context "SwiBlock1" fn in
1177 let bb2 = append_block context "SwiBlock2" fn in
1178 ignore (build_unreachable (builder_at_end context bb2));
1179 let bb3 = append_block context "SwiBlock3" fn in
1180 ignore (build_unreachable (builder_at_end context bb3));
1181 let si = build_switch p1 bb3 1 (builder_at_end context bb1) in begin
1182 ignore (add_case si (const_int i32_type 2) bb2);
1183 insist (switch_default_dest si = bb3);
1187 group "malloc/free"; begin
1188 (* CHECK: call{{.*}}@malloc(i32 ptrtoint
1189 * CHECK: call{{.*}}@free(i8*
1190 * CHECK: call{{.*}}@malloc(i32 %
1192 let bb1 = append_block context "MallocBlock1" fn in
1193 let m1 = (build_malloc (pointer_type i32_type) "m1"
1194 (builder_at_end context bb1)) in
1195 ignore (build_free m1 (builder_at_end context bb1));
1196 ignore (build_array_malloc i32_type p1 "m2" (builder_at_end context bb1));
1197 ignore (build_unreachable (builder_at_end context bb1));
1200 group "indirectbr"; begin
1201 (* CHECK: indirectbr i8* blockaddress(@X7, %IBRBlock2), [label %IBRBlock2, label %IBRBlock3]
1203 let bb1 = append_block context "IBRBlock1" fn in
1205 let bb2 = append_block context "IBRBlock2" fn in
1206 ignore (build_unreachable (builder_at_end context bb2));
1208 let bb3 = append_block context "IBRBlock3" fn in
1209 ignore (build_unreachable (builder_at_end context bb3));
1211 let addr = block_address fn bb2 in
1212 let ibr = build_indirect_br addr 2 (builder_at_end context bb1) in
1213 ignore (add_destination ibr bb2);
1214 ignore (add_destination ibr bb3)
1217 group "invoke"; begin
1218 (* CHECK: build_invoke{{.*}}invoke{{.*}}P1{{.*}}P2
1219 * CHECK: to{{.*}}Bb04{{.*}}unwind{{.*}}Bblpad
1221 let bb04 = append_block context "Bb04" fn in
1222 let b = builder_at_end context bb04 in
1223 ignore (build_invoke fn [| p1; p2 |] bb04 bblpad "build_invoke" b)
1226 group "unreachable"; begin
1227 (* CHECK: unreachable
1229 let bb06 = append_block context "Bb06" fn in
1230 let b = builder_at_end context bb06 in
1231 ignore (build_unreachable b)
1234 group "arithmetic"; begin
1235 let bb07 = append_block context "Bb07" fn in
1236 let b = builder_at_end context bb07 in
1238 (* CHECK: %build_add = add i32 %P1, %P2
1239 * CHECK: %build_nsw_add = add nsw i32 %P1, %P2
1240 * CHECK: %build_nuw_add = add nuw i32 %P1, %P2
1241 * CHECK: %build_fadd = fadd float %F1, %F2
1242 * CHECK: %build_sub = sub i32 %P1, %P2
1243 * CHECK: %build_nsw_sub = sub nsw i32 %P1, %P2
1244 * CHECK: %build_nuw_sub = sub nuw i32 %P1, %P2
1245 * CHECK: %build_fsub = fsub float %F1, %F2
1246 * CHECK: %build_mul = mul i32 %P1, %P2
1247 * CHECK: %build_nsw_mul = mul nsw i32 %P1, %P2
1248 * CHECK: %build_nuw_mul = mul nuw i32 %P1, %P2
1249 * CHECK: %build_fmul = fmul float %F1, %F2
1250 * CHECK: %build_udiv = udiv i32 %P1, %P2
1251 * CHECK: %build_sdiv = sdiv i32 %P1, %P2
1252 * CHECK: %build_exact_sdiv = sdiv exact i32 %P1, %P2
1253 * CHECK: %build_fdiv = fdiv float %F1, %F2
1254 * CHECK: %build_urem = urem i32 %P1, %P2
1255 * CHECK: %build_srem = srem i32 %P1, %P2
1256 * CHECK: %build_frem = frem float %F1, %F2
1257 * CHECK: %build_shl = shl i32 %P1, %P2
1258 * CHECK: %build_lshl = lshr i32 %P1, %P2
1259 * CHECK: %build_ashl = ashr i32 %P1, %P2
1260 * CHECK: %build_and = and i32 %P1, %P2
1261 * CHECK: %build_or = or i32 %P1, %P2
1262 * CHECK: %build_xor = xor i32 %P1, %P2
1263 * CHECK: %build_neg = sub i32 0, %P1
1264 * CHECK: %build_nsw_neg = sub nsw i32 0, %P1
1265 * CHECK: %build_nuw_neg = sub nuw i32 0, %P1
1266 * CHECK: %build_fneg = fsub float {{.*}}0{{.*}}, %F1
1267 * CHECK: %build_not = xor i32 %P1, -1
1269 ignore (build_add p1 p2 "build_add" b);
1270 ignore (build_nsw_add p1 p2 "build_nsw_add" b);
1271 ignore (build_nuw_add p1 p2 "build_nuw_add" b);
1272 ignore (build_fadd f1 f2 "build_fadd" b);
1273 ignore (build_sub p1 p2 "build_sub" b);
1274 ignore (build_nsw_sub p1 p2 "build_nsw_sub" b);
1275 ignore (build_nuw_sub p1 p2 "build_nuw_sub" b);
1276 ignore (build_fsub f1 f2 "build_fsub" b);
1277 ignore (build_mul p1 p2 "build_mul" b);
1278 ignore (build_nsw_mul p1 p2 "build_nsw_mul" b);
1279 ignore (build_nuw_mul p1 p2 "build_nuw_mul" b);
1280 ignore (build_fmul f1 f2 "build_fmul" b);
1281 ignore (build_udiv p1 p2 "build_udiv" b);
1282 ignore (build_sdiv p1 p2 "build_sdiv" b);
1283 ignore (build_exact_sdiv p1 p2 "build_exact_sdiv" b);
1284 ignore (build_fdiv f1 f2 "build_fdiv" b);
1285 ignore (build_urem p1 p2 "build_urem" b);
1286 ignore (build_srem p1 p2 "build_srem" b);
1287 ignore (build_frem f1 f2 "build_frem" b);
1288 ignore (build_shl p1 p2 "build_shl" b);
1289 ignore (build_lshr p1 p2 "build_lshl" b);
1290 ignore (build_ashr p1 p2 "build_ashl" b);
1291 ignore (build_and p1 p2 "build_and" b);
1292 ignore (build_or p1 p2 "build_or" b);
1293 ignore (build_xor p1 p2 "build_xor" b);
1294 ignore (build_neg p1 "build_neg" b);
1295 ignore (build_nsw_neg p1 "build_nsw_neg" b);
1296 ignore (build_nuw_neg p1 "build_nuw_neg" b);
1297 ignore (build_fneg f1 "build_fneg" b);
1298 ignore (build_not p1 "build_not" b);
1299 ignore (build_unreachable b)
1302 group "memory"; begin
1303 let bb08 = append_block context "Bb08" fn in
1304 let b = builder_at_end context bb08 in
1306 (* CHECK: %build_alloca = alloca i32
1307 * CHECK: %build_array_alloca = alloca i32, i32 %P2
1308 * CHECK: %build_load = load volatile i32* %build_array_alloca, align 4
1309 * CHECK: store volatile i32 %P2, i32* %build_alloca, align 4
1310 * CHECK: %build_gep = getelementptr i32* %build_array_alloca, i32 %P2
1311 * CHECK: %build_in_bounds_gep = getelementptr inbounds i32* %build_array_alloca, i32 %P2
1312 * CHECK: %build_struct_gep = getelementptr inbounds{{.*}}%build_alloca2, i32 0, i32 1
1313 * CHECK: %build_atomicrmw = atomicrmw xchg i8* %p, i8 42 seq_cst
1315 let alloca = build_alloca i32_type "build_alloca" b in
1316 let array_alloca = build_array_alloca i32_type p2 "build_array_alloca" b in
1318 let load = build_load array_alloca "build_load" b in
1319 ignore(set_alignment 4 load);
1320 ignore(set_volatile true load);
1321 insist(true = is_volatile load);
1322 insist(4 = alignment load);
1324 let store = build_store p2 alloca b in
1325 ignore(set_volatile true store);
1326 ignore(set_alignment 4 store);
1327 insist(true = is_volatile store);
1328 insist(4 = alignment store);
1329 ignore(build_gep array_alloca [| p2 |] "build_gep" b);
1330 ignore(build_in_bounds_gep array_alloca [| p2 |] "build_in_bounds_gep" b);
1332 let sty = struct_type context [| i32_type; i8_type |] in
1333 let alloca2 = build_alloca sty "build_alloca2" b in
1334 ignore(build_struct_gep alloca2 1 "build_struct_gep" b);
1336 let p = build_alloca i8_type "p" b in
1337 ignore(build_atomicrmw AtomicRMWBinOp.Xchg p (const_int i8_type 42)
1338 AtomicOrdering.SequentiallyConsistent false "build_atomicrmw"
1341 ignore(build_unreachable b)
1344 group "string"; begin
1345 let bb09 = append_block context "Bb09" fn in
1346 let b = builder_at_end context bb09 in
1347 let p = build_alloca (pointer_type i8_type) "p" b in
1348 (* build_global_string is emitted above.
1349 * CHECK: store{{.*}}build_global_string1{{.*}}p
1351 ignore (build_global_string "stringval" "build_global_string" b);
1352 let g = build_global_stringptr "stringval" "build_global_string1" b in
1353 ignore (build_store g p b);
1354 ignore(build_unreachable b);
1358 (* CHECK: PhiNode{{.*}}P1{{.*}}PhiBlock1{{.*}}P2{{.*}}PhiBlock2
1360 let b1 = append_block context "PhiBlock1" fn in
1361 let b2 = append_block context "PhiBlock2" fn in
1363 let jb = append_block context "PhiJoinBlock" fn in
1364 ignore (build_br jb (builder_at_end context b1));
1365 ignore (build_br jb (builder_at_end context b2));
1366 let at_jb = builder_at_end context jb in
1368 let phi = build_phi [(p1, b1)] "PhiNode" at_jb in
1369 insist ([(p1, b1)] = incoming phi);
1371 add_incoming (p2, b2) phi;
1372 insist ([(p1, b1); (p2, b2)] = incoming phi);
1374 ignore (build_unreachable at_jb);
1377 (* End-of-file checks for things like metdata and attributes.
1378 * CHECK: attributes #0 = {{.*}}uwtable{{.*}}
1379 * CHECK: !llvm.module.flags = !{!0}
1380 * CHECK: !0 = metadata !{i32 1, metadata !"Debug Info Version", i32 2}
1381 * CHECK: !1 = metadata !{i32 1, metadata !"metadata test"}
1382 * CHECK: !2 = metadata !{i32 2, i32 3, metadata !3, metadata !3}
1385 (*===-- Pass Managers -----------------------------------------------------===*)
1387 let test_pass_manager () =
1388 let (++) x f = ignore (f x); x in
1390 begin group "module pass manager";
1391 ignore (PassManager.create ()
1392 ++ PassManager.run_module m
1393 ++ PassManager.dispose)
1396 begin group "function pass manager";
1397 let fty = function_type void_type [| |] in
1398 let fn = define_function "FunctionPassManager" fty m in
1399 ignore (build_ret_void (builder_at_end context (entry_block fn)));
1401 ignore (PassManager.create_function m
1402 ++ PassManager.initialize
1403 ++ PassManager.run_function fn
1404 ++ PassManager.finalize
1405 ++ PassManager.dispose)
1409 (*===-- Memory Buffer -----------------------------------------------------===*)
1411 let test_memory_buffer () =
1412 group "memory buffer";
1413 let buf = MemoryBuffer.of_string "foobar" in
1414 insist ((MemoryBuffer.as_string buf) = "foobar")
1417 (*===-- Writer ------------------------------------------------------------===*)
1419 let test_writer () =
1421 insist (match Llvm_analysis.verify_module m with
1423 | Some msg -> prerr_string msg; false);
1426 insist (write_bitcode_file m filename);
1431 (*===-- Driver ------------------------------------------------------------===*)
1434 suite "conversion" test_conversion;
1435 suite "target" test_target;
1436 suite "constants" test_constants;
1437 suite "global values" test_global_values;
1438 suite "global variables" test_global_variables;
1439 suite "uses" test_uses;
1440 suite "users" test_users;
1441 suite "aliases" test_aliases;
1442 suite "functions" test_functions;
1443 suite "params" test_params;
1444 suite "basic blocks" test_basic_blocks;
1445 suite "instructions" test_instructions;
1446 suite "builder" test_builder;
1447 suite "pass manager" test_pass_manager;
1448 suite "memory buffer" test_memory_buffer;
1449 suite "writer" test_writer; (* Keep this last; it disposes m. *)