summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bindings/ocaml/Makefile2
-rw-r--r--bindings/ocaml/transforms/Makefile13
-rw-r--r--bindings/ocaml/transforms/scalar/Makefile20
-rw-r--r--bindings/ocaml/transforms/scalar/llvm_scalar_opts.ml24
-rw-r--r--bindings/ocaml/transforms/scalar/llvm_scalar_opts.mli38
-rw-r--r--bindings/ocaml/transforms/scalar/scalar_opts_ocaml.c50
-rw-r--r--include/llvm-c/Transforms/Scalar.h47
-rw-r--r--lib/Transforms/Scalar/Scalar.cpp39
-rw-r--r--test/Bindings/Ocaml/scalar_opts.ml50
9 files changed, 282 insertions, 1 deletions
diff --git a/bindings/ocaml/Makefile b/bindings/ocaml/Makefile
index 5ae6fe06d1..868b110652 100644
--- a/bindings/ocaml/Makefile
+++ b/bindings/ocaml/Makefile
@@ -8,7 +8,7 @@
##===----------------------------------------------------------------------===##
LEVEL := ../..
-DIRS = llvm bitreader bitwriter analysis executionengine
+DIRS = llvm bitreader bitwriter analysis executionengine transforms
ExtraMakefiles = $(PROJ_OBJ_DIR)/Makefile.ocaml
ocamldoc:
diff --git a/bindings/ocaml/transforms/Makefile b/bindings/ocaml/transforms/Makefile
new file mode 100644
index 0000000000..61800a9bbe
--- /dev/null
+++ b/bindings/ocaml/transforms/Makefile
@@ -0,0 +1,13 @@
+##===- bindings/ocaml/transforms/Makefile ------------------*- Makefile -*-===##
+#
+# The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+
+LEVEL := ../../..
+DIRS = scalar
+
+include $(LEVEL)/Makefile.common
diff --git a/bindings/ocaml/transforms/scalar/Makefile b/bindings/ocaml/transforms/scalar/Makefile
new file mode 100644
index 0000000000..cbaffa4ea7
--- /dev/null
+++ b/bindings/ocaml/transforms/scalar/Makefile
@@ -0,0 +1,20 @@
+##===- bindings/ocaml/transforms/scalar/Makefile -----------*- Makefile -*-===##
+#
+# The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+#
+# This is the makefile for the Objective Caml Llvm_scalar_opts interface.
+#
+##===----------------------------------------------------------------------===##
+
+LEVEL := ../../../..
+LIBRARYNAME := llvm_scalar_opts
+DONT_BUILD_RELINKED := 1
+UsedComponents := scalaropts
+UsedOcamlInterfaces := llvm
+
+include ../../Makefile.ocaml
diff --git a/bindings/ocaml/transforms/scalar/llvm_scalar_opts.ml b/bindings/ocaml/transforms/scalar/llvm_scalar_opts.ml
new file mode 100644
index 0000000000..8b6b7f9879
--- /dev/null
+++ b/bindings/ocaml/transforms/scalar/llvm_scalar_opts.ml
@@ -0,0 +1,24 @@
+(*===-- llvm_scalar_opts.ml - LLVM Ocaml Interface -------------*- OCaml -*-===*
+ *
+ * The LLVM Compiler Infrastructure
+ *
+ * This file is distributed under the University of Illinois Open Source
+ * License. See LICENSE.TXT for details.
+ *
+ *===----------------------------------------------------------------------===*)
+
+external add_constant_propagation : [<Llvm.PassManager.any] Llvm.PassManager.t
+ -> unit
+ = "llvm_add_constant_propagation"
+external add_instruction_combining : [<Llvm.PassManager.any] Llvm.PassManager.t
+ -> unit
+ = "llvm_add_instruction_combining"
+external add_reassociation : [<Llvm.PassManager.any] Llvm.PassManager.t
+ -> unit
+ = "llvm_add_reassociation"
+external add_gvn : [<Llvm.PassManager.any] Llvm.PassManager.t
+ -> unit
+ = "llvm_add_gvn"
+external add_cfg_simplification : [<Llvm.PassManager.any] Llvm.PassManager.t
+ -> unit
+ = "llvm_add_cfg_simplification"
diff --git a/bindings/ocaml/transforms/scalar/llvm_scalar_opts.mli b/bindings/ocaml/transforms/scalar/llvm_scalar_opts.mli
new file mode 100644
index 0000000000..19efaa015e
--- /dev/null
+++ b/bindings/ocaml/transforms/scalar/llvm_scalar_opts.mli
@@ -0,0 +1,38 @@
+(*===-- llvm_scalar_opts.mli - LLVM Ocaml Interface ------------*- OCaml -*-===*
+ *
+ * The LLVM Compiler Infrastructure
+ *
+ * This file is distributed under the University of Illinois Open Source
+ * License. See LICENSE.TXT for details.
+ *
+ *===----------------------------------------------------------------------===*)
+
+(** Scalar Transforms.
+
+ This interface provides an ocaml API for LLVM scalar transforms, the
+ classes in the [LLVMScalarOpts] library. *)
+
+(** See the [llvm::createConstantPropogationPass] function. *)
+external add_constant_propagation : [<Llvm.PassManager.any] Llvm.PassManager.t
+ -> unit
+ = "llvm_add_constant_propagation"
+
+(** See the [llvm::createInstructionCombiningPass] function. *)
+external add_instruction_combining : [<Llvm.PassManager.any] Llvm.PassManager.t
+ -> unit
+ = "llvm_add_instruction_combining"
+
+(** See the [llvm::createReassociatePass] function. *)
+external add_reassociation : [<Llvm.PassManager.any] Llvm.PassManager.t
+ -> unit
+ = "llvm_add_reassociation"
+
+(** See the [llvm::createGVNPass] function. *)
+external add_gvn : [<Llvm.PassManager.any] Llvm.PassManager.t
+ -> unit
+ = "llvm_add_gvn"
+
+(** See the [llvm::createCFGSimplificationPass] function. *)
+external add_cfg_simplification : [<Llvm.PassManager.any] Llvm.PassManager.t
+ -> unit
+ = "llvm_add_cfg_simplification"
diff --git a/bindings/ocaml/transforms/scalar/scalar_opts_ocaml.c b/bindings/ocaml/transforms/scalar/scalar_opts_ocaml.c
new file mode 100644
index 0000000000..5ceb3699cc
--- /dev/null
+++ b/bindings/ocaml/transforms/scalar/scalar_opts_ocaml.c
@@ -0,0 +1,50 @@
+/*===-- scalar_opts_ocaml.c - LLVM Ocaml Glue -------------------*- C++ -*-===*\
+|* *|
+|* The LLVM Compiler Infrastructure *|
+|* *|
+|* This file is distributed under the University of Illinois Open Source *|
+|* License. See LICENSE.TXT for details. *|
+|* *|
+|*===----------------------------------------------------------------------===*|
+|* *|
+|* This file glues LLVM's ocaml interface to its C interface. These functions *|
+|* are by and large transparent wrappers to the corresponding C functions. *|
+|* *|
+|* Note that these functions intentionally take liberties with the CAMLparamX *|
+|* macros, since most of the parameters are not GC heap objects. *|
+|* *|
+\*===----------------------------------------------------------------------===*/
+
+#include "llvm-c/Transforms/Scalar.h"
+#include "caml/mlvalues.h"
+#include "caml/misc.h"
+
+/* [<Llvm.PassManager.any] Llvm.PassManager.t -> unit */
+CAMLprim value llvm_add_constant_propagation(LLVMPassManagerRef PM) {
+ LLVMAddConstantPropagationPass(PM);
+ return Val_unit;
+}
+
+/* [<Llvm.PassManager.any] Llvm.PassManager.t -> unit */
+CAMLprim value llvm_add_instruction_combining(LLVMPassManagerRef PM) {
+ LLVMAddInstructionCombiningPass(PM);
+ return Val_unit;
+}
+
+/* [<Llvm.PassManager.any] Llvm.PassManager.t -> unit */
+CAMLprim value llvm_add_reassociation(LLVMPassManagerRef PM) {
+ LLVMAddReassociatePass(PM);
+ return Val_unit;
+}
+
+/* [<Llvm.PassManager.any] Llvm.PassManager.t -> unit */
+CAMLprim value llvm_add_gvn(LLVMPassManagerRef PM) {
+ LLVMAddGVNPass(PM);
+ return Val_unit;
+}
+
+/* [<Llvm.PassManager.any] Llvm.PassManager.t -> unit */
+CAMLprim value llvm_add_cfg_simplification(LLVMPassManagerRef PM) {
+ LLVMAddCFGSimplificationPass(PM);
+ return Val_unit;
+}
diff --git a/include/llvm-c/Transforms/Scalar.h b/include/llvm-c/Transforms/Scalar.h
new file mode 100644
index 0000000000..00e8c35102
--- /dev/null
+++ b/include/llvm-c/Transforms/Scalar.h
@@ -0,0 +1,47 @@
+/*===-- Scalar.h - Scalar Transformation Library C Interface ----*- C++ -*-===*\
+|* *|
+|* The LLVM Compiler Infrastructure *|
+|* *|
+|* This file is distributed under the University of Illinois Open Source *|
+|* License. See LICENSE.TXT for details. *|
+|* *|
+|*===----------------------------------------------------------------------===*|
+|* *|
+|* This header declares the C interface to libLLVMScalarOpts.a, which *|
+|* implements various scalar transformations of the LLVM IR. *|
+|* *|
+|* Many exotic languages can interoperate with C code but have a harder time *|
+|* with C++ due to name mangling. So in addition to C, this interface enables *|
+|* tools written in such languages. *|
+|* *|
+\*===----------------------------------------------------------------------===*/
+
+#ifndef LLVM_C_TRANSFORMS_SCALAR_H
+#define LLVM_C_TRANSFORMS_SCALAR_H
+
+#include "llvm-c/Core.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** See llvm::createConstantPropagationPass function. */
+void LLVMAddConstantPropagationPass(LLVMPassManagerRef PM);
+
+/** See llvm::createInstructionCombiningPass function. */
+void LLVMAddInstructionCombiningPass(LLVMPassManagerRef PM);
+
+/** See llvm::createReassociatePass function. */
+void LLVMAddReassociatePass(LLVMPassManagerRef PM);
+
+/** See llvm::createGVNPass function. */
+void LLVMAddGVNPass(LLVMPassManagerRef PM);
+
+/** See llvm::createCFGSimplificationPass function. */
+void LLVMAddCFGSimplificationPass(LLVMPassManagerRef PM);
+
+#ifdef __cplusplus
+}
+#endif /* defined(__cplusplus) */
+
+#endif
diff --git a/lib/Transforms/Scalar/Scalar.cpp b/lib/Transforms/Scalar/Scalar.cpp
new file mode 100644
index 0000000000..08b6bb3eee
--- /dev/null
+++ b/lib/Transforms/Scalar/Scalar.cpp
@@ -0,0 +1,39 @@
+//===-- Scalar.cpp --------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements the C bindings for libLLVMScalarOpts.a, which implements
+// several scalar transformations over the LLVM intermediate representation.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm-c/Transforms/Scalar.h"
+#include "llvm/PassManager.h"
+#include "llvm/Transforms/Scalar.h"
+
+using namespace llvm;
+
+void LLVMAddConstantPropagationPass(LLVMPassManagerRef PM) {
+ unwrap(PM)->add(createConstantPropagationPass());
+}
+
+void LLVMAddInstructionCombiningPass(LLVMPassManagerRef PM) {
+ unwrap(PM)->add(createInstructionCombiningPass());
+}
+
+void LLVMAddReassociatePass(LLVMPassManagerRef PM) {
+ unwrap(PM)->add(createReassociatePass());
+}
+
+void LLVMAddGVNPass(LLVMPassManagerRef PM) {
+ unwrap(PM)->add(createGVNPass());
+}
+
+void LLVMAddCFGSimplificationPass(LLVMPassManagerRef PM) {
+ unwrap(PM)->add(createCFGSimplificationPass());
+}
diff --git a/test/Bindings/Ocaml/scalar_opts.ml b/test/Bindings/Ocaml/scalar_opts.ml
new file mode 100644
index 0000000000..2f520b1001
--- /dev/null
+++ b/test/Bindings/Ocaml/scalar_opts.ml
@@ -0,0 +1,50 @@
+(* RUN: %ocamlc -warn-error A llvm.cma llvm_scalar_opts.cma %s -o %t
+ *)
+
+(* Note: It takes several seconds for ocamlc to link an executable with
+ libLLVMCore.a, so it's better to write a big test than a bunch of
+ little ones. *)
+
+open Llvm
+open Llvm_scalar_opts
+
+
+(* Tiny unit test framework - really just to help find which line is busted *)
+let suite name f =
+ prerr_endline (name ^ ":");
+ f ()
+
+
+(*===-- Fixture -----------------------------------------------------------===*)
+
+let filename = Sys.argv.(1)
+let m = create_module filename
+let mp = ModuleProvider.create m
+
+
+(*===-- Transforms --------------------------------------------------------===*)
+
+let test_transforms () =
+ let (++) x f = ignore (f x); x in
+
+ let fty = function_type void_type [| |] in
+ let fn = define_function "fn" fty m in
+ ignore (build_ret_void (builder_at_end (entry_block fn)));
+
+ ignore (PassManager.create_function mp
+ (* ++ add_instruction_combining Requires target data. *)
+ ++ add_reassociation
+ ++ add_gvn
+ ++ add_cfg_simplification
+ ++ add_constant_propagation
+ ++ PassManager.initialize
+ ++ PassManager.run_function fn
+ ++ PassManager.finalize
+ ++ PassManager.dispose)
+
+
+(*===-- Driver ------------------------------------------------------------===*)
+
+let _ =
+ suite "transforms" test_transforms;
+ ModuleProvider.dispose mp