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 AlignLoops("align-loops", cl::init(true), cl::Hidden,
42 cl::desc("Align loop headers"));
44 PerformLICM("machine-licm",
45 cl::init(false), cl::Hidden,
46 cl::desc("Perform loop-invariant code motion on machine code"));
48 // When this works it will be on by default.
50 DisablePostRAScheduler("disable-post-RA-scheduler",
51 cl::desc("Disable scheduling after register allocation"),
55 LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
57 CodeGenFileType FileType,
59 // Standard LLVM-Level Passes.
61 // Run loop strength reduction before anything else.
63 PM.add(createLoopStrengthReducePass(getTargetLowering()));
65 PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr));
68 PM.add(createGCLoweringPass());
70 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
71 PM.add(createLowerInvokePass(getTargetLowering()));
73 // Make sure that no unreachable blocks are instruction selected.
74 PM.add(createUnreachableBlockEliminationPass());
77 PM.add(createCodeGenPreparePass(getTargetLowering()));
80 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n",
83 // Ask the target for an isel.
84 if (addInstSelector(PM, Fast))
85 return FileModel::Error;
87 // Print the instruction selected machine code...
89 PM.add(createMachineFunctionPrinterPass(cerr));
92 PM.add(createMachineLICMPass());
95 PM.add(createMachineSinkingPass());
98 if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
99 PM.add(createMachineFunctionPrinterPass(cerr));
101 // Perform register allocation to convert to a concrete x86 representation
102 PM.add(createRegisterAllocator());
104 if (PrintMachineCode)
105 PM.add(createMachineFunctionPrinterPass(cerr));
107 PM.add(createLowerSubregsPass());
109 if (PrintMachineCode) // Print the subreg lowered code
110 PM.add(createMachineFunctionPrinterPass(cerr));
112 // Run post-ra passes.
113 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
114 PM.add(createMachineFunctionPrinterPass(cerr));
116 // Insert prolog/epilog code. Eliminate abstract frame index references...
117 PM.add(createPrologEpilogCodeInserter());
119 // Second pass scheduler.
120 if (!Fast && !DisablePostRAScheduler)
121 PM.add(createPostRAScheduler());
123 // Branch folding must be run after regalloc and prolog/epilog insertion.
125 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
127 PM.add(createGCMachineCodeAnalysisPass());
128 if (PrintMachineCode)
129 PM.add(createMachineFunctionPrinterPass(cerr));
132 PM.add(createCollectorMetadataPrinter(*cerr));
134 // Fold redundant debug labels.
135 PM.add(createDebugLabelFoldingPass());
137 if (PrintMachineCode) // Print the register-allocated code
138 PM.add(createMachineFunctionPrinterPass(cerr));
140 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
141 PM.add(createMachineFunctionPrinterPass(cerr));
143 if (AlignLoops && !OptimizeForSize)
144 PM.add(createLoopAlignerPass());
149 case TargetMachine::AssemblyFile:
150 if (addAssemblyEmitter(PM, Fast, Out))
151 return FileModel::Error;
152 return FileModel::AsmFile;
153 case TargetMachine::ObjectFile:
154 if (getMachOWriterInfo())
155 return FileModel::MachOFile;
156 else if (getELFWriterInfo())
157 return FileModel::ElfFile;
160 return FileModel::Error;
163 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to
164 /// be split up (e.g., to add an object writer pass), this method can be used to
165 /// finish up adding passes to emit the file, if necessary.
166 bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
167 MachineCodeEmitter *MCE,
170 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
172 PM.add(createCollectorMetadataDeleter());
174 // Delete machine code for this function
175 PM.add(createMachineCodeDeleter());
177 return false; // success!
180 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
181 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
182 /// actually outputting the machine code and resolving things like the address
183 /// of functions. This method should returns true if machine code emission is
186 bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
187 MachineCodeEmitter &MCE,
189 // Standard LLVM-Level Passes.
191 // Run loop strength reduction before anything else.
193 PM.add(createLoopStrengthReducePass(getTargetLowering()));
195 PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr));
198 PM.add(createGCLoweringPass());
200 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
201 PM.add(createLowerInvokePass(getTargetLowering()));
203 // Make sure that no unreachable blocks are instruction selected.
204 PM.add(createUnreachableBlockEliminationPass());
207 PM.add(createCodeGenPreparePass(getTargetLowering()));
210 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n",
213 // Ask the target for an isel.
214 if (addInstSelector(PM, Fast))
217 // Print the instruction selected machine code...
218 if (PrintMachineCode)
219 PM.add(createMachineFunctionPrinterPass(cerr));
222 PM.add(createMachineLICMPass());
225 PM.add(createMachineSinkingPass());
227 // Run pre-ra passes.
228 if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
229 PM.add(createMachineFunctionPrinterPass(cerr));
231 // Perform register allocation to convert to a concrete x86 representation
232 PM.add(createRegisterAllocator());
234 if (PrintMachineCode)
235 PM.add(createMachineFunctionPrinterPass(cerr));
237 PM.add(createLowerSubregsPass());
239 if (PrintMachineCode) // Print the subreg lowered code
240 PM.add(createMachineFunctionPrinterPass(cerr));
242 // Run post-ra passes.
243 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
244 PM.add(createMachineFunctionPrinterPass(cerr));
246 // Insert prolog/epilog code. Eliminate abstract frame index references...
247 PM.add(createPrologEpilogCodeInserter());
249 if (PrintMachineCode) // Print the register-allocated code
250 PM.add(createMachineFunctionPrinterPass(cerr));
252 // Second pass scheduler.
254 PM.add(createPostRAScheduler());
256 // Branch folding must be run after regalloc and prolog/epilog insertion.
258 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
260 PM.add(createGCMachineCodeAnalysisPass());
261 if (PrintMachineCode)
262 PM.add(createMachineFunctionPrinterPass(cerr));
265 PM.add(createCollectorMetadataPrinter(*cerr));
267 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
268 PM.add(createMachineFunctionPrinterPass(cerr));
270 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
272 PM.add(createCollectorMetadataDeleter());
274 // Delete machine code for this function
275 PM.add(createMachineCodeDeleter());
277 return false; // success!