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 EnableStackColoring("stack-coloring",
42 cl::init(true), cl::Hidden,
43 cl::desc("Perform stack slot coloring"));
45 EnableLICM("machine-licm",
46 cl::init(false), cl::Hidden,
47 cl::desc("Perform loop-invariant code motion on machine code"));
49 // When this works it will be on by default.
51 DisablePostRAScheduler("disable-post-RA-scheduler",
52 cl::desc("Disable scheduling after register allocation"),
56 LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
58 CodeGenFileType FileType,
60 // Standard LLVM-Level Passes.
62 // Run loop strength reduction before anything else.
64 PM.add(createLoopStrengthReducePass(getTargetLowering()));
66 PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr));
69 PM.add(createGCLoweringPass());
71 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
72 PM.add(createLowerInvokePass(getTargetLowering()));
74 // Make sure that no unreachable blocks are instruction selected.
75 PM.add(createUnreachableBlockEliminationPass());
78 PM.add(createCodeGenPreparePass(getTargetLowering()));
81 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n",
84 // Ask the target for an isel.
85 if (addInstSelector(PM, Fast))
86 return FileModel::Error;
88 // Print the instruction selected machine code...
90 PM.add(createMachineFunctionPrinterPass(cerr));
93 PM.add(createMachineLICMPass());
96 PM.add(createMachineSinkingPass());
99 if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
100 PM.add(createMachineFunctionPrinterPass(cerr));
102 // Perform register allocation to convert to a concrete x86 representation
103 PM.add(createRegisterAllocator());
105 // Perform stack slot coloring.
106 if (EnableStackColoring)
107 PM.add(createStackSlotColoringPass());
109 if (PrintMachineCode) // Print the register-allocated code
110 PM.add(createMachineFunctionPrinterPass(cerr));
112 // Run post-ra passes.
113 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
114 PM.add(createMachineFunctionPrinterPass(cerr));
116 PM.add(createLowerSubregsPass());
118 if (PrintMachineCode) // Print the subreg lowered code
119 PM.add(createMachineFunctionPrinterPass(cerr));
121 // Insert prolog/epilog code. Eliminate abstract frame index references...
122 PM.add(createPrologEpilogCodeInserter());
124 if (PrintMachineCode)
125 PM.add(createMachineFunctionPrinterPass(cerr));
127 // Second pass scheduler.
128 if (!Fast && !DisablePostRAScheduler)
129 PM.add(createPostRAScheduler());
131 // Branch folding must be run after regalloc and prolog/epilog insertion.
133 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
135 PM.add(createGCMachineCodeAnalysisPass());
136 if (PrintMachineCode)
137 PM.add(createMachineFunctionPrinterPass(cerr));
140 PM.add(createCollectorMetadataPrinter(*cerr));
142 // Fold redundant debug labels.
143 PM.add(createDebugLabelFoldingPass());
145 if (PrintMachineCode) // Print the register-allocated code
146 PM.add(createMachineFunctionPrinterPass(cerr));
148 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
149 PM.add(createMachineFunctionPrinterPass(cerr));
151 if (!Fast && !OptimizeForSize)
152 PM.add(createLoopAlignerPass());
157 case TargetMachine::AssemblyFile:
158 if (addAssemblyEmitter(PM, Fast, Out))
159 return FileModel::Error;
160 return FileModel::AsmFile;
161 case TargetMachine::ObjectFile:
162 if (getMachOWriterInfo())
163 return FileModel::MachOFile;
164 else if (getELFWriterInfo())
165 return FileModel::ElfFile;
168 return FileModel::Error;
171 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to
172 /// be split up (e.g., to add an object writer pass), this method can be used to
173 /// finish up adding passes to emit the file, if necessary.
174 bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
175 MachineCodeEmitter *MCE,
178 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
180 PM.add(createCollectorMetadataDeleter());
182 // Delete machine code for this function
183 PM.add(createMachineCodeDeleter());
185 return false; // success!
188 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
189 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
190 /// actually outputting the machine code and resolving things like the address
191 /// of functions. This method should returns true if machine code emission is
194 bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
195 MachineCodeEmitter &MCE,
197 // Standard LLVM-Level Passes.
199 // Run loop strength reduction before anything else.
201 PM.add(createLoopStrengthReducePass(getTargetLowering()));
203 PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr));
206 PM.add(createGCLoweringPass());
208 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
209 PM.add(createLowerInvokePass(getTargetLowering()));
211 // Make sure that no unreachable blocks are instruction selected.
212 PM.add(createUnreachableBlockEliminationPass());
215 PM.add(createCodeGenPreparePass(getTargetLowering()));
218 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n",
221 // Ask the target for an isel.
222 if (addInstSelector(PM, Fast))
225 // Print the instruction selected machine code...
226 if (PrintMachineCode)
227 PM.add(createMachineFunctionPrinterPass(cerr));
230 PM.add(createMachineLICMPass());
233 PM.add(createMachineSinkingPass());
235 // Run pre-ra passes.
236 if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
237 PM.add(createMachineFunctionPrinterPass(cerr));
239 // Perform register allocation.
240 PM.add(createRegisterAllocator());
242 // Perform stack slot coloring.
243 if (EnableStackColoring)
244 PM.add(createStackSlotColoringPass());
246 if (PrintMachineCode)
247 PM.add(createMachineFunctionPrinterPass(cerr));
249 // Run post-ra passes.
250 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
251 PM.add(createMachineFunctionPrinterPass(cerr));
253 if (PrintMachineCode) // Print the register-allocated code
254 PM.add(createMachineFunctionPrinterPass(cerr));
256 PM.add(createLowerSubregsPass());
258 if (PrintMachineCode) // Print the subreg lowered code
259 PM.add(createMachineFunctionPrinterPass(cerr));
261 // Insert prolog/epilog code. Eliminate abstract frame index references...
262 PM.add(createPrologEpilogCodeInserter());
264 if (PrintMachineCode)
265 PM.add(createMachineFunctionPrinterPass(cerr));
267 // Second pass scheduler.
269 PM.add(createPostRAScheduler());
271 // Branch folding must be run after regalloc and prolog/epilog insertion.
273 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
275 PM.add(createGCMachineCodeAnalysisPass());
277 if (PrintMachineCode)
278 PM.add(createMachineFunctionPrinterPass(cerr));
281 PM.add(createCollectorMetadataPrinter(*cerr));
283 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
284 PM.add(createMachineFunctionPrinterPass(cerr));
286 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
288 PM.add(createCollectorMetadataDeleter());
290 // Delete machine code for this function
291 PM.add(createMachineCodeDeleter());
293 return false; // success!