1 //===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the LLVMTargetMachine class.
12 //===----------------------------------------------------------------------===//
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"
23 bool LLVMTargetMachine::addPassesToEmitFile(FunctionPassManager &PM,
25 CodeGenFileType FileType,
27 // Standard LLVM-Level Passes.
29 // Run loop strength reduction before anything else.
30 if (!Fast) PM.add(createLoopStrengthReducePass(getTargetLowering()));
32 // FIXME: Implement efficient support for garbage collection intrinsics.
33 PM.add(createLowerGCPass());
35 // FIXME: Implement the invoke/unwind instructions!
36 PM.add(createLowerInvokePass(getTargetLowering()));
38 // Make sure that no unreachable blocks are instruction selected.
39 PM.add(createUnreachableBlockEliminationPass());
42 // Ask the target for an isel.
43 if (addInstSelector(PM, Fast))
47 // Print the instruction selected machine code...
49 PM.add(createMachineFunctionPrinterPass(&std::cerr));
51 // Perform register allocation to convert to a concrete x86 representation
52 PM.add(createRegisterAllocator());
55 PM.add(createMachineFunctionPrinterPass(&std::cerr));
58 // Run post-ra passes.
59 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
60 PM.add(createMachineFunctionPrinterPass(&std::cerr));
63 // Insert prolog/epilog code. Eliminate abstract frame index references...
64 PM.add(createPrologEpilogCodeInserter());
66 if (PrintMachineCode) // Print the register-allocated code
67 PM.add(createMachineFunctionPrinterPass(&std::cerr));
70 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
71 PM.add(createMachineFunctionPrinterPass(&std::cerr));
76 case TargetMachine::AssemblyFile:
77 if (addAssemblyEmitter(PM, Fast, Out))
80 case TargetMachine::ObjectFile:
81 if (addObjectWriter(PM, Fast, Out))
86 // Delete machine code for this function
87 PM.add(createMachineCodeDeleter());
89 return false; // success!
92 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
93 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
94 /// actually outputting the machine code and resolving things like the address
95 /// of functions. This method should returns true if machine code emission is
98 bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
99 MachineCodeEmitter &MCE,
101 // Standard LLVM-Level Passes.
103 // Run loop strength reduction before anything else.
104 if (!Fast) PM.add(createLoopStrengthReducePass(getTargetLowering()));
106 // FIXME: Implement efficient support for garbage collection intrinsics.
107 PM.add(createLowerGCPass());
109 // FIXME: Implement the invoke/unwind instructions!
110 PM.add(createLowerInvokePass(getTargetLowering()));
112 // Make sure that no unreachable blocks are instruction selected.
113 PM.add(createUnreachableBlockEliminationPass());
116 // Ask the target for an isel.
117 if (addInstSelector(PM, Fast))
121 // Print the instruction selected machine code...
122 if (PrintMachineCode)
123 PM.add(createMachineFunctionPrinterPass(&std::cerr));
125 // Perform register allocation to convert to a concrete x86 representation
126 PM.add(createRegisterAllocator());
128 if (PrintMachineCode)
129 PM.add(createMachineFunctionPrinterPass(&std::cerr));
132 // Run post-ra passes.
133 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
134 PM.add(createMachineFunctionPrinterPass(&std::cerr));
137 // Insert prolog/epilog code. Eliminate abstract frame index references...
138 PM.add(createPrologEpilogCodeInserter());
140 if (PrintMachineCode) // Print the register-allocated code
141 PM.add(createMachineFunctionPrinterPass(&std::cerr));
144 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
145 PM.add(createMachineFunctionPrinterPass(&std::cerr));
148 addCodeEmitter(PM, Fast, MCE);
150 // Delete machine code for this function
151 PM.add(createMachineCodeDeleter());
153 return false; // success!