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);
129 if Sys.word_size = 64; then begin
131 let c = const_int i64_type (1 lsl 61) in
132 insist (c = const_of_int64 i64_type (Int64.of_int (1 lsl 61)) false)
135 (* CHECK: @const_string = global {{.*}}c"cruel\00world"
138 let c = const_string context "cruel\000world" in
139 ignore (define_global "const_string" c m);
140 insist ((array_type i8_type 11) = type_of c);
142 (* CHECK: const_stringz{{.*}}"hi\00again\00"
145 let c = const_stringz context "hi\000again" in
146 ignore (define_global "const_stringz" c m);
147 insist ((array_type i8_type 9) = type_of c);
149 (* CHECK: const_single{{.*}}2.75
150 * CHECK: const_double{{.*}}3.1459
151 * CHECK: const_double_string{{.*}}1.25
154 let cs = const_float float_type 2.75 in
155 ignore (define_global "const_single" cs m);
156 insist (float_type = type_of cs);
158 let cd = const_float double_type 3.1459 in
159 ignore (define_global "const_double" cd m);
160 insist (double_type = type_of cd);
162 let cd = const_float_of_string double_type "1.25" in
163 ignore (define_global "const_double_string" cd m);
164 insist (double_type = type_of cd)
167 let one = const_int i16_type 1 in
168 let two = const_int i16_type 2 in
169 let three = const_int i32_type 3 in
170 let four = const_int i32_type 4 in
172 (* CHECK: const_array{{.*}}[i32 3, i32 4]
175 let c = const_array i32_type [| three; four |] in
176 ignore (define_global "const_array" c m);
177 insist ((array_type i32_type 2) = (type_of c));
179 (* CHECK: const_vector{{.*}}<i16 1, i16 2{{.*}}>
182 let c = const_vector [| one; two; one; two;
183 one; two; one; two |] in
184 ignore (define_global "const_vector" c m);
185 insist ((vector_type i16_type 8) = (type_of c));
187 (* CHECK: const_structure{{.*.}}i16 1, i16 2, i32 3, i32 4
190 let c = const_struct context [| one; two; three; four |] in
191 ignore (define_global "const_structure" c m);
192 insist ((struct_type context [| i16_type; i16_type; i32_type; i32_type |])
195 (* CHECK: const_null{{.*}}zeroinit
198 let c = const_null (packed_struct_type context [| i1_type; i8_type; i64_type;
200 ignore (define_global "const_null" c m);
202 (* CHECK: const_all_ones{{.*}}-1
205 let c = const_all_ones i64_type in
206 ignore (define_global "const_all_ones" c m);
208 group "pointer null"; begin
209 (* CHECK: const_pointer_null = global i64* null
211 let c = const_pointer_null (pointer_type i64_type) in
212 ignore (define_global "const_pointer_null" c m);
215 (* CHECK: const_undef{{.*}}undef
218 let c = undef i1_type in
219 ignore (define_global "const_undef" c m);
220 insist (i1_type = type_of c);
223 group "constant arithmetic";
224 (* CHECK: @const_neg = global i64 sub
225 * CHECK: @const_nsw_neg = global i64 sub nsw
226 * CHECK: @const_nuw_neg = global i64 sub nuw
227 * CHECK: @const_fneg = global double fsub
228 * CHECK: @const_not = global i64 xor
229 * CHECK: @const_add = global i64 add
230 * CHECK: @const_nsw_add = global i64 add nsw
231 * CHECK: @const_nuw_add = global i64 add nuw
232 * CHECK: @const_fadd = global double fadd
233 * CHECK: @const_sub = global i64 sub
234 * CHECK: @const_nsw_sub = global i64 sub nsw
235 * CHECK: @const_nuw_sub = global i64 sub nuw
236 * CHECK: @const_fsub = global double fsub
237 * CHECK: @const_mul = global i64 mul
238 * CHECK: @const_nsw_mul = global i64 mul nsw
239 * CHECK: @const_nuw_mul = global i64 mul nuw
240 * CHECK: @const_fmul = global double fmul
241 * CHECK: @const_udiv = global i64 udiv
242 * CHECK: @const_sdiv = global i64 sdiv
243 * CHECK: @const_exact_sdiv = global i64 sdiv exact
244 * CHECK: @const_fdiv = global double fdiv
245 * CHECK: @const_urem = global i64 urem
246 * CHECK: @const_srem = global i64 srem
247 * CHECK: @const_frem = global double frem
248 * CHECK: @const_and = global i64 and
249 * CHECK: @const_or = global i64 or
250 * CHECK: @const_xor = global i64 xor
251 * CHECK: @const_icmp = global i1 icmp sle
252 * CHECK: @const_fcmp = global i1 fcmp ole
254 let void_ptr = pointer_type i8_type in
255 let five = const_int i64_type 5 in
256 let ffive = const_uitofp five double_type in
257 let foldbomb_gv = define_global "FoldBomb" (const_null i8_type) m in
258 let foldbomb = const_ptrtoint foldbomb_gv i64_type in
259 let ffoldbomb = const_uitofp foldbomb double_type in
260 ignore (define_global "const_neg" (const_neg foldbomb) m);
261 ignore (define_global "const_nsw_neg" (const_nsw_neg foldbomb) m);
262 ignore (define_global "const_nuw_neg" (const_nuw_neg foldbomb) m);
263 ignore (define_global "const_fneg" (const_fneg ffoldbomb) m);
264 ignore (define_global "const_not" (const_not foldbomb) m);
265 ignore (define_global "const_add" (const_add foldbomb five) m);
266 ignore (define_global "const_nsw_add" (const_nsw_add foldbomb five) m);
267 ignore (define_global "const_nuw_add" (const_nuw_add foldbomb five) m);
268 ignore (define_global "const_fadd" (const_fadd ffoldbomb ffive) m);
269 ignore (define_global "const_sub" (const_sub foldbomb five) m);
270 ignore (define_global "const_nsw_sub" (const_nsw_sub foldbomb five) m);
271 ignore (define_global "const_nuw_sub" (const_nuw_sub foldbomb five) m);
272 ignore (define_global "const_fsub" (const_fsub ffoldbomb ffive) m);
273 ignore (define_global "const_mul" (const_mul foldbomb five) m);
274 ignore (define_global "const_nsw_mul" (const_nsw_mul foldbomb five) m);
275 ignore (define_global "const_nuw_mul" (const_nuw_mul foldbomb five) m);
276 ignore (define_global "const_fmul" (const_fmul ffoldbomb ffive) m);
277 ignore (define_global "const_udiv" (const_udiv foldbomb five) m);
278 ignore (define_global "const_sdiv" (const_sdiv foldbomb five) m);
279 ignore (define_global "const_exact_sdiv" (const_exact_sdiv foldbomb five) m);
280 ignore (define_global "const_fdiv" (const_fdiv ffoldbomb ffive) m);
281 ignore (define_global "const_urem" (const_urem foldbomb five) m);
282 ignore (define_global "const_srem" (const_srem foldbomb five) m);
283 ignore (define_global "const_frem" (const_frem ffoldbomb ffive) m);
284 ignore (define_global "const_and" (const_and foldbomb five) m);
285 ignore (define_global "const_or" (const_or foldbomb five) m);
286 ignore (define_global "const_xor" (const_xor foldbomb five) m);
287 ignore (define_global "const_icmp" (const_icmp Icmp.Sle foldbomb five) m);
288 ignore (define_global "const_fcmp" (const_fcmp Fcmp.Ole ffoldbomb ffive) m);
290 group "constant casts";
291 (* CHECK: const_trunc{{.*}}trunc
292 * CHECK: const_sext{{.*}}sext
293 * CHECK: const_zext{{.*}}zext
294 * CHECK: const_fptrunc{{.*}}fptrunc
295 * CHECK: const_fpext{{.*}}fpext
296 * CHECK: const_uitofp{{.*}}uitofp
297 * CHECK: const_sitofp{{.*}}sitofp
298 * CHECK: const_fptoui{{.*}}fptoui
299 * CHECK: const_fptosi{{.*}}fptosi
300 * CHECK: const_ptrtoint{{.*}}ptrtoint
301 * CHECK: const_inttoptr{{.*}}inttoptr
302 * CHECK: const_bitcast{{.*}}bitcast
303 * CHECK: const_intcast{{.*}}zext
305 let i128_type = integer_type context 128 in
306 ignore (define_global "const_trunc" (const_trunc (const_add foldbomb five)
308 ignore (define_global "const_sext" (const_sext foldbomb i128_type) m);
309 ignore (define_global "const_zext" (const_zext foldbomb i128_type) m);
310 ignore (define_global "const_fptrunc" (const_fptrunc ffoldbomb float_type) m);
311 ignore (define_global "const_fpext" (const_fpext ffoldbomb fp128_type) m);
312 ignore (define_global "const_uitofp" (const_uitofp foldbomb double_type) m);
313 ignore (define_global "const_sitofp" (const_sitofp foldbomb double_type) m);
314 ignore (define_global "const_fptoui" (const_fptoui ffoldbomb i32_type) m);
315 ignore (define_global "const_fptosi" (const_fptosi ffoldbomb i32_type) m);
316 ignore (define_global "const_ptrtoint" (const_ptrtoint
317 (const_gep (const_null (pointer_type i8_type))
318 [| const_int i32_type 1 |])
320 ignore (define_global "const_inttoptr" (const_inttoptr (const_add foldbomb five)
322 ignore (define_global "const_bitcast" (const_bitcast ffoldbomb i64_type) m);
323 ignore (define_global "const_intcast"
324 (const_intcast foldbomb i128_type ~is_signed:false) m);
326 group "misc constants";
327 (* CHECK: const_size_of{{.*}}getelementptr{{.*}}null
328 * CHECK: const_gep{{.*}}getelementptr
329 * CHECK: const_select{{.*}}select
330 * CHECK: const_extractelement{{.*}}extractelement
331 * CHECK: const_insertelement{{.*}}insertelement
332 * CHECK: const_shufflevector = global <4 x i32> <i32 0, i32 1, i32 1, i32 0>
334 ignore (define_global "const_size_of" (size_of (pointer_type i8_type)) m);
335 ignore (define_global "const_gep" (const_gep foldbomb_gv [| five |]) m);
336 ignore (define_global "const_select" (const_select
337 (const_icmp Icmp.Sle foldbomb five)
338 (const_int i8_type (-1))
339 (const_int i8_type 0)) m);
340 let zero = const_int i32_type 0 in
341 let one = const_int i32_type 1 in
342 ignore (define_global "const_extractelement" (const_extractelement
343 (const_vector [| zero; one; zero; one |])
344 (const_trunc foldbomb i32_type)) m);
345 ignore (define_global "const_insertelement" (const_insertelement
346 (const_vector [| zero; one; zero; one |])
347 zero (const_trunc foldbomb i32_type)) m);
348 ignore (define_global "const_shufflevector" (const_shufflevector
349 (const_vector [| zero; one |])
350 (const_vector [| one; zero |])
351 (const_vector [| const_int i32_type 0; const_int i32_type 1;
352 const_int i32_type 2; const_int i32_type 3 |])) m);
355 let ft = function_type void_type [| i32_type; i32_type; i32_type |] in
356 ignore (const_inline_asm
359 "{cx},{ax},{di},~{dirflag},~{fpsr},~{flags},~{edi},~{ecx}"
364 group "recursive struct"; begin
365 let nsty = named_struct_type context "rec" in
366 let pty = pointer_type nsty in
367 struct_set_body nsty [| i32_type; pty |] false;
368 let elts = [| const_int i32_type 4; const_pointer_null pty |] in
369 let grec_init = const_named_struct nsty elts in
370 ignore (define_global "grec" grec_init m);
371 ignore (string_of_lltype nsty);
375 (*===-- Global Values -----------------------------------------------------===*)
377 let test_global_values () =
378 let (++) x f = f x; x in
379 let zero32 = const_null i32_type in
384 let g = define_global "TEMPORARY" zero32 m in
385 insist ("TEMPORARY" = value_name g);
386 set_value_name "GVal01" g;
387 insist ("GVal01" = value_name g);
389 (* CHECK: GVal02{{.*}}linkonce
392 let g = define_global "GVal02" zero32 m ++
393 set_linkage Linkage.Link_once in
394 insist (Linkage.Link_once = linkage g);
396 (* CHECK: GVal03{{.*}}Hanalei
399 let g = define_global "GVal03" zero32 m ++
400 set_section "Hanalei" in
401 insist ("Hanalei" = section g);
403 (* CHECK: GVal04{{.*}}hidden
406 let g = define_global "GVal04" zero32 m ++
407 set_visibility Visibility.Hidden in
408 insist (Visibility.Hidden = visibility g);
410 (* CHECK: GVal05{{.*}}align 128
413 let g = define_global "GVal05" zero32 m ++
415 insist (128 = alignment g)
418 (*===-- Global Variables --------------------------------------------------===*)
420 let test_global_variables () =
421 let (++) x f = f x; x in
422 let forty_two32 = const_int i32_type 42 in
424 group "declarations"; begin
425 (* CHECK: @GVar01 = external global i32
426 * CHECK: @QGVar01 = external addrspace(3) global i32
428 insist (None == lookup_global "GVar01" m);
429 let g = declare_global i32_type "GVar01" m in
430 insist (is_declaration g);
431 insist (pointer_type float_type ==
432 type_of (declare_global float_type "GVar01" m));
433 insist (g == declare_global i32_type "GVar01" m);
434 insist (match lookup_global "GVar01" m with Some x -> x = g
437 insist (None == lookup_global "QGVar01" m);
438 let g = declare_qualified_global i32_type "QGVar01" 3 m in
439 insist (is_declaration g);
440 insist (qualified_pointer_type float_type 3 ==
441 type_of (declare_qualified_global float_type "QGVar01" 3 m));
442 insist (g == declare_qualified_global i32_type "QGVar01" 3 m);
443 insist (match lookup_global "QGVar01" m with Some x -> x = g
447 group "definitions"; begin
448 (* CHECK: @GVar02 = global i32 42
449 * CHECK: @GVar03 = global i32 42
450 * CHECK: @QGVar02 = addrspace(3) global i32 42
451 * CHECK: @QGVar03 = addrspace(3) global i32 42
453 let g = define_global "GVar02" forty_two32 m in
454 let g2 = declare_global i32_type "GVar03" m ++
455 set_initializer forty_two32 in
456 insist (not (is_declaration g));
457 insist (not (is_declaration g2));
458 insist ((global_initializer g) == (global_initializer g2));
460 let g = define_qualified_global "QGVar02" forty_two32 3 m in
461 let g2 = declare_qualified_global i32_type "QGVar03" 3 m ++
462 set_initializer forty_two32 in
463 insist (not (is_declaration g));
464 insist (not (is_declaration g2));
465 insist ((global_initializer g) == (global_initializer g2));
468 (* CHECK: GVar04{{.*}}thread_local
471 let g = define_global "GVar04" forty_two32 m ++
472 set_thread_local true in
473 insist (is_thread_local g);
475 (* CHECK: GVar05{{.*}}thread_local(initialexec)
477 group "threadlocal_mode";
478 let g = define_global "GVar05" forty_two32 m ++
479 set_thread_local_mode ThreadLocalMode.InitialExec in
480 insist ((thread_local_mode g) = ThreadLocalMode.InitialExec);
482 (* CHECK: GVar06{{.*}}externally_initialized
484 group "externally_initialized";
485 let g = define_global "GVar06" forty_two32 m ++
486 set_externally_initialized true in
487 insist (is_externally_initialized g);
489 (* CHECK-NOWHERE-NOT: GVar07
492 let g = define_global "GVar07" forty_two32 m in
495 (* CHECK: ConstGlobalVar{{.*}}constant
498 let g = define_global "ConstGlobalVar" forty_two32 m in
499 insist (not (is_global_constant g));
500 set_global_constant true g;
501 insist (is_global_constant g);
503 begin group "iteration";
504 let m = create_module context "temp" in
506 insist (At_end m = global_begin m);
507 insist (At_start m = global_end m);
509 let g1 = declare_global i32_type "One" m in
510 let g2 = declare_global i32_type "Two" m in
512 insist (Before g1 = global_begin m);
513 insist (Before g2 = global_succ g1);
514 insist (At_end m = global_succ g2);
516 insist (After g2 = global_end m);
517 insist (After g1 = global_pred g2);
518 insist (At_start m = global_pred g1);
520 let lf s x = s ^ "->" ^ value_name x in
521 insist ("->One->Two" = fold_left_globals lf "" m);
523 let rf x s = value_name x ^ "<-" ^ s in
524 insist ("One<-Two<-" = fold_right_globals rf m "");
529 (* String globals built below are emitted here.
530 * CHECK: build_global_string{{.*}}stringval
534 (*===-- Uses --------------------------------------------------------------===*)
537 let ty = function_type i32_type [| i32_type; i32_type |] in
538 let fn = define_function "use_function" ty m in
539 let b = builder_at_end context (entry_block fn) in
541 let p1 = param fn 0 in
542 let p2 = param fn 1 in
543 let v1 = build_add p1 p2 "v1" b in
544 let v2 = build_add p1 v1 "v2" b in
545 let _ = build_add v1 v2 "v3" b in
547 let lf s u = value_name (user u) ^ "->" ^ s in
548 insist ("v2->v3->" = fold_left_uses lf "" v1);
549 let rf u s = value_name (user u) ^ "<-" ^ s in
550 insist ("v3<-v2<-" = fold_right_uses rf v1 "");
552 let lf s u = value_name (used_value u) ^ "->" ^ s in
553 insist ("v1->v1->" = fold_left_uses lf "" v1);
555 let rf u s = value_name (used_value u) ^ "<-" ^ s in
556 insist ("v1<-v1<-" = fold_right_uses rf v1 "");
558 ignore (build_unreachable b)
561 (*===-- Users -------------------------------------------------------------===*)
564 let ty = function_type i32_type [| i32_type; i32_type |] in
565 let fn = define_function "user_function" ty m in
566 let b = builder_at_end context (entry_block fn) in
568 let p1 = param fn 0 in
569 let p2 = param fn 1 in
570 let a3 = build_alloca i32_type "user_alloca" b in
571 let p3 = build_load a3 "user_load" b in
572 let i = build_add p1 p2 "sum" b in
574 insist ((num_operands i) = 2);
575 insist ((operand i 0) = p1);
576 insist ((operand i 1) = p2);
579 insist ((operand i 1) != p2);
580 insist ((operand i 1) = p3);
582 ignore (build_unreachable b)
585 (*===-- Aliases -----------------------------------------------------------===*)
587 let test_aliases () =
588 (* CHECK: @alias = alias i32* @aliasee
590 let forty_two32 = const_int i32_type 42 in
591 let v = define_global "aliasee" forty_two32 m in
592 ignore (add_alias m (pointer_type i32_type) v "alias")
595 (*===-- Functions ---------------------------------------------------------===*)
597 let test_functions () =
598 let ty = function_type i32_type [| i32_type; i64_type |] in
599 let ty2 = function_type i8_type [| i8_type; i64_type |] in
601 (* CHECK: declare i32 @Fn1(i32, i64)
603 begin group "declare";
604 insist (None = lookup_function "Fn1" m);
605 let fn = declare_function "Fn1" ty m in
606 insist (pointer_type ty = type_of fn);
607 insist (is_declaration fn);
608 insist (0 = Array.length (basic_blocks fn));
609 insist (pointer_type ty2 == type_of (declare_function "Fn1" ty2 m));
610 insist (fn == declare_function "Fn1" ty m);
611 insist (None <> lookup_function "Fn1" m);
612 insist (match lookup_function "Fn1" m with Some x -> x = fn
614 insist (m == global_parent fn)
617 (* CHECK-NOWHERE-NOT: Fn2
620 let fn = declare_function "Fn2" ty m in
623 (* CHECK: define{{.*}}Fn3
626 let fn = define_function "Fn3" ty m in
627 insist (not (is_declaration fn));
628 insist (1 = Array.length (basic_blocks fn));
629 ignore (build_unreachable (builder_at_end context (entry_block fn)));
631 (* CHECK: define{{.*}}Fn4{{.*}}Param1{{.*}}Param2
634 let fn = define_function "Fn4" ty m in
635 let params = params fn in
636 insist (2 = Array.length params);
637 insist (params.(0) = param fn 0);
638 insist (params.(1) = param fn 1);
639 insist (i32_type = type_of params.(0));
640 insist (i64_type = type_of params.(1));
641 set_value_name "Param1" params.(0);
642 set_value_name "Param2" params.(1);
643 ignore (build_unreachable (builder_at_end context (entry_block fn)));
645 (* CHECK: fastcc{{.*}}Fn5
648 let fn = define_function "Fn5" ty m in
649 insist (CallConv.c = function_call_conv fn);
650 set_function_call_conv CallConv.fast fn;
651 insist (CallConv.fast = function_call_conv fn);
652 ignore (build_unreachable (builder_at_end context (entry_block fn)));
655 (* CHECK: Fn6{{.*}}gc{{.*}}shadowstack
657 let fn = define_function "Fn6" ty m in
658 insist (None = gc fn);
659 set_gc (Some "ocaml") fn;
660 insist (Some "ocaml" = gc fn);
662 insist (None = gc fn);
663 set_gc (Some "shadowstack") fn;
664 ignore (build_unreachable (builder_at_end context (entry_block fn)));
667 begin group "iteration";
668 let m = create_module context "temp" in
670 insist (At_end m = function_begin m);
671 insist (At_start m = function_end m);
673 let f1 = define_function "One" ty m in
674 let f2 = define_function "Two" ty m in
676 insist (Before f1 = function_begin m);
677 insist (Before f2 = function_succ f1);
678 insist (At_end m = function_succ f2);
680 insist (After f2 = function_end m);
681 insist (After f1 = function_pred f2);
682 insist (At_start m = function_pred f1);
684 let lf s x = s ^ "->" ^ value_name x in
685 insist ("->One->Two" = fold_left_functions lf "" m);
687 let rf x s = value_name x ^ "<-" ^ s in
688 insist ("One<-Two<-" = fold_right_functions rf m "");
694 (*===-- Params ------------------------------------------------------------===*)
697 begin group "iteration";
698 let m = create_module context "temp" in
700 let vf = define_function "void" (function_type void_type [| |]) m in
702 insist (At_end vf = param_begin vf);
703 insist (At_start vf = param_end vf);
705 let ty = function_type void_type [| i32_type; i32_type |] in
706 let f = define_function "f" ty m in
707 let p1 = param f 0 in
708 let p2 = param f 1 in
709 set_value_name "One" p1;
710 set_value_name "Two" p2;
711 add_param_attr p1 Attribute.Sext;
712 add_param_attr p2 Attribute.Noalias;
713 remove_param_attr p2 Attribute.Noalias;
714 add_function_attr f Attribute.Nounwind;
715 add_function_attr f Attribute.Noreturn;
716 remove_function_attr f Attribute.Noreturn;
718 insist (Before p1 = param_begin f);
719 insist (Before p2 = param_succ p1);
720 insist (At_end f = param_succ p2);
722 insist (After p2 = param_end f);
723 insist (After p1 = param_pred p2);
724 insist (At_start f = param_pred p1);
726 let lf s x = s ^ "->" ^ value_name x in
727 insist ("->One->Two" = fold_left_params lf "" f);
729 let rf x s = value_name x ^ "<-" ^ s in
730 insist ("One<-Two<-" = fold_right_params rf f "");
736 (*===-- Basic Blocks ------------------------------------------------------===*)
738 let test_basic_blocks () =
739 let ty = function_type void_type [| |] in
744 let fn = declare_function "X" ty m in
745 let bb = append_block context "Bb1" fn in
746 insist (bb = entry_block fn);
747 ignore (build_unreachable (builder_at_end context bb));
749 (* CHECK-NOWHERE-NOT: Bb2
752 let fn = declare_function "X2" ty m in
753 let bb = append_block context "Bb2" fn in
757 let fn = declare_function "X3" ty m in
758 let bbb = append_block context "b" fn in
759 let bba = insert_block context "a" bbb in
760 insist ([| bba; bbb |] = basic_blocks fn);
761 ignore (build_unreachable (builder_at_end context bba));
762 ignore (build_unreachable (builder_at_end context bbb));
767 let fn = define_function "X4" ty m in
768 let bb = entry_block fn in
769 ignore (build_unreachable (builder_at_end context bb));
770 let bbv = value_of_block bb in
771 set_value_name "Bb3" bbv;
772 insist ("Bb3" = value_name bbv);
775 let fn = define_function "X5" ty m in
776 let bb = entry_block fn in
777 ignore (build_unreachable (builder_at_end context bb));
778 insist (bb = block_of_value (value_of_block bb));
779 insist (value_is_block (value_of_block bb));
780 insist (not (value_is_block (const_null i32_type)));
782 begin group "iteration";
783 let m = create_module context "temp" in
784 let f = declare_function "Temp" (function_type i32_type [| |]) m in
786 insist (At_end f = block_begin f);
787 insist (At_start f = block_end f);
789 let b1 = append_block context "One" f in
790 let b2 = append_block context "Two" f in
792 insist (Before b1 = block_begin f);
793 insist (Before b2 = block_succ b1);
794 insist (At_end f = block_succ b2);
796 insist (After b2 = block_end f);
797 insist (After b1 = block_pred b2);
798 insist (At_start f = block_pred b1);
800 let lf s x = s ^ "->" ^ value_name (value_of_block x) in
801 insist ("->One->Two" = fold_left_blocks lf "" f);
803 let rf x s = value_name (value_of_block x) ^ "<-" ^ s in
804 insist ("One<-Two<-" = fold_right_blocks rf f "");
810 (*===-- Instructions ------------------------------------------------------===*)
812 let test_instructions () =
813 begin group "iteration";
814 let m = create_module context "temp" in
815 let fty = function_type void_type [| i32_type; i32_type |] in
816 let f = define_function "f" fty m in
817 let bb = entry_block f in
818 let b = builder_at context (At_end bb) in
820 insist (At_end bb = instr_begin bb);
821 insist (At_start bb = instr_end bb);
823 let i1 = build_add (param f 0) (param f 1) "One" b in
824 let i2 = build_sub (param f 0) (param f 1) "Two" b in
826 insist (Before i1 = instr_begin bb);
827 insist (Before i2 = instr_succ i1);
828 insist (At_end bb = instr_succ i2);
830 insist (After i2 = instr_end bb);
831 insist (After i1 = instr_pred i2);
832 insist (At_start bb = instr_pred i1);
834 let lf s x = s ^ "->" ^ value_name x in
835 insist ("->One->Two" = fold_left_instrs lf "" bb);
837 let rf x s = value_name x ^ "<-" ^ s in
838 insist ("One<-Two<-" = fold_right_instrs rf bb "");
844 (*===-- Builder -----------------------------------------------------------===*)
846 let test_builder () =
847 let (++) x f = f x; x in
849 begin group "parent";
851 ignore (insertion_block (builder context));
856 let fty = function_type void_type [| i32_type |] in
857 let fn = define_function "BuilderParent" fty m in
858 let bb = entry_block fn in
859 let b = builder_at_end context bb in
860 let p = param fn 0 in
861 let sum = build_add p p "sum" b in
862 ignore (build_ret_void b);
864 insist (fn = block_parent bb);
865 insist (fn = param_parent p);
866 insist (bb = instr_parent sum);
867 insist (bb = insertion_block b)
874 let fty = function_type void_type [| |] in
875 let fn = declare_function "X6" fty m in
876 let b = builder_at_end context (append_block context "Bb01" fn) in
877 ignore (build_ret_void b)
880 group "ret aggregate";
882 (* CHECK: ret { i8, i64 } { i8 4, i64 5 }
884 let sty = struct_type context [| i8_type; i64_type |] in
885 let fty = function_type sty [| |] in
886 let fn = declare_function "XA6" fty m in
887 let b = builder_at_end context (append_block context "Bb01" fn) in
888 let agg = [| const_int i8_type 4; const_int i64_type 5 |] in
889 ignore (build_aggregate_ret agg b)
892 (* The rest of the tests will use one big function. *)
893 let fty = function_type i32_type [| i32_type; i32_type |] in
894 let fn = define_function "X7" fty m in
895 let atentry = builder_at_end context (entry_block fn) in
896 let p1 = param fn 0 ++ set_value_name "P1" in
897 let p2 = param fn 1 ++ set_value_name "P2" in
898 let f1 = build_uitofp p1 float_type "F1" atentry in
899 let f2 = build_uitofp p2 float_type "F2" atentry in
901 let bb00 = append_block context "Bb00" fn in
902 ignore (build_unreachable (builder_at_end context bb00));
904 group "function attribute";
906 ignore (add_function_attr fn Attribute.UWTable);
908 * #0 is uwtable, defined at EOF.
910 insist ([Attribute.UWTable] = function_attr fn);
914 let void_ptr = pointer_type i8_type in
916 (* CHECK-DAG: %build_trunc = trunc i32 %P1 to i8
917 * CHECK-DAG: %build_trunc2 = trunc i32 %P1 to i8
918 * CHECK-DAG: %build_trunc3 = trunc i32 %P1 to i8
919 * CHECK-DAG: %build_zext = zext i8 %build_trunc to i32
920 * CHECK-DAG: %build_zext2 = zext i8 %build_trunc to i32
921 * CHECK-DAG: %build_sext = sext i32 %build_zext to i64
922 * CHECK-DAG: %build_sext2 = sext i32 %build_zext to i64
923 * CHECK-DAG: %build_sext3 = sext i32 %build_zext to i64
924 * CHECK-DAG: %build_uitofp = uitofp i64 %build_sext to float
925 * CHECK-DAG: %build_sitofp = sitofp i32 %build_zext to double
926 * CHECK-DAG: %build_fptoui = fptoui float %build_uitofp to i32
927 * CHECK-DAG: %build_fptosi = fptosi double %build_sitofp to i64
928 * CHECK-DAG: %build_fptrunc = fptrunc double %build_sitofp to float
929 * CHECK-DAG: %build_fptrunc2 = fptrunc double %build_sitofp to float
930 * CHECK-DAG: %build_fpext = fpext float %build_fptrunc to double
931 * CHECK-DAG: %build_fpext2 = fpext float %build_fptrunc to double
932 * CHECK-DAG: %build_inttoptr = inttoptr i32 %P1 to i8*
933 * CHECK-DAG: %build_ptrtoint = ptrtoint i8* %build_inttoptr to i64
934 * CHECK-DAG: %build_ptrtoint2 = ptrtoint i8* %build_inttoptr to i64
935 * CHECK-DAG: %build_bitcast = bitcast i64 %build_ptrtoint to double
936 * CHECK-DAG: %build_bitcast2 = bitcast i64 %build_ptrtoint to double
937 * CHECK-DAG: %build_bitcast3 = bitcast i64 %build_ptrtoint to double
938 * CHECK-DAG: %build_bitcast4 = bitcast i64 %build_ptrtoint to double
939 * CHECK-DAG: %build_pointercast = bitcast i8* %build_inttoptr to i16*
941 let inst28 = build_trunc p1 i8_type "build_trunc" atentry in
942 let inst29 = build_zext inst28 i32_type "build_zext" atentry in
943 let inst30 = build_sext inst29 i64_type "build_sext" atentry in
944 let inst31 = build_uitofp inst30 float_type "build_uitofp" atentry in
945 let inst32 = build_sitofp inst29 double_type "build_sitofp" atentry in
946 ignore(build_fptoui inst31 i32_type "build_fptoui" atentry);
947 ignore(build_fptosi inst32 i64_type "build_fptosi" atentry);
948 let inst35 = build_fptrunc inst32 float_type "build_fptrunc" atentry in
949 ignore(build_fpext inst35 double_type "build_fpext" atentry);
950 let inst37 = build_inttoptr p1 void_ptr "build_inttoptr" atentry in
951 let inst38 = build_ptrtoint inst37 i64_type "build_ptrtoint" atentry in
952 ignore(build_bitcast inst38 double_type "build_bitcast" atentry);
953 ignore(build_zext_or_bitcast inst38 double_type "build_bitcast2" atentry);
954 ignore(build_sext_or_bitcast inst38 double_type "build_bitcast3" atentry);
955 ignore(build_trunc_or_bitcast inst38 double_type "build_bitcast4" atentry);
956 ignore(build_pointercast inst37 (pointer_type i16_type) "build_pointercast" atentry);
958 ignore(build_zext_or_bitcast inst28 i32_type "build_zext2" atentry);
959 ignore(build_sext_or_bitcast inst29 i64_type "build_sext2" atentry);
960 ignore(build_trunc_or_bitcast p1 i8_type "build_trunc2" atentry);
961 ignore(build_pointercast inst37 i64_type "build_ptrtoint2" atentry);
962 ignore(build_intcast inst29 i64_type "build_sext3" atentry);
963 ignore(build_intcast p1 i8_type "build_trunc3" atentry);
964 ignore(build_fpcast inst35 double_type "build_fpext2" atentry);
965 ignore(build_fpcast inst32 float_type "build_fptrunc2" atentry);
968 group "comparisons"; begin
969 (* CHECK: %build_icmp_ne = icmp ne i32 %P1, %P2
970 * CHECK: %build_icmp_sle = icmp sle i32 %P2, %P1
971 * CHECK: %build_fcmp_false = fcmp false float %F1, %F2
972 * CHECK: %build_fcmp_true = fcmp true float %F2, %F1
973 * CHECK: %build_is_null{{.*}}= icmp eq{{.*}}%X0,{{.*}}null
974 * CHECK: %build_is_not_null = icmp ne i8* %X1, null
975 * CHECK: %build_ptrdiff
977 ignore (build_icmp Icmp.Ne p1 p2 "build_icmp_ne" atentry);
978 ignore (build_icmp Icmp.Sle p2 p1 "build_icmp_sle" atentry);
979 ignore (build_fcmp Fcmp.False f1 f2 "build_fcmp_false" atentry);
980 ignore (build_fcmp Fcmp.True f2 f1 "build_fcmp_true" atentry);
981 let g0 = declare_global (pointer_type i8_type) "g0" m in
982 let g1 = declare_global (pointer_type i8_type) "g1" m in
983 let p0 = build_load g0 "X0" atentry in
984 let p1 = build_load g1 "X1" atentry in
985 ignore (build_is_null p0 "build_is_null" atentry);
986 ignore (build_is_not_null p1 "build_is_not_null" atentry);
987 ignore (build_ptrdiff p1 p0 "build_ptrdiff" atentry);
990 group "miscellaneous"; begin
991 (* CHECK: %build_call = tail call cc63 i32 @{{.*}}(i32 signext %P2, i32 %P1)
992 * CHECK: %build_select = select i1 %build_icmp, i32 %P1, i32 %P2
993 * CHECK: %build_va_arg = va_arg i8** null, i32
994 * CHECK: %build_extractelement = extractelement <4 x i32> %Vec1, i32 %P2
995 * CHECK: %build_insertelement = insertelement <4 x i32> %Vec1, i32 %P1, i32 %P2
996 * CHECK: %build_shufflevector = shufflevector <4 x i32> %Vec1, <4 x i32> %Vec2, <4 x i32> <i32 1, i32 1, i32 0, i32 0>
997 * CHECK: %build_insertvalue0 = insertvalue{{.*}}%bl, i32 1, 0
998 * CHECK: %build_extractvalue = extractvalue{{.*}}%build_insertvalue1, 1
1000 let ci = build_call fn [| p2; p1 |] "build_call" atentry in
1001 insist (CallConv.c = instruction_call_conv ci);
1002 set_instruction_call_conv 63 ci;
1003 insist (63 = instruction_call_conv ci);
1004 insist (not (is_tail_call ci));
1005 set_tail_call true ci;
1006 insist (is_tail_call ci);
1007 add_instruction_param_attr ci 1 Attribute.Sext;
1008 add_instruction_param_attr ci 2 Attribute.Noalias;
1009 remove_instruction_param_attr ci 2 Attribute.Noalias;
1011 let inst46 = build_icmp Icmp.Eq p1 p2 "build_icmp" atentry in
1012 ignore (build_select inst46 p1 p2 "build_select" atentry);
1013 ignore (build_va_arg
1014 (const_null (pointer_type (pointer_type i8_type)))
1015 i32_type "build_va_arg" atentry);
1017 (* Set up some vector vregs. *)
1018 let one = const_int i32_type 1 in
1019 let zero = const_int i32_type 0 in
1020 let t1 = const_vector [| one; zero; one; zero |] in
1021 let t2 = const_vector [| zero; one; zero; one |] in
1022 let t3 = const_vector [| one; one; zero; zero |] in
1023 let vec1 = build_insertelement t1 p1 p2 "Vec1" atentry in
1024 let vec2 = build_insertelement t2 p1 p2 "Vec2" atentry in
1025 let sty = struct_type context [| i32_type; i8_type |] in
1027 ignore (build_extractelement vec1 p2 "build_extractelement" atentry);
1028 ignore (build_insertelement vec1 p1 p2 "build_insertelement" atentry);
1029 ignore (build_shufflevector vec1 vec2 t3 "build_shufflevector" atentry);
1031 let p = build_alloca sty "ba" atentry in
1032 let agg = build_load p "bl" atentry in
1033 let agg0 = build_insertvalue agg (const_int i32_type 1) 0
1034 "build_insertvalue0" atentry in
1035 let agg1 = build_insertvalue agg0 (const_int i8_type 2) 1
1036 "build_insertvalue1" atentry in
1037 ignore (build_extractvalue agg1 1 "build_extractvalue" atentry)
1040 group "metadata"; begin
1041 (* CHECK: %metadata = add i32 %P1, %P2, !test !1
1042 * !1 is metadata emitted at EOF.
1044 let i = build_add p1 p2 "metadata" atentry in
1045 insist ((has_metadata i) = false);
1047 let m1 = const_int i32_type 1 in
1048 let m2 = mdstring context "metadata test" in
1049 let md = mdnode context [| m1; m2 |] in
1051 let kind = mdkind_id context "test" in
1052 set_metadata i kind md;
1054 insist ((has_metadata i) = true);
1055 insist ((metadata i kind) = Some md);
1057 clear_metadata i kind;
1059 insist ((has_metadata i) = false);
1060 insist ((metadata i kind) = None);
1062 set_metadata i kind md
1065 group "named metadata"; begin
1066 (* !llvm.module.flags is emitted at EOF. *)
1067 let n1 = const_int i32_type 1 in
1068 let n2 = mdstring context "Debug Info Version" in
1069 let md = mdnode context [| n1; n2; n1 |] in
1070 add_named_metadata_operand m "llvm.module.flags" md;
1072 insist ((get_named_metadata m "llvm.module.flags") = [| md |])
1076 (* CHECK: %dbg = add i32 %P1, %P2, !dbg !2
1077 * !2 is metadata emitted at EOF.
1079 insist ((current_debug_location atentry) = None);
1081 let m_line = const_int i32_type 2 in
1082 let m_col = const_int i32_type 3 in
1083 let m_scope = mdnode context [| |] in
1084 let m_inlined = mdnode context [| |] in
1085 let md = mdnode context [| m_line; m_col; m_scope; m_inlined |] in
1086 set_current_debug_location atentry md;
1088 insist ((current_debug_location atentry) = Some md);
1090 let i = build_add p1 p2 "dbg" atentry in
1091 insist ((has_metadata i) = true);
1093 clear_current_debug_location atentry
1097 (* CHECK: ret{{.*}}P1
1099 let ret = build_ret p1 atentry in
1100 position_before ret atentry
1103 (* see test/Feature/exception.ll *)
1104 let bblpad = append_block context "Bblpad" fn in
1105 let rt = struct_type context [| pointer_type i8_type; i32_type |] in
1106 let ft = var_arg_function_type i32_type [||] in
1107 let personality = declare_function "__gxx_personality_v0" ft m in
1108 let ztic = declare_global (pointer_type i8_type) "_ZTIc" m in
1109 let ztid = declare_global (pointer_type i8_type) "_ZTId" m in
1110 let ztipkc = declare_global (pointer_type i8_type) "_ZTIPKc" m in
1112 set_global_constant true ztic;
1113 set_global_constant true ztid;
1114 set_global_constant true ztipkc;
1115 let lp = build_landingpad rt personality 0 "lpad"
1116 (builder_at_end context bblpad) in begin
1117 set_cleanup lp true;
1119 insist((pointer_type (pointer_type i8_type)) = type_of ztid);
1120 let ety = pointer_type (pointer_type i8_type) in
1121 add_clause lp (const_array ety [| ztipkc; ztid |]);
1122 ignore (build_resume lp (builder_at_end context bblpad));
1124 (* CHECK: landingpad{{.*}}personality{{.*}}__gxx_personality_v0
1126 * CHECK: catch{{.*}}i8**{{.*}}@_ZTIc
1127 * CHECK: filter{{.*}}@_ZTIPKc{{.*}}@_ZTId
1133 (* CHECK: br{{.*}}Bb02
1135 let bb02 = append_block context "Bb02" fn in
1136 let b = builder_at_end context bb02 in
1137 ignore (build_br bb02 b)
1140 group "cond_br"; begin
1141 (* CHECK: br{{.*}}build_br{{.*}}Bb03{{.*}}Bb00
1143 let bb03 = append_block context "Bb03" fn in
1144 let b = builder_at_end context bb03 in
1145 let cond = build_trunc p1 i1_type "build_br" b in
1146 ignore (build_cond_br cond bb03 bb00 b)
1149 group "switch"; begin
1150 (* CHECK: switch{{.*}}P1{{.*}}SwiBlock3
1151 * CHECK: 2,{{.*}}SwiBlock2
1153 let bb1 = append_block context "SwiBlock1" fn in
1154 let bb2 = append_block context "SwiBlock2" fn in
1155 ignore (build_unreachable (builder_at_end context bb2));
1156 let bb3 = append_block context "SwiBlock3" fn in
1157 ignore (build_unreachable (builder_at_end context bb3));
1158 let si = build_switch p1 bb3 1 (builder_at_end context bb1) in begin
1159 ignore (add_case si (const_int i32_type 2) bb2);
1160 insist (switch_default_dest si = bb3);
1164 group "malloc/free"; begin
1165 (* CHECK: call{{.*}}@malloc(i32 ptrtoint
1166 * CHECK: call{{.*}}@free(i8*
1167 * CHECK: call{{.*}}@malloc(i32 %
1169 let bb1 = append_block context "MallocBlock1" fn in
1170 let m1 = (build_malloc (pointer_type i32_type) "m1"
1171 (builder_at_end context bb1)) in
1172 ignore (build_free m1 (builder_at_end context bb1));
1173 ignore (build_array_malloc i32_type p1 "m2" (builder_at_end context bb1));
1174 ignore (build_unreachable (builder_at_end context bb1));
1177 group "indirectbr"; begin
1178 (* CHECK: indirectbr i8* blockaddress(@X7, %IBRBlock2), [label %IBRBlock2, label %IBRBlock3]
1180 let bb1 = append_block context "IBRBlock1" fn in
1182 let bb2 = append_block context "IBRBlock2" fn in
1183 ignore (build_unreachable (builder_at_end context bb2));
1185 let bb3 = append_block context "IBRBlock3" fn in
1186 ignore (build_unreachable (builder_at_end context bb3));
1188 let addr = block_address fn bb2 in
1189 let ibr = build_indirect_br addr 2 (builder_at_end context bb1) in
1190 ignore (add_destination ibr bb2);
1191 ignore (add_destination ibr bb3)
1194 group "invoke"; begin
1195 (* CHECK: build_invoke{{.*}}invoke{{.*}}P1{{.*}}P2
1196 * CHECK: to{{.*}}Bb04{{.*}}unwind{{.*}}Bblpad
1198 let bb04 = append_block context "Bb04" fn in
1199 let b = builder_at_end context bb04 in
1200 ignore (build_invoke fn [| p1; p2 |] bb04 bblpad "build_invoke" b)
1203 group "unreachable"; begin
1204 (* CHECK: unreachable
1206 let bb06 = append_block context "Bb06" fn in
1207 let b = builder_at_end context bb06 in
1208 ignore (build_unreachable b)
1211 group "arithmetic"; begin
1212 let bb07 = append_block context "Bb07" fn in
1213 let b = builder_at_end context bb07 in
1215 (* CHECK: %build_add = add i32 %P1, %P2
1216 * CHECK: %build_nsw_add = add nsw i32 %P1, %P2
1217 * CHECK: %build_nuw_add = add nuw i32 %P1, %P2
1218 * CHECK: %build_fadd = fadd float %F1, %F2
1219 * CHECK: %build_sub = sub i32 %P1, %P2
1220 * CHECK: %build_nsw_sub = sub nsw i32 %P1, %P2
1221 * CHECK: %build_nuw_sub = sub nuw i32 %P1, %P2
1222 * CHECK: %build_fsub = fsub float %F1, %F2
1223 * CHECK: %build_mul = mul i32 %P1, %P2
1224 * CHECK: %build_nsw_mul = mul nsw i32 %P1, %P2
1225 * CHECK: %build_nuw_mul = mul nuw i32 %P1, %P2
1226 * CHECK: %build_fmul = fmul float %F1, %F2
1227 * CHECK: %build_udiv = udiv i32 %P1, %P2
1228 * CHECK: %build_sdiv = sdiv i32 %P1, %P2
1229 * CHECK: %build_exact_sdiv = sdiv exact i32 %P1, %P2
1230 * CHECK: %build_fdiv = fdiv float %F1, %F2
1231 * CHECK: %build_urem = urem i32 %P1, %P2
1232 * CHECK: %build_srem = srem i32 %P1, %P2
1233 * CHECK: %build_frem = frem float %F1, %F2
1234 * CHECK: %build_shl = shl i32 %P1, %P2
1235 * CHECK: %build_lshl = lshr i32 %P1, %P2
1236 * CHECK: %build_ashl = ashr i32 %P1, %P2
1237 * CHECK: %build_and = and i32 %P1, %P2
1238 * CHECK: %build_or = or i32 %P1, %P2
1239 * CHECK: %build_xor = xor i32 %P1, %P2
1240 * CHECK: %build_neg = sub i32 0, %P1
1241 * CHECK: %build_nsw_neg = sub nsw i32 0, %P1
1242 * CHECK: %build_nuw_neg = sub nuw i32 0, %P1
1243 * CHECK: %build_fneg = fsub float {{.*}}0{{.*}}, %F1
1244 * CHECK: %build_not = xor i32 %P1, -1
1246 ignore (build_add p1 p2 "build_add" b);
1247 ignore (build_nsw_add p1 p2 "build_nsw_add" b);
1248 ignore (build_nuw_add p1 p2 "build_nuw_add" b);
1249 ignore (build_fadd f1 f2 "build_fadd" b);
1250 ignore (build_sub p1 p2 "build_sub" b);
1251 ignore (build_nsw_sub p1 p2 "build_nsw_sub" b);
1252 ignore (build_nuw_sub p1 p2 "build_nuw_sub" b);
1253 ignore (build_fsub f1 f2 "build_fsub" b);
1254 ignore (build_mul p1 p2 "build_mul" b);
1255 ignore (build_nsw_mul p1 p2 "build_nsw_mul" b);
1256 ignore (build_nuw_mul p1 p2 "build_nuw_mul" b);
1257 ignore (build_fmul f1 f2 "build_fmul" b);
1258 ignore (build_udiv p1 p2 "build_udiv" b);
1259 ignore (build_sdiv p1 p2 "build_sdiv" b);
1260 ignore (build_exact_sdiv p1 p2 "build_exact_sdiv" b);
1261 ignore (build_fdiv f1 f2 "build_fdiv" b);
1262 ignore (build_urem p1 p2 "build_urem" b);
1263 ignore (build_srem p1 p2 "build_srem" b);
1264 ignore (build_frem f1 f2 "build_frem" b);
1265 ignore (build_shl p1 p2 "build_shl" b);
1266 ignore (build_lshr p1 p2 "build_lshl" b);
1267 ignore (build_ashr p1 p2 "build_ashl" b);
1268 ignore (build_and p1 p2 "build_and" b);
1269 ignore (build_or p1 p2 "build_or" b);
1270 ignore (build_xor p1 p2 "build_xor" b);
1271 ignore (build_neg p1 "build_neg" b);
1272 ignore (build_nsw_neg p1 "build_nsw_neg" b);
1273 ignore (build_nuw_neg p1 "build_nuw_neg" b);
1274 ignore (build_fneg f1 "build_fneg" b);
1275 ignore (build_not p1 "build_not" b);
1276 ignore (build_unreachable b)
1279 group "memory"; begin
1280 let bb08 = append_block context "Bb08" fn in
1281 let b = builder_at_end context bb08 in
1283 (* CHECK: %build_alloca = alloca i32
1284 * CHECK: %build_array_alloca = alloca i32, i32 %P2
1285 * CHECK: %build_load = load volatile i32* %build_array_alloca, align 4
1286 * CHECK: store volatile i32 %P2, i32* %build_alloca, align 4
1287 * CHECK: %build_gep = getelementptr i32* %build_array_alloca, i32 %P2
1288 * CHECK: %build_in_bounds_gep = getelementptr inbounds i32* %build_array_alloca, i32 %P2
1289 * CHECK: %build_struct_gep = getelementptr inbounds{{.*}}%build_alloca2, i32 0, i32 1
1290 * CHECK: %build_atomicrmw = atomicrmw xchg i8* %p, i8 42 seq_cst
1292 let alloca = build_alloca i32_type "build_alloca" b in
1293 let array_alloca = build_array_alloca i32_type p2 "build_array_alloca" b in
1295 let load = build_load array_alloca "build_load" b in
1296 ignore(set_alignment 4 load);
1297 ignore(set_volatile true load);
1298 insist(true = is_volatile load);
1299 insist(4 = alignment load);
1301 let store = build_store p2 alloca b in
1302 ignore(set_volatile true store);
1303 ignore(set_alignment 4 store);
1304 insist(true = is_volatile store);
1305 insist(4 = alignment store);
1306 ignore(build_gep array_alloca [| p2 |] "build_gep" b);
1307 ignore(build_in_bounds_gep array_alloca [| p2 |] "build_in_bounds_gep" b);
1309 let sty = struct_type context [| i32_type; i8_type |] in
1310 let alloca2 = build_alloca sty "build_alloca2" b in
1311 ignore(build_struct_gep alloca2 1 "build_struct_gep" b);
1313 let p = build_alloca i8_type "p" b in
1314 ignore(build_atomicrmw AtomicRMWBinOp.Xchg p (const_int i8_type 42)
1315 AtomicOrdering.SequentiallyConsistent false "build_atomicrmw"
1318 ignore(build_unreachable b)
1321 group "string"; begin
1322 let bb09 = append_block context "Bb09" fn in
1323 let b = builder_at_end context bb09 in
1324 let p = build_alloca (pointer_type i8_type) "p" b in
1325 (* build_global_string is emitted above.
1326 * CHECK: store{{.*}}build_global_string1{{.*}}p
1328 ignore (build_global_string "stringval" "build_global_string" b);
1329 let g = build_global_stringptr "stringval" "build_global_string1" b in
1330 ignore (build_store g p b);
1331 ignore(build_unreachable b);
1335 (* CHECK: PhiNode{{.*}}P1{{.*}}PhiBlock1{{.*}}P2{{.*}}PhiBlock2
1337 let b1 = append_block context "PhiBlock1" fn in
1338 let b2 = append_block context "PhiBlock2" fn in
1340 let jb = append_block context "PhiJoinBlock" fn in
1341 ignore (build_br jb (builder_at_end context b1));
1342 ignore (build_br jb (builder_at_end context b2));
1343 let at_jb = builder_at_end context jb in
1345 let phi = build_phi [(p1, b1)] "PhiNode" at_jb in
1346 insist ([(p1, b1)] = incoming phi);
1348 add_incoming (p2, b2) phi;
1349 insist ([(p1, b1); (p2, b2)] = incoming phi);
1351 ignore (build_unreachable at_jb);
1354 (* End-of-file checks for things like metdata and attributes.
1355 * CHECK: attributes #0 = {{.*}}uwtable{{.*}}
1356 * CHECK: !llvm.module.flags = !{!0}
1357 * CHECK: !0 = metadata !{i32 1, metadata !"Debug Info Version", i32 1}
1358 * CHECK: !1 = metadata !{i32 1, metadata !"metadata test"}
1359 * CHECK: !2 = metadata !{i32 2, i32 3, metadata !3, metadata !3}
1362 (*===-- Pass Managers -----------------------------------------------------===*)
1364 let test_pass_manager () =
1365 let (++) x f = ignore (f x); x in
1367 begin group "module pass manager";
1368 ignore (PassManager.create ()
1369 ++ PassManager.run_module m
1370 ++ PassManager.dispose)
1373 begin group "function pass manager";
1374 let fty = function_type void_type [| |] in
1375 let fn = define_function "FunctionPassManager" fty m in
1376 ignore (build_ret_void (builder_at_end context (entry_block fn)));
1378 ignore (PassManager.create_function m
1379 ++ PassManager.initialize
1380 ++ PassManager.run_function fn
1381 ++ PassManager.finalize
1382 ++ PassManager.dispose)
1386 (*===-- Memory Buffer -----------------------------------------------------===*)
1388 let test_memory_buffer () =
1389 group "memory buffer";
1390 let buf = MemoryBuffer.of_string "foobar" in
1391 insist ((MemoryBuffer.as_string buf) = "foobar")
1394 (*===-- Writer ------------------------------------------------------------===*)
1396 let test_writer () =
1398 insist (match Llvm_analysis.verify_module m with
1400 | Some msg -> prerr_string msg; false);
1403 insist (write_bitcode_file m filename);
1408 (*===-- Driver ------------------------------------------------------------===*)
1411 suite "conversion" test_conversion;
1412 suite "target" test_target;
1413 suite "constants" test_constants;
1414 suite "global values" test_global_values;
1415 suite "global variables" test_global_variables;
1416 suite "uses" test_uses;
1417 suite "users" test_users;
1418 suite "aliases" test_aliases;
1419 suite "functions" test_functions;
1420 suite "params" test_params;
1421 suite "basic blocks" test_basic_blocks;
1422 suite "instructions" test_instructions;
1423 suite "builder" test_builder;
1424 suite "pass manager" test_pass_manager;
1425 suite "memory buffer" test_memory_buffer;
1426 suite "writer" test_writer; (* Keep this last; it disposes m. *)