blob: 3371b7948b6468357fcf130d95ddb35cc836e6ae [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 Lattner31442f92007-03-31 00:24:43 +000025static cl::opt<bool> PrintLSR("print-lsr-output");
Chris Lattnerc8d288f2007-03-31 04:18:03 +000026static cl::opt<bool> PrintISelInput("print-isel-input");
Bill Wendling04523ea2007-02-08 01:36:53 +000027FileModel::Model
28LLVMTargetMachine::addPassesToEmitFile(FunctionPassManager &PM,
29 std::ostream &Out,
30 CodeGenFileType FileType,
31 bool Fast) {
Chris Lattner47877052006-09-04 04:16:09 +000032 // Standard LLVM-Level Passes.
33
34 // Run loop strength reduction before anything else.
Chris Lattner31442f92007-03-31 00:24:43 +000035 if (!Fast) {
36 PM.add(createLoopStrengthReducePass(getTargetLowering()));
37 if (PrintLSR)
38 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
39 }
Chris Lattner47877052006-09-04 04:16:09 +000040
41 // FIXME: Implement efficient support for garbage collection intrinsics.
42 PM.add(createLowerGCPass());
43
44 // FIXME: Implement the invoke/unwind instructions!
Jim Laskeya4e7cd92007-02-22 16:22:15 +000045 if (!ExceptionHandling)
46 PM.add(createLowerInvokePass(getTargetLowering()));
Chris Lattner47877052006-09-04 04:16:09 +000047
48 // Make sure that no unreachable blocks are instruction selected.
49 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +000050
Chris Lattnerc8d288f2007-03-31 04:18:03 +000051 if (!Fast)
52 PM.add(createCodeGenPreparePass(getTargetLowering()));
53
54 if (PrintISelInput)
55 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
56 &cerr));
57
Chris Lattner47877052006-09-04 04:16:09 +000058 // Ask the target for an isel.
59 if (addInstSelector(PM, Fast))
Bill Wendling04523ea2007-02-08 01:36:53 +000060 return FileModel::Error;
61
Chris Lattner47877052006-09-04 04:16:09 +000062 // Print the instruction selected machine code...
63 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000064 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +000065
66 // Perform register allocation to convert to a concrete x86 representation
67 PM.add(createRegisterAllocator());
68
69 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000070 PM.add(createMachineFunctionPrinterPass(cerr));
71
Chris Lattner47877052006-09-04 04:16:09 +000072 // Run post-ra passes.
73 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000074 PM.add(createMachineFunctionPrinterPass(cerr));
75
Chris Lattner47877052006-09-04 04:16:09 +000076 // Insert prolog/epilog code. Eliminate abstract frame index references...
77 PM.add(createPrologEpilogCodeInserter());
78
Chris Lattner4a84ad72006-10-13 20:45:56 +000079 // Branch folding must be run after regalloc and prolog/epilog insertion.
Jim Laskey62d07d62006-10-24 16:11:49 +000080 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +000081 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Jim Laskey9d4209f2006-11-07 19:33:46 +000082
83 // Fold redundant debug labels.
84 PM.add(createDebugLabelFoldingPass());
Chris Lattner4a84ad72006-10-13 20:45:56 +000085
Chris Lattner47877052006-09-04 04:16:09 +000086 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +000087 PM.add(createMachineFunctionPrinterPass(cerr));
88
Chris Lattner47877052006-09-04 04:16:09 +000089 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000090 PM.add(createMachineFunctionPrinterPass(cerr));
91
Chris Lattner47877052006-09-04 04:16:09 +000092 switch (FileType) {
Bill Wendling04523ea2007-02-08 01:36:53 +000093 default:
94 break;
95 case TargetMachine::AssemblyFile:
96 if (addAssemblyEmitter(PM, Fast, Out))
97 return FileModel::Error;
98 return FileModel::AsmFile;
99 case TargetMachine::ObjectFile:
100 if (getMachOWriterInfo())
101 return FileModel::MachOFile;
102 else if (getELFWriterInfo())
103 return FileModel::ElfFile;
Chris Lattner47877052006-09-04 04:16:09 +0000104 }
Bill Wendling04523ea2007-02-08 01:36:53 +0000105
106 return FileModel::Error;
107}
108
109/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
110/// be split up (e.g., to add an object writer pass), this method can be used to
111/// finish up adding passes to emit the file, if necessary.
112bool LLVMTargetMachine::addPassesToEmitFileFinish(FunctionPassManager &PM,
113 MachineCodeEmitter *MCE,
114 bool Fast) {
115 if (MCE)
116 addSimpleCodeEmitter(PM, Fast, *MCE);
117
Chris Lattner47877052006-09-04 04:16:09 +0000118 // Delete machine code for this function
119 PM.add(createMachineCodeDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000120
Chris Lattner47877052006-09-04 04:16:09 +0000121 return false; // success!
122}
123
124/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
125/// get machine code emitted. This uses a MachineCodeEmitter object to handle
126/// actually outputting the machine code and resolving things like the address
127/// of functions. This method should returns true if machine code emission is
128/// not supported.
129///
130bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
131 MachineCodeEmitter &MCE,
132 bool Fast) {
133 // Standard LLVM-Level Passes.
134
135 // Run loop strength reduction before anything else.
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000136 if (!Fast) {
137 PM.add(createLoopStrengthReducePass(getTargetLowering()));
138 if (PrintLSR)
139 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
140 }
Chris Lattner47877052006-09-04 04:16:09 +0000141
142 // FIXME: Implement efficient support for garbage collection intrinsics.
143 PM.add(createLowerGCPass());
144
145 // FIXME: Implement the invoke/unwind instructions!
Duraid Madina2a0013f2006-09-04 06:21:35 +0000146 PM.add(createLowerInvokePass(getTargetLowering()));
Chris Lattner47877052006-09-04 04:16:09 +0000147
148 // Make sure that no unreachable blocks are instruction selected.
149 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +0000150
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000151 if (!Fast)
152 PM.add(createCodeGenPreparePass(getTargetLowering()));
153
154 if (PrintISelInput)
155 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
156 &cerr));
157
Chris Lattner47877052006-09-04 04:16:09 +0000158 // Ask the target for an isel.
159 if (addInstSelector(PM, Fast))
160 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000161
Chris Lattner47877052006-09-04 04:16:09 +0000162 // Print the instruction selected machine code...
163 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000164 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +0000165
166 // Perform register allocation to convert to a concrete x86 representation
167 PM.add(createRegisterAllocator());
168
169 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000170 PM.add(createMachineFunctionPrinterPass(cerr));
171
Chris Lattner47877052006-09-04 04:16:09 +0000172 // Run post-ra passes.
173 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000174 PM.add(createMachineFunctionPrinterPass(cerr));
175
Chris Lattner47877052006-09-04 04:16:09 +0000176 // Insert prolog/epilog code. Eliminate abstract frame index references...
177 PM.add(createPrologEpilogCodeInserter());
178
179 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000180 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +0000181
Chris Lattnere01eaa02006-11-16 01:00:07 +0000182 // Branch folding must be run after regalloc and prolog/epilog insertion.
183 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +0000184 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Chris Lattner47877052006-09-04 04:16:09 +0000185
186 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000187 PM.add(createMachineFunctionPrinterPass(cerr));
188
Chris Lattner47877052006-09-04 04:16:09 +0000189 addCodeEmitter(PM, Fast, MCE);
190
191 // Delete machine code for this function
192 PM.add(createMachineCodeDeleter());
193
194 return false; // success!
195}