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/Collector.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"
27 static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
28 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
29 static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
30 cl::desc("Print LLVM IR input to isel pass"));
31 static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden,
32 cl::desc("Dump emitter generated instructions as assembly"));
33 static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
34 cl::desc("Dump garbage collector data"));
36 // Hidden options to help debugging
38 EnableSinking("enable-sinking", cl::init(false), cl::Hidden,
39 cl::desc("Perform sinking on machine code"));
41 EnableLICM("machine-licm",
42 cl::init(false), cl::Hidden,
43 cl::desc("Perform loop-invariant code motion on machine code"));
45 // When this works it will be on by default.
47 DisablePostRAScheduler("disable-post-RA-scheduler",
48 cl::desc("Disable scheduling after register allocation"),
52 LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
54 CodeGenFileType FileType,
56 // Standard LLVM-Level Passes.
58 // Run loop strength reduction before anything else.
60 PM.add(createLoopStrengthReducePass(getTargetLowering()));
62 PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr));
65 PM.add(createGCLoweringPass());
67 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
68 PM.add(createLowerInvokePass(getTargetLowering()));
70 // Make sure that no unreachable blocks are instruction selected.
71 PM.add(createUnreachableBlockEliminationPass());
74 PM.add(createCodeGenPreparePass(getTargetLowering()));
77 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n",
80 // Ask the target for an isel.
81 if (addInstSelector(PM, Fast))
82 return FileModel::Error;
84 // Print the instruction selected machine code...
86 PM.add(createMachineFunctionPrinterPass(cerr));
89 PM.add(createMachineLICMPass());
92 PM.add(createMachineSinkingPass());
95 if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
96 PM.add(createMachineFunctionPrinterPass(cerr));
98 // Perform register allocation to convert to a concrete x86 representation
99 PM.add(createRegisterAllocator());
101 // Perform stack slot coloring.
102 PM.add(createStackSlotColoringPass());
104 if (PrintMachineCode) // Print the register-allocated code
105 PM.add(createMachineFunctionPrinterPass(cerr));
107 // Run post-ra passes.
108 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
109 PM.add(createMachineFunctionPrinterPass(cerr));
111 PM.add(createLowerSubregsPass());
113 if (PrintMachineCode) // Print the subreg lowered code
114 PM.add(createMachineFunctionPrinterPass(cerr));
116 // Insert prolog/epilog code. Eliminate abstract frame index references...
117 PM.add(createPrologEpilogCodeInserter());
119 if (PrintMachineCode)
120 PM.add(createMachineFunctionPrinterPass(cerr));
122 // Second pass scheduler.
123 if (!Fast && !DisablePostRAScheduler)
124 PM.add(createPostRAScheduler());
126 // Branch folding must be run after regalloc and prolog/epilog insertion.
128 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
130 PM.add(createGCMachineCodeAnalysisPass());
131 if (PrintMachineCode)
132 PM.add(createMachineFunctionPrinterPass(cerr));
135 PM.add(createCollectorMetadataPrinter(*cerr));
137 // Fold redundant debug labels.
138 PM.add(createDebugLabelFoldingPass());
140 if (PrintMachineCode) // Print the register-allocated code
141 PM.add(createMachineFunctionPrinterPass(cerr));
143 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
144 PM.add(createMachineFunctionPrinterPass(cerr));
146 if (!Fast && !OptimizeForSize)
147 PM.add(createLoopAlignerPass());
152 case TargetMachine::AssemblyFile:
153 if (addAssemblyEmitter(PM, Fast, Out))
154 return FileModel::Error;
155 return FileModel::AsmFile;
156 case TargetMachine::ObjectFile:
157 if (getMachOWriterInfo())
158 return FileModel::MachOFile;
159 else if (getELFWriterInfo())
160 return FileModel::ElfFile;
163 return FileModel::Error;
166 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to
167 /// be split up (e.g., to add an object writer pass), this method can be used to
168 /// finish up adding passes to emit the file, if necessary.
169 bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
170 MachineCodeEmitter *MCE,
173 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
175 PM.add(createCollectorMetadataDeleter());
177 // Delete machine code for this function
178 PM.add(createMachineCodeDeleter());
180 return false; // success!
183 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
184 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
185 /// actually outputting the machine code and resolving things like the address
186 /// of functions. This method should returns true if machine code emission is
189 bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
190 MachineCodeEmitter &MCE,
192 // Standard LLVM-Level Passes.
194 // Run loop strength reduction before anything else.
196 PM.add(createLoopStrengthReducePass(getTargetLowering()));
198 PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr));
201 PM.add(createGCLoweringPass());
203 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
204 PM.add(createLowerInvokePass(getTargetLowering()));
206 // Make sure that no unreachable blocks are instruction selected.
207 PM.add(createUnreachableBlockEliminationPass());
210 PM.add(createCodeGenPreparePass(getTargetLowering()));
213 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n",
216 // Ask the target for an isel.
217 if (addInstSelector(PM, Fast))
220 // Print the instruction selected machine code...
221 if (PrintMachineCode)
222 PM.add(createMachineFunctionPrinterPass(cerr));
225 PM.add(createMachineLICMPass());
228 PM.add(createMachineSinkingPass());
230 // Run pre-ra passes.
231 if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
232 PM.add(createMachineFunctionPrinterPass(cerr));
234 // Perform register allocation.
235 PM.add(createRegisterAllocator());
237 // Perform stack slot coloring.
238 PM.add(createStackSlotColoringPass());
240 if (PrintMachineCode)
241 PM.add(createMachineFunctionPrinterPass(cerr));
243 // Run post-ra passes.
244 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
245 PM.add(createMachineFunctionPrinterPass(cerr));
247 if (PrintMachineCode) // Print the register-allocated code
248 PM.add(createMachineFunctionPrinterPass(cerr));
250 PM.add(createLowerSubregsPass());
252 if (PrintMachineCode) // Print the subreg lowered code
253 PM.add(createMachineFunctionPrinterPass(cerr));
255 // Insert prolog/epilog code. Eliminate abstract frame index references...
256 PM.add(createPrologEpilogCodeInserter());
258 if (PrintMachineCode)
259 PM.add(createMachineFunctionPrinterPass(cerr));
261 // Second pass scheduler.
263 PM.add(createPostRAScheduler());
265 // Branch folding must be run after regalloc and prolog/epilog insertion.
267 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
269 PM.add(createGCMachineCodeAnalysisPass());
271 if (PrintMachineCode)
272 PM.add(createMachineFunctionPrinterPass(cerr));
275 PM.add(createCollectorMetadataPrinter(*cerr));
277 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
278 PM.add(createMachineFunctionPrinterPass(cerr));
280 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
282 PM.add(createCollectorMetadataDeleter());
284 // Delete machine code for this function
285 PM.add(createMachineCodeDeleter());
287 return false; // success!