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/Analysis/LoopPass.h"
18 #include "llvm/CodeGen/Passes.h"
19 #include "llvm/Target/TargetOptions.h"
20 #include "llvm/Transforms/Scalar.h"
24 LLVMTargetMachine::addPassesToEmitFile(FunctionPassManager &PM,
26 CodeGenFileType FileType,
28 // Standard LLVM-Level Passes.
30 // Run loop strength reduction before anything else.
31 if (!Fast) PM.add(createLoopStrengthReducePass(getTargetLowering()));
33 // FIXME: Implement efficient support for garbage collection intrinsics.
34 PM.add(createLowerGCPass());
36 // FIXME: Implement the invoke/unwind instructions!
37 if (!ExceptionHandling)
38 PM.add(createLowerInvokePass(getTargetLowering()));
40 // Make sure that no unreachable blocks are instruction selected.
41 PM.add(createUnreachableBlockEliminationPass());
43 // Ask the target for an isel.
44 if (addInstSelector(PM, Fast))
45 return FileModel::Error;
47 // Print the instruction selected machine code...
49 PM.add(createMachineFunctionPrinterPass(cerr));
51 // Perform register allocation to convert to a concrete x86 representation
52 PM.add(createRegisterAllocator());
55 PM.add(createMachineFunctionPrinterPass(cerr));
57 // Run post-ra passes.
58 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
59 PM.add(createMachineFunctionPrinterPass(cerr));
61 // Insert prolog/epilog code. Eliminate abstract frame index references...
62 PM.add(createPrologEpilogCodeInserter());
64 // Branch folding must be run after regalloc and prolog/epilog insertion.
66 PM.add(createBranchFoldingPass());
68 // Fold redundant debug labels.
69 PM.add(createDebugLabelFoldingPass());
71 if (PrintMachineCode) // Print the register-allocated code
72 PM.add(createMachineFunctionPrinterPass(cerr));
74 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
75 PM.add(createMachineFunctionPrinterPass(cerr));
80 case TargetMachine::AssemblyFile:
81 if (addAssemblyEmitter(PM, Fast, Out))
82 return FileModel::Error;
83 return FileModel::AsmFile;
84 case TargetMachine::ObjectFile:
85 if (getMachOWriterInfo())
86 return FileModel::MachOFile;
87 else if (getELFWriterInfo())
88 return FileModel::ElfFile;
91 return FileModel::Error;
94 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to
95 /// be split up (e.g., to add an object writer pass), this method can be used to
96 /// finish up adding passes to emit the file, if necessary.
97 bool LLVMTargetMachine::addPassesToEmitFileFinish(FunctionPassManager &PM,
98 MachineCodeEmitter *MCE,
101 addSimpleCodeEmitter(PM, Fast, *MCE);
103 // Delete machine code for this function
104 PM.add(createMachineCodeDeleter());
106 return false; // success!
109 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
110 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
111 /// actually outputting the machine code and resolving things like the address
112 /// of functions. This method should returns true if machine code emission is
115 bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
116 MachineCodeEmitter &MCE,
118 // Standard LLVM-Level Passes.
120 // Run loop strength reduction before anything else.
121 if (!Fast) PM.add(createLoopStrengthReducePass(getTargetLowering()));
123 // FIXME: Implement efficient support for garbage collection intrinsics.
124 PM.add(createLowerGCPass());
126 // FIXME: Implement the invoke/unwind instructions!
127 PM.add(createLowerInvokePass(getTargetLowering()));
129 // Make sure that no unreachable blocks are instruction selected.
130 PM.add(createUnreachableBlockEliminationPass());
132 // Ask the target for an isel.
133 if (addInstSelector(PM, Fast))
136 // Print the instruction selected machine code...
137 if (PrintMachineCode)
138 PM.add(createMachineFunctionPrinterPass(cerr));
140 // Perform register allocation to convert to a concrete x86 representation
141 PM.add(createRegisterAllocator());
143 if (PrintMachineCode)
144 PM.add(createMachineFunctionPrinterPass(cerr));
146 // Run post-ra passes.
147 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
148 PM.add(createMachineFunctionPrinterPass(cerr));
150 // Insert prolog/epilog code. Eliminate abstract frame index references...
151 PM.add(createPrologEpilogCodeInserter());
153 if (PrintMachineCode) // Print the register-allocated code
154 PM.add(createMachineFunctionPrinterPass(cerr));
156 // Branch folding must be run after regalloc and prolog/epilog insertion.
158 PM.add(createBranchFoldingPass());
160 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
161 PM.add(createMachineFunctionPrinterPass(cerr));
163 addCodeEmitter(PM, Fast, MCE);
165 // Delete machine code for this function
166 PM.add(createMachineCodeDeleter());
168 return false; // success!