llvm.org GIT mirror llvm / d78c0f5
C and Objective Caml bindings for PassManagers. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@48413 91177308-0d34-0410-b5e6-96231b3b80d8 Gordon Henriksen 12 years ago
6 changed file(s) with 235 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
482482 end
483483
484484
485 (*===-- Pass Manager ------------------------------------------------------===*)
486
487 module PassManager = struct
488 type 'a t
489 type any = [ `Module | `Function ]
490 external create : unit -> [ `Module ] t = "llvm_passmanager_create"
491 external create_function : llmoduleprovider -> [ `Function ] t
492 = "LLVMCreateFunctionPassManager"
493 external run_module : llmodule -> [ `Module ] t -> bool
494 = "llvm_passmanager_run_module"
495 external initialize : [ `Function ] t -> bool = "llvm_passmanager_initialize"
496 external run_function : llvalue -> [ `Function ] t -> bool
497 = "llvm_passmanager_run_function"
498 external finalize : [ `Function ] t -> bool = "llvm_passmanager_finalize"
499 external dispose : [< any ] t -> unit = "llvm_passmanager_dispose"
500 end
501
502
485503 (*===-- Non-Externs -------------------------------------------------------===*)
486504 (* These functions are built using the externals, so must be declared late. *)
487505
13561356 (** Disposes of a memory buffer. *)
13571357 external dispose : llmemorybuffer -> unit = "llvm_memorybuffer_dispose"
13581358 end
1359
1360
1361 (** {6 Pass Managers} *)
1362
1363 module PassManager : sig
1364 (** *)
1365 type 'a t
1366 type any = [ `Module | `Function ]
1367
1368 (** [PassManager.create ()] constructs a new whole-module pass pipeline. This
1369 type of pipeline is suitable for link-time optimization and whole-module
1370 transformations.
1371 See the constructor of [llvm::PassManager]. *)
1372 external create : unit -> [ `Module ] t = "llvm_passmanager_create"
1373
1374 (** [PassManager.create_function mp] constructs a new function-by-function
1375 pass pipeline over the module provider [mp]. It does not take ownership of
1376 [mp]. This type of pipeline is suitable for code generation and JIT
1377 compilation tasks.
1378 See the constructor of [llvm::FunctionPassManager]. *)
1379 external create_function : llmoduleprovider -> [ `Function ] t
1380 = "LLVMCreateFunctionPassManager"
1381
1382 (** [run_module m pm] initializes, executes on the module [m], and finalizes
1383 all of the passes scheduled in the pass manager [pm]. Returns [true] if
1384 any of the passes modified the module, [false] otherwise.
1385 See the [llvm::PassManager::run] method. *)
1386 external run_module : llmodule -> [ `Module ] t -> bool
1387 = "llvm_passmanager_run_module"
1388
1389 (** [initialize fpm] initializes all of the function passes scheduled in the
1390 function pass manager [fpm]. Returns [true] if any of the passes modified
1391 the module, [false] otherwise.
1392 See the [llvm::FunctionPassManager::doInitialization] method. *)
1393 external initialize : [ `Function ] t -> bool = "llvm_passmanager_initialize"
1394
1395 (** [run_function f fpm] executes all of the function passes scheduled in the
1396 function pass manager [fpm] over the function [f]. Returns [true] if any
1397 of the passes modified [f], [false] otherwise.
1398 See the [llvm::FunctionPassManager::run] method. *)
1399 external run_function : llvalue -> [ `Function ] t -> bool
1400 = "llvm_passmanager_run_function"
1401
1402 (** [finalize fpm] finalizes all of the function passes scheduled in in the
1403 function pass manager [fpm]. Returns [true] if any of the passes
1404 modified the module, [false] otherwise.
1405 See the [llvm::FunctionPassManager::doFinalization] method. *)
1406 external finalize : [ `Function ] t -> bool = "llvm_passmanager_finalize"
1407
1408 (** Frees the memory of a pass pipeline. For function pipelines, does not free
1409 the module provider.
1410 See the destructor of [llvm::BasePassManager]. *)
1411 external dispose : [< any ] t -> unit = "llvm_passmanager_dispose"
1412 end
11791179 return Val_unit;
11801180 }
11811181
1182 /*===-- Pass Managers -----------------------------------------------------===*/
1183
1184 /* unit -> [ `Module ] PassManager.t */
1185 CAMLprim LLVMPassManagerRef llvm_passmanager_create(value Unit) {
1186 return LLVMCreatePassManager();
1187 }
1188
1189 /* llmodule -> [ `Function ] PassManager.t -> bool */
1190 CAMLprim value llvm_passmanager_run_module(LLVMModuleRef M,
1191 LLVMPassManagerRef PM) {
1192 return Val_bool(LLVMRunPassManager(PM, M));
1193 }
1194
1195 /* [ `Function ] PassManager.t -> bool */
1196 CAMLprim value llvm_passmanager_initialize(LLVMPassManagerRef FPM) {
1197 return Val_bool(LLVMInitializeFunctionPassManager(FPM));
1198 }
1199
1200 /* llvalue -> [ `Function ] PassManager.t -> bool */
1201 CAMLprim value llvm_passmanager_run_function(LLVMValueRef F,
1202 LLVMPassManagerRef FPM) {
1203 return Val_bool(LLVMRunFunctionPassManager(FPM, F));
1204 }
1205
1206 /* [ `Function ] PassManager.t -> bool */
1207 CAMLprim value llvm_passmanager_finalize(LLVMPassManagerRef FPM) {
1208 return Val_bool(LLVMFinalizeFunctionPassManager(FPM));
1209 }
1210
1211 /* PassManager.any PassManager.t -> unit */
1212 CAMLprim value llvm_passmanager_dispose(LLVMPassManagerRef PM) {
1213 LLVMDisposePassManager(PM);
1214 return Val_unit;
1215 }
3838 and 'unwrap' conversion functions. */
3939 #include "llvm/Module.h"
4040 #include "llvm/Support/LLVMBuilder.h"
41 #include "llvm/Pass.h"
42 #include "llvm/PassManager.h"
4143
4244 extern "C" {
4345 #endif
7779 * See the llvm::MemoryBuffer class.
7880 */
7981 typedef struct LLVMOpaqueMemoryBuffer *LLVMMemoryBufferRef;
82
83 /** See the llvm::PassManagerBase class. */
84 typedef struct LLVMOpaquePassManager *LLVMPassManagerRef;
8085
8186 typedef enum {
8287 LLVMVoidTypeKind, /**< type with no size */
574579 char **OutMessage);
575580 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf);
576581
582
583 /*===-- Pass Managers -----------------------------------------------------===*/
584
585 /** Constructs a new whole-module pass pipeline. This type of pipeline is
586 suitable for link-time optimization and whole-module transformations.
587 See llvm::PassManager::PassManager. */
588 LLVMPassManagerRef LLVMCreatePassManager();
589
590 /** Constructs a new function-by-function pass pipeline over the module
591 provider. It does not take ownership of the module provider. This type of
592 pipeline is suitable for code generation and JIT compilation tasks.
593 See llvm::FunctionPassManager::FunctionPassManager. */
594 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef MP);
595
596 /** Initializes, executes on the provided module, and finalizes all of the
597 passes scheduled in the pass manager. Returns 1 if any of the passes
598 modified the module, 0 otherwise. See llvm::PassManager::run(Module&). */
599 int LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M);
600
601 /** Initializes all of the function passes scheduled in the function pass
602 manager. Returns 1 if any of the passes modified the module, 0 otherwise.
603 See llvm::FunctionPassManager::doInitialization. */
604 int LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM);
605
606 /** Executes all of the function passes scheduled in the function pass manager
607 on the provided function. Returns 1 if any of the passes modified the
608 function, false otherwise.
609 See llvm::FunctionPassManager::run(Function&). */
610 int LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F);
611
612 /** Finalizes all of the function passes scheduled in in the function pass
613 manager. Returns 1 if any of the passes modified the module, 0 otherwise.
614 See llvm::FunctionPassManager::doFinalization. */
615 int LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM);
616
617 /** Frees the memory of a pass pipeline. For function pipelines, does not free
618 the module provider.
619 See llvm::PassManagerBase::~PassManagerBase. */
620 void LLVMDisposePassManager(LLVMPassManagerRef PM);
621
622
577623 #ifdef __cplusplus
578624 }
579625
590636 return reinterpret_cast(const_cast(P)); \
591637 }
592638
593 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Type, LLVMTypeRef )
594 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Value, LLVMValueRef )
639 #define DEFINE_ISA_CONVERSION_FUNCTIONS(ty, ref) \
640 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref) \
641 \
642 template \
643 inline T *unwrap(ref P) { \
644 return cast(unwrap(P)); \
645 }
646
647 #define DEFINE_STDCXX_CONVERSION_FUNCTIONS(ty, ref) \
648 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref) \
649 \
650 template \
651 inline T *unwrap(ref P) { \
652 T *Q = dynamic_cast(unwrap(P)); \
653 assert(Q && "Invalid cast!"); \
654 return Q; \
655 }
656
657 DEFINE_ISA_CONVERSION_FUNCTIONS (Type, LLVMTypeRef )
658 DEFINE_ISA_CONVERSION_FUNCTIONS (Value, LLVMValueRef )
595659 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Module, LLVMModuleRef )
596660 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(BasicBlock, LLVMBasicBlockRef )
597661 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMFoldingBuilder, LLVMBuilderRef )
598662 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(PATypeHolder, LLVMTypeHandleRef )
599663 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ModuleProvider, LLVMModuleProviderRef)
600664 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MemoryBuffer, LLVMMemoryBufferRef )
601
665 DEFINE_STDCXX_CONVERSION_FUNCTIONS(PassManagerBase, LLVMPassManagerRef )
666
667 #undef DEFINE_STDCXX_CONVERSION_FUNCTIONS
668 #undef DEFINE_ISA_CONVERSION_FUNCTIONS
602669 #undef DEFINE_SIMPLE_CONVERSION_FUNCTIONS
603670
604671 /* Specialized opaque type conversions.
605672 */
606 template
607 inline T *unwrap(LLVMTypeRef Ty) {
608 return cast(unwrap(Ty));
609 }
610
611673 inline Type **unwrap(LLVMTypeRef* Tys) {
612674 return reinterpret_cast(Tys);
613675 }
618680
619681 /* Specialized opaque value conversions.
620682 */
621 template
622 inline T *unwrap(LLVMValueRef Val) {
623 return cast(unwrap(Val));
624 }
625
626683 inline Value **unwrap(LLVMValueRef *Vals) {
627684 return reinterpret_cast(Vals);
628685 }
1818 #include "llvm/ModuleProvider.h"
1919 #include "llvm/Support/Streams.h"
2020 #include "llvm/Support/ManagedStatic.h"
21 #include "llvm-c/Core.h"
2122 #include
2223 #include
2324 #include
15251526 }
15261527
15271528 PassManagerBase::~PassManagerBase() {}
1529
1530 /*===-- C Bindings --------------------------------------------------------===*/
1531
1532 LLVMPassManagerRef LLVMCreatePassManager() {
1533 return wrap(new PassManager());
1534 }
1535
1536 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
1537 return wrap(new FunctionPassManager(unwrap(P)));
1538 }
1539
1540 int LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
1541 return unwrap(PM)->run(*unwrap(M));
1542 }
1543
1544 int LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
1545 return unwrap(FPM)->doInitialization();
1546 }
1547
1548 int LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
1549 return unwrap(FPM)->run(*unwrap(F));
1550 }
1551
1552 int LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
1553 return unwrap(FPM)->doFinalization();
1554 }
1555
1556 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
1557 delete unwrap(PM);
1558 }
3333
3434 let filename = Sys.argv.(1)
3535 let m = create_module filename
36 let mp = ModuleProvider.create m
3637
3738
3839 (*===-- Target ------------------------------------------------------------===*)
835836 ModuleProvider.dispose mp
836837
837838
839 (*===-- Pass Managers -----------------------------------------------------===*)
840
841 let test_pass_manager () =
842 let (++) x f = ignore (f x); x in
843
844 begin group "module pass manager";
845 ignore (PassManager.create ()
846 ++ PassManager.run_module m
847 ++ PassManager.dispose)
848 end;
849
850 begin group "function pass manager";
851 let fty = function_type void_type [| |] in
852 let fn = define_function "FunctionPassManager" fty m in
853 ignore (build_ret_void (builder_at_end (entry_block fn)));
854
855 ignore (PassManager.create_function mp
856 ++ PassManager.initialize
857 ++ PassManager.run_function fn
858 ++ PassManager.finalize
859 ++ PassManager.dispose)
860 end
861
862
838863 (*===-- Writer ------------------------------------------------------------===*)
839864
840865 let test_writer () =
846871 group "writer";
847872 insist (write_bitcode_file m filename);
848873
849 dispose_module m
874 ModuleProvider.dispose mp
850875
851876
852877 (*===-- Driver ------------------------------------------------------------===*)
861886 suite "basic blocks" test_basic_blocks;
862887 suite "builder" test_builder;
863888 suite "module provider" test_module_provider;
864 suite "writer" test_writer;
889 suite "pass manager" test_pass_manager;
890 suite "writer" test_writer; (* Keep this last; it disposes m. *)
865891 exit !exit_status