21#ifdef USE_WORKAROUND_LLVM_PR39177
25#include "llvm/Analysis/GlobalsModRef.h"
26#include "llvm/Analysis/Passes.h"
27#include "llvm/Analysis/LoopPass.h"
28#include "llvm/IR/Module.h"
29#include "llvm/IR/DataLayout.h"
30#include "llvm/IR/LegacyPassManager.h"
31#include "llvm/IR/Verifier.h"
32#include "llvm/Support/CommandLine.h"
33#include "llvm/Support/DynamicLibrary.h"
34#include "llvm/Support/PluginLoader.h"
35#include "llvm/Target/TargetMachine.h"
36#include "llvm/Transforms/IPO.h"
37#include "llvm/Transforms/IPO/FunctionAttrs.h"
38#include "llvm/Transforms/Scalar.h"
39#include "llvm/Transforms/Scalar/GVN.h"
41#if LLVM_VERSION_CODE >= LLVM_VERSION(7, 0)
42#include "llvm/Transforms/Utils.h"
43#include "llvm/Transforms/InstCombine/InstCombine.h"
50 cl::desc(
"Do not run the inliner pass (default=false)"),
54 "disable-internalize",
55 cl::desc(
"Do not mark all symbols as internal (default=false)"),
60 cl::desc(
"Verify intermediate results of all optimization passes (default=false)"),
66 cl::desc(
"Alias for -disable-internalize"));
69 Strip(
"strip-all", cl::desc(
"Strip all symbol information from executable"),
72static cl::alias
A0(
"s", cl::desc(
"Alias for --strip-all"),
77 cl::desc(
"Strip debugger symbol info from executable"),
80static cl::alias
A1(
"S", cl::desc(
"Alias for --strip-debug"),
85static inline void addPass(legacy::PassManager &PM, Pass *P) {
91 PM.add(createVerifierPass());
98 PM.add(createVerifierPass());
102 addPass(PM, createStripSymbolsPass(
true));
104 addPass(PM, createCFGSimplificationPass());
105 addPass(PM, createPromoteMemoryToRegisterPass());
106 addPass(PM, createGlobalOptimizerPass());
107 addPass(PM, createGlobalDCEPass());
108#if LLVM_VERSION_CODE >= LLVM_VERSION(11, 0)
111 addPass(PM, createIPConstantPropagationPass());
113 addPass(PM, createDeadArgEliminationPass());
114 addPass(PM, createInstructionCombiningPass());
115 addPass(PM, createCFGSimplificationPass());
117 addPass(PM, createPruneEHPass());
118 addPass(PM, createPostOrderFunctionAttrsLegacyPass());
119 addPass(PM, createReversePostOrderFunctionAttrsPass());
122 addPass(PM, createFunctionInliningPass());
123 addPass(PM, createArgumentPromotionPass());
125 addPass(PM, createInstructionCombiningPass());
126 addPass(PM, createJumpThreadingPass());
127 addPass(PM, createCFGSimplificationPass());
129 addPass(PM, createInstructionCombiningPass());
131 addPass(PM, createTailCallEliminationPass());
132 addPass(PM, createCFGSimplificationPass());
133 addPass(PM, createReassociatePass());
134 addPass(PM, createLoopRotatePass());
136 addPass(PM, createLoopUnswitchPass());
138 addPass(PM, createInstructionCombiningPass());
139 addPass(PM, createIndVarSimplifyPass());
140 addPass(PM, createLoopDeletionPass());
141 addPass(PM, createLoopUnrollPass());
142 addPass(PM, createInstructionCombiningPass());
144 addPass(PM, createMemCpyOptPass());
149 addPass(PM, createInstructionCombiningPass());
151 addPass(PM, createDeadStoreEliminationPass());
152 addPass(PM, createAggressiveDCEPass());
153 addPass(PM, createCFGSimplificationPass());
154 addPass(PM, createStripDeadPrototypesPass());
155 addPass(PM, createConstantMergePass());
161void Optimize(Module *M, llvm::ArrayRef<const char *> preservedFunctions) {
164 legacy::PassManager Passes;
168 Passes.add(createVerifierPass());
170#ifdef USE_WORKAROUND_LLVM_PR39177
171 addPass(Passes,
new klee::WorkaroundLLVMPR39177Pass());
181 auto PreserveFunctions = [=](
const GlobalValue &GV) {
182 StringRef GVName = GV.getName();
184 for (
const char *fun : preservedFunctions)
185 if (GVName.equals(fun))
190 ModulePass *pass = createInternalizePass(PreserveFunctions);
197 addPass(Passes, createIPSCCPPass());
200 addPass(Passes, createGlobalOptimizerPass());
204 addPass(Passes, createConstantMergePass());
207 addPass(Passes, createDeadArgEliminationPass());
213 addPass(Passes, createInstructionCombiningPass());
216 addPass(Passes, createFunctionInliningPass());
218 addPass(Passes, createPruneEHPass());
219 addPass(Passes, createGlobalOptimizerPass());
220 addPass(Passes, createGlobalDCEPass());
224 addPass(Passes, createArgumentPromotionPass());
227 addPass(Passes, createInstructionCombiningPass());
228 addPass(Passes, createJumpThreadingPass());
229 addPass(Passes, createSROAPass());
232 addPass(Passes, createPostOrderFunctionAttrsLegacyPass());
233 addPass(Passes, createReversePostOrderFunctionAttrsPass());
234 addPass(Passes, createGlobalsAAWrapperPass());
236 addPass(Passes, createLICMPass());
237 addPass(Passes, createGVNPass());
238 addPass(Passes, createMemCpyOptPass());
239 addPass(Passes, createDeadStoreEliminationPass());
242 addPass(Passes, createInstructionCombiningPass());
244 addPass(Passes, createJumpThreadingPass());
245 addPass(Passes, createPromoteMemoryToRegisterPass());
248 addPass(Passes, createCFGSimplificationPass());
251 addPass(Passes, createGlobalDCEPass());
260 addPass(Passes, createInstructionCombiningPass());
261 addPass(Passes, createCFGSimplificationPass());
262 addPass(Passes, createAggressiveDCEPass());
263 addPass(Passes, createGlobalDCEPass());
static cl::opt< bool > Strip("strip-all", cl::desc("Strip all symbol information from executable"), cl::init(false), cl::cat(klee::ModuleCat))
static cl::opt< bool > StripDebug("strip-debug", cl::desc("Strip debugger symbol info from executable"), cl::init(false), cl::cat(klee::ModuleCat))
static void addPass(legacy::PassManager &PM, Pass *P)
static cl::opt< bool > DisableInline("disable-inlining", cl::desc("Do not run the inliner pass (default=false)"), cl::init(false), cl::cat(klee::ModuleCat))
static cl::alias A1("S", cl::desc("Alias for --strip-debug"), cl::aliasopt(StripDebug))
static cl::alias A0("s", cl::desc("Alias for --strip-all"), cl::aliasopt(Strip))
static cl::opt< bool > DisableInternalize("disable-internalize", cl::desc("Do not mark all symbols as internal (default=false)"), cl::init(false), cl::cat(klee::ModuleCat))
static cl::alias ExportDynamic("export-dynamic", cl::aliasopt(DisableInternalize), cl::desc("Alias for -disable-internalize"))
static cl::opt< bool > VerifyEach("verify-each", cl::desc("Verify intermediate results of all optimization passes (default=false)"), cl::init(false), cl::cat(klee::ModuleCat))
llvm::cl::OptionCategory ModuleCat
static void AddStandardCompilePasses(legacy::PassManager &PM)
void Optimize(Module *, llvm::ArrayRef< const char * > preservedFunctions)