TMP: LEA64_32r fixing
[oota-llvm.git] / lib / Target / X86 / X86InstrInfo.cpp
1 //===-- X86InstrInfo.cpp - X86 Instruction Information --------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the X86 implementation of the TargetInstrInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "X86InstrInfo.h"
15 #include "X86.h"
16 #include "X86InstrBuilder.h"
17 #include "X86MachineFunctionInfo.h"
18 #include "X86Subtarget.h"
19 #include "X86TargetMachine.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/CodeGen/LiveVariables.h"
22 #include "llvm/CodeGen/MachineConstantPool.h"
23 #include "llvm/CodeGen/MachineDominators.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineInstrBuilder.h"
26 #include "llvm/CodeGen/MachineRegisterInfo.h"
27 #include "llvm/IR/DerivedTypes.h"
28 #include "llvm/IR/LLVMContext.h"
29 #include "llvm/MC/MCAsmInfo.h"
30 #include "llvm/MC/MCInst.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/Target/TargetOptions.h"
36 #include <limits>
37
38 #define GET_INSTRINFO_CTOR
39 #include "X86GenInstrInfo.inc"
40
41 using namespace llvm;
42
43 static cl::opt<bool>
44 NoFusing("disable-spill-fusing",
45          cl::desc("Disable fusing of spill code into instructions"));
46 static cl::opt<bool>
47 PrintFailedFusing("print-failed-fuse-candidates",
48                   cl::desc("Print instructions that the allocator wants to"
49                            " fuse, but the X86 backend currently can't"),
50                   cl::Hidden);
51 static cl::opt<bool>
52 ReMatPICStubLoad("remat-pic-stub-load",
53                  cl::desc("Re-materialize load from stub in PIC mode"),
54                  cl::init(false), cl::Hidden);
55
56 enum {
57   // Select which memory operand is being unfolded.
58   // (stored in bits 0 - 3)
59   TB_INDEX_0    = 0,
60   TB_INDEX_1    = 1,
61   TB_INDEX_2    = 2,
62   TB_INDEX_3    = 3,
63   TB_INDEX_MASK = 0xf,
64
65   // Do not insert the reverse map (MemOp -> RegOp) into the table.
66   // This may be needed because there is a many -> one mapping.
67   TB_NO_REVERSE   = 1 << 4,
68
69   // Do not insert the forward map (RegOp -> MemOp) into the table.
70   // This is needed for Native Client, which prohibits branch
71   // instructions from using a memory operand.
72   TB_NO_FORWARD   = 1 << 5,
73
74   TB_FOLDED_LOAD  = 1 << 6,
75   TB_FOLDED_STORE = 1 << 7,
76
77   // Minimum alignment required for load/store.
78   // Used for RegOp->MemOp conversion.
79   // (stored in bits 8 - 15)
80   TB_ALIGN_SHIFT = 8,
81   TB_ALIGN_NONE  =    0 << TB_ALIGN_SHIFT,
82   TB_ALIGN_16    =   16 << TB_ALIGN_SHIFT,
83   TB_ALIGN_32    =   32 << TB_ALIGN_SHIFT,
84   TB_ALIGN_MASK  = 0xff << TB_ALIGN_SHIFT
85 };
86
87 struct X86OpTblEntry {
88   uint16_t RegOp;
89   uint16_t MemOp;
90   uint16_t Flags;
91 };
92
93 X86InstrInfo::X86InstrInfo(X86TargetMachine &tm)
94   : X86GenInstrInfo((tm.getSubtarget<X86Subtarget>().is64Bit()
95                      ? X86::ADJCALLSTACKDOWN64
96                      : X86::ADJCALLSTACKDOWN32),
97                     (tm.getSubtarget<X86Subtarget>().is64Bit()
98                      ? X86::ADJCALLSTACKUP64
99                      : X86::ADJCALLSTACKUP32)),
100     TM(tm), RI(tm, *this) {
101
102   static const X86OpTblEntry OpTbl2Addr[] = {
103     { X86::ADC32ri,     X86::ADC32mi,    0 },
104     { X86::ADC32ri8,    X86::ADC32mi8,   0 },
105     { X86::ADC32rr,     X86::ADC32mr,    0 },
106     { X86::ADC64ri32,   X86::ADC64mi32,  0 },
107     { X86::ADC64ri8,    X86::ADC64mi8,   0 },
108     { X86::ADC64rr,     X86::ADC64mr,    0 },
109     { X86::ADD16ri,     X86::ADD16mi,    0 },
110     { X86::ADD16ri8,    X86::ADD16mi8,   0 },
111     { X86::ADD16ri_DB,  X86::ADD16mi,    TB_NO_REVERSE },
112     { X86::ADD16ri8_DB, X86::ADD16mi8,   TB_NO_REVERSE },
113     { X86::ADD16rr,     X86::ADD16mr,    0 },
114     { X86::ADD16rr_DB,  X86::ADD16mr,    TB_NO_REVERSE },
115     { X86::ADD32ri,     X86::ADD32mi,    0 },
116     { X86::ADD32ri8,    X86::ADD32mi8,   0 },
117     { X86::ADD32ri_DB,  X86::ADD32mi,    TB_NO_REVERSE },
118     { X86::ADD32ri8_DB, X86::ADD32mi8,   TB_NO_REVERSE },
119     { X86::ADD32rr,     X86::ADD32mr,    0 },
120     { X86::ADD32rr_DB,  X86::ADD32mr,    TB_NO_REVERSE },
121     { X86::ADD64ri32,   X86::ADD64mi32,  0 },
122     { X86::ADD64ri8,    X86::ADD64mi8,   0 },
123     { X86::ADD64ri32_DB,X86::ADD64mi32,  TB_NO_REVERSE },
124     { X86::ADD64ri8_DB, X86::ADD64mi8,   TB_NO_REVERSE },
125     { X86::ADD64rr,     X86::ADD64mr,    0 },
126     { X86::ADD64rr_DB,  X86::ADD64mr,    TB_NO_REVERSE },
127     { X86::ADD8ri,      X86::ADD8mi,     0 },
128     { X86::ADD8rr,      X86::ADD8mr,     0 },
129     { X86::AND16ri,     X86::AND16mi,    0 },
130     { X86::AND16ri8,    X86::AND16mi8,   0 },
131     { X86::AND16rr,     X86::AND16mr,    0 },
132     { X86::AND32ri,     X86::AND32mi,    0 },
133     { X86::AND32ri8,    X86::AND32mi8,   0 },
134     { X86::AND32rr,     X86::AND32mr,    0 },
135     { X86::AND64ri32,   X86::AND64mi32,  0 },
136     { X86::AND64ri8,    X86::AND64mi8,   0 },
137     { X86::AND64rr,     X86::AND64mr,    0 },
138     { X86::AND8ri,      X86::AND8mi,     0 },
139     { X86::AND8rr,      X86::AND8mr,     0 },
140     { X86::DEC16r,      X86::DEC16m,     0 },
141     { X86::DEC32r,      X86::DEC32m,     0 },
142     { X86::DEC64_16r,   X86::DEC64_16m,  0 },
143     { X86::DEC64_32r,   X86::DEC64_32m,  0 },
144     { X86::DEC64r,      X86::DEC64m,     0 },
145     { X86::DEC8r,       X86::DEC8m,      0 },
146     { X86::INC16r,      X86::INC16m,     0 },
147     { X86::INC32r,      X86::INC32m,     0 },
148     { X86::INC64_16r,   X86::INC64_16m,  0 },
149     { X86::INC64_32r,   X86::INC64_32m,  0 },
150     { X86::INC64r,      X86::INC64m,     0 },
151     { X86::INC8r,       X86::INC8m,      0 },
152     { X86::NEG16r,      X86::NEG16m,     0 },
153     { X86::NEG32r,      X86::NEG32m,     0 },
154     { X86::NEG64r,      X86::NEG64m,     0 },
155     { X86::NEG8r,       X86::NEG8m,      0 },
156     { X86::NOT16r,      X86::NOT16m,     0 },
157     { X86::NOT32r,      X86::NOT32m,     0 },
158     { X86::NOT64r,      X86::NOT64m,     0 },
159     { X86::NOT8r,       X86::NOT8m,      0 },
160     { X86::OR16ri,      X86::OR16mi,     0 },
161     { X86::OR16ri8,     X86::OR16mi8,    0 },
162     { X86::OR16rr,      X86::OR16mr,     0 },
163     { X86::OR32ri,      X86::OR32mi,     0 },
164     { X86::OR32ri8,     X86::OR32mi8,    0 },
165     { X86::OR32rr,      X86::OR32mr,     0 },
166     { X86::OR64ri32,    X86::OR64mi32,   0 },
167     { X86::OR64ri8,     X86::OR64mi8,    0 },
168     { X86::OR64rr,      X86::OR64mr,     0 },
169     { X86::OR8ri,       X86::OR8mi,      0 },
170     { X86::OR8rr,       X86::OR8mr,      0 },
171     { X86::ROL16r1,     X86::ROL16m1,    0 },
172     { X86::ROL16rCL,    X86::ROL16mCL,   0 },
173     { X86::ROL16ri,     X86::ROL16mi,    0 },
174     { X86::ROL32r1,     X86::ROL32m1,    0 },
175     { X86::ROL32rCL,    X86::ROL32mCL,   0 },
176     { X86::ROL32ri,     X86::ROL32mi,    0 },
177     { X86::ROL64r1,     X86::ROL64m1,    0 },
178     { X86::ROL64rCL,    X86::ROL64mCL,   0 },
179     { X86::ROL64ri,     X86::ROL64mi,    0 },
180     { X86::ROL8r1,      X86::ROL8m1,     0 },
181     { X86::ROL8rCL,     X86::ROL8mCL,    0 },
182     { X86::ROL8ri,      X86::ROL8mi,     0 },
183     { X86::ROR16r1,     X86::ROR16m1,    0 },
184     { X86::ROR16rCL,    X86::ROR16mCL,   0 },
185     { X86::ROR16ri,     X86::ROR16mi,    0 },
186     { X86::ROR32r1,     X86::ROR32m1,    0 },
187     { X86::ROR32rCL,    X86::ROR32mCL,   0 },
188     { X86::ROR32ri,     X86::ROR32mi,    0 },
189     { X86::ROR64r1,     X86::ROR64m1,    0 },
190     { X86::ROR64rCL,    X86::ROR64mCL,   0 },
191     { X86::ROR64ri,     X86::ROR64mi,    0 },
192     { X86::ROR8r1,      X86::ROR8m1,     0 },
193     { X86::ROR8rCL,     X86::ROR8mCL,    0 },
194     { X86::ROR8ri,      X86::ROR8mi,     0 },
195     { X86::SAR16r1,     X86::SAR16m1,    0 },
196     { X86::SAR16rCL,    X86::SAR16mCL,   0 },
197     { X86::SAR16ri,     X86::SAR16mi,    0 },
198     { X86::SAR32r1,     X86::SAR32m1,    0 },
199     { X86::SAR32rCL,    X86::SAR32mCL,   0 },
200     { X86::SAR32ri,     X86::SAR32mi,    0 },
201     { X86::SAR64r1,     X86::SAR64m1,    0 },
202     { X86::SAR64rCL,    X86::SAR64mCL,   0 },
203     { X86::SAR64ri,     X86::SAR64mi,    0 },
204     { X86::SAR8r1,      X86::SAR8m1,     0 },
205     { X86::SAR8rCL,     X86::SAR8mCL,    0 },
206     { X86::SAR8ri,      X86::SAR8mi,     0 },
207     { X86::SBB32ri,     X86::SBB32mi,    0 },
208     { X86::SBB32ri8,    X86::SBB32mi8,   0 },
209     { X86::SBB32rr,     X86::SBB32mr,    0 },
210     { X86::SBB64ri32,   X86::SBB64mi32,  0 },
211     { X86::SBB64ri8,    X86::SBB64mi8,   0 },
212     { X86::SBB64rr,     X86::SBB64mr,    0 },
213     { X86::SHL16rCL,    X86::SHL16mCL,   0 },
214     { X86::SHL16ri,     X86::SHL16mi,    0 },
215     { X86::SHL32rCL,    X86::SHL32mCL,   0 },
216     { X86::SHL32ri,     X86::SHL32mi,    0 },
217     { X86::SHL64rCL,    X86::SHL64mCL,   0 },
218     { X86::SHL64ri,     X86::SHL64mi,    0 },
219     { X86::SHL8rCL,     X86::SHL8mCL,    0 },
220     { X86::SHL8ri,      X86::SHL8mi,     0 },
221     { X86::SHLD16rrCL,  X86::SHLD16mrCL, 0 },
222     { X86::SHLD16rri8,  X86::SHLD16mri8, 0 },
223     { X86::SHLD32rrCL,  X86::SHLD32mrCL, 0 },
224     { X86::SHLD32rri8,  X86::SHLD32mri8, 0 },
225     { X86::SHLD64rrCL,  X86::SHLD64mrCL, 0 },
226     { X86::SHLD64rri8,  X86::SHLD64mri8, 0 },
227     { X86::SHR16r1,     X86::SHR16m1,    0 },
228     { X86::SHR16rCL,    X86::SHR16mCL,   0 },
229     { X86::SHR16ri,     X86::SHR16mi,    0 },
230     { X86::SHR32r1,     X86::SHR32m1,    0 },
231     { X86::SHR32rCL,    X86::SHR32mCL,   0 },
232     { X86::SHR32ri,     X86::SHR32mi,    0 },
233     { X86::SHR64r1,     X86::SHR64m1,    0 },
234     { X86::SHR64rCL,    X86::SHR64mCL,   0 },
235     { X86::SHR64ri,     X86::SHR64mi,    0 },
236     { X86::SHR8r1,      X86::SHR8m1,     0 },
237     { X86::SHR8rCL,     X86::SHR8mCL,    0 },
238     { X86::SHR8ri,      X86::SHR8mi,     0 },
239     { X86::SHRD16rrCL,  X86::SHRD16mrCL, 0 },
240     { X86::SHRD16rri8,  X86::SHRD16mri8, 0 },
241     { X86::SHRD32rrCL,  X86::SHRD32mrCL, 0 },
242     { X86::SHRD32rri8,  X86::SHRD32mri8, 0 },
243     { X86::SHRD64rrCL,  X86::SHRD64mrCL, 0 },
244     { X86::SHRD64rri8,  X86::SHRD64mri8, 0 },
245     { X86::SUB16ri,     X86::SUB16mi,    0 },
246     { X86::SUB16ri8,    X86::SUB16mi8,   0 },
247     { X86::SUB16rr,     X86::SUB16mr,    0 },
248     { X86::SUB32ri,     X86::SUB32mi,    0 },
249     { X86::SUB32ri8,    X86::SUB32mi8,   0 },
250     { X86::SUB32rr,     X86::SUB32mr,    0 },
251     { X86::SUB64ri32,   X86::SUB64mi32,  0 },
252     { X86::SUB64ri8,    X86::SUB64mi8,   0 },
253     { X86::SUB64rr,     X86::SUB64mr,    0 },
254     { X86::SUB8ri,      X86::SUB8mi,     0 },
255     { X86::SUB8rr,      X86::SUB8mr,     0 },
256     { X86::XOR16ri,     X86::XOR16mi,    0 },
257     { X86::XOR16ri8,    X86::XOR16mi8,   0 },
258     { X86::XOR16rr,     X86::XOR16mr,    0 },
259     { X86::XOR32ri,     X86::XOR32mi,    0 },
260     { X86::XOR32ri8,    X86::XOR32mi8,   0 },
261     { X86::XOR32rr,     X86::XOR32mr,    0 },
262     { X86::XOR64ri32,   X86::XOR64mi32,  0 },
263     { X86::XOR64ri8,    X86::XOR64mi8,   0 },
264     { X86::XOR64rr,     X86::XOR64mr,    0 },
265     { X86::XOR8ri,      X86::XOR8mi,     0 },
266     { X86::XOR8rr,      X86::XOR8mr,     0 }
267   };
268
269   for (unsigned i = 0, e = array_lengthof(OpTbl2Addr); i != e; ++i) {
270     unsigned RegOp = OpTbl2Addr[i].RegOp;
271     unsigned MemOp = OpTbl2Addr[i].MemOp;
272     unsigned Flags = OpTbl2Addr[i].Flags;
273     AddTableEntry(RegOp2MemOpTable2Addr, MemOp2RegOpTable,
274                   RegOp, MemOp,
275                   // Index 0, folded load and store, no alignment requirement.
276                   Flags | TB_INDEX_0 | TB_FOLDED_LOAD | TB_FOLDED_STORE);
277   }
278
279   static const X86OpTblEntry OpTbl0[] = {
280     { X86::BT16ri8,     X86::BT16mi8,       TB_FOLDED_LOAD },
281     { X86::BT32ri8,     X86::BT32mi8,       TB_FOLDED_LOAD },
282     { X86::BT64ri8,     X86::BT64mi8,       TB_FOLDED_LOAD },
283     { X86::CALL32r,     X86::CALL32m,       TB_FOLDED_LOAD },
284     { X86::CALL64r,     X86::CALL64m,       TB_FOLDED_LOAD },
285     { X86::CMP16ri,     X86::CMP16mi,       TB_FOLDED_LOAD },
286     { X86::CMP16ri8,    X86::CMP16mi8,      TB_FOLDED_LOAD },
287     { X86::CMP16rr,     X86::CMP16mr,       TB_FOLDED_LOAD },
288     { X86::CMP32ri,     X86::CMP32mi,       TB_FOLDED_LOAD },
289     { X86::CMP32ri8,    X86::CMP32mi8,      TB_FOLDED_LOAD },
290     { X86::CMP32rr,     X86::CMP32mr,       TB_FOLDED_LOAD },
291     { X86::CMP64ri32,   X86::CMP64mi32,     TB_FOLDED_LOAD },
292     { X86::CMP64ri8,    X86::CMP64mi8,      TB_FOLDED_LOAD },
293     { X86::CMP64rr,     X86::CMP64mr,       TB_FOLDED_LOAD },
294     { X86::CMP8ri,      X86::CMP8mi,        TB_FOLDED_LOAD },
295     { X86::CMP8rr,      X86::CMP8mr,        TB_FOLDED_LOAD },
296     { X86::DIV16r,      X86::DIV16m,        TB_FOLDED_LOAD },
297     { X86::DIV32r,      X86::DIV32m,        TB_FOLDED_LOAD },
298     { X86::DIV64r,      X86::DIV64m,        TB_FOLDED_LOAD },
299     { X86::DIV8r,       X86::DIV8m,         TB_FOLDED_LOAD },
300     { X86::EXTRACTPSrr, X86::EXTRACTPSmr,   TB_FOLDED_STORE },
301     { X86::FsMOVAPDrr,  X86::MOVSDmr,       TB_FOLDED_STORE | TB_NO_REVERSE },
302     { X86::FsMOVAPSrr,  X86::MOVSSmr,       TB_FOLDED_STORE | TB_NO_REVERSE },
303     { X86::IDIV16r,     X86::IDIV16m,       TB_FOLDED_LOAD },
304     { X86::IDIV32r,     X86::IDIV32m,       TB_FOLDED_LOAD },
305     { X86::IDIV64r,     X86::IDIV64m,       TB_FOLDED_LOAD },
306     { X86::IDIV8r,      X86::IDIV8m,        TB_FOLDED_LOAD },
307     { X86::IMUL16r,     X86::IMUL16m,       TB_FOLDED_LOAD },
308     { X86::IMUL32r,     X86::IMUL32m,       TB_FOLDED_LOAD },
309     { X86::IMUL64r,     X86::IMUL64m,       TB_FOLDED_LOAD },
310     { X86::IMUL8r,      X86::IMUL8m,        TB_FOLDED_LOAD },
311     { X86::JMP32r,      X86::JMP32m,        TB_FOLDED_LOAD },
312     { X86::JMP64r,      X86::JMP64m,        TB_FOLDED_LOAD },
313     { X86::MOV16ri,     X86::MOV16mi,       TB_FOLDED_STORE },
314     { X86::MOV16rr,     X86::MOV16mr,       TB_FOLDED_STORE },
315     { X86::MOV32ri,     X86::MOV32mi,       TB_FOLDED_STORE },
316     { X86::MOV32rr,     X86::MOV32mr,       TB_FOLDED_STORE },
317     { X86::MOV64ri32,   X86::MOV64mi32,     TB_FOLDED_STORE },
318     { X86::MOV64rr,     X86::MOV64mr,       TB_FOLDED_STORE },
319     { X86::MOV8ri,      X86::MOV8mi,        TB_FOLDED_STORE },
320     { X86::MOV8rr,      X86::MOV8mr,        TB_FOLDED_STORE },
321     { X86::MOV8rr_NOREX, X86::MOV8mr_NOREX, TB_FOLDED_STORE },
322     { X86::MOVAPDrr,    X86::MOVAPDmr,      TB_FOLDED_STORE | TB_ALIGN_16 },
323     { X86::MOVAPSrr,    X86::MOVAPSmr,      TB_FOLDED_STORE | TB_ALIGN_16 },
324     { X86::MOVDQArr,    X86::MOVDQAmr,      TB_FOLDED_STORE | TB_ALIGN_16 },
325     { X86::MOVPDI2DIrr, X86::MOVPDI2DImr,   TB_FOLDED_STORE },
326     { X86::MOVPQIto64rr,X86::MOVPQI2QImr,   TB_FOLDED_STORE },
327     { X86::MOVSDto64rr, X86::MOVSDto64mr,   TB_FOLDED_STORE },
328     { X86::MOVSS2DIrr,  X86::MOVSS2DImr,    TB_FOLDED_STORE },
329     { X86::MOVUPDrr,    X86::MOVUPDmr,      TB_FOLDED_STORE },
330     { X86::MOVUPSrr,    X86::MOVUPSmr,      TB_FOLDED_STORE },
331     { X86::MUL16r,      X86::MUL16m,        TB_FOLDED_LOAD },
332     { X86::MUL32r,      X86::MUL32m,        TB_FOLDED_LOAD },
333     { X86::MUL64r,      X86::MUL64m,        TB_FOLDED_LOAD },
334     { X86::MUL8r,       X86::MUL8m,         TB_FOLDED_LOAD },
335     { X86::SETAEr,      X86::SETAEm,        TB_FOLDED_STORE },
336     { X86::SETAr,       X86::SETAm,         TB_FOLDED_STORE },
337     { X86::SETBEr,      X86::SETBEm,        TB_FOLDED_STORE },
338     { X86::SETBr,       X86::SETBm,         TB_FOLDED_STORE },
339     { X86::SETEr,       X86::SETEm,         TB_FOLDED_STORE },
340     { X86::SETGEr,      X86::SETGEm,        TB_FOLDED_STORE },
341     { X86::SETGr,       X86::SETGm,         TB_FOLDED_STORE },
342     { X86::SETLEr,      X86::SETLEm,        TB_FOLDED_STORE },
343     { X86::SETLr,       X86::SETLm,         TB_FOLDED_STORE },
344     { X86::SETNEr,      X86::SETNEm,        TB_FOLDED_STORE },
345     { X86::SETNOr,      X86::SETNOm,        TB_FOLDED_STORE },
346     { X86::SETNPr,      X86::SETNPm,        TB_FOLDED_STORE },
347     { X86::SETNSr,      X86::SETNSm,        TB_FOLDED_STORE },
348     { X86::SETOr,       X86::SETOm,         TB_FOLDED_STORE },
349     { X86::SETPr,       X86::SETPm,         TB_FOLDED_STORE },
350     { X86::SETSr,       X86::SETSm,         TB_FOLDED_STORE },
351     { X86::TAILJMPr,    X86::TAILJMPm,      TB_FOLDED_LOAD },
352     { X86::TAILJMPr64,  X86::TAILJMPm64,    TB_FOLDED_LOAD },
353     { X86::TEST16ri,    X86::TEST16mi,      TB_FOLDED_LOAD },
354     { X86::TEST32ri,    X86::TEST32mi,      TB_FOLDED_LOAD },
355     { X86::TEST64ri32,  X86::TEST64mi32,    TB_FOLDED_LOAD },
356     { X86::TEST8ri,     X86::TEST8mi,       TB_FOLDED_LOAD },
357     // AVX 128-bit versions of foldable instructions
358     { X86::VEXTRACTPSrr,X86::VEXTRACTPSmr,  TB_FOLDED_STORE  },
359     { X86::FsVMOVAPDrr, X86::VMOVSDmr,      TB_FOLDED_STORE | TB_NO_REVERSE },
360     { X86::FsVMOVAPSrr, X86::VMOVSSmr,      TB_FOLDED_STORE | TB_NO_REVERSE },
361     { X86::VEXTRACTF128rr, X86::VEXTRACTF128mr, TB_FOLDED_STORE | TB_ALIGN_16 },
362     { X86::VMOVAPDrr,   X86::VMOVAPDmr,     TB_FOLDED_STORE | TB_ALIGN_16 },
363     { X86::VMOVAPSrr,   X86::VMOVAPSmr,     TB_FOLDED_STORE | TB_ALIGN_16 },
364     { X86::VMOVDQArr,   X86::VMOVDQAmr,     TB_FOLDED_STORE | TB_ALIGN_16 },
365     { X86::VMOVPDI2DIrr,X86::VMOVPDI2DImr,  TB_FOLDED_STORE },
366     { X86::VMOVPQIto64rr, X86::VMOVPQI2QImr,TB_FOLDED_STORE },
367     { X86::VMOVSDto64rr,X86::VMOVSDto64mr,  TB_FOLDED_STORE },
368     { X86::VMOVSS2DIrr, X86::VMOVSS2DImr,   TB_FOLDED_STORE },
369     { X86::VMOVUPDrr,   X86::VMOVUPDmr,     TB_FOLDED_STORE },
370     { X86::VMOVUPSrr,   X86::VMOVUPSmr,     TB_FOLDED_STORE },
371     // AVX 256-bit foldable instructions
372     { X86::VEXTRACTI128rr, X86::VEXTRACTI128mr, TB_FOLDED_STORE | TB_ALIGN_16 },
373     { X86::VMOVAPDYrr,  X86::VMOVAPDYmr,    TB_FOLDED_STORE | TB_ALIGN_32 },
374     { X86::VMOVAPSYrr,  X86::VMOVAPSYmr,    TB_FOLDED_STORE | TB_ALIGN_32 },
375     { X86::VMOVDQAYrr,  X86::VMOVDQAYmr,    TB_FOLDED_STORE | TB_ALIGN_32 },
376     { X86::VMOVUPDYrr,  X86::VMOVUPDYmr,    TB_FOLDED_STORE },
377     { X86::VMOVUPSYrr,  X86::VMOVUPSYmr,    TB_FOLDED_STORE }
378   };
379
380   for (unsigned i = 0, e = array_lengthof(OpTbl0); i != e; ++i) {
381     unsigned RegOp      = OpTbl0[i].RegOp;
382     unsigned MemOp      = OpTbl0[i].MemOp;
383     unsigned Flags      = OpTbl0[i].Flags;
384     AddTableEntry(RegOp2MemOpTable0, MemOp2RegOpTable,
385                   RegOp, MemOp, TB_INDEX_0 | Flags);
386   }
387
388   static const X86OpTblEntry OpTbl1[] = {
389     { X86::CMP16rr,         X86::CMP16rm,             0 },
390     { X86::CMP32rr,         X86::CMP32rm,             0 },
391     { X86::CMP64rr,         X86::CMP64rm,             0 },
392     { X86::CMP8rr,          X86::CMP8rm,              0 },
393     { X86::CVTSD2SSrr,      X86::CVTSD2SSrm,          0 },
394     { X86::CVTSI2SD64rr,    X86::CVTSI2SD64rm,        0 },
395     { X86::CVTSI2SDrr,      X86::CVTSI2SDrm,          0 },
396     { X86::CVTSI2SS64rr,    X86::CVTSI2SS64rm,        0 },
397     { X86::CVTSI2SSrr,      X86::CVTSI2SSrm,          0 },
398     { X86::CVTSS2SDrr,      X86::CVTSS2SDrm,          0 },
399     { X86::CVTTSD2SI64rr,   X86::CVTTSD2SI64rm,       0 },
400     { X86::CVTTSD2SIrr,     X86::CVTTSD2SIrm,         0 },
401     { X86::CVTTSS2SI64rr,   X86::CVTTSS2SI64rm,       0 },
402     { X86::CVTTSS2SIrr,     X86::CVTTSS2SIrm,         0 },
403     { X86::FsMOVAPDrr,      X86::MOVSDrm,             TB_NO_REVERSE },
404     { X86::FsMOVAPSrr,      X86::MOVSSrm,             TB_NO_REVERSE },
405     { X86::IMUL16rri,       X86::IMUL16rmi,           0 },
406     { X86::IMUL16rri8,      X86::IMUL16rmi8,          0 },
407     { X86::IMUL32rri,       X86::IMUL32rmi,           0 },
408     { X86::IMUL32rri8,      X86::IMUL32rmi8,          0 },
409     { X86::IMUL64rri32,     X86::IMUL64rmi32,         0 },
410     { X86::IMUL64rri8,      X86::IMUL64rmi8,          0 },
411     { X86::Int_COMISDrr,    X86::Int_COMISDrm,        0 },
412     { X86::Int_COMISSrr,    X86::Int_COMISSrm,        0 },
413     { X86::CVTSD2SI64rr,    X86::CVTSD2SI64rm,        0 },
414     { X86::CVTSD2SIrr,      X86::CVTSD2SIrm,          0 },
415     { X86::CVTSS2SI64rr,    X86::CVTSS2SI64rm,        0 },
416     { X86::CVTSS2SIrr,      X86::CVTSS2SIrm,          0 },
417     { X86::CVTTPD2DQrr,     X86::CVTTPD2DQrm,         TB_ALIGN_16 },
418     { X86::CVTTPS2DQrr,     X86::CVTTPS2DQrm,         TB_ALIGN_16 },
419     { X86::Int_CVTTSD2SI64rr,X86::Int_CVTTSD2SI64rm,  0 },
420     { X86::Int_CVTTSD2SIrr, X86::Int_CVTTSD2SIrm,     0 },
421     { X86::Int_CVTTSS2SI64rr,X86::Int_CVTTSS2SI64rm,  0 },
422     { X86::Int_CVTTSS2SIrr, X86::Int_CVTTSS2SIrm,     0 },
423     { X86::Int_UCOMISDrr,   X86::Int_UCOMISDrm,       0 },
424     { X86::Int_UCOMISSrr,   X86::Int_UCOMISSrm,       0 },
425     { X86::MOV16rr,         X86::MOV16rm,             0 },
426     { X86::MOV32rr,         X86::MOV32rm,             0 },
427     { X86::MOV64rr,         X86::MOV64rm,             0 },
428     { X86::MOV64toPQIrr,    X86::MOVQI2PQIrm,         0 },
429     { X86::MOV64toSDrr,     X86::MOV64toSDrm,         0 },
430     { X86::MOV8rr,          X86::MOV8rm,              0 },
431     { X86::MOVAPDrr,        X86::MOVAPDrm,            TB_ALIGN_16 },
432     { X86::MOVAPSrr,        X86::MOVAPSrm,            TB_ALIGN_16 },
433     { X86::MOVDDUPrr,       X86::MOVDDUPrm,           0 },
434     { X86::MOVDI2PDIrr,     X86::MOVDI2PDIrm,         0 },
435     { X86::MOVDI2SSrr,      X86::MOVDI2SSrm,          0 },
436     { X86::MOVDQArr,        X86::MOVDQArm,            TB_ALIGN_16 },
437     { X86::MOVSHDUPrr,      X86::MOVSHDUPrm,          TB_ALIGN_16 },
438     { X86::MOVSLDUPrr,      X86::MOVSLDUPrm,          TB_ALIGN_16 },
439     { X86::MOVSX16rr8,      X86::MOVSX16rm8,          0 },
440     { X86::MOVSX32rr16,     X86::MOVSX32rm16,         0 },
441     { X86::MOVSX32rr8,      X86::MOVSX32rm8,          0 },
442     { X86::MOVSX64rr16,     X86::MOVSX64rm16,         0 },
443     { X86::MOVSX64rr32,     X86::MOVSX64rm32,         0 },
444     { X86::MOVSX64rr8,      X86::MOVSX64rm8,          0 },
445     { X86::MOVUPDrr,        X86::MOVUPDrm,            TB_ALIGN_16 },
446     { X86::MOVUPSrr,        X86::MOVUPSrm,            0 },
447     { X86::MOVZDI2PDIrr,    X86::MOVZDI2PDIrm,        0 },
448     { X86::MOVZQI2PQIrr,    X86::MOVZQI2PQIrm,        0 },
449     { X86::MOVZPQILo2PQIrr, X86::MOVZPQILo2PQIrm,     TB_ALIGN_16 },
450     { X86::MOVZX16rr8,      X86::MOVZX16rm8,          0 },
451     { X86::MOVZX32rr16,     X86::MOVZX32rm16,         0 },
452     { X86::MOVZX32_NOREXrr8, X86::MOVZX32_NOREXrm8,   0 },
453     { X86::MOVZX32rr8,      X86::MOVZX32rm8,          0 },
454     { X86::PABSBrr128,      X86::PABSBrm128,          TB_ALIGN_16 },
455     { X86::PABSDrr128,      X86::PABSDrm128,          TB_ALIGN_16 },
456     { X86::PABSWrr128,      X86::PABSWrm128,          TB_ALIGN_16 },
457     { X86::PSHUFDri,        X86::PSHUFDmi,            TB_ALIGN_16 },
458     { X86::PSHUFHWri,       X86::PSHUFHWmi,           TB_ALIGN_16 },
459     { X86::PSHUFLWri,       X86::PSHUFLWmi,           TB_ALIGN_16 },
460     { X86::RCPPSr,          X86::RCPPSm,              TB_ALIGN_16 },
461     { X86::RCPPSr_Int,      X86::RCPPSm_Int,          TB_ALIGN_16 },
462     { X86::RSQRTPSr,        X86::RSQRTPSm,            TB_ALIGN_16 },
463     { X86::RSQRTPSr_Int,    X86::RSQRTPSm_Int,        TB_ALIGN_16 },
464     { X86::RSQRTSSr,        X86::RSQRTSSm,            0 },
465     { X86::RSQRTSSr_Int,    X86::RSQRTSSm_Int,        0 },
466     { X86::SQRTPDr,         X86::SQRTPDm,             TB_ALIGN_16 },
467     { X86::SQRTPSr,         X86::SQRTPSm,             TB_ALIGN_16 },
468     { X86::SQRTSDr,         X86::SQRTSDm,             0 },
469     { X86::SQRTSDr_Int,     X86::SQRTSDm_Int,         0 },
470     { X86::SQRTSSr,         X86::SQRTSSm,             0 },
471     { X86::SQRTSSr_Int,     X86::SQRTSSm_Int,         0 },
472     { X86::TEST16rr,        X86::TEST16rm,            0 },
473     { X86::TEST32rr,        X86::TEST32rm,            0 },
474     { X86::TEST64rr,        X86::TEST64rm,            0 },
475     { X86::TEST8rr,         X86::TEST8rm,             0 },
476     // FIXME: TEST*rr EAX,EAX ---> CMP [mem], 0
477     { X86::UCOMISDrr,       X86::UCOMISDrm,           0 },
478     { X86::UCOMISSrr,       X86::UCOMISSrm,           0 },
479     // AVX 128-bit versions of foldable instructions
480     { X86::Int_VCOMISDrr,   X86::Int_VCOMISDrm,       0 },
481     { X86::Int_VCOMISSrr,   X86::Int_VCOMISSrm,       0 },
482     { X86::Int_VUCOMISDrr,  X86::Int_VUCOMISDrm,      0 },
483     { X86::Int_VUCOMISSrr,  X86::Int_VUCOMISSrm,      0 },
484     { X86::VCVTTSD2SI64rr,  X86::VCVTTSD2SI64rm,      0 },
485     { X86::Int_VCVTTSD2SI64rr,X86::Int_VCVTTSD2SI64rm,0 },
486     { X86::VCVTTSD2SIrr,    X86::VCVTTSD2SIrm,        0 },
487     { X86::Int_VCVTTSD2SIrr,X86::Int_VCVTTSD2SIrm,    0 },
488     { X86::VCVTTSS2SI64rr,  X86::VCVTTSS2SI64rm,      0 },
489     { X86::Int_VCVTTSS2SI64rr,X86::Int_VCVTTSS2SI64rm,0 },
490     { X86::VCVTTSS2SIrr,    X86::VCVTTSS2SIrm,        0 },
491     { X86::Int_VCVTTSS2SIrr,X86::Int_VCVTTSS2SIrm,    0 },
492     { X86::VCVTSD2SI64rr,   X86::VCVTSD2SI64rm,       0 },
493     { X86::VCVTSD2SIrr,     X86::VCVTSD2SIrm,         0 },
494     { X86::VCVTSS2SI64rr,   X86::VCVTSS2SI64rm,       0 },
495     { X86::VCVTSS2SIrr,     X86::VCVTSS2SIrm,         0 },
496     { X86::FsVMOVAPDrr,     X86::VMOVSDrm,            TB_NO_REVERSE },
497     { X86::FsVMOVAPSrr,     X86::VMOVSSrm,            TB_NO_REVERSE },
498     { X86::VMOV64toPQIrr,   X86::VMOVQI2PQIrm,        0 },
499     { X86::VMOV64toSDrr,    X86::VMOV64toSDrm,        0 },
500     { X86::VMOVAPDrr,       X86::VMOVAPDrm,           TB_ALIGN_16 },
501     { X86::VMOVAPSrr,       X86::VMOVAPSrm,           TB_ALIGN_16 },
502     { X86::VMOVDDUPrr,      X86::VMOVDDUPrm,          0 },
503     { X86::VMOVDI2PDIrr,    X86::VMOVDI2PDIrm,        0 },
504     { X86::VMOVDI2SSrr,     X86::VMOVDI2SSrm,         0 },
505     { X86::VMOVDQArr,       X86::VMOVDQArm,           TB_ALIGN_16 },
506     { X86::VMOVSLDUPrr,     X86::VMOVSLDUPrm,         TB_ALIGN_16 },
507     { X86::VMOVSHDUPrr,     X86::VMOVSHDUPrm,         TB_ALIGN_16 },
508     { X86::VMOVUPDrr,       X86::VMOVUPDrm,           0 },
509     { X86::VMOVUPSrr,       X86::VMOVUPSrm,           0 },
510     { X86::VMOVZDI2PDIrr,   X86::VMOVZDI2PDIrm,       0 },
511     { X86::VMOVZQI2PQIrr,   X86::VMOVZQI2PQIrm,       0 },
512     { X86::VMOVZPQILo2PQIrr,X86::VMOVZPQILo2PQIrm,    TB_ALIGN_16 },
513     { X86::VPABSBrr128,     X86::VPABSBrm128,         0 },
514     { X86::VPABSDrr128,     X86::VPABSDrm128,         0 },
515     { X86::VPABSWrr128,     X86::VPABSWrm128,         0 },
516     { X86::VPERMILPDri,     X86::VPERMILPDmi,         0 },
517     { X86::VPERMILPSri,     X86::VPERMILPSmi,         0 },
518     { X86::VPSHUFDri,       X86::VPSHUFDmi,           0 },
519     { X86::VPSHUFHWri,      X86::VPSHUFHWmi,          0 },
520     { X86::VPSHUFLWri,      X86::VPSHUFLWmi,          0 },
521     { X86::VRCPPSr,         X86::VRCPPSm,             0 },
522     { X86::VRCPPSr_Int,     X86::VRCPPSm_Int,         0 },
523     { X86::VRSQRTPSr,       X86::VRSQRTPSm,           0 },
524     { X86::VRSQRTPSr_Int,   X86::VRSQRTPSm_Int,       0 },
525     { X86::VSQRTPDr,        X86::VSQRTPDm,            0 },
526     { X86::VSQRTPSr,        X86::VSQRTPSm,            0 },
527     { X86::VUCOMISDrr,      X86::VUCOMISDrm,          0 },
528     { X86::VUCOMISSrr,      X86::VUCOMISSrm,          0 },
529     { X86::VBROADCASTSSrr,  X86::VBROADCASTSSrm,      TB_NO_REVERSE },
530
531     // AVX 256-bit foldable instructions
532     { X86::VMOVAPDYrr,      X86::VMOVAPDYrm,          TB_ALIGN_32 },
533     { X86::VMOVAPSYrr,      X86::VMOVAPSYrm,          TB_ALIGN_32 },
534     { X86::VMOVDQAYrr,      X86::VMOVDQAYrm,          TB_ALIGN_32 },
535     { X86::VMOVUPDYrr,      X86::VMOVUPDYrm,          0 },
536     { X86::VMOVUPSYrr,      X86::VMOVUPSYrm,          0 },
537     { X86::VPERMILPDYri,    X86::VPERMILPDYmi,        0 },
538     { X86::VPERMILPSYri,    X86::VPERMILPSYmi,        0 },
539
540     // AVX2 foldable instructions
541     { X86::VPABSBrr256,     X86::VPABSBrm256,         0 },
542     { X86::VPABSDrr256,     X86::VPABSDrm256,         0 },
543     { X86::VPABSWrr256,     X86::VPABSWrm256,         0 },
544     { X86::VPSHUFDYri,      X86::VPSHUFDYmi,          0 },
545     { X86::VPSHUFHWYri,     X86::VPSHUFHWYmi,         0 },
546     { X86::VPSHUFLWYri,     X86::VPSHUFLWYmi,         0 },
547     { X86::VRCPPSYr,        X86::VRCPPSYm,            0 },
548     { X86::VRCPPSYr_Int,    X86::VRCPPSYm_Int,        0 },
549     { X86::VRSQRTPSYr,      X86::VRSQRTPSYm,          0 },
550     { X86::VSQRTPDYr,       X86::VSQRTPDYm,           0 },
551     { X86::VSQRTPSYr,       X86::VSQRTPSYm,           0 },
552     { X86::VBROADCASTSSYrr, X86::VBROADCASTSSYrm,     TB_NO_REVERSE },
553     { X86::VBROADCASTSDYrr, X86::VBROADCASTSDYrm,     TB_NO_REVERSE },
554
555     // BMI/BMI2/LZCNT/POPCNT foldable instructions
556     { X86::BEXTR32rr,       X86::BEXTR32rm,           0 },
557     { X86::BEXTR64rr,       X86::BEXTR64rm,           0 },
558     { X86::BLSI32rr,        X86::BLSI32rm,            0 },
559     { X86::BLSI64rr,        X86::BLSI64rm,            0 },
560     { X86::BLSMSK32rr,      X86::BLSMSK32rm,          0 },
561     { X86::BLSMSK64rr,      X86::BLSMSK64rm,          0 },
562     { X86::BLSR32rr,        X86::BLSR32rm,            0 },
563     { X86::BLSR64rr,        X86::BLSR64rm,            0 },
564     { X86::BZHI32rr,        X86::BZHI32rm,            0 },
565     { X86::BZHI64rr,        X86::BZHI64rm,            0 },
566     { X86::LZCNT16rr,       X86::LZCNT16rm,           0 },
567     { X86::LZCNT32rr,       X86::LZCNT32rm,           0 },
568     { X86::LZCNT64rr,       X86::LZCNT64rm,           0 },
569     { X86::POPCNT16rr,      X86::POPCNT16rm,          0 },
570     { X86::POPCNT32rr,      X86::POPCNT32rm,          0 },
571     { X86::POPCNT64rr,      X86::POPCNT64rm,          0 },
572     { X86::RORX32ri,        X86::RORX32mi,            0 },
573     { X86::RORX64ri,        X86::RORX64mi,            0 },
574     { X86::SARX32rr,        X86::SARX32rm,            0 },
575     { X86::SARX64rr,        X86::SARX64rm,            0 },
576     { X86::SHRX32rr,        X86::SHRX32rm,            0 },
577     { X86::SHRX64rr,        X86::SHRX64rm,            0 },
578     { X86::SHLX32rr,        X86::SHLX32rm,            0 },
579     { X86::SHLX64rr,        X86::SHLX64rm,            0 },
580     { X86::TZCNT16rr,       X86::TZCNT16rm,           0 },
581     { X86::TZCNT32rr,       X86::TZCNT32rm,           0 },
582     { X86::TZCNT64rr,       X86::TZCNT64rm,           0 },
583   };
584
585   for (unsigned i = 0, e = array_lengthof(OpTbl1); i != e; ++i) {
586     unsigned RegOp = OpTbl1[i].RegOp;
587     unsigned MemOp = OpTbl1[i].MemOp;
588     unsigned Flags = OpTbl1[i].Flags;
589     AddTableEntry(RegOp2MemOpTable1, MemOp2RegOpTable,
590                   RegOp, MemOp,
591                   // Index 1, folded load
592                   Flags | TB_INDEX_1 | TB_FOLDED_LOAD);
593   }
594
595   static const X86OpTblEntry OpTbl2[] = {
596     { X86::ADC32rr,         X86::ADC32rm,       0 },
597     { X86::ADC64rr,         X86::ADC64rm,       0 },
598     { X86::ADD16rr,         X86::ADD16rm,       0 },
599     { X86::ADD16rr_DB,      X86::ADD16rm,       TB_NO_REVERSE },
600     { X86::ADD32rr,         X86::ADD32rm,       0 },
601     { X86::ADD32rr_DB,      X86::ADD32rm,       TB_NO_REVERSE },
602     { X86::ADD64rr,         X86::ADD64rm,       0 },
603     { X86::ADD64rr_DB,      X86::ADD64rm,       TB_NO_REVERSE },
604     { X86::ADD8rr,          X86::ADD8rm,        0 },
605     { X86::ADDPDrr,         X86::ADDPDrm,       TB_ALIGN_16 },
606     { X86::ADDPSrr,         X86::ADDPSrm,       TB_ALIGN_16 },
607     { X86::ADDSDrr,         X86::ADDSDrm,       0 },
608     { X86::ADDSSrr,         X86::ADDSSrm,       0 },
609     { X86::ADDSUBPDrr,      X86::ADDSUBPDrm,    TB_ALIGN_16 },
610     { X86::ADDSUBPSrr,      X86::ADDSUBPSrm,    TB_ALIGN_16 },
611     { X86::AND16rr,         X86::AND16rm,       0 },
612     { X86::AND32rr,         X86::AND32rm,       0 },
613     { X86::AND64rr,         X86::AND64rm,       0 },
614     { X86::AND8rr,          X86::AND8rm,        0 },
615     { X86::ANDNPDrr,        X86::ANDNPDrm,      TB_ALIGN_16 },
616     { X86::ANDNPSrr,        X86::ANDNPSrm,      TB_ALIGN_16 },
617     { X86::ANDPDrr,         X86::ANDPDrm,       TB_ALIGN_16 },
618     { X86::ANDPSrr,         X86::ANDPSrm,       TB_ALIGN_16 },
619     { X86::BLENDPDrri,      X86::BLENDPDrmi,    TB_ALIGN_16 },
620     { X86::BLENDPSrri,      X86::BLENDPSrmi,    TB_ALIGN_16 },
621     { X86::BLENDVPDrr0,     X86::BLENDVPDrm0,   TB_ALIGN_16 },
622     { X86::BLENDVPSrr0,     X86::BLENDVPSrm0,   TB_ALIGN_16 },
623     { X86::CMOVA16rr,       X86::CMOVA16rm,     0 },
624     { X86::CMOVA32rr,       X86::CMOVA32rm,     0 },
625     { X86::CMOVA64rr,       X86::CMOVA64rm,     0 },
626     { X86::CMOVAE16rr,      X86::CMOVAE16rm,    0 },
627     { X86::CMOVAE32rr,      X86::CMOVAE32rm,    0 },
628     { X86::CMOVAE64rr,      X86::CMOVAE64rm,    0 },
629     { X86::CMOVB16rr,       X86::CMOVB16rm,     0 },
630     { X86::CMOVB32rr,       X86::CMOVB32rm,     0 },
631     { X86::CMOVB64rr,       X86::CMOVB64rm,     0 },
632     { X86::CMOVBE16rr,      X86::CMOVBE16rm,    0 },
633     { X86::CMOVBE32rr,      X86::CMOVBE32rm,    0 },
634     { X86::CMOVBE64rr,      X86::CMOVBE64rm,    0 },
635     { X86::CMOVE16rr,       X86::CMOVE16rm,     0 },
636     { X86::CMOVE32rr,       X86::CMOVE32rm,     0 },
637     { X86::CMOVE64rr,       X86::CMOVE64rm,     0 },
638     { X86::CMOVG16rr,       X86::CMOVG16rm,     0 },
639     { X86::CMOVG32rr,       X86::CMOVG32rm,     0 },
640     { X86::CMOVG64rr,       X86::CMOVG64rm,     0 },
641     { X86::CMOVGE16rr,      X86::CMOVGE16rm,    0 },
642     { X86::CMOVGE32rr,      X86::CMOVGE32rm,    0 },
643     { X86::CMOVGE64rr,      X86::CMOVGE64rm,    0 },
644     { X86::CMOVL16rr,       X86::CMOVL16rm,     0 },
645     { X86::CMOVL32rr,       X86::CMOVL32rm,     0 },
646     { X86::CMOVL64rr,       X86::CMOVL64rm,     0 },
647     { X86::CMOVLE16rr,      X86::CMOVLE16rm,    0 },
648     { X86::CMOVLE32rr,      X86::CMOVLE32rm,    0 },
649     { X86::CMOVLE64rr,      X86::CMOVLE64rm,    0 },
650     { X86::CMOVNE16rr,      X86::CMOVNE16rm,    0 },
651     { X86::CMOVNE32rr,      X86::CMOVNE32rm,    0 },
652     { X86::CMOVNE64rr,      X86::CMOVNE64rm,    0 },
653     { X86::CMOVNO16rr,      X86::CMOVNO16rm,    0 },
654     { X86::CMOVNO32rr,      X86::CMOVNO32rm,    0 },
655     { X86::CMOVNO64rr,      X86::CMOVNO64rm,    0 },
656     { X86::CMOVNP16rr,      X86::CMOVNP16rm,    0 },
657     { X86::CMOVNP32rr,      X86::CMOVNP32rm,    0 },
658     { X86::CMOVNP64rr,      X86::CMOVNP64rm,    0 },
659     { X86::CMOVNS16rr,      X86::CMOVNS16rm,    0 },
660     { X86::CMOVNS32rr,      X86::CMOVNS32rm,    0 },
661     { X86::CMOVNS64rr,      X86::CMOVNS64rm,    0 },
662     { X86::CMOVO16rr,       X86::CMOVO16rm,     0 },
663     { X86::CMOVO32rr,       X86::CMOVO32rm,     0 },
664     { X86::CMOVO64rr,       X86::CMOVO64rm,     0 },
665     { X86::CMOVP16rr,       X86::CMOVP16rm,     0 },
666     { X86::CMOVP32rr,       X86::CMOVP32rm,     0 },
667     { X86::CMOVP64rr,       X86::CMOVP64rm,     0 },
668     { X86::CMOVS16rr,       X86::CMOVS16rm,     0 },
669     { X86::CMOVS32rr,       X86::CMOVS32rm,     0 },
670     { X86::CMOVS64rr,       X86::CMOVS64rm,     0 },
671     { X86::CMPPDrri,        X86::CMPPDrmi,      TB_ALIGN_16 },
672     { X86::CMPPSrri,        X86::CMPPSrmi,      TB_ALIGN_16 },
673     { X86::CMPSDrr,         X86::CMPSDrm,       0 },
674     { X86::CMPSSrr,         X86::CMPSSrm,       0 },
675     { X86::DIVPDrr,         X86::DIVPDrm,       TB_ALIGN_16 },
676     { X86::DIVPSrr,         X86::DIVPSrm,       TB_ALIGN_16 },
677     { X86::DIVSDrr,         X86::DIVSDrm,       0 },
678     { X86::DIVSSrr,         X86::DIVSSrm,       0 },
679     { X86::FsANDNPDrr,      X86::FsANDNPDrm,    TB_ALIGN_16 },
680     { X86::FsANDNPSrr,      X86::FsANDNPSrm,    TB_ALIGN_16 },
681     { X86::FsANDPDrr,       X86::FsANDPDrm,     TB_ALIGN_16 },
682     { X86::FsANDPSrr,       X86::FsANDPSrm,     TB_ALIGN_16 },
683     { X86::FsORPDrr,        X86::FsORPDrm,      TB_ALIGN_16 },
684     { X86::FsORPSrr,        X86::FsORPSrm,      TB_ALIGN_16 },
685     { X86::FsXORPDrr,       X86::FsXORPDrm,     TB_ALIGN_16 },
686     { X86::FsXORPSrr,       X86::FsXORPSrm,     TB_ALIGN_16 },
687     { X86::HADDPDrr,        X86::HADDPDrm,      TB_ALIGN_16 },
688     { X86::HADDPSrr,        X86::HADDPSrm,      TB_ALIGN_16 },
689     { X86::HSUBPDrr,        X86::HSUBPDrm,      TB_ALIGN_16 },
690     { X86::HSUBPSrr,        X86::HSUBPSrm,      TB_ALIGN_16 },
691     { X86::IMUL16rr,        X86::IMUL16rm,      0 },
692     { X86::IMUL32rr,        X86::IMUL32rm,      0 },
693     { X86::IMUL64rr,        X86::IMUL64rm,      0 },
694     { X86::Int_CMPSDrr,     X86::Int_CMPSDrm,   0 },
695     { X86::Int_CMPSSrr,     X86::Int_CMPSSrm,   0 },
696     { X86::Int_CVTSD2SSrr,  X86::Int_CVTSD2SSrm,      0 },
697     { X86::Int_CVTSI2SD64rr,X86::Int_CVTSI2SD64rm,    0 },
698     { X86::Int_CVTSI2SDrr,  X86::Int_CVTSI2SDrm,      0 },
699     { X86::Int_CVTSI2SS64rr,X86::Int_CVTSI2SS64rm,    0 },
700     { X86::Int_CVTSI2SSrr,  X86::Int_CVTSI2SSrm,      0 },
701     { X86::Int_CVTSS2SDrr,  X86::Int_CVTSS2SDrm,      0 },
702     { X86::MAXPDrr,         X86::MAXPDrm,       TB_ALIGN_16 },
703     { X86::MAXPSrr,         X86::MAXPSrm,       TB_ALIGN_16 },
704     { X86::MAXSDrr,         X86::MAXSDrm,       0 },
705     { X86::MAXSSrr,         X86::MAXSSrm,       0 },
706     { X86::MINPDrr,         X86::MINPDrm,       TB_ALIGN_16 },
707     { X86::MINPSrr,         X86::MINPSrm,       TB_ALIGN_16 },
708     { X86::MINSDrr,         X86::MINSDrm,       0 },
709     { X86::MINSSrr,         X86::MINSSrm,       0 },
710     { X86::MPSADBWrri,      X86::MPSADBWrmi,    TB_ALIGN_16 },
711     { X86::MULPDrr,         X86::MULPDrm,       TB_ALIGN_16 },
712     { X86::MULPSrr,         X86::MULPSrm,       TB_ALIGN_16 },
713     { X86::MULSDrr,         X86::MULSDrm,       0 },
714     { X86::MULSSrr,         X86::MULSSrm,       0 },
715     { X86::OR16rr,          X86::OR16rm,        0 },
716     { X86::OR32rr,          X86::OR32rm,        0 },
717     { X86::OR64rr,          X86::OR64rm,        0 },
718     { X86::OR8rr,           X86::OR8rm,         0 },
719     { X86::ORPDrr,          X86::ORPDrm,        TB_ALIGN_16 },
720     { X86::ORPSrr,          X86::ORPSrm,        TB_ALIGN_16 },
721     { X86::PACKSSDWrr,      X86::PACKSSDWrm,    TB_ALIGN_16 },
722     { X86::PACKSSWBrr,      X86::PACKSSWBrm,    TB_ALIGN_16 },
723     { X86::PACKUSDWrr,      X86::PACKUSDWrm,    TB_ALIGN_16 },
724     { X86::PACKUSWBrr,      X86::PACKUSWBrm,    TB_ALIGN_16 },
725     { X86::PADDBrr,         X86::PADDBrm,       TB_ALIGN_16 },
726     { X86::PADDDrr,         X86::PADDDrm,       TB_ALIGN_16 },
727     { X86::PADDQrr,         X86::PADDQrm,       TB_ALIGN_16 },
728     { X86::PADDSBrr,        X86::PADDSBrm,      TB_ALIGN_16 },
729     { X86::PADDSWrr,        X86::PADDSWrm,      TB_ALIGN_16 },
730     { X86::PADDUSBrr,       X86::PADDUSBrm,     TB_ALIGN_16 },
731     { X86::PADDUSWrr,       X86::PADDUSWrm,     TB_ALIGN_16 },
732     { X86::PADDWrr,         X86::PADDWrm,       TB_ALIGN_16 },
733     { X86::PALIGNR128rr,    X86::PALIGNR128rm,  TB_ALIGN_16 },
734     { X86::PANDNrr,         X86::PANDNrm,       TB_ALIGN_16 },
735     { X86::PANDrr,          X86::PANDrm,        TB_ALIGN_16 },
736     { X86::PAVGBrr,         X86::PAVGBrm,       TB_ALIGN_16 },
737     { X86::PAVGWrr,         X86::PAVGWrm,       TB_ALIGN_16 },
738     { X86::PBLENDWrri,      X86::PBLENDWrmi,    TB_ALIGN_16 },
739     { X86::PCMPEQBrr,       X86::PCMPEQBrm,     TB_ALIGN_16 },
740     { X86::PCMPEQDrr,       X86::PCMPEQDrm,     TB_ALIGN_16 },
741     { X86::PCMPEQQrr,       X86::PCMPEQQrm,     TB_ALIGN_16 },
742     { X86::PCMPEQWrr,       X86::PCMPEQWrm,     TB_ALIGN_16 },
743     { X86::PCMPGTBrr,       X86::PCMPGTBrm,     TB_ALIGN_16 },
744     { X86::PCMPGTDrr,       X86::PCMPGTDrm,     TB_ALIGN_16 },
745     { X86::PCMPGTQrr,       X86::PCMPGTQrm,     TB_ALIGN_16 },
746     { X86::PCMPGTWrr,       X86::PCMPGTWrm,     TB_ALIGN_16 },
747     { X86::PHADDDrr,        X86::PHADDDrm,      TB_ALIGN_16 },
748     { X86::PHADDWrr,        X86::PHADDWrm,      TB_ALIGN_16 },
749     { X86::PHADDSWrr128,    X86::PHADDSWrm128,  TB_ALIGN_16 },
750     { X86::PHSUBDrr,        X86::PHSUBDrm,      TB_ALIGN_16 },
751     { X86::PHSUBSWrr128,    X86::PHSUBSWrm128,  TB_ALIGN_16 },
752     { X86::PHSUBWrr,        X86::PHSUBWrm,      TB_ALIGN_16 },
753     { X86::PINSRWrri,       X86::PINSRWrmi,     TB_ALIGN_16 },
754     { X86::PMADDUBSWrr128,  X86::PMADDUBSWrm128, TB_ALIGN_16 },
755     { X86::PMADDWDrr,       X86::PMADDWDrm,     TB_ALIGN_16 },
756     { X86::PMAXSWrr,        X86::PMAXSWrm,      TB_ALIGN_16 },
757     { X86::PMAXUBrr,        X86::PMAXUBrm,      TB_ALIGN_16 },
758     { X86::PMINSWrr,        X86::PMINSWrm,      TB_ALIGN_16 },
759     { X86::PMINUBrr,        X86::PMINUBrm,      TB_ALIGN_16 },
760     { X86::PMINSBrr,        X86::PMINSBrm,      TB_ALIGN_16 },
761     { X86::PMINSDrr,        X86::PMINSDrm,      TB_ALIGN_16 },
762     { X86::PMINUDrr,        X86::PMINUDrm,      TB_ALIGN_16 },
763     { X86::PMINUWrr,        X86::PMINUWrm,      TB_ALIGN_16 },
764     { X86::PMAXSBrr,        X86::PMAXSBrm,      TB_ALIGN_16 },
765     { X86::PMAXSDrr,        X86::PMAXSDrm,      TB_ALIGN_16 },
766     { X86::PMAXUDrr,        X86::PMAXUDrm,      TB_ALIGN_16 },
767     { X86::PMAXUWrr,        X86::PMAXUWrm,      TB_ALIGN_16 },
768     { X86::PMULDQrr,        X86::PMULDQrm,      TB_ALIGN_16 },
769     { X86::PMULHRSWrr128,   X86::PMULHRSWrm128, TB_ALIGN_16 },
770     { X86::PMULHUWrr,       X86::PMULHUWrm,     TB_ALIGN_16 },
771     { X86::PMULHWrr,        X86::PMULHWrm,      TB_ALIGN_16 },
772     { X86::PMULLDrr,        X86::PMULLDrm,      TB_ALIGN_16 },
773     { X86::PMULLWrr,        X86::PMULLWrm,      TB_ALIGN_16 },
774     { X86::PMULUDQrr,       X86::PMULUDQrm,     TB_ALIGN_16 },
775     { X86::PORrr,           X86::PORrm,         TB_ALIGN_16 },
776     { X86::PSADBWrr,        X86::PSADBWrm,      TB_ALIGN_16 },
777     { X86::PSHUFBrr,        X86::PSHUFBrm,      TB_ALIGN_16 },
778     { X86::PSIGNBrr,        X86::PSIGNBrm,      TB_ALIGN_16 },
779     { X86::PSIGNWrr,        X86::PSIGNWrm,      TB_ALIGN_16 },
780     { X86::PSIGNDrr,        X86::PSIGNDrm,      TB_ALIGN_16 },
781     { X86::PSLLDrr,         X86::PSLLDrm,       TB_ALIGN_16 },
782     { X86::PSLLQrr,         X86::PSLLQrm,       TB_ALIGN_16 },
783     { X86::PSLLWrr,         X86::PSLLWrm,       TB_ALIGN_16 },
784     { X86::PSRADrr,         X86::PSRADrm,       TB_ALIGN_16 },
785     { X86::PSRAWrr,         X86::PSRAWrm,       TB_ALIGN_16 },
786     { X86::PSRLDrr,         X86::PSRLDrm,       TB_ALIGN_16 },
787     { X86::PSRLQrr,         X86::PSRLQrm,       TB_ALIGN_16 },
788     { X86::PSRLWrr,         X86::PSRLWrm,       TB_ALIGN_16 },
789     { X86::PSUBBrr,         X86::PSUBBrm,       TB_ALIGN_16 },
790     { X86::PSUBDrr,         X86::PSUBDrm,       TB_ALIGN_16 },
791     { X86::PSUBSBrr,        X86::PSUBSBrm,      TB_ALIGN_16 },
792     { X86::PSUBSWrr,        X86::PSUBSWrm,      TB_ALIGN_16 },
793     { X86::PSUBWrr,         X86::PSUBWrm,       TB_ALIGN_16 },
794     { X86::PUNPCKHBWrr,     X86::PUNPCKHBWrm,   TB_ALIGN_16 },
795     { X86::PUNPCKHDQrr,     X86::PUNPCKHDQrm,   TB_ALIGN_16 },
796     { X86::PUNPCKHQDQrr,    X86::PUNPCKHQDQrm,  TB_ALIGN_16 },
797     { X86::PUNPCKHWDrr,     X86::PUNPCKHWDrm,   TB_ALIGN_16 },
798     { X86::PUNPCKLBWrr,     X86::PUNPCKLBWrm,   TB_ALIGN_16 },
799     { X86::PUNPCKLDQrr,     X86::PUNPCKLDQrm,   TB_ALIGN_16 },
800     { X86::PUNPCKLQDQrr,    X86::PUNPCKLQDQrm,  TB_ALIGN_16 },
801     { X86::PUNPCKLWDrr,     X86::PUNPCKLWDrm,   TB_ALIGN_16 },
802     { X86::PXORrr,          X86::PXORrm,        TB_ALIGN_16 },
803     { X86::SBB32rr,         X86::SBB32rm,       0 },
804     { X86::SBB64rr,         X86::SBB64rm,       0 },
805     { X86::SHUFPDrri,       X86::SHUFPDrmi,     TB_ALIGN_16 },
806     { X86::SHUFPSrri,       X86::SHUFPSrmi,     TB_ALIGN_16 },
807     { X86::SUB16rr,         X86::SUB16rm,       0 },
808     { X86::SUB32rr,         X86::SUB32rm,       0 },
809     { X86::SUB64rr,         X86::SUB64rm,       0 },
810     { X86::SUB8rr,          X86::SUB8rm,        0 },
811     { X86::SUBPDrr,         X86::SUBPDrm,       TB_ALIGN_16 },
812     { X86::SUBPSrr,         X86::SUBPSrm,       TB_ALIGN_16 },
813     { X86::SUBSDrr,         X86::SUBSDrm,       0 },
814     { X86::SUBSSrr,         X86::SUBSSrm,       0 },
815     // FIXME: TEST*rr -> swapped operand of TEST*mr.
816     { X86::UNPCKHPDrr,      X86::UNPCKHPDrm,    TB_ALIGN_16 },
817     { X86::UNPCKHPSrr,      X86::UNPCKHPSrm,    TB_ALIGN_16 },
818     { X86::UNPCKLPDrr,      X86::UNPCKLPDrm,    TB_ALIGN_16 },
819     { X86::UNPCKLPSrr,      X86::UNPCKLPSrm,    TB_ALIGN_16 },
820     { X86::XOR16rr,         X86::XOR16rm,       0 },
821     { X86::XOR32rr,         X86::XOR32rm,       0 },
822     { X86::XOR64rr,         X86::XOR64rm,       0 },
823     { X86::XOR8rr,          X86::XOR8rm,        0 },
824     { X86::XORPDrr,         X86::XORPDrm,       TB_ALIGN_16 },
825     { X86::XORPSrr,         X86::XORPSrm,       TB_ALIGN_16 },
826     // AVX 128-bit versions of foldable instructions
827     { X86::VCVTSD2SSrr,       X86::VCVTSD2SSrm,        0 },
828     { X86::Int_VCVTSD2SSrr,   X86::Int_VCVTSD2SSrm,    0 },
829     { X86::VCVTSI2SD64rr,     X86::VCVTSI2SD64rm,      0 },
830     { X86::Int_VCVTSI2SD64rr, X86::Int_VCVTSI2SD64rm,  0 },
831     { X86::VCVTSI2SDrr,       X86::VCVTSI2SDrm,        0 },
832     { X86::Int_VCVTSI2SDrr,   X86::Int_VCVTSI2SDrm,    0 },
833     { X86::VCVTSI2SS64rr,     X86::VCVTSI2SS64rm,      0 },
834     { X86::Int_VCVTSI2SS64rr, X86::Int_VCVTSI2SS64rm,  0 },
835     { X86::VCVTSI2SSrr,       X86::VCVTSI2SSrm,        0 },
836     { X86::Int_VCVTSI2SSrr,   X86::Int_VCVTSI2SSrm,    0 },
837     { X86::VCVTSS2SDrr,       X86::VCVTSS2SDrm,        0 },
838     { X86::Int_VCVTSS2SDrr,   X86::Int_VCVTSS2SDrm,    0 },
839     { X86::VCVTTPD2DQrr,      X86::VCVTTPD2DQXrm,      0 },
840     { X86::VCVTTPS2DQrr,      X86::VCVTTPS2DQrm,       0 },
841     { X86::VRSQRTSSr,         X86::VRSQRTSSm,          0 },
842     { X86::VSQRTSDr,          X86::VSQRTSDm,           0 },
843     { X86::VSQRTSSr,          X86::VSQRTSSm,           0 },
844     { X86::VADDPDrr,          X86::VADDPDrm,           0 },
845     { X86::VADDPSrr,          X86::VADDPSrm,           0 },
846     { X86::VADDSDrr,          X86::VADDSDrm,           0 },
847     { X86::VADDSSrr,          X86::VADDSSrm,           0 },
848     { X86::VADDSUBPDrr,       X86::VADDSUBPDrm,        0 },
849     { X86::VADDSUBPSrr,       X86::VADDSUBPSrm,        0 },
850     { X86::VANDNPDrr,         X86::VANDNPDrm,          0 },
851     { X86::VANDNPSrr,         X86::VANDNPSrm,          0 },
852     { X86::VANDPDrr,          X86::VANDPDrm,           0 },
853     { X86::VANDPSrr,          X86::VANDPSrm,           0 },
854     { X86::VBLENDPDrri,       X86::VBLENDPDrmi,        0 },
855     { X86::VBLENDPSrri,       X86::VBLENDPSrmi,        0 },
856     { X86::VBLENDVPDrr,       X86::VBLENDVPDrm,        0 },
857     { X86::VBLENDVPSrr,       X86::VBLENDVPSrm,        0 },
858     { X86::VCMPPDrri,         X86::VCMPPDrmi,          0 },
859     { X86::VCMPPSrri,         X86::VCMPPSrmi,          0 },
860     { X86::VCMPSDrr,          X86::VCMPSDrm,           0 },
861     { X86::VCMPSSrr,          X86::VCMPSSrm,           0 },
862     { X86::VDIVPDrr,          X86::VDIVPDrm,           0 },
863     { X86::VDIVPSrr,          X86::VDIVPSrm,           0 },
864     { X86::VDIVSDrr,          X86::VDIVSDrm,           0 },
865     { X86::VDIVSSrr,          X86::VDIVSSrm,           0 },
866     { X86::VFsANDNPDrr,       X86::VFsANDNPDrm,        TB_ALIGN_16 },
867     { X86::VFsANDNPSrr,       X86::VFsANDNPSrm,        TB_ALIGN_16 },
868     { X86::VFsANDPDrr,        X86::VFsANDPDrm,         TB_ALIGN_16 },
869     { X86::VFsANDPSrr,        X86::VFsANDPSrm,         TB_ALIGN_16 },
870     { X86::VFsORPDrr,         X86::VFsORPDrm,          TB_ALIGN_16 },
871     { X86::VFsORPSrr,         X86::VFsORPSrm,          TB_ALIGN_16 },
872     { X86::VFsXORPDrr,        X86::VFsXORPDrm,         TB_ALIGN_16 },
873     { X86::VFsXORPSrr,        X86::VFsXORPSrm,         TB_ALIGN_16 },
874     { X86::VHADDPDrr,         X86::VHADDPDrm,          0 },
875     { X86::VHADDPSrr,         X86::VHADDPSrm,          0 },
876     { X86::VHSUBPDrr,         X86::VHSUBPDrm,          0 },
877     { X86::VHSUBPSrr,         X86::VHSUBPSrm,          0 },
878     { X86::Int_VCMPSDrr,      X86::Int_VCMPSDrm,       0 },
879     { X86::Int_VCMPSSrr,      X86::Int_VCMPSSrm,       0 },
880     { X86::VMAXPDrr,          X86::VMAXPDrm,           0 },
881     { X86::VMAXPSrr,          X86::VMAXPSrm,           0 },
882     { X86::VMAXSDrr,          X86::VMAXSDrm,           0 },
883     { X86::VMAXSSrr,          X86::VMAXSSrm,           0 },
884     { X86::VMINPDrr,          X86::VMINPDrm,           0 },
885     { X86::VMINPSrr,          X86::VMINPSrm,           0 },
886     { X86::VMINSDrr,          X86::VMINSDrm,           0 },
887     { X86::VMINSSrr,          X86::VMINSSrm,           0 },
888     { X86::VMPSADBWrri,       X86::VMPSADBWrmi,        0 },
889     { X86::VMULPDrr,          X86::VMULPDrm,           0 },
890     { X86::VMULPSrr,          X86::VMULPSrm,           0 },
891     { X86::VMULSDrr,          X86::VMULSDrm,           0 },
892     { X86::VMULSSrr,          X86::VMULSSrm,           0 },
893     { X86::VORPDrr,           X86::VORPDrm,            0 },
894     { X86::VORPSrr,           X86::VORPSrm,            0 },
895     { X86::VPACKSSDWrr,       X86::VPACKSSDWrm,        0 },
896     { X86::VPACKSSWBrr,       X86::VPACKSSWBrm,        0 },
897     { X86::VPACKUSDWrr,       X86::VPACKUSDWrm,        0 },
898     { X86::VPACKUSWBrr,       X86::VPACKUSWBrm,        0 },
899     { X86::VPADDBrr,          X86::VPADDBrm,           0 },
900     { X86::VPADDDrr,          X86::VPADDDrm,           0 },
901     { X86::VPADDQrr,          X86::VPADDQrm,           0 },
902     { X86::VPADDSBrr,         X86::VPADDSBrm,          0 },
903     { X86::VPADDSWrr,         X86::VPADDSWrm,          0 },
904     { X86::VPADDUSBrr,        X86::VPADDUSBrm,         0 },
905     { X86::VPADDUSWrr,        X86::VPADDUSWrm,         0 },
906     { X86::VPADDWrr,          X86::VPADDWrm,           0 },
907     { X86::VPALIGNR128rr,     X86::VPALIGNR128rm,      0 },
908     { X86::VPANDNrr,          X86::VPANDNrm,           0 },
909     { X86::VPANDrr,           X86::VPANDrm,            0 },
910     { X86::VPAVGBrr,          X86::VPAVGBrm,           0 },
911     { X86::VPAVGWrr,          X86::VPAVGWrm,           0 },
912     { X86::VPBLENDWrri,       X86::VPBLENDWrmi,        0 },
913     { X86::VPCMPEQBrr,        X86::VPCMPEQBrm,         0 },
914     { X86::VPCMPEQDrr,        X86::VPCMPEQDrm,         0 },
915     { X86::VPCMPEQQrr,        X86::VPCMPEQQrm,         0 },
916     { X86::VPCMPEQWrr,        X86::VPCMPEQWrm,         0 },
917     { X86::VPCMPGTBrr,        X86::VPCMPGTBrm,         0 },
918     { X86::VPCMPGTDrr,        X86::VPCMPGTDrm,         0 },
919     { X86::VPCMPGTQrr,        X86::VPCMPGTQrm,         0 },
920     { X86::VPCMPGTWrr,        X86::VPCMPGTWrm,         0 },
921     { X86::VPHADDDrr,         X86::VPHADDDrm,          0 },
922     { X86::VPHADDSWrr128,     X86::VPHADDSWrm128,      0 },
923     { X86::VPHADDWrr,         X86::VPHADDWrm,          0 },
924     { X86::VPHSUBDrr,         X86::VPHSUBDrm,          0 },
925     { X86::VPHSUBSWrr128,     X86::VPHSUBSWrm128,      0 },
926     { X86::VPHSUBWrr,         X86::VPHSUBWrm,          0 },
927     { X86::VPERMILPDrr,       X86::VPERMILPDrm,        0 },
928     { X86::VPERMILPSrr,       X86::VPERMILPSrm,        0 },
929     { X86::VPINSRWrri,        X86::VPINSRWrmi,         0 },
930     { X86::VPMADDUBSWrr128,   X86::VPMADDUBSWrm128,    0 },
931     { X86::VPMADDWDrr,        X86::VPMADDWDrm,         0 },
932     { X86::VPMAXSWrr,         X86::VPMAXSWrm,          0 },
933     { X86::VPMAXUBrr,         X86::VPMAXUBrm,          0 },
934     { X86::VPMINSWrr,         X86::VPMINSWrm,          0 },
935     { X86::VPMINUBrr,         X86::VPMINUBrm,          0 },
936     { X86::VPMINSBrr,         X86::VPMINSBrm,          0 },
937     { X86::VPMINSDrr,         X86::VPMINSDrm,          0 },
938     { X86::VPMINUDrr,         X86::VPMINUDrm,          0 },
939     { X86::VPMINUWrr,         X86::VPMINUWrm,          0 },
940     { X86::VPMAXSBrr,         X86::VPMAXSBrm,          0 },
941     { X86::VPMAXSDrr,         X86::VPMAXSDrm,          0 },
942     { X86::VPMAXUDrr,         X86::VPMAXUDrm,          0 },
943     { X86::VPMAXUWrr,         X86::VPMAXUWrm,          0 },
944     { X86::VPMULDQrr,         X86::VPMULDQrm,          0 },
945     { X86::VPMULHRSWrr128,    X86::VPMULHRSWrm128,     0 },
946     { X86::VPMULHUWrr,        X86::VPMULHUWrm,         0 },
947     { X86::VPMULHWrr,         X86::VPMULHWrm,          0 },
948     { X86::VPMULLDrr,         X86::VPMULLDrm,          0 },
949     { X86::VPMULLWrr,         X86::VPMULLWrm,          0 },
950     { X86::VPMULUDQrr,        X86::VPMULUDQrm,         0 },
951     { X86::VPORrr,            X86::VPORrm,             0 },
952     { X86::VPSADBWrr,         X86::VPSADBWrm,          0 },
953     { X86::VPSHUFBrr,         X86::VPSHUFBrm,          0 },
954     { X86::VPSIGNBrr,         X86::VPSIGNBrm,          0 },
955     { X86::VPSIGNWrr,         X86::VPSIGNWrm,          0 },
956     { X86::VPSIGNDrr,         X86::VPSIGNDrm,          0 },
957     { X86::VPSLLDrr,          X86::VPSLLDrm,           0 },
958     { X86::VPSLLQrr,          X86::VPSLLQrm,           0 },
959     { X86::VPSLLWrr,          X86::VPSLLWrm,           0 },
960     { X86::VPSRADrr,          X86::VPSRADrm,           0 },
961     { X86::VPSRAWrr,          X86::VPSRAWrm,           0 },
962     { X86::VPSRLDrr,          X86::VPSRLDrm,           0 },
963     { X86::VPSRLQrr,          X86::VPSRLQrm,           0 },
964     { X86::VPSRLWrr,          X86::VPSRLWrm,           0 },
965     { X86::VPSUBBrr,          X86::VPSUBBrm,           0 },
966     { X86::VPSUBDrr,          X86::VPSUBDrm,           0 },
967     { X86::VPSUBSBrr,         X86::VPSUBSBrm,          0 },
968     { X86::VPSUBSWrr,         X86::VPSUBSWrm,          0 },
969     { X86::VPSUBWrr,          X86::VPSUBWrm,           0 },
970     { X86::VPUNPCKHBWrr,      X86::VPUNPCKHBWrm,       0 },
971     { X86::VPUNPCKHDQrr,      X86::VPUNPCKHDQrm,       0 },
972     { X86::VPUNPCKHQDQrr,     X86::VPUNPCKHQDQrm,      0 },
973     { X86::VPUNPCKHWDrr,      X86::VPUNPCKHWDrm,       0 },
974     { X86::VPUNPCKLBWrr,      X86::VPUNPCKLBWrm,       0 },
975     { X86::VPUNPCKLDQrr,      X86::VPUNPCKLDQrm,       0 },
976     { X86::VPUNPCKLQDQrr,     X86::VPUNPCKLQDQrm,      0 },
977     { X86::VPUNPCKLWDrr,      X86::VPUNPCKLWDrm,       0 },
978     { X86::VPXORrr,           X86::VPXORrm,            0 },
979     { X86::VSHUFPDrri,        X86::VSHUFPDrmi,         0 },
980     { X86::VSHUFPSrri,        X86::VSHUFPSrmi,         0 },
981     { X86::VSUBPDrr,          X86::VSUBPDrm,           0 },
982     { X86::VSUBPSrr,          X86::VSUBPSrm,           0 },
983     { X86::VSUBSDrr,          X86::VSUBSDrm,           0 },
984     { X86::VSUBSSrr,          X86::VSUBSSrm,           0 },
985     { X86::VUNPCKHPDrr,       X86::VUNPCKHPDrm,        0 },
986     { X86::VUNPCKHPSrr,       X86::VUNPCKHPSrm,        0 },
987     { X86::VUNPCKLPDrr,       X86::VUNPCKLPDrm,        0 },
988     { X86::VUNPCKLPSrr,       X86::VUNPCKLPSrm,        0 },
989     { X86::VXORPDrr,          X86::VXORPDrm,           0 },
990     { X86::VXORPSrr,          X86::VXORPSrm,           0 },
991     // AVX 256-bit foldable instructions
992     { X86::VADDPDYrr,         X86::VADDPDYrm,          0 },
993     { X86::VADDPSYrr,         X86::VADDPSYrm,          0 },
994     { X86::VADDSUBPDYrr,      X86::VADDSUBPDYrm,       0 },
995     { X86::VADDSUBPSYrr,      X86::VADDSUBPSYrm,       0 },
996     { X86::VANDNPDYrr,        X86::VANDNPDYrm,         0 },
997     { X86::VANDNPSYrr,        X86::VANDNPSYrm,         0 },
998     { X86::VANDPDYrr,         X86::VANDPDYrm,          0 },
999     { X86::VANDPSYrr,         X86::VANDPSYrm,          0 },
1000     { X86::VBLENDPDYrri,      X86::VBLENDPDYrmi,       0 },
1001     { X86::VBLENDPSYrri,      X86::VBLENDPSYrmi,       0 },
1002     { X86::VBLENDVPDYrr,      X86::VBLENDVPDYrm,       0 },
1003     { X86::VBLENDVPSYrr,      X86::VBLENDVPSYrm,       0 },
1004     { X86::VCMPPDYrri,        X86::VCMPPDYrmi,         0 },
1005     { X86::VCMPPSYrri,        X86::VCMPPSYrmi,         0 },
1006     { X86::VDIVPDYrr,         X86::VDIVPDYrm,          0 },
1007     { X86::VDIVPSYrr,         X86::VDIVPSYrm,          0 },
1008     { X86::VHADDPDYrr,        X86::VHADDPDYrm,         0 },
1009     { X86::VHADDPSYrr,        X86::VHADDPSYrm,         0 },
1010     { X86::VHSUBPDYrr,        X86::VHSUBPDYrm,         0 },
1011     { X86::VHSUBPSYrr,        X86::VHSUBPSYrm,         0 },
1012     { X86::VINSERTF128rr,     X86::VINSERTF128rm,      0 },
1013     { X86::VMAXPDYrr,         X86::VMAXPDYrm,          0 },
1014     { X86::VMAXPSYrr,         X86::VMAXPSYrm,          0 },
1015     { X86::VMINPDYrr,         X86::VMINPDYrm,          0 },
1016     { X86::VMINPSYrr,         X86::VMINPSYrm,          0 },
1017     { X86::VMULPDYrr,         X86::VMULPDYrm,          0 },
1018     { X86::VMULPSYrr,         X86::VMULPSYrm,          0 },
1019     { X86::VORPDYrr,          X86::VORPDYrm,           0 },
1020     { X86::VORPSYrr,          X86::VORPSYrm,           0 },
1021     { X86::VPERM2F128rr,      X86::VPERM2F128rm,       0 },
1022     { X86::VPERMILPDYrr,      X86::VPERMILPDYrm,       0 },
1023     { X86::VPERMILPSYrr,      X86::VPERMILPSYrm,       0 },
1024     { X86::VSHUFPDYrri,       X86::VSHUFPDYrmi,        0 },
1025     { X86::VSHUFPSYrri,       X86::VSHUFPSYrmi,        0 },
1026     { X86::VSUBPDYrr,         X86::VSUBPDYrm,          0 },
1027     { X86::VSUBPSYrr,         X86::VSUBPSYrm,          0 },
1028     { X86::VUNPCKHPDYrr,      X86::VUNPCKHPDYrm,       0 },
1029     { X86::VUNPCKHPSYrr,      X86::VUNPCKHPSYrm,       0 },
1030     { X86::VUNPCKLPDYrr,      X86::VUNPCKLPDYrm,       0 },
1031     { X86::VUNPCKLPSYrr,      X86::VUNPCKLPSYrm,       0 },
1032     { X86::VXORPDYrr,         X86::VXORPDYrm,          0 },
1033     { X86::VXORPSYrr,         X86::VXORPSYrm,          0 },
1034     // AVX2 foldable instructions
1035     { X86::VINSERTI128rr,     X86::VINSERTI128rm,      0 },
1036     { X86::VPACKSSDWYrr,      X86::VPACKSSDWYrm,       0 },
1037     { X86::VPACKSSWBYrr,      X86::VPACKSSWBYrm,       0 },
1038     { X86::VPACKUSDWYrr,      X86::VPACKUSDWYrm,       0 },
1039     { X86::VPACKUSWBYrr,      X86::VPACKUSWBYrm,       0 },
1040     { X86::VPADDBYrr,         X86::VPADDBYrm,          0 },
1041     { X86::VPADDDYrr,         X86::VPADDDYrm,          0 },
1042     { X86::VPADDQYrr,         X86::VPADDQYrm,          0 },
1043     { X86::VPADDSBYrr,        X86::VPADDSBYrm,         0 },
1044     { X86::VPADDSWYrr,        X86::VPADDSWYrm,         0 },
1045     { X86::VPADDUSBYrr,       X86::VPADDUSBYrm,        0 },
1046     { X86::VPADDUSWYrr,       X86::VPADDUSWYrm,        0 },
1047     { X86::VPADDWYrr,         X86::VPADDWYrm,          0 },
1048     { X86::VPALIGNR256rr,     X86::VPALIGNR256rm,      0 },
1049     { X86::VPANDNYrr,         X86::VPANDNYrm,          0 },
1050     { X86::VPANDYrr,          X86::VPANDYrm,           0 },
1051     { X86::VPAVGBYrr,         X86::VPAVGBYrm,          0 },
1052     { X86::VPAVGWYrr,         X86::VPAVGWYrm,          0 },
1053     { X86::VPBLENDDrri,       X86::VPBLENDDrmi,        0 },
1054     { X86::VPBLENDDYrri,      X86::VPBLENDDYrmi,       0 },
1055     { X86::VPBLENDWYrri,      X86::VPBLENDWYrmi,       0 },
1056     { X86::VPCMPEQBYrr,       X86::VPCMPEQBYrm,        0 },
1057     { X86::VPCMPEQDYrr,       X86::VPCMPEQDYrm,        0 },
1058     { X86::VPCMPEQQYrr,       X86::VPCMPEQQYrm,        0 },
1059     { X86::VPCMPEQWYrr,       X86::VPCMPEQWYrm,        0 },
1060     { X86::VPCMPGTBYrr,       X86::VPCMPGTBYrm,        0 },
1061     { X86::VPCMPGTDYrr,       X86::VPCMPGTDYrm,        0 },
1062     { X86::VPCMPGTQYrr,       X86::VPCMPGTQYrm,        0 },
1063     { X86::VPCMPGTWYrr,       X86::VPCMPGTWYrm,        0 },
1064     { X86::VPERM2I128rr,      X86::VPERM2I128rm,       0 },
1065     { X86::VPERMDYrr,         X86::VPERMDYrm,          0 },
1066     { X86::VPERMPDYri,        X86::VPERMPDYmi,         0 },
1067     { X86::VPERMPSYrr,        X86::VPERMPSYrm,         0 },
1068     { X86::VPERMQYri,         X86::VPERMQYmi,          0 },
1069     { X86::VPHADDDYrr,        X86::VPHADDDYrm,         0 },
1070     { X86::VPHADDSWrr256,     X86::VPHADDSWrm256,      0 },
1071     { X86::VPHADDWYrr,        X86::VPHADDWYrm,         0 },
1072     { X86::VPHSUBDYrr,        X86::VPHSUBDYrm,         0 },
1073     { X86::VPHSUBSWrr256,     X86::VPHSUBSWrm256,      0 },
1074     { X86::VPHSUBWYrr,        X86::VPHSUBWYrm,         0 },
1075     { X86::VPMADDUBSWrr256,   X86::VPMADDUBSWrm256,    0 },
1076     { X86::VPMADDWDYrr,       X86::VPMADDWDYrm,        0 },
1077     { X86::VPMAXSWYrr,        X86::VPMAXSWYrm,         0 },
1078     { X86::VPMAXUBYrr,        X86::VPMAXUBYrm,         0 },
1079     { X86::VPMINSWYrr,        X86::VPMINSWYrm,         0 },
1080     { X86::VPMINUBYrr,        X86::VPMINUBYrm,         0 },
1081     { X86::VPMINSBYrr,        X86::VPMINSBYrm,         0 },
1082     { X86::VPMINSDYrr,        X86::VPMINSDYrm,         0 },
1083     { X86::VPMINUDYrr,        X86::VPMINUDYrm,         0 },
1084     { X86::VPMINUWYrr,        X86::VPMINUWYrm,         0 },
1085     { X86::VPMAXSBYrr,        X86::VPMAXSBYrm,         0 },
1086     { X86::VPMAXSDYrr,        X86::VPMAXSDYrm,         0 },
1087     { X86::VPMAXUDYrr,        X86::VPMAXUDYrm,         0 },
1088     { X86::VPMAXUWYrr,        X86::VPMAXUWYrm,         0 },
1089     { X86::VMPSADBWYrri,      X86::VMPSADBWYrmi,       0 },
1090     { X86::VPMULDQYrr,        X86::VPMULDQYrm,         0 },
1091     { X86::VPMULHRSWrr256,    X86::VPMULHRSWrm256,     0 },
1092     { X86::VPMULHUWYrr,       X86::VPMULHUWYrm,        0 },
1093     { X86::VPMULHWYrr,        X86::VPMULHWYrm,         0 },
1094     { X86::VPMULLDYrr,        X86::VPMULLDYrm,         0 },
1095     { X86::VPMULLWYrr,        X86::VPMULLWYrm,         0 },
1096     { X86::VPMULUDQYrr,       X86::VPMULUDQYrm,        0 },
1097     { X86::VPORYrr,           X86::VPORYrm,            0 },
1098     { X86::VPSADBWYrr,        X86::VPSADBWYrm,         0 },
1099     { X86::VPSHUFBYrr,        X86::VPSHUFBYrm,         0 },
1100     { X86::VPSIGNBYrr,        X86::VPSIGNBYrm,         0 },
1101     { X86::VPSIGNWYrr,        X86::VPSIGNWYrm,         0 },
1102     { X86::VPSIGNDYrr,        X86::VPSIGNDYrm,         0 },
1103     { X86::VPSLLDYrr,         X86::VPSLLDYrm,          0 },
1104     { X86::VPSLLQYrr,         X86::VPSLLQYrm,          0 },
1105     { X86::VPSLLWYrr,         X86::VPSLLWYrm,          0 },
1106     { X86::VPSLLVDrr,         X86::VPSLLVDrm,          0 },
1107     { X86::VPSLLVDYrr,        X86::VPSLLVDYrm,         0 },
1108     { X86::VPSLLVQrr,         X86::VPSLLVQrm,          0 },
1109     { X86::VPSLLVQYrr,        X86::VPSLLVQYrm,         0 },
1110     { X86::VPSRADYrr,         X86::VPSRADYrm,          0 },
1111     { X86::VPSRAWYrr,         X86::VPSRAWYrm,          0 },
1112     { X86::VPSRAVDrr,         X86::VPSRAVDrm,          0 },
1113     { X86::VPSRAVDYrr,        X86::VPSRAVDYrm,         0 },
1114     { X86::VPSRLDYrr,         X86::VPSRLDYrm,          0 },
1115     { X86::VPSRLQYrr,         X86::VPSRLQYrm,          0 },
1116     { X86::VPSRLWYrr,         X86::VPSRLWYrm,          0 },
1117     { X86::VPSRLVDrr,         X86::VPSRLVDrm,          0 },
1118     { X86::VPSRLVDYrr,        X86::VPSRLVDYrm,         0 },
1119     { X86::VPSRLVQrr,         X86::VPSRLVQrm,          0 },
1120     { X86::VPSRLVQYrr,        X86::VPSRLVQYrm,         0 },
1121     { X86::VPSUBBYrr,         X86::VPSUBBYrm,          0 },
1122     { X86::VPSUBDYrr,         X86::VPSUBDYrm,          0 },
1123     { X86::VPSUBSBYrr,        X86::VPSUBSBYrm,         0 },
1124     { X86::VPSUBSWYrr,        X86::VPSUBSWYrm,         0 },
1125     { X86::VPSUBWYrr,         X86::VPSUBWYrm,          0 },
1126     { X86::VPUNPCKHBWYrr,     X86::VPUNPCKHBWYrm,      0 },
1127     { X86::VPUNPCKHDQYrr,     X86::VPUNPCKHDQYrm,      0 },
1128     { X86::VPUNPCKHQDQYrr,    X86::VPUNPCKHQDQYrm,     0 },
1129     { X86::VPUNPCKHWDYrr,     X86::VPUNPCKHWDYrm,      0 },
1130     { X86::VPUNPCKLBWYrr,     X86::VPUNPCKLBWYrm,      0 },
1131     { X86::VPUNPCKLDQYrr,     X86::VPUNPCKLDQYrm,      0 },
1132     { X86::VPUNPCKLQDQYrr,    X86::VPUNPCKLQDQYrm,     0 },
1133     { X86::VPUNPCKLWDYrr,     X86::VPUNPCKLWDYrm,      0 },
1134     { X86::VPXORYrr,          X86::VPXORYrm,           0 },
1135     // FIXME: add AVX 256-bit foldable instructions
1136
1137     // FMA4 foldable patterns
1138     { X86::VFMADDSS4rr,       X86::VFMADDSS4mr,        0           },
1139     { X86::VFMADDSD4rr,       X86::VFMADDSD4mr,        0           },
1140     { X86::VFMADDPS4rr,       X86::VFMADDPS4mr,        TB_ALIGN_16 },
1141     { X86::VFMADDPD4rr,       X86::VFMADDPD4mr,        TB_ALIGN_16 },
1142     { X86::VFMADDPS4rrY,      X86::VFMADDPS4mrY,       TB_ALIGN_32 },
1143     { X86::VFMADDPD4rrY,      X86::VFMADDPD4mrY,       TB_ALIGN_32 },
1144     { X86::VFNMADDSS4rr,      X86::VFNMADDSS4mr,       0           },
1145     { X86::VFNMADDSD4rr,      X86::VFNMADDSD4mr,       0           },
1146     { X86::VFNMADDPS4rr,      X86::VFNMADDPS4mr,       TB_ALIGN_16 },
1147     { X86::VFNMADDPD4rr,      X86::VFNMADDPD4mr,       TB_ALIGN_16 },
1148     { X86::VFNMADDPS4rrY,     X86::VFNMADDPS4mrY,      TB_ALIGN_32 },
1149     { X86::VFNMADDPD4rrY,     X86::VFNMADDPD4mrY,      TB_ALIGN_32 },
1150     { X86::VFMSUBSS4rr,       X86::VFMSUBSS4mr,        0           },
1151     { X86::VFMSUBSD4rr,       X86::VFMSUBSD4mr,        0           },
1152     { X86::VFMSUBPS4rr,       X86::VFMSUBPS4mr,        TB_ALIGN_16 },
1153     { X86::VFMSUBPD4rr,       X86::VFMSUBPD4mr,        TB_ALIGN_16 },
1154     { X86::VFMSUBPS4rrY,      X86::VFMSUBPS4mrY,       TB_ALIGN_32 },
1155     { X86::VFMSUBPD4rrY,      X86::VFMSUBPD4mrY,       TB_ALIGN_32 },
1156     { X86::VFNMSUBSS4rr,      X86::VFNMSUBSS4mr,       0           },
1157     { X86::VFNMSUBSD4rr,      X86::VFNMSUBSD4mr,       0           },
1158     { X86::VFNMSUBPS4rr,      X86::VFNMSUBPS4mr,       TB_ALIGN_16 },
1159     { X86::VFNMSUBPD4rr,      X86::VFNMSUBPD4mr,       TB_ALIGN_16 },
1160     { X86::VFNMSUBPS4rrY,     X86::VFNMSUBPS4mrY,      TB_ALIGN_32 },
1161     { X86::VFNMSUBPD4rrY,     X86::VFNMSUBPD4mrY,      TB_ALIGN_32 },
1162     { X86::VFMADDSUBPS4rr,    X86::VFMADDSUBPS4mr,     TB_ALIGN_16 },
1163     { X86::VFMADDSUBPD4rr,    X86::VFMADDSUBPD4mr,     TB_ALIGN_16 },
1164     { X86::VFMADDSUBPS4rrY,   X86::VFMADDSUBPS4mrY,    TB_ALIGN_32 },
1165     { X86::VFMADDSUBPD4rrY,   X86::VFMADDSUBPD4mrY,    TB_ALIGN_32 },
1166     { X86::VFMSUBADDPS4rr,    X86::VFMSUBADDPS4mr,     TB_ALIGN_16 },
1167     { X86::VFMSUBADDPD4rr,    X86::VFMSUBADDPD4mr,     TB_ALIGN_16 },
1168     { X86::VFMSUBADDPS4rrY,   X86::VFMSUBADDPS4mrY,    TB_ALIGN_32 },
1169     { X86::VFMSUBADDPD4rrY,   X86::VFMSUBADDPD4mrY,    TB_ALIGN_32 },
1170
1171     // BMI/BMI2 foldable instructions
1172     { X86::ANDN32rr,          X86::ANDN32rm,            0 },
1173     { X86::ANDN64rr,          X86::ANDN64rm,            0 },
1174     { X86::MULX32rr,          X86::MULX32rm,            0 },
1175     { X86::MULX64rr,          X86::MULX64rm,            0 },
1176     { X86::PDEP32rr,          X86::PDEP32rm,            0 },
1177     { X86::PDEP64rr,          X86::PDEP64rm,            0 },
1178     { X86::PEXT32rr,          X86::PEXT32rm,            0 },
1179     { X86::PEXT64rr,          X86::PEXT64rm,            0 },
1180   };
1181
1182   for (unsigned i = 0, e = array_lengthof(OpTbl2); i != e; ++i) {
1183     unsigned RegOp = OpTbl2[i].RegOp;
1184     unsigned MemOp = OpTbl2[i].MemOp;
1185     unsigned Flags = OpTbl2[i].Flags;
1186     AddTableEntry(RegOp2MemOpTable2, MemOp2RegOpTable,
1187                   RegOp, MemOp,
1188                   // Index 2, folded load
1189                   Flags | TB_INDEX_2 | TB_FOLDED_LOAD);
1190   }
1191
1192   static const X86OpTblEntry OpTbl3[] = {
1193     // FMA foldable instructions
1194     { X86::VFMADDSSr231r,         X86::VFMADDSSr231m,         0 },
1195     { X86::VFMADDSDr231r,         X86::VFMADDSDr231m,         0 },
1196     { X86::VFMADDSSr132r,         X86::VFMADDSSr132m,         0 },
1197     { X86::VFMADDSDr132r,         X86::VFMADDSDr132m,         0 },
1198     { X86::VFMADDSSr213r,         X86::VFMADDSSr213m,         0 },
1199     { X86::VFMADDSDr213r,         X86::VFMADDSDr213m,         0 },
1200     { X86::VFMADDSSr213r_Int,     X86::VFMADDSSr213m_Int,     0 },
1201     { X86::VFMADDSDr213r_Int,     X86::VFMADDSDr213m_Int,     0 },
1202
1203     { X86::VFMADDPSr231r,         X86::VFMADDPSr231m,         TB_ALIGN_16 },
1204     { X86::VFMADDPDr231r,         X86::VFMADDPDr231m,         TB_ALIGN_16 },
1205     { X86::VFMADDPSr132r,         X86::VFMADDPSr132m,         TB_ALIGN_16 },
1206     { X86::VFMADDPDr132r,         X86::VFMADDPDr132m,         TB_ALIGN_16 },
1207     { X86::VFMADDPSr213r,         X86::VFMADDPSr213m,         TB_ALIGN_16 },
1208     { X86::VFMADDPDr213r,         X86::VFMADDPDr213m,         TB_ALIGN_16 },
1209     { X86::VFMADDPSr231rY,        X86::VFMADDPSr231mY,        TB_ALIGN_32 },
1210     { X86::VFMADDPDr231rY,        X86::VFMADDPDr231mY,        TB_ALIGN_32 },
1211     { X86::VFMADDPSr132rY,        X86::VFMADDPSr132mY,        TB_ALIGN_32 },
1212     { X86::VFMADDPDr132rY,        X86::VFMADDPDr132mY,        TB_ALIGN_32 },
1213     { X86::VFMADDPSr213rY,        X86::VFMADDPSr213mY,        TB_ALIGN_32 },
1214     { X86::VFMADDPDr213rY,        X86::VFMADDPDr213mY,        TB_ALIGN_32 },
1215
1216     { X86::VFNMADDSSr231r,        X86::VFNMADDSSr231m,        0 },
1217     { X86::VFNMADDSDr231r,        X86::VFNMADDSDr231m,        0 },
1218     { X86::VFNMADDSSr132r,        X86::VFNMADDSSr132m,        0 },
1219     { X86::VFNMADDSDr132r,        X86::VFNMADDSDr132m,        0 },
1220     { X86::VFNMADDSSr213r,        X86::VFNMADDSSr213m,        0 },
1221     { X86::VFNMADDSDr213r,        X86::VFNMADDSDr213m,        0 },
1222     { X86::VFNMADDSSr213r_Int,    X86::VFNMADDSSr213m_Int,    0 },
1223     { X86::VFNMADDSDr213r_Int,    X86::VFNMADDSDr213m_Int,    0 },
1224
1225     { X86::VFNMADDPSr231r,        X86::VFNMADDPSr231m,        TB_ALIGN_16 },
1226     { X86::VFNMADDPDr231r,        X86::VFNMADDPDr231m,        TB_ALIGN_16 },
1227     { X86::VFNMADDPSr132r,        X86::VFNMADDPSr132m,        TB_ALIGN_16 },
1228     { X86::VFNMADDPDr132r,        X86::VFNMADDPDr132m,        TB_ALIGN_16 },
1229     { X86::VFNMADDPSr213r,        X86::VFNMADDPSr213m,        TB_ALIGN_16 },
1230     { X86::VFNMADDPDr213r,        X86::VFNMADDPDr213m,        TB_ALIGN_16 },
1231     { X86::VFNMADDPSr231rY,       X86::VFNMADDPSr231mY,       TB_ALIGN_32 },
1232     { X86::VFNMADDPDr231rY,       X86::VFNMADDPDr231mY,       TB_ALIGN_32 },
1233     { X86::VFNMADDPSr132rY,       X86::VFNMADDPSr132mY,       TB_ALIGN_32 },
1234     { X86::VFNMADDPDr132rY,       X86::VFNMADDPDr132mY,       TB_ALIGN_32 },
1235     { X86::VFNMADDPSr213rY,       X86::VFNMADDPSr213mY,       TB_ALIGN_32 },
1236     { X86::VFNMADDPDr213rY,       X86::VFNMADDPDr213mY,       TB_ALIGN_32 },
1237
1238     { X86::VFMSUBSSr231r,         X86::VFMSUBSSr231m,         0 },
1239     { X86::VFMSUBSDr231r,         X86::VFMSUBSDr231m,         0 },
1240     { X86::VFMSUBSSr132r,         X86::VFMSUBSSr132m,         0 },
1241     { X86::VFMSUBSDr132r,         X86::VFMSUBSDr132m,         0 },
1242     { X86::VFMSUBSSr213r,         X86::VFMSUBSSr213m,         0 },
1243     { X86::VFMSUBSDr213r,         X86::VFMSUBSDr213m,         0 },
1244     { X86::VFMSUBSSr213r_Int,     X86::VFMSUBSSr213m_Int,     0 },
1245     { X86::VFMSUBSDr213r_Int,     X86::VFMSUBSDr213m_Int,     0 },
1246
1247     { X86::VFMSUBPSr231r,         X86::VFMSUBPSr231m,         TB_ALIGN_16 },
1248     { X86::VFMSUBPDr231r,         X86::VFMSUBPDr231m,         TB_ALIGN_16 },
1249     { X86::VFMSUBPSr132r,         X86::VFMSUBPSr132m,         TB_ALIGN_16 },
1250     { X86::VFMSUBPDr132r,         X86::VFMSUBPDr132m,         TB_ALIGN_16 },
1251     { X86::VFMSUBPSr213r,         X86::VFMSUBPSr213m,         TB_ALIGN_16 },
1252     { X86::VFMSUBPDr213r,         X86::VFMSUBPDr213m,         TB_ALIGN_16 },
1253     { X86::VFMSUBPSr231rY,        X86::VFMSUBPSr231mY,        TB_ALIGN_32 },
1254     { X86::VFMSUBPDr231rY,        X86::VFMSUBPDr231mY,        TB_ALIGN_32 },
1255     { X86::VFMSUBPSr132rY,        X86::VFMSUBPSr132mY,        TB_ALIGN_32 },
1256     { X86::VFMSUBPDr132rY,        X86::VFMSUBPDr132mY,        TB_ALIGN_32 },
1257     { X86::VFMSUBPSr213rY,        X86::VFMSUBPSr213mY,        TB_ALIGN_32 },
1258     { X86::VFMSUBPDr213rY,        X86::VFMSUBPDr213mY,        TB_ALIGN_32 },
1259
1260     { X86::VFNMSUBSSr231r,        X86::VFNMSUBSSr231m,        0 },
1261     { X86::VFNMSUBSDr231r,        X86::VFNMSUBSDr231m,        0 },
1262     { X86::VFNMSUBSSr132r,        X86::VFNMSUBSSr132m,        0 },
1263     { X86::VFNMSUBSDr132r,        X86::VFNMSUBSDr132m,        0 },
1264     { X86::VFNMSUBSSr213r,        X86::VFNMSUBSSr213m,        0 },
1265     { X86::VFNMSUBSDr213r,        X86::VFNMSUBSDr213m,        0 },
1266     { X86::VFNMSUBSSr213r_Int,    X86::VFNMSUBSSr213m_Int,    0 },
1267     { X86::VFNMSUBSDr213r_Int,    X86::VFNMSUBSDr213m_Int,    0 },
1268
1269     { X86::VFNMSUBPSr231r,        X86::VFNMSUBPSr231m,        TB_ALIGN_16 },
1270     { X86::VFNMSUBPDr231r,        X86::VFNMSUBPDr231m,        TB_ALIGN_16 },
1271     { X86::VFNMSUBPSr132r,        X86::VFNMSUBPSr132m,        TB_ALIGN_16 },
1272     { X86::VFNMSUBPDr132r,        X86::VFNMSUBPDr132m,        TB_ALIGN_16 },
1273     { X86::VFNMSUBPSr213r,        X86::VFNMSUBPSr213m,        TB_ALIGN_16 },
1274     { X86::VFNMSUBPDr213r,        X86::VFNMSUBPDr213m,        TB_ALIGN_16 },
1275     { X86::VFNMSUBPSr231rY,       X86::VFNMSUBPSr231mY,       TB_ALIGN_32 },
1276     { X86::VFNMSUBPDr231rY,       X86::VFNMSUBPDr231mY,       TB_ALIGN_32 },
1277     { X86::VFNMSUBPSr132rY,       X86::VFNMSUBPSr132mY,       TB_ALIGN_32 },
1278     { X86::VFNMSUBPDr132rY,       X86::VFNMSUBPDr132mY,       TB_ALIGN_32 },
1279     { X86::VFNMSUBPSr213rY,       X86::VFNMSUBPSr213mY,       TB_ALIGN_32 },
1280     { X86::VFNMSUBPDr213rY,       X86::VFNMSUBPDr213mY,       TB_ALIGN_32 },
1281
1282     { X86::VFMADDSUBPSr231r,      X86::VFMADDSUBPSr231m,      TB_ALIGN_16 },
1283     { X86::VFMADDSUBPDr231r,      X86::VFMADDSUBPDr231m,      TB_ALIGN_16 },
1284     { X86::VFMADDSUBPSr132r,      X86::VFMADDSUBPSr132m,      TB_ALIGN_16 },
1285     { X86::VFMADDSUBPDr132r,      X86::VFMADDSUBPDr132m,      TB_ALIGN_16 },
1286     { X86::VFMADDSUBPSr213r,      X86::VFMADDSUBPSr213m,      TB_ALIGN_16 },
1287     { X86::VFMADDSUBPDr213r,      X86::VFMADDSUBPDr213m,      TB_ALIGN_16 },
1288     { X86::VFMADDSUBPSr231rY,     X86::VFMADDSUBPSr231mY,     TB_ALIGN_32 },
1289     { X86::VFMADDSUBPDr231rY,     X86::VFMADDSUBPDr231mY,     TB_ALIGN_32 },
1290     { X86::VFMADDSUBPSr132rY,     X86::VFMADDSUBPSr132mY,     TB_ALIGN_32 },
1291     { X86::VFMADDSUBPDr132rY,     X86::VFMADDSUBPDr132mY,     TB_ALIGN_32 },
1292     { X86::VFMADDSUBPSr213rY,     X86::VFMADDSUBPSr213mY,     TB_ALIGN_32 },
1293     { X86::VFMADDSUBPDr213rY,     X86::VFMADDSUBPDr213mY,     TB_ALIGN_32 },
1294
1295     { X86::VFMSUBADDPSr231r,      X86::VFMSUBADDPSr231m,      TB_ALIGN_16 },
1296     { X86::VFMSUBADDPDr231r,      X86::VFMSUBADDPDr231m,      TB_ALIGN_16 },
1297     { X86::VFMSUBADDPSr132r,      X86::VFMSUBADDPSr132m,      TB_ALIGN_16 },
1298     { X86::VFMSUBADDPDr132r,      X86::VFMSUBADDPDr132m,      TB_ALIGN_16 },
1299     { X86::VFMSUBADDPSr213r,      X86::VFMSUBADDPSr213m,      TB_ALIGN_16 },
1300     { X86::VFMSUBADDPDr213r,      X86::VFMSUBADDPDr213m,      TB_ALIGN_16 },
1301     { X86::VFMSUBADDPSr231rY,     X86::VFMSUBADDPSr231mY,     TB_ALIGN_32 },
1302     { X86::VFMSUBADDPDr231rY,     X86::VFMSUBADDPDr231mY,     TB_ALIGN_32 },
1303     { X86::VFMSUBADDPSr132rY,     X86::VFMSUBADDPSr132mY,     TB_ALIGN_32 },
1304     { X86::VFMSUBADDPDr132rY,     X86::VFMSUBADDPDr132mY,     TB_ALIGN_32 },
1305     { X86::VFMSUBADDPSr213rY,     X86::VFMSUBADDPSr213mY,     TB_ALIGN_32 },
1306     { X86::VFMSUBADDPDr213rY,     X86::VFMSUBADDPDr213mY,     TB_ALIGN_32 },
1307
1308     // FMA4 foldable patterns
1309     { X86::VFMADDSS4rr,           X86::VFMADDSS4rm,           0           },
1310     { X86::VFMADDSD4rr,           X86::VFMADDSD4rm,           0           },
1311     { X86::VFMADDPS4rr,           X86::VFMADDPS4rm,           TB_ALIGN_16 },
1312     { X86::VFMADDPD4rr,           X86::VFMADDPD4rm,           TB_ALIGN_16 },
1313     { X86::VFMADDPS4rrY,          X86::VFMADDPS4rmY,          TB_ALIGN_32 },
1314     { X86::VFMADDPD4rrY,          X86::VFMADDPD4rmY,          TB_ALIGN_32 },
1315     { X86::VFNMADDSS4rr,          X86::VFNMADDSS4rm,          0           },
1316     { X86::VFNMADDSD4rr,          X86::VFNMADDSD4rm,          0           },
1317     { X86::VFNMADDPS4rr,          X86::VFNMADDPS4rm,          TB_ALIGN_16 },
1318     { X86::VFNMADDPD4rr,          X86::VFNMADDPD4rm,          TB_ALIGN_16 },
1319     { X86::VFNMADDPS4rrY,         X86::VFNMADDPS4rmY,         TB_ALIGN_32 },
1320     { X86::VFNMADDPD4rrY,         X86::VFNMADDPD4rmY,         TB_ALIGN_32 },
1321     { X86::VFMSUBSS4rr,           X86::VFMSUBSS4rm,           0           },
1322     { X86::VFMSUBSD4rr,           X86::VFMSUBSD4rm,           0           },
1323     { X86::VFMSUBPS4rr,           X86::VFMSUBPS4rm,           TB_ALIGN_16 },
1324     { X86::VFMSUBPD4rr,           X86::VFMSUBPD4rm,           TB_ALIGN_16 },
1325     { X86::VFMSUBPS4rrY,          X86::VFMSUBPS4rmY,          TB_ALIGN_32 },
1326     { X86::VFMSUBPD4rrY,          X86::VFMSUBPD4rmY,          TB_ALIGN_32 },
1327     { X86::VFNMSUBSS4rr,          X86::VFNMSUBSS4rm,          0           },
1328     { X86::VFNMSUBSD4rr,          X86::VFNMSUBSD4rm,          0           },
1329     { X86::VFNMSUBPS4rr,          X86::VFNMSUBPS4rm,          TB_ALIGN_16 },
1330     { X86::VFNMSUBPD4rr,          X86::VFNMSUBPD4rm,          TB_ALIGN_16 },
1331     { X86::VFNMSUBPS4rrY,         X86::VFNMSUBPS4rmY,         TB_ALIGN_32 },
1332     { X86::VFNMSUBPD4rrY,         X86::VFNMSUBPD4rmY,         TB_ALIGN_32 },
1333     { X86::VFMADDSUBPS4rr,        X86::VFMADDSUBPS4rm,        TB_ALIGN_16 },
1334     { X86::VFMADDSUBPD4rr,        X86::VFMADDSUBPD4rm,        TB_ALIGN_16 },
1335     { X86::VFMADDSUBPS4rrY,       X86::VFMADDSUBPS4rmY,       TB_ALIGN_32 },
1336     { X86::VFMADDSUBPD4rrY,       X86::VFMADDSUBPD4rmY,       TB_ALIGN_32 },
1337     { X86::VFMSUBADDPS4rr,        X86::VFMSUBADDPS4rm,        TB_ALIGN_16 },
1338     { X86::VFMSUBADDPD4rr,        X86::VFMSUBADDPD4rm,        TB_ALIGN_16 },
1339     { X86::VFMSUBADDPS4rrY,       X86::VFMSUBADDPS4rmY,       TB_ALIGN_32 },
1340     { X86::VFMSUBADDPD4rrY,       X86::VFMSUBADDPD4rmY,       TB_ALIGN_32 },
1341   };
1342
1343   for (unsigned i = 0, e = array_lengthof(OpTbl3); i != e; ++i) {
1344     unsigned RegOp = OpTbl3[i].RegOp;
1345     unsigned MemOp = OpTbl3[i].MemOp;
1346     unsigned Flags = OpTbl3[i].Flags;
1347     AddTableEntry(RegOp2MemOpTable3, MemOp2RegOpTable,
1348                   RegOp, MemOp,
1349                   // Index 3, folded load
1350                   Flags | TB_INDEX_3 | TB_FOLDED_LOAD);
1351   }
1352
1353 }
1354
1355 void
1356 X86InstrInfo::AddTableEntry(RegOp2MemOpTableType &R2MTable,
1357                             MemOp2RegOpTableType &M2RTable,
1358                             unsigned RegOp, unsigned MemOp, unsigned Flags) {
1359     if ((Flags & TB_NO_FORWARD) == 0) {
1360       assert(!R2MTable.count(RegOp) && "Duplicate entry!");
1361       R2MTable[RegOp] = std::make_pair(MemOp, Flags);
1362     }
1363     if ((Flags & TB_NO_REVERSE) == 0) {
1364       assert(!M2RTable.count(MemOp) &&
1365            "Duplicated entries in unfolding maps?");
1366       M2RTable[MemOp] = std::make_pair(RegOp, Flags);
1367     }
1368 }
1369
1370 bool
1371 X86InstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
1372                                     unsigned &SrcReg, unsigned &DstReg,
1373                                     unsigned &SubIdx) const {
1374   switch (MI.getOpcode()) {
1375   default: break;
1376   case X86::MOVSX16rr8:
1377   case X86::MOVZX16rr8:
1378   case X86::MOVSX32rr8:
1379   case X86::MOVZX32rr8:
1380   case X86::MOVSX64rr8:
1381     if (!TM.getSubtarget<X86Subtarget>().is64Bit())
1382       // It's not always legal to reference the low 8-bit of the larger
1383       // register in 32-bit mode.
1384       return false;
1385   case X86::MOVSX32rr16:
1386   case X86::MOVZX32rr16:
1387   case X86::MOVSX64rr16:
1388   case X86::MOVSX64rr32: {
1389     if (MI.getOperand(0).getSubReg() || MI.getOperand(1).getSubReg())
1390       // Be conservative.
1391       return false;
1392     SrcReg = MI.getOperand(1).getReg();
1393     DstReg = MI.getOperand(0).getReg();
1394     switch (MI.getOpcode()) {
1395     default: llvm_unreachable("Unreachable!");
1396     case X86::MOVSX16rr8:
1397     case X86::MOVZX16rr8:
1398     case X86::MOVSX32rr8:
1399     case X86::MOVZX32rr8:
1400     case X86::MOVSX64rr8:
1401       SubIdx = X86::sub_8bit;
1402       break;
1403     case X86::MOVSX32rr16:
1404     case X86::MOVZX32rr16:
1405     case X86::MOVSX64rr16:
1406       SubIdx = X86::sub_16bit;
1407       break;
1408     case X86::MOVSX64rr32:
1409       SubIdx = X86::sub_32bit;
1410       break;
1411     }
1412     return true;
1413   }
1414   }
1415   return false;
1416 }
1417
1418 /// isFrameOperand - Return true and the FrameIndex if the specified
1419 /// operand and follow operands form a reference to the stack frame.
1420 bool X86InstrInfo::isFrameOperand(const MachineInstr *MI, unsigned int Op,
1421                                   int &FrameIndex) const {
1422   if (MI->getOperand(Op).isFI() && MI->getOperand(Op+1).isImm() &&
1423       MI->getOperand(Op+2).isReg() && MI->getOperand(Op+3).isImm() &&
1424       MI->getOperand(Op+1).getImm() == 1 &&
1425       MI->getOperand(Op+2).getReg() == 0 &&
1426       MI->getOperand(Op+3).getImm() == 0) {
1427     FrameIndex = MI->getOperand(Op).getIndex();
1428     return true;
1429   }
1430   return false;
1431 }
1432
1433 static bool isFrameLoadOpcode(int Opcode) {
1434   switch (Opcode) {
1435   default:
1436     return false;
1437   case X86::MOV8rm:
1438   case X86::MOV16rm:
1439   case X86::MOV32rm:
1440   case X86::MOV64rm:
1441   case X86::LD_Fp64m:
1442   case X86::MOVSSrm:
1443   case X86::MOVSDrm:
1444   case X86::MOVAPSrm:
1445   case X86::MOVAPDrm:
1446   case X86::MOVDQArm:
1447   case X86::VMOVSSrm:
1448   case X86::VMOVSDrm:
1449   case X86::VMOVAPSrm:
1450   case X86::VMOVAPDrm:
1451   case X86::VMOVDQArm:
1452   case X86::VMOVAPSYrm:
1453   case X86::VMOVAPDYrm:
1454   case X86::VMOVDQAYrm:
1455   case X86::MMX_MOVD64rm:
1456   case X86::MMX_MOVQ64rm:
1457     return true;
1458   }
1459 }
1460
1461 static bool isFrameStoreOpcode(int Opcode) {
1462   switch (Opcode) {
1463   default: break;
1464   case X86::MOV8mr:
1465   case X86::MOV16mr:
1466   case X86::MOV32mr:
1467   case X86::MOV64mr:
1468   case X86::ST_FpP64m:
1469   case X86::MOVSSmr:
1470   case X86::MOVSDmr:
1471   case X86::MOVAPSmr:
1472   case X86::MOVAPDmr:
1473   case X86::MOVDQAmr:
1474   case X86::VMOVSSmr:
1475   case X86::VMOVSDmr:
1476   case X86::VMOVAPSmr:
1477   case X86::VMOVAPDmr:
1478   case X86::VMOVDQAmr:
1479   case X86::VMOVAPSYmr:
1480   case X86::VMOVAPDYmr:
1481   case X86::VMOVDQAYmr:
1482   case X86::MMX_MOVD64mr:
1483   case X86::MMX_MOVQ64mr:
1484   case X86::MMX_MOVNTQmr:
1485     return true;
1486   }
1487   return false;
1488 }
1489
1490 unsigned X86InstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
1491                                            int &FrameIndex) const {
1492   if (isFrameLoadOpcode(MI->getOpcode()))
1493     if (MI->getOperand(0).getSubReg() == 0 && isFrameOperand(MI, 1, FrameIndex))
1494       return MI->getOperand(0).getReg();
1495   return 0;
1496 }
1497
1498 unsigned X86InstrInfo::isLoadFromStackSlotPostFE(const MachineInstr *MI,
1499                                                  int &FrameIndex) const {
1500   if (isFrameLoadOpcode(MI->getOpcode())) {
1501     unsigned Reg;
1502     if ((Reg = isLoadFromStackSlot(MI, FrameIndex)))
1503       return Reg;
1504     // Check for post-frame index elimination operations
1505     const MachineMemOperand *Dummy;
1506     return hasLoadFromStackSlot(MI, Dummy, FrameIndex);
1507   }
1508   return 0;
1509 }
1510
1511 unsigned X86InstrInfo::isStoreToStackSlot(const MachineInstr *MI,
1512                                           int &FrameIndex) const {
1513   if (isFrameStoreOpcode(MI->getOpcode()))
1514     if (MI->getOperand(X86::AddrNumOperands).getSubReg() == 0 &&
1515         isFrameOperand(MI, 0, FrameIndex))
1516       return MI->getOperand(X86::AddrNumOperands).getReg();
1517   return 0;
1518 }
1519
1520 unsigned X86InstrInfo::isStoreToStackSlotPostFE(const MachineInstr *MI,
1521                                                 int &FrameIndex) const {
1522   if (isFrameStoreOpcode(MI->getOpcode())) {
1523     unsigned Reg;
1524     if ((Reg = isStoreToStackSlot(MI, FrameIndex)))
1525       return Reg;
1526     // Check for post-frame index elimination operations
1527     const MachineMemOperand *Dummy;
1528     return hasStoreToStackSlot(MI, Dummy, FrameIndex);
1529   }
1530   return 0;
1531 }
1532
1533 /// regIsPICBase - Return true if register is PIC base (i.e.g defined by
1534 /// X86::MOVPC32r.
1535 static bool regIsPICBase(unsigned BaseReg, const MachineRegisterInfo &MRI) {
1536   // Don't waste compile time scanning use-def chains of physregs.
1537   if (!TargetRegisterInfo::isVirtualRegister(BaseReg))
1538     return false;
1539   bool isPICBase = false;
1540   for (MachineRegisterInfo::def_iterator I = MRI.def_begin(BaseReg),
1541          E = MRI.def_end(); I != E; ++I) {
1542     MachineInstr *DefMI = I.getOperand().getParent();
1543     if (DefMI->getOpcode() != X86::MOVPC32r)
1544       return false;
1545     assert(!isPICBase && "More than one PIC base?");
1546     isPICBase = true;
1547   }
1548   return isPICBase;
1549 }
1550
1551 bool
1552 X86InstrInfo::isReallyTriviallyReMaterializable(const MachineInstr *MI,
1553                                                 AliasAnalysis *AA) const {
1554   switch (MI->getOpcode()) {
1555   default: break;
1556   case X86::MOV8rm:
1557   case X86::MOV16rm:
1558   case X86::MOV32rm:
1559   case X86::MOV64rm:
1560   case X86::LD_Fp64m:
1561   case X86::MOVSSrm:
1562   case X86::MOVSDrm:
1563   case X86::MOVAPSrm:
1564   case X86::MOVUPSrm:
1565   case X86::MOVAPDrm:
1566   case X86::MOVDQArm:
1567   case X86::MOVDQUrm:
1568   case X86::VMOVSSrm:
1569   case X86::VMOVSDrm:
1570   case X86::VMOVAPSrm:
1571   case X86::VMOVUPSrm:
1572   case X86::VMOVAPDrm:
1573   case X86::VMOVDQArm:
1574   case X86::VMOVDQUrm:
1575   case X86::VMOVAPSYrm:
1576   case X86::VMOVUPSYrm:
1577   case X86::VMOVAPDYrm:
1578   case X86::VMOVDQAYrm:
1579   case X86::VMOVDQUYrm:
1580   case X86::MMX_MOVD64rm:
1581   case X86::MMX_MOVQ64rm:
1582   case X86::FsVMOVAPSrm:
1583   case X86::FsVMOVAPDrm:
1584   case X86::FsMOVAPSrm:
1585   case X86::FsMOVAPDrm: {
1586     // Loads from constant pools are trivially rematerializable.
1587     if (MI->getOperand(1).isReg() &&
1588         MI->getOperand(2).isImm() &&
1589         MI->getOperand(3).isReg() && MI->getOperand(3).getReg() == 0 &&
1590         MI->isInvariantLoad(AA)) {
1591       unsigned BaseReg = MI->getOperand(1).getReg();
1592       if (BaseReg == 0 || BaseReg == X86::RIP)
1593         return true;
1594       // Allow re-materialization of PIC load.
1595       if (!ReMatPICStubLoad && MI->getOperand(4).isGlobal())
1596         return false;
1597       const MachineFunction &MF = *MI->getParent()->getParent();
1598       const MachineRegisterInfo &MRI = MF.getRegInfo();
1599       return regIsPICBase(BaseReg, MRI);
1600     }
1601     return false;
1602   }
1603
1604   case X86::LEA32r:
1605   case X86::LEA64r: {
1606     if (MI->getOperand(2).isImm() &&
1607         MI->getOperand(3).isReg() && MI->getOperand(3).getReg() == 0 &&
1608         !MI->getOperand(4).isReg()) {
1609       // lea fi#, lea GV, etc. are all rematerializable.
1610       if (!MI->getOperand(1).isReg())
1611         return true;
1612       unsigned BaseReg = MI->getOperand(1).getReg();
1613       if (BaseReg == 0)
1614         return true;
1615       // Allow re-materialization of lea PICBase + x.
1616       const MachineFunction &MF = *MI->getParent()->getParent();
1617       const MachineRegisterInfo &MRI = MF.getRegInfo();
1618       return regIsPICBase(BaseReg, MRI);
1619     }
1620     return false;
1621   }
1622   }
1623
1624   // All other instructions marked M_REMATERIALIZABLE are always trivially
1625   // rematerializable.
1626   return true;
1627 }
1628
1629 /// isSafeToClobberEFLAGS - Return true if it's safe insert an instruction that
1630 /// would clobber the EFLAGS condition register. Note the result may be
1631 /// conservative. If it cannot definitely determine the safety after visiting
1632 /// a few instructions in each direction it assumes it's not safe.
1633 static bool isSafeToClobberEFLAGS(MachineBasicBlock &MBB,
1634                                   MachineBasicBlock::iterator I) {
1635   MachineBasicBlock::iterator E = MBB.end();
1636
1637   // For compile time consideration, if we are not able to determine the
1638   // safety after visiting 4 instructions in each direction, we will assume
1639   // it's not safe.
1640   MachineBasicBlock::iterator Iter = I;
1641   for (unsigned i = 0; Iter != E && i < 4; ++i) {
1642     bool SeenDef = false;
1643     for (unsigned j = 0, e = Iter->getNumOperands(); j != e; ++j) {
1644       MachineOperand &MO = Iter->getOperand(j);
1645       if (MO.isRegMask() && MO.clobbersPhysReg(X86::EFLAGS))
1646         SeenDef = true;
1647       if (!MO.isReg())
1648         continue;
1649       if (MO.getReg() == X86::EFLAGS) {
1650         if (MO.isUse())
1651           return false;
1652         SeenDef = true;
1653       }
1654     }
1655
1656     if (SeenDef)
1657       // This instruction defines EFLAGS, no need to look any further.
1658       return true;
1659     ++Iter;
1660     // Skip over DBG_VALUE.
1661     while (Iter != E && Iter->isDebugValue())
1662       ++Iter;
1663   }
1664
1665   // It is safe to clobber EFLAGS at the end of a block of no successor has it
1666   // live in.
1667   if (Iter == E) {
1668     for (MachineBasicBlock::succ_iterator SI = MBB.succ_begin(),
1669            SE = MBB.succ_end(); SI != SE; ++SI)
1670       if ((*SI)->isLiveIn(X86::EFLAGS))
1671         return false;
1672     return true;
1673   }
1674
1675   MachineBasicBlock::iterator B = MBB.begin();
1676   Iter = I;
1677   for (unsigned i = 0; i < 4; ++i) {
1678     // If we make it to the beginning of the block, it's safe to clobber
1679     // EFLAGS iff EFLAGS is not live-in.
1680     if (Iter == B)
1681       return !MBB.isLiveIn(X86::EFLAGS);
1682
1683     --Iter;
1684     // Skip over DBG_VALUE.
1685     while (Iter != B && Iter->isDebugValue())
1686       --Iter;
1687
1688     bool SawKill = false;
1689     for (unsigned j = 0, e = Iter->getNumOperands(); j != e; ++j) {
1690       MachineOperand &MO = Iter->getOperand(j);
1691       // A register mask may clobber EFLAGS, but we should still look for a
1692       // live EFLAGS def.
1693       if (MO.isRegMask() && MO.clobbersPhysReg(X86::EFLAGS))
1694         SawKill = true;
1695       if (MO.isReg() && MO.getReg() == X86::EFLAGS) {
1696         if (MO.isDef()) return MO.isDead();
1697         if (MO.isKill()) SawKill = true;
1698       }
1699     }
1700
1701     if (SawKill)
1702       // This instruction kills EFLAGS and doesn't redefine it, so
1703       // there's no need to look further.
1704       return true;
1705   }
1706
1707   // Conservative answer.
1708   return false;
1709 }
1710
1711 void X86InstrInfo::reMaterialize(MachineBasicBlock &MBB,
1712                                  MachineBasicBlock::iterator I,
1713                                  unsigned DestReg, unsigned SubIdx,
1714                                  const MachineInstr *Orig,
1715                                  const TargetRegisterInfo &TRI) const {
1716   // MOV32r0 is implemented with a xor which clobbers condition code.
1717   // Re-materialize it as movri instructions to avoid side effects.
1718   unsigned Opc = Orig->getOpcode();
1719   if (Opc == X86::MOV32r0 && !isSafeToClobberEFLAGS(MBB, I)) {
1720     DebugLoc DL = Orig->getDebugLoc();
1721     BuildMI(MBB, I, DL, get(X86::MOV32ri)).addOperand(Orig->getOperand(0))
1722       .addImm(0);
1723   } else {
1724     MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
1725     MBB.insert(I, MI);
1726   }
1727
1728   MachineInstr *NewMI = prior(I);
1729   NewMI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI);
1730 }
1731
1732 /// hasLiveCondCodeDef - True if MI has a condition code def, e.g. EFLAGS, that
1733 /// is not marked dead.
1734 static bool hasLiveCondCodeDef(MachineInstr *MI) {
1735   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1736     MachineOperand &MO = MI->getOperand(i);
1737     if (MO.isReg() && MO.isDef() &&
1738         MO.getReg() == X86::EFLAGS && !MO.isDead()) {
1739       return true;
1740     }
1741   }
1742   return false;
1743 }
1744
1745 /// getTruncatedShiftCount - check whether the shift count for a machine operand
1746 /// is non-zero.
1747 inline static unsigned getTruncatedShiftCount(MachineInstr *MI,
1748                                               unsigned ShiftAmtOperandIdx) {
1749   // The shift count is six bits with the REX.W prefix and five bits without.
1750   unsigned ShiftCountMask = (MI->getDesc().TSFlags & X86II::REX_W) ? 63 : 31;
1751   unsigned Imm = MI->getOperand(ShiftAmtOperandIdx).getImm();
1752   return Imm & ShiftCountMask;
1753 }
1754
1755 /// isTruncatedShiftCountForLEA - check whether the given shift count is appropriate
1756 /// can be represented by a LEA instruction.
1757 inline static bool isTruncatedShiftCountForLEA(unsigned ShAmt) {
1758   // Left shift instructions can be transformed into load-effective-address
1759   // instructions if we can encode them appropriately.
1760   // A LEA instruction utilizes a SIB byte to encode it's scale factor.
1761   // The SIB.scale field is two bits wide which means that we can encode any
1762   // shift amount less than 4.
1763   return ShAmt < 4 && ShAmt > 0;
1764 }
1765
1766 /// convertToThreeAddressWithLEA - Helper for convertToThreeAddress when
1767 /// 16-bit LEA is disabled, use 32-bit LEA to form 3-address code by promoting
1768 /// to a 32-bit superregister and then truncating back down to a 16-bit
1769 /// subregister.
1770 MachineInstr *
1771 X86InstrInfo::convertToThreeAddressWithLEA(unsigned MIOpc,
1772                                            MachineFunction::iterator &MFI,
1773                                            MachineBasicBlock::iterator &MBBI,
1774                                            LiveVariables *LV) const {
1775   MachineInstr *MI = MBBI;
1776   unsigned Dest = MI->getOperand(0).getReg();
1777   unsigned Src = MI->getOperand(1).getReg();
1778   bool isDead = MI->getOperand(0).isDead();
1779   bool isKill = MI->getOperand(1).isKill();
1780
1781   MachineRegisterInfo &RegInfo = MFI->getParent()->getRegInfo();
1782   unsigned leaOutReg = RegInfo.createVirtualRegister(&X86::GR32RegClass);
1783   unsigned Opc, leaInReg;
1784   if (TM.getSubtarget<X86Subtarget>().is64Bit()) {
1785     Opc = X86::LEA64_32r;
1786     leaInReg = RegInfo.createVirtualRegister(&X86::GR64_NOSPRegClass);
1787   } else {
1788     Opc = X86::LEA32r;
1789     leaInReg = RegInfo.createVirtualRegister(&X86::GR32_NOSPRegClass);
1790   }
1791
1792   // Build and insert into an implicit UNDEF value. This is OK because
1793   // well be shifting and then extracting the lower 16-bits.
1794   // This has the potential to cause partial register stall. e.g.
1795   //   movw    (%rbp,%rcx,2), %dx
1796   //   leal    -65(%rdx), %esi
1797   // But testing has shown this *does* help performance in 64-bit mode (at
1798   // least on modern x86 machines).
1799   BuildMI(*MFI, MBBI, MI->getDebugLoc(), get(X86::IMPLICIT_DEF), leaInReg);
1800   MachineInstr *InsMI =
1801     BuildMI(*MFI, MBBI, MI->getDebugLoc(), get(TargetOpcode::COPY))
1802     .addReg(leaInReg, RegState::Define, X86::sub_16bit)
1803     .addReg(Src, getKillRegState(isKill));
1804
1805   MachineInstrBuilder MIB = BuildMI(*MFI, MBBI, MI->getDebugLoc(),
1806                                     get(Opc), leaOutReg);
1807   switch (MIOpc) {
1808   default: llvm_unreachable("Unreachable!");
1809   case X86::SHL16ri: {
1810     unsigned ShAmt = MI->getOperand(2).getImm();
1811     MIB.addReg(0).addImm(1 << ShAmt)
1812        .addReg(leaInReg, RegState::Kill).addImm(0).addReg(0);
1813     break;
1814   }
1815   case X86::INC16r:
1816   case X86::INC64_16r:
1817     addRegOffset(MIB, leaInReg, true, 1);
1818     break;
1819   case X86::DEC16r:
1820   case X86::DEC64_16r:
1821     addRegOffset(MIB, leaInReg, true, -1);
1822     break;
1823   case X86::ADD16ri:
1824   case X86::ADD16ri8:
1825   case X86::ADD16ri_DB:
1826   case X86::ADD16ri8_DB:
1827     addRegOffset(MIB, leaInReg, true, MI->getOperand(2).getImm());
1828     break;
1829   case X86::ADD16rr:
1830   case X86::ADD16rr_DB: {
1831     unsigned Src2 = MI->getOperand(2).getReg();
1832     bool isKill2 = MI->getOperand(2).isKill();
1833     unsigned leaInReg2 = 0;
1834     MachineInstr *InsMI2 = 0;
1835     if (Src == Src2) {
1836       // ADD16rr %reg1028<kill>, %reg1028
1837       // just a single insert_subreg.
1838       addRegReg(MIB, leaInReg, true, leaInReg, false);
1839     } else {
1840       if (TM.getSubtarget<X86Subtarget>().is64Bit())
1841         leaInReg2 = RegInfo.createVirtualRegister(&X86::GR64_NOSPRegClass);
1842       else
1843         leaInReg2 = RegInfo.createVirtualRegister(&X86::GR32_NOSPRegClass);
1844       // Build and insert into an implicit UNDEF value. This is OK because
1845       // well be shifting and then extracting the lower 16-bits.
1846       BuildMI(*MFI, &*MIB, MI->getDebugLoc(), get(X86::IMPLICIT_DEF),leaInReg2);
1847       InsMI2 =
1848         BuildMI(*MFI, &*MIB, MI->getDebugLoc(), get(TargetOpcode::COPY))
1849         .addReg(leaInReg2, RegState::Define, X86::sub_16bit)
1850         .addReg(Src2, getKillRegState(isKill2));
1851       addRegReg(MIB, leaInReg, true, leaInReg2, true);
1852     }
1853     if (LV && isKill2 && InsMI2)
1854       LV->replaceKillInstruction(Src2, MI, InsMI2);
1855     break;
1856   }
1857   }
1858
1859   MachineInstr *NewMI = MIB;
1860   MachineInstr *ExtMI =
1861     BuildMI(*MFI, MBBI, MI->getDebugLoc(), get(TargetOpcode::COPY))
1862     .addReg(Dest, RegState::Define | getDeadRegState(isDead))
1863     .addReg(leaOutReg, RegState::Kill, X86::sub_16bit);
1864
1865   if (LV) {
1866     // Update live variables
1867     LV->getVarInfo(leaInReg).Kills.push_back(NewMI);
1868     LV->getVarInfo(leaOutReg).Kills.push_back(ExtMI);
1869     if (isKill)
1870       LV->replaceKillInstruction(Src, MI, InsMI);
1871     if (isDead)
1872       LV->replaceKillInstruction(Dest, MI, ExtMI);
1873   }
1874
1875   return ExtMI;
1876 }
1877
1878 /// convertToThreeAddress - This method must be implemented by targets that
1879 /// set the M_CONVERTIBLE_TO_3_ADDR flag.  When this flag is set, the target
1880 /// may be able to convert a two-address instruction into a true
1881 /// three-address instruction on demand.  This allows the X86 target (for
1882 /// example) to convert ADD and SHL instructions into LEA instructions if they
1883 /// would require register copies due to two-addressness.
1884 ///
1885 /// This method returns a null pointer if the transformation cannot be
1886 /// performed, otherwise it returns the new instruction.
1887 ///
1888 MachineInstr *
1889 X86InstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
1890                                     MachineBasicBlock::iterator &MBBI,
1891                                     LiveVariables *LV) const {
1892   MachineInstr *MI = MBBI;
1893
1894   // The following opcodes also sets the condition code register(s). Only
1895   // convert them to equivalent lea if the condition code register def's
1896   // are dead!
1897   if (hasLiveCondCodeDef(MI))
1898     return 0;
1899
1900   MachineFunction &MF = *MI->getParent()->getParent();
1901   // All instructions input are two-addr instructions.  Get the known operands.
1902   const MachineOperand &Dest = MI->getOperand(0);
1903   const MachineOperand &Src = MI->getOperand(1);
1904
1905   MachineInstr *NewMI = NULL;
1906   // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's.  When
1907   // we have better subtarget support, enable the 16-bit LEA generation here.
1908   // 16-bit LEA is also slow on Core2.
1909   bool DisableLEA16 = true;
1910   bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
1911
1912   unsigned MIOpc = MI->getOpcode();
1913   switch (MIOpc) {
1914   case X86::SHUFPSrri: {
1915     assert(MI->getNumOperands() == 4 && "Unknown shufps instruction!");
1916     if (!TM.getSubtarget<X86Subtarget>().hasSSE2()) return 0;
1917
1918     unsigned B = MI->getOperand(1).getReg();
1919     unsigned C = MI->getOperand(2).getReg();
1920     if (B != C) return 0;
1921     unsigned M = MI->getOperand(3).getImm();
1922     NewMI = BuildMI(MF, MI->getDebugLoc(), get(X86::PSHUFDri))
1923       .addOperand(Dest).addOperand(Src).addImm(M);
1924     break;
1925   }
1926   case X86::SHUFPDrri: {
1927     assert(MI->getNumOperands() == 4 && "Unknown shufpd instruction!");
1928     if (!TM.getSubtarget<X86Subtarget>().hasSSE2()) return 0;
1929
1930     unsigned B = MI->getOperand(1).getReg();
1931     unsigned C = MI->getOperand(2).getReg();
1932     if (B != C) return 0;
1933     unsigned M = MI->getOperand(3).getImm();
1934
1935     // Convert to PSHUFD mask.
1936     M = ((M & 1) << 1) | ((M & 1) << 3) | ((M & 2) << 4) | ((M & 2) << 6)| 0x44;
1937
1938     NewMI = BuildMI(MF, MI->getDebugLoc(), get(X86::PSHUFDri))
1939       .addOperand(Dest).addOperand(Src).addImm(M);
1940     break;
1941   }
1942   case X86::SHL64ri: {
1943     assert(MI->getNumOperands() >= 3 && "Unknown shift instruction!");
1944     unsigned ShAmt = getTruncatedShiftCount(MI, 2);
1945     if (!isTruncatedShiftCountForLEA(ShAmt)) return 0;
1946
1947     // LEA can't handle RSP.
1948     if (TargetRegisterInfo::isVirtualRegister(Src.getReg()) &&
1949         !MF.getRegInfo().constrainRegClass(Src.getReg(),
1950                                            &X86::GR64_NOSPRegClass))
1951       return 0;
1952
1953     NewMI = BuildMI(MF, MI->getDebugLoc(), get(X86::LEA64r))
1954       .addOperand(Dest)
1955       .addReg(0).addImm(1 << ShAmt).addOperand(Src).addImm(0).addReg(0);
1956     break;
1957   }
1958   case X86::SHL32ri: {
1959     assert(MI->getNumOperands() >= 3 && "Unknown shift instruction!");
1960     unsigned ShAmt = getTruncatedShiftCount(MI, 2);
1961     if (!isTruncatedShiftCountForLEA(ShAmt)) return 0;
1962
1963     // LEA can't handle ESP.
1964     bool isKill = Src.isKill();
1965     unsigned SrcReg = Src.getReg();
1966     if (is64Bit) {
1967       unsigned NewSrc = MF.getRegInfo().createVirtualRegister(&X86::GR64_NOSPRegClass);
1968       BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), get(TargetOpcode::COPY))
1969         .addReg(NewSrc, RegState::Define | RegState::Undef, X86::sub_32bit)
1970         .addOperand(Src);
1971
1972       SrcReg = NewSrc;
1973       isKill = true;
1974     } else if (TargetRegisterInfo::isVirtualRegister(SrcReg) &&
1975                !MF.getRegInfo().constrainRegClass(SrcReg,
1976                                                   &X86::GR32_NOSPRegClass)) {
1977       return 0;
1978     }
1979
1980     unsigned Opc = is64Bit ? X86::LEA64_32r : X86::LEA32r;
1981     NewMI = BuildMI(MF, MI->getDebugLoc(), get(Opc))
1982       .addOperand(Dest)
1983       .addReg(0).addImm(1 << ShAmt)
1984       .addReg(SrcReg, getKillRegState(isKill)).addImm(0).addReg(0);
1985     break;
1986   }
1987   case X86::SHL16ri: {
1988     assert(MI->getNumOperands() >= 3 && "Unknown shift instruction!");
1989     unsigned ShAmt = getTruncatedShiftCount(MI, 2);
1990     if (!isTruncatedShiftCountForLEA(ShAmt)) return 0;
1991
1992     if (DisableLEA16)
1993       return is64Bit ? convertToThreeAddressWithLEA(MIOpc, MFI, MBBI, LV) : 0;
1994     NewMI = BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
1995       .addOperand(Dest)
1996       .addReg(0).addImm(1 << ShAmt).addOperand(Src).addImm(0).addReg(0);
1997     break;
1998   }
1999   default: {
2000
2001     switch (MIOpc) {
2002     default: return 0;
2003     case X86::INC64r:
2004     case X86::INC32r:
2005     case X86::INC64_32r: {
2006       assert(MI->getNumOperands() >= 2 && "Unknown inc instruction!");
2007       unsigned Opc = MIOpc == X86::INC64r ? X86::LEA64r
2008         : (is64Bit ? X86::LEA64_32r : X86::LEA32r);
2009       const TargetRegisterClass *RC = is64Bit ?
2010         (const TargetRegisterClass*)&X86::GR64_NOSPRegClass :
2011         (const TargetRegisterClass*)&X86::GR32_NOSPRegClass;
2012
2013       // LEA can't handle RSP.
2014       bool isKill = Src.isKill();
2015       unsigned SrcReg = Src.getReg();
2016       if (Opc == X86::LEA64_32r) {
2017         unsigned NewSrc = MF.getRegInfo().createVirtualRegister(&X86::GR64_NOSPRegClass);
2018         BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
2019                 get(TargetOpcode::COPY))
2020             .addReg(NewSrc, RegState::Define | RegState::Undef, X86::sub_32bit)
2021             .addOperand(Src);
2022
2023         SrcReg = NewSrc;
2024         isKill = true;
2025       } else if (TargetRegisterInfo::isVirtualRegister(SrcReg) &&
2026                  !MF.getRegInfo().constrainRegClass(SrcReg, RC))
2027         return 0;
2028
2029       NewMI = addOffset(BuildMI(MF, MI->getDebugLoc(), get(Opc))
2030                   .addOperand(Dest).addReg(SrcReg, getKillRegState(isKill)), 1);
2031       break;
2032     }
2033     case X86::INC16r:
2034     case X86::INC64_16r:
2035       if (DisableLEA16)
2036         return is64Bit ? convertToThreeAddressWithLEA(MIOpc, MFI, MBBI, LV) : 0;
2037       assert(MI->getNumOperands() >= 2 && "Unknown inc instruction!");
2038       NewMI = addOffset(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
2039                         .addOperand(Dest).addOperand(Src), 1);
2040       break;
2041     case X86::DEC64r:
2042     case X86::DEC32r:
2043     case X86::DEC64_32r: {
2044       assert(MI->getNumOperands() >= 2 && "Unknown dec instruction!");
2045       unsigned Opc = MIOpc == X86::DEC64r ? X86::LEA64r
2046         : (is64Bit ? X86::LEA64_32r : X86::LEA32r);
2047       const TargetRegisterClass *RC = is64Bit ?
2048         (const TargetRegisterClass*)&X86::GR64_NOSPRegClass :
2049         (const TargetRegisterClass*)&X86::GR32_NOSPRegClass;
2050       // LEA can't handle RSP.
2051       bool isKill = Src.isKill();
2052       unsigned SrcReg = Src.getReg();
2053       if (Opc == X86::LEA64_32r) {
2054         unsigned NewSrc =
2055             MF.getRegInfo().createVirtualRegister(&X86::GR64_NOSPRegClass);
2056         BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
2057                 get(TargetOpcode::COPY))
2058             .addReg(NewSrc, RegState::Define | RegState::Undef, X86::sub_32bit)
2059             .addOperand(Src);
2060
2061         SrcReg = NewSrc;
2062         isKill = true;
2063       } else if (TargetRegisterInfo::isVirtualRegister(SrcReg) &&
2064           !MF.getRegInfo().constrainRegClass(SrcReg, RC))
2065         return 0;
2066
2067       NewMI = addOffset(BuildMI(MF, MI->getDebugLoc(), get(Opc))
2068                  .addOperand(Dest).addReg(SrcReg, getKillRegState(isKill)), -1);
2069       break;
2070     }
2071     case X86::DEC16r:
2072     case X86::DEC64_16r:
2073       if (DisableLEA16)
2074         return is64Bit ? convertToThreeAddressWithLEA(MIOpc, MFI, MBBI, LV) : 0;
2075       assert(MI->getNumOperands() >= 2 && "Unknown dec instruction!");
2076       NewMI = addOffset(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
2077                         .addOperand(Dest).addOperand(Src), -1);
2078       break;
2079     case X86::ADD64rr:
2080     case X86::ADD64rr_DB:
2081     case X86::ADD32rr:
2082     case X86::ADD32rr_DB: {
2083       assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
2084       unsigned Opc;
2085       const TargetRegisterClass *RC;
2086       if (MIOpc == X86::ADD64rr || MIOpc == X86::ADD64rr_DB) {
2087         Opc = X86::LEA64r;
2088         RC = &X86::GR64_NOSPRegClass;
2089       } else {
2090         Opc = is64Bit ? X86::LEA64_32r : X86::LEA32r;
2091         RC = is64Bit ? &X86::GR64_NOSPRegClass : &X86::GR32_NOSPRegClass;
2092       }
2093
2094       unsigned SrcReg = Src.getReg();
2095       unsigned isKill = Src.isKill();
2096       const MachineOperand &Src2 = MI->getOperand(2);
2097       unsigned Src2Reg = Src2.getReg();
2098       bool isKill2 = Src2.isKill();
2099
2100       // LEA can't handle RSP.
2101       if (Opc == X86::LEA64_32r) {
2102         MachineBasicBlock &MBB = *MI->getParent();
2103
2104         unsigned NewSrc = MF.getRegInfo().createVirtualRegister(RC);
2105         BuildMI(MBB, MI, MI->getDebugLoc(), get(TargetOpcode::COPY))
2106             .addReg(NewSrc, RegState::Define | RegState::Undef, X86::sub_32bit)
2107             .addOperand(Src);
2108         SrcReg = NewSrc;
2109         isKill = true;
2110
2111         NewSrc = MF.getRegInfo().createVirtualRegister(RC);
2112         BuildMI(MBB, MI, MI->getDebugLoc(), get(TargetOpcode::COPY))
2113             .addReg(NewSrc, RegState::Define | RegState::Undef, X86::sub_32bit)
2114             .addOperand(Src2);
2115         Src2Reg = NewSrc;
2116         isKill2 = true;
2117       } else if (TargetRegisterInfo::isVirtualRegister(Src2Reg) &&
2118                  !MF.getRegInfo().constrainRegClass(Src2Reg, RC))
2119         return 0;
2120
2121       NewMI = addRegReg(BuildMI(MF, MI->getDebugLoc(), get(Opc))
2122                         .addOperand(Dest),
2123                         SrcReg, isKill, Src2Reg, isKill2);
2124
2125       // Preserve undefness of the operands.
2126       if (!is64Bit) {
2127         bool isUndef = MI->getOperand(1).isUndef();
2128         bool isUndef2 = MI->getOperand(2).isUndef();
2129         NewMI->getOperand(1).setIsUndef(isUndef);
2130         NewMI->getOperand(3).setIsUndef(isUndef2);
2131       }
2132
2133       if (LV && Src2.isKill())
2134         LV->replaceKillInstruction(Src2Reg, MI, NewMI);
2135       break;
2136     }
2137     case X86::ADD16rr:
2138     case X86::ADD16rr_DB: {
2139       if (DisableLEA16)
2140         return is64Bit ? convertToThreeAddressWithLEA(MIOpc, MFI, MBBI, LV) : 0;
2141       assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
2142       unsigned Src2 = MI->getOperand(2).getReg();
2143       bool isKill2 = MI->getOperand(2).isKill();
2144       NewMI = addRegReg(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
2145                         .addOperand(Dest),
2146                         Src.getReg(), Src.isKill(), Src2, isKill2);
2147
2148       // Preserve undefness of the operands.
2149       bool isUndef = MI->getOperand(1).isUndef();
2150       bool isUndef2 = MI->getOperand(2).isUndef();
2151       NewMI->getOperand(1).setIsUndef(isUndef);
2152       NewMI->getOperand(3).setIsUndef(isUndef2);
2153
2154       if (LV && isKill2)
2155         LV->replaceKillInstruction(Src2, MI, NewMI);
2156       break;
2157     }
2158     case X86::ADD64ri32:
2159     case X86::ADD64ri8:
2160     case X86::ADD64ri32_DB:
2161     case X86::ADD64ri8_DB:
2162       assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
2163       NewMI = addOffset(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA64r))
2164                         .addOperand(Dest).addOperand(Src),
2165                         MI->getOperand(2).getImm());
2166       break;
2167     case X86::ADD32ri:
2168     case X86::ADD32ri8:
2169     case X86::ADD32ri_DB:
2170     case X86::ADD32ri8_DB: {
2171       assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
2172       if (is64Bit) {
2173         unsigned NewSrc =
2174             MF.getRegInfo().createVirtualRegister(&X86::GR64_NOSPRegClass);
2175         BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
2176                 get(TargetOpcode::COPY))
2177             .addReg(NewSrc, RegState::Define | RegState::Undef, X86::sub_32bit)
2178             .addOperand(Src);
2179
2180         NewMI = addOffset(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA64_32r))
2181                               .addOperand(Dest)
2182                               .addReg(NewSrc, getKillRegState(true)),
2183                           MI->getOperand(2).getImm());
2184       } else {
2185         NewMI = addOffset(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA32r))
2186                               .addOperand(Dest).addOperand(Src),
2187                           MI->getOperand(2).getImm());
2188       }
2189
2190       break;
2191     }
2192     case X86::ADD16ri:
2193     case X86::ADD16ri8:
2194     case X86::ADD16ri_DB:
2195     case X86::ADD16ri8_DB:
2196       if (DisableLEA16)
2197         return is64Bit ? convertToThreeAddressWithLEA(MIOpc, MFI, MBBI, LV) : 0;
2198       assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
2199       NewMI = addOffset(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
2200                         .addOperand(Dest).addOperand(Src),
2201                         MI->getOperand(2).getImm());
2202       break;
2203     }
2204   }
2205   }
2206
2207   if (!NewMI) return 0;
2208
2209   if (LV) {  // Update live variables
2210     if (Src.isKill())
2211       LV->replaceKillInstruction(Src.getReg(), MI, NewMI);
2212     if (Dest.isDead())
2213       LV->replaceKillInstruction(Dest.getReg(), MI, NewMI);
2214   }
2215
2216   MFI->insert(MBBI, NewMI);          // Insert the new inst
2217   return NewMI;
2218 }
2219
2220 /// commuteInstruction - We have a few instructions that must be hacked on to
2221 /// commute them.
2222 ///
2223 MachineInstr *
2224 X86InstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
2225   switch (MI->getOpcode()) {
2226   case X86::SHRD16rri8: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I)
2227   case X86::SHLD16rri8: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I)
2228   case X86::SHRD32rri8: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I)
2229   case X86::SHLD32rri8: // A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I)
2230   case X86::SHRD64rri8: // A = SHRD64rri8 B, C, I -> A = SHLD64rri8 C, B, (64-I)
2231   case X86::SHLD64rri8:{// A = SHLD64rri8 B, C, I -> A = SHRD64rri8 C, B, (64-I)
2232     unsigned Opc;
2233     unsigned Size;
2234     switch (MI->getOpcode()) {
2235     default: llvm_unreachable("Unreachable!");
2236     case X86::SHRD16rri8: Size = 16; Opc = X86::SHLD16rri8; break;
2237     case X86::SHLD16rri8: Size = 16; Opc = X86::SHRD16rri8; break;
2238     case X86::SHRD32rri8: Size = 32; Opc = X86::SHLD32rri8; break;
2239     case X86::SHLD32rri8: Size = 32; Opc = X86::SHRD32rri8; break;
2240     case X86::SHRD64rri8: Size = 64; Opc = X86::SHLD64rri8; break;
2241     case X86::SHLD64rri8: Size = 64; Opc = X86::SHRD64rri8; break;
2242     }
2243     unsigned Amt = MI->getOperand(3).getImm();
2244     if (NewMI) {
2245       MachineFunction &MF = *MI->getParent()->getParent();
2246       MI = MF.CloneMachineInstr(MI);
2247       NewMI = false;
2248     }
2249     MI->setDesc(get(Opc));
2250     MI->getOperand(3).setImm(Size-Amt);
2251     return TargetInstrInfo::commuteInstruction(MI, NewMI);
2252   }
2253   case X86::CMOVB16rr:  case X86::CMOVB32rr:  case X86::CMOVB64rr:
2254   case X86::CMOVAE16rr: case X86::CMOVAE32rr: case X86::CMOVAE64rr:
2255   case X86::CMOVE16rr:  case X86::CMOVE32rr:  case X86::CMOVE64rr:
2256   case X86::CMOVNE16rr: case X86::CMOVNE32rr: case X86::CMOVNE64rr:
2257   case X86::CMOVBE16rr: case X86::CMOVBE32rr: case X86::CMOVBE64rr:
2258   case X86::CMOVA16rr:  case X86::CMOVA32rr:  case X86::CMOVA64rr:
2259   case X86::CMOVL16rr:  case X86::CMOVL32rr:  case X86::CMOVL64rr:
2260   case X86::CMOVGE16rr: case X86::CMOVGE32rr: case X86::CMOVGE64rr:
2261   case X86::CMOVLE16rr: case X86::CMOVLE32rr: case X86::CMOVLE64rr:
2262   case X86::CMOVG16rr:  case X86::CMOVG32rr:  case X86::CMOVG64rr:
2263   case X86::CMOVS16rr:  case X86::CMOVS32rr:  case X86::CMOVS64rr:
2264   case X86::CMOVNS16rr: case X86::CMOVNS32rr: case X86::CMOVNS64rr:
2265   case X86::CMOVP16rr:  case X86::CMOVP32rr:  case X86::CMOVP64rr:
2266   case X86::CMOVNP16rr: case X86::CMOVNP32rr: case X86::CMOVNP64rr:
2267   case X86::CMOVO16rr:  case X86::CMOVO32rr:  case X86::CMOVO64rr:
2268   case X86::CMOVNO16rr: case X86::CMOVNO32rr: case X86::CMOVNO64rr: {
2269     unsigned Opc;
2270     switch (MI->getOpcode()) {
2271     default: llvm_unreachable("Unreachable!");
2272     case X86::CMOVB16rr:  Opc = X86::CMOVAE16rr; break;
2273     case X86::CMOVB32rr:  Opc = X86::CMOVAE32rr; break;
2274     case X86::CMOVB64rr:  Opc = X86::CMOVAE64rr; break;
2275     case X86::CMOVAE16rr: Opc = X86::CMOVB16rr; break;
2276     case X86::CMOVAE32rr: Opc = X86::CMOVB32rr; break;
2277     case X86::CMOVAE64rr: Opc = X86::CMOVB64rr; break;
2278     case X86::CMOVE16rr:  Opc = X86::CMOVNE16rr; break;
2279     case X86::CMOVE32rr:  Opc = X86::CMOVNE32rr; break;
2280     case X86::CMOVE64rr:  Opc = X86::CMOVNE64rr; break;
2281     case X86::CMOVNE16rr: Opc = X86::CMOVE16rr; break;
2282     case X86::CMOVNE32rr: Opc = X86::CMOVE32rr; break;
2283     case X86::CMOVNE64rr: Opc = X86::CMOVE64rr; break;
2284     case X86::CMOVBE16rr: Opc = X86::CMOVA16rr; break;
2285     case X86::CMOVBE32rr: Opc = X86::CMOVA32rr; break;
2286     case X86::CMOVBE64rr: Opc = X86::CMOVA64rr; break;
2287     case X86::CMOVA16rr:  Opc = X86::CMOVBE16rr; break;
2288     case X86::CMOVA32rr:  Opc = X86::CMOVBE32rr; break;
2289     case X86::CMOVA64rr:  Opc = X86::CMOVBE64rr; break;
2290     case X86::CMOVL16rr:  Opc = X86::CMOVGE16rr; break;
2291     case X86::CMOVL32rr:  Opc = X86::CMOVGE32rr; break;
2292     case X86::CMOVL64rr:  Opc = X86::CMOVGE64rr; break;
2293     case X86::CMOVGE16rr: Opc = X86::CMOVL16rr; break;
2294     case X86::CMOVGE32rr: Opc = X86::CMOVL32rr; break;
2295     case X86::CMOVGE64rr: Opc = X86::CMOVL64rr; break;
2296     case X86::CMOVLE16rr: Opc = X86::CMOVG16rr; break;
2297     case X86::CMOVLE32rr: Opc = X86::CMOVG32rr; break;
2298     case X86::CMOVLE64rr: Opc = X86::CMOVG64rr; break;
2299     case X86::CMOVG16rr:  Opc = X86::CMOVLE16rr; break;
2300     case X86::CMOVG32rr:  Opc = X86::CMOVLE32rr; break;
2301     case X86::CMOVG64rr:  Opc = X86::CMOVLE64rr; break;
2302     case X86::CMOVS16rr:  Opc = X86::CMOVNS16rr; break;
2303     case X86::CMOVS32rr:  Opc = X86::CMOVNS32rr; break;
2304     case X86::CMOVS64rr:  Opc = X86::CMOVNS64rr; break;
2305     case X86::CMOVNS16rr: Opc = X86::CMOVS16rr; break;
2306     case X86::CMOVNS32rr: Opc = X86::CMOVS32rr; break;
2307     case X86::CMOVNS64rr: Opc = X86::CMOVS64rr; break;
2308     case X86::CMOVP16rr:  Opc = X86::CMOVNP16rr; break;
2309     case X86::CMOVP32rr:  Opc = X86::CMOVNP32rr; break;
2310     case X86::CMOVP64rr:  Opc = X86::CMOVNP64rr; break;
2311     case X86::CMOVNP16rr: Opc = X86::CMOVP16rr; break;
2312     case X86::CMOVNP32rr: Opc = X86::CMOVP32rr; break;
2313     case X86::CMOVNP64rr: Opc = X86::CMOVP64rr; break;
2314     case X86::CMOVO16rr:  Opc = X86::CMOVNO16rr; break;
2315     case X86::CMOVO32rr:  Opc = X86::CMOVNO32rr; break;
2316     case X86::CMOVO64rr:  Opc = X86::CMOVNO64rr; break;
2317     case X86::CMOVNO16rr: Opc = X86::CMOVO16rr; break;
2318     case X86::CMOVNO32rr: Opc = X86::CMOVO32rr; break;
2319     case X86::CMOVNO64rr: Opc = X86::CMOVO64rr; break;
2320     }
2321     if (NewMI) {
2322       MachineFunction &MF = *MI->getParent()->getParent();
2323       MI = MF.CloneMachineInstr(MI);
2324       NewMI = false;
2325     }
2326     MI->setDesc(get(Opc));
2327     // Fallthrough intended.
2328   }
2329   default:
2330     return TargetInstrInfo::commuteInstruction(MI, NewMI);
2331   }
2332 }
2333
2334 static X86::CondCode getCondFromBranchOpc(unsigned BrOpc) {
2335   switch (BrOpc) {
2336   default: return X86::COND_INVALID;
2337   case X86::JE_4:  return X86::COND_E;
2338   case X86::JNE_4: return X86::COND_NE;
2339   case X86::JL_4:  return X86::COND_L;
2340   case X86::JLE_4: return X86::COND_LE;
2341   case X86::JG_4:  return X86::COND_G;
2342   case X86::JGE_4: return X86::COND_GE;
2343   case X86::JB_4:  return X86::COND_B;
2344   case X86::JBE_4: return X86::COND_BE;
2345   case X86::JA_4:  return X86::COND_A;
2346   case X86::JAE_4: return X86::COND_AE;
2347   case X86::JS_4:  return X86::COND_S;
2348   case X86::JNS_4: return X86::COND_NS;
2349   case X86::JP_4:  return X86::COND_P;
2350   case X86::JNP_4: return X86::COND_NP;
2351   case X86::JO_4:  return X86::COND_O;
2352   case X86::JNO_4: return X86::COND_NO;
2353   }
2354 }
2355
2356 /// getCondFromSETOpc - return condition code of a SET opcode.
2357 static X86::CondCode getCondFromSETOpc(unsigned Opc) {
2358   switch (Opc) {
2359   default: return X86::COND_INVALID;
2360   case X86::SETAr:  case X86::SETAm:  return X86::COND_A;
2361   case X86::SETAEr: case X86::SETAEm: return X86::COND_AE;
2362   case X86::SETBr:  case X86::SETBm:  return X86::COND_B;
2363   case X86::SETBEr: case X86::SETBEm: return X86::COND_BE;
2364   case X86::SETEr:  case X86::SETEm:  return X86::COND_E;
2365   case X86::SETGr:  case X86::SETGm:  return X86::COND_G;
2366   case X86::SETGEr: case X86::SETGEm: return X86::COND_GE;
2367   case X86::SETLr:  case X86::SETLm:  return X86::COND_L;
2368   case X86::SETLEr: case X86::SETLEm: return X86::COND_LE;
2369   case X86::SETNEr: case X86::SETNEm: return X86::COND_NE;
2370   case X86::SETNOr: case X86::SETNOm: return X86::COND_NO;
2371   case X86::SETNPr: case X86::SETNPm: return X86::COND_NP;
2372   case X86::SETNSr: case X86::SETNSm: return X86::COND_NS;
2373   case X86::SETOr:  case X86::SETOm:  return X86::COND_O;
2374   case X86::SETPr:  case X86::SETPm:  return X86::COND_P;
2375   case X86::SETSr:  case X86::SETSm:  return X86::COND_S;
2376   }
2377 }
2378
2379 /// getCondFromCmovOpc - return condition code of a CMov opcode.
2380 X86::CondCode X86::getCondFromCMovOpc(unsigned Opc) {
2381   switch (Opc) {
2382   default: return X86::COND_INVALID;
2383   case X86::CMOVA16rm:  case X86::CMOVA16rr:  case X86::CMOVA32rm:
2384   case X86::CMOVA32rr:  case X86::CMOVA64rm:  case X86::CMOVA64rr:
2385     return X86::COND_A;
2386   case X86::CMOVAE16rm: case X86::CMOVAE16rr: case X86::CMOVAE32rm:
2387   case X86::CMOVAE32rr: case X86::CMOVAE64rm: case X86::CMOVAE64rr:
2388     return X86::COND_AE;
2389   case X86::CMOVB16rm:  case X86::CMOVB16rr:  case X86::CMOVB32rm:
2390   case X86::CMOVB32rr:  case X86::CMOVB64rm:  case X86::CMOVB64rr:
2391     return X86::COND_B;
2392   case X86::CMOVBE16rm: case X86::CMOVBE16rr: case X86::CMOVBE32rm:
2393   case X86::CMOVBE32rr: case X86::CMOVBE64rm: case X86::CMOVBE64rr:
2394     return X86::COND_BE;
2395   case X86::CMOVE16rm:  case X86::CMOVE16rr:  case X86::CMOVE32rm:
2396   case X86::CMOVE32rr:  case X86::CMOVE64rm:  case X86::CMOVE64rr:
2397     return X86::COND_E;
2398   case X86::CMOVG16rm:  case X86::CMOVG16rr:  case X86::CMOVG32rm:
2399   case X86::CMOVG32rr:  case X86::CMOVG64rm:  case X86::CMOVG64rr:
2400     return X86::COND_G;
2401   case X86::CMOVGE16rm: case X86::CMOVGE16rr: case X86::CMOVGE32rm:
2402   case X86::CMOVGE32rr: case X86::CMOVGE64rm: case X86::CMOVGE64rr:
2403     return X86::COND_GE;
2404   case X86::CMOVL16rm:  case X86::CMOVL16rr:  case X86::CMOVL32rm:
2405   case X86::CMOVL32rr:  case X86::CMOVL64rm:  case X86::CMOVL64rr:
2406     return X86::COND_L;
2407   case X86::CMOVLE16rm: case X86::CMOVLE16rr: case X86::CMOVLE32rm:
2408   case X86::CMOVLE32rr: case X86::CMOVLE64rm: case X86::CMOVLE64rr:
2409     return X86::COND_LE;
2410   case X86::CMOVNE16rm: case X86::CMOVNE16rr: case X86::CMOVNE32rm:
2411   case X86::CMOVNE32rr: case X86::CMOVNE64rm: case X86::CMOVNE64rr:
2412     return X86::COND_NE;
2413   case X86::CMOVNO16rm: case X86::CMOVNO16rr: case X86::CMOVNO32rm:
2414   case X86::CMOVNO32rr: case X86::CMOVNO64rm: case X86::CMOVNO64rr:
2415     return X86::COND_NO;
2416   case X86::CMOVNP16rm: case X86::CMOVNP16rr: case X86::CMOVNP32rm:
2417   case X86::CMOVNP32rr: case X86::CMOVNP64rm: case X86::CMOVNP64rr:
2418     return X86::COND_NP;
2419   case X86::CMOVNS16rm: case X86::CMOVNS16rr: case X86::CMOVNS32rm:
2420   case X86::CMOVNS32rr: case X86::CMOVNS64rm: case X86::CMOVNS64rr:
2421     return X86::COND_NS;
2422   case X86::CMOVO16rm:  case X86::CMOVO16rr:  case X86::CMOVO32rm:
2423   case X86::CMOVO32rr:  case X86::CMOVO64rm:  case X86::CMOVO64rr:
2424     return X86::COND_O;
2425   case X86::CMOVP16rm:  case X86::CMOVP16rr:  case X86::CMOVP32rm:
2426   case X86::CMOVP32rr:  case X86::CMOVP64rm:  case X86::CMOVP64rr:
2427     return X86::COND_P;
2428   case X86::CMOVS16rm:  case X86::CMOVS16rr:  case X86::CMOVS32rm:
2429   case X86::CMOVS32rr:  case X86::CMOVS64rm:  case X86::CMOVS64rr:
2430     return X86::COND_S;
2431   }
2432 }
2433
2434 unsigned X86::GetCondBranchFromCond(X86::CondCode CC) {
2435   switch (CC) {
2436   default: llvm_unreachable("Illegal condition code!");
2437   case X86::COND_E:  return X86::JE_4;
2438   case X86::COND_NE: return X86::JNE_4;
2439   case X86::COND_L:  return X86::JL_4;
2440   case X86::COND_LE: return X86::JLE_4;
2441   case X86::COND_G:  return X86::JG_4;
2442   case X86::COND_GE: return X86::JGE_4;
2443   case X86::COND_B:  return X86::JB_4;
2444   case X86::COND_BE: return X86::JBE_4;
2445   case X86::COND_A:  return X86::JA_4;
2446   case X86::COND_AE: return X86::JAE_4;
2447   case X86::COND_S:  return X86::JS_4;
2448   case X86::COND_NS: return X86::JNS_4;
2449   case X86::COND_P:  return X86::JP_4;
2450   case X86::COND_NP: return X86::JNP_4;
2451   case X86::COND_O:  return X86::JO_4;
2452   case X86::COND_NO: return X86::JNO_4;
2453   }
2454 }
2455
2456 /// GetOppositeBranchCondition - Return the inverse of the specified condition,
2457 /// e.g. turning COND_E to COND_NE.
2458 X86::CondCode X86::GetOppositeBranchCondition(X86::CondCode CC) {
2459   switch (CC) {
2460   default: llvm_unreachable("Illegal condition code!");
2461   case X86::COND_E:  return X86::COND_NE;
2462   case X86::COND_NE: return X86::COND_E;
2463   case X86::COND_L:  return X86::COND_GE;
2464   case X86::COND_LE: return X86::COND_G;
2465   case X86::COND_G:  return X86::COND_LE;
2466   case X86::COND_GE: return X86::COND_L;
2467   case X86::COND_B:  return X86::COND_AE;
2468   case X86::COND_BE: return X86::COND_A;
2469   case X86::COND_A:  return X86::COND_BE;
2470   case X86::COND_AE: return X86::COND_B;
2471   case X86::COND_S:  return X86::COND_NS;
2472   case X86::COND_NS: return X86::COND_S;
2473   case X86::COND_P:  return X86::COND_NP;
2474   case X86::COND_NP: return X86::COND_P;
2475   case X86::COND_O:  return X86::COND_NO;
2476   case X86::COND_NO: return X86::COND_O;
2477   }
2478 }
2479
2480 /// getSwappedCondition - assume the flags are set by MI(a,b), return
2481 /// the condition code if we modify the instructions such that flags are
2482 /// set by MI(b,a).
2483 static X86::CondCode getSwappedCondition(X86::CondCode CC) {
2484   switch (CC) {
2485   default: return X86::COND_INVALID;
2486   case X86::COND_E:  return X86::COND_E;
2487   case X86::COND_NE: return X86::COND_NE;
2488   case X86::COND_L:  return X86::COND_G;
2489   case X86::COND_LE: return X86::COND_GE;
2490   case X86::COND_G:  return X86::COND_L;
2491   case X86::COND_GE: return X86::COND_LE;
2492   case X86::COND_B:  return X86::COND_A;
2493   case X86::COND_BE: return X86::COND_AE;
2494   case X86::COND_A:  return X86::COND_B;
2495   case X86::COND_AE: return X86::COND_BE;
2496   }
2497 }
2498
2499 /// getSETFromCond - Return a set opcode for the given condition and
2500 /// whether it has memory operand.
2501 static unsigned getSETFromCond(X86::CondCode CC,
2502                                bool HasMemoryOperand) {
2503   static const uint16_t Opc[16][2] = {
2504     { X86::SETAr,  X86::SETAm  },
2505     { X86::SETAEr, X86::SETAEm },
2506     { X86::SETBr,  X86::SETBm  },
2507     { X86::SETBEr, X86::SETBEm },
2508     { X86::SETEr,  X86::SETEm  },
2509     { X86::SETGr,  X86::SETGm  },
2510     { X86::SETGEr, X86::SETGEm },
2511     { X86::SETLr,  X86::SETLm  },
2512     { X86::SETLEr, X86::SETLEm },
2513     { X86::SETNEr, X86::SETNEm },
2514     { X86::SETNOr, X86::SETNOm },
2515     { X86::SETNPr, X86::SETNPm },
2516     { X86::SETNSr, X86::SETNSm },
2517     { X86::SETOr,  X86::SETOm  },
2518     { X86::SETPr,  X86::SETPm  },
2519     { X86::SETSr,  X86::SETSm  }
2520   };
2521
2522   assert(CC < 16 && "Can only handle standard cond codes");
2523   return Opc[CC][HasMemoryOperand ? 1 : 0];
2524 }
2525
2526 /// getCMovFromCond - Return a cmov opcode for the given condition,
2527 /// register size in bytes, and operand type.
2528 static unsigned getCMovFromCond(X86::CondCode CC, unsigned RegBytes,
2529                                 bool HasMemoryOperand) {
2530   static const uint16_t Opc[32][3] = {
2531     { X86::CMOVA16rr,  X86::CMOVA32rr,  X86::CMOVA64rr  },
2532     { X86::CMOVAE16rr, X86::CMOVAE32rr, X86::CMOVAE64rr },
2533     { X86::CMOVB16rr,  X86::CMOVB32rr,  X86::CMOVB64rr  },
2534     { X86::CMOVBE16rr, X86::CMOVBE32rr, X86::CMOVBE64rr },
2535     { X86::CMOVE16rr,  X86::CMOVE32rr,  X86::CMOVE64rr  },
2536     { X86::CMOVG16rr,  X86::CMOVG32rr,  X86::CMOVG64rr  },
2537     { X86::CMOVGE16rr, X86::CMOVGE32rr, X86::CMOVGE64rr },
2538     { X86::CMOVL16rr,  X86::CMOVL32rr,  X86::CMOVL64rr  },
2539     { X86::CMOVLE16rr, X86::CMOVLE32rr, X86::CMOVLE64rr },
2540     { X86::CMOVNE16rr, X86::CMOVNE32rr, X86::CMOVNE64rr },
2541     { X86::CMOVNO16rr, X86::CMOVNO32rr, X86::CMOVNO64rr },
2542     { X86::CMOVNP16rr, X86::CMOVNP32rr, X86::CMOVNP64rr },
2543     { X86::CMOVNS16rr, X86::CMOVNS32rr, X86::CMOVNS64rr },
2544     { X86::CMOVO16rr,  X86::CMOVO32rr,  X86::CMOVO64rr  },
2545     { X86::CMOVP16rr,  X86::CMOVP32rr,  X86::CMOVP64rr  },
2546     { X86::CMOVS16rr,  X86::CMOVS32rr,  X86::CMOVS64rr  },
2547     { X86::CMOVA16rm,  X86::CMOVA32rm,  X86::CMOVA64rm  },
2548     { X86::CMOVAE16rm, X86::CMOVAE32rm, X86::CMOVAE64rm },
2549     { X86::CMOVB16rm,  X86::CMOVB32rm,  X86::CMOVB64rm  },
2550     { X86::CMOVBE16rm, X86::CMOVBE32rm, X86::CMOVBE64rm },
2551     { X86::CMOVE16rm,  X86::CMOVE32rm,  X86::CMOVE64rm  },
2552     { X86::CMOVG16rm,  X86::CMOVG32rm,  X86::CMOVG64rm  },
2553     { X86::CMOVGE16rm, X86::CMOVGE32rm, X86::CMOVGE64rm },
2554     { X86::CMOVL16rm,  X86::CMOVL32rm,  X86::CMOVL64rm  },
2555     { X86::CMOVLE16rm, X86::CMOVLE32rm, X86::CMOVLE64rm },
2556     { X86::CMOVNE16rm, X86::CMOVNE32rm, X86::CMOVNE64rm },
2557     { X86::CMOVNO16rm, X86::CMOVNO32rm, X86::CMOVNO64rm },
2558     { X86::CMOVNP16rm, X86::CMOVNP32rm, X86::CMOVNP64rm },
2559     { X86::CMOVNS16rm, X86::CMOVNS32rm, X86::CMOVNS64rm },
2560     { X86::CMOVO16rm,  X86::CMOVO32rm,  X86::CMOVO64rm  },
2561     { X86::CMOVP16rm,  X86::CMOVP32rm,  X86::CMOVP64rm  },
2562     { X86::CMOVS16rm,  X86::CMOVS32rm,  X86::CMOVS64rm  }
2563   };
2564
2565   assert(CC < 16 && "Can only handle standard cond codes");
2566   unsigned Idx = HasMemoryOperand ? 16+CC : CC;
2567   switch(RegBytes) {
2568   default: llvm_unreachable("Illegal register size!");
2569   case 2: return Opc[Idx][0];
2570   case 4: return Opc[Idx][1];
2571   case 8: return Opc[Idx][2];
2572   }
2573 }
2574
2575 bool X86InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
2576   if (!MI->isTerminator()) return false;
2577
2578   // Conditional branch is a special case.
2579   if (MI->isBranch() && !MI->isBarrier())
2580     return true;
2581   if (!MI->isPredicable())
2582     return true;
2583   return !isPredicated(MI);
2584 }
2585
2586 bool X86InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
2587                                  MachineBasicBlock *&TBB,
2588                                  MachineBasicBlock *&FBB,
2589                                  SmallVectorImpl<MachineOperand> &Cond,
2590                                  bool AllowModify) const {
2591   // Start from the bottom of the block and work up, examining the
2592   // terminator instructions.
2593   MachineBasicBlock::iterator I = MBB.end();
2594   MachineBasicBlock::iterator UnCondBrIter = MBB.end();
2595   while (I != MBB.begin()) {
2596     --I;
2597     if (I->isDebugValue())
2598       continue;
2599
2600     // Working from the bottom, when we see a non-terminator instruction, we're
2601     // done.
2602     if (!isUnpredicatedTerminator(I))
2603       break;
2604
2605     // A terminator that isn't a branch can't easily be handled by this
2606     // analysis.
2607     if (!I->isBranch())
2608       return true;
2609
2610     // Handle unconditional branches.
2611     if (I->getOpcode() == X86::JMP_4) {
2612       UnCondBrIter = I;
2613
2614       if (!AllowModify) {
2615         TBB = I->getOperand(0).getMBB();
2616         continue;
2617       }
2618
2619       // If the block has any instructions after a JMP, delete them.
2620       while (llvm::next(I) != MBB.end())
2621         llvm::next(I)->eraseFromParent();
2622
2623       Cond.clear();
2624       FBB = 0;
2625
2626       // Delete the JMP if it's equivalent to a fall-through.
2627       if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
2628         TBB = 0;
2629         I->eraseFromParent();
2630         I = MBB.end();
2631         UnCondBrIter = MBB.end();
2632         continue;
2633       }
2634
2635       // TBB is used to indicate the unconditional destination.
2636       TBB = I->getOperand(0).getMBB();
2637       continue;
2638     }
2639
2640     // Handle conditional branches.
2641     X86::CondCode BranchCode = getCondFromBranchOpc(I->getOpcode());
2642     if (BranchCode == X86::COND_INVALID)
2643       return true;  // Can't handle indirect branch.
2644
2645     // Working from the bottom, handle the first conditional branch.
2646     if (Cond.empty()) {
2647       MachineBasicBlock *TargetBB = I->getOperand(0).getMBB();
2648       if (AllowModify && UnCondBrIter != MBB.end() &&
2649           MBB.isLayoutSuccessor(TargetBB)) {
2650         // If we can modify the code and it ends in something like:
2651         //
2652         //     jCC L1
2653         //     jmp L2
2654         //   L1:
2655         //     ...
2656         //   L2:
2657         //
2658         // Then we can change this to:
2659         //
2660         //     jnCC L2
2661         //   L1:
2662         //     ...
2663         //   L2:
2664         //
2665         // Which is a bit more efficient.
2666         // We conditionally jump to the fall-through block.
2667         BranchCode = GetOppositeBranchCondition(BranchCode);
2668         unsigned JNCC = GetCondBranchFromCond(BranchCode);
2669         MachineBasicBlock::iterator OldInst = I;
2670
2671         BuildMI(MBB, UnCondBrIter, MBB.findDebugLoc(I), get(JNCC))
2672           .addMBB(UnCondBrIter->getOperand(0).getMBB());
2673         BuildMI(MBB, UnCondBrIter, MBB.findDebugLoc(I), get(X86::JMP_4))
2674           .addMBB(TargetBB);
2675
2676         OldInst->eraseFromParent();
2677         UnCondBrIter->eraseFromParent();
2678
2679         // Restart the analysis.
2680         UnCondBrIter = MBB.end();
2681         I = MBB.end();
2682         continue;
2683       }
2684
2685       FBB = TBB;
2686       TBB = I->getOperand(0).getMBB();
2687       Cond.push_back(MachineOperand::CreateImm(BranchCode));
2688       continue;
2689     }
2690
2691     // Handle subsequent conditional branches. Only handle the case where all
2692     // conditional branches branch to the same destination and their condition
2693     // opcodes fit one of the special multi-branch idioms.
2694     assert(Cond.size() == 1);
2695     assert(TBB);
2696
2697     // Only handle the case where all conditional branches branch to the same
2698     // destination.
2699     if (TBB != I->getOperand(0).getMBB())
2700       return true;
2701
2702     // If the conditions are the same, we can leave them alone.
2703     X86::CondCode OldBranchCode = (X86::CondCode)Cond[0].getImm();
2704     if (OldBranchCode == BranchCode)
2705       continue;
2706
2707     // If they differ, see if they fit one of the known patterns. Theoretically,
2708     // we could handle more patterns here, but we shouldn't expect to see them
2709     // if instruction selection has done a reasonable job.
2710     if ((OldBranchCode == X86::COND_NP &&
2711          BranchCode == X86::COND_E) ||
2712         (OldBranchCode == X86::COND_E &&
2713          BranchCode == X86::COND_NP))
2714       BranchCode = X86::COND_NP_OR_E;
2715     else if ((OldBranchCode == X86::COND_P &&
2716               BranchCode == X86::COND_NE) ||
2717              (OldBranchCode == X86::COND_NE &&
2718               BranchCode == X86::COND_P))
2719       BranchCode = X86::COND_NE_OR_P;
2720     else
2721       return true;
2722
2723     // Update the MachineOperand.
2724     Cond[0].setImm(BranchCode);
2725   }
2726
2727   return false;
2728 }
2729
2730 unsigned X86InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
2731   MachineBasicBlock::iterator I = MBB.end();
2732   unsigned Count = 0;
2733
2734   while (I != MBB.begin()) {
2735     --I;
2736     if (I->isDebugValue())
2737       continue;
2738     if (I->getOpcode() != X86::JMP_4 &&
2739         getCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
2740       break;
2741     // Remove the branch.
2742     I->eraseFromParent();
2743     I = MBB.end();
2744     ++Count;
2745   }
2746
2747   return Count;
2748 }
2749
2750 unsigned
2751 X86InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
2752                            MachineBasicBlock *FBB,
2753                            const SmallVectorImpl<MachineOperand> &Cond,
2754                            DebugLoc DL) const {
2755   // Shouldn't be a fall through.
2756   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
2757   assert((Cond.size() == 1 || Cond.size() == 0) &&
2758          "X86 branch conditions have one component!");
2759
2760   if (Cond.empty()) {
2761     // Unconditional branch?
2762     assert(!FBB && "Unconditional branch with multiple successors!");
2763     BuildMI(&MBB, DL, get(X86::JMP_4)).addMBB(TBB);
2764     return 1;
2765   }
2766
2767   // Conditional branch.
2768   unsigned Count = 0;
2769   X86::CondCode CC = (X86::CondCode)Cond[0].getImm();
2770   switch (CC) {
2771   case X86::COND_NP_OR_E:
2772     // Synthesize NP_OR_E with two branches.
2773     BuildMI(&MBB, DL, get(X86::JNP_4)).addMBB(TBB);
2774     ++Count;
2775     BuildMI(&MBB, DL, get(X86::JE_4)).addMBB(TBB);
2776     ++Count;
2777     break;
2778   case X86::COND_NE_OR_P:
2779     // Synthesize NE_OR_P with two branches.
2780     BuildMI(&MBB, DL, get(X86::JNE_4)).addMBB(TBB);
2781     ++Count;
2782     BuildMI(&MBB, DL, get(X86::JP_4)).addMBB(TBB);
2783     ++Count;
2784     break;
2785   default: {
2786     unsigned Opc = GetCondBranchFromCond(CC);
2787     BuildMI(&MBB, DL, get(Opc)).addMBB(TBB);
2788     ++Count;
2789   }
2790   }
2791   if (FBB) {
2792     // Two-way Conditional branch. Insert the second branch.
2793     BuildMI(&MBB, DL, get(X86::JMP_4)).addMBB(FBB);
2794     ++Count;
2795   }
2796   return Count;
2797 }
2798
2799 bool X86InstrInfo::
2800 canInsertSelect(const MachineBasicBlock &MBB,
2801                 const SmallVectorImpl<MachineOperand> &Cond,
2802                 unsigned TrueReg, unsigned FalseReg,
2803                 int &CondCycles, int &TrueCycles, int &FalseCycles) const {
2804   // Not all subtargets have cmov instructions.
2805   if (!TM.getSubtarget<X86Subtarget>().hasCMov())
2806     return false;
2807   if (Cond.size() != 1)
2808     return false;
2809   // We cannot do the composite conditions, at least not in SSA form.
2810   if ((X86::CondCode)Cond[0].getImm() > X86::COND_S)
2811     return false;
2812
2813   // Check register classes.
2814   const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
2815   const TargetRegisterClass *RC =
2816     RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
2817   if (!RC)
2818     return false;
2819
2820   // We have cmov instructions for 16, 32, and 64 bit general purpose registers.
2821   if (X86::GR16RegClass.hasSubClassEq(RC) ||
2822       X86::GR32RegClass.hasSubClassEq(RC) ||
2823       X86::GR64RegClass.hasSubClassEq(RC)) {
2824     // This latency applies to Pentium M, Merom, Wolfdale, Nehalem, and Sandy
2825     // Bridge. Probably Ivy Bridge as well.
2826     CondCycles = 2;
2827     TrueCycles = 2;
2828     FalseCycles = 2;
2829     return true;
2830   }
2831
2832   // Can't do vectors.
2833   return false;
2834 }
2835
2836 void X86InstrInfo::insertSelect(MachineBasicBlock &MBB,
2837                                 MachineBasicBlock::iterator I, DebugLoc DL,
2838                                 unsigned DstReg,
2839                                 const SmallVectorImpl<MachineOperand> &Cond,
2840                                 unsigned TrueReg, unsigned FalseReg) const {
2841    MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
2842    assert(Cond.size() == 1 && "Invalid Cond array");
2843    unsigned Opc = getCMovFromCond((X86::CondCode)Cond[0].getImm(),
2844                                   MRI.getRegClass(DstReg)->getSize(),
2845                                   false/*HasMemoryOperand*/);
2846    BuildMI(MBB, I, DL, get(Opc), DstReg).addReg(FalseReg).addReg(TrueReg);
2847 }
2848
2849 /// isHReg - Test if the given register is a physical h register.
2850 static bool isHReg(unsigned Reg) {
2851   return X86::GR8_ABCD_HRegClass.contains(Reg);
2852 }
2853
2854 // Try and copy between VR128/VR64 and GR64 registers.
2855 static unsigned CopyToFromAsymmetricReg(unsigned DestReg, unsigned SrcReg,
2856                                         bool HasAVX) {
2857   // SrcReg(VR128) -> DestReg(GR64)
2858   // SrcReg(VR64)  -> DestReg(GR64)
2859   // SrcReg(GR64)  -> DestReg(VR128)
2860   // SrcReg(GR64)  -> DestReg(VR64)
2861
2862   if (X86::GR64RegClass.contains(DestReg)) {
2863     if (X86::VR128RegClass.contains(SrcReg))
2864       // Copy from a VR128 register to a GR64 register.
2865       return HasAVX ? X86::VMOVPQIto64rr : X86::MOVPQIto64rr;
2866     if (X86::VR64RegClass.contains(SrcReg))
2867       // Copy from a VR64 register to a GR64 register.
2868       return X86::MOVSDto64rr;
2869   } else if (X86::GR64RegClass.contains(SrcReg)) {
2870     // Copy from a GR64 register to a VR128 register.
2871     if (X86::VR128RegClass.contains(DestReg))
2872       return HasAVX ? X86::VMOV64toPQIrr : X86::MOV64toPQIrr;
2873     // Copy from a GR64 register to a VR64 register.
2874     if (X86::VR64RegClass.contains(DestReg))
2875       return X86::MOV64toSDrr;
2876   }
2877
2878   // SrcReg(FR32) -> DestReg(GR32)
2879   // SrcReg(GR32) -> DestReg(FR32)
2880
2881   if (X86::GR32RegClass.contains(DestReg) && X86::FR32RegClass.contains(SrcReg))
2882     // Copy from a FR32 register to a GR32 register.
2883     return HasAVX ? X86::VMOVSS2DIrr : X86::MOVSS2DIrr;
2884
2885   if (X86::FR32RegClass.contains(DestReg) && X86::GR32RegClass.contains(SrcReg))
2886     // Copy from a GR32 register to a FR32 register.
2887     return HasAVX ? X86::VMOVDI2SSrr : X86::MOVDI2SSrr;
2888
2889   return 0;
2890 }
2891
2892 void X86InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
2893                                MachineBasicBlock::iterator MI, DebugLoc DL,
2894                                unsigned DestReg, unsigned SrcReg,
2895                                bool KillSrc) const {
2896   // First deal with the normal symmetric copies.
2897   bool HasAVX = TM.getSubtarget<X86Subtarget>().hasAVX();
2898   unsigned Opc;
2899   if (X86::GR64RegClass.contains(DestReg, SrcReg))
2900     Opc = X86::MOV64rr;
2901   else if (X86::GR32RegClass.contains(DestReg, SrcReg))
2902     Opc = X86::MOV32rr;
2903   else if (X86::GR16RegClass.contains(DestReg, SrcReg))
2904     Opc = X86::MOV16rr;
2905   else if (X86::GR8RegClass.contains(DestReg, SrcReg)) {
2906     // Copying to or from a physical H register on x86-64 requires a NOREX
2907     // move.  Otherwise use a normal move.
2908     if ((isHReg(DestReg) || isHReg(SrcReg)) &&
2909         TM.getSubtarget<X86Subtarget>().is64Bit()) {
2910       Opc = X86::MOV8rr_NOREX;
2911       // Both operands must be encodable without an REX prefix.
2912       assert(X86::GR8_NOREXRegClass.contains(SrcReg, DestReg) &&
2913              "8-bit H register can not be copied outside GR8_NOREX");
2914     } else
2915       Opc = X86::MOV8rr;
2916   } else if (X86::VR128RegClass.contains(DestReg, SrcReg))
2917     Opc = HasAVX ? X86::VMOVAPSrr : X86::MOVAPSrr;
2918   else if (X86::VR256RegClass.contains(DestReg, SrcReg))
2919     Opc = X86::VMOVAPSYrr;
2920   else if (X86::VR64RegClass.contains(DestReg, SrcReg))
2921     Opc = X86::MMX_MOVQ64rr;
2922   else
2923     Opc = CopyToFromAsymmetricReg(DestReg, SrcReg, HasAVX);
2924
2925   if (Opc) {
2926     BuildMI(MBB, MI, DL, get(Opc), DestReg)
2927       .addReg(SrcReg, getKillRegState(KillSrc));
2928     return;
2929   }
2930
2931   // Moving EFLAGS to / from another register requires a push and a pop.
2932   // Notice that we have to adjust the stack if we don't want to clobber the
2933   // first frame index. See X86FrameLowering.cpp - colobbersTheStack.
2934   if (SrcReg == X86::EFLAGS) {
2935     if (X86::GR64RegClass.contains(DestReg)) {
2936       BuildMI(MBB, MI, DL, get(X86::PUSHF64));
2937       BuildMI(MBB, MI, DL, get(X86::POP64r), DestReg);
2938       return;
2939     }
2940     if (X86::GR32RegClass.contains(DestReg)) {
2941       BuildMI(MBB, MI, DL, get(X86::PUSHF32));
2942       BuildMI(MBB, MI, DL, get(X86::POP32r), DestReg);
2943       return;
2944     }
2945   }
2946   if (DestReg == X86::EFLAGS) {
2947     if (X86::GR64RegClass.contains(SrcReg)) {
2948       BuildMI(MBB, MI, DL, get(X86::PUSH64r))
2949         .addReg(SrcReg, getKillRegState(KillSrc));
2950       BuildMI(MBB, MI, DL, get(X86::POPF64));
2951       return;
2952     }
2953     if (X86::GR32RegClass.contains(SrcReg)) {
2954       BuildMI(MBB, MI, DL, get(X86::PUSH32r))
2955         .addReg(SrcReg, getKillRegState(KillSrc));
2956       BuildMI(MBB, MI, DL, get(X86::POPF32));
2957       return;
2958     }
2959   }
2960
2961   DEBUG(dbgs() << "Cannot copy " << RI.getName(SrcReg)
2962                << " to " << RI.getName(DestReg) << '\n');
2963   llvm_unreachable("Cannot emit physreg copy instruction");
2964 }
2965
2966 static unsigned getLoadStoreRegOpcode(unsigned Reg,
2967                                       const TargetRegisterClass *RC,
2968                                       bool isStackAligned,
2969                                       const TargetMachine &TM,
2970                                       bool load) {
2971   bool HasAVX = TM.getSubtarget<X86Subtarget>().hasAVX();
2972   switch (RC->getSize()) {
2973   default:
2974     llvm_unreachable("Unknown spill size");
2975   case 1:
2976     assert(X86::GR8RegClass.hasSubClassEq(RC) && "Unknown 1-byte regclass");
2977     if (TM.getSubtarget<X86Subtarget>().is64Bit())
2978       // Copying to or from a physical H register on x86-64 requires a NOREX
2979       // move.  Otherwise use a normal move.
2980       if (isHReg(Reg) || X86::GR8_ABCD_HRegClass.hasSubClassEq(RC))
2981         return load ? X86::MOV8rm_NOREX : X86::MOV8mr_NOREX;
2982     return load ? X86::MOV8rm : X86::MOV8mr;
2983   case 2:
2984     assert(X86::GR16RegClass.hasSubClassEq(RC) && "Unknown 2-byte regclass");
2985     return load ? X86::MOV16rm : X86::MOV16mr;
2986   case 4:
2987     if (X86::GR32RegClass.hasSubClassEq(RC))
2988       return load ? X86::MOV32rm : X86::MOV32mr;
2989     if (X86::FR32RegClass.hasSubClassEq(RC))
2990       return load ?
2991         (HasAVX ? X86::VMOVSSrm : X86::MOVSSrm) :
2992         (HasAVX ? X86::VMOVSSmr : X86::MOVSSmr);
2993     if (X86::RFP32RegClass.hasSubClassEq(RC))
2994       return load ? X86::LD_Fp32m : X86::ST_Fp32m;
2995     llvm_unreachable("Unknown 4-byte regclass");
2996   case 8:
2997     if (X86::GR64RegClass.hasSubClassEq(RC))
2998       return load ? X86::MOV64rm : X86::MOV64mr;
2999     if (X86::FR64RegClass.hasSubClassEq(RC))
3000       return load ?
3001         (HasAVX ? X86::VMOVSDrm : X86::MOVSDrm) :
3002         (HasAVX ? X86::VMOVSDmr : X86::MOVSDmr);
3003     if (X86::VR64RegClass.hasSubClassEq(RC))
3004       return load ? X86::MMX_MOVQ64rm : X86::MMX_MOVQ64mr;
3005     if (X86::RFP64RegClass.hasSubClassEq(RC))
3006       return load ? X86::LD_Fp64m : X86::ST_Fp64m;
3007     llvm_unreachable("Unknown 8-byte regclass");
3008   case 10:
3009     assert(X86::RFP80RegClass.hasSubClassEq(RC) && "Unknown 10-byte regclass");
3010     return load ? X86::LD_Fp80m : X86::ST_FpP80m;
3011   case 16: {
3012     assert(X86::VR128RegClass.hasSubClassEq(RC) && "Unknown 16-byte regclass");
3013     // If stack is realigned we can use aligned stores.
3014     if (isStackAligned)
3015       return load ?
3016         (HasAVX ? X86::VMOVAPSrm : X86::MOVAPSrm) :
3017         (HasAVX ? X86::VMOVAPSmr : X86::MOVAPSmr);
3018     else
3019       return load ?
3020         (HasAVX ? X86::VMOVUPSrm : X86::MOVUPSrm) :
3021         (HasAVX ? X86::VMOVUPSmr : X86::MOVUPSmr);
3022   }
3023   case 32:
3024     assert(X86::VR256RegClass.hasSubClassEq(RC) && "Unknown 32-byte regclass");
3025     // If stack is realigned we can use aligned stores.
3026     if (isStackAligned)
3027       return load ? X86::VMOVAPSYrm : X86::VMOVAPSYmr;
3028     else
3029       return load ? X86::VMOVUPSYrm : X86::VMOVUPSYmr;
3030   }
3031 }
3032
3033 static unsigned getStoreRegOpcode(unsigned SrcReg,
3034                                   const TargetRegisterClass *RC,
3035                                   bool isStackAligned,
3036                                   TargetMachine &TM) {
3037   return getLoadStoreRegOpcode(SrcReg, RC, isStackAligned, TM, false);
3038 }
3039
3040
3041 static unsigned getLoadRegOpcode(unsigned DestReg,
3042                                  const TargetRegisterClass *RC,
3043                                  bool isStackAligned,
3044                                  const TargetMachine &TM) {
3045   return getLoadStoreRegOpcode(DestReg, RC, isStackAligned, TM, true);
3046 }
3047
3048 void X86InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
3049                                        MachineBasicBlock::iterator MI,
3050                                        unsigned SrcReg, bool isKill, int FrameIdx,
3051                                        const TargetRegisterClass *RC,
3052                                        const TargetRegisterInfo *TRI) const {
3053   const MachineFunction &MF = *MBB.getParent();
3054   assert(MF.getFrameInfo()->getObjectSize(FrameIdx) >= RC->getSize() &&
3055          "Stack slot too small for store");
3056   unsigned Alignment = RC->getSize() == 32 ? 32 : 16;
3057   bool isAligned = (TM.getFrameLowering()->getStackAlignment() >= Alignment) ||
3058     RI.canRealignStack(MF);
3059   unsigned Opc = getStoreRegOpcode(SrcReg, RC, isAligned, TM);
3060   DebugLoc DL = MBB.findDebugLoc(MI);
3061   addFrameReference(BuildMI(MBB, MI, DL, get(Opc)), FrameIdx)
3062     .addReg(SrcReg, getKillRegState(isKill));
3063 }
3064
3065 void X86InstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
3066                                   bool isKill,
3067                                   SmallVectorImpl<MachineOperand> &Addr,
3068                                   const TargetRegisterClass *RC,
3069                                   MachineInstr::mmo_iterator MMOBegin,
3070                                   MachineInstr::mmo_iterator MMOEnd,
3071                                   SmallVectorImpl<MachineInstr*> &NewMIs) const {
3072   unsigned Alignment = RC->getSize() == 32 ? 32 : 16;
3073   bool isAligned = MMOBegin != MMOEnd &&
3074                    (*MMOBegin)->getAlignment() >= Alignment;
3075   unsigned Opc = getStoreRegOpcode(SrcReg, RC, isAligned, TM);
3076   DebugLoc DL;
3077   MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc));
3078   for (unsigned i = 0, e = Addr.size(); i != e; ++i)
3079     MIB.addOperand(Addr[i]);
3080   MIB.addReg(SrcReg, getKillRegState(isKill));
3081   (*MIB).setMemRefs(MMOBegin, MMOEnd);
3082   NewMIs.push_back(MIB);
3083 }
3084
3085
3086 void X86InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
3087                                         MachineBasicBlock::iterator MI,
3088                                         unsigned DestReg, int FrameIdx,
3089                                         const TargetRegisterClass *RC,
3090                                         const TargetRegisterInfo *TRI) const {
3091   const MachineFunction &MF = *MBB.getParent();
3092   unsigned Alignment = RC->getSize() == 32 ? 32 : 16;
3093   bool isAligned = (TM.getFrameLowering()->getStackAlignment() >= Alignment) ||
3094     RI.canRealignStack(MF);
3095   unsigned Opc = getLoadRegOpcode(DestReg, RC, isAligned, TM);
3096   DebugLoc DL = MBB.findDebugLoc(MI);
3097   addFrameReference(BuildMI(MBB, MI, DL, get(Opc), DestReg), FrameIdx);
3098 }
3099
3100 void X86InstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
3101                                  SmallVectorImpl<MachineOperand> &Addr,
3102                                  const TargetRegisterClass *RC,
3103                                  MachineInstr::mmo_iterator MMOBegin,
3104                                  MachineInstr::mmo_iterator MMOEnd,
3105                                  SmallVectorImpl<MachineInstr*> &NewMIs) const {
3106   unsigned Alignment = RC->getSize() == 32 ? 32 : 16;
3107   bool isAligned = MMOBegin != MMOEnd &&
3108                    (*MMOBegin)->getAlignment() >= Alignment;
3109   unsigned Opc = getLoadRegOpcode(DestReg, RC, isAligned, TM);
3110   DebugLoc DL;
3111   MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc), DestReg);
3112   for (unsigned i = 0, e = Addr.size(); i != e; ++i)
3113     MIB.addOperand(Addr[i]);
3114   (*MIB).setMemRefs(MMOBegin, MMOEnd);
3115   NewMIs.push_back(MIB);
3116 }
3117
3118 bool X86InstrInfo::
3119 analyzeCompare(const MachineInstr *MI, unsigned &SrcReg, unsigned &SrcReg2,
3120                int &CmpMask, int &CmpValue) const {
3121   switch (MI->getOpcode()) {
3122   default: break;
3123   case X86::CMP64ri32:
3124   case X86::CMP64ri8:
3125   case X86::CMP32ri:
3126   case X86::CMP32ri8:
3127   case X86::CMP16ri:
3128   case X86::CMP16ri8:
3129   case X86::CMP8ri:
3130     SrcReg = MI->getOperand(0).getReg();
3131     SrcReg2 = 0;
3132     CmpMask = ~0;
3133     CmpValue = MI->getOperand(1).getImm();
3134     return true;
3135   // A SUB can be used to perform comparison.
3136   case X86::SUB64rm:
3137   case X86::SUB32rm:
3138   case X86::SUB16rm:
3139   case X86::SUB8rm:
3140     SrcReg = MI->getOperand(1).getReg();
3141     SrcReg2 = 0;
3142     CmpMask = ~0;
3143     CmpValue = 0;
3144     return true;
3145   case X86::SUB64rr:
3146   case X86::SUB32rr:
3147   case X86::SUB16rr:
3148   case X86::SUB8rr:
3149     SrcReg = MI->getOperand(1).getReg();
3150     SrcReg2 = MI->getOperand(2).getReg();
3151     CmpMask = ~0;
3152     CmpValue = 0;
3153     return true;
3154   case X86::SUB64ri32:
3155   case X86::SUB64ri8:
3156   case X86::SUB32ri:
3157   case X86::SUB32ri8:
3158   case X86::SUB16ri:
3159   case X86::SUB16ri8:
3160   case X86::SUB8ri:
3161     SrcReg = MI->getOperand(1).getReg();
3162     SrcReg2 = 0;
3163     CmpMask = ~0;
3164     CmpValue = MI->getOperand(2).getImm();
3165     return true;
3166   case X86::CMP64rr:
3167   case X86::CMP32rr:
3168   case X86::CMP16rr:
3169   case X86::CMP8rr:
3170     SrcReg = MI->getOperand(0).getReg();
3171     SrcReg2 = MI->getOperand(1).getReg();
3172     CmpMask = ~0;
3173     CmpValue = 0;
3174     return true;
3175   case X86::TEST8rr:
3176   case X86::TEST16rr:
3177   case X86::TEST32rr:
3178   case X86::TEST64rr:
3179     SrcReg = MI->getOperand(0).getReg();
3180     if (MI->getOperand(1).getReg() != SrcReg) return false;
3181     // Compare against zero.
3182     SrcReg2 = 0;
3183     CmpMask = ~0;
3184     CmpValue = 0;
3185     return true;
3186   }
3187   return false;
3188 }
3189
3190 /// isRedundantFlagInstr - check whether the first instruction, whose only
3191 /// purpose is to update flags, can be made redundant.
3192 /// CMPrr can be made redundant by SUBrr if the operands are the same.
3193 /// This function can be extended later on.
3194 /// SrcReg, SrcRegs: register operands for FlagI.
3195 /// ImmValue: immediate for FlagI if it takes an immediate.
3196 inline static bool isRedundantFlagInstr(MachineInstr *FlagI, unsigned SrcReg,
3197                                         unsigned SrcReg2, int ImmValue,
3198                                         MachineInstr *OI) {
3199   if (((FlagI->getOpcode() == X86::CMP64rr &&
3200         OI->getOpcode() == X86::SUB64rr) ||
3201        (FlagI->getOpcode() == X86::CMP32rr &&
3202         OI->getOpcode() == X86::SUB32rr)||
3203        (FlagI->getOpcode() == X86::CMP16rr &&
3204         OI->getOpcode() == X86::SUB16rr)||
3205        (FlagI->getOpcode() == X86::CMP8rr &&
3206         OI->getOpcode() == X86::SUB8rr)) &&
3207       ((OI->getOperand(1).getReg() == SrcReg &&
3208         OI->getOperand(2).getReg() == SrcReg2) ||
3209        (OI->getOperand(1).getReg() == SrcReg2 &&
3210         OI->getOperand(2).getReg() == SrcReg)))
3211     return true;
3212
3213   if (((FlagI->getOpcode() == X86::CMP64ri32 &&
3214         OI->getOpcode() == X86::SUB64ri32) ||
3215        (FlagI->getOpcode() == X86::CMP64ri8 &&
3216         OI->getOpcode() == X86::SUB64ri8) ||
3217        (FlagI->getOpcode() == X86::CMP32ri &&
3218         OI->getOpcode() == X86::SUB32ri) ||
3219        (FlagI->getOpcode() == X86::CMP32ri8 &&
3220         OI->getOpcode() == X86::SUB32ri8) ||
3221        (FlagI->getOpcode() == X86::CMP16ri &&
3222         OI->getOpcode() == X86::SUB16ri) ||
3223        (FlagI->getOpcode() == X86::CMP16ri8 &&
3224         OI->getOpcode() == X86::SUB16ri8) ||
3225        (FlagI->getOpcode() == X86::CMP8ri &&
3226         OI->getOpcode() == X86::SUB8ri)) &&
3227       OI->getOperand(1).getReg() == SrcReg &&
3228       OI->getOperand(2).getImm() == ImmValue)
3229     return true;
3230   return false;
3231 }
3232
3233 /// isDefConvertible - check whether the definition can be converted
3234 /// to remove a comparison against zero.
3235 inline static bool isDefConvertible(MachineInstr *MI) {
3236   switch (MI->getOpcode()) {
3237   default: return false;
3238
3239   // The shift instructions only modify ZF if their shift count is non-zero.
3240   // N.B.: The processor truncates the shift count depending on the encoding.
3241   case X86::SAR8ri:    case X86::SAR16ri:  case X86::SAR32ri:case X86::SAR64ri:
3242   case X86::SHR8ri:    case X86::SHR16ri:  case X86::SHR32ri:case X86::SHR64ri:
3243      return getTruncatedShiftCount(MI, 2) != 0;
3244
3245   // Some left shift instructions can be turned into LEA instructions but only
3246   // if their flags aren't used. Avoid transforming such instructions.
3247   case X86::SHL8ri:    case X86::SHL16ri:  case X86::SHL32ri:case X86::SHL64ri:{
3248     unsigned ShAmt = getTruncatedShiftCount(MI, 2);
3249     if (isTruncatedShiftCountForLEA(ShAmt)) return false;
3250     return ShAmt != 0;
3251   }
3252
3253   case X86::SHRD16rri8:case X86::SHRD32rri8:case X86::SHRD64rri8:
3254   case X86::SHLD16rri8:case X86::SHLD32rri8:case X86::SHLD64rri8:
3255      return getTruncatedShiftCount(MI, 3) != 0;
3256
3257   case X86::SUB64ri32: case X86::SUB64ri8: case X86::SUB32ri:
3258   case X86::SUB32ri8:  case X86::SUB16ri:  case X86::SUB16ri8:
3259   case X86::SUB8ri:    case X86::SUB64rr:  case X86::SUB32rr:
3260   case X86::SUB16rr:   case X86::SUB8rr:   case X86::SUB64rm:
3261   case X86::SUB32rm:   case X86::SUB16rm:  case X86::SUB8rm:
3262   case X86::DEC64r:    case X86::DEC32r:   case X86::DEC16r: case X86::DEC8r:
3263   case X86::DEC64_32r: case X86::DEC64_16r:
3264   case X86::ADD64ri32: case X86::ADD64ri8: case X86::ADD32ri:
3265   case X86::ADD32ri8:  case X86::ADD16ri:  case X86::ADD16ri8:
3266   case X86::ADD8ri:    case X86::ADD64rr:  case X86::ADD32rr:
3267   case X86::ADD16rr:   case X86::ADD8rr:   case X86::ADD64rm:
3268   case X86::ADD32rm:   case X86::ADD16rm:  case X86::ADD8rm:
3269   case X86::INC64r:    case X86::INC32r:   case X86::INC16r: case X86::INC8r:
3270   case X86::INC64_32r: case X86::INC64_16r:
3271   case X86::AND64ri32: case X86::AND64ri8: case X86::AND32ri:
3272   case X86::AND32ri8:  case X86::AND16ri:  case X86::AND16ri8:
3273   case X86::AND8ri:    case X86::AND64rr:  case X86::AND32rr:
3274   case X86::AND16rr:   case X86::AND8rr:   case X86::AND64rm:
3275   case X86::AND32rm:   case X86::AND16rm:  case X86::AND8rm:
3276   case X86::XOR64ri32: case X86::XOR64ri8: case X86::XOR32ri:
3277   case X86::XOR32ri8:  case X86::XOR16ri:  case X86::XOR16ri8:
3278   case X86::XOR8ri:    case X86::XOR64rr:  case X86::XOR32rr:
3279   case X86::XOR16rr:   case X86::XOR8rr:   case X86::XOR64rm:
3280   case X86::XOR32rm:   case X86::XOR16rm:  case X86::XOR8rm:
3281   case X86::OR64ri32:  case X86::OR64ri8:  case X86::OR32ri:
3282   case X86::OR32ri8:   case X86::OR16ri:   case X86::OR16ri8:
3283   case X86::OR8ri:     case X86::OR64rr:   case X86::OR32rr:
3284   case X86::OR16rr:    case X86::OR8rr:    case X86::OR64rm:
3285   case X86::OR32rm:    case X86::OR16rm:   case X86::OR8rm:
3286   case X86::NEG8r:     case X86::NEG16r:   case X86::NEG32r: case X86::NEG64r:
3287   case X86::SAR8r1:    case X86::SAR16r1:  case X86::SAR32r1:case X86::SAR64r1:
3288   case X86::SHR8r1:    case X86::SHR16r1:  case X86::SHR32r1:case X86::SHR64r1:
3289   case X86::SHL8r1:    case X86::SHL16r1:  case X86::SHL32r1:case X86::SHL64r1:
3290   case X86::ADC32ri:   case X86::ADC32ri8:
3291   case X86::ADC32rr:   case X86::ADC64ri32:
3292   case X86::ADC64ri8:  case X86::ADC64rr:
3293   case X86::SBB32ri:   case X86::SBB32ri8:
3294   case X86::SBB32rr:   case X86::SBB64ri32:
3295   case X86::SBB64ri8:  case X86::SBB64rr:
3296   case X86::ANDN32rr:  case X86::ANDN32rm:
3297   case X86::ANDN64rr:  case X86::ANDN64rm:
3298   case X86::BEXTR32rr: case X86::BEXTR64rr:
3299   case X86::BEXTR32rm: case X86::BEXTR64rm:
3300   case X86::BLSI32rr:  case X86::BLSI32rm:
3301   case X86::BLSI64rr:  case X86::BLSI64rm:
3302   case X86::BLSMSK32rr:case X86::BLSMSK32rm:
3303   case X86::BLSMSK64rr:case X86::BLSMSK64rm:
3304   case X86::BLSR32rr:  case X86::BLSR32rm:
3305   case X86::BLSR64rr:  case X86::BLSR64rm:
3306   case X86::BZHI32rr:  case X86::BZHI32rm:
3307   case X86::BZHI64rr:  case X86::BZHI64rm:
3308   case X86::LZCNT16rr: case X86::LZCNT16rm:
3309   case X86::LZCNT32rr: case X86::LZCNT32rm:
3310   case X86::LZCNT64rr: case X86::LZCNT64rm:
3311   case X86::POPCNT16rr:case X86::POPCNT16rm:
3312   case X86::POPCNT32rr:case X86::POPCNT32rm:
3313   case X86::POPCNT64rr:case X86::POPCNT64rm:
3314   case X86::TZCNT16rr: case X86::TZCNT16rm:
3315   case X86::TZCNT32rr: case X86::TZCNT32rm:
3316   case X86::TZCNT64rr: case X86::TZCNT64rm:
3317     return true;
3318   }
3319 }
3320
3321 /// optimizeCompareInstr - Check if there exists an earlier instruction that
3322 /// operates on the same source operands and sets flags in the same way as
3323 /// Compare; remove Compare if possible.
3324 bool X86InstrInfo::
3325 optimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg, unsigned SrcReg2,
3326                      int CmpMask, int CmpValue,
3327                      const MachineRegisterInfo *MRI) const {
3328   // Check whether we can replace SUB with CMP.
3329   unsigned NewOpcode = 0;
3330   switch (CmpInstr->getOpcode()) {
3331   default: break;
3332   case X86::SUB64ri32:
3333   case X86::SUB64ri8:
3334   case X86::SUB32ri:
3335   case X86::SUB32ri8:
3336   case X86::SUB16ri:
3337   case X86::SUB16ri8:
3338   case X86::SUB8ri:
3339   case X86::SUB64rm:
3340   case X86::SUB32rm:
3341   case X86::SUB16rm:
3342   case X86::SUB8rm:
3343   case X86::SUB64rr:
3344   case X86::SUB32rr:
3345   case X86::SUB16rr:
3346   case X86::SUB8rr: {
3347     if (!MRI->use_nodbg_empty(CmpInstr->getOperand(0).getReg()))
3348       return false;
3349     // There is no use of the destination register, we can replace SUB with CMP.
3350     switch (CmpInstr->getOpcode()) {
3351     default: llvm_unreachable("Unreachable!");
3352     case X86::SUB64rm:   NewOpcode = X86::CMP64rm;   break;
3353     case X86::SUB32rm:   NewOpcode = X86::CMP32rm;   break;
3354     case X86::SUB16rm:   NewOpcode = X86::CMP16rm;   break;
3355     case X86::SUB8rm:    NewOpcode = X86::CMP8rm;    break;
3356     case X86::SUB64rr:   NewOpcode = X86::CMP64rr;   break;
3357     case X86::SUB32rr:   NewOpcode = X86::CMP32rr;   break;
3358     case X86::SUB16rr:   NewOpcode = X86::CMP16rr;   break;
3359     case X86::SUB8rr:    NewOpcode = X86::CMP8rr;    break;
3360     case X86::SUB64ri32: NewOpcode = X86::CMP64ri32; break;
3361     case X86::SUB64ri8:  NewOpcode = X86::CMP64ri8;  break;
3362     case X86::SUB32ri:   NewOpcode = X86::CMP32ri;   break;
3363     case X86::SUB32ri8:  NewOpcode = X86::CMP32ri8;  break;
3364     case X86::SUB16ri:   NewOpcode = X86::CMP16ri;   break;
3365     case X86::SUB16ri8:  NewOpcode = X86::CMP16ri8;  break;
3366     case X86::SUB8ri:    NewOpcode = X86::CMP8ri;    break;
3367     }
3368     CmpInstr->setDesc(get(NewOpcode));
3369     CmpInstr->RemoveOperand(0);
3370     // Fall through to optimize Cmp if Cmp is CMPrr or CMPri.
3371     if (NewOpcode == X86::CMP64rm || NewOpcode == X86::CMP32rm ||
3372         NewOpcode == X86::CMP16rm || NewOpcode == X86::CMP8rm)
3373       return false;
3374   }
3375   }
3376
3377   // Get the unique definition of SrcReg.
3378   MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
3379   if (!MI) return false;
3380
3381   // CmpInstr is the first instruction of the BB.
3382   MachineBasicBlock::iterator I = CmpInstr, Def = MI;
3383
3384   // If we are comparing against zero, check whether we can use MI to update
3385   // EFLAGS. If MI is not in the same BB as CmpInstr, do not optimize.
3386   bool IsCmpZero = (SrcReg2 == 0 && CmpValue == 0);
3387   if (IsCmpZero && (MI->getParent() != CmpInstr->getParent() ||
3388       !isDefConvertible(MI)))
3389     return false;
3390
3391   // We are searching for an earlier instruction that can make CmpInstr
3392   // redundant and that instruction will be saved in Sub.
3393   MachineInstr *Sub = NULL;
3394   const TargetRegisterInfo *TRI = &getRegisterInfo();
3395
3396   // We iterate backward, starting from the instruction before CmpInstr and
3397   // stop when reaching the definition of a source register or done with the BB.
3398   // RI points to the instruction before CmpInstr.
3399   // If the definition is in this basic block, RE points to the definition;
3400   // otherwise, RE is the rend of the basic block.
3401   MachineBasicBlock::reverse_iterator
3402       RI = MachineBasicBlock::reverse_iterator(I),
3403       RE = CmpInstr->getParent() == MI->getParent() ?
3404            MachineBasicBlock::reverse_iterator(++Def) /* points to MI */ :
3405            CmpInstr->getParent()->rend();
3406   MachineInstr *Movr0Inst = 0;
3407   for (; RI != RE; ++RI) {
3408     MachineInstr *Instr = &*RI;
3409     // Check whether CmpInstr can be made redundant by the current instruction.
3410     if (!IsCmpZero &&
3411         isRedundantFlagInstr(CmpInstr, SrcReg, SrcReg2, CmpValue, Instr)) {
3412       Sub = Instr;
3413       break;
3414     }
3415
3416     if (Instr->modifiesRegister(X86::EFLAGS, TRI) ||
3417         Instr->readsRegister(X86::EFLAGS, TRI)) {
3418       // This instruction modifies or uses EFLAGS.
3419
3420       // MOV32r0 etc. are implemented with xor which clobbers condition code.
3421       // They are safe to move up, if the definition to EFLAGS is dead and
3422       // earlier instructions do not read or write EFLAGS.
3423       if (!Movr0Inst && Instr->getOpcode() == X86::MOV32r0 &&
3424           Instr->registerDefIsDead(X86::EFLAGS, TRI)) {
3425         Movr0Inst = Instr;
3426         continue;
3427       }
3428
3429       // We can't remove CmpInstr.
3430       return false;
3431     }
3432   }
3433
3434   // Return false if no candidates exist.
3435   if (!IsCmpZero && !Sub)
3436     return false;
3437
3438   bool IsSwapped = (SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
3439                     Sub->getOperand(2).getReg() == SrcReg);
3440
3441   // Scan forward from the instruction after CmpInstr for uses of EFLAGS.
3442   // It is safe to remove CmpInstr if EFLAGS is redefined or killed.
3443   // If we are done with the basic block, we need to check whether EFLAGS is
3444   // live-out.
3445   bool IsSafe = false;
3446   SmallVector<std::pair<MachineInstr*, unsigned /*NewOpc*/>, 4> OpsToUpdate;
3447   MachineBasicBlock::iterator E = CmpInstr->getParent()->end();
3448   for (++I; I != E; ++I) {
3449     const MachineInstr &Instr = *I;
3450     bool ModifyEFLAGS = Instr.modifiesRegister(X86::EFLAGS, TRI);
3451     bool UseEFLAGS = Instr.readsRegister(X86::EFLAGS, TRI);
3452     // We should check the usage if this instruction uses and updates EFLAGS.
3453     if (!UseEFLAGS && ModifyEFLAGS) {
3454       // It is safe to remove CmpInstr if EFLAGS is updated again.
3455       IsSafe = true;
3456       break;
3457     }
3458     if (!UseEFLAGS && !ModifyEFLAGS)
3459       continue;
3460
3461     // EFLAGS is used by this instruction.
3462     X86::CondCode OldCC;
3463     bool OpcIsSET = false;
3464     if (IsCmpZero || IsSwapped) {
3465       // We decode the condition code from opcode.
3466       if (Instr.isBranch())
3467         OldCC = getCondFromBranchOpc(Instr.getOpcode());
3468       else {
3469         OldCC = getCondFromSETOpc(Instr.getOpcode());
3470         if (OldCC != X86::COND_INVALID)
3471           OpcIsSET = true;
3472         else
3473           OldCC = X86::getCondFromCMovOpc(Instr.getOpcode());
3474       }
3475       if (OldCC == X86::COND_INVALID) return false;
3476     }
3477     if (IsCmpZero) {
3478       switch (OldCC) {
3479       default: break;
3480       case X86::COND_A: case X86::COND_AE:
3481       case X86::COND_B: case X86::COND_BE:
3482       case X86::COND_G: case X86::COND_GE:
3483       case X86::COND_L: case X86::COND_LE:
3484       case X86::COND_O: case X86::COND_NO:
3485         // CF and OF are used, we can't perform this optimization.
3486         return false;
3487       }
3488     } else if (IsSwapped) {
3489       // If we have SUB(r1, r2) and CMP(r2, r1), the condition code needs
3490       // to be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
3491       // We swap the condition code and synthesize the new opcode.
3492       X86::CondCode NewCC = getSwappedCondition(OldCC);
3493       if (NewCC == X86::COND_INVALID) return false;
3494
3495       // Synthesize the new opcode.
3496       bool HasMemoryOperand = Instr.hasOneMemOperand();
3497       unsigned NewOpc;
3498       if (Instr.isBranch())
3499         NewOpc = GetCondBranchFromCond(NewCC);
3500       else if(OpcIsSET)
3501         NewOpc = getSETFromCond(NewCC, HasMemoryOperand);
3502       else {
3503         unsigned DstReg = Instr.getOperand(0).getReg();
3504         NewOpc = getCMovFromCond(NewCC, MRI->getRegClass(DstReg)->getSize(),
3505                                  HasMemoryOperand);
3506       }
3507
3508       // Push the MachineInstr to OpsToUpdate.
3509       // If it is safe to remove CmpInstr, the condition code of these
3510       // instructions will be modified.
3511       OpsToUpdate.push_back(std::make_pair(&*I, NewOpc));
3512     }
3513     if (ModifyEFLAGS || Instr.killsRegister(X86::EFLAGS, TRI)) {
3514       // It is safe to remove CmpInstr if EFLAGS is updated again or killed.
3515       IsSafe = true;
3516       break;
3517     }
3518   }
3519
3520   // If EFLAGS is not killed nor re-defined, we should check whether it is
3521   // live-out. If it is live-out, do not optimize.
3522   if ((IsCmpZero || IsSwapped) && !IsSafe) {
3523     MachineBasicBlock *MBB = CmpInstr->getParent();
3524     for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
3525              SE = MBB->succ_end(); SI != SE; ++SI)
3526       if ((*SI)->isLiveIn(X86::EFLAGS))
3527         return false;
3528   }
3529
3530   // The instruction to be updated is either Sub or MI.
3531   Sub = IsCmpZero ? MI : Sub;
3532   // Move Movr0Inst to the appropriate place before Sub.
3533   if (Movr0Inst) {
3534     // Look backwards until we find a def that doesn't use the current EFLAGS.
3535     Def = Sub;
3536     MachineBasicBlock::reverse_iterator
3537       InsertI = MachineBasicBlock::reverse_iterator(++Def),
3538                 InsertE = Sub->getParent()->rend();
3539     for (; InsertI != InsertE; ++InsertI) {
3540       MachineInstr *Instr = &*InsertI;
3541       if (!Instr->readsRegister(X86::EFLAGS, TRI) &&
3542           Instr->modifiesRegister(X86::EFLAGS, TRI)) {
3543         Sub->getParent()->remove(Movr0Inst);
3544         Instr->getParent()->insert(MachineBasicBlock::iterator(Instr),
3545                                    Movr0Inst);
3546         break;
3547       }
3548     }
3549     if (InsertI == InsertE)
3550       return false;
3551   }
3552
3553   // Make sure Sub instruction defines EFLAGS and mark the def live.
3554   unsigned i = 0, e = Sub->getNumOperands();
3555   for (; i != e; ++i) {
3556     MachineOperand &MO = Sub->getOperand(i);
3557     if (MO.isReg() && MO.isDef() && MO.getReg() == X86::EFLAGS) {
3558       MO.setIsDead(false);
3559       break;
3560     }
3561   }
3562   assert(i != e && "Unable to locate a def EFLAGS operand");
3563
3564   CmpInstr->eraseFromParent();
3565
3566   // Modify the condition code of instructions in OpsToUpdate.
3567   for (unsigned i = 0, e = OpsToUpdate.size(); i < e; i++)
3568     OpsToUpdate[i].first->setDesc(get(OpsToUpdate[i].second));
3569   return true;
3570 }
3571
3572 /// optimizeLoadInstr - Try to remove the load by folding it to a register
3573 /// operand at the use. We fold the load instructions if load defines a virtual
3574 /// register, the virtual register is used once in the same BB, and the
3575 /// instructions in-between do not load or store, and have no side effects.
3576 MachineInstr* X86InstrInfo::
3577 optimizeLoadInstr(MachineInstr *MI, const MachineRegisterInfo *MRI,
3578                   unsigned &FoldAsLoadDefReg,
3579                   MachineInstr *&DefMI) const {
3580   if (FoldAsLoadDefReg == 0)
3581     return 0;
3582   // To be conservative, if there exists another load, clear the load candidate.
3583   if (MI->mayLoad()) {
3584     FoldAsLoadDefReg = 0;
3585     return 0;
3586   }
3587
3588   // Check whether we can move DefMI here.
3589   DefMI = MRI->getVRegDef(FoldAsLoadDefReg);
3590   assert(DefMI);
3591   bool SawStore = false;
3592   if (!DefMI->isSafeToMove(this, 0, SawStore))
3593     return 0;
3594
3595   // We try to commute MI if possible.
3596   unsigned IdxEnd = (MI->isCommutable()) ? 2 : 1;
3597   for (unsigned Idx = 0; Idx < IdxEnd; Idx++) {
3598     // Collect information about virtual register operands of MI.
3599     unsigned SrcOperandId = 0;
3600     bool FoundSrcOperand = false;
3601     for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) {
3602       MachineOperand &MO = MI->getOperand(i);
3603       if (!MO.isReg())
3604         continue;
3605       unsigned Reg = MO.getReg();
3606       if (Reg != FoldAsLoadDefReg)
3607         continue;
3608       // Do not fold if we have a subreg use or a def or multiple uses.
3609       if (MO.getSubReg() || MO.isDef() || FoundSrcOperand)
3610         return 0;
3611
3612       SrcOperandId = i;
3613       FoundSrcOperand = true;
3614     }
3615     if (!FoundSrcOperand) return 0;
3616
3617     // Check whether we can fold the def into SrcOperandId.
3618     SmallVector<unsigned, 8> Ops;
3619     Ops.push_back(SrcOperandId);
3620     MachineInstr *FoldMI = foldMemoryOperand(MI, Ops, DefMI);
3621     if (FoldMI) {
3622       FoldAsLoadDefReg = 0;
3623       return FoldMI;
3624     }
3625
3626     if (Idx == 1) {
3627       // MI was changed but it didn't help, commute it back!
3628       commuteInstruction(MI, false);
3629       return 0;
3630     }
3631
3632     // Check whether we can commute MI and enable folding.
3633     if (MI->isCommutable()) {
3634       MachineInstr *NewMI = commuteInstruction(MI, false);
3635       // Unable to commute.
3636       if (!NewMI) return 0;
3637       if (NewMI != MI) {
3638         // New instruction. It doesn't need to be kept.
3639         NewMI->eraseFromParent();
3640         return 0;
3641       }
3642     }
3643   }
3644   return 0;
3645 }
3646
3647 /// Expand2AddrUndef - Expand a single-def pseudo instruction to a two-addr
3648 /// instruction with two undef reads of the register being defined.  This is
3649 /// used for mapping:
3650 ///   %xmm4 = V_SET0
3651 /// to:
3652 ///   %xmm4 = PXORrr %xmm4<undef>, %xmm4<undef>
3653 ///
3654 static bool Expand2AddrUndef(MachineInstrBuilder &MIB,
3655                              const MCInstrDesc &Desc) {
3656   assert(Desc.getNumOperands() == 3 && "Expected two-addr instruction.");
3657   unsigned Reg = MIB->getOperand(0).getReg();
3658   MIB->setDesc(Desc);
3659
3660   // MachineInstr::addOperand() will insert explicit operands before any
3661   // implicit operands.
3662   MIB.addReg(Reg, RegState::Undef).addReg(Reg, RegState::Undef);
3663   // But we don't trust that.
3664   assert(MIB->getOperand(1).getReg() == Reg &&
3665          MIB->getOperand(2).getReg() == Reg && "Misplaced operand");
3666   return true;
3667 }
3668
3669 bool X86InstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
3670   bool HasAVX = TM.getSubtarget<X86Subtarget>().hasAVX();
3671   MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
3672   switch (MI->getOpcode()) {
3673   case X86::SETB_C8r:
3674     return Expand2AddrUndef(MIB, get(X86::SBB8rr));
3675   case X86::SETB_C16r:
3676     return Expand2AddrUndef(MIB, get(X86::SBB16rr));
3677   case X86::SETB_C32r:
3678     return Expand2AddrUndef(MIB, get(X86::SBB32rr));
3679   case X86::SETB_C64r:
3680     return Expand2AddrUndef(MIB, get(X86::SBB64rr));
3681   case X86::V_SET0:
3682   case X86::FsFLD0SS:
3683   case X86::FsFLD0SD:
3684     return Expand2AddrUndef(MIB, get(HasAVX ? X86::VXORPSrr : X86::XORPSrr));
3685   case X86::AVX_SET0:
3686     assert(HasAVX && "AVX not supported");
3687     return Expand2AddrUndef(MIB, get(X86::VXORPSYrr));
3688   case X86::V_SETALLONES:
3689     return Expand2AddrUndef(MIB, get(HasAVX ? X86::VPCMPEQDrr : X86::PCMPEQDrr));
3690   case X86::AVX2_SETALLONES:
3691     return Expand2AddrUndef(MIB, get(X86::VPCMPEQDYrr));
3692   case X86::TEST8ri_NOREX:
3693     MI->setDesc(get(X86::TEST8ri));
3694     return true;
3695   }
3696   return false;
3697 }
3698
3699 MachineInstr*
3700 X86InstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
3701                                        int FrameIx, uint64_t Offset,
3702                                        const MDNode *MDPtr,
3703                                        DebugLoc DL) const {
3704   X86AddressMode AM;
3705   AM.BaseType = X86AddressMode::FrameIndexBase;
3706   AM.Base.FrameIndex = FrameIx;
3707   MachineInstrBuilder MIB = BuildMI(MF, DL, get(X86::DBG_VALUE));
3708   addFullAddress(MIB, AM).addImm(Offset).addMetadata(MDPtr);
3709   return &*MIB;
3710 }
3711
3712 static MachineInstr *FuseTwoAddrInst(MachineFunction &MF, unsigned Opcode,
3713                                      const SmallVectorImpl<MachineOperand> &MOs,
3714                                      MachineInstr *MI,
3715                                      const TargetInstrInfo &TII) {
3716   // Create the base instruction with the memory operand as the first part.
3717   // Omit the implicit operands, something BuildMI can't do.
3718   MachineInstr *NewMI = MF.CreateMachineInstr(TII.get(Opcode),
3719                                               MI->getDebugLoc(), true);
3720   MachineInstrBuilder MIB(MF, NewMI);
3721   unsigned NumAddrOps = MOs.size();
3722   for (unsigned i = 0; i != NumAddrOps; ++i)
3723     MIB.addOperand(MOs[i]);
3724   if (NumAddrOps < 4)  // FrameIndex only
3725     addOffset(MIB, 0);
3726
3727   // Loop over the rest of the ri operands, converting them over.
3728   unsigned NumOps = MI->getDesc().getNumOperands()-2;
3729   for (unsigned i = 0; i != NumOps; ++i) {
3730     MachineOperand &MO = MI->getOperand(i+2);
3731     MIB.addOperand(MO);
3732   }
3733   for (unsigned i = NumOps+2, e = MI->getNumOperands(); i != e; ++i) {
3734     MachineOperand &MO = MI->getOperand(i);
3735     MIB.addOperand(MO);
3736   }
3737   return MIB;
3738 }
3739
3740 static MachineInstr *FuseInst(MachineFunction &MF,
3741                               unsigned Opcode, unsigned OpNo,
3742                               const SmallVectorImpl<MachineOperand> &MOs,
3743                               MachineInstr *MI, const TargetInstrInfo &TII) {
3744   // Omit the implicit operands, something BuildMI can't do.
3745   MachineInstr *NewMI = MF.CreateMachineInstr(TII.get(Opcode),
3746                                               MI->getDebugLoc(), true);
3747   MachineInstrBuilder MIB(MF, NewMI);
3748
3749   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
3750     MachineOperand &MO = MI->getOperand(i);
3751     if (i == OpNo) {
3752       assert(MO.isReg() && "Expected to fold into reg operand!");
3753       unsigned NumAddrOps = MOs.size();
3754       for (unsigned i = 0; i != NumAddrOps; ++i)
3755         MIB.addOperand(MOs[i]);
3756       if (NumAddrOps < 4)  // FrameIndex only
3757         addOffset(MIB, 0);
3758     } else {
3759       MIB.addOperand(MO);
3760     }
3761   }
3762   return MIB;
3763 }
3764
3765 static MachineInstr *MakeM0Inst(const TargetInstrInfo &TII, unsigned Opcode,
3766                                 const SmallVectorImpl<MachineOperand> &MOs,
3767                                 MachineInstr *MI) {
3768   MachineFunction &MF = *MI->getParent()->getParent();
3769   MachineInstrBuilder MIB = BuildMI(MF, MI->getDebugLoc(), TII.get(Opcode));
3770
3771   unsigned NumAddrOps = MOs.size();
3772   for (unsigned i = 0; i != NumAddrOps; ++i)
3773     MIB.addOperand(MOs[i]);
3774   if (NumAddrOps < 4)  // FrameIndex only
3775     addOffset(MIB, 0);
3776   return MIB.addImm(0);
3777 }
3778
3779 MachineInstr*
3780 X86InstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
3781                                     MachineInstr *MI, unsigned i,
3782                                     const SmallVectorImpl<MachineOperand> &MOs,
3783                                     unsigned Size, unsigned Align) const {
3784   const DenseMap<unsigned, std::pair<unsigned,unsigned> > *OpcodeTablePtr = 0;
3785   bool isCallRegIndirect = TM.getSubtarget<X86Subtarget>().callRegIndirect();
3786   bool isTwoAddrFold = false;
3787
3788   // Atom favors register form of call. So, we do not fold loads into calls
3789   // when X86Subtarget is Atom.
3790   if (isCallRegIndirect &&
3791     (MI->getOpcode() == X86::CALL32r || MI->getOpcode() == X86::CALL64r)) {
3792     return NULL;
3793   }
3794
3795   unsigned NumOps = MI->getDesc().getNumOperands();
3796   bool isTwoAddr = NumOps > 1 &&
3797     MI->getDesc().getOperandConstraint(1, MCOI::TIED_TO) != -1;
3798
3799   // FIXME: AsmPrinter doesn't know how to handle
3800   // X86II::MO_GOT_ABSOLUTE_ADDRESS after folding.
3801   if (MI->getOpcode() == X86::ADD32ri &&
3802       MI->getOperand(2).getTargetFlags() == X86II::MO_GOT_ABSOLUTE_ADDRESS)
3803     return NULL;
3804
3805   MachineInstr *NewMI = NULL;
3806   // Folding a memory location into the two-address part of a two-address
3807   // instruction is different than folding it other places.  It requires
3808   // replacing the *two* registers with the memory location.
3809   if (isTwoAddr && NumOps >= 2 && i < 2 &&
3810       MI->getOperand(0).isReg() &&
3811       MI->getOperand(1).isReg() &&
3812       MI->getOperand(0).getReg() == MI->getOperand(1).getReg()) {
3813     OpcodeTablePtr = &RegOp2MemOpTable2Addr;
3814     isTwoAddrFold = true;
3815   } else if (i == 0) { // If operand 0
3816     if (MI->getOpcode() == X86::MOV32r0) {
3817       NewMI = MakeM0Inst(*this, X86::MOV32mi, MOs, MI);
3818       if (NewMI)
3819         return NewMI;
3820     }
3821
3822     OpcodeTablePtr = &RegOp2MemOpTable0;
3823   } else if (i == 1) {
3824     OpcodeTablePtr = &RegOp2MemOpTable1;
3825   } else if (i == 2) {
3826     OpcodeTablePtr = &RegOp2MemOpTable2;
3827   } else if (i == 3) {
3828     OpcodeTablePtr = &RegOp2MemOpTable3;
3829   }
3830
3831   // If table selected...
3832   if (OpcodeTablePtr) {
3833     // Find the Opcode to fuse
3834     DenseMap<unsigned, std::pair<unsigned,unsigned> >::const_iterator I =
3835       OpcodeTablePtr->find(MI->getOpcode());
3836     if (I != OpcodeTablePtr->end()) {
3837       unsigned Opcode = I->second.first;
3838       unsigned MinAlign = (I->second.second & TB_ALIGN_MASK) >> TB_ALIGN_SHIFT;
3839       if (Align < MinAlign)
3840         return NULL;
3841       bool NarrowToMOV32rm = false;
3842       if (Size) {
3843         unsigned RCSize = getRegClass(MI->getDesc(), i, &RI, MF)->getSize();
3844         if (Size < RCSize) {
3845           // Check if it's safe to fold the load. If the size of the object is
3846           // narrower than the load width, then it's not.
3847           if (Opcode != X86::MOV64rm || RCSize != 8 || Size != 4)
3848             return NULL;
3849           // If this is a 64-bit load, but the spill slot is 32, then we can do
3850           // a 32-bit load which is implicitly zero-extended. This likely is due
3851           // to liveintervalanalysis remat'ing a load from stack slot.
3852           if (MI->getOperand(0).getSubReg() || MI->getOperand(1).getSubReg())
3853             return NULL;
3854           Opcode = X86::MOV32rm;
3855           NarrowToMOV32rm = true;
3856         }
3857       }
3858
3859       if (isTwoAddrFold)
3860         NewMI = FuseTwoAddrInst(MF, Opcode, MOs, MI, *this);
3861       else
3862         NewMI = FuseInst(MF, Opcode, i, MOs, MI, *this);
3863
3864       if (NarrowToMOV32rm) {
3865         // If this is the special case where we use a MOV32rm to load a 32-bit
3866         // value and zero-extend the top bits. Change the destination register
3867         // to a 32-bit one.
3868         unsigned DstReg = NewMI->getOperand(0).getReg();
3869         if (TargetRegisterInfo::isPhysicalRegister(DstReg))
3870           NewMI->getOperand(0).setReg(RI.getSubReg(DstReg,
3871                                                    X86::sub_32bit));
3872         else
3873           NewMI->getOperand(0).setSubReg(X86::sub_32bit);
3874       }
3875       return NewMI;
3876     }
3877   }
3878
3879   // No fusion
3880   if (PrintFailedFusing && !MI->isCopy())
3881     dbgs() << "We failed to fuse operand " << i << " in " << *MI;
3882   return NULL;
3883 }
3884
3885 /// hasPartialRegUpdate - Return true for all instructions that only update
3886 /// the first 32 or 64-bits of the destination register and leave the rest
3887 /// unmodified. This can be used to avoid folding loads if the instructions
3888 /// only update part of the destination register, and the non-updated part is
3889 /// not needed. e.g. cvtss2sd, sqrtss. Unfolding the load from these
3890 /// instructions breaks the partial register dependency and it can improve
3891 /// performance. e.g.:
3892 ///
3893 ///   movss (%rdi), %xmm0
3894 ///   cvtss2sd %xmm0, %xmm0
3895 ///
3896 /// Instead of
3897 ///   cvtss2sd (%rdi), %xmm0
3898 ///
3899 /// FIXME: This should be turned into a TSFlags.
3900 ///
3901 static bool hasPartialRegUpdate(unsigned Opcode) {
3902   switch (Opcode) {
3903   case X86::CVTSI2SSrr:
3904   case X86::CVTSI2SS64rr:
3905   case X86::CVTSI2SDrr:
3906   case X86::CVTSI2SD64rr:
3907   case X86::CVTSD2SSrr:
3908   case X86::Int_CVTSD2SSrr:
3909   case X86::CVTSS2SDrr:
3910   case X86::Int_CVTSS2SDrr:
3911   case X86::RCPSSr:
3912   case X86::RCPSSr_Int:
3913   case X86::ROUNDSDr:
3914   case X86::ROUNDSDr_Int:
3915   case X86::ROUNDSSr:
3916   case X86::ROUNDSSr_Int:
3917   case X86::RSQRTSSr:
3918   case X86::RSQRTSSr_Int:
3919   case X86::SQRTSSr:
3920   case X86::SQRTSSr_Int:
3921   // AVX encoded versions
3922   case X86::VCVTSD2SSrr:
3923   case X86::Int_VCVTSD2SSrr:
3924   case X86::VCVTSS2SDrr:
3925   case X86::Int_VCVTSS2SDrr:
3926   case X86::VRCPSSr:
3927   case X86::VROUNDSDr:
3928   case X86::VROUNDSDr_Int:
3929   case X86::VROUNDSSr:
3930   case X86::VROUNDSSr_Int:
3931   case X86::VRSQRTSSr:
3932   case X86::VSQRTSSr:
3933     return true;
3934   }
3935
3936   return false;
3937 }
3938
3939 /// getPartialRegUpdateClearance - Inform the ExeDepsFix pass how many idle
3940 /// instructions we would like before a partial register update.
3941 unsigned X86InstrInfo::
3942 getPartialRegUpdateClearance(const MachineInstr *MI, unsigned OpNum,
3943                              const TargetRegisterInfo *TRI) const {
3944   if (OpNum != 0 || !hasPartialRegUpdate(MI->getOpcode()))
3945     return 0;
3946
3947   // If MI is marked as reading Reg, the partial register update is wanted.
3948   const MachineOperand &MO = MI->getOperand(0);
3949   unsigned Reg = MO.getReg();
3950   if (TargetRegisterInfo::isVirtualRegister(Reg)) {
3951     if (MO.readsReg() || MI->readsVirtualRegister(Reg))
3952       return 0;
3953   } else {
3954     if (MI->readsRegister(Reg, TRI))
3955       return 0;
3956   }
3957
3958   // If any of the preceding 16 instructions are reading Reg, insert a
3959   // dependency breaking instruction.  The magic number is based on a few
3960   // Nehalem experiments.
3961   return 16;
3962 }
3963
3964 void X86InstrInfo::
3965 breakPartialRegDependency(MachineBasicBlock::iterator MI, unsigned OpNum,
3966                           const TargetRegisterInfo *TRI) const {
3967   unsigned Reg = MI->getOperand(OpNum).getReg();
3968   if (X86::VR128RegClass.contains(Reg)) {
3969     // These instructions are all floating point domain, so xorps is the best
3970     // choice.
3971     bool HasAVX = TM.getSubtarget<X86Subtarget>().hasAVX();
3972     unsigned Opc = HasAVX ? X86::VXORPSrr : X86::XORPSrr;
3973     BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), get(Opc), Reg)
3974       .addReg(Reg, RegState::Undef).addReg(Reg, RegState::Undef);
3975   } else if (X86::VR256RegClass.contains(Reg)) {
3976     // Use vxorps to clear the full ymm register.
3977     // It wants to read and write the xmm sub-register.
3978     unsigned XReg = TRI->getSubReg(Reg, X86::sub_xmm);
3979     BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), get(X86::VXORPSrr), XReg)
3980       .addReg(XReg, RegState::Undef).addReg(XReg, RegState::Undef)
3981       .addReg(Reg, RegState::ImplicitDefine);
3982   } else
3983     return;
3984   MI->addRegisterKilled(Reg, TRI, true);
3985 }
3986
3987 MachineInstr* X86InstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
3988                                                   MachineInstr *MI,
3989                                            const SmallVectorImpl<unsigned> &Ops,
3990                                                   int FrameIndex) const {
3991   // Check switch flag
3992   if (NoFusing) return NULL;
3993
3994   // Unless optimizing for size, don't fold to avoid partial
3995   // register update stalls
3996   if (!MF.getFunction()->getAttributes().
3997         hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize) &&
3998       hasPartialRegUpdate(MI->getOpcode()))
3999     return 0;
4000
4001   const MachineFrameInfo *MFI = MF.getFrameInfo();
4002   unsigned Size = MFI->getObjectSize(FrameIndex);
4003   unsigned Alignment = MFI->getObjectAlignment(FrameIndex);
4004   if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
4005     unsigned NewOpc = 0;
4006     unsigned RCSize = 0;
4007     switch (MI->getOpcode()) {
4008     default: return NULL;
4009     case X86::TEST8rr:  NewOpc = X86::CMP8ri; RCSize = 1; break;
4010     case X86::TEST16rr: NewOpc = X86::CMP16ri8; RCSize = 2; break;
4011     case X86::TEST32rr: NewOpc = X86::CMP32ri8; RCSize = 4; break;
4012     case X86::TEST64rr: NewOpc = X86::CMP64ri8; RCSize = 8; break;
4013     }
4014     // Check if it's safe to fold the load. If the size of the object is
4015     // narrower than the load width, then it's not.
4016     if (Size < RCSize)
4017       return NULL;
4018     // Change to CMPXXri r, 0 first.
4019     MI->setDesc(get(NewOpc));
4020     MI->getOperand(1).ChangeToImmediate(0);
4021   } else if (Ops.size() != 1)
4022     return NULL;
4023
4024   SmallVector<MachineOperand,4> MOs;
4025   MOs.push_back(MachineOperand::CreateFI(FrameIndex));
4026   return foldMemoryOperandImpl(MF, MI, Ops[0], MOs, Size, Alignment);
4027 }
4028
4029 MachineInstr* X86InstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
4030                                                   MachineInstr *MI,
4031                                            const SmallVectorImpl<unsigned> &Ops,
4032                                                   MachineInstr *LoadMI) const {
4033   // Check switch flag
4034   if (NoFusing) return NULL;
4035
4036   // Unless optimizing for size, don't fold to avoid partial
4037   // register update stalls
4038   if (!MF.getFunction()->getAttributes().
4039         hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize) &&
4040       hasPartialRegUpdate(MI->getOpcode()))
4041     return 0;
4042
4043   // Determine the alignment of the load.
4044   unsigned Alignment = 0;
4045   if (LoadMI->hasOneMemOperand())
4046     Alignment = (*LoadMI->memoperands_begin())->getAlignment();
4047   else
4048     switch (LoadMI->getOpcode()) {
4049     case X86::AVX2_SETALLONES:
4050     case X86::AVX_SET0:
4051       Alignment = 32;
4052       break;
4053     case X86::V_SET0:
4054     case X86::V_SETALLONES:
4055       Alignment = 16;
4056       break;
4057     case X86::FsFLD0SD:
4058       Alignment = 8;
4059       break;
4060     case X86::FsFLD0SS:
4061       Alignment = 4;
4062       break;
4063     default:
4064       return 0;
4065     }
4066   if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
4067     unsigned NewOpc = 0;
4068     switch (MI->getOpcode()) {
4069     default: return NULL;
4070     case X86::TEST8rr:  NewOpc = X86::CMP8ri; break;
4071     case X86::TEST16rr: NewOpc = X86::CMP16ri8; break;
4072     case X86::TEST32rr: NewOpc = X86::CMP32ri8; break;
4073     case X86::TEST64rr: NewOpc = X86::CMP64ri8; break;
4074     }
4075     // Change to CMPXXri r, 0 first.
4076     MI->setDesc(get(NewOpc));
4077     MI->getOperand(1).ChangeToImmediate(0);
4078   } else if (Ops.size() != 1)
4079     return NULL;
4080
4081   // Make sure the subregisters match.
4082   // Otherwise we risk changing the size of the load.
4083   if (LoadMI->getOperand(0).getSubReg() != MI->getOperand(Ops[0]).getSubReg())
4084     return NULL;
4085
4086   SmallVector<MachineOperand,X86::AddrNumOperands> MOs;
4087   switch (LoadMI->getOpcode()) {
4088   case X86::V_SET0:
4089   case X86::V_SETALLONES:
4090   case X86::AVX2_SETALLONES:
4091   case X86::AVX_SET0:
4092   case X86::FsFLD0SD:
4093   case X86::FsFLD0SS: {
4094     // Folding a V_SET0 or V_SETALLONES as a load, to ease register pressure.
4095     // Create a constant-pool entry and operands to load from it.
4096
4097     // Medium and large mode can't fold loads this way.
4098     if (TM.getCodeModel() != CodeModel::Small &&
4099         TM.getCodeModel() != CodeModel::Kernel)
4100       return NULL;
4101
4102     // x86-32 PIC requires a PIC base register for constant pools.
4103     unsigned PICBase = 0;
4104     if (TM.getRelocationModel() == Reloc::PIC_) {
4105       if (TM.getSubtarget<X86Subtarget>().is64Bit())
4106         PICBase = X86::RIP;
4107       else
4108         // FIXME: PICBase = getGlobalBaseReg(&MF);
4109         // This doesn't work for several reasons.
4110         // 1. GlobalBaseReg may have been spilled.
4111         // 2. It may not be live at MI.
4112         return NULL;
4113     }
4114
4115     // Create a constant-pool entry.
4116     MachineConstantPool &MCP = *MF.getConstantPool();
4117     Type *Ty;
4118     unsigned Opc = LoadMI->getOpcode();
4119     if (Opc == X86::FsFLD0SS)
4120       Ty = Type::getFloatTy(MF.getFunction()->getContext());
4121     else if (Opc == X86::FsFLD0SD)
4122       Ty = Type::getDoubleTy(MF.getFunction()->getContext());
4123     else if (Opc == X86::AVX2_SETALLONES || Opc == X86::AVX_SET0)
4124       Ty = VectorType::get(Type::getInt32Ty(MF.getFunction()->getContext()), 8);
4125     else
4126       Ty = VectorType::get(Type::getInt32Ty(MF.getFunction()->getContext()), 4);
4127
4128     bool IsAllOnes = (Opc == X86::V_SETALLONES || Opc == X86::AVX2_SETALLONES);
4129     const Constant *C = IsAllOnes ? Constant::getAllOnesValue(Ty) :
4130                                     Constant::getNullValue(Ty);
4131     unsigned CPI = MCP.getConstantPoolIndex(C, Alignment);
4132
4133     // Create operands to load from the constant pool entry.
4134     MOs.push_back(MachineOperand::CreateReg(PICBase, false));
4135     MOs.push_back(MachineOperand::CreateImm(1));
4136     MOs.push_back(MachineOperand::CreateReg(0, false));
4137     MOs.push_back(MachineOperand::CreateCPI(CPI, 0));
4138     MOs.push_back(MachineOperand::CreateReg(0, false));
4139     break;
4140   }
4141   default: {
4142     if ((LoadMI->getOpcode() == X86::MOVSSrm ||
4143          LoadMI->getOpcode() == X86::VMOVSSrm) &&
4144         MF.getRegInfo().getRegClass(LoadMI->getOperand(0).getReg())->getSize()
4145           > 4)
4146       // These instructions only load 32 bits, we can't fold them if the
4147       // destination register is wider than 32 bits (4 bytes).
4148       return NULL;
4149     if ((LoadMI->getOpcode() == X86::MOVSDrm ||
4150          LoadMI->getOpcode() == X86::VMOVSDrm) &&
4151         MF.getRegInfo().getRegClass(LoadMI->getOperand(0).getReg())->getSize()
4152           > 8)
4153       // These instructions only load 64 bits, we can't fold them if the
4154       // destination register is wider than 64 bits (8 bytes).
4155       return NULL;
4156
4157     // Folding a normal load. Just copy the load's address operands.
4158     unsigned NumOps = LoadMI->getDesc().getNumOperands();
4159     for (unsigned i = NumOps - X86::AddrNumOperands; i != NumOps; ++i)
4160       MOs.push_back(LoadMI->getOperand(i));
4161     break;
4162   }
4163   }
4164   return foldMemoryOperandImpl(MF, MI, Ops[0], MOs, 0, Alignment);
4165 }
4166
4167
4168 bool X86InstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
4169                                   const SmallVectorImpl<unsigned> &Ops) const {
4170   // Check switch flag
4171   if (NoFusing) return 0;
4172
4173   if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
4174     switch (MI->getOpcode()) {
4175     default: return false;
4176     case X86::TEST8rr:
4177     case X86::TEST16rr:
4178     case X86::TEST32rr:
4179     case X86::TEST64rr:
4180       return true;
4181     case X86::ADD32ri:
4182       // FIXME: AsmPrinter doesn't know how to handle
4183       // X86II::MO_GOT_ABSOLUTE_ADDRESS after folding.
4184       if (MI->getOperand(2).getTargetFlags() == X86II::MO_GOT_ABSOLUTE_ADDRESS)
4185         return false;
4186       break;
4187     }
4188   }
4189
4190   if (Ops.size() != 1)
4191     return false;
4192
4193   unsigned OpNum = Ops[0];
4194   unsigned Opc = MI->getOpcode();
4195   unsigned NumOps = MI->getDesc().getNumOperands();
4196   bool isTwoAddr = NumOps > 1 &&
4197     MI->getDesc().getOperandConstraint(1, MCOI::TIED_TO) != -1;
4198
4199   // Folding a memory location into the two-address part of a two-address
4200   // instruction is different than folding it other places.  It requires
4201   // replacing the *two* registers with the memory location.
4202   const DenseMap<unsigned, std::pair<unsigned,unsigned> > *OpcodeTablePtr = 0;
4203   if (isTwoAddr && NumOps >= 2 && OpNum < 2) {
4204     OpcodeTablePtr = &RegOp2MemOpTable2Addr;
4205   } else if (OpNum == 0) { // If operand 0
4206     if (Opc == X86::MOV32r0)
4207       return true;
4208
4209     OpcodeTablePtr = &RegOp2MemOpTable0;
4210   } else if (OpNum == 1) {
4211     OpcodeTablePtr = &RegOp2MemOpTable1;
4212   } else if (OpNum == 2) {
4213     OpcodeTablePtr = &RegOp2MemOpTable2;
4214   } else if (OpNum == 3) {
4215     OpcodeTablePtr = &RegOp2MemOpTable3;
4216   }
4217
4218   if (OpcodeTablePtr && OpcodeTablePtr->count(Opc))
4219     return true;
4220   return TargetInstrInfo::canFoldMemoryOperand(MI, Ops);
4221 }
4222
4223 bool X86InstrInfo::unfoldMemoryOperand(MachineFunction &MF, MachineInstr *MI,
4224                                 unsigned Reg, bool UnfoldLoad, bool UnfoldStore,
4225                                 SmallVectorImpl<MachineInstr*> &NewMIs) const {
4226   DenseMap<unsigned, std::pair<unsigned,unsigned> >::const_iterator I =
4227     MemOp2RegOpTable.find(MI->getOpcode());
4228   if (I == MemOp2RegOpTable.end())
4229     return false;
4230   unsigned Opc = I->second.first;
4231   unsigned Index = I->second.second & TB_INDEX_MASK;
4232   bool FoldedLoad = I->second.second & TB_FOLDED_LOAD;
4233   bool FoldedStore = I->second.second & TB_FOLDED_STORE;
4234   if (UnfoldLoad && !FoldedLoad)
4235     return false;
4236   UnfoldLoad &= FoldedLoad;
4237   if (UnfoldStore && !FoldedStore)
4238     return false;
4239   UnfoldStore &= FoldedStore;
4240
4241   const MCInstrDesc &MCID = get(Opc);
4242   const TargetRegisterClass *RC = getRegClass(MCID, Index, &RI, MF);
4243   if (!MI->hasOneMemOperand() &&
4244       RC == &X86::VR128RegClass &&
4245       !TM.getSubtarget<X86Subtarget>().isUnalignedMemAccessFast())
4246     // Without memoperands, loadRegFromAddr and storeRegToStackSlot will
4247     // conservatively assume the address is unaligned. That's bad for
4248     // performance.
4249     return false;
4250   SmallVector<MachineOperand, X86::AddrNumOperands> AddrOps;
4251   SmallVector<MachineOperand,2> BeforeOps;
4252   SmallVector<MachineOperand,2> AfterOps;
4253   SmallVector<MachineOperand,4> ImpOps;
4254   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
4255     MachineOperand &Op = MI->getOperand(i);
4256     if (i >= Index && i < Index + X86::AddrNumOperands)
4257       AddrOps.push_back(Op);
4258     else if (Op.isReg() && Op.isImplicit())
4259       ImpOps.push_back(Op);
4260     else if (i < Index)
4261       BeforeOps.push_back(Op);
4262     else if (i > Index)
4263       AfterOps.push_back(Op);
4264   }
4265
4266   // Emit the load instruction.
4267   if (UnfoldLoad) {
4268     std::pair<MachineInstr::mmo_iterator,
4269               MachineInstr::mmo_iterator> MMOs =
4270       MF.extractLoadMemRefs(MI->memoperands_begin(),
4271                             MI->memoperands_end());
4272     loadRegFromAddr(MF, Reg, AddrOps, RC, MMOs.first, MMOs.second, NewMIs);
4273     if (UnfoldStore) {
4274       // Address operands cannot be marked isKill.
4275       for (unsigned i = 1; i != 1 + X86::AddrNumOperands; ++i) {
4276         MachineOperand &MO = NewMIs[0]->getOperand(i);
4277         if (MO.isReg())
4278           MO.setIsKill(false);
4279       }
4280     }
4281   }
4282
4283   // Emit the data processing instruction.
4284   MachineInstr *DataMI = MF.CreateMachineInstr(MCID, MI->getDebugLoc(), true);
4285   MachineInstrBuilder MIB(MF, DataMI);
4286
4287   if (FoldedStore)
4288     MIB.addReg(Reg, RegState::Define);
4289   for (unsigned i = 0, e = BeforeOps.size(); i != e; ++i)
4290     MIB.addOperand(BeforeOps[i]);
4291   if (FoldedLoad)
4292     MIB.addReg(Reg);
4293   for (unsigned i = 0, e = AfterOps.size(); i != e; ++i)
4294     MIB.addOperand(AfterOps[i]);
4295   for (unsigned i = 0, e = ImpOps.size(); i != e; ++i) {
4296     MachineOperand &MO = ImpOps[i];
4297     MIB.addReg(MO.getReg(),
4298                getDefRegState(MO.isDef()) |
4299                RegState::Implicit |
4300                getKillRegState(MO.isKill()) |
4301                getDeadRegState(MO.isDead()) |
4302                getUndefRegState(MO.isUndef()));
4303   }
4304   // Change CMP32ri r, 0 back to TEST32rr r, r, etc.
4305   switch (DataMI->getOpcode()) {
4306   default: break;
4307   case X86::CMP64ri32:
4308   case X86::CMP64ri8:
4309   case X86::CMP32ri:
4310   case X86::CMP32ri8:
4311   case X86::CMP16ri:
4312   case X86::CMP16ri8:
4313   case X86::CMP8ri: {
4314     MachineOperand &MO0 = DataMI->getOperand(0);
4315     MachineOperand &MO1 = DataMI->getOperand(1);
4316     if (MO1.getImm() == 0) {
4317       unsigned NewOpc;
4318       switch (DataMI->getOpcode()) {
4319       default: llvm_unreachable("Unreachable!");
4320       case X86::CMP64ri8:
4321       case X86::CMP64ri32: NewOpc = X86::TEST64rr; break;
4322       case X86::CMP32ri8:
4323       case X86::CMP32ri:   NewOpc = X86::TEST32rr; break;
4324       case X86::CMP16ri8:
4325       case X86::CMP16ri:   NewOpc = X86::TEST16rr; break;
4326       case X86::CMP8ri:    NewOpc = X86::TEST8rr; break;
4327       }
4328       DataMI->setDesc(get(NewOpc));
4329       MO1.ChangeToRegister(MO0.getReg(), false);
4330     }
4331   }
4332   }
4333   NewMIs.push_back(DataMI);
4334
4335   // Emit the store instruction.
4336   if (UnfoldStore) {
4337     const TargetRegisterClass *DstRC = getRegClass(MCID, 0, &RI, MF);
4338     std::pair<MachineInstr::mmo_iterator,
4339               MachineInstr::mmo_iterator> MMOs =
4340       MF.extractStoreMemRefs(MI->memoperands_begin(),
4341                              MI->memoperands_end());
4342     storeRegToAddr(MF, Reg, true, AddrOps, DstRC, MMOs.first, MMOs.second, NewMIs);
4343   }
4344
4345   return true;
4346 }
4347
4348 bool
4349 X86InstrInfo::unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
4350                                   SmallVectorImpl<SDNode*> &NewNodes) const {
4351   if (!N->isMachineOpcode())
4352     return false;
4353
4354   DenseMap<unsigned, std::pair<unsigned,unsigned> >::const_iterator I =
4355     MemOp2RegOpTable.find(N->getMachineOpcode());
4356   if (I == MemOp2RegOpTable.end())
4357     return false;
4358   unsigned Opc = I->second.first;
4359   unsigned Index = I->second.second & TB_INDEX_MASK;
4360   bool FoldedLoad = I->second.second & TB_FOLDED_LOAD;
4361   bool FoldedStore = I->second.second & TB_FOLDED_STORE;
4362   const MCInstrDesc &MCID = get(Opc);
4363   MachineFunction &MF = DAG.getMachineFunction();
4364   const TargetRegisterClass *RC = getRegClass(MCID, Index, &RI, MF);
4365   unsigned NumDefs = MCID.NumDefs;
4366   std::vector<SDValue> AddrOps;
4367   std::vector<SDValue> BeforeOps;
4368   std::vector<SDValue> AfterOps;
4369   SDLoc dl(N);
4370   unsigned NumOps = N->getNumOperands();
4371   for (unsigned i = 0; i != NumOps-1; ++i) {
4372     SDValue Op = N->getOperand(i);
4373     if (i >= Index-NumDefs && i < Index-NumDefs + X86::AddrNumOperands)
4374       AddrOps.push_back(Op);
4375     else if (i < Index-NumDefs)
4376       BeforeOps.push_back(Op);
4377     else if (i > Index-NumDefs)
4378       AfterOps.push_back(Op);
4379   }
4380   SDValue Chain = N->getOperand(NumOps-1);
4381   AddrOps.push_back(Chain);
4382
4383   // Emit the load instruction.
4384   SDNode *Load = 0;
4385   if (FoldedLoad) {
4386     EVT VT = *RC->vt_begin();
4387     std::pair<MachineInstr::mmo_iterator,
4388               MachineInstr::mmo_iterator> MMOs =
4389       MF.extractLoadMemRefs(cast<MachineSDNode>(N)->memoperands_begin(),
4390                             cast<MachineSDNode>(N)->memoperands_end());
4391     if (!(*MMOs.first) &&
4392         RC == &X86::VR128RegClass &&
4393         !TM.getSubtarget<X86Subtarget>().isUnalignedMemAccessFast())
4394       // Do not introduce a slow unaligned load.
4395       return false;
4396     unsigned Alignment = RC->getSize() == 32 ? 32 : 16;
4397     bool isAligned = (*MMOs.first) &&
4398                      (*MMOs.first)->getAlignment() >= Alignment;
4399     Load = DAG.getMachineNode(getLoadRegOpcode(0, RC, isAligned, TM), dl,
4400                               VT, MVT::Other, AddrOps);
4401     NewNodes.push_back(Load);
4402
4403     // Preserve memory reference information.
4404     cast<MachineSDNode>(Load)->setMemRefs(MMOs.first, MMOs.second);
4405   }
4406
4407   // Emit the data processing instruction.
4408   std::vector<EVT> VTs;
4409   const TargetRegisterClass *DstRC = 0;
4410   if (MCID.getNumDefs() > 0) {
4411     DstRC = getRegClass(MCID, 0, &RI, MF);
4412     VTs.push_back(*DstRC->vt_begin());
4413   }
4414   for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
4415     EVT VT = N->getValueType(i);
4416     if (VT != MVT::Other && i >= (unsigned)MCID.getNumDefs())
4417       VTs.push_back(VT);
4418   }
4419   if (Load)
4420     BeforeOps.push_back(SDValue(Load, 0));
4421   std::copy(AfterOps.begin(), AfterOps.end(), std::back_inserter(BeforeOps));
4422   SDNode *NewNode= DAG.getMachineNode(Opc, dl, VTs, BeforeOps);
4423   NewNodes.push_back(NewNode);
4424
4425   // Emit the store instruction.
4426   if (FoldedStore) {
4427     AddrOps.pop_back();
4428     AddrOps.push_back(SDValue(NewNode, 0));
4429     AddrOps.push_back(Chain);
4430     std::pair<MachineInstr::mmo_iterator,
4431               MachineInstr::mmo_iterator> MMOs =
4432       MF.extractStoreMemRefs(cast<MachineSDNode>(N)->memoperands_begin(),
4433                              cast<MachineSDNode>(N)->memoperands_end());
4434     if (!(*MMOs.first) &&
4435         RC == &X86::VR128RegClass &&
4436         !TM.getSubtarget<X86Subtarget>().isUnalignedMemAccessFast())
4437       // Do not introduce a slow unaligned store.
4438       return false;
4439     unsigned Alignment = RC->getSize() == 32 ? 32 : 16;
4440     bool isAligned = (*MMOs.first) &&
4441                      (*MMOs.first)->getAlignment() >= Alignment;
4442     SDNode *Store = DAG.getMachineNode(getStoreRegOpcode(0, DstRC,
4443                                                          isAligned, TM),
4444                                        dl, MVT::Other, AddrOps);
4445     NewNodes.push_back(Store);
4446
4447     // Preserve memory reference information.
4448     cast<MachineSDNode>(Load)->setMemRefs(MMOs.first, MMOs.second);
4449   }
4450
4451   return true;
4452 }
4453
4454 unsigned X86InstrInfo::getOpcodeAfterMemoryUnfold(unsigned Opc,
4455                                       bool UnfoldLoad, bool UnfoldStore,
4456                                       unsigned *LoadRegIndex) const {
4457   DenseMap<unsigned, std::pair<unsigned,unsigned> >::const_iterator I =
4458     MemOp2RegOpTable.find(Opc);
4459   if (I == MemOp2RegOpTable.end())
4460     return 0;
4461   bool FoldedLoad = I->second.second & TB_FOLDED_LOAD;
4462   bool FoldedStore = I->second.second & TB_FOLDED_STORE;
4463   if (UnfoldLoad && !FoldedLoad)
4464     return 0;
4465   if (UnfoldStore && !FoldedStore)
4466     return 0;
4467   if (LoadRegIndex)
4468     *LoadRegIndex = I->second.second & TB_INDEX_MASK;
4469   return I->second.first;
4470 }
4471
4472 bool
4473 X86InstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
4474                                      int64_t &Offset1, int64_t &Offset2) const {
4475   if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
4476     return false;
4477   unsigned Opc1 = Load1->getMachineOpcode();
4478   unsigned Opc2 = Load2->getMachineOpcode();
4479   switch (Opc1) {
4480   default: return false;
4481   case X86::MOV8rm:
4482   case X86::MOV16rm:
4483   case X86::MOV32rm:
4484   case X86::MOV64rm:
4485   case X86::LD_Fp32m:
4486   case X86::LD_Fp64m:
4487   case X86::LD_Fp80m:
4488   case X86::MOVSSrm:
4489   case X86::MOVSDrm:
4490   case X86::MMX_MOVD64rm:
4491   case X86::MMX_MOVQ64rm:
4492   case X86::FsMOVAPSrm:
4493   case X86::FsMOVAPDrm:
4494   case X86::MOVAPSrm:
4495   case X86::MOVUPSrm:
4496   case X86::MOVAPDrm:
4497   case X86::MOVDQArm:
4498   case X86::MOVDQUrm:
4499   // AVX load instructions
4500   case X86::VMOVSSrm:
4501   case X86::VMOVSDrm:
4502   case X86::FsVMOVAPSrm:
4503   case X86::FsVMOVAPDrm:
4504   case X86::VMOVAPSrm:
4505   case X86::VMOVUPSrm:
4506   case X86::VMOVAPDrm:
4507   case X86::VMOVDQArm:
4508   case X86::VMOVDQUrm:
4509   case X86::VMOVAPSYrm:
4510   case X86::VMOVUPSYrm:
4511   case X86::VMOVAPDYrm:
4512   case X86::VMOVDQAYrm:
4513   case X86::VMOVDQUYrm:
4514     break;
4515   }
4516   switch (Opc2) {
4517   default: return false;
4518   case X86::MOV8rm:
4519   case X86::MOV16rm:
4520   case X86::MOV32rm:
4521   case X86::MOV64rm:
4522   case X86::LD_Fp32m:
4523   case X86::LD_Fp64m:
4524   case X86::LD_Fp80m:
4525   case X86::MOVSSrm:
4526   case X86::MOVSDrm:
4527   case X86::MMX_MOVD64rm:
4528   case X86::MMX_MOVQ64rm:
4529   case X86::FsMOVAPSrm:
4530   case X86::FsMOVAPDrm:
4531   case X86::MOVAPSrm:
4532   case X86::MOVUPSrm:
4533   case X86::MOVAPDrm:
4534   case X86::MOVDQArm:
4535   case X86::MOVDQUrm:
4536   // AVX load instructions
4537   case X86::VMOVSSrm:
4538   case X86::VMOVSDrm:
4539   case X86::FsVMOVAPSrm:
4540   case X86::FsVMOVAPDrm:
4541   case X86::VMOVAPSrm:
4542   case X86::VMOVUPSrm:
4543   case X86::VMOVAPDrm:
4544   case X86::VMOVDQArm:
4545   case X86::VMOVDQUrm:
4546   case X86::VMOVAPSYrm:
4547   case X86::VMOVUPSYrm:
4548   case X86::VMOVAPDYrm:
4549   case X86::VMOVDQAYrm:
4550   case X86::VMOVDQUYrm:
4551     break;
4552   }
4553
4554   // Check if chain operands and base addresses match.
4555   if (Load1->getOperand(0) != Load2->getOperand(0) ||
4556       Load1->getOperand(5) != Load2->getOperand(5))
4557     return false;
4558   // Segment operands should match as well.
4559   if (Load1->getOperand(4) != Load2->getOperand(4))
4560     return false;
4561   // Scale should be 1, Index should be Reg0.
4562   if (Load1->getOperand(1) == Load2->getOperand(1) &&
4563       Load1->getOperand(2) == Load2->getOperand(2)) {
4564     if (cast<ConstantSDNode>(Load1->getOperand(1))->getZExtValue() != 1)
4565       return false;
4566
4567     // Now let's examine the displacements.
4568     if (isa<ConstantSDNode>(Load1->getOperand(3)) &&
4569         isa<ConstantSDNode>(Load2->getOperand(3))) {
4570       Offset1 = cast<ConstantSDNode>(Load1->getOperand(3))->getSExtValue();
4571       Offset2 = cast<ConstantSDNode>(Load2->getOperand(3))->getSExtValue();
4572       return true;
4573     }
4574   }
4575   return false;
4576 }
4577
4578 bool X86InstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
4579                                            int64_t Offset1, int64_t Offset2,
4580                                            unsigned NumLoads) const {
4581   assert(Offset2 > Offset1);
4582   if ((Offset2 - Offset1) / 8 > 64)
4583     return false;
4584
4585   unsigned Opc1 = Load1->getMachineOpcode();
4586   unsigned Opc2 = Load2->getMachineOpcode();
4587   if (Opc1 != Opc2)
4588     return false;  // FIXME: overly conservative?
4589
4590   switch (Opc1) {
4591   default: break;
4592   case X86::LD_Fp32m:
4593   case X86::LD_Fp64m:
4594   case X86::LD_Fp80m:
4595   case X86::MMX_MOVD64rm:
4596   case X86::MMX_MOVQ64rm:
4597     return false;
4598   }
4599
4600   EVT VT = Load1->getValueType(0);
4601   switch (VT.getSimpleVT().SimpleTy) {
4602   default:
4603     // XMM registers. In 64-bit mode we can be a bit more aggressive since we
4604     // have 16 of them to play with.
4605     if (TM.getSubtargetImpl()->is64Bit()) {
4606       if (NumLoads >= 3)
4607         return false;
4608     } else if (NumLoads) {
4609       return false;
4610     }
4611     break;
4612   case MVT::i8:
4613   case MVT::i16:
4614   case MVT::i32:
4615   case MVT::i64:
4616   case MVT::f32:
4617   case MVT::f64:
4618     if (NumLoads)
4619       return false;
4620     break;
4621   }
4622
4623   return true;
4624 }
4625
4626
4627 bool X86InstrInfo::
4628 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
4629   assert(Cond.size() == 1 && "Invalid X86 branch condition!");
4630   X86::CondCode CC = static_cast<X86::CondCode>(Cond[0].getImm());
4631   if (CC == X86::COND_NE_OR_P || CC == X86::COND_NP_OR_E)
4632     return true;
4633   Cond[0].setImm(GetOppositeBranchCondition(CC));
4634   return false;
4635 }
4636
4637 bool X86InstrInfo::
4638 isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const {
4639   // FIXME: Return false for x87 stack register classes for now. We can't
4640   // allow any loads of these registers before FpGet_ST0_80.
4641   return !(RC == &X86::CCRRegClass || RC == &X86::RFP32RegClass ||
4642            RC == &X86::RFP64RegClass || RC == &X86::RFP80RegClass);
4643 }
4644
4645 /// getGlobalBaseReg - Return a virtual register initialized with the
4646 /// the global base register value. Output instructions required to
4647 /// initialize the register in the function entry block, if necessary.
4648 ///
4649 /// TODO: Eliminate this and move the code to X86MachineFunctionInfo.
4650 ///
4651 unsigned X86InstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
4652   assert(!TM.getSubtarget<X86Subtarget>().is64Bit() &&
4653          "X86-64 PIC uses RIP relative addressing");
4654
4655   X86MachineFunctionInfo *X86FI = MF->getInfo<X86MachineFunctionInfo>();
4656   unsigned GlobalBaseReg = X86FI->getGlobalBaseReg();
4657   if (GlobalBaseReg != 0)
4658     return GlobalBaseReg;
4659
4660   // Create the register. The code to initialize it is inserted
4661   // later, by the CGBR pass (below).
4662   MachineRegisterInfo &RegInfo = MF->getRegInfo();
4663   GlobalBaseReg = RegInfo.createVirtualRegister(&X86::GR32_NOSPRegClass);
4664   X86FI->setGlobalBaseReg(GlobalBaseReg);
4665   return GlobalBaseReg;
4666 }
4667
4668 // These are the replaceable SSE instructions. Some of these have Int variants
4669 // that we don't include here. We don't want to replace instructions selected
4670 // by intrinsics.
4671 static const uint16_t ReplaceableInstrs[][3] = {
4672   //PackedSingle     PackedDouble    PackedInt
4673   { X86::MOVAPSmr,   X86::MOVAPDmr,  X86::MOVDQAmr  },
4674   { X86::MOVAPSrm,   X86::MOVAPDrm,  X86::MOVDQArm  },
4675   { X86::MOVAPSrr,   X86::MOVAPDrr,  X86::MOVDQArr  },
4676   { X86::MOVUPSmr,   X86::MOVUPDmr,  X86::MOVDQUmr  },
4677   { X86::MOVUPSrm,   X86::MOVUPDrm,  X86::MOVDQUrm  },
4678   { X86::MOVNTPSmr,  X86::MOVNTPDmr, X86::MOVNTDQmr },
4679   { X86::ANDNPSrm,   X86::ANDNPDrm,  X86::PANDNrm   },
4680   { X86::ANDNPSrr,   X86::ANDNPDrr,  X86::PANDNrr   },
4681   { X86::ANDPSrm,    X86::ANDPDrm,   X86::PANDrm    },
4682   { X86::ANDPSrr,    X86::ANDPDrr,   X86::PANDrr    },
4683   { X86::ORPSrm,     X86::ORPDrm,    X86::PORrm     },
4684   { X86::ORPSrr,     X86::ORPDrr,    X86::PORrr     },
4685   { X86::XORPSrm,    X86::XORPDrm,   X86::PXORrm    },
4686   { X86::XORPSrr,    X86::XORPDrr,   X86::PXORrr    },
4687   // AVX 128-bit support
4688   { X86::VMOVAPSmr,  X86::VMOVAPDmr,  X86::VMOVDQAmr  },
4689   { X86::VMOVAPSrm,  X86::VMOVAPDrm,  X86::VMOVDQArm  },
4690   { X86::VMOVAPSrr,  X86::VMOVAPDrr,  X86::VMOVDQArr  },
4691   { X86::VMOVUPSmr,  X86::VMOVUPDmr,  X86::VMOVDQUmr  },
4692   { X86::VMOVUPSrm,  X86::VMOVUPDrm,  X86::VMOVDQUrm  },
4693   { X86::VMOVNTPSmr, X86::VMOVNTPDmr, X86::VMOVNTDQmr },
4694   { X86::VANDNPSrm,  X86::VANDNPDrm,  X86::VPANDNrm   },
4695   { X86::VANDNPSrr,  X86::VANDNPDrr,  X86::VPANDNrr   },
4696   { X86::VANDPSrm,   X86::VANDPDrm,   X86::VPANDrm    },
4697   { X86::VANDPSrr,   X86::VANDPDrr,   X86::VPANDrr    },
4698   { X86::VORPSrm,    X86::VORPDrm,    X86::VPORrm     },
4699   { X86::VORPSrr,    X86::VORPDrr,    X86::VPORrr     },
4700   { X86::VXORPSrm,   X86::VXORPDrm,   X86::VPXORrm    },
4701   { X86::VXORPSrr,   X86::VXORPDrr,   X86::VPXORrr    },
4702   // AVX 256-bit support
4703   { X86::VMOVAPSYmr,   X86::VMOVAPDYmr,   X86::VMOVDQAYmr  },
4704   { X86::VMOVAPSYrm,   X86::VMOVAPDYrm,   X86::VMOVDQAYrm  },
4705   { X86::VMOVAPSYrr,   X86::VMOVAPDYrr,   X86::VMOVDQAYrr  },
4706   { X86::VMOVUPSYmr,   X86::VMOVUPDYmr,   X86::VMOVDQUYmr  },
4707   { X86::VMOVUPSYrm,   X86::VMOVUPDYrm,   X86::VMOVDQUYrm  },
4708   { X86::VMOVNTPSYmr,  X86::VMOVNTPDYmr,  X86::VMOVNTDQYmr }
4709 };
4710
4711 static const uint16_t ReplaceableInstrsAVX2[][3] = {
4712   //PackedSingle       PackedDouble       PackedInt
4713   { X86::VANDNPSYrm,   X86::VANDNPDYrm,   X86::VPANDNYrm   },
4714   { X86::VANDNPSYrr,   X86::VANDNPDYrr,   X86::VPANDNYrr   },
4715   { X86::VANDPSYrm,    X86::VANDPDYrm,    X86::VPANDYrm    },
4716   { X86::VANDPSYrr,    X86::VANDPDYrr,    X86::VPANDYrr    },
4717   { X86::VORPSYrm,     X86::VORPDYrm,     X86::VPORYrm     },
4718   { X86::VORPSYrr,     X86::VORPDYrr,     X86::VPORYrr     },
4719   { X86::VXORPSYrm,    X86::VXORPDYrm,    X86::VPXORYrm    },
4720   { X86::VXORPSYrr,    X86::VXORPDYrr,    X86::VPXORYrr    },
4721   { X86::VEXTRACTF128mr, X86::VEXTRACTF128mr, X86::VEXTRACTI128mr },
4722   { X86::VEXTRACTF128rr, X86::VEXTRACTF128rr, X86::VEXTRACTI128rr },
4723   { X86::VINSERTF128rm,  X86::VINSERTF128rm,  X86::VINSERTI128rm },
4724   { X86::VINSERTF128rr,  X86::VINSERTF128rr,  X86::VINSERTI128rr },
4725   { X86::VPERM2F128rm,   X86::VPERM2F128rm,   X86::VPERM2I128rm },
4726   { X86::VPERM2F128rr,   X86::VPERM2F128rr,   X86::VPERM2I128rr }
4727 };
4728
4729 // FIXME: Some shuffle and unpack instructions have equivalents in different
4730 // domains, but they require a bit more work than just switching opcodes.
4731
4732 static const uint16_t *lookup(unsigned opcode, unsigned domain) {
4733   for (unsigned i = 0, e = array_lengthof(ReplaceableInstrs); i != e; ++i)
4734     if (ReplaceableInstrs[i][domain-1] == opcode)
4735       return ReplaceableInstrs[i];
4736   return 0;
4737 }
4738
4739 static const uint16_t *lookupAVX2(unsigned opcode, unsigned domain) {
4740   for (unsigned i = 0, e = array_lengthof(ReplaceableInstrsAVX2); i != e; ++i)
4741     if (ReplaceableInstrsAVX2[i][domain-1] == opcode)
4742       return ReplaceableInstrsAVX2[i];
4743   return 0;
4744 }
4745
4746 std::pair<uint16_t, uint16_t>
4747 X86InstrInfo::getExecutionDomain(const MachineInstr *MI) const {
4748   uint16_t domain = (MI->getDesc().TSFlags >> X86II::SSEDomainShift) & 3;
4749   bool hasAVX2 = TM.getSubtarget<X86Subtarget>().hasAVX2();
4750   uint16_t validDomains = 0;
4751   if (domain && lookup(MI->getOpcode(), domain))
4752     validDomains = 0xe;
4753   else if (domain && lookupAVX2(MI->getOpcode(), domain))
4754     validDomains = hasAVX2 ? 0xe : 0x6;
4755   return std::make_pair(domain, validDomains);
4756 }
4757
4758 void X86InstrInfo::setExecutionDomain(MachineInstr *MI, unsigned Domain) const {
4759   assert(Domain>0 && Domain<4 && "Invalid execution domain");
4760   uint16_t dom = (MI->getDesc().TSFlags >> X86II::SSEDomainShift) & 3;
4761   assert(dom && "Not an SSE instruction");
4762   const uint16_t *table = lookup(MI->getOpcode(), dom);
4763   if (!table) { // try the other table
4764     assert((TM.getSubtarget<X86Subtarget>().hasAVX2() || Domain < 3) &&
4765            "256-bit vector operations only available in AVX2");
4766     table = lookupAVX2(MI->getOpcode(), dom);
4767   }
4768   assert(table && "Cannot change domain");
4769   MI->setDesc(get(table[Domain-1]));
4770 }
4771
4772 /// getNoopForMachoTarget - Return the noop instruction to use for a noop.
4773 void X86InstrInfo::getNoopForMachoTarget(MCInst &NopInst) const {
4774   NopInst.setOpcode(X86::NOOP);
4775 }
4776
4777 bool X86InstrInfo::isHighLatencyDef(int opc) const {
4778   switch (opc) {
4779   default: return false;
4780   case X86::DIVSDrm:
4781   case X86::DIVSDrm_Int:
4782   case X86::DIVSDrr:
4783   case X86::DIVSDrr_Int:
4784   case X86::DIVSSrm:
4785   case X86::DIVSSrm_Int:
4786   case X86::DIVSSrr:
4787   case X86::DIVSSrr_Int:
4788   case X86::SQRTPDm:
4789   case X86::SQRTPDr:
4790   case X86::SQRTPSm:
4791   case X86::SQRTPSr:
4792   case X86::SQRTSDm:
4793   case X86::SQRTSDm_Int:
4794   case X86::SQRTSDr:
4795   case X86::SQRTSDr_Int:
4796   case X86::SQRTSSm:
4797   case X86::SQRTSSm_Int:
4798   case X86::SQRTSSr:
4799   case X86::SQRTSSr_Int:
4800   // AVX instructions with high latency
4801   case X86::VDIVSDrm:
4802   case X86::VDIVSDrm_Int:
4803   case X86::VDIVSDrr:
4804   case X86::VDIVSDrr_Int:
4805   case X86::VDIVSSrm:
4806   case X86::VDIVSSrm_Int:
4807   case X86::VDIVSSrr:
4808   case X86::VDIVSSrr_Int:
4809   case X86::VSQRTPDm:
4810   case X86::VSQRTPDr:
4811   case X86::VSQRTPSm:
4812   case X86::VSQRTPSr:
4813   case X86::VSQRTSDm:
4814   case X86::VSQRTSDm_Int:
4815   case X86::VSQRTSDr:
4816   case X86::VSQRTSSm:
4817   case X86::VSQRTSSm_Int:
4818   case X86::VSQRTSSr:
4819     return true;
4820   }
4821 }
4822
4823 bool X86InstrInfo::
4824 hasHighOperandLatency(const InstrItineraryData *ItinData,
4825                       const MachineRegisterInfo *MRI,
4826                       const MachineInstr *DefMI, unsigned DefIdx,
4827                       const MachineInstr *UseMI, unsigned UseIdx) const {
4828   return isHighLatencyDef(DefMI->getOpcode());
4829 }
4830
4831 namespace {
4832   /// CGBR - Create Global Base Reg pass. This initializes the PIC
4833   /// global base register for x86-32.
4834   struct CGBR : public MachineFunctionPass {
4835     static char ID;
4836     CGBR() : MachineFunctionPass(ID) {}
4837
4838     virtual bool runOnMachineFunction(MachineFunction &MF) {
4839       const X86TargetMachine *TM =
4840         static_cast<const X86TargetMachine *>(&MF.getTarget());
4841
4842       assert(!TM->getSubtarget<X86Subtarget>().is64Bit() &&
4843              "X86-64 PIC uses RIP relative addressing");
4844
4845       // Only emit a global base reg in PIC mode.
4846       if (TM->getRelocationModel() != Reloc::PIC_)
4847         return false;
4848
4849       X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
4850       unsigned GlobalBaseReg = X86FI->getGlobalBaseReg();
4851
4852       // If we didn't need a GlobalBaseReg, don't insert code.
4853       if (GlobalBaseReg == 0)
4854         return false;
4855
4856       // Insert the set of GlobalBaseReg into the first MBB of the function
4857       MachineBasicBlock &FirstMBB = MF.front();
4858       MachineBasicBlock::iterator MBBI = FirstMBB.begin();
4859       DebugLoc DL = FirstMBB.findDebugLoc(MBBI);
4860       MachineRegisterInfo &RegInfo = MF.getRegInfo();
4861       const X86InstrInfo *TII = TM->getInstrInfo();
4862
4863       unsigned PC;
4864       if (TM->getSubtarget<X86Subtarget>().isPICStyleGOT())
4865         PC = RegInfo.createVirtualRegister(&X86::GR32RegClass);
4866       else
4867         PC = GlobalBaseReg;
4868
4869       // Operand of MovePCtoStack is completely ignored by asm printer. It's
4870       // only used in JIT code emission as displacement to pc.
4871       BuildMI(FirstMBB, MBBI, DL, TII->get(X86::MOVPC32r), PC).addImm(0);
4872
4873       // If we're using vanilla 'GOT' PIC style, we should use relative addressing
4874       // not to pc, but to _GLOBAL_OFFSET_TABLE_ external.
4875       if (TM->getSubtarget<X86Subtarget>().isPICStyleGOT()) {
4876         // Generate addl $__GLOBAL_OFFSET_TABLE_ + [.-piclabel], %some_register
4877         BuildMI(FirstMBB, MBBI, DL, TII->get(X86::ADD32ri), GlobalBaseReg)
4878           .addReg(PC).addExternalSymbol("_GLOBAL_OFFSET_TABLE_",
4879                                         X86II::MO_GOT_ABSOLUTE_ADDRESS);
4880       }
4881
4882       return true;
4883     }
4884
4885     virtual const char *getPassName() const {
4886       return "X86 PIC Global Base Reg Initialization";
4887     }
4888
4889     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
4890       AU.setPreservesCFG();
4891       MachineFunctionPass::getAnalysisUsage(AU);
4892     }
4893   };
4894 }
4895
4896 char CGBR::ID = 0;
4897 FunctionPass*
4898 llvm::createGlobalBaseRegPass() { return new CGBR(); }
4899
4900 namespace {
4901   struct LDTLSCleanup : public MachineFunctionPass {
4902     static char ID;
4903     LDTLSCleanup() : MachineFunctionPass(ID) {}
4904
4905     virtual bool runOnMachineFunction(MachineFunction &MF) {
4906       X86MachineFunctionInfo* MFI = MF.getInfo<X86MachineFunctionInfo>();
4907       if (MFI->getNumLocalDynamicTLSAccesses() < 2) {
4908         // No point folding accesses if there isn't at least two.
4909         return false;
4910       }
4911
4912       MachineDominatorTree *DT = &getAnalysis<MachineDominatorTree>();
4913       return VisitNode(DT->getRootNode(), 0);
4914     }
4915
4916     // Visit the dominator subtree rooted at Node in pre-order.
4917     // If TLSBaseAddrReg is non-null, then use that to replace any
4918     // TLS_base_addr instructions. Otherwise, create the register
4919     // when the first such instruction is seen, and then use it
4920     // as we encounter more instructions.
4921     bool VisitNode(MachineDomTreeNode *Node, unsigned TLSBaseAddrReg) {
4922       MachineBasicBlock *BB = Node->getBlock();
4923       bool Changed = false;
4924
4925       // Traverse the current block.
4926       for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;
4927            ++I) {
4928         switch (I->getOpcode()) {
4929           case X86::TLS_base_addr32:
4930           case X86::TLS_base_addr64:
4931             if (TLSBaseAddrReg)
4932               I = ReplaceTLSBaseAddrCall(I, TLSBaseAddrReg);
4933             else
4934               I = SetRegister(I, &TLSBaseAddrReg);
4935             Changed = true;
4936             break;
4937           default:
4938             break;
4939         }
4940       }
4941
4942       // Visit the children of this block in the dominator tree.
4943       for (MachineDomTreeNode::iterator I = Node->begin(), E = Node->end();
4944            I != E; ++I) {
4945         Changed |= VisitNode(*I, TLSBaseAddrReg);
4946       }
4947
4948       return Changed;
4949     }
4950
4951     // Replace the TLS_base_addr instruction I with a copy from
4952     // TLSBaseAddrReg, returning the new instruction.
4953     MachineInstr *ReplaceTLSBaseAddrCall(MachineInstr *I,
4954                                          unsigned TLSBaseAddrReg) {
4955       MachineFunction *MF = I->getParent()->getParent();
4956       const X86TargetMachine *TM =
4957           static_cast<const X86TargetMachine *>(&MF->getTarget());
4958       const bool is64Bit = TM->getSubtarget<X86Subtarget>().is64Bit();
4959       const X86InstrInfo *TII = TM->getInstrInfo();
4960
4961       // Insert a Copy from TLSBaseAddrReg to RAX/EAX.
4962       MachineInstr *Copy = BuildMI(*I->getParent(), I, I->getDebugLoc(),
4963                                    TII->get(TargetOpcode::COPY),
4964                                    is64Bit ? X86::RAX : X86::EAX)
4965                                    .addReg(TLSBaseAddrReg);
4966
4967       // Erase the TLS_base_addr instruction.
4968       I->eraseFromParent();
4969
4970       return Copy;
4971     }
4972
4973     // Create a virtal register in *TLSBaseAddrReg, and populate it by
4974     // inserting a copy instruction after I. Returns the new instruction.
4975     MachineInstr *SetRegister(MachineInstr *I, unsigned *TLSBaseAddrReg) {
4976       MachineFunction *MF = I->getParent()->getParent();
4977       const X86TargetMachine *TM =
4978           static_cast<const X86TargetMachine *>(&MF->getTarget());
4979       const bool is64Bit = TM->getSubtarget<X86Subtarget>().is64Bit();
4980       const X86InstrInfo *TII = TM->getInstrInfo();
4981
4982       // Create a virtual register for the TLS base address.
4983       MachineRegisterInfo &RegInfo = MF->getRegInfo();
4984       *TLSBaseAddrReg = RegInfo.createVirtualRegister(is64Bit
4985                                                       ? &X86::GR64RegClass
4986                                                       : &X86::GR32RegClass);
4987
4988       // Insert a copy from RAX/EAX to TLSBaseAddrReg.
4989       MachineInstr *Next = I->getNextNode();
4990       MachineInstr *Copy = BuildMI(*I->getParent(), Next, I->getDebugLoc(),
4991                                    TII->get(TargetOpcode::COPY),
4992                                    *TLSBaseAddrReg)
4993                                    .addReg(is64Bit ? X86::RAX : X86::EAX);
4994
4995       return Copy;
4996     }
4997
4998     virtual const char *getPassName() const {
4999       return "Local Dynamic TLS Access Clean-up";
5000     }
5001
5002     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
5003       AU.setPreservesCFG();
5004       AU.addRequired<MachineDominatorTree>();
5005       MachineFunctionPass::getAnalysisUsage(AU);
5006     }
5007   };
5008 }
5009
5010 char LDTLSCleanup::ID = 0;
5011 FunctionPass*
5012 llvm::createCleanupLocalDynamicTLSPass() { return new LDTLSCleanup(); }