blob: 319c2c865039829f05a43a91c9410a6f7f78010a [file] [log] [blame]
Chris Lattner27dd6422003-12-28 07:59:53 +00001//===-- Passes.cpp - Target independent code generation passes ------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Alkis Evlogimenos5facafa2003-10-02 16:57:49 +00009//
10// This file defines interfaces to access the target independent code
11// generation passes provided by the LLVM backend.
12//
13//===---------------------------------------------------------------------===//
14
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/CodeGen/Passes.h"
Andrew Trickde401d32012-02-04 02:56:48 +000016#include "llvm/Analysis/Passes.h"
Andrew Trickde401d32012-02-04 02:56:48 +000017#include "llvm/CodeGen/GCStrategy.h"
Andrew Trickde401d32012-02-04 02:56:48 +000018#include "llvm/CodeGen/MachineFunctionPass.h"
Andrew Trickde401d32012-02-04 02:56:48 +000019#include "llvm/CodeGen/RegAllocRegistry.h"
Chandler Carruthb8ddc702014-01-12 11:10:32 +000020#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000021#include "llvm/IR/Verifier.h"
Bob Wilsonbbd38dd2012-07-02 19:48:31 +000022#include "llvm/MC/MCAsmInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/PassManager.h"
Andrew Trickde401d32012-02-04 02:56:48 +000024#include "llvm/Support/CommandLine.h"
25#include "llvm/Support/Debug.h"
Andrew Trickb7551332012-02-04 02:56:45 +000026#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Target/TargetLowering.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000028#include "llvm/Target/TargetSubtargetInfo.h"
29#include "llvm/Transforms/Scalar.h"
Jim Laskey95eda5b2006-08-01 14:21:23 +000030
Chris Lattner27dd6422003-12-28 07:59:53 +000031using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000032
Andrew Trickde401d32012-02-04 02:56:48 +000033static cl::opt<bool> DisablePostRA("disable-post-ra", cl::Hidden,
34 cl::desc("Disable Post Regalloc"));
35static cl::opt<bool> DisableBranchFold("disable-branch-fold", cl::Hidden,
36 cl::desc("Disable branch folding"));
37static cl::opt<bool> DisableTailDuplicate("disable-tail-duplicate", cl::Hidden,
38 cl::desc("Disable tail duplication"));
39static cl::opt<bool> DisableEarlyTailDup("disable-early-taildup", cl::Hidden,
40 cl::desc("Disable pre-register allocation tail duplication"));
Chandler Carruth4190b502012-04-16 13:49:17 +000041static cl::opt<bool> DisableBlockPlacement("disable-block-placement",
Benjamin Kramer70671b92013-03-29 17:14:24 +000042 cl::Hidden, cl::desc("Disable probability-driven block placement"));
Andrew Trickde401d32012-02-04 02:56:48 +000043static cl::opt<bool> EnableBlockPlacementStats("enable-block-placement-stats",
44 cl::Hidden, cl::desc("Collect probability-driven block placement stats"));
Andrew Trickde401d32012-02-04 02:56:48 +000045static cl::opt<bool> DisableSSC("disable-ssc", cl::Hidden,
46 cl::desc("Disable Stack Slot Coloring"));
47static cl::opt<bool> DisableMachineDCE("disable-machine-dce", cl::Hidden,
48 cl::desc("Disable Machine Dead Code Elimination"));
Jakob Stoklund Olesen0f6e8bb2012-10-03 00:51:32 +000049static cl::opt<bool> DisableEarlyIfConversion("disable-early-ifcvt", cl::Hidden,
50 cl::desc("Disable Early If-conversion"));
Andrew Trickde401d32012-02-04 02:56:48 +000051static cl::opt<bool> DisableMachineLICM("disable-machine-licm", cl::Hidden,
52 cl::desc("Disable Machine LICM"));
53static cl::opt<bool> DisableMachineCSE("disable-machine-cse", cl::Hidden,
54 cl::desc("Disable Machine Common Subexpression Elimination"));
Andrew Trickd3f8fe82012-02-10 04:10:36 +000055static cl::opt<cl::boolOrDefault>
56OptimizeRegAlloc("optimize-regalloc", cl::Hidden,
57 cl::desc("Enable optimized register allocation compilation path."));
Andrew Trickee874db2012-02-11 07:11:32 +000058static cl::opt<cl::boolOrDefault>
Andrew Trick7daf6a42014-01-13 20:08:27 +000059EnableMachineSched("enable-misched",
Andrew Trickd3f8fe82012-02-10 04:10:36 +000060 cl::desc("Enable the machine instruction scheduling pass."));
Andrew Trickde401d32012-02-04 02:56:48 +000061static cl::opt<bool> DisablePostRAMachineLICM("disable-postra-machine-licm",
62 cl::Hidden,
63 cl::desc("Disable Machine LICM"));
64static cl::opt<bool> DisableMachineSink("disable-machine-sink", cl::Hidden,
65 cl::desc("Disable Machine Sinking"));
66static cl::opt<bool> DisableLSR("disable-lsr", cl::Hidden,
67 cl::desc("Disable Loop Strength Reduction Pass"));
Juergen Ributzkaf26beda2014-01-25 02:02:55 +000068static cl::opt<bool> DisableConstantHoisting("disable-constant-hoisting",
69 cl::Hidden, cl::desc("Disable ConstantHoisting"));
Andrew Trickde401d32012-02-04 02:56:48 +000070static cl::opt<bool> DisableCGP("disable-cgp", cl::Hidden,
71 cl::desc("Disable Codegen Prepare"));
72static cl::opt<bool> DisableCopyProp("disable-copyprop", cl::Hidden,
Evan Cheng63618f92012-02-20 23:28:17 +000073 cl::desc("Disable Copy Propagation pass"));
James Molloybc9fed82014-07-23 13:33:00 +000074static cl::opt<bool> DisablePartialLibcallInlining("disable-partial-libcall-inlining",
75 cl::Hidden, cl::desc("Disable Partial Libcall Inlining"));
Andrew Trickde401d32012-02-04 02:56:48 +000076static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
77 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
78static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
79 cl::desc("Print LLVM IR input to isel pass"));
80static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
81 cl::desc("Dump garbage collector data"));
82static cl::opt<bool> VerifyMachineCode("verify-machineinstrs", cl::Hidden,
83 cl::desc("Verify generated machine code"),
Craig Topperc0196b12014-04-14 00:51:57 +000084 cl::init(getenv("LLVM_VERIFY_MACHINEINSTRS")!=nullptr));
Bob Wilson33e51882012-05-30 00:17:12 +000085static cl::opt<std::string>
86PrintMachineInstrs("print-machineinstrs", cl::ValueOptional,
87 cl::desc("Print machine instrs"),
88 cl::value_desc("pass-name"), cl::init("option-unspecified"));
Andrew Trickde401d32012-02-04 02:56:48 +000089
Andrew Trick17080b92013-12-28 21:56:51 +000090// Temporary option to allow experimenting with MachineScheduler as a post-RA
91// scheduler. Targets can "properly" enable this with
Andrew Trick8d2ee372014-06-04 07:06:27 +000092// substitutePass(&PostRASchedulerID, &PostMachineSchedulerID); Ideally it
93// wouldn't be part of the standard pass pipeline, and the target would just add
94// a PostRA scheduling pass wherever it wants.
Andrew Trick17080b92013-12-28 21:56:51 +000095static cl::opt<bool> MISchedPostRA("misched-postra", cl::Hidden,
96 cl::desc("Run MachineScheduler post regalloc (independent of preRA sched)"));
97
Cameron Zwarich71f0acb2013-02-10 06:42:34 +000098// Experimental option to run live interval analysis early.
Jakob Stoklund Olesen1c465892012-08-03 22:12:54 +000099static cl::opt<bool> EarlyLiveIntervals("early-live-intervals", cl::Hidden,
100 cl::desc("Run live interval analysis earlier in the pipeline"));
101
Andrew Tricke9a951c2012-02-15 03:21:51 +0000102/// Allow standard passes to be disabled by command line options. This supports
103/// simple binary flags that either suppress the pass or do nothing.
104/// i.e. -disable-mypass=false has no effect.
105/// These should be converted to boolOrDefault in order to use applyOverride.
Andrew Tricke2203232013-04-10 01:06:56 +0000106static IdentifyingPassPtr applyDisable(IdentifyingPassPtr PassID,
107 bool Override) {
Andrew Tricke9a951c2012-02-15 03:21:51 +0000108 if (Override)
Andrew Tricke2203232013-04-10 01:06:56 +0000109 return IdentifyingPassPtr();
Bob Wilsonb9b69362012-07-02 19:48:37 +0000110 return PassID;
Andrew Tricke9a951c2012-02-15 03:21:51 +0000111}
112
113/// Allow Pass selection to be overriden by command line options. This supports
114/// flags with ternary conditions. TargetID is passed through by default. The
115/// pass is suppressed when the option is false. When the option is true, the
116/// StandardID is selected if the target provides no default.
Andrew Tricke2203232013-04-10 01:06:56 +0000117static IdentifyingPassPtr applyOverride(IdentifyingPassPtr TargetID,
118 cl::boolOrDefault Override,
119 AnalysisID StandardID) {
Andrew Trickee874db2012-02-11 07:11:32 +0000120 switch (Override) {
121 case cl::BOU_UNSET:
Andrew Tricke9a951c2012-02-15 03:21:51 +0000122 return TargetID;
Andrew Trickee874db2012-02-11 07:11:32 +0000123 case cl::BOU_TRUE:
Andrew Tricke2203232013-04-10 01:06:56 +0000124 if (TargetID.isValid())
Andrew Tricke9a951c2012-02-15 03:21:51 +0000125 return TargetID;
Craig Topperc0196b12014-04-14 00:51:57 +0000126 if (StandardID == nullptr)
Andrew Trickee874db2012-02-11 07:11:32 +0000127 report_fatal_error("Target cannot enable pass");
Andrew Tricke9a951c2012-02-15 03:21:51 +0000128 return StandardID;
Andrew Trickee874db2012-02-11 07:11:32 +0000129 case cl::BOU_FALSE:
Andrew Tricke2203232013-04-10 01:06:56 +0000130 return IdentifyingPassPtr();
Andrew Trickee874db2012-02-11 07:11:32 +0000131 }
132 llvm_unreachable("Invalid command line option state");
133}
134
Andrew Tricke9a951c2012-02-15 03:21:51 +0000135/// Allow standard passes to be disabled by the command line, regardless of who
136/// is adding the pass.
137///
138/// StandardID is the pass identified in the standard pass pipeline and provided
139/// to addPass(). It may be a target-specific ID in the case that the target
140/// directly adds its own pass, but in that case we harmlessly fall through.
141///
142/// TargetID is the pass that the target has configured to override StandardID.
143///
144/// StandardID may be a pseudo ID. In that case TargetID is the name of the real
145/// pass to run. This allows multiple options to control a single pass depending
146/// on where in the pipeline that pass is added.
Andrew Tricke2203232013-04-10 01:06:56 +0000147static IdentifyingPassPtr overridePass(AnalysisID StandardID,
148 IdentifyingPassPtr TargetID) {
Andrew Tricke9a951c2012-02-15 03:21:51 +0000149 if (StandardID == &PostRASchedulerID)
150 return applyDisable(TargetID, DisablePostRA);
151
152 if (StandardID == &BranchFolderPassID)
153 return applyDisable(TargetID, DisableBranchFold);
154
155 if (StandardID == &TailDuplicateID)
156 return applyDisable(TargetID, DisableTailDuplicate);
157
158 if (StandardID == &TargetPassConfig::EarlyTailDuplicateID)
159 return applyDisable(TargetID, DisableEarlyTailDup);
160
161 if (StandardID == &MachineBlockPlacementID)
Benjamin Kramer70671b92013-03-29 17:14:24 +0000162 return applyDisable(TargetID, DisableBlockPlacement);
Andrew Tricke9a951c2012-02-15 03:21:51 +0000163
164 if (StandardID == &StackSlotColoringID)
165 return applyDisable(TargetID, DisableSSC);
166
167 if (StandardID == &DeadMachineInstructionElimID)
168 return applyDisable(TargetID, DisableMachineDCE);
169
Jakob Stoklund Olesenf8a63a12012-07-04 00:09:54 +0000170 if (StandardID == &EarlyIfConverterID)
Jakob Stoklund Olesen0f6e8bb2012-10-03 00:51:32 +0000171 return applyDisable(TargetID, DisableEarlyIfConversion);
Jakob Stoklund Olesenf8a63a12012-07-04 00:09:54 +0000172
Andrew Tricke9a951c2012-02-15 03:21:51 +0000173 if (StandardID == &MachineLICMID)
174 return applyDisable(TargetID, DisableMachineLICM);
175
176 if (StandardID == &MachineCSEID)
177 return applyDisable(TargetID, DisableMachineCSE);
178
179 if (StandardID == &MachineSchedulerID)
180 return applyOverride(TargetID, EnableMachineSched, StandardID);
181
182 if (StandardID == &TargetPassConfig::PostRAMachineLICMID)
183 return applyDisable(TargetID, DisablePostRAMachineLICM);
184
185 if (StandardID == &MachineSinkingID)
186 return applyDisable(TargetID, DisableMachineSink);
187
188 if (StandardID == &MachineCopyPropagationID)
189 return applyDisable(TargetID, DisableCopyProp);
190
191 return TargetID;
192}
193
Jim Laskey29e635d2006-08-02 12:30:23 +0000194//===---------------------------------------------------------------------===//
Andrew Trickb7551332012-02-04 02:56:45 +0000195/// TargetPassConfig
196//===---------------------------------------------------------------------===//
197
198INITIALIZE_PASS(TargetPassConfig, "targetpassconfig",
199 "Target Pass Configuration", false, false)
200char TargetPassConfig::ID = 0;
201
Andrew Tricke9a951c2012-02-15 03:21:51 +0000202// Pseudo Pass IDs.
203char TargetPassConfig::EarlyTailDuplicateID = 0;
204char TargetPassConfig::PostRAMachineLICMID = 0;
205
Andrew Trickc9ce9d22012-02-15 03:21:47 +0000206namespace llvm {
207class PassConfigImpl {
208public:
209 // List of passes explicitly substituted by this target. Normally this is
210 // empty, but it is a convenient way to suppress or replace specific passes
211 // that are part of a standard pass pipeline without overridding the entire
212 // pipeline. This mechanism allows target options to inherit a standard pass's
213 // user interface. For example, a target may disable a standard pass by
Bob Wilsonb9b69362012-07-02 19:48:37 +0000214 // default by substituting a pass ID of zero, and the user may still enable
215 // that standard pass with an explicit command line option.
Andrew Tricke2203232013-04-10 01:06:56 +0000216 DenseMap<AnalysisID,IdentifyingPassPtr> TargetPasses;
Bob Wilson33e51882012-05-30 00:17:12 +0000217
218 /// Store the pairs of <AnalysisID, AnalysisID> of which the second pass
219 /// is inserted after each instance of the first one.
Andrew Tricke2203232013-04-10 01:06:56 +0000220 SmallVector<std::pair<AnalysisID, IdentifyingPassPtr>, 4> InsertedPasses;
Andrew Trickc9ce9d22012-02-15 03:21:47 +0000221};
222} // namespace llvm
223
Andrew Trickb7551332012-02-04 02:56:45 +0000224// Out of line virtual method.
Andrew Trickc9ce9d22012-02-15 03:21:47 +0000225TargetPassConfig::~TargetPassConfig() {
226 delete Impl;
227}
Andrew Trickb7551332012-02-04 02:56:45 +0000228
Andrew Trick58648e42012-02-08 21:22:48 +0000229// Out of line constructor provides default values for pass options and
230// registers all common codegen passes.
Andrew Trickf8ea1082012-02-04 02:56:59 +0000231TargetPassConfig::TargetPassConfig(TargetMachine *tm, PassManagerBase &pm)
Craig Topperc0196b12014-04-14 00:51:57 +0000232 : ImmutablePass(ID), PM(&pm), StartAfter(nullptr), StopAfter(nullptr),
233 Started(true), Stopped(false), TM(tm), Impl(nullptr), Initialized(false),
Andrew Trickdd37d522012-02-08 21:22:39 +0000234 DisableVerify(false),
235 EnableTailMerge(true) {
236
Andrew Trickc9ce9d22012-02-15 03:21:47 +0000237 Impl = new PassConfigImpl();
238
Andrew Trickb7551332012-02-04 02:56:45 +0000239 // Register all target independent codegen passes to activate their PassIDs,
240 // including this pass itself.
241 initializeCodeGen(*PassRegistry::getPassRegistry());
Andrew Tricke9a951c2012-02-15 03:21:51 +0000242
243 // Substitute Pseudo Pass IDs for real ones.
Bob Wilsonb9b69362012-07-02 19:48:37 +0000244 substitutePass(&EarlyTailDuplicateID, &TailDuplicateID);
245 substitutePass(&PostRAMachineLICMID, &MachineLICMID);
Andrew Tricke9a951c2012-02-15 03:21:51 +0000246
247 // Temporarily disable experimental passes.
Andrew Trick108c88c2012-11-13 08:47:29 +0000248 const TargetSubtargetInfo &ST = TM->getSubtarget<TargetSubtargetInfo>();
Andrew Trick71e8bb62013-09-26 05:53:35 +0000249 if (!ST.useMachineScheduler())
Andrew Trick108c88c2012-11-13 08:47:29 +0000250 disablePass(&MachineSchedulerID);
Andrew Trickb7551332012-02-04 02:56:45 +0000251}
252
Bob Wilson33e51882012-05-30 00:17:12 +0000253/// Insert InsertedPassID pass after TargetPassID.
Bob Wilsonb9b69362012-07-02 19:48:37 +0000254void TargetPassConfig::insertPass(AnalysisID TargetPassID,
Andrew Tricke2203232013-04-10 01:06:56 +0000255 IdentifyingPassPtr InsertedPassID) {
Benjamin Kramere7c45bc2013-04-11 11:57:01 +0000256 assert(((!InsertedPassID.isInstance() &&
257 TargetPassID != InsertedPassID.getID()) ||
258 (InsertedPassID.isInstance() &&
259 TargetPassID != InsertedPassID.getInstance()->getPassID())) &&
Andrew Tricke2203232013-04-10 01:06:56 +0000260 "Insert a pass after itself!");
261 std::pair<AnalysisID, IdentifyingPassPtr> P(TargetPassID, InsertedPassID);
Bob Wilson33e51882012-05-30 00:17:12 +0000262 Impl->InsertedPasses.push_back(P);
263}
264
Andrew Trickb7551332012-02-04 02:56:45 +0000265/// createPassConfig - Create a pass configuration object to be used by
266/// addPassToEmitX methods for generating a pipeline of CodeGen passes.
267///
268/// Targets may override this to extend TargetPassConfig.
Andrew Trickf8ea1082012-02-04 02:56:59 +0000269TargetPassConfig *LLVMTargetMachine::createPassConfig(PassManagerBase &PM) {
270 return new TargetPassConfig(this, PM);
Andrew Trickb7551332012-02-04 02:56:45 +0000271}
272
273TargetPassConfig::TargetPassConfig()
Craig Topperc0196b12014-04-14 00:51:57 +0000274 : ImmutablePass(ID), PM(nullptr) {
Andrew Trickb7551332012-02-04 02:56:45 +0000275 llvm_unreachable("TargetPassConfig should not be constructed on-the-fly");
276}
277
Andrew Trickdd37d522012-02-08 21:22:39 +0000278// Helper to verify the analysis is really immutable.
279void TargetPassConfig::setOpt(bool &Opt, bool Val) {
280 assert(!Initialized && "PassConfig is immutable");
281 Opt = Val;
282}
283
Bob Wilsonb9b69362012-07-02 19:48:37 +0000284void TargetPassConfig::substitutePass(AnalysisID StandardID,
Andrew Tricke2203232013-04-10 01:06:56 +0000285 IdentifyingPassPtr TargetID) {
Bob Wilsonb9b69362012-07-02 19:48:37 +0000286 Impl->TargetPasses[StandardID] = TargetID;
Andrew Trickc9ce9d22012-02-15 03:21:47 +0000287}
Andrew Trickee874db2012-02-11 07:11:32 +0000288
Andrew Tricke2203232013-04-10 01:06:56 +0000289IdentifyingPassPtr TargetPassConfig::getPassSubstitution(AnalysisID ID) const {
290 DenseMap<AnalysisID, IdentifyingPassPtr>::const_iterator
Andrew Trickc9ce9d22012-02-15 03:21:47 +0000291 I = Impl->TargetPasses.find(ID);
292 if (I == Impl->TargetPasses.end())
293 return ID;
294 return I->second;
295}
296
Bob Wilsoncac3b902012-07-02 19:48:45 +0000297/// Add a pass to the PassManager if that pass is supposed to be run. If the
298/// Started/Stopped flags indicate either that the compilation should start at
299/// a later pass or that it should stop after an earlier pass, then do not add
300/// the pass. Finally, compare the current pass against the StartAfter
301/// and StopAfter options and change the Started/Stopped flags accordingly.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000302void TargetPassConfig::addPass(Pass *P) {
Bob Wilsona3f9fa72012-07-02 19:48:39 +0000303 assert(!Initialized && "PassConfig is immutable");
304
Chandler Carruth34263a02012-07-02 22:56:41 +0000305 // Cache the Pass ID here in case the pass manager finds this pass is
306 // redundant with ones already scheduled / available, and deletes it.
307 // Fundamentally, once we add the pass to the manager, we no longer own it
308 // and shouldn't reference it.
309 AnalysisID PassID = P->getPassID();
310
Bob Wilsoncac3b902012-07-02 19:48:45 +0000311 if (Started && !Stopped)
312 PM->add(P);
Benjamin Kramer483b9fb2013-08-05 11:11:11 +0000313 else
314 delete P;
Chandler Carruth34263a02012-07-02 22:56:41 +0000315 if (StopAfter == PassID)
Bob Wilsoncac3b902012-07-02 19:48:45 +0000316 Stopped = true;
Chandler Carruth34263a02012-07-02 22:56:41 +0000317 if (StartAfter == PassID)
Bob Wilsoncac3b902012-07-02 19:48:45 +0000318 Started = true;
319 if (Stopped && !Started)
320 report_fatal_error("Cannot stop compilation after pass that is not run");
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000321}
322
Andrew Trickc9ce9d22012-02-15 03:21:47 +0000323/// Add a CodeGen pass at this point in the pipeline after checking for target
324/// and command line overrides.
Andrew Tricke2203232013-04-10 01:06:56 +0000325///
326/// addPass cannot return a pointer to the pass instance because is internal the
327/// PassManager and the instance we create here may already be freed.
Bob Wilsonb9b69362012-07-02 19:48:37 +0000328AnalysisID TargetPassConfig::addPass(AnalysisID PassID) {
Andrew Tricke2203232013-04-10 01:06:56 +0000329 IdentifyingPassPtr TargetID = getPassSubstitution(PassID);
330 IdentifyingPassPtr FinalPtr = overridePass(PassID, TargetID);
331 if (!FinalPtr.isValid())
Craig Topperc0196b12014-04-14 00:51:57 +0000332 return nullptr;
Andrew Trickc9ce9d22012-02-15 03:21:47 +0000333
Andrew Tricke2203232013-04-10 01:06:56 +0000334 Pass *P;
335 if (FinalPtr.isInstance())
336 P = FinalPtr.getInstance();
337 else {
338 P = Pass::createPass(FinalPtr.getID());
339 if (!P)
340 llvm_unreachable("Pass ID not registered");
341 }
342 AnalysisID FinalID = P->getPassID();
343 addPass(P); // Ends the lifetime of P.
344
Bob Wilson33e51882012-05-30 00:17:12 +0000345 // Add the passes after the pass P if there is any.
Craig Toppere1c1d362013-07-03 05:11:49 +0000346 for (SmallVectorImpl<std::pair<AnalysisID, IdentifyingPassPtr> >::iterator
Bob Wilson33e51882012-05-30 00:17:12 +0000347 I = Impl->InsertedPasses.begin(), E = Impl->InsertedPasses.end();
348 I != E; ++I) {
Bob Wilsonb9b69362012-07-02 19:48:37 +0000349 if ((*I).first == PassID) {
Andrew Tricke2203232013-04-10 01:06:56 +0000350 assert((*I).second.isValid() && "Illegal Pass ID!");
351 Pass *NP;
352 if ((*I).second.isInstance())
353 NP = (*I).second.getInstance();
354 else {
355 NP = Pass::createPass((*I).second.getID());
356 assert(NP && "Pass ID not registered");
357 }
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000358 addPass(NP);
Bob Wilson33e51882012-05-30 00:17:12 +0000359 }
360 }
Andrew Trickc9ce9d22012-02-15 03:21:47 +0000361 return FinalID;
Andrew Trickf8ea1082012-02-04 02:56:59 +0000362}
Andrew Trickde401d32012-02-04 02:56:48 +0000363
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000364void TargetPassConfig::printAndVerify(const char *Banner) {
Andrew Trickde401d32012-02-04 02:56:48 +0000365 if (TM->shouldPrintMachineCode())
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000366 addPass(createMachineFunctionPrinterPass(dbgs(), Banner));
Andrew Trickde401d32012-02-04 02:56:48 +0000367
368 if (VerifyMachineCode)
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000369 addPass(createMachineVerifierPass(Banner));
Andrew Trickde401d32012-02-04 02:56:48 +0000370}
371
Andrew Trickf8ea1082012-02-04 02:56:59 +0000372/// Add common target configurable passes that perform LLVM IR to IR transforms
373/// following machine independent optimization.
374void TargetPassConfig::addIRPasses() {
Andrew Trickde401d32012-02-04 02:56:48 +0000375 // Basic AliasAnalysis support.
376 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
377 // BasicAliasAnalysis wins if they disagree. This is intended to help
378 // support "obvious" type-punning idioms.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000379 addPass(createTypeBasedAliasAnalysisPass());
Hal Finkel94146652014-07-24 14:25:39 +0000380 addPass(createScopedNoAliasAAPass());
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000381 addPass(createBasicAliasAnalysisPass());
Andrew Trickde401d32012-02-04 02:56:48 +0000382
383 // Before running any passes, run the verifier to determine if the input
384 // coming from the front-end and/or optimizer is valid.
Duncan P. N. Exon Smith6ef5f282014-04-15 16:27:38 +0000385 if (!DisableVerify) {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000386 addPass(createVerifierPass());
Duncan P. N. Exon Smith6ef5f282014-04-15 16:27:38 +0000387 addPass(createDebugInfoVerifierPass());
388 }
Andrew Trickde401d32012-02-04 02:56:48 +0000389
390 // Run loop strength reduction before anything else.
391 if (getOptLevel() != CodeGenOpt::None && !DisableLSR) {
Chandler Carruth26c59fa2013-01-07 14:41:08 +0000392 addPass(createLoopStrengthReducePass());
Andrew Trickde401d32012-02-04 02:56:48 +0000393 if (PrintLSR)
Chandler Carruth9d805132014-01-12 11:30:46 +0000394 addPass(createPrintFunctionPass(dbgs(), "\n\n*** Code after LSR ***\n"));
Andrew Trickde401d32012-02-04 02:56:48 +0000395 }
396
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000397 addPass(createGCLoweringPass());
Andrew Trickde401d32012-02-04 02:56:48 +0000398
399 // Make sure that no unreachable blocks are instruction selected.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000400 addPass(createUnreachableBlockEliminationPass());
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000401
402 // Prepare expensive constants for SelectionDAG.
403 if (getOptLevel() != CodeGenOpt::None && !DisableConstantHoisting)
404 addPass(createConstantHoistingPass());
James Molloybc9fed82014-07-23 13:33:00 +0000405
406 if (getOptLevel() != CodeGenOpt::None && !DisablePartialLibcallInlining)
407 addPass(createPartiallyInlineLibCallsPass());
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000408}
409
410/// Turn exception handling constructs into something the code generators can
411/// handle.
412void TargetPassConfig::addPassesToHandleExceptions() {
413 switch (TM->getMCAsmInfo()->getExceptionHandlingType()) {
414 case ExceptionHandling::SjLj:
415 // SjLj piggy-backs on dwarf for this bit. The cleanups done apply to both
416 // Dwarf EH prepare needs to be run after SjLj prepare. Otherwise,
417 // catch info can get misplaced when a selector ends up more than one block
418 // removed from the parent invoke(s). This could happen when a landing
419 // pad is shared by multiple invokes and is also a target of a normal
420 // edge from elsewhere.
Bill Wendlingafc10362013-06-19 20:51:24 +0000421 addPass(createSjLjEHPreparePass(TM));
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000422 // FALLTHROUGH
423 case ExceptionHandling::DwarfCFI:
424 case ExceptionHandling::ARM:
Saleem Abdulrasool67b54812014-06-29 21:43:47 +0000425 case ExceptionHandling::WinEH:
Bill Wendlingafc10362013-06-19 20:51:24 +0000426 addPass(createDwarfEHPass(TM));
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000427 break;
428 case ExceptionHandling::None:
Mark Seabornb6118c52014-03-20 19:54:47 +0000429 addPass(createLowerInvokePass());
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000430
431 // The lower invoke pass may create unreachable code. Remove it.
432 addPass(createUnreachableBlockEliminationPass());
433 break;
434 }
Andrew Trickf8ea1082012-02-04 02:56:59 +0000435}
Andrew Trickde401d32012-02-04 02:56:48 +0000436
Bill Wendlingc786b312012-11-30 22:08:55 +0000437/// Add pass to prepare the LLVM IR for code generation. This should be done
438/// before exception handling preparation passes.
439void TargetPassConfig::addCodeGenPrepare() {
440 if (getOptLevel() != CodeGenOpt::None && !DisableCGP)
Bill Wendling7a639ea2013-06-19 21:07:11 +0000441 addPass(createCodeGenPreparePass(TM));
Bill Wendlingc786b312012-11-30 22:08:55 +0000442}
443
Andrew Trickf8ea1082012-02-04 02:56:59 +0000444/// Add common passes that perform LLVM IR to IR transforms in preparation for
445/// instruction selection.
446void TargetPassConfig::addISelPrepare() {
Andrew Trickde401d32012-02-04 02:56:48 +0000447 addPreISel();
448
Duncan P. N. Exon Smith6ef5f282014-04-15 16:27:38 +0000449 // Need to verify DebugInfo *before* creating the stack protector analysis.
450 // It's a function pass, and verifying between it and its users causes a
451 // crash.
452 if (!DisableVerify)
453 addPass(createDebugInfoVerifierPass());
454
Josh Magee22b8ba22013-12-19 03:17:11 +0000455 addPass(createStackProtectorPass(TM));
456
Andrew Trickde401d32012-02-04 02:56:48 +0000457 if (PrintISelInput)
Chandler Carruth9d805132014-01-12 11:30:46 +0000458 addPass(createPrintFunctionPass(
459 dbgs(), "\n\n*** Final LLVM Code input to ISel ***\n"));
Andrew Trickde401d32012-02-04 02:56:48 +0000460
461 // All passes which modify the LLVM IR are now complete; run the verifier
462 // to ensure that the IR is valid.
463 if (!DisableVerify)
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000464 addPass(createVerifierPass());
Andrew Trickf8ea1082012-02-04 02:56:59 +0000465}
Andrew Trickde401d32012-02-04 02:56:48 +0000466
Andrew Trickf5426752012-02-09 00:40:55 +0000467/// Add the complete set of target-independent postISel code generator passes.
468///
469/// This can be read as the standard order of major LLVM CodeGen stages. Stages
470/// with nontrivial configuration or multiple passes are broken out below in
471/// add%Stage routines.
472///
473/// Any TargetPassConfig::addXX routine may be overriden by the Target. The
474/// addPre/Post methods with empty header implementations allow injecting
475/// target-specific fixups just before or after major stages. Additionally,
476/// targets have the flexibility to change pass order within a stage by
477/// overriding default implementation of add%Stage routines below. Each
478/// technique has maintainability tradeoffs because alternate pass orders are
479/// not well supported. addPre/Post works better if the target pass is easily
480/// tied to a common pass. But if it has subtle dependencies on multiple passes,
Andrew Trick09fc1bb2012-02-10 07:08:25 +0000481/// the target should override the stage instead.
Andrew Trickf5426752012-02-09 00:40:55 +0000482///
483/// TODO: We could use a single addPre/Post(ID) hook to allow pass injection
484/// before/after any target-independent pass. But it's currently overkill.
Andrew Trickf8ea1082012-02-04 02:56:59 +0000485void TargetPassConfig::addMachinePasses() {
Bob Wilson33e51882012-05-30 00:17:12 +0000486 // Insert a machine instr printer pass after the specified pass.
487 // If -print-machineinstrs specified, print machineinstrs after all passes.
488 if (StringRef(PrintMachineInstrs.getValue()).equals(""))
489 TM->Options.PrintMachineCode = true;
490 else if (!StringRef(PrintMachineInstrs.getValue())
491 .equals("option-unspecified")) {
492 const PassRegistry *PR = PassRegistry::getPassRegistry();
493 const PassInfo *TPI = PR->getPassInfo(PrintMachineInstrs.getValue());
494 const PassInfo *IPI = PR->getPassInfo(StringRef("print-machineinstrs"));
495 assert (TPI && IPI && "Pass ID not registered!");
Roman Divackyad06cee2012-09-05 22:26:57 +0000496 const char *TID = (const char *)(TPI->getTypeInfo());
497 const char *IID = (const char *)(IPI->getTypeInfo());
Bob Wilsonb9b69362012-07-02 19:48:37 +0000498 insertPass(TID, IID);
Bob Wilson33e51882012-05-30 00:17:12 +0000499 }
500
Jakob Stoklund Olesen29506f52012-07-04 19:28:27 +0000501 // Print the instruction selected machine code...
502 printAndVerify("After Instruction Selection");
503
Andrew Trickde401d32012-02-04 02:56:48 +0000504 // Expand pseudo-instructions emitted by ISel.
Jakob Stoklund Olesen1d026262012-08-20 20:52:08 +0000505 if (addPass(&ExpandISelPseudosID))
506 printAndVerify("After ExpandISelPseudos");
Andrew Trickde401d32012-02-04 02:56:48 +0000507
Andrew Trickf5426752012-02-09 00:40:55 +0000508 // Add passes that optimize machine instructions in SSA form.
Andrew Trickde401d32012-02-04 02:56:48 +0000509 if (getOptLevel() != CodeGenOpt::None) {
Andrew Trickf5426752012-02-09 00:40:55 +0000510 addMachineSSAOptimization();
Craig Topper36f29122012-11-19 00:11:50 +0000511 } else {
Andrew Trickf5426752012-02-09 00:40:55 +0000512 // If the target requests it, assign local variables to stack slots relative
513 // to one another and simplify frame index references where possible.
Bob Wilsonb9b69362012-07-02 19:48:37 +0000514 addPass(&LocalStackSlotAllocationID);
Andrew Trickde401d32012-02-04 02:56:48 +0000515 }
516
517 // Run pre-ra passes.
518 if (addPreRegAlloc())
519 printAndVerify("After PreRegAlloc passes");
520
Andrew Trickf5426752012-02-09 00:40:55 +0000521 // Run register allocation and passes that are tightly coupled with it,
522 // including phi elimination and scheduling.
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000523 if (getOptimizeRegAlloc())
524 addOptimizedRegAlloc(createRegAllocPass(true));
525 else
526 addFastRegAlloc(createRegAllocPass(false));
Andrew Trickde401d32012-02-04 02:56:48 +0000527
528 // Run post-ra passes.
529 if (addPostRegAlloc())
530 printAndVerify("After PostRegAlloc passes");
531
532 // Insert prolog/epilog code. Eliminate abstract frame index references...
Bob Wilsonb9b69362012-07-02 19:48:37 +0000533 addPass(&PrologEpilogCodeInserterID);
Andrew Trickde401d32012-02-04 02:56:48 +0000534 printAndVerify("After PrologEpilogCodeInserter");
535
Andrew Trickf5426752012-02-09 00:40:55 +0000536 /// Add passes that optimize machine instructions after register allocation.
537 if (getOptLevel() != CodeGenOpt::None)
538 addMachineLateOptimization();
Andrew Trickde401d32012-02-04 02:56:48 +0000539
540 // Expand pseudo instructions before second scheduling pass.
Bob Wilsonb9b69362012-07-02 19:48:37 +0000541 addPass(&ExpandPostRAPseudosID);
Jakob Stoklund Olesene433c682012-03-28 20:49:30 +0000542 printAndVerify("After ExpandPostRAPseudos");
Andrew Trickde401d32012-02-04 02:56:48 +0000543
544 // Run pre-sched2 passes.
545 if (addPreSched2())
Jakob Stoklund Olesend1bd8fb2012-03-28 23:31:15 +0000546 printAndVerify("After PreSched2 passes");
Andrew Trickde401d32012-02-04 02:56:48 +0000547
548 // Second pass scheduler.
Andrew Tricke9a951c2012-02-15 03:21:51 +0000549 if (getOptLevel() != CodeGenOpt::None) {
Andrew Trick17080b92013-12-28 21:56:51 +0000550 if (MISchedPostRA)
551 addPass(&PostMachineSchedulerID);
552 else
553 addPass(&PostRASchedulerID);
Jakob Stoklund Olesenc3e80cc2012-03-28 23:54:28 +0000554 printAndVerify("After PostRAScheduler");
Andrew Trickde401d32012-02-04 02:56:48 +0000555 }
556
Andrew Trickf5426752012-02-09 00:40:55 +0000557 // GC
Evan Cheng59421ae2012-12-21 02:57:04 +0000558 if (addGCPasses()) {
559 if (PrintGCInfo)
560 addPass(createGCInfoPrinter(dbgs()));
561 }
Andrew Trickde401d32012-02-04 02:56:48 +0000562
Andrew Trickf5426752012-02-09 00:40:55 +0000563 // Basic block placement.
Andrew Tricke9a951c2012-02-15 03:21:51 +0000564 if (getOptLevel() != CodeGenOpt::None)
Andrew Trickf5426752012-02-09 00:40:55 +0000565 addBlockPlacement();
Andrew Trickde401d32012-02-04 02:56:48 +0000566
567 if (addPreEmitPass())
Jakob Stoklund Olesenc3e80cc2012-03-28 23:54:28 +0000568 printAndVerify("After PreEmit passes");
Juergen Ributzkae8294752013-12-14 06:53:06 +0000569
Juergen Ributzka009bff22014-06-26 23:39:52 +0000570 addPass(&StackMapLivenessID);
Andrew Trickde401d32012-02-04 02:56:48 +0000571}
572
Andrew Trickf5426752012-02-09 00:40:55 +0000573/// Add passes that optimize machine instructions in SSA form.
574void TargetPassConfig::addMachineSSAOptimization() {
575 // Pre-ra tail duplication.
Bob Wilsonb9b69362012-07-02 19:48:37 +0000576 if (addPass(&EarlyTailDuplicateID))
Andrew Trickf5426752012-02-09 00:40:55 +0000577 printAndVerify("After Pre-RegAlloc TailDuplicate");
Andrew Trickf5426752012-02-09 00:40:55 +0000578
579 // Optimize PHIs before DCE: removing dead PHI cycles may make more
580 // instructions dead.
Bob Wilsonb9b69362012-07-02 19:48:37 +0000581 addPass(&OptimizePHIsID);
Andrew Trickf5426752012-02-09 00:40:55 +0000582
Nadav Rotem7c277da2012-09-06 09:17:37 +0000583 // This pass merges large allocas. StackSlotColoring is a different pass
584 // which merges spill slots.
585 addPass(&StackColoringID);
586
Andrew Trickf5426752012-02-09 00:40:55 +0000587 // If the target requests it, assign local variables to stack slots relative
588 // to one another and simplify frame index references where possible.
Bob Wilsonb9b69362012-07-02 19:48:37 +0000589 addPass(&LocalStackSlotAllocationID);
Andrew Trickf5426752012-02-09 00:40:55 +0000590
591 // With optimization, dead code should already be eliminated. However
592 // there is one known exception: lowered code for arguments that are only
593 // used by tail calls, where the tail calls reuse the incoming stack
594 // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll).
Bob Wilsonb9b69362012-07-02 19:48:37 +0000595 addPass(&DeadMachineInstructionElimID);
Andrew Trickf5426752012-02-09 00:40:55 +0000596 printAndVerify("After codegen DCE pass");
597
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000598 // Allow targets to insert passes that improve instruction level parallelism,
599 // like if-conversion. Such passes will typically need dominator trees and
600 // loop info, just like LICM and CSE below.
601 if (addILPOpts())
602 printAndVerify("After ILP optimizations");
603
Bob Wilsonb9b69362012-07-02 19:48:37 +0000604 addPass(&MachineLICMID);
605 addPass(&MachineCSEID);
606 addPass(&MachineSinkingID);
Andrew Trickf5426752012-02-09 00:40:55 +0000607 printAndVerify("After Machine LICM, CSE and Sinking passes");
608
Bob Wilsonb9b69362012-07-02 19:48:37 +0000609 addPass(&PeepholeOptimizerID);
Quentin Colombet03e43f82014-08-20 17:41:48 +0000610 // Clean-up the dead code that may have been generated by peephole
611 // rewriting.
612 addPass(&DeadMachineInstructionElimID);
Andrew Trickf5426752012-02-09 00:40:55 +0000613 printAndVerify("After codegen peephole optimization pass");
614}
615
Andrew Trickb7551332012-02-04 02:56:45 +0000616//===---------------------------------------------------------------------===//
Andrew Trickf5426752012-02-09 00:40:55 +0000617/// Register Allocation Pass Configuration
Jim Laskey29e635d2006-08-02 12:30:23 +0000618//===---------------------------------------------------------------------===//
Andrew Trickf5426752012-02-09 00:40:55 +0000619
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000620bool TargetPassConfig::getOptimizeRegAlloc() const {
621 switch (OptimizeRegAlloc) {
622 case cl::BOU_UNSET: return getOptLevel() != CodeGenOpt::None;
623 case cl::BOU_TRUE: return true;
624 case cl::BOU_FALSE: return false;
625 }
626 llvm_unreachable("Invalid optimize-regalloc state");
627}
628
Andrew Trickf5426752012-02-09 00:40:55 +0000629/// RegisterRegAlloc's global Registry tracks allocator registration.
Jim Laskey29e635d2006-08-02 12:30:23 +0000630MachinePassRegistry RegisterRegAlloc::Registry;
631
Andrew Trickf5426752012-02-09 00:40:55 +0000632/// A dummy default pass factory indicates whether the register allocator is
633/// overridden on the command line.
Craig Topperc0196b12014-04-14 00:51:57 +0000634static FunctionPass *useDefaultRegisterAllocator() { return nullptr; }
Jakob Stoklund Olesenb613ae22010-05-27 23:57:25 +0000635static RegisterRegAlloc
636defaultRegAlloc("default",
637 "pick register allocator based on -O option",
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000638 useDefaultRegisterAllocator);
Jim Laskey29e635d2006-08-02 12:30:23 +0000639
Andrew Trickf5426752012-02-09 00:40:55 +0000640/// -regalloc=... command line option.
Dan Gohmand78c4002008-05-13 00:00:25 +0000641static cl::opt<RegisterRegAlloc::FunctionPassCtor, false,
642 RegisterPassParser<RegisterRegAlloc> >
643RegAlloc("regalloc",
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000644 cl::init(&useDefaultRegisterAllocator),
Jakob Stoklund Olesenb613ae22010-05-27 23:57:25 +0000645 cl::desc("Register allocator to use"));
Alkis Evlogimenos5facafa2003-10-02 16:57:49 +0000646
Jim Laskey29e635d2006-08-02 12:30:23 +0000647
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000648/// Instantiate the default register allocator pass for this target for either
649/// the optimized or unoptimized allocation path. This will be added to the pass
650/// manager by addFastRegAlloc in the unoptimized case or addOptimizedRegAlloc
651/// in the optimized case.
652///
653/// A target that uses the standard regalloc pass order for fast or optimized
654/// allocation may still override this for per-target regalloc
655/// selection. But -regalloc=... always takes precedence.
656FunctionPass *TargetPassConfig::createTargetRegisterAllocator(bool Optimized) {
657 if (Optimized)
658 return createGreedyRegisterAllocator();
659 else
660 return createFastRegisterAllocator();
661}
662
663/// Find and instantiate the register allocation pass requested by this target
664/// at the current optimization level. Different register allocators are
665/// defined as separate passes because they may require different analysis.
666///
667/// This helper ensures that the regalloc= option is always available,
668/// even for targets that override the default allocator.
669///
670/// FIXME: When MachinePassRegistry register pass IDs instead of function ptrs,
671/// this can be folded into addPass.
672FunctionPass *TargetPassConfig::createRegAllocPass(bool Optimized) {
Jim Laskey03593f72006-08-01 18:29:48 +0000673 RegisterRegAlloc::FunctionPassCtor Ctor = RegisterRegAlloc::getDefault();
Jakob Stoklund Olesenb613ae22010-05-27 23:57:25 +0000674
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000675 // Initialize the global default.
Jim Laskey95eda5b2006-08-01 14:21:23 +0000676 if (!Ctor) {
Jim Laskey29e635d2006-08-02 12:30:23 +0000677 Ctor = RegAlloc;
678 RegisterRegAlloc::setDefault(RegAlloc);
Jim Laskey95eda5b2006-08-01 14:21:23 +0000679 }
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000680 if (Ctor != useDefaultRegisterAllocator)
Jakob Stoklund Olesenb613ae22010-05-27 23:57:25 +0000681 return Ctor();
682
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000683 // With no -regalloc= override, ask the target for a regalloc pass.
684 return createTargetRegisterAllocator(Optimized);
685}
686
687/// Add the minimum set of target-independent passes that are required for
688/// register allocation. No coalescing or scheduling.
689void TargetPassConfig::addFastRegAlloc(FunctionPass *RegAllocPass) {
Bob Wilsonb9b69362012-07-02 19:48:37 +0000690 addPass(&PHIEliminationID);
691 addPass(&TwoAddressInstructionPassID);
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000692
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000693 addPass(RegAllocPass);
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000694 printAndVerify("After Register Allocation");
Jim Laskeyd1a714e2006-07-27 20:05:00 +0000695}
Andrew Trickf5426752012-02-09 00:40:55 +0000696
697/// Add standard target-independent passes that are tightly coupled with
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000698/// optimized register allocation, including coalescing, machine instruction
699/// scheduling, and register allocation itself.
700void TargetPassConfig::addOptimizedRegAlloc(FunctionPass *RegAllocPass) {
Bob Wilsonb9b69362012-07-02 19:48:37 +0000701 addPass(&ProcessImplicitDefsID);
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +0000702
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000703 // LiveVariables currently requires pure SSA form.
704 //
705 // FIXME: Once TwoAddressInstruction pass no longer uses kill flags,
706 // LiveVariables can be removed completely, and LiveIntervals can be directly
707 // computed. (We still either need to regenerate kill flags after regalloc, or
708 // preferably fix the scavenger to not depend on them).
Bob Wilsonb9b69362012-07-02 19:48:37 +0000709 addPass(&LiveVariablesID);
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000710
Rafael Espindola9770bde2013-10-14 16:39:04 +0000711 // Edge splitting is smarter with machine loop info.
712 addPass(&MachineLoopInfoID);
713 addPass(&PHIEliminationID);
Jakob Stoklund Olesen1c465892012-08-03 22:12:54 +0000714
715 // Eventually, we want to run LiveIntervals before PHI elimination.
716 if (EarlyLiveIntervals)
717 addPass(&LiveIntervalsID);
718
Bob Wilsonb9b69362012-07-02 19:48:37 +0000719 addPass(&TwoAddressInstructionPassID);
Bob Wilsonb9b69362012-07-02 19:48:37 +0000720 addPass(&RegisterCoalescerID);
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000721
722 // PreRA instruction scheduling.
Bob Wilsonb9b69362012-07-02 19:48:37 +0000723 if (addPass(&MachineSchedulerID))
Andrew Trick8823dec2012-03-14 04:00:41 +0000724 printAndVerify("After Machine Scheduling");
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000725
726 // Add the selected register allocation pass.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000727 addPass(RegAllocPass);
Jakob Stoklund Olesen59a0d322012-06-26 17:09:29 +0000728 printAndVerify("After Register Allocation, before rewriter");
729
730 // Allow targets to change the register assignments before rewriting.
731 if (addPreRewrite())
732 printAndVerify("After pre-rewrite passes");
Andrew Trickf5426752012-02-09 00:40:55 +0000733
Jakob Stoklund Olesen12243122012-06-08 23:44:45 +0000734 // Finally rewrite virtual registers.
Bob Wilsonb9b69362012-07-02 19:48:37 +0000735 addPass(&VirtRegRewriterID);
Jakob Stoklund Olesen12243122012-06-08 23:44:45 +0000736 printAndVerify("After Virtual Register Rewriter");
737
Andrew Trickf5426752012-02-09 00:40:55 +0000738 // Perform stack slot coloring and post-ra machine LICM.
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000739 //
740 // FIXME: Re-enable coloring with register when it's capable of adding
741 // kill markers.
Bob Wilsonb9b69362012-07-02 19:48:37 +0000742 addPass(&StackSlotColoringID);
Andrew Trick899f46c2012-02-15 07:57:03 +0000743
744 // Run post-ra machine LICM to hoist reloads / remats.
745 //
746 // FIXME: can this move into MachineLateOptimization?
Bob Wilsonb9b69362012-07-02 19:48:37 +0000747 addPass(&PostRAMachineLICMID);
Andrew Trick899f46c2012-02-15 07:57:03 +0000748
749 printAndVerify("After StackSlotColoring and postra Machine LICM");
Andrew Trickf5426752012-02-09 00:40:55 +0000750}
751
752//===---------------------------------------------------------------------===//
753/// Post RegAlloc Pass Configuration
754//===---------------------------------------------------------------------===//
755
756/// Add passes that optimize machine instructions after register allocation.
757void TargetPassConfig::addMachineLateOptimization() {
758 // Branch folding must be run after regalloc and prolog/epilog insertion.
Bob Wilsonb9b69362012-07-02 19:48:37 +0000759 if (addPass(&BranchFolderPassID))
Jakob Stoklund Olesen341e06f2012-03-28 20:47:37 +0000760 printAndVerify("After BranchFolding");
Andrew Trickf5426752012-02-09 00:40:55 +0000761
762 // Tail duplication.
Vincent Lejeune92b0a642013-12-07 01:49:19 +0000763 // Note that duplicating tail just increases code size and degrades
764 // performance for targets that require Structured Control Flow.
765 // In addition it can also make CFG irreducible. Thus we disable it.
766 if (!TM->requiresStructuredCFG() && addPass(&TailDuplicateID))
Jakob Stoklund Olesen341e06f2012-03-28 20:47:37 +0000767 printAndVerify("After TailDuplicate");
Andrew Trickf5426752012-02-09 00:40:55 +0000768
769 // Copy propagation.
Bob Wilsonb9b69362012-07-02 19:48:37 +0000770 if (addPass(&MachineCopyPropagationID))
Jakob Stoklund Olesen341e06f2012-03-28 20:47:37 +0000771 printAndVerify("After copy propagation pass");
Andrew Trickf5426752012-02-09 00:40:55 +0000772}
773
Evan Cheng59421ae2012-12-21 02:57:04 +0000774/// Add standard GC passes.
775bool TargetPassConfig::addGCPasses() {
776 addPass(&GCMachineCodeAnalysisID);
777 return true;
778}
779
Andrew Trickf5426752012-02-09 00:40:55 +0000780/// Add standard basic block placement passes.
781void TargetPassConfig::addBlockPlacement() {
Benjamin Kramer70671b92013-03-29 17:14:24 +0000782 if (addPass(&MachineBlockPlacementID)) {
Andrew Tricke9a951c2012-02-15 03:21:51 +0000783 // Run a separate pass to collect block placement statistics.
784 if (EnableBlockPlacementStats)
Bob Wilsonb9b69362012-07-02 19:48:37 +0000785 addPass(&MachineBlockPlacementStatsID);
Andrew Trickf5426752012-02-09 00:40:55 +0000786
Jakob Stoklund Olesenc3e80cc2012-03-28 23:54:28 +0000787 printAndVerify("After machine block placement.");
Andrew Trickf5426752012-02-09 00:40:55 +0000788 }
789}