blob: 60925f31ddf0347a4b568ba328d95fefd5ddd92f [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
Chris Lattner85ef2542007-06-19 05:47:49 +000028static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
29 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
30static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
31 cl::desc("Print LLVM IR input to isel pass"));
Evan Cheng8bd60352007-07-20 21:56:13 +000032static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden,
33 cl::desc("Dump emitter generated instructions as assembly"));
Gordon Henriksen93f96d02008-01-07 01:33:09 +000034static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
35 cl::desc("Dump garbage collector data"));
Chris Lattner85ef2542007-06-19 05:47:49 +000036
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000037// Hidden options to help debugging
38static cl::opt<bool>
39EnableSinking("enable-sinking", cl::init(false), cl::Hidden,
40 cl::desc("Perform sinking on machine code"));
Bill Wendlingcc8f6032008-01-04 08:11:03 +000041static cl::opt<bool>
Evan Cheng3f32d652008-06-04 09:18:41 +000042EnableLICM("machine-licm",
43 cl::init(false), cl::Hidden,
44 cl::desc("Perform loop-invariant code motion on machine code"));
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000045
Chris Lattner459525d2008-01-14 19:00:06 +000046// When this works it will be on by default.
47static cl::opt<bool>
48DisablePostRAScheduler("disable-post-RA-scheduler",
49 cl::desc("Disable scheduling after register allocation"),
50 cl::init(true));
51
Bill Wendling04523ea2007-02-08 01:36:53 +000052FileModel::Model
Dan Gohmanbfae8312008-03-11 22:29:46 +000053LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
Owen Andersoncb371882008-08-21 00:14:44 +000054 raw_ostream &Out,
Bill Wendling04523ea2007-02-08 01:36:53 +000055 CodeGenFileType FileType,
56 bool Fast) {
Dan Gohman02dae4b2008-09-25 00:37:07 +000057 // Add common CodeGen passes.
58 if (addCommonCodeGenPasses(PM, Fast))
Bill Wendling04523ea2007-02-08 01:36:53 +000059 return FileModel::Error;
60
Jim Laskey9d4209f2006-11-07 19:33:46 +000061 // Fold redundant debug labels.
62 PM.add(createDebugLabelFoldingPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +000063
64 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000065 PM.add(createMachineFunctionPrinterPass(cerr));
66
Chris Lattner47877052006-09-04 04:16:09 +000067 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000068 PM.add(createMachineFunctionPrinterPass(cerr));
69
Evan Cheng3f32d652008-06-04 09:18:41 +000070 if (!Fast && !OptimizeForSize)
Evan Chengd703ed62008-02-28 23:29:57 +000071 PM.add(createLoopAlignerPass());
72
Chris Lattner47877052006-09-04 04:16:09 +000073 switch (FileType) {
Bill Wendling04523ea2007-02-08 01:36:53 +000074 default:
75 break;
76 case TargetMachine::AssemblyFile:
77 if (addAssemblyEmitter(PM, Fast, Out))
78 return FileModel::Error;
79 return FileModel::AsmFile;
80 case TargetMachine::ObjectFile:
81 if (getMachOWriterInfo())
82 return FileModel::MachOFile;
83 else if (getELFWriterInfo())
84 return FileModel::ElfFile;
Chris Lattner47877052006-09-04 04:16:09 +000085 }
Bill Wendling04523ea2007-02-08 01:36:53 +000086
87 return FileModel::Error;
88}
Dan Gohman02dae4b2008-09-25 00:37:07 +000089
Bill Wendling04523ea2007-02-08 01:36:53 +000090/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
91/// be split up (e.g., to add an object writer pass), this method can be used to
92/// finish up adding passes to emit the file, if necessary.
Dan Gohmanbfae8312008-03-11 22:29:46 +000093bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
Bill Wendling04523ea2007-02-08 01:36:53 +000094 MachineCodeEmitter *MCE,
95 bool Fast) {
96 if (MCE)
Evan Cheng8bd60352007-07-20 21:56:13 +000097 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
Dan Gohman02dae4b2008-09-25 00:37:07 +000098
Gordon Henriksen5eca0752008-08-17 18:44:35 +000099 PM.add(createGCInfoDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000100
Chris Lattner47877052006-09-04 04:16:09 +0000101 // Delete machine code for this function
102 PM.add(createMachineCodeDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000103
Chris Lattner47877052006-09-04 04:16:09 +0000104 return false; // success!
105}
106
107/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
108/// get machine code emitted. This uses a MachineCodeEmitter object to handle
109/// actually outputting the machine code and resolving things like the address
110/// of functions. This method should returns true if machine code emission is
111/// not supported.
112///
Dan Gohmanbfae8312008-03-11 22:29:46 +0000113bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
Chris Lattner47877052006-09-04 04:16:09 +0000114 MachineCodeEmitter &MCE,
115 bool Fast) {
Dan Gohman02dae4b2008-09-25 00:37:07 +0000116 // Add common CodeGen passes.
117 if (addCommonCodeGenPasses(PM, Fast))
118 return true;
119
120 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
121 PM.add(createMachineFunctionPrinterPass(cerr));
122
123 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
124
125 PM.add(createGCInfoDeleter());
126
127 // Delete machine code for this function
128 PM.add(createMachineCodeDeleter());
129
130 return false; // success!
131}
132
133/// addCommonCodeGenPasses - Add standard LLVM codegen passes used for
134/// both emitting to assembly files or machine code output.
135///
136bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM, bool Fast) {
Chris Lattner47877052006-09-04 04:16:09 +0000137 // Standard LLVM-Level Passes.
Dan Gohman02dae4b2008-09-25 00:37:07 +0000138
Chris Lattner47877052006-09-04 04:16:09 +0000139 // Run loop strength reduction before anything else.
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000140 if (!Fast) {
141 PM.add(createLoopStrengthReducePass(getTargetLowering()));
142 if (PrintLSR)
Dan Gohman62c7b8c2008-03-25 21:38:12 +0000143 PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr));
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000144 }
Dan Gohman02dae4b2008-09-25 00:37:07 +0000145
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000146 PM.add(createGCLoweringPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000147
Dale Johannesen1532f3d2008-04-02 00:25:04 +0000148 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
Dale Johannesenb6d5b142008-04-01 20:00:57 +0000149 PM.add(createLowerInvokePass(getTargetLowering()));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000150
Chris Lattner47877052006-09-04 04:16:09 +0000151 // Make sure that no unreachable blocks are instruction selected.
152 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +0000153
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000154 if (!Fast)
155 PM.add(createCodeGenPreparePass(getTargetLowering()));
156
157 if (PrintISelInput)
Dan Gohman62c7b8c2008-03-25 21:38:12 +0000158 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n",
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000159 &cerr));
160
Dan Gohman02dae4b2008-09-25 00:37:07 +0000161 // Standard Lower-Level Passes.
162
Chris Lattner47877052006-09-04 04:16:09 +0000163 // Ask the target for an isel.
164 if (addInstSelector(PM, Fast))
165 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000166
Chris Lattner47877052006-09-04 04:16:09 +0000167 // Print the instruction selected machine code...
168 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000169 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling0f940c92007-12-07 21:42:31 +0000170
Evan Cheng3f32d652008-06-04 09:18:41 +0000171 if (EnableLICM)
Bill Wendlingcc8f6032008-01-04 08:11:03 +0000172 PM.add(createMachineLICMPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000173
Chris Lattner3c42f122008-01-05 06:14:16 +0000174 if (EnableSinking)
175 PM.add(createMachineSinkingPass());
Bill Wendling0f940c92007-12-07 21:42:31 +0000176
Anton Korobeynikovb013f502008-04-23 18:26:03 +0000177 // Run pre-ra passes.
178 if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
179 PM.add(createMachineFunctionPrinterPass(cerr));
180
Evan Cheng3f32d652008-06-04 09:18:41 +0000181 // Perform register allocation.
Chris Lattner47877052006-09-04 04:16:09 +0000182 PM.add(createRegisterAllocator());
Evan Cheng3f32d652008-06-04 09:18:41 +0000183
184 // Perform stack slot coloring.
Evan Cheng9ef4c532008-06-30 22:33:16 +0000185 if (!Fast)
186 PM.add(createStackSlotColoringPass());
Evan Cheng3f32d652008-06-04 09:18:41 +0000187
Dan Gohman02dae4b2008-09-25 00:37:07 +0000188 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000189 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000190
Evan Cheng3f32d652008-06-04 09:18:41 +0000191 // Run post-ra passes.
192 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
193 PM.add(createMachineFunctionPrinterPass(cerr));
194
Dan Gohman02dae4b2008-09-25 00:37:07 +0000195 if (PrintMachineCode)
Evan Cheng3f32d652008-06-04 09:18:41 +0000196 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000197
Christopher Lambada779f2007-07-27 07:36:14 +0000198 PM.add(createLowerSubregsPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000199
Christopher Lambada779f2007-07-27 07:36:14 +0000200 if (PrintMachineCode) // Print the subreg lowered code
201 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000202
Chris Lattner47877052006-09-04 04:16:09 +0000203 // Insert prolog/epilog code. Eliminate abstract frame index references...
204 PM.add(createPrologEpilogCodeInserter());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000205
Evan Cheng3f32d652008-06-04 09:18:41 +0000206 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000207 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000208
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000209 // Second pass scheduler.
Dan Gohman02dae4b2008-09-25 00:37:07 +0000210 if (!Fast && !DisablePostRAScheduler)
Dale Johannesen72f15962007-07-13 17:31:29 +0000211 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000212
Chris Lattnere01eaa02006-11-16 01:00:07 +0000213 // Branch folding must be run after regalloc and prolog/epilog insertion.
214 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +0000215 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendling0f940c92007-12-07 21:42:31 +0000216
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000217 PM.add(createGCMachineCodeAnalysisPass());
Evan Cheng3f32d652008-06-04 09:18:41 +0000218
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000219 if (PrintMachineCode)
220 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000221
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000222 if (PrintGCInfo)
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000223 PM.add(createGCInfoPrinter(*cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000224
Dan Gohman02dae4b2008-09-25 00:37:07 +0000225 return false;
Chris Lattner47877052006-09-04 04:16:09 +0000226}