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