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"
22 bool LLVMTargetMachine::addPassesToEmitFile(FunctionPassManager &PM,
24 CodeGenFileType FileType,
26 // Standard LLVM-Level Passes.
28 // Run loop strength reduction before anything else.
29 if (!Fast) PM.add(createLoopStrengthReducePass(getTargetLowering()));
31 // FIXME: Implement efficient support for garbage collection intrinsics.
32 PM.add(createLowerGCPass());
34 // FIXME: Implement the invoke/unwind instructions!
35 PM.add(createLowerInvokePass(getTargetLowering()));
37 // Make sure that no unreachable blocks are instruction selected.
38 PM.add(createUnreachableBlockEliminationPass());
41 // Ask the target for an isel.
42 if (addInstSelector(PM, Fast))
46 // Print the instruction selected machine code...
48 PM.add(createMachineFunctionPrinterPass(cerr.stream()));
50 // Perform register allocation to convert to a concrete x86 representation
51 PM.add(createRegisterAllocator());
54 PM.add(createMachineFunctionPrinterPass(cerr.stream()));
57 // Run post-ra passes.
58 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
59 PM.add(createMachineFunctionPrinterPass(cerr.stream()));
62 // Insert prolog/epilog code. Eliminate abstract frame index references...
63 PM.add(createPrologEpilogCodeInserter());
65 // Branch folding must be run after regalloc and prolog/epilog insertion.
67 PM.add(createBranchFoldingPass());
69 // Fold redundant debug labels.
70 PM.add(createDebugLabelFoldingPass());
72 if (PrintMachineCode) // Print the register-allocated code
73 PM.add(createMachineFunctionPrinterPass(cerr.stream()));
76 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
77 PM.add(createMachineFunctionPrinterPass(cerr.stream()));
82 case TargetMachine::AssemblyFile:
83 if (addAssemblyEmitter(PM, Fast, Out))
86 case TargetMachine::ObjectFile:
87 if (addObjectWriter(PM, Fast, Out))
92 // Delete machine code for this function
93 PM.add(createMachineCodeDeleter());
95 return false; // success!
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
104 bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
105 MachineCodeEmitter &MCE,
107 // Standard LLVM-Level Passes.
109 // Run loop strength reduction before anything else.
110 if (!Fast) PM.add(createLoopStrengthReducePass(getTargetLowering()));
112 // FIXME: Implement efficient support for garbage collection intrinsics.
113 PM.add(createLowerGCPass());
115 // FIXME: Implement the invoke/unwind instructions!
116 PM.add(createLowerInvokePass(getTargetLowering()));
118 // Make sure that no unreachable blocks are instruction selected.
119 PM.add(createUnreachableBlockEliminationPass());
122 // Ask the target for an isel.
123 if (addInstSelector(PM, Fast))
127 // Print the instruction selected machine code...
128 if (PrintMachineCode)
129 PM.add(createMachineFunctionPrinterPass(cerr.stream()));
131 // Perform register allocation to convert to a concrete x86 representation
132 PM.add(createRegisterAllocator());
134 if (PrintMachineCode)
135 PM.add(createMachineFunctionPrinterPass(cerr.stream()));
138 // Run post-ra passes.
139 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
140 PM.add(createMachineFunctionPrinterPass(cerr.stream()));
143 // Insert prolog/epilog code. Eliminate abstract frame index references...
144 PM.add(createPrologEpilogCodeInserter());
146 if (PrintMachineCode) // Print the register-allocated code
147 PM.add(createMachineFunctionPrinterPass(cerr.stream()));
149 // Branch folding must be run after regalloc and prolog/epilog insertion.
151 PM.add(createBranchFoldingPass());
153 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
154 PM.add(createMachineFunctionPrinterPass(cerr.stream()));
157 addCodeEmitter(PM, Fast, MCE);
159 // Delete machine code for this function
160 PM.add(createMachineCodeDeleter());
162 return false; // success!