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/Target/TargetOptions.h"
21 #include "llvm/Transforms/Scalar.h"
22 #include "llvm/Support/CommandLine.h"
25 static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
26 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
27 static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
28 cl::desc("Print LLVM IR input to isel pass"));
29 static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden,
30 cl::desc("Dump emitter generated instructions as assembly"));
32 // Hidden options to help debugging
34 EnableSinking("enable-sinking", cl::init(false), cl::Hidden,
35 cl::desc("Perform sinking on machine code"));
39 LLVMTargetMachine::addPassesToEmitFile(FunctionPassManager &PM,
41 CodeGenFileType FileType,
43 // Standard LLVM-Level Passes.
45 // Run loop strength reduction before anything else.
47 PM.add(createLoopStrengthReducePass(getTargetLowering()));
49 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
52 // FIXME: Implement efficient support for garbage collection intrinsics.
53 PM.add(createLowerGCPass());
55 if (!ExceptionHandling)
56 PM.add(createLowerInvokePass(getTargetLowering()));
58 // Make sure that no unreachable blocks are instruction selected.
59 PM.add(createUnreachableBlockEliminationPass());
62 PM.add(createCodeGenPreparePass(getTargetLowering()));
65 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
68 // Ask the target for an isel.
69 if (addInstSelector(PM, Fast))
70 return FileModel::Error;
72 // Print the instruction selected machine code...
74 PM.add(createMachineFunctionPrinterPass(cerr));
76 PM.add(createMachineLICMPass());
79 PM.add(createMachineSinkingPass());
81 // Perform register allocation to convert to a concrete x86 representation
82 PM.add(createRegisterAllocator());
85 PM.add(createMachineFunctionPrinterPass(cerr));
87 PM.add(createLowerSubregsPass());
89 if (PrintMachineCode) // Print the subreg lowered code
90 PM.add(createMachineFunctionPrinterPass(cerr));
92 // Run post-ra passes.
93 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
94 PM.add(createMachineFunctionPrinterPass(cerr));
96 // Insert prolog/epilog code. Eliminate abstract frame index references...
97 PM.add(createPrologEpilogCodeInserter());
99 // Second pass scheduler.
101 PM.add(createPostRAScheduler());
103 // Branch folding must be run after regalloc and prolog/epilog insertion.
105 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
107 // Fold redundant debug labels.
108 PM.add(createDebugLabelFoldingPass());
110 if (PrintMachineCode) // Print the register-allocated code
111 PM.add(createMachineFunctionPrinterPass(cerr));
113 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
114 PM.add(createMachineFunctionPrinterPass(cerr));
119 case TargetMachine::AssemblyFile:
120 if (addAssemblyEmitter(PM, Fast, Out))
121 return FileModel::Error;
122 return FileModel::AsmFile;
123 case TargetMachine::ObjectFile:
124 if (getMachOWriterInfo())
125 return FileModel::MachOFile;
126 else if (getELFWriterInfo())
127 return FileModel::ElfFile;
130 return FileModel::Error;
133 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to
134 /// be split up (e.g., to add an object writer pass), this method can be used to
135 /// finish up adding passes to emit the file, if necessary.
136 bool LLVMTargetMachine::addPassesToEmitFileFinish(FunctionPassManager &PM,
137 MachineCodeEmitter *MCE,
140 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
142 // Delete machine code for this function
143 PM.add(createMachineCodeDeleter());
145 return false; // success!
148 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
149 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
150 /// actually outputting the machine code and resolving things like the address
151 /// of functions. This method should returns true if machine code emission is
154 bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
155 MachineCodeEmitter &MCE,
157 // Standard LLVM-Level Passes.
159 // Run loop strength reduction before anything else.
161 PM.add(createLoopStrengthReducePass(getTargetLowering()));
163 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
166 // FIXME: Implement efficient support for garbage collection intrinsics.
167 PM.add(createLowerGCPass());
169 // FIXME: Implement the invoke/unwind instructions!
170 PM.add(createLowerInvokePass(getTargetLowering()));
172 // Make sure that no unreachable blocks are instruction selected.
173 PM.add(createUnreachableBlockEliminationPass());
176 PM.add(createCodeGenPreparePass(getTargetLowering()));
179 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
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));
190 PM.add(createMachineLICMPass());
192 // Perform register allocation to convert to a concrete x86 representation
193 PM.add(createRegisterAllocator());
195 if (PrintMachineCode)
196 PM.add(createMachineFunctionPrinterPass(cerr));
198 PM.add(createLowerSubregsPass());
200 if (PrintMachineCode) // Print the subreg lowered code
201 PM.add(createMachineFunctionPrinterPass(cerr));
203 // Run post-ra passes.
204 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
205 PM.add(createMachineFunctionPrinterPass(cerr));
207 // Insert prolog/epilog code. Eliminate abstract frame index references...
208 PM.add(createPrologEpilogCodeInserter());
210 if (PrintMachineCode) // Print the register-allocated code
211 PM.add(createMachineFunctionPrinterPass(cerr));
213 // Second pass scheduler.
215 PM.add(createPostRAScheduler());
217 // Branch folding must be run after regalloc and prolog/epilog insertion.
219 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
221 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
222 PM.add(createMachineFunctionPrinterPass(cerr));
224 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
226 // Delete machine code for this function
227 PM.add(createMachineCodeDeleter());
229 return false; // success!