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/Transforms/Scalar.h"
23 #include "llvm/Support/CommandLine.h"
26 static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
27 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
28 static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
29 cl::desc("Print LLVM IR input to isel pass"));
30 static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden,
31 cl::desc("Dump emitter generated instructions as assembly"));
32 static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
33 cl::desc("Dump garbage collector data"));
35 // Hidden options to help debugging
37 EnableSinking("enable-sinking", cl::init(false), cl::Hidden,
38 cl::desc("Perform sinking on machine code"));
40 AlignLoops("align-loops", cl::init(true), cl::Hidden,
41 cl::desc("Align loop headers"));
43 PerformLICM("machine-licm",
44 cl::init(false), cl::Hidden,
45 cl::desc("Perform loop-invariant code motion on machine code"));
47 // When this works it will be on by default.
49 DisablePostRAScheduler("disable-post-RA-scheduler",
50 cl::desc("Disable scheduling after register allocation"),
54 LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
56 CodeGenFileType FileType,
58 // Standard LLVM-Level Passes.
60 // Run loop strength reduction before anything else.
62 PM.add(createLoopStrengthReducePass(getTargetLowering()));
64 PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr));
67 PM.add(createGCLoweringPass());
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());
95 // Perform register allocation to convert to a concrete x86 representation
96 PM.add(createRegisterAllocator());
99 PM.add(createMachineFunctionPrinterPass(cerr));
101 PM.add(createLowerSubregsPass());
103 if (PrintMachineCode) // Print the subreg lowered code
104 PM.add(createMachineFunctionPrinterPass(cerr));
106 // Run post-ra passes.
107 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
108 PM.add(createMachineFunctionPrinterPass(cerr));
110 // Insert prolog/epilog code. Eliminate abstract frame index references...
111 PM.add(createPrologEpilogCodeInserter());
113 // Second pass scheduler.
114 if (!Fast && !DisablePostRAScheduler)
115 PM.add(createPostRAScheduler());
117 // Branch folding must be run after regalloc and prolog/epilog insertion.
119 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
121 PM.add(createGCMachineCodeAnalysisPass());
122 if (PrintMachineCode)
123 PM.add(createMachineFunctionPrinterPass(cerr));
126 PM.add(createCollectorMetadataPrinter(*cerr));
128 // Fold redundant debug labels.
129 PM.add(createDebugLabelFoldingPass());
131 if (PrintMachineCode) // Print the register-allocated code
132 PM.add(createMachineFunctionPrinterPass(cerr));
134 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
135 PM.add(createMachineFunctionPrinterPass(cerr));
137 if (AlignLoops && !OptimizeForSize)
138 PM.add(createLoopAlignerPass());
143 case TargetMachine::AssemblyFile:
144 if (addAssemblyEmitter(PM, Fast, Out))
145 return FileModel::Error;
146 return FileModel::AsmFile;
147 case TargetMachine::ObjectFile:
148 if (getMachOWriterInfo())
149 return FileModel::MachOFile;
150 else if (getELFWriterInfo())
151 return FileModel::ElfFile;
154 return FileModel::Error;
157 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to
158 /// be split up (e.g., to add an object writer pass), this method can be used to
159 /// finish up adding passes to emit the file, if necessary.
160 bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
161 MachineCodeEmitter *MCE,
164 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
166 PM.add(createCollectorMetadataDeleter());
168 // Delete machine code for this function
169 PM.add(createMachineCodeDeleter());
171 return false; // success!
174 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
175 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
176 /// actually outputting the machine code and resolving things like the address
177 /// of functions. This method should returns true if machine code emission is
180 bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
181 MachineCodeEmitter &MCE,
183 // Standard LLVM-Level Passes.
185 // Run loop strength reduction before anything else.
187 PM.add(createLoopStrengthReducePass(getTargetLowering()));
189 PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr));
192 PM.add(createGCLoweringPass());
194 PM.add(createLowerInvokePass(getTargetLowering()));
196 // Make sure that no unreachable blocks are instruction selected.
197 PM.add(createUnreachableBlockEliminationPass());
200 PM.add(createCodeGenPreparePass(getTargetLowering()));
203 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n",
206 // Ask the target for an isel.
207 if (addInstSelector(PM, Fast))
210 // Print the instruction selected machine code...
211 if (PrintMachineCode)
212 PM.add(createMachineFunctionPrinterPass(cerr));
215 PM.add(createMachineLICMPass());
218 PM.add(createMachineSinkingPass());
220 // Perform register allocation to convert to a concrete x86 representation
221 PM.add(createRegisterAllocator());
223 if (PrintMachineCode)
224 PM.add(createMachineFunctionPrinterPass(cerr));
226 PM.add(createLowerSubregsPass());
228 if (PrintMachineCode) // Print the subreg lowered code
229 PM.add(createMachineFunctionPrinterPass(cerr));
231 // Run post-ra passes.
232 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
233 PM.add(createMachineFunctionPrinterPass(cerr));
235 // Insert prolog/epilog code. Eliminate abstract frame index references...
236 PM.add(createPrologEpilogCodeInserter());
238 if (PrintMachineCode) // Print the register-allocated code
239 PM.add(createMachineFunctionPrinterPass(cerr));
241 // Second pass scheduler.
243 PM.add(createPostRAScheduler());
245 // Branch folding must be run after regalloc and prolog/epilog insertion.
247 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
249 PM.add(createGCMachineCodeAnalysisPass());
250 if (PrintMachineCode)
251 PM.add(createMachineFunctionPrinterPass(cerr));
254 PM.add(createCollectorMetadataPrinter(*cerr));
256 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
257 PM.add(createMachineFunctionPrinterPass(cerr));
259 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
261 PM.add(createCollectorMetadataDeleter());
263 // Delete machine code for this function
264 PM.add(createMachineCodeDeleter());
266 return false; // success!