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"));
31 LLVMTargetMachine::addPassesToEmitFile(FunctionPassManager &PM,
33 CodeGenFileType FileType,
35 // Standard LLVM-Level Passes.
37 // Run loop strength reduction before anything else.
39 PM.add(createLoopStrengthReducePass(getTargetLowering()));
41 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
44 // FIXME: Implement efficient support for garbage collection intrinsics.
45 PM.add(createLowerGCPass());
47 if (!ExceptionHandling)
48 PM.add(createLowerInvokePass(getTargetLowering()));
50 // Make sure that no unreachable blocks are instruction selected.
51 PM.add(createUnreachableBlockEliminationPass());
54 PM.add(createCodeGenPreparePass(getTargetLowering()));
57 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
60 // Ask the target for an isel.
61 if (addInstSelector(PM, Fast))
62 return FileModel::Error;
64 // Print the instruction selected machine code...
66 PM.add(createMachineFunctionPrinterPass(cerr));
68 // Perform register allocation to convert to a concrete x86 representation
69 PM.add(createRegisterAllocator());
72 PM.add(createMachineFunctionPrinterPass(cerr));
74 // Run post-ra passes.
75 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
76 PM.add(createMachineFunctionPrinterPass(cerr));
78 // Insert prolog/epilog code. Eliminate abstract frame index references...
79 PM.add(createPrologEpilogCodeInserter());
81 // Second pass scheduler.
83 PM.add(createPostRAScheduler());
85 // Branch folding must be run after regalloc and prolog/epilog insertion.
87 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
89 // Fold redundant debug labels.
90 PM.add(createDebugLabelFoldingPass());
92 if (PrintMachineCode) // Print the register-allocated code
93 PM.add(createMachineFunctionPrinterPass(cerr));
95 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
96 PM.add(createMachineFunctionPrinterPass(cerr));
101 case TargetMachine::AssemblyFile:
102 if (addAssemblyEmitter(PM, Fast, Out))
103 return FileModel::Error;
104 return FileModel::AsmFile;
105 case TargetMachine::ObjectFile:
106 if (getMachOWriterInfo())
107 return FileModel::MachOFile;
108 else if (getELFWriterInfo())
109 return FileModel::ElfFile;
112 return FileModel::Error;
115 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to
116 /// be split up (e.g., to add an object writer pass), this method can be used to
117 /// finish up adding passes to emit the file, if necessary.
118 bool LLVMTargetMachine::addPassesToEmitFileFinish(FunctionPassManager &PM,
119 MachineCodeEmitter *MCE,
122 addSimpleCodeEmitter(PM, Fast, *MCE);
124 // Delete machine code for this function
125 PM.add(createMachineCodeDeleter());
127 return false; // success!
130 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
131 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
132 /// actually outputting the machine code and resolving things like the address
133 /// of functions. This method should returns true if machine code emission is
136 bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
137 MachineCodeEmitter &MCE,
139 // Standard LLVM-Level Passes.
141 // Run loop strength reduction before anything else.
143 PM.add(createLoopStrengthReducePass(getTargetLowering()));
145 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
148 // FIXME: Implement efficient support for garbage collection intrinsics.
149 PM.add(createLowerGCPass());
151 // FIXME: Implement the invoke/unwind instructions!
152 PM.add(createLowerInvokePass(getTargetLowering()));
154 // Make sure that no unreachable blocks are instruction selected.
155 PM.add(createUnreachableBlockEliminationPass());
158 PM.add(createCodeGenPreparePass(getTargetLowering()));
161 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
164 // Ask the target for an isel.
165 if (addInstSelector(PM, Fast))
168 // Print the instruction selected machine code...
169 if (PrintMachineCode)
170 PM.add(createMachineFunctionPrinterPass(cerr));
172 // Perform register allocation to convert to a concrete x86 representation
173 PM.add(createRegisterAllocator());
175 if (PrintMachineCode)
176 PM.add(createMachineFunctionPrinterPass(cerr));
178 // Run post-ra passes.
179 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
180 PM.add(createMachineFunctionPrinterPass(cerr));
182 // Insert prolog/epilog code. Eliminate abstract frame index references...
183 PM.add(createPrologEpilogCodeInserter());
185 if (PrintMachineCode) // Print the register-allocated code
186 PM.add(createMachineFunctionPrinterPass(cerr));
188 // Second pass scheduler.
190 PM.add(createPostRAScheduler());
192 // Branch folding must be run after regalloc and prolog/epilog insertion.
194 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
196 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
197 PM.add(createMachineFunctionPrinterPass(cerr));
199 addCodeEmitter(PM, Fast, MCE);
201 // Delete machine code for this function
202 PM.add(createMachineCodeDeleter());
204 return false; // success!