blob: d7b27bd84d2cf0b0772992384e52bfdda224e8b5 [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"
Devang Patel0f54dcb2007-03-06 21:14:09 +000017#include "llvm/Analysis/LoopPass.h"
Chris Lattner47877052006-09-04 04:16:09 +000018#include "llvm/CodeGen/Passes.h"
19#include "llvm/Target/TargetOptions.h"
20#include "llvm/Transforms/Scalar.h"
Chris Lattner47877052006-09-04 04:16:09 +000021using namespace llvm;
22
Bill Wendling04523ea2007-02-08 01:36:53 +000023FileModel::Model
24LLVMTargetMachine::addPassesToEmitFile(FunctionPassManager &PM,
25 std::ostream &Out,
26 CodeGenFileType FileType,
27 bool Fast) {
Chris Lattner47877052006-09-04 04:16:09 +000028 // Standard LLVM-Level Passes.
29
30 // Run loop strength reduction before anything else.
31 if (!Fast) PM.add(createLoopStrengthReducePass(getTargetLowering()));
32
33 // FIXME: Implement efficient support for garbage collection intrinsics.
34 PM.add(createLowerGCPass());
35
36 // FIXME: Implement the invoke/unwind instructions!
Jim Laskeya4e7cd92007-02-22 16:22:15 +000037 if (!ExceptionHandling)
38 PM.add(createLowerInvokePass(getTargetLowering()));
Chris Lattner47877052006-09-04 04:16:09 +000039
40 // Make sure that no unreachable blocks are instruction selected.
41 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +000042
Chris Lattner47877052006-09-04 04:16:09 +000043 // Ask the target for an isel.
44 if (addInstSelector(PM, Fast))
Bill Wendling04523ea2007-02-08 01:36:53 +000045 return FileModel::Error;
46
Chris Lattner47877052006-09-04 04:16:09 +000047 // Print the instruction selected machine code...
48 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000049 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +000050
51 // Perform register allocation to convert to a concrete x86 representation
52 PM.add(createRegisterAllocator());
53
54 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000055 PM.add(createMachineFunctionPrinterPass(cerr));
56
Chris Lattner47877052006-09-04 04:16:09 +000057 // Run post-ra passes.
58 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000059 PM.add(createMachineFunctionPrinterPass(cerr));
60
Chris Lattner47877052006-09-04 04:16:09 +000061 // Insert prolog/epilog code. Eliminate abstract frame index references...
62 PM.add(createPrologEpilogCodeInserter());
63
Chris Lattner4a84ad72006-10-13 20:45:56 +000064 // Branch folding must be run after regalloc and prolog/epilog insertion.
Jim Laskey62d07d62006-10-24 16:11:49 +000065 if (!Fast)
66 PM.add(createBranchFoldingPass());
Jim Laskey9d4209f2006-11-07 19:33:46 +000067
68 // Fold redundant debug labels.
69 PM.add(createDebugLabelFoldingPass());
Chris Lattner4a84ad72006-10-13 20:45:56 +000070
Chris Lattner47877052006-09-04 04:16:09 +000071 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +000072 PM.add(createMachineFunctionPrinterPass(cerr));
73
Chris Lattner47877052006-09-04 04:16:09 +000074 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000075 PM.add(createMachineFunctionPrinterPass(cerr));
76
Chris Lattner47877052006-09-04 04:16:09 +000077 switch (FileType) {
Bill Wendling04523ea2007-02-08 01:36:53 +000078 default:
79 break;
80 case TargetMachine::AssemblyFile:
81 if (addAssemblyEmitter(PM, Fast, Out))
82 return FileModel::Error;
83 return FileModel::AsmFile;
84 case TargetMachine::ObjectFile:
85 if (getMachOWriterInfo())
86 return FileModel::MachOFile;
87 else if (getELFWriterInfo())
88 return FileModel::ElfFile;
Chris Lattner47877052006-09-04 04:16:09 +000089 }
Bill Wendling04523ea2007-02-08 01:36:53 +000090
91 return FileModel::Error;
92}
93
94/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
95/// be split up (e.g., to add an object writer pass), this method can be used to
96/// finish up adding passes to emit the file, if necessary.
97bool LLVMTargetMachine::addPassesToEmitFileFinish(FunctionPassManager &PM,
98 MachineCodeEmitter *MCE,
99 bool Fast) {
100 if (MCE)
101 addSimpleCodeEmitter(PM, Fast, *MCE);
102
Chris Lattner47877052006-09-04 04:16:09 +0000103 // Delete machine code for this function
104 PM.add(createMachineCodeDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000105
Chris Lattner47877052006-09-04 04:16:09 +0000106 return false; // success!
107}
108
109/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
110/// get machine code emitted. This uses a MachineCodeEmitter object to handle
111/// actually outputting the machine code and resolving things like the address
112/// of functions. This method should returns true if machine code emission is
113/// not supported.
114///
115bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
116 MachineCodeEmitter &MCE,
117 bool Fast) {
118 // Standard LLVM-Level Passes.
119
120 // Run loop strength reduction before anything else.
121 if (!Fast) PM.add(createLoopStrengthReducePass(getTargetLowering()));
122
123 // FIXME: Implement efficient support for garbage collection intrinsics.
124 PM.add(createLowerGCPass());
125
126 // FIXME: Implement the invoke/unwind instructions!
Duraid Madina2a0013f2006-09-04 06:21:35 +0000127 PM.add(createLowerInvokePass(getTargetLowering()));
Chris Lattner47877052006-09-04 04:16:09 +0000128
129 // Make sure that no unreachable blocks are instruction selected.
130 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +0000131
Chris Lattner47877052006-09-04 04:16:09 +0000132 // Ask the target for an isel.
133 if (addInstSelector(PM, Fast))
134 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000135
Chris Lattner47877052006-09-04 04:16:09 +0000136 // Print the instruction selected machine code...
137 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000138 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +0000139
140 // Perform register allocation to convert to a concrete x86 representation
141 PM.add(createRegisterAllocator());
142
143 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000144 PM.add(createMachineFunctionPrinterPass(cerr));
145
Chris Lattner47877052006-09-04 04:16:09 +0000146 // Run post-ra passes.
147 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000148 PM.add(createMachineFunctionPrinterPass(cerr));
149
Chris Lattner47877052006-09-04 04:16:09 +0000150 // Insert prolog/epilog code. Eliminate abstract frame index references...
151 PM.add(createPrologEpilogCodeInserter());
152
153 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000154 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +0000155
Chris Lattnere01eaa02006-11-16 01:00:07 +0000156 // Branch folding must be run after regalloc and prolog/epilog insertion.
157 if (!Fast)
158 PM.add(createBranchFoldingPass());
Chris Lattner47877052006-09-04 04:16:09 +0000159
160 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000161 PM.add(createMachineFunctionPrinterPass(cerr));
162
Chris Lattner47877052006-09-04 04:16:09 +0000163 addCodeEmitter(PM, Fast, MCE);
164
165 // Delete machine code for this function
166 PM.add(createMachineCodeDeleter());
167
168 return false; // success!
169}