llvm.org GIT mirror llvm / 41ba154
C and Objective Caml bindings for several scalar transforms. Patch originally by Erick Tryzelaar, but has been modified somewhat. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@48419 91177308-0d34-0410-b5e6-96231b3b80d8 Gordon Henriksen 11 years ago
9 changed file(s) with 282 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
77 ##===----------------------------------------------------------------------===##
88
99 LEVEL := ../..
10 DIRS = llvm bitreader bitwriter analysis executionengine
10 DIRS = llvm bitreader bitwriter analysis executionengine transforms
1111 ExtraMakefiles = $(PROJ_OBJ_DIR)/Makefile.ocaml
1212
1313 ocamldoc:
0 ##===- bindings/ocaml/transforms/Makefile ------------------*- Makefile -*-===##
1 #
2 # The LLVM Compiler Infrastructure
3 #
4 # This file is distributed under the University of Illinois Open Source
5 # License. See LICENSE.TXT for details.
6 #
7 ##===----------------------------------------------------------------------===##
8
9 LEVEL := ../../..
10 DIRS = scalar
11
12 include $(LEVEL)/Makefile.common
0 ##===- bindings/ocaml/transforms/scalar/Makefile -----------*- Makefile -*-===##
1 #
2 # The LLVM Compiler Infrastructure
3 #
4 # This file is distributed under the University of Illinois Open Source
5 # License. See LICENSE.TXT for details.
6 #
7 ##===----------------------------------------------------------------------===##
8 #
9 # This is the makefile for the Objective Caml Llvm_scalar_opts interface.
10 #
11 ##===----------------------------------------------------------------------===##
12
13 LEVEL := ../../../..
14 LIBRARYNAME := llvm_scalar_opts
15 DONT_BUILD_RELINKED := 1
16 UsedComponents := scalaropts
17 UsedOcamlInterfaces := llvm
18
19 include ../../Makefile.ocaml
0 (*===-- llvm_scalar_opts.ml - LLVM Ocaml Interface -------------*- OCaml -*-===*
1 *
2 * The LLVM Compiler Infrastructure
3 *
4 * This file is distributed under the University of Illinois Open Source
5 * License. See LICENSE.TXT for details.
6 *
7 *===----------------------------------------------------------------------===*)
8
9 external add_constant_propagation : [
10 -> unit
11 = "llvm_add_constant_propagation"
12 external add_instruction_combining : [
13 -> unit
14 = "llvm_add_instruction_combining"
15 external add_reassociation : [
16 -> unit
17 = "llvm_add_reassociation"
18 external add_gvn : [
19 -> unit
20 = "llvm_add_gvn"
21 external add_cfg_simplification : [
22 -> unit
23 = "llvm_add_cfg_simplification"
0 (*===-- llvm_scalar_opts.mli - LLVM Ocaml Interface ------------*- OCaml -*-===*
1 *
2 * The LLVM Compiler Infrastructure
3 *
4 * This file is distributed under the University of Illinois Open Source
5 * License. See LICENSE.TXT for details.
6 *
7 *===----------------------------------------------------------------------===*)
8
9 (** Scalar Transforms.
10
11 This interface provides an ocaml API for LLVM scalar transforms, the
12 classes in the [LLVMScalarOpts] library. *)
13
14 (** See the [llvm::createConstantPropogationPass] function. *)
15 external add_constant_propagation : [
16 -> unit
17 = "llvm_add_constant_propagation"
18
19 (** See the [llvm::createInstructionCombiningPass] function. *)
20 external add_instruction_combining : [
21 -> unit
22 = "llvm_add_instruction_combining"
23
24 (** See the [llvm::createReassociatePass] function. *)
25 external add_reassociation : [
26 -> unit
27 = "llvm_add_reassociation"
28
29 (** See the [llvm::createGVNPass] function. *)
30 external add_gvn : [
31 -> unit
32 = "llvm_add_gvn"
33
34 (** See the [llvm::createCFGSimplificationPass] function. *)
35 external add_cfg_simplification : [
36 -> unit
37 = "llvm_add_cfg_simplification"
0 /*===-- scalar_opts_ocaml.c - LLVM Ocaml Glue -------------------*- C++ -*-===*\
1 |* *|
2 |* The LLVM Compiler Infrastructure *|
3 |* *|
4 |* This file is distributed under the University of Illinois Open Source *|
5 |* License. See LICENSE.TXT for details. *|
6 |* *|
7 |*===----------------------------------------------------------------------===*|
8 |* *|
9 |* This file glues LLVM's ocaml interface to its C interface. These functions *|
10 |* are by and large transparent wrappers to the corresponding C functions. *|
11 |* *|
12 |* Note that these functions intentionally take liberties with the CAMLparamX *|
13 |* macros, since most of the parameters are not GC heap objects. *|
14 |* *|
15 \*===----------------------------------------------------------------------===*/
16
17 #include "llvm-c/Transforms/Scalar.h"
18 #include "caml/mlvalues.h"
19 #include "caml/misc.h"
20
21 /* [ unit */
22 CAMLprim value llvm_add_constant_propagation(LLVMPassManagerRef PM) {
23 LLVMAddConstantPropagationPass(PM);
24 return Val_unit;
25 }
26
27 /* [ unit */
28 CAMLprim value llvm_add_instruction_combining(LLVMPassManagerRef PM) {
29 LLVMAddInstructionCombiningPass(PM);
30 return Val_unit;
31 }
32
33 /* [ unit */
34 CAMLprim value llvm_add_reassociation(LLVMPassManagerRef PM) {
35 LLVMAddReassociatePass(PM);
36 return Val_unit;
37 }
38
39 /* [ unit */
40 CAMLprim value llvm_add_gvn(LLVMPassManagerRef PM) {
41 LLVMAddGVNPass(PM);
42 return Val_unit;
43 }
44
45 /* [ unit */
46 CAMLprim value llvm_add_cfg_simplification(LLVMPassManagerRef PM) {
47 LLVMAddCFGSimplificationPass(PM);
48 return Val_unit;
49 }
0 /*===-- Scalar.h - Scalar Transformation Library C Interface ----*- C++ -*-===*\
1 |* *|
2 |* The LLVM Compiler Infrastructure *|
3 |* *|
4 |* This file is distributed under the University of Illinois Open Source *|
5 |* License. See LICENSE.TXT for details. *|
6 |* *|
7 |*===----------------------------------------------------------------------===*|
8 |* *|
9 |* This header declares the C interface to libLLVMScalarOpts.a, which *|
10 |* implements various scalar transformations of the LLVM IR. *|
11 |* *|
12 |* Many exotic languages can interoperate with C code but have a harder time *|
13 |* with C++ due to name mangling. So in addition to C, this interface enables *|
14 |* tools written in such languages. *|
15 |* *|
16 \*===----------------------------------------------------------------------===*/
17
18 #ifndef LLVM_C_TRANSFORMS_SCALAR_H
19 #define LLVM_C_TRANSFORMS_SCALAR_H
20
21 #include "llvm-c/Core.h"
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26
27 /** See llvm::createConstantPropagationPass function. */
28 void LLVMAddConstantPropagationPass(LLVMPassManagerRef PM);
29
30 /** See llvm::createInstructionCombiningPass function. */
31 void LLVMAddInstructionCombiningPass(LLVMPassManagerRef PM);
32
33 /** See llvm::createReassociatePass function. */
34 void LLVMAddReassociatePass(LLVMPassManagerRef PM);
35
36 /** See llvm::createGVNPass function. */
37 void LLVMAddGVNPass(LLVMPassManagerRef PM);
38
39 /** See llvm::createCFGSimplificationPass function. */
40 void LLVMAddCFGSimplificationPass(LLVMPassManagerRef PM);
41
42 #ifdef __cplusplus
43 }
44 #endif /* defined(__cplusplus) */
45
46 #endif
0 //===-- Scalar.cpp --------------------------------------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the C bindings for libLLVMScalarOpts.a, which implements
10 // several scalar transformations over the LLVM intermediate representation.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm-c/Transforms/Scalar.h"
15 #include "llvm/PassManager.h"
16 #include "llvm/Transforms/Scalar.h"
17
18 using namespace llvm;
19
20 void LLVMAddConstantPropagationPass(LLVMPassManagerRef PM) {
21 unwrap(PM)->add(createConstantPropagationPass());
22 }
23
24 void LLVMAddInstructionCombiningPass(LLVMPassManagerRef PM) {
25 unwrap(PM)->add(createInstructionCombiningPass());
26 }
27
28 void LLVMAddReassociatePass(LLVMPassManagerRef PM) {
29 unwrap(PM)->add(createReassociatePass());
30 }
31
32 void LLVMAddGVNPass(LLVMPassManagerRef PM) {
33 unwrap(PM)->add(createGVNPass());
34 }
35
36 void LLVMAddCFGSimplificationPass(LLVMPassManagerRef PM) {
37 unwrap(PM)->add(createCFGSimplificationPass());
38 }
0 (* RUN: %ocamlc -warn-error A llvm.cma llvm_scalar_opts.cma %s -o %t
1 *)
2
3 (* Note: It takes several seconds for ocamlc to link an executable with
4 libLLVMCore.a, so it's better to write a big test than a bunch of
5 little ones. *)
6
7 open Llvm
8 open Llvm_scalar_opts
9
10
11 (* Tiny unit test framework - really just to help find which line is busted *)
12 let suite name f =
13 prerr_endline (name ^ ":");
14 f ()
15
16
17 (*===-- Fixture -----------------------------------------------------------===*)
18
19 let filename = Sys.argv.(1)
20 let m = create_module filename
21 let mp = ModuleProvider.create m
22
23
24 (*===-- Transforms --------------------------------------------------------===*)
25
26 let test_transforms () =
27 let (++) x f = ignore (f x); x in
28
29 let fty = function_type void_type [| |] in
30 let fn = define_function "fn" fty m in
31 ignore (build_ret_void (builder_at_end (entry_block fn)));
32
33 ignore (PassManager.create_function mp
34 (* ++ add_instruction_combining Requires target data. *)
35 ++ add_reassociation
36 ++ add_gvn
37 ++ add_cfg_simplification
38 ++ add_constant_propagation
39 ++ PassManager.initialize
40 ++ PassManager.run_function fn
41 ++ PassManager.finalize
42 ++ PassManager.dispose)
43
44
45 (*===-- Driver ------------------------------------------------------------===*)
46
47 let _ =
48 suite "transforms" test_transforms;
49 ModuleProvider.dispose mp