llvm.org GIT mirror llvm / 705443f
Add support for use to ocaml. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@97586 91177308-0d34-0410-b5e6-96231b3b80d8 Erick Tryzelaar 10 years ago
4 changed file(s) with 132 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
1212 type lltype
1313 type lltypehandle
1414 type llvalue
15 type lluse
1516 type llbasicblock
1617 type llbuilder
1718 type llmoduleprovider
240241 external dump_value : llvalue -> unit = "llvm_dump_value"
241242 external replace_all_uses_with : llvalue -> llvalue -> unit
242243 = "LLVMReplaceAllUsesWith"
244
245 (*--... Operations on uses .................................................--*)
246 external use_begin : llvalue -> lluse option = "llvm_use_begin"
247 external use_succ : lluse -> lluse option = "llvm_use_succ"
248 external user : lluse -> llvalue = "llvm_user"
249 external used_value : lluse -> llvalue = "llvm_used_value"
250
251 let iter_uses f v =
252 let rec aux = function
253 | None -> ()
254 | Some u ->
255 f u;
256 aux (use_succ u)
257 in
258 aux (use_begin v)
259
260 let fold_left_uses f init v =
261 let rec aux init u =
262 match u with
263 | None -> init
264 | Some u -> aux (f init u) (use_succ u)
265 in
266 aux init (use_begin v)
267
268 let fold_right_uses f v init =
269 let rec aux u init =
270 match u with
271 | None -> init
272 | Some u -> f u (aux (use_succ u) init)
273 in
274 aux (use_begin v) init
275
243276
244277 (*--... Operations on users ................................................--*)
245278 external operand : llvalue -> int -> llvalue = "llvm_operand"
3737 constants, and much more are all [llvalues]. See the [llvm::Value] class.
3838 This type covers a wide range of subclasses. *)
3939 type llvalue
40
41 (** Used to store users and usees of values. See the [llvm::Use] class. *)
42 type lluse
4043
4144 (** A basic block in LLVM IR. See the [llvm::BasicBlock] class. *)
4245 type llbasicblock
510513 * with the value [new]. See the method [llvm::Value::replaceAllUsesWith]. *)
511514 external replace_all_uses_with : llvalue -> llvalue -> unit
512515 = "LLVMReplaceAllUsesWith"
516
517
518 (* {6 Uses} *)
519
520 (** [use_begin v] returns the first position in the use list for the value [v].
521 [use_begin] and [use_succ] can e used to iterate over the use list in order.
522 See the method [llvm::Value::use_begin]. *)
523 external use_begin : llvalue -> lluse option = "llvm_use_begin"
524
525 (** [use_succ u] returns the use list position succeeding [u].
526 See the method [llvm::use_value_iterator::operator++]. *)
527 external use_succ : lluse -> lluse option = "llvm_use_succ"
528
529 (** [user u] returns the user of the use [u].
530 See the method [llvm::Use::getUser]. *)
531 external user : lluse -> llvalue = "llvm_user"
532
533 (** [used_value u] returns the usee of the use [u].
534 See the method [llvm::Use::getUsedValue]. *)
535 external used_value : lluse -> llvalue = "llvm_used_value"
536
537 (** [iter_uses f v] applies function [f] to each of the users of the value [v]
538 in order. Tail recursive. *)
539 val iter_uses : (lluse -> unit) -> llvalue -> unit
540
541 (** [fold_left_uses f init v] is [f (... (f init u1) ...) uN] where
542 [u1,...,uN] are the users of the value [v]. Tail recursive. *)
543 val fold_left_uses : ('a -> lluse -> 'a) -> 'a -> llvalue -> 'a
544
545 (** [fold_right_uses f v init] is [f u1 (... (f uN init) ...)] where
546 [u1,...,uN] are the users of the value [v]. Not tail recursive. *)
547 val fold_right_uses : (lluse -> 'a -> 'a) -> llvalue -> 'a -> 'a
513548
514549
515550 (* {6 Users} *)
706706 return Val_unit;
707707 }
708708
709 /*--... Operations on uses .................................................--*/
710
711 /* llvalue -> lluse option */
712 CAMLprim value llvm_use_begin(LLVMValueRef Val) {
713 CAMLparam0();
714 LLVMUseRef First;
715 if ((First = LLVMGetFirstUse(Val))) {
716 value Option = alloc(1, 0);
717 Field(Option, 0) = (value) First;
718 CAMLreturn(Option);
719 }
720 CAMLreturn(Val_int(0));
721 }
722
723 /* lluse -> lluse option */
724 CAMLprim value llvm_use_succ(LLVMUseRef U) {
725 CAMLparam0();
726 LLVMUseRef Next;
727 if ((Next = LLVMGetNextUse(U))) {
728 value Option = alloc(1, 0);
729 Field(Option, 0) = (value) Next;
730 CAMLreturn(Option);
731 }
732 CAMLreturn(Val_int(0));
733 }
734
735 /* lluse -> llvalue */
736 CAMLprim LLVMValueRef llvm_user(LLVMUseRef UR) {
737 return LLVMGetUser(UR);
738 }
739
740 /* lluse -> llvalue */
741 CAMLprim LLVMValueRef llvm_used_value(LLVMUseRef UR) {
742 return LLVMGetUsedValue(UR);
743 }
744
709745 /*--... Operations on global variables .....................................--*/
710746
711747 DEFINE_ITERATORS(global, Global, LLVMModuleRef, LLVMValueRef,
606606 end
607607
608608
609 (*===-- Uses --------------------------------------------------------------===*)
610
611 let test_uses () =
612 let ty = function_type i32_type [| i32_type; i32_type |] in
613 let fn = define_function "use_function" ty m in
614 let b = builder_at_end context (entry_block fn) in
615
616 let p1 = param fn 0 in
617 let p2 = param fn 1 in
618 let v1 = build_add p1 p2 "v1" b in
619 let v2 = build_add p1 v1 "v2" b in
620 let _ = build_add v1 v2 "v3" b in
621
622 let lf s u = value_name (user u) ^ "->" ^ s in
623 insist ("v2->v3->" = fold_left_uses lf "" v1);
624 let rf u s = value_name (user u) ^ "<-" ^ s in
625 insist ("v3<-v2<-" = fold_right_uses rf v1 "");
626
627 let lf s u = value_name (used_value u) ^ "->" ^ s in
628 insist ("v1->v1->" = fold_left_uses lf "" v1);
629
630 let rf u s = value_name (used_value u) ^ "<-" ^ s in
631 insist ("v1<-v1<-" = fold_right_uses rf v1 "");
632
633 ignore (build_unreachable b)
634
635
609636 (*===-- Users -------------------------------------------------------------===*)
610637
611638 let test_users () =
12901317 suite "constants" test_constants;
12911318 suite "global values" test_global_values;
12921319 suite "global variables" test_global_variables;
1320 suite "uses" test_uses;
12931321 suite "users" test_users;
12941322 suite "aliases" test_aliases;
12951323 suite "functions" test_functions;