When the allocator rewrite a spill register with new virtual register, it replaces...
[oota-llvm.git] / lib / CodeGen / LLVMTargetMachine.cpp
1 //===-- 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/Assembly/PrintModulePass.h"
18 #include "llvm/Analysis/LoopPass.h"
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/Target/TargetOptions.h"
21 #include "llvm/Transforms/Scalar.h"
22 #include "llvm/Support/CommandLine.h"
23 using namespace llvm;
24
25 static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
26     cl::desc("Print LLVM IR produced by the loop-reduce pass"));
27 static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
28     cl::desc("Print LLVM IR input to isel pass"));
29 static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden,
30     cl::desc("Dump emitter generated instructions as assembly"));
31
32 FileModel::Model
33 LLVMTargetMachine::addPassesToEmitFile(FunctionPassManager &PM,
34                                        std::ostream &Out,
35                                        CodeGenFileType FileType,
36                                        bool Fast) {
37   // Standard LLVM-Level Passes.
38   
39   // Run loop strength reduction before anything else.
40   if (!Fast) {
41     PM.add(createLoopStrengthReducePass(getTargetLowering()));
42     if (PrintLSR)
43       PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
44   }
45   
46   // FIXME: Implement efficient support for garbage collection intrinsics.
47   PM.add(createLowerGCPass());
48
49   if (!ExceptionHandling)
50     PM.add(createLowerInvokePass(getTargetLowering()));
51
52   // Make sure that no unreachable blocks are instruction selected.
53   PM.add(createUnreachableBlockEliminationPass());
54
55   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   
62   // Ask the target for an isel.
63   if (addInstSelector(PM, Fast))
64     return FileModel::Error;
65
66   // Print the instruction selected machine code...
67   if (PrintMachineCode)
68     PM.add(createMachineFunctionPrinterPass(cerr));
69   
70   // Perform register allocation to convert to a concrete x86 representation
71   PM.add(createRegisterAllocator());
72   
73   if (PrintMachineCode)
74     PM.add(createMachineFunctionPrinterPass(cerr));
75     
76   PM.add(createLowerSubregsPass());
77   
78   if (PrintMachineCode)  // Print the subreg lowered code
79     PM.add(createMachineFunctionPrinterPass(cerr));
80
81   // Run post-ra passes.
82   if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
83     PM.add(createMachineFunctionPrinterPass(cerr));
84
85   // Insert prolog/epilog code.  Eliminate abstract frame index references...
86   PM.add(createPrologEpilogCodeInserter());
87   
88   // Second pass scheduler.
89   if (!Fast)
90     PM.add(createPostRAScheduler());
91
92   // Branch folding must be run after regalloc and prolog/epilog insertion.
93   if (!Fast)
94     PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
95     
96   // Fold redundant debug labels.
97   PM.add(createDebugLabelFoldingPass());
98   
99   if (PrintMachineCode)  // Print the register-allocated code
100     PM.add(createMachineFunctionPrinterPass(cerr));
101
102   if (addPreEmitPass(PM, Fast) && PrintMachineCode)
103     PM.add(createMachineFunctionPrinterPass(cerr));
104
105   switch (FileType) {
106   default:
107     break;
108   case TargetMachine::AssemblyFile:
109     if (addAssemblyEmitter(PM, Fast, Out))
110       return FileModel::Error;
111     return FileModel::AsmFile;
112   case TargetMachine::ObjectFile:
113     if (getMachOWriterInfo())
114       return FileModel::MachOFile;
115     else if (getELFWriterInfo())
116       return FileModel::ElfFile;
117   }
118
119   return FileModel::Error;
120 }
121  
122 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to
123 /// be split up (e.g., to add an object writer pass), this method can be used to
124 /// finish up adding passes to emit the file, if necessary.
125 bool LLVMTargetMachine::addPassesToEmitFileFinish(FunctionPassManager &PM,
126                                                   MachineCodeEmitter *MCE,
127                                                   bool Fast) {
128   if (MCE)
129     addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
130
131   // Delete machine code for this function
132   PM.add(createMachineCodeDeleter());
133
134   return false; // success!
135 }
136
137 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
138 /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
139 /// actually outputting the machine code and resolving things like the address
140 /// of functions.  This method should returns true if machine code emission is
141 /// not supported.
142 ///
143 bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
144                                                    MachineCodeEmitter &MCE,
145                                                    bool Fast) {
146   // Standard LLVM-Level Passes.
147   
148   // Run loop strength reduction before anything else.
149   if (!Fast) {
150     PM.add(createLoopStrengthReducePass(getTargetLowering()));
151     if (PrintLSR)
152       PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
153   }
154   
155   // FIXME: Implement efficient support for garbage collection intrinsics.
156   PM.add(createLowerGCPass());
157   
158   // FIXME: Implement the invoke/unwind instructions!
159   PM.add(createLowerInvokePass(getTargetLowering()));
160   
161   // Make sure that no unreachable blocks are instruction selected.
162   PM.add(createUnreachableBlockEliminationPass());
163
164   if (!Fast)
165     PM.add(createCodeGenPreparePass(getTargetLowering()));
166
167   if (PrintISelInput)
168     PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
169                                  &cerr));
170
171   // Ask the target for an isel.
172   if (addInstSelector(PM, Fast))
173     return true;
174
175   // Print the instruction selected machine code...
176   if (PrintMachineCode)
177     PM.add(createMachineFunctionPrinterPass(cerr));
178   
179   // Perform register allocation to convert to a concrete x86 representation
180   PM.add(createRegisterAllocator());
181   
182   if (PrintMachineCode)
183     PM.add(createMachineFunctionPrinterPass(cerr));
184     
185   PM.add(createLowerSubregsPass());
186   
187   if (PrintMachineCode)  // Print the subreg lowered code
188     PM.add(createMachineFunctionPrinterPass(cerr));
189
190   // Run post-ra passes.
191   if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
192     PM.add(createMachineFunctionPrinterPass(cerr));
193
194   // Insert prolog/epilog code.  Eliminate abstract frame index references...
195   PM.add(createPrologEpilogCodeInserter());
196   
197   if (PrintMachineCode)  // Print the register-allocated code
198     PM.add(createMachineFunctionPrinterPass(cerr));
199   
200   // Second pass scheduler.
201   if (!Fast)
202     PM.add(createPostRAScheduler());
203
204   // Branch folding must be run after regalloc and prolog/epilog insertion.
205   if (!Fast)
206     PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
207   
208   if (addPreEmitPass(PM, Fast) && PrintMachineCode)
209     PM.add(createMachineFunctionPrinterPass(cerr));
210
211   addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
212   
213   // Delete machine code for this function
214   PM.add(createMachineCodeDeleter());
215   
216   return false; // success!
217 }