From a75a50215b0075e1e7aea9bf7f89dc5bc0ceeb3f Mon Sep 17 00:00:00 2001 From: Timotej Kapus Date: Wed, 19 Jun 2019 02:22:00 -0700 Subject: [PATCH] [sledge] Add LLVM passes that reduce bitcode size MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: : This patch adds several passes that reduce the amount of bitcode making sledge's job easier, more info: https://llvm.org/docs/Passes.html `-mergefunc` This pass merges functions that do the same thing, this can be because of templating or casts (ie. same functionality but on 32bit and 64bit ints, which is the same in machine code). More details at http://llvm.org/docs/MergeFunctions.html Note that this pass is currently not available through C/OCaml API. `-constmerge` This merges constants that have the same value, this is possible to do when the constants are internalized. `-argpromotion` ``` This pass promotes “by reference” arguments to be “by value” arguments. In practice, this means looking for internal functions that have pointer arguments. If it can prove, through the use of alias analysis, that an argument is only loaded, then it can pass the value into the function instead of the address of the value. This can cause recursive simplification of code and lead to the elimination of allocas (especially in C++ template code like the STL). ``` `-ipsccp` ``` Sparse conditional constant propagation and merging, which can be summarized as: Assumes values are constant unless proven otherwise Assumes BasicBlocks are dead unless proven otherwise Proves values to be constant, and replaces them with constants Proves conditional branches to be unconditional ``` `-deadargelim` Removes dead arguments of internal functions, good to run after other inter-procedural passes. Seems to crash llvm if run directly after `ipsccp`. Note that while this might look like doing full link-time optimisation, we are actually picking relatively cheap optimisations that mostly look at globals and walk their use chains. The main reason link-time optimisations are expensive is due to inlining and then running the full optimisation again from there. Reviewed By: jberdine Differential Revision: D15851408 fbshipit-source-id: be7191683 --- sledge/src/llair/frontend.ml | 6 ++++++ sledge/src/sledge_buck.ml | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/sledge/src/llair/frontend.ml b/sledge/src/llair/frontend.ml index 2f3064a6e..e2630974b 100644 --- a/sledge/src/llair/frontend.ml +++ b/sledge/src/llair/frontend.ml @@ -1362,6 +1362,12 @@ let transform : Llvm.llmodule -> unit = List.exists entry_points ~f:(String.equal fn) ) ; Llvm_ipo.add_global_dce pm ; Llvm_ipo.add_global_optimizer pm ; + Llvm_ipo.add_constant_merge pm ; + Llvm_ipo.add_argument_promotion pm ; + Llvm_ipo.add_ipsccp pm ; + Llvm_scalar_opts.add_memory_to_register_promotion pm ; + Llvm_ipo.add_global_dce pm ; + Llvm_ipo.add_dead_arg_elimination pm ; Llvm_scalar_opts.add_lower_atomic pm ; Llvm_scalar_opts.add_scalar_repl_aggregation pm ; Llvm_scalar_opts.add_scalarizer pm ; diff --git a/sledge/src/sledge_buck.ml b/sledge/src/sledge_buck.ml index c105af0d0..dbe681442 100644 --- a/sledge/src/sledge_buck.ml +++ b/sledge/src/sledge_buck.ml @@ -146,7 +146,9 @@ let llvm_link_opt ~lib_fuzzer_harness ~output modules = :: "-o=-" :: modules ) |- run (Lazy.force llvm_bin ^ "opt") - ["-o=" ^ output; "-globaldce"; "-globalopt"] ) + [ "-o=" ^ output; "-globaldce"; "-globalopt"; "-mergefunc" + ; "-constmerge"; "-argpromotion"; "-ipsccp"; "-mem2reg"; "-dce" + ; "-globaldce"; "-deadargelim" ] ) (** command line interface *)