blob: b72704bd8051d33811e5d9f86c039aa90a05ef5a [file] [log] [blame]
Chris Lattner47877052006-09-04 04:16:09 +00001//===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"));
29
Bill Wendling04523ea2007-02-08 01:36:53 +000030FileModel::Model
31LLVMTargetMachine::addPassesToEmitFile(FunctionPassManager &PM,
32 std::ostream &Out,
33 CodeGenFileType FileType,
34 bool Fast) {
Chris Lattner47877052006-09-04 04:16:09 +000035 // Standard LLVM-Level Passes.
36
37 // Run loop strength reduction before anything else.
Chris Lattner31442f92007-03-31 00:24:43 +000038 if (!Fast) {
39 PM.add(createLoopStrengthReducePass(getTargetLowering()));
40 if (PrintLSR)
41 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
42 }
Chris Lattner47877052006-09-04 04:16:09 +000043
44 // FIXME: Implement efficient support for garbage collection intrinsics.
45 PM.add(createLowerGCPass());
Duncan Sandsc3751602007-07-11 16:59:20 +000046
Jim Laskeya4e7cd92007-02-22 16:22:15 +000047 if (!ExceptionHandling)
48 PM.add(createLowerInvokePass(getTargetLowering()));
Duncan Sandsc3751602007-07-11 16:59:20 +000049
Chris Lattner47877052006-09-04 04:16:09 +000050 // Make sure that no unreachable blocks are instruction selected.
51 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +000052
Chris Lattnerc8d288f2007-03-31 04:18:03 +000053 if (!Fast)
54 PM.add(createCodeGenPreparePass(getTargetLowering()));
55
56 if (PrintISelInput)
57 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
58 &cerr));
59
Chris Lattner47877052006-09-04 04:16:09 +000060 // Ask the target for an isel.
61 if (addInstSelector(PM, Fast))
Bill Wendling04523ea2007-02-08 01:36:53 +000062 return FileModel::Error;
63
Chris Lattner47877052006-09-04 04:16:09 +000064 // Print the instruction selected machine code...
65 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000066 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +000067
68 // Perform register allocation to convert to a concrete x86 representation
69 PM.add(createRegisterAllocator());
70
71 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000072 PM.add(createMachineFunctionPrinterPass(cerr));
73
Chris Lattner47877052006-09-04 04:16:09 +000074 // Run post-ra passes.
75 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000076 PM.add(createMachineFunctionPrinterPass(cerr));
77
Chris Lattner47877052006-09-04 04:16:09 +000078 // Insert prolog/epilog code. Eliminate abstract frame index references...
79 PM.add(createPrologEpilogCodeInserter());
80
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000081 // Second pass scheduler.
Dale Johannesen72f15962007-07-13 17:31:29 +000082 if (!Fast)
83 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000084
Chris Lattner4a84ad72006-10-13 20:45:56 +000085 // Branch folding must be run after regalloc and prolog/epilog insertion.
Jim Laskey62d07d62006-10-24 16:11:49 +000086 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +000087 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Jim Laskey9d4209f2006-11-07 19:33:46 +000088
89 // Fold redundant debug labels.
90 PM.add(createDebugLabelFoldingPass());
Chris Lattner4a84ad72006-10-13 20:45:56 +000091
Chris Lattner47877052006-09-04 04:16:09 +000092 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +000093 PM.add(createMachineFunctionPrinterPass(cerr));
94
Chris Lattner47877052006-09-04 04:16:09 +000095 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000096 PM.add(createMachineFunctionPrinterPass(cerr));
97
Chris Lattner47877052006-09-04 04:16:09 +000098 switch (FileType) {
Bill Wendling04523ea2007-02-08 01:36:53 +000099 default:
100 break;
101 case TargetMachine::AssemblyFile:
102 if (addAssemblyEmitter(PM, Fast, Out))
103 return FileModel::Error;
104 return FileModel::AsmFile;
105 case TargetMachine::ObjectFile:
106 if (getMachOWriterInfo())
107 return FileModel::MachOFile;
108 else if (getELFWriterInfo())
109 return FileModel::ElfFile;
Chris Lattner47877052006-09-04 04:16:09 +0000110 }
Bill Wendling04523ea2007-02-08 01:36:53 +0000111
112 return FileModel::Error;
113}
114
115/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
116/// be split up (e.g., to add an object writer pass), this method can be used to
117/// finish up adding passes to emit the file, if necessary.
118bool LLVMTargetMachine::addPassesToEmitFileFinish(FunctionPassManager &PM,
119 MachineCodeEmitter *MCE,
120 bool Fast) {
121 if (MCE)
122 addSimpleCodeEmitter(PM, Fast, *MCE);
123
Chris Lattner47877052006-09-04 04:16:09 +0000124 // Delete machine code for this function
125 PM.add(createMachineCodeDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000126
Chris Lattner47877052006-09-04 04:16:09 +0000127 return false; // success!
128}
129
130/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
131/// get machine code emitted. This uses a MachineCodeEmitter object to handle
132/// actually outputting the machine code and resolving things like the address
133/// of functions. This method should returns true if machine code emission is
134/// not supported.
135///
136bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
137 MachineCodeEmitter &MCE,
138 bool Fast) {
139 // Standard LLVM-Level Passes.
140
141 // Run loop strength reduction before anything else.
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000142 if (!Fast) {
143 PM.add(createLoopStrengthReducePass(getTargetLowering()));
144 if (PrintLSR)
145 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
146 }
Chris Lattner47877052006-09-04 04:16:09 +0000147
148 // FIXME: Implement efficient support for garbage collection intrinsics.
149 PM.add(createLowerGCPass());
150
151 // FIXME: Implement the invoke/unwind instructions!
Duraid Madina2a0013f2006-09-04 06:21:35 +0000152 PM.add(createLowerInvokePass(getTargetLowering()));
Chris Lattner47877052006-09-04 04:16:09 +0000153
154 // Make sure that no unreachable blocks are instruction selected.
155 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +0000156
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000157 if (!Fast)
158 PM.add(createCodeGenPreparePass(getTargetLowering()));
159
160 if (PrintISelInput)
161 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
162 &cerr));
163
Chris Lattner47877052006-09-04 04:16:09 +0000164 // Ask the target for an isel.
165 if (addInstSelector(PM, Fast))
166 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000167
Chris Lattner47877052006-09-04 04:16:09 +0000168 // Print the instruction selected machine code...
169 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000170 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +0000171
172 // Perform register allocation to convert to a concrete x86 representation
173 PM.add(createRegisterAllocator());
174
175 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000176 PM.add(createMachineFunctionPrinterPass(cerr));
177
Chris Lattner47877052006-09-04 04:16:09 +0000178 // Run post-ra passes.
179 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000180 PM.add(createMachineFunctionPrinterPass(cerr));
181
Chris Lattner47877052006-09-04 04:16:09 +0000182 // Insert prolog/epilog code. Eliminate abstract frame index references...
183 PM.add(createPrologEpilogCodeInserter());
184
185 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000186 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +0000187
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000188 // Second pass scheduler.
Dale Johannesen72f15962007-07-13 17:31:29 +0000189 if (!Fast)
190 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000191
Chris Lattnere01eaa02006-11-16 01:00:07 +0000192 // Branch folding must be run after regalloc and prolog/epilog insertion.
193 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +0000194 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Chris Lattner47877052006-09-04 04:16:09 +0000195
196 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000197 PM.add(createMachineFunctionPrinterPass(cerr));
198
Chris Lattner47877052006-09-04 04:16:09 +0000199 addCodeEmitter(PM, Fast, MCE);
200
201 // Delete machine code for this function
202 PM.add(createMachineCodeDeleter());
203
204 return false; // success!
205}