You can't just blat the address into memory, you have to blat its
[oota-llvm.git] / lib / Target / X86 / X86TargetMachine.cpp
1 //===-- 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"
8 #include "X86.h"
9 #include "llvm/Module.h"
10 #include "llvm/PassManager.h"
11 #include "llvm/Target/TargetMachineImpls.h"
12 #include "llvm/CodeGen/MachineFunction.h"
13 #include "llvm/CodeGen/Passes.h"
14 #include "llvm/Transforms/Scalar.h"
15 #include "Support/CommandLine.h"
16 #include "Support/Statistic.h"
17
18 namespace {
19   cl::opt<bool> PrintCode("print-machineinstrs",
20                           cl::desc("Print generated machine code"));
21   cl::opt<bool> NoPatternISel("disable-pattern-isel", cl::init(true),
22                         cl::desc("Use the 'simple' X86 instruction selector"));
23 }
24
25 // allocateX86TargetMachine - Allocate and return a subclass of TargetMachine
26 // that implements the X86 backend.
27 //
28 TargetMachine *allocateX86TargetMachine(const Module &M) {
29   return new X86TargetMachine(M);
30 }
31
32
33 /// X86TargetMachine ctor - Create an ILP32 architecture model
34 ///
35 X86TargetMachine::X86TargetMachine(const Module &M)
36   : TargetMachine("X86", 
37                   M.getEndianness() != Module::BigEndian,
38                   M.getPointerSize() != Module::Pointer64 ? 4 : 8,
39                   M.getPointerSize() != Module::Pointer64 ? 4 : 8,
40                   M.getPointerSize() != Module::Pointer64 ? 4 : 8,
41                   4, M.getPointerSize() != Module::Pointer64 ? 4 : 8),
42     FrameInfo(TargetFrameInfo::StackGrowsDown, 8/*16 for SSE*/, 4) {
43 }
44
45
46 // addPassesToEmitAssembly - We currently use all of the same passes as the JIT
47 // does to emit statically compiled machine code.
48 bool X86TargetMachine::addPassesToEmitAssembly(PassManager &PM,
49                                                std::ostream &Out) {
50   // FIXME: Implement the switch instruction in the instruction selector!
51   PM.add(createLowerSwitchPass());
52
53   // FIXME: Implement the invoke/unwind instructions!
54   PM.add(createLowerInvokePass());
55
56   // FIXME: The code generator does not properly handle functions with
57   // unreachable basic blocks.
58   PM.add(createCFGSimplificationPass());
59
60   if (NoPatternISel)
61     PM.add(createX86SimpleInstructionSelector(*this));
62   else
63     PM.add(createX86PatternInstructionSelector(*this));
64
65   // TODO: optional optimizations go here
66
67   // FIXME: Add SSA based peephole optimizer here.
68
69   // Print the instruction selected machine code...
70   if (PrintCode)
71     PM.add(createMachineFunctionPrinterPass());
72
73   // Perform register allocation to convert to a concrete x86 representation
74   PM.add(createRegisterAllocator());
75
76   if (PrintCode)
77     PM.add(createMachineFunctionPrinterPass());
78
79   PM.add(createX86FloatingPointStackifierPass());
80
81   if (PrintCode)
82     PM.add(createMachineFunctionPrinterPass());
83
84   // Insert prolog/epilog code.  Eliminate abstract frame index references...
85   PM.add(createPrologEpilogCodeInserter());
86
87   PM.add(createX86PeepholeOptimizerPass());
88
89   if (PrintCode)  // Print the register-allocated code
90     PM.add(createX86CodePrinterPass(std::cerr, *this));
91
92   PM.add(createX86CodePrinterPass(Out, *this));
93   return false; // success!
94 }
95
96 /// addPassesToJITCompile - Add passes to the specified pass manager to
97 /// implement a fast dynamic compiler for this target.  Return true if this is
98 /// not supported for this target.
99 ///
100 bool X86TargetMachine::addPassesToJITCompile(FunctionPassManager &PM) {
101   // FIXME: Implement the switch instruction in the instruction selector!
102   PM.add(createLowerSwitchPass());
103
104   // FIXME: Implement the invoke/unwind instructions!
105   PM.add(createLowerInvokePass());
106
107   // FIXME: The code generator does not properly handle functions with
108   // unreachable basic blocks.
109   PM.add(createCFGSimplificationPass());
110
111   if (NoPatternISel)
112     PM.add(createX86SimpleInstructionSelector(*this));
113   else
114     PM.add(createX86PatternInstructionSelector(*this));
115
116   // TODO: optional optimizations go here
117
118   // FIXME: Add SSA based peephole optimizer here.
119
120   // Print the instruction selected machine code...
121   if (PrintCode)
122     PM.add(createMachineFunctionPrinterPass());
123
124   // Perform register allocation to convert to a concrete x86 representation
125   PM.add(createRegisterAllocator());
126
127   if (PrintCode)
128     PM.add(createMachineFunctionPrinterPass());
129
130   PM.add(createX86FloatingPointStackifierPass());
131
132   if (PrintCode)
133     PM.add(createMachineFunctionPrinterPass());
134
135   // Insert prolog/epilog code.  Eliminate abstract frame index references...
136   PM.add(createPrologEpilogCodeInserter());
137
138   PM.add(createX86PeepholeOptimizerPass());
139
140   if (PrintCode)  // Print the register-allocated code
141     PM.add(createX86CodePrinterPass(std::cerr, *this));
142   return false; // success!
143 }
144
145 bool X86TargetMachine::replaceMachineCodeForFunction (void *Old, void *New) {
146   // FIXME: This code could perhaps live in a more appropriate place.
147   char *OldByte = (char *) Old;
148   *OldByte++ = 0xE9;                // Emit JMP opcode.
149   int32_t *OldWord = (int32_t *) OldByte;
150   int32_t NewAddr = (int32_t) New;
151   int32_t OldAddr = (int32_t) OldWord;
152   *OldWord = NewAddr - OldAddr - 4; // Emit PC-relative addr of New code.
153   return false;                     // success!
154 }