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