blob: 85d365306a27b823009102f0d50398537290c07a [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 }
Devang Patel29012312010-12-09 23:37:07 +0000367 BasicBlock &EntryBB = F.getEntryBlock();
368 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
369 BasicBlock *BB = I;
370 if (BB == &EntryBB) continue;
371 for (BasicBlock::iterator BI = I->begin(), BE = I->end(); BI != BE; ++BI)
372 if (CallInst *CI = dyn_cast<CallInst>(BI)) {
373 const DebugLoc DL = CI->getDebugLoc();
374 if (!DL.isUnknown()) {
375 DIScope S(DL.getScope(getGlobalContext()));
376 Out << S.getFilename() << " " << DL.getLine() << "\n";
377 }
378 }
379 }
Devang Patelf5f23002010-12-07 00:33:43 +0000380 return false;
381 }
382
383 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
384 AU.setPreservesAll();
385 }
386};
387
388char BreakpointPrinter::ID = 0;
389
Dan Gohman9cc1c182010-05-14 15:36:54 +0000390inline void addPass(PassManagerBase &PM, Pass *P) {
Reid Spencer74ed9972007-02-02 14:46:29 +0000391 // Add the pass to the pass manager...
392 PM.add(P);
393
394 // If we are verifying all of the intermediate steps, add the verifier...
395 if (VerifyEach) PM.add(createVerifierPass());
396}
397
Eric Christophera887ae42009-08-21 23:29:40 +0000398/// AddOptimizationPasses - This routine adds optimization passes
399/// based on selected optimization level, OptLevel. This routine
Devang Patel2d7551c2008-09-16 22:25:14 +0000400/// duplicates llvm-gcc behaviour.
401///
402/// OptLevel - Optimization Level
Dan Gohman9cc1c182010-05-14 15:36:54 +0000403void AddOptimizationPasses(PassManagerBase &MPM, PassManagerBase &FPM,
Zhongxing Xu3253f4c2008-11-26 02:57:24 +0000404 unsigned OptLevel) {
Daniel Dunbarca8131e2009-06-03 18:22:15 +0000405 createStandardFunctionPasses(&FPM, OptLevel);
Devang Patel2d7551c2008-09-16 22:25:14 +0000406
Eli Friedman74733a72010-01-18 22:38:31 +0000407 llvm::Pass *InliningPass = 0;
408 if (DisableInline) {
409 // No inlining pass
410 } else if (OptLevel) {
Jakob Stoklund Olesene6c6cec2010-11-02 23:40:28 +0000411 unsigned Threshold = 225;
Eli Friedman74733a72010-01-18 22:38:31 +0000412 if (OptLevel > 2)
Jakob Stoklund Olesene6c6cec2010-11-02 23:40:28 +0000413 Threshold = 275;
Eli Friedman74733a72010-01-18 22:38:31 +0000414 InliningPass = createFunctionInliningPass(Threshold);
415 } else {
416 InliningPass = createAlwaysInlinerPass();
417 }
Daniel Dunbarca8131e2009-06-03 18:22:15 +0000418 createStandardModulePasses(&MPM, OptLevel,
419 /*OptimizeSize=*/ false,
420 UnitAtATime,
421 /*UnrollLoops=*/ OptLevel > 1,
422 !DisableSimplifyLibCalls,
423 /*HaveExceptions=*/ true,
424 InliningPass);
Devang Patel2d7551c2008-09-16 22:25:14 +0000425}
426
Dan Gohman9cc1c182010-05-14 15:36:54 +0000427void AddStandardCompilePasses(PassManagerBase &PM) {
Reid Spencer74ed9972007-02-02 14:46:29 +0000428 PM.add(createVerifierPass()); // Verify that input is correct
429
430 addPass(PM, createLowerSetJmpPass()); // Lower llvm.setjmp/.longjmp
Reid Spencer74ed9972007-02-02 14:46:29 +0000431
432 // If the -strip-debug command line option was specified, do it.
433 if (StripDebug)
434 addPass(PM, createStripSymbolsPass(true));
435
436 if (DisableOptimizations) return;
437
Daniel Dunbarca8131e2009-06-03 18:22:15 +0000438 llvm::Pass *InliningPass = !DisableInline ? createFunctionInliningPass() : 0;
Reid Spencer74ed9972007-02-02 14:46:29 +0000439
Daniel Dunbarca8131e2009-06-03 18:22:15 +0000440 // -std-compile-opts adds the same module passes as -O3.
Eric Christophera887ae42009-08-21 23:29:40 +0000441 createStandardModulePasses(&PM, 3,
Daniel Dunbarca8131e2009-06-03 18:22:15 +0000442 /*OptimizeSize=*/ false,
443 /*UnitAtATime=*/ true,
444 /*UnrollLoops=*/ true,
445 /*SimplifyLibCalls=*/ true,
446 /*HaveExceptions=*/ true,
447 InliningPass);
Reid Spencer74ed9972007-02-02 14:46:29 +0000448}
449
Dan Gohman9cc1c182010-05-14 15:36:54 +0000450void AddStandardLinkPasses(PassManagerBase &PM) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000451 PM.add(createVerifierPass()); // Verify that input is correct
452
453 // If the -strip-debug command line option was specified, do it.
454 if (StripDebug)
455 addPass(PM, createStripSymbolsPass(true));
456
457 if (DisableOptimizations) return;
458
459 createStandardLTOPasses(&PM, /*Internalize=*/ !DisableInternalize,
460 /*RunInliner=*/ !DisableInline,
461 /*VerifyEach=*/ VerifyEach);
462}
463
Reid Spencerfd90dd52006-08-18 06:34:30 +0000464} // anonymous namespace
465
Chris Lattner0be41012002-02-01 04:54:11 +0000466
Chris Lattnerc0ce68b2002-07-23 18:12:22 +0000467//===----------------------------------------------------------------------===//
468// main for opt
469//
Chris Lattner00950542001-06-06 20:29:01 +0000470int main(int argc, char **argv) {
Chris Lattnerc0d91b72009-12-09 00:41:28 +0000471 sys::PrintStackTraceOnErrorSignal();
472 llvm::PrettyStackTraceProgram X(argc, argv);
Dan Gohmand4c45432010-09-01 14:20:41 +0000473
David Greene08fc0d32010-01-05 01:30:32 +0000474 // Enable debug stream buffering.
475 EnableDebugBuffering = true;
476
Chris Lattnerc0d91b72009-12-09 00:41:28 +0000477 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Owen Anderson0d7c6952009-07-15 22:16:10 +0000478 LLVMContext &Context = getGlobalContext();
Chris Lattner61db1a12009-10-22 00:46:41 +0000479
Owen Anderson081c34b2010-10-19 17:21:58 +0000480 // Initialize passes
481 PassRegistry &Registry = *PassRegistry::getPassRegistry();
482 initializeCore(Registry);
483 initializeScalarOpts(Registry);
484 initializeIPO(Registry);
485 initializeAnalysis(Registry);
486 initializeIPA(Registry);
487 initializeTransformUtils(Registry);
488 initializeInstCombine(Registry);
489 initializeInstrumentation(Registry);
490 initializeTarget(Registry);
491
Chris Lattner61db1a12009-10-22 00:46:41 +0000492 cl::ParseCommandLineOptions(argc, argv,
493 "llvm .bc -> .bc modular optimizer and analysis printer\n");
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000494
Tobias Grosser7593f342010-12-02 20:35:16 +0000495 if (AnalyzeOnly && NoOutput) {
496 errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
497 return 1;
498 }
499
Chris Lattner61db1a12009-10-22 00:46:41 +0000500 // Allocate a full target machine description only if necessary.
501 // FIXME: The choice of target should be controllable on the command line.
502 std::auto_ptr<TargetMachine> target;
Devang Patelff5d06d2008-08-27 20:51:49 +0000503
Chris Lattner61db1a12009-10-22 00:46:41 +0000504 SMDiagnostic Err;
Vikram S. Adve18fdfc42002-09-16 16:09:43 +0000505
Chris Lattner61db1a12009-10-22 00:46:41 +0000506 // Load the input module...
507 std::auto_ptr<Module> M;
508 M.reset(ParseIRFile(InputFilename, Err, Context));
Eric Christophera887ae42009-08-21 23:29:40 +0000509
Chris Lattner61db1a12009-10-22 00:46:41 +0000510 if (M.get() == 0) {
511 Err.Print(argv[0], errs());
512 return 1;
513 }
514
515 // Figure out what stream we are supposed to write to...
Dan Gohmand5826a32010-08-20 01:07:01 +0000516 OwningPtr<tool_output_file> Out;
Dan Gohman4931b312010-08-18 17:42:59 +0000517 if (NoOutput) {
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000518 if (!OutputFilename.empty())
519 errs() << "WARNING: The -o (output filename) option is ignored when\n"
Dan Gohman4931b312010-08-18 17:42:59 +0000520 "the --disable-output option is used.\n";
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000521 } else {
522 // Default to standard output.
523 if (OutputFilename.empty())
524 OutputFilename = "-";
Chris Lattner61db1a12009-10-22 00:46:41 +0000525
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000526 std::string ErrorInfo;
Dan Gohmand5826a32010-08-20 01:07:01 +0000527 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
528 raw_fd_ostream::F_Binary));
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000529 if (!ErrorInfo.empty()) {
530 errs() << ErrorInfo << '\n';
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000531 return 1;
Chris Lattner00950542001-06-06 20:29:01 +0000532 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000533 }
Chris Lattner76d12292002-04-18 19:55:25 +0000534
Chris Lattner61db1a12009-10-22 00:46:41 +0000535 // If the output is set to be emitted to standard out, and standard out is a
536 // console, print out a warning message and refuse to do it. We don't
537 // impress anyone by spewing tons of binary goo to a terminal.
Dan Gohmanb56bf582010-01-17 17:47:24 +0000538 if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
Dan Gohmand4c45432010-09-01 14:20:41 +0000539 if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
Chris Lattner61db1a12009-10-22 00:46:41 +0000540 NoOutput = true;
Dan Gohmanec080462009-09-11 20:46:33 +0000541
Chris Lattner61db1a12009-10-22 00:46:41 +0000542 // Create a PassManager to hold and optimize the collection of passes we are
543 // about to build...
544 //
545 PassManager Passes;
Chris Lattner00950542001-06-06 20:29:01 +0000546
Chris Lattner61db1a12009-10-22 00:46:41 +0000547 // Add an appropriate TargetData instance for this module...
548 TargetData *TD = 0;
549 const std::string &ModuleDataLayout = M.get()->getDataLayout();
550 if (!ModuleDataLayout.empty())
551 TD = new TargetData(ModuleDataLayout);
Kenneth Uildriksb908f8a2009-11-03 15:29:06 +0000552 else if (!DefaultDataLayout.empty())
Chris Lattner61db1a12009-10-22 00:46:41 +0000553 TD = new TargetData(DefaultDataLayout);
Chris Lattner9c3b55e2003-04-24 19:13:02 +0000554
Chris Lattner61db1a12009-10-22 00:46:41 +0000555 if (TD)
556 Passes.add(TD);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000557
Dan Gohman9cc1c182010-05-14 15:36:54 +0000558 OwningPtr<PassManager> FPasses;
Chris Lattner61db1a12009-10-22 00:46:41 +0000559 if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
Dan Gohman9cc1c182010-05-14 15:36:54 +0000560 FPasses.reset(new PassManager());
Chris Lattnerd331cb32009-10-22 00:44:10 +0000561 if (TD)
Chris Lattner61db1a12009-10-22 00:46:41 +0000562 FPasses->add(new TargetData(*TD));
563 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000564
Devang Patelf5f23002010-12-07 00:33:43 +0000565 if (PrintBreakpoints) {
566 // Default to standard output.
567 if (!Out) {
568 if (OutputFilename.empty())
569 OutputFilename = "-";
570
571 std::string ErrorInfo;
572 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
573 raw_fd_ostream::F_Binary));
574 if (!ErrorInfo.empty()) {
575 errs() << ErrorInfo << '\n';
576 return 1;
577 }
578 }
579 Passes.add(new BreakpointPrinter(Out->os()));
580 NoOutput = true;
581 }
582
Chris Lattner61db1a12009-10-22 00:46:41 +0000583 // If the -strip-debug command line option was specified, add it. If
584 // -std-compile-opts was also specified, it will handle StripDebug.
585 if (StripDebug && !StandardCompileOpts)
586 addPass(Passes, createStripSymbolsPass(true));
Eric Christophera887ae42009-08-21 23:29:40 +0000587
Chris Lattner61db1a12009-10-22 00:46:41 +0000588 // Create a new optimization pass for each one specified on the command line
589 for (unsigned i = 0; i < PassList.size(); ++i) {
590 // Check to see if -std-compile-opts was specified before this option. If
591 // so, handle it.
592 if (StandardCompileOpts &&
593 StandardCompileOpts.getPosition() < PassList.getPosition(i)) {
Chris Lattner3dda08a2008-07-13 19:35:21 +0000594 AddStandardCompilePasses(Passes);
595 StandardCompileOpts = false;
Eric Christophera887ae42009-08-21 23:29:40 +0000596 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000597
Chris Lattner61db1a12009-10-22 00:46:41 +0000598 if (StandardLinkOpts &&
599 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000600 AddStandardLinkPasses(Passes);
601 StandardLinkOpts = false;
Eric Christophera887ae42009-08-21 23:29:40 +0000602 }
Daniel Dunbaradc82882009-07-17 18:09:39 +0000603
Chris Lattner61db1a12009-10-22 00:46:41 +0000604 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000605 AddOptimizationPasses(Passes, *FPasses, 1);
Chris Lattner61db1a12009-10-22 00:46:41 +0000606 OptLevelO1 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000607 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000608
Chris Lattner61db1a12009-10-22 00:46:41 +0000609 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000610 AddOptimizationPasses(Passes, *FPasses, 2);
Chris Lattner61db1a12009-10-22 00:46:41 +0000611 OptLevelO2 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000612 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000613
Chris Lattner61db1a12009-10-22 00:46:41 +0000614 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000615 AddOptimizationPasses(Passes, *FPasses, 3);
Chris Lattner61db1a12009-10-22 00:46:41 +0000616 OptLevelO3 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000617 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000618
Chris Lattner61db1a12009-10-22 00:46:41 +0000619 const PassInfo *PassInf = PassList[i];
620 Pass *P = 0;
621 if (PassInf->getNormalCtor())
622 P = PassInf->getNormalCtor()();
623 else
624 errs() << argv[0] << ": cannot create pass: "
625 << PassInf->getPassName() << "\n";
626 if (P) {
Benjamin Kramer3460f222010-02-18 12:57:05 +0000627 PassKind Kind = P->getPassKind();
Chris Lattner61db1a12009-10-22 00:46:41 +0000628 addPass(Passes, P);
629
630 if (AnalyzeOnly) {
Benjamin Kramer3460f222010-02-18 12:57:05 +0000631 switch (Kind) {
Chris Lattner476e9bd2010-01-22 06:03:06 +0000632 case PT_BasicBlock:
Dan Gohmand4c45432010-09-01 14:20:41 +0000633 Passes.add(new BasicBlockPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000634 break;
Tobias Grosser65513602010-10-20 01:54:44 +0000635 case PT_Region:
636 Passes.add(new RegionPassPrinter(PassInf, Out->os()));
637 break;
Chris Lattner476e9bd2010-01-22 06:03:06 +0000638 case PT_Loop:
Dan Gohmand4c45432010-09-01 14:20:41 +0000639 Passes.add(new LoopPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000640 break;
641 case PT_Function:
Dan Gohmand4c45432010-09-01 14:20:41 +0000642 Passes.add(new FunctionPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000643 break;
644 case PT_CallGraphSCC:
Dan Gohmand4c45432010-09-01 14:20:41 +0000645 Passes.add(new CallGraphSCCPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000646 break;
647 default:
Dan Gohmand4c45432010-09-01 14:20:41 +0000648 Passes.add(new ModulePassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000649 break;
650 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000651 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000652 }
653
Chris Lattner61db1a12009-10-22 00:46:41 +0000654 if (PrintEachXForm)
655 Passes.add(createPrintModulePass(&errs()));
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000656 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000657
658 // If -std-compile-opts was specified at the end of the pass list, add them.
659 if (StandardCompileOpts) {
660 AddStandardCompilePasses(Passes);
661 StandardCompileOpts = false;
662 }
663
664 if (StandardLinkOpts) {
665 AddStandardLinkPasses(Passes);
666 StandardLinkOpts = false;
667 }
668
669 if (OptLevelO1)
670 AddOptimizationPasses(Passes, *FPasses, 1);
671
672 if (OptLevelO2)
673 AddOptimizationPasses(Passes, *FPasses, 2);
674
675 if (OptLevelO3)
676 AddOptimizationPasses(Passes, *FPasses, 3);
677
Dan Gohman9cc1c182010-05-14 15:36:54 +0000678 if (OptLevelO1 || OptLevelO2 || OptLevelO3)
679 FPasses->run(*M.get());
Chris Lattner61db1a12009-10-22 00:46:41 +0000680
681 // Check that the module is well formed on completion of optimization
682 if (!NoVerify && !VerifyEach)
683 Passes.add(createVerifierPass());
684
Dan Gohman4931b312010-08-18 17:42:59 +0000685 // Write bitcode or assembly to the output as the last step...
Chris Lattner61db1a12009-10-22 00:46:41 +0000686 if (!NoOutput && !AnalyzeOnly) {
687 if (OutputAssembly)
Dan Gohmand4c45432010-09-01 14:20:41 +0000688 Passes.add(createPrintModulePass(&Out->os()));
Chris Lattner61db1a12009-10-22 00:46:41 +0000689 else
Dan Gohmand4c45432010-09-01 14:20:41 +0000690 Passes.add(createBitcodeWriterPass(Out->os()));
Chris Lattner61db1a12009-10-22 00:46:41 +0000691 }
692
693 // Now that we have all of the passes ready, run them.
694 Passes.run(*M.get());
695
Dan Gohmand5826a32010-08-20 01:07:01 +0000696 // Declare success.
Devang Patelf5f23002010-12-07 00:33:43 +0000697 if (!NoOutput || PrintBreakpoints)
Dan Gohmand5826a32010-08-20 01:07:01 +0000698 Out->keep();
699
Chris Lattner61db1a12009-10-22 00:46:41 +0000700 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000701}