blob: c1e0ad128bc2b009cbd9f6a29d5a2dbb020fbea1 [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"
Vikram S. Adve18fdfc42002-09-16 16:09:43 +000027#include "llvm/Target/TargetMachine.h"
Chris Lattner2053a2a2002-07-26 21:09:32 +000028#include "llvm/Support/PassNameParser.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000029#include "llvm/Support/Signals.h"
David Greene08fc0d32010-01-05 01:30:32 +000030#include "llvm/Support/Debug.h"
Dan Gohman99ed4162009-09-03 16:00:08 +000031#include "llvm/Support/IRReader.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000032#include "llvm/Support/ManagedStatic.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000033#include "llvm/Support/PluginLoader.h"
Chris Lattnerc0d91b72009-12-09 00:41:28 +000034#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbarca8131e2009-06-03 18:22:15 +000035#include "llvm/Support/StandardPasses.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000036#include "llvm/Support/SystemUtils.h"
Dan Gohmane4f1a9b2010-10-07 20:32:40 +000037#include "llvm/Support/ToolOutputFile.h"
Reid Spencer62c51052006-08-21 05:34:03 +000038#include "llvm/LinkAllPasses.h"
Reid Spenceraf303d52006-06-07 23:03:13 +000039#include "llvm/LinkAllVMCore.h"
Chris Lattner63202322001-11-26 19:22:39 +000040#include <memory>
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000041#include <algorithm>
Brian Gaeked0fde302003-11-11 22:41:34 +000042using namespace llvm;
Chris Lattner9d6e7eb2002-04-12 18:21:13 +000043
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000044// The OptimizationList is automatically populated with registered Passes by the
45// PassNameParser.
46//
Chris Lattner7f500f72006-08-27 22:07:01 +000047static cl::list<const PassInfo*, bool, PassNameParser>
48PassList(cl::desc("Optimizations available:"));
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000049
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000050// Other command line options...
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000051//
Chris Lattner6c8103f2003-05-22 20:13:16 +000052static cl::opt<std::string>
Eric Christophera887ae42009-08-21 23:29:40 +000053InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
Reid Spencerfd90dd52006-08-18 06:34:30 +000054 cl::init("-"), cl::value_desc("filename"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000055
Chris Lattner6c8103f2003-05-22 20:13:16 +000056static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000057OutputFilename("o", cl::desc("Override output filename"),
Dan Gohman86cbc1b2010-08-18 17:40:10 +000058 cl::value_desc("filename"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000059
60static cl::opt<bool>
Dan Gohmanbaa26392009-08-25 15:34:52 +000061Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000062
63static cl::opt<bool>
64PrintEachXForm("p", cl::desc("Print module after each transformation"));
65
66static cl::opt<bool>
Chris Lattnerddd5b412003-02-26 20:00:41 +000067NoOutput("disable-output",
Gabor Greifa99be512007-07-05 17:07:56 +000068 cl::desc("Do not write result bitcode file"), cl::Hidden);
Chris Lattnerd70b68e2003-02-12 18:43:33 +000069
70static cl::opt<bool>
Duncan Sands81b0b642009-10-14 20:01:39 +000071OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
Daniel Dunbar8c042c22009-09-05 11:34:53 +000072
73static cl::opt<bool>
Chris Lattnerddd5b412003-02-26 20:00:41 +000074NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
Chris Lattnerf3bafc12003-02-12 18:45:08 +000075
76static cl::opt<bool>
Reid Spencer74ed9972007-02-02 14:46:29 +000077VerifyEach("verify-each", cl::desc("Verify after each transform"));
78
79static cl::opt<bool>
80StripDebug("strip-debug",
81 cl::desc("Strip debugger symbol info from translation unit"));
82
83static cl::opt<bool>
84DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
85
Eric Christophera887ae42009-08-21 23:29:40 +000086static cl::opt<bool>
87DisableOptimizations("disable-opt",
Reid Spencer74ed9972007-02-02 14:46:29 +000088 cl::desc("Do not run any optimization passes"));
89
Eric Christophera887ae42009-08-21 23:29:40 +000090static cl::opt<bool>
Daniel Dunbaradc82882009-07-17 18:09:39 +000091DisableInternalize("disable-internalize",
92 cl::desc("Do not mark all symbols as internal"));
93
Reid Spencer74ed9972007-02-02 14:46:29 +000094static cl::opt<bool>
Eric Christophera887ae42009-08-21 23:29:40 +000095StandardCompileOpts("std-compile-opts",
Reid Spencer74ed9972007-02-02 14:46:29 +000096 cl::desc("Include the standard compile time optimizations"));
97
98static cl::opt<bool>
Eric Christophera887ae42009-08-21 23:29:40 +000099StandardLinkOpts("std-link-opts",
Daniel Dunbaradc82882009-07-17 18:09:39 +0000100 cl::desc("Include the standard link time optimizations"));
101
102static cl::opt<bool>
Devang Patel2d7551c2008-09-16 22:25:14 +0000103OptLevelO1("O1",
104 cl::desc("Optimization level 1. Similar to llvm-gcc -O1"));
105
106static cl::opt<bool>
107OptLevelO2("O2",
Devang Pateld9424ed2008-09-17 00:01:04 +0000108 cl::desc("Optimization level 2. Similar to llvm-gcc -O2"));
Devang Patel2d7551c2008-09-16 22:25:14 +0000109
110static cl::opt<bool>
111OptLevelO3("O3",
Devang Pateld9424ed2008-09-17 00:01:04 +0000112 cl::desc("Optimization level 3. Similar to llvm-gcc -O3"));
Devang Patel2d7551c2008-09-16 22:25:14 +0000113
114static cl::opt<bool>
115UnitAtATime("funit-at-a-time",
Eric Christopherc4769ba2009-08-21 23:30:30 +0000116 cl::desc("Enable IPO. This is same as llvm-gcc's -funit-at-a-time"),
Duncan Sands34727662010-07-12 08:16:59 +0000117 cl::init(true));
Devang Patel2d7551c2008-09-16 22:25:14 +0000118
119static cl::opt<bool>
120DisableSimplifyLibCalls("disable-simplify-libcalls",
Devang Patel442b1aa2008-09-17 16:01:39 +0000121 cl::desc("Disable simplify-libcalls"));
Devang Patel2d7551c2008-09-16 22:25:14 +0000122
123static cl::opt<bool>
Chris Lattner3153e4f2004-05-27 20:32:10 +0000124Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
Chris Lattner5ff62e92002-07-22 02:10:13 +0000125
Reid Spencerec7eb452004-05-27 16:28:54 +0000126static cl::alias
127QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
128
Reid Spencerfd90dd52006-08-18 06:34:30 +0000129static cl::opt<bool>
130AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
131
Devang Patelf5f23002010-12-07 00:33:43 +0000132static cl::opt<bool>
133PrintBreakpoints("print-breakpoints-for-testing",
134 cl::desc("Print select breakpoints location for testing"));
135
Chris Lattnerd331cb32009-10-22 00:44:10 +0000136static cl::opt<std::string>
137DefaultDataLayout("default-data-layout",
138 cl::desc("data layout string to use if not specified by module"),
139 cl::value_desc("layout-string"), cl::init(""));
140
Reid Spencerfd90dd52006-08-18 06:34:30 +0000141// ---------- Define Printers for module and function passes ------------
142namespace {
143
Devang Patel28552da2007-06-28 23:09:25 +0000144struct CallGraphSCCPassPrinter : public CallGraphSCCPass {
145 static char ID;
146 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000147 raw_ostream &Out;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000148 std::string PassName;
149
Dan Gohman4931b312010-08-18 17:42:59 +0000150 CallGraphSCCPassPrinter(const PassInfo *PI, raw_ostream &out) :
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000151 CallGraphSCCPass(ID), PassToPrint(PI), Out(out) {
152 std::string PassToPrintName = PassToPrint->getPassName();
153 PassName = "CallGraphSCCPass Printer: " + PassToPrintName;
154 }
Devang Patel28552da2007-06-28 23:09:25 +0000155
Chris Lattner2decb222010-04-16 22:42:17 +0000156 virtual bool runOnSCC(CallGraphSCC &SCC) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000157 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000158 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Devang Patel28552da2007-06-28 23:09:25 +0000159
Devang Patel28552da2007-06-28 23:09:25 +0000160 // Get and print pass...
Tobias Grossere9dcd032010-09-08 15:02:51 +0000161 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
162 Function *F = (*I)->getFunction();
163 if (F)
164 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
165 F->getParent());
166 }
Devang Patel28552da2007-06-28 23:09:25 +0000167 return false;
168 }
Eric Christophera887ae42009-08-21 23:29:40 +0000169
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000170 virtual const char *getPassName() const { return PassName.c_str(); }
Devang Patel28552da2007-06-28 23:09:25 +0000171
172 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000173 AU.addRequiredID(PassToPrint->getTypeInfo());
Devang Patel28552da2007-06-28 23:09:25 +0000174 AU.setPreservesAll();
175 }
176};
177
178char CallGraphSCCPassPrinter::ID = 0;
179
Reid Spencerfd90dd52006-08-18 06:34:30 +0000180struct ModulePassPrinter : public ModulePass {
Devang Patel19974732007-05-03 01:11:54 +0000181 static char ID;
Reid Spencerfd90dd52006-08-18 06:34:30 +0000182 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000183 raw_ostream &Out;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000184 std::string PassName;
185
Dan Gohman4931b312010-08-18 17:42:59 +0000186 ModulePassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000187 : ModulePass(ID), PassToPrint(PI), Out(out) {
188 std::string PassToPrintName = PassToPrint->getPassName();
189 PassName = "ModulePass Printer: " + PassToPrintName;
190 }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000191
192 virtual bool runOnModule(Module &M) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000193 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000194 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Reid Spencerfd90dd52006-08-18 06:34:30 +0000195
196 // Get and print pass...
Tobias Grossere9dcd032010-09-08 15:02:51 +0000197 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, &M);
Reid Spencerfd90dd52006-08-18 06:34:30 +0000198 return false;
199 }
200
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000201 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000202
203 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000204 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000205 AU.setPreservesAll();
206 }
207};
208
Devang Patel19974732007-05-03 01:11:54 +0000209char ModulePassPrinter::ID = 0;
Reid Spencerfd90dd52006-08-18 06:34:30 +0000210struct FunctionPassPrinter : public FunctionPass {
211 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000212 raw_ostream &Out;
Devang Patel19974732007-05-03 01:11:54 +0000213 static char ID;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000214 std::string PassName;
215
Dan Gohman4931b312010-08-18 17:42:59 +0000216 FunctionPassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000217 : FunctionPass(ID), PassToPrint(PI), Out(out) {
218 std::string PassToPrintName = PassToPrint->getPassName();
219 PassName = "FunctionPass Printer: " + PassToPrintName;
220 }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000221
222 virtual bool runOnFunction(Function &F) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000223 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000224 Out << "Printing analysis '" << PassToPrint->getPassName()
225 << "' for function '" << F.getName() << "':\n";
Tobias Grossere9dcd032010-09-08 15:02:51 +0000226
Reid Spencerfd90dd52006-08-18 06:34:30 +0000227 // Get and print pass...
Dan Gohman4931b312010-08-18 17:42:59 +0000228 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
Owen Anderson90c579d2010-08-06 18:33:48 +0000229 F.getParent());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000230 return false;
231 }
232
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000233 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000234
235 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000236 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000237 AU.setPreservesAll();
238 }
239};
240
Devang Patel19974732007-05-03 01:11:54 +0000241char FunctionPassPrinter::ID = 0;
Devang Patel56fb1642007-07-05 15:32:03 +0000242
243struct LoopPassPrinter : public LoopPass {
244 static char ID;
245 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000246 raw_ostream &Out;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000247 std::string PassName;
248
Dan Gohman4931b312010-08-18 17:42:59 +0000249 LoopPassPrinter(const PassInfo *PI, raw_ostream &out) :
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000250 LoopPass(ID), PassToPrint(PI), Out(out) {
251 std::string PassToPrintName = PassToPrint->getPassName();
252 PassName = "LoopPass Printer: " + PassToPrintName;
253 }
254
Devang Patel56fb1642007-07-05 15:32:03 +0000255
256 virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000257 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000258 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Tobias Grossere9dcd032010-09-08 15:02:51 +0000259
Devang Patel56fb1642007-07-05 15:32:03 +0000260 // Get and print pass...
Tobias Grossere9dcd032010-09-08 15:02:51 +0000261 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
262 L->getHeader()->getParent()->getParent());
Devang Patel56fb1642007-07-05 15:32:03 +0000263 return false;
264 }
Eric Christophera887ae42009-08-21 23:29:40 +0000265
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000266 virtual const char *getPassName() const { return PassName.c_str(); }
Devang Patel56fb1642007-07-05 15:32:03 +0000267
268 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000269 AU.addRequiredID(PassToPrint->getTypeInfo());
Devang Patel56fb1642007-07-05 15:32:03 +0000270 AU.setPreservesAll();
271 }
272};
273
274char LoopPassPrinter::ID = 0;
275
Tobias Grosser65513602010-10-20 01:54:44 +0000276struct RegionPassPrinter : public RegionPass {
277 static char ID;
278 const PassInfo *PassToPrint;
279 raw_ostream &Out;
280 std::string PassName;
281
282 RegionPassPrinter(const PassInfo *PI, raw_ostream &out) : RegionPass(ID),
283 PassToPrint(PI), Out(out) {
284 std::string PassToPrintName = PassToPrint->getPassName();
285 PassName = "LoopPass Printer: " + PassToPrintName;
286 }
287
288 virtual bool runOnRegion(Region *R, RGPassManager &RGM) {
289 if (!Quiet) {
290 Out << "Printing analysis '" << PassToPrint->getPassName() << "' for "
291 << "region: '" << R->getNameStr() << "' in function '"
292 << R->getEntry()->getParent()->getNameStr() << "':\n";
293 }
294 // Get and print pass...
295 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
296 R->getEntry()->getParent()->getParent());
297 return false;
298 }
299
300 virtual const char *getPassName() const { return "'Pass' Printer"; }
301
302 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
303 AU.addRequiredID(PassToPrint->getTypeInfo());
304 AU.setPreservesAll();
305 }
306};
307
308char RegionPassPrinter::ID = 0;
309
Reid Spencerfd90dd52006-08-18 06:34:30 +0000310struct BasicBlockPassPrinter : public BasicBlockPass {
311 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000312 raw_ostream &Out;
Devang Patel19974732007-05-03 01:11:54 +0000313 static char ID;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000314 std::string PassName;
315
Dan Gohman4931b312010-08-18 17:42:59 +0000316 BasicBlockPassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000317 : BasicBlockPass(ID), PassToPrint(PI), Out(out) {
318 std::string PassToPrintName = PassToPrint->getPassName();
319 PassName = "BasicBlockPass Printer: " + PassToPrintName;
320 }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000321
322 virtual bool runOnBasicBlock(BasicBlock &BB) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000323 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000324 Out << "Printing Analysis info for BasicBlock '" << BB.getName()
325 << "': Pass " << PassToPrint->getPassName() << ":\n";
Reid Spencerfd90dd52006-08-18 06:34:30 +0000326
327 // Get and print pass...
Dan Gohman4931b312010-08-18 17:42:59 +0000328 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
Owen Anderson90c579d2010-08-06 18:33:48 +0000329 BB.getParent()->getParent());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000330 return false;
331 }
332
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000333 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000334
335 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000336 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000337 AU.setPreservesAll();
338 }
339};
340
Devang Patel19974732007-05-03 01:11:54 +0000341char BasicBlockPassPrinter::ID = 0;
Devang Patelf5f23002010-12-07 00:33:43 +0000342
343struct BreakpointPrinter : public FunctionPass {
344 raw_ostream &Out;
345 static char ID;
346
347 BreakpointPrinter(raw_ostream &out)
348 : FunctionPass(ID), Out(out) {
349 }
350
351 virtual bool runOnFunction(Function &F) {
352 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
353 BasicBlock::const_iterator BI = I->end();
354 --BI;
355 do {
356 const Instruction *In = BI;
357 const DebugLoc DL = In->getDebugLoc();
358 if (!DL.isUnknown()) {
359 DIScope S(DL.getScope(getGlobalContext()));
360 Out << S.getFilename() << " " << DL.getLine() << "\n";
361 break;
362 }
363 --BI;
364 } while (BI != I->begin());
365 break;
366 }
367 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,
432 /*SimplifyLibCalls=*/ true,
433 /*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
530 // about to build...
531 //
532 PassManager Passes;
Chris Lattner00950542001-06-06 20:29:01 +0000533
Chris Lattner61db1a12009-10-22 00:46:41 +0000534 // Add an appropriate TargetData instance for this module...
535 TargetData *TD = 0;
536 const std::string &ModuleDataLayout = M.get()->getDataLayout();
537 if (!ModuleDataLayout.empty())
538 TD = new TargetData(ModuleDataLayout);
Kenneth Uildriksb908f8a2009-11-03 15:29:06 +0000539 else if (!DefaultDataLayout.empty())
Chris Lattner61db1a12009-10-22 00:46:41 +0000540 TD = new TargetData(DefaultDataLayout);
Chris Lattner9c3b55e2003-04-24 19:13:02 +0000541
Chris Lattner61db1a12009-10-22 00:46:41 +0000542 if (TD)
543 Passes.add(TD);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000544
Dan Gohman9cc1c182010-05-14 15:36:54 +0000545 OwningPtr<PassManager> FPasses;
Chris Lattner61db1a12009-10-22 00:46:41 +0000546 if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
Dan Gohman9cc1c182010-05-14 15:36:54 +0000547 FPasses.reset(new PassManager());
Chris Lattnerd331cb32009-10-22 00:44:10 +0000548 if (TD)
Chris Lattner61db1a12009-10-22 00:46:41 +0000549 FPasses->add(new TargetData(*TD));
550 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000551
Devang Patelf5f23002010-12-07 00:33:43 +0000552 if (PrintBreakpoints) {
553 // Default to standard output.
554 if (!Out) {
555 if (OutputFilename.empty())
556 OutputFilename = "-";
557
558 std::string ErrorInfo;
559 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
560 raw_fd_ostream::F_Binary));
561 if (!ErrorInfo.empty()) {
562 errs() << ErrorInfo << '\n';
563 return 1;
564 }
565 }
566 Passes.add(new BreakpointPrinter(Out->os()));
567 NoOutput = true;
568 }
569
Chris Lattner61db1a12009-10-22 00:46:41 +0000570 // If the -strip-debug command line option was specified, add it. If
571 // -std-compile-opts was also specified, it will handle StripDebug.
572 if (StripDebug && !StandardCompileOpts)
573 addPass(Passes, createStripSymbolsPass(true));
Eric Christophera887ae42009-08-21 23:29:40 +0000574
Chris Lattner61db1a12009-10-22 00:46:41 +0000575 // Create a new optimization pass for each one specified on the command line
576 for (unsigned i = 0; i < PassList.size(); ++i) {
577 // Check to see if -std-compile-opts was specified before this option. If
578 // so, handle it.
579 if (StandardCompileOpts &&
580 StandardCompileOpts.getPosition() < PassList.getPosition(i)) {
Chris Lattner3dda08a2008-07-13 19:35:21 +0000581 AddStandardCompilePasses(Passes);
582 StandardCompileOpts = false;
Eric Christophera887ae42009-08-21 23:29:40 +0000583 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000584
Chris Lattner61db1a12009-10-22 00:46:41 +0000585 if (StandardLinkOpts &&
586 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000587 AddStandardLinkPasses(Passes);
588 StandardLinkOpts = false;
Eric Christophera887ae42009-08-21 23:29:40 +0000589 }
Daniel Dunbaradc82882009-07-17 18:09:39 +0000590
Chris Lattner61db1a12009-10-22 00:46:41 +0000591 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000592 AddOptimizationPasses(Passes, *FPasses, 1);
Chris Lattner61db1a12009-10-22 00:46:41 +0000593 OptLevelO1 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000594 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000595
Chris Lattner61db1a12009-10-22 00:46:41 +0000596 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000597 AddOptimizationPasses(Passes, *FPasses, 2);
Chris Lattner61db1a12009-10-22 00:46:41 +0000598 OptLevelO2 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000599 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000600
Chris Lattner61db1a12009-10-22 00:46:41 +0000601 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000602 AddOptimizationPasses(Passes, *FPasses, 3);
Chris Lattner61db1a12009-10-22 00:46:41 +0000603 OptLevelO3 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000604 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000605
Chris Lattner61db1a12009-10-22 00:46:41 +0000606 const PassInfo *PassInf = PassList[i];
607 Pass *P = 0;
608 if (PassInf->getNormalCtor())
609 P = PassInf->getNormalCtor()();
610 else
611 errs() << argv[0] << ": cannot create pass: "
612 << PassInf->getPassName() << "\n";
613 if (P) {
Benjamin Kramer3460f222010-02-18 12:57:05 +0000614 PassKind Kind = P->getPassKind();
Chris Lattner61db1a12009-10-22 00:46:41 +0000615 addPass(Passes, P);
616
617 if (AnalyzeOnly) {
Benjamin Kramer3460f222010-02-18 12:57:05 +0000618 switch (Kind) {
Chris Lattner476e9bd2010-01-22 06:03:06 +0000619 case PT_BasicBlock:
Dan Gohmand4c45432010-09-01 14:20:41 +0000620 Passes.add(new BasicBlockPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000621 break;
Tobias Grosser65513602010-10-20 01:54:44 +0000622 case PT_Region:
623 Passes.add(new RegionPassPrinter(PassInf, Out->os()));
624 break;
Chris Lattner476e9bd2010-01-22 06:03:06 +0000625 case PT_Loop:
Dan Gohmand4c45432010-09-01 14:20:41 +0000626 Passes.add(new LoopPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000627 break;
628 case PT_Function:
Dan Gohmand4c45432010-09-01 14:20:41 +0000629 Passes.add(new FunctionPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000630 break;
631 case PT_CallGraphSCC:
Dan Gohmand4c45432010-09-01 14:20:41 +0000632 Passes.add(new CallGraphSCCPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000633 break;
634 default:
Dan Gohmand4c45432010-09-01 14:20:41 +0000635 Passes.add(new ModulePassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000636 break;
637 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000638 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000639 }
640
Chris Lattner61db1a12009-10-22 00:46:41 +0000641 if (PrintEachXForm)
642 Passes.add(createPrintModulePass(&errs()));
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000643 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000644
645 // If -std-compile-opts was specified at the end of the pass list, add them.
646 if (StandardCompileOpts) {
647 AddStandardCompilePasses(Passes);
648 StandardCompileOpts = false;
649 }
650
651 if (StandardLinkOpts) {
652 AddStandardLinkPasses(Passes);
653 StandardLinkOpts = false;
654 }
655
656 if (OptLevelO1)
657 AddOptimizationPasses(Passes, *FPasses, 1);
658
659 if (OptLevelO2)
660 AddOptimizationPasses(Passes, *FPasses, 2);
661
662 if (OptLevelO3)
663 AddOptimizationPasses(Passes, *FPasses, 3);
664
Dan Gohman9cc1c182010-05-14 15:36:54 +0000665 if (OptLevelO1 || OptLevelO2 || OptLevelO3)
666 FPasses->run(*M.get());
Chris Lattner61db1a12009-10-22 00:46:41 +0000667
668 // Check that the module is well formed on completion of optimization
669 if (!NoVerify && !VerifyEach)
670 Passes.add(createVerifierPass());
671
Dan Gohman4931b312010-08-18 17:42:59 +0000672 // Write bitcode or assembly to the output as the last step...
Chris Lattner61db1a12009-10-22 00:46:41 +0000673 if (!NoOutput && !AnalyzeOnly) {
674 if (OutputAssembly)
Dan Gohmand4c45432010-09-01 14:20:41 +0000675 Passes.add(createPrintModulePass(&Out->os()));
Chris Lattner61db1a12009-10-22 00:46:41 +0000676 else
Dan Gohmand4c45432010-09-01 14:20:41 +0000677 Passes.add(createBitcodeWriterPass(Out->os()));
Chris Lattner61db1a12009-10-22 00:46:41 +0000678 }
679
680 // Now that we have all of the passes ready, run them.
681 Passes.run(*M.get());
682
Dan Gohmand5826a32010-08-20 01:07:01 +0000683 // Declare success.
Devang Patelf5f23002010-12-07 00:33:43 +0000684 if (!NoOutput || PrintBreakpoints)
Dan Gohmand5826a32010-08-20 01:07:01 +0000685 Out->keep();
686
Chris Lattner61db1a12009-10-22 00:46:41 +0000687 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000688}