diff --git a/bindings/ocaml/llvm/llvm.ml b/bindings/ocaml/llvm/llvm.ml index 042edcba84e..9a3cb1f0de0 100644 --- a/bindings/ocaml/llvm/llvm.ml +++ b/bindings/ocaml/llvm/llvm.ml @@ -1300,6 +1300,8 @@ external build_fcmp : Fcmp.t -> llvalue -> llvalue -> string -> (*--... Miscellaneous instructions .........................................--*) external build_phi : (llvalue * llbasicblock) list -> string -> llbuilder -> llvalue = "llvm_build_phi" +external build_empty_phi : lltype -> string -> llbuilder -> llvalue + = "llvm_build_empty_phi" external build_call : llvalue -> llvalue array -> string -> llbuilder -> llvalue = "llvm_build_call" external build_select : llvalue -> llvalue -> llvalue -> string -> llbuilder -> diff --git a/bindings/ocaml/llvm/llvm.mli b/bindings/ocaml/llvm/llvm.mli index 8fdddd1895e..dcda02764f5 100644 --- a/bindings/ocaml/llvm/llvm.mli +++ b/bindings/ocaml/llvm/llvm.mli @@ -2422,6 +2422,12 @@ val build_fcmp : Fcmp.t -> llvalue -> llvalue -> string -> val build_phi : (llvalue * llbasicblock) list -> string -> llbuilder -> llvalue +(** [build_empty_phi ty name b] creates a + [%name = phi %ty] instruction at the position specified by + the instruction builder [b]. [ty] is the type of the instruction. + See the method [llvm::LLVMBuilder::CreatePHI]. *) +val build_empty_phi : lltype -> string -> llbuilder -> llvalue + (** [build_call fn args name b] creates a [%name = call %fn(args...)] instruction at the position specified by the instruction builder [b]. diff --git a/bindings/ocaml/llvm/llvm_ocaml.c b/bindings/ocaml/llvm/llvm_ocaml.c index 1fa4d0f32d0..3889f9276cc 100644 --- a/bindings/ocaml/llvm/llvm_ocaml.c +++ b/bindings/ocaml/llvm/llvm_ocaml.c @@ -2191,6 +2191,15 @@ CAMLprim LLVMValueRef llvm_build_phi(value Incoming, value Name, value B) { return PhiNode; } +/* lltype -> string -> llbuilder -> value */ +CAMLprim LLVMValueRef llvm_build_empty_phi(LLVMTypeRef Type, value Name, value B) { + LLVMValueRef PhiNode; + + return LLVMBuildPhi(Builder_val(B), Type, String_val(Name)); + + return PhiNode; +} + /* llvalue -> llvalue array -> string -> llbuilder -> llvalue */ CAMLprim LLVMValueRef llvm_build_call(LLVMValueRef Fn, value Params, value Name, value B) { diff --git a/test/Bindings/OCaml/core.ml b/test/Bindings/OCaml/core.ml index 0f3daac5bfd..c5e47e79cf2 100644 --- a/test/Bindings/OCaml/core.ml +++ b/test/Bindings/OCaml/core.ml @@ -1428,6 +1428,15 @@ let test_builder () = add_incoming (p2, b2) phi; insist ([(p1, b1); (p2, b2)] = incoming phi); + (* CHECK: %PhiEmptyNode = phi i8 + *) + let phi_empty = build_empty_phi i8_type "PhiEmptyNode" at_jb in + insist ([] = incoming phi_empty); + + (* can't emit an empty phi to bitcode *) + add_incoming (const_int i8_type 1, b1) phi_empty; + add_incoming (const_int i8_type 2, b2) phi_empty; + ignore (build_unreachable at_jb); end