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.
103 PM.add(createStackSlotColoringPass());
105 if (PrintMachineCode) // Print the register-allocated code
106 PM.add(createMachineFunctionPrinterPass(cerr));
108 // Run post-ra passes.
109 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
110 PM.add(createMachineFunctionPrinterPass(cerr));
112 PM.add(createLowerSubregsPass());
114 if (PrintMachineCode) // Print the subreg lowered code
115 PM.add(createMachineFunctionPrinterPass(cerr));
117 // Insert prolog/epilog code. Eliminate abstract frame index references...
118 PM.add(createPrologEpilogCodeInserter());
120 if (PrintMachineCode)
121 PM.add(createMachineFunctionPrinterPass(cerr));
123 // Second pass scheduler.
124 if (!Fast && !DisablePostRAScheduler)
125 PM.add(createPostRAScheduler());
127 // Branch folding must be run after regalloc and prolog/epilog insertion.
129 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
131 PM.add(createGCMachineCodeAnalysisPass());
132 if (PrintMachineCode)
133 PM.add(createMachineFunctionPrinterPass(cerr));
136 PM.add(createCollectorMetadataPrinter(*cerr));
138 // Fold redundant debug labels.
139 PM.add(createDebugLabelFoldingPass());
141 if (PrintMachineCode) // Print the register-allocated code
142 PM.add(createMachineFunctionPrinterPass(cerr));
144 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
145 PM.add(createMachineFunctionPrinterPass(cerr));
147 if (!Fast && !OptimizeForSize)
148 PM.add(createLoopAlignerPass());
153 case TargetMachine::AssemblyFile:
154 if (addAssemblyEmitter(PM, Fast, Out))
155 return FileModel::Error;
156 return FileModel::AsmFile;
157 case TargetMachine::ObjectFile:
158 if (getMachOWriterInfo())
159 return FileModel::MachOFile;
160 else if (getELFWriterInfo())
161 return FileModel::ElfFile;
164 return FileModel::Error;
167 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to
168 /// be split up (e.g., to add an object writer pass), this method can be used to
169 /// finish up adding passes to emit the file, if necessary.
170 bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
171 MachineCodeEmitter *MCE,
174 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
176 PM.add(createCollectorMetadataDeleter());
178 // Delete machine code for this function
179 PM.add(createMachineCodeDeleter());
181 return false; // success!
184 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
185 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
186 /// actually outputting the machine code and resolving things like the address
187 /// of functions. This method should returns true if machine code emission is
190 bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
191 MachineCodeEmitter &MCE,
193 // Standard LLVM-Level Passes.
195 // Run loop strength reduction before anything else.
197 PM.add(createLoopStrengthReducePass(getTargetLowering()));
199 PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr));
202 PM.add(createGCLoweringPass());
204 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
205 PM.add(createLowerInvokePass(getTargetLowering()));
207 // Make sure that no unreachable blocks are instruction selected.
208 PM.add(createUnreachableBlockEliminationPass());
211 PM.add(createCodeGenPreparePass(getTargetLowering()));
214 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n",
217 // Ask the target for an isel.
218 if (addInstSelector(PM, Fast))
221 // Print the instruction selected machine code...
222 if (PrintMachineCode)
223 PM.add(createMachineFunctionPrinterPass(cerr));
226 PM.add(createMachineLICMPass());
229 PM.add(createMachineSinkingPass());
231 // Run pre-ra passes.
232 if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
233 PM.add(createMachineFunctionPrinterPass(cerr));
235 // Perform register allocation.
236 PM.add(createRegisterAllocator());
238 // Perform stack slot coloring.
240 PM.add(createStackSlotColoringPass());
242 if (PrintMachineCode)
243 PM.add(createMachineFunctionPrinterPass(cerr));
245 // Run post-ra passes.
246 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
247 PM.add(createMachineFunctionPrinterPass(cerr));
249 if (PrintMachineCode) // Print the register-allocated code
250 PM.add(createMachineFunctionPrinterPass(cerr));
252 PM.add(createLowerSubregsPass());
254 if (PrintMachineCode) // Print the subreg lowered code
255 PM.add(createMachineFunctionPrinterPass(cerr));
257 // Insert prolog/epilog code. Eliminate abstract frame index references...
258 PM.add(createPrologEpilogCodeInserter());
260 if (PrintMachineCode)
261 PM.add(createMachineFunctionPrinterPass(cerr));
263 // Second pass scheduler.
265 PM.add(createPostRAScheduler());
267 // Branch folding must be run after regalloc and prolog/epilog insertion.
269 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
271 PM.add(createGCMachineCodeAnalysisPass());
273 if (PrintMachineCode)
274 PM.add(createMachineFunctionPrinterPass(cerr));
277 PM.add(createCollectorMetadataPrinter(*cerr));
279 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
280 PM.add(createMachineFunctionPrinterPass(cerr));
282 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
284 PM.add(createCollectorMetadataDeleter());
286 // Delete machine code for this function
287 PM.add(createMachineCodeDeleter());
289 return false; // success!