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 PM.add(createLowerInvokePass(getTargetLowering()));
38 // Make sure that no unreachable blocks are instruction selected.
39 PM.add(createUnreachableBlockEliminationPass());
41 // Ask the target for an isel.
42 if (addInstSelector(PM, Fast))
43 return FileModel::Error;
45 // Print the instruction selected machine code...
47 PM.add(createMachineFunctionPrinterPass(cerr));
49 // Perform register allocation to convert to a concrete x86 representation
50 PM.add(createRegisterAllocator());
53 PM.add(createMachineFunctionPrinterPass(cerr));
55 // Run post-ra passes.
56 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
57 PM.add(createMachineFunctionPrinterPass(cerr));
59 // Insert prolog/epilog code. Eliminate abstract frame index references...
60 PM.add(createPrologEpilogCodeInserter());
62 // Branch folding must be run after regalloc and prolog/epilog insertion.
64 PM.add(createBranchFoldingPass());
66 // Fold redundant debug labels.
67 PM.add(createDebugLabelFoldingPass());
69 if (PrintMachineCode) // Print the register-allocated code
70 PM.add(createMachineFunctionPrinterPass(cerr));
72 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
73 PM.add(createMachineFunctionPrinterPass(cerr));
78 case TargetMachine::AssemblyFile:
79 if (addAssemblyEmitter(PM, Fast, Out))
80 return FileModel::Error;
81 return FileModel::AsmFile;
82 case TargetMachine::ObjectFile:
83 if (getMachOWriterInfo())
84 return FileModel::MachOFile;
85 else if (getELFWriterInfo())
86 return FileModel::ElfFile;
89 return FileModel::Error;
92 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to
93 /// be split up (e.g., to add an object writer pass), this method can be used to
94 /// finish up adding passes to emit the file, if necessary.
95 bool LLVMTargetMachine::addPassesToEmitFileFinish(FunctionPassManager &PM,
96 MachineCodeEmitter *MCE,
99 addSimpleCodeEmitter(PM, Fast, *MCE);
101 // Delete machine code for this function
102 PM.add(createMachineCodeDeleter());
104 return false; // success!
107 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
108 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
109 /// actually outputting the machine code and resolving things like the address
110 /// of functions. This method should returns true if machine code emission is
113 bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
114 MachineCodeEmitter &MCE,
116 // Standard LLVM-Level Passes.
118 // Run loop strength reduction before anything else.
119 if (!Fast) PM.add(createLoopStrengthReducePass(getTargetLowering()));
121 // FIXME: Implement efficient support for garbage collection intrinsics.
122 PM.add(createLowerGCPass());
124 // FIXME: Implement the invoke/unwind instructions!
125 PM.add(createLowerInvokePass(getTargetLowering()));
127 // Make sure that no unreachable blocks are instruction selected.
128 PM.add(createUnreachableBlockEliminationPass());
130 // Ask the target for an isel.
131 if (addInstSelector(PM, Fast))
134 // Print the instruction selected machine code...
135 if (PrintMachineCode)
136 PM.add(createMachineFunctionPrinterPass(cerr));
138 // Perform register allocation to convert to a concrete x86 representation
139 PM.add(createRegisterAllocator());
141 if (PrintMachineCode)
142 PM.add(createMachineFunctionPrinterPass(cerr));
144 // Run post-ra passes.
145 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
146 PM.add(createMachineFunctionPrinterPass(cerr));
148 // Insert prolog/epilog code. Eliminate abstract frame index references...
149 PM.add(createPrologEpilogCodeInserter());
151 if (PrintMachineCode) // Print the register-allocated code
152 PM.add(createMachineFunctionPrinterPass(cerr));
154 // Branch folding must be run after regalloc and prolog/epilog insertion.
156 PM.add(createBranchFoldingPass());
158 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
159 PM.add(createMachineFunctionPrinterPass(cerr));
161 addCodeEmitter(PM, Fast, MCE);
163 // Delete machine code for this function
164 PM.add(createMachineCodeDeleter());
166 return false; // success!