blob: 526c5255fe3a277dd96fc40cae5fba2cf4a0dced [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"));
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));
Chris Lattner47877052006-09-04 04:16:09 +000069
70 // Perform register allocation to convert to a concrete x86 representation
71 PM.add(createRegisterAllocator());
72
73 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000074 PM.add(createMachineFunctionPrinterPass(cerr));
Christopher Lambada779f2007-07-27 07:36:14 +000075
76 PM.add(createLowerSubregsPass());
77
78 if (PrintMachineCode) // Print the subreg lowered code
79 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +000080
Chris Lattner47877052006-09-04 04:16:09 +000081 // Run post-ra passes.
82 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000083 PM.add(createMachineFunctionPrinterPass(cerr));
84
Chris Lattner47877052006-09-04 04:16:09 +000085 // Insert prolog/epilog code. Eliminate abstract frame index references...
86 PM.add(createPrologEpilogCodeInserter());
87
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000088 // Second pass scheduler.
Dale Johannesen72f15962007-07-13 17:31:29 +000089 if (!Fast)
90 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000091
Chris Lattner4a84ad72006-10-13 20:45:56 +000092 // Branch folding must be run after regalloc and prolog/epilog insertion.
Jim Laskey62d07d62006-10-24 16:11:49 +000093 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +000094 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Jim Laskey9d4209f2006-11-07 19:33:46 +000095
96 // Fold redundant debug labels.
97 PM.add(createDebugLabelFoldingPass());
Chris Lattner4a84ad72006-10-13 20:45:56 +000098
Chris Lattner47877052006-09-04 04:16:09 +000099 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000100 PM.add(createMachineFunctionPrinterPass(cerr));
101
Chris Lattner47877052006-09-04 04:16:09 +0000102 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000103 PM.add(createMachineFunctionPrinterPass(cerr));
104
Chris Lattner47877052006-09-04 04:16:09 +0000105 switch (FileType) {
Bill Wendling04523ea2007-02-08 01:36:53 +0000106 default:
107 break;
108 case TargetMachine::AssemblyFile:
109 if (addAssemblyEmitter(PM, Fast, Out))
110 return FileModel::Error;
111 return FileModel::AsmFile;
112 case TargetMachine::ObjectFile:
113 if (getMachOWriterInfo())
114 return FileModel::MachOFile;
115 else if (getELFWriterInfo())
116 return FileModel::ElfFile;
Chris Lattner47877052006-09-04 04:16:09 +0000117 }
Bill Wendling04523ea2007-02-08 01:36:53 +0000118
119 return FileModel::Error;
120}
121
122/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
123/// be split up (e.g., to add an object writer pass), this method can be used to
124/// finish up adding passes to emit the file, if necessary.
125bool LLVMTargetMachine::addPassesToEmitFileFinish(FunctionPassManager &PM,
126 MachineCodeEmitter *MCE,
127 bool Fast) {
128 if (MCE)
Evan Cheng8bd60352007-07-20 21:56:13 +0000129 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
Bill Wendling04523ea2007-02-08 01:36:53 +0000130
Chris Lattner47877052006-09-04 04:16:09 +0000131 // Delete machine code for this function
132 PM.add(createMachineCodeDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000133
Chris Lattner47877052006-09-04 04:16:09 +0000134 return false; // success!
135}
136
137/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
138/// get machine code emitted. This uses a MachineCodeEmitter object to handle
139/// actually outputting the machine code and resolving things like the address
140/// of functions. This method should returns true if machine code emission is
141/// not supported.
142///
143bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
144 MachineCodeEmitter &MCE,
145 bool Fast) {
146 // Standard LLVM-Level Passes.
147
148 // Run loop strength reduction before anything else.
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000149 if (!Fast) {
150 PM.add(createLoopStrengthReducePass(getTargetLowering()));
151 if (PrintLSR)
152 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
153 }
Chris Lattner47877052006-09-04 04:16:09 +0000154
155 // FIXME: Implement efficient support for garbage collection intrinsics.
156 PM.add(createLowerGCPass());
157
158 // FIXME: Implement the invoke/unwind instructions!
Duraid Madina2a0013f2006-09-04 06:21:35 +0000159 PM.add(createLowerInvokePass(getTargetLowering()));
Chris Lattner47877052006-09-04 04:16:09 +0000160
161 // Make sure that no unreachable blocks are instruction selected.
162 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +0000163
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000164 if (!Fast)
165 PM.add(createCodeGenPreparePass(getTargetLowering()));
166
167 if (PrintISelInput)
168 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
169 &cerr));
170
Chris Lattner47877052006-09-04 04:16:09 +0000171 // Ask the target for an isel.
172 if (addInstSelector(PM, Fast))
173 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000174
Chris Lattner47877052006-09-04 04:16:09 +0000175 // Print the instruction selected machine code...
176 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000177 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +0000178
179 // Perform register allocation to convert to a concrete x86 representation
180 PM.add(createRegisterAllocator());
181
182 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000183 PM.add(createMachineFunctionPrinterPass(cerr));
Christopher Lambada779f2007-07-27 07:36:14 +0000184
185 PM.add(createLowerSubregsPass());
186
187 if (PrintMachineCode) // Print the subreg lowered code
188 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000189
Chris Lattner47877052006-09-04 04:16:09 +0000190 // Run post-ra passes.
191 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000192 PM.add(createMachineFunctionPrinterPass(cerr));
193
Chris Lattner47877052006-09-04 04:16:09 +0000194 // Insert prolog/epilog code. Eliminate abstract frame index references...
195 PM.add(createPrologEpilogCodeInserter());
196
197 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000198 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +0000199
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000200 // Second pass scheduler.
Dale Johannesen72f15962007-07-13 17:31:29 +0000201 if (!Fast)
202 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000203
Chris Lattnere01eaa02006-11-16 01:00:07 +0000204 // Branch folding must be run after regalloc and prolog/epilog insertion.
205 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +0000206 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Chris Lattner47877052006-09-04 04:16:09 +0000207
208 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000209 PM.add(createMachineFunctionPrinterPass(cerr));
210
Evan Cheng8bd60352007-07-20 21:56:13 +0000211 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
Chris Lattner47877052006-09-04 04:16:09 +0000212
213 // Delete machine code for this function
214 PM.add(createMachineCodeDeleter());
215
216 return false; // success!
217}