blob: c7f50b72534d186e2b3eb6e84d9fa329206a541f [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
Bill Wendling04523ea2007-02-08 01:36:53 +000022FileModel::Model
23LLVMTargetMachine::addPassesToEmitFile(FunctionPassManager &PM,
24 std::ostream &Out,
25 CodeGenFileType FileType,
26 bool Fast) {
Chris Lattner47877052006-09-04 04:16:09 +000027 // Standard LLVM-Level Passes.
28
29 // Run loop strength reduction before anything else.
30 if (!Fast) PM.add(createLoopStrengthReducePass(getTargetLowering()));
31
32 // FIXME: Implement efficient support for garbage collection intrinsics.
33 PM.add(createLowerGCPass());
34
35 // FIXME: Implement the invoke/unwind instructions!
Duraid Madina2a0013f2006-09-04 06:21:35 +000036 PM.add(createLowerInvokePass(getTargetLowering()));
Chris Lattner47877052006-09-04 04:16:09 +000037
38 // Make sure that no unreachable blocks are instruction selected.
39 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +000040
Chris Lattner47877052006-09-04 04:16:09 +000041 // Ask the target for an isel.
42 if (addInstSelector(PM, Fast))
Bill Wendling04523ea2007-02-08 01:36:53 +000043 return FileModel::Error;
44
Chris Lattner47877052006-09-04 04:16:09 +000045 // Print the instruction selected machine code...
46 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000047 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +000048
49 // Perform register allocation to convert to a concrete x86 representation
50 PM.add(createRegisterAllocator());
51
52 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000053 PM.add(createMachineFunctionPrinterPass(cerr));
54
Chris Lattner47877052006-09-04 04:16:09 +000055 // Run post-ra passes.
56 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000057 PM.add(createMachineFunctionPrinterPass(cerr));
58
Chris Lattner47877052006-09-04 04:16:09 +000059 // Insert prolog/epilog code. Eliminate abstract frame index references...
60 PM.add(createPrologEpilogCodeInserter());
61
Chris Lattner4a84ad72006-10-13 20:45:56 +000062 // Branch folding must be run after regalloc and prolog/epilog insertion.
Jim Laskey62d07d62006-10-24 16:11:49 +000063 if (!Fast)
64 PM.add(createBranchFoldingPass());
Jim Laskey9d4209f2006-11-07 19:33:46 +000065
66 // Fold redundant debug labels.
67 PM.add(createDebugLabelFoldingPass());
Chris Lattner4a84ad72006-10-13 20:45:56 +000068
Chris Lattner47877052006-09-04 04:16:09 +000069 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +000070 PM.add(createMachineFunctionPrinterPass(cerr));
71
Chris Lattner47877052006-09-04 04:16:09 +000072 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000073 PM.add(createMachineFunctionPrinterPass(cerr));
74
Chris Lattner47877052006-09-04 04:16:09 +000075 switch (FileType) {
Bill Wendling04523ea2007-02-08 01:36:53 +000076 default:
77 break;
78 case TargetMachine::AssemblyFile:
79 if (addAssemblyEmitter(PM, Fast, Out))
80 return FileModel::Error;
81 return FileModel::AsmFile;
82 case TargetMachine::ObjectFile:
83 if (getMachOWriterInfo())
84 return FileModel::MachOFile;
85 else if (getELFWriterInfo())
86 return FileModel::ElfFile;
Chris Lattner47877052006-09-04 04:16:09 +000087 }
Bill Wendling04523ea2007-02-08 01:36:53 +000088
89 return FileModel::Error;
90}
91
92/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
93/// be split up (e.g., to add an object writer pass), this method can be used to
94/// finish up adding passes to emit the file, if necessary.
95bool LLVMTargetMachine::addPassesToEmitFileFinish(FunctionPassManager &PM,
96 MachineCodeEmitter *MCE,
97 bool Fast) {
98 if (MCE)
99 addSimpleCodeEmitter(PM, Fast, *MCE);
100
Chris Lattner47877052006-09-04 04:16:09 +0000101 // Delete machine code for this function
102 PM.add(createMachineCodeDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000103
Chris Lattner47877052006-09-04 04:16:09 +0000104 return false; // success!
105}
106
107/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
108/// get machine code emitted. This uses a MachineCodeEmitter object to handle
109/// actually outputting the machine code and resolving things like the address
110/// of functions. This method should returns true if machine code emission is
111/// not supported.
112///
113bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
114 MachineCodeEmitter &MCE,
115 bool Fast) {
116 // Standard LLVM-Level Passes.
117
118 // Run loop strength reduction before anything else.
119 if (!Fast) PM.add(createLoopStrengthReducePass(getTargetLowering()));
120
121 // FIXME: Implement efficient support for garbage collection intrinsics.
122 PM.add(createLowerGCPass());
123
124 // FIXME: Implement the invoke/unwind instructions!
Duraid Madina2a0013f2006-09-04 06:21:35 +0000125 PM.add(createLowerInvokePass(getTargetLowering()));
Chris Lattner47877052006-09-04 04:16:09 +0000126
127 // Make sure that no unreachable blocks are instruction selected.
128 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +0000129
Chris Lattner47877052006-09-04 04:16:09 +0000130 // Ask the target for an isel.
131 if (addInstSelector(PM, Fast))
132 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000133
Chris Lattner47877052006-09-04 04:16:09 +0000134 // Print the instruction selected machine code...
135 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000136 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +0000137
138 // Perform register allocation to convert to a concrete x86 representation
139 PM.add(createRegisterAllocator());
140
141 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000142 PM.add(createMachineFunctionPrinterPass(cerr));
143
Chris Lattner47877052006-09-04 04:16:09 +0000144 // Run post-ra passes.
145 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000146 PM.add(createMachineFunctionPrinterPass(cerr));
147
Chris Lattner47877052006-09-04 04:16:09 +0000148 // Insert prolog/epilog code. Eliminate abstract frame index references...
149 PM.add(createPrologEpilogCodeInserter());
150
151 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000152 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +0000153
Chris Lattnere01eaa02006-11-16 01:00:07 +0000154 // Branch folding must be run after regalloc and prolog/epilog insertion.
155 if (!Fast)
156 PM.add(createBranchFoldingPass());
Chris Lattner47877052006-09-04 04:16:09 +0000157
158 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000159 PM.add(createMachineFunctionPrinterPass(cerr));
160
Chris Lattner47877052006-09-04 04:16:09 +0000161 addCodeEmitter(PM, Fast, MCE);
162
163 // Delete machine code for this function
164 PM.add(createMachineCodeDeleter());
165
166 return false; // success!
167}