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 // FIXME: Implement the invoke/unwind instructions!
48 if (!ExceptionHandling)
49 PM.add(createLowerInvokePass(getTargetLowering()));
51 // Make sure that no unreachable blocks are instruction selected.
52 PM.add(createUnreachableBlockEliminationPass());
55 PM.add(createCodeGenPreparePass(getTargetLowering()));
58 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
61 // Ask the target for an isel.
62 if (addInstSelector(PM, Fast))
63 return FileModel::Error;
65 // Print the instruction selected machine code...
67 PM.add(createMachineFunctionPrinterPass(cerr));
69 // Perform register allocation to convert to a concrete x86 representation
70 PM.add(createRegisterAllocator());
73 PM.add(createMachineFunctionPrinterPass(cerr));
75 // Run post-ra passes.
76 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
77 PM.add(createMachineFunctionPrinterPass(cerr));
79 // Insert prolog/epilog code. Eliminate abstract frame index references...
80 PM.add(createPrologEpilogCodeInserter());
82 // Branch folding must be run after regalloc and prolog/epilog insertion.
84 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
86 // Fold redundant debug labels.
87 PM.add(createDebugLabelFoldingPass());
89 if (PrintMachineCode) // Print the register-allocated code
90 PM.add(createMachineFunctionPrinterPass(cerr));
92 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
93 PM.add(createMachineFunctionPrinterPass(cerr));
98 case TargetMachine::AssemblyFile:
99 if (addAssemblyEmitter(PM, Fast, Out))
100 return FileModel::Error;
101 return FileModel::AsmFile;
102 case TargetMachine::ObjectFile:
103 if (getMachOWriterInfo())
104 return FileModel::MachOFile;
105 else if (getELFWriterInfo())
106 return FileModel::ElfFile;
109 return FileModel::Error;
112 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to
113 /// be split up (e.g., to add an object writer pass), this method can be used to
114 /// finish up adding passes to emit the file, if necessary.
115 bool LLVMTargetMachine::addPassesToEmitFileFinish(FunctionPassManager &PM,
116 MachineCodeEmitter *MCE,
119 addSimpleCodeEmitter(PM, Fast, *MCE);
121 // Delete machine code for this function
122 PM.add(createMachineCodeDeleter());
124 return false; // success!
127 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
128 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
129 /// actually outputting the machine code and resolving things like the address
130 /// of functions. This method should returns true if machine code emission is
133 bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
134 MachineCodeEmitter &MCE,
136 // Standard LLVM-Level Passes.
138 // Run loop strength reduction before anything else.
140 PM.add(createLoopStrengthReducePass(getTargetLowering()));
142 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
145 // FIXME: Implement efficient support for garbage collection intrinsics.
146 PM.add(createLowerGCPass());
148 // FIXME: Implement the invoke/unwind instructions!
149 PM.add(createLowerInvokePass(getTargetLowering()));
151 // Make sure that no unreachable blocks are instruction selected.
152 PM.add(createUnreachableBlockEliminationPass());
155 PM.add(createCodeGenPreparePass(getTargetLowering()));
158 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
161 // Ask the target for an isel.
162 if (addInstSelector(PM, Fast))
165 // Print the instruction selected machine code...
166 if (PrintMachineCode)
167 PM.add(createMachineFunctionPrinterPass(cerr));
169 // Perform register allocation to convert to a concrete x86 representation
170 PM.add(createRegisterAllocator());
172 if (PrintMachineCode)
173 PM.add(createMachineFunctionPrinterPass(cerr));
175 // Run post-ra passes.
176 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
177 PM.add(createMachineFunctionPrinterPass(cerr));
179 // Insert prolog/epilog code. Eliminate abstract frame index references...
180 PM.add(createPrologEpilogCodeInserter());
182 if (PrintMachineCode) // Print the register-allocated code
183 PM.add(createMachineFunctionPrinterPass(cerr));
185 // Branch folding must be run after regalloc and prolog/epilog insertion.
187 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
189 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
190 PM.add(createMachineFunctionPrinterPass(cerr));
192 addCodeEmitter(PM, Fast, MCE);
194 // Delete machine code for this function
195 PM.add(createMachineCodeDeleter());
197 return false; // success!