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/GCStrategy.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"
25 #include "llvm/Support/raw_ostream.h"
32 static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
33 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
34 static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
35 cl::desc("Print LLVM IR input to isel pass"));
36 static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden,
37 cl::desc("Dump emitter generated instructions as assembly"));
38 static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
39 cl::desc("Dump garbage collector data"));
41 // Hidden options to help debugging
43 EnableSinking("enable-sinking", cl::init(false), cl::Hidden,
44 cl::desc("Perform sinking on machine code"));
46 EnableLICM("machine-licm",
47 cl::init(false), cl::Hidden,
48 cl::desc("Perform loop-invariant code motion on machine code"));
50 // When this works it will be on by default.
52 DisablePostRAScheduler("disable-post-RA-scheduler",
53 cl::desc("Disable scheduling after register allocation"),
56 // Enable or disable FastISel. Both options are needed, because
57 // FastISel is enabled by default with -fast, and we wish to be
58 // able to enable or disable fast-isel independently from -fast.
59 static cl::opt<cl::boolOrDefault>
60 EnableFastISelOption("fast-isel", cl::Hidden,
61 cl::desc("Enable the experimental \"fast\" instruction selector"));
64 LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
66 CodeGenFileType FileType,
68 // Add common CodeGen passes.
69 if (addCommonCodeGenPasses(PM, Fast))
70 return FileModel::Error;
72 // Fold redundant debug labels.
73 PM.add(createDebugLabelFoldingPass());
76 PM.add(createMachineFunctionPrinterPass(cerr));
78 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
79 PM.add(createMachineFunctionPrinterPass(cerr));
82 PM.add(createLoopAlignerPass());
87 case TargetMachine::AssemblyFile:
88 if (addAssemblyEmitter(PM, Fast, Out))
89 return FileModel::Error;
90 return FileModel::AsmFile;
91 case TargetMachine::ObjectFile:
92 if (getMachOWriterInfo())
93 return FileModel::MachOFile;
94 else if (getELFWriterInfo())
95 return FileModel::ElfFile;
98 return FileModel::Error;
101 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to
102 /// be split up (e.g., to add an object writer pass), this method can be used to
103 /// finish up adding passes to emit the file, if necessary.
104 bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
105 MachineCodeEmitter *MCE,
108 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
110 PM.add(createGCInfoDeleter());
112 // Delete machine code for this function
113 PM.add(createMachineCodeDeleter());
115 return false; // success!
118 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
119 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
120 /// actually outputting the machine code and resolving things like the address
121 /// of functions. This method should returns true if machine code emission is
124 bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
125 MachineCodeEmitter &MCE,
127 // Add common CodeGen passes.
128 if (addCommonCodeGenPasses(PM, Fast))
131 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
132 PM.add(createMachineFunctionPrinterPass(cerr));
134 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
136 PM.add(createGCInfoDeleter());
138 // Delete machine code for this function
139 PM.add(createMachineCodeDeleter());
141 return false; // success!
144 /// addCommonCodeGenPasses - Add standard LLVM codegen passes used for
145 /// both emitting to assembly files or machine code output.
147 bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM, bool Fast) {
148 // Standard LLVM-Level Passes.
150 // Run loop strength reduction before anything else.
152 PM.add(createLoopStrengthReducePass(getTargetLowering()));
154 PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &errs()));
157 PM.add(createGCLoweringPass());
159 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
160 PM.add(createLowerInvokePass(getTargetLowering()));
162 // Make sure that no unreachable blocks are instruction selected.
163 PM.add(createUnreachableBlockEliminationPass());
166 PM.add(createCodeGenPreparePass(getTargetLowering()));
168 PM.add(createStackProtectorPass(getTargetLowering()));
171 PM.add(createPrintFunctionPass("\n\n"
172 "*** Final LLVM Code input to ISel ***\n",
175 // Standard Lower-Level Passes.
177 // Enable FastISel with -fast, but allow that to be overridden.
178 if (EnableFastISelOption == cl::BOU_TRUE ||
179 (Fast && EnableFastISelOption != cl::BOU_FALSE))
180 EnableFastISel = true;
182 // Ask the target for an isel.
183 if (addInstSelector(PM, Fast))
186 // Print the instruction selected machine code...
187 if (PrintMachineCode)
188 PM.add(createMachineFunctionPrinterPass(cerr));
191 PM.add(createMachineLICMPass());
194 PM.add(createMachineSinkingPass());
196 // Run pre-ra passes.
197 if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
198 PM.add(createMachineFunctionPrinterPass(cerr));
200 // Perform register allocation.
201 PM.add(createRegisterAllocator());
203 // Perform stack slot coloring.
205 PM.add(createStackSlotColoringPass());
207 if (PrintMachineCode) // Print the register-allocated code
208 PM.add(createMachineFunctionPrinterPass(cerr));
210 // Run post-ra passes.
211 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
212 PM.add(createMachineFunctionPrinterPass(cerr));
214 if (PrintMachineCode)
215 PM.add(createMachineFunctionPrinterPass(cerr));
217 PM.add(createLowerSubregsPass());
219 if (PrintMachineCode) // Print the subreg lowered code
220 PM.add(createMachineFunctionPrinterPass(cerr));
222 // Insert prolog/epilog code. Eliminate abstract frame index references...
223 PM.add(createPrologEpilogCodeInserter());
225 if (PrintMachineCode)
226 PM.add(createMachineFunctionPrinterPass(cerr));
228 // Second pass scheduler.
229 if (!Fast && !DisablePostRAScheduler) {
230 PM.add(createPostRAScheduler());
232 if (PrintMachineCode)
233 PM.add(createMachineFunctionPrinterPass(cerr));
236 // Branch folding must be run after regalloc and prolog/epilog insertion.
238 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
240 if (PrintMachineCode)
241 PM.add(createMachineFunctionPrinterPass(cerr));
243 PM.add(createGCMachineCodeAnalysisPass());
245 if (PrintMachineCode)
246 PM.add(createMachineFunctionPrinterPass(cerr));
249 PM.add(createGCInfoPrinter(*cerr));