1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
use libc::c_int;
use ffi::{core, LLVMPassManager};
use ffi::prelude::LLVMPassManagerRef;
use cbox::CSemiBox;
use std::marker::PhantomData;
use module::Module;
use ffi::transforms::scalar::*;
use ffi::transforms::vectorize::*;
use ffi::transforms::ipo::*;
use value::Value;
use ffi::LLVMPassRegistry;
use ffi::core::LLVMGetGlobalPassRegistry;
use ffi::initialization::*;

/// The struct responsible for setting up optimization sequences
pub struct PassManager(PhantomData<[u8]>);
native_ref!{&PassManager = LLVMPassManagerRef}
dispose!{PassManager,LLVMPassManager,core::LLVMDisposePassManager}

impl<'a> PassManager {
    /// Create a new pass manager
    pub fn new() -> CSemiBox<'a, PassManager> {
        unsafe { core::LLVMCreatePassManager() }.into()
    }

    /// Create a new function pass manager for a given module. It runs the optimizations
    /// on each function immediatly as it is generated
    pub fn new_func_pass(module: &'a Module) -> CSemiBox<'a, PassManager> {
        unsafe { core::LLVMCreateFunctionPassManagerForModule(module.into()) }.into()
    }

    // Run the function pass manager
    pub fn run_func_pass(&self, f: &'a Value) -> bool {
        unsafe { core::LLVMRunFunctionPassManager(self.into(), f.into()) != 0 }
    }

    pub fn init_func_pass(&self) {
        unsafe { core::LLVMInitializeFunctionPassManager(self.into()) };
    }

    pub fn finalize_func_pass(&self) {
        unsafe { core::LLVMFinalizeFunctionPassManager(self.into()) };
    }
}

macro_rules! add_pass {
    ($name:ident, $passname:expr) => {
        impl <'a> PassManager {
            pub fn $name(&self) -> &PassManager {
                unsafe {$passname(self.into())};
                self
            }
        }
    };
}

// Scalar transformations
add_pass!{add_agressive_dce,LLVMAddAggressiveDCEPass}
add_pass!{add_alingmnet_from_assum,LLVMAddAlignmentFromAssumptionsPass}
add_pass!{add_basic_alias_analysis,LLVMAddBasicAliasAnalysisPass}
add_pass!{add_bit_tacking_dce,LLVMAddBitTrackingDCEPass}
add_pass!{add_cfg,LLVMAddCFGSimplificationPass}
add_pass!{add_constant_propagation,LLVMAddConstantPropagationPass}
add_pass!{add_dead_store_elimination,LLVMAddDeadStoreEliminationPass}
add_pass!{add_demote_memory_to_register,LLVMAddDemoteMemoryToRegisterPass}
add_pass!{add_early_cse,LLVMAddEarlyCSEPass}
add_pass!{add_correlated_value_propagation,LLVMAddCorrelatedValuePropagationPass}
add_pass!{add_gvn,LLVMAddGVNPass}
add_pass!{add_ind_var_simplify,LLVMAddIndVarSimplifyPass}
add_pass!{add_instruction_combining,LLVMAddInstructionCombiningPass}
add_pass!{add_licm,LLVMAddLICMPass}
add_pass!{add_loop_deletion,LLVMAddLoopDeletionPass}
add_pass!{add_loop_idiom,LLVMAddLoopIdiomPass}
add_pass!{add_loop_reroll,LLVMAddLoopRerollPass}
add_pass!{add_loop_rotate,LLVMAddLoopRotatePass}
add_pass!{add_loop_unroll,LLVMAddLoopUnrollPass}
add_pass!{add_loop_nswitch,LLVMAddLoopUnswitchPass}
add_pass!{add_lower_expect_intrinsic,LLVMAddLowerExpectIntrinsicPass}
add_pass!{add_lower_swithc,LLVMAddLowerSwitchPass}
add_pass!{add_mem_cpy,LLVMAddMemCpyOptPass}
add_pass!{add_merged_load_store_motion,LLVMAddMergedLoadStoreMotionPass}
add_pass!{add_partially_inline_lib_calls,LLVMAddPartiallyInlineLibCallsPass}
add_pass!{add_promote_memory_to_register,LLVMAddPromoteMemoryToRegisterPass}
add_pass!{add_reassociate,LLVMAddReassociatePass}
add_pass!{add_sccp,LLVMAddSCCPPass}
add_pass!{add_scalar_repl_aggregates,LLVMAddScalarReplAggregatesPass}
add_pass!{add_scalar_repl_aggregates_ssa,LLVMAddScalarReplAggregatesPassSSA}
add_pass!{add_scalarizer,LLVMAddScalarizerPass}
add_pass!{add_scoped_no_alias_aa,LLVMAddScopedNoAliasAAPass}
add_pass!{add_simplify_lib_calls,LLVMAddSimplifyLibCallsPass}
add_pass!{add_tail_call_elimination,LLVMAddTailCallEliminationPass}
add_pass!{add_type_based_alias_nalysis,LLVMAddTypeBasedAliasAnalysisPass}
add_pass!{add_verifier,LLVMAddVerifierPass}

