blob: 8f6829f6de4ad49341048714df9e58e013257e98 [file] [log] [blame]
Chris Lattnerb4f68ed2002-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 Lattner5bcd95c2002-12-24 00:04:01 +00008#include "X86.h"
Chris Lattnerbb144a82003-08-24 19:49:48 +00009#include "llvm/Module.h"
Chris Lattner155e68f2003-04-23 16:24:55 +000010#include "llvm/PassManager.h"
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000011#include "llvm/Target/TargetMachineImpls.h"
Chris Lattner3dffa792002-10-30 00:47:49 +000012#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnerd91d86f2003-01-13 00:51:23 +000013#include "llvm/CodeGen/Passes.h"
Chris Lattner155e68f2003-04-23 16:24:55 +000014#include "llvm/Transforms/Scalar.h"
Chris Lattner439a27a2002-12-16 16:15:51 +000015#include "Support/CommandLine.h"
16#include "Support/Statistic.h"
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000017
Chris Lattner439a27a2002-12-16 16:15:51 +000018namespace {
Alkis Evlogimenoseed462b2003-10-02 06:13:19 +000019 cl::opt<RegAllocName>
20 RegAlloc("regalloc",
21 cl::desc("Register allocator to use: (default = simple)"),
22 cl::Prefix,
23 cl::values(clEnumVal(simple, " simple register allocator"),
24 clEnumVal(local, " local register allocator"),
25 0),
26 cl::init(local));
27
Chris Lattner5bcd95c2002-12-24 00:04:01 +000028 cl::opt<bool> PrintCode("print-machineinstrs",
29 cl::desc("Print generated machine code"));
Chris Lattnerac0c8682003-08-11 14:59:22 +000030 cl::opt<bool> NoPatternISel("disable-pattern-isel", cl::init(true),
31 cl::desc("Use the 'simple' X86 instruction selector"));
Chris Lattner439a27a2002-12-16 16:15:51 +000032}
33
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000034// allocateX86TargetMachine - Allocate and return a subclass of TargetMachine
35// that implements the X86 backend.
36//
Chris Lattnerbb144a82003-08-24 19:49:48 +000037TargetMachine *allocateX86TargetMachine(const Module &M) {
38 return new X86TargetMachine(M);
Chris Lattner5bcd95c2002-12-24 00:04:01 +000039}
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000040
41
42/// X86TargetMachine ctor - Create an ILP32 architecture model
43///
Chris Lattnerbb144a82003-08-24 19:49:48 +000044X86TargetMachine::X86TargetMachine(const Module &M)
Chris Lattner5bcd95c2002-12-24 00:04:01 +000045 : TargetMachine("X86",
Chris Lattnerbb144a82003-08-24 19:49:48 +000046 M.getEndianness() != Module::BigEndian,
47 M.getPointerSize() != Module::Pointer64 ? 4 : 8,
48 M.getPointerSize() != Module::Pointer64 ? 4 : 8,
49 M.getPointerSize() != Module::Pointer64 ? 4 : 8,
50 4, M.getPointerSize() != Module::Pointer64 ? 4 : 8),
51 FrameInfo(TargetFrameInfo::StackGrowsDown, 8/*16 for SSE*/, 4) {
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000052}
53
Chris Lattnerc9bbfbc2003-08-05 16:34:44 +000054
55// addPassesToEmitAssembly - We currently use all of the same passes as the JIT
56// does to emit statically compiled machine code.
Brian Gaekede3aa4f2003-06-18 21:43:21 +000057bool X86TargetMachine::addPassesToEmitAssembly(PassManager &PM,
58 std::ostream &Out) {
Brian Gaekeb4286542003-08-13 18:15:52 +000059 // FIXME: Implement the switch instruction in the instruction selector!
60 PM.add(createLowerSwitchPass());
61
62 if (NoPatternISel)
63 PM.add(createX86SimpleInstructionSelector(*this));
64 else
65 PM.add(createX86PatternInstructionSelector(*this));
66
67 // TODO: optional optimizations go here
68
69 // FIXME: Add SSA based peephole optimizer here.
70
71 // Print the instruction selected machine code...
72 if (PrintCode)
73 PM.add(createMachineFunctionPrinterPass());
74
75 // Perform register allocation to convert to a concrete x86 representation
Alkis Evlogimenoseed462b2003-10-02 06:13:19 +000076 switch (RegAlloc) {
77 case simple:
Brian Gaekeb4286542003-08-13 18:15:52 +000078 PM.add(createSimpleRegisterAllocator());
Alkis Evlogimenoseed462b2003-10-02 06:13:19 +000079 break;
80 case local:
Brian Gaekeb4286542003-08-13 18:15:52 +000081 PM.add(createLocalRegisterAllocator());
Alkis Evlogimenoseed462b2003-10-02 06:13:19 +000082 break;
83 default:
84 assert(0 && "no register allocator selected");
85 }
Brian Gaekeb4286542003-08-13 18:15:52 +000086
87 if (PrintCode)
88 PM.add(createMachineFunctionPrinterPass());
89
90 PM.add(createX86FloatingPointStackifierPass());
91
92 if (PrintCode)
93 PM.add(createMachineFunctionPrinterPass());
94
95 // Insert prolog/epilog code. Eliminate abstract frame index references...
96 PM.add(createPrologEpilogCodeInserter());
97
98 PM.add(createX86PeepholeOptimizerPass());
99
100 if (PrintCode) // Print the register-allocated code
101 PM.add(createX86CodePrinterPass(std::cerr, *this));
102
Brian Gaekede420ae2003-07-23 20:25:08 +0000103 PM.add(createX86CodePrinterPass(Out, *this));
Brian Gaekede3aa4f2003-06-18 21:43:21 +0000104 return false; // success!
105}
106
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000107/// addPassesToJITCompile - Add passes to the specified pass manager to
108/// implement a fast dynamic compiler for this target. Return true if this is
109/// not supported for this target.
110///
Brian Gaekeb4286542003-08-13 18:15:52 +0000111bool X86TargetMachine::addPassesToJITCompile(FunctionPassManager &PM) {
Chris Lattner155e68f2003-04-23 16:24:55 +0000112 // FIXME: Implement the switch instruction in the instruction selector!
113 PM.add(createLowerSwitchPass());
114
Chris Lattnerac0c8682003-08-11 14:59:22 +0000115 if (NoPatternISel)
116 PM.add(createX86SimpleInstructionSelector(*this));
117 else
118 PM.add(createX86PatternInstructionSelector(*this));
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000119
120 // TODO: optional optimizations go here
121
Chris Lattnerd91d86f2003-01-13 00:51:23 +0000122 // FIXME: Add SSA based peephole optimizer here.
123
Chris Lattner3dffa792002-10-30 00:47:49 +0000124 // Print the instruction selected machine code...
Chris Lattner5bcd95c2002-12-24 00:04:01 +0000125 if (PrintCode)
126 PM.add(createMachineFunctionPrinterPass());
Chris Lattner3dffa792002-10-30 00:47:49 +0000127
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000128 // Perform register allocation to convert to a concrete x86 representation
Alkis Evlogimenoseed462b2003-10-02 06:13:19 +0000129 switch (RegAlloc) {
130 case simple:
Chris Lattnerd282cfe2002-12-28 20:33:32 +0000131 PM.add(createSimpleRegisterAllocator());
Alkis Evlogimenoseed462b2003-10-02 06:13:19 +0000132 break;
133 case local:
Chris Lattnerd282cfe2002-12-28 20:33:32 +0000134 PM.add(createLocalRegisterAllocator());
Alkis Evlogimenoseed462b2003-10-02 06:13:19 +0000135 break;
136 default:
137 assert(0 && "no register allocator selected");
138 }
Chris Lattnerd282cfe2002-12-28 20:33:32 +0000139
140 if (PrintCode)
141 PM.add(createMachineFunctionPrinterPass());
142
Chris Lattnerd91d86f2003-01-13 00:51:23 +0000143 PM.add(createX86FloatingPointStackifierPass());
144
145 if (PrintCode)
146 PM.add(createMachineFunctionPrinterPass());
147
Chris Lattnerd282cfe2002-12-28 20:33:32 +0000148 // Insert prolog/epilog code. Eliminate abstract frame index references...
149 PM.add(createPrologEpilogCodeInserter());
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000150
Chris Lattnerd91d86f2003-01-13 00:51:23 +0000151 PM.add(createX86PeepholeOptimizerPass());
152
Chris Lattner430cda72002-12-25 05:06:21 +0000153 if (PrintCode) // Print the register-allocated code
Brian Gaekede420ae2003-07-23 20:25:08 +0000154 PM.add(createX86CodePrinterPass(std::cerr, *this));
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000155 return false; // success!
156}
157