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