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