// Vectorization transformations
add_pass!{add_bb_vectorize,LLVMAddBBVectorizePass}
add_pass!{add_loop_vectorize,LLVMAddLoopVectorizePass}
add_pass!{add_slp_vectorize,LLVMAddSLPVectorizePass}

// Interprocedural transformations
add_pass!{add_always_inline,LLVMAddAlwaysInlinerPass}
add_pass!{add_argument_promotion,LLVMAddArgumentPromotionPass}
add_pass!{add_constant_merge,LLVMAddConstantMergePass}
add_pass!{add_dead_arg_elimination,LLVMAddDeadArgEliminationPass}
add_pass!{add_function_attrs,LLVMAddFunctionAttrsPass}
add_pass!{add_function_inlining,LLVMAddFunctionInliningPass}
add_pass!{add_global_dce,LLVMAddGlobalDCEPass}
add_pass!{add_global_pptimizer,LLVMAddGlobalOptimizerPass}
add_pass!{add_ip_constant_propagation,LLVMAddIPConstantPropagationPass}
add_pass!{add_ipsccp,LLVMAddIPSCCPPass}
add_pass!{add_prune_eh,LLVMAddPruneEHPass}
add_pass!{add_strip_dead_prototypes,LLVMAddStripDeadPrototypesPass}
add_pass!{add_strip_symbols,LLVMAddStripSymbolsPass}

use ffi::transforms::pass_manager_builder::*;

/// Used to custimize a pass sequence in various ways
/// For more information go to [llvm](http://llvm.org/doxygen/classllvm_1_1PassManagerBuilder.html)
pub struct PassManagerBuilder(PhantomData<[u8]>);
native_ref!{&PassManagerBuilder = LLVMPassManagerBuilderRef}
dispose!{PassManagerBuilder,LLVMOpaquePassManagerBuilder,LLVMPassManagerBuilderDispose}

impl<'a> PassManagerBuilder {
    /// Create a new `PassManagerBuilder`
    pub fn new() -> CSemiBox<'a, PassManagerBuilder> {
        unsafe { LLVMPassManagerBuilderCreate() }.into()
    }

    /// Specify the basic optimization level
    pub fn set_opt(&self, level: u32) {
        unsafe { LLVMPassManagerBuilderSetOptLevel(self.into(), level.into()) };
    }

    /// How much we're optimizing for size
    pub fn set_size(&self, level: u32) {
        unsafe { LLVMPassManagerBuilderSetOptLevel(self.into(), level.into()) };
    }

    ///
    pub fn set_disable_simplify_lib_calls(&self, opt: bool) {
        unsafe { LLVMPassManagerBuilderSetDisableSimplifyLibCalls(self.into(), opt as c_int) }
    }

    pub fn disable_unit_at_a_time(&self, opt: bool) {
        unsafe { LLVMPassManagerBuilderSetDisableUnitAtATime(self.into(), opt as c_int) }
    }

    pub fn disable_unroll_loop(&self, opt: bool) {
        unsafe { LLVMPassManagerBuilderSetDisableUnrollLoops(self.into(), opt as c_int) }
    }

    pub fn populate_lto_pass_manger(
        &self,
        pass_manager: &PassManager,
        internalize: bool,
        run_inliner: bool,
    ) {
        unsafe {
            LLVMPassManagerBuilderPopulateLTOPassManager(
                self.into(),
                pass_manager.into(),
                internalize as c_int,
                run_inliner as c_int,
            )
        }
    }

    pub fn inline_with_threshold(&self, threshold: u32) {
        unsafe { LLVMPassManagerBuilderUseInlinerWithThreshold(self.into(), threshold) }
    }

    pub fn populate_module_pass_manager(&self, pass_manager: &PassManager) {
        unsafe { LLVMPassManagerBuilderPopulateModulePassManager(self.into(), pass_manager.into()) }
    }

    pub fn populate_function_pass_manager(&self, pass_manager: &PassManager) {
        unsafe {
            LLVMPassManagerBuilderPopulateFunctionPassManager(self.into(), pass_manager.into())
        }
    }
}

/// This class manages the registration and intitialization of
/// the pass subsystem as application startup, and assists the PassManager
/// in resolving pass dependencies
pub struct PassRegistry(*mut LLVMPassRegistry);

// Waiting on stabilisation of #13231
//impl !Send for PassRegistry {}
//impl !Sync for PassRegistry{}

impl PassRegistry {
    pub fn new() -> Self {
        PassRegistry(unsafe { LLVMGetGlobalPassRegistry() })
    }

    pub fn init_all(&self) {
        self.init_analyis()
            .init_codegen()
            .init_core()
            .init_ipa()
            .init_ipo()
            .init_inst_combine()
            .init_instrumentation()
            .init_scalar_opts()
            .init_target()
            .init_transfrom_utils()
            .init_vectorization();
    }

    fn get(&self) -> *mut LLVMPassRegistry {
        self.0
    }

    pub fn init_analyis(&self) -> &PassRegistry {
        unsafe { LLVMInitializeAnalysis(self.get()) }
        self
    }

    pub fn init_codegen(&self) -> &PassRegistry {
        unsafe { LLVMInitializeCodeGen(self.get()) }

        self
    }

    pub fn init_core(&self) -> &PassRegistry {
        unsafe { LLVMInitializeCore(self.get()) }

        self
    }

    pub fn init_ipa(&self) -> &PassRegistry {
        unsafe { LLVMInitializeIPA(self.get()) }

        self
    }

    pub fn init_ipo(&self) -> &PassRegistry {
        unsafe { LLVMInitializeIPO(self.get()) }

        self
    }

    pub fn init_inst_combine(&self) -> &PassRegistry {
        unsafe { LLVMInitializeInstCombine(self.get()) }

        self
    }

    pub fn init_instrumentation(&self) -> &PassRegistry {
        unsafe { LLVMInitializeInstrumentation(self.get()) }

        self
    }

    pub fn init_scalar_opts(&self) -> &PassRegistry {
        unsafe { LLVMInitializeScalarOpts(self.get()) }

        self
    }

    pub fn init_target(&self) -> &PassRegistry {
        unsafe { LLVMInitializeTarget(self.get()) }

        self
    }

    pub fn init_transfrom_utils(&self) -> &PassRegistry {
        unsafe { LLVMInitializeTransformUtils(self.get()) }

        self
    }

    pub fn init_vectorization(&self) -> &PassRegistry {
        unsafe { LLVMInitializeVectorization(self.get()) }
        self
    }
}