1 //===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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/Assembly/PrintModulePass.h"
18 #include "llvm/Analysis/LoopPass.h"
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/Target/TargetOptions.h"
21 #include "llvm/Transforms/Scalar.h"
22 #include "llvm/Support/CommandLine.h"
25 static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
26 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
27 static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
28 cl::desc("Print LLVM IR input to isel pass"));
29 static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden,
30 cl::desc("Dump emitter generated instructions as assembly"));
32 // Hidden options to help debugging
34 EnableSinking("enable-sinking", cl::init(false), cl::Hidden,
35 cl::desc("Perform sinking on machine code"));
37 PerformLICM("machine-licm",
38 cl::init(false), cl::Hidden,
39 cl::desc("Perform loop-invariant code motion on machine code"));
42 LLVMTargetMachine::addPassesToEmitFile(FunctionPassManager &PM,
44 CodeGenFileType FileType,
46 // Standard LLVM-Level Passes.
48 // Run loop strength reduction before anything else.
50 PM.add(createLoopStrengthReducePass(getTargetLowering()));
52 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
55 // FIXME: Implement efficient support for garbage collection intrinsics.
56 PM.add(createLowerGCPass());
58 if (!ExceptionHandling)
59 PM.add(createLowerInvokePass(getTargetLowering()));
61 // Make sure that no unreachable blocks are instruction selected.
62 PM.add(createUnreachableBlockEliminationPass());
65 PM.add(createCodeGenPreparePass(getTargetLowering()));
68 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
71 // Ask the target for an isel.
72 if (addInstSelector(PM, Fast))
73 return FileModel::Error;
75 // Print the instruction selected machine code...
77 PM.add(createMachineFunctionPrinterPass(cerr));
80 PM.add(createMachineLICMPass());
83 PM.add(createMachineSinkingPass());
85 // Perform register allocation to convert to a concrete x86 representation
86 PM.add(createRegisterAllocator());
89 PM.add(createMachineFunctionPrinterPass(cerr));
91 PM.add(createLowerSubregsPass());
93 if (PrintMachineCode) // Print the subreg lowered code
94 PM.add(createMachineFunctionPrinterPass(cerr));
96 // Run post-ra passes.
97 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
98 PM.add(createMachineFunctionPrinterPass(cerr));
100 // Insert prolog/epilog code. Eliminate abstract frame index references...
101 PM.add(createPrologEpilogCodeInserter());
103 // Second pass scheduler.
105 PM.add(createPostRAScheduler());
107 // Branch folding must be run after regalloc and prolog/epilog insertion.
109 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
111 // Fold redundant debug labels.
112 PM.add(createDebugLabelFoldingPass());
114 if (PrintMachineCode) // Print the register-allocated code
115 PM.add(createMachineFunctionPrinterPass(cerr));
117 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
118 PM.add(createMachineFunctionPrinterPass(cerr));
123 case TargetMachine::AssemblyFile:
124 if (addAssemblyEmitter(PM, Fast, Out))
125 return FileModel::Error;
126 return FileModel::AsmFile;
127 case TargetMachine::ObjectFile:
128 if (getMachOWriterInfo())
129 return FileModel::MachOFile;
130 else if (getELFWriterInfo())
131 return FileModel::ElfFile;
134 return FileModel::Error;
137 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to
138 /// be split up (e.g., to add an object writer pass), this method can be used to
139 /// finish up adding passes to emit the file, if necessary.
140 bool LLVMTargetMachine::addPassesToEmitFileFinish(FunctionPassManager &PM,
141 MachineCodeEmitter *MCE,
144 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
146 // Delete machine code for this function
147 PM.add(createMachineCodeDeleter());
149 return false; // success!
152 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
153 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
154 /// actually outputting the machine code and resolving things like the address
155 /// of functions. This method should returns true if machine code emission is
158 bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
159 MachineCodeEmitter &MCE,
161 // Standard LLVM-Level Passes.
163 // Run loop strength reduction before anything else.
165 PM.add(createLoopStrengthReducePass(getTargetLowering()));
167 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
170 // FIXME: Implement efficient support for garbage collection intrinsics.
171 PM.add(createLowerGCPass());
173 // FIXME: Implement the invoke/unwind instructions!
174 PM.add(createLowerInvokePass(getTargetLowering()));
176 // Make sure that no unreachable blocks are instruction selected.
177 PM.add(createUnreachableBlockEliminationPass());
180 PM.add(createCodeGenPreparePass(getTargetLowering()));
183 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
186 // Ask the target for an isel.
187 if (addInstSelector(PM, Fast))
190 // Print the instruction selected machine code...
191 if (PrintMachineCode)
192 PM.add(createMachineFunctionPrinterPass(cerr));
195 PM.add(createMachineLICMPass());
198 PM.add(createMachineSinkingPass());
200 // Perform register allocation to convert to a concrete x86 representation
201 PM.add(createRegisterAllocator());
203 if (PrintMachineCode)
204 PM.add(createMachineFunctionPrinterPass(cerr));
206 PM.add(createLowerSubregsPass());
208 if (PrintMachineCode) // Print the subreg lowered code
209 PM.add(createMachineFunctionPrinterPass(cerr));
211 // Run post-ra passes.
212 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
213 PM.add(createMachineFunctionPrinterPass(cerr));
215 // Insert prolog/epilog code. Eliminate abstract frame index references...
216 PM.add(createPrologEpilogCodeInserter());
218 if (PrintMachineCode) // Print the register-allocated code
219 PM.add(createMachineFunctionPrinterPass(cerr));
221 // Second pass scheduler.
223 PM.add(createPostRAScheduler());
225 // Branch folding must be run after regalloc and prolog/epilog insertion.
227 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
229 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
230 PM.add(createMachineFunctionPrinterPass(cerr));
232 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
234 // Delete machine code for this function
235 PM.add(createMachineCodeDeleter());
237 return false; // success!