OptimizationOptions.cpp revision a790f0a8f3175183bea088389b3e4ae41813e192
1//===- OptimizationOptions.cpp --------------------------------------------===//
2//
3//                     The MCLinker Project
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9#include <mcld/OptimizationOptions.h>
10#include <mcld/LinkerConfig.h>
11#include <mcld/Support/CommandLine.h>
12#include <mcld/Support/MsgHandling.h>
13
14namespace {
15
16bool ArgGCSections;
17
18llvm::cl::opt<bool, true> ArgGCSectionsFlag("gc-sections",
19  llvm::cl::ZeroOrMore,
20  llvm::cl::location(ArgGCSections),
21  llvm::cl::desc("Enable garbage collection of unused input sections."),
22  llvm::cl::init(false));
23
24llvm::cl::opt<bool, true, llvm::cl::FalseParser> ArgNoGCSectionsFlag("no-gc-sections",
25  llvm::cl::ZeroOrMore,
26  llvm::cl::location(ArgGCSections),
27  llvm::cl::desc("disable garbage collection of unused input sections."),
28  llvm::cl::init(false));
29
30bool ArgPrintGCSections;
31
32llvm::cl::opt<bool, true> ArgPrintGCSectionsFlag("print-gc-sections",
33  llvm::cl::ZeroOrMore,
34  llvm::cl::location(ArgPrintGCSections),
35  llvm::cl::desc("List all sections removed by garbage collection."),
36  llvm::cl::init(false));
37
38llvm::cl::opt<bool, true, llvm::cl::FalseParser> ArgNoPrintGCSectionsFlag("no-print-gc-sections",
39  llvm::cl::ZeroOrMore,
40  llvm::cl::location(ArgPrintGCSections),
41  llvm::cl::desc("disable --print-gc-sections"),
42  llvm::cl::init(false));
43
44bool ArgGenUnwindInfo;
45
46llvm::cl::opt<bool, true, llvm::cl::FalseParser>
47ArgNoGenUnwindInfoFlag("no-ld-generated-unwind-info",
48                       llvm::cl::ZeroOrMore,
49                       llvm::cl::location(ArgGenUnwindInfo),
50                       llvm::cl::desc("Don't create unwind info for linker"
51                                      " generated sections to save size"),
52                       llvm::cl::init(false),
53                       llvm::cl::ValueDisallowed);
54llvm::cl::opt<bool, true>
55ArgGenUnwindInfoFlag("ld-generated-unwind-info",
56                     llvm::cl::ZeroOrMore,
57                     llvm::cl::location(ArgGenUnwindInfo),
58                     llvm::cl::desc("Request creation of unwind info for linker"
59                                    " generated code sections like PLT."),
60                     llvm::cl::init(true),
61                     llvm::cl::ValueDisallowed);
62
63llvm::cl::opt<mcld::GeneralOptions::ICF> ArgICF("icf",
64  llvm::cl::ZeroOrMore,
65  llvm::cl::desc("Identical Code Folding"),
66  llvm::cl::init(mcld::GeneralOptions::ICF_None),
67  llvm::cl::values(
68    clEnumValN(mcld::GeneralOptions::ICF_None, "none",
69      "do not perform cold folding"),
70    clEnumValN(mcld::GeneralOptions::ICF_All, "all",
71      "always preform cold folding"),
72    clEnumValN(mcld::GeneralOptions::ICF_Safe, "safe",
73      "Folds those whose pointers are definitely not taken."),
74    clEnumValEnd));
75
76llvm::cl::opt<unsigned> ArgICFIterations("icf-iterations",
77  llvm::cl::desc("Number of iterations to do ICF."),
78  llvm::cl::init(2));
79
80llvm::cl::opt<bool> ArgPrintICFSections("print-icf-sections",
81  llvm::cl::desc("Print the folded identical sections."),
82  llvm::cl::init(false));
83
84llvm::cl::opt<char> ArgOptLevel("O",
85  llvm::cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
86                 "(default = '-O2')"),
87  llvm::cl::Prefix,
88  llvm::cl::ZeroOrMore,
89  llvm::cl::init(' '));
90
91llvm::cl::list<std::string> ArgPlugin("plugin",
92  llvm::cl::desc("Load a plugin library."),
93  llvm::cl::value_desc("plugin"));
94
95llvm::cl::list<std::string> ArgPluginOpt("plugin-opt",
96  llvm::cl::desc("Pass an option to the plugin."),
97  llvm::cl::value_desc("option"));
98
99} // anonymous namespace
100
101using namespace mcld;
102
103//===----------------------------------------------------------------------===//
104// OptimizationOptions
105//===----------------------------------------------------------------------===//
106OptimizationOptions::OptimizationOptions()
107  : m_GCSections(ArgGCSections),
108    m_PrintGCSections(ArgPrintGCSections),
109    m_GenUnwindInfo(ArgGenUnwindInfo),
110    m_ICF(ArgICF),
111    m_ICFIterations(ArgICFIterations),
112    m_PrintICFSections(ArgPrintICFSections),
113    m_OptLevel(ArgOptLevel),
114    m_Plugin(ArgPlugin),
115    m_PluginOpt(ArgPluginOpt) {
116}
117
118bool OptimizationOptions::parse(LinkerConfig& pConfig)
119{
120  // set --gc-sections
121  if (m_GCSections)
122    pConfig.options().setGCSections();
123
124  // set --print-gc-sections
125  if (m_PrintGCSections)
126    pConfig.options().setPrintGCSections();
127
128  // set --ld-generated-unwind-info (or not)
129  pConfig.options().setGenUnwindInfo(m_GenUnwindInfo);
130
131  // set --icf [mode]
132  pConfig.options().setICFMode(m_ICF);
133  pConfig.options().setICFIterations(m_ICFIterations);
134  pConfig.options().setPrintICFSections(m_PrintICFSections);
135
136  return true;
137}
138