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