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");
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());
51 // Ask the target for an isel.
52 if (addInstSelector(PM, Fast))
53 return FileModel::Error;
55 // Print the instruction selected machine code...
57 PM.add(createMachineFunctionPrinterPass(cerr));
59 // Perform register allocation to convert to a concrete x86 representation
60 PM.add(createRegisterAllocator());
63 PM.add(createMachineFunctionPrinterPass(cerr));
65 // Run post-ra passes.
66 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
67 PM.add(createMachineFunctionPrinterPass(cerr));
69 // Insert prolog/epilog code. Eliminate abstract frame index references...
70 PM.add(createPrologEpilogCodeInserter());
72 // Branch folding must be run after regalloc and prolog/epilog insertion.
74 PM.add(createBranchFoldingPass());
76 // Fold redundant debug labels.
77 PM.add(createDebugLabelFoldingPass());
79 if (PrintMachineCode) // Print the register-allocated code
80 PM.add(createMachineFunctionPrinterPass(cerr));
82 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
83 PM.add(createMachineFunctionPrinterPass(cerr));
88 case TargetMachine::AssemblyFile:
89 if (addAssemblyEmitter(PM, Fast, Out))
90 return FileModel::Error;
91 return FileModel::AsmFile;
92 case TargetMachine::ObjectFile:
93 if (getMachOWriterInfo())
94 return FileModel::MachOFile;
95 else if (getELFWriterInfo())
96 return FileModel::ElfFile;
99 return FileModel::Error;
102 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to
103 /// be split up (e.g., to add an object writer pass), this method can be used to
104 /// finish up adding passes to emit the file, if necessary.
105 bool LLVMTargetMachine::addPassesToEmitFileFinish(FunctionPassManager &PM,
106 MachineCodeEmitter *MCE,
109 addSimpleCodeEmitter(PM, Fast, *MCE);
111 // Delete machine code for this function
112 PM.add(createMachineCodeDeleter());
114 return false; // success!
117 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
118 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
119 /// actually outputting the machine code and resolving things like the address
120 /// of functions. This method should returns true if machine code emission is
123 bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
124 MachineCodeEmitter &MCE,
126 // Standard LLVM-Level Passes.
128 // Run loop strength reduction before anything else.
129 if (!Fast) PM.add(createLoopStrengthReducePass(getTargetLowering()));
131 // FIXME: Implement efficient support for garbage collection intrinsics.
132 PM.add(createLowerGCPass());
134 // FIXME: Implement the invoke/unwind instructions!
135 PM.add(createLowerInvokePass(getTargetLowering()));
137 // Make sure that no unreachable blocks are instruction selected.
138 PM.add(createUnreachableBlockEliminationPass());
140 // Ask the target for an isel.
141 if (addInstSelector(PM, Fast))
144 // Print the instruction selected machine code...
145 if (PrintMachineCode)
146 PM.add(createMachineFunctionPrinterPass(cerr));
148 // Perform register allocation to convert to a concrete x86 representation
149 PM.add(createRegisterAllocator());
151 if (PrintMachineCode)
152 PM.add(createMachineFunctionPrinterPass(cerr));
154 // Run post-ra passes.
155 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
156 PM.add(createMachineFunctionPrinterPass(cerr));
158 // Insert prolog/epilog code. Eliminate abstract frame index references...
159 PM.add(createPrologEpilogCodeInserter());
161 if (PrintMachineCode) // Print the register-allocated code
162 PM.add(createMachineFunctionPrinterPass(cerr));
164 // Branch folding must be run after regalloc and prolog/epilog insertion.
166 PM.add(createBranchFoldingPass());
168 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
169 PM.add(createMachineFunctionPrinterPass(cerr));
171 addCodeEmitter(PM, Fast, MCE);
173 // Delete machine code for this function
174 PM.add(createMachineCodeDeleter());
176 return false; // success!