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