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/CodeGen/GCStrategy.h"
21 #include "llvm/Target/TargetOptions.h"
22 #include "llvm/Target/TargetAsmInfo.h"
23 #include "llvm/Transforms/Scalar.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/raw_ostream.h"
28 static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
29 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
30 static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
31 cl::desc("Print LLVM IR input to isel pass"));
32 static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden,
33 cl::desc("Dump emitter generated instructions as assembly"));
34 static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
35 cl::desc("Dump garbage collector data"));
37 // Hidden options to help debugging
39 EnableSinking("enable-sinking", cl::init(false), cl::Hidden,
40 cl::desc("Perform sinking on machine code"));
42 EnableLICM("machine-licm",
43 cl::init(false), cl::Hidden,
44 cl::desc("Perform loop-invariant code motion on machine code"));
46 // When this works it will be on by default.
48 DisablePostRAScheduler("disable-post-RA-scheduler",
49 cl::desc("Disable scheduling after register allocation"),
53 LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
55 CodeGenFileType FileType,
57 // Standard LLVM-Level Passes.
59 // Run loop strength reduction before anything else.
61 PM.add(createLoopStrengthReducePass(getTargetLowering()));
63 PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr));
66 PM.add(createGCLoweringPass());
68 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
69 PM.add(createLowerInvokePass(getTargetLowering()));
71 // Make sure that no unreachable blocks are instruction selected.
72 PM.add(createUnreachableBlockEliminationPass());
75 PM.add(createCodeGenPreparePass(getTargetLowering()));
78 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n",
81 // Ask the target for an isel.
82 if (addInstSelector(PM, Fast))
83 return FileModel::Error;
85 // Print the instruction selected machine code...
87 PM.add(createMachineFunctionPrinterPass(cerr));
90 PM.add(createMachineLICMPass());
93 PM.add(createMachineSinkingPass());
96 if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
97 PM.add(createMachineFunctionPrinterPass(cerr));
99 // Perform register allocation to convert to a concrete x86 representation
100 PM.add(createRegisterAllocator());
102 // Perform stack slot coloring.
104 PM.add(createStackSlotColoringPass());
106 if (PrintMachineCode) // Print the register-allocated code
107 PM.add(createMachineFunctionPrinterPass(cerr));
109 // Run post-ra passes.
110 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
111 PM.add(createMachineFunctionPrinterPass(cerr));
113 PM.add(createLowerSubregsPass());
115 if (PrintMachineCode) // Print the subreg lowered code
116 PM.add(createMachineFunctionPrinterPass(cerr));
118 // Insert prolog/epilog code. Eliminate abstract frame index references...
119 PM.add(createPrologEpilogCodeInserter());
121 if (PrintMachineCode)
122 PM.add(createMachineFunctionPrinterPass(cerr));
124 // Second pass scheduler.
125 if (!Fast && !DisablePostRAScheduler)
126 PM.add(createPostRAScheduler());
128 // Branch folding must be run after regalloc and prolog/epilog insertion.
130 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
132 PM.add(createGCMachineCodeAnalysisPass());
133 if (PrintMachineCode)
134 PM.add(createMachineFunctionPrinterPass(cerr));
137 PM.add(createGCInfoPrinter(*cerr));
139 // Fold redundant debug labels.
140 PM.add(createDebugLabelFoldingPass());
142 if (PrintMachineCode) // Print the register-allocated code
143 PM.add(createMachineFunctionPrinterPass(cerr));
145 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
146 PM.add(createMachineFunctionPrinterPass(cerr));
148 if (!Fast && !OptimizeForSize)
149 PM.add(createLoopAlignerPass());
154 case TargetMachine::AssemblyFile:
155 if (addAssemblyEmitter(PM, Fast, Out))
156 return FileModel::Error;
157 return FileModel::AsmFile;
158 case TargetMachine::ObjectFile:
159 if (getMachOWriterInfo())
160 return FileModel::MachOFile;
161 else if (getELFWriterInfo())
162 return FileModel::ElfFile;
165 return FileModel::Error;
168 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to
169 /// be split up (e.g., to add an object writer pass), this method can be used to
170 /// finish up adding passes to emit the file, if necessary.
171 bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
172 MachineCodeEmitter *MCE,
175 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
177 PM.add(createGCInfoDeleter());
179 // Delete machine code for this function
180 PM.add(createMachineCodeDeleter());
182 return false; // success!
185 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
186 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
187 /// actually outputting the machine code and resolving things like the address
188 /// of functions. This method should returns true if machine code emission is
191 bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
192 MachineCodeEmitter &MCE,
194 // Standard LLVM-Level Passes.
196 // Run loop strength reduction before anything else.
198 PM.add(createLoopStrengthReducePass(getTargetLowering()));
200 PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr));
203 PM.add(createGCLoweringPass());
205 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
206 PM.add(createLowerInvokePass(getTargetLowering()));
208 // Make sure that no unreachable blocks are instruction selected.
209 PM.add(createUnreachableBlockEliminationPass());
212 PM.add(createCodeGenPreparePass(getTargetLowering()));
215 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n",
218 // Ask the target for an isel.
219 if (addInstSelector(PM, Fast))
222 // Print the instruction selected machine code...
223 if (PrintMachineCode)
224 PM.add(createMachineFunctionPrinterPass(cerr));
227 PM.add(createMachineLICMPass());
230 PM.add(createMachineSinkingPass());
232 // Run pre-ra passes.
233 if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
234 PM.add(createMachineFunctionPrinterPass(cerr));
236 // Perform register allocation.
237 PM.add(createRegisterAllocator());
239 // Perform stack slot coloring.
241 PM.add(createStackSlotColoringPass());
243 if (PrintMachineCode)
244 PM.add(createMachineFunctionPrinterPass(cerr));
246 // Run post-ra passes.
247 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
248 PM.add(createMachineFunctionPrinterPass(cerr));
250 if (PrintMachineCode) // Print the register-allocated code
251 PM.add(createMachineFunctionPrinterPass(cerr));
253 PM.add(createLowerSubregsPass());
255 if (PrintMachineCode) // Print the subreg lowered code
256 PM.add(createMachineFunctionPrinterPass(cerr));
258 // Insert prolog/epilog code. Eliminate abstract frame index references...
259 PM.add(createPrologEpilogCodeInserter());
261 if (PrintMachineCode)
262 PM.add(createMachineFunctionPrinterPass(cerr));
264 // Second pass scheduler.
266 PM.add(createPostRAScheduler());
268 // Branch folding must be run after regalloc and prolog/epilog insertion.
270 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
272 PM.add(createGCMachineCodeAnalysisPass());
274 if (PrintMachineCode)
275 PM.add(createMachineFunctionPrinterPass(cerr));
278 PM.add(createGCInfoPrinter(*cerr));
280 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
281 PM.add(createMachineFunctionPrinterPass(cerr));
283 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
285 PM.add(createGCInfoDeleter());
287 // Delete machine code for this function
288 PM.add(createMachineCodeDeleter());
290 return false; // success!