blob: 0a945df7f7be1854c99f31dcd74aa16aa6d79f34 [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();
Tobias Grosser58396142011-01-20 21:03:20 +0000285 PassName = "RegionPass Printer: " + PassToPrintName;
Tobias Grosser65513602010-10-20 01:54:44 +0000286 }
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
Tobias Grosser58396142011-01-20 21:03:20 +0000300 virtual const char *getPassName() const { return PassName.c_str(); }
Tobias Grosser65513602010-10-20 01:54:44 +0000301
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) {
Devang Patel29012312010-12-09 23:37:07 +0000352 BasicBlock &EntryBB = F.getEntryBlock();
Devang Patel97f6d5b2011-01-31 21:36:24 +0000353 BasicBlock::const_iterator BI = EntryBB.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 != EntryBB.begin());
Devang Patelf5f23002010-12-07 00:33:43 +0000365 return false;
366 }
367
368 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
369 AU.setPreservesAll();
370 }
371};
372
373char BreakpointPrinter::ID = 0;
374
Dan Gohman9cc1c182010-05-14 15:36:54 +0000375inline void addPass(PassManagerBase &PM, Pass *P) {
Reid Spencer74ed9972007-02-02 14:46:29 +0000376 // Add the pass to the pass manager...
377 PM.add(P);
378
379 // If we are verifying all of the intermediate steps, add the verifier...
380 if (VerifyEach) PM.add(createVerifierPass());
381}
382
Eric Christophera887ae42009-08-21 23:29:40 +0000383/// AddOptimizationPasses - This routine adds optimization passes
384/// based on selected optimization level, OptLevel. This routine
Devang Patel2d7551c2008-09-16 22:25:14 +0000385/// duplicates llvm-gcc behaviour.
386///
387/// OptLevel - Optimization Level
Dan Gohman9cc1c182010-05-14 15:36:54 +0000388void AddOptimizationPasses(PassManagerBase &MPM, PassManagerBase &FPM,
Zhongxing Xu3253f4c2008-11-26 02:57:24 +0000389 unsigned OptLevel) {
Daniel Dunbarca8131e2009-06-03 18:22:15 +0000390 createStandardFunctionPasses(&FPM, OptLevel);
Devang Patel2d7551c2008-09-16 22:25:14 +0000391
Eli Friedman74733a72010-01-18 22:38:31 +0000392 llvm::Pass *InliningPass = 0;
393 if (DisableInline) {
394 // No inlining pass
395 } else if (OptLevel) {
Jakob Stoklund Olesene6c6cec2010-11-02 23:40:28 +0000396 unsigned Threshold = 225;
Eli Friedman74733a72010-01-18 22:38:31 +0000397 if (OptLevel > 2)
Jakob Stoklund Olesene6c6cec2010-11-02 23:40:28 +0000398 Threshold = 275;
Eli Friedman74733a72010-01-18 22:38:31 +0000399 InliningPass = createFunctionInliningPass(Threshold);
400 } else {
401 InliningPass = createAlwaysInlinerPass();
402 }
Daniel Dunbarca8131e2009-06-03 18:22:15 +0000403 createStandardModulePasses(&MPM, OptLevel,
404 /*OptimizeSize=*/ false,
405 UnitAtATime,
406 /*UnrollLoops=*/ OptLevel > 1,
407 !DisableSimplifyLibCalls,
408 /*HaveExceptions=*/ true,
409 InliningPass);
Devang Patel2d7551c2008-09-16 22:25:14 +0000410}
411
Dan Gohman9cc1c182010-05-14 15:36:54 +0000412void AddStandardCompilePasses(PassManagerBase &PM) {
Reid Spencer74ed9972007-02-02 14:46:29 +0000413 PM.add(createVerifierPass()); // Verify that input is correct
414
415 addPass(PM, createLowerSetJmpPass()); // Lower llvm.setjmp/.longjmp
Reid Spencer74ed9972007-02-02 14:46:29 +0000416
417 // If the -strip-debug command line option was specified, do it.
418 if (StripDebug)
419 addPass(PM, createStripSymbolsPass(true));
420
421 if (DisableOptimizations) return;
422
Daniel Dunbarca8131e2009-06-03 18:22:15 +0000423 llvm::Pass *InliningPass = !DisableInline ? createFunctionInliningPass() : 0;
Reid Spencer74ed9972007-02-02 14:46:29 +0000424
Daniel Dunbarca8131e2009-06-03 18:22:15 +0000425 // -std-compile-opts adds the same module passes as -O3.
Eric Christophera887ae42009-08-21 23:29:40 +0000426 createStandardModulePasses(&PM, 3,
Daniel Dunbarca8131e2009-06-03 18:22:15 +0000427 /*OptimizeSize=*/ false,
428 /*UnitAtATime=*/ true,
429 /*UnrollLoops=*/ true,
Peter Collingbourne90d238c2011-02-18 02:59:21 +0000430 !DisableSimplifyLibCalls,
Daniel Dunbarca8131e2009-06-03 18:22:15 +0000431 /*HaveExceptions=*/ true,
432 InliningPass);
Reid Spencer74ed9972007-02-02 14:46:29 +0000433}
434
Dan Gohman9cc1c182010-05-14 15:36:54 +0000435void AddStandardLinkPasses(PassManagerBase &PM) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000436 PM.add(createVerifierPass()); // Verify that input is correct
437
438 // If the -strip-debug command line option was specified, do it.
439 if (StripDebug)
440 addPass(PM, createStripSymbolsPass(true));
441
442 if (DisableOptimizations) return;
443
444 createStandardLTOPasses(&PM, /*Internalize=*/ !DisableInternalize,
445 /*RunInliner=*/ !DisableInline,
446 /*VerifyEach=*/ VerifyEach);
447}
448
Reid Spencerfd90dd52006-08-18 06:34:30 +0000449} // anonymous namespace
450
Chris Lattner0be41012002-02-01 04:54:11 +0000451
Chris Lattnerc0ce68b2002-07-23 18:12:22 +0000452//===----------------------------------------------------------------------===//
453// main for opt
454//
Chris Lattner00950542001-06-06 20:29:01 +0000455int main(int argc, char **argv) {
Chris Lattnerc0d91b72009-12-09 00:41:28 +0000456 sys::PrintStackTraceOnErrorSignal();
457 llvm::PrettyStackTraceProgram X(argc, argv);
Dan Gohmand4c45432010-09-01 14:20:41 +0000458
David Greene08fc0d32010-01-05 01:30:32 +0000459 // Enable debug stream buffering.
460 EnableDebugBuffering = true;
461
Chris Lattnerc0d91b72009-12-09 00:41:28 +0000462 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Owen Anderson0d7c6952009-07-15 22:16:10 +0000463 LLVMContext &Context = getGlobalContext();
Chris Lattner61db1a12009-10-22 00:46:41 +0000464
Owen Anderson081c34b2010-10-19 17:21:58 +0000465 // Initialize passes
466 PassRegistry &Registry = *PassRegistry::getPassRegistry();
467 initializeCore(Registry);
468 initializeScalarOpts(Registry);
469 initializeIPO(Registry);
470 initializeAnalysis(Registry);
471 initializeIPA(Registry);
472 initializeTransformUtils(Registry);
473 initializeInstCombine(Registry);
474 initializeInstrumentation(Registry);
475 initializeTarget(Registry);
476
Chris Lattner61db1a12009-10-22 00:46:41 +0000477 cl::ParseCommandLineOptions(argc, argv,
478 "llvm .bc -> .bc modular optimizer and analysis printer\n");
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000479
Tobias Grosser7593f342010-12-02 20:35:16 +0000480 if (AnalyzeOnly && NoOutput) {
481 errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
482 return 1;
483 }
484
Chris Lattner61db1a12009-10-22 00:46:41 +0000485 // Allocate a full target machine description only if necessary.
486 // FIXME: The choice of target should be controllable on the command line.
487 std::auto_ptr<TargetMachine> target;
Devang Patelff5d06d2008-08-27 20:51:49 +0000488
Chris Lattner61db1a12009-10-22 00:46:41 +0000489 SMDiagnostic Err;
Vikram S. Adve18fdfc42002-09-16 16:09:43 +0000490
Chris Lattner61db1a12009-10-22 00:46:41 +0000491 // Load the input module...
492 std::auto_ptr<Module> M;
493 M.reset(ParseIRFile(InputFilename, Err, Context));
Eric Christophera887ae42009-08-21 23:29:40 +0000494
Chris Lattner61db1a12009-10-22 00:46:41 +0000495 if (M.get() == 0) {
496 Err.Print(argv[0], errs());
497 return 1;
498 }
499
500 // Figure out what stream we are supposed to write to...
Dan Gohmand5826a32010-08-20 01:07:01 +0000501 OwningPtr<tool_output_file> Out;
Dan Gohman4931b312010-08-18 17:42:59 +0000502 if (NoOutput) {
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000503 if (!OutputFilename.empty())
504 errs() << "WARNING: The -o (output filename) option is ignored when\n"
Dan Gohman4931b312010-08-18 17:42:59 +0000505 "the --disable-output option is used.\n";
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000506 } else {
507 // Default to standard output.
508 if (OutputFilename.empty())
509 OutputFilename = "-";
Chris Lattner61db1a12009-10-22 00:46:41 +0000510
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000511 std::string ErrorInfo;
Dan Gohmand5826a32010-08-20 01:07:01 +0000512 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
513 raw_fd_ostream::F_Binary));
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000514 if (!ErrorInfo.empty()) {
515 errs() << ErrorInfo << '\n';
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000516 return 1;
Chris Lattner00950542001-06-06 20:29:01 +0000517 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000518 }
Chris Lattner76d12292002-04-18 19:55:25 +0000519
Chris Lattner61db1a12009-10-22 00:46:41 +0000520 // If the output is set to be emitted to standard out, and standard out is a
521 // console, print out a warning message and refuse to do it. We don't
522 // impress anyone by spewing tons of binary goo to a terminal.
Dan Gohmanb56bf582010-01-17 17:47:24 +0000523 if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
Dan Gohmand4c45432010-09-01 14:20:41 +0000524 if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
Chris Lattner61db1a12009-10-22 00:46:41 +0000525 NoOutput = true;
Dan Gohmanec080462009-09-11 20:46:33 +0000526
Chris Lattner61db1a12009-10-22 00:46:41 +0000527 // Create a PassManager to hold and optimize the collection of passes we are
528 // about to build...
529 //
530 PassManager Passes;
Chris Lattner00950542001-06-06 20:29:01 +0000531
Chris Lattner61db1a12009-10-22 00:46:41 +0000532 // Add an appropriate TargetData instance for this module...
533 TargetData *TD = 0;
534 const std::string &ModuleDataLayout = M.get()->getDataLayout();
535 if (!ModuleDataLayout.empty())
536 TD = new TargetData(ModuleDataLayout);
Kenneth Uildriksb908f8a2009-11-03 15:29:06 +0000537 else if (!DefaultDataLayout.empty())
Chris Lattner61db1a12009-10-22 00:46:41 +0000538 TD = new TargetData(DefaultDataLayout);
Chris Lattner9c3b55e2003-04-24 19:13:02 +0000539
Chris Lattner61db1a12009-10-22 00:46:41 +0000540 if (TD)
541 Passes.add(TD);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000542
Dan Gohman9cc1c182010-05-14 15:36:54 +0000543 OwningPtr<PassManager> FPasses;
Chris Lattner61db1a12009-10-22 00:46:41 +0000544 if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
Dan Gohman9cc1c182010-05-14 15:36:54 +0000545 FPasses.reset(new PassManager());
Chris Lattnerd331cb32009-10-22 00:44:10 +0000546 if (TD)
Chris Lattner61db1a12009-10-22 00:46:41 +0000547 FPasses->add(new TargetData(*TD));
548 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000549
Devang Patelf5f23002010-12-07 00:33:43 +0000550 if (PrintBreakpoints) {
551 // Default to standard output.
552 if (!Out) {
553 if (OutputFilename.empty())
554 OutputFilename = "-";
555
556 std::string ErrorInfo;
557 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
558 raw_fd_ostream::F_Binary));
559 if (!ErrorInfo.empty()) {
560 errs() << ErrorInfo << '\n';
561 return 1;
562 }
563 }
564 Passes.add(new BreakpointPrinter(Out->os()));
565 NoOutput = true;
566 }
567
Chris Lattner61db1a12009-10-22 00:46:41 +0000568 // If the -strip-debug command line option was specified, add it. If
569 // -std-compile-opts was also specified, it will handle StripDebug.
570 if (StripDebug && !StandardCompileOpts)
571 addPass(Passes, createStripSymbolsPass(true));
Eric Christophera887ae42009-08-21 23:29:40 +0000572
Chris Lattner61db1a12009-10-22 00:46:41 +0000573 // Create a new optimization pass for each one specified on the command line
574 for (unsigned i = 0; i < PassList.size(); ++i) {
575 // Check to see if -std-compile-opts was specified before this option. If
576 // so, handle it.
577 if (StandardCompileOpts &&
578 StandardCompileOpts.getPosition() < PassList.getPosition(i)) {
Chris Lattner3dda08a2008-07-13 19:35:21 +0000579 AddStandardCompilePasses(Passes);
580 StandardCompileOpts = false;
Eric Christophera887ae42009-08-21 23:29:40 +0000581 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000582
Chris Lattner61db1a12009-10-22 00:46:41 +0000583 if (StandardLinkOpts &&
584 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000585 AddStandardLinkPasses(Passes);
586 StandardLinkOpts = false;
Eric Christophera887ae42009-08-21 23:29:40 +0000587 }
Daniel Dunbaradc82882009-07-17 18:09:39 +0000588
Chris Lattner61db1a12009-10-22 00:46:41 +0000589 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000590 AddOptimizationPasses(Passes, *FPasses, 1);
Chris Lattner61db1a12009-10-22 00:46:41 +0000591 OptLevelO1 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000592 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000593
Chris Lattner61db1a12009-10-22 00:46:41 +0000594 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000595 AddOptimizationPasses(Passes, *FPasses, 2);
Chris Lattner61db1a12009-10-22 00:46:41 +0000596 OptLevelO2 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000597 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000598
Chris Lattner61db1a12009-10-22 00:46:41 +0000599 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000600 AddOptimizationPasses(Passes, *FPasses, 3);
Chris Lattner61db1a12009-10-22 00:46:41 +0000601 OptLevelO3 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000602 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000603
Chris Lattner61db1a12009-10-22 00:46:41 +0000604 const PassInfo *PassInf = PassList[i];
605 Pass *P = 0;
606 if (PassInf->getNormalCtor())
607 P = PassInf->getNormalCtor()();
608 else
609 errs() << argv[0] << ": cannot create pass: "
610 << PassInf->getPassName() << "\n";
611 if (P) {
Benjamin Kramer3460f222010-02-18 12:57:05 +0000612 PassKind Kind = P->getPassKind();
Chris Lattner61db1a12009-10-22 00:46:41 +0000613 addPass(Passes, P);
614
615 if (AnalyzeOnly) {
Benjamin Kramer3460f222010-02-18 12:57:05 +0000616 switch (Kind) {
Chris Lattner476e9bd2010-01-22 06:03:06 +0000617 case PT_BasicBlock:
Dan Gohmand4c45432010-09-01 14:20:41 +0000618 Passes.add(new BasicBlockPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000619 break;
Tobias Grosser65513602010-10-20 01:54:44 +0000620 case PT_Region:
621 Passes.add(new RegionPassPrinter(PassInf, Out->os()));
622 break;
Chris Lattner476e9bd2010-01-22 06:03:06 +0000623 case PT_Loop:
Dan Gohmand4c45432010-09-01 14:20:41 +0000624 Passes.add(new LoopPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000625 break;
626 case PT_Function:
Dan Gohmand4c45432010-09-01 14:20:41 +0000627 Passes.add(new FunctionPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000628 break;
629 case PT_CallGraphSCC:
Dan Gohmand4c45432010-09-01 14:20:41 +0000630 Passes.add(new CallGraphSCCPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000631 break;
632 default:
Dan Gohmand4c45432010-09-01 14:20:41 +0000633 Passes.add(new ModulePassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000634 break;
635 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000636 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000637 }
638
Chris Lattner61db1a12009-10-22 00:46:41 +0000639 if (PrintEachXForm)
640 Passes.add(createPrintModulePass(&errs()));
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000641 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000642
643 // If -std-compile-opts was specified at the end of the pass list, add them.
644 if (StandardCompileOpts) {
645 AddStandardCompilePasses(Passes);
646 StandardCompileOpts = false;
647 }
648
649 if (StandardLinkOpts) {
650 AddStandardLinkPasses(Passes);
651 StandardLinkOpts = false;
652 }
653
654 if (OptLevelO1)
655 AddOptimizationPasses(Passes, *FPasses, 1);
656
657 if (OptLevelO2)
658 AddOptimizationPasses(Passes, *FPasses, 2);
659
660 if (OptLevelO3)
661 AddOptimizationPasses(Passes, *FPasses, 3);
662
Dan Gohman9cc1c182010-05-14 15:36:54 +0000663 if (OptLevelO1 || OptLevelO2 || OptLevelO3)
664 FPasses->run(*M.get());
Chris Lattner61db1a12009-10-22 00:46:41 +0000665
666 // Check that the module is well formed on completion of optimization
667 if (!NoVerify && !VerifyEach)
668 Passes.add(createVerifierPass());
669
Dan Gohman4931b312010-08-18 17:42:59 +0000670 // Write bitcode or assembly to the output as the last step...
Chris Lattner61db1a12009-10-22 00:46:41 +0000671 if (!NoOutput && !AnalyzeOnly) {
672 if (OutputAssembly)
Dan Gohmand4c45432010-09-01 14:20:41 +0000673 Passes.add(createPrintModulePass(&Out->os()));
Chris Lattner61db1a12009-10-22 00:46:41 +0000674 else
Dan Gohmand4c45432010-09-01 14:20:41 +0000675 Passes.add(createBitcodeWriterPass(Out->os()));
Chris Lattner61db1a12009-10-22 00:46:41 +0000676 }
677
678 // Now that we have all of the passes ready, run them.
679 Passes.run(*M.get());
680
Dan Gohmand5826a32010-08-20 01:07:01 +0000681 // Declare success.
Devang Patelf5f23002010-12-07 00:33:43 +0000682 if (!NoOutput || PrintBreakpoints)
Dan Gohmand5826a32010-08-20 01:07:01 +0000683 Out->keep();
684
Chris Lattner61db1a12009-10-22 00:46:41 +0000685 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000686}