blob: 8ccd8e877b96c0d326608f272e15d8f488408600 [file] [log] [blame]
Chris Lattner76351aa2004-04-02 05:06:57 +00001//===- opt.cpp - The LLVM Modular Optimizer -------------------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Chris Lattner00950542001-06-06 20:29:01 +000010// Optimizations may be specified an arbitrary number of times on the command
Reid Spencerfd90dd52006-08-18 06:34:30 +000011// line, They are run in the order specified.
Chris Lattner00950542001-06-06 20:29:01 +000012//
Chris Lattner0eafc312001-10-18 06:05:15 +000013//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000014
Owen Anderson8b477ed2009-07-01 16:58:40 +000015#include "llvm/LLVMContext.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/Module.h"
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000017#include "llvm/PassManager.h"
Devang Patel28552da2007-06-28 23:09:25 +000018#include "llvm/CallGraphSCCPass.h"
Chris Lattnerb330e382007-05-06 02:42:03 +000019#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattnerffa6f9c2001-10-19 15:39:14 +000020#include "llvm/Assembly/PrintModulePass.h"
Devang Patelf5f23002010-12-07 00:33:43 +000021#include "llvm/Analysis/DebugInfo.h"
Chris Lattner22d26d72002-02-20 17:56:53 +000022#include "llvm/Analysis/Verifier.h"
Devang Patel1bc89362007-03-07 00:26:10 +000023#include "llvm/Analysis/LoopPass.h"
Tobias Grosser65513602010-10-20 01:54:44 +000024#include "llvm/Analysis/RegionPass.h"
Devang Patel28552da2007-06-28 23:09:25 +000025#include "llvm/Analysis/CallGraph.h"
Owen Anderson07000c62006-05-12 06:33:49 +000026#include "llvm/Target/TargetData.h"
Chris Lattner2a66aca2011-02-18 22:13:01 +000027#include "llvm/Target/TargetLibraryInfo.h"
Vikram S. Adve18fdfc42002-09-16 16:09:43 +000028#include "llvm/Target/TargetMachine.h"
Chris Lattner2a66aca2011-02-18 22:13:01 +000029#include "llvm/ADT/Triple.h"
Chris Lattner2053a2a2002-07-26 21:09:32 +000030#include "llvm/Support/PassNameParser.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000031#include "llvm/Support/Signals.h"
David Greene08fc0d32010-01-05 01:30:32 +000032#include "llvm/Support/Debug.h"
Dan Gohman99ed4162009-09-03 16:00:08 +000033#include "llvm/Support/IRReader.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000034#include "llvm/Support/ManagedStatic.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000035#include "llvm/Support/PluginLoader.h"
Chris Lattnerc0d91b72009-12-09 00:41:28 +000036#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbarca8131e2009-06-03 18:22:15 +000037#include "llvm/Support/StandardPasses.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000038#include "llvm/Support/SystemUtils.h"
Dan Gohmane4f1a9b2010-10-07 20:32:40 +000039#include "llvm/Support/ToolOutputFile.h"
Reid Spencer62c51052006-08-21 05:34:03 +000040#include "llvm/LinkAllPasses.h"
Reid Spenceraf303d52006-06-07 23:03:13 +000041#include "llvm/LinkAllVMCore.h"
Chris Lattner63202322001-11-26 19:22:39 +000042#include <memory>
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000043#include <algorithm>
Brian Gaeked0fde302003-11-11 22:41:34 +000044using namespace llvm;
Chris Lattner9d6e7eb2002-04-12 18:21:13 +000045
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000046// The OptimizationList is automatically populated with registered Passes by the
47// PassNameParser.
48//
Chris Lattner7f500f72006-08-27 22:07:01 +000049static cl::list<const PassInfo*, bool, PassNameParser>
50PassList(cl::desc("Optimizations available:"));
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000051
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000052// Other command line options...
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000053//
Chris Lattner6c8103f2003-05-22 20:13:16 +000054static cl::opt<std::string>
Eric Christophera887ae42009-08-21 23:29:40 +000055InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
Reid Spencerfd90dd52006-08-18 06:34:30 +000056 cl::init("-"), cl::value_desc("filename"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000057
Chris Lattner6c8103f2003-05-22 20:13:16 +000058static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000059OutputFilename("o", cl::desc("Override output filename"),
Dan Gohman86cbc1b2010-08-18 17:40:10 +000060 cl::value_desc("filename"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000061
62static cl::opt<bool>
Dan Gohmanbaa26392009-08-25 15:34:52 +000063Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000064
65static cl::opt<bool>
66PrintEachXForm("p", cl::desc("Print module after each transformation"));
67
68static cl::opt<bool>
Chris Lattnerddd5b412003-02-26 20:00:41 +000069NoOutput("disable-output",
Gabor Greifa99be512007-07-05 17:07:56 +000070 cl::desc("Do not write result bitcode file"), cl::Hidden);
Chris Lattnerd70b68e2003-02-12 18:43:33 +000071
72static cl::opt<bool>
Duncan Sands81b0b642009-10-14 20:01:39 +000073OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
Daniel Dunbar8c042c22009-09-05 11:34:53 +000074
75static cl::opt<bool>
Chris Lattnerddd5b412003-02-26 20:00:41 +000076NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
Chris Lattnerf3bafc12003-02-12 18:45:08 +000077
78static cl::opt<bool>
Reid Spencer74ed9972007-02-02 14:46:29 +000079VerifyEach("verify-each", cl::desc("Verify after each transform"));
80
81static cl::opt<bool>
82StripDebug("strip-debug",
83 cl::desc("Strip debugger symbol info from translation unit"));
84
85static cl::opt<bool>
86DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
87
Eric Christophera887ae42009-08-21 23:29:40 +000088static cl::opt<bool>
89DisableOptimizations("disable-opt",
Reid Spencer74ed9972007-02-02 14:46:29 +000090 cl::desc("Do not run any optimization passes"));
91
Eric Christophera887ae42009-08-21 23:29:40 +000092static cl::opt<bool>
Daniel Dunbaradc82882009-07-17 18:09:39 +000093DisableInternalize("disable-internalize",
94 cl::desc("Do not mark all symbols as internal"));
95
Reid Spencer74ed9972007-02-02 14:46:29 +000096static cl::opt<bool>
Eric Christophera887ae42009-08-21 23:29:40 +000097StandardCompileOpts("std-compile-opts",
Reid Spencer74ed9972007-02-02 14:46:29 +000098 cl::desc("Include the standard compile time optimizations"));
99
100static cl::opt<bool>
Eric Christophera887ae42009-08-21 23:29:40 +0000101StandardLinkOpts("std-link-opts",
Daniel Dunbaradc82882009-07-17 18:09:39 +0000102 cl::desc("Include the standard link time optimizations"));
103
104static cl::opt<bool>
Devang Patel2d7551c2008-09-16 22:25:14 +0000105OptLevelO1("O1",
106 cl::desc("Optimization level 1. Similar to llvm-gcc -O1"));
107
108static cl::opt<bool>
109OptLevelO2("O2",
Devang Pateld9424ed2008-09-17 00:01:04 +0000110 cl::desc("Optimization level 2. Similar to llvm-gcc -O2"));
Devang Patel2d7551c2008-09-16 22:25:14 +0000111
112static cl::opt<bool>
113OptLevelO3("O3",
Devang Pateld9424ed2008-09-17 00:01:04 +0000114 cl::desc("Optimization level 3. Similar to llvm-gcc -O3"));
Devang Patel2d7551c2008-09-16 22:25:14 +0000115
116static cl::opt<bool>
117UnitAtATime("funit-at-a-time",
Eric Christopherc4769ba2009-08-21 23:30:30 +0000118 cl::desc("Enable IPO. This is same as llvm-gcc's -funit-at-a-time"),
Duncan Sands34727662010-07-12 08:16:59 +0000119 cl::init(true));
Devang Patel2d7551c2008-09-16 22:25:14 +0000120
121static cl::opt<bool>
122DisableSimplifyLibCalls("disable-simplify-libcalls",
Devang Patel442b1aa2008-09-17 16:01:39 +0000123 cl::desc("Disable simplify-libcalls"));
Devang Patel2d7551c2008-09-16 22:25:14 +0000124
125static cl::opt<bool>
Chris Lattner3153e4f2004-05-27 20:32:10 +0000126Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
Chris Lattner5ff62e92002-07-22 02:10:13 +0000127
Reid Spencerec7eb452004-05-27 16:28:54 +0000128static cl::alias
129QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
130
Reid Spencerfd90dd52006-08-18 06:34:30 +0000131static cl::opt<bool>
132AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
133
Devang Patelf5f23002010-12-07 00:33:43 +0000134static cl::opt<bool>
135PrintBreakpoints("print-breakpoints-for-testing",
136 cl::desc("Print select breakpoints location for testing"));
137
Chris Lattnerd331cb32009-10-22 00:44:10 +0000138static cl::opt<std::string>
139DefaultDataLayout("default-data-layout",
140 cl::desc("data layout string to use if not specified by module"),
141 cl::value_desc("layout-string"), cl::init(""));
142
Reid Spencerfd90dd52006-08-18 06:34:30 +0000143// ---------- Define Printers for module and function passes ------------
144namespace {
145
Devang Patel28552da2007-06-28 23:09:25 +0000146struct CallGraphSCCPassPrinter : public CallGraphSCCPass {
147 static char ID;
148 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000149 raw_ostream &Out;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000150 std::string PassName;
151
Dan Gohman4931b312010-08-18 17:42:59 +0000152 CallGraphSCCPassPrinter(const PassInfo *PI, raw_ostream &out) :
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000153 CallGraphSCCPass(ID), PassToPrint(PI), Out(out) {
154 std::string PassToPrintName = PassToPrint->getPassName();
155 PassName = "CallGraphSCCPass Printer: " + PassToPrintName;
156 }
Devang Patel28552da2007-06-28 23:09:25 +0000157
Chris Lattner2decb222010-04-16 22:42:17 +0000158 virtual bool runOnSCC(CallGraphSCC &SCC) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000159 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000160 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Devang Patel28552da2007-06-28 23:09:25 +0000161
Devang Patel28552da2007-06-28 23:09:25 +0000162 // Get and print pass...
Tobias Grossere9dcd032010-09-08 15:02:51 +0000163 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
164 Function *F = (*I)->getFunction();
165 if (F)
166 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
167 F->getParent());
168 }
Devang Patel28552da2007-06-28 23:09:25 +0000169 return false;
170 }
Eric Christophera887ae42009-08-21 23:29:40 +0000171
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000172 virtual const char *getPassName() const { return PassName.c_str(); }
Devang Patel28552da2007-06-28 23:09:25 +0000173
174 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000175 AU.addRequiredID(PassToPrint->getTypeInfo());
Devang Patel28552da2007-06-28 23:09:25 +0000176 AU.setPreservesAll();
177 }
178};
179
180char CallGraphSCCPassPrinter::ID = 0;
181
Reid Spencerfd90dd52006-08-18 06:34:30 +0000182struct ModulePassPrinter : public ModulePass {
Devang Patel19974732007-05-03 01:11:54 +0000183 static char ID;
Reid Spencerfd90dd52006-08-18 06:34:30 +0000184 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000185 raw_ostream &Out;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000186 std::string PassName;
187
Dan Gohman4931b312010-08-18 17:42:59 +0000188 ModulePassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000189 : ModulePass(ID), PassToPrint(PI), Out(out) {
190 std::string PassToPrintName = PassToPrint->getPassName();
191 PassName = "ModulePass Printer: " + PassToPrintName;
192 }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000193
194 virtual bool runOnModule(Module &M) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000195 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000196 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Reid Spencerfd90dd52006-08-18 06:34:30 +0000197
198 // Get and print pass...
Tobias Grossere9dcd032010-09-08 15:02:51 +0000199 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, &M);
Reid Spencerfd90dd52006-08-18 06:34:30 +0000200 return false;
201 }
202
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000203 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000204
205 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000206 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000207 AU.setPreservesAll();
208 }
209};
210
Devang Patel19974732007-05-03 01:11:54 +0000211char ModulePassPrinter::ID = 0;
Reid Spencerfd90dd52006-08-18 06:34:30 +0000212struct FunctionPassPrinter : public FunctionPass {
213 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000214 raw_ostream &Out;
Devang Patel19974732007-05-03 01:11:54 +0000215 static char ID;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000216 std::string PassName;
217
Dan Gohman4931b312010-08-18 17:42:59 +0000218 FunctionPassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000219 : FunctionPass(ID), PassToPrint(PI), Out(out) {
220 std::string PassToPrintName = PassToPrint->getPassName();
221 PassName = "FunctionPass Printer: " + PassToPrintName;
222 }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000223
224 virtual bool runOnFunction(Function &F) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000225 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000226 Out << "Printing analysis '" << PassToPrint->getPassName()
227 << "' for function '" << F.getName() << "':\n";
Tobias Grossere9dcd032010-09-08 15:02:51 +0000228
Reid Spencerfd90dd52006-08-18 06:34:30 +0000229 // Get and print pass...
Dan Gohman4931b312010-08-18 17:42:59 +0000230 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
Owen Anderson90c579d2010-08-06 18:33:48 +0000231 F.getParent());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000232 return false;
233 }
234
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000235 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000236
237 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000238 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000239 AU.setPreservesAll();
240 }
241};
242
Devang Patel19974732007-05-03 01:11:54 +0000243char FunctionPassPrinter::ID = 0;
Devang Patel56fb1642007-07-05 15:32:03 +0000244
245struct LoopPassPrinter : public LoopPass {
246 static char ID;
247 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000248 raw_ostream &Out;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000249 std::string PassName;
250
Dan Gohman4931b312010-08-18 17:42:59 +0000251 LoopPassPrinter(const PassInfo *PI, raw_ostream &out) :
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000252 LoopPass(ID), PassToPrint(PI), Out(out) {
253 std::string PassToPrintName = PassToPrint->getPassName();
254 PassName = "LoopPass Printer: " + PassToPrintName;
255 }
256
Devang Patel56fb1642007-07-05 15:32:03 +0000257
258 virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000259 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000260 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Tobias Grossere9dcd032010-09-08 15:02:51 +0000261
Devang Patel56fb1642007-07-05 15:32:03 +0000262 // Get and print pass...
Tobias Grossere9dcd032010-09-08 15:02:51 +0000263 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
264 L->getHeader()->getParent()->getParent());
Devang Patel56fb1642007-07-05 15:32:03 +0000265 return false;
266 }
Eric Christophera887ae42009-08-21 23:29:40 +0000267
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000268 virtual const char *getPassName() const { return PassName.c_str(); }
Devang Patel56fb1642007-07-05 15:32:03 +0000269
270 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000271 AU.addRequiredID(PassToPrint->getTypeInfo());
Devang Patel56fb1642007-07-05 15:32:03 +0000272 AU.setPreservesAll();
273 }
274};
275
276char LoopPassPrinter::ID = 0;
277
Tobias Grosser65513602010-10-20 01:54:44 +0000278struct RegionPassPrinter : public RegionPass {
279 static char ID;
280 const PassInfo *PassToPrint;
281 raw_ostream &Out;
282 std::string PassName;
283
284 RegionPassPrinter(const PassInfo *PI, raw_ostream &out) : RegionPass(ID),
285 PassToPrint(PI), Out(out) {
286 std::string PassToPrintName = PassToPrint->getPassName();
Tobias Grosser58396142011-01-20 21:03:20 +0000287 PassName = "RegionPass Printer: " + PassToPrintName;
Tobias Grosser65513602010-10-20 01:54:44 +0000288 }
289
290 virtual bool runOnRegion(Region *R, RGPassManager &RGM) {
291 if (!Quiet) {
292 Out << "Printing analysis '" << PassToPrint->getPassName() << "' for "
293 << "region: '" << R->getNameStr() << "' in function '"
294 << R->getEntry()->getParent()->getNameStr() << "':\n";
295 }
296 // Get and print pass...
297 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
298 R->getEntry()->getParent()->getParent());
299 return false;
300 }
301
Tobias Grosser58396142011-01-20 21:03:20 +0000302 virtual const char *getPassName() const { return PassName.c_str(); }
Tobias Grosser65513602010-10-20 01:54:44 +0000303
304 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
305 AU.addRequiredID(PassToPrint->getTypeInfo());
306 AU.setPreservesAll();
307 }
308};
309
310char RegionPassPrinter::ID = 0;
311
Reid Spencerfd90dd52006-08-18 06:34:30 +0000312struct BasicBlockPassPrinter : public BasicBlockPass {
313 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000314 raw_ostream &Out;
Devang Patel19974732007-05-03 01:11:54 +0000315 static char ID;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000316 std::string PassName;
317
Dan Gohman4931b312010-08-18 17:42:59 +0000318 BasicBlockPassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000319 : BasicBlockPass(ID), PassToPrint(PI), Out(out) {
320 std::string PassToPrintName = PassToPrint->getPassName();
321 PassName = "BasicBlockPass Printer: " + PassToPrintName;
322 }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000323
324 virtual bool runOnBasicBlock(BasicBlock &BB) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000325 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000326 Out << "Printing Analysis info for BasicBlock '" << BB.getName()
327 << "': Pass " << PassToPrint->getPassName() << ":\n";
Reid Spencerfd90dd52006-08-18 06:34:30 +0000328
329 // Get and print pass...
Dan Gohman4931b312010-08-18 17:42:59 +0000330 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
Owen Anderson90c579d2010-08-06 18:33:48 +0000331 BB.getParent()->getParent());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000332 return false;
333 }
334
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000335 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000336
337 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000338 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000339 AU.setPreservesAll();
340 }
341};
342
Devang Patel19974732007-05-03 01:11:54 +0000343char BasicBlockPassPrinter::ID = 0;
Devang Patelf5f23002010-12-07 00:33:43 +0000344
345struct BreakpointPrinter : public FunctionPass {
346 raw_ostream &Out;
347 static char ID;
348
349 BreakpointPrinter(raw_ostream &out)
350 : FunctionPass(ID), Out(out) {
351 }
352
353 virtual bool runOnFunction(Function &F) {
Devang Patel29012312010-12-09 23:37:07 +0000354 BasicBlock &EntryBB = F.getEntryBlock();
Devang Patel97f6d5b2011-01-31 21:36:24 +0000355 BasicBlock::const_iterator BI = EntryBB.end();
356 --BI;
357 do {
358 const Instruction *In = BI;
359 const DebugLoc DL = In->getDebugLoc();
360 if (!DL.isUnknown()) {
361 DIScope S(DL.getScope(getGlobalContext()));
362 Out << S.getFilename() << " " << DL.getLine() << "\n";
363 break;
364 }
365 --BI;
366 } while (BI != EntryBB.begin());
Devang Patelf5f23002010-12-07 00:33:43 +0000367 return false;
368 }
369
370 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
371 AU.setPreservesAll();
372 }
373};
374
375char BreakpointPrinter::ID = 0;
376
Dan Gohman9cc1c182010-05-14 15:36:54 +0000377inline void addPass(PassManagerBase &PM, Pass *P) {
Reid Spencer74ed9972007-02-02 14:46:29 +0000378 // Add the pass to the pass manager...
379 PM.add(P);
380
381 // If we are verifying all of the intermediate steps, add the verifier...
382 if (VerifyEach) PM.add(createVerifierPass());
383}
384
Eric Christophera887ae42009-08-21 23:29:40 +0000385/// AddOptimizationPasses - This routine adds optimization passes
386/// based on selected optimization level, OptLevel. This routine
Devang Patel2d7551c2008-09-16 22:25:14 +0000387/// duplicates llvm-gcc behaviour.
388///
389/// OptLevel - Optimization Level
Dan Gohman9cc1c182010-05-14 15:36:54 +0000390void AddOptimizationPasses(PassManagerBase &MPM, PassManagerBase &FPM,
Zhongxing Xu3253f4c2008-11-26 02:57:24 +0000391 unsigned OptLevel) {
Daniel Dunbarca8131e2009-06-03 18:22:15 +0000392 createStandardFunctionPasses(&FPM, OptLevel);
Devang Patel2d7551c2008-09-16 22:25:14 +0000393
Eli Friedman74733a72010-01-18 22:38:31 +0000394 llvm::Pass *InliningPass = 0;
395 if (DisableInline) {
396 // No inlining pass
397 } else if (OptLevel) {
Jakob Stoklund Olesene6c6cec2010-11-02 23:40:28 +0000398 unsigned Threshold = 225;
Eli Friedman74733a72010-01-18 22:38:31 +0000399 if (OptLevel > 2)
Jakob Stoklund Olesene6c6cec2010-11-02 23:40:28 +0000400 Threshold = 275;
Eli Friedman74733a72010-01-18 22:38:31 +0000401 InliningPass = createFunctionInliningPass(Threshold);
402 } else {
403 InliningPass = createAlwaysInlinerPass();
404 }
Daniel Dunbarca8131e2009-06-03 18:22:15 +0000405 createStandardModulePasses(&MPM, OptLevel,
406 /*OptimizeSize=*/ false,
407 UnitAtATime,
408 /*UnrollLoops=*/ OptLevel > 1,
409 !DisableSimplifyLibCalls,
410 /*HaveExceptions=*/ true,
411 InliningPass);
Devang Patel2d7551c2008-09-16 22:25:14 +0000412}
413
Dan Gohman9cc1c182010-05-14 15:36:54 +0000414void AddStandardCompilePasses(PassManagerBase &PM) {
Reid Spencer74ed9972007-02-02 14:46:29 +0000415 PM.add(createVerifierPass()); // Verify that input is correct
416
417 addPass(PM, createLowerSetJmpPass()); // Lower llvm.setjmp/.longjmp
Reid Spencer74ed9972007-02-02 14:46:29 +0000418
419 // If the -strip-debug command line option was specified, do it.
420 if (StripDebug)
421 addPass(PM, createStripSymbolsPass(true));
422
423 if (DisableOptimizations) return;
424
Daniel Dunbarca8131e2009-06-03 18:22:15 +0000425 llvm::Pass *InliningPass = !DisableInline ? createFunctionInliningPass() : 0;
Reid Spencer74ed9972007-02-02 14:46:29 +0000426
Daniel Dunbarca8131e2009-06-03 18:22:15 +0000427 // -std-compile-opts adds the same module passes as -O3.
Eric Christophera887ae42009-08-21 23:29:40 +0000428 createStandardModulePasses(&PM, 3,
Daniel Dunbarca8131e2009-06-03 18:22:15 +0000429 /*OptimizeSize=*/ false,
430 /*UnitAtATime=*/ true,
431 /*UnrollLoops=*/ true,
Peter Collingbourne90d238c2011-02-18 02:59:21 +0000432 !DisableSimplifyLibCalls,
Daniel Dunbarca8131e2009-06-03 18:22:15 +0000433 /*HaveExceptions=*/ true,
434 InliningPass);
Reid Spencer74ed9972007-02-02 14:46:29 +0000435}
436
Dan Gohman9cc1c182010-05-14 15:36:54 +0000437void AddStandardLinkPasses(PassManagerBase &PM) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000438 PM.add(createVerifierPass()); // Verify that input is correct
439
440 // If the -strip-debug command line option was specified, do it.
441 if (StripDebug)
442 addPass(PM, createStripSymbolsPass(true));
443
444 if (DisableOptimizations) return;
445
446 createStandardLTOPasses(&PM, /*Internalize=*/ !DisableInternalize,
447 /*RunInliner=*/ !DisableInline,
448 /*VerifyEach=*/ VerifyEach);
449}
450
Reid Spencerfd90dd52006-08-18 06:34:30 +0000451} // anonymous namespace
452
Chris Lattner0be41012002-02-01 04:54:11 +0000453
Chris Lattnerc0ce68b2002-07-23 18:12:22 +0000454//===----------------------------------------------------------------------===//
455// main for opt
456//
Chris Lattner00950542001-06-06 20:29:01 +0000457int main(int argc, char **argv) {
Chris Lattnerc0d91b72009-12-09 00:41:28 +0000458 sys::PrintStackTraceOnErrorSignal();
459 llvm::PrettyStackTraceProgram X(argc, argv);
Dan Gohmand4c45432010-09-01 14:20:41 +0000460
David Greene08fc0d32010-01-05 01:30:32 +0000461 // Enable debug stream buffering.
462 EnableDebugBuffering = true;
463
Chris Lattnerc0d91b72009-12-09 00:41:28 +0000464 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Owen Anderson0d7c6952009-07-15 22:16:10 +0000465 LLVMContext &Context = getGlobalContext();
Chris Lattner61db1a12009-10-22 00:46:41 +0000466
Owen Anderson081c34b2010-10-19 17:21:58 +0000467 // Initialize passes
468 PassRegistry &Registry = *PassRegistry::getPassRegistry();
469 initializeCore(Registry);
470 initializeScalarOpts(Registry);
471 initializeIPO(Registry);
472 initializeAnalysis(Registry);
473 initializeIPA(Registry);
474 initializeTransformUtils(Registry);
475 initializeInstCombine(Registry);
476 initializeInstrumentation(Registry);
477 initializeTarget(Registry);
478
Chris Lattner61db1a12009-10-22 00:46:41 +0000479 cl::ParseCommandLineOptions(argc, argv,
480 "llvm .bc -> .bc modular optimizer and analysis printer\n");
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000481
Tobias Grosser7593f342010-12-02 20:35:16 +0000482 if (AnalyzeOnly && NoOutput) {
483 errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
484 return 1;
485 }
486
Chris Lattner61db1a12009-10-22 00:46:41 +0000487 // Allocate a full target machine description only if necessary.
488 // FIXME: The choice of target should be controllable on the command line.
489 std::auto_ptr<TargetMachine> target;
Devang Patelff5d06d2008-08-27 20:51:49 +0000490
Chris Lattner61db1a12009-10-22 00:46:41 +0000491 SMDiagnostic Err;
Vikram S. Adve18fdfc42002-09-16 16:09:43 +0000492
Chris Lattner61db1a12009-10-22 00:46:41 +0000493 // Load the input module...
494 std::auto_ptr<Module> M;
495 M.reset(ParseIRFile(InputFilename, Err, Context));
Eric Christophera887ae42009-08-21 23:29:40 +0000496
Chris Lattner61db1a12009-10-22 00:46:41 +0000497 if (M.get() == 0) {
498 Err.Print(argv[0], errs());
499 return 1;
500 }
501
502 // Figure out what stream we are supposed to write to...
Dan Gohmand5826a32010-08-20 01:07:01 +0000503 OwningPtr<tool_output_file> Out;
Dan Gohman4931b312010-08-18 17:42:59 +0000504 if (NoOutput) {
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000505 if (!OutputFilename.empty())
506 errs() << "WARNING: The -o (output filename) option is ignored when\n"
Dan Gohman4931b312010-08-18 17:42:59 +0000507 "the --disable-output option is used.\n";
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000508 } else {
509 // Default to standard output.
510 if (OutputFilename.empty())
511 OutputFilename = "-";
Chris Lattner61db1a12009-10-22 00:46:41 +0000512
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000513 std::string ErrorInfo;
Dan Gohmand5826a32010-08-20 01:07:01 +0000514 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
515 raw_fd_ostream::F_Binary));
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000516 if (!ErrorInfo.empty()) {
517 errs() << ErrorInfo << '\n';
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000518 return 1;
Chris Lattner00950542001-06-06 20:29:01 +0000519 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000520 }
Chris Lattner76d12292002-04-18 19:55:25 +0000521
Chris Lattner61db1a12009-10-22 00:46:41 +0000522 // If the output is set to be emitted to standard out, and standard out is a
523 // console, print out a warning message and refuse to do it. We don't
524 // impress anyone by spewing tons of binary goo to a terminal.
Dan Gohmanb56bf582010-01-17 17:47:24 +0000525 if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
Dan Gohmand4c45432010-09-01 14:20:41 +0000526 if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
Chris Lattner61db1a12009-10-22 00:46:41 +0000527 NoOutput = true;
Dan Gohmanec080462009-09-11 20:46:33 +0000528
Chris Lattner61db1a12009-10-22 00:46:41 +0000529 // Create a PassManager to hold and optimize the collection of passes we are
Chris Lattner2a66aca2011-02-18 22:13:01 +0000530 // about to build.
Chris Lattner61db1a12009-10-22 00:46:41 +0000531 //
532 PassManager Passes;
Chris Lattner00950542001-06-06 20:29:01 +0000533
Chris Lattner2a66aca2011-02-18 22:13:01 +0000534 // Add an appropriate TargetLibraryInfo pass for the module's triple.
535 if (!M->getTargetTriple().empty())
536 Passes.add(new TargetLibraryInfo(Triple(M->getTargetTriple())));
537
538 // Add an appropriate TargetData instance for this module.
Chris Lattner61db1a12009-10-22 00:46:41 +0000539 TargetData *TD = 0;
540 const std::string &ModuleDataLayout = M.get()->getDataLayout();
541 if (!ModuleDataLayout.empty())
542 TD = new TargetData(ModuleDataLayout);
Kenneth Uildriksb908f8a2009-11-03 15:29:06 +0000543 else if (!DefaultDataLayout.empty())
Chris Lattner61db1a12009-10-22 00:46:41 +0000544 TD = new TargetData(DefaultDataLayout);
Chris Lattner9c3b55e2003-04-24 19:13:02 +0000545
Chris Lattner61db1a12009-10-22 00:46:41 +0000546 if (TD)
547 Passes.add(TD);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000548
Dan Gohman9cc1c182010-05-14 15:36:54 +0000549 OwningPtr<PassManager> FPasses;
Chris Lattner61db1a12009-10-22 00:46:41 +0000550 if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
Dan Gohman9cc1c182010-05-14 15:36:54 +0000551 FPasses.reset(new PassManager());
Chris Lattnerd331cb32009-10-22 00:44:10 +0000552 if (TD)
Chris Lattner61db1a12009-10-22 00:46:41 +0000553 FPasses->add(new TargetData(*TD));
554 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000555
Devang Patelf5f23002010-12-07 00:33:43 +0000556 if (PrintBreakpoints) {
557 // Default to standard output.
558 if (!Out) {
559 if (OutputFilename.empty())
560 OutputFilename = "-";
561
562 std::string ErrorInfo;
563 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
564 raw_fd_ostream::F_Binary));
565 if (!ErrorInfo.empty()) {
566 errs() << ErrorInfo << '\n';
567 return 1;
568 }
569 }
570 Passes.add(new BreakpointPrinter(Out->os()));
571 NoOutput = true;
572 }
573
Chris Lattner61db1a12009-10-22 00:46:41 +0000574 // If the -strip-debug command line option was specified, add it. If
575 // -std-compile-opts was also specified, it will handle StripDebug.
576 if (StripDebug && !StandardCompileOpts)
577 addPass(Passes, createStripSymbolsPass(true));
Eric Christophera887ae42009-08-21 23:29:40 +0000578
Chris Lattner61db1a12009-10-22 00:46:41 +0000579 // Create a new optimization pass for each one specified on the command line
580 for (unsigned i = 0; i < PassList.size(); ++i) {
581 // Check to see if -std-compile-opts was specified before this option. If
582 // so, handle it.
583 if (StandardCompileOpts &&
584 StandardCompileOpts.getPosition() < PassList.getPosition(i)) {
Chris Lattner3dda08a2008-07-13 19:35:21 +0000585 AddStandardCompilePasses(Passes);
586 StandardCompileOpts = false;
Eric Christophera887ae42009-08-21 23:29:40 +0000587 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000588
Chris Lattner61db1a12009-10-22 00:46:41 +0000589 if (StandardLinkOpts &&
590 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000591 AddStandardLinkPasses(Passes);
592 StandardLinkOpts = false;
Eric Christophera887ae42009-08-21 23:29:40 +0000593 }
Daniel Dunbaradc82882009-07-17 18:09:39 +0000594
Chris Lattner61db1a12009-10-22 00:46:41 +0000595 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000596 AddOptimizationPasses(Passes, *FPasses, 1);
Chris Lattner61db1a12009-10-22 00:46:41 +0000597 OptLevelO1 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000598 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000599
Chris Lattner61db1a12009-10-22 00:46:41 +0000600 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000601 AddOptimizationPasses(Passes, *FPasses, 2);
Chris Lattner61db1a12009-10-22 00:46:41 +0000602 OptLevelO2 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000603 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000604
Chris Lattner61db1a12009-10-22 00:46:41 +0000605 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000606 AddOptimizationPasses(Passes, *FPasses, 3);
Chris Lattner61db1a12009-10-22 00:46:41 +0000607 OptLevelO3 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000608 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000609
Chris Lattner61db1a12009-10-22 00:46:41 +0000610 const PassInfo *PassInf = PassList[i];
611 Pass *P = 0;
612 if (PassInf->getNormalCtor())
613 P = PassInf->getNormalCtor()();
614 else
615 errs() << argv[0] << ": cannot create pass: "
616 << PassInf->getPassName() << "\n";
617 if (P) {
Benjamin Kramer3460f222010-02-18 12:57:05 +0000618 PassKind Kind = P->getPassKind();
Chris Lattner61db1a12009-10-22 00:46:41 +0000619 addPass(Passes, P);
620
621 if (AnalyzeOnly) {
Benjamin Kramer3460f222010-02-18 12:57:05 +0000622 switch (Kind) {
Chris Lattner476e9bd2010-01-22 06:03:06 +0000623 case PT_BasicBlock:
Dan Gohmand4c45432010-09-01 14:20:41 +0000624 Passes.add(new BasicBlockPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000625 break;
Tobias Grosser65513602010-10-20 01:54:44 +0000626 case PT_Region:
627 Passes.add(new RegionPassPrinter(PassInf, Out->os()));
628 break;
Chris Lattner476e9bd2010-01-22 06:03:06 +0000629 case PT_Loop:
Dan Gohmand4c45432010-09-01 14:20:41 +0000630 Passes.add(new LoopPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000631 break;
632 case PT_Function:
Dan Gohmand4c45432010-09-01 14:20:41 +0000633 Passes.add(new FunctionPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000634 break;
635 case PT_CallGraphSCC:
Dan Gohmand4c45432010-09-01 14:20:41 +0000636 Passes.add(new CallGraphSCCPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000637 break;
638 default:
Dan Gohmand4c45432010-09-01 14:20:41 +0000639 Passes.add(new ModulePassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000640 break;
641 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000642 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000643 }
644
Chris Lattner61db1a12009-10-22 00:46:41 +0000645 if (PrintEachXForm)
646 Passes.add(createPrintModulePass(&errs()));
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000647 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000648
649 // If -std-compile-opts was specified at the end of the pass list, add them.
650 if (StandardCompileOpts) {
651 AddStandardCompilePasses(Passes);
652 StandardCompileOpts = false;
653 }
654
655 if (StandardLinkOpts) {
656 AddStandardLinkPasses(Passes);
657 StandardLinkOpts = false;
658 }
659
660 if (OptLevelO1)
661 AddOptimizationPasses(Passes, *FPasses, 1);
662
663 if (OptLevelO2)
664 AddOptimizationPasses(Passes, *FPasses, 2);
665
666 if (OptLevelO3)
667 AddOptimizationPasses(Passes, *FPasses, 3);
668
Dan Gohman9cc1c182010-05-14 15:36:54 +0000669 if (OptLevelO1 || OptLevelO2 || OptLevelO3)
670 FPasses->run(*M.get());
Chris Lattner61db1a12009-10-22 00:46:41 +0000671
672 // Check that the module is well formed on completion of optimization
673 if (!NoVerify && !VerifyEach)
674 Passes.add(createVerifierPass());
675
Dan Gohman4931b312010-08-18 17:42:59 +0000676 // Write bitcode or assembly to the output as the last step...
Chris Lattner61db1a12009-10-22 00:46:41 +0000677 if (!NoOutput && !AnalyzeOnly) {
678 if (OutputAssembly)
Dan Gohmand4c45432010-09-01 14:20:41 +0000679 Passes.add(createPrintModulePass(&Out->os()));
Chris Lattner61db1a12009-10-22 00:46:41 +0000680 else
Dan Gohmand4c45432010-09-01 14:20:41 +0000681 Passes.add(createBitcodeWriterPass(Out->os()));
Chris Lattner61db1a12009-10-22 00:46:41 +0000682 }
683
684 // Now that we have all of the passes ready, run them.
685 Passes.run(*M.get());
686
Dan Gohmand5826a32010-08-20 01:07:01 +0000687 // Declare success.
Devang Patelf5f23002010-12-07 00:33:43 +0000688 if (!NoOutput || PrintBreakpoints)
Dan Gohmand5826a32010-08-20 01:07:01 +0000689 Out->keep();
690
Chris Lattner61db1a12009-10-22 00:46:41 +0000691 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000692}