1 //===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source 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"));
33 LLVMTargetMachine::addPassesToEmitFile(FunctionPassManager &PM,
35 CodeGenFileType FileType,
37 // Standard LLVM-Level Passes.
39 // Run loop strength reduction before anything else.
41 PM.add(createLoopStrengthReducePass(getTargetLowering()));
43 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
46 // FIXME: Implement efficient support for garbage collection intrinsics.
47 PM.add(createLowerGCPass());
49 if (!ExceptionHandling)
50 PM.add(createLowerInvokePass(getTargetLowering()));
52 // Make sure that no unreachable blocks are instruction selected.
53 PM.add(createUnreachableBlockEliminationPass());
56 PM.add(createCodeGenPreparePass(getTargetLowering()));
59 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
62 // Ask the target for an isel.
63 if (addInstSelector(PM, Fast))
64 return FileModel::Error;
66 // Print the instruction selected machine code...
68 PM.add(createMachineFunctionPrinterPass(cerr));
70 // Perform register allocation to convert to a concrete x86 representation
71 PM.add(createRegisterAllocator());
74 PM.add(createMachineFunctionPrinterPass(cerr));
76 // Run post-ra passes.
77 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
78 PM.add(createMachineFunctionPrinterPass(cerr));
80 // Insert prolog/epilog code. Eliminate abstract frame index references...
81 PM.add(createPrologEpilogCodeInserter());
83 // Second pass scheduler.
85 PM.add(createPostRAScheduler());
87 // Branch folding must be run after regalloc and prolog/epilog insertion.
89 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
91 // Fold redundant debug labels.
92 PM.add(createDebugLabelFoldingPass());
94 if (PrintMachineCode) // Print the register-allocated code
95 PM.add(createMachineFunctionPrinterPass(cerr));
97 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
98 PM.add(createMachineFunctionPrinterPass(cerr));
100 PM.add(createLowerSubregsPass());
105 case TargetMachine::AssemblyFile:
106 if (addAssemblyEmitter(PM, Fast, Out))
107 return FileModel::Error;
108 return FileModel::AsmFile;
109 case TargetMachine::ObjectFile:
110 if (getMachOWriterInfo())
111 return FileModel::MachOFile;
112 else if (getELFWriterInfo())
113 return FileModel::ElfFile;
116 return FileModel::Error;
119 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to
120 /// be split up (e.g., to add an object writer pass), this method can be used to
121 /// finish up adding passes to emit the file, if necessary.
122 bool LLVMTargetMachine::addPassesToEmitFileFinish(FunctionPassManager &PM,
123 MachineCodeEmitter *MCE,
126 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
128 // Delete machine code for this function
129 PM.add(createMachineCodeDeleter());
131 return false; // success!
134 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
135 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
136 /// actually outputting the machine code and resolving things like the address
137 /// of functions. This method should returns true if machine code emission is
140 bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
141 MachineCodeEmitter &MCE,
143 // Standard LLVM-Level Passes.
145 // Run loop strength reduction before anything else.
147 PM.add(createLoopStrengthReducePass(getTargetLowering()));
149 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
152 // FIXME: Implement efficient support for garbage collection intrinsics.
153 PM.add(createLowerGCPass());
155 // FIXME: Implement the invoke/unwind instructions!
156 PM.add(createLowerInvokePass(getTargetLowering()));
158 // Make sure that no unreachable blocks are instruction selected.
159 PM.add(createUnreachableBlockEliminationPass());
162 PM.add(createCodeGenPreparePass(getTargetLowering()));
165 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
168 // Ask the target for an isel.
169 if (addInstSelector(PM, Fast))
172 // Print the instruction selected machine code...
173 if (PrintMachineCode)
174 PM.add(createMachineFunctionPrinterPass(cerr));
176 // Perform register allocation to convert to a concrete x86 representation
177 PM.add(createRegisterAllocator());
179 if (PrintMachineCode)
180 PM.add(createMachineFunctionPrinterPass(cerr));
182 // Run post-ra passes.
183 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
184 PM.add(createMachineFunctionPrinterPass(cerr));
186 // Insert prolog/epilog code. Eliminate abstract frame index references...
187 PM.add(createPrologEpilogCodeInserter());
189 if (PrintMachineCode) // Print the register-allocated code
190 PM.add(createMachineFunctionPrinterPass(cerr));
192 // Second pass scheduler.
194 PM.add(createPostRAScheduler());
196 // Branch folding must be run after regalloc and prolog/epilog insertion.
198 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
200 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
201 PM.add(createMachineFunctionPrinterPass(cerr));
203 PM.add(createLowerSubregsPass());
205 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
207 // Delete machine code for this function
208 PM.add(createMachineCodeDeleter());
210 return false; // success!