blob: 74b0936d85d1f21445c6bca5a52ab7312889cffd [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 {
Chris Lattner5bcd95c2002-12-24 00:04:01 +000019 cl::opt<bool> PrintCode("print-machineinstrs",
20 cl::desc("Print generated machine code"));
Chris Lattnerac0c8682003-08-11 14:59:22 +000021 cl::opt<bool> NoPatternISel("disable-pattern-isel", cl::init(true),
22 cl::desc("Use the 'simple' X86 instruction selector"));
Chris Lattner439a27a2002-12-16 16:15:51 +000023}
24
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000025// allocateX86TargetMachine - Allocate and return a subclass of TargetMachine
26// that implements the X86 backend.
27//
Chris Lattnerbb144a82003-08-24 19:49:48 +000028TargetMachine *allocateX86TargetMachine(const Module &M) {
29 return new X86TargetMachine(M);
Chris Lattner5bcd95c2002-12-24 00:04:01 +000030}
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000031
32
33/// X86TargetMachine ctor - Create an ILP32 architecture model
34///
Chris Lattnerbb144a82003-08-24 19:49:48 +000035X86TargetMachine::X86TargetMachine(const Module &M)
Chris Lattner6c09db22003-10-20 04:11:23 +000036 : TargetMachine("X86", true, 4, 4, 4, 4, 4),
Chris Lattnerbb144a82003-08-24 19:49:48 +000037 FrameInfo(TargetFrameInfo::StackGrowsDown, 8/*16 for SSE*/, 4) {
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000038}
39
Chris Lattnerc9bbfbc2003-08-05 16:34:44 +000040
41// addPassesToEmitAssembly - We currently use all of the same passes as the JIT
42// does to emit statically compiled machine code.
Brian Gaekede3aa4f2003-06-18 21:43:21 +000043bool X86TargetMachine::addPassesToEmitAssembly(PassManager &PM,
44 std::ostream &Out) {
Brian Gaekeb4286542003-08-13 18:15:52 +000045 // FIXME: Implement the switch instruction in the instruction selector!
46 PM.add(createLowerSwitchPass());
47
Chris Lattnerc58c1692003-10-05 19:15:47 +000048 // FIXME: Implement the invoke/unwind instructions!
49 PM.add(createLowerInvokePass());
50
51 // FIXME: The code generator does not properly handle functions with
52 // unreachable basic blocks.
53 PM.add(createCFGSimplificationPass());
54
Brian Gaekeb4286542003-08-13 18:15:52 +000055 if (NoPatternISel)
56 PM.add(createX86SimpleInstructionSelector(*this));
57 else
58 PM.add(createX86PatternInstructionSelector(*this));
59
60 // TODO: optional optimizations go here
61
62 // FIXME: Add SSA based peephole optimizer here.
63
64 // Print the instruction selected machine code...
65 if (PrintCode)
66 PM.add(createMachineFunctionPrinterPass());
67
68 // Perform register allocation to convert to a concrete x86 representation
Alkis Evlogimenos7237ece2003-10-02 16:57:49 +000069 PM.add(createRegisterAllocator());
Brian Gaekeb4286542003-08-13 18:15:52 +000070
71 if (PrintCode)
72 PM.add(createMachineFunctionPrinterPass());
73
74 PM.add(createX86FloatingPointStackifierPass());
75
76 if (PrintCode)
77 PM.add(createMachineFunctionPrinterPass());
78
79 // Insert prolog/epilog code. Eliminate abstract frame index references...
80 PM.add(createPrologEpilogCodeInserter());
81
82 PM.add(createX86PeepholeOptimizerPass());
83
84 if (PrintCode) // Print the register-allocated code
85 PM.add(createX86CodePrinterPass(std::cerr, *this));
86
Brian Gaekede420ae2003-07-23 20:25:08 +000087 PM.add(createX86CodePrinterPass(Out, *this));
Brian Gaekede3aa4f2003-06-18 21:43:21 +000088 return false; // success!
89}
90
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000091/// addPassesToJITCompile - Add passes to the specified pass manager to
92/// implement a fast dynamic compiler for this target. Return true if this is
93/// not supported for this target.
94///
Brian Gaekeb4286542003-08-13 18:15:52 +000095bool X86TargetMachine::addPassesToJITCompile(FunctionPassManager &PM) {
Chris Lattner155e68f2003-04-23 16:24:55 +000096 // FIXME: Implement the switch instruction in the instruction selector!
97 PM.add(createLowerSwitchPass());
98
Chris Lattnerc58c1692003-10-05 19:15:47 +000099 // FIXME: Implement the invoke/unwind instructions!
100 PM.add(createLowerInvokePass());
101
102 // FIXME: The code generator does not properly handle functions with
103 // unreachable basic blocks.
104 PM.add(createCFGSimplificationPass());
105
Chris Lattnerac0c8682003-08-11 14:59:22 +0000106 if (NoPatternISel)
107 PM.add(createX86SimpleInstructionSelector(*this));
108 else
109 PM.add(createX86PatternInstructionSelector(*this));
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000110
111 // TODO: optional optimizations go here
112
Chris Lattnerd91d86f2003-01-13 00:51:23 +0000113 // FIXME: Add SSA based peephole optimizer here.
114
Chris Lattner3dffa792002-10-30 00:47:49 +0000115 // Print the instruction selected machine code...
Chris Lattner5bcd95c2002-12-24 00:04:01 +0000116 if (PrintCode)
117 PM.add(createMachineFunctionPrinterPass());
Chris Lattner3dffa792002-10-30 00:47:49 +0000118
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000119 // Perform register allocation to convert to a concrete x86 representation
Alkis Evlogimenos7237ece2003-10-02 16:57:49 +0000120 PM.add(createRegisterAllocator());
Chris Lattnerd282cfe2002-12-28 20:33:32 +0000121
122 if (PrintCode)
123 PM.add(createMachineFunctionPrinterPass());
124
Chris Lattnerd91d86f2003-01-13 00:51:23 +0000125 PM.add(createX86FloatingPointStackifierPass());
126
127 if (PrintCode)
128 PM.add(createMachineFunctionPrinterPass());
129
Chris Lattnerd282cfe2002-12-28 20:33:32 +0000130 // Insert prolog/epilog code. Eliminate abstract frame index references...
131 PM.add(createPrologEpilogCodeInserter());
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000132
Chris Lattnerd91d86f2003-01-13 00:51:23 +0000133 PM.add(createX86PeepholeOptimizerPass());
134
Chris Lattner430cda72002-12-25 05:06:21 +0000135 if (PrintCode) // Print the register-allocated code
Brian Gaekede420ae2003-07-23 20:25:08 +0000136 PM.add(createX86CodePrinterPass(std::cerr, *this));
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000137 return false; // success!
138}
139
Brian Gaeke9b8c2912003-10-17 18:27:46 +0000140bool X86TargetMachine::replaceMachineCodeForFunction (void *Old, void *New) {
Brian Gaekee5f68592003-10-17 21:47:25 +0000141 // FIXME: This code could perhaps live in a more appropriate place.
Brian Gaeke9b8c2912003-10-17 18:27:46 +0000142 char *OldByte = (char *) Old;
Brian Gaekee5f68592003-10-17 21:47:25 +0000143 *OldByte++ = 0xE9; // Emit JMP opcode.
144 int32_t *OldWord = (int32_t *) OldByte;
145 int32_t NewAddr = (int32_t) New;
146 int32_t OldAddr = (int32_t) OldWord;
147 *OldWord = NewAddr - OldAddr - 4; // Emit PC-relative addr of New code.
148 return false; // success!
Brian Gaeke9b8c2912003-10-17 18:27:46 +0000149}