blob: d4f3645d7569d11bd4a89b7c19951e3e611c85de [file] [log] [blame]
Chris Lattner47877052006-09-04 04:16:09 +00001//===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
2//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LLVMTargetMachine class.
11//
12//===----------------------------------------------------------------------===//
13
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"
Chris Lattner47877052006-09-04 04:16:09 +000020using namespace llvm;
21
Bill Wendling04523ea2007-02-08 01:36:53 +000022FileModel::Model
23LLVMTargetMachine::addPassesToEmitFile(FunctionPassManager &PM,
24 std::ostream &Out,
25 CodeGenFileType FileType,
26 bool Fast) {
Chris Lattner47877052006-09-04 04:16:09 +000027 // Standard LLVM-Level Passes.
28
29 // Run loop strength reduction before anything else.
30 if (!Fast) PM.add(createLoopStrengthReducePass(getTargetLowering()));
31
32 // FIXME: Implement efficient support for garbage collection intrinsics.
33 PM.add(createLowerGCPass());
34
35 // FIXME: Implement the invoke/unwind instructions!
Jim Laskeya4e7cd92007-02-22 16:22:15 +000036 if (!ExceptionHandling)
37 PM.add(createLowerInvokePass(getTargetLowering()));
Chris Lattner47877052006-09-04 04:16:09 +000038
39 // Make sure that no unreachable blocks are instruction selected.
40 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +000041
Chris Lattner47877052006-09-04 04:16:09 +000042 // Ask the target for an isel.
43 if (addInstSelector(PM, Fast))
Bill Wendling04523ea2007-02-08 01:36:53 +000044 return FileModel::Error;
45
Chris Lattner47877052006-09-04 04:16:09 +000046 // Print the instruction selected machine code...
47 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000048 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +000049
50 // Perform register allocation to convert to a concrete x86 representation
51 PM.add(createRegisterAllocator());
52
53 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000054 PM.add(createMachineFunctionPrinterPass(cerr));
55
Chris Lattner47877052006-09-04 04:16:09 +000056 // Run post-ra passes.
57 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000058 PM.add(createMachineFunctionPrinterPass(cerr));
59
Chris Lattner47877052006-09-04 04:16:09 +000060 // Insert prolog/epilog code. Eliminate abstract frame index references...
61 PM.add(createPrologEpilogCodeInserter());
62
Chris Lattner4a84ad72006-10-13 20:45:56 +000063 // Branch folding must be run after regalloc and prolog/epilog insertion.
Jim Laskey62d07d62006-10-24 16:11:49 +000064 if (!Fast)
65 PM.add(createBranchFoldingPass());
Jim Laskey9d4209f2006-11-07 19:33:46 +000066
67 // Fold redundant debug labels.
68 PM.add(createDebugLabelFoldingPass());
Chris Lattner4a84ad72006-10-13 20:45:56 +000069
Chris Lattner47877052006-09-04 04:16:09 +000070 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +000071 PM.add(createMachineFunctionPrinterPass(cerr));
72
Chris Lattner47877052006-09-04 04:16:09 +000073 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000074 PM.add(createMachineFunctionPrinterPass(cerr));
75
Chris Lattner47877052006-09-04 04:16:09 +000076 switch (FileType) {
Bill Wendling04523ea2007-02-08 01:36:53 +000077 default:
78 break;
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;
Chris Lattner47877052006-09-04 04:16:09 +000088 }
Bill Wendling04523ea2007-02-08 01:36:53 +000089
90 return FileModel::Error;
91}
92
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.
96bool LLVMTargetMachine::addPassesToEmitFileFinish(FunctionPassManager &PM,
97 MachineCodeEmitter *MCE,
98 bool Fast) {
99 if (MCE)
100 addSimpleCodeEmitter(PM, Fast, *MCE);
101
Chris Lattner47877052006-09-04 04:16:09 +0000102 // Delete machine code for this function
103 PM.add(createMachineCodeDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000104
Chris Lattner47877052006-09-04 04:16:09 +0000105 return false; // success!
106}
107
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
112/// not supported.
113///
114bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
115 MachineCodeEmitter &MCE,
116 bool Fast) {
117 // Standard LLVM-Level Passes.
118
119 // Run loop strength reduction before anything else.
120 if (!Fast) PM.add(createLoopStrengthReducePass(getTargetLowering()));
121
122 // FIXME: Implement efficient support for garbage collection intrinsics.
123 PM.add(createLowerGCPass());
124
125 // FIXME: Implement the invoke/unwind instructions!
Duraid Madina2a0013f2006-09-04 06:21:35 +0000126 PM.add(createLowerInvokePass(getTargetLowering()));
Chris Lattner47877052006-09-04 04:16:09 +0000127
128 // Make sure that no unreachable blocks are instruction selected.
129 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +0000130
Chris Lattner47877052006-09-04 04:16:09 +0000131 // Ask the target for an isel.
132 if (addInstSelector(PM, Fast))
133 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000134
Chris Lattner47877052006-09-04 04:16:09 +0000135 // Print the instruction selected machine code...
136 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000137 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +0000138
139 // Perform register allocation to convert to a concrete x86 representation
140 PM.add(createRegisterAllocator());
141
142 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000143 PM.add(createMachineFunctionPrinterPass(cerr));
144
Chris Lattner47877052006-09-04 04:16:09 +0000145 // Run post-ra passes.
146 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000147 PM.add(createMachineFunctionPrinterPass(cerr));
148
Chris Lattner47877052006-09-04 04:16:09 +0000149 // Insert prolog/epilog code. Eliminate abstract frame index references...
150 PM.add(createPrologEpilogCodeInserter());
151
152 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000153 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +0000154
Chris Lattnere01eaa02006-11-16 01:00:07 +0000155 // Branch folding must be run after regalloc and prolog/epilog insertion.
156 if (!Fast)
157 PM.add(createBranchFoldingPass());
Chris Lattner47877052006-09-04 04:16:09 +0000158
159 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000160 PM.add(createMachineFunctionPrinterPass(cerr));
161
Chris Lattner47877052006-09-04 04:16:09 +0000162 addCodeEmitter(PM, Fast, MCE);
163
164 // Delete machine code for this function
165 PM.add(createMachineCodeDeleter());
166
167 return false; // success!
168}