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");
26 static cl::opt<bool> PrintISelInput("print-isel-input");
28 LLVMTargetMachine::addPassesToEmitFile(FunctionPassManager &PM,
30 CodeGenFileType FileType,
32 // Standard LLVM-Level Passes.
34 // Run loop strength reduction before anything else.
36 PM.add(createLoopStrengthReducePass(getTargetLowering()));
38 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
41 // FIXME: Implement efficient support for garbage collection intrinsics.
42 PM.add(createLowerGCPass());
44 // FIXME: Implement the invoke/unwind instructions!
45 if (!ExceptionHandling)
46 PM.add(createLowerInvokePass(getTargetLowering()));
48 // Make sure that no unreachable blocks are instruction selected.
49 PM.add(createUnreachableBlockEliminationPass());
52 PM.add(createCodeGenPreparePass(getTargetLowering()));
55 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
58 // Ask the target for an isel.
59 if (addInstSelector(PM, Fast))
60 return FileModel::Error;
62 // Print the instruction selected machine code...
64 PM.add(createMachineFunctionPrinterPass(cerr));
66 // Perform register allocation to convert to a concrete x86 representation
67 PM.add(createRegisterAllocator());
70 PM.add(createMachineFunctionPrinterPass(cerr));
72 // Run post-ra passes.
73 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
74 PM.add(createMachineFunctionPrinterPass(cerr));
76 // Insert prolog/epilog code. Eliminate abstract frame index references...
77 PM.add(createPrologEpilogCodeInserter());
79 // Branch folding must be run after regalloc and prolog/epilog insertion.
81 PM.add(createBranchFoldingPass());
83 // Fold redundant debug labels.
84 PM.add(createDebugLabelFoldingPass());
86 if (PrintMachineCode) // Print the register-allocated code
87 PM.add(createMachineFunctionPrinterPass(cerr));
89 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
90 PM.add(createMachineFunctionPrinterPass(cerr));
95 case TargetMachine::AssemblyFile:
96 if (addAssemblyEmitter(PM, Fast, Out))
97 return FileModel::Error;
98 return FileModel::AsmFile;
99 case TargetMachine::ObjectFile:
100 if (getMachOWriterInfo())
101 return FileModel::MachOFile;
102 else if (getELFWriterInfo())
103 return FileModel::ElfFile;
106 return FileModel::Error;
109 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to
110 /// be split up (e.g., to add an object writer pass), this method can be used to
111 /// finish up adding passes to emit the file, if necessary.
112 bool LLVMTargetMachine::addPassesToEmitFileFinish(FunctionPassManager &PM,
113 MachineCodeEmitter *MCE,
116 addSimpleCodeEmitter(PM, Fast, *MCE);
118 // Delete machine code for this function
119 PM.add(createMachineCodeDeleter());
121 return false; // success!
124 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
125 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
126 /// actually outputting the machine code and resolving things like the address
127 /// of functions. This method should returns true if machine code emission is
130 bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
131 MachineCodeEmitter &MCE,
133 // Standard LLVM-Level Passes.
135 // Run loop strength reduction before anything else.
137 PM.add(createLoopStrengthReducePass(getTargetLowering()));
139 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
142 // FIXME: Implement efficient support for garbage collection intrinsics.
143 PM.add(createLowerGCPass());
145 // FIXME: Implement the invoke/unwind instructions!
146 PM.add(createLowerInvokePass(getTargetLowering()));
148 // Make sure that no unreachable blocks are instruction selected.
149 PM.add(createUnreachableBlockEliminationPass());
152 PM.add(createCodeGenPreparePass(getTargetLowering()));
155 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
158 // Ask the target for an isel.
159 if (addInstSelector(PM, Fast))
162 // Print the instruction selected machine code...
163 if (PrintMachineCode)
164 PM.add(createMachineFunctionPrinterPass(cerr));
166 // Perform register allocation to convert to a concrete x86 representation
167 PM.add(createRegisterAllocator());
169 if (PrintMachineCode)
170 PM.add(createMachineFunctionPrinterPass(cerr));
172 // Run post-ra passes.
173 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
174 PM.add(createMachineFunctionPrinterPass(cerr));
176 // Insert prolog/epilog code. Eliminate abstract frame index references...
177 PM.add(createPrologEpilogCodeInserter());
179 if (PrintMachineCode) // Print the register-allocated code
180 PM.add(createMachineFunctionPrinterPass(cerr));
182 // Branch folding must be run after regalloc and prolog/epilog insertion.
184 PM.add(createBranchFoldingPass());
186 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
187 PM.add(createMachineFunctionPrinterPass(cerr));
189 addCodeEmitter(PM, Fast, MCE);
191 // Delete machine code for this function
192 PM.add(createMachineCodeDeleter());
194 return false; // success!