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