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