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