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/CodeGen/Passes.h"
18 #include "llvm/Target/TargetOptions.h"
19 #include "llvm/Transforms/Scalar.h"
23 LLVMTargetMachine::addPassesToEmitFile(FunctionPassManager &PM,
25 CodeGenFileType FileType,
27 // Standard LLVM-Level Passes.
29 // Run loop strength reduction before anything else.
30 if (!Fast) PM.add(createLoopStrengthReducePass(getTargetLowering()));
32 // FIXME: Implement efficient support for garbage collection intrinsics.
33 PM.add(createLowerGCPass());
35 // FIXME: Implement the invoke/unwind instructions!
36 if (!ExceptionHandling)
37 PM.add(createLowerInvokePass(getTargetLowering()));
39 // Make sure that no unreachable blocks are instruction selected.
40 PM.add(createUnreachableBlockEliminationPass());
42 // Ask the target for an isel.
43 if (addInstSelector(PM, Fast))
44 return FileModel::Error;
46 // Print the instruction selected machine code...
48 PM.add(createMachineFunctionPrinterPass(cerr));
50 // Perform register allocation to convert to a concrete x86 representation
51 PM.add(createRegisterAllocator());
54 PM.add(createMachineFunctionPrinterPass(cerr));
56 // Run post-ra passes.
57 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
58 PM.add(createMachineFunctionPrinterPass(cerr));
60 // Insert prolog/epilog code. Eliminate abstract frame index references...
61 PM.add(createPrologEpilogCodeInserter());
63 // Branch folding must be run after regalloc and prolog/epilog insertion.
65 PM.add(createBranchFoldingPass());
67 // Fold redundant debug labels.
68 PM.add(createDebugLabelFoldingPass());
70 if (PrintMachineCode) // Print the register-allocated code
71 PM.add(createMachineFunctionPrinterPass(cerr));
73 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
74 PM.add(createMachineFunctionPrinterPass(cerr));
79 case TargetMachine::AssemblyFile:
80 if (addAssemblyEmitter(PM, Fast, Out))
81 return FileModel::Error;
82 return FileModel::AsmFile;
83 case TargetMachine::ObjectFile:
84 if (getMachOWriterInfo())
85 return FileModel::MachOFile;
86 else if (getELFWriterInfo())
87 return FileModel::ElfFile;
90 return FileModel::Error;
93 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to
94 /// be split up (e.g., to add an object writer pass), this method can be used to
95 /// finish up adding passes to emit the file, if necessary.
96 bool LLVMTargetMachine::addPassesToEmitFileFinish(FunctionPassManager &PM,
97 MachineCodeEmitter *MCE,
100 addSimpleCodeEmitter(PM, Fast, *MCE);
102 // Delete machine code for this function
103 PM.add(createMachineCodeDeleter());
105 return false; // success!
108 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
109 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
110 /// actually outputting the machine code and resolving things like the address
111 /// of functions. This method should returns true if machine code emission is
114 bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
115 MachineCodeEmitter &MCE,
117 // Standard LLVM-Level Passes.
119 // Run loop strength reduction before anything else.
120 if (!Fast) PM.add(createLoopStrengthReducePass(getTargetLowering()));
122 // FIXME: Implement efficient support for garbage collection intrinsics.
123 PM.add(createLowerGCPass());
125 // FIXME: Implement the invoke/unwind instructions!
126 PM.add(createLowerInvokePass(getTargetLowering()));
128 // Make sure that no unreachable blocks are instruction selected.
129 PM.add(createUnreachableBlockEliminationPass());
131 // Ask the target for an isel.
132 if (addInstSelector(PM, Fast))
135 // Print the instruction selected machine code...
136 if (PrintMachineCode)
137 PM.add(createMachineFunctionPrinterPass(cerr));
139 // Perform register allocation to convert to a concrete x86 representation
140 PM.add(createRegisterAllocator());
142 if (PrintMachineCode)
143 PM.add(createMachineFunctionPrinterPass(cerr));
145 // Run post-ra passes.
146 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
147 PM.add(createMachineFunctionPrinterPass(cerr));
149 // Insert prolog/epilog code. Eliminate abstract frame index references...
150 PM.add(createPrologEpilogCodeInserter());
152 if (PrintMachineCode) // Print the register-allocated code
153 PM.add(createMachineFunctionPrinterPass(cerr));
155 // Branch folding must be run after regalloc and prolog/epilog insertion.
157 PM.add(createBranchFoldingPass());
159 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
160 PM.add(createMachineFunctionPrinterPass(cerr));
162 addCodeEmitter(PM, Fast, MCE);
164 // Delete machine code for this function
165 PM.add(createMachineCodeDeleter());
167 return false; // success!