X86RegisterInfo.cpp revision 58184e6878fdab651bc7c9a59dab2687ca82ede2
1//===- X86RegisterInfo.cpp - X86 Register Information -----------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the X86 implementation of the MRegisterInfo class.  This
11// file is responsible for the frame pointer elimination optimization on X86.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
16#include "X86RegisterInfo.h"
17#include "X86InstrBuilder.h"
18#include "X86MachineFunctionInfo.h"
19#include "X86Subtarget.h"
20#include "X86TargetMachine.h"
21#include "llvm/Constants.h"
22#include "llvm/Function.h"
23#include "llvm/Type.h"
24#include "llvm/CodeGen/ValueTypes.h"
25#include "llvm/CodeGen/MachineInstrBuilder.h"
26#include "llvm/CodeGen/MachineFunction.h"
27#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineLocation.h"
29#include "llvm/CodeGen/SSARegMap.h"
30#include "llvm/Target/TargetAsmInfo.h"
31#include "llvm/Target/TargetFrameInfo.h"
32#include "llvm/Target/TargetInstrInfo.h"
33#include "llvm/Target/TargetMachine.h"
34#include "llvm/Target/TargetOptions.h"
35#include "llvm/Support/CommandLine.h"
36#include "llvm/ADT/BitVector.h"
37#include "llvm/ADT/STLExtras.h"
38using namespace llvm;
39
40namespace {
41  cl::opt<bool>
42  NoFusing("disable-spill-fusing",
43           cl::desc("Disable fusing of spill code into instructions"));
44  cl::opt<bool>
45  PrintFailedFusing("print-failed-fuse-candidates",
46                    cl::desc("Print instructions that the allocator wants to"
47                             " fuse, but the X86 backend currently can't"),
48                    cl::Hidden);
49}
50
51X86RegisterInfo::X86RegisterInfo(X86TargetMachine &tm,
52                                 const TargetInstrInfo &tii)
53  : X86GenRegisterInfo(X86::ADJCALLSTACKDOWN, X86::ADJCALLSTACKUP),
54    TM(tm), TII(tii) {
55  // Cache some information.
56  const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
57  Is64Bit = Subtarget->is64Bit();
58  if (Is64Bit) {
59    SlotSize = 8;
60    StackPtr = X86::RSP;
61    FramePtr = X86::RBP;
62  } else {
63    SlotSize = 4;
64    StackPtr = X86::ESP;
65    FramePtr = X86::EBP;
66  }
67
68  SmallVector<unsigned,16> AmbEntries;
69  static const unsigned OpTbl2Addr[][2] = {
70    { X86::ADC32ri,     X86::ADC32mi },
71    { X86::ADC32ri8,    X86::ADC32mi8 },
72    { X86::ADC32rr,     X86::ADC32mr },
73    { X86::ADC64ri32,   X86::ADC64mi32 },
74    { X86::ADC64ri8,    X86::ADC64mi8 },
75    { X86::ADC64rr,     X86::ADC64mr },
76    { X86::ADD16ri,     X86::ADD16mi },
77    { X86::ADD16ri8,    X86::ADD16mi8 },
78    { X86::ADD16rr,     X86::ADD16mr },
79    { X86::ADD32ri,     X86::ADD32mi },
80    { X86::ADD32ri8,    X86::ADD32mi8 },
81    { X86::ADD32rr,     X86::ADD32mr },
82    { X86::ADD64ri32,   X86::ADD64mi32 },
83    { X86::ADD64ri8,    X86::ADD64mi8 },
84    { X86::ADD64rr,     X86::ADD64mr },
85    { X86::ADD8ri,      X86::ADD8mi },
86    { X86::ADD8rr,      X86::ADD8mr },
87    { X86::AND16ri,     X86::AND16mi },
88    { X86::AND16ri8,    X86::AND16mi8 },
89    { X86::AND16rr,     X86::AND16mr },
90    { X86::AND32ri,     X86::AND32mi },
91    { X86::AND32ri8,    X86::AND32mi8 },
92    { X86::AND32rr,     X86::AND32mr },
93    { X86::AND64ri32,   X86::AND64mi32 },
94    { X86::AND64ri8,    X86::AND64mi8 },
95    { X86::AND64rr,     X86::AND64mr },
96    { X86::AND8ri,      X86::AND8mi },
97    { X86::AND8rr,      X86::AND8mr },
98    { X86::DEC16r,      X86::DEC16m },
99    { X86::DEC32r,      X86::DEC32m },
100    { X86::DEC64_16r,   X86::DEC16m },
101    { X86::DEC64_32r,   X86::DEC32m },
102    { X86::DEC64r,      X86::DEC64m },
103    { X86::DEC8r,       X86::DEC8m },
104    { X86::INC16r,      X86::INC16m },
105    { X86::INC32r,      X86::INC32m },
106    { X86::INC64_16r,   X86::INC16m },
107    { X86::INC64_32r,   X86::INC32m },
108    { X86::INC64r,      X86::INC64m },
109    { X86::INC8r,       X86::INC8m },
110    { X86::NEG16r,      X86::NEG16m },
111    { X86::NEG32r,      X86::NEG32m },
112    { X86::NEG64r,      X86::NEG64m },
113    { X86::NEG8r,       X86::NEG8m },
114    { X86::NOT16r,      X86::NOT16m },
115    { X86::NOT32r,      X86::NOT32m },
116    { X86::NOT64r,      X86::NOT64m },
117    { X86::NOT8r,       X86::NOT8m },
118    { X86::OR16ri,      X86::OR16mi },
119    { X86::OR16ri8,     X86::OR16mi8 },
120    { X86::OR16rr,      X86::OR16mr },
121    { X86::OR32ri,      X86::OR32mi },
122    { X86::OR32ri8,     X86::OR32mi8 },
123    { X86::OR32rr,      X86::OR32mr },
124    { X86::OR64ri32,    X86::OR64mi32 },
125    { X86::OR64ri8,     X86::OR64mi8 },
126    { X86::OR64rr,      X86::OR64mr },
127    { X86::OR8ri,       X86::OR8mi },
128    { X86::OR8rr,       X86::OR8mr },
129    { X86::ROL16r1,     X86::ROL16m1 },
130    { X86::ROL16rCL,    X86::ROL16mCL },
131    { X86::ROL16ri,     X86::ROL16mi },
132    { X86::ROL32r1,     X86::ROL32m1 },
133    { X86::ROL32rCL,    X86::ROL32mCL },
134    { X86::ROL32ri,     X86::ROL32mi },
135    { X86::ROL64r1,     X86::ROL64m1 },
136    { X86::ROL64rCL,    X86::ROL64mCL },
137    { X86::ROL64ri,     X86::ROL64mi },
138    { X86::ROL8r1,      X86::ROL8m1 },
139    { X86::ROL8rCL,     X86::ROL8mCL },
140    { X86::ROL8ri,      X86::ROL8mi },
141    { X86::ROR16r1,     X86::ROR16m1 },
142    { X86::ROR16rCL,    X86::ROR16mCL },
143    { X86::ROR16ri,     X86::ROR16mi },
144    { X86::ROR32r1,     X86::ROR32m1 },
145    { X86::ROR32rCL,    X86::ROR32mCL },
146    { X86::ROR32ri,     X86::ROR32mi },
147    { X86::ROR64r1,     X86::ROR64m1 },
148    { X86::ROR64rCL,    X86::ROR64mCL },
149    { X86::ROR64ri,     X86::ROR64mi },
150    { X86::ROR8r1,      X86::ROR8m1 },
151    { X86::ROR8rCL,     X86::ROR8mCL },
152    { X86::ROR8ri,      X86::ROR8mi },
153    { X86::SAR16r1,     X86::SAR16m1 },
154    { X86::SAR16rCL,    X86::SAR16mCL },
155    { X86::SAR16ri,     X86::SAR16mi },
156    { X86::SAR32r1,     X86::SAR32m1 },
157    { X86::SAR32rCL,    X86::SAR32mCL },
158    { X86::SAR32ri,     X86::SAR32mi },
159    { X86::SAR64r1,     X86::SAR64m1 },
160    { X86::SAR64rCL,    X86::SAR64mCL },
161    { X86::SAR64ri,     X86::SAR64mi },
162    { X86::SAR8r1,      X86::SAR8m1 },
163    { X86::SAR8rCL,     X86::SAR8mCL },
164    { X86::SAR8ri,      X86::SAR8mi },
165    { X86::SBB32ri,     X86::SBB32mi },
166    { X86::SBB32ri8,    X86::SBB32mi8 },
167    { X86::SBB32rr,     X86::SBB32mr },
168    { X86::SBB64ri32,   X86::SBB64mi32 },
169    { X86::SBB64ri8,    X86::SBB64mi8 },
170    { X86::SBB64rr,     X86::SBB64mr },
171    { X86::SHL16r1,     X86::SHL16m1 },
172    { 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
1276
1277const unsigned *
1278X86RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
1279  static const unsigned CalleeSavedRegs32Bit[] = {
1280    X86::ESI, X86::EDI, X86::EBX, X86::EBP,  0
1281  };
1282
1283  static const unsigned CalleeSavedRegs32EHRet[] = {
1284    X86::EAX, X86::EDX, X86::ESI, X86::EDI, X86::EBX, X86::EBP,  0
1285  };
1286
1287  static const unsigned CalleeSavedRegs64Bit[] = {
1288    X86::RBX, X86::R12, X86::R13, X86::R14, X86::R15, X86::RBP, 0
1289  };
1290
1291  if (Is64Bit)
1292    return CalleeSavedRegs64Bit;
1293  else {
1294    if (MF) {
1295        MachineFrameInfo *MFI = MF->getFrameInfo();
1296        MachineModuleInfo *MMI = MFI->getMachineModuleInfo();
1297        if (MMI && MMI->callsEHReturn())
1298          return CalleeSavedRegs32EHRet;
1299    }
1300    return CalleeSavedRegs32Bit;
1301  }
1302}
1303
1304const TargetRegisterClass* const*
1305X86RegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const {
1306  static const TargetRegisterClass * const CalleeSavedRegClasses32Bit[] = {
1307    &X86::GR32RegClass, &X86::GR32RegClass,
1308    &X86::GR32RegClass, &X86::GR32RegClass,  0
1309  };
1310  static const TargetRegisterClass * const CalleeSavedRegClasses32EHRet[] = {
1311    &X86::GR32RegClass, &X86::GR32RegClass,
1312    &X86::GR32RegClass, &X86::GR32RegClass,
1313    &X86::GR32RegClass, &X86::GR32RegClass,  0
1314  };
1315  static const TargetRegisterClass * const CalleeSavedRegClasses64Bit[] = {
1316    &X86::GR64RegClass, &X86::GR64RegClass,
1317    &X86::GR64RegClass, &X86::GR64RegClass,
1318    &X86::GR64RegClass, &X86::GR64RegClass, 0
1319  };
1320
1321  if (Is64Bit)
1322    return CalleeSavedRegClasses64Bit;
1323  else {
1324    if (MF) {
1325        MachineFrameInfo *MFI = MF->getFrameInfo();
1326        MachineModuleInfo *MMI = MFI->getMachineModuleInfo();
1327        if (MMI && MMI->callsEHReturn())
1328          return CalleeSavedRegClasses32EHRet;
1329    }
1330    return CalleeSavedRegClasses32Bit;
1331  }
1332
1333}
1334
1335BitVector X86RegisterInfo::getReservedRegs(const MachineFunction &MF) const {
1336  BitVector Reserved(getNumRegs());
1337  Reserved.set(X86::RSP);
1338  Reserved.set(X86::ESP);
1339  Reserved.set(X86::SP);
1340  Reserved.set(X86::SPL);
1341  if (hasFP(MF)) {
1342    Reserved.set(X86::RBP);
1343    Reserved.set(X86::EBP);
1344    Reserved.set(X86::BP);
1345    Reserved.set(X86::BPL);
1346  }
1347  return Reserved;
1348}
1349
1350//===----------------------------------------------------------------------===//
1351// Stack Frame Processing methods
1352//===----------------------------------------------------------------------===//
1353
1354// hasFP - Return true if the specified function should have a dedicated frame
1355// pointer register.  This is true if the function has variable sized allocas or
1356// if frame pointer elimination is disabled.
1357//
1358bool X86RegisterInfo::hasFP(const MachineFunction &MF) const {
1359  MachineFrameInfo *MFI = MF.getFrameInfo();
1360  MachineModuleInfo *MMI = MFI->getMachineModuleInfo();
1361
1362  return (NoFramePointerElim ||
1363          MFI->hasVarSizedObjects() ||
1364          MF.getInfo<X86MachineFunctionInfo>()->getForceFramePointer() ||
1365          (MMI && MMI->callsUnwindInit()));
1366}
1367
1368bool X86RegisterInfo::hasReservedCallFrame(MachineFunction &MF) const {
1369  return !MF.getFrameInfo()->hasVarSizedObjects();
1370}
1371
1372void X86RegisterInfo::
1373eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
1374                              MachineBasicBlock::iterator I) const {
1375  if (!hasReservedCallFrame(MF)) {
1376    // If the stack pointer can be changed after prologue, turn the
1377    // adjcallstackup instruction into a 'sub ESP, <amt>' and the
1378    // adjcallstackdown instruction into 'add ESP, <amt>'
1379    // TODO: consider using push / pop instead of sub + store / add
1380    MachineInstr *Old = I;
1381    uint64_t Amount = Old->getOperand(0).getImm();
1382    if (Amount != 0) {
1383      // We need to keep the stack aligned properly.  To do this, we round the
1384      // amount of space needed for the outgoing arguments up to the next
1385      // alignment boundary.
1386      unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1387      Amount = (Amount+Align-1)/Align*Align;
1388
1389      MachineInstr *New = 0;
1390      if (Old->getOpcode() == X86::ADJCALLSTACKDOWN) {
1391        New=BuildMI(TII.get(Is64Bit ? X86::SUB64ri32 : X86::SUB32ri), StackPtr)
1392          .addReg(StackPtr).addImm(Amount);
1393      } else {
1394        assert(Old->getOpcode() == X86::ADJCALLSTACKUP);
1395        // factor out the amount the callee already popped.
1396        uint64_t CalleeAmt = Old->getOperand(1).getImm();
1397        Amount -= CalleeAmt;
1398        if (Amount) {
1399          unsigned Opc = (Amount < 128) ?
1400            (Is64Bit ? X86::ADD64ri8 : X86::ADD32ri8) :
1401            (Is64Bit ? X86::ADD64ri32 : X86::ADD32ri);
1402          New = BuildMI(TII.get(Opc),  StackPtr)
1403                        .addReg(StackPtr).addImm(Amount);
1404        }
1405      }
1406
1407      // Replace the pseudo instruction with a new instruction...
1408      if (New) MBB.insert(I, New);
1409    }
1410  } else if (I->getOpcode() == X86::ADJCALLSTACKUP) {
1411    // If we are performing frame pointer elimination and if the callee pops
1412    // something off the stack pointer, add it back.  We do this until we have
1413    // more advanced stack pointer tracking ability.
1414    if (uint64_t CalleeAmt = I->getOperand(1).getImm()) {
1415      unsigned Opc = (CalleeAmt < 128) ?
1416        (Is64Bit ? X86::SUB64ri8 : X86::SUB32ri8) :
1417        (Is64Bit ? X86::SUB64ri32 : X86::SUB32ri);
1418      MachineInstr *New =
1419        BuildMI(TII.get(Opc), StackPtr).addReg(StackPtr).addImm(CalleeAmt);
1420      MBB.insert(I, New);
1421    }
1422  }
1423
1424  MBB.erase(I);
1425}
1426
1427void X86RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
1428                                          int SPAdj, RegScavenger *RS) const{
1429  assert(SPAdj == 0 && "Unexpected");
1430
1431  unsigned i = 0;
1432  MachineInstr &MI = *II;
1433  MachineFunction &MF = *MI.getParent()->getParent();
1434  while (!MI.getOperand(i).isFrameIndex()) {
1435    ++i;
1436    assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
1437  }
1438
1439  int FrameIndex = MI.getOperand(i).getFrameIndex();
1440  // This must be part of a four operand memory reference.  Replace the
1441  // FrameIndex with base register with EBP.  Add an offset to the offset.
1442  MI.getOperand(i).ChangeToRegister(hasFP(MF) ? FramePtr : StackPtr, false);
1443
1444  // Now add the frame object offset to the offset from EBP.
1445  int64_t Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) +
1446                   MI.getOperand(i+3).getImm()+SlotSize;
1447
1448  if (!hasFP(MF))
1449    Offset += MF.getFrameInfo()->getStackSize();
1450  else {
1451    Offset += SlotSize;  // Skip the saved EBP
1452    // Skip the RETADDR move area
1453    X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
1454    int TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta();
1455    if (TailCallReturnAddrDelta < 0) Offset -= TailCallReturnAddrDelta;
1456  }
1457
1458  MI.getOperand(i+3).ChangeToImmediate(Offset);
1459}
1460
1461void
1462X86RegisterInfo::processFunctionBeforeFrameFinalized(MachineFunction &MF) const{
1463  X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
1464  int32_t TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta();
1465  if (TailCallReturnAddrDelta < 0) {
1466    // create RETURNADDR area
1467    //   arg
1468    //   arg
1469    //   RETADDR
1470    //   { ...
1471    //     RETADDR area
1472    //     ...
1473    //   }
1474    //   [EBP]
1475    MF.getFrameInfo()->
1476      CreateFixedObject(-TailCallReturnAddrDelta,
1477                        (-1*SlotSize)+TailCallReturnAddrDelta);
1478  }
1479  if (hasFP(MF)) {
1480    assert((TailCallReturnAddrDelta <= 0) &&
1481           "The Delta should always be zero or negative");
1482    // Create a frame entry for the EBP register that must be saved.
1483    int FrameIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize,
1484                                                        (int)SlotSize * -2+
1485                                                       TailCallReturnAddrDelta);
1486    assert(FrameIdx == MF.getFrameInfo()->getObjectIndexBegin() &&
1487           "Slot for EBP register must be last in order to be found!");
1488  }
1489}
1490
1491/// emitSPUpdate - Emit a series of instructions to increment / decrement the
1492/// stack pointer by a constant value.
1493static
1494void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
1495                  unsigned StackPtr, int64_t NumBytes, bool Is64Bit,
1496                  const TargetInstrInfo &TII) {
1497  bool isSub = NumBytes < 0;
1498  uint64_t Offset = isSub ? -NumBytes : NumBytes;
1499  unsigned Opc = isSub
1500    ? ((Offset < 128) ?
1501       (Is64Bit ? X86::SUB64ri8 : X86::SUB32ri8) :
1502       (Is64Bit ? X86::SUB64ri32 : X86::SUB32ri))
1503    : ((Offset < 128) ?
1504       (Is64Bit ? X86::ADD64ri8 : X86::ADD32ri8) :
1505       (Is64Bit ? X86::ADD64ri32 : X86::ADD32ri));
1506  uint64_t Chunk = (1LL << 31) - 1;
1507
1508  while (Offset) {
1509    uint64_t ThisVal = (Offset > Chunk) ? Chunk : Offset;
1510    BuildMI(MBB, MBBI, TII.get(Opc), StackPtr).addReg(StackPtr).addImm(ThisVal);
1511    Offset -= ThisVal;
1512  }
1513}
1514
1515// mergeSPUpdatesUp - Merge two stack-manipulating instructions upper iterator.
1516static
1517void mergeSPUpdatesUp(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
1518                      unsigned StackPtr, uint64_t *NumBytes = NULL) {
1519  if (MBBI == MBB.begin()) return;
1520
1521  MachineBasicBlock::iterator PI = prior(MBBI);
1522  unsigned Opc = PI->getOpcode();
1523  if ((Opc == X86::ADD64ri32 || Opc == X86::ADD64ri8 ||
1524       Opc == X86::ADD32ri || Opc == X86::ADD32ri8) &&
1525      PI->getOperand(0).getReg() == StackPtr) {
1526    if (NumBytes)
1527      *NumBytes += PI->getOperand(2).getImm();
1528    MBB.erase(PI);
1529  } else if ((Opc == X86::SUB64ri32 || Opc == X86::SUB64ri8 ||
1530              Opc == X86::SUB32ri || Opc == X86::SUB32ri8) &&
1531             PI->getOperand(0).getReg() == StackPtr) {
1532    if (NumBytes)
1533      *NumBytes -= PI->getOperand(2).getImm();
1534    MBB.erase(PI);
1535  }
1536}
1537
1538// mergeSPUpdatesUp - Merge two stack-manipulating instructions lower iterator.
1539static
1540void mergeSPUpdatesDown(MachineBasicBlock &MBB,
1541                        MachineBasicBlock::iterator &MBBI,
1542                        unsigned StackPtr, uint64_t *NumBytes = NULL) {
1543  return;
1544
1545  if (MBBI == MBB.end()) return;
1546
1547  MachineBasicBlock::iterator NI = next(MBBI);
1548  if (NI == MBB.end()) return;
1549
1550  unsigned Opc = NI->getOpcode();
1551  if ((Opc == X86::ADD64ri32 || Opc == X86::ADD64ri8 ||
1552       Opc == X86::ADD32ri || Opc == X86::ADD32ri8) &&
1553      NI->getOperand(0).getReg() == StackPtr) {
1554    if (NumBytes)
1555      *NumBytes -= NI->getOperand(2).getImm();
1556    MBB.erase(NI);
1557    MBBI = NI;
1558  } else if ((Opc == X86::SUB64ri32 || Opc == X86::SUB64ri8 ||
1559              Opc == X86::SUB32ri || Opc == X86::SUB32ri8) &&
1560             NI->getOperand(0).getReg() == StackPtr) {
1561    if (NumBytes)
1562      *NumBytes += NI->getOperand(2).getImm();
1563    MBB.erase(NI);
1564    MBBI = NI;
1565  }
1566}
1567
1568/// mergeSPUpdates - Checks the instruction before/after the passed
1569/// instruction. If it is an ADD/SUB instruction it is deleted
1570/// argument and the stack adjustment is returned as a positive value for ADD
1571/// and a negative for SUB.
1572static int mergeSPUpdates(MachineBasicBlock &MBB,
1573                           MachineBasicBlock::iterator &MBBI,
1574                           unsigned StackPtr,
1575                           bool doMergeWithPrevious) {
1576
1577  if ((doMergeWithPrevious && MBBI == MBB.begin()) ||
1578      (!doMergeWithPrevious && MBBI == MBB.end()))
1579    return 0;
1580
1581  int Offset = 0;
1582
1583  MachineBasicBlock::iterator PI = doMergeWithPrevious ? prior(MBBI) : MBBI;
1584  MachineBasicBlock::iterator NI = doMergeWithPrevious ? 0 : next(MBBI);
1585  unsigned Opc = PI->getOpcode();
1586  if ((Opc == X86::ADD64ri32 || Opc == X86::ADD64ri8 ||
1587       Opc == X86::ADD32ri || Opc == X86::ADD32ri8) &&
1588      PI->getOperand(0).getReg() == StackPtr){
1589    Offset += PI->getOperand(2).getImm();
1590    MBB.erase(PI);
1591    if (!doMergeWithPrevious) MBBI = NI;
1592  } else if ((Opc == X86::SUB64ri32 || Opc == X86::SUB64ri8 ||
1593              Opc == X86::SUB32ri || Opc == X86::SUB32ri8) &&
1594             PI->getOperand(0).getReg() == StackPtr) {
1595    Offset -= PI->getOperand(2).getImm();
1596    MBB.erase(PI);
1597    if (!doMergeWithPrevious) MBBI = NI;
1598  }
1599
1600  return Offset;
1601}
1602
1603void X86RegisterInfo::emitPrologue(MachineFunction &MF) const {
1604  MachineBasicBlock &MBB = MF.front();   // Prolog goes in entry BB
1605  MachineFrameInfo *MFI = MF.getFrameInfo();
1606  unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1607  const Function* Fn = MF.getFunction();
1608  const X86Subtarget* Subtarget = &MF.getTarget().getSubtarget<X86Subtarget>();
1609  MachineModuleInfo *MMI = MFI->getMachineModuleInfo();
1610  X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
1611  MachineBasicBlock::iterator MBBI = MBB.begin();
1612
1613  // Prepare for frame info.
1614  unsigned FrameLabelId = 0;
1615
1616  // Get the number of bytes to allocate from the FrameInfo.
1617  uint64_t StackSize = MFI->getStackSize();
1618  // Add RETADDR move area to callee saved frame size.
1619  int TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta();
1620  if (TailCallReturnAddrDelta < 0)
1621    X86FI->setCalleeSavedFrameSize(
1622          X86FI->getCalleeSavedFrameSize() +(-TailCallReturnAddrDelta));
1623  uint64_t NumBytes = StackSize - X86FI->getCalleeSavedFrameSize();
1624
1625  // Insert stack pointer adjustment for later moving of return addr.  Only
1626  // applies to tail call optimized functions where the callee argument stack
1627  // size is bigger than the callers.
1628  if (TailCallReturnAddrDelta < 0) {
1629    BuildMI(MBB, MBBI, TII.get(Is64Bit? X86::SUB64ri32 : X86::SUB32ri),
1630            StackPtr).addReg(StackPtr).addImm(-TailCallReturnAddrDelta);
1631  }
1632
1633  if (hasFP(MF)) {
1634    // Get the offset of the stack slot for the EBP register... which is
1635    // guaranteed to be the last slot by processFunctionBeforeFrameFinalized.
1636    // Update the frame offset adjustment.
1637    MFI->setOffsetAdjustment(SlotSize-NumBytes);
1638
1639    // Save EBP into the appropriate stack slot...
1640    BuildMI(MBB, MBBI, TII.get(Is64Bit ? X86::PUSH64r : X86::PUSH32r))
1641      .addReg(FramePtr);
1642    NumBytes -= SlotSize;
1643
1644    if (MMI && MMI->needsFrameInfo()) {
1645      // Mark effective beginning of when frame pointer becomes valid.
1646      FrameLabelId = MMI->NextLabelID();
1647      BuildMI(MBB, MBBI, TII.get(X86::LABEL)).addImm(FrameLabelId);
1648    }
1649
1650    // Update EBP with the new base value...
1651    BuildMI(MBB, MBBI, TII.get(Is64Bit ? X86::MOV64rr : X86::MOV32rr), FramePtr)
1652      .addReg(StackPtr);
1653  }
1654
1655  unsigned ReadyLabelId = 0;
1656  if (MMI && MMI->needsFrameInfo()) {
1657    // Mark effective beginning of when frame pointer is ready.
1658    ReadyLabelId = MMI->NextLabelID();
1659    BuildMI(MBB, MBBI, TII.get(X86::LABEL)).addImm(ReadyLabelId);
1660  }
1661
1662  // Skip the callee-saved push instructions.
1663  while (MBBI != MBB.end() &&
1664         (MBBI->getOpcode() == X86::PUSH32r ||
1665          MBBI->getOpcode() == X86::PUSH64r))
1666    ++MBBI;
1667
1668  if (NumBytes) {   // adjust stack pointer: ESP -= numbytes
1669    if (NumBytes >= 4096 && Subtarget->isTargetCygMing()) {
1670      // Check, whether EAX is livein for this function
1671      bool isEAXAlive = false;
1672      for (MachineFunction::livein_iterator II = MF.livein_begin(),
1673             EE = MF.livein_end(); (II != EE) && !isEAXAlive; ++II) {
1674        unsigned Reg = II->first;
1675        isEAXAlive = (Reg == X86::EAX || Reg == X86::AX ||
1676                      Reg == X86::AH || Reg == X86::AL);
1677      }
1678
1679      // Function prologue calls _alloca to probe the stack when allocating
1680      // more than 4k bytes in one go. Touching the stack at 4K increments is
1681      // necessary to ensure that the guard pages used by the OS virtual memory
1682      // manager are allocated in correct sequence.
1683      if (!isEAXAlive) {
1684        BuildMI(MBB, MBBI, TII.get(X86::MOV32ri), X86::EAX).addImm(NumBytes);
1685        BuildMI(MBB, MBBI, TII.get(X86::CALLpcrel32))
1686          .addExternalSymbol("_alloca");
1687      } else {
1688        // Save EAX
1689        BuildMI(MBB, MBBI, TII.get(X86::PUSH32r), X86::EAX);
1690        // Allocate NumBytes-4 bytes on stack. We'll also use 4 already
1691        // allocated bytes for EAX.
1692        BuildMI(MBB, MBBI, TII.get(X86::MOV32ri), X86::EAX).addImm(NumBytes-4);
1693        BuildMI(MBB, MBBI, TII.get(X86::CALLpcrel32))
1694          .addExternalSymbol("_alloca");
1695        // Restore EAX
1696        MachineInstr *MI = addRegOffset(BuildMI(TII.get(X86::MOV32rm),X86::EAX),
1697                                        StackPtr, NumBytes-4);
1698        MBB.insert(MBBI, MI);
1699      }
1700    } else {
1701      // If there is an SUB32ri of ESP immediately before this instruction,
1702      // merge the two. This can be the case when tail call elimination is
1703      // enabled and the callee has more arguments then the caller.
1704      NumBytes -= mergeSPUpdates(MBB, MBBI, StackPtr, true);
1705      // If there is an ADD32ri or SUB32ri of ESP immediately after this
1706      // instruction, merge the two instructions.
1707      mergeSPUpdatesDown(MBB, MBBI, StackPtr, &NumBytes);
1708
1709      if (NumBytes)
1710        emitSPUpdate(MBB, MBBI, StackPtr, -(int64_t)NumBytes, Is64Bit, TII);
1711    }
1712  }
1713
1714  if (MMI && MMI->needsFrameInfo()) {
1715    std::vector<MachineMove> &Moves = MMI->getFrameMoves();
1716    const TargetData *TD = MF.getTarget().getTargetData();
1717
1718    // Calculate amount of bytes used for return address storing
1719    int stackGrowth =
1720      (MF.getTarget().getFrameInfo()->getStackGrowthDirection() ==
1721       TargetFrameInfo::StackGrowsUp ?
1722       TD->getPointerSize() : -TD->getPointerSize());
1723
1724    if (StackSize) {
1725      // Show update of SP.
1726      if (hasFP(MF)) {
1727        // Adjust SP
1728        MachineLocation SPDst(MachineLocation::VirtualFP);
1729        MachineLocation SPSrc(MachineLocation::VirtualFP, 2*stackGrowth);
1730        Moves.push_back(MachineMove(FrameLabelId, SPDst, SPSrc));
1731      } else {
1732        MachineLocation SPDst(MachineLocation::VirtualFP);
1733        MachineLocation SPSrc(MachineLocation::VirtualFP, -StackSize+stackGrowth);
1734        Moves.push_back(MachineMove(FrameLabelId, SPDst, SPSrc));
1735      }
1736    } else {
1737      //FIXME: Verify & implement for FP
1738      MachineLocation SPDst(StackPtr);
1739      MachineLocation SPSrc(StackPtr, stackGrowth);
1740      Moves.push_back(MachineMove(FrameLabelId, SPDst, SPSrc));
1741    }
1742
1743    // Add callee saved registers to move list.
1744    const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
1745
1746    // FIXME: This is dirty hack. The code itself is pretty mess right now.
1747    // It should be rewritten from scratch and generalized sometimes.
1748
1749    // Determine maximum offset (minumum due to stack growth)
1750    int64_t MaxOffset = 0;
1751    for (unsigned I = 0, E = CSI.size(); I!=E; ++I)
1752      MaxOffset = std::min(MaxOffset,
1753                           MFI->getObjectOffset(CSI[I].getFrameIdx()));
1754
1755    // Calculate offsets
1756    for (unsigned I = 0, E = CSI.size(); I!=E; ++I) {
1757      int64_t Offset = MFI->getObjectOffset(CSI[I].getFrameIdx());
1758      unsigned Reg = CSI[I].getReg();
1759      Offset = (MaxOffset-Offset+3*stackGrowth);
1760      MachineLocation CSDst(MachineLocation::VirtualFP, Offset);
1761      MachineLocation CSSrc(Reg);
1762      Moves.push_back(MachineMove(FrameLabelId, CSDst, CSSrc));
1763    }
1764
1765    if (hasFP(MF)) {
1766      // Save FP
1767      MachineLocation FPDst(MachineLocation::VirtualFP, 2*stackGrowth);
1768      MachineLocation FPSrc(FramePtr);
1769      Moves.push_back(MachineMove(ReadyLabelId, FPDst, FPSrc));
1770    }
1771
1772    MachineLocation FPDst(hasFP(MF) ? FramePtr : StackPtr);
1773    MachineLocation FPSrc(MachineLocation::VirtualFP);
1774    Moves.push_back(MachineMove(ReadyLabelId, FPDst, FPSrc));
1775  }
1776
1777  // If it's main() on Cygwin\Mingw32 we should align stack as well
1778  if (Fn->hasExternalLinkage() && Fn->getName() == "main" &&
1779      Subtarget->isTargetCygMing()) {
1780    BuildMI(MBB, MBBI, TII.get(X86::AND32ri), X86::ESP)
1781                .addReg(X86::ESP).addImm(-Align);
1782
1783    // Probe the stack
1784    BuildMI(MBB, MBBI, TII.get(X86::MOV32ri), X86::EAX).addImm(Align);
1785    BuildMI(MBB, MBBI, TII.get(X86::CALLpcrel32)).addExternalSymbol("_alloca");
1786  }
1787}
1788
1789void X86RegisterInfo::emitEpilogue(MachineFunction &MF,
1790                                   MachineBasicBlock &MBB) const {
1791  const MachineFrameInfo *MFI = MF.getFrameInfo();
1792  const Function* Fn = MF.getFunction();
1793  X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
1794  const X86Subtarget* Subtarget = &MF.getTarget().getSubtarget<X86Subtarget>();
1795  MachineBasicBlock::iterator MBBI = prior(MBB.end());
1796  unsigned RetOpcode = MBBI->getOpcode();
1797
1798  switch (RetOpcode) {
1799  case X86::RET:
1800  case X86::RETI:
1801  case X86::TCRETURNdi:
1802  case X86::TCRETURNri:
1803  case X86::TCRETURNri64:
1804  case X86::TCRETURNdi64:
1805  case X86::EH_RETURN:
1806  case X86::TAILJMPd:
1807  case X86::TAILJMPr:
1808  case X86::TAILJMPm: break;  // These are ok
1809  default:
1810    assert(0 && "Can only insert epilog into returning blocks");
1811  }
1812
1813  // Get the number of bytes to allocate from the FrameInfo
1814  uint64_t StackSize = MFI->getStackSize();
1815  unsigned CSSize = X86FI->getCalleeSavedFrameSize();
1816  uint64_t NumBytes = StackSize - CSSize;
1817
1818  if (hasFP(MF)) {
1819    // pop EBP.
1820    BuildMI(MBB, MBBI, TII.get(Is64Bit ? X86::POP64r : X86::POP32r), FramePtr);
1821    NumBytes -= SlotSize;
1822  }
1823
1824  // Skip the callee-saved pop instructions.
1825  while (MBBI != MBB.begin()) {
1826    MachineBasicBlock::iterator PI = prior(MBBI);
1827    unsigned Opc = PI->getOpcode();
1828    if (Opc != X86::POP32r && Opc != X86::POP64r && !TII.isTerminatorInstr(Opc))
1829      break;
1830    --MBBI;
1831  }
1832
1833  // If there is an ADD32ri or SUB32ri of ESP immediately before this
1834  // instruction, merge the two instructions.
1835  if (NumBytes || MFI->hasVarSizedObjects())
1836    mergeSPUpdatesUp(MBB, MBBI, StackPtr, &NumBytes);
1837
1838  // If dynamic alloca is used, then reset esp to point to the last callee-saved
1839  // slot before popping them off!  Also, if it's main() on Cygwin/Mingw32 we
1840  // aligned stack in the prologue, - revert stack changes back. Note: we're
1841  // assuming, that frame pointer was forced for main()
1842  if (MFI->hasVarSizedObjects() ||
1843      (Fn->hasExternalLinkage() && Fn->getName() == "main" &&
1844       Subtarget->isTargetCygMing())) {
1845    unsigned Opc = Is64Bit ? X86::LEA64r : X86::LEA32r;
1846    if (CSSize) {
1847      MachineInstr *MI = addRegOffset(BuildMI(TII.get(Opc), StackPtr),
1848                                      FramePtr, -CSSize);
1849      MBB.insert(MBBI, MI);
1850    } else
1851      BuildMI(MBB, MBBI, TII.get(Is64Bit ? X86::MOV64rr : X86::MOV32rr),StackPtr).
1852        addReg(FramePtr);
1853
1854    NumBytes = 0;
1855  }
1856
1857  // adjust stack pointer back: ESP += numbytes
1858  if (NumBytes)
1859    emitSPUpdate(MBB, MBBI, StackPtr, NumBytes, Is64Bit, TII);
1860
1861  // We're returning from function via eh_return.
1862  if (RetOpcode == X86::EH_RETURN) {
1863    MBBI = prior(MBB.end());
1864    MachineOperand &DestAddr  = MBBI->getOperand(0);
1865    assert(DestAddr.isRegister() && "Offset should be in register!");
1866    BuildMI(MBB, MBBI, TII.get(Is64Bit ? X86::MOV64rr : X86::MOV32rr),StackPtr).
1867      addReg(DestAddr.getReg());
1868  // Tail call return: adjust the stack pointer and jump to callee
1869  } else if (RetOpcode == X86::TCRETURNri || RetOpcode == X86::TCRETURNdi ||
1870             RetOpcode== X86::TCRETURNri64 || RetOpcode == X86::TCRETURNdi64) {
1871    MBBI = prior(MBB.end());
1872    MachineOperand &JumpTarget = MBBI->getOperand(0);
1873    MachineOperand &StackAdjust = MBBI->getOperand(1);
1874    assert( StackAdjust.isImmediate() && "Expecting immediate value.");
1875
1876    // Adjust stack pointer.
1877    int StackAdj = StackAdjust.getImm();
1878    int MaxTCDelta = X86FI->getTCReturnAddrDelta();
1879    int Offset = 0;
1880    assert(MaxTCDelta <= 0 && "MaxTCDelta should never be positive");
1881    // Incoporate the retaddr area.
1882    Offset = StackAdj-MaxTCDelta;
1883    assert(Offset >= 0 && "Offset should never be negative");
1884    if (Offset) {
1885      // Check for possible merge with preceeding ADD instruction.
1886      Offset += mergeSPUpdates(MBB, MBBI, StackPtr, true);
1887      emitSPUpdate(MBB, MBBI, StackPtr, Offset, Is64Bit, TII);
1888    }
1889    // Jump to label or value in register.
1890    if (RetOpcode == X86::TCRETURNdi|| RetOpcode == X86::TCRETURNdi64)
1891      BuildMI(MBB, MBBI, TII.get(X86::TAILJMPd)).
1892        addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
1893    else if (RetOpcode== X86::TCRETURNri64) {
1894      BuildMI(MBB, MBBI, TII.get(X86::TAILJMPr64), JumpTarget.getReg());
1895    } else
1896       BuildMI(MBB, MBBI, TII.get(X86::TAILJMPr), JumpTarget.getReg());
1897    // Delete the pseudo instruction TCRETURN.
1898    MBB.erase(MBBI);
1899  } else if ((RetOpcode == X86::RET || RetOpcode == X86::RETI) &&
1900             (X86FI->getTCReturnAddrDelta() < 0)) {
1901    // Add the return addr area delta back since we are not tail calling.
1902    int delta = -1*X86FI->getTCReturnAddrDelta();
1903    MBBI = prior(MBB.end());
1904    // Check for possible merge with preceeding ADD instruction.
1905    delta += mergeSPUpdates(MBB, MBBI, StackPtr, true);
1906    emitSPUpdate(MBB, MBBI, StackPtr, delta, Is64Bit, TII);
1907  }
1908}
1909
1910unsigned X86RegisterInfo::getRARegister() const {
1911  if (Is64Bit)
1912    return X86::RIP;  // Should have dwarf #16
1913  else
1914    return X86::EIP;  // Should have dwarf #8
1915}
1916
1917unsigned X86RegisterInfo::getFrameRegister(MachineFunction &MF) const {
1918  return hasFP(MF) ? FramePtr : StackPtr;
1919}
1920
1921void X86RegisterInfo::getInitialFrameState(std::vector<MachineMove> &Moves)
1922                                                                         const {
1923  // Calculate amount of bytes used for return address storing
1924  int stackGrowth = (Is64Bit ? -8 : -4);
1925
1926  // Initial state of the frame pointer is esp+4.
1927  MachineLocation Dst(MachineLocation::VirtualFP);
1928  MachineLocation Src(StackPtr, stackGrowth);
1929  Moves.push_back(MachineMove(0, Dst, Src));
1930
1931  // Add return address to move list
1932  MachineLocation CSDst(StackPtr, stackGrowth);
1933  MachineLocation CSSrc(getRARegister());
1934  Moves.push_back(MachineMove(0, CSDst, CSSrc));
1935}
1936
1937unsigned X86RegisterInfo::getEHExceptionRegister() const {
1938  assert(0 && "What is the exception register");
1939  return 0;
1940}
1941
1942unsigned X86RegisterInfo::getEHHandlerRegister() const {
1943  assert(0 && "What is the exception handler register");
1944  return 0;
1945}
1946
1947namespace llvm {
1948unsigned getX86SubSuperRegister(unsigned Reg, MVT::ValueType VT, bool High) {
1949  switch (VT) {
1950  default: return Reg;
1951  case MVT::i8:
1952    if (High) {
1953      switch (Reg) {
1954      default: return 0;
1955      case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX:
1956        return X86::AH;
1957      case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX:
1958        return X86::DH;
1959      case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX:
1960        return X86::CH;
1961      case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX:
1962        return X86::BH;
1963      }
1964    } else {
1965      switch (Reg) {
1966      default: return 0;
1967      case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX:
1968        return X86::AL;
1969      case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX:
1970        return X86::DL;
1971      case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX:
1972        return X86::CL;
1973      case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX:
1974        return X86::BL;
1975      case X86::SIL: case X86::SI: case X86::ESI: case X86::RSI:
1976        return X86::SIL;
1977      case X86::DIL: case X86::DI: case X86::EDI: case X86::RDI:
1978        return X86::DIL;
1979      case X86::BPL: case X86::BP: case X86::EBP: case X86::RBP:
1980        return X86::BPL;
1981      case X86::SPL: case X86::SP: case X86::ESP: case X86::RSP:
1982        return X86::SPL;
1983      case X86::R8B: case X86::R8W: case X86::R8D: case X86::R8:
1984        return X86::R8B;
1985      case X86::R9B: case X86::R9W: case X86::R9D: case X86::R9:
1986        return X86::R9B;
1987      case X86::R10B: case X86::R10W: case X86::R10D: case X86::R10:
1988        return X86::R10B;
1989      case X86::R11B: case X86::R11W: case X86::R11D: case X86::R11:
1990        return X86::R11B;
1991      case X86::R12B: case X86::R12W: case X86::R12D: case X86::R12:
1992        return X86::R12B;
1993      case X86::R13B: case X86::R13W: case X86::R13D: case X86::R13:
1994        return X86::R13B;
1995      case X86::R14B: case X86::R14W: case X86::R14D: case X86::R14:
1996        return X86::R14B;
1997      case X86::R15B: case X86::R15W: case X86::R15D: case X86::R15:
1998        return X86::R15B;
1999      }
2000    }
2001  case MVT::i16:
2002    switch (Reg) {
2003    default: return Reg;
2004    case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX:
2005      return X86::AX;
2006    case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX:
2007      return X86::DX;
2008    case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX:
2009      return X86::CX;
2010    case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX:
2011      return X86::BX;
2012    case X86::SIL: case X86::SI: case X86::ESI: case X86::RSI:
2013      return X86::SI;
2014    case X86::DIL: case X86::DI: case X86::EDI: case X86::RDI:
2015      return X86::DI;
2016    case X86::BPL: case X86::BP: case X86::EBP: case X86::RBP:
2017      return X86::BP;
2018    case X86::SPL: case X86::SP: case X86::ESP: case X86::RSP:
2019      return X86::SP;
2020    case X86::R8B: case X86::R8W: case X86::R8D: case X86::R8:
2021      return X86::R8W;
2022    case X86::R9B: case X86::R9W: case X86::R9D: case X86::R9:
2023      return X86::R9W;
2024    case X86::R10B: case X86::R10W: case X86::R10D: case X86::R10:
2025      return X86::R10W;
2026    case X86::R11B: case X86::R11W: case X86::R11D: case X86::R11:
2027      return X86::R11W;
2028    case X86::R12B: case X86::R12W: case X86::R12D: case X86::R12:
2029      return X86::R12W;
2030    case X86::R13B: case X86::R13W: case X86::R13D: case X86::R13:
2031      return X86::R13W;
2032    case X86::R14B: case X86::R14W: case X86::R14D: case X86::R14:
2033      return X86::R14W;
2034    case X86::R15B: case X86::R15W: case X86::R15D: case X86::R15:
2035      return X86::R15W;
2036    }
2037  case MVT::i32:
2038    switch (Reg) {
2039    default: return Reg;
2040    case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX:
2041      return X86::EAX;
2042    case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX:
2043      return X86::EDX;
2044    case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX:
2045      return X86::ECX;
2046    case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX:
2047      return X86::EBX;
2048    case X86::SIL: case X86::SI: case X86::ESI: case X86::RSI:
2049      return X86::ESI;
2050    case X86::DIL: case X86::DI: case X86::EDI: case X86::RDI:
2051      return X86::EDI;
2052    case X86::BPL: case X86::BP: case X86::EBP: case X86::RBP:
2053      return X86::EBP;
2054    case X86::SPL: case X86::SP: case X86::ESP: case X86::RSP:
2055      return X86::ESP;
2056    case X86::R8B: case X86::R8W: case X86::R8D: case X86::R8:
2057      return X86::R8D;
2058    case X86::R9B: case X86::R9W: case X86::R9D: case X86::R9:
2059      return X86::R9D;
2060    case X86::R10B: case X86::R10W: case X86::R10D: case X86::R10:
2061      return X86::R10D;
2062    case X86::R11B: case X86::R11W: case X86::R11D: case X86::R11:
2063      return X86::R11D;
2064    case X86::R12B: case X86::R12W: case X86::R12D: case X86::R12:
2065      return X86::R12D;
2066    case X86::R13B: case X86::R13W: case X86::R13D: case X86::R13:
2067      return X86::R13D;
2068    case X86::R14B: case X86::R14W: case X86::R14D: case X86::R14:
2069      return X86::R14D;
2070    case X86::R15B: case X86::R15W: case X86::R15D: case X86::R15:
2071      return X86::R15D;
2072    }
2073  case MVT::i64:
2074    switch (Reg) {
2075    default: return Reg;
2076    case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX:
2077      return X86::RAX;
2078    case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX:
2079      return X86::RDX;
2080    case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX:
2081      return X86::RCX;
2082    case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX:
2083      return X86::RBX;
2084    case X86::SIL: case X86::SI: case X86::ESI: case X86::RSI:
2085      return X86::RSI;
2086    case X86::DIL: case X86::DI: case X86::EDI: case X86::RDI:
2087      return X86::RDI;
2088    case X86::BPL: case X86::BP: case X86::EBP: case X86::RBP:
2089      return X86::RBP;
2090    case X86::SPL: case X86::SP: case X86::ESP: case X86::RSP:
2091      return X86::RSP;
2092    case X86::R8B: case X86::R8W: case X86::R8D: case X86::R8:
2093      return X86::R8;
2094    case X86::R9B: case X86::R9W: case X86::R9D: case X86::R9:
2095      return X86::R9;
2096    case X86::R10B: case X86::R10W: case X86::R10D: case X86::R10:
2097      return X86::R10;
2098    case X86::R11B: case X86::R11W: case X86::R11D: case X86::R11:
2099      return X86::R11;
2100    case X86::R12B: case X86::R12W: case X86::R12D: case X86::R12:
2101      return X86::R12;
2102    case X86::R13B: case X86::R13W: case X86::R13D: case X86::R13:
2103      return X86::R13;
2104    case X86::R14B: case X86::R14W: case X86::R14D: case X86::R14:
2105      return X86::R14;
2106    case X86::R15B: case X86::R15W: case X86::R15D: case X86::R15:
2107      return X86::R15;
2108    }
2109  }
2110
2111  return Reg;
2112}
2113}
2114
2115#include "X86GenRegisterInfo.inc"
2116
2117