blob: 0098a42dc91442426bdce502db7043dfba0d1e03 [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"
17#include "llvm/CodeGen/Passes.h"
18#include "llvm/Target/TargetOptions.h"
19#include "llvm/Transforms/Scalar.h"
Chris Lattner47877052006-09-04 04:16:09 +000020using namespace llvm;
21
22bool LLVMTargetMachine::addPassesToEmitFile(FunctionPassManager &PM,
23 std::ostream &Out,
24 CodeGenFileType FileType,
25 bool Fast) {
26 // Standard LLVM-Level Passes.
27
28 // Run loop strength reduction before anything else.
29 if (!Fast) PM.add(createLoopStrengthReducePass(getTargetLowering()));
30
31 // FIXME: Implement efficient support for garbage collection intrinsics.
32 PM.add(createLowerGCPass());
33
34 // FIXME: Implement the invoke/unwind instructions!
Duraid Madina2a0013f2006-09-04 06:21:35 +000035 PM.add(createLowerInvokePass(getTargetLowering()));
Chris Lattner47877052006-09-04 04:16:09 +000036
37 // Make sure that no unreachable blocks are instruction selected.
38 PM.add(createUnreachableBlockEliminationPass());
39
40
41 // Ask the target for an isel.
42 if (addInstSelector(PM, Fast))
43 return true;
44
45
46 // Print the instruction selected machine code...
47 if (PrintMachineCode)
Bill Wendlingbcd24982006-12-07 20:28:15 +000048 PM.add(createMachineFunctionPrinterPass(cerr.stream()));
Chris Lattner47877052006-09-04 04:16:09 +000049
50 // Perform register allocation to convert to a concrete x86 representation
51 PM.add(createRegisterAllocator());
52
53 if (PrintMachineCode)
Bill Wendlingbcd24982006-12-07 20:28:15 +000054 PM.add(createMachineFunctionPrinterPass(cerr.stream()));
Chris Lattner47877052006-09-04 04:16:09 +000055
56
57 // Run post-ra passes.
58 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendlingbcd24982006-12-07 20:28:15 +000059 PM.add(createMachineFunctionPrinterPass(cerr.stream()));
Chris Lattner47877052006-09-04 04:16:09 +000060
61
62 // Insert prolog/epilog code. Eliminate abstract frame index references...
63 PM.add(createPrologEpilogCodeInserter());
64
Chris Lattner4a84ad72006-10-13 20:45:56 +000065 // Branch folding must be run after regalloc and prolog/epilog insertion.
Jim Laskey62d07d62006-10-24 16:11:49 +000066 if (!Fast)
67 PM.add(createBranchFoldingPass());
Jim Laskey9d4209f2006-11-07 19:33:46 +000068
69 // Fold redundant debug labels.
70 PM.add(createDebugLabelFoldingPass());
Chris Lattner4a84ad72006-10-13 20:45:56 +000071
Chris Lattner47877052006-09-04 04:16:09 +000072 if (PrintMachineCode) // Print the register-allocated code
Bill Wendlingbcd24982006-12-07 20:28:15 +000073 PM.add(createMachineFunctionPrinterPass(cerr.stream()));
Chris Lattner47877052006-09-04 04:16:09 +000074
75
76 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendlingbcd24982006-12-07 20:28:15 +000077 PM.add(createMachineFunctionPrinterPass(cerr.stream()));
Chris Lattner47877052006-09-04 04:16:09 +000078
79
80 switch (FileType) {
81 default: return true;
82 case TargetMachine::AssemblyFile:
83 if (addAssemblyEmitter(PM, Fast, Out))
84 return true;
85 break;
86 case TargetMachine::ObjectFile:
87 if (addObjectWriter(PM, Fast, Out))
88 return true;
89 break;
90 }
91
92 // Delete machine code for this function
93 PM.add(createMachineCodeDeleter());
94
95 return false; // success!
96}
97
98/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
99/// get machine code emitted. This uses a MachineCodeEmitter object to handle
100/// actually outputting the machine code and resolving things like the address
101/// of functions. This method should returns true if machine code emission is
102/// not supported.
103///
104bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
105 MachineCodeEmitter &MCE,
106 bool Fast) {
107 // Standard LLVM-Level Passes.
108
109 // Run loop strength reduction before anything else.
110 if (!Fast) PM.add(createLoopStrengthReducePass(getTargetLowering()));
111
112 // FIXME: Implement efficient support for garbage collection intrinsics.
113 PM.add(createLowerGCPass());
114
115 // FIXME: Implement the invoke/unwind instructions!
Duraid Madina2a0013f2006-09-04 06:21:35 +0000116 PM.add(createLowerInvokePass(getTargetLowering()));
Chris Lattner47877052006-09-04 04:16:09 +0000117
118 // Make sure that no unreachable blocks are instruction selected.
119 PM.add(createUnreachableBlockEliminationPass());
120
121
122 // Ask the target for an isel.
123 if (addInstSelector(PM, Fast))
124 return true;
125
126
127 // Print the instruction selected machine code...
128 if (PrintMachineCode)
Bill Wendlingbcd24982006-12-07 20:28:15 +0000129 PM.add(createMachineFunctionPrinterPass(cerr.stream()));
Chris Lattner47877052006-09-04 04:16:09 +0000130
131 // Perform register allocation to convert to a concrete x86 representation
132 PM.add(createRegisterAllocator());
133
134 if (PrintMachineCode)
Bill Wendlingbcd24982006-12-07 20:28:15 +0000135 PM.add(createMachineFunctionPrinterPass(cerr.stream()));
Chris Lattner47877052006-09-04 04:16:09 +0000136
137
138 // Run post-ra passes.
139 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendlingbcd24982006-12-07 20:28:15 +0000140 PM.add(createMachineFunctionPrinterPass(cerr.stream()));
Chris Lattner47877052006-09-04 04:16:09 +0000141
142
143 // Insert prolog/epilog code. Eliminate abstract frame index references...
144 PM.add(createPrologEpilogCodeInserter());
145
146 if (PrintMachineCode) // Print the register-allocated code
Bill Wendlingbcd24982006-12-07 20:28:15 +0000147 PM.add(createMachineFunctionPrinterPass(cerr.stream()));
Chris Lattner47877052006-09-04 04:16:09 +0000148
Chris Lattnere01eaa02006-11-16 01:00:07 +0000149 // Branch folding must be run after regalloc and prolog/epilog insertion.
150 if (!Fast)
151 PM.add(createBranchFoldingPass());
Chris Lattner47877052006-09-04 04:16:09 +0000152
153 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendlingbcd24982006-12-07 20:28:15 +0000154 PM.add(createMachineFunctionPrinterPass(cerr.stream()));
Chris Lattner47877052006-09-04 04:16:09 +0000155
156
157 addCodeEmitter(PM, Fast, MCE);
158
159 // Delete machine code for this function
160 PM.add(createMachineCodeDeleter());
161
162 return false; // success!
163}