blob: 641c04653a07a2f68b24cf99ad2f201cb5729836 [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");
26
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 Lattner47877052006-09-04 04:16:09 +000051 // Ask the target for an isel.
52 if (addInstSelector(PM, Fast))
Bill Wendling04523ea2007-02-08 01:36:53 +000053 return FileModel::Error;
54
Chris Lattner47877052006-09-04 04:16:09 +000055 // Print the instruction selected machine code...
56 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000057 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +000058
59 // Perform register allocation to convert to a concrete x86 representation
60 PM.add(createRegisterAllocator());
61
62 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000063 PM.add(createMachineFunctionPrinterPass(cerr));
64
Chris Lattner47877052006-09-04 04:16:09 +000065 // Run post-ra passes.
66 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000067 PM.add(createMachineFunctionPrinterPass(cerr));
68
Chris Lattner47877052006-09-04 04:16:09 +000069 // Insert prolog/epilog code. Eliminate abstract frame index references...
70 PM.add(createPrologEpilogCodeInserter());
71
Chris Lattner4a84ad72006-10-13 20:45:56 +000072 // Branch folding must be run after regalloc and prolog/epilog insertion.
Jim Laskey62d07d62006-10-24 16:11:49 +000073 if (!Fast)
74 PM.add(createBranchFoldingPass());
Jim Laskey9d4209f2006-11-07 19:33:46 +000075
76 // Fold redundant debug labels.
77 PM.add(createDebugLabelFoldingPass());
Chris Lattner4a84ad72006-10-13 20:45:56 +000078
Chris Lattner47877052006-09-04 04:16:09 +000079 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +000080 PM.add(createMachineFunctionPrinterPass(cerr));
81
Chris Lattner47877052006-09-04 04:16:09 +000082 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000083 PM.add(createMachineFunctionPrinterPass(cerr));
84
Chris Lattner47877052006-09-04 04:16:09 +000085 switch (FileType) {
Bill Wendling04523ea2007-02-08 01:36:53 +000086 default:
87 break;
88 case TargetMachine::AssemblyFile:
89 if (addAssemblyEmitter(PM, Fast, Out))
90 return FileModel::Error;
91 return FileModel::AsmFile;
92 case TargetMachine::ObjectFile:
93 if (getMachOWriterInfo())
94 return FileModel::MachOFile;
95 else if (getELFWriterInfo())
96 return FileModel::ElfFile;
Chris Lattner47877052006-09-04 04:16:09 +000097 }
Bill Wendling04523ea2007-02-08 01:36:53 +000098
99 return FileModel::Error;
100}
101
102/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
103/// be split up (e.g., to add an object writer pass), this method can be used to
104/// finish up adding passes to emit the file, if necessary.
105bool LLVMTargetMachine::addPassesToEmitFileFinish(FunctionPassManager &PM,
106 MachineCodeEmitter *MCE,
107 bool Fast) {
108 if (MCE)
109 addSimpleCodeEmitter(PM, Fast, *MCE);
110
Chris Lattner47877052006-09-04 04:16:09 +0000111 // Delete machine code for this function
112 PM.add(createMachineCodeDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000113
Chris Lattner47877052006-09-04 04:16:09 +0000114 return false; // success!
115}
116
117/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
118/// get machine code emitted. This uses a MachineCodeEmitter object to handle
119/// actually outputting the machine code and resolving things like the address
120/// of functions. This method should returns true if machine code emission is
121/// not supported.
122///
123bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
124 MachineCodeEmitter &MCE,
125 bool Fast) {
126 // Standard LLVM-Level Passes.
127
128 // Run loop strength reduction before anything else.
129 if (!Fast) PM.add(createLoopStrengthReducePass(getTargetLowering()));
130
131 // FIXME: Implement efficient support for garbage collection intrinsics.
132 PM.add(createLowerGCPass());
133
134 // FIXME: Implement the invoke/unwind instructions!
Duraid Madina2a0013f2006-09-04 06:21:35 +0000135 PM.add(createLowerInvokePass(getTargetLowering()));
Chris Lattner47877052006-09-04 04:16:09 +0000136
137 // Make sure that no unreachable blocks are instruction selected.
138 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +0000139
Chris Lattner47877052006-09-04 04:16:09 +0000140 // Ask the target for an isel.
141 if (addInstSelector(PM, Fast))
142 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000143
Chris Lattner47877052006-09-04 04:16:09 +0000144 // Print the instruction selected machine code...
145 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000146 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +0000147
148 // Perform register allocation to convert to a concrete x86 representation
149 PM.add(createRegisterAllocator());
150
151 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000152 PM.add(createMachineFunctionPrinterPass(cerr));
153
Chris Lattner47877052006-09-04 04:16:09 +0000154 // Run post-ra passes.
155 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000156 PM.add(createMachineFunctionPrinterPass(cerr));
157
Chris Lattner47877052006-09-04 04:16:09 +0000158 // Insert prolog/epilog code. Eliminate abstract frame index references...
159 PM.add(createPrologEpilogCodeInserter());
160
161 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000162 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +0000163
Chris Lattnere01eaa02006-11-16 01:00:07 +0000164 // Branch folding must be run after regalloc and prolog/epilog insertion.
165 if (!Fast)
166 PM.add(createBranchFoldingPass());
Chris Lattner47877052006-09-04 04:16:09 +0000167
168 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000169 PM.add(createMachineFunctionPrinterPass(cerr));
170
Chris Lattner47877052006-09-04 04:16:09 +0000171 addCodeEmitter(PM, Fast, MCE);
172
173 // Delete machine code for this function
174 PM.add(createMachineCodeDeleter());
175
176 return false; // success!
177}