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"));
40 static cl::opt<bool> VerifyMachineCode("verify-machineinstrs", cl::Hidden,
41 cl::desc("Verify generated machine code"),
42 cl::init(getenv("LLVM_VERIFY_MACHINEINSTRS")!=NULL));
44 // When this works it will be on by default.
46 DisablePostRAScheduler("disable-post-RA-scheduler",
47 cl::desc("Disable scheduling after register allocation"),
50 // Enable or disable FastISel. Both options are needed, because
51 // FastISel is enabled by default with -fast, and we wish to be
52 // able to enable or disable fast-isel independently from -fast.
53 static cl::opt<cl::boolOrDefault>
54 EnableFastISelOption("fast-isel", cl::Hidden,
55 cl::desc("Enable the experimental \"fast\" instruction selector"));
58 LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
60 CodeGenFileType FileType,
61 CodeGenOpt::Level OptLevel) {
62 // Add common CodeGen passes.
63 if (addCommonCodeGenPasses(PM, OptLevel))
64 return FileModel::Error;
66 // Fold redundant debug labels.
67 PM.add(createDebugLabelFoldingPass());
70 PM.add(createMachineFunctionPrinterPass(cerr));
72 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
73 PM.add(createMachineFunctionPrinterPass(cerr));
75 if (OptLevel != CodeGenOpt::None)
76 PM.add(createCodePlacementOptPass());
81 case TargetMachine::AssemblyFile:
82 if (addAssemblyEmitter(PM, OptLevel, getAsmVerbosityDefault(), Out))
83 return FileModel::Error;
84 return FileModel::AsmFile;
85 case TargetMachine::ObjectFile:
86 if (getMachOWriterInfo())
87 return FileModel::MachOFile;
88 else if (getELFWriterInfo())
89 return FileModel::ElfFile;
92 return FileModel::Error;
95 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to
96 /// be split up (e.g., to add an object writer pass), this method can be used to
97 /// finish up adding passes to emit the file, if necessary.
98 bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
99 MachineCodeEmitter *MCE,
100 CodeGenOpt::Level OptLevel) {
102 addSimpleCodeEmitter(PM, OptLevel, PrintEmittedAsm, *MCE);
104 PM.add(createGCInfoDeleter());
106 // Delete machine code for this function
107 PM.add(createMachineCodeDeleter());
109 return false; // success!
112 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
113 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
114 /// actually outputting the machine code and resolving things like the address
115 /// of functions. This method should returns true if machine code emission is
118 bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
119 MachineCodeEmitter &MCE,
120 CodeGenOpt::Level OptLevel) {
121 // Add common CodeGen passes.
122 if (addCommonCodeGenPasses(PM, OptLevel))
125 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
126 PM.add(createMachineFunctionPrinterPass(cerr));
128 addCodeEmitter(PM, OptLevel, PrintEmittedAsm, MCE);
130 PM.add(createGCInfoDeleter());
132 // Delete machine code for this function
133 PM.add(createMachineCodeDeleter());
135 return false; // success!
138 static void printAndVerify(PassManagerBase &PM,
139 bool allowDoubleDefs = false) {
140 if (PrintMachineCode)
141 PM.add(createMachineFunctionPrinterPass(cerr));
143 if (VerifyMachineCode)
144 PM.add(createMachineVerifierPass(allowDoubleDefs));
147 /// addCommonCodeGenPasses - Add standard LLVM codegen passes used for both
148 /// emitting to assembly files or machine code output.
150 bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM,
151 CodeGenOpt::Level OptLevel) {
152 // Standard LLVM-Level Passes.
154 // Run loop strength reduction before anything else.
155 if (OptLevel != CodeGenOpt::None) {
156 PM.add(createLoopStrengthReducePass(getTargetLowering()));
158 PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &errs()));
161 // Turn exception handling constructs into something the code generators can
163 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
164 PM.add(createLowerInvokePass(getTargetLowering()));
166 PM.add(createDwarfEHPass(getTargetLowering(), OptLevel==CodeGenOpt::None));
168 PM.add(createGCLoweringPass());
170 // Make sure that no unreachable blocks are instruction selected.
171 PM.add(createUnreachableBlockEliminationPass());
173 if (OptLevel != CodeGenOpt::None)
174 PM.add(createCodeGenPreparePass(getTargetLowering()));
176 PM.add(createStackProtectorPass(getTargetLowering()));
179 PM.add(createPrintFunctionPass("\n\n"
180 "*** Final LLVM Code input to ISel ***\n",
183 // Standard Lower-Level Passes.
185 // Enable FastISel with -fast, but allow that to be overridden.
186 if (EnableFastISelOption == cl::BOU_TRUE ||
187 (OptLevel == CodeGenOpt::None && EnableFastISelOption != cl::BOU_FALSE))
188 EnableFastISel = true;
190 // Ask the target for an isel.
191 if (addInstSelector(PM, OptLevel))
194 // Print the instruction selected machine code...
195 printAndVerify(PM, /* allowDoubleDefs= */ true);
197 if (OptLevel != CodeGenOpt::None) {
198 PM.add(createMachineLICMPass());
199 PM.add(createMachineSinkingPass());
200 printAndVerify(PM, /* allowDoubleDefs= */ true);
203 // Run pre-ra passes.
204 if (addPreRegAlloc(PM, OptLevel))
207 // Perform register allocation.
208 PM.add(createRegisterAllocator());
210 // Perform stack slot coloring.
211 if (OptLevel != CodeGenOpt::None)
212 PM.add(createStackSlotColoringPass(OptLevel >= CodeGenOpt::Aggressive));
214 printAndVerify(PM); // Print the register-allocated code
216 // Run post-ra passes.
217 if (addPostRegAlloc(PM, OptLevel))
220 PM.add(createLowerSubregsPass());
223 // Insert prolog/epilog code. Eliminate abstract frame index references...
224 PM.add(createPrologEpilogCodeInserter());
227 // Second pass scheduler.
228 if (OptLevel != CodeGenOpt::None && !DisablePostRAScheduler) {
229 PM.add(createPostRAScheduler());
233 // Branch folding must be run after regalloc and prolog/epilog insertion.
234 if (OptLevel != CodeGenOpt::None) {
235 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
239 PM.add(createGCMachineCodeAnalysisPass());
243 PM.add(createGCInfoPrinter(*cerr));