Home | History | Annotate | Download | only in X86
      1 //===- X86InstrInfo.cpp - X86 Instruction Information -----------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file contains the X86 implementation of the TargetInstrInfo class.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "X86InstrInfo.h"
     15 #include "X86.h"
     16 #include "X86InstrBuilder.h"
     17 #include "X86MachineFunctionInfo.h"
     18 #include "X86Subtarget.h"
     19 #include "X86TargetMachine.h"
     20 #include "llvm/DerivedTypes.h"
     21 #include "llvm/LLVMContext.h"
     22 #include "llvm/ADT/STLExtras.h"
     23 #include "llvm/CodeGen/MachineConstantPool.h"
     24 #include "llvm/CodeGen/MachineFrameInfo.h"
     25 #include "llvm/CodeGen/MachineInstrBuilder.h"
     26 #include "llvm/CodeGen/MachineRegisterInfo.h"
     27 #include "llvm/CodeGen/LiveVariables.h"
     28 #include "llvm/CodeGen/PseudoSourceValue.h"
     29 #include "llvm/MC/MCInst.h"
     30 #include "llvm/Support/CommandLine.h"
     31 #include "llvm/Support/Debug.h"
     32 #include "llvm/Support/ErrorHandling.h"
     33 #include "llvm/Support/raw_ostream.h"
     34 #include "llvm/Target/TargetOptions.h"
     35 #include "llvm/MC/MCAsmInfo.h"
     36 #include <limits>
     37 
     38 #define GET_INSTRINFO_CTOR
     39 #include "X86GenInstrInfo.inc"
     40 
     41 using namespace llvm;
     42 
     43 static cl::opt<bool>
     44 NoFusing("disable-spill-fusing",
     45          cl::desc("Disable fusing of spill code into instructions"));
     46 static cl::opt<bool>
     47 PrintFailedFusing("print-failed-fuse-candidates",
     48                   cl::desc("Print instructions that the allocator wants to"
     49                            " fuse, but the X86 backend currently can't"),
     50                   cl::Hidden);
     51 static cl::opt<bool>
     52 ReMatPICStubLoad("remat-pic-stub-load",
     53                  cl::desc("Re-materialize load from stub in PIC mode"),
     54                  cl::init(false), cl::Hidden);
     55 
     56 X86InstrInfo::X86InstrInfo(X86TargetMachine &tm)
     57   : X86GenInstrInfo((tm.getSubtarget<X86Subtarget>().is64Bit()
     58                      ? X86::ADJCALLSTACKDOWN64
     59                      : X86::ADJCALLSTACKDOWN32),
     60                     (tm.getSubtarget<X86Subtarget>().is64Bit()
     61                      ? X86::ADJCALLSTACKUP64
     62                      : X86::ADJCALLSTACKUP32)),
     63     TM(tm), RI(tm, *this) {
     64   enum {
     65     TB_NOT_REVERSABLE = 1U << 31,
     66     TB_FLAGS = TB_NOT_REVERSABLE
     67   };
     68 
     69   static const unsigned OpTbl2Addr[][2] = {
     70     { X86::ADC32ri,     X86::ADC32mi },
     71     { X86::ADC32ri8,    X86::ADC32mi8 },
     72     { X86::ADC32rr,     X86::ADC32mr },
     73     { X86::ADC64ri32,   X86::ADC64mi32 },
     74     { X86::ADC64ri8,    X86::ADC64mi8 },
     75     { X86::ADC64rr,     X86::ADC64mr },
     76     { X86::ADD16ri,     X86::ADD16mi },
     77     { X86::ADD16ri8,    X86::ADD16mi8 },
     78     { X86::ADD16ri_DB,  X86::ADD16mi  | TB_NOT_REVERSABLE },
     79     { X86::ADD16ri8_DB, X86::ADD16mi8 | TB_NOT_REVERSABLE },
     80     { X86::ADD16rr,     X86::ADD16mr },
     81     { X86::ADD16rr_DB,  X86::ADD16mr | TB_NOT_REVERSABLE },
     82     { X86::ADD32ri,     X86::ADD32mi },
     83     { X86::ADD32ri8,    X86::ADD32mi8 },
     84     { X86::ADD32ri_DB,  X86::ADD32mi | TB_NOT_REVERSABLE },
     85     { X86::ADD32ri8_DB, X86::ADD32mi8 | TB_NOT_REVERSABLE },
     86     { X86::ADD32rr,     X86::ADD32mr },
     87     { X86::ADD32rr_DB,  X86::ADD32mr | TB_NOT_REVERSABLE },
     88     { X86::ADD64ri32,   X86::ADD64mi32 },
     89     { X86::ADD64ri8,    X86::ADD64mi8 },
     90     { X86::ADD64ri32_DB,X86::ADD64mi32 | TB_NOT_REVERSABLE },
     91     { X86::ADD64ri8_DB, X86::ADD64mi8 | TB_NOT_REVERSABLE },
     92     { X86::ADD64rr,     X86::ADD64mr },
     93     { X86::ADD64rr_DB,  X86::ADD64mr | TB_NOT_REVERSABLE },
     94     { X86::ADD8ri,      X86::ADD8mi },
     95     { X86::ADD8rr,      X86::ADD8mr },
     96     { X86::AND16ri,     X86::AND16mi },
     97     { X86::AND16ri8,    X86::AND16mi8 },
     98     { X86::AND16rr,     X86::AND16mr },
     99     { X86::AND32ri,     X86::AND32mi },
    100     { X86::AND32ri8,    X86::AND32mi8 },
    101     { X86::AND32rr,     X86::AND32mr },
    102     { X86::AND64ri32,   X86::AND64mi32 },
    103     { X86::AND64ri8,    X86::AND64mi8 },
    104     { X86::AND64rr,     X86::AND64mr },
    105     { X86::AND8ri,      X86::AND8mi },
    106     { X86::AND8rr,      X86::AND8mr },
    107     { X86::DEC16r,      X86::DEC16m },
    108     { X86::DEC32r,      X86::DEC32m },
    109     { X86::DEC64_16r,   X86::DEC64_16m },
    110     { X86::DEC64_32r,   X86::DEC64_32m },
    111     { X86::DEC64r,      X86::DEC64m },
    112     { X86::DEC8r,       X86::DEC8m },
    113     { X86::INC16r,      X86::INC16m },
    114     { X86::INC32r,      X86::INC32m },
    115     { X86::INC64_16r,   X86::INC64_16m },
    116     { X86::INC64_32r,   X86::INC64_32m },
    117     { X86::INC64r,      X86::INC64m },
    118     { X86::INC8r,       X86::INC8m },
    119     { X86::NEG16r,      X86::NEG16m },
    120     { X86::NEG32r,      X86::NEG32m },
    121     { X86::NEG64r,      X86::NEG64m },
    122     { X86::NEG8r,       X86::NEG8m },
    123     { X86::NOT16r,      X86::NOT16m },
    124     { X86::NOT32r,      X86::NOT32m },
    125     { X86::NOT64r,      X86::NOT64m },
    126     { X86::NOT8r,       X86::NOT8m },
    127     { X86::OR16ri,      X86::OR16mi },
    128     { X86::OR16ri8,     X86::OR16mi8 },
    129     { X86::OR16rr,      X86::OR16mr },
    130     { X86::OR32ri,      X86::OR32mi },
    131     { X86::OR32ri8,     X86::OR32mi8 },
    132     { X86::OR32rr,      X86::OR32mr },
    133     { X86::OR64ri32,    X86::OR64mi32 },
    134     { X86::OR64ri8,     X86::OR64mi8 },
    135     { X86::OR64rr,      X86::OR64mr },
    136     { X86::OR8ri,       X86::OR8mi },
    137     { X86::OR8rr,       X86::OR8mr },
    138     { X86::ROL16r1,     X86::ROL16m1 },
    139     { X86::ROL16rCL,    X86::ROL16mCL },
    140     { X86::ROL16ri,     X86::ROL16mi },
    141     { X86::ROL32r1,     X86::ROL32m1 },
    142     { X86::ROL32rCL,    X86::ROL32mCL },
    143     { X86::ROL32ri,     X86::ROL32mi },
    144     { X86::ROL64r1,     X86::ROL64m1 },
    145     { X86::ROL64rCL,    X86::ROL64mCL },
    146     { X86::ROL64ri,     X86::ROL64mi },
    147     { X86::ROL8r1,      X86::ROL8m1 },
    148     { X86::ROL8rCL,     X86::ROL8mCL },
    149     { X86::ROL8ri,      X86::ROL8mi },
    150     { X86::ROR16r1,     X86::ROR16m1 },
    151     { X86::ROR16rCL,    X86::ROR16mCL },
    152     { X86::ROR16ri,     X86::ROR16mi },
    153     { X86::ROR32r1,     X86::ROR32m1 },
    154     { X86::ROR32rCL,    X86::ROR32mCL },
    155     { X86::ROR32ri,     X86::ROR32mi },
    156     { X86::ROR64r1,     X86::ROR64m1 },
    157     { X86::ROR64rCL,    X86::ROR64mCL },
    158     { X86::ROR64ri,     X86::ROR64mi },
    159     { X86::ROR8r1,      X86::ROR8m1 },
    160     { X86::ROR8rCL,     X86::ROR8mCL },
    161     { X86::ROR8ri,      X86::ROR8mi },
    162     { X86::SAR16r1,     X86::SAR16m1 },
    163     { X86::SAR16rCL,    X86::SAR16mCL },
    164     { X86::SAR16ri,     X86::SAR16mi },
    165     { X86::SAR32r1,     X86::SAR32m1 },
    166     { X86::SAR32rCL,    X86::SAR32mCL },
    167     { X86::SAR32ri,     X86::SAR32mi },
    168     { X86::SAR64r1,     X86::SAR64m1 },
    169     { X86::SAR64rCL,    X86::SAR64mCL },
    170     { X86::SAR64ri,     X86::SAR64mi },
    171     { X86::SAR8r1,      X86::SAR8m1 },
    172     { X86::SAR8rCL,     X86::SAR8mCL },
    173     { X86::SAR8ri,      X86::SAR8mi },
    174     { X86::SBB32ri,     X86::SBB32mi },
    175     { X86::SBB32ri8,    X86::SBB32mi8 },
    176     { X86::SBB32rr,     X86::SBB32mr },
    177     { X86::SBB64ri32,   X86::SBB64mi32 },
    178     { X86::SBB64ri8,    X86::SBB64mi8 },
    179     { X86::SBB64rr,     X86::SBB64mr },
    180     { X86::SHL16rCL,    X86::SHL16mCL },
    181     { X86::SHL16ri,     X86::SHL16mi },
    182     { X86::SHL32rCL,    X86::SHL32mCL },
    183     { X86::SHL32ri,     X86::SHL32mi },
    184     { X86::SHL64rCL,    X86::SHL64mCL },
    185     { X86::SHL64ri,     X86::SHL64mi },
    186     { X86::SHL8rCL,     X86::SHL8mCL },
    187     { X86::SHL8ri,      X86::SHL8mi },
    188     { X86::SHLD16rrCL,  X86::SHLD16mrCL },
    189     { X86::SHLD16rri8,  X86::SHLD16mri8 },
    190     { X86::SHLD32rrCL,  X86::SHLD32mrCL },
    191     { X86::SHLD32rri8,  X86::SHLD32mri8 },
    192     { X86::SHLD64rrCL,  X86::SHLD64mrCL },
    193     { X86::SHLD64rri8,  X86::SHLD64mri8 },
    194     { X86::SHR16r1,     X86::SHR16m1 },
    195     { X86::SHR16rCL,    X86::SHR16mCL },
    196     { X86::SHR16ri,     X86::SHR16mi },
    197     { X86::SHR32r1,     X86::SHR32m1 },
    198     { X86::SHR32rCL,    X86::SHR32mCL },
    199     { X86::SHR32ri,     X86::SHR32mi },
    200     { X86::SHR64r1,     X86::SHR64m1 },
    201     { X86::SHR64rCL,    X86::SHR64mCL },
    202     { X86::SHR64ri,     X86::SHR64mi },
    203     { X86::SHR8r1,      X86::SHR8m1 },
    204     { X86::SHR8rCL,     X86::SHR8mCL },
    205     { X86::SHR8ri,      X86::SHR8mi },
    206     { X86::SHRD16rrCL,  X86::SHRD16mrCL },
    207     { X86::SHRD16rri8,  X86::SHRD16mri8 },
    208     { X86::SHRD32rrCL,  X86::SHRD32mrCL },
    209     { X86::SHRD32rri8,  X86::SHRD32mri8 },
    210     { X86::SHRD64rrCL,  X86::SHRD64mrCL },
    211     { X86::SHRD64rri8,  X86::SHRD64mri8 },
    212     { X86::SUB16ri,     X86::SUB16mi },
    213     { X86::SUB16ri8,    X86::SUB16mi8 },
    214     { X86::SUB16rr,     X86::SUB16mr },
    215     { X86::SUB32ri,     X86::SUB32mi },
    216     { X86::SUB32ri8,    X86::SUB32mi8 },
    217     { X86::SUB32rr,     X86::SUB32mr },
    218     { X86::SUB64ri32,   X86::SUB64mi32 },
    219     { X86::SUB64ri8,    X86::SUB64mi8 },
    220     { X86::SUB64rr,     X86::SUB64mr },
    221     { X86::SUB8ri,      X86::SUB8mi },
    222     { X86::SUB8rr,      X86::SUB8mr },
    223     { X86::XOR16ri,     X86::XOR16mi },
    224     { X86::XOR16ri8,    X86::XOR16mi8 },
    225     { X86::XOR16rr,     X86::XOR16mr },
    226     { X86::XOR32ri,     X86::XOR32mi },
    227     { X86::XOR32ri8,    X86::XOR32mi8 },
    228     { X86::XOR32rr,     X86::XOR32mr },
    229     { X86::XOR64ri32,   X86::XOR64mi32 },
    230     { X86::XOR64ri8,    X86::XOR64mi8 },
    231     { X86::XOR64rr,     X86::XOR64mr },
    232     { X86::XOR8ri,      X86::XOR8mi },
    233     { X86::XOR8rr,      X86::XOR8mr }
    234   };
    235 
    236   for (unsigned i = 0, e = array_lengthof(OpTbl2Addr); i != e; ++i) {
    237     unsigned RegOp = OpTbl2Addr[i][0];
    238     unsigned MemOp = OpTbl2Addr[i][1] & ~TB_FLAGS;
    239     assert(!RegOp2MemOpTable2Addr.count(RegOp) && "Duplicated entries?");
    240     RegOp2MemOpTable2Addr[RegOp] = std::make_pair(MemOp, 0U);
    241 
    242     // If this is not a reversible operation (because there is a many->one)
    243     // mapping, don't insert the reverse of the operation into MemOp2RegOpTable.
    244     if (OpTbl2Addr[i][1] & TB_NOT_REVERSABLE)
    245       continue;
    246 
    247     // Index 0, folded load and store, no alignment requirement.
    248     unsigned AuxInfo = 0 | (1 << 4) | (1 << 5);
    249 
    250     assert(!MemOp2RegOpTable.count(MemOp) &&
    251             "Duplicated entries in unfolding maps?");
    252     MemOp2RegOpTable[MemOp] = std::make_pair(RegOp, AuxInfo);
    253   }
    254 
    255   // If the third value is 1, then it's folding either a load or a store.
    256   static const unsigned OpTbl0[][4] = {
    257     { X86::BT16ri8,     X86::BT16mi8, 1, 0 },
    258     { X86::BT32ri8,     X86::BT32mi8, 1, 0 },
    259     { X86::BT64ri8,     X86::BT64mi8, 1, 0 },
    260     { X86::CALL32r,     X86::CALL32m, 1, 0 },
    261     { X86::CALL64r,     X86::CALL64m, 1, 0 },
    262     { X86::WINCALL64r,  X86::WINCALL64m, 1, 0 },
    263     { X86::CMP16ri,     X86::CMP16mi, 1, 0 },
    264     { X86::CMP16ri8,    X86::CMP16mi8, 1, 0 },
    265     { X86::CMP16rr,     X86::CMP16mr, 1, 0 },
    266     { X86::CMP32ri,     X86::CMP32mi, 1, 0 },
    267     { X86::CMP32ri8,    X86::CMP32mi8, 1, 0 },
    268     { X86::CMP32rr,     X86::CMP32mr, 1, 0 },
    269     { X86::CMP64ri32,   X86::CMP64mi32, 1, 0 },
    270     { X86::CMP64ri8,    X86::CMP64mi8, 1, 0 },
    271     { X86::CMP64rr,     X86::CMP64mr, 1, 0 },
    272     { X86::CMP8ri,      X86::CMP8mi, 1, 0 },
    273     { X86::CMP8rr,      X86::CMP8mr, 1, 0 },
    274     { X86::DIV16r,      X86::DIV16m, 1, 0 },
    275     { X86::DIV32r,      X86::DIV32m, 1, 0 },
    276     { X86::DIV64r,      X86::DIV64m, 1, 0 },
    277     { X86::DIV8r,       X86::DIV8m, 1, 0 },
    278     { X86::EXTRACTPSrr, X86::EXTRACTPSmr, 0, 16 },
    279     { X86::FsMOVAPDrr,  X86::MOVSDmr | TB_NOT_REVERSABLE , 0, 0 },
    280     { X86::FsMOVAPSrr,  X86::MOVSSmr | TB_NOT_REVERSABLE , 0, 0 },
    281     { X86::IDIV16r,     X86::IDIV16m, 1, 0 },
    282     { X86::IDIV32r,     X86::IDIV32m, 1, 0 },
    283     { X86::IDIV64r,     X86::IDIV64m, 1, 0 },
    284     { X86::IDIV8r,      X86::IDIV8m, 1, 0 },
    285     { X86::IMUL16r,     X86::IMUL16m, 1, 0 },
    286     { X86::IMUL32r,     X86::IMUL32m, 1, 0 },
    287     { X86::IMUL64r,     X86::IMUL64m, 1, 0 },
    288     { X86::IMUL8r,      X86::IMUL8m, 1, 0 },
    289     { X86::JMP32r,      X86::JMP32m, 1, 0 },
    290     { X86::JMP64r,      X86::JMP64m, 1, 0 },
    291     { X86::MOV16ri,     X86::MOV16mi, 0, 0 },
    292     { X86::MOV16rr,     X86::MOV16mr, 0, 0 },
    293     { X86::MOV32ri,     X86::MOV32mi, 0, 0 },
    294     { X86::MOV32rr,     X86::MOV32mr, 0, 0 },
    295     { X86::MOV64ri32,   X86::MOV64mi32, 0, 0 },
    296     { X86::MOV64rr,     X86::MOV64mr, 0, 0 },
    297     { X86::MOV8ri,      X86::MOV8mi, 0, 0 },
    298     { X86::MOV8rr,      X86::MOV8mr, 0, 0 },
    299     { X86::MOV8rr_NOREX, X86::MOV8mr_NOREX, 0, 0 },
    300     { X86::MOVAPDrr,    X86::MOVAPDmr, 0, 16 },
    301     { X86::MOVAPSrr,    X86::MOVAPSmr, 0, 16 },
    302     { X86::MOVDQArr,    X86::MOVDQAmr, 0, 16 },
    303     { X86::VMOVAPDYrr,  X86::VMOVAPDYmr, 0, 32 },
    304     { X86::VMOVAPSYrr,  X86::VMOVAPSYmr, 0, 32 },
    305     { X86::VMOVDQAYrr,  X86::VMOVDQAYmr, 0, 32 },
    306     { X86::MOVPDI2DIrr, X86::MOVPDI2DImr, 0, 0 },
    307     { X86::MOVPQIto64rr,X86::MOVPQI2QImr, 0, 0 },
    308     { X86::MOVSDto64rr, X86::MOVSDto64mr, 0, 0 },
    309     { X86::MOVSS2DIrr,  X86::MOVSS2DImr, 0, 0 },
    310     { X86::MOVUPDrr,    X86::MOVUPDmr, 0, 0 },
    311     { X86::MOVUPSrr,    X86::MOVUPSmr, 0, 0 },
    312     { X86::VMOVUPDYrr,  X86::VMOVUPDYmr, 0, 0 },
    313     { X86::VMOVUPSYrr,  X86::VMOVUPSYmr, 0, 0 },
    314     { X86::MUL16r,      X86::MUL16m, 1, 0 },
    315     { X86::MUL32r,      X86::MUL32m, 1, 0 },
    316     { X86::MUL64r,      X86::MUL64m, 1, 0 },
    317     { X86::MUL8r,       X86::MUL8m, 1, 0 },
    318     { X86::SETAEr,      X86::SETAEm, 0, 0 },
    319     { X86::SETAr,       X86::SETAm, 0, 0 },
    320     { X86::SETBEr,      X86::SETBEm, 0, 0 },
    321     { X86::SETBr,       X86::SETBm, 0, 0 },
    322     { X86::SETEr,       X86::SETEm, 0, 0 },
    323     { X86::SETGEr,      X86::SETGEm, 0, 0 },
    324     { X86::SETGr,       X86::SETGm, 0, 0 },
    325     { X86::SETLEr,      X86::SETLEm, 0, 0 },
    326     { X86::SETLr,       X86::SETLm, 0, 0 },
    327     { X86::SETNEr,      X86::SETNEm, 0, 0 },
    328     { X86::SETNOr,      X86::SETNOm, 0, 0 },
    329     { X86::SETNPr,      X86::SETNPm, 0, 0 },
    330     { X86::SETNSr,      X86::SETNSm, 0, 0 },
    331     { X86::SETOr,       X86::SETOm, 0, 0 },
    332     { X86::SETPr,       X86::SETPm, 0, 0 },
    333     { X86::SETSr,       X86::SETSm, 0, 0 },
    334     { X86::TAILJMPr,    X86::TAILJMPm, 1, 0 },
    335     { X86::TAILJMPr64,  X86::TAILJMPm64, 1, 0 },
    336     { X86::TEST16ri,    X86::TEST16mi, 1, 0 },
    337     { X86::TEST32ri,    X86::TEST32mi, 1, 0 },
    338     { X86::TEST64ri32,  X86::TEST64mi32, 1, 0 },
    339     { X86::TEST8ri,     X86::TEST8mi, 1, 0 }
    340   };
    341 
    342   for (unsigned i = 0, e = array_lengthof(OpTbl0); i != e; ++i) {
    343     unsigned RegOp      = OpTbl0[i][0];
    344     unsigned MemOp      = OpTbl0[i][1] & ~TB_FLAGS;
    345     unsigned FoldedLoad = OpTbl0[i][2];
    346     unsigned Align      = OpTbl0[i][3];
    347     assert(!RegOp2MemOpTable0.count(RegOp) && "Duplicated entries?");
    348     RegOp2MemOpTable0[RegOp] = std::make_pair(MemOp, Align);
    349 
    350     // If this is not a reversible operation (because there is a many->one)
    351     // mapping, don't insert the reverse of the operation into MemOp2RegOpTable.
    352     if (OpTbl0[i][1] & TB_NOT_REVERSABLE)
    353       continue;
    354 
    355     // Index 0, folded load or store.
    356     unsigned AuxInfo = 0 | (FoldedLoad << 4) | ((FoldedLoad^1) << 5);
    357     assert(!MemOp2RegOpTable.count(MemOp) && "Duplicated entries?");
    358     MemOp2RegOpTable[MemOp] = std::make_pair(RegOp, AuxInfo);
    359   }
    360 
    361   static const unsigned OpTbl1[][3] = {
    362     { X86::CMP16rr,         X86::CMP16rm, 0 },
    363     { X86::CMP32rr,         X86::CMP32rm, 0 },
    364     { X86::CMP64rr,         X86::CMP64rm, 0 },
    365     { X86::CMP8rr,          X86::CMP8rm, 0 },
    366     { X86::CVTSD2SSrr,      X86::CVTSD2SSrm, 0 },
    367     { X86::CVTSI2SD64rr,    X86::CVTSI2SD64rm, 0 },
    368     { X86::CVTSI2SDrr,      X86::CVTSI2SDrm, 0 },
    369     { X86::CVTSI2SS64rr,    X86::CVTSI2SS64rm, 0 },
    370     { X86::CVTSI2SSrr,      X86::CVTSI2SSrm, 0 },
    371     { X86::CVTSS2SDrr,      X86::CVTSS2SDrm, 0 },
    372     { X86::CVTTSD2SI64rr,   X86::CVTTSD2SI64rm, 0 },
    373     { X86::CVTTSD2SIrr,     X86::CVTTSD2SIrm, 0 },
    374     { X86::CVTTSS2SI64rr,   X86::CVTTSS2SI64rm, 0 },
    375     { X86::CVTTSS2SIrr,     X86::CVTTSS2SIrm, 0 },
    376     { X86::FsMOVAPDrr,      X86::MOVSDrm | TB_NOT_REVERSABLE , 0 },
    377     { X86::FsMOVAPSrr,      X86::MOVSSrm | TB_NOT_REVERSABLE , 0 },
    378     { X86::IMUL16rri,       X86::IMUL16rmi, 0 },
    379     { X86::IMUL16rri8,      X86::IMUL16rmi8, 0 },
    380     { X86::IMUL32rri,       X86::IMUL32rmi, 0 },
    381     { X86::IMUL32rri8,      X86::IMUL32rmi8, 0 },
    382     { X86::IMUL64rri32,     X86::IMUL64rmi32, 0 },
    383     { X86::IMUL64rri8,      X86::IMUL64rmi8, 0 },
    384     { X86::Int_COMISDrr,    X86::Int_COMISDrm, 0 },
    385     { X86::Int_COMISSrr,    X86::Int_COMISSrm, 0 },
    386     { X86::Int_CVTDQ2PDrr,  X86::Int_CVTDQ2PDrm, 16 },
    387     { X86::Int_CVTDQ2PSrr,  X86::Int_CVTDQ2PSrm, 16 },
    388     { X86::Int_CVTPD2DQrr,  X86::Int_CVTPD2DQrm, 16 },
    389     { X86::Int_CVTPD2PSrr,  X86::Int_CVTPD2PSrm, 16 },
    390     { X86::Int_CVTPS2DQrr,  X86::Int_CVTPS2DQrm, 16 },
    391     { X86::Int_CVTPS2PDrr,  X86::Int_CVTPS2PDrm, 0 },
    392     { X86::CVTSD2SI64rr,    X86::CVTSD2SI64rm, 0 },
    393     { X86::CVTSD2SIrr,      X86::CVTSD2SIrm, 0 },
    394     { X86::Int_CVTSD2SSrr,  X86::Int_CVTSD2SSrm, 0 },
    395     { X86::Int_CVTSI2SD64rr,X86::Int_CVTSI2SD64rm, 0 },
    396     { X86::Int_CVTSI2SDrr,  X86::Int_CVTSI2SDrm, 0 },
    397     { X86::Int_CVTSI2SS64rr,X86::Int_CVTSI2SS64rm, 0 },
    398     { X86::Int_CVTSI2SSrr,  X86::Int_CVTSI2SSrm, 0 },
    399     { X86::Int_CVTSS2SDrr,  X86::Int_CVTSS2SDrm, 0 },
    400     { X86::Int_CVTSS2SI64rr,X86::Int_CVTSS2SI64rm, 0 },
    401     { X86::Int_CVTSS2SIrr,  X86::Int_CVTSS2SIrm, 0 },
    402     { X86::CVTTPD2DQrr,     X86::CVTTPD2DQrm, 16 },
    403     { X86::CVTTPS2DQrr,     X86::CVTTPS2DQrm, 16 },
    404     { X86::Int_CVTTSD2SI64rr,X86::Int_CVTTSD2SI64rm, 0 },
    405     { X86::Int_CVTTSD2SIrr, X86::Int_CVTTSD2SIrm, 0 },
    406     { X86::Int_CVTTSS2SI64rr,X86::Int_CVTTSS2SI64rm, 0 },
    407     { X86::Int_CVTTSS2SIrr, X86::Int_CVTTSS2SIrm, 0 },
    408     { X86::Int_UCOMISDrr,   X86::Int_UCOMISDrm, 0 },
    409     { X86::Int_UCOMISSrr,   X86::Int_UCOMISSrm, 0 },
    410     { X86::MOV16rr,         X86::MOV16rm, 0 },
    411     { X86::MOV32rr,         X86::MOV32rm, 0 },
    412     { X86::MOV64rr,         X86::MOV64rm, 0 },
    413     { X86::MOV64toPQIrr,    X86::MOVQI2PQIrm, 0 },
    414     { X86::MOV64toSDrr,     X86::MOV64toSDrm, 0 },
    415     { X86::MOV8rr,          X86::MOV8rm, 0 },
    416     { X86::MOVAPDrr,        X86::MOVAPDrm, 16 },
    417     { X86::MOVAPSrr,        X86::MOVAPSrm, 16 },
    418     { X86::VMOVAPDYrr,      X86::VMOVAPDYrm, 32 },
    419     { X86::VMOVAPSYrr,      X86::VMOVAPSYrm, 32 },
    420     { X86::MOVDDUPrr,       X86::MOVDDUPrm, 0 },
    421     { X86::MOVDI2PDIrr,     X86::MOVDI2PDIrm, 0 },
    422     { X86::MOVDI2SSrr,      X86::MOVDI2SSrm, 0 },
    423     { X86::MOVDQArr,        X86::MOVDQArm, 16 },
    424     { X86::VMOVDQAYrr,      X86::VMOVDQAYrm, 16 },
    425     { X86::MOVSHDUPrr,      X86::MOVSHDUPrm, 16 },
    426     { X86::MOVSLDUPrr,      X86::MOVSLDUPrm, 16 },
    427     { X86::MOVSX16rr8,      X86::MOVSX16rm8, 0 },
    428     { X86::MOVSX32rr16,     X86::MOVSX32rm16, 0 },
    429     { X86::MOVSX32rr8,      X86::MOVSX32rm8, 0 },
    430     { X86::MOVSX64rr16,     X86::MOVSX64rm16, 0 },
    431     { X86::MOVSX64rr32,     X86::MOVSX64rm32, 0 },
    432     { X86::MOVSX64rr8,      X86::MOVSX64rm8, 0 },
    433     { X86::MOVUPDrr,        X86::MOVUPDrm, 16 },
    434     { X86::MOVUPSrr,        X86::MOVUPSrm, 0 },
    435     { X86::VMOVUPDYrr,      X86::VMOVUPDYrm, 0 },
    436     { X86::VMOVUPSYrr,      X86::VMOVUPSYrm, 0 },
    437     { X86::MOVZDI2PDIrr,    X86::MOVZDI2PDIrm, 0 },
    438     { X86::MOVZQI2PQIrr,    X86::MOVZQI2PQIrm, 0 },
    439     { X86::MOVZPQILo2PQIrr, X86::MOVZPQILo2PQIrm, 16 },
    440     { X86::MOVZX16rr8,      X86::MOVZX16rm8, 0 },
    441     { X86::MOVZX32rr16,     X86::MOVZX32rm16, 0 },
    442     { X86::MOVZX32_NOREXrr8, X86::MOVZX32_NOREXrm8, 0 },
    443     { X86::MOVZX32rr8,      X86::MOVZX32rm8, 0 },
    444     { X86::MOVZX64rr16,     X86::MOVZX64rm16, 0 },
    445     { X86::MOVZX64rr32,     X86::MOVZX64rm32, 0 },
    446     { X86::MOVZX64rr8,      X86::MOVZX64rm8, 0 },
    447     { X86::PSHUFDri,        X86::PSHUFDmi, 16 },
    448     { X86::PSHUFHWri,       X86::PSHUFHWmi, 16 },
    449     { X86::PSHUFLWri,       X86::PSHUFLWmi, 16 },
    450     { X86::RCPPSr,          X86::RCPPSm, 16 },
    451     { X86::RCPPSr_Int,      X86::RCPPSm_Int, 16 },
    452     { X86::RSQRTPSr,        X86::RSQRTPSm, 16 },
    453     { X86::RSQRTPSr_Int,    X86::RSQRTPSm_Int, 16 },
    454     { X86::RSQRTSSr,        X86::RSQRTSSm, 0 },
    455     { X86::RSQRTSSr_Int,    X86::RSQRTSSm_Int, 0 },
    456     { X86::SQRTPDr,         X86::SQRTPDm, 16 },
    457     { X86::SQRTPDr_Int,     X86::SQRTPDm_Int, 16 },
    458     { X86::SQRTPSr,         X86::SQRTPSm, 16 },
    459     { X86::SQRTPSr_Int,     X86::SQRTPSm_Int, 16 },
    460     { X86::SQRTSDr,         X86::SQRTSDm, 0 },
    461     { X86::SQRTSDr_Int,     X86::SQRTSDm_Int, 0 },
    462     { X86::SQRTSSr,         X86::SQRTSSm, 0 },
    463     { X86::SQRTSSr_Int,     X86::SQRTSSm_Int, 0 },
    464     { X86::TEST16rr,        X86::TEST16rm, 0 },
    465     { X86::TEST32rr,        X86::TEST32rm, 0 },
    466     { X86::TEST64rr,        X86::TEST64rm, 0 },
    467     { X86::TEST8rr,         X86::TEST8rm, 0 },
    468     // FIXME: TEST*rr EAX,EAX ---> CMP [mem], 0
    469     { X86::UCOMISDrr,       X86::UCOMISDrm, 0 },
    470     { X86::UCOMISSrr,       X86::UCOMISSrm, 0 }
    471   };
    472 
    473   for (unsigned i = 0, e = array_lengthof(OpTbl1); i != e; ++i) {
    474     unsigned RegOp = OpTbl1[i][0];
    475     unsigned MemOp = OpTbl1[i][1] & ~TB_FLAGS;
    476     unsigned Align = OpTbl1[i][2];
    477     assert(!RegOp2MemOpTable1.count(RegOp) && "Duplicate entries");
    478     RegOp2MemOpTable1[RegOp] = std::make_pair(MemOp, Align);
    479 
    480     // If this is not a reversible operation (because there is a many->one)
    481     // mapping, don't insert the reverse of the operation into MemOp2RegOpTable.
    482     if (OpTbl1[i][1] & TB_NOT_REVERSABLE)
    483       continue;
    484 
    485     // Index 1, folded load
    486     unsigned AuxInfo = 1 | (1 << 4);
    487     assert(!MemOp2RegOpTable.count(MemOp) && "Duplicate entries");
    488     MemOp2RegOpTable[MemOp] = std::make_pair(RegOp, AuxInfo);
    489   }
    490 
    491   static const unsigned OpTbl2[][3] = {
    492     { X86::ADC32rr,         X86::ADC32rm, 0 },
    493     { X86::ADC64rr,         X86::ADC64rm, 0 },
    494     { X86::ADD16rr,         X86::ADD16rm, 0 },
    495     { X86::ADD16rr_DB,      X86::ADD16rm | TB_NOT_REVERSABLE, 0 },
    496     { X86::ADD32rr,         X86::ADD32rm, 0 },
    497     { X86::ADD32rr_DB,      X86::ADD32rm | TB_NOT_REVERSABLE, 0 },
    498     { X86::ADD64rr,         X86::ADD64rm, 0 },
    499     { X86::ADD64rr_DB,      X86::ADD64rm | TB_NOT_REVERSABLE, 0 },
    500     { X86::ADD8rr,          X86::ADD8rm, 0 },
    501     { X86::ADDPDrr,         X86::ADDPDrm, 16 },
    502     { X86::ADDPSrr,         X86::ADDPSrm, 16 },
    503     { X86::ADDSDrr,         X86::ADDSDrm, 0 },
    504     { X86::ADDSSrr,         X86::ADDSSrm, 0 },
    505     { X86::ADDSUBPDrr,      X86::ADDSUBPDrm, 16 },
    506     { X86::ADDSUBPSrr,      X86::ADDSUBPSrm, 16 },
    507     { X86::AND16rr,         X86::AND16rm, 0 },
    508     { X86::AND32rr,         X86::AND32rm, 0 },
    509     { X86::AND64rr,         X86::AND64rm, 0 },
    510     { X86::AND8rr,          X86::AND8rm, 0 },
    511     { X86::ANDNPDrr,        X86::ANDNPDrm, 16 },
    512     { X86::ANDNPSrr,        X86::ANDNPSrm, 16 },
    513     { X86::ANDPDrr,         X86::ANDPDrm, 16 },
    514     { X86::ANDPSrr,         X86::ANDPSrm, 16 },
    515     { X86::CMOVA16rr,       X86::CMOVA16rm, 0 },
    516     { X86::CMOVA32rr,       X86::CMOVA32rm, 0 },
    517     { X86::CMOVA64rr,       X86::CMOVA64rm, 0 },
    518     { X86::CMOVAE16rr,      X86::CMOVAE16rm, 0 },
    519     { X86::CMOVAE32rr,      X86::CMOVAE32rm, 0 },
    520     { X86::CMOVAE64rr,      X86::CMOVAE64rm, 0 },
    521     { X86::CMOVB16rr,       X86::CMOVB16rm, 0 },
    522     { X86::CMOVB32rr,       X86::CMOVB32rm, 0 },
    523     { X86::CMOVB64rr,       X86::CMOVB64rm, 0 },
    524     { X86::CMOVBE16rr,      X86::CMOVBE16rm, 0 },
    525     { X86::CMOVBE32rr,      X86::CMOVBE32rm, 0 },
    526     { X86::CMOVBE64rr,      X86::CMOVBE64rm, 0 },
    527     { X86::CMOVE16rr,       X86::CMOVE16rm, 0 },
    528     { X86::CMOVE32rr,       X86::CMOVE32rm, 0 },
    529     { X86::CMOVE64rr,       X86::CMOVE64rm, 0 },
    530     { X86::CMOVG16rr,       X86::CMOVG16rm, 0 },
    531     { X86::CMOVG32rr,       X86::CMOVG32rm, 0 },
    532     { X86::CMOVG64rr,       X86::CMOVG64rm, 0 },
    533     { X86::CMOVGE16rr,      X86::CMOVGE16rm, 0 },
    534     { X86::CMOVGE32rr,      X86::CMOVGE32rm, 0 },
    535     { X86::CMOVGE64rr,      X86::CMOVGE64rm, 0 },
    536     { X86::CMOVL16rr,       X86::CMOVL16rm, 0 },
    537     { X86::CMOVL32rr,       X86::CMOVL32rm, 0 },
    538     { X86::CMOVL64rr,       X86::CMOVL64rm, 0 },
    539     { X86::CMOVLE16rr,      X86::CMOVLE16rm, 0 },
    540     { X86::CMOVLE32rr,      X86::CMOVLE32rm, 0 },
    541     { X86::CMOVLE64rr,      X86::CMOVLE64rm, 0 },
    542     { X86::CMOVNE16rr,      X86::CMOVNE16rm, 0 },
    543     { X86::CMOVNE32rr,      X86::CMOVNE32rm, 0 },
    544     { X86::CMOVNE64rr,      X86::CMOVNE64rm, 0 },
    545     { X86::CMOVNO16rr,      X86::CMOVNO16rm, 0 },
    546     { X86::CMOVNO32rr,      X86::CMOVNO32rm, 0 },
    547     { X86::CMOVNO64rr,      X86::CMOVNO64rm, 0 },
    548     { X86::CMOVNP16rr,      X86::CMOVNP16rm, 0 },
    549     { X86::CMOVNP32rr,      X86::CMOVNP32rm, 0 },
    550     { X86::CMOVNP64rr,      X86::CMOVNP64rm, 0 },
    551     { X86::CMOVNS16rr,      X86::CMOVNS16rm, 0 },
    552     { X86::CMOVNS32rr,      X86::CMOVNS32rm, 0 },
    553     { X86::CMOVNS64rr,      X86::CMOVNS64rm, 0 },
    554     { X86::CMOVO16rr,       X86::CMOVO16rm, 0 },
    555     { X86::CMOVO32rr,       X86::CMOVO32rm, 0 },
    556     { X86::CMOVO64rr,       X86::CMOVO64rm, 0 },
    557     { X86::CMOVP16rr,       X86::CMOVP16rm, 0 },
    558     { X86::CMOVP32rr,       X86::CMOVP32rm, 0 },
    559     { X86::CMOVP64rr,       X86::CMOVP64rm, 0 },
    560     { X86::CMOVS16rr,       X86::CMOVS16rm, 0 },
    561     { X86::CMOVS32rr,       X86::CMOVS32rm, 0 },
    562     { X86::CMOVS64rr,       X86::CMOVS64rm, 0 },
    563     { X86::CMPPDrri,        X86::CMPPDrmi, 16 },
    564     { X86::CMPPSrri,        X86::CMPPSrmi, 16 },
    565     { X86::CMPSDrr,         X86::CMPSDrm, 0 },
    566     { X86::CMPSSrr,         X86::CMPSSrm, 0 },
    567     { X86::DIVPDrr,         X86::DIVPDrm, 16 },
    568     { X86::DIVPSrr,         X86::DIVPSrm, 16 },
    569     { X86::DIVSDrr,         X86::DIVSDrm, 0 },
    570     { X86::DIVSSrr,         X86::DIVSSrm, 0 },
    571     { X86::FsANDNPDrr,      X86::FsANDNPDrm, 16 },
    572     { X86::FsANDNPSrr,      X86::FsANDNPSrm, 16 },
    573     { X86::FsANDPDrr,       X86::FsANDPDrm, 16 },
    574     { X86::FsANDPSrr,       X86::FsANDPSrm, 16 },
    575     { X86::FsORPDrr,        X86::FsORPDrm, 16 },
    576     { X86::FsORPSrr,        X86::FsORPSrm, 16 },
    577     { X86::FsXORPDrr,       X86::FsXORPDrm, 16 },
    578     { X86::FsXORPSrr,       X86::FsXORPSrm, 16 },
    579     { X86::HADDPDrr,        X86::HADDPDrm, 16 },
    580     { X86::HADDPSrr,        X86::HADDPSrm, 16 },
    581     { X86::HSUBPDrr,        X86::HSUBPDrm, 16 },
    582     { X86::HSUBPSrr,        X86::HSUBPSrm, 16 },
    583     { X86::IMUL16rr,        X86::IMUL16rm, 0 },
    584     { X86::IMUL32rr,        X86::IMUL32rm, 0 },
    585     { X86::IMUL64rr,        X86::IMUL64rm, 0 },
    586     { X86::Int_CMPSDrr,     X86::Int_CMPSDrm, 0 },
    587     { X86::Int_CMPSSrr,     X86::Int_CMPSSrm, 0 },
    588     { X86::MAXPDrr,         X86::MAXPDrm, 16 },
    589     { X86::MAXPDrr_Int,     X86::MAXPDrm_Int, 16 },
    590     { X86::MAXPSrr,         X86::MAXPSrm, 16 },
    591     { X86::MAXPSrr_Int,     X86::MAXPSrm_Int, 16 },
    592     { X86::MAXSDrr,         X86::MAXSDrm, 0 },
    593     { X86::MAXSDrr_Int,     X86::MAXSDrm_Int, 0 },
    594     { X86::MAXSSrr,         X86::MAXSSrm, 0 },
    595     { X86::MAXSSrr_Int,     X86::MAXSSrm_Int, 0 },
    596     { X86::MINPDrr,         X86::MINPDrm, 16 },
    597     { X86::MINPDrr_Int,     X86::MINPDrm_Int, 16 },
    598     { X86::MINPSrr,         X86::MINPSrm, 16 },
    599     { X86::MINPSrr_Int,     X86::MINPSrm_Int, 16 },
    600     { X86::MINSDrr,         X86::MINSDrm, 0 },
    601     { X86::MINSDrr_Int,     X86::MINSDrm_Int, 0 },
    602     { X86::MINSSrr,         X86::MINSSrm, 0 },
    603     { X86::MINSSrr_Int,     X86::MINSSrm_Int, 0 },
    604     { X86::MULPDrr,         X86::MULPDrm, 16 },
    605     { X86::MULPSrr,         X86::MULPSrm, 16 },
    606     { X86::MULSDrr,         X86::MULSDrm, 0 },
    607     { X86::MULSSrr,         X86::MULSSrm, 0 },
    608     { X86::OR16rr,          X86::OR16rm, 0 },
    609     { X86::OR32rr,          X86::OR32rm, 0 },
    610     { X86::OR64rr,          X86::OR64rm, 0 },
    611     { X86::OR8rr,           X86::OR8rm, 0 },
    612     { X86::ORPDrr,          X86::ORPDrm, 16 },
    613     { X86::ORPSrr,          X86::ORPSrm, 16 },
    614     { X86::PACKSSDWrr,      X86::PACKSSDWrm, 16 },
    615     { X86::PACKSSWBrr,      X86::PACKSSWBrm, 16 },
    616     { X86::PACKUSWBrr,      X86::PACKUSWBrm, 16 },
    617     { X86::PADDBrr,         X86::PADDBrm, 16 },
    618     { X86::PADDDrr,         X86::PADDDrm, 16 },
    619     { X86::PADDQrr,         X86::PADDQrm, 16 },
    620     { X86::PADDSBrr,        X86::PADDSBrm, 16 },
    621     { X86::PADDSWrr,        X86::PADDSWrm, 16 },
    622     { X86::PADDWrr,         X86::PADDWrm, 16 },
    623     { X86::PANDNrr,         X86::PANDNrm, 16 },
    624     { X86::PANDrr,          X86::PANDrm, 16 },
    625     { X86::PAVGBrr,         X86::PAVGBrm, 16 },
    626     { X86::PAVGWrr,         X86::PAVGWrm, 16 },
    627     { X86::PCMPEQBrr,       X86::PCMPEQBrm, 16 },
    628     { X86::PCMPEQDrr,       X86::PCMPEQDrm, 16 },
    629     { X86::PCMPEQWrr,       X86::PCMPEQWrm, 16 },
    630     { X86::PCMPGTBrr,       X86::PCMPGTBrm, 16 },
    631     { X86::PCMPGTDrr,       X86::PCMPGTDrm, 16 },
    632     { X86::PCMPGTWrr,       X86::PCMPGTWrm, 16 },
    633     { X86::PINSRWrri,       X86::PINSRWrmi, 16 },
    634     { X86::PMADDWDrr,       X86::PMADDWDrm, 16 },
    635     { X86::PMAXSWrr,        X86::PMAXSWrm, 16 },
    636     { X86::PMAXUBrr,        X86::PMAXUBrm, 16 },
    637     { X86::PMINSWrr,        X86::PMINSWrm, 16 },
    638     { X86::PMINUBrr,        X86::PMINUBrm, 16 },
    639     { X86::PMULDQrr,        X86::PMULDQrm, 16 },
    640     { X86::PMULHUWrr,       X86::PMULHUWrm, 16 },
    641     { X86::PMULHWrr,        X86::PMULHWrm, 16 },
    642     { X86::PMULLDrr,        X86::PMULLDrm, 16 },
    643     { X86::PMULLWrr,        X86::PMULLWrm, 16 },
    644     { X86::PMULUDQrr,       X86::PMULUDQrm, 16 },
    645     { X86::PORrr,           X86::PORrm, 16 },
    646     { X86::PSADBWrr,        X86::PSADBWrm, 16 },
    647     { X86::PSLLDrr,         X86::PSLLDrm, 16 },
    648     { X86::PSLLQrr,         X86::PSLLQrm, 16 },
    649     { X86::PSLLWrr,         X86::PSLLWrm, 16 },
    650     { X86::PSRADrr,         X86::PSRADrm, 16 },
    651     { X86::PSRAWrr,         X86::PSRAWrm, 16 },
    652     { X86::PSRLDrr,         X86::PSRLDrm, 16 },
    653     { X86::PSRLQrr,         X86::PSRLQrm, 16 },
    654     { X86::PSRLWrr,         X86::PSRLWrm, 16 },
    655     { X86::PSUBBrr,         X86::PSUBBrm, 16 },
    656     { X86::PSUBDrr,         X86::PSUBDrm, 16 },
    657     { X86::PSUBSBrr,        X86::PSUBSBrm, 16 },
    658     { X86::PSUBSWrr,        X86::PSUBSWrm, 16 },
    659     { X86::PSUBWrr,         X86::PSUBWrm, 16 },
    660     { X86::PUNPCKHBWrr,     X86::PUNPCKHBWrm, 16 },
    661     { X86::PUNPCKHDQrr,     X86::PUNPCKHDQrm, 16 },
    662     { X86::PUNPCKHQDQrr,    X86::PUNPCKHQDQrm, 16 },
    663     { X86::PUNPCKHWDrr,     X86::PUNPCKHWDrm, 16 },
    664     { X86::PUNPCKLBWrr,     X86::PUNPCKLBWrm, 16 },
    665     { X86::PUNPCKLDQrr,     X86::PUNPCKLDQrm, 16 },
    666     { X86::PUNPCKLQDQrr,    X86::PUNPCKLQDQrm, 16 },
    667     { X86::PUNPCKLWDrr,     X86::PUNPCKLWDrm, 16 },
    668     { X86::PXORrr,          X86::PXORrm, 16 },
    669     { X86::SBB32rr,         X86::SBB32rm, 0 },
    670     { X86::SBB64rr,         X86::SBB64rm, 0 },
    671     { X86::SHUFPDrri,       X86::SHUFPDrmi, 16 },
    672     { X86::SHUFPSrri,       X86::SHUFPSrmi, 16 },
    673     { X86::SUB16rr,         X86::SUB16rm, 0 },
    674     { X86::SUB32rr,         X86::SUB32rm, 0 },
    675     { X86::SUB64rr,         X86::SUB64rm, 0 },
    676     { X86::SUB8rr,          X86::SUB8rm, 0 },
    677     { X86::SUBPDrr,         X86::SUBPDrm, 16 },
    678     { X86::SUBPSrr,         X86::SUBPSrm, 16 },
    679     { X86::SUBSDrr,         X86::SUBSDrm, 0 },
    680     { X86::SUBSSrr,         X86::SUBSSrm, 0 },
    681     // FIXME: TEST*rr -> swapped operand of TEST*mr.
    682     { X86::UNPCKHPDrr,      X86::UNPCKHPDrm, 16 },
    683     { X86::UNPCKHPSrr,      X86::UNPCKHPSrm, 16 },
    684     { X86::UNPCKLPDrr,      X86::UNPCKLPDrm, 16 },
    685     { X86::UNPCKLPSrr,      X86::UNPCKLPSrm, 16 },
    686     { X86::XOR16rr,         X86::XOR16rm, 0 },
    687     { X86::XOR32rr,         X86::XOR32rm, 0 },
    688     { X86::XOR64rr,         X86::XOR64rm, 0 },
    689     { X86::XOR8rr,          X86::XOR8rm, 0 },
    690     { X86::XORPDrr,         X86::XORPDrm, 16 },
    691     { X86::XORPSrr,         X86::XORPSrm, 16 }
    692   };
    693 
    694   for (unsigned i = 0, e = array_lengthof(OpTbl2); i != e; ++i) {
    695     unsigned RegOp = OpTbl2[i][0];
    696     unsigned MemOp = OpTbl2[i][1] & ~TB_FLAGS;
    697     unsigned Align = OpTbl2[i][2];
    698 
    699     assert(!RegOp2MemOpTable2.count(RegOp) && "Duplicate entry!");
    700     RegOp2MemOpTable2[RegOp] = std::make_pair(MemOp, Align);
    701 
    702     // If this is not a reversible operation (because there is a many->one)
    703     // mapping, don't insert the reverse of the operation into MemOp2RegOpTable.
    704     if (OpTbl2[i][1] & TB_NOT_REVERSABLE)
    705       continue;
    706 
    707     // Index 2, folded load
    708     unsigned AuxInfo = 2 | (1 << 4);
    709     assert(!MemOp2RegOpTable.count(MemOp) &&
    710            "Duplicated entries in unfolding maps?");
    711     MemOp2RegOpTable[MemOp] = std::make_pair(RegOp, AuxInfo);
    712   }
    713 }
    714 
    715 bool
    716 X86InstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
    717                                     unsigned &SrcReg, unsigned &DstReg,
    718                                     unsigned &SubIdx) const {
    719   switch (MI.getOpcode()) {
    720   default: break;
    721   case X86::MOVSX16rr8:
    722   case X86::MOVZX16rr8:
    723   case X86::MOVSX32rr8:
    724   case X86::MOVZX32rr8:
    725   case X86::MOVSX64rr8:
    726   case X86::MOVZX64rr8:
    727     if (!TM.getSubtarget<X86Subtarget>().is64Bit())
    728       // It's not always legal to reference the low 8-bit of the larger
    729       // register in 32-bit mode.
    730       return false;
    731   case X86::MOVSX32rr16:
    732   case X86::MOVZX32rr16:
    733   case X86::MOVSX64rr16:
    734   case X86::MOVZX64rr16:
    735   case X86::MOVSX64rr32:
    736   case X86::MOVZX64rr32: {
    737     if (MI.getOperand(0).getSubReg() || MI.getOperand(1).getSubReg())
    738       // Be conservative.
    739       return false;
    740     SrcReg = MI.getOperand(1).getReg();
    741     DstReg = MI.getOperand(0).getReg();
    742     switch (MI.getOpcode()) {
    743     default:
    744       llvm_unreachable(0);
    745       break;
    746     case X86::MOVSX16rr8:
    747     case X86::MOVZX16rr8:
    748     case X86::MOVSX32rr8:
    749     case X86::MOVZX32rr8:
    750     case X86::MOVSX64rr8:
    751     case X86::MOVZX64rr8:
    752       SubIdx = X86::sub_8bit;
    753       break;
    754     case X86::MOVSX32rr16:
    755     case X86::MOVZX32rr16:
    756     case X86::MOVSX64rr16:
    757     case X86::MOVZX64rr16:
    758       SubIdx = X86::sub_16bit;
    759       break;
    760     case X86::MOVSX64rr32:
    761     case X86::MOVZX64rr32:
    762       SubIdx = X86::sub_32bit;
    763       break;
    764     }
    765     return true;
    766   }
    767   }
    768   return false;
    769 }
    770 
    771 /// isFrameOperand - Return true and the FrameIndex if the specified
    772 /// operand and follow operands form a reference to the stack frame.
    773 bool X86InstrInfo::isFrameOperand(const MachineInstr *MI, unsigned int Op,
    774                                   int &FrameIndex) const {
    775   if (MI->getOperand(Op).isFI() && MI->getOperand(Op+1).isImm() &&
    776       MI->getOperand(Op+2).isReg() && MI->getOperand(Op+3).isImm() &&
    777       MI->getOperand(Op+1).getImm() == 1 &&
    778       MI->getOperand(Op+2).getReg() == 0 &&
    779       MI->getOperand(Op+3).getImm() == 0) {
    780     FrameIndex = MI->getOperand(Op).getIndex();
    781     return true;
    782   }
    783   return false;
    784 }
    785 
    786 static bool isFrameLoadOpcode(int Opcode) {
    787   switch (Opcode) {
    788   default: break;
    789   case X86::MOV8rm:
    790   case X86::MOV16rm:
    791   case X86::MOV32rm:
    792   case X86::MOV64rm:
    793   case X86::LD_Fp64m:
    794   case X86::MOVSSrm:
    795   case X86::MOVSDrm:
    796   case X86::MOVAPSrm:
    797   case X86::MOVAPDrm:
    798   case X86::MOVDQArm:
    799   case X86::VMOVAPSYrm:
    800   case X86::VMOVAPDYrm:
    801   case X86::VMOVDQAYrm:
    802   case X86::MMX_MOVD64rm:
    803   case X86::MMX_MOVQ64rm:
    804     return true;
    805     break;
    806   }
    807   return false;
    808 }
    809 
    810 static bool isFrameStoreOpcode(int Opcode) {
    811   switch (Opcode) {
    812   default: break;
    813   case X86::MOV8mr:
    814   case X86::MOV16mr:
    815   case X86::MOV32mr:
    816   case X86::MOV64mr:
    817   case X86::ST_FpP64m:
    818   case X86::MOVSSmr:
    819   case X86::MOVSDmr:
    820   case X86::MOVAPSmr:
    821   case X86::MOVAPDmr:
    822   case X86::MOVDQAmr:
    823   case X86::VMOVAPSYmr:
    824   case X86::VMOVAPDYmr:
    825   case X86::VMOVDQAYmr:
    826   case X86::MMX_MOVD64mr:
    827   case X86::MMX_MOVQ64mr:
    828   case X86::MMX_MOVNTQmr:
    829     return true;
    830   }
    831   return false;
    832 }
    833 
    834 unsigned X86InstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
    835                                            int &FrameIndex) const {
    836   if (isFrameLoadOpcode(MI->getOpcode()))
    837     if (MI->getOperand(0).getSubReg() == 0 && isFrameOperand(MI, 1, FrameIndex))
    838       return MI->getOperand(0).getReg();
    839   return 0;
    840 }
    841 
    842 unsigned X86InstrInfo::isLoadFromStackSlotPostFE(const MachineInstr *MI,
    843                                                  int &FrameIndex) const {
    844   if (isFrameLoadOpcode(MI->getOpcode())) {
    845     unsigned Reg;
    846     if ((Reg = isLoadFromStackSlot(MI, FrameIndex)))
    847       return Reg;
    848     // Check for post-frame index elimination operations
    849     const MachineMemOperand *Dummy;
    850     return hasLoadFromStackSlot(MI, Dummy, FrameIndex);
    851   }
    852   return 0;
    853 }
    854 
    855 bool X86InstrInfo::hasLoadFromStackSlot(const MachineInstr *MI,
    856                                         const MachineMemOperand *&MMO,
    857                                         int &FrameIndex) const {
    858   for (MachineInstr::mmo_iterator o = MI->memoperands_begin(),
    859          oe = MI->memoperands_end();
    860        o != oe;
    861        ++o) {
    862     if ((*o)->isLoad() && (*o)->getValue())
    863       if (const FixedStackPseudoSourceValue *Value =
    864           dyn_cast<const FixedStackPseudoSourceValue>((*o)->getValue())) {
    865         FrameIndex = Value->getFrameIndex();
    866         MMO = *o;
    867         return true;
    868       }
    869   }
    870   return false;
    871 }
    872 
    873 unsigned X86InstrInfo::isStoreToStackSlot(const MachineInstr *MI,
    874                                           int &FrameIndex) const {
    875   if (isFrameStoreOpcode(MI->getOpcode()))
    876     if (MI->getOperand(X86::AddrNumOperands).getSubReg() == 0 &&
    877         isFrameOperand(MI, 0, FrameIndex))
    878       return MI->getOperand(X86::AddrNumOperands).getReg();
    879   return 0;
    880 }
    881 
    882 unsigned X86InstrInfo::isStoreToStackSlotPostFE(const MachineInstr *MI,
    883                                                 int &FrameIndex) const {
    884   if (isFrameStoreOpcode(MI->getOpcode())) {
    885     unsigned Reg;
    886     if ((Reg = isStoreToStackSlot(MI, FrameIndex)))
    887       return Reg;
    888     // Check for post-frame index elimination operations
    889     const MachineMemOperand *Dummy;
    890     return hasStoreToStackSlot(MI, Dummy, FrameIndex);
    891   }
    892   return 0;
    893 }
    894 
    895 bool X86InstrInfo::hasStoreToStackSlot(const MachineInstr *MI,
    896                                        const MachineMemOperand *&MMO,
    897                                        int &FrameIndex) const {
    898   for (MachineInstr::mmo_iterator o = MI->memoperands_begin(),
    899          oe = MI->memoperands_end();
    900        o != oe;
    901        ++o) {
    902     if ((*o)->isStore() && (*o)->getValue())
    903       if (const FixedStackPseudoSourceValue *Value =
    904           dyn_cast<const FixedStackPseudoSourceValue>((*o)->getValue())) {
    905         FrameIndex = Value->getFrameIndex();
    906         MMO = *o;
    907         return true;
    908       }
    909   }
    910   return false;
    911 }
    912 
    913 /// regIsPICBase - Return true if register is PIC base (i.e.g defined by
    914 /// X86::MOVPC32r.
    915 static bool regIsPICBase(unsigned BaseReg, const MachineRegisterInfo &MRI) {
    916   bool isPICBase = false;
    917   for (MachineRegisterInfo::def_iterator I = MRI.def_begin(BaseReg),
    918          E = MRI.def_end(); I != E; ++I) {
    919     MachineInstr *DefMI = I.getOperand().getParent();
    920     if (DefMI->getOpcode() != X86::MOVPC32r)
    921       return false;
    922     assert(!isPICBase && "More than one PIC base?");
    923     isPICBase = true;
    924   }
    925   return isPICBase;
    926 }
    927 
    928 bool
    929 X86InstrInfo::isReallyTriviallyReMaterializable(const MachineInstr *MI,
    930                                                 AliasAnalysis *AA) const {
    931   switch (MI->getOpcode()) {
    932   default: break;
    933     case X86::MOV8rm:
    934     case X86::MOV16rm:
    935     case X86::MOV32rm:
    936     case X86::MOV64rm:
    937     case X86::LD_Fp64m:
    938     case X86::MOVSSrm:
    939     case X86::MOVSDrm:
    940     case X86::MOVAPSrm:
    941     case X86::MOVUPSrm:
    942     case X86::MOVAPDrm:
    943     case X86::MOVDQArm:
    944     case X86::VMOVAPSYrm:
    945     case X86::VMOVUPSYrm:
    946     case X86::VMOVAPDYrm:
    947     case X86::VMOVDQAYrm:
    948     case X86::MMX_MOVD64rm:
    949     case X86::MMX_MOVQ64rm:
    950     case X86::FsMOVAPSrm:
    951     case X86::FsMOVAPDrm: {
    952       // Loads from constant pools are trivially rematerializable.
    953       if (MI->getOperand(1).isReg() &&
    954           MI->getOperand(2).isImm() &&
    955           MI->getOperand(3).isReg() && MI->getOperand(3).getReg() == 0 &&
    956           MI->isInvariantLoad(AA)) {
    957         unsigned BaseReg = MI->getOperand(1).getReg();
    958         if (BaseReg == 0 || BaseReg == X86::RIP)
    959           return true;
    960         // Allow re-materialization of PIC load.
    961         if (!ReMatPICStubLoad && MI->getOperand(4).isGlobal())
    962           return false;
    963         const MachineFunction &MF = *MI->getParent()->getParent();
    964         const MachineRegisterInfo &MRI = MF.getRegInfo();
    965         bool isPICBase = false;
    966         for (MachineRegisterInfo::def_iterator I = MRI.def_begin(BaseReg),
    967                E = MRI.def_end(); I != E; ++I) {
    968           MachineInstr *DefMI = I.getOperand().getParent();
    969           if (DefMI->getOpcode() != X86::MOVPC32r)
    970             return false;
    971           assert(!isPICBase && "More than one PIC base?");
    972           isPICBase = true;
    973         }
    974         return isPICBase;
    975       }
    976       return false;
    977     }
    978 
    979      case X86::LEA32r:
    980      case X86::LEA64r: {
    981        if (MI->getOperand(2).isImm() &&
    982            MI->getOperand(3).isReg() && MI->getOperand(3).getReg() == 0 &&
    983            !MI->getOperand(4).isReg()) {
    984          // lea fi#, lea GV, etc. are all rematerializable.
    985          if (!MI->getOperand(1).isReg())
    986            return true;
    987          unsigned BaseReg = MI->getOperand(1).getReg();
    988          if (BaseReg == 0)
    989            return true;
    990          // Allow re-materialization of lea PICBase + x.
    991          const MachineFunction &MF = *MI->getParent()->getParent();
    992          const MachineRegisterInfo &MRI = MF.getRegInfo();
    993          return regIsPICBase(BaseReg, MRI);
    994        }
    995        return false;
    996      }
    997   }
    998 
    999   // All other instructions marked M_REMATERIALIZABLE are always trivially
   1000   // rematerializable.
   1001   return true;
   1002 }
   1003 
   1004 /// isSafeToClobberEFLAGS - Return true if it's safe insert an instruction that
   1005 /// would clobber the EFLAGS condition register. Note the result may be
   1006 /// conservative. If it cannot definitely determine the safety after visiting
   1007 /// a few instructions in each direction it assumes it's not safe.
   1008 static bool isSafeToClobberEFLAGS(MachineBasicBlock &MBB,
   1009                                   MachineBasicBlock::iterator I) {
   1010   MachineBasicBlock::iterator E = MBB.end();
   1011 
   1012   // It's always safe to clobber EFLAGS at the end of a block.
   1013   if (I == E)
   1014     return true;
   1015 
   1016   // For compile time consideration, if we are not able to determine the
   1017   // safety after visiting 4 instructions in each direction, we will assume
   1018   // it's not safe.
   1019   MachineBasicBlock::iterator Iter = I;
   1020   for (unsigned i = 0; i < 4; ++i) {
   1021     bool SeenDef = false;
   1022     for (unsigned j = 0, e = Iter->getNumOperands(); j != e; ++j) {
   1023       MachineOperand &MO = Iter->getOperand(j);
   1024       if (!MO.isReg())
   1025         continue;
   1026       if (MO.getReg() == X86::EFLAGS) {
   1027         if (MO.isUse())
   1028           return false;
   1029         SeenDef = true;
   1030       }
   1031     }
   1032 
   1033     if (SeenDef)
   1034       // This instruction defines EFLAGS, no need to look any further.
   1035       return true;
   1036     ++Iter;
   1037     // Skip over DBG_VALUE.
   1038     while (Iter != E && Iter->isDebugValue())
   1039       ++Iter;
   1040 
   1041     // If we make it to the end of the block, it's safe to clobber EFLAGS.
   1042     if (Iter == E)
   1043       return true;
   1044   }
   1045 
   1046   MachineBasicBlock::iterator B = MBB.begin();
   1047   Iter = I;
   1048   for (unsigned i = 0; i < 4; ++i) {
   1049     // If we make it to the beginning of the block, it's safe to clobber
   1050     // EFLAGS iff EFLAGS is not live-in.
   1051     if (Iter == B)
   1052       return !MBB.isLiveIn(X86::EFLAGS);
   1053 
   1054     --Iter;
   1055     // Skip over DBG_VALUE.
   1056     while (Iter != B && Iter->isDebugValue())
   1057       --Iter;
   1058 
   1059     bool SawKill = false;
   1060     for (unsigned j = 0, e = Iter->getNumOperands(); j != e; ++j) {
   1061       MachineOperand &MO = Iter->getOperand(j);
   1062       if (MO.isReg() && MO.getReg() == X86::EFLAGS) {
   1063         if (MO.isDef()) return MO.isDead();
   1064         if (MO.isKill()) SawKill = true;
   1065       }
   1066     }
   1067 
   1068     if (SawKill)
   1069       // This instruction kills EFLAGS and doesn't redefine it, so
   1070       // there's no need to look further.
   1071       return true;
   1072   }
   1073 
   1074   // Conservative answer.
   1075   return false;
   1076 }
   1077 
   1078 void X86InstrInfo::reMaterialize(MachineBasicBlock &MBB,
   1079                                  MachineBasicBlock::iterator I,
   1080                                  unsigned DestReg, unsigned SubIdx,
   1081                                  const MachineInstr *Orig,
   1082                                  const TargetRegisterInfo &TRI) const {
   1083   DebugLoc DL = Orig->getDebugLoc();
   1084 
   1085   // MOV32r0 etc. are implemented with xor which clobbers condition code.
   1086   // Re-materialize them as movri instructions to avoid side effects.
   1087   bool Clone = true;
   1088   unsigned Opc = Orig->getOpcode();
   1089   switch (Opc) {
   1090   default: break;
   1091   case X86::MOV8r0:
   1092   case X86::MOV16r0:
   1093   case X86::MOV32r0:
   1094   case X86::MOV64r0: {
   1095     if (!isSafeToClobberEFLAGS(MBB, I)) {
   1096       switch (Opc) {
   1097       default: break;
   1098       case X86::MOV8r0:  Opc = X86::MOV8ri;  break;
   1099       case X86::MOV16r0: Opc = X86::MOV16ri; break;
   1100       case X86::MOV32r0: Opc = X86::MOV32ri; break;
   1101       case X86::MOV64r0: Opc = X86::MOV64ri64i32; break;
   1102       }
   1103       Clone = false;
   1104     }
   1105     break;
   1106   }
   1107   }
   1108 
   1109   if (Clone) {
   1110     MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
   1111     MBB.insert(I, MI);
   1112   } else {
   1113     BuildMI(MBB, I, DL, get(Opc)).addOperand(Orig->getOperand(0)).addImm(0);
   1114   }
   1115 
   1116   MachineInstr *NewMI = prior(I);
   1117   NewMI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI);
   1118 }
   1119 
   1120 /// hasLiveCondCodeDef - True if MI has a condition code def, e.g. EFLAGS, that
   1121 /// is not marked dead.
   1122 static bool hasLiveCondCodeDef(MachineInstr *MI) {
   1123   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
   1124     MachineOperand &MO = MI->getOperand(i);
   1125     if (MO.isReg() && MO.isDef() &&
   1126         MO.getReg() == X86::EFLAGS && !MO.isDead()) {
   1127       return true;
   1128     }
   1129   }
   1130   return false;
   1131 }
   1132 
   1133 /// convertToThreeAddressWithLEA - Helper for convertToThreeAddress when
   1134 /// 16-bit LEA is disabled, use 32-bit LEA to form 3-address code by promoting
   1135 /// to a 32-bit superregister and then truncating back down to a 16-bit
   1136 /// subregister.
   1137 MachineInstr *
   1138 X86InstrInfo::convertToThreeAddressWithLEA(unsigned MIOpc,
   1139                                            MachineFunction::iterator &MFI,
   1140                                            MachineBasicBlock::iterator &MBBI,
   1141                                            LiveVariables *LV) const {
   1142   MachineInstr *MI = MBBI;
   1143   unsigned Dest = MI->getOperand(0).getReg();
   1144   unsigned Src = MI->getOperand(1).getReg();
   1145   bool isDead = MI->getOperand(0).isDead();
   1146   bool isKill = MI->getOperand(1).isKill();
   1147 
   1148   unsigned Opc = TM.getSubtarget<X86Subtarget>().is64Bit()
   1149     ? X86::LEA64_32r : X86::LEA32r;
   1150   MachineRegisterInfo &RegInfo = MFI->getParent()->getRegInfo();
   1151   unsigned leaInReg = RegInfo.createVirtualRegister(&X86::GR32_NOSPRegClass);
   1152   unsigned leaOutReg = RegInfo.createVirtualRegister(&X86::GR32RegClass);
   1153 
   1154   // Build and insert into an implicit UNDEF value. This is OK because
   1155   // well be shifting and then extracting the lower 16-bits.
   1156   // This has the potential to cause partial register stall. e.g.
   1157   //   movw    (%rbp,%rcx,2), %dx
   1158   //   leal    -65(%rdx), %esi
   1159   // But testing has shown this *does* help performance in 64-bit mode (at
   1160   // least on modern x86 machines).
   1161   BuildMI(*MFI, MBBI, MI->getDebugLoc(), get(X86::IMPLICIT_DEF), leaInReg);
   1162   MachineInstr *InsMI =
   1163     BuildMI(*MFI, MBBI, MI->getDebugLoc(), get(TargetOpcode::COPY))
   1164     .addReg(leaInReg, RegState::Define, X86::sub_16bit)
   1165     .addReg(Src, getKillRegState(isKill));
   1166 
   1167   MachineInstrBuilder MIB = BuildMI(*MFI, MBBI, MI->getDebugLoc(),
   1168                                     get(Opc), leaOutReg);
   1169   switch (MIOpc) {
   1170   default:
   1171     llvm_unreachable(0);
   1172     break;
   1173   case X86::SHL16ri: {
   1174     unsigned ShAmt = MI->getOperand(2).getImm();
   1175     MIB.addReg(0).addImm(1 << ShAmt)
   1176        .addReg(leaInReg, RegState::Kill).addImm(0).addReg(0);
   1177     break;
   1178   }
   1179   case X86::INC16r:
   1180   case X86::INC64_16r:
   1181     addRegOffset(MIB, leaInReg, true, 1);
   1182     break;
   1183   case X86::DEC16r:
   1184   case X86::DEC64_16r:
   1185     addRegOffset(MIB, leaInReg, true, -1);
   1186     break;
   1187   case X86::ADD16ri:
   1188   case X86::ADD16ri8:
   1189   case X86::ADD16ri_DB:
   1190   case X86::ADD16ri8_DB:
   1191     addRegOffset(MIB, leaInReg, true, MI->getOperand(2).getImm());
   1192     break;
   1193   case X86::ADD16rr:
   1194   case X86::ADD16rr_DB: {
   1195     unsigned Src2 = MI->getOperand(2).getReg();
   1196     bool isKill2 = MI->getOperand(2).isKill();
   1197     unsigned leaInReg2 = 0;
   1198     MachineInstr *InsMI2 = 0;
   1199     if (Src == Src2) {
   1200       // ADD16rr %reg1028<kill>, %reg1028
   1201       // just a single insert_subreg.
   1202       addRegReg(MIB, leaInReg, true, leaInReg, false);
   1203     } else {
   1204       leaInReg2 = RegInfo.createVirtualRegister(&X86::GR32_NOSPRegClass);
   1205       // Build and insert into an implicit UNDEF value. This is OK because
   1206       // well be shifting and then extracting the lower 16-bits.
   1207       BuildMI(*MFI, MIB, MI->getDebugLoc(), get(X86::IMPLICIT_DEF), leaInReg2);
   1208       InsMI2 =
   1209         BuildMI(*MFI, MIB, MI->getDebugLoc(), get(TargetOpcode::COPY))
   1210         .addReg(leaInReg2, RegState::Define, X86::sub_16bit)
   1211         .addReg(Src2, getKillRegState(isKill2));
   1212       addRegReg(MIB, leaInReg, true, leaInReg2, true);
   1213     }
   1214     if (LV && isKill2 && InsMI2)
   1215       LV->replaceKillInstruction(Src2, MI, InsMI2);
   1216     break;
   1217   }
   1218   }
   1219 
   1220   MachineInstr *NewMI = MIB;
   1221   MachineInstr *ExtMI =
   1222     BuildMI(*MFI, MBBI, MI->getDebugLoc(), get(TargetOpcode::COPY))
   1223     .addReg(Dest, RegState::Define | getDeadRegState(isDead))
   1224     .addReg(leaOutReg, RegState::Kill, X86::sub_16bit);
   1225 
   1226   if (LV) {
   1227     // Update live variables
   1228     LV->getVarInfo(leaInReg).Kills.push_back(NewMI);
   1229     LV->getVarInfo(leaOutReg).Kills.push_back(ExtMI);
   1230     if (isKill)
   1231       LV->replaceKillInstruction(Src, MI, InsMI);
   1232     if (isDead)
   1233       LV->replaceKillInstruction(Dest, MI, ExtMI);
   1234   }
   1235 
   1236   return ExtMI;
   1237 }
   1238 
   1239 /// convertToThreeAddress - This method must be implemented by targets that
   1240 /// set the M_CONVERTIBLE_TO_3_ADDR flag.  When this flag is set, the target
   1241 /// may be able to convert a two-address instruction into a true
   1242 /// three-address instruction on demand.  This allows the X86 target (for
   1243 /// example) to convert ADD and SHL instructions into LEA instructions if they
   1244 /// would require register copies due to two-addressness.
   1245 ///
   1246 /// This method returns a null pointer if the transformation cannot be
   1247 /// performed, otherwise it returns the new instruction.
   1248 ///
   1249 MachineInstr *
   1250 X86InstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
   1251                                     MachineBasicBlock::iterator &MBBI,
   1252                                     LiveVariables *LV) const {
   1253   MachineInstr *MI = MBBI;
   1254   MachineFunction &MF = *MI->getParent()->getParent();
   1255   // All instructions input are two-addr instructions.  Get the known operands.
   1256   unsigned Dest = MI->getOperand(0).getReg();
   1257   unsigned Src = MI->getOperand(1).getReg();
   1258   bool isDead = MI->getOperand(0).isDead();
   1259   bool isKill = MI->getOperand(1).isKill();
   1260 
   1261   MachineInstr *NewMI = NULL;
   1262   // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's.  When
   1263   // we have better subtarget support, enable the 16-bit LEA generation here.
   1264   // 16-bit LEA is also slow on Core2.
   1265   bool DisableLEA16 = true;
   1266   bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
   1267 
   1268   unsigned MIOpc = MI->getOpcode();
   1269   switch (MIOpc) {
   1270   case X86::SHUFPSrri: {
   1271     assert(MI->getNumOperands() == 4 && "Unknown shufps instruction!");
   1272     if (!TM.getSubtarget<X86Subtarget>().hasSSE2()) return 0;
   1273 
   1274     unsigned B = MI->getOperand(1).getReg();
   1275     unsigned C = MI->getOperand(2).getReg();
   1276     if (B != C) return 0;
   1277     unsigned A = MI->getOperand(0).getReg();
   1278     unsigned M = MI->getOperand(3).getImm();
   1279     NewMI = BuildMI(MF, MI->getDebugLoc(), get(X86::PSHUFDri))
   1280       .addReg(A, RegState::Define | getDeadRegState(isDead))
   1281       .addReg(B, getKillRegState(isKill)).addImm(M);
   1282     break;
   1283   }
   1284   case X86::SHL64ri: {
   1285     assert(MI->getNumOperands() >= 3 && "Unknown shift instruction!");
   1286     // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
   1287     // the flags produced by a shift yet, so this is safe.
   1288     unsigned ShAmt = MI->getOperand(2).getImm();
   1289     if (ShAmt == 0 || ShAmt >= 4) return 0;
   1290 
   1291     // LEA can't handle RSP.
   1292     if (TargetRegisterInfo::isVirtualRegister(Src) &&
   1293         !MF.getRegInfo().constrainRegClass(Src, &X86::GR64_NOSPRegClass))
   1294       return 0;
   1295 
   1296     NewMI = BuildMI(MF, MI->getDebugLoc(), get(X86::LEA64r))
   1297       .addReg(Dest, RegState::Define | getDeadRegState(isDead))
   1298       .addReg(0).addImm(1 << ShAmt)
   1299       .addReg(Src, getKillRegState(isKill))
   1300       .addImm(0).addReg(0);
   1301     break;
   1302   }
   1303   case X86::SHL32ri: {
   1304     assert(MI->getNumOperands() >= 3 && "Unknown shift instruction!");
   1305     // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
   1306     // the flags produced by a shift yet, so this is safe.
   1307     unsigned ShAmt = MI->getOperand(2).getImm();
   1308     if (ShAmt == 0 || ShAmt >= 4) return 0;
   1309 
   1310     // LEA can't handle ESP.
   1311     if (TargetRegisterInfo::isVirtualRegister(Src) &&
   1312         !MF.getRegInfo().constrainRegClass(Src, &X86::GR32_NOSPRegClass))
   1313       return 0;
   1314 
   1315     unsigned Opc = is64Bit ? X86::LEA64_32r : X86::LEA32r;
   1316     NewMI = BuildMI(MF, MI->getDebugLoc(), get(Opc))
   1317       .addReg(Dest, RegState::Define | getDeadRegState(isDead))
   1318       .addReg(0).addImm(1 << ShAmt)
   1319       .addReg(Src, getKillRegState(isKill)).addImm(0).addReg(0);
   1320     break;
   1321   }
   1322   case X86::SHL16ri: {
   1323     assert(MI->getNumOperands() >= 3 && "Unknown shift instruction!");
   1324     // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
   1325     // the flags produced by a shift yet, so this is safe.
   1326     unsigned ShAmt = MI->getOperand(2).getImm();
   1327     if (ShAmt == 0 || ShAmt >= 4) return 0;
   1328 
   1329     if (DisableLEA16)
   1330       return is64Bit ? convertToThreeAddressWithLEA(MIOpc, MFI, MBBI, LV) : 0;
   1331     NewMI = BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
   1332       .addReg(Dest, RegState::Define | getDeadRegState(isDead))
   1333       .addReg(0).addImm(1 << ShAmt)
   1334       .addReg(Src, getKillRegState(isKill))
   1335       .addImm(0).addReg(0);
   1336     break;
   1337   }
   1338   default: {
   1339     // The following opcodes also sets the condition code register(s). Only
   1340     // convert them to equivalent lea if the condition code register def's
   1341     // are dead!
   1342     if (hasLiveCondCodeDef(MI))
   1343       return 0;
   1344 
   1345     switch (MIOpc) {
   1346     default: return 0;
   1347     case X86::INC64r:
   1348     case X86::INC32r:
   1349     case X86::INC64_32r: {
   1350       assert(MI->getNumOperands() >= 2 && "Unknown inc instruction!");
   1351       unsigned Opc = MIOpc == X86::INC64r ? X86::LEA64r
   1352         : (is64Bit ? X86::LEA64_32r : X86::LEA32r);
   1353 
   1354       // LEA can't handle RSP.
   1355       if (TargetRegisterInfo::isVirtualRegister(Src) &&
   1356           !MF.getRegInfo().constrainRegClass(Src,
   1357                             MIOpc == X86::INC64r ? X86::GR64_NOSPRegisterClass :
   1358                                                    X86::GR32_NOSPRegisterClass))
   1359         return 0;
   1360 
   1361       NewMI = addRegOffset(BuildMI(MF, MI->getDebugLoc(), get(Opc))
   1362                               .addReg(Dest, RegState::Define |
   1363                                       getDeadRegState(isDead)),
   1364                               Src, isKill, 1);
   1365       break;
   1366     }
   1367     case X86::INC16r:
   1368     case X86::INC64_16r:
   1369       if (DisableLEA16)
   1370         return is64Bit ? convertToThreeAddressWithLEA(MIOpc, MFI, MBBI, LV) : 0;
   1371       assert(MI->getNumOperands() >= 2 && "Unknown inc instruction!");
   1372       NewMI = addRegOffset(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
   1373                            .addReg(Dest, RegState::Define |
   1374                                    getDeadRegState(isDead)),
   1375                            Src, isKill, 1);
   1376       break;
   1377     case X86::DEC64r:
   1378     case X86::DEC32r:
   1379     case X86::DEC64_32r: {
   1380       assert(MI->getNumOperands() >= 2 && "Unknown dec instruction!");
   1381       unsigned Opc = MIOpc == X86::DEC64r ? X86::LEA64r
   1382         : (is64Bit ? X86::LEA64_32r : X86::LEA32r);
   1383       // LEA can't handle RSP.
   1384       if (TargetRegisterInfo::isVirtualRegister(Src) &&
   1385           !MF.getRegInfo().constrainRegClass(Src,
   1386                             MIOpc == X86::DEC64r ? X86::GR64_NOSPRegisterClass :
   1387                                                    X86::GR32_NOSPRegisterClass))
   1388         return 0;
   1389 
   1390       NewMI = addRegOffset(BuildMI(MF, MI->getDebugLoc(), get(Opc))
   1391                               .addReg(Dest, RegState::Define |
   1392                                       getDeadRegState(isDead)),
   1393                               Src, isKill, -1);
   1394       break;
   1395     }
   1396     case X86::DEC16r:
   1397     case X86::DEC64_16r:
   1398       if (DisableLEA16)
   1399         return is64Bit ? convertToThreeAddressWithLEA(MIOpc, MFI, MBBI, LV) : 0;
   1400       assert(MI->getNumOperands() >= 2 && "Unknown dec instruction!");
   1401       NewMI = addRegOffset(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
   1402                            .addReg(Dest, RegState::Define |
   1403                                    getDeadRegState(isDead)),
   1404                            Src, isKill, -1);
   1405       break;
   1406     case X86::ADD64rr:
   1407     case X86::ADD64rr_DB:
   1408     case X86::ADD32rr:
   1409     case X86::ADD32rr_DB: {
   1410       assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
   1411       unsigned Opc;
   1412       TargetRegisterClass *RC;
   1413       if (MIOpc == X86::ADD64rr || MIOpc == X86::ADD64rr_DB) {
   1414         Opc = X86::LEA64r;
   1415         RC = X86::GR64_NOSPRegisterClass;
   1416       } else {
   1417         Opc = is64Bit ? X86::LEA64_32r : X86::LEA32r;
   1418         RC = X86::GR32_NOSPRegisterClass;
   1419       }
   1420 
   1421 
   1422       unsigned Src2 = MI->getOperand(2).getReg();
   1423       bool isKill2 = MI->getOperand(2).isKill();
   1424 
   1425       // LEA can't handle RSP.
   1426       if (TargetRegisterInfo::isVirtualRegister(Src2) &&
   1427           !MF.getRegInfo().constrainRegClass(Src2, RC))
   1428         return 0;
   1429 
   1430       NewMI = addRegReg(BuildMI(MF, MI->getDebugLoc(), get(Opc))
   1431                         .addReg(Dest, RegState::Define |
   1432                                 getDeadRegState(isDead)),
   1433                         Src, isKill, Src2, isKill2);
   1434       if (LV && isKill2)
   1435         LV->replaceKillInstruction(Src2, MI, NewMI);
   1436       break;
   1437     }
   1438     case X86::ADD16rr:
   1439     case X86::ADD16rr_DB: {
   1440       if (DisableLEA16)
   1441         return is64Bit ? convertToThreeAddressWithLEA(MIOpc, MFI, MBBI, LV) : 0;
   1442       assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
   1443       unsigned Src2 = MI->getOperand(2).getReg();
   1444       bool isKill2 = MI->getOperand(2).isKill();
   1445       NewMI = addRegReg(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
   1446                         .addReg(Dest, RegState::Define |
   1447                                 getDeadRegState(isDead)),
   1448                         Src, isKill, Src2, isKill2);
   1449       if (LV && isKill2)
   1450         LV->replaceKillInstruction(Src2, MI, NewMI);
   1451       break;
   1452     }
   1453     case X86::ADD64ri32:
   1454     case X86::ADD64ri8:
   1455     case X86::ADD64ri32_DB:
   1456     case X86::ADD64ri8_DB:
   1457       assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
   1458       NewMI = addRegOffset(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA64r))
   1459                               .addReg(Dest, RegState::Define |
   1460                                       getDeadRegState(isDead)),
   1461                               Src, isKill, MI->getOperand(2).getImm());
   1462       break;
   1463     case X86::ADD32ri:
   1464     case X86::ADD32ri8:
   1465     case X86::ADD32ri_DB:
   1466     case X86::ADD32ri8_DB: {
   1467       assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
   1468       unsigned Opc = is64Bit ? X86::LEA64_32r : X86::LEA32r;
   1469       NewMI = addRegOffset(BuildMI(MF, MI->getDebugLoc(), get(Opc))
   1470                               .addReg(Dest, RegState::Define |
   1471                                       getDeadRegState(isDead)),
   1472                                 Src, isKill, MI->getOperand(2).getImm());
   1473       break;
   1474     }
   1475     case X86::ADD16ri:
   1476     case X86::ADD16ri8:
   1477     case X86::ADD16ri_DB:
   1478     case X86::ADD16ri8_DB:
   1479       if (DisableLEA16)
   1480         return is64Bit ? convertToThreeAddressWithLEA(MIOpc, MFI, MBBI, LV) : 0;
   1481       assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
   1482       NewMI = addRegOffset(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
   1483                               .addReg(Dest, RegState::Define |
   1484                                       getDeadRegState(isDead)),
   1485                               Src, isKill, MI->getOperand(2).getImm());
   1486       break;
   1487     }
   1488   }
   1489   }
   1490 
   1491   if (!NewMI) return 0;
   1492 
   1493   if (LV) {  // Update live variables
   1494     if (isKill)
   1495       LV->replaceKillInstruction(Src, MI, NewMI);
   1496     if (isDead)
   1497       LV->replaceKillInstruction(Dest, MI, NewMI);
   1498   }
   1499 
   1500   MFI->insert(MBBI, NewMI);          // Insert the new inst
   1501   return NewMI;
   1502 }
   1503 
   1504 /// commuteInstruction - We have a few instructions that must be hacked on to
   1505 /// commute them.
   1506 ///
   1507 MachineInstr *
   1508 X86InstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
   1509   switch (MI->getOpcode()) {
   1510   case X86::SHRD16rri8: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I)
   1511   case X86::SHLD16rri8: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I)
   1512   case X86::SHRD32rri8: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I)
   1513   case X86::SHLD32rri8: // A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I)
   1514   case X86::SHRD64rri8: // A = SHRD64rri8 B, C, I -> A = SHLD64rri8 C, B, (64-I)
   1515   case X86::SHLD64rri8:{// A = SHLD64rri8 B, C, I -> A = SHRD64rri8 C, B, (64-I)
   1516     unsigned Opc;
   1517     unsigned Size;
   1518     switch (MI->getOpcode()) {
   1519     default: llvm_unreachable("Unreachable!");
   1520     case X86::SHRD16rri8: Size = 16; Opc = X86::SHLD16rri8; break;
   1521     case X86::SHLD16rri8: Size = 16; Opc = X86::SHRD16rri8; break;
   1522     case X86::SHRD32rri8: Size = 32; Opc = X86::SHLD32rri8; break;
   1523     case X86::SHLD32rri8: Size = 32; Opc = X86::SHRD32rri8; break;
   1524     case X86::SHRD64rri8: Size = 64; Opc = X86::SHLD64rri8; break;
   1525     case X86::SHLD64rri8: Size = 64; Opc = X86::SHRD64rri8; break;
   1526     }
   1527     unsigned Amt = MI->getOperand(3).getImm();
   1528     if (NewMI) {
   1529       MachineFunction &MF = *MI->getParent()->getParent();
   1530       MI = MF.CloneMachineInstr(MI);
   1531       NewMI = false;
   1532     }
   1533     MI->setDesc(get(Opc));
   1534     MI->getOperand(3).setImm(Size-Amt);
   1535     return TargetInstrInfoImpl::commuteInstruction(MI, NewMI);
   1536   }
   1537   case X86::CMOVB16rr:
   1538   case X86::CMOVB32rr:
   1539   case X86::CMOVB64rr:
   1540   case X86::CMOVAE16rr:
   1541   case X86::CMOVAE32rr:
   1542   case X86::CMOVAE64rr:
   1543   case X86::CMOVE16rr:
   1544   case X86::CMOVE32rr:
   1545   case X86::CMOVE64rr:
   1546   case X86::CMOVNE16rr:
   1547   case X86::CMOVNE32rr:
   1548   case X86::CMOVNE64rr:
   1549   case X86::CMOVBE16rr:
   1550   case X86::CMOVBE32rr:
   1551   case X86::CMOVBE64rr:
   1552   case X86::CMOVA16rr:
   1553   case X86::CMOVA32rr:
   1554   case X86::CMOVA64rr:
   1555   case X86::CMOVL16rr:
   1556   case X86::CMOVL32rr:
   1557   case X86::CMOVL64rr:
   1558   case X86::CMOVGE16rr:
   1559   case X86::CMOVGE32rr:
   1560   case X86::CMOVGE64rr:
   1561   case X86::CMOVLE16rr:
   1562   case X86::CMOVLE32rr:
   1563   case X86::CMOVLE64rr:
   1564   case X86::CMOVG16rr:
   1565   case X86::CMOVG32rr:
   1566   case X86::CMOVG64rr:
   1567   case X86::CMOVS16rr:
   1568   case X86::CMOVS32rr:
   1569   case X86::CMOVS64rr:
   1570   case X86::CMOVNS16rr:
   1571   case X86::CMOVNS32rr:
   1572   case X86::CMOVNS64rr:
   1573   case X86::CMOVP16rr:
   1574   case X86::CMOVP32rr:
   1575   case X86::CMOVP64rr:
   1576   case X86::CMOVNP16rr:
   1577   case X86::CMOVNP32rr:
   1578   case X86::CMOVNP64rr:
   1579   case X86::CMOVO16rr:
   1580   case X86::CMOVO32rr:
   1581   case X86::CMOVO64rr:
   1582   case X86::CMOVNO16rr:
   1583   case X86::CMOVNO32rr:
   1584   case X86::CMOVNO64rr: {
   1585     unsigned Opc = 0;
   1586     switch (MI->getOpcode()) {
   1587     default: break;
   1588     case X86::CMOVB16rr:  Opc = X86::CMOVAE16rr; break;
   1589     case X86::CMOVB32rr:  Opc = X86::CMOVAE32rr; break;
   1590     case X86::CMOVB64rr:  Opc = X86::CMOVAE64rr; break;
   1591     case X86::CMOVAE16rr: Opc = X86::CMOVB16rr; break;
   1592     case X86::CMOVAE32rr: Opc = X86::CMOVB32rr; break;
   1593     case X86::CMOVAE64rr: Opc = X86::CMOVB64rr; break;
   1594     case X86::CMOVE16rr:  Opc = X86::CMOVNE16rr; break;
   1595     case X86::CMOVE32rr:  Opc = X86::CMOVNE32rr; break;
   1596     case X86::CMOVE64rr:  Opc = X86::CMOVNE64rr; break;
   1597     case X86::CMOVNE16rr: Opc = X86::CMOVE16rr; break;
   1598     case X86::CMOVNE32rr: Opc = X86::CMOVE32rr; break;
   1599     case X86::CMOVNE64rr: Opc = X86::CMOVE64rr; break;
   1600     case X86::CMOVBE16rr: Opc = X86::CMOVA16rr; break;
   1601     case X86::CMOVBE32rr: Opc = X86::CMOVA32rr; break;
   1602     case X86::CMOVBE64rr: Opc = X86::CMOVA64rr; break;
   1603     case X86::CMOVA16rr:  Opc = X86::CMOVBE16rr; break;
   1604     case X86::CMOVA32rr:  Opc = X86::CMOVBE32rr; break;
   1605     case X86::CMOVA64rr:  Opc = X86::CMOVBE64rr; break;
   1606     case X86::CMOVL16rr:  Opc = X86::CMOVGE16rr; break;
   1607     case X86::CMOVL32rr:  Opc = X86::CMOVGE32rr; break;
   1608     case X86::CMOVL64rr:  Opc = X86::CMOVGE64rr; break;
   1609     case X86::CMOVGE16rr: Opc = X86::CMOVL16rr; break;
   1610     case X86::CMOVGE32rr: Opc = X86::CMOVL32rr; break;
   1611     case X86::CMOVGE64rr: Opc = X86::CMOVL64rr; break;
   1612     case X86::CMOVLE16rr: Opc = X86::CMOVG16rr; break;
   1613     case X86::CMOVLE32rr: Opc = X86::CMOVG32rr; break;
   1614     case X86::CMOVLE64rr: Opc = X86::CMOVG64rr; break;
   1615     case X86::CMOVG16rr:  Opc = X86::CMOVLE16rr; break;
   1616     case X86::CMOVG32rr:  Opc = X86::CMOVLE32rr; break;
   1617     case X86::CMOVG64rr:  Opc = X86::CMOVLE64rr; break;
   1618     case X86::CMOVS16rr:  Opc = X86::CMOVNS16rr; break;
   1619     case X86::CMOVS32rr:  Opc = X86::CMOVNS32rr; break;
   1620     case X86::CMOVS64rr:  Opc = X86::CMOVNS64rr; break;
   1621     case X86::CMOVNS16rr: Opc = X86::CMOVS16rr; break;
   1622     case X86::CMOVNS32rr: Opc = X86::CMOVS32rr; break;
   1623     case X86::CMOVNS64rr: Opc = X86::CMOVS64rr; break;
   1624     case X86::CMOVP16rr:  Opc = X86::CMOVNP16rr; break;
   1625     case X86::CMOVP32rr:  Opc = X86::CMOVNP32rr; break;
   1626     case X86::CMOVP64rr:  Opc = X86::CMOVNP64rr; break;
   1627     case X86::CMOVNP16rr: Opc = X86::CMOVP16rr; break;
   1628     case X86::CMOVNP32rr: Opc = X86::CMOVP32rr; break;
   1629     case X86::CMOVNP64rr: Opc = X86::CMOVP64rr; break;
   1630     case X86::CMOVO16rr:  Opc = X86::CMOVNO16rr; break;
   1631     case X86::CMOVO32rr:  Opc = X86::CMOVNO32rr; break;
   1632     case X86::CMOVO64rr:  Opc = X86::CMOVNO64rr; break;
   1633     case X86::CMOVNO16rr: Opc = X86::CMOVO16rr; break;
   1634     case X86::CMOVNO32rr: Opc = X86::CMOVO32rr; break;
   1635     case X86::CMOVNO64rr: Opc = X86::CMOVO64rr; break;
   1636     }
   1637     if (NewMI) {
   1638       MachineFunction &MF = *MI->getParent()->getParent();
   1639       MI = MF.CloneMachineInstr(MI);
   1640       NewMI = false;
   1641     }
   1642     MI->setDesc(get(Opc));
   1643     // Fallthrough intended.
   1644   }
   1645   default:
   1646     return TargetInstrInfoImpl::commuteInstruction(MI, NewMI);
   1647   }
   1648 }
   1649 
   1650 static X86::CondCode GetCondFromBranchOpc(unsigned BrOpc) {
   1651   switch (BrOpc) {
   1652   default: return X86::COND_INVALID;
   1653   case X86::JE_4:  return X86::COND_E;
   1654   case X86::JNE_4: return X86::COND_NE;
   1655   case X86::JL_4:  return X86::COND_L;
   1656   case X86::JLE_4: return X86::COND_LE;
   1657   case X86::JG_4:  return X86::COND_G;
   1658   case X86::JGE_4: return X86::COND_GE;
   1659   case X86::JB_4:  return X86::COND_B;
   1660   case X86::JBE_4: return X86::COND_BE;
   1661   case X86::JA_4:  return X86::COND_A;
   1662   case X86::JAE_4: return X86::COND_AE;
   1663   case X86::JS_4:  return X86::COND_S;
   1664   case X86::JNS_4: return X86::COND_NS;
   1665   case X86::JP_4:  return X86::COND_P;
   1666   case X86::JNP_4: return X86::COND_NP;
   1667   case X86::JO_4:  return X86::COND_O;
   1668   case X86::JNO_4: return X86::COND_NO;
   1669   }
   1670 }
   1671 
   1672 unsigned X86::GetCondBranchFromCond(X86::CondCode CC) {
   1673   switch (CC) {
   1674   default: llvm_unreachable("Illegal condition code!");
   1675   case X86::COND_E:  return X86::JE_4;
   1676   case X86::COND_NE: return X86::JNE_4;
   1677   case X86::COND_L:  return X86::JL_4;
   1678   case X86::COND_LE: return X86::JLE_4;
   1679   case X86::COND_G:  return X86::JG_4;
   1680   case X86::COND_GE: return X86::JGE_4;
   1681   case X86::COND_B:  return X86::JB_4;
   1682   case X86::COND_BE: return X86::JBE_4;
   1683   case X86::COND_A:  return X86::JA_4;
   1684   case X86::COND_AE: return X86::JAE_4;
   1685   case X86::COND_S:  return X86::JS_4;
   1686   case X86::COND_NS: return X86::JNS_4;
   1687   case X86::COND_P:  return X86::JP_4;
   1688   case X86::COND_NP: return X86::JNP_4;
   1689   case X86::COND_O:  return X86::JO_4;
   1690   case X86::COND_NO: return X86::JNO_4;
   1691   }
   1692 }
   1693 
   1694 /// GetOppositeBranchCondition - Return the inverse of the specified condition,
   1695 /// e.g. turning COND_E to COND_NE.
   1696 X86::CondCode X86::GetOppositeBranchCondition(X86::CondCode CC) {
   1697   switch (CC) {
   1698   default: llvm_unreachable("Illegal condition code!");
   1699   case X86::COND_E:  return X86::COND_NE;
   1700   case X86::COND_NE: return X86::COND_E;
   1701   case X86::COND_L:  return X86::COND_GE;
   1702   case X86::COND_LE: return X86::COND_G;
   1703   case X86::COND_G:  return X86::COND_LE;
   1704   case X86::COND_GE: return X86::COND_L;
   1705   case X86::COND_B:  return X86::COND_AE;
   1706   case X86::COND_BE: return X86::COND_A;
   1707   case X86::COND_A:  return X86::COND_BE;
   1708   case X86::COND_AE: return X86::COND_B;
   1709   case X86::COND_S:  return X86::COND_NS;
   1710   case X86::COND_NS: return X86::COND_S;
   1711   case X86::COND_P:  return X86::COND_NP;
   1712   case X86::COND_NP: return X86::COND_P;
   1713   case X86::COND_O:  return X86::COND_NO;
   1714   case X86::COND_NO: return X86::COND_O;
   1715   }
   1716 }
   1717 
   1718 bool X86InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
   1719   const MCInstrDesc &MCID = MI->getDesc();
   1720   if (!MCID.isTerminator()) return false;
   1721 
   1722   // Conditional branch is a special case.
   1723   if (MCID.isBranch() && !MCID.isBarrier())
   1724     return true;
   1725   if (!MCID.isPredicable())
   1726     return true;
   1727   return !isPredicated(MI);
   1728 }
   1729 
   1730 bool X86InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
   1731                                  MachineBasicBlock *&TBB,
   1732                                  MachineBasicBlock *&FBB,
   1733                                  SmallVectorImpl<MachineOperand> &Cond,
   1734                                  bool AllowModify) const {
   1735   // Start from the bottom of the block and work up, examining the
   1736   // terminator instructions.
   1737   MachineBasicBlock::iterator I = MBB.end();
   1738   MachineBasicBlock::iterator UnCondBrIter = MBB.end();
   1739   while (I != MBB.begin()) {
   1740     --I;
   1741     if (I->isDebugValue())
   1742       continue;
   1743 
   1744     // Working from the bottom, when we see a non-terminator instruction, we're
   1745     // done.
   1746     if (!isUnpredicatedTerminator(I))
   1747       break;
   1748 
   1749     // A terminator that isn't a branch can't easily be handled by this
   1750     // analysis.
   1751     if (!I->getDesc().isBranch())
   1752       return true;
   1753 
   1754     // Handle unconditional branches.
   1755     if (I->getOpcode() == X86::JMP_4) {
   1756       UnCondBrIter = I;
   1757 
   1758       if (!AllowModify) {
   1759         TBB = I->getOperand(0).getMBB();
   1760         continue;
   1761       }
   1762 
   1763       // If the block has any instructions after a JMP, delete them.
   1764       while (llvm::next(I) != MBB.end())
   1765         llvm::next(I)->eraseFromParent();
   1766 
   1767       Cond.clear();
   1768       FBB = 0;
   1769 
   1770       // Delete the JMP if it's equivalent to a fall-through.
   1771       if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
   1772         TBB = 0;
   1773         I->eraseFromParent();
   1774         I = MBB.end();
   1775         UnCondBrIter = MBB.end();
   1776         continue;
   1777       }
   1778 
   1779       // TBB is used to indicate the unconditional destination.
   1780       TBB = I->getOperand(0).getMBB();
   1781       continue;
   1782     }
   1783 
   1784     // Handle conditional branches.
   1785     X86::CondCode BranchCode = GetCondFromBranchOpc(I->getOpcode());
   1786     if (BranchCode == X86::COND_INVALID)
   1787       return true;  // Can't handle indirect branch.
   1788 
   1789     // Working from the bottom, handle the first conditional branch.
   1790     if (Cond.empty()) {
   1791       MachineBasicBlock *TargetBB = I->getOperand(0).getMBB();
   1792       if (AllowModify && UnCondBrIter != MBB.end() &&
   1793           MBB.isLayoutSuccessor(TargetBB)) {
   1794         // If we can modify the code and it ends in something like:
   1795         //
   1796         //     jCC L1
   1797         //     jmp L2
   1798         //   L1:
   1799         //     ...
   1800         //   L2:
   1801         //
   1802         // Then we can change this to:
   1803         //
   1804         //     jnCC L2
   1805         //   L1:
   1806         //     ...
   1807         //   L2:
   1808         //
   1809         // Which is a bit more efficient.
   1810         // We conditionally jump to the fall-through block.
   1811         BranchCode = GetOppositeBranchCondition(BranchCode);
   1812         unsigned JNCC = GetCondBranchFromCond(BranchCode);
   1813         MachineBasicBlock::iterator OldInst = I;
   1814 
   1815         BuildMI(MBB, UnCondBrIter, MBB.findDebugLoc(I), get(JNCC))
   1816           .addMBB(UnCondBrIter->getOperand(0).getMBB());
   1817         BuildMI(MBB, UnCondBrIter, MBB.findDebugLoc(I), get(X86::JMP_4))
   1818           .addMBB(TargetBB);
   1819 
   1820         OldInst->eraseFromParent();
   1821         UnCondBrIter->eraseFromParent();
   1822 
   1823         // Restart the analysis.
   1824         UnCondBrIter = MBB.end();
   1825         I = MBB.end();
   1826         continue;
   1827       }
   1828 
   1829       FBB = TBB;
   1830       TBB = I->getOperand(0).getMBB();
   1831       Cond.push_back(MachineOperand::CreateImm(BranchCode));
   1832       continue;
   1833     }
   1834 
   1835     // Handle subsequent conditional branches. Only handle the case where all
   1836     // conditional branches branch to the same destination and their condition
   1837     // opcodes fit one of the special multi-branch idioms.
   1838     assert(Cond.size() == 1);
   1839     assert(TBB);
   1840 
   1841     // Only handle the case where all conditional branches branch to the same
   1842     // destination.
   1843     if (TBB != I->getOperand(0).getMBB())
   1844       return true;
   1845 
   1846     // If the conditions are the same, we can leave them alone.
   1847     X86::CondCode OldBranchCode = (X86::CondCode)Cond[0].getImm();
   1848     if (OldBranchCode == BranchCode)
   1849       continue;
   1850 
   1851     // If they differ, see if they fit one of the known patterns. Theoretically,
   1852     // we could handle more patterns here, but we shouldn't expect to see them
   1853     // if instruction selection has done a reasonable job.
   1854     if ((OldBranchCode == X86::COND_NP &&
   1855          BranchCode == X86::COND_E) ||
   1856         (OldBranchCode == X86::COND_E &&
   1857          BranchCode == X86::COND_NP))
   1858       BranchCode = X86::COND_NP_OR_E;
   1859     else if ((OldBranchCode == X86::COND_P &&
   1860               BranchCode == X86::COND_NE) ||
   1861              (OldBranchCode == X86::COND_NE &&
   1862               BranchCode == X86::COND_P))
   1863       BranchCode = X86::COND_NE_OR_P;
   1864     else
   1865       return true;
   1866 
   1867     // Update the MachineOperand.
   1868     Cond[0].setImm(BranchCode);
   1869   }
   1870 
   1871   return false;
   1872 }
   1873 
   1874 unsigned X86InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
   1875   MachineBasicBlock::iterator I = MBB.end();
   1876   unsigned Count = 0;
   1877 
   1878   while (I != MBB.begin()) {
   1879     --I;
   1880     if (I->isDebugValue())
   1881       continue;
   1882     if (I->getOpcode() != X86::JMP_4 &&
   1883         GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
   1884       break;
   1885     // Remove the branch.
   1886     I->eraseFromParent();
   1887     I = MBB.end();
   1888     ++Count;
   1889   }
   1890 
   1891   return Count;
   1892 }
   1893 
   1894 unsigned
   1895 X86InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
   1896                            MachineBasicBlock *FBB,
   1897                            const SmallVectorImpl<MachineOperand> &Cond,
   1898                            DebugLoc DL) const {
   1899   // Shouldn't be a fall through.
   1900   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
   1901   assert((Cond.size() == 1 || Cond.size() == 0) &&
   1902          "X86 branch conditions have one component!");
   1903 
   1904   if (Cond.empty()) {
   1905     // Unconditional branch?
   1906     assert(!FBB && "Unconditional branch with multiple successors!");
   1907     BuildMI(&MBB, DL, get(X86::JMP_4)).addMBB(TBB);
   1908     return 1;
   1909   }
   1910 
   1911   // Conditional branch.
   1912   unsigned Count = 0;
   1913   X86::CondCode CC = (X86::CondCode)Cond[0].getImm();
   1914   switch (CC) {
   1915   case X86::COND_NP_OR_E:
   1916     // Synthesize NP_OR_E with two branches.
   1917     BuildMI(&MBB, DL, get(X86::JNP_4)).addMBB(TBB);
   1918     ++Count;
   1919     BuildMI(&MBB, DL, get(X86::JE_4)).addMBB(TBB);
   1920     ++Count;
   1921     break;
   1922   case X86::COND_NE_OR_P:
   1923     // Synthesize NE_OR_P with two branches.
   1924     BuildMI(&MBB, DL, get(X86::JNE_4)).addMBB(TBB);
   1925     ++Count;
   1926     BuildMI(&MBB, DL, get(X86::JP_4)).addMBB(TBB);
   1927     ++Count;
   1928     break;
   1929   default: {
   1930     unsigned Opc = GetCondBranchFromCond(CC);
   1931     BuildMI(&MBB, DL, get(Opc)).addMBB(TBB);
   1932     ++Count;
   1933   }
   1934   }
   1935   if (FBB) {
   1936     // Two-way Conditional branch. Insert the second branch.
   1937     BuildMI(&MBB, DL, get(X86::JMP_4)).addMBB(FBB);
   1938     ++Count;
   1939   }
   1940   return Count;
   1941 }
   1942 
   1943 /// isHReg - Test if the given register is a physical h register.
   1944 static bool isHReg(unsigned Reg) {
   1945   return X86::GR8_ABCD_HRegClass.contains(Reg);
   1946 }
   1947 
   1948 // Try and copy between VR128/VR64 and GR64 registers.
   1949 static unsigned CopyToFromAsymmetricReg(unsigned DestReg, unsigned SrcReg) {
   1950   // SrcReg(VR128) -> DestReg(GR64)
   1951   // SrcReg(VR64)  -> DestReg(GR64)
   1952   // SrcReg(GR64)  -> DestReg(VR128)
   1953   // SrcReg(GR64)  -> DestReg(VR64)
   1954 
   1955   if (X86::GR64RegClass.contains(DestReg)) {
   1956     if (X86::VR128RegClass.contains(SrcReg)) {
   1957       // Copy from a VR128 register to a GR64 register.
   1958       return X86::MOVPQIto64rr;
   1959     } else if (X86::VR64RegClass.contains(SrcReg)) {
   1960       // Copy from a VR64 register to a GR64 register.
   1961       return X86::MOVSDto64rr;
   1962     }
   1963   } else if (X86::GR64RegClass.contains(SrcReg)) {
   1964     // Copy from a GR64 register to a VR128 register.
   1965     if (X86::VR128RegClass.contains(DestReg))
   1966       return X86::MOV64toPQIrr;
   1967     // Copy from a GR64 register to a VR64 register.
   1968     else if (X86::VR64RegClass.contains(DestReg))
   1969       return X86::MOV64toSDrr;
   1970   }
   1971 
   1972   return 0;
   1973 }
   1974 
   1975 void X86InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
   1976                                MachineBasicBlock::iterator MI, DebugLoc DL,
   1977                                unsigned DestReg, unsigned SrcReg,
   1978                                bool KillSrc) const {
   1979   // First deal with the normal symmetric copies.
   1980   unsigned Opc = 0;
   1981   if (X86::GR64RegClass.contains(DestReg, SrcReg))
   1982     Opc = X86::MOV64rr;
   1983   else if (X86::GR32RegClass.contains(DestReg, SrcReg))
   1984     Opc = X86::MOV32rr;
   1985   else if (X86::GR16RegClass.contains(DestReg, SrcReg))
   1986     Opc = X86::MOV16rr;
   1987   else if (X86::GR8RegClass.contains(DestReg, SrcReg)) {
   1988     // Copying to or from a physical H register on x86-64 requires a NOREX
   1989     // move.  Otherwise use a normal move.
   1990     if ((isHReg(DestReg) || isHReg(SrcReg)) &&
   1991         TM.getSubtarget<X86Subtarget>().is64Bit())
   1992       Opc = X86::MOV8rr_NOREX;
   1993     else
   1994       Opc = X86::MOV8rr;
   1995   } else if (X86::VR128RegClass.contains(DestReg, SrcReg))
   1996     Opc = X86::MOVAPSrr;
   1997   else if (X86::VR256RegClass.contains(DestReg, SrcReg))
   1998     Opc = X86::VMOVAPSYrr;
   1999   else if (X86::VR64RegClass.contains(DestReg, SrcReg))
   2000     Opc = X86::MMX_MOVQ64rr;
   2001   else
   2002     Opc = CopyToFromAsymmetricReg(DestReg, SrcReg);
   2003 
   2004   if (Opc) {
   2005     BuildMI(MBB, MI, DL, get(Opc), DestReg)
   2006       .addReg(SrcReg, getKillRegState(KillSrc));
   2007     return;
   2008   }
   2009 
   2010   // Moving EFLAGS to / from another register requires a push and a pop.
   2011   if (SrcReg == X86::EFLAGS) {
   2012     if (X86::GR64RegClass.contains(DestReg)) {
   2013       BuildMI(MBB, MI, DL, get(X86::PUSHF64));
   2014       BuildMI(MBB, MI, DL, get(X86::POP64r), DestReg);
   2015       return;
   2016     } else if (X86::GR32RegClass.contains(DestReg)) {
   2017       BuildMI(MBB, MI, DL, get(X86::PUSHF32));
   2018       BuildMI(MBB, MI, DL, get(X86::POP32r), DestReg);
   2019       return;
   2020     }
   2021   }
   2022   if (DestReg == X86::EFLAGS) {
   2023     if (X86::GR64RegClass.contains(SrcReg)) {
   2024       BuildMI(MBB, MI, DL, get(X86::PUSH64r))
   2025         .addReg(SrcReg, getKillRegState(KillSrc));
   2026       BuildMI(MBB, MI, DL, get(X86::POPF64));
   2027       return;
   2028     } else if (X86::GR32RegClass.contains(SrcReg)) {
   2029       BuildMI(MBB, MI, DL, get(X86::PUSH32r))
   2030         .addReg(SrcReg, getKillRegState(KillSrc));
   2031       BuildMI(MBB, MI, DL, get(X86::POPF32));
   2032       return;
   2033     }
   2034   }
   2035 
   2036   DEBUG(dbgs() << "Cannot copy " << RI.getName(SrcReg)
   2037                << " to " << RI.getName(DestReg) << '\n');
   2038   llvm_unreachable("Cannot emit physreg copy instruction");
   2039 }
   2040 
   2041 static unsigned getLoadStoreRegOpcode(unsigned Reg,
   2042                                       const TargetRegisterClass *RC,
   2043                                       bool isStackAligned,
   2044                                       const TargetMachine &TM,
   2045                                       bool load) {
   2046   switch (RC->getSize()) {
   2047   default:
   2048     llvm_unreachable("Unknown spill size");
   2049   case 1:
   2050     assert(X86::GR8RegClass.hasSubClassEq(RC) && "Unknown 1-byte regclass");
   2051     if (TM.getSubtarget<X86Subtarget>().is64Bit())
   2052       // Copying to or from a physical H register on x86-64 requires a NOREX
   2053       // move.  Otherwise use a normal move.
   2054       if (isHReg(Reg) || X86::GR8_ABCD_HRegClass.hasSubClassEq(RC))
   2055         return load ? X86::MOV8rm_NOREX : X86::MOV8mr_NOREX;
   2056     return load ? X86::MOV8rm : X86::MOV8mr;
   2057   case 2:
   2058     assert(X86::GR16RegClass.hasSubClassEq(RC) && "Unknown 2-byte regclass");
   2059     return load ? X86::MOV16rm : X86::MOV16mr;
   2060   case 4:
   2061     if (X86::GR32RegClass.hasSubClassEq(RC))
   2062       return load ? X86::MOV32rm : X86::MOV32mr;
   2063     if (X86::FR32RegClass.hasSubClassEq(RC))
   2064       return load ? X86::MOVSSrm : X86::MOVSSmr;
   2065     if (X86::RFP32RegClass.hasSubClassEq(RC))
   2066       return load ? X86::LD_Fp32m : X86::ST_Fp32m;
   2067     llvm_unreachable("Unknown 4-byte regclass");
   2068   case 8:
   2069     if (X86::GR64RegClass.hasSubClassEq(RC))
   2070       return load ? X86::MOV64rm : X86::MOV64mr;
   2071     if (X86::FR64RegClass.hasSubClassEq(RC))
   2072       return load ? X86::MOVSDrm : X86::MOVSDmr;
   2073     if (X86::VR64RegClass.hasSubClassEq(RC))
   2074       return load ? X86::MMX_MOVQ64rm : X86::MMX_MOVQ64mr;
   2075     if (X86::RFP64RegClass.hasSubClassEq(RC))
   2076       return load ? X86::LD_Fp64m : X86::ST_Fp64m;
   2077     llvm_unreachable("Unknown 8-byte regclass");
   2078   case 10:
   2079     assert(X86::RFP80RegClass.hasSubClassEq(RC) && "Unknown 10-byte regclass");
   2080     return load ? X86::LD_Fp80m : X86::ST_FpP80m;
   2081   case 16:
   2082     assert(X86::VR128RegClass.hasSubClassEq(RC) && "Unknown 16-byte regclass");
   2083     // If stack is realigned we can use aligned stores.
   2084     if (isStackAligned)
   2085       return load ? X86::MOVAPSrm : X86::MOVAPSmr;
   2086     else
   2087       return load ? X86::MOVUPSrm : X86::MOVUPSmr;
   2088   case 32:
   2089     assert(X86::VR256RegClass.hasSubClassEq(RC) && "Unknown 32-byte regclass");
   2090     // If stack is realigned we can use aligned stores.
   2091     if (isStackAligned)
   2092       return load ? X86::VMOVAPSYrm : X86::VMOVAPSYmr;
   2093     else
   2094       return load ? X86::VMOVUPSYrm : X86::VMOVUPSYmr;
   2095   }
   2096 }
   2097 
   2098 static unsigned getStoreRegOpcode(unsigned SrcReg,
   2099                                   const TargetRegisterClass *RC,
   2100                                   bool isStackAligned,
   2101                                   TargetMachine &TM) {
   2102   return getLoadStoreRegOpcode(SrcReg, RC, isStackAligned, TM, false);
   2103 }
   2104 
   2105 
   2106 static unsigned getLoadRegOpcode(unsigned DestReg,
   2107                                  const TargetRegisterClass *RC,
   2108                                  bool isStackAligned,
   2109                                  const TargetMachine &TM) {
   2110   return getLoadStoreRegOpcode(DestReg, RC, isStackAligned, TM, true);
   2111 }
   2112 
   2113 void X86InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
   2114                                        MachineBasicBlock::iterator MI,
   2115                                        unsigned SrcReg, bool isKill, int FrameIdx,
   2116                                        const TargetRegisterClass *RC,
   2117                                        const TargetRegisterInfo *TRI) const {
   2118   const MachineFunction &MF = *MBB.getParent();
   2119   assert(MF.getFrameInfo()->getObjectSize(FrameIdx) >= RC->getSize() &&
   2120          "Stack slot too small for store");
   2121   bool isAligned = (TM.getFrameLowering()->getStackAlignment() >= 16) ||
   2122     RI.canRealignStack(MF);
   2123   unsigned Opc = getStoreRegOpcode(SrcReg, RC, isAligned, TM);
   2124   DebugLoc DL = MBB.findDebugLoc(MI);
   2125   addFrameReference(BuildMI(MBB, MI, DL, get(Opc)), FrameIdx)
   2126     .addReg(SrcReg, getKillRegState(isKill));
   2127 }
   2128 
   2129 void X86InstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
   2130                                   bool isKill,
   2131                                   SmallVectorImpl<MachineOperand> &Addr,
   2132                                   const TargetRegisterClass *RC,
   2133                                   MachineInstr::mmo_iterator MMOBegin,
   2134                                   MachineInstr::mmo_iterator MMOEnd,
   2135                                   SmallVectorImpl<MachineInstr*> &NewMIs) const {
   2136   bool isAligned = MMOBegin != MMOEnd && (*MMOBegin)->getAlignment() >= 16;
   2137   unsigned Opc = getStoreRegOpcode(SrcReg, RC, isAligned, TM);
   2138   DebugLoc DL;
   2139   MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc));
   2140   for (unsigned i = 0, e = Addr.size(); i != e; ++i)
   2141     MIB.addOperand(Addr[i]);
   2142   MIB.addReg(SrcReg, getKillRegState(isKill));
   2143   (*MIB).setMemRefs(MMOBegin, MMOEnd);
   2144   NewMIs.push_back(MIB);
   2145 }
   2146 
   2147 
   2148 void X86InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
   2149                                         MachineBasicBlock::iterator MI,
   2150                                         unsigned DestReg, int FrameIdx,
   2151                                         const TargetRegisterClass *RC,
   2152                                         const TargetRegisterInfo *TRI) const {
   2153   const MachineFunction &MF = *MBB.getParent();
   2154   bool isAligned = (TM.getFrameLowering()->getStackAlignment() >= 16) ||
   2155     RI.canRealignStack(MF);
   2156   unsigned Opc = getLoadRegOpcode(DestReg, RC, isAligned, TM);
   2157   DebugLoc DL = MBB.findDebugLoc(MI);
   2158   addFrameReference(BuildMI(MBB, MI, DL, get(Opc), DestReg), FrameIdx);
   2159 }
   2160 
   2161 void X86InstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
   2162                                  SmallVectorImpl<MachineOperand> &Addr,
   2163                                  const TargetRegisterClass *RC,
   2164                                  MachineInstr::mmo_iterator MMOBegin,
   2165                                  MachineInstr::mmo_iterator MMOEnd,
   2166                                  SmallVectorImpl<MachineInstr*> &NewMIs) const {
   2167   bool isAligned = MMOBegin != MMOEnd && (*MMOBegin)->getAlignment() >= 16;
   2168   unsigned Opc = getLoadRegOpcode(DestReg, RC, isAligned, TM);
   2169   DebugLoc DL;
   2170   MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc), DestReg);
   2171   for (unsigned i = 0, e = Addr.size(); i != e; ++i)
   2172     MIB.addOperand(Addr[i]);
   2173   (*MIB).setMemRefs(MMOBegin, MMOEnd);
   2174   NewMIs.push_back(MIB);
   2175 }
   2176 
   2177 MachineInstr*
   2178 X86InstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
   2179                                        int FrameIx, uint64_t Offset,
   2180                                        const MDNode *MDPtr,
   2181                                        DebugLoc DL) const {
   2182   X86AddressMode AM;
   2183   AM.BaseType = X86AddressMode::FrameIndexBase;
   2184   AM.Base.FrameIndex = FrameIx;
   2185   MachineInstrBuilder MIB = BuildMI(MF, DL, get(X86::DBG_VALUE));
   2186   addFullAddress(MIB, AM).addImm(Offset).addMetadata(MDPtr);
   2187   return &*MIB;
   2188 }
   2189 
   2190 static MachineInstr *FuseTwoAddrInst(MachineFunction &MF, unsigned Opcode,
   2191                                      const SmallVectorImpl<MachineOperand> &MOs,
   2192                                      MachineInstr *MI,
   2193                                      const TargetInstrInfo &TII) {
   2194   // Create the base instruction with the memory operand as the first part.
   2195   MachineInstr *NewMI = MF.CreateMachineInstr(TII.get(Opcode),
   2196                                               MI->getDebugLoc(), true);
   2197   MachineInstrBuilder MIB(NewMI);
   2198   unsigned NumAddrOps = MOs.size();
   2199   for (unsigned i = 0; i != NumAddrOps; ++i)
   2200     MIB.addOperand(MOs[i]);
   2201   if (NumAddrOps < 4)  // FrameIndex only
   2202     addOffset(MIB, 0);
   2203 
   2204   // Loop over the rest of the ri operands, converting them over.
   2205   unsigned NumOps = MI->getDesc().getNumOperands()-2;
   2206   for (unsigned i = 0; i != NumOps; ++i) {
   2207     MachineOperand &MO = MI->getOperand(i+2);
   2208     MIB.addOperand(MO);
   2209   }
   2210   for (unsigned i = NumOps+2, e = MI->getNumOperands(); i != e; ++i) {
   2211     MachineOperand &MO = MI->getOperand(i);
   2212     MIB.addOperand(MO);
   2213   }
   2214   return MIB;
   2215 }
   2216 
   2217 static MachineInstr *FuseInst(MachineFunction &MF,
   2218                               unsigned Opcode, unsigned OpNo,
   2219                               const SmallVectorImpl<MachineOperand> &MOs,
   2220                               MachineInstr *MI, const TargetInstrInfo &TII) {
   2221   MachineInstr *NewMI = MF.CreateMachineInstr(TII.get(Opcode),
   2222                                               MI->getDebugLoc(), true);
   2223   MachineInstrBuilder MIB(NewMI);
   2224 
   2225   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
   2226     MachineOperand &MO = MI->getOperand(i);
   2227     if (i == OpNo) {
   2228       assert(MO.isReg() && "Expected to fold into reg operand!");
   2229       unsigned NumAddrOps = MOs.size();
   2230       for (unsigned i = 0; i != NumAddrOps; ++i)
   2231         MIB.addOperand(MOs[i]);
   2232       if (NumAddrOps < 4)  // FrameIndex only
   2233         addOffset(MIB, 0);
   2234     } else {
   2235       MIB.addOperand(MO);
   2236     }
   2237   }
   2238   return MIB;
   2239 }
   2240 
   2241 static MachineInstr *MakeM0Inst(const TargetInstrInfo &TII, unsigned Opcode,
   2242                                 const SmallVectorImpl<MachineOperand> &MOs,
   2243                                 MachineInstr *MI) {
   2244   MachineFunction &MF = *MI->getParent()->getParent();
   2245   MachineInstrBuilder MIB = BuildMI(MF, MI->getDebugLoc(), TII.get(Opcode));
   2246 
   2247   unsigned NumAddrOps = MOs.size();
   2248   for (unsigned i = 0; i != NumAddrOps; ++i)
   2249     MIB.addOperand(MOs[i]);
   2250   if (NumAddrOps < 4)  // FrameIndex only
   2251     addOffset(MIB, 0);
   2252   return MIB.addImm(0);
   2253 }
   2254 
   2255 MachineInstr*
   2256 X86InstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
   2257                                     MachineInstr *MI, unsigned i,
   2258                                     const SmallVectorImpl<MachineOperand> &MOs,
   2259                                     unsigned Size, unsigned Align) const {
   2260   const DenseMap<unsigned, std::pair<unsigned,unsigned> > *OpcodeTablePtr = 0;
   2261   bool isTwoAddrFold = false;
   2262   unsigned NumOps = MI->getDesc().getNumOperands();
   2263   bool isTwoAddr = NumOps > 1 &&
   2264     MI->getDesc().getOperandConstraint(1, MCOI::TIED_TO) != -1;
   2265 
   2266   // FIXME: AsmPrinter doesn't know how to handle
   2267   // X86II::MO_GOT_ABSOLUTE_ADDRESS after folding.
   2268   if (MI->getOpcode() == X86::ADD32ri &&
   2269       MI->getOperand(2).getTargetFlags() == X86II::MO_GOT_ABSOLUTE_ADDRESS)
   2270     return NULL;
   2271 
   2272   MachineInstr *NewMI = NULL;
   2273   // Folding a memory location into the two-address part of a two-address
   2274   // instruction is different than folding it other places.  It requires
   2275   // replacing the *two* registers with the memory location.
   2276   if (isTwoAddr && NumOps >= 2 && i < 2 &&
   2277       MI->getOperand(0).isReg() &&
   2278       MI->getOperand(1).isReg() &&
   2279       MI->getOperand(0).getReg() == MI->getOperand(1).getReg()) {
   2280     OpcodeTablePtr = &RegOp2MemOpTable2Addr;
   2281     isTwoAddrFold = true;
   2282   } else if (i == 0) { // If operand 0
   2283     if (MI->getOpcode() == X86::MOV64r0)
   2284       NewMI = MakeM0Inst(*this, X86::MOV64mi32, MOs, MI);
   2285     else if (MI->getOpcode() == X86::MOV32r0)
   2286       NewMI = MakeM0Inst(*this, X86::MOV32mi, MOs, MI);
   2287     else if (MI->getOpcode() == X86::MOV16r0)
   2288       NewMI = MakeM0Inst(*this, X86::MOV16mi, MOs, MI);
   2289     else if (MI->getOpcode() == X86::MOV8r0)
   2290       NewMI = MakeM0Inst(*this, X86::MOV8mi, MOs, MI);
   2291     if (NewMI)
   2292       return NewMI;
   2293 
   2294     OpcodeTablePtr = &RegOp2MemOpTable0;
   2295   } else if (i == 1) {
   2296     OpcodeTablePtr = &RegOp2MemOpTable1;
   2297   } else if (i == 2) {
   2298     OpcodeTablePtr = &RegOp2MemOpTable2;
   2299   }
   2300 
   2301   // If table selected...
   2302   if (OpcodeTablePtr) {
   2303     // Find the Opcode to fuse
   2304     DenseMap<unsigned, std::pair<unsigned,unsigned> >::const_iterator I =
   2305       OpcodeTablePtr->find(MI->getOpcode());
   2306     if (I != OpcodeTablePtr->end()) {
   2307       unsigned Opcode = I->second.first;
   2308       unsigned MinAlign = I->second.second;
   2309       if (Align < MinAlign)
   2310         return NULL;
   2311       bool NarrowToMOV32rm = false;
   2312       if (Size) {
   2313         unsigned RCSize = getRegClass(MI->getDesc(), i, &RI)->getSize();
   2314         if (Size < RCSize) {
   2315           // Check if it's safe to fold the load. If the size of the object is
   2316           // narrower than the load width, then it's not.
   2317           if (Opcode != X86::MOV64rm || RCSize != 8 || Size != 4)
   2318             return NULL;
   2319           // If this is a 64-bit load, but the spill slot is 32, then we can do
   2320           // a 32-bit load which is implicitly zero-extended. This likely is due
   2321           // to liveintervalanalysis remat'ing a load from stack slot.
   2322           if (MI->getOperand(0).getSubReg() || MI->getOperand(1).getSubReg())
   2323             return NULL;
   2324           Opcode = X86::MOV32rm;
   2325           NarrowToMOV32rm = true;
   2326         }
   2327       }
   2328 
   2329       if (isTwoAddrFold)
   2330         NewMI = FuseTwoAddrInst(MF, Opcode, MOs, MI, *this);
   2331       else
   2332         NewMI = FuseInst(MF, Opcode, i, MOs, MI, *this);
   2333 
   2334       if (NarrowToMOV32rm) {
   2335         // If this is the special case where we use a MOV32rm to load a 32-bit
   2336         // value and zero-extend the top bits. Change the destination register
   2337         // to a 32-bit one.
   2338         unsigned DstReg = NewMI->getOperand(0).getReg();
   2339         if (TargetRegisterInfo::isPhysicalRegister(DstReg))
   2340           NewMI->getOperand(0).setReg(RI.getSubReg(DstReg,
   2341                                                    X86::sub_32bit));
   2342         else
   2343           NewMI->getOperand(0).setSubReg(X86::sub_32bit);
   2344       }
   2345       return NewMI;
   2346     }
   2347   }
   2348 
   2349   // No fusion
   2350   if (PrintFailedFusing && !MI->isCopy())
   2351     dbgs() << "We failed to fuse operand " << i << " in " << *MI;
   2352   return NULL;
   2353 }
   2354 
   2355 
   2356 MachineInstr* X86InstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
   2357                                                   MachineInstr *MI,
   2358                                            const SmallVectorImpl<unsigned> &Ops,
   2359                                                   int FrameIndex) const {
   2360   // Check switch flag
   2361   if (NoFusing) return NULL;
   2362 
   2363   if (!MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize))
   2364     switch (MI->getOpcode()) {
   2365     case X86::CVTSD2SSrr:
   2366     case X86::Int_CVTSD2SSrr:
   2367     case X86::CVTSS2SDrr:
   2368     case X86::Int_CVTSS2SDrr:
   2369     case X86::RCPSSr:
   2370     case X86::RCPSSr_Int:
   2371     case X86::ROUNDSDr:
   2372     case X86::ROUNDSSr:
   2373     case X86::RSQRTSSr:
   2374     case X86::RSQRTSSr_Int:
   2375     case X86::SQRTSSr:
   2376     case X86::SQRTSSr_Int:
   2377       return 0;
   2378     }
   2379 
   2380   const MachineFrameInfo *MFI = MF.getFrameInfo();
   2381   unsigned Size = MFI->getObjectSize(FrameIndex);
   2382   unsigned Alignment = MFI->getObjectAlignment(FrameIndex);
   2383   if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
   2384     unsigned NewOpc = 0;
   2385     unsigned RCSize = 0;
   2386     switch (MI->getOpcode()) {
   2387     default: return NULL;
   2388     case X86::TEST8rr:  NewOpc = X86::CMP8ri; RCSize = 1; break;
   2389     case X86::TEST16rr: NewOpc = X86::CMP16ri8; RCSize = 2; break;
   2390     case X86::TEST32rr: NewOpc = X86::CMP32ri8; RCSize = 4; break;
   2391     case X86::TEST64rr: NewOpc = X86::CMP64ri8; RCSize = 8; break;
   2392     }
   2393     // Check if it's safe to fold the load. If the size of the object is
   2394     // narrower than the load width, then it's not.
   2395     if (Size < RCSize)
   2396       return NULL;
   2397     // Change to CMPXXri r, 0 first.
   2398     MI->setDesc(get(NewOpc));
   2399     MI->getOperand(1).ChangeToImmediate(0);
   2400   } else if (Ops.size() != 1)
   2401     return NULL;
   2402 
   2403   SmallVector<MachineOperand,4> MOs;
   2404   MOs.push_back(MachineOperand::CreateFI(FrameIndex));
   2405   return foldMemoryOperandImpl(MF, MI, Ops[0], MOs, Size, Alignment);
   2406 }
   2407 
   2408 MachineInstr* X86InstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
   2409                                                   MachineInstr *MI,
   2410                                            const SmallVectorImpl<unsigned> &Ops,
   2411                                                   MachineInstr *LoadMI) const {
   2412   // Check switch flag
   2413   if (NoFusing) return NULL;
   2414 
   2415   if (!MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize))
   2416     switch (MI->getOpcode()) {
   2417     case X86::CVTSD2SSrr:
   2418     case X86::Int_CVTSD2SSrr:
   2419     case X86::CVTSS2SDrr:
   2420     case X86::Int_CVTSS2SDrr:
   2421     case X86::RCPSSr:
   2422     case X86::RCPSSr_Int:
   2423     case X86::ROUNDSDr:
   2424     case X86::ROUNDSSr:
   2425     case X86::RSQRTSSr:
   2426     case X86::RSQRTSSr_Int:
   2427     case X86::SQRTSSr:
   2428     case X86::SQRTSSr_Int:
   2429       return 0;
   2430     }
   2431 
   2432   // Determine the alignment of the load.
   2433   unsigned Alignment = 0;
   2434   if (LoadMI->hasOneMemOperand())
   2435     Alignment = (*LoadMI->memoperands_begin())->getAlignment();
   2436   else
   2437     switch (LoadMI->getOpcode()) {
   2438     case X86::AVX_SET0PSY:
   2439     case X86::AVX_SET0PDY:
   2440       Alignment = 32;
   2441       break;
   2442     case X86::V_SET0PS:
   2443     case X86::V_SET0PD:
   2444     case X86::V_SET0PI:
   2445     case X86::V_SETALLONES:
   2446     case X86::AVX_SET0PS:
   2447     case X86::AVX_SET0PD:
   2448     case X86::AVX_SET0PI:
   2449       Alignment = 16;
   2450       break;
   2451     case X86::FsFLD0SD:
   2452     case X86::VFsFLD0SD:
   2453       Alignment = 8;
   2454       break;
   2455     case X86::FsFLD0SS:
   2456     case X86::VFsFLD0SS:
   2457       Alignment = 4;
   2458       break;
   2459     default:
   2460       return 0;
   2461     }
   2462   if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
   2463     unsigned NewOpc = 0;
   2464     switch (MI->getOpcode()) {
   2465     default: return NULL;
   2466     case X86::TEST8rr:  NewOpc = X86::CMP8ri; break;
   2467     case X86::TEST16rr: NewOpc = X86::CMP16ri8; break;
   2468     case X86::TEST32rr: NewOpc = X86::CMP32ri8; break;
   2469     case X86::TEST64rr: NewOpc = X86::CMP64ri8; break;
   2470     }
   2471     // Change to CMPXXri r, 0 first.
   2472     MI->setDesc(get(NewOpc));
   2473     MI->getOperand(1).ChangeToImmediate(0);
   2474   } else if (Ops.size() != 1)
   2475     return NULL;
   2476 
   2477   // Make sure the subregisters match.
   2478   // Otherwise we risk changing the size of the load.
   2479   if (LoadMI->getOperand(0).getSubReg() != MI->getOperand(Ops[0]).getSubReg())
   2480     return NULL;
   2481 
   2482   SmallVector<MachineOperand,X86::AddrNumOperands> MOs;
   2483   switch (LoadMI->getOpcode()) {
   2484   case X86::V_SET0PS:
   2485   case X86::V_SET0PD:
   2486   case X86::V_SET0PI:
   2487   case X86::V_SETALLONES:
   2488   case X86::AVX_SET0PS:
   2489   case X86::AVX_SET0PD:
   2490   case X86::AVX_SET0PI:
   2491   case X86::AVX_SET0PSY:
   2492   case X86::AVX_SET0PDY:
   2493   case X86::FsFLD0SD:
   2494   case X86::FsFLD0SS: {
   2495     // Folding a V_SET0P? or V_SETALLONES as a load, to ease register pressure.
   2496     // Create a constant-pool entry and operands to load from it.
   2497 
   2498     // Medium and large mode can't fold loads this way.
   2499     if (TM.getCodeModel() != CodeModel::Small &&
   2500         TM.getCodeModel() != CodeModel::Kernel)
   2501       return NULL;
   2502 
   2503     // x86-32 PIC requires a PIC base register for constant pools.
   2504     unsigned PICBase = 0;
   2505     if (TM.getRelocationModel() == Reloc::PIC_) {
   2506       if (TM.getSubtarget<X86Subtarget>().is64Bit())
   2507         PICBase = X86::RIP;
   2508       else
   2509         // FIXME: PICBase = getGlobalBaseReg(&MF);
   2510         // This doesn't work for several reasons.
   2511         // 1. GlobalBaseReg may have been spilled.
   2512         // 2. It may not be live at MI.
   2513         return NULL;
   2514     }
   2515 
   2516     // Create a constant-pool entry.
   2517     MachineConstantPool &MCP = *MF.getConstantPool();
   2518     Type *Ty;
   2519     unsigned Opc = LoadMI->getOpcode();
   2520     if (Opc == X86::FsFLD0SS || Opc == X86::VFsFLD0SS)
   2521       Ty = Type::getFloatTy(MF.getFunction()->getContext());
   2522     else if (Opc == X86::FsFLD0SD || Opc == X86::VFsFLD0SD)
   2523       Ty = Type::getDoubleTy(MF.getFunction()->getContext());
   2524     else if (Opc == X86::AVX_SET0PSY || Opc == X86::AVX_SET0PDY)
   2525       Ty = VectorType::get(Type::getFloatTy(MF.getFunction()->getContext()), 8);
   2526     else
   2527       Ty = VectorType::get(Type::getInt32Ty(MF.getFunction()->getContext()), 4);
   2528     const Constant *C = LoadMI->getOpcode() == X86::V_SETALLONES ?
   2529                     Constant::getAllOnesValue(Ty) :
   2530                     Constant::getNullValue(Ty);
   2531     unsigned CPI = MCP.getConstantPoolIndex(C, Alignment);
   2532 
   2533     // Create operands to load from the constant pool entry.
   2534     MOs.push_back(MachineOperand::CreateReg(PICBase, false));
   2535     MOs.push_back(MachineOperand::CreateImm(1));
   2536     MOs.push_back(MachineOperand::CreateReg(0, false));
   2537     MOs.push_back(MachineOperand::CreateCPI(CPI, 0));
   2538     MOs.push_back(MachineOperand::CreateReg(0, false));
   2539     break;
   2540   }
   2541   default: {
   2542     // Folding a normal load. Just copy the load's address operands.
   2543     unsigned NumOps = LoadMI->getDesc().getNumOperands();
   2544     for (unsigned i = NumOps - X86::AddrNumOperands; i != NumOps; ++i)
   2545       MOs.push_back(LoadMI->getOperand(i));
   2546     break;
   2547   }
   2548   }
   2549   return foldMemoryOperandImpl(MF, MI, Ops[0], MOs, 0, Alignment);
   2550 }
   2551 
   2552 
   2553 bool X86InstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
   2554                                   const SmallVectorImpl<unsigned> &Ops) const {
   2555   // Check switch flag
   2556   if (NoFusing) return 0;
   2557 
   2558   if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
   2559     switch (MI->getOpcode()) {
   2560     default: return false;
   2561     case X86::TEST8rr:
   2562     case X86::TEST16rr:
   2563     case X86::TEST32rr:
   2564     case X86::TEST64rr:
   2565       return true;
   2566     case X86::ADD32ri:
   2567       // FIXME: AsmPrinter doesn't know how to handle
   2568       // X86II::MO_GOT_ABSOLUTE_ADDRESS after folding.
   2569       if (MI->getOperand(2).getTargetFlags() == X86II::MO_GOT_ABSOLUTE_ADDRESS)
   2570         return false;
   2571       break;
   2572     }
   2573   }
   2574 
   2575   if (Ops.size() != 1)
   2576     return false;
   2577 
   2578   unsigned OpNum = Ops[0];
   2579   unsigned Opc = MI->getOpcode();
   2580   unsigned NumOps = MI->getDesc().getNumOperands();
   2581   bool isTwoAddr = NumOps > 1 &&
   2582     MI->getDesc().getOperandConstraint(1, MCOI::TIED_TO) != -1;
   2583 
   2584   // Folding a memory location into the two-address part of a two-address
   2585   // instruction is different than folding it other places.  It requires
   2586   // replacing the *two* registers with the memory location.
   2587   const DenseMap<unsigned, std::pair<unsigned,unsigned> > *OpcodeTablePtr = 0;
   2588   if (isTwoAddr && NumOps >= 2 && OpNum < 2) {
   2589     OpcodeTablePtr = &RegOp2MemOpTable2Addr;
   2590   } else if (OpNum == 0) { // If operand 0
   2591     switch (Opc) {
   2592     case X86::MOV8r0:
   2593     case X86::MOV16r0:
   2594     case X86::MOV32r0:
   2595     case X86::MOV64r0: return true;
   2596     default: break;
   2597     }
   2598     OpcodeTablePtr = &RegOp2MemOpTable0;
   2599   } else if (OpNum == 1) {
   2600     OpcodeTablePtr = &RegOp2MemOpTable1;
   2601   } else if (OpNum == 2) {
   2602     OpcodeTablePtr = &RegOp2MemOpTable2;
   2603   }
   2604 
   2605   if (OpcodeTablePtr && OpcodeTablePtr->count(Opc))
   2606     return true;
   2607   return TargetInstrInfoImpl::canFoldMemoryOperand(MI, Ops);
   2608 }
   2609 
   2610 bool X86InstrInfo::unfoldMemoryOperand(MachineFunction &MF, MachineInstr *MI,
   2611                                 unsigned Reg, bool UnfoldLoad, bool UnfoldStore,
   2612                                 SmallVectorImpl<MachineInstr*> &NewMIs) const {
   2613   DenseMap<unsigned, std::pair<unsigned,unsigned> >::const_iterator I =
   2614     MemOp2RegOpTable.find(MI->getOpcode());
   2615   if (I == MemOp2RegOpTable.end())
   2616     return false;
   2617   unsigned Opc = I->second.first;
   2618   unsigned Index = I->second.second & 0xf;
   2619   bool FoldedLoad = I->second.second & (1 << 4);
   2620   bool FoldedStore = I->second.second & (1 << 5);
   2621   if (UnfoldLoad && !FoldedLoad)
   2622     return false;
   2623   UnfoldLoad &= FoldedLoad;
   2624   if (UnfoldStore && !FoldedStore)
   2625     return false;
   2626   UnfoldStore &= FoldedStore;
   2627 
   2628   const MCInstrDesc &MCID = get(Opc);
   2629   const TargetRegisterClass *RC = getRegClass(MCID, Index, &RI);
   2630   if (!MI->hasOneMemOperand() &&
   2631       RC == &X86::VR128RegClass &&
   2632       !TM.getSubtarget<X86Subtarget>().isUnalignedMemAccessFast())
   2633     // Without memoperands, loadRegFromAddr and storeRegToStackSlot will
   2634     // conservatively assume the address is unaligned. That's bad for
   2635     // performance.
   2636     return false;
   2637   SmallVector<MachineOperand, X86::AddrNumOperands> AddrOps;
   2638   SmallVector<MachineOperand,2> BeforeOps;
   2639   SmallVector<MachineOperand,2> AfterOps;
   2640   SmallVector<MachineOperand,4> ImpOps;
   2641   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
   2642     MachineOperand &Op = MI->getOperand(i);
   2643     if (i >= Index && i < Index + X86::AddrNumOperands)
   2644       AddrOps.push_back(Op);
   2645     else if (Op.isReg() && Op.isImplicit())
   2646       ImpOps.push_back(Op);
   2647     else if (i < Index)
   2648       BeforeOps.push_back(Op);
   2649     else if (i > Index)
   2650       AfterOps.push_back(Op);
   2651   }
   2652 
   2653   // Emit the load instruction.
   2654   if (UnfoldLoad) {
   2655     std::pair<MachineInstr::mmo_iterator,
   2656               MachineInstr::mmo_iterator> MMOs =
   2657       MF.extractLoadMemRefs(MI->memoperands_begin(),
   2658                             MI->memoperands_end());
   2659     loadRegFromAddr(MF, Reg, AddrOps, RC, MMOs.first, MMOs.second, NewMIs);
   2660     if (UnfoldStore) {
   2661       // Address operands cannot be marked isKill.
   2662       for (unsigned i = 1; i != 1 + X86::AddrNumOperands; ++i) {
   2663         MachineOperand &MO = NewMIs[0]->getOperand(i);
   2664         if (MO.isReg())
   2665           MO.setIsKill(false);
   2666       }
   2667     }
   2668   }
   2669 
   2670   // Emit the data processing instruction.
   2671   MachineInstr *DataMI = MF.CreateMachineInstr(MCID, MI->getDebugLoc(), true);
   2672   MachineInstrBuilder MIB(DataMI);
   2673 
   2674   if (FoldedStore)
   2675     MIB.addReg(Reg, RegState::Define);
   2676   for (unsigned i = 0, e = BeforeOps.size(); i != e; ++i)
   2677     MIB.addOperand(BeforeOps[i]);
   2678   if (FoldedLoad)
   2679     MIB.addReg(Reg);
   2680   for (unsigned i = 0, e = AfterOps.size(); i != e; ++i)
   2681     MIB.addOperand(AfterOps[i]);
   2682   for (unsigned i = 0, e = ImpOps.size(); i != e; ++i) {
   2683     MachineOperand &MO = ImpOps[i];
   2684     MIB.addReg(MO.getReg(),
   2685                getDefRegState(MO.isDef()) |
   2686                RegState::Implicit |
   2687                getKillRegState(MO.isKill()) |
   2688                getDeadRegState(MO.isDead()) |
   2689                getUndefRegState(MO.isUndef()));
   2690   }
   2691   // Change CMP32ri r, 0 back to TEST32rr r, r, etc.
   2692   unsigned NewOpc = 0;
   2693   switch (DataMI->getOpcode()) {
   2694   default: break;
   2695   case X86::CMP64ri32:
   2696   case X86::CMP64ri8:
   2697   case X86::CMP32ri:
   2698   case X86::CMP32ri8:
   2699   case X86::CMP16ri:
   2700   case X86::CMP16ri8:
   2701   case X86::CMP8ri: {
   2702     MachineOperand &MO0 = DataMI->getOperand(0);
   2703     MachineOperand &MO1 = DataMI->getOperand(1);
   2704     if (MO1.getImm() == 0) {
   2705       switch (DataMI->getOpcode()) {
   2706       default: break;
   2707       case X86::CMP64ri8:
   2708       case X86::CMP64ri32: NewOpc = X86::TEST64rr; break;
   2709       case X86::CMP32ri8:
   2710       case X86::CMP32ri:   NewOpc = X86::TEST32rr; break;
   2711       case X86::CMP16ri8:
   2712       case X86::CMP16ri:   NewOpc = X86::TEST16rr; break;
   2713       case X86::CMP8ri:    NewOpc = X86::TEST8rr; break;
   2714       }
   2715       DataMI->setDesc(get(NewOpc));
   2716       MO1.ChangeToRegister(MO0.getReg(), false);
   2717     }
   2718   }
   2719   }
   2720   NewMIs.push_back(DataMI);
   2721 
   2722   // Emit the store instruction.
   2723   if (UnfoldStore) {
   2724     const TargetRegisterClass *DstRC = getRegClass(MCID, 0, &RI);
   2725     std::pair<MachineInstr::mmo_iterator,
   2726               MachineInstr::mmo_iterator> MMOs =
   2727       MF.extractStoreMemRefs(MI->memoperands_begin(),
   2728                              MI->memoperands_end());
   2729     storeRegToAddr(MF, Reg, true, AddrOps, DstRC, MMOs.first, MMOs.second, NewMIs);
   2730   }
   2731 
   2732   return true;
   2733 }
   2734 
   2735 bool
   2736 X86InstrInfo::unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
   2737                                   SmallVectorImpl<SDNode*> &NewNodes) const {
   2738   if (!N->isMachineOpcode())
   2739     return false;
   2740 
   2741   DenseMap<unsigned, std::pair<unsigned,unsigned> >::const_iterator I =
   2742     MemOp2RegOpTable.find(N->getMachineOpcode());
   2743   if (I == MemOp2RegOpTable.end())
   2744     return false;
   2745   unsigned Opc = I->second.first;
   2746   unsigned Index = I->second.second & 0xf;
   2747   bool FoldedLoad = I->second.second & (1 << 4);
   2748   bool FoldedStore = I->second.second & (1 << 5);
   2749   const MCInstrDesc &MCID = get(Opc);
   2750   const TargetRegisterClass *RC = getRegClass(MCID, Index, &RI);
   2751   unsigned NumDefs = MCID.NumDefs;
   2752   std::vector<SDValue> AddrOps;
   2753   std::vector<SDValue> BeforeOps;
   2754   std::vector<SDValue> AfterOps;
   2755   DebugLoc dl = N->getDebugLoc();
   2756   unsigned NumOps = N->getNumOperands();
   2757   for (unsigned i = 0; i != NumOps-1; ++i) {
   2758     SDValue Op = N->getOperand(i);
   2759     if (i >= Index-NumDefs && i < Index-NumDefs + X86::AddrNumOperands)
   2760       AddrOps.push_back(Op);
   2761     else if (i < Index-NumDefs)
   2762       BeforeOps.push_back(Op);
   2763     else if (i > Index-NumDefs)
   2764       AfterOps.push_back(Op);
   2765   }
   2766   SDValue Chain = N->getOperand(NumOps-1);
   2767   AddrOps.push_back(Chain);
   2768 
   2769   // Emit the load instruction.
   2770   SDNode *Load = 0;
   2771   MachineFunction &MF = DAG.getMachineFunction();
   2772   if (FoldedLoad) {
   2773     EVT VT = *RC->vt_begin();
   2774     std::pair<MachineInstr::mmo_iterator,
   2775               MachineInstr::mmo_iterator> MMOs =
   2776       MF.extractLoadMemRefs(cast<MachineSDNode>(N)->memoperands_begin(),
   2777                             cast<MachineSDNode>(N)->memoperands_end());
   2778     if (!(*MMOs.first) &&
   2779         RC == &X86::VR128RegClass &&
   2780         !TM.getSubtarget<X86Subtarget>().isUnalignedMemAccessFast())
   2781       // Do not introduce a slow unaligned load.
   2782       return false;
   2783     bool isAligned = (*MMOs.first) && (*MMOs.first)->getAlignment() >= 16;
   2784     Load = DAG.getMachineNode(getLoadRegOpcode(0, RC, isAligned, TM), dl,
   2785                               VT, MVT::Other, &AddrOps[0], AddrOps.size());
   2786     NewNodes.push_back(Load);
   2787 
   2788     // Preserve memory reference information.
   2789     cast<MachineSDNode>(Load)->setMemRefs(MMOs.first, MMOs.second);
   2790   }
   2791 
   2792   // Emit the data processing instruction.
   2793   std::vector<EVT> VTs;
   2794   const TargetRegisterClass *DstRC = 0;
   2795   if (MCID.getNumDefs() > 0) {
   2796     DstRC = getRegClass(MCID, 0, &RI);
   2797     VTs.push_back(*DstRC->vt_begin());
   2798   }
   2799   for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
   2800     EVT VT = N->getValueType(i);
   2801     if (VT != MVT::Other && i >= (unsigned)MCID.getNumDefs())
   2802       VTs.push_back(VT);
   2803   }
   2804   if (Load)
   2805     BeforeOps.push_back(SDValue(Load, 0));
   2806   std::copy(AfterOps.begin(), AfterOps.end(), std::back_inserter(BeforeOps));
   2807   SDNode *NewNode= DAG.getMachineNode(Opc, dl, VTs, &BeforeOps[0],
   2808                                       BeforeOps.size());
   2809   NewNodes.push_back(NewNode);
   2810 
   2811   // Emit the store instruction.
   2812   if (FoldedStore) {
   2813     AddrOps.pop_back();
   2814     AddrOps.push_back(SDValue(NewNode, 0));
   2815     AddrOps.push_back(Chain);
   2816     std::pair<MachineInstr::mmo_iterator,
   2817               MachineInstr::mmo_iterator> MMOs =
   2818       MF.extractStoreMemRefs(cast<MachineSDNode>(N)->memoperands_begin(),
   2819                              cast<MachineSDNode>(N)->memoperands_end());
   2820     if (!(*MMOs.first) &&
   2821         RC == &X86::VR128RegClass &&
   2822         !TM.getSubtarget<X86Subtarget>().isUnalignedMemAccessFast())
   2823       // Do not introduce a slow unaligned store.
   2824       return false;
   2825     bool isAligned = (*MMOs.first) && (*MMOs.first)->getAlignment() >= 16;
   2826     SDNode *Store = DAG.getMachineNode(getStoreRegOpcode(0, DstRC,
   2827                                                          isAligned, TM),
   2828                                        dl, MVT::Other,
   2829                                        &AddrOps[0], AddrOps.size());
   2830     NewNodes.push_back(Store);
   2831 
   2832     // Preserve memory reference information.
   2833     cast<MachineSDNode>(Load)->setMemRefs(MMOs.first, MMOs.second);
   2834   }
   2835 
   2836   return true;
   2837 }
   2838 
   2839 unsigned X86InstrInfo::getOpcodeAfterMemoryUnfold(unsigned Opc,
   2840                                       bool UnfoldLoad, bool UnfoldStore,
   2841                                       unsigned *LoadRegIndex) const {
   2842   DenseMap<unsigned, std::pair<unsigned,unsigned> >::const_iterator I =
   2843     MemOp2RegOpTable.find(Opc);
   2844   if (I == MemOp2RegOpTable.end())
   2845     return 0;
   2846   bool FoldedLoad = I->second.second & (1 << 4);
   2847   bool FoldedStore = I->second.second & (1 << 5);
   2848   if (UnfoldLoad && !FoldedLoad)
   2849     return 0;
   2850   if (UnfoldStore && !FoldedStore)
   2851     return 0;
   2852   if (LoadRegIndex)
   2853     *LoadRegIndex = I->second.second & 0xf;
   2854   return I->second.first;
   2855 }
   2856 
   2857 bool
   2858 X86InstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
   2859                                      int64_t &Offset1, int64_t &Offset2) const {
   2860   if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
   2861     return false;
   2862   unsigned Opc1 = Load1->getMachineOpcode();
   2863   unsigned Opc2 = Load2->getMachineOpcode();
   2864   switch (Opc1) {
   2865   default: return false;
   2866   case X86::MOV8rm:
   2867   case X86::MOV16rm:
   2868   case X86::MOV32rm:
   2869   case X86::MOV64rm:
   2870   case X86::LD_Fp32m:
   2871   case X86::LD_Fp64m:
   2872   case X86::LD_Fp80m:
   2873   case X86::MOVSSrm:
   2874   case X86::MOVSDrm:
   2875   case X86::MMX_MOVD64rm:
   2876   case X86::MMX_MOVQ64rm:
   2877   case X86::FsMOVAPSrm:
   2878   case X86::FsMOVAPDrm:
   2879   case X86::MOVAPSrm:
   2880   case X86::MOVUPSrm:
   2881   case X86::MOVAPDrm:
   2882   case X86::MOVDQArm:
   2883   case X86::MOVDQUrm:
   2884   case X86::VMOVAPSYrm:
   2885   case X86::VMOVUPSYrm:
   2886   case X86::VMOVAPDYrm:
   2887   case X86::VMOVDQAYrm:
   2888   case X86::VMOVDQUYrm:
   2889     break;
   2890   }
   2891   switch (Opc2) {
   2892   default: return false;
   2893   case X86::MOV8rm:
   2894   case X86::MOV16rm:
   2895   case X86::MOV32rm:
   2896   case X86::MOV64rm:
   2897   case X86::LD_Fp32m:
   2898   case X86::LD_Fp64m:
   2899   case X86::LD_Fp80m:
   2900   case X86::MOVSSrm:
   2901   case X86::MOVSDrm:
   2902   case X86::MMX_MOVD64rm:
   2903   case X86::MMX_MOVQ64rm:
   2904   case X86::FsMOVAPSrm:
   2905   case X86::FsMOVAPDrm:
   2906   case X86::MOVAPSrm:
   2907   case X86::MOVUPSrm:
   2908   case X86::MOVAPDrm:
   2909   case X86::MOVDQArm:
   2910   case X86::MOVDQUrm:
   2911   case X86::VMOVAPSYrm:
   2912   case X86::VMOVUPSYrm:
   2913   case X86::VMOVAPDYrm:
   2914   case X86::VMOVDQAYrm:
   2915   case X86::VMOVDQUYrm:
   2916     break;
   2917   }
   2918 
   2919   // Check if chain operands and base addresses match.
   2920   if (Load1->getOperand(0) != Load2->getOperand(0) ||
   2921       Load1->getOperand(5) != Load2->getOperand(5))
   2922     return false;
   2923   // Segment operands should match as well.
   2924   if (Load1->getOperand(4) != Load2->getOperand(4))
   2925     return false;
   2926   // Scale should be 1, Index should be Reg0.
   2927   if (Load1->getOperand(1) == Load2->getOperand(1) &&
   2928       Load1->getOperand(2) == Load2->getOperand(2)) {
   2929     if (cast<ConstantSDNode>(Load1->getOperand(1))->getZExtValue() != 1)
   2930       return false;
   2931 
   2932     // Now let's examine the displacements.
   2933     if (isa<ConstantSDNode>(Load1->getOperand(3)) &&
   2934         isa<ConstantSDNode>(Load2->getOperand(3))) {
   2935       Offset1 = cast<ConstantSDNode>(Load1->getOperand(3))->getSExtValue();
   2936       Offset2 = cast<ConstantSDNode>(Load2->getOperand(3))->getSExtValue();
   2937       return true;
   2938     }
   2939   }
   2940   return false;
   2941 }
   2942 
   2943 bool X86InstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
   2944                                            int64_t Offset1, int64_t Offset2,
   2945                                            unsigned NumLoads) const {
   2946   assert(Offset2 > Offset1);
   2947   if ((Offset2 - Offset1) / 8 > 64)
   2948     return false;
   2949 
   2950   unsigned Opc1 = Load1->getMachineOpcode();
   2951   unsigned Opc2 = Load2->getMachineOpcode();
   2952   if (Opc1 != Opc2)
   2953     return false;  // FIXME: overly conservative?
   2954 
   2955   switch (Opc1) {
   2956   default: break;
   2957   case X86::LD_Fp32m:
   2958   case X86::LD_Fp64m:
   2959   case X86::LD_Fp80m:
   2960   case X86::MMX_MOVD64rm:
   2961   case X86::MMX_MOVQ64rm:
   2962     return false;
   2963   }
   2964 
   2965   EVT VT = Load1->getValueType(0);
   2966   switch (VT.getSimpleVT().SimpleTy) {
   2967   default:
   2968     // XMM registers. In 64-bit mode we can be a bit more aggressive since we
   2969     // have 16 of them to play with.
   2970     if (TM.getSubtargetImpl()->is64Bit()) {
   2971       if (NumLoads >= 3)
   2972         return false;
   2973     } else if (NumLoads) {
   2974       return false;
   2975     }
   2976     break;
   2977   case MVT::i8:
   2978   case MVT::i16:
   2979   case MVT::i32:
   2980   case MVT::i64:
   2981   case MVT::f32:
   2982   case MVT::f64:
   2983     if (NumLoads)
   2984       return false;
   2985     break;
   2986   }
   2987 
   2988   return true;
   2989 }
   2990 
   2991 
   2992 bool X86InstrInfo::
   2993 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
   2994   assert(Cond.size() == 1 && "Invalid X86 branch condition!");
   2995   X86::CondCode CC = static_cast<X86::CondCode>(Cond[0].getImm());
   2996   if (CC == X86::COND_NE_OR_P || CC == X86::COND_NP_OR_E)
   2997     return true;
   2998   Cond[0].setImm(GetOppositeBranchCondition(CC));
   2999   return false;
   3000 }
   3001 
   3002 bool X86InstrInfo::
   3003 isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const {
   3004   // FIXME: Return false for x87 stack register classes for now. We can't
   3005   // allow any loads of these registers before FpGet_ST0_80.
   3006   return !(RC == &X86::CCRRegClass || RC == &X86::RFP32RegClass ||
   3007            RC == &X86::RFP64RegClass || RC == &X86::RFP80RegClass);
   3008 }
   3009 
   3010 
   3011 /// isX86_64ExtendedReg - Is the MachineOperand a x86-64 extended (r8 or higher)
   3012 /// register?  e.g. r8, xmm8, xmm13, etc.
   3013 bool X86InstrInfo::isX86_64ExtendedReg(unsigned RegNo) {
   3014   switch (RegNo) {
   3015   default: break;
   3016   case X86::R8:    case X86::R9:    case X86::R10:   case X86::R11:
   3017   case X86::R12:   case X86::R13:   case X86::R14:   case X86::R15:
   3018   case X86::R8D:   case X86::R9D:   case X86::R10D:  case X86::R11D:
   3019   case X86::R12D:  case X86::R13D:  case X86::R14D:  case X86::R15D:
   3020   case X86::R8W:   case X86::R9W:   case X86::R10W:  case X86::R11W:
   3021   case X86::R12W:  case X86::R13W:  case X86::R14W:  case X86::R15W:
   3022   case X86::R8B:   case X86::R9B:   case X86::R10B:  case X86::R11B:
   3023   case X86::R12B:  case X86::R13B:  case X86::R14B:  case X86::R15B:
   3024   case X86::XMM8:  case X86::XMM9:  case X86::XMM10: case X86::XMM11:
   3025   case X86::XMM12: case X86::XMM13: case X86::XMM14: case X86::XMM15:
   3026   case X86::YMM8:  case X86::YMM9:  case X86::YMM10: case X86::YMM11:
   3027   case X86::YMM12: case X86::YMM13: case X86::YMM14: case X86::YMM15:
   3028   case X86::CR8:   case X86::CR9:   case X86::CR10:  case X86::CR11:
   3029   case X86::CR12:  case X86::CR13:  case X86::CR14:  case X86::CR15:
   3030     return true;
   3031   }
   3032   return false;
   3033 }
   3034 
   3035 /// getGlobalBaseReg - Return a virtual register initialized with the
   3036 /// the global base register value. Output instructions required to
   3037 /// initialize the register in the function entry block, if necessary.
   3038 ///
   3039 /// TODO: Eliminate this and move the code to X86MachineFunctionInfo.
   3040 ///
   3041 unsigned X86InstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
   3042   assert(!TM.getSubtarget<X86Subtarget>().is64Bit() &&
   3043          "X86-64 PIC uses RIP relative addressing");
   3044 
   3045   X86MachineFunctionInfo *X86FI = MF->getInfo<X86MachineFunctionInfo>();
   3046   unsigned GlobalBaseReg = X86FI->getGlobalBaseReg();
   3047   if (GlobalBaseReg != 0)
   3048     return GlobalBaseReg;
   3049 
   3050   // Create the register. The code to initialize it is inserted
   3051   // later, by the CGBR pass (below).
   3052   MachineRegisterInfo &RegInfo = MF->getRegInfo();
   3053   GlobalBaseReg = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
   3054   X86FI->setGlobalBaseReg(GlobalBaseReg);
   3055   return GlobalBaseReg;
   3056 }
   3057 
   3058 // These are the replaceable SSE instructions. Some of these have Int variants
   3059 // that we don't include here. We don't want to replace instructions selected
   3060 // by intrinsics.
   3061 static const unsigned ReplaceableInstrs[][3] = {
   3062   //PackedSingle     PackedDouble    PackedInt
   3063   { X86::MOVAPSmr,   X86::MOVAPDmr,  X86::MOVDQAmr  },
   3064   { X86::MOVAPSrm,   X86::MOVAPDrm,  X86::MOVDQArm  },
   3065   { X86::MOVAPSrr,   X86::MOVAPDrr,  X86::MOVDQArr  },
   3066   { X86::MOVUPSmr,   X86::MOVUPDmr,  X86::MOVDQUmr  },
   3067   { X86::MOVUPSrm,   X86::MOVUPDrm,  X86::MOVDQUrm  },
   3068   { X86::MOVNTPSmr,  X86::MOVNTPDmr, X86::MOVNTDQmr },
   3069   { X86::ANDNPSrm,   X86::ANDNPDrm,  X86::PANDNrm   },
   3070   { X86::ANDNPSrr,   X86::ANDNPDrr,  X86::PANDNrr   },
   3071   { X86::ANDPSrm,    X86::ANDPDrm,   X86::PANDrm    },
   3072   { X86::ANDPSrr,    X86::ANDPDrr,   X86::PANDrr    },
   3073   { X86::ORPSrm,     X86::ORPDrm,    X86::PORrm     },
   3074   { X86::ORPSrr,     X86::ORPDrr,    X86::PORrr     },
   3075   { X86::V_SET0PS,   X86::V_SET0PD,  X86::V_SET0PI  },
   3076   { X86::XORPSrm,    X86::XORPDrm,   X86::PXORrm    },
   3077   { X86::XORPSrr,    X86::XORPDrr,   X86::PXORrr    },
   3078   // AVX 128-bit support
   3079   { X86::VMOVAPSmr,  X86::VMOVAPDmr,  X86::VMOVDQAmr  },
   3080   { X86::VMOVAPSrm,  X86::VMOVAPDrm,  X86::VMOVDQArm  },
   3081   { X86::VMOVAPSrr,  X86::VMOVAPDrr,  X86::VMOVDQArr  },
   3082   { X86::VMOVUPSmr,  X86::VMOVUPDmr,  X86::VMOVDQUmr  },
   3083   { X86::VMOVUPSrm,  X86::VMOVUPDrm,  X86::VMOVDQUrm  },
   3084   { X86::VMOVNTPSmr, X86::VMOVNTPDmr, X86::VMOVNTDQmr },
   3085   { X86::VANDNPSrm,  X86::VANDNPDrm,  X86::VPANDNrm   },
   3086   { X86::VANDNPSrr,  X86::VANDNPDrr,  X86::VPANDNrr   },
   3087   { X86::VANDPSrm,   X86::VANDPDrm,   X86::VPANDrm    },
   3088   { X86::VANDPSrr,   X86::VANDPDrr,   X86::VPANDrr    },
   3089   { X86::VORPSrm,    X86::VORPDrm,    X86::VPORrm     },
   3090   { X86::VORPSrr,    X86::VORPDrr,    X86::VPORrr     },
   3091   { X86::AVX_SET0PS, X86::AVX_SET0PD, X86::AVX_SET0PI },
   3092   { X86::VXORPSrm,   X86::VXORPDrm,   X86::VPXORrm    },
   3093   { X86::VXORPSrr,   X86::VXORPDrr,   X86::VPXORrr    },
   3094   // AVX 256-bit support
   3095   { X86::VMOVAPSYmr,   X86::VMOVAPDYmr,   X86::VMOVDQAYmr  },
   3096   { X86::VMOVAPSYrm,   X86::VMOVAPDYrm,   X86::VMOVDQAYrm  },
   3097   { X86::VMOVAPSYrr,   X86::VMOVAPDYrr,   X86::VMOVDQAYrr  },
   3098   { X86::VMOVUPSYmr,   X86::VMOVUPDYmr,   X86::VMOVDQUYmr  },
   3099   { X86::VMOVUPSYrm,   X86::VMOVUPDYrm,   X86::VMOVDQUYrm  },
   3100   { X86::VMOVNTPSYmr,  X86::VMOVNTPDYmr,  X86::VMOVNTDQYmr },
   3101 };
   3102 
   3103 // FIXME: Some shuffle and unpack instructions have equivalents in different
   3104 // domains, but they require a bit more work than just switching opcodes.
   3105 
   3106 static const unsigned *lookup(unsigned opcode, unsigned domain) {
   3107   for (unsigned i = 0, e = array_lengthof(ReplaceableInstrs); i != e; ++i)
   3108     if (ReplaceableInstrs[i][domain-1] == opcode)
   3109       return ReplaceableInstrs[i];
   3110   return 0;
   3111 }
   3112 
   3113 std::pair<uint16_t, uint16_t>
   3114 X86InstrInfo::GetSSEDomain(const MachineInstr *MI) const {
   3115   uint16_t domain = (MI->getDesc().TSFlags >> X86II::SSEDomainShift) & 3;
   3116   return std::make_pair(domain,
   3117                         domain && lookup(MI->getOpcode(), domain) ? 0xe : 0);
   3118 }
   3119 
   3120 void X86InstrInfo::SetSSEDomain(MachineInstr *MI, unsigned Domain) const {
   3121   assert(Domain>0 && Domain<4 && "Invalid execution domain");
   3122   uint16_t dom = (MI->getDesc().TSFlags >> X86II::SSEDomainShift) & 3;
   3123   assert(dom && "Not an SSE instruction");
   3124   const unsigned *table = lookup(MI->getOpcode(), dom);
   3125   assert(table && "Cannot change domain");
   3126   MI->setDesc(get(table[Domain-1]));
   3127 }
   3128 
   3129 /// getNoopForMachoTarget - Return the noop instruction to use for a noop.
   3130 void X86InstrInfo::getNoopForMachoTarget(MCInst &NopInst) const {
   3131   NopInst.setOpcode(X86::NOOP);
   3132 }
   3133 
   3134 bool X86InstrInfo::isHighLatencyDef(int opc) const {
   3135   switch (opc) {
   3136   default: return false;
   3137   case X86::DIVSDrm:
   3138   case X86::DIVSDrm_Int:
   3139   case X86::DIVSDrr:
   3140   case X86::DIVSDrr_Int:
   3141   case X86::DIVSSrm:
   3142   case X86::DIVSSrm_Int:
   3143   case X86::DIVSSrr:
   3144   case X86::DIVSSrr_Int:
   3145   case X86::SQRTPDm:
   3146   case X86::SQRTPDm_Int:
   3147   case X86::SQRTPDr:
   3148   case X86::SQRTPDr_Int:
   3149   case X86::SQRTPSm:
   3150   case X86::SQRTPSm_Int:
   3151   case X86::SQRTPSr:
   3152   case X86::SQRTPSr_Int:
   3153   case X86::SQRTSDm:
   3154   case X86::SQRTSDm_Int:
   3155   case X86::SQRTSDr:
   3156   case X86::SQRTSDr_Int:
   3157   case X86::SQRTSSm:
   3158   case X86::SQRTSSm_Int:
   3159   case X86::SQRTSSr:
   3160   case X86::SQRTSSr_Int:
   3161     return true;
   3162   }
   3163 }
   3164 
   3165 bool X86InstrInfo::
   3166 hasHighOperandLatency(const InstrItineraryData *ItinData,
   3167                       const MachineRegisterInfo *MRI,
   3168                       const MachineInstr *DefMI, unsigned DefIdx,
   3169                       const MachineInstr *UseMI, unsigned UseIdx) const {
   3170   return isHighLatencyDef(DefMI->getOpcode());
   3171 }
   3172 
   3173 namespace {
   3174   /// CGBR - Create Global Base Reg pass. This initializes the PIC
   3175   /// global base register for x86-32.
   3176   struct CGBR : public MachineFunctionPass {
   3177     static char ID;
   3178     CGBR() : MachineFunctionPass(ID) {}
   3179 
   3180     virtual bool runOnMachineFunction(MachineFunction &MF) {
   3181       const X86TargetMachine *TM =
   3182         static_cast<const X86TargetMachine *>(&MF.getTarget());
   3183 
   3184       assert(!TM->getSubtarget<X86Subtarget>().is64Bit() &&
   3185              "X86-64 PIC uses RIP relative addressing");
   3186 
   3187       // Only emit a global base reg in PIC mode.
   3188       if (TM->getRelocationModel() != Reloc::PIC_)
   3189         return false;
   3190 
   3191       X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
   3192       unsigned GlobalBaseReg = X86FI->getGlobalBaseReg();
   3193 
   3194       // If we didn't need a GlobalBaseReg, don't insert code.
   3195       if (GlobalBaseReg == 0)
   3196         return false;
   3197 
   3198       // Insert the set of GlobalBaseReg into the first MBB of the function
   3199       MachineBasicBlock &FirstMBB = MF.front();
   3200       MachineBasicBlock::iterator MBBI = FirstMBB.begin();
   3201       DebugLoc DL = FirstMBB.findDebugLoc(MBBI);
   3202       MachineRegisterInfo &RegInfo = MF.getRegInfo();
   3203       const X86InstrInfo *TII = TM->getInstrInfo();
   3204 
   3205       unsigned PC;
   3206       if (TM->getSubtarget<X86Subtarget>().isPICStyleGOT())
   3207         PC = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
   3208       else
   3209         PC = GlobalBaseReg;
   3210 
   3211       // Operand of MovePCtoStack is completely ignored by asm printer. It's
   3212       // only used in JIT code emission as displacement to pc.
   3213       BuildMI(FirstMBB, MBBI, DL, TII->get(X86::MOVPC32r), PC).addImm(0);
   3214 
   3215       // If we're using vanilla 'GOT' PIC style, we should use relative addressing
   3216       // not to pc, but to _GLOBAL_OFFSET_TABLE_ external.
   3217       if (TM->getSubtarget<X86Subtarget>().isPICStyleGOT()) {
   3218         // Generate addl $__GLOBAL_OFFSET_TABLE_ + [.-piclabel], %some_register
   3219         BuildMI(FirstMBB, MBBI, DL, TII->get(X86::ADD32ri), GlobalBaseReg)
   3220           .addReg(PC).addExternalSymbol("_GLOBAL_OFFSET_TABLE_",
   3221                                         X86II::MO_GOT_ABSOLUTE_ADDRESS);
   3222       }
   3223 
   3224       return true;
   3225     }
   3226 
   3227     virtual const char *getPassName() const {
   3228       return "X86 PIC Global Base Reg Initialization";
   3229     }
   3230 
   3231     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
   3232       AU.setPreservesCFG();
   3233       MachineFunctionPass::getAnalysisUsage(AU);
   3234     }
   3235   };
   3236 }
   3237 
   3238 char CGBR::ID = 0;
   3239 FunctionPass*
   3240 llvm::createGlobalBaseRegPass() { return new CGBR(); }
   3241