blob: e58ac31c7f2350a73849feaf031c951bcb614d06 [file] [log] [blame]
Chris Lattner02a3d832002-10-29 22:37:54 +00001//===-- X86TargetMachine.cpp - Define TargetMachine for the X86 -----------===//
2//
3// This file defines the X86 specific subclass of TargetMachine.
4//
5//===----------------------------------------------------------------------===//
6
7#include "X86TargetMachine.h"
Chris Lattnera32b4052002-12-24 00:04:01 +00008#include "X86.h"
Chris Lattner4fd144a2003-08-24 19:49:48 +00009#include "llvm/Module.h"
Chris Lattner524608a2003-04-23 16:24:55 +000010#include "llvm/PassManager.h"
Chris Lattner02a3d832002-10-29 22:37:54 +000011#include "llvm/Target/TargetMachineImpls.h"
Chris Lattnerd7a85562002-10-30 00:47:49 +000012#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner962d5be2003-01-13 00:51:23 +000013#include "llvm/CodeGen/Passes.h"
Chris Lattner524608a2003-04-23 16:24:55 +000014#include "llvm/Transforms/Scalar.h"
Chris Lattnere0c25aa2002-12-16 16:15:51 +000015#include "Support/CommandLine.h"
16#include "Support/Statistic.h"
Chris Lattner02a3d832002-10-29 22:37:54 +000017
Chris Lattnere0c25aa2002-12-16 16:15:51 +000018namespace {
Alkis Evlogimenos568e4382003-10-01 19:38:10 +000019 cl::opt<RegAllocName>
20 RegAlloc("regalloc",
21 cl::desc("Register allocator to use:"), cl::Prefix,
22 cl::values(clEnumVal(simple, "simple register allocator)"),
23 clEnumVal(local, "local register allocator"),
24 clEnumVal(linearscan, "linear scan global register allocator")),
25 cl::init(local));
26
Chris Lattner30f40d92003-02-26 20:00:41 +000027 cl::opt<bool> NoLocalRA("disable-local-ra",
Chris Lattnerd9c6f2a2002-12-17 02:51:15 +000028 cl::desc("Use Simple RA instead of Local RegAlloc"));
Chris Lattnera32b4052002-12-24 00:04:01 +000029 cl::opt<bool> PrintCode("print-machineinstrs",
30 cl::desc("Print generated machine code"));
Chris Lattnere61db422003-08-11 14:59:22 +000031 cl::opt<bool> NoPatternISel("disable-pattern-isel", cl::init(true),
32 cl::desc("Use the 'simple' X86 instruction selector"));
Chris Lattnere0c25aa2002-12-16 16:15:51 +000033}
34
Chris Lattner02a3d832002-10-29 22:37:54 +000035// allocateX86TargetMachine - Allocate and return a subclass of TargetMachine
36// that implements the X86 backend.
37//
Chris Lattner4fd144a2003-08-24 19:49:48 +000038TargetMachine *allocateX86TargetMachine(const Module &M) {
39 return new X86TargetMachine(M);
Chris Lattnera32b4052002-12-24 00:04:01 +000040}
Chris Lattner02a3d832002-10-29 22:37:54 +000041
42
43/// X86TargetMachine ctor - Create an ILP32 architecture model
44///
Chris Lattner4fd144a2003-08-24 19:49:48 +000045X86TargetMachine::X86TargetMachine(const Module &M)
Chris Lattnera32b4052002-12-24 00:04:01 +000046 : TargetMachine("X86",
Chris Lattner4fd144a2003-08-24 19:49:48 +000047 M.getEndianness() != Module::BigEndian,
48 M.getPointerSize() != Module::Pointer64 ? 4 : 8,
49 M.getPointerSize() != Module::Pointer64 ? 4 : 8,
50 M.getPointerSize() != Module::Pointer64 ? 4 : 8,
51 4, M.getPointerSize() != Module::Pointer64 ? 4 : 8),
52 FrameInfo(TargetFrameInfo::StackGrowsDown, 8/*16 for SSE*/, 4) {
Chris Lattner02a3d832002-10-29 22:37:54 +000053}
54
Chris Lattner1d6ba3e2003-08-05 16:34:44 +000055
56// addPassesToEmitAssembly - We currently use all of the same passes as the JIT
57// does to emit statically compiled machine code.
Brian Gaekeac94bab2003-06-18 21:43:21 +000058bool X86TargetMachine::addPassesToEmitAssembly(PassManager &PM,
59 std::ostream &Out) {
Brian Gaeke02cbe282003-08-13 18:15:52 +000060 // FIXME: Implement the switch instruction in the instruction selector!
61 PM.add(createLowerSwitchPass());
62
63 if (NoPatternISel)
64 PM.add(createX86SimpleInstructionSelector(*this));
65 else
66 PM.add(createX86PatternInstructionSelector(*this));
67
68 // TODO: optional optimizations go here
69
70 // FIXME: Add SSA based peephole optimizer here.
71
72 // Print the instruction selected machine code...
73 if (PrintCode)
74 PM.add(createMachineFunctionPrinterPass());
75
76 // Perform register allocation to convert to a concrete x86 representation
77 if (NoLocalRA)
78 PM.add(createSimpleRegisterAllocator());
79 else
80 PM.add(createLocalRegisterAllocator());
81
82 if (PrintCode)
83 PM.add(createMachineFunctionPrinterPass());
84
85 PM.add(createX86FloatingPointStackifierPass());
86
87 if (PrintCode)
88 PM.add(createMachineFunctionPrinterPass());
89
90 // Insert prolog/epilog code. Eliminate abstract frame index references...
91 PM.add(createPrologEpilogCodeInserter());
92
93 PM.add(createX86PeepholeOptimizerPass());
94
95 if (PrintCode) // Print the register-allocated code
96 PM.add(createX86CodePrinterPass(std::cerr, *this));
97
Brian Gaekea92dce42003-07-23 20:25:08 +000098 PM.add(createX86CodePrinterPass(Out, *this));
Brian Gaekeac94bab2003-06-18 21:43:21 +000099 return false; // success!
100}
101
Chris Lattner02a3d832002-10-29 22:37:54 +0000102/// addPassesToJITCompile - Add passes to the specified pass manager to
103/// implement a fast dynamic compiler for this target. Return true if this is
104/// not supported for this target.
105///
Brian Gaeke02cbe282003-08-13 18:15:52 +0000106bool X86TargetMachine::addPassesToJITCompile(FunctionPassManager &PM) {
Chris Lattner524608a2003-04-23 16:24:55 +0000107 // FIXME: Implement the switch instruction in the instruction selector!
108 PM.add(createLowerSwitchPass());
109
Chris Lattnere61db422003-08-11 14:59:22 +0000110 if (NoPatternISel)
111 PM.add(createX86SimpleInstructionSelector(*this));
112 else
113 PM.add(createX86PatternInstructionSelector(*this));
Chris Lattner02a3d832002-10-29 22:37:54 +0000114
115 // TODO: optional optimizations go here
116
Chris Lattner962d5be2003-01-13 00:51:23 +0000117 // FIXME: Add SSA based peephole optimizer here.
118
Chris Lattnerd7a85562002-10-30 00:47:49 +0000119 // Print the instruction selected machine code...
Chris Lattnera32b4052002-12-24 00:04:01 +0000120 if (PrintCode)
121 PM.add(createMachineFunctionPrinterPass());
Chris Lattnerd7a85562002-10-30 00:47:49 +0000122
Chris Lattner02a3d832002-10-29 22:37:54 +0000123 // Perform register allocation to convert to a concrete x86 representation
Alkis Evlogimenos568e4382003-10-01 19:38:10 +0000124 switch (RegAlloc) {
125 case simple:
Chris Lattner9a81e692002-12-28 20:33:32 +0000126 PM.add(createSimpleRegisterAllocator());
Alkis Evlogimenos568e4382003-10-01 19:38:10 +0000127 break;
128 case local:
Chris Lattner9a81e692002-12-28 20:33:32 +0000129 PM.add(createLocalRegisterAllocator());
Alkis Evlogimenos568e4382003-10-01 19:38:10 +0000130 break;
131 case linearscan:
132 PM.add(createLinearScanRegisterAllocator());
133 break;
134 default:
135 assert(0 && "no register allocator selected");
136 }
Chris Lattner9a81e692002-12-28 20:33:32 +0000137
138 if (PrintCode)
139 PM.add(createMachineFunctionPrinterPass());
140
Chris Lattner962d5be2003-01-13 00:51:23 +0000141 PM.add(createX86FloatingPointStackifierPass());
142
143 if (PrintCode)
144 PM.add(createMachineFunctionPrinterPass());
145
Chris Lattner9a81e692002-12-28 20:33:32 +0000146 // Insert prolog/epilog code. Eliminate abstract frame index references...
147 PM.add(createPrologEpilogCodeInserter());
Chris Lattner02a3d832002-10-29 22:37:54 +0000148
Chris Lattner962d5be2003-01-13 00:51:23 +0000149 PM.add(createX86PeepholeOptimizerPass());
150
Chris Lattner55aaff52002-12-25 05:06:21 +0000151 if (PrintCode) // Print the register-allocated code
Brian Gaekea92dce42003-07-23 20:25:08 +0000152 PM.add(createX86CodePrinterPass(std::cerr, *this));
Chris Lattner02a3d832002-10-29 22:37:54 +0000153 return false; // success!
154}
155