blob: 4620abb7211b00afeddde09165f9c40ae848b7d5 [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 Lattner85ef2542007-06-19 05:47:49 +000025static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
26 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
27static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
28 cl::desc("Print LLVM IR input to isel pass"));
Evan Cheng8bd60352007-07-20 21:56:13 +000029static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden,
30 cl::desc("Dump emitter generated instructions as assembly"));
Chris Lattner85ef2542007-06-19 05:47:49 +000031
Bill Wendling04523ea2007-02-08 01:36:53 +000032FileModel::Model
33LLVMTargetMachine::addPassesToEmitFile(FunctionPassManager &PM,
34 std::ostream &Out,
35 CodeGenFileType FileType,
36 bool Fast) {
Chris Lattner47877052006-09-04 04:16:09 +000037 // Standard LLVM-Level Passes.
38
39 // Run loop strength reduction before anything else.
Chris Lattner31442f92007-03-31 00:24:43 +000040 if (!Fast) {
41 PM.add(createLoopStrengthReducePass(getTargetLowering()));
42 if (PrintLSR)
43 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
44 }
Chris Lattner47877052006-09-04 04:16:09 +000045
46 // FIXME: Implement efficient support for garbage collection intrinsics.
47 PM.add(createLowerGCPass());
Duncan Sandsc3751602007-07-11 16:59:20 +000048
Jim Laskeya4e7cd92007-02-22 16:22:15 +000049 if (!ExceptionHandling)
50 PM.add(createLowerInvokePass(getTargetLowering()));
Duncan Sandsc3751602007-07-11 16:59:20 +000051
Chris Lattner47877052006-09-04 04:16:09 +000052 // Make sure that no unreachable blocks are instruction selected.
53 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +000054
Chris Lattnerc8d288f2007-03-31 04:18:03 +000055 if (!Fast)
56 PM.add(createCodeGenPreparePass(getTargetLowering()));
57
58 if (PrintISelInput)
59 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
60 &cerr));
61
Chris Lattner47877052006-09-04 04:16:09 +000062 // Ask the target for an isel.
63 if (addInstSelector(PM, Fast))
Bill Wendling04523ea2007-02-08 01:36:53 +000064 return FileModel::Error;
65
Chris Lattner47877052006-09-04 04:16:09 +000066 // Print the instruction selected machine code...
67 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000068 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +000069
70 // Perform register allocation to convert to a concrete x86 representation
71 PM.add(createRegisterAllocator());
72
73 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000074 PM.add(createMachineFunctionPrinterPass(cerr));
75
Chris Lattner47877052006-09-04 04:16:09 +000076 // Run post-ra passes.
77 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000078 PM.add(createMachineFunctionPrinterPass(cerr));
79
Chris Lattner47877052006-09-04 04:16:09 +000080 // Insert prolog/epilog code. Eliminate abstract frame index references...
81 PM.add(createPrologEpilogCodeInserter());
82
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000083 // Second pass scheduler.
Dale Johannesen72f15962007-07-13 17:31:29 +000084 if (!Fast)
85 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000086
Chris Lattner4a84ad72006-10-13 20:45:56 +000087 // Branch folding must be run after regalloc and prolog/epilog insertion.
Jim Laskey62d07d62006-10-24 16:11:49 +000088 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +000089 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Jim Laskey9d4209f2006-11-07 19:33:46 +000090
91 // Fold redundant debug labels.
92 PM.add(createDebugLabelFoldingPass());
Chris Lattner4a84ad72006-10-13 20:45:56 +000093
Chris Lattner47877052006-09-04 04:16:09 +000094 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +000095 PM.add(createMachineFunctionPrinterPass(cerr));
96
Chris Lattner47877052006-09-04 04:16:09 +000097 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000098 PM.add(createMachineFunctionPrinterPass(cerr));
Christopher Lambbab24742007-07-26 08:18:32 +000099
100 PM.add(createLowerSubregsPass());
Bill Wendling04523ea2007-02-08 01:36:53 +0000101
Chris Lattner47877052006-09-04 04:16:09 +0000102 switch (FileType) {
Bill Wendling04523ea2007-02-08 01:36:53 +0000103 default:
104 break;
105 case TargetMachine::AssemblyFile:
106 if (addAssemblyEmitter(PM, Fast, Out))
107 return FileModel::Error;
108 return FileModel::AsmFile;
109 case TargetMachine::ObjectFile:
110 if (getMachOWriterInfo())
111 return FileModel::MachOFile;
112 else if (getELFWriterInfo())
113 return FileModel::ElfFile;
Chris Lattner47877052006-09-04 04:16:09 +0000114 }
Bill Wendling04523ea2007-02-08 01:36:53 +0000115
116 return FileModel::Error;
117}
118
119/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
120/// be split up (e.g., to add an object writer pass), this method can be used to
121/// finish up adding passes to emit the file, if necessary.
122bool LLVMTargetMachine::addPassesToEmitFileFinish(FunctionPassManager &PM,
123 MachineCodeEmitter *MCE,
124 bool Fast) {
125 if (MCE)
Evan Cheng8bd60352007-07-20 21:56:13 +0000126 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
Bill Wendling04523ea2007-02-08 01:36:53 +0000127
Chris Lattner47877052006-09-04 04:16:09 +0000128 // Delete machine code for this function
129 PM.add(createMachineCodeDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000130
Chris Lattner47877052006-09-04 04:16:09 +0000131 return false; // success!
132}
133
134/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
135/// get machine code emitted. This uses a MachineCodeEmitter object to handle
136/// actually outputting the machine code and resolving things like the address
137/// of functions. This method should returns true if machine code emission is
138/// not supported.
139///
140bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
141 MachineCodeEmitter &MCE,
142 bool Fast) {
143 // Standard LLVM-Level Passes.
144
145 // Run loop strength reduction before anything else.
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000146 if (!Fast) {
147 PM.add(createLoopStrengthReducePass(getTargetLowering()));
148 if (PrintLSR)
149 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
150 }
Chris Lattner47877052006-09-04 04:16:09 +0000151
152 // FIXME: Implement efficient support for garbage collection intrinsics.
153 PM.add(createLowerGCPass());
154
155 // FIXME: Implement the invoke/unwind instructions!
Duraid Madina2a0013f2006-09-04 06:21:35 +0000156 PM.add(createLowerInvokePass(getTargetLowering()));
Chris Lattner47877052006-09-04 04:16:09 +0000157
158 // Make sure that no unreachable blocks are instruction selected.
159 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +0000160
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000161 if (!Fast)
162 PM.add(createCodeGenPreparePass(getTargetLowering()));
163
164 if (PrintISelInput)
165 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
166 &cerr));
167
Chris Lattner47877052006-09-04 04:16:09 +0000168 // Ask the target for an isel.
169 if (addInstSelector(PM, Fast))
170 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000171
Chris Lattner47877052006-09-04 04:16:09 +0000172 // Print the instruction selected machine code...
173 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000174 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +0000175
176 // Perform register allocation to convert to a concrete x86 representation
177 PM.add(createRegisterAllocator());
178
179 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000180 PM.add(createMachineFunctionPrinterPass(cerr));
181
Chris Lattner47877052006-09-04 04:16:09 +0000182 // Run post-ra passes.
183 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000184 PM.add(createMachineFunctionPrinterPass(cerr));
185
Chris Lattner47877052006-09-04 04:16:09 +0000186 // Insert prolog/epilog code. Eliminate abstract frame index references...
187 PM.add(createPrologEpilogCodeInserter());
188
189 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000190 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +0000191
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000192 // Second pass scheduler.
Dale Johannesen72f15962007-07-13 17:31:29 +0000193 if (!Fast)
194 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000195
Chris Lattnere01eaa02006-11-16 01:00:07 +0000196 // Branch folding must be run after regalloc and prolog/epilog insertion.
197 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +0000198 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Chris Lattner47877052006-09-04 04:16:09 +0000199
200 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000201 PM.add(createMachineFunctionPrinterPass(cerr));
Christopher Lambbab24742007-07-26 08:18:32 +0000202
203 PM.add(createLowerSubregsPass());
Bill Wendling04523ea2007-02-08 01:36:53 +0000204
Evan Cheng8bd60352007-07-20 21:56:13 +0000205 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
Chris Lattner47877052006-09-04 04:16:09 +0000206
207 // Delete machine code for this function
208 PM.add(createMachineCodeDeleter());
209
210 return false; // success!
211}