llvm.org GIT mirror llvm / 033d778
Extend the builder interface to use the new instruction positioning code. This adds support for instruction iterators, as well as rewriting the builder code to use these new functions. This lets us eliminate the C bindings for moving around the builder. Patch by Erick Tryzelaar! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@48774 91177308-0d34-0410-b5e6-96231b3b80d8 Gordon Henriksen 12 years ago
3 changed file(s) with 130 addition(s) and 36 deletion(s). Raw diff Collapse all Expand all
530530
531531 (*--... Operations on instructions .........................................--*)
532532 external instr_parent : llvalue -> llbasicblock = "LLVMGetInstructionParent"
533 external instr_begin : llbasicblock -> (llbasicblock, llvalue) llpos
534 = "llvm_instr_begin"
535 external instr_succ : llvalue -> (llbasicblock, llvalue) llpos
536 = "llvm_instr_succ"
537 external instr_end : llbasicblock -> (llbasicblock, llvalue) llrev_pos
538 = "llvm_instr_end"
539 external instr_pred : llvalue -> (llbasicblock, llvalue) llrev_pos
540 = "llvm_instr_pred"
541
542 let rec iter_instrs_range f i e =
543 if i = e then () else
544 match i with
545 | At_end _ -> raise (Invalid_argument "Invalid instruction range.")
546 | Before i ->
547 f i;
548 iter_instrs_range f (instr_succ i) e
549
550 let iter_instrs f bb =
551 iter_instrs_range f (instr_begin bb) (At_end bb)
552
553 let rec fold_left_instrs_range f init i e =
554 if i = e then init else
555 match i with
556 | At_end _ -> raise (Invalid_argument "Invalid instruction range.")
557 | Before i -> fold_left_instrs_range f (f init i) (instr_succ i) e
558
559 let fold_left_instrs f init bb =
560 fold_left_instrs_range f init (instr_begin bb) (At_end bb)
561
562 let rec rev_iter_instrs_range f i e =
563 if i = e then () else
564 match i with
565 | At_start _ -> raise (Invalid_argument "Invalid instruction range.")
566 | After i ->
567 f i;
568 rev_iter_instrs_range f (instr_pred i) e
569
570 let rev_iter_instrs f bb =
571 rev_iter_instrs_range f (instr_end bb) (At_start bb)
572
573 let rec fold_right_instr_range f i e init =
574 if i = e then init else
575 match i with
576 | At_start _ -> raise (Invalid_argument "Invalid instruction range.")
577 | After i -> fold_right_instr_range f (instr_pred i) e (f i init)
578
579 let fold_right_instrs f bb init =
580 fold_right_instr_range f (instr_end bb) (At_start bb) init
581
533582
534583 (*--... Operations on call sites ...........................................--*)
535584 external instruction_call_conv: llvalue -> int
544593
545594
546595 (*===-- Instruction builders ----------------------------------------------===*)
547 external builder: unit-> llbuilder = "llvm_builder"
548 external builder_before : llvalue -> llbuilder = "llvm_builder_before"
549 external builder_at_end : llbasicblock -> llbuilder = "llvm_builder_at_end"
550 external position_before : llvalue -> llbuilder -> unit = "llvm_position_before"
551 external position_at_end : llbasicblock -> llbuilder -> unit
552 = "llvm_position_at_end"
596 external builder : unit -> llbuilder = "llvm_builder"
597 external position_builder : (llbasicblock, llvalue) llpos -> llbuilder -> unit
598 = "llvm_position_builder"
553599 external insertion_block : llbuilder -> llbasicblock = "llvm_insertion_block"
600
601 let builder_at ip =
602 let b = builder () in
603 position_builder ip b;
604 b
605
606 let builder_before i = builder_at (Before i)
607 let builder_at_end bb = builder_at (At_end bb)
608
609 let position_before i = position_builder (Before i)
610 let position_at_end bb = position_builder (At_end bb)
611
554612
555613 (*--... Terminators ........................................................--*)
556614 external build_ret_void : llbuilder -> llvalue = "llvm_build_ret_void"
10761076 See the method [llvm::Instruction::getParent]. *)
10771077 external instr_parent : llvalue -> llbasicblock = "LLVMGetInstructionParent"
10781078
1079 (** [instr_begin bb] returns the first position in the instruction list of the
1080 basic block [bb]. [instr_begin] and [instr_succ] can be used to iterate over
1081 the instruction list in order.
1082 See the method [llvm::BasicBlock::begin]. *)
1083 external instr_begin : llbasicblock -> (llbasicblock, llvalue) llpos
1084 = "llvm_instr_begin"
1085
1086 (** [instr_succ i] returns the instruction list position succeeding [Before i].
1087 See the method [llvm::BasicBlock::iterator::operator++]. *)
1088 external instr_succ : llvalue -> (llbasicblock, llvalue) llpos
1089 = "llvm_instr_succ"
1090
1091 (** [iter_instrs f bb] applies function [f] to each of the instructions of basic
1092 block [bb] in order. Tail recursive. *)
1093 val iter_instrs: (llvalue -> unit) -> llbasicblock -> unit
1094
1095 (** [fold_left_instrs f init bb] is [f (... (f init g1) ...) gN] where
1096 [g1,...,gN] are the instructions of basic block [bb]. Tail recursive. *)
1097 val fold_left_instrs: ('a -> llvalue -> 'a) -> 'a -> llbasicblock -> 'a
1098
1099 (** [instr_end bb] returns the last position in the instruction list of the
1100 basic block [bb]. [instr_end] and [instr_pred] can be used to iterate over
1101 the instruction list in reverse.
1102 See the method [llvm::BasicBlock::end]. *)
1103 external instr_end : llbasicblock -> (llbasicblock, llvalue) llrev_pos
1104 = "llvm_instr_end"
1105
1106 (** [instr_pred i] returns the instruction list position preceding [After i].
1107 See the method [llvm::BasicBlock::iterator::operator--]. *)
1108 external instr_pred : llvalue -> (llbasicblock, llvalue) llrev_pos
1109 = "llvm_instr_pred"
1110
1111 (** [fold_right_instrs f bb init] is [f (... (f init fN) ...) f1] where
1112 [f1,...,fN] are the instructions of basic block [bb]. Tail recursive. *)
1113 val fold_right_instrs: (llvalue -> 'a -> 'a) -> llbasicblock -> 'a -> 'a
1114
10791115
10801116 (** {7 Operations on call sites} *)
10811117
11131149 (** [builder ()] creates an instruction builder with no position. It is invalid
11141150 to use this builder until its position is set with {!position_before} or
11151151 {!position_at_end}. See the constructor for [llvm::LLVMBuilder]. *)
1116 external builder: unit-> llbuilder
1117 = "llvm_builder"
1152 external builder : unit -> llbuilder = "llvm_builder"
1153
1154 (** [builder_at ip] creates an instruction builder positioned at [ip].
1155 See the constructor for [llvm::LLVMBuilder]. *)
1156 val builder_at : (llbasicblock, llvalue) llpos -> llbuilder
11181157
11191158 (** [builder_before ins] creates an instruction builder positioned before the
11201159 instruction [isn]. See the constructor for [llvm::LLVMBuilder]. *)
1121 external builder_before : llvalue -> llbuilder = "llvm_builder_before"
1160 val builder_before : llvalue -> llbuilder
11221161
11231162 (** [builder_at_end bb] creates an instruction builder positioned at the end of
11241163 the basic block [bb]. See the constructor for [llvm::LLVMBuilder]. *)
1125 external builder_at_end : llbasicblock -> llbuilder = "llvm_builder_at_end"
1164 val builder_at_end : llbasicblock -> llbuilder
1165
1166 (** [position_builder ip bb] moves the instruction builder [bb] to the position
1167 [ip].
1168 See the constructor for [llvm::LLVMBuilder]. *)
1169 external position_builder : (llbasicblock, llvalue) llpos -> llbuilder -> unit
1170 = "llvm_position_builder"
11261171
11271172 (** [position_before ins b] moves the instruction builder [b] to before the
11281173 instruction [isn]. See the method [llvm::LLVMBuilder::SetInsertPoint]. *)
1129 external position_before : llvalue -> llbuilder -> unit = "llvm_position_before"
1174 val position_before : llvalue -> llbuilder -> unit
11301175
11311176 (** [position_at_end bb b] moves the instruction builder [b] to the end of the
11321177 basic block [bb]. See the method [llvm::LLVMBuilder::SetInsertPoint]. *)
1133 external position_at_end : llbasicblock -> llbuilder -> unit
1134 = "llvm_position_at_end"
1178 val position_at_end : llbasicblock -> llbuilder -> unit
11351179
11361180 (** [insertion_block b] returns the basic block that the builder [b] is
11371181 positioned to insert into. Raises [Not_Found] if the instruction builder is
713713 return Val_bool(LLVMValueIsBasicBlock(Val));
714714 }
715715
716 /*--... Operations on instructions .........................................--*/
717
718 DEFINE_ITERATORS(instr, Instruction, LLVMBasicBlockRef, LLVMValueRef,
719 LLVMGetInstructionParent)
720
721
716722 /*--... Operations on call sites ...........................................--*/
717723
718724 /* llvalue -> int */
788794 return alloc_builder(LLVMCreateBuilder());
789795 }
790796
791 /* llvalue -> llbuilder */
792 CAMLprim value llvm_builder_before(LLVMValueRef Inst) {
793 LLVMBuilderRef B = LLVMCreateBuilder();
794 LLVMPositionBuilderBefore(B, Inst);
795 return alloc_builder(B);
796 }
797
798 /* llbasicblock -> llbuilder */
799 CAMLprim value llvm_builder_at_end(LLVMBasicBlockRef BB) {
800 LLVMBuilderRef B = LLVMCreateBuilder();
801 LLVMPositionBuilderAtEnd(B, BB);
802 return alloc_builder(B);
803 }
804
805 /* llvalue -> llbuilder -> unit */
806 CAMLprim value llvm_position_before(LLVMValueRef Inst, value B) {
807 LLVMPositionBuilderBefore(Builder_val(B), Inst);
808 return Val_unit;
809 }
810
811 /* llbasicblock -> llbuilder -> unit */
812 CAMLprim value llvm_position_at_end(LLVMBasicBlockRef BB, value B) {
813 LLVMPositionBuilderAtEnd(Builder_val(B), BB);
797 /* (llbasicblock, llvalue) llpos -> llbuilder -> unit */
798 CAMLprim value llvm_position_builder(value Pos, value B) {
799 if (Tag_val(Pos) == 0) {
800 LLVMBasicBlockRef BB = (LLVMBasicBlockRef) Op_val(Field(Pos, 0));
801 LLVMPositionBuilderAtEnd(Builder_val(B), BB);
802 } else {
803 LLVMValueRef I = (LLVMValueRef) Op_val(Field(Pos, 0));
804 LLVMPositionBuilderBefore(Builder_val(B), I);
805 }
814806 return Val_unit;
815807 }
816808