blob: 6a8e775e6b21b42310543fb2a8c9b9c33d0d87a4 [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"
20#include "llvm/Target/TargetOptions.h"
21#include "llvm/Transforms/Scalar.h"
Chris Lattner31442f92007-03-31 00:24:43 +000022#include "llvm/Support/CommandLine.h"
Chris Lattner47877052006-09-04 04:16:09 +000023using namespace llvm;
24
Chris Lattner85ef2542007-06-19 05:47:49 +000025static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
26 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
27static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
28 cl::desc("Print LLVM IR input to isel pass"));
Evan Cheng8bd60352007-07-20 21:56:13 +000029static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden,
30 cl::desc("Dump emitter generated instructions as assembly"));
Chris Lattner85ef2542007-06-19 05:47:49 +000031
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000032// Hidden options to help debugging
33static cl::opt<bool>
34EnableSinking("enable-sinking", cl::init(false), cl::Hidden,
35 cl::desc("Perform sinking on machine code"));
36
37
Bill Wendling04523ea2007-02-08 01:36:53 +000038FileModel::Model
39LLVMTargetMachine::addPassesToEmitFile(FunctionPassManager &PM,
40 std::ostream &Out,
41 CodeGenFileType FileType,
42 bool Fast) {
Chris Lattner47877052006-09-04 04:16:09 +000043 // Standard LLVM-Level Passes.
44
45 // Run loop strength reduction before anything else.
Chris Lattner31442f92007-03-31 00:24:43 +000046 if (!Fast) {
47 PM.add(createLoopStrengthReducePass(getTargetLowering()));
48 if (PrintLSR)
49 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
50 }
Chris Lattner47877052006-09-04 04:16:09 +000051
52 // FIXME: Implement efficient support for garbage collection intrinsics.
53 PM.add(createLowerGCPass());
Duncan Sandsc3751602007-07-11 16:59:20 +000054
Jim Laskeya4e7cd92007-02-22 16:22:15 +000055 if (!ExceptionHandling)
56 PM.add(createLowerInvokePass(getTargetLowering()));
Duncan Sandsc3751602007-07-11 16:59:20 +000057
Chris Lattner47877052006-09-04 04:16:09 +000058 // Make sure that no unreachable blocks are instruction selected.
59 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +000060
Chris Lattnerc8d288f2007-03-31 04:18:03 +000061 if (!Fast)
62 PM.add(createCodeGenPreparePass(getTargetLowering()));
63
64 if (PrintISelInput)
65 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
66 &cerr));
67
Chris Lattner47877052006-09-04 04:16:09 +000068 // Ask the target for an isel.
69 if (addInstSelector(PM, Fast))
Bill Wendling04523ea2007-02-08 01:36:53 +000070 return FileModel::Error;
71
Chris Lattner47877052006-09-04 04:16:09 +000072 // Print the instruction selected machine code...
73 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000074 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling0f940c92007-12-07 21:42:31 +000075
76 PM.add(createMachineLICMPass());
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000077
78 if (EnableSinking)
79 PM.add(createMachineSinkingPass());
Bill Wendling0f940c92007-12-07 21:42:31 +000080
Chris Lattner47877052006-09-04 04:16:09 +000081 // Perform register allocation to convert to a concrete x86 representation
82 PM.add(createRegisterAllocator());
83
84 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000085 PM.add(createMachineFunctionPrinterPass(cerr));
Christopher Lambada779f2007-07-27 07:36:14 +000086
87 PM.add(createLowerSubregsPass());
88
89 if (PrintMachineCode) // Print the subreg lowered code
90 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +000091
Chris Lattner47877052006-09-04 04:16:09 +000092 // Run post-ra passes.
93 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000094 PM.add(createMachineFunctionPrinterPass(cerr));
95
Chris Lattner47877052006-09-04 04:16:09 +000096 // Insert prolog/epilog code. Eliminate abstract frame index references...
97 PM.add(createPrologEpilogCodeInserter());
98
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000099 // Second pass scheduler.
Dale Johannesen72f15962007-07-13 17:31:29 +0000100 if (!Fast)
101 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000102
Chris Lattner4a84ad72006-10-13 20:45:56 +0000103 // Branch folding must be run after regalloc and prolog/epilog insertion.
Jim Laskey62d07d62006-10-24 16:11:49 +0000104 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +0000105 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendling0f940c92007-12-07 21:42:31 +0000106
Jim Laskey9d4209f2006-11-07 19:33:46 +0000107 // Fold redundant debug labels.
108 PM.add(createDebugLabelFoldingPass());
Chris Lattner4a84ad72006-10-13 20:45:56 +0000109
Chris Lattner47877052006-09-04 04:16:09 +0000110 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000111 PM.add(createMachineFunctionPrinterPass(cerr));
112
Chris Lattner47877052006-09-04 04:16:09 +0000113 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000114 PM.add(createMachineFunctionPrinterPass(cerr));
115
Chris Lattner47877052006-09-04 04:16:09 +0000116 switch (FileType) {
Bill Wendling04523ea2007-02-08 01:36:53 +0000117 default:
118 break;
119 case TargetMachine::AssemblyFile:
120 if (addAssemblyEmitter(PM, Fast, Out))
121 return FileModel::Error;
122 return FileModel::AsmFile;
123 case TargetMachine::ObjectFile:
124 if (getMachOWriterInfo())
125 return FileModel::MachOFile;
126 else if (getELFWriterInfo())
127 return FileModel::ElfFile;
Chris Lattner47877052006-09-04 04:16:09 +0000128 }
Bill Wendling04523ea2007-02-08 01:36:53 +0000129
130 return FileModel::Error;
131}
132
133/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
134/// be split up (e.g., to add an object writer pass), this method can be used to
135/// finish up adding passes to emit the file, if necessary.
136bool LLVMTargetMachine::addPassesToEmitFileFinish(FunctionPassManager &PM,
137 MachineCodeEmitter *MCE,
138 bool Fast) {
139 if (MCE)
Evan Cheng8bd60352007-07-20 21:56:13 +0000140 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
Bill Wendling04523ea2007-02-08 01:36:53 +0000141
Chris Lattner47877052006-09-04 04:16:09 +0000142 // Delete machine code for this function
143 PM.add(createMachineCodeDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000144
Chris Lattner47877052006-09-04 04:16:09 +0000145 return false; // success!
146}
147
148/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
149/// get machine code emitted. This uses a MachineCodeEmitter object to handle
150/// actually outputting the machine code and resolving things like the address
151/// of functions. This method should returns true if machine code emission is
152/// not supported.
153///
154bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
155 MachineCodeEmitter &MCE,
156 bool Fast) {
157 // Standard LLVM-Level Passes.
158
159 // Run loop strength reduction before anything else.
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000160 if (!Fast) {
161 PM.add(createLoopStrengthReducePass(getTargetLowering()));
162 if (PrintLSR)
163 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
164 }
Chris Lattner47877052006-09-04 04:16:09 +0000165
166 // FIXME: Implement efficient support for garbage collection intrinsics.
167 PM.add(createLowerGCPass());
168
169 // FIXME: Implement the invoke/unwind instructions!
Duraid Madina2a0013f2006-09-04 06:21:35 +0000170 PM.add(createLowerInvokePass(getTargetLowering()));
Chris Lattner47877052006-09-04 04:16:09 +0000171
172 // Make sure that no unreachable blocks are instruction selected.
173 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +0000174
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000175 if (!Fast)
176 PM.add(createCodeGenPreparePass(getTargetLowering()));
177
178 if (PrintISelInput)
179 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
180 &cerr));
181
Chris Lattner47877052006-09-04 04:16:09 +0000182 // Ask the target for an isel.
183 if (addInstSelector(PM, Fast))
184 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000185
Chris Lattner47877052006-09-04 04:16:09 +0000186 // Print the instruction selected machine code...
187 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000188 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling0f940c92007-12-07 21:42:31 +0000189
190 PM.add(createMachineLICMPass());
191
Chris Lattner47877052006-09-04 04:16:09 +0000192 // Perform register allocation to convert to a concrete x86 representation
193 PM.add(createRegisterAllocator());
194
195 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000196 PM.add(createMachineFunctionPrinterPass(cerr));
Christopher Lambada779f2007-07-27 07:36:14 +0000197
198 PM.add(createLowerSubregsPass());
199
200 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 // Run post-ra passes.
204 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000205 PM.add(createMachineFunctionPrinterPass(cerr));
206
Chris Lattner47877052006-09-04 04:16:09 +0000207 // Insert prolog/epilog code. Eliminate abstract frame index references...
208 PM.add(createPrologEpilogCodeInserter());
209
210 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000211 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +0000212
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000213 // Second pass scheduler.
Dale Johannesen72f15962007-07-13 17:31:29 +0000214 if (!Fast)
215 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000216
Chris Lattnere01eaa02006-11-16 01:00:07 +0000217 // Branch folding must be run after regalloc and prolog/epilog insertion.
218 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +0000219 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendling0f940c92007-12-07 21:42:31 +0000220
Chris Lattner47877052006-09-04 04:16:09 +0000221 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000222 PM.add(createMachineFunctionPrinterPass(cerr));
223
Evan Cheng8bd60352007-07-20 21:56:13 +0000224 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
Chris Lattner47877052006-09-04 04:16:09 +0000225
226 // Delete machine code for this function
227 PM.add(createMachineCodeDeleter());
228
229 return false; // success!
230}