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