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