X86RegisterInfo.cpp revision f0a0cddbcda344a90b7217b744c78dccec71851c
1324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver//===- X86RegisterInfo.cpp - X86 Register Information -----------*- C++ -*-===//
2324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver//
3324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver//                     The LLVM Compiler Infrastructure
4324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver//
5324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// This file was developed by the LLVM research group and is distributed under
6324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// the University of Illinois Open Source License. See LICENSE.TXT for details.
7324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver//
8324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver//===----------------------------------------------------------------------===//
9324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver//
10324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// This file contains the X86 implementation of the MRegisterInfo class.  This
11324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// file is responsible for the frame pointer elimination optimization on X86.
12324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver//
13324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver//===----------------------------------------------------------------------===//
14324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
15324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver#include "X86.h"
16324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver#include "X86RegisterInfo.h"
17324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver#include "X86InstrBuilder.h"
18324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver#include "X86MachineFunctionInfo.h"
19324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver#include "X86Subtarget.h"
20324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver#include "X86TargetMachine.h"
21324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver#include "llvm/Constants.h"
22324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver#include "llvm/Function.h"
23324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver#include "llvm/Type.h"
24324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver#include "llvm/CodeGen/ValueTypes.h"
25324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver#include "llvm/CodeGen/MachineInstrBuilder.h"
26324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver#include "llvm/CodeGen/MachineFunction.h"
27324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver#include "llvm/CodeGen/MachineFrameInfo.h"
28324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver#include "llvm/CodeGen/MachineLocation.h"
29324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver#include "llvm/CodeGen/SSARegMap.h"
30324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver#include "llvm/Target/TargetAsmInfo.h"
31324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver#include "llvm/Target/TargetFrameInfo.h"
32324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver#include "llvm/Target/TargetInstrInfo.h"
33324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver#include "llvm/Target/TargetMachine.h"
34324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver#include "llvm/Target/TargetOptions.h"
35324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver#include "llvm/Support/CommandLine.h"
36324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver#include "llvm/ADT/BitVector.h"
37324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver#include "llvm/ADT/STLExtras.h"
38324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruverusing namespace llvm;
39324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
40324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruvernamespace {
41324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver  cl::opt<bool>
42324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver  NoFusing("disable-spill-fusing",
43324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver           cl::desc("Disable fusing of spill code into instructions"));
44324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver  cl::opt<bool>
45324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver  PrintFailedFusing("print-failed-fuse-candidates",
46324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                    cl::desc("Print instructions that the allocator wants to"
47324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                             " fuse, but the X86 backend currently can't"),
48324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                    cl::Hidden);
49324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver}
50324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
51324c4644fee44b9898524c09511bd33c3f12e2dfBen GruverX86RegisterInfo::X86RegisterInfo(X86TargetMachine &tm,
52324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                                 const TargetInstrInfo &tii)
53324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver  : X86GenRegisterInfo(X86::ADJCALLSTACKDOWN, X86::ADJCALLSTACKUP),
54324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    TM(tm), TII(tii) {
55324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver  // Cache some information.
56324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver  const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
57324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver  Is64Bit = Subtarget->is64Bit();
58324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver  if (Is64Bit) {
59324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    SlotSize = 8;
60324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    StackPtr = X86::RSP;
61324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    FramePtr = X86::RBP;
62324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver  } else {
63324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    SlotSize = 4;
64324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    StackPtr = X86::ESP;
65324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    FramePtr = X86::EBP;
66324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver  }
67324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
68324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver  SmallVector<unsigned,16> AmbEntries;
69324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver  static const unsigned OpTbl2Addr[][2] = {
70324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ADC32ri,     X86::ADC32mi },
71324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ADC32ri8,    X86::ADC32mi8 },
72324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ADC32rr,     X86::ADC32mr },
73324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ADC64ri32,   X86::ADC64mi32 },
74324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ADC64ri8,    X86::ADC64mi8 },
75324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ADC64rr,     X86::ADC64mr },
76324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ADD16ri,     X86::ADD16mi },
77324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ADD16ri8,    X86::ADD16mi8 },
78324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ADD16rr,     X86::ADD16mr },
79324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ADD32ri,     X86::ADD32mi },
80324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ADD32ri8,    X86::ADD32mi8 },
81324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ADD32rr,     X86::ADD32mr },
82324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ADD64ri32,   X86::ADD64mi32 },
83324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ADD64ri8,    X86::ADD64mi8 },
84324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ADD64rr,     X86::ADD64mr },
85324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ADD8ri,      X86::ADD8mi },
86324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ADD8rr,      X86::ADD8mr },
87324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::AND16ri,     X86::AND16mi },
88324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::AND16ri8,    X86::AND16mi8 },
89324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::AND16rr,     X86::AND16mr },
90324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::AND32ri,     X86::AND32mi },
91324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::AND32ri8,    X86::AND32mi8 },
92324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::AND32rr,     X86::AND32mr },
93324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::AND64ri32,   X86::AND64mi32 },
94324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::AND64ri8,    X86::AND64mi8 },
95324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::AND64rr,     X86::AND64mr },
96324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::AND8ri,      X86::AND8mi },
97324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::AND8rr,      X86::AND8mr },
98324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::DEC16r,      X86::DEC16m },
99324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::DEC32r,      X86::DEC32m },
100324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::DEC64_16r,   X86::DEC16m },
101324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::DEC64_32r,   X86::DEC32m },
102324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::DEC64r,      X86::DEC64m },
103324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::DEC8r,       X86::DEC8m },
104324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::INC16r,      X86::INC16m },
105324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::INC32r,      X86::INC32m },
106324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::INC64_16r,   X86::INC16m },
107324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::INC64_32r,   X86::INC32m },
108324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::INC64r,      X86::INC64m },
109324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::INC8r,       X86::INC8m },
110324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::NEG16r,      X86::NEG16m },
111324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::NEG32r,      X86::NEG32m },
112324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::NEG64r,      X86::NEG64m },
113324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::NEG8r,       X86::NEG8m },
114324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::NOT16r,      X86::NOT16m },
115324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::NOT32r,      X86::NOT32m },
116324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::NOT64r,      X86::NOT64m },
117324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::NOT8r,       X86::NOT8m },
118324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::OR16ri,      X86::OR16mi },
119324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::OR16ri8,     X86::OR16mi8 },
120324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::OR16rr,      X86::OR16mr },
121324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::OR32ri,      X86::OR32mi },
122324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::OR32ri8,     X86::OR32mi8 },
123324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::OR32rr,      X86::OR32mr },
124324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::OR64ri32,    X86::OR64mi32 },
125324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::OR64ri8,     X86::OR64mi8 },
126324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::OR64rr,      X86::OR64mr },
127324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::OR8ri,       X86::OR8mi },
128324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::OR8rr,       X86::OR8mr },
129324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ROL16r1,     X86::ROL16m1 },
130324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ROL16rCL,    X86::ROL16mCL },
131324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ROL16ri,     X86::ROL16mi },
132324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ROL32r1,     X86::ROL32m1 },
133324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ROL32rCL,    X86::ROL32mCL },
134324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ROL32ri,     X86::ROL32mi },
135324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ROL64r1,     X86::ROL64m1 },
136324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ROL64rCL,    X86::ROL64mCL },
137324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ROL64ri,     X86::ROL64mi },
138324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ROL8r1,      X86::ROL8m1 },
139324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ROL8rCL,     X86::ROL8mCL },
140324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ROL8ri,      X86::ROL8mi },
141324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ROR16r1,     X86::ROR16m1 },
142324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ROR16rCL,    X86::ROR16mCL },
143324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ROR16ri,     X86::ROR16mi },
144324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ROR32r1,     X86::ROR32m1 },
145324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ROR32rCL,    X86::ROR32mCL },
146324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ROR32ri,     X86::ROR32mi },
147324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ROR64r1,     X86::ROR64m1 },
148324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ROR64rCL,    X86::ROR64mCL },
149324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ROR64ri,     X86::ROR64mi },
150324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ROR8r1,      X86::ROR8m1 },
151324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ROR8rCL,     X86::ROR8mCL },
152324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::ROR8ri,      X86::ROR8mi },
153324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::SAR16r1,     X86::SAR16m1 },
154324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::SAR16rCL,    X86::SAR16mCL },
155324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::SAR16ri,     X86::SAR16mi },
156324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::SAR32r1,     X86::SAR32m1 },
157324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::SAR32rCL,    X86::SAR32mCL },
158324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::SAR32ri,     X86::SAR32mi },
159324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::SAR64r1,     X86::SAR64m1 },
160324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::SAR64rCL,    X86::SAR64mCL },
161324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::SAR64ri,     X86::SAR64mi },
162324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::SAR8r1,      X86::SAR8m1 },
163324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::SAR8rCL,     X86::SAR8mCL },
164324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::SAR8ri,      X86::SAR8mi },
165324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::SBB32ri,     X86::SBB32mi },
166324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::SBB32ri8,    X86::SBB32mi8 },
167324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::SBB32rr,     X86::SBB32mr },
168324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::SBB64ri32,   X86::SBB64mi32 },
169324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::SBB64ri8,    X86::SBB64mi8 },
170324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::SBB64rr,     X86::SBB64mr },
171324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::SHL16r1,     X86::SHL16m1 },
172324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    { X86::SHL16rCL,    X86::SHL16mCL },
173    { X86::SHL16ri,     X86::SHL16mi },
174    { X86::SHL32r1,     X86::SHL32m1 },
175    { X86::SHL32rCL,    X86::SHL32mCL },
176    { X86::SHL32ri,     X86::SHL32mi },
177    { X86::SHL64r1,     X86::SHL64m1 },
178    { X86::SHL64rCL,    X86::SHL64mCL },
179    { X86::SHL64ri,     X86::SHL64mi },
180    { X86::SHL8r1,      X86::SHL8m1 },
181    { X86::SHL8rCL,     X86::SHL8mCL },
182    { X86::SHL8ri,      X86::SHL8mi },
183    { X86::SHLD16rrCL,  X86::SHLD16mrCL },
184    { X86::SHLD16rri8,  X86::SHLD16mri8 },
185    { X86::SHLD32rrCL,  X86::SHLD32mrCL },
186    { X86::SHLD32rri8,  X86::SHLD32mri8 },
187    { X86::SHLD64rrCL,  X86::SHLD64mrCL },
188    { X86::SHLD64rri8,  X86::SHLD64mri8 },
189    { X86::SHR16r1,     X86::SHR16m1 },
190    { X86::SHR16rCL,    X86::SHR16mCL },
191    { X86::SHR16ri,     X86::SHR16mi },
192    { X86::SHR32r1,     X86::SHR32m1 },
193    { X86::SHR32rCL,    X86::SHR32mCL },
194    { X86::SHR32ri,     X86::SHR32mi },
195    { X86::SHR64r1,     X86::SHR64m1 },
196    { X86::SHR64rCL,    X86::SHR64mCL },
197    { X86::SHR64ri,     X86::SHR64mi },
198    { X86::SHR8r1,      X86::SHR8m1 },
199    { X86::SHR8rCL,     X86::SHR8mCL },
200    { X86::SHR8ri,      X86::SHR8mi },
201    { X86::SHRD16rrCL,  X86::SHRD16mrCL },
202    { X86::SHRD16rri8,  X86::SHRD16mri8 },
203    { X86::SHRD32rrCL,  X86::SHRD32mrCL },
204    { X86::SHRD32rri8,  X86::SHRD32mri8 },
205    { X86::SHRD64rrCL,  X86::SHRD64mrCL },
206    { X86::SHRD64rri8,  X86::SHRD64mri8 },
207    { X86::SUB16ri,     X86::SUB16mi },
208    { X86::SUB16ri8,    X86::SUB16mi8 },
209    { X86::SUB16rr,     X86::SUB16mr },
210    { X86::SUB32ri,     X86::SUB32mi },
211    { X86::SUB32ri8,    X86::SUB32mi8 },
212    { X86::SUB32rr,     X86::SUB32mr },
213    { X86::SUB64ri32,   X86::SUB64mi32 },
214    { X86::SUB64ri8,    X86::SUB64mi8 },
215    { X86::SUB64rr,     X86::SUB64mr },
216    { X86::SUB8ri,      X86::SUB8mi },
217    { X86::SUB8rr,      X86::SUB8mr },
218    { X86::XOR16ri,     X86::XOR16mi },
219    { X86::XOR16ri8,    X86::XOR16mi8 },
220    { X86::XOR16rr,     X86::XOR16mr },
221    { X86::XOR32ri,     X86::XOR32mi },
222    { X86::XOR32ri8,    X86::XOR32mi8 },
223    { X86::XOR32rr,     X86::XOR32mr },
224    { X86::XOR64ri32,   X86::XOR64mi32 },
225    { X86::XOR64ri8,    X86::XOR64mi8 },
226    { X86::XOR64rr,     X86::XOR64mr },
227    { X86::XOR8ri,      X86::XOR8mi },
228    { X86::XOR8rr,      X86::XOR8mr }
229  };
230
231  for (unsigned i = 0, e = array_lengthof(OpTbl2Addr); i != e; ++i) {
232    unsigned RegOp = OpTbl2Addr[i][0];
233    unsigned MemOp = OpTbl2Addr[i][1];
234    if (!RegOp2MemOpTable2Addr.insert(std::make_pair((unsigned*)RegOp, MemOp)))
235      assert(false && "Duplicated entries?");
236    unsigned AuxInfo = 0 | (1 << 4) | (1 << 5); // Index 0,folded load and store
237    if (!MemOp2RegOpTable.insert(std::make_pair((unsigned*)MemOp,
238                                               std::make_pair(RegOp, AuxInfo))))
239      AmbEntries.push_back(MemOp);
240  }
241
242  // If the third value is 1, then it's folding either a load or a store.
243  static const unsigned OpTbl0[][3] = {
244    { X86::CALL32r,     X86::CALL32m, 1 },
245    { X86::CALL64r,     X86::CALL64m, 1 },
246    { X86::CMP16ri,     X86::CMP16mi, 1 },
247    { X86::CMP16ri8,    X86::CMP16mi8, 1 },
248    { X86::CMP32ri,     X86::CMP32mi, 1 },
249    { X86::CMP32ri8,    X86::CMP32mi8, 1 },
250    { X86::CMP64ri32,   X86::CMP64mi32, 1 },
251    { X86::CMP64ri8,    X86::CMP64mi8, 1 },
252    { X86::CMP8ri,      X86::CMP8mi, 1 },
253    { X86::DIV16r,      X86::DIV16m, 1 },
254    { X86::DIV32r,      X86::DIV32m, 1 },
255    { X86::DIV64r,      X86::DIV64m, 1 },
256    { X86::DIV8r,       X86::DIV8m, 1 },
257    { X86::FsMOVAPDrr,  X86::MOVSDmr, 0 },
258    { X86::FsMOVAPSrr,  X86::MOVSSmr, 0 },
259    { X86::IDIV16r,     X86::IDIV16m, 1 },
260    { X86::IDIV32r,     X86::IDIV32m, 1 },
261    { X86::IDIV64r,     X86::IDIV64m, 1 },
262    { X86::IDIV8r,      X86::IDIV8m, 1 },
263    { X86::IMUL16r,     X86::IMUL16m, 1 },
264    { X86::IMUL32r,     X86::IMUL32m, 1 },
265    { X86::IMUL64r,     X86::IMUL64m, 1 },
266    { X86::IMUL8r,      X86::IMUL8m, 1 },
267    { X86::JMP32r,      X86::JMP32m, 1 },
268    { X86::JMP64r,      X86::JMP64m, 1 },
269    { X86::MOV16ri,     X86::MOV16mi, 0 },
270    { X86::MOV16rr,     X86::MOV16mr, 0 },
271    { X86::MOV16to16_,  X86::MOV16_mr, 0 },
272    { X86::MOV32ri,     X86::MOV32mi, 0 },
273    { X86::MOV32rr,     X86::MOV32mr, 0 },
274    { X86::MOV32to32_,  X86::MOV32_mr, 0 },
275    { X86::MOV64ri32,   X86::MOV64mi32, 0 },
276    { X86::MOV64rr,     X86::MOV64mr, 0 },
277    { X86::MOV8ri,      X86::MOV8mi, 0 },
278    { X86::MOV8rr,      X86::MOV8mr, 0 },
279    { X86::MOVAPDrr,    X86::MOVAPDmr, 0 },
280    { X86::MOVAPSrr,    X86::MOVAPSmr, 0 },
281    { X86::MOVPDI2DIrr, X86::MOVPDI2DImr, 0 },
282    { X86::MOVPQIto64rr,X86::MOVPQIto64mr, 0 },
283    { X86::MOVPS2SSrr,  X86::MOVPS2SSmr, 0 },
284    { X86::MOVSDrr,     X86::MOVSDmr, 0 },
285    { X86::MOVSDto64rr, X86::MOVSDto64mr, 0 },
286    { X86::MOVSS2DIrr,  X86::MOVSS2DImr, 0 },
287    { X86::MOVSSrr,     X86::MOVSSmr, 0 },
288    { X86::MOVUPDrr,    X86::MOVUPDmr, 0 },
289    { X86::MOVUPSrr,    X86::MOVUPSmr, 0 },
290    { X86::MUL16r,      X86::MUL16m, 1 },
291    { X86::MUL32r,      X86::MUL32m, 1 },
292    { X86::MUL64r,      X86::MUL64m, 1 },
293    { X86::MUL8r,       X86::MUL8m, 1 },
294    { X86::SETAEr,      X86::SETAEm, 0 },
295    { X86::SETAr,       X86::SETAm, 0 },
296    { X86::SETBEr,      X86::SETBEm, 0 },
297    { X86::SETBr,       X86::SETBm, 0 },
298    { X86::SETEr,       X86::SETEm, 0 },
299    { X86::SETGEr,      X86::SETGEm, 0 },
300    { X86::SETGr,       X86::SETGm, 0 },
301    { X86::SETLEr,      X86::SETLEm, 0 },
302    { X86::SETLr,       X86::SETLm, 0 },
303    { X86::SETNEr,      X86::SETNEm, 0 },
304    { X86::SETNPr,      X86::SETNPm, 0 },
305    { X86::SETNSr,      X86::SETNSm, 0 },
306    { X86::SETPr,       X86::SETPm, 0 },
307    { X86::SETSr,       X86::SETSm, 0 },
308    { X86::TAILJMPr,    X86::TAILJMPm, 1 },
309    { X86::TEST16ri,    X86::TEST16mi, 1 },
310    { X86::TEST32ri,    X86::TEST32mi, 1 },
311    { X86::TEST64ri32,  X86::TEST64mi32, 1 },
312    { X86::TEST8ri,     X86::TEST8mi, 1 },
313    { X86::XCHG16rr,    X86::XCHG16mr, 0 },
314    { X86::XCHG32rr,    X86::XCHG32mr, 0 },
315    { X86::XCHG64rr,    X86::XCHG64mr, 0 },
316    { X86::XCHG8rr,     X86::XCHG8mr, 0 }
317  };
318
319  for (unsigned i = 0, e = array_lengthof(OpTbl0); i != e; ++i) {
320    unsigned RegOp = OpTbl0[i][0];
321    unsigned MemOp = OpTbl0[i][1];
322    if (!RegOp2MemOpTable0.insert(std::make_pair((unsigned*)RegOp, MemOp)))
323      assert(false && "Duplicated entries?");
324    unsigned FoldedLoad = OpTbl0[i][2];
325    // Index 0, folded load or store.
326    unsigned AuxInfo = 0 | (FoldedLoad << 4) | ((FoldedLoad^1) << 5);
327    if (!MemOp2RegOpTable.insert(std::make_pair((unsigned*)MemOp,
328                                               std::make_pair(RegOp, AuxInfo))))
329      AmbEntries.push_back(MemOp);
330  }
331
332  static const unsigned OpTbl1[][2] = {
333    { X86::CMP16rr,         X86::CMP16rm },
334    { X86::CMP32rr,         X86::CMP32rm },
335    { X86::CMP64rr,         X86::CMP64rm },
336    { X86::CMP8rr,          X86::CMP8rm },
337    { X86::CVTSD2SSrr,      X86::CVTSD2SSrm },
338    { X86::CVTSI2SD64rr,    X86::CVTSI2SD64rm },
339    { X86::CVTSI2SDrr,      X86::CVTSI2SDrm },
340    { X86::CVTSI2SS64rr,    X86::CVTSI2SS64rm },
341    { X86::CVTSI2SSrr,      X86::CVTSI2SSrm },
342    { X86::CVTSS2SDrr,      X86::CVTSS2SDrm },
343    { X86::CVTTSD2SI64rr,   X86::CVTTSD2SI64rm },
344    { X86::CVTTSD2SIrr,     X86::CVTTSD2SIrm },
345    { X86::CVTTSS2SI64rr,   X86::CVTTSS2SI64rm },
346    { X86::CVTTSS2SIrr,     X86::CVTTSS2SIrm },
347    { X86::FsMOVAPDrr,      X86::MOVSDrm },
348    { X86::FsMOVAPSrr,      X86::MOVSSrm },
349    { X86::IMUL16rri,       X86::IMUL16rmi },
350    { X86::IMUL16rri8,      X86::IMUL16rmi8 },
351    { X86::IMUL32rri,       X86::IMUL32rmi },
352    { X86::IMUL32rri8,      X86::IMUL32rmi8 },
353    { X86::IMUL64rri32,     X86::IMUL64rmi32 },
354    { X86::IMUL64rri8,      X86::IMUL64rmi8 },
355    { X86::Int_CMPSDrr,     X86::Int_CMPSDrm },
356    { X86::Int_CMPSSrr,     X86::Int_CMPSSrm },
357    { X86::Int_COMISDrr,    X86::Int_COMISDrm },
358    { X86::Int_COMISSrr,    X86::Int_COMISSrm },
359    { X86::Int_CVTDQ2PDrr,  X86::Int_CVTDQ2PDrm },
360    { X86::Int_CVTDQ2PSrr,  X86::Int_CVTDQ2PSrm },
361    { X86::Int_CVTPD2DQrr,  X86::Int_CVTPD2DQrm },
362    { X86::Int_CVTPD2PSrr,  X86::Int_CVTPD2PSrm },
363    { X86::Int_CVTPS2DQrr,  X86::Int_CVTPS2DQrm },
364    { X86::Int_CVTPS2PDrr,  X86::Int_CVTPS2PDrm },
365    { X86::Int_CVTSD2SI64rr,X86::Int_CVTSD2SI64rm },
366    { X86::Int_CVTSD2SIrr,  X86::Int_CVTSD2SIrm },
367    { X86::Int_CVTSD2SSrr,  X86::Int_CVTSD2SSrm },
368    { X86::Int_CVTSI2SD64rr,X86::Int_CVTSI2SD64rm },
369    { X86::Int_CVTSI2SDrr,  X86::Int_CVTSI2SDrm },
370    { X86::Int_CVTSI2SS64rr,X86::Int_CVTSI2SS64rm },
371    { X86::Int_CVTSI2SSrr,  X86::Int_CVTSI2SSrm },
372    { X86::Int_CVTSS2SDrr,  X86::Int_CVTSS2SDrm },
373    { X86::Int_CVTSS2SI64rr,X86::Int_CVTSS2SI64rm },
374    { X86::Int_CVTSS2SIrr,  X86::Int_CVTSS2SIrm },
375    { X86::Int_CVTTPD2DQrr, X86::Int_CVTTPD2DQrm },
376    { X86::Int_CVTTPS2DQrr, X86::Int_CVTTPS2DQrm },
377    { X86::Int_CVTTSD2SI64rr,X86::Int_CVTTSD2SI64rm },
378    { X86::Int_CVTTSD2SIrr, X86::Int_CVTTSD2SIrm },
379    { X86::Int_CVTTSS2SI64rr,X86::Int_CVTTSS2SI64rm },
380    { X86::Int_CVTTSS2SIrr, X86::Int_CVTTSS2SIrm },
381    { X86::Int_UCOMISDrr,   X86::Int_UCOMISDrm },
382    { X86::Int_UCOMISSrr,   X86::Int_UCOMISSrm },
383    { X86::MOV16rr,         X86::MOV16rm },
384    { X86::MOV16to16_,      X86::MOV16_rm },
385    { X86::MOV32rr,         X86::MOV32rm },
386    { X86::MOV32to32_,      X86::MOV32_rm },
387    { X86::MOV64rr,         X86::MOV64rm },
388    { X86::MOV64toPQIrr,    X86::MOV64toPQIrm },
389    { X86::MOV64toSDrr,     X86::MOV64toSDrm },
390    { X86::MOV8rr,          X86::MOV8rm },
391    { X86::MOVAPDrr,        X86::MOVAPDrm },
392    { X86::MOVAPSrr,        X86::MOVAPSrm },
393    { X86::MOVDDUPrr,       X86::MOVDDUPrm },
394    { X86::MOVDI2PDIrr,     X86::MOVDI2PDIrm },
395    { X86::MOVDI2SSrr,      X86::MOVDI2SSrm },
396    { X86::MOVSD2PDrr,      X86::MOVSD2PDrm },
397    { X86::MOVSDrr,         X86::MOVSDrm },
398    { X86::MOVSHDUPrr,      X86::MOVSHDUPrm },
399    { X86::MOVSLDUPrr,      X86::MOVSLDUPrm },
400    { X86::MOVSS2PSrr,      X86::MOVSS2PSrm },
401    { X86::MOVSSrr,         X86::MOVSSrm },
402    { X86::MOVSX16rr8,      X86::MOVSX16rm8 },
403    { X86::MOVSX32rr16,     X86::MOVSX32rm16 },
404    { X86::MOVSX32rr8,      X86::MOVSX32rm8 },
405    { X86::MOVSX64rr16,     X86::MOVSX64rm16 },
406    { X86::MOVSX64rr32,     X86::MOVSX64rm32 },
407    { X86::MOVSX64rr8,      X86::MOVSX64rm8 },
408    { X86::MOVUPDrr,        X86::MOVUPDrm },
409    { X86::MOVUPSrr,        X86::MOVUPSrm },
410    { X86::MOVZX16rr8,      X86::MOVZX16rm8 },
411    { X86::MOVZX32rr16,     X86::MOVZX32rm16 },
412    { X86::MOVZX32rr8,      X86::MOVZX32rm8 },
413    { X86::MOVZX64rr16,     X86::MOVZX64rm16 },
414    { X86::MOVZX64rr8,      X86::MOVZX64rm8 },
415    { X86::PSHUFDri,        X86::PSHUFDmi },
416    { X86::PSHUFHWri,       X86::PSHUFHWmi },
417    { X86::PSHUFLWri,       X86::PSHUFLWmi },
418    { X86::PsMOVZX64rr32,   X86::PsMOVZX64rm32 },
419    { X86::RCPPSr,          X86::RCPPSm },
420    { X86::RCPPSr_Int,      X86::RCPPSm_Int },
421    { X86::RSQRTPSr,        X86::RSQRTPSm },
422    { X86::RSQRTPSr_Int,    X86::RSQRTPSm_Int },
423    { X86::RSQRTSSr,        X86::RSQRTSSm },
424    { X86::RSQRTSSr_Int,    X86::RSQRTSSm_Int },
425    { X86::SQRTPDr,         X86::SQRTPDm },
426    { X86::SQRTPDr_Int,     X86::SQRTPDm_Int },
427    { X86::SQRTPSr,         X86::SQRTPSm },
428    { X86::SQRTPSr_Int,     X86::SQRTPSm_Int },
429    { X86::SQRTSDr,         X86::SQRTSDm },
430    { X86::SQRTSDr_Int,     X86::SQRTSDm_Int },
431    { X86::SQRTSSr,         X86::SQRTSSm },
432    { X86::SQRTSSr_Int,     X86::SQRTSSm_Int },
433    { X86::TEST16rr,        X86::TEST16rm },
434    { X86::TEST32rr,        X86::TEST32rm },
435    { X86::TEST64rr,        X86::TEST64rm },
436    { X86::TEST8rr,         X86::TEST8rm },
437    // FIXME: TEST*rr EAX,EAX ---> CMP [mem], 0
438    { X86::UCOMISDrr,       X86::UCOMISDrm },
439    { X86::UCOMISSrr,       X86::UCOMISSrm },
440    { X86::XCHG16rr,        X86::XCHG16rm },
441    { X86::XCHG32rr,        X86::XCHG32rm },
442    { X86::XCHG64rr,        X86::XCHG64rm },
443    { X86::XCHG8rr,         X86::XCHG8rm }
444  };
445
446  for (unsigned i = 0, e = array_lengthof(OpTbl1); i != e; ++i) {
447    unsigned RegOp = OpTbl1[i][0];
448    unsigned MemOp = OpTbl1[i][1];
449    if (!RegOp2MemOpTable1.insert(std::make_pair((unsigned*)RegOp, MemOp)))
450      assert(false && "Duplicated entries?");
451    unsigned AuxInfo = 1 | (1 << 4); // Index 1, folded load
452    if (!MemOp2RegOpTable.insert(std::make_pair((unsigned*)MemOp,
453                                               std::make_pair(RegOp, AuxInfo))))
454      AmbEntries.push_back(MemOp);
455  }
456
457  static const unsigned OpTbl2[][2] = {
458    { X86::ADC32rr,         X86::ADC32rm },
459    { X86::ADC64rr,         X86::ADC64rm },
460    { X86::ADD16rr,         X86::ADD16rm },
461    { X86::ADD32rr,         X86::ADD32rm },
462    { X86::ADD64rr,         X86::ADD64rm },
463    { X86::ADD8rr,          X86::ADD8rm },
464    { X86::ADDPDrr,         X86::ADDPDrm },
465    { X86::ADDPSrr,         X86::ADDPSrm },
466    { X86::ADDSDrr,         X86::ADDSDrm },
467    { X86::ADDSSrr,         X86::ADDSSrm },
468    { X86::ADDSUBPDrr,      X86::ADDSUBPDrm },
469    { X86::ADDSUBPSrr,      X86::ADDSUBPSrm },
470    { X86::AND16rr,         X86::AND16rm },
471    { X86::AND32rr,         X86::AND32rm },
472    { X86::AND64rr,         X86::AND64rm },
473    { X86::AND8rr,          X86::AND8rm },
474    { X86::ANDNPDrr,        X86::ANDNPDrm },
475    { X86::ANDNPSrr,        X86::ANDNPSrm },
476    { X86::ANDPDrr,         X86::ANDPDrm },
477    { X86::ANDPSrr,         X86::ANDPSrm },
478    { X86::CMOVA16rr,       X86::CMOVA16rm },
479    { X86::CMOVA32rr,       X86::CMOVA32rm },
480    { X86::CMOVA64rr,       X86::CMOVA64rm },
481    { X86::CMOVAE16rr,      X86::CMOVAE16rm },
482    { X86::CMOVAE32rr,      X86::CMOVAE32rm },
483    { X86::CMOVAE64rr,      X86::CMOVAE64rm },
484    { X86::CMOVB16rr,       X86::CMOVB16rm },
485    { X86::CMOVB32rr,       X86::CMOVB32rm },
486    { X86::CMOVB64rr,       X86::CMOVB64rm },
487    { X86::CMOVBE16rr,      X86::CMOVBE16rm },
488    { X86::CMOVBE32rr,      X86::CMOVBE32rm },
489    { X86::CMOVBE64rr,      X86::CMOVBE64rm },
490    { X86::CMOVE16rr,       X86::CMOVE16rm },
491    { X86::CMOVE32rr,       X86::CMOVE32rm },
492    { X86::CMOVE64rr,       X86::CMOVE64rm },
493    { X86::CMOVG16rr,       X86::CMOVG16rm },
494    { X86::CMOVG32rr,       X86::CMOVG32rm },
495    { X86::CMOVG64rr,       X86::CMOVG64rm },
496    { X86::CMOVGE16rr,      X86::CMOVGE16rm },
497    { X86::CMOVGE32rr,      X86::CMOVGE32rm },
498    { X86::CMOVGE64rr,      X86::CMOVGE64rm },
499    { X86::CMOVL16rr,       X86::CMOVL16rm },
500    { X86::CMOVL32rr,       X86::CMOVL32rm },
501    { X86::CMOVL64rr,       X86::CMOVL64rm },
502    { X86::CMOVLE16rr,      X86::CMOVLE16rm },
503    { X86::CMOVLE32rr,      X86::CMOVLE32rm },
504    { X86::CMOVLE64rr,      X86::CMOVLE64rm },
505    { X86::CMOVNE16rr,      X86::CMOVNE16rm },
506    { X86::CMOVNE32rr,      X86::CMOVNE32rm },
507    { X86::CMOVNE64rr,      X86::CMOVNE64rm },
508    { X86::CMOVNP16rr,      X86::CMOVNP16rm },
509    { X86::CMOVNP32rr,      X86::CMOVNP32rm },
510    { X86::CMOVNP64rr,      X86::CMOVNP64rm },
511    { X86::CMOVNS16rr,      X86::CMOVNS16rm },
512    { X86::CMOVNS32rr,      X86::CMOVNS32rm },
513    { X86::CMOVNS64rr,      X86::CMOVNS64rm },
514    { X86::CMOVP16rr,       X86::CMOVP16rm },
515    { X86::CMOVP32rr,       X86::CMOVP32rm },
516    { X86::CMOVP64rr,       X86::CMOVP64rm },
517    { X86::CMOVS16rr,       X86::CMOVS16rm },
518    { X86::CMOVS32rr,       X86::CMOVS32rm },
519    { X86::CMOVS64rr,       X86::CMOVS64rm },
520    { X86::CMPPDrri,        X86::CMPPDrmi },
521    { X86::CMPPSrri,        X86::CMPPSrmi },
522    { X86::CMPSDrr,         X86::CMPSDrm },
523    { X86::CMPSSrr,         X86::CMPSSrm },
524    { X86::DIVPDrr,         X86::DIVPDrm },
525    { X86::DIVPSrr,         X86::DIVPSrm },
526    { X86::DIVSDrr,         X86::DIVSDrm },
527    { X86::DIVSSrr,         X86::DIVSSrm },
528    { X86::HADDPDrr,        X86::HADDPDrm },
529    { X86::HADDPSrr,        X86::HADDPSrm },
530    { X86::HSUBPDrr,        X86::HSUBPDrm },
531    { X86::HSUBPSrr,        X86::HSUBPSrm },
532    { X86::IMUL16rr,        X86::IMUL16rm },
533    { X86::IMUL32rr,        X86::IMUL32rm },
534    { X86::IMUL64rr,        X86::IMUL64rm },
535    { X86::MAXPDrr,         X86::MAXPDrm },
536    { X86::MAXPDrr_Int,     X86::MAXPDrm_Int },
537    { X86::MAXPSrr,         X86::MAXPSrm },
538    { X86::MAXPSrr_Int,     X86::MAXPSrm_Int },
539    { X86::MAXSDrr,         X86::MAXSDrm },
540    { X86::MAXSDrr_Int,     X86::MAXSDrm_Int },
541    { X86::MAXSSrr,         X86::MAXSSrm },
542    { X86::MAXSSrr_Int,     X86::MAXSSrm_Int },
543    { X86::MINPDrr,         X86::MINPDrm },
544    { X86::MINPDrr_Int,     X86::MINPDrm_Int },
545    { X86::MINPSrr,         X86::MINPSrm },
546    { X86::MINPSrr_Int,     X86::MINPSrm_Int },
547    { X86::MINSDrr,         X86::MINSDrm },
548    { X86::MINSDrr_Int,     X86::MINSDrm_Int },
549    { X86::MINSSrr,         X86::MINSSrm },
550    { X86::MINSSrr_Int,     X86::MINSSrm_Int },
551    { X86::MULPDrr,         X86::MULPDrm },
552    { X86::MULPSrr,         X86::MULPSrm },
553    { X86::MULSDrr,         X86::MULSDrm },
554    { X86::MULSSrr,         X86::MULSSrm },
555    { X86::OR16rr,          X86::OR16rm },
556    { X86::OR32rr,          X86::OR32rm },
557    { X86::OR64rr,          X86::OR64rm },
558    { X86::OR8rr,           X86::OR8rm },
559    { X86::ORPDrr,          X86::ORPDrm },
560    { X86::ORPSrr,          X86::ORPSrm },
561    { X86::PACKSSDWrr,      X86::PACKSSDWrm },
562    { X86::PACKSSWBrr,      X86::PACKSSWBrm },
563    { X86::PACKUSWBrr,      X86::PACKUSWBrm },
564    { X86::PADDBrr,         X86::PADDBrm },
565    { X86::PADDDrr,         X86::PADDDrm },
566    { X86::PADDQrr,         X86::PADDQrm },
567    { X86::PADDSBrr,        X86::PADDSBrm },
568    { X86::PADDSWrr,        X86::PADDSWrm },
569    { X86::PADDWrr,         X86::PADDWrm },
570    { X86::PANDNrr,         X86::PANDNrm },
571    { X86::PANDrr,          X86::PANDrm },
572    { X86::PAVGBrr,         X86::PAVGBrm },
573    { X86::PAVGWrr,         X86::PAVGWrm },
574    { X86::PCMPEQBrr,       X86::PCMPEQBrm },
575    { X86::PCMPEQDrr,       X86::PCMPEQDrm },
576    { X86::PCMPEQWrr,       X86::PCMPEQWrm },
577    { X86::PCMPGTBrr,       X86::PCMPGTBrm },
578    { X86::PCMPGTDrr,       X86::PCMPGTDrm },
579    { X86::PCMPGTWrr,       X86::PCMPGTWrm },
580    { X86::PINSRWrri,       X86::PINSRWrmi },
581    { X86::PMADDWDrr,       X86::PMADDWDrm },
582    { X86::PMAXSWrr,        X86::PMAXSWrm },
583    { X86::PMAXUBrr,        X86::PMAXUBrm },
584    { X86::PMINSWrr,        X86::PMINSWrm },
585    { X86::PMINUBrr,        X86::PMINUBrm },
586    { X86::PMULHUWrr,       X86::PMULHUWrm },
587    { X86::PMULHWrr,        X86::PMULHWrm },
588    { X86::PMULLWrr,        X86::PMULLWrm },
589    { X86::PMULUDQrr,       X86::PMULUDQrm },
590    { X86::PORrr,           X86::PORrm },
591    { X86::PSADBWrr,        X86::PSADBWrm },
592    { X86::PSLLDrr,         X86::PSLLDrm },
593    { X86::PSLLQrr,         X86::PSLLQrm },
594    { X86::PSLLWrr,         X86::PSLLWrm },
595    { X86::PSRADrr,         X86::PSRADrm },
596    { X86::PSRAWrr,         X86::PSRAWrm },
597    { X86::PSRLDrr,         X86::PSRLDrm },
598    { X86::PSRLQrr,         X86::PSRLQrm },
599    { X86::PSRLWrr,         X86::PSRLWrm },
600    { X86::PSUBBrr,         X86::PSUBBrm },
601    { X86::PSUBDrr,         X86::PSUBDrm },
602    { X86::PSUBSBrr,        X86::PSUBSBrm },
603    { X86::PSUBSWrr,        X86::PSUBSWrm },
604    { X86::PSUBWrr,         X86::PSUBWrm },
605    { X86::PUNPCKHBWrr,     X86::PUNPCKHBWrm },
606    { X86::PUNPCKHDQrr,     X86::PUNPCKHDQrm },
607    { X86::PUNPCKHQDQrr,    X86::PUNPCKHQDQrm },
608    { X86::PUNPCKHWDrr,     X86::PUNPCKHWDrm },
609    { X86::PUNPCKLBWrr,     X86::PUNPCKLBWrm },
610    { X86::PUNPCKLDQrr,     X86::PUNPCKLDQrm },
611    { X86::PUNPCKLQDQrr,    X86::PUNPCKLQDQrm },
612    { X86::PUNPCKLWDrr,     X86::PUNPCKLWDrm },
613    { X86::PXORrr,          X86::PXORrm },
614    { X86::SBB32rr,         X86::SBB32rm },
615    { X86::SBB64rr,         X86::SBB64rm },
616    { X86::SHUFPDrri,       X86::SHUFPDrmi },
617    { X86::SHUFPSrri,       X86::SHUFPSrmi },
618    { X86::SUB16rr,         X86::SUB16rm },
619    { X86::SUB32rr,         X86::SUB32rm },
620    { X86::SUB64rr,         X86::SUB64rm },
621    { X86::SUB8rr,          X86::SUB8rm },
622    { X86::SUBPDrr,         X86::SUBPDrm },
623    { X86::SUBPSrr,         X86::SUBPSrm },
624    { X86::SUBSDrr,         X86::SUBSDrm },
625    { X86::SUBSSrr,         X86::SUBSSrm },
626    // FIXME: TEST*rr -> swapped operand of TEST*mr.
627    { X86::UNPCKHPDrr,      X86::UNPCKHPDrm },
628    { X86::UNPCKHPSrr,      X86::UNPCKHPSrm },
629    { X86::UNPCKLPDrr,      X86::UNPCKLPDrm },
630    { X86::UNPCKLPSrr,      X86::UNPCKLPSrm },
631    { X86::XOR16rr,         X86::XOR16rm },
632    { X86::XOR32rr,         X86::XOR32rm },
633    { X86::XOR64rr,         X86::XOR64rm },
634    { X86::XOR8rr,          X86::XOR8rm },
635    { X86::XORPDrr,         X86::XORPDrm },
636    { X86::XORPSrr,         X86::XORPSrm }
637  };
638
639  for (unsigned i = 0, e = array_lengthof(OpTbl2); i != e; ++i) {
640    unsigned RegOp = OpTbl2[i][0];
641    unsigned MemOp = OpTbl2[i][1];
642    if (!RegOp2MemOpTable2.insert(std::make_pair((unsigned*)RegOp, MemOp)))
643      assert(false && "Duplicated entries?");
644    unsigned AuxInfo = 2 | (1 << 4); // Index 1, folded load
645    if (!MemOp2RegOpTable.insert(std::make_pair((unsigned*)MemOp,
646                                               std::make_pair(RegOp, AuxInfo))))
647      AmbEntries.push_back(MemOp);
648  }
649
650  // Remove ambiguous entries.
651  for (unsigned i = 0, e = AmbEntries.size(); i != e; ++i)
652    MemOp2RegOpTable.erase((unsigned*)AmbEntries[i]);
653}
654
655// getX86RegNum - This function maps LLVM register identifiers to their X86
656// specific numbering, which is used in various places encoding instructions.
657//
658unsigned X86RegisterInfo::getX86RegNum(unsigned RegNo) {
659  switch(RegNo) {
660  case X86::RAX: case X86::EAX: case X86::AX: case X86::AL: return N86::EAX;
661  case X86::RCX: case X86::ECX: case X86::CX: case X86::CL: return N86::ECX;
662  case X86::RDX: case X86::EDX: case X86::DX: case X86::DL: return N86::EDX;
663  case X86::RBX: case X86::EBX: case X86::BX: case X86::BL: return N86::EBX;
664  case X86::RSP: case X86::ESP: case X86::SP: case X86::SPL: case X86::AH:
665    return N86::ESP;
666  case X86::RBP: case X86::EBP: case X86::BP: case X86::BPL: case X86::CH:
667    return N86::EBP;
668  case X86::RSI: case X86::ESI: case X86::SI: case X86::SIL: case X86::DH:
669    return N86::ESI;
670  case X86::RDI: case X86::EDI: case X86::DI: case X86::DIL: case X86::BH:
671    return N86::EDI;
672
673  case X86::R8:  case X86::R8D:  case X86::R8W:  case X86::R8B:
674    return N86::EAX;
675  case X86::R9:  case X86::R9D:  case X86::R9W:  case X86::R9B:
676    return N86::ECX;
677  case X86::R10: case X86::R10D: case X86::R10W: case X86::R10B:
678    return N86::EDX;
679  case X86::R11: case X86::R11D: case X86::R11W: case X86::R11B:
680    return N86::EBX;
681  case X86::R12: case X86::R12D: case X86::R12W: case X86::R12B:
682    return N86::ESP;
683  case X86::R13: case X86::R13D: case X86::R13W: case X86::R13B:
684    return N86::EBP;
685  case X86::R14: case X86::R14D: case X86::R14W: case X86::R14B:
686    return N86::ESI;
687  case X86::R15: case X86::R15D: case X86::R15W: case X86::R15B:
688    return N86::EDI;
689
690  case X86::ST0: case X86::ST1: case X86::ST2: case X86::ST3:
691  case X86::ST4: case X86::ST5: case X86::ST6: case X86::ST7:
692    return RegNo-X86::ST0;
693
694  case X86::XMM0:  case X86::XMM1:  case X86::XMM2:  case X86::XMM3:
695  case X86::XMM4:  case X86::XMM5:  case X86::XMM6:  case X86::XMM7:
696    return getDwarfRegNum(RegNo) - getDwarfRegNum(X86::XMM0);
697  case X86::XMM8:  case X86::XMM9:  case X86::XMM10: case X86::XMM11:
698  case X86::XMM12: case X86::XMM13: case X86::XMM14: case X86::XMM15:
699    return getDwarfRegNum(RegNo) - getDwarfRegNum(X86::XMM8);
700
701  default:
702    assert(isVirtualRegister(RegNo) && "Unknown physical register!");
703    assert(0 && "Register allocator hasn't allocated reg correctly yet!");
704    return 0;
705  }
706}
707
708bool X86RegisterInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
709                                                MachineBasicBlock::iterator MI,
710                                const std::vector<CalleeSavedInfo> &CSI) const {
711  if (CSI.empty())
712    return false;
713
714  MachineFunction &MF = *MBB.getParent();
715  X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
716  X86FI->setCalleeSavedFrameSize(CSI.size() * SlotSize);
717  unsigned Opc = Is64Bit ? X86::PUSH64r : X86::PUSH32r;
718  for (unsigned i = CSI.size(); i != 0; --i) {
719    unsigned Reg = CSI[i-1].getReg();
720    // Add the callee-saved register as live-in. It's killed at the spill.
721    MBB.addLiveIn(Reg);
722    BuildMI(MBB, MI, TII.get(Opc)).addReg(Reg);
723  }
724  return true;
725}
726
727bool X86RegisterInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
728                                                 MachineBasicBlock::iterator MI,
729                                const std::vector<CalleeSavedInfo> &CSI) const {
730  if (CSI.empty())
731    return false;
732
733  unsigned Opc = Is64Bit ? X86::POP64r : X86::POP32r;
734  for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
735    unsigned Reg = CSI[i].getReg();
736    BuildMI(MBB, MI, TII.get(Opc), Reg);
737  }
738  return true;
739}
740
741static const MachineInstrBuilder &X86InstrAddOperand(MachineInstrBuilder &MIB,
742                                                     MachineOperand &MO) {
743  if (MO.isRegister())
744    MIB = MIB.addReg(MO.getReg(), MO.isDef(), MO.isImplicit());
745  else if (MO.isImmediate())
746    MIB = MIB.addImm(MO.getImm());
747  else if (MO.isFrameIndex())
748    MIB = MIB.addFrameIndex(MO.getFrameIndex());
749  else if (MO.isGlobalAddress())
750    MIB = MIB.addGlobalAddress(MO.getGlobal(), MO.getOffset());
751  else if (MO.isConstantPoolIndex())
752    MIB = MIB.addConstantPoolIndex(MO.getConstantPoolIndex(), MO.getOffset());
753  else if (MO.isJumpTableIndex())
754    MIB = MIB.addJumpTableIndex(MO.getJumpTableIndex());
755  else if (MO.isExternalSymbol())
756    MIB = MIB.addExternalSymbol(MO.getSymbolName());
757  else
758    assert(0 && "Unknown operand for X86InstrAddOperand!");
759
760  return MIB;
761}
762
763static unsigned getStoreRegOpcode(const TargetRegisterClass *RC) {
764  unsigned Opc = 0;
765  if (RC == &X86::GR64RegClass) {
766    Opc = X86::MOV64mr;
767  } else if (RC == &X86::GR32RegClass) {
768    Opc = X86::MOV32mr;
769  } else if (RC == &X86::GR16RegClass) {
770    Opc = X86::MOV16mr;
771  } else if (RC == &X86::GR8RegClass) {
772    Opc = X86::MOV8mr;
773  } else if (RC == &X86::GR32_RegClass) {
774    Opc = X86::MOV32_mr;
775  } else if (RC == &X86::GR16_RegClass) {
776    Opc = X86::MOV16_mr;
777  } else if (RC == &X86::RFP80RegClass) {
778    Opc = X86::ST_FpP80m;   // pops
779  } else if (RC == &X86::RFP64RegClass) {
780    Opc = X86::ST_Fp64m;
781  } else if (RC == &X86::RFP32RegClass) {
782    Opc = X86::ST_Fp32m;
783  } else if (RC == &X86::FR32RegClass) {
784    Opc = X86::MOVSSmr;
785  } else if (RC == &X86::FR64RegClass) {
786    Opc = X86::MOVSDmr;
787  } else if (RC == &X86::VR128RegClass) {
788    Opc = X86::MOVAPSmr;
789  } else if (RC == &X86::VR64RegClass) {
790    Opc = X86::MMX_MOVQ64mr;
791  } else {
792    assert(0 && "Unknown regclass");
793    abort();
794  }
795
796  return Opc;
797}
798
799void X86RegisterInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
800                                          MachineBasicBlock::iterator MI,
801                                          unsigned SrcReg, int FrameIdx,
802                                          const TargetRegisterClass *RC) const {
803  unsigned Opc = getStoreRegOpcode(RC);
804  addFrameReference(BuildMI(MBB, MI, TII.get(Opc)), FrameIdx)
805    .addReg(SrcReg, false, false, true);
806}
807
808void X86RegisterInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
809                                     SmallVectorImpl<MachineOperand> &Addr,
810                                     const TargetRegisterClass *RC,
811                                 SmallVectorImpl<MachineInstr*> &NewMIs) const {
812  unsigned Opc = getStoreRegOpcode(RC);
813  MachineInstrBuilder MIB = BuildMI(TII.get(Opc));
814  for (unsigned i = 0, e = Addr.size(); i != e; ++i)
815    MIB = X86InstrAddOperand(MIB, Addr[i]);
816  MIB.addReg(SrcReg, false, false, true);
817  NewMIs.push_back(MIB);
818}
819
820static unsigned getLoadRegOpcode(const TargetRegisterClass *RC) {
821  unsigned Opc = 0;
822  if (RC == &X86::GR64RegClass) {
823    Opc = X86::MOV64rm;
824  } else if (RC == &X86::GR32RegClass) {
825    Opc = X86::MOV32rm;
826  } else if (RC == &X86::GR16RegClass) {
827    Opc = X86::MOV16rm;
828  } else if (RC == &X86::GR8RegClass) {
829    Opc = X86::MOV8rm;
830  } else if (RC == &X86::GR32_RegClass) {
831    Opc = X86::MOV32_rm;
832  } else if (RC == &X86::GR16_RegClass) {
833    Opc = X86::MOV16_rm;
834  } else if (RC == &X86::RFP80RegClass) {
835    Opc = X86::LD_Fp80m;
836  } else if (RC == &X86::RFP64RegClass) {
837    Opc = X86::LD_Fp64m;
838  } else if (RC == &X86::RFP32RegClass) {
839    Opc = X86::LD_Fp32m;
840  } else if (RC == &X86::FR32RegClass) {
841    Opc = X86::MOVSSrm;
842  } else if (RC == &X86::FR64RegClass) {
843    Opc = X86::MOVSDrm;
844  } else if (RC == &X86::VR128RegClass) {
845    Opc = X86::MOVAPSrm;
846  } else if (RC == &X86::VR64RegClass) {
847    Opc = X86::MMX_MOVQ64rm;
848  } else {
849    assert(0 && "Unknown regclass");
850    abort();
851  }
852
853  return Opc;
854}
855
856void X86RegisterInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
857                                           MachineBasicBlock::iterator MI,
858                                           unsigned DestReg, int FrameIdx,
859                                           const TargetRegisterClass *RC) const{
860  unsigned Opc = getLoadRegOpcode(RC);
861  addFrameReference(BuildMI(MBB, MI, TII.get(Opc), DestReg), FrameIdx);
862}
863
864void X86RegisterInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
865                                      SmallVectorImpl<MachineOperand> &Addr,
866                                      const TargetRegisterClass *RC,
867                                 SmallVectorImpl<MachineInstr*> &NewMIs) const {
868  unsigned Opc = getLoadRegOpcode(RC);
869  MachineInstrBuilder MIB = BuildMI(TII.get(Opc), DestReg);
870  for (unsigned i = 0, e = Addr.size(); i != e; ++i)
871    MIB = X86InstrAddOperand(MIB, Addr[i]);
872  NewMIs.push_back(MIB);
873}
874
875void X86RegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
876                                   MachineBasicBlock::iterator MI,
877                                   unsigned DestReg, unsigned SrcReg,
878                                   const TargetRegisterClass *DestRC,
879                                   const TargetRegisterClass *SrcRC) const {
880  if (DestRC != SrcRC) {
881    // Moving EFLAGS to / from another register requires a push and a pop.
882    if (SrcRC == &X86::CCRRegClass) {
883      assert(SrcReg == X86::EFLAGS);
884      if (DestRC == &X86::GR64RegClass) {
885        BuildMI(MBB, MI, TII.get(X86::PUSHFQ));
886        BuildMI(MBB, MI, TII.get(X86::POP64r), DestReg);
887        return;
888      } else if (DestRC == &X86::GR32RegClass) {
889        BuildMI(MBB, MI, TII.get(X86::PUSHFD));
890        BuildMI(MBB, MI, TII.get(X86::POP32r), DestReg);
891        return;
892      }
893    } else if (DestRC == &X86::CCRRegClass) {
894      assert(DestReg == X86::EFLAGS);
895      if (SrcRC == &X86::GR64RegClass) {
896        BuildMI(MBB, MI, TII.get(X86::PUSH64r)).addReg(SrcReg);
897        BuildMI(MBB, MI, TII.get(X86::POPFQ));
898        return;
899      } else if (SrcRC == &X86::GR32RegClass) {
900        BuildMI(MBB, MI, TII.get(X86::PUSH32r)).addReg(SrcReg);
901        BuildMI(MBB, MI, TII.get(X86::POPFD));
902        return;
903      }
904    }
905    cerr << "Not yet supported!";
906    abort();
907  }
908
909  unsigned Opc;
910  if (DestRC == &X86::GR64RegClass) {
911    Opc = X86::MOV64rr;
912  } else if (DestRC == &X86::GR32RegClass) {
913    Opc = X86::MOV32rr;
914  } else if (DestRC == &X86::GR16RegClass) {
915    Opc = X86::MOV16rr;
916  } else if (DestRC == &X86::GR8RegClass) {
917    Opc = X86::MOV8rr;
918  } else if (DestRC == &X86::GR32_RegClass) {
919    Opc = X86::MOV32_rr;
920  } else if (DestRC == &X86::GR16_RegClass) {
921    Opc = X86::MOV16_rr;
922  } else if (DestRC == &X86::RFP32RegClass) {
923    Opc = X86::MOV_Fp3232;
924  } else if (DestRC == &X86::RFP64RegClass || DestRC == &X86::RSTRegClass) {
925    Opc = X86::MOV_Fp6464;
926  } else if (DestRC == &X86::RFP80RegClass) {
927    Opc = X86::MOV_Fp8080;
928  } else if (DestRC == &X86::FR32RegClass) {
929    Opc = X86::FsMOVAPSrr;
930  } else if (DestRC == &X86::FR64RegClass) {
931    Opc = X86::FsMOVAPDrr;
932  } else if (DestRC == &X86::VR128RegClass) {
933    Opc = X86::MOVAPSrr;
934  } else if (DestRC == &X86::VR64RegClass) {
935    Opc = X86::MMX_MOVQ64rr;
936  } else {
937    assert(0 && "Unknown regclass");
938    abort();
939  }
940  BuildMI(MBB, MI, TII.get(Opc), DestReg).addReg(SrcReg);
941}
942
943const TargetRegisterClass *
944X86RegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const {
945  if (RC == &X86::CCRRegClass)
946    if (Is64Bit)
947      return &X86::GR64RegClass;
948    else
949      return &X86::GR32RegClass;
950  return NULL;
951}
952
953void X86RegisterInfo::reMaterialize(MachineBasicBlock &MBB,
954                                    MachineBasicBlock::iterator I,
955                                    unsigned DestReg,
956                                    const MachineInstr *Orig) const {
957  // MOV32r0 etc. are implemented with xor which clobbers condition code.
958  // Re-materialize them as movri instructions to avoid side effects.
959  switch (Orig->getOpcode()) {
960  case X86::MOV8r0:
961    BuildMI(MBB, I, TII.get(X86::MOV8ri), DestReg).addImm(0);
962    break;
963  case X86::MOV16r0:
964    BuildMI(MBB, I, TII.get(X86::MOV16ri), DestReg).addImm(0);
965    break;
966  case X86::MOV32r0:
967    BuildMI(MBB, I, TII.get(X86::MOV32ri), DestReg).addImm(0);
968    break;
969  case X86::MOV64r0:
970    BuildMI(MBB, I, TII.get(X86::MOV64ri32), DestReg).addImm(0);
971    break;
972  default: {
973    MachineInstr *MI = Orig->clone();
974    MI->getOperand(0).setReg(DestReg);
975    MBB.insert(I, MI);
976    break;
977  }
978  }
979}
980
981static MachineInstr *FuseTwoAddrInst(unsigned Opcode,
982                                     SmallVector<MachineOperand,4> &MOs,
983                                 MachineInstr *MI, const TargetInstrInfo &TII) {
984  unsigned NumOps = TII.getNumOperands(MI->getOpcode())-2;
985
986  // Create the base instruction with the memory operand as the first part.
987  MachineInstrBuilder MIB = BuildMI(TII.get(Opcode));
988  unsigned NumAddrOps = MOs.size();
989  for (unsigned i = 0; i != NumAddrOps; ++i)
990    MIB = X86InstrAddOperand(MIB, MOs[i]);
991  if (NumAddrOps < 4)  // FrameIndex only
992    MIB.addImm(1).addReg(0).addImm(0);
993
994  // Loop over the rest of the ri operands, converting them over.
995  for (unsigned i = 0; i != NumOps; ++i) {
996    MachineOperand &MO = MI->getOperand(i+2);
997    MIB = X86InstrAddOperand(MIB, MO);
998  }
999  return MIB;
1000}
1001
1002static MachineInstr *FuseInst(unsigned Opcode, unsigned OpNo,
1003                              SmallVector<MachineOperand,4> &MOs,
1004                              MachineInstr *MI, const TargetInstrInfo &TII) {
1005  MachineInstrBuilder MIB = BuildMI(TII.get(Opcode));
1006
1007  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1008    MachineOperand &MO = MI->getOperand(i);
1009    if (i == OpNo) {
1010      assert(MO.isRegister() && "Expected to fold into reg operand!");
1011      unsigned NumAddrOps = MOs.size();
1012      for (unsigned i = 0; i != NumAddrOps; ++i)
1013        MIB = X86InstrAddOperand(MIB, MOs[i]);
1014      if (NumAddrOps < 4)  // FrameIndex only
1015        MIB.addImm(1).addReg(0).addImm(0);
1016    } else {
1017      MIB = X86InstrAddOperand(MIB, MO);
1018    }
1019  }
1020  return MIB;
1021}
1022
1023static MachineInstr *MakeM0Inst(const TargetInstrInfo &TII, unsigned Opcode,
1024                                SmallVector<MachineOperand,4> &MOs,
1025                                MachineInstr *MI) {
1026  MachineInstrBuilder MIB = BuildMI(TII.get(Opcode));
1027
1028  unsigned NumAddrOps = MOs.size();
1029  for (unsigned i = 0; i != NumAddrOps; ++i)
1030    MIB = X86InstrAddOperand(MIB, MOs[i]);
1031  if (NumAddrOps < 4)  // FrameIndex only
1032    MIB.addImm(1).addReg(0).addImm(0);
1033  return MIB.addImm(0);
1034}
1035
1036MachineInstr*
1037X86RegisterInfo::foldMemoryOperand(MachineInstr *MI, unsigned i,
1038                                   SmallVector<MachineOperand,4> &MOs) const {
1039  // Table (and size) to search
1040  const DenseMap<unsigned*, unsigned> *OpcodeTablePtr = NULL;
1041  bool isTwoAddrFold = false;
1042  unsigned NumOps = TII.getNumOperands(MI->getOpcode());
1043  bool isTwoAddr = NumOps > 1 &&
1044    MI->getInstrDescriptor()->getOperandConstraint(1, TOI::TIED_TO) != -1;
1045
1046  MachineInstr *NewMI = NULL;
1047  // Folding a memory location into the two-address part of a two-address
1048  // instruction is different than folding it other places.  It requires
1049  // replacing the *two* registers with the memory location.
1050  if (isTwoAddr && NumOps >= 2 && i < 2 &&
1051      MI->getOperand(0).isRegister() &&
1052      MI->getOperand(1).isRegister() &&
1053      MI->getOperand(0).getReg() == MI->getOperand(1).getReg()) {
1054    OpcodeTablePtr = &RegOp2MemOpTable2Addr;
1055    isTwoAddrFold = true;
1056  } else if (i == 0) { // If operand 0
1057    if (MI->getOpcode() == X86::MOV16r0)
1058      NewMI = MakeM0Inst(TII, X86::MOV16mi, MOs, MI);
1059    else if (MI->getOpcode() == X86::MOV32r0)
1060      NewMI = MakeM0Inst(TII, X86::MOV32mi, MOs, MI);
1061    else if (MI->getOpcode() == X86::MOV64r0)
1062      NewMI = MakeM0Inst(TII, X86::MOV64mi32, MOs, MI);
1063    else if (MI->getOpcode() == X86::MOV8r0)
1064      NewMI = MakeM0Inst(TII, X86::MOV8mi, MOs, MI);
1065    if (NewMI) {
1066      NewMI->copyKillDeadInfo(MI);
1067      return NewMI;
1068    }
1069
1070    OpcodeTablePtr = &RegOp2MemOpTable0;
1071  } else if (i == 1) {
1072    OpcodeTablePtr = &RegOp2MemOpTable1;
1073  } else if (i == 2) {
1074    OpcodeTablePtr = &RegOp2MemOpTable2;
1075  }
1076
1077  // If table selected...
1078  if (OpcodeTablePtr) {
1079    // Find the Opcode to fuse
1080    DenseMap<unsigned*, unsigned>::iterator I =
1081      OpcodeTablePtr->find((unsigned*)MI->getOpcode());
1082    if (I != OpcodeTablePtr->end()) {
1083      if (isTwoAddrFold)
1084        NewMI = FuseTwoAddrInst(I->second, MOs, MI, TII);
1085      else
1086        NewMI = FuseInst(I->second, i, MOs, MI, TII);
1087      NewMI->copyKillDeadInfo(MI);
1088      return NewMI;
1089    }
1090  }
1091
1092  // No fusion
1093  if (PrintFailedFusing)
1094    cerr << "We failed to fuse ("
1095         << ((i == 1) ? "r" : "s") << "): " << *MI;
1096  return NULL;
1097}
1098
1099
1100MachineInstr* X86RegisterInfo::foldMemoryOperand(MachineInstr *MI, unsigned OpNum,
1101                                                 int FrameIndex) const {
1102  // Check switch flag
1103  if (NoFusing) return NULL;
1104  SmallVector<MachineOperand,4> MOs;
1105  MOs.push_back(MachineOperand::CreateFrameIndex(FrameIndex));
1106  return foldMemoryOperand(MI, OpNum, MOs);
1107}
1108
1109MachineInstr* X86RegisterInfo::foldMemoryOperand(MachineInstr *MI, unsigned OpNum,
1110                                                 MachineInstr *LoadMI) const {
1111  // Check switch flag
1112  if (NoFusing) return NULL;
1113  SmallVector<MachineOperand,4> MOs;
1114  unsigned NumOps = TII.getNumOperands(LoadMI->getOpcode());
1115  for (unsigned i = NumOps - 4; i != NumOps; ++i)
1116    MOs.push_back(LoadMI->getOperand(i));
1117  return foldMemoryOperand(MI, OpNum, MOs);
1118}
1119
1120bool X86RegisterInfo::unfoldMemoryOperand(MachineFunction &MF, MachineInstr *MI,
1121                                unsigned Reg, bool UnfoldLoad, bool UnfoldStore,
1122                                 SmallVectorImpl<MachineInstr*> &NewMIs) const {
1123  DenseMap<unsigned*, std::pair<unsigned,unsigned> >::iterator I =
1124    MemOp2RegOpTable.find((unsigned*)MI->getOpcode());
1125  if (I == MemOp2RegOpTable.end())
1126    return false;
1127  unsigned Opc = I->second.first;
1128  unsigned Index = I->second.second & 0xf;
1129  bool HasLoad = I->second.second & (1 << 4);
1130  bool HasStore = I->second.second & (1 << 5);
1131  if (UnfoldLoad && !HasLoad)
1132    return false;
1133  HasLoad &= UnfoldLoad;
1134  if (UnfoldStore && !HasStore)
1135    return false;
1136  HasStore &= UnfoldStore;
1137
1138  const TargetInstrDescriptor &TID = TII.get(Opc);
1139  const TargetOperandInfo &TOI = TID.OpInfo[Index];
1140  const TargetRegisterClass *RC = (TOI.Flags & M_LOOK_UP_PTR_REG_CLASS)
1141    ? TII.getPointerRegClass() : getRegClass(TOI.RegClass);
1142  SmallVector<MachineOperand,4> AddrOps;
1143  SmallVector<MachineOperand,2> BeforeOps;
1144  SmallVector<MachineOperand,2> AfterOps;
1145  SmallVector<MachineOperand,4> ImpOps;
1146  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1147    MachineOperand &Op = MI->getOperand(i);
1148    if (i >= Index && i < Index+4)
1149      AddrOps.push_back(Op);
1150    else if (Op.isRegister() && Op.isImplicit())
1151      ImpOps.push_back(Op);
1152    else if (i < Index)
1153      BeforeOps.push_back(Op);
1154    else if (i > Index)
1155      AfterOps.push_back(Op);
1156  }
1157
1158  // Emit the load instruction.
1159  if (HasLoad) {
1160    loadRegFromAddr(MF, Reg, AddrOps, RC, NewMIs);
1161    if (HasStore) {
1162      // Address operands cannot be marked isKill.
1163      for (unsigned i = 1; i != 5; ++i) {
1164        MachineOperand &MO = NewMIs[0]->getOperand(i);
1165        if (MO.isRegister())
1166          MO.unsetIsKill();
1167      }
1168    }
1169  }
1170
1171  // Emit the data processing instruction.
1172  MachineInstr *DataMI = new MachineInstr (TID, true);
1173  MachineInstrBuilder MIB(DataMI);
1174  const TargetRegisterClass *DstRC = 0;
1175  if (HasStore) {
1176    const TargetOperandInfo &DstTOI = TID.OpInfo[0];
1177    DstRC = (DstTOI.Flags & M_LOOK_UP_PTR_REG_CLASS)
1178      ? TII.getPointerRegClass() : getRegClass(DstTOI.RegClass);
1179    MIB.addReg(Reg, true);
1180  }
1181  for (unsigned i = 0, e = BeforeOps.size(); i != e; ++i)
1182    MIB = X86InstrAddOperand(MIB, BeforeOps[i]);
1183  MIB.addReg(Reg);
1184  for (unsigned i = 0, e = AfterOps.size(); i != e; ++i)
1185    MIB = X86InstrAddOperand(MIB, AfterOps[i]);
1186  for (unsigned i = 0, e = ImpOps.size(); i != e; ++i) {
1187    MachineOperand &MO = ImpOps[i];
1188    MIB.addReg(MO.getReg(), MO.isDef(), true, MO.isKill(), MO.isDead());
1189  }
1190  NewMIs.push_back(MIB);
1191
1192  // Emit the store instruction.
1193  if (HasStore)
1194    storeRegToAddr(MF, Reg, AddrOps, DstRC, NewMIs);
1195
1196  return true;
1197}
1198
1199
1200bool
1201X86RegisterInfo::unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
1202                                     SmallVectorImpl<SDNode*> &NewNodes) const {
1203  if (!N->isTargetOpcode())
1204    return false;
1205
1206  DenseMap<unsigned*, std::pair<unsigned,unsigned> >::iterator I =
1207    MemOp2RegOpTable.find((unsigned*)N->getTargetOpcode());
1208  if (I == MemOp2RegOpTable.end())
1209    return false;
1210  unsigned Opc = I->second.first;
1211  unsigned Index = I->second.second & 0xf;
1212  bool HasLoad = I->second.second & (1 << 4);
1213  bool HasStore = I->second.second & (1 << 5);
1214  const TargetInstrDescriptor &TID = TII.get(Opc);
1215  const TargetOperandInfo &TOI = TID.OpInfo[Index];
1216  const TargetRegisterClass *RC = (TOI.Flags & M_LOOK_UP_PTR_REG_CLASS)
1217    ? TII.getPointerRegClass() : getRegClass(TOI.RegClass);
1218  std::vector<SDOperand> AddrOps;
1219  std::vector<SDOperand> BeforeOps;
1220  std::vector<SDOperand> AfterOps;
1221  unsigned NumOps = N->getNumOperands();
1222  for (unsigned i = 0; i != NumOps-1; ++i) {
1223    SDOperand Op = N->getOperand(i);
1224    if (i >= Index && i < Index+4)
1225      AddrOps.push_back(Op);
1226    else if (i < Index)
1227      BeforeOps.push_back(Op);
1228    else if (i > Index)
1229      AfterOps.push_back(Op);
1230  }
1231  SDOperand Chain = N->getOperand(NumOps-1);
1232  AddrOps.push_back(Chain);
1233
1234  // Emit the load instruction.
1235  SDNode *Load = 0;
1236  if (HasLoad) {
1237    MVT::ValueType VT = *RC->vt_begin();
1238    Load = DAG.getTargetNode(getLoadRegOpcode(RC), VT, MVT::Other,
1239                             &AddrOps[0], AddrOps.size());
1240    NewNodes.push_back(Load);
1241  }
1242
1243  // Emit the data processing instruction.
1244  std::vector<MVT::ValueType> VTs;
1245  const TargetRegisterClass *DstRC = 0;
1246  if (TID.numDefs > 0) {
1247    const TargetOperandInfo &DstTOI = TID.OpInfo[0];
1248    DstRC = (DstTOI.Flags & M_LOOK_UP_PTR_REG_CLASS)
1249      ? TII.getPointerRegClass() : getRegClass(DstTOI.RegClass);
1250    VTs.push_back(*DstRC->vt_begin());
1251  }
1252  for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
1253    MVT::ValueType VT = N->getValueType(i);
1254    if (VT != MVT::Other && i >= TID.numDefs)
1255      VTs.push_back(VT);
1256  }
1257  if (Load)
1258    BeforeOps.push_back(SDOperand(Load, 0));
1259  std::copy(AfterOps.begin(), AfterOps.end(), std::back_inserter(BeforeOps));
1260  SDNode *NewNode= DAG.getTargetNode(Opc, VTs, &BeforeOps[0], BeforeOps.size());
1261  NewNodes.push_back(NewNode);
1262
1263  // Emit the store instruction.
1264  if (HasStore) {
1265    AddrOps.pop_back();
1266    AddrOps.push_back(SDOperand(NewNode, 0));
1267    AddrOps.push_back(Chain);
1268    SDNode *Store = DAG.getTargetNode(getStoreRegOpcode(DstRC),
1269                                      MVT::Other, &AddrOps[0], AddrOps.size());
1270    NewNodes.push_back(Store);
1271  }
1272
1273  return true;
1274}
1275
1276unsigned X86RegisterInfo::getOpcodeAfterMemoryUnfold(unsigned Opc,
1277                                      bool UnfoldLoad, bool UnfoldStore) const {
1278  DenseMap<unsigned*, std::pair<unsigned,unsigned> >::iterator I =
1279    MemOp2RegOpTable.find((unsigned*)Opc);
1280  if (I == MemOp2RegOpTable.end())
1281    return 0;
1282  bool HasLoad = I->second.second & (1 << 4);
1283  bool HasStore = I->second.second & (1 << 5);
1284  if (UnfoldLoad && !HasLoad)
1285    return 0;
1286  if (UnfoldStore && !HasStore)
1287    return 0;
1288  return I->second.first;
1289}
1290
1291const unsigned *
1292X86RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
1293  static const unsigned CalleeSavedRegs32Bit[] = {
1294    X86::ESI, X86::EDI, X86::EBX, X86::EBP,  0
1295  };
1296
1297  static const unsigned CalleeSavedRegs32EHRet[] = {
1298    X86::EAX, X86::EDX, X86::ESI, X86::EDI, X86::EBX, X86::EBP,  0
1299  };
1300
1301  static const unsigned CalleeSavedRegs64Bit[] = {
1302    X86::RBX, X86::R12, X86::R13, X86::R14, X86::R15, X86::RBP, 0
1303  };
1304
1305  if (Is64Bit)
1306    return CalleeSavedRegs64Bit;
1307  else {
1308    if (MF) {
1309        MachineFrameInfo *MFI = MF->getFrameInfo();
1310        MachineModuleInfo *MMI = MFI->getMachineModuleInfo();
1311        if (MMI && MMI->callsEHReturn())
1312          return CalleeSavedRegs32EHRet;
1313    }
1314    return CalleeSavedRegs32Bit;
1315  }
1316}
1317
1318const TargetRegisterClass* const*
1319X86RegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const {
1320  static const TargetRegisterClass * const CalleeSavedRegClasses32Bit[] = {
1321    &X86::GR32RegClass, &X86::GR32RegClass,
1322    &X86::GR32RegClass, &X86::GR32RegClass,  0
1323  };
1324  static const TargetRegisterClass * const CalleeSavedRegClasses32EHRet[] = {
1325    &X86::GR32RegClass, &X86::GR32RegClass,
1326    &X86::GR32RegClass, &X86::GR32RegClass,
1327    &X86::GR32RegClass, &X86::GR32RegClass,  0
1328  };
1329  static const TargetRegisterClass * const CalleeSavedRegClasses64Bit[] = {
1330    &X86::GR64RegClass, &X86::GR64RegClass,
1331    &X86::GR64RegClass, &X86::GR64RegClass,
1332    &X86::GR64RegClass, &X86::GR64RegClass, 0
1333  };
1334
1335  if (Is64Bit)
1336    return CalleeSavedRegClasses64Bit;
1337  else {
1338    if (MF) {
1339        MachineFrameInfo *MFI = MF->getFrameInfo();
1340        MachineModuleInfo *MMI = MFI->getMachineModuleInfo();
1341        if (MMI && MMI->callsEHReturn())
1342          return CalleeSavedRegClasses32EHRet;
1343    }
1344    return CalleeSavedRegClasses32Bit;
1345  }
1346
1347}
1348
1349BitVector X86RegisterInfo::getReservedRegs(const MachineFunction &MF) const {
1350  BitVector Reserved(getNumRegs());
1351  Reserved.set(X86::RSP);
1352  Reserved.set(X86::ESP);
1353  Reserved.set(X86::SP);
1354  Reserved.set(X86::SPL);
1355  if (hasFP(MF)) {
1356    Reserved.set(X86::RBP);
1357    Reserved.set(X86::EBP);
1358    Reserved.set(X86::BP);
1359    Reserved.set(X86::BPL);
1360  }
1361  return Reserved;
1362}
1363
1364//===----------------------------------------------------------------------===//
1365// Stack Frame Processing methods
1366//===----------------------------------------------------------------------===//
1367
1368// hasFP - Return true if the specified function should have a dedicated frame
1369// pointer register.  This is true if the function has variable sized allocas or
1370// if frame pointer elimination is disabled.
1371//
1372bool X86RegisterInfo::hasFP(const MachineFunction &MF) const {
1373  MachineFrameInfo *MFI = MF.getFrameInfo();
1374  MachineModuleInfo *MMI = MFI->getMachineModuleInfo();
1375
1376  return (NoFramePointerElim ||
1377          MFI->hasVarSizedObjects() ||
1378          MF.getInfo<X86MachineFunctionInfo>()->getForceFramePointer() ||
1379          (MMI && MMI->callsUnwindInit()));
1380}
1381
1382bool X86RegisterInfo::hasReservedCallFrame(MachineFunction &MF) const {
1383  return !MF.getFrameInfo()->hasVarSizedObjects();
1384}
1385
1386void X86RegisterInfo::
1387eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
1388                              MachineBasicBlock::iterator I) const {
1389  if (!hasReservedCallFrame(MF)) {
1390    // If the stack pointer can be changed after prologue, turn the
1391    // adjcallstackup instruction into a 'sub ESP, <amt>' and the
1392    // adjcallstackdown instruction into 'add ESP, <amt>'
1393    // TODO: consider using push / pop instead of sub + store / add
1394    MachineInstr *Old = I;
1395    uint64_t Amount = Old->getOperand(0).getImm();
1396    if (Amount != 0) {
1397      // We need to keep the stack aligned properly.  To do this, we round the
1398      // amount of space needed for the outgoing arguments up to the next
1399      // alignment boundary.
1400      unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1401      Amount = (Amount+Align-1)/Align*Align;
1402
1403      MachineInstr *New = 0;
1404      if (Old->getOpcode() == X86::ADJCALLSTACKDOWN) {
1405        New=BuildMI(TII.get(Is64Bit ? X86::SUB64ri32 : X86::SUB32ri), StackPtr)
1406          .addReg(StackPtr).addImm(Amount);
1407      } else {
1408        assert(Old->getOpcode() == X86::ADJCALLSTACKUP);
1409        // factor out the amount the callee already popped.
1410        uint64_t CalleeAmt = Old->getOperand(1).getImm();
1411        Amount -= CalleeAmt;
1412        if (Amount) {
1413          unsigned Opc = (Amount < 128) ?
1414            (Is64Bit ? X86::ADD64ri8 : X86::ADD32ri8) :
1415            (Is64Bit ? X86::ADD64ri32 : X86::ADD32ri);
1416          New = BuildMI(TII.get(Opc),  StackPtr)
1417                        .addReg(StackPtr).addImm(Amount);
1418        }
1419      }
1420
1421      // Replace the pseudo instruction with a new instruction...
1422      if (New) MBB.insert(I, New);
1423    }
1424  } else if (I->getOpcode() == X86::ADJCALLSTACKUP) {
1425    // If we are performing frame pointer elimination and if the callee pops
1426    // something off the stack pointer, add it back.  We do this until we have
1427    // more advanced stack pointer tracking ability.
1428    if (uint64_t CalleeAmt = I->getOperand(1).getImm()) {
1429      unsigned Opc = (CalleeAmt < 128) ?
1430        (Is64Bit ? X86::SUB64ri8 : X86::SUB32ri8) :
1431        (Is64Bit ? X86::SUB64ri32 : X86::SUB32ri);
1432      MachineInstr *New =
1433        BuildMI(TII.get(Opc), StackPtr).addReg(StackPtr).addImm(CalleeAmt);
1434      MBB.insert(I, New);
1435    }
1436  }
1437
1438  MBB.erase(I);
1439}
1440
1441void X86RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
1442                                          int SPAdj, RegScavenger *RS) const{
1443  assert(SPAdj == 0 && "Unexpected");
1444
1445  unsigned i = 0;
1446  MachineInstr &MI = *II;
1447  MachineFunction &MF = *MI.getParent()->getParent();
1448  while (!MI.getOperand(i).isFrameIndex()) {
1449    ++i;
1450    assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
1451  }
1452
1453  int FrameIndex = MI.getOperand(i).getFrameIndex();
1454  // This must be part of a four operand memory reference.  Replace the
1455  // FrameIndex with base register with EBP.  Add an offset to the offset.
1456  MI.getOperand(i).ChangeToRegister(hasFP(MF) ? FramePtr : StackPtr, false);
1457
1458  // Now add the frame object offset to the offset from EBP.
1459  int64_t Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) +
1460                   MI.getOperand(i+3).getImm()+SlotSize;
1461
1462  if (!hasFP(MF))
1463    Offset += MF.getFrameInfo()->getStackSize();
1464  else {
1465    Offset += SlotSize;  // Skip the saved EBP
1466    // Skip the RETADDR move area
1467    X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
1468    int TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta();
1469    if (TailCallReturnAddrDelta < 0) Offset -= TailCallReturnAddrDelta;
1470  }
1471
1472  MI.getOperand(i+3).ChangeToImmediate(Offset);
1473}
1474
1475void
1476X86RegisterInfo::processFunctionBeforeFrameFinalized(MachineFunction &MF) const{
1477  X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
1478  int32_t TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta();
1479  if (TailCallReturnAddrDelta < 0) {
1480    // create RETURNADDR area
1481    //   arg
1482    //   arg
1483    //   RETADDR
1484    //   { ...
1485    //     RETADDR area
1486    //     ...
1487    //   }
1488    //   [EBP]
1489    MF.getFrameInfo()->
1490      CreateFixedObject(-TailCallReturnAddrDelta,
1491                        (-1*SlotSize)+TailCallReturnAddrDelta);
1492  }
1493  if (hasFP(MF)) {
1494    assert((TailCallReturnAddrDelta <= 0) &&
1495           "The Delta should always be zero or negative");
1496    // Create a frame entry for the EBP register that must be saved.
1497    int FrameIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize,
1498                                                        (int)SlotSize * -2+
1499                                                       TailCallReturnAddrDelta);
1500    assert(FrameIdx == MF.getFrameInfo()->getObjectIndexBegin() &&
1501           "Slot for EBP register must be last in order to be found!");
1502  }
1503}
1504
1505/// emitSPUpdate - Emit a series of instructions to increment / decrement the
1506/// stack pointer by a constant value.
1507static
1508void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
1509                  unsigned StackPtr, int64_t NumBytes, bool Is64Bit,
1510                  const TargetInstrInfo &TII) {
1511  bool isSub = NumBytes < 0;
1512  uint64_t Offset = isSub ? -NumBytes : NumBytes;
1513  unsigned Opc = isSub
1514    ? ((Offset < 128) ?
1515       (Is64Bit ? X86::SUB64ri8 : X86::SUB32ri8) :
1516       (Is64Bit ? X86::SUB64ri32 : X86::SUB32ri))
1517    : ((Offset < 128) ?
1518       (Is64Bit ? X86::ADD64ri8 : X86::ADD32ri8) :
1519       (Is64Bit ? X86::ADD64ri32 : X86::ADD32ri));
1520  uint64_t Chunk = (1LL << 31) - 1;
1521
1522  while (Offset) {
1523    uint64_t ThisVal = (Offset > Chunk) ? Chunk : Offset;
1524    BuildMI(MBB, MBBI, TII.get(Opc), StackPtr).addReg(StackPtr).addImm(ThisVal);
1525    Offset -= ThisVal;
1526  }
1527}
1528
1529// mergeSPUpdatesUp - Merge two stack-manipulating instructions upper iterator.
1530static
1531void mergeSPUpdatesUp(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
1532                      unsigned StackPtr, uint64_t *NumBytes = NULL) {
1533  if (MBBI == MBB.begin()) return;
1534
1535  MachineBasicBlock::iterator PI = prior(MBBI);
1536  unsigned Opc = PI->getOpcode();
1537  if ((Opc == X86::ADD64ri32 || Opc == X86::ADD64ri8 ||
1538       Opc == X86::ADD32ri || Opc == X86::ADD32ri8) &&
1539      PI->getOperand(0).getReg() == StackPtr) {
1540    if (NumBytes)
1541      *NumBytes += PI->getOperand(2).getImm();
1542    MBB.erase(PI);
1543  } else if ((Opc == X86::SUB64ri32 || Opc == X86::SUB64ri8 ||
1544              Opc == X86::SUB32ri || Opc == X86::SUB32ri8) &&
1545             PI->getOperand(0).getReg() == StackPtr) {
1546    if (NumBytes)
1547      *NumBytes -= PI->getOperand(2).getImm();
1548    MBB.erase(PI);
1549  }
1550}
1551
1552// mergeSPUpdatesUp - Merge two stack-manipulating instructions lower iterator.
1553static
1554void mergeSPUpdatesDown(MachineBasicBlock &MBB,
1555                        MachineBasicBlock::iterator &MBBI,
1556                        unsigned StackPtr, uint64_t *NumBytes = NULL) {
1557  return;
1558
1559  if (MBBI == MBB.end()) return;
1560
1561  MachineBasicBlock::iterator NI = next(MBBI);
1562  if (NI == MBB.end()) return;
1563
1564  unsigned Opc = NI->getOpcode();
1565  if ((Opc == X86::ADD64ri32 || Opc == X86::ADD64ri8 ||
1566       Opc == X86::ADD32ri || Opc == X86::ADD32ri8) &&
1567      NI->getOperand(0).getReg() == StackPtr) {
1568    if (NumBytes)
1569      *NumBytes -= NI->getOperand(2).getImm();
1570    MBB.erase(NI);
1571    MBBI = NI;
1572  } else if ((Opc == X86::SUB64ri32 || Opc == X86::SUB64ri8 ||
1573              Opc == X86::SUB32ri || Opc == X86::SUB32ri8) &&
1574             NI->getOperand(0).getReg() == StackPtr) {
1575    if (NumBytes)
1576      *NumBytes += NI->getOperand(2).getImm();
1577    MBB.erase(NI);
1578    MBBI = NI;
1579  }
1580}
1581
1582/// mergeSPUpdates - Checks the instruction before/after the passed
1583/// instruction. If it is an ADD/SUB instruction it is deleted
1584/// argument and the stack adjustment is returned as a positive value for ADD
1585/// and a negative for SUB.
1586static int mergeSPUpdates(MachineBasicBlock &MBB,
1587                           MachineBasicBlock::iterator &MBBI,
1588                           unsigned StackPtr,
1589                           bool doMergeWithPrevious) {
1590
1591  if ((doMergeWithPrevious && MBBI == MBB.begin()) ||
1592      (!doMergeWithPrevious && MBBI == MBB.end()))
1593    return 0;
1594
1595  int Offset = 0;
1596
1597  MachineBasicBlock::iterator PI = doMergeWithPrevious ? prior(MBBI) : MBBI;
1598  MachineBasicBlock::iterator NI = doMergeWithPrevious ? 0 : next(MBBI);
1599  unsigned Opc = PI->getOpcode();
1600  if ((Opc == X86::ADD64ri32 || Opc == X86::ADD64ri8 ||
1601       Opc == X86::ADD32ri || Opc == X86::ADD32ri8) &&
1602      PI->getOperand(0).getReg() == StackPtr){
1603    Offset += PI->getOperand(2).getImm();
1604    MBB.erase(PI);
1605    if (!doMergeWithPrevious) MBBI = NI;
1606  } else if ((Opc == X86::SUB64ri32 || Opc == X86::SUB64ri8 ||
1607              Opc == X86::SUB32ri || Opc == X86::SUB32ri8) &&
1608             PI->getOperand(0).getReg() == StackPtr) {
1609    Offset -= PI->getOperand(2).getImm();
1610    MBB.erase(PI);
1611    if (!doMergeWithPrevious) MBBI = NI;
1612  }
1613
1614  return Offset;
1615}
1616
1617void X86RegisterInfo::emitPrologue(MachineFunction &MF) const {
1618  MachineBasicBlock &MBB = MF.front();   // Prolog goes in entry BB
1619  MachineFrameInfo *MFI = MF.getFrameInfo();
1620  unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1621  const Function* Fn = MF.getFunction();
1622  const X86Subtarget* Subtarget = &MF.getTarget().getSubtarget<X86Subtarget>();
1623  MachineModuleInfo *MMI = MFI->getMachineModuleInfo();
1624  X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
1625  MachineBasicBlock::iterator MBBI = MBB.begin();
1626
1627  // Prepare for frame info.
1628  unsigned FrameLabelId = 0;
1629
1630  // Get the number of bytes to allocate from the FrameInfo.
1631  uint64_t StackSize = MFI->getStackSize();
1632  // Add RETADDR move area to callee saved frame size.
1633  int TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta();
1634  if (TailCallReturnAddrDelta < 0)
1635    X86FI->setCalleeSavedFrameSize(
1636          X86FI->getCalleeSavedFrameSize() +(-TailCallReturnAddrDelta));
1637  uint64_t NumBytes = StackSize - X86FI->getCalleeSavedFrameSize();
1638
1639  // Insert stack pointer adjustment for later moving of return addr.  Only
1640  // applies to tail call optimized functions where the callee argument stack
1641  // size is bigger than the callers.
1642  if (TailCallReturnAddrDelta < 0) {
1643    BuildMI(MBB, MBBI, TII.get(Is64Bit? X86::SUB64ri32 : X86::SUB32ri),
1644            StackPtr).addReg(StackPtr).addImm(-TailCallReturnAddrDelta);
1645  }
1646
1647  if (hasFP(MF)) {
1648    // Get the offset of the stack slot for the EBP register... which is
1649    // guaranteed to be the last slot by processFunctionBeforeFrameFinalized.
1650    // Update the frame offset adjustment.
1651    MFI->setOffsetAdjustment(SlotSize-NumBytes);
1652
1653    // Save EBP into the appropriate stack slot...
1654    BuildMI(MBB, MBBI, TII.get(Is64Bit ? X86::PUSH64r : X86::PUSH32r))
1655      .addReg(FramePtr);
1656    NumBytes -= SlotSize;
1657
1658    if (MMI && MMI->needsFrameInfo()) {
1659      // Mark effective beginning of when frame pointer becomes valid.
1660      FrameLabelId = MMI->NextLabelID();
1661      BuildMI(MBB, MBBI, TII.get(X86::LABEL)).addImm(FrameLabelId);
1662    }
1663
1664    // Update EBP with the new base value...
1665    BuildMI(MBB, MBBI, TII.get(Is64Bit ? X86::MOV64rr : X86::MOV32rr), FramePtr)
1666      .addReg(StackPtr);
1667  }
1668
1669  unsigned ReadyLabelId = 0;
1670  if (MMI && MMI->needsFrameInfo()) {
1671    // Mark effective beginning of when frame pointer is ready.
1672    ReadyLabelId = MMI->NextLabelID();
1673    BuildMI(MBB, MBBI, TII.get(X86::LABEL)).addImm(ReadyLabelId);
1674  }
1675
1676  // Skip the callee-saved push instructions.
1677  while (MBBI != MBB.end() &&
1678         (MBBI->getOpcode() == X86::PUSH32r ||
1679          MBBI->getOpcode() == X86::PUSH64r))
1680    ++MBBI;
1681
1682  if (NumBytes) {   // adjust stack pointer: ESP -= numbytes
1683    if (NumBytes >= 4096 && Subtarget->isTargetCygMing()) {
1684      // Check, whether EAX is livein for this function
1685      bool isEAXAlive = false;
1686      for (MachineFunction::livein_iterator II = MF.livein_begin(),
1687             EE = MF.livein_end(); (II != EE) && !isEAXAlive; ++II) {
1688        unsigned Reg = II->first;
1689        isEAXAlive = (Reg == X86::EAX || Reg == X86::AX ||
1690                      Reg == X86::AH || Reg == X86::AL);
1691      }
1692
1693      // Function prologue calls _alloca to probe the stack when allocating
1694      // more than 4k bytes in one go. Touching the stack at 4K increments is
1695      // necessary to ensure that the guard pages used by the OS virtual memory
1696      // manager are allocated in correct sequence.
1697      if (!isEAXAlive) {
1698        BuildMI(MBB, MBBI, TII.get(X86::MOV32ri), X86::EAX).addImm(NumBytes);
1699        BuildMI(MBB, MBBI, TII.get(X86::CALLpcrel32))
1700          .addExternalSymbol("_alloca");
1701      } else {
1702        // Save EAX
1703        BuildMI(MBB, MBBI, TII.get(X86::PUSH32r), X86::EAX);
1704        // Allocate NumBytes-4 bytes on stack. We'll also use 4 already
1705        // allocated bytes for EAX.
1706        BuildMI(MBB, MBBI, TII.get(X86::MOV32ri), X86::EAX).addImm(NumBytes-4);
1707        BuildMI(MBB, MBBI, TII.get(X86::CALLpcrel32))
1708          .addExternalSymbol("_alloca");
1709        // Restore EAX
1710        MachineInstr *MI = addRegOffset(BuildMI(TII.get(X86::MOV32rm),X86::EAX),
1711                                        StackPtr, NumBytes-4);
1712        MBB.insert(MBBI, MI);
1713      }
1714    } else {
1715      // If there is an SUB32ri of ESP immediately before this instruction,
1716      // merge the two. This can be the case when tail call elimination is
1717      // enabled and the callee has more arguments then the caller.
1718      NumBytes -= mergeSPUpdates(MBB, MBBI, StackPtr, true);
1719      // If there is an ADD32ri or SUB32ri of ESP immediately after this
1720      // instruction, merge the two instructions.
1721      mergeSPUpdatesDown(MBB, MBBI, StackPtr, &NumBytes);
1722
1723      if (NumBytes)
1724        emitSPUpdate(MBB, MBBI, StackPtr, -(int64_t)NumBytes, Is64Bit, TII);
1725    }
1726  }
1727
1728  if (MMI && MMI->needsFrameInfo()) {
1729    std::vector<MachineMove> &Moves = MMI->getFrameMoves();
1730    const TargetData *TD = MF.getTarget().getTargetData();
1731
1732    // Calculate amount of bytes used for return address storing
1733    int stackGrowth =
1734      (MF.getTarget().getFrameInfo()->getStackGrowthDirection() ==
1735       TargetFrameInfo::StackGrowsUp ?
1736       TD->getPointerSize() : -TD->getPointerSize());
1737
1738    if (StackSize) {
1739      // Show update of SP.
1740      if (hasFP(MF)) {
1741        // Adjust SP
1742        MachineLocation SPDst(MachineLocation::VirtualFP);
1743        MachineLocation SPSrc(MachineLocation::VirtualFP, 2*stackGrowth);
1744        Moves.push_back(MachineMove(FrameLabelId, SPDst, SPSrc));
1745      } else {
1746        MachineLocation SPDst(MachineLocation::VirtualFP);
1747        MachineLocation SPSrc(MachineLocation::VirtualFP, -StackSize+stackGrowth);
1748        Moves.push_back(MachineMove(FrameLabelId, SPDst, SPSrc));
1749      }
1750    } else {
1751      //FIXME: Verify & implement for FP
1752      MachineLocation SPDst(StackPtr);
1753      MachineLocation SPSrc(StackPtr, stackGrowth);
1754      Moves.push_back(MachineMove(FrameLabelId, SPDst, SPSrc));
1755    }
1756
1757    // Add callee saved registers to move list.
1758    const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
1759
1760    // FIXME: This is dirty hack. The code itself is pretty mess right now.
1761    // It should be rewritten from scratch and generalized sometimes.
1762
1763    // Determine maximum offset (minumum due to stack growth)
1764    int64_t MaxOffset = 0;
1765    for (unsigned I = 0, E = CSI.size(); I!=E; ++I)
1766      MaxOffset = std::min(MaxOffset,
1767                           MFI->getObjectOffset(CSI[I].getFrameIdx()));
1768
1769    // Calculate offsets
1770    for (unsigned I = 0, E = CSI.size(); I!=E; ++I) {
1771      int64_t Offset = MFI->getObjectOffset(CSI[I].getFrameIdx());
1772      unsigned Reg = CSI[I].getReg();
1773      Offset = (MaxOffset-Offset+3*stackGrowth);
1774      MachineLocation CSDst(MachineLocation::VirtualFP, Offset);
1775      MachineLocation CSSrc(Reg);
1776      Moves.push_back(MachineMove(FrameLabelId, CSDst, CSSrc));
1777    }
1778
1779    if (hasFP(MF)) {
1780      // Save FP
1781      MachineLocation FPDst(MachineLocation::VirtualFP, 2*stackGrowth);
1782      MachineLocation FPSrc(FramePtr);
1783      Moves.push_back(MachineMove(ReadyLabelId, FPDst, FPSrc));
1784    }
1785
1786    MachineLocation FPDst(hasFP(MF) ? FramePtr : StackPtr);
1787    MachineLocation FPSrc(MachineLocation::VirtualFP);
1788    Moves.push_back(MachineMove(ReadyLabelId, FPDst, FPSrc));
1789  }
1790
1791  // If it's main() on Cygwin\Mingw32 we should align stack as well
1792  if (Fn->hasExternalLinkage() && Fn->getName() == "main" &&
1793      Subtarget->isTargetCygMing()) {
1794    BuildMI(MBB, MBBI, TII.get(X86::AND32ri), X86::ESP)
1795                .addReg(X86::ESP).addImm(-Align);
1796
1797    // Probe the stack
1798    BuildMI(MBB, MBBI, TII.get(X86::MOV32ri), X86::EAX).addImm(Align);
1799    BuildMI(MBB, MBBI, TII.get(X86::CALLpcrel32)).addExternalSymbol("_alloca");
1800  }
1801}
1802
1803void X86RegisterInfo::emitEpilogue(MachineFunction &MF,
1804                                   MachineBasicBlock &MBB) const {
1805  const MachineFrameInfo *MFI = MF.getFrameInfo();
1806  const Function* Fn = MF.getFunction();
1807  X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
1808  const X86Subtarget* Subtarget = &MF.getTarget().getSubtarget<X86Subtarget>();
1809  MachineBasicBlock::iterator MBBI = prior(MBB.end());
1810  unsigned RetOpcode = MBBI->getOpcode();
1811
1812  switch (RetOpcode) {
1813  case X86::RET:
1814  case X86::RETI:
1815  case X86::TCRETURNdi:
1816  case X86::TCRETURNri:
1817  case X86::TCRETURNri64:
1818  case X86::TCRETURNdi64:
1819  case X86::EH_RETURN:
1820  case X86::TAILJMPd:
1821  case X86::TAILJMPr:
1822  case X86::TAILJMPm: break;  // These are ok
1823  default:
1824    assert(0 && "Can only insert epilog into returning blocks");
1825  }
1826
1827  // Get the number of bytes to allocate from the FrameInfo
1828  uint64_t StackSize = MFI->getStackSize();
1829  unsigned CSSize = X86FI->getCalleeSavedFrameSize();
1830  uint64_t NumBytes = StackSize - CSSize;
1831
1832  if (hasFP(MF)) {
1833    // pop EBP.
1834    BuildMI(MBB, MBBI, TII.get(Is64Bit ? X86::POP64r : X86::POP32r), FramePtr);
1835    NumBytes -= SlotSize;
1836  }
1837
1838  // Skip the callee-saved pop instructions.
1839  while (MBBI != MBB.begin()) {
1840    MachineBasicBlock::iterator PI = prior(MBBI);
1841    unsigned Opc = PI->getOpcode();
1842    if (Opc != X86::POP32r && Opc != X86::POP64r && !TII.isTerminatorInstr(Opc))
1843      break;
1844    --MBBI;
1845  }
1846
1847  // If there is an ADD32ri or SUB32ri of ESP immediately before this
1848  // instruction, merge the two instructions.
1849  if (NumBytes || MFI->hasVarSizedObjects())
1850    mergeSPUpdatesUp(MBB, MBBI, StackPtr, &NumBytes);
1851
1852  // If dynamic alloca is used, then reset esp to point to the last callee-saved
1853  // slot before popping them off!  Also, if it's main() on Cygwin/Mingw32 we
1854  // aligned stack in the prologue, - revert stack changes back. Note: we're
1855  // assuming, that frame pointer was forced for main()
1856  if (MFI->hasVarSizedObjects() ||
1857      (Fn->hasExternalLinkage() && Fn->getName() == "main" &&
1858       Subtarget->isTargetCygMing())) {
1859    unsigned Opc = Is64Bit ? X86::LEA64r : X86::LEA32r;
1860    if (CSSize) {
1861      MachineInstr *MI = addRegOffset(BuildMI(TII.get(Opc), StackPtr),
1862                                      FramePtr, -CSSize);
1863      MBB.insert(MBBI, MI);
1864    } else
1865      BuildMI(MBB, MBBI, TII.get(Is64Bit ? X86::MOV64rr : X86::MOV32rr),StackPtr).
1866        addReg(FramePtr);
1867
1868    NumBytes = 0;
1869  }
1870
1871  // adjust stack pointer back: ESP += numbytes
1872  if (NumBytes)
1873    emitSPUpdate(MBB, MBBI, StackPtr, NumBytes, Is64Bit, TII);
1874
1875  // We're returning from function via eh_return.
1876  if (RetOpcode == X86::EH_RETURN) {
1877    MBBI = prior(MBB.end());
1878    MachineOperand &DestAddr  = MBBI->getOperand(0);
1879    assert(DestAddr.isRegister() && "Offset should be in register!");
1880    BuildMI(MBB, MBBI, TII.get(Is64Bit ? X86::MOV64rr : X86::MOV32rr),StackPtr).
1881      addReg(DestAddr.getReg());
1882  // Tail call return: adjust the stack pointer and jump to callee
1883  } else if (RetOpcode == X86::TCRETURNri || RetOpcode == X86::TCRETURNdi ||
1884             RetOpcode== X86::TCRETURNri64 || RetOpcode == X86::TCRETURNdi64) {
1885    MBBI = prior(MBB.end());
1886    MachineOperand &JumpTarget = MBBI->getOperand(0);
1887    MachineOperand &StackAdjust = MBBI->getOperand(1);
1888    assert( StackAdjust.isImmediate() && "Expecting immediate value.");
1889
1890    // Adjust stack pointer.
1891    int StackAdj = StackAdjust.getImm();
1892    int MaxTCDelta = X86FI->getTCReturnAddrDelta();
1893    int Offset = 0;
1894    assert(MaxTCDelta <= 0 && "MaxTCDelta should never be positive");
1895    // Incoporate the retaddr area.
1896    Offset = StackAdj-MaxTCDelta;
1897    assert(Offset >= 0 && "Offset should never be negative");
1898    if (Offset) {
1899      // Check for possible merge with preceeding ADD instruction.
1900      Offset += mergeSPUpdates(MBB, MBBI, StackPtr, true);
1901      emitSPUpdate(MBB, MBBI, StackPtr, Offset, Is64Bit, TII);
1902    }
1903    // Jump to label or value in register.
1904    if (RetOpcode == X86::TCRETURNdi|| RetOpcode == X86::TCRETURNdi64)
1905      BuildMI(MBB, MBBI, TII.get(X86::TAILJMPd)).
1906        addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
1907    else if (RetOpcode== X86::TCRETURNri64) {
1908      BuildMI(MBB, MBBI, TII.get(X86::TAILJMPr64), JumpTarget.getReg());
1909    } else
1910       BuildMI(MBB, MBBI, TII.get(X86::TAILJMPr), JumpTarget.getReg());
1911    // Delete the pseudo instruction TCRETURN.
1912    MBB.erase(MBBI);
1913  } else if ((RetOpcode == X86::RET || RetOpcode == X86::RETI) &&
1914             (X86FI->getTCReturnAddrDelta() < 0)) {
1915    // Add the return addr area delta back since we are not tail calling.
1916    int delta = -1*X86FI->getTCReturnAddrDelta();
1917    MBBI = prior(MBB.end());
1918    // Check for possible merge with preceeding ADD instruction.
1919    delta += mergeSPUpdates(MBB, MBBI, StackPtr, true);
1920    emitSPUpdate(MBB, MBBI, StackPtr, delta, Is64Bit, TII);
1921  }
1922}
1923
1924unsigned X86RegisterInfo::getRARegister() const {
1925  if (Is64Bit)
1926    return X86::RIP;  // Should have dwarf #16
1927  else
1928    return X86::EIP;  // Should have dwarf #8
1929}
1930
1931unsigned X86RegisterInfo::getFrameRegister(MachineFunction &MF) const {
1932  return hasFP(MF) ? FramePtr : StackPtr;
1933}
1934
1935void X86RegisterInfo::getInitialFrameState(std::vector<MachineMove> &Moves)
1936                                                                         const {
1937  // Calculate amount of bytes used for return address storing
1938  int stackGrowth = (Is64Bit ? -8 : -4);
1939
1940  // Initial state of the frame pointer is esp+4.
1941  MachineLocation Dst(MachineLocation::VirtualFP);
1942  MachineLocation Src(StackPtr, stackGrowth);
1943  Moves.push_back(MachineMove(0, Dst, Src));
1944
1945  // Add return address to move list
1946  MachineLocation CSDst(StackPtr, stackGrowth);
1947  MachineLocation CSSrc(getRARegister());
1948  Moves.push_back(MachineMove(0, CSDst, CSSrc));
1949}
1950
1951unsigned X86RegisterInfo::getEHExceptionRegister() const {
1952  assert(0 && "What is the exception register");
1953  return 0;
1954}
1955
1956unsigned X86RegisterInfo::getEHHandlerRegister() const {
1957  assert(0 && "What is the exception handler register");
1958  return 0;
1959}
1960
1961namespace llvm {
1962unsigned getX86SubSuperRegister(unsigned Reg, MVT::ValueType VT, bool High) {
1963  switch (VT) {
1964  default: return Reg;
1965  case MVT::i8:
1966    if (High) {
1967      switch (Reg) {
1968      default: return 0;
1969      case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX:
1970        return X86::AH;
1971      case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX:
1972        return X86::DH;
1973      case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX:
1974        return X86::CH;
1975      case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX:
1976        return X86::BH;
1977      }
1978    } else {
1979      switch (Reg) {
1980      default: return 0;
1981      case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX:
1982        return X86::AL;
1983      case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX:
1984        return X86::DL;
1985      case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX:
1986        return X86::CL;
1987      case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX:
1988        return X86::BL;
1989      case X86::SIL: case X86::SI: case X86::ESI: case X86::RSI:
1990        return X86::SIL;
1991      case X86::DIL: case X86::DI: case X86::EDI: case X86::RDI:
1992        return X86::DIL;
1993      case X86::BPL: case X86::BP: case X86::EBP: case X86::RBP:
1994        return X86::BPL;
1995      case X86::SPL: case X86::SP: case X86::ESP: case X86::RSP:
1996        return X86::SPL;
1997      case X86::R8B: case X86::R8W: case X86::R8D: case X86::R8:
1998        return X86::R8B;
1999      case X86::R9B: case X86::R9W: case X86::R9D: case X86::R9:
2000        return X86::R9B;
2001      case X86::R10B: case X86::R10W: case X86::R10D: case X86::R10:
2002        return X86::R10B;
2003      case X86::R11B: case X86::R11W: case X86::R11D: case X86::R11:
2004        return X86::R11B;
2005      case X86::R12B: case X86::R12W: case X86::R12D: case X86::R12:
2006        return X86::R12B;
2007      case X86::R13B: case X86::R13W: case X86::R13D: case X86::R13:
2008        return X86::R13B;
2009      case X86::R14B: case X86::R14W: case X86::R14D: case X86::R14:
2010        return X86::R14B;
2011      case X86::R15B: case X86::R15W: case X86::R15D: case X86::R15:
2012        return X86::R15B;
2013      }
2014    }
2015  case MVT::i16:
2016    switch (Reg) {
2017    default: return Reg;
2018    case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX:
2019      return X86::AX;
2020    case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX:
2021      return X86::DX;
2022    case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX:
2023      return X86::CX;
2024    case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX:
2025      return X86::BX;
2026    case X86::SIL: case X86::SI: case X86::ESI: case X86::RSI:
2027      return X86::SI;
2028    case X86::DIL: case X86::DI: case X86::EDI: case X86::RDI:
2029      return X86::DI;
2030    case X86::BPL: case X86::BP: case X86::EBP: case X86::RBP:
2031      return X86::BP;
2032    case X86::SPL: case X86::SP: case X86::ESP: case X86::RSP:
2033      return X86::SP;
2034    case X86::R8B: case X86::R8W: case X86::R8D: case X86::R8:
2035      return X86::R8W;
2036    case X86::R9B: case X86::R9W: case X86::R9D: case X86::R9:
2037      return X86::R9W;
2038    case X86::R10B: case X86::R10W: case X86::R10D: case X86::R10:
2039      return X86::R10W;
2040    case X86::R11B: case X86::R11W: case X86::R11D: case X86::R11:
2041      return X86::R11W;
2042    case X86::R12B: case X86::R12W: case X86::R12D: case X86::R12:
2043      return X86::R12W;
2044    case X86::R13B: case X86::R13W: case X86::R13D: case X86::R13:
2045      return X86::R13W;
2046    case X86::R14B: case X86::R14W: case X86::R14D: case X86::R14:
2047      return X86::R14W;
2048    case X86::R15B: case X86::R15W: case X86::R15D: case X86::R15:
2049      return X86::R15W;
2050    }
2051  case MVT::i32:
2052    switch (Reg) {
2053    default: return Reg;
2054    case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX:
2055      return X86::EAX;
2056    case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX:
2057      return X86::EDX;
2058    case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX:
2059      return X86::ECX;
2060    case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX:
2061      return X86::EBX;
2062    case X86::SIL: case X86::SI: case X86::ESI: case X86::RSI:
2063      return X86::ESI;
2064    case X86::DIL: case X86::DI: case X86::EDI: case X86::RDI:
2065      return X86::EDI;
2066    case X86::BPL: case X86::BP: case X86::EBP: case X86::RBP:
2067      return X86::EBP;
2068    case X86::SPL: case X86::SP: case X86::ESP: case X86::RSP:
2069      return X86::ESP;
2070    case X86::R8B: case X86::R8W: case X86::R8D: case X86::R8:
2071      return X86::R8D;
2072    case X86::R9B: case X86::R9W: case X86::R9D: case X86::R9:
2073      return X86::R9D;
2074    case X86::R10B: case X86::R10W: case X86::R10D: case X86::R10:
2075      return X86::R10D;
2076    case X86::R11B: case X86::R11W: case X86::R11D: case X86::R11:
2077      return X86::R11D;
2078    case X86::R12B: case X86::R12W: case X86::R12D: case X86::R12:
2079      return X86::R12D;
2080    case X86::R13B: case X86::R13W: case X86::R13D: case X86::R13:
2081      return X86::R13D;
2082    case X86::R14B: case X86::R14W: case X86::R14D: case X86::R14:
2083      return X86::R14D;
2084    case X86::R15B: case X86::R15W: case X86::R15D: case X86::R15:
2085      return X86::R15D;
2086    }
2087  case MVT::i64:
2088    switch (Reg) {
2089    default: return Reg;
2090    case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX:
2091      return X86::RAX;
2092    case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX:
2093      return X86::RDX;
2094    case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX:
2095      return X86::RCX;
2096    case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX:
2097      return X86::RBX;
2098    case X86::SIL: case X86::SI: case X86::ESI: case X86::RSI:
2099      return X86::RSI;
2100    case X86::DIL: case X86::DI: case X86::EDI: case X86::RDI:
2101      return X86::RDI;
2102    case X86::BPL: case X86::BP: case X86::EBP: case X86::RBP:
2103      return X86::RBP;
2104    case X86::SPL: case X86::SP: case X86::ESP: case X86::RSP:
2105      return X86::RSP;
2106    case X86::R8B: case X86::R8W: case X86::R8D: case X86::R8:
2107      return X86::R8;
2108    case X86::R9B: case X86::R9W: case X86::R9D: case X86::R9:
2109      return X86::R9;
2110    case X86::R10B: case X86::R10W: case X86::R10D: case X86::R10:
2111      return X86::R10;
2112    case X86::R11B: case X86::R11W: case X86::R11D: case X86::R11:
2113      return X86::R11;
2114    case X86::R12B: case X86::R12W: case X86::R12D: case X86::R12:
2115      return X86::R12;
2116    case X86::R13B: case X86::R13W: case X86::R13D: case X86::R13:
2117      return X86::R13;
2118    case X86::R14B: case X86::R14W: case X86::R14D: case X86::R14:
2119      return X86::R14;
2120    case X86::R15B: case X86::R15W: case X86::R15D: case X86::R15:
2121      return X86::R15;
2122    }
2123  }
2124
2125  return Reg;
2126}
2127}
2128
2129#include "X86GenRegisterInfo.inc"
2130
2131