blob: 15c1d67f3fc206af4c4148f3fcae89f24aa76468 [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"));
Bill Wendlingcc8f6032008-01-04 08:11:03 +000036static cl::opt<bool>
37PerformLICM("machine-licm",
38 cl::init(false), cl::Hidden,
39 cl::desc("Perform loop-invariant code motion on machine code"));
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000040
Bill Wendling04523ea2007-02-08 01:36:53 +000041FileModel::Model
42LLVMTargetMachine::addPassesToEmitFile(FunctionPassManager &PM,
43 std::ostream &Out,
44 CodeGenFileType FileType,
45 bool Fast) {
Chris Lattner47877052006-09-04 04:16:09 +000046 // Standard LLVM-Level Passes.
47
48 // Run loop strength reduction before anything else.
Chris Lattner31442f92007-03-31 00:24:43 +000049 if (!Fast) {
50 PM.add(createLoopStrengthReducePass(getTargetLowering()));
51 if (PrintLSR)
52 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
53 }
Chris Lattner47877052006-09-04 04:16:09 +000054
55 // FIXME: Implement efficient support for garbage collection intrinsics.
56 PM.add(createLowerGCPass());
Duncan Sandsc3751602007-07-11 16:59:20 +000057
Jim Laskeya4e7cd92007-02-22 16:22:15 +000058 if (!ExceptionHandling)
59 PM.add(createLowerInvokePass(getTargetLowering()));
Duncan Sandsc3751602007-07-11 16:59:20 +000060
Chris Lattner47877052006-09-04 04:16:09 +000061 // Make sure that no unreachable blocks are instruction selected.
62 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +000063
Chris Lattnerc8d288f2007-03-31 04:18:03 +000064 if (!Fast)
65 PM.add(createCodeGenPreparePass(getTargetLowering()));
66
67 if (PrintISelInput)
68 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
69 &cerr));
70
Chris Lattner47877052006-09-04 04:16:09 +000071 // Ask the target for an isel.
72 if (addInstSelector(PM, Fast))
Bill Wendling04523ea2007-02-08 01:36:53 +000073 return FileModel::Error;
74
Chris Lattner47877052006-09-04 04:16:09 +000075 // Print the instruction selected machine code...
76 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000077 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling0f940c92007-12-07 21:42:31 +000078
Bill Wendlingcc8f6032008-01-04 08:11:03 +000079 if (PerformLICM)
80 PM.add(createMachineLICMPass());
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000081
82 if (EnableSinking)
83 PM.add(createMachineSinkingPass());
Bill Wendling0f940c92007-12-07 21:42:31 +000084
Chris Lattner47877052006-09-04 04:16:09 +000085 // Perform register allocation to convert to a concrete x86 representation
86 PM.add(createRegisterAllocator());
87
88 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000089 PM.add(createMachineFunctionPrinterPass(cerr));
Christopher Lambada779f2007-07-27 07:36:14 +000090
91 PM.add(createLowerSubregsPass());
92
93 if (PrintMachineCode) // Print the subreg lowered code
94 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +000095
Chris Lattner47877052006-09-04 04:16:09 +000096 // Run post-ra passes.
97 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000098 PM.add(createMachineFunctionPrinterPass(cerr));
99
Chris Lattner47877052006-09-04 04:16:09 +0000100 // Insert prolog/epilog code. Eliminate abstract frame index references...
101 PM.add(createPrologEpilogCodeInserter());
102
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000103 // Second pass scheduler.
Dale Johannesen72f15962007-07-13 17:31:29 +0000104 if (!Fast)
105 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000106
Chris Lattner4a84ad72006-10-13 20:45:56 +0000107 // Branch folding must be run after regalloc and prolog/epilog insertion.
Jim Laskey62d07d62006-10-24 16:11:49 +0000108 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +0000109 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendling0f940c92007-12-07 21:42:31 +0000110
Jim Laskey9d4209f2006-11-07 19:33:46 +0000111 // Fold redundant debug labels.
112 PM.add(createDebugLabelFoldingPass());
Chris Lattner4a84ad72006-10-13 20:45:56 +0000113
Chris Lattner47877052006-09-04 04:16:09 +0000114 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000115 PM.add(createMachineFunctionPrinterPass(cerr));
116
Chris Lattner47877052006-09-04 04:16:09 +0000117 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000118 PM.add(createMachineFunctionPrinterPass(cerr));
119
Chris Lattner47877052006-09-04 04:16:09 +0000120 switch (FileType) {
Bill Wendling04523ea2007-02-08 01:36:53 +0000121 default:
122 break;
123 case TargetMachine::AssemblyFile:
124 if (addAssemblyEmitter(PM, Fast, Out))
125 return FileModel::Error;
126 return FileModel::AsmFile;
127 case TargetMachine::ObjectFile:
128 if (getMachOWriterInfo())
129 return FileModel::MachOFile;
130 else if (getELFWriterInfo())
131 return FileModel::ElfFile;
Chris Lattner47877052006-09-04 04:16:09 +0000132 }
Bill Wendling04523ea2007-02-08 01:36:53 +0000133
134 return FileModel::Error;
135}
136
137/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
138/// be split up (e.g., to add an object writer pass), this method can be used to
139/// finish up adding passes to emit the file, if necessary.
140bool LLVMTargetMachine::addPassesToEmitFileFinish(FunctionPassManager &PM,
141 MachineCodeEmitter *MCE,
142 bool Fast) {
143 if (MCE)
Evan Cheng8bd60352007-07-20 21:56:13 +0000144 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
Bill Wendling04523ea2007-02-08 01:36:53 +0000145
Chris Lattner47877052006-09-04 04:16:09 +0000146 // Delete machine code for this function
147 PM.add(createMachineCodeDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000148
Chris Lattner47877052006-09-04 04:16:09 +0000149 return false; // success!
150}
151
152/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
153/// get machine code emitted. This uses a MachineCodeEmitter object to handle
154/// actually outputting the machine code and resolving things like the address
155/// of functions. This method should returns true if machine code emission is
156/// not supported.
157///
158bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
159 MachineCodeEmitter &MCE,
160 bool Fast) {
161 // Standard LLVM-Level Passes.
162
163 // Run loop strength reduction before anything else.
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000164 if (!Fast) {
165 PM.add(createLoopStrengthReducePass(getTargetLowering()));
166 if (PrintLSR)
167 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
168 }
Chris Lattner47877052006-09-04 04:16:09 +0000169
170 // FIXME: Implement efficient support for garbage collection intrinsics.
171 PM.add(createLowerGCPass());
172
173 // FIXME: Implement the invoke/unwind instructions!
Duraid Madina2a0013f2006-09-04 06:21:35 +0000174 PM.add(createLowerInvokePass(getTargetLowering()));
Chris Lattner47877052006-09-04 04:16:09 +0000175
176 // Make sure that no unreachable blocks are instruction selected.
177 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +0000178
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000179 if (!Fast)
180 PM.add(createCodeGenPreparePass(getTargetLowering()));
181
182 if (PrintISelInput)
183 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
184 &cerr));
185
Chris Lattner47877052006-09-04 04:16:09 +0000186 // Ask the target for an isel.
187 if (addInstSelector(PM, Fast))
188 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000189
Chris Lattner47877052006-09-04 04:16:09 +0000190 // Print the instruction selected machine code...
191 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000192 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling0f940c92007-12-07 21:42:31 +0000193
Bill Wendlingcc8f6032008-01-04 08:11:03 +0000194 if (PerformLICM)
195 PM.add(createMachineLICMPass());
Bill Wendling0f940c92007-12-07 21:42:31 +0000196
Chris Lattner47877052006-09-04 04:16:09 +0000197 // Perform register allocation to convert to a concrete x86 representation
198 PM.add(createRegisterAllocator());
199
200 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000201 PM.add(createMachineFunctionPrinterPass(cerr));
Christopher Lambada779f2007-07-27 07:36:14 +0000202
203 PM.add(createLowerSubregsPass());
204
205 if (PrintMachineCode) // Print the subreg lowered code
206 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000207
Chris Lattner47877052006-09-04 04:16:09 +0000208 // Run post-ra passes.
209 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000210 PM.add(createMachineFunctionPrinterPass(cerr));
211
Chris Lattner47877052006-09-04 04:16:09 +0000212 // Insert prolog/epilog code. Eliminate abstract frame index references...
213 PM.add(createPrologEpilogCodeInserter());
214
215 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000216 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +0000217
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000218 // Second pass scheduler.
Dale Johannesen72f15962007-07-13 17:31:29 +0000219 if (!Fast)
220 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000221
Chris Lattnere01eaa02006-11-16 01:00:07 +0000222 // Branch folding must be run after regalloc and prolog/epilog insertion.
223 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +0000224 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendling0f940c92007-12-07 21:42:31 +0000225
Chris Lattner47877052006-09-04 04:16:09 +0000226 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000227 PM.add(createMachineFunctionPrinterPass(cerr));
228
Evan Cheng8bd60352007-07-20 21:56:13 +0000229 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
Chris Lattner47877052006-09-04 04:16:09 +0000230
231 // Delete machine code for this function
232 PM.add(createMachineCodeDeleter());
233
234 return false; // success!
235}