Eliminate code for pointer size and endianness emulation.
[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", true, 4, 4, 4, 4, 4),
37     FrameInfo(TargetFrameInfo::StackGrowsDown, 8/*16 for SSE*/, 4) {
38 }
39
40
41 // addPassesToEmitAssembly - We currently use all of the same passes as the JIT
42 // does to emit statically compiled machine code.
43 bool X86TargetMachine::addPassesToEmitAssembly(PassManager &PM,
44                                                std::ostream &Out) {
45   // FIXME: Implement the switch instruction in the instruction selector!
46   PM.add(createLowerSwitchPass());
47
48   // 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
55   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
69   PM.add(createRegisterAllocator());
70
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
87   PM.add(createX86CodePrinterPass(Out, *this));
88   return false; // success!
89 }
90
91 /// 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 ///
95 bool X86TargetMachine::addPassesToJITCompile(FunctionPassManager &PM) {
96   // FIXME: Implement the switch instruction in the instruction selector!
97   PM.add(createLowerSwitchPass());
98
99   // 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
106   if (NoPatternISel)
107     PM.add(createX86SimpleInstructionSelector(*this));
108   else
109     PM.add(createX86PatternInstructionSelector(*this));
110
111   // TODO: optional optimizations go here
112
113   // FIXME: Add SSA based peephole optimizer here.
114
115   // Print the instruction selected machine code...
116   if (PrintCode)
117     PM.add(createMachineFunctionPrinterPass());
118
119   // Perform register allocation to convert to a concrete x86 representation
120   PM.add(createRegisterAllocator());
121
122   if (PrintCode)
123     PM.add(createMachineFunctionPrinterPass());
124
125   PM.add(createX86FloatingPointStackifierPass());
126
127   if (PrintCode)
128     PM.add(createMachineFunctionPrinterPass());
129
130   // Insert prolog/epilog code.  Eliminate abstract frame index references...
131   PM.add(createPrologEpilogCodeInserter());
132
133   PM.add(createX86PeepholeOptimizerPass());
134
135   if (PrintCode)  // Print the register-allocated code
136     PM.add(createX86CodePrinterPass(std::cerr, *this));
137   return false; // success!
138 }
139
140 bool X86TargetMachine::replaceMachineCodeForFunction (void *Old, void *New) {
141   // FIXME: This code could perhaps live in a more appropriate place.
142   char *OldByte = (char *) Old;
143   *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!
149 }