blob: 936854013693b7a093d54d53b2436ad7091e4a78 [file] [log] [blame]
Chris Lattner47877052006-09-04 04:16:09 +00001//===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner47877052006-09-04 04:16:09 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LLVMTargetMachine class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Target/TargetMachine.h"
15#include "llvm/PassManager.h"
16#include "llvm/Pass.h"
Chris Lattner31442f92007-03-31 00:24:43 +000017#include "llvm/Assembly/PrintModulePass.h"
Devang Patel0f54dcb2007-03-06 21:14:09 +000018#include "llvm/Analysis/LoopPass.h"
Chris Lattner47877052006-09-04 04:16:09 +000019#include "llvm/CodeGen/Passes.h"
Gordon Henriksen5a29c9e2008-08-17 12:56:54 +000020#include "llvm/CodeGen/GCStrategy.h"
Chris Lattner47877052006-09-04 04:16:09 +000021#include "llvm/Target/TargetOptions.h"
Dale Johannesen1532f3d2008-04-02 00:25:04 +000022#include "llvm/Target/TargetAsmInfo.h"
Chris Lattner47877052006-09-04 04:16:09 +000023#include "llvm/Transforms/Scalar.h"
Chris Lattner31442f92007-03-31 00:24:43 +000024#include "llvm/Support/CommandLine.h"
Owen Andersoncb371882008-08-21 00:14:44 +000025#include "llvm/Support/raw_ostream.h"
Chris Lattner47877052006-09-04 04:16:09 +000026using namespace llvm;
27
Dan Gohman2c4bf112008-09-25 01:14:49 +000028namespace llvm {
29 bool EnableFastISel;
30}
31
Chris Lattner85ef2542007-06-19 05:47:49 +000032static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
33 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
34static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
35 cl::desc("Print LLVM IR input to isel pass"));
Evan Cheng8bd60352007-07-20 21:56:13 +000036static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden,
37 cl::desc("Dump emitter generated instructions as assembly"));
Gordon Henriksen93f96d02008-01-07 01:33:09 +000038static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
39 cl::desc("Dump garbage collector data"));
Chris Lattner85ef2542007-06-19 05:47:49 +000040
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000041// Hidden options to help debugging
42static cl::opt<bool>
43EnableSinking("enable-sinking", cl::init(false), cl::Hidden,
44 cl::desc("Perform sinking on machine code"));
Bill Wendlingcc8f6032008-01-04 08:11:03 +000045static cl::opt<bool>
Evan Cheng3f32d652008-06-04 09:18:41 +000046EnableLICM("machine-licm",
47 cl::init(false), cl::Hidden,
48 cl::desc("Perform loop-invariant code motion on machine code"));
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000049
Chris Lattner459525d2008-01-14 19:00:06 +000050// When this works it will be on by default.
51static cl::opt<bool>
52DisablePostRAScheduler("disable-post-RA-scheduler",
53 cl::desc("Disable scheduling after register allocation"),
54 cl::init(true));
55
Dan Gohman2c4bf112008-09-25 01:14:49 +000056static cl::opt<bool, true>
57FastISelOption("fast-isel", cl::Hidden,
58 cl::desc("Enable the experimental \"fast\" instruction selector"),
59 cl::location(EnableFastISel));
60
Bill Wendling04523ea2007-02-08 01:36:53 +000061FileModel::Model
Dan Gohmanbfae8312008-03-11 22:29:46 +000062LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
Owen Andersoncb371882008-08-21 00:14:44 +000063 raw_ostream &Out,
Bill Wendling04523ea2007-02-08 01:36:53 +000064 CodeGenFileType FileType,
65 bool Fast) {
Dan Gohman02dae4b2008-09-25 00:37:07 +000066 // Add common CodeGen passes.
67 if (addCommonCodeGenPasses(PM, Fast))
Bill Wendling04523ea2007-02-08 01:36:53 +000068 return FileModel::Error;
69
Jim Laskey9d4209f2006-11-07 19:33:46 +000070 // Fold redundant debug labels.
71 PM.add(createDebugLabelFoldingPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +000072
73 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000074 PM.add(createMachineFunctionPrinterPass(cerr));
75
Chris Lattner47877052006-09-04 04:16:09 +000076 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000077 PM.add(createMachineFunctionPrinterPass(cerr));
78
Evan Cheng3f32d652008-06-04 09:18:41 +000079 if (!Fast && !OptimizeForSize)
Evan Chengd703ed62008-02-28 23:29:57 +000080 PM.add(createLoopAlignerPass());
81
Chris Lattner47877052006-09-04 04:16:09 +000082 switch (FileType) {
Bill Wendling04523ea2007-02-08 01:36:53 +000083 default:
84 break;
85 case TargetMachine::AssemblyFile:
86 if (addAssemblyEmitter(PM, Fast, Out))
87 return FileModel::Error;
88 return FileModel::AsmFile;
89 case TargetMachine::ObjectFile:
90 if (getMachOWriterInfo())
91 return FileModel::MachOFile;
92 else if (getELFWriterInfo())
93 return FileModel::ElfFile;
Chris Lattner47877052006-09-04 04:16:09 +000094 }
Bill Wendling04523ea2007-02-08 01:36:53 +000095
96 return FileModel::Error;
97}
Dan Gohman02dae4b2008-09-25 00:37:07 +000098
Bill Wendling04523ea2007-02-08 01:36:53 +000099/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
100/// be split up (e.g., to add an object writer pass), this method can be used to
101/// finish up adding passes to emit the file, if necessary.
Dan Gohmanbfae8312008-03-11 22:29:46 +0000102bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
Bill Wendling04523ea2007-02-08 01:36:53 +0000103 MachineCodeEmitter *MCE,
104 bool Fast) {
105 if (MCE)
Evan Cheng8bd60352007-07-20 21:56:13 +0000106 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
Dan Gohman02dae4b2008-09-25 00:37:07 +0000107
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000108 PM.add(createGCInfoDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000109
Chris Lattner47877052006-09-04 04:16:09 +0000110 // Delete machine code for this function
111 PM.add(createMachineCodeDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000112
Chris Lattner47877052006-09-04 04:16:09 +0000113 return false; // success!
114}
115
116/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
117/// get machine code emitted. This uses a MachineCodeEmitter object to handle
118/// actually outputting the machine code and resolving things like the address
119/// of functions. This method should returns true if machine code emission is
120/// not supported.
121///
Dan Gohmanbfae8312008-03-11 22:29:46 +0000122bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
Chris Lattner47877052006-09-04 04:16:09 +0000123 MachineCodeEmitter &MCE,
124 bool Fast) {
Dan Gohman02dae4b2008-09-25 00:37:07 +0000125 // Add common CodeGen passes.
126 if (addCommonCodeGenPasses(PM, Fast))
127 return true;
128
129 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
130 PM.add(createMachineFunctionPrinterPass(cerr));
131
132 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
133
134 PM.add(createGCInfoDeleter());
135
136 // Delete machine code for this function
137 PM.add(createMachineCodeDeleter());
138
139 return false; // success!
140}
141
142/// addCommonCodeGenPasses - Add standard LLVM codegen passes used for
143/// both emitting to assembly files or machine code output.
144///
145bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM, bool Fast) {
Chris Lattner47877052006-09-04 04:16:09 +0000146 // Standard LLVM-Level Passes.
Dan Gohman02dae4b2008-09-25 00:37:07 +0000147
Chris Lattner47877052006-09-04 04:16:09 +0000148 // Run loop strength reduction before anything else.
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000149 if (!Fast) {
150 PM.add(createLoopStrengthReducePass(getTargetLowering()));
151 if (PrintLSR)
Dan Gohman62c7b8c2008-03-25 21:38:12 +0000152 PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr));
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000153 }
Dan Gohman02dae4b2008-09-25 00:37:07 +0000154
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000155 PM.add(createGCLoweringPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000156
Dale Johannesen1532f3d2008-04-02 00:25:04 +0000157 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
Dale Johannesenb6d5b142008-04-01 20:00:57 +0000158 PM.add(createLowerInvokePass(getTargetLowering()));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000159
Chris Lattner47877052006-09-04 04:16:09 +0000160 // Make sure that no unreachable blocks are instruction selected.
161 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +0000162
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000163 if (!Fast)
164 PM.add(createCodeGenPreparePass(getTargetLowering()));
165
166 if (PrintISelInput)
Dan Gohman62c7b8c2008-03-25 21:38:12 +0000167 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n",
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000168 &cerr));
169
Dan Gohman02dae4b2008-09-25 00:37:07 +0000170 // Standard Lower-Level Passes.
171
Chris Lattner47877052006-09-04 04:16:09 +0000172 // Ask the target for an isel.
173 if (addInstSelector(PM, Fast))
174 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000175
Chris Lattner47877052006-09-04 04:16:09 +0000176 // Print the instruction selected machine code...
177 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000178 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling0f940c92007-12-07 21:42:31 +0000179
Dan Gohman2c4bf112008-09-25 01:14:49 +0000180 // If we're using Fast-ISel, clean up the mess.
181 if (EnableFastISel)
182 PM.add(createDeadMachineInstructionElimPass());
183
Evan Cheng3f32d652008-06-04 09:18:41 +0000184 if (EnableLICM)
Bill Wendlingcc8f6032008-01-04 08:11:03 +0000185 PM.add(createMachineLICMPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000186
Chris Lattner3c42f122008-01-05 06:14:16 +0000187 if (EnableSinking)
188 PM.add(createMachineSinkingPass());
Bill Wendling0f940c92007-12-07 21:42:31 +0000189
Anton Korobeynikovb013f502008-04-23 18:26:03 +0000190 // Run pre-ra passes.
191 if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
192 PM.add(createMachineFunctionPrinterPass(cerr));
193
Evan Cheng3f32d652008-06-04 09:18:41 +0000194 // Perform register allocation.
Chris Lattner47877052006-09-04 04:16:09 +0000195 PM.add(createRegisterAllocator());
Evan Cheng3f32d652008-06-04 09:18:41 +0000196
197 // Perform stack slot coloring.
Evan Cheng9ef4c532008-06-30 22:33:16 +0000198 if (!Fast)
199 PM.add(createStackSlotColoringPass());
Evan Cheng3f32d652008-06-04 09:18:41 +0000200
Dan Gohman02dae4b2008-09-25 00:37:07 +0000201 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000202 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000203
Evan Cheng3f32d652008-06-04 09:18:41 +0000204 // Run post-ra passes.
205 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
206 PM.add(createMachineFunctionPrinterPass(cerr));
207
Dan Gohman02dae4b2008-09-25 00:37:07 +0000208 if (PrintMachineCode)
Evan Cheng3f32d652008-06-04 09:18:41 +0000209 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000210
Christopher Lambada779f2007-07-27 07:36:14 +0000211 PM.add(createLowerSubregsPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000212
Christopher Lambada779f2007-07-27 07:36:14 +0000213 if (PrintMachineCode) // Print the subreg lowered code
214 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000215
Chris Lattner47877052006-09-04 04:16:09 +0000216 // Insert prolog/epilog code. Eliminate abstract frame index references...
217 PM.add(createPrologEpilogCodeInserter());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000218
Evan Cheng3f32d652008-06-04 09:18:41 +0000219 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000220 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000221
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000222 // Second pass scheduler.
Dan Gohman02dae4b2008-09-25 00:37:07 +0000223 if (!Fast && !DisablePostRAScheduler)
Dale Johannesen72f15962007-07-13 17:31:29 +0000224 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000225
Chris Lattnere01eaa02006-11-16 01:00:07 +0000226 // Branch folding must be run after regalloc and prolog/epilog insertion.
227 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +0000228 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendling0f940c92007-12-07 21:42:31 +0000229
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000230 PM.add(createGCMachineCodeAnalysisPass());
Evan Cheng3f32d652008-06-04 09:18:41 +0000231
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000232 if (PrintMachineCode)
233 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000234
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000235 if (PrintGCInfo)
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000236 PM.add(createGCInfoPrinter(*cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000237
Dan Gohman02dae4b2008-09-25 00:37:07 +0000238 return false;
Chris Lattner47877052006-09-04 04:16:09 +0000239}