blob: 05284347f09d75aeb131573e50a847a8f0c10a1c [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
Bill Wendling04523ea2007-02-08 01:36:53 +000032FileModel::Model
33LLVMTargetMachine::addPassesToEmitFile(FunctionPassManager &PM,
34 std::ostream &Out,
35 CodeGenFileType FileType,
36 bool Fast) {
Chris Lattner47877052006-09-04 04:16:09 +000037 // Standard LLVM-Level Passes.
38
39 // Run loop strength reduction before anything else.
Chris Lattner31442f92007-03-31 00:24:43 +000040 if (!Fast) {
41 PM.add(createLoopStrengthReducePass(getTargetLowering()));
42 if (PrintLSR)
43 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
44 }
Chris Lattner47877052006-09-04 04:16:09 +000045
46 // FIXME: Implement efficient support for garbage collection intrinsics.
47 PM.add(createLowerGCPass());
Duncan Sandsc3751602007-07-11 16:59:20 +000048
Jim Laskeya4e7cd92007-02-22 16:22:15 +000049 if (!ExceptionHandling)
50 PM.add(createLowerInvokePass(getTargetLowering()));
Duncan Sandsc3751602007-07-11 16:59:20 +000051
Chris Lattner47877052006-09-04 04:16:09 +000052 // Make sure that no unreachable blocks are instruction selected.
53 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +000054
Chris Lattnerc8d288f2007-03-31 04:18:03 +000055 if (!Fast)
56 PM.add(createCodeGenPreparePass(getTargetLowering()));
57
58 if (PrintISelInput)
59 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
60 &cerr));
61
Chris Lattner47877052006-09-04 04:16:09 +000062 // Ask the target for an isel.
63 if (addInstSelector(PM, Fast))
Bill Wendling04523ea2007-02-08 01:36:53 +000064 return FileModel::Error;
65
Chris Lattner47877052006-09-04 04:16:09 +000066 // Print the instruction selected machine code...
67 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000068 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling0f940c92007-12-07 21:42:31 +000069
70 PM.add(createMachineLICMPass());
71
Chris Lattner47877052006-09-04 04:16:09 +000072 // Perform register allocation to convert to a concrete x86 representation
73 PM.add(createRegisterAllocator());
74
75 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000076 PM.add(createMachineFunctionPrinterPass(cerr));
Christopher Lambada779f2007-07-27 07:36:14 +000077
78 PM.add(createLowerSubregsPass());
79
80 if (PrintMachineCode) // Print the subreg lowered code
81 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +000082
Chris Lattner47877052006-09-04 04:16:09 +000083 // Run post-ra passes.
84 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000085 PM.add(createMachineFunctionPrinterPass(cerr));
86
Chris Lattner47877052006-09-04 04:16:09 +000087 // Insert prolog/epilog code. Eliminate abstract frame index references...
88 PM.add(createPrologEpilogCodeInserter());
89
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000090 // Second pass scheduler.
Dale Johannesen72f15962007-07-13 17:31:29 +000091 if (!Fast)
92 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000093
Chris Lattner4a84ad72006-10-13 20:45:56 +000094 // Branch folding must be run after regalloc and prolog/epilog insertion.
Jim Laskey62d07d62006-10-24 16:11:49 +000095 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +000096 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendling0f940c92007-12-07 21:42:31 +000097
Jim Laskey9d4209f2006-11-07 19:33:46 +000098 // Fold redundant debug labels.
99 PM.add(createDebugLabelFoldingPass());
Chris Lattner4a84ad72006-10-13 20:45:56 +0000100
Chris Lattner47877052006-09-04 04:16:09 +0000101 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000102 PM.add(createMachineFunctionPrinterPass(cerr));
103
Chris Lattner47877052006-09-04 04:16:09 +0000104 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000105 PM.add(createMachineFunctionPrinterPass(cerr));
106
Chris Lattner47877052006-09-04 04:16:09 +0000107 switch (FileType) {
Bill Wendling04523ea2007-02-08 01:36:53 +0000108 default:
109 break;
110 case TargetMachine::AssemblyFile:
111 if (addAssemblyEmitter(PM, Fast, Out))
112 return FileModel::Error;
113 return FileModel::AsmFile;
114 case TargetMachine::ObjectFile:
115 if (getMachOWriterInfo())
116 return FileModel::MachOFile;
117 else if (getELFWriterInfo())
118 return FileModel::ElfFile;
Chris Lattner47877052006-09-04 04:16:09 +0000119 }
Bill Wendling04523ea2007-02-08 01:36:53 +0000120
121 return FileModel::Error;
122}
123
124/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
125/// be split up (e.g., to add an object writer pass), this method can be used to
126/// finish up adding passes to emit the file, if necessary.
127bool LLVMTargetMachine::addPassesToEmitFileFinish(FunctionPassManager &PM,
128 MachineCodeEmitter *MCE,
129 bool Fast) {
130 if (MCE)
Evan Cheng8bd60352007-07-20 21:56:13 +0000131 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
Bill Wendling04523ea2007-02-08 01:36:53 +0000132
Chris Lattner47877052006-09-04 04:16:09 +0000133 // Delete machine code for this function
134 PM.add(createMachineCodeDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000135
Chris Lattner47877052006-09-04 04:16:09 +0000136 return false; // success!
137}
138
139/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
140/// get machine code emitted. This uses a MachineCodeEmitter object to handle
141/// actually outputting the machine code and resolving things like the address
142/// of functions. This method should returns true if machine code emission is
143/// not supported.
144///
145bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
146 MachineCodeEmitter &MCE,
147 bool Fast) {
148 // Standard LLVM-Level Passes.
149
150 // Run loop strength reduction before anything else.
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000151 if (!Fast) {
152 PM.add(createLoopStrengthReducePass(getTargetLowering()));
153 if (PrintLSR)
154 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
155 }
Chris Lattner47877052006-09-04 04:16:09 +0000156
157 // FIXME: Implement efficient support for garbage collection intrinsics.
158 PM.add(createLowerGCPass());
159
160 // FIXME: Implement the invoke/unwind instructions!
Duraid Madina2a0013f2006-09-04 06:21:35 +0000161 PM.add(createLowerInvokePass(getTargetLowering()));
Chris Lattner47877052006-09-04 04:16:09 +0000162
163 // Make sure that no unreachable blocks are instruction selected.
164 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +0000165
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000166 if (!Fast)
167 PM.add(createCodeGenPreparePass(getTargetLowering()));
168
169 if (PrintISelInput)
170 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
171 &cerr));
172
Chris Lattner47877052006-09-04 04:16:09 +0000173 // Ask the target for an isel.
174 if (addInstSelector(PM, Fast))
175 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000176
Chris Lattner47877052006-09-04 04:16:09 +0000177 // Print the instruction selected machine code...
178 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000179 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling0f940c92007-12-07 21:42:31 +0000180
181 PM.add(createMachineLICMPass());
182
Chris Lattner47877052006-09-04 04:16:09 +0000183 // Perform register allocation to convert to a concrete x86 representation
184 PM.add(createRegisterAllocator());
185
186 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000187 PM.add(createMachineFunctionPrinterPass(cerr));
Christopher Lambada779f2007-07-27 07:36:14 +0000188
189 PM.add(createLowerSubregsPass());
190
191 if (PrintMachineCode) // Print the subreg lowered code
192 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000193
Chris Lattner47877052006-09-04 04:16:09 +0000194 // Run post-ra passes.
195 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000196 PM.add(createMachineFunctionPrinterPass(cerr));
197
Chris Lattner47877052006-09-04 04:16:09 +0000198 // Insert prolog/epilog code. Eliminate abstract frame index references...
199 PM.add(createPrologEpilogCodeInserter());
200
201 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000202 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +0000203
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000204 // Second pass scheduler.
Dale Johannesen72f15962007-07-13 17:31:29 +0000205 if (!Fast)
206 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000207
Chris Lattnere01eaa02006-11-16 01:00:07 +0000208 // Branch folding must be run after regalloc and prolog/epilog insertion.
209 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +0000210 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendling0f940c92007-12-07 21:42:31 +0000211
Chris Lattner47877052006-09-04 04:16:09 +0000212 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000213 PM.add(createMachineFunctionPrinterPass(cerr));
214
Evan Cheng8bd60352007-07-20 21:56:13 +0000215 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
Chris Lattner47877052006-09-04 04:16:09 +0000216
217 // Delete machine code for this function
218 PM.add(createMachineCodeDeleter());
219
220 return false; // success!
221}