LLVM API Documentation

X86InstrInfo.cpp

Go to the documentation of this file.
00001 //===- X86InstrInfo.cpp - X86 Instruction Information -----------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the X86 implementation of the TargetInstrInfo class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "X86InstrInfo.h"
00015 #include "X86.h"
00016 #include "X86GenInstrInfo.inc"
00017 #include "X86InstrBuilder.h"
00018 #include "X86MachineFunctionInfo.h"
00019 #include "X86Subtarget.h"
00020 #include "X86TargetMachine.h"
00021 #include "llvm/GlobalVariable.h"
00022 #include "llvm/DerivedTypes.h"
00023 #include "llvm/LLVMContext.h"
00024 #include "llvm/ADT/STLExtras.h"
00025 #include "llvm/CodeGen/MachineConstantPool.h"
00026 #include "llvm/CodeGen/MachineFrameInfo.h"
00027 #include "llvm/CodeGen/MachineInstrBuilder.h"
00028 #include "llvm/CodeGen/MachineRegisterInfo.h"
00029 #include "llvm/CodeGen/LiveVariables.h"
00030 #include "llvm/Support/CommandLine.h"
00031 #include "llvm/Support/ErrorHandling.h"
00032 #include "llvm/Support/raw_ostream.h"
00033 #include "llvm/Target/TargetOptions.h"
00034 #include "llvm/Target/TargetAsmInfo.h"
00035 using namespace llvm;
00036 
00037 namespace {
00038   cl::opt<bool>
00039   NoFusing("disable-spill-fusing",
00040            cl::desc("Disable fusing of spill code into instructions"));
00041   cl::opt<bool>
00042   PrintFailedFusing("print-failed-fuse-candidates",
00043                     cl::desc("Print instructions that the allocator wants to"
00044                              " fuse, but the X86 backend currently can't"),
00045                     cl::Hidden);
00046   cl::opt<bool>
00047   ReMatPICStubLoad("remat-pic-stub-load",
00048                    cl::desc("Re-materialize load from stub in PIC mode"),
00049                    cl::init(false), cl::Hidden);
00050 }
00051 
00052 X86InstrInfo::X86InstrInfo(X86TargetMachine &tm)
00053   : TargetInstrInfoImpl(X86Insts, array_lengthof(X86Insts)),
00054     TM(tm), RI(tm, *this) {
00055   SmallVector<unsigned,16> AmbEntries;
00056   static const unsigned OpTbl2Addr[][2] = {
00057     { X86::ADC32ri,     X86::ADC32mi },
00058     { X86::ADC32ri8,    X86::ADC32mi8 },
00059     { X86::ADC32rr,     X86::ADC32mr },
00060     { X86::ADC64ri32,   X86::ADC64mi32 },
00061     { X86::ADC64ri8,    X86::ADC64mi8 },
00062     { X86::ADC64rr,     X86::ADC64mr },
00063     { X86::ADD16ri,     X86::ADD16mi },
00064     { X86::ADD16ri8,    X86::ADD16mi8 },
00065     { X86::ADD16rr,     X86::ADD16mr },
00066     { X86::ADD32ri,     X86::ADD32mi },
00067     { X86::ADD32ri8,    X86::ADD32mi8 },
00068     { X86::ADD32rr,     X86::ADD32mr },
00069     { X86::ADD64ri32,   X86::ADD64mi32 },
00070     { X86::ADD64ri8,    X86::ADD64mi8 },
00071     { X86::ADD64rr,     X86::ADD64mr },
00072     { X86::ADD8ri,      X86::ADD8mi },
00073     { X86::ADD8rr,      X86::ADD8mr },
00074     { X86::AND16ri,     X86::AND16mi },
00075     { X86::AND16ri8,    X86::AND16mi8 },
00076     { X86::AND16rr,     X86::AND16mr },
00077     { X86::AND32ri,     X86::AND32mi },
00078     { X86::AND32ri8,    X86::AND32mi8 },
00079     { X86::AND32rr,     X86::AND32mr },
00080     { X86::AND64ri32,   X86::AND64mi32 },
00081     { X86::AND64ri8,    X86::AND64mi8 },
00082     { X86::AND64rr,     X86::AND64mr },
00083     { X86::AND8ri,      X86::AND8mi },
00084     { X86::AND8rr,      X86::AND8mr },
00085     { X86::DEC16r,      X86::DEC16m },
00086     { X86::DEC32r,      X86::DEC32m },
00087     { X86::DEC64_16r,   X86::DEC64_16m },
00088     { X86::DEC64_32r,   X86::DEC64_32m },
00089     { X86::DEC64r,      X86::DEC64m },
00090     { X86::DEC8r,       X86::DEC8m },
00091     { X86::INC16r,      X86::INC16m },
00092     { X86::INC32r,      X86::INC32m },
00093     { X86::INC64_16r,   X86::INC64_16m },
00094     { X86::INC64_32r,   X86::INC64_32m },
00095     { X86::INC64r,      X86::INC64m },
00096     { X86::INC8r,       X86::INC8m },
00097     { X86::NEG16r,      X86::NEG16m },
00098     { X86::NEG32r,      X86::NEG32m },
00099     { X86::NEG64r,      X86::NEG64m },
00100     { X86::NEG8r,       X86::NEG8m },
00101     { X86::NOT16r,      X86::NOT16m },
00102     { X86::NOT32r,      X86::NOT32m },
00103     { X86::NOT64r,      X86::NOT64m },
00104     { X86::NOT8r,       X86::NOT8m },
00105     { X86::OR16ri,      X86::OR16mi },
00106     { X86::OR16ri8,     X86::OR16mi8 },
00107     { X86::OR16rr,      X86::OR16mr },
00108     { X86::OR32ri,      X86::OR32mi },
00109     { X86::OR32ri8,     X86::OR32mi8 },
00110     { X86::OR32rr,      X86::OR32mr },
00111     { X86::OR64ri32,    X86::OR64mi32 },
00112     { X86::OR64ri8,     X86::OR64mi8 },
00113     { X86::OR64rr,      X86::OR64mr },
00114     { X86::OR8ri,       X86::OR8mi },
00115     { X86::OR8rr,       X86::OR8mr },
00116     { X86::ROL16r1,     X86::ROL16m1 },
00117     { X86::ROL16rCL,    X86::ROL16mCL },
00118     { X86::ROL16ri,     X86::ROL16mi },
00119     { X86::ROL32r1,     X86::ROL32m1 },
00120     { X86::ROL32rCL,    X86::ROL32mCL },
00121     { X86::ROL32ri,     X86::ROL32mi },
00122     { X86::ROL64r1,     X86::ROL64m1 },
00123     { X86::ROL64rCL,    X86::ROL64mCL },
00124     { X86::ROL64ri,     X86::ROL64mi },
00125     { X86::ROL8r1,      X86::ROL8m1 },
00126     { X86::ROL8rCL,     X86::ROL8mCL },
00127     { X86::ROL8ri,      X86::ROL8mi },
00128     { X86::ROR16r1,     X86::ROR16m1 },
00129     { X86::ROR16rCL,    X86::ROR16mCL },
00130     { X86::ROR16ri,     X86::ROR16mi },
00131     { X86::ROR32r1,     X86::ROR32m1 },
00132     { X86::ROR32rCL,    X86::ROR32mCL },
00133     { X86::ROR32ri,     X86::ROR32mi },
00134     { X86::ROR64r1,     X86::ROR64m1 },
00135     { X86::ROR64rCL,    X86::ROR64mCL },
00136     { X86::ROR64ri,     X86::ROR64mi },
00137     { X86::ROR8r1,      X86::ROR8m1 },
00138     { X86::ROR8rCL,     X86::ROR8mCL },
00139     { X86::ROR8ri,      X86::ROR8mi },
00140     { X86::SAR16r1,     X86::SAR16m1 },
00141     { X86::SAR16rCL,    X86::SAR16mCL },
00142     { X86::SAR16ri,     X86::SAR16mi },
00143     { X86::SAR32r1,     X86::SAR32m1 },
00144     { X86::SAR32rCL,    X86::SAR32mCL },
00145     { X86::SAR32ri,     X86::SAR32mi },
00146     { X86::SAR64r1,     X86::SAR64m1 },
00147     { X86::SAR64rCL,    X86::SAR64mCL },
00148     { X86::SAR64ri,     X86::SAR64mi },
00149     { X86::SAR8r1,      X86::SAR8m1 },
00150     { X86::SAR8rCL,     X86::SAR8mCL },
00151     { X86::SAR8ri,      X86::SAR8mi },
00152     { X86::SBB32ri,     X86::SBB32mi },
00153     { X86::SBB32ri8,    X86::SBB32mi8 },
00154     { X86::SBB32rr,     X86::SBB32mr },
00155     { X86::SBB64ri32,   X86::SBB64mi32 },
00156     { X86::SBB64ri8,    X86::SBB64mi8 },
00157     { X86::SBB64rr,     X86::SBB64mr },
00158     { X86::SHL16rCL,    X86::SHL16mCL },
00159     { X86::SHL16ri,     X86::SHL16mi },
00160     { X86::SHL32rCL,    X86::SHL32mCL },
00161     { X86::SHL32ri,     X86::SHL32mi },
00162     { X86::SHL64rCL,    X86::SHL64mCL },
00163     { X86::SHL64ri,     X86::SHL64mi },
00164     { X86::SHL8rCL,     X86::SHL8mCL },
00165     { X86::SHL8ri,      X86::SHL8mi },
00166     { X86::SHLD16rrCL,  X86::SHLD16mrCL },
00167     { X86::SHLD16rri8,  X86::SHLD16mri8 },
00168     { X86::SHLD32rrCL,  X86::SHLD32mrCL },
00169     { X86::SHLD32rri8,  X86::SHLD32mri8 },
00170     { X86::SHLD64rrCL,  X86::SHLD64mrCL },
00171     { X86::SHLD64rri8,  X86::SHLD64mri8 },
00172     { X86::SHR16r1,     X86::SHR16m1 },
00173     { X86::SHR16rCL,    X86::SHR16mCL },
00174     { X86::SHR16ri,     X86::SHR16mi },
00175     { X86::SHR32r1,     X86::SHR32m1 },
00176     { X86::SHR32rCL,    X86::SHR32mCL },
00177     { X86::SHR32ri,     X86::SHR32mi },
00178     { X86::SHR64r1,     X86::SHR64m1 },
00179     { X86::SHR64rCL,    X86::SHR64mCL },
00180     { X86::SHR64ri,     X86::SHR64mi },
00181     { X86::SHR8r1,      X86::SHR8m1 },
00182     { X86::SHR8rCL,     X86::SHR8mCL },
00183     { X86::SHR8ri,      X86::SHR8mi },
00184     { X86::SHRD16rrCL,  X86::SHRD16mrCL },
00185     { X86::SHRD16rri8,  X86::SHRD16mri8 },
00186     { X86::SHRD32rrCL,  X86::SHRD32mrCL },
00187     { X86::SHRD32rri8,  X86::SHRD32mri8 },
00188     { X86::SHRD64rrCL,  X86::SHRD64mrCL },
00189     { X86::SHRD64rri8,  X86::SHRD64mri8 },
00190     { X86::SUB16ri,     X86::SUB16mi },
00191     { X86::SUB16ri8,    X86::SUB16mi8 },
00192     { X86::SUB16rr,     X86::SUB16mr },
00193     { X86::SUB32ri,     X86::SUB32mi },
00194     { X86::SUB32ri8,    X86::SUB32mi8 },
00195     { X86::SUB32rr,     X86::SUB32mr },
00196     { X86::SUB64ri32,   X86::SUB64mi32 },
00197     { X86::SUB64ri8,    X86::SUB64mi8 },
00198     { X86::SUB64rr,     X86::SUB64mr },
00199     { X86::SUB8ri,      X86::SUB8mi },
00200     { X86::SUB8rr,      X86::SUB8mr },
00201     { X86::XOR16ri,     X86::XOR16mi },
00202     { X86::XOR16ri8,    X86::XOR16mi8 },
00203     { X86::XOR16rr,     X86::XOR16mr },
00204     { X86::XOR32ri,     X86::XOR32mi },
00205     { X86::XOR32ri8,    X86::XOR32mi8 },
00206     { X86::XOR32rr,     X86::XOR32mr },
00207     { X86::XOR64ri32,   X86::XOR64mi32 },
00208     { X86::XOR64ri8,    X86::XOR64mi8 },
00209     { X86::XOR64rr,     X86::XOR64mr },
00210     { X86::XOR8ri,      X86::XOR8mi },
00211     { X86::XOR8rr,      X86::XOR8mr }
00212   };
00213 
00214   for (unsigned i = 0, e = array_lengthof(OpTbl2Addr); i != e; ++i) {
00215     unsigned RegOp = OpTbl2Addr[i][0];
00216     unsigned MemOp = OpTbl2Addr[i][1];
00217     if (!RegOp2MemOpTable2Addr.insert(std::make_pair((unsigned*)RegOp,
00218                                                std::make_pair(MemOp,0))).second)
00219       assert(false && "Duplicated entries?");
00220     // Index 0, folded load and store, no alignment requirement.
00221     unsigned AuxInfo = 0 | (1 << 4) | (1 << 5);
00222     if (!MemOp2RegOpTable.insert(std::make_pair((unsigned*)MemOp,
00223                                                 std::make_pair(RegOp,
00224                                                               AuxInfo))).second)
00225       AmbEntries.push_back(MemOp);
00226   }
00227 
00228   // If the third value is 1, then it's folding either a load or a store.
00229   static const unsigned OpTbl0[][4] = {
00230     { X86::BT16ri8,     X86::BT16mi8, 1, 0 },
00231     { X86::BT32ri8,     X86::BT32mi8, 1, 0 },
00232     { X86::BT64ri8,     X86::BT64mi8, 1, 0 },
00233     { X86::CALL32r,     X86::CALL32m, 1, 0 },
00234     { X86::CALL64r,     X86::CALL64m, 1, 0 },
00235     { X86::CMP16ri,     X86::CMP16mi, 1, 0 },
00236     { X86::CMP16ri8,    X86::CMP16mi8, 1, 0 },
00237     { X86::CMP16rr,     X86::CMP16mr, 1, 0 },
00238     { X86::CMP32ri,     X86::CMP32mi, 1, 0 },
00239     { X86::CMP32ri8,    X86::CMP32mi8, 1, 0 },
00240     { X86::CMP32rr,     X86::CMP32mr, 1, 0 },
00241     { X86::CMP64ri32,   X86::CMP64mi32, 1, 0 },
00242     { X86::CMP64ri8,    X86::CMP64mi8, 1, 0 },
00243     { X86::CMP64rr,     X86::CMP64mr, 1, 0 },
00244     { X86::CMP8ri,      X86::CMP8mi, 1, 0 },
00245     { X86::CMP8rr,      X86::CMP8mr, 1, 0 },
00246     { X86::DIV16r,      X86::DIV16m, 1, 0 },
00247     { X86::DIV32r,      X86::DIV32m, 1, 0 },
00248     { X86::DIV64r,      X86::DIV64m, 1, 0 },
00249     { X86::DIV8r,       X86::DIV8m, 1, 0 },
00250     { X86::EXTRACTPSrr, X86::EXTRACTPSmr, 0, 16 },
00251     { X86::FsMOVAPDrr,  X86::MOVSDmr, 0, 0 },
00252     { X86::FsMOVAPSrr,  X86::MOVSSmr, 0, 0 },
00253     { X86::IDIV16r,     X86::IDIV16m, 1, 0 },
00254     { X86::IDIV32r,     X86::IDIV32m, 1, 0 },
00255     { X86::IDIV64r,     X86::IDIV64m, 1, 0 },
00256     { X86::IDIV8r,      X86::IDIV8m, 1, 0 },
00257     { X86::IMUL16r,     X86::IMUL16m, 1, 0 },
00258     { X86::IMUL32r,     X86::IMUL32m, 1, 0 },
00259     { X86::IMUL64r,     X86::IMUL64m, 1, 0 },
00260     { X86::IMUL8r,      X86::IMUL8m, 1, 0 },
00261     { X86::JMP32r,      X86::JMP32m, 1, 0 },
00262     { X86::JMP64r,      X86::JMP64m, 1, 0 },
00263     { X86::MOV16ri,     X86::MOV16mi, 0, 0 },
00264     { X86::MOV16rr,     X86::MOV16mr, 0, 0 },
00265     { X86::MOV32ri,     X86::MOV32mi, 0, 0 },
00266     { X86::MOV32rr,     X86::MOV32mr, 0, 0 },
00267     { X86::MOV64ri32,   X86::MOV64mi32, 0, 0 },
00268     { X86::MOV64rr,     X86::MOV64mr, 0, 0 },
00269     { X86::MOV8ri,      X86::MOV8mi, 0, 0 },
00270     { X86::MOV8rr,      X86::MOV8mr, 0, 0 },
00271     { X86::MOV8rr_NOREX, X86::MOV8mr_NOREX, 0, 0 },
00272     { X86::MOVAPDrr,    X86::MOVAPDmr, 0, 16 },
00273     { X86::MOVAPSrr,    X86::MOVAPSmr, 0, 16 },
00274     { X86::MOVDQArr,    X86::MOVDQAmr, 0, 16 },
00275     { X86::MOVPDI2DIrr, X86::MOVPDI2DImr, 0, 0 },
00276     { X86::MOVPQIto64rr,X86::MOVPQI2QImr, 0, 0 },
00277     { X86::MOVPS2SSrr,  X86::MOVPS2SSmr, 0, 0 },
00278     { X86::MOVSDrr,     X86::MOVSDmr, 0, 0 },
00279     { X86::MOVSDto64rr, X86::MOVSDto64mr, 0, 0 },
00280     { X86::MOVSS2DIrr,  X86::MOVSS2DImr, 0, 0 },
00281     { X86::MOVSSrr,     X86::MOVSSmr, 0, 0 },
00282     { X86::MOVUPDrr,    X86::MOVUPDmr, 0, 0 },
00283     { X86::MOVUPSrr,    X86::MOVUPSmr, 0, 0 },
00284     { X86::MUL16r,      X86::MUL16m, 1, 0 },
00285     { X86::MUL32r,      X86::MUL32m, 1, 0 },
00286     { X86::MUL64r,      X86::MUL64m, 1, 0 },
00287     { X86::MUL8r,       X86::MUL8m, 1, 0 },
00288     { X86::SETAEr,      X86::SETAEm, 0, 0 },
00289     { X86::SETAr,       X86::SETAm, 0, 0 },
00290     { X86::SETBEr,      X86::SETBEm, 0, 0 },
00291     { X86::SETBr,       X86::SETBm, 0, 0 },
00292     { X86::SETEr,       X86::SETEm, 0, 0 },
00293     { X86::SETGEr,      X86::SETGEm, 0, 0 },
00294     { X86::SETGr,       X86::SETGm, 0, 0 },
00295     { X86::SETLEr,      X86::SETLEm, 0, 0 },
00296     { X86::SETLr,       X86::SETLm, 0, 0 },
00297     { X86::SETNEr,      X86::SETNEm, 0, 0 },
00298     { X86::SETNOr,      X86::SETNOm, 0, 0 },
00299     { X86::SETNPr,      X86::SETNPm, 0, 0 },
00300     { X86::SETNSr,      X86::SETNSm, 0, 0 },
00301     { X86::SETOr,       X86::SETOm, 0, 0 },
00302     { X86::SETPr,       X86::SETPm, 0, 0 },
00303     { X86::SETSr,       X86::SETSm, 0, 0 },
00304     { X86::TAILJMPr,    X86::TAILJMPm, 1, 0 },
00305     { X86::TEST16ri,    X86::TEST16mi, 1, 0 },
00306     { X86::TEST32ri,    X86::TEST32mi, 1, 0 },
00307     { X86::TEST64ri32,  X86::TEST64mi32, 1, 0 },
00308     { X86::TEST8ri,     X86::TEST8mi, 1, 0 }
00309   };
00310 
00311   for (unsigned i = 0, e = array_lengthof(OpTbl0); i != e; ++i) {
00312     unsigned RegOp = OpTbl0[i][0];
00313     unsigned MemOp = OpTbl0[i][1];
00314     unsigned Align = OpTbl0[i][3];
00315     if (!RegOp2MemOpTable0.insert(std::make_pair((unsigned*)RegOp,
00316                                            std::make_pair(MemOp,Align))).second)
00317       assert(false && "Duplicated entries?");
00318     unsigned FoldedLoad = OpTbl0[i][2];
00319     // Index 0, folded load or store.
00320     unsigned AuxInfo = 0 | (FoldedLoad << 4) | ((FoldedLoad^1) << 5);
00321     if (RegOp != X86::FsMOVAPDrr && RegOp != X86::FsMOVAPSrr)
00322       if (!MemOp2RegOpTable.insert(std::make_pair((unsigned*)MemOp,
00323                                      std::make_pair(RegOp, AuxInfo))).second)
00324         AmbEntries.push_back(MemOp);
00325   }
00326 
00327   static const unsigned OpTbl1[][3] = {
00328     { X86::CMP16rr,         X86::CMP16rm, 0 },
00329     { X86::CMP32rr,         X86::CMP32rm, 0 },
00330     { X86::CMP64rr,         X86::CMP64rm, 0 },
00331     { X86::CMP8rr,          X86::CMP8rm, 0 },
00332     { X86::CVTSD2SSrr,      X86::CVTSD2SSrm, 0 },
00333     { X86::CVTSI2SD64rr,    X86::CVTSI2SD64rm, 0 },
00334     { X86::CVTSI2SDrr,      X86::CVTSI2SDrm, 0 },
00335     { X86::CVTSI2SS64rr,    X86::CVTSI2SS64rm, 0 },
00336     { X86::CVTSI2SSrr,      X86::CVTSI2SSrm, 0 },
00337     { X86::CVTSS2SDrr,      X86::CVTSS2SDrm, 0 },
00338     { X86::CVTTSD2SI64rr,   X86::CVTTSD2SI64rm, 0 },
00339     { X86::CVTTSD2SIrr,     X86::CVTTSD2SIrm, 0 },
00340     { X86::CVTTSS2SI64rr,   X86::CVTTSS2SI64rm, 0 },
00341     { X86::CVTTSS2SIrr,     X86::CVTTSS2SIrm, 0 },
00342     { X86::FsMOVAPDrr,      X86::MOVSDrm, 0 },
00343     { X86::FsMOVAPSrr,      X86::MOVSSrm, 0 },
00344     { X86::IMUL16rri,       X86::IMUL16rmi, 0 },
00345     { X86::IMUL16rri8,      X86::IMUL16rmi8, 0 },
00346     { X86::IMUL32rri,       X86::IMUL32rmi, 0 },
00347     { X86::IMUL32rri8,      X86::IMUL32rmi8, 0 },
00348     { X86::IMUL64rri32,     X86::IMUL64rmi32, 0 },
00349     { X86::IMUL64rri8,      X86::IMUL64rmi8, 0 },
00350     { X86::Int_CMPSDrr,     X86::Int_CMPSDrm, 0 },
00351     { X86::Int_CMPSSrr,     X86::Int_CMPSSrm, 0 },
00352     { X86::Int_COMISDrr,    X86::Int_COMISDrm, 0 },
00353     { X86::Int_COMISSrr,    X86::Int_COMISSrm, 0 },
00354     { X86::Int_CVTDQ2PDrr,  X86::Int_CVTDQ2PDrm, 16 },
00355     { X86::Int_CVTDQ2PSrr,  X86::Int_CVTDQ2PSrm, 16 },
00356     { X86::Int_CVTPD2DQrr,  X86::Int_CVTPD2DQrm, 16 },
00357     { X86::Int_CVTPD2PSrr,  X86::Int_CVTPD2PSrm, 16 },
00358     { X86::Int_CVTPS2DQrr,  X86::Int_CVTPS2DQrm, 16 },
00359     { X86::Int_CVTPS2PDrr,  X86::Int_CVTPS2PDrm, 0 },
00360     { X86::Int_CVTSD2SI64rr,X86::Int_CVTSD2SI64rm, 0 },
00361     { X86::Int_CVTSD2SIrr,  X86::Int_CVTSD2SIrm, 0 },
00362     { X86::Int_CVTSD2SSrr,  X86::Int_CVTSD2SSrm, 0 },
00363     { X86::Int_CVTSI2SD64rr,X86::Int_CVTSI2SD64rm, 0 },
00364     { X86::Int_CVTSI2SDrr,  X86::Int_CVTSI2SDrm, 0 },
00365     { X86::Int_CVTSI2SS64rr,X86::Int_CVTSI2SS64rm, 0 },
00366     { X86::Int_CVTSI2SSrr,  X86::Int_CVTSI2SSrm, 0 },
00367     { X86::Int_CVTSS2SDrr,  X86::Int_CVTSS2SDrm, 0 },
00368     { X86::Int_CVTSS2SI64rr,X86::Int_CVTSS2SI64rm, 0 },
00369     { X86::Int_CVTSS2SIrr,  X86::Int_CVTSS2SIrm, 0 },
00370     { X86::Int_CVTTPD2DQrr, X86::Int_CVTTPD2DQrm, 16 },
00371     { X86::Int_CVTTPS2DQrr, X86::Int_CVTTPS2DQrm, 16 },
00372     { X86::Int_CVTTSD2SI64rr,X86::Int_CVTTSD2SI64rm, 0 },
00373     { X86::Int_CVTTSD2SIrr, X86::Int_CVTTSD2SIrm, 0 },
00374     { X86::Int_CVTTSS2SI64rr,X86::Int_CVTTSS2SI64rm, 0 },
00375     { X86::Int_CVTTSS2SIrr, X86::Int_CVTTSS2SIrm, 0 },
00376     { X86::Int_UCOMISDrr,   X86::Int_UCOMISDrm, 0 },
00377     { X86::Int_UCOMISSrr,   X86::Int_UCOMISSrm, 0 },
00378     { X86::MOV16rr,         X86::MOV16rm, 0 },
00379     { X86::MOV32rr,         X86::MOV32rm, 0 },
00380     { X86::MOV64rr,         X86::MOV64rm, 0 },
00381     { X86::MOV64toPQIrr,    X86::MOVQI2PQIrm, 0 },
00382     { X86::MOV64toSDrr,     X86::MOV64toSDrm, 0 },
00383     { X86::MOV8rr,          X86::MOV8rm, 0 },
00384     { X86::MOVAPDrr,        X86::MOVAPDrm, 16 },
00385     { X86::MOVAPSrr,        X86::MOVAPSrm, 16 },
00386     { X86::MOVDDUPrr,       X86::MOVDDUPrm, 0 },
00387     { X86::MOVDI2PDIrr,     X86::MOVDI2PDIrm, 0 },
00388     { X86::MOVDI2SSrr,      X86::MOVDI2SSrm, 0 },
00389     { X86::MOVDQArr,        X86::MOVDQArm, 16 },
00390     { X86::MOVSD2PDrr,      X86::MOVSD2PDrm, 0 },
00391     { X86::MOVSDrr,         X86::MOVSDrm, 0 },
00392     { X86::MOVSHDUPrr,      X86::MOVSHDUPrm, 16 },
00393     { X86::MOVSLDUPrr,      X86::MOVSLDUPrm, 16 },
00394     { X86::MOVSS2PSrr,      X86::MOVSS2PSrm, 0 },
00395     { X86::MOVSSrr,         X86::MOVSSrm, 0 },
00396     { X86::MOVSX16rr8,      X86::MOVSX16rm8, 0 },
00397     { X86::MOVSX32rr16,     X86::MOVSX32rm16, 0 },
00398     { X86::MOVSX32rr8,      X86::MOVSX32rm8, 0 },
00399     { X86::MOVSX64rr16,     X86::MOVSX64rm16, 0 },
00400     { X86::MOVSX64rr32,     X86::MOVSX64rm32, 0 },
00401     { X86::MOVSX64rr8,      X86::MOVSX64rm8, 0 },
00402     { X86::MOVUPDrr,        X86::MOVUPDrm, 16 },
00403     { X86::MOVUPSrr,        X86::MOVUPSrm, 16 },
00404     { X86::MOVZDI2PDIrr,    X86::MOVZDI2PDIrm, 0 },
00405     { X86::MOVZQI2PQIrr,    X86::MOVZQI2PQIrm, 0 },
00406     { X86::MOVZPQILo2PQIrr, X86::MOVZPQILo2PQIrm, 16 },
00407     { X86::MOVZX16rr8,      X86::MOVZX16rm8, 0 },
00408     { X86::MOVZX32rr16,     X86::MOVZX32rm16, 0 },
00409     { X86::MOVZX32_NOREXrr8, X86::MOVZX32_NOREXrm8, 0 },
00410     { X86::MOVZX32rr8,      X86::MOVZX32rm8, 0 },
00411     { X86::MOVZX64rr16,     X86::MOVZX64rm16, 0 },
00412     { X86::MOVZX64rr32,     X86::MOVZX64rm32, 0 },
00413     { X86::MOVZX64rr8,      X86::MOVZX64rm8, 0 },
00414     { X86::PSHUFDri,        X86::PSHUFDmi, 16 },
00415     { X86::PSHUFHWri,       X86::PSHUFHWmi, 16 },
00416     { X86::PSHUFLWri,       X86::PSHUFLWmi, 16 },
00417     { X86::RCPPSr,          X86::RCPPSm, 16 },
00418     { X86::RCPPSr_Int,      X86::RCPPSm_Int, 16 },
00419     { X86::RSQRTPSr,        X86::RSQRTPSm, 16 },
00420     { X86::RSQRTPSr_Int,    X86::RSQRTPSm_Int, 16 },
00421     { X86::RSQRTSSr,        X86::RSQRTSSm, 0 },
00422     { X86::RSQRTSSr_Int,    X86::RSQRTSSm_Int, 0 },
00423     { X86::SQRTPDr,         X86::SQRTPDm, 16 },
00424     { X86::SQRTPDr_Int,     X86::SQRTPDm_Int, 16 },
00425     { X86::SQRTPSr,         X86::SQRTPSm, 16 },
00426     { X86::SQRTPSr_Int,     X86::SQRTPSm_Int, 16 },
00427     { X86::SQRTSDr,         X86::SQRTSDm, 0 },
00428     { X86::SQRTSDr_Int,     X86::SQRTSDm_Int, 0 },
00429     { X86::SQRTSSr,         X86::SQRTSSm, 0 },
00430     { X86::SQRTSSr_Int,     X86::SQRTSSm_Int, 0 },
00431     { X86::TEST16rr,        X86::TEST16rm, 0 },
00432     { X86::TEST32rr,        X86::TEST32rm, 0 },
00433     { X86::TEST64rr,        X86::TEST64rm, 0 },
00434     { X86::TEST8rr,         X86::TEST8rm, 0 },
00435     // FIXME: TEST*rr EAX,EAX ---> CMP [mem], 0
00436     { X86::UCOMISDrr,       X86::UCOMISDrm, 0 },
00437     { X86::UCOMISSrr,       X86::UCOMISSrm, 0 }
00438   };
00439 
00440   for (unsigned i = 0, e = array_lengthof(OpTbl1); i != e; ++i) {
00441     unsigned RegOp = OpTbl1[i][0];
00442     unsigned MemOp = OpTbl1[i][1];
00443     unsigned Align = OpTbl1[i][2];
00444     if (!RegOp2MemOpTable1.insert(std::make_pair((unsigned*)RegOp,
00445                                            std::make_pair(MemOp,Align))).second)
00446       assert(false && "Duplicated entries?");
00447     // Index 1, folded load
00448     unsigned AuxInfo = 1 | (1 << 4);
00449     if (RegOp != X86::FsMOVAPDrr && RegOp != X86::FsMOVAPSrr)
00450       if (!MemOp2RegOpTable.insert(std::make_pair((unsigned*)MemOp,
00451                                      std::make_pair(RegOp, AuxInfo))).second)
00452         AmbEntries.push_back(MemOp);
00453   }
00454 
00455   static const unsigned OpTbl2[][3] = {
00456     { X86::ADC32rr,         X86::ADC32rm, 0 },
00457     { X86::ADC64rr,         X86::ADC64rm, 0 },
00458     { X86::ADD16rr,         X86::ADD16rm, 0 },
00459     { X86::ADD32rr,         X86::ADD32rm, 0 },
00460     { X86::ADD64rr,         X86::ADD64rm, 0 },
00461     { X86::ADD8rr,          X86::ADD8rm, 0 },
00462     { X86::ADDPDrr,         X86::ADDPDrm, 16 },
00463     { X86::ADDPSrr,         X86::ADDPSrm, 16 },
00464     { X86::ADDSDrr,         X86::ADDSDrm, 0 },
00465     { X86::ADDSSrr,         X86::ADDSSrm, 0 },
00466     { X86::ADDSUBPDrr,      X86::ADDSUBPDrm, 16 },
00467     { X86::ADDSUBPSrr,      X86::ADDSUBPSrm, 16 },
00468     { X86::AND16rr,         X86::AND16rm, 0 },
00469     { X86::AND32rr,         X86::AND32rm, 0 },
00470     { X86::AND64rr,         X86::AND64rm, 0 },
00471     { X86::AND8rr,          X86::AND8rm, 0 },
00472     { X86::ANDNPDrr,        X86::ANDNPDrm, 16 },
00473     { X86::ANDNPSrr,        X86::ANDNPSrm, 16 },
00474     { X86::ANDPDrr,         X86::ANDPDrm, 16 },
00475     { X86::ANDPSrr,         X86::ANDPSrm, 16 },
00476     { X86::CMOVA16rr,       X86::CMOVA16rm, 0 },
00477     { X86::CMOVA32rr,       X86::CMOVA32rm, 0 },
00478     { X86::CMOVA64rr,       X86::CMOVA64rm, 0 },
00479     { X86::CMOVAE16rr,      X86::CMOVAE16rm, 0 },
00480     { X86::CMOVAE32rr,      X86::CMOVAE32rm, 0 },
00481     { X86::CMOVAE64rr,      X86::CMOVAE64rm, 0 },
00482     { X86::CMOVB16rr,       X86::CMOVB16rm, 0 },
00483     { X86::CMOVB32rr,       X86::CMOVB32rm, 0 },
00484     { X86::CMOVB64rr,       X86::CMOVB64rm, 0 },
00485     { X86::CMOVBE16rr,      X86::CMOVBE16rm, 0 },
00486     { X86::CMOVBE32rr,      X86::CMOVBE32rm, 0 },
00487     { X86::CMOVBE64rr,      X86::CMOVBE64rm, 0 },
00488     { X86::CMOVE16rr,       X86::CMOVE16rm, 0 },
00489     { X86::CMOVE32rr,       X86::CMOVE32rm, 0 },
00490     { X86::CMOVE64rr,       X86::CMOVE64rm, 0 },
00491     { X86::CMOVG16rr,       X86::CMOVG16rm, 0 },
00492     { X86::CMOVG32rr,       X86::CMOVG32rm, 0 },
00493     { X86::CMOVG64rr,       X86::CMOVG64rm, 0 },
00494     { X86::CMOVGE16rr,      X86::CMOVGE16rm, 0 },
00495     { X86::CMOVGE32rr,      X86::CMOVGE32rm, 0 },
00496     { X86::CMOVGE64rr,      X86::CMOVGE64rm, 0 },
00497     { X86::CMOVL16rr,       X86::CMOVL16rm, 0 },
00498     { X86::CMOVL32rr,       X86::CMOVL32rm, 0 },
00499     { X86::CMOVL64rr,       X86::CMOVL64rm, 0 },
00500     { X86::CMOVLE16rr,      X86::CMOVLE16rm, 0 },
00501     { X86::CMOVLE32rr,      X86::CMOVLE32rm, 0 },
00502     { X86::CMOVLE64rr,      X86::CMOVLE64rm, 0 },
00503     { X86::CMOVNE16rr,      X86::CMOVNE16rm, 0 },
00504     { X86::CMOVNE32rr,      X86::CMOVNE32rm, 0 },
00505     { X86::CMOVNE64rr,      X86::CMOVNE64rm, 0 },
00506     { X86::CMOVNO16rr,      X86::CMOVNO16rm, 0 },
00507     { X86::CMOVNO32rr,      X86::CMOVNO32rm, 0 },
00508     { X86::CMOVNO64rr,      X86::CMOVNO64rm, 0 },
00509     { X86::CMOVNP16rr,      X86::CMOVNP16rm, 0 },
00510     { X86::CMOVNP32rr,      X86::CMOVNP32rm, 0 },
00511     { X86::CMOVNP64rr,      X86::CMOVNP64rm, 0 },
00512     { X86::CMOVNS16rr,      X86::CMOVNS16rm, 0 },
00513     { X86::CMOVNS32rr,      X86::CMOVNS32rm, 0 },
00514     { X86::CMOVNS64rr,      X86::CMOVNS64rm, 0 },
00515     { X86::CMOVO16rr,       X86::CMOVO16rm, 0 },
00516     { X86::CMOVO32rr,       X86::CMOVO32rm, 0 },
00517     { X86::CMOVO64rr,       X86::CMOVO64rm, 0 },
00518     { X86::CMOVP16rr,       X86::CMOVP16rm, 0 },
00519     { X86::CMOVP32rr,       X86::CMOVP32rm, 0 },
00520     { X86::CMOVP64rr,       X86::CMOVP64rm, 0 },
00521     { X86::CMOVS16rr,       X86::CMOVS16rm, 0 },
00522     { X86::CMOVS32rr,       X86::CMOVS32rm, 0 },
00523     { X86::CMOVS64rr,       X86::CMOVS64rm, 0 },
00524     { X86::CMPPDrri,        X86::CMPPDrmi, 16 },
00525     { X86::CMPPSrri,        X86::CMPPSrmi, 16 },
00526     { X86::CMPSDrr,         X86::CMPSDrm, 0 },
00527     { X86::CMPSSrr,         X86::CMPSSrm, 0 },
00528     { X86::DIVPDrr,         X86::DIVPDrm, 16 },
00529     { X86::DIVPSrr,         X86::DIVPSrm, 16 },
00530     { X86::DIVSDrr,         X86::DIVSDrm, 0 },
00531     { X86::DIVSSrr,         X86::DIVSSrm, 0 },
00532     { X86::FsANDNPDrr,      X86::FsANDNPDrm, 16 },
00533     { X86::FsANDNPSrr,      X86::FsANDNPSrm, 16 },
00534     { X86::FsANDPDrr,       X86::FsANDPDrm, 16 },
00535     { X86::FsANDPSrr,       X86::FsANDPSrm, 16 },
00536     { X86::FsORPDrr,        X86::FsORPDrm, 16 },
00537     { X86::FsORPSrr,        X86::FsORPSrm, 16 },
00538     { X86::FsXORPDrr,       X86::FsXORPDrm, 16 },
00539     { X86::FsXORPSrr,       X86::FsXORPSrm, 16 },
00540     { X86::HADDPDrr,        X86::HADDPDrm, 16 },
00541     { X86::HADDPSrr,        X86::HADDPSrm, 16 },
00542     { X86::HSUBPDrr,        X86::HSUBPDrm, 16 },
00543     { X86::HSUBPSrr,        X86::HSUBPSrm, 16 },
00544     { X86::IMUL16rr,        X86::IMUL16rm, 0 },
00545     { X86::IMUL32rr,        X86::IMUL32rm, 0 },
00546     { X86::IMUL64rr,        X86::IMUL64rm, 0 },
00547     { X86::MAXPDrr,         X86::MAXPDrm, 16 },
00548     { X86::MAXPDrr_Int,     X86::MAXPDrm_Int, 16 },
00549     { X86::MAXPSrr,         X86::MAXPSrm, 16 },
00550     { X86::MAXPSrr_Int,     X86::MAXPSrm_Int, 16 },
00551     { X86::MAXSDrr,         X86::MAXSDrm, 0 },
00552     { X86::MAXSDrr_Int,     X86::MAXSDrm_Int, 0 },
00553     { X86::MAXSSrr,         X86::MAXSSrm, 0 },
00554     { X86::MAXSSrr_Int,     X86::MAXSSrm_Int, 0 },
00555     { X86::MINPDrr,         X86::MINPDrm, 16 },
00556     { X86::MINPDrr_Int,     X86::MINPDrm_Int, 16 },
00557     { X86::MINPSrr,         X86::MINPSrm, 16 },
00558     { X86::MINPSrr_Int,     X86::MINPSrm_Int, 16 },
00559     { X86::MINSDrr,         X86::MINSDrm, 0 },
00560     { X86::MINSDrr_Int,     X86::MINSDrm_Int, 0 },
00561     { X86::MINSSrr,         X86::MINSSrm, 0 },
00562     { X86::MINSSrr_Int,     X86::MINSSrm_Int, 0 },
00563     { X86::MULPDrr,         X86::MULPDrm, 16 },
00564     { X86::MULPSrr,         X86::MULPSrm, 16 },
00565     { X86::MULSDrr,         X86::MULSDrm, 0 },
00566     { X86::MULSSrr,         X86::MULSSrm, 0 },
00567     { X86::OR16rr,          X86::OR16rm, 0 },
00568     { X86::OR32rr,          X86::OR32rm, 0 },
00569     { X86::OR64rr,          X86::OR64rm, 0 },
00570     { X86::OR8rr,           X86::OR8rm, 0 },
00571     { X86::ORPDrr,          X86::ORPDrm, 16 },
00572     { X86::ORPSrr,          X86::ORPSrm, 16 },
00573     { X86::PACKSSDWrr,      X86::PACKSSDWrm, 16 },
00574     { X86::PACKSSWBrr,      X86::PACKSSWBrm, 16 },
00575     { X86::PACKUSWBrr,      X86::PACKUSWBrm, 16 },
00576     { X86::PADDBrr,         X86::PADDBrm, 16 },
00577     { X86::PADDDrr,         X86::PADDDrm, 16 },
00578     { X86::PADDQrr,         X86::PADDQrm, 16 },
00579     { X86::PADDSBrr,        X86::PADDSBrm, 16 },
00580     { X86::PADDSWrr,        X86::PADDSWrm, 16 },
00581     { X86::PADDWrr,         X86::PADDWrm, 16 },
00582     { X86::PANDNrr,         X86::PANDNrm, 16 },
00583     { X86::PANDrr,          X86::PANDrm, 16 },
00584     { X86::PAVGBrr,         X86::PAVGBrm, 16 },
00585     { X86::PAVGWrr,         X86::PAVGWrm, 16 },
00586     { X86::PCMPEQBrr,       X86::PCMPEQBrm, 16 },
00587     { X86::PCMPEQDrr,       X86::PCMPEQDrm, 16 },
00588     { X86::PCMPEQWrr,       X86::PCMPEQWrm, 16 },
00589     { X86::PCMPGTBrr,       X86::PCMPGTBrm, 16 },
00590     { X86::PCMPGTDrr,       X86::PCMPGTDrm, 16 },
00591     { X86::PCMPGTWrr,       X86::PCMPGTWrm, 16 },
00592     { X86::PINSRWrri,       X86::PINSRWrmi, 16 },
00593     { X86::PMADDWDrr,       X86::PMADDWDrm, 16 },
00594     { X86::PMAXSWrr,        X86::PMAXSWrm, 16 },
00595     { X86::PMAXUBrr,        X86::PMAXUBrm, 16 },
00596     { X86::PMINSWrr,        X86::PMINSWrm, 16 },
00597     { X86::PMINUBrr,        X86::PMINUBrm, 16 },
00598     { X86::PMULDQrr,        X86::PMULDQrm, 16 },
00599     { X86::PMULHUWrr,       X86::PMULHUWrm, 16 },
00600     { X86::PMULHWrr,        X86::PMULHWrm, 16 },
00601     { X86::PMULLDrr,        X86::PMULLDrm, 16 },
00602     { X86::PMULLDrr_int,    X86::PMULLDrm_int, 16 },
00603     { X86::PMULLWrr,        X86::PMULLWrm, 16 },
00604     { X86::PMULUDQrr,       X86::PMULUDQrm, 16 },
00605     { X86::PORrr,           X86::PORrm, 16 },
00606     { X86::PSADBWrr,        X86::PSADBWrm, 16 },
00607     { X86::PSLLDrr,         X86::PSLLDrm, 16 },
00608     { X86::PSLLQrr,         X86::PSLLQrm, 16 },
00609     { X86::PSLLWrr,         X86::PSLLWrm, 16 },
00610     { X86::PSRADrr,         X86::PSRADrm, 16 },
00611     { X86::PSRAWrr,         X86::PSRAWrm, 16 },
00612     { X86::PSRLDrr,         X86::PSRLDrm, 16 },
00613     { X86::PSRLQrr,         X86::PSRLQrm, 16 },
00614     { X86::PSRLWrr,         X86::PSRLWrm, 16 },
00615     { X86::PSUBBrr,         X86::PSUBBrm, 16 },
00616     { X86::PSUBDrr,         X86::PSUBDrm, 16 },
00617     { X86::PSUBSBrr,        X86::PSUBSBrm, 16 },
00618     { X86::PSUBSWrr,        X86::PSUBSWrm, 16 },
00619     { X86::PSUBWrr,         X86::PSUBWrm, 16 },
00620     { X86::PUNPCKHBWrr,     X86::PUNPCKHBWrm, 16 },
00621     { X86::PUNPCKHDQrr,     X86::PUNPCKHDQrm, 16 },
00622     { X86::PUNPCKHQDQrr,    X86::PUNPCKHQDQrm, 16 },
00623     { X86::PUNPCKHWDrr,     X86::PUNPCKHWDrm, 16 },
00624     { X86::PUNPCKLBWrr,     X86::PUNPCKLBWrm, 16 },
00625     { X86::PUNPCKLDQrr,     X86::PUNPCKLDQrm, 16 },
00626     { X86::PUNPCKLQDQrr,    X86::PUNPCKLQDQrm, 16 },
00627     { X86::PUNPCKLWDrr,     X86::PUNPCKLWDrm, 16 },
00628     { X86::PXORrr,          X86::PXORrm, 16 },
00629     { X86::SBB32rr,         X86::SBB32rm, 0 },
00630     { X86::SBB64rr,         X86::SBB64rm, 0 },
00631     { X86::SHUFPDrri,       X86::SHUFPDrmi, 16 },
00632     { X86::SHUFPSrri,       X86::SHUFPSrmi, 16 },
00633     { X86::SUB16rr,         X86::SUB16rm, 0 },
00634     { X86::SUB32rr,         X86::SUB32rm, 0 },
00635     { X86::SUB64rr,         X86::SUB64rm, 0 },
00636     { X86::SUB8rr,          X86::SUB8rm, 0 },
00637     { X86::SUBPDrr,         X86::SUBPDrm, 16 },
00638     { X86::SUBPSrr,         X86::SUBPSrm, 16 },
00639     { X86::SUBSDrr,         X86::SUBSDrm, 0 },
00640     { X86::SUBSSrr,         X86::SUBSSrm, 0 },
00641     // FIXME: TEST*rr -> swapped operand of TEST*mr.
00642     { X86::UNPCKHPDrr,      X86::UNPCKHPDrm, 16 },
00643     { X86::UNPCKHPSrr,      X86::UNPCKHPSrm, 16 },
00644     { X86::UNPCKLPDrr,      X86::UNPCKLPDrm, 16 },
00645     { X86::UNPCKLPSrr,      X86::UNPCKLPSrm, 16 },
00646     { X86::XOR16rr,         X86::XOR16rm, 0 },
00647     { X86::XOR32rr,         X86::XOR32rm, 0 },
00648     { X86::XOR64rr,         X86::XOR64rm, 0 },
00649     { X86::XOR8rr,          X86::XOR8rm, 0 },
00650     { X86::XORPDrr,         X86::XORPDrm, 16 },
00651     { X86::XORPSrr,         X86::XORPSrm, 16 }
00652   };
00653 
00654   for (unsigned i = 0, e = array_lengthof(OpTbl2); i != e; ++i) {
00655     unsigned RegOp = OpTbl2[i][0];
00656     unsigned MemOp = OpTbl2[i][1];
00657     unsigned Align = OpTbl2[i][2];
00658     if (!RegOp2MemOpTable2.insert(std::make_pair((unsigned*)RegOp,
00659                                            std::make_pair(MemOp,Align))).second)
00660       assert(false && "Duplicated entries?");
00661     // Index 2, folded load
00662     unsigned AuxInfo = 2 | (1 << 4);
00663     if (!MemOp2RegOpTable.insert(std::make_pair((unsigned*)MemOp,
00664                                    std::make_pair(RegOp, AuxInfo))).second)
00665       AmbEntries.push_back(MemOp);
00666   }
00667 
00668   // Remove ambiguous entries.
00669   assert(AmbEntries.empty() && "Duplicated entries in unfolding maps?");
00670 }
00671 
00672 bool X86InstrInfo::isMoveInstr(const MachineInstr& MI,
00673                                unsigned &SrcReg, unsigned &DstReg,
00674                                unsigned &SrcSubIdx, unsigned &DstSubIdx) const {
00675   switch (MI.getOpcode()) {
00676   default:
00677     return false;
00678   case X86::MOV8rr:
00679   case X86::MOV8rr_NOREX:
00680   case X86::MOV16rr:
00681   case X86::MOV32rr: 
00682   case X86::MOV64rr:
00683   case X86::MOVSSrr:
00684   case X86::MOVSDrr:
00685 
00686   // FP Stack register class copies
00687   case X86::MOV_Fp3232: case X86::MOV_Fp6464: case X86::MOV_Fp8080:
00688   case X86::MOV_Fp3264: case X86::MOV_Fp3280:
00689   case X86::MOV_Fp6432: case X86::MOV_Fp8032:
00690       
00691   case X86::FsMOVAPSrr:
00692   case X86::FsMOVAPDrr:
00693   case X86::MOVAPSrr:
00694   case X86::MOVAPDrr:
00695   case X86::MOVDQArr:
00696   case X86::MOVSS2PSrr:
00697   case X86::MOVSD2PDrr:
00698   case X86::MOVPS2SSrr:
00699   case X86::MOVPD2SDrr:
00700   case X86::MMX_MOVQ64rr:
00701     assert(MI.getNumOperands() >= 2 &&
00702            MI.getOperand(0).isReg() &&
00703            MI.getOperand(1).isReg() &&
00704            "invalid register-register move instruction");
00705     SrcReg = MI.getOperand(1).getReg();
00706     DstReg = MI.getOperand(0).getReg();
00707     SrcSubIdx = MI.getOperand(1).getSubReg();
00708     DstSubIdx = MI.getOperand(0).getSubReg();
00709     return true;
00710   }
00711 }
00712 
00713 unsigned X86InstrInfo::isLoadFromStackSlot(const MachineInstr *MI, 
00714                                            int &FrameIndex) const {
00715   switch (MI->getOpcode()) {
00716   default: break;
00717   case X86::MOV8rm:
00718   case X86::MOV16rm:
00719   case X86::MOV32rm:
00720   case X86::MOV64rm:
00721   case X86::LD_Fp64m:
00722   case X86::MOVSSrm:
00723   case X86::MOVSDrm:
00724   case X86::MOVAPSrm:
00725   case X86::MOVAPDrm:
00726   case X86::MOVDQArm:
00727   case X86::MMX_MOVD64rm:
00728   case X86::MMX_MOVQ64rm:
00729     if (MI->getOperand(1).isFI() && MI->getOperand(2).isImm() &&
00730         MI->getOperand(3).isReg() && MI->getOperand(4).isImm() &&
00731         MI->getOperand(2).getImm() == 1 &&
00732         MI->getOperand(3).getReg() == 0 &&
00733         MI->getOperand(4).getImm() == 0) {
00734       FrameIndex = MI->getOperand(1).getIndex();
00735       return MI->getOperand(0).getReg();
00736     }
00737     break;
00738   }
00739   return 0;
00740 }
00741 
00742 unsigned X86InstrInfo::isStoreToStackSlot(const MachineInstr *MI,
00743                                           int &FrameIndex) const {
00744   switch (MI->getOpcode()) {
00745   default: break;
00746   case X86::MOV8mr:
00747   case X86::MOV16mr:
00748   case X86::MOV32mr:
00749   case X86::MOV64mr:
00750   case X86::ST_FpP64m:
00751   case X86::MOVSSmr:
00752   case X86::MOVSDmr:
00753   case X86::MOVAPSmr:
00754   case X86::MOVAPDmr:
00755   case X86::MOVDQAmr:
00756   case X86::MMX_MOVD64mr:
00757   case X86::MMX_MOVQ64mr:
00758   case X86::MMX_MOVNTQmr:
00759     if (MI->getOperand(0).isFI() && MI->getOperand(1).isImm() &&
00760         MI->getOperand(2).isReg() && MI->getOperand(3).isImm() &&
00761         MI->getOperand(1).getImm() == 1 &&
00762         MI->getOperand(2).getReg() == 0 &&
00763         MI->getOperand(3).getImm() == 0) {
00764       FrameIndex = MI->getOperand(0).getIndex();
00765       return MI->getOperand(X86AddrNumOperands).getReg();
00766     }
00767     break;
00768   }
00769   return 0;
00770 }
00771 
00772 /// regIsPICBase - Return true if register is PIC base (i.e.g defined by
00773 /// X86::MOVPC32r.
00774 static bool regIsPICBase(unsigned BaseReg, const MachineRegisterInfo &MRI) {
00775   bool isPICBase = false;
00776   for (MachineRegisterInfo::def_iterator I = MRI.def_begin(BaseReg),
00777          E = MRI.def_end(); I != E; ++I) {
00778     MachineInstr *DefMI = I.getOperand().getParent();
00779     if (DefMI->getOpcode() != X86::MOVPC32r)
00780       return false;
00781     assert(!isPICBase && "More than one PIC base?");
00782     isPICBase = true;
00783   }
00784   return isPICBase;
00785 }
00786 
00787 /// CanRematLoadWithDispOperand - Return true if a load with the specified
00788 /// operand is a candidate for remat: for this to be true we need to know that
00789 /// the load will always return the same value, even if moved.
00790 static bool CanRematLoadWithDispOperand(const MachineOperand &MO,
00791                                         X86TargetMachine &TM) {
00792   // Loads from constant pool entries can be remat'd.
00793   if (MO.isCPI()) return true;
00794   
00795   // We can remat globals in some cases.
00796   if (MO.isGlobal()) {
00797     // If this is a load of a stub, not of the global, we can remat it.  This
00798     // access will always return the address of the global.
00799     if (isGlobalStubReference(MO.getTargetFlags()))
00800       return true;
00801     
00802     // If the global itself is constant, we can remat the load.
00803     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(MO.getGlobal()))
00804       if (GV->isConstant())
00805         return true;
00806   }
00807   return false;
00808 }
00809  
00810 bool
00811 X86InstrInfo::isReallyTriviallyReMaterializable(const MachineInstr *MI) const {
00812   switch (MI->getOpcode()) {
00813   default: break;
00814     case X86::MOV8rm:
00815     case X86::MOV16rm:
00816     case X86::MOV32rm:
00817     case X86::MOV64rm:
00818     case X86::LD_Fp64m:
00819     case X86::MOVSSrm:
00820     case X86::MOVSDrm:
00821     case X86::MOVAPSrm:
00822     case X86::MOVAPDrm:
00823     case X86::MOVDQArm:
00824     case X86::MMX_MOVD64rm:
00825     case X86::MMX_MOVQ64rm: {
00826       // Loads from constant pools are trivially rematerializable.
00827       if (MI->getOperand(1).isReg() &&
00828           MI->getOperand(2).isImm() &&
00829           MI->getOperand(3).isReg() && MI->getOperand(3).getReg() == 0 &&
00830           CanRematLoadWithDispOperand(MI->getOperand(4), TM)) {
00831         unsigned BaseReg = MI->getOperand(1).getReg();
00832         if (BaseReg == 0 || BaseReg == X86::RIP)
00833           return true;
00834         // Allow re-materialization of PIC load.
00835         if (!ReMatPICStubLoad && MI->getOperand(4).isGlobal())
00836           return false;
00837         const MachineFunction &MF = *MI->getParent()->getParent();
00838         const MachineRegisterInfo &MRI = MF.getRegInfo();
00839         bool isPICBase = false;
00840         for (MachineRegisterInfo::def_iterator I = MRI.def_begin(BaseReg),
00841                E = MRI.def_end(); I != E; ++I) {
00842           MachineInstr *DefMI = I.getOperand().getParent();
00843           if (DefMI->getOpcode() != X86::MOVPC32r)
00844             return false;
00845           assert(!isPICBase && "More than one PIC base?");
00846           isPICBase = true;
00847         }
00848         return isPICBase;
00849       } 
00850       return false;
00851     }
00852  
00853      case X86::LEA32r:
00854      case X86::LEA64r: {
00855        if (MI->getOperand(2).isImm() &&
00856            MI->getOperand(3).isReg() && MI->getOperand(3).getReg() == 0 &&
00857            !MI->getOperand(4).isReg()) {
00858          // lea fi#, lea GV, etc. are all rematerializable.
00859          if (!MI->getOperand(1).isReg())
00860            return true;
00861          unsigned BaseReg = MI->getOperand(1).getReg();
00862          if (BaseReg == 0)
00863            return true;
00864          // Allow re-materialization of lea PICBase + x.
00865          const MachineFunction &MF = *MI->getParent()->getParent();
00866          const MachineRegisterInfo &MRI = MF.getRegInfo();
00867          return regIsPICBase(BaseReg, MRI);
00868        }
00869        return false;
00870      }
00871   }
00872 
00873   // All other instructions marked M_REMATERIALIZABLE are always trivially
00874   // rematerializable.
00875   return true;
00876 }
00877 
00878 /// isSafeToClobberEFLAGS - Return true if it's safe insert an instruction that
00879 /// would clobber the EFLAGS condition register. Note the result may be
00880 /// conservative. If it cannot definitely determine the safety after visiting
00881 /// two instructions it assumes it's not safe.
00882 static bool isSafeToClobberEFLAGS(MachineBasicBlock &MBB,
00883                                   MachineBasicBlock::iterator I) {
00884   // It's always safe to clobber EFLAGS at the end of a block.
00885   if (I == MBB.end())
00886     return true;
00887 
00888   // For compile time consideration, if we are not able to determine the
00889   // safety after visiting 2 instructions, we will assume it's not safe.
00890   for (unsigned i = 0; i < 2; ++i) {
00891     bool SeenDef = false;
00892     for (unsigned j = 0, e = I->getNumOperands(); j != e; ++j) {
00893       MachineOperand &MO = I->getOperand(j);
00894       if (!MO.isReg())
00895         continue;
00896       if (MO.getReg() == X86::EFLAGS) {
00897         if (MO.isUse())
00898           return false;
00899         SeenDef = true;
00900       }
00901     }
00902 
00903     if (SeenDef)
00904       // This instruction defines EFLAGS, no need to look any further.
00905       return true;
00906     ++I;
00907 
00908     // If we make it to the end of the block, it's safe to clobber EFLAGS.
00909     if (I == MBB.end())
00910       return true;
00911   }
00912 
00913   // Conservative answer.
00914   return false;
00915 }
00916 
00917 void X86InstrInfo::reMaterialize(MachineBasicBlock &MBB,
00918                                  MachineBasicBlock::iterator I,
00919                                  unsigned DestReg, unsigned SubIdx,
00920                                  const MachineInstr *Orig) const {
00921   DebugLoc DL = DebugLoc::getUnknownLoc();
00922   if (I != MBB.end()) DL = I->getDebugLoc();
00923 
00924   if (SubIdx && TargetRegisterInfo::isPhysicalRegister(DestReg)) {
00925     DestReg = RI.getSubReg(DestReg, SubIdx);
00926     SubIdx = 0;
00927   }
00928 
00929   // MOV32r0 etc. are implemented with xor which clobbers condition code.
00930   // Re-materialize them as movri instructions to avoid side effects.
00931   bool Clone = true;
00932   unsigned Opc = Orig->getOpcode();
00933   switch (Opc) {
00934   default: break;
00935   case X86::MOV8r0:
00936   case X86::MOV16r0:
00937   case X86::MOV32r0: {
00938     if (!isSafeToClobberEFLAGS(MBB, I)) {
00939       switch (Opc) {
00940       default: break;
00941       case X86::MOV8r0:  Opc = X86::MOV8ri;  break;
00942       case X86::MOV16r0: Opc = X86::MOV16ri; break;
00943       case X86::MOV32r0: Opc = X86::MOV32ri; break;
00944       }
00945       Clone = false;
00946     }
00947     break;
00948   }
00949   }
00950 
00951   if (Clone) {
00952     MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
00953     MI->getOperand(0).setReg(DestReg);
00954     MBB.insert(I, MI);
00955   } else {
00956     BuildMI(MBB, I, DL, get(Opc), DestReg).addImm(0);
00957   }
00958 
00959   MachineInstr *NewMI = prior(I);
00960   NewMI->getOperand(0).setSubReg(SubIdx);
00961 }
00962 
00963 /// isInvariantLoad - Return true if the specified instruction (which is marked
00964 /// mayLoad) is loading from a location whose value is invariant across the
00965 /// function.  For example, loading a value from the constant pool or from
00966 /// from the argument area of a function if it does not change.  This should
00967 /// only return true of *all* loads the instruction does are invariant (if it
00968 /// does multiple loads).
00969 bool X86InstrInfo::isInvariantLoad(const MachineInstr *MI) const {
00970   // This code cares about loads from three cases: constant pool entries,
00971   // invariant argument slots, and global stubs.  In order to handle these cases
00972   // for all of the myriad of X86 instructions, we just scan for a CP/FI/GV
00973   // operand and base our analysis on it.  This is safe because the address of
00974   // none of these three cases is ever used as anything other than a load base
00975   // and X86 doesn't have any instructions that load from multiple places.
00976   
00977   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
00978     const MachineOperand &MO = MI->getOperand(i);
00979     // Loads from constant pools are trivially invariant.
00980     if (MO.isCPI())
00981       return true;
00982 
00983     if (MO.isGlobal())
00984       return isGlobalStubReference(MO.getTargetFlags());
00985 
00986     // If this is a load from an invariant stack slot, the load is a constant.
00987     if (MO.isFI()) {
00988       const MachineFrameInfo &MFI =
00989         *MI->getParent()->getParent()->getFrameInfo();
00990       int Idx = MO.getIndex();
00991       return MFI.isFixedObjectIndex(Idx) && MFI.isImmutableObjectIndex(Idx);
00992     }
00993   }
00994   
00995   // All other instances of these instructions are presumed to have other
00996   // issues.
00997   return false;
00998 }
00999 
01000 /// hasLiveCondCodeDef - True if MI has a condition code def, e.g. EFLAGS, that
01001 /// is not marked dead.
01002 static bool hasLiveCondCodeDef(MachineInstr *MI) {
01003   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
01004     MachineOperand &MO = MI->getOperand(i);
01005     if (MO.isReg() && MO.isDef() &&
01006         MO.getReg() == X86::EFLAGS && !MO.isDead()) {
01007       return true;
01008     }
01009   }
01010   return false;
01011 }
01012 
01013 /// convertToThreeAddress - This method must be implemented by targets that
01014 /// set the M_CONVERTIBLE_TO_3_ADDR flag.  When this flag is set, the target
01015 /// may be able to convert a two-address instruction into a true
01016 /// three-address instruction on demand.  This allows the X86 target (for
01017 /// example) to convert ADD and SHL instructions into LEA instructions if they
01018 /// would require register copies due to two-addressness.
01019 ///
01020 /// This method returns a null pointer if the transformation cannot be
01021 /// performed, otherwise it returns the new instruction.
01022 ///
01023 MachineInstr *
01024 X86InstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
01025                                     MachineBasicBlock::iterator &MBBI,
01026                                     LiveVariables *LV) const {
01027   MachineInstr *MI = MBBI;
01028   MachineFunction &MF = *MI->getParent()->getParent();
01029   // All instructions input are two-addr instructions.  Get the known operands.
01030   unsigned Dest = MI->getOperand(0).getReg();
01031   unsigned Src = MI->getOperand(1).getReg();
01032   bool isDead = MI->getOperand(0).isDead();
01033   bool isKill = MI->getOperand(1).isKill();
01034 
01035   MachineInstr *NewMI = NULL;
01036   // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's.  When
01037   // we have better subtarget support, enable the 16-bit LEA generation here.
01038   bool DisableLEA16 = true;
01039 
01040   unsigned MIOpc = MI->getOpcode();
01041   switch (MIOpc) {
01042   case X86::SHUFPSrri: {
01043     assert(MI->getNumOperands() == 4 && "Unknown shufps instruction!");
01044     if (!TM.getSubtarget<X86Subtarget>().hasSSE2()) return 0;
01045     
01046     unsigned B = MI->getOperand(1).getReg();
01047     unsigned C = MI->getOperand(2).getReg();
01048     if (B != C) return 0;
01049     unsigned A = MI->getOperand(0).getReg();
01050     unsigned M = MI->getOperand(3).getImm();
01051     NewMI = BuildMI(MF, MI->getDebugLoc(), get(X86::PSHUFDri))
01052       .addReg(A, RegState::Define | getDeadRegState(isDead))
01053       .addReg(B, getKillRegState(isKill)).addImm(M);
01054     break;
01055   }
01056   case X86::SHL64ri: {
01057     assert(MI->getNumOperands() >= 3 && "Unknown shift instruction!");
01058     // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
01059     // the flags produced by a shift yet, so this is safe.
01060     unsigned ShAmt = MI->getOperand(2).getImm();
01061     if (ShAmt == 0 || ShAmt >= 4) return 0;
01062 
01063     NewMI = BuildMI(MF, MI->getDebugLoc(), get(X86::LEA64r))
01064       .addReg(Dest, RegState::Define | getDeadRegState(isDead))
01065       .addReg(0).addImm(1 << ShAmt)
01066       .addReg(Src, getKillRegState(isKill))
01067       .addImm(0);
01068     break;
01069   }
01070   case X86::SHL32ri: {
01071     assert(MI->getNumOperands() >= 3 && "Unknown shift instruction!");
01072     // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
01073     // the flags produced by a shift yet, so this is safe.
01074     unsigned ShAmt = MI->getOperand(2).getImm();
01075     if (ShAmt == 0 || ShAmt >= 4) return 0;
01076 
01077     unsigned Opc = TM.getSubtarget<X86Subtarget>().is64Bit() ?
01078       X86::LEA64_32r : X86::LEA32r;
01079     NewMI = BuildMI(MF, MI->getDebugLoc(), get(Opc))
01080       .addReg(Dest, RegState::Define | getDeadRegState(isDead))
01081       .addReg(0).addImm(1 << ShAmt)
01082       .addReg(Src, getKillRegState(isKill)).addImm(0);
01083     break;
01084   }
01085   case X86::SHL16ri: {
01086     assert(MI->getNumOperands() >= 3 && "Unknown shift instruction!");
01087     // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
01088     // the flags produced by a shift yet, so this is safe.
01089     unsigned ShAmt = MI->getOperand(2).getImm();
01090     if (ShAmt == 0 || ShAmt >= 4) return 0;
01091 
01092     if (DisableLEA16) {
01093       // If 16-bit LEA is disabled, use 32-bit LEA via subregisters.
01094       MachineRegisterInfo &RegInfo = MFI->getParent()->getRegInfo();
01095       unsigned Opc = TM.getSubtarget<X86Subtarget>().is64Bit()
01096         ? X86::LEA64_32r : X86::LEA32r;
01097       unsigned leaInReg = RegInfo.createVirtualRegister(&X86::GR32RegClass);
01098       unsigned leaOutReg = RegInfo.createVirtualRegister(&X86::GR32RegClass);
01099             
01100       // Build and insert into an implicit UNDEF value. This is OK because
01101       // well be shifting and then extracting the lower 16-bits. 
01102       BuildMI(*MFI, MBBI, MI->getDebugLoc(), get(X86::IMPLICIT_DEF), leaInReg);
01103       MachineInstr *InsMI =
01104         BuildMI(*MFI, MBBI, MI->getDebugLoc(), get(X86::INSERT_SUBREG),leaInReg)
01105         .addReg(leaInReg)
01106         .addReg(Src, getKillRegState(isKill))
01107         .addImm(X86::SUBREG_16BIT);
01108       
01109       NewMI = BuildMI(*MFI, MBBI, MI->getDebugLoc(), get(Opc), leaOutReg)
01110         .addReg(0).addImm(1 << ShAmt)
01111         .addReg(leaInReg, RegState::Kill)
01112         .addImm(0);
01113       
01114       MachineInstr *ExtMI =
01115         BuildMI(*MFI, MBBI, MI->getDebugLoc(), get(X86::EXTRACT_SUBREG))
01116         .addReg(Dest, RegState::Define | getDeadRegState(isDead))
01117         .addReg(leaOutReg, RegState::Kill)
01118         .addImm(X86::SUBREG_16BIT);
01119 
01120       if (LV) {
01121         // Update live variables
01122         LV->getVarInfo(leaInReg).Kills.push_back(NewMI);
01123         LV->getVarInfo(leaOutReg).Kills.push_back(ExtMI);
01124         if (isKill)
01125           LV->replaceKillInstruction(Src, MI, InsMI);
01126         if (isDead)
01127           LV->replaceKillInstruction(Dest, MI, ExtMI);
01128       }
01129       return ExtMI;
01130     } else {
01131       NewMI = BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
01132         .addReg(Dest, RegState::Define | getDeadRegState(isDead))
01133         .addReg(0).addImm(1 << ShAmt)
01134         .addReg(Src, getKillRegState(isKill))
01135         .addImm(0);
01136     }
01137     break;
01138   }
01139   default: {
01140     // The following opcodes also sets the condition code register(s). Only
01141     // convert them to equivalent lea if the condition code register def's
01142     // are dead!
01143     if (hasLiveCondCodeDef(MI))
01144       return 0;
01145 
01146     bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
01147     switch (MIOpc) {
01148     default: return 0;
01149     case X86::INC64r:
01150     case X86::INC32r:
01151     case X86::INC64_32r: {
01152       assert(MI->getNumOperands() >= 2 && "Unknown inc instruction!");
01153       unsigned Opc = MIOpc == X86::INC64r ? X86::LEA64r
01154         : (is64Bit ? X86::LEA64_32r : X86::LEA32r);
01155       NewMI = addLeaRegOffset(BuildMI(MF, MI->getDebugLoc(), get(Opc))
01156                               .addReg(Dest, RegState::Define |
01157                                       getDeadRegState(isDead)),
01158                               Src, isKill, 1);
01159       break;
01160     }
01161     case X86::INC16r:
01162     case X86::INC64_16r:
01163       if (DisableLEA16) return 0;
01164       assert(MI->getNumOperands() >= 2 && "Unknown inc instruction!");
01165       NewMI = addRegOffset(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
01166                            .addReg(Dest, RegState::Define |
01167                                    getDeadRegState(isDead)),
01168                            Src, isKill, 1);
01169       break;
01170     case X86::DEC64r:
01171     case X86::DEC32r:
01172     case X86::DEC64_32r: {
01173       assert(MI->getNumOperands() >= 2 && "Unknown dec instruction!");
01174       unsigned Opc = MIOpc == X86::DEC64r ? X86::LEA64r
01175         : (is64Bit ? X86::LEA64_32r : X86::LEA32r);
01176       NewMI = addLeaRegOffset(BuildMI(MF, MI->getDebugLoc(), get(Opc))
01177                               .addReg(Dest, RegState::Define |
01178                                       getDeadRegState(isDead)),
01179                               Src, isKill, -1);
01180       break;
01181     }
01182     case X86::DEC16r:
01183     case X86::DEC64_16r:
01184       if (DisableLEA16) return 0;
01185       assert(MI->getNumOperands() >= 2 && "Unknown dec instruction!");
01186       NewMI = addRegOffset(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
01187                            .addReg(Dest, RegState::Define |
01188                                    getDeadRegState(isDead)),
01189                            Src, isKill, -1);
01190       break;
01191     case X86::ADD64rr:
01192     case X86::ADD32rr: {
01193       assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
01194       unsigned Opc = MIOpc == X86::ADD64rr ? X86::LEA64r
01195         : (is64Bit ? X86::LEA64_32r : X86::LEA32r);
01196       unsigned Src2 = MI->getOperand(2).getReg();
01197       bool isKill2 = MI->getOperand(2).isKill();
01198       NewMI = addRegReg(BuildMI(MF, MI->getDebugLoc(), get(Opc))
01199                         .addReg(Dest, RegState::Define |
01200                                 getDeadRegState(isDead)),
01201                         Src, isKill, Src2, isKill2);
01202       if (LV && isKill2)
01203         LV->replaceKillInstruction(Src2, MI, NewMI);
01204       break;
01205     }
01206     case X86::ADD16rr: {
01207       if (DisableLEA16) return 0;
01208       assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
01209       unsigned Src2 = MI->getOperand(2).getReg();
01210       bool isKill2 = MI->getOperand(2).isKill();
01211       NewMI = addRegReg(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
01212                         .addReg(Dest, RegState::Define |
01213                                 getDeadRegState(isDead)),
01214                         Src, isKill, Src2, isKill2);
01215       if (LV && isKill2)
01216         LV->replaceKillInstruction(Src2, MI, NewMI);
01217       break;
01218     }
01219     case X86::ADD64ri32:
01220     case X86::ADD64ri8:
01221       assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
01222       if (MI->getOperand(2).isImm())
01223         NewMI = addLeaRegOffset(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA64r))
01224                                 .addReg(Dest, RegState::Define |
01225                                         getDeadRegState(isDead)),
01226                                 Src, isKill, MI->getOperand(2).getImm());
01227       break;
01228     case X86::ADD32ri:
01229     case X86::ADD32ri8:
01230       assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
01231       if (MI->getOperand(2).isImm()) {
01232         unsigned Opc = is64Bit ? X86::LEA64_32r : X86::LEA32r;
01233         NewMI = addLeaRegOffset(BuildMI(MF, MI->getDebugLoc(), get(Opc))
01234                                 .addReg(Dest, RegState::Define |
01235                                         getDeadRegState(isDead)),
01236                                 Src, isKill, MI->getOperand(2).getImm());
01237       }
01238       break;
01239     case X86::ADD16ri:
01240     case X86::ADD16ri8:
01241       if (DisableLEA16) return 0;
01242       assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
01243       if (MI->getOperand(2).isImm())
01244         NewMI = addRegOffset(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
01245                              .addReg(Dest, RegState::Define |
01246                                      getDeadRegState(isDead)),
01247                              Src, isKill, MI->getOperand(2).getImm());
01248       break;
01249     case X86::SHL16ri:
01250       if (DisableLEA16) return 0;
01251     case X86::SHL32ri:
01252     case X86::SHL64ri: {
01253       assert(MI->getNumOperands() >= 3 && MI->getOperand(2).isImm() &&
01254              "Unknown shl instruction!");
01255       unsigned ShAmt = MI->getOperand(2).getImm();
01256       if (ShAmt == 1 || ShAmt == 2 || ShAmt == 3) {
01257         X86AddressMode AM;
01258         AM.Scale = 1 << ShAmt;
01259         AM.IndexReg = Src;
01260         unsigned Opc = MIOpc == X86::SHL64ri ? X86::LEA64r
01261           : (MIOpc == X86::SHL32ri
01262              ? (is64Bit ? X86::LEA64_32r : X86::LEA32r) : X86::LEA16r);
01263         NewMI = addFullAddress(BuildMI(MF, MI->getDebugLoc(), get(Opc))
01264                                .addReg(Dest, RegState::Define |
01265                                        getDeadRegState(isDead)), AM);
01266         if (isKill)
01267           NewMI->getOperand(3).setIsKill(true);
01268       }
01269       break;
01270     }
01271     }
01272   }
01273   }
01274 
01275   if (!NewMI) return 0;
01276 
01277   if (LV) {  // Update live variables
01278     if (isKill)
01279       LV->replaceKillInstruction(Src, MI, NewMI);
01280     if (isDead)
01281       LV->replaceKillInstruction(Dest, MI, NewMI);
01282   }
01283 
01284   MFI->insert(MBBI, NewMI);          // Insert the new inst    
01285   return NewMI;
01286 }
01287 
01288 /// commuteInstruction - We have a few instructions that must be hacked on to
01289 /// commute them.
01290 ///
01291 MachineInstr *
01292 X86InstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
01293   switch (MI->getOpcode()) {
01294   case X86::SHRD16rri8: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I)
01295   case X86::SHLD16rri8: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I)
01296   case X86::SHRD32rri8: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I)
01297   case X86::SHLD32rri8: // A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I)
01298   case X86::SHRD64rri8: // A = SHRD64rri8 B, C, I -> A = SHLD64rri8 C, B, (64-I)
01299   case X86::SHLD64rri8:{// A = SHLD64rri8 B, C, I -> A = SHRD64rri8 C, B, (64-I)
01300     unsigned Opc;
01301     unsigned Size;
01302     switch (MI->getOpcode()) {
01303     default: llvm_unreachable("Unreachable!");
01304     case X86::SHRD16rri8: Size = 16; Opc = X86::SHLD16rri8; break;
01305     case X86::SHLD16rri8: Size = 16; Opc = X86::SHRD16rri8; break;
01306     case X86::SHRD32rri8: Size = 32; Opc = X86::SHLD32rri8; break;
01307     case X86::SHLD32rri8: Size = 32; Opc = X86::SHRD32rri8; break;
01308     case X86::SHRD64rri8: Size = 64; Opc = X86::SHLD64rri8; break;
01309     case X86::SHLD64rri8: Size = 64; Opc = X86::SHRD64rri8; break;
01310     }
01311     unsigned Amt = MI->getOperand(3).getImm();
01312     if (NewMI) {
01313       MachineFunction &MF = *MI->getParent()->getParent();
01314       MI = MF.CloneMachineInstr(MI);
01315       NewMI = false;
01316     }
01317     MI->setDesc(get(Opc));
01318     MI->getOperand(3).setImm(Size-Amt);
01319     return TargetInstrInfoImpl::commuteInstruction(MI, NewMI);
01320   }
01321   case X86::CMOVB16rr:
01322   case X86::CMOVB32rr:
01323   case X86::CMOVB64rr:
01324   case X86::CMOVAE16rr:
01325   case X86::CMOVAE32rr:
01326   case X86::CMOVAE64rr:
01327   case X86::CMOVE16rr:
01328   case X86::CMOVE32rr:
01329   case X86::CMOVE64rr:
01330   case X86::CMOVNE16rr:
01331   case X86::CMOVNE32rr:
01332   case X86::CMOVNE64rr:
01333   case X86::CMOVBE16rr:
01334   case X86::CMOVBE32rr:
01335   case X86::CMOVBE64rr:
01336   case X86::CMOVA16rr:
01337   case X86::CMOVA32rr:
01338   case X86::CMOVA64rr:
01339   case X86::CMOVL16rr:
01340   case X86::CMOVL32rr:
01341   case X86::CMOVL64rr:
01342   case X86::CMOVGE16rr:
01343   case X86::CMOVGE32rr:
01344   case X86::CMOVGE64rr:
01345   case X86::CMOVLE16rr:
01346   case X86::CMOVLE32rr:
01347   case X86::CMOVLE64rr:
01348   case X86::CMOVG16rr:
01349   case X86::CMOVG32rr:
01350   case X86::CMOVG64rr:
01351   case X86::CMOVS16rr:
01352   case X86::CMOVS32rr:
01353   case X86::CMOVS64rr:
01354   case X86::CMOVNS16rr:
01355   case X86::CMOVNS32rr:
01356   case X86::CMOVNS64rr:
01357   case X86::CMOVP16rr:
01358   case X86::CMOVP32rr:
01359   case X86::CMOVP64rr:
01360   case X86::CMOVNP16rr:
01361   case X86::CMOVNP32rr:
01362   case X86::CMOVNP64rr:
01363   case X86::CMOVO16rr:
01364   case X86::CMOVO32rr:
01365   case X86::CMOVO64rr:
01366   case X86::CMOVNO16rr:
01367   case X86::CMOVNO32rr:
01368   case X86::CMOVNO64rr: {
01369     unsigned Opc = 0;
01370     switch (MI->getOpcode()) {
01371     default: break;
01372     case X86::CMOVB16rr:  Opc = X86::CMOVAE16rr; break;
01373     case X86::CMOVB32rr:  Opc = X86::CMOVAE32rr; break;
01374     case X86::CMOVB64rr:  Opc = X86::CMOVAE64rr; break;
01375     case X86::CMOVAE16rr: Opc = X86::CMOVB16rr; break;
01376     case X86::CMOVAE32rr: Opc = X86::CMOVB32rr; break;
01377     case X86::CMOVAE64rr: Opc = X86::CMOVB64rr; break;
01378     case X86::CMOVE16rr:  Opc = X86::CMOVNE16rr; break;
01379     case X86::CMOVE32rr:  Opc = X86::CMOVNE32rr; break;
01380     case X86::CMOVE64rr:  Opc = X86::CMOVNE64rr; break;
01381     case X86::CMOVNE16rr: Opc = X86::CMOVE16rr; break;
01382     case X86::CMOVNE32rr: Opc = X86::CMOVE32rr; break;
01383     case X86::CMOVNE64rr: Opc = X86::CMOVE64rr; break;
01384     case X86::CMOVBE16rr: Opc = X86::CMOVA16rr; break;
01385     case X86::CMOVBE32rr: Opc = X86::CMOVA32rr; break;
01386     case X86::CMOVBE64rr: Opc = X86::CMOVA64rr; break;
01387     case X86::CMOVA16rr:  Opc = X86::CMOVBE16rr; break;
01388     case X86::CMOVA32rr:  Opc = X86::CMOVBE32rr; break;
01389     case X86::CMOVA64rr:  Opc = X86::CMOVBE64rr; break;
01390     case X86::CMOVL16rr:  Opc = X86::CMOVGE16rr; break;
01391     case X86::CMOVL32rr:  Opc = X86::CMOVGE32rr; break;
01392     case X86::CMOVL64rr:  Opc = X86::CMOVGE64rr; break;
01393     case X86::CMOVGE16rr: Opc = X86::CMOVL16rr; break;
01394     case X86::CMOVGE32rr: Opc = X86::CMOVL32rr; break;
01395     case X86::CMOVGE64rr: Opc = X86::CMOVL64rr; break;
01396     case X86::CMOVLE16rr: Opc = X86::CMOVG16rr; break;
01397     case X86::CMOVLE32rr: Opc = X86::CMOVG32rr; break;
01398     case X86::CMOVLE64rr: Opc = X86::CMOVG64rr; break;
01399     case X86::CMOVG16rr:  Opc = X86::CMOVLE16rr; break;
01400     case X86::CMOVG32rr:  Opc = X86::CMOVLE32rr; break;
01401     case X86::CMOVG64rr:  Opc = X86::CMOVLE64rr; break;
01402     case X86::CMOVS16rr:  Opc = X86::CMOVNS16rr; break;
01403     case X86::CMOVS32rr:  Opc = X86::CMOVNS32rr; break;
01404     case X86::CMOVS64rr:  Opc = X86::CMOVNS64rr; break;
01405     case X86::CMOVNS16rr: Opc = X86::CMOVS16rr; break;
01406     case X86::CMOVNS32rr: Opc = X86::CMOVS32rr; break;
01407     case X86::CMOVNS64rr: Opc = X86::CMOVS64rr; break;
01408     case X86::CMOVP16rr:  Opc = X86::CMOVNP16rr; break;
01409     case X86::CMOVP32rr:  Opc = X86::CMOVNP32rr; break;
01410     case X86::CMOVP64rr:  Opc = X86::CMOVNP64rr; break;
01411     case X86::CMOVNP16rr: Opc = X86::CMOVP16rr; break;
01412     case X86::CMOVNP32rr: Opc = X86::CMOVP32rr; break;
01413     case X86::CMOVNP64rr: Opc = X86::CMOVP64rr; break;
01414     case X86::CMOVO16rr:  Opc = X86::CMOVNO16rr; break;
01415     case X86::CMOVO32rr:  Opc = X86::CMOVNO32rr; break;
01416     case X86::CMOVO64rr:  Opc = X86::CMOVNO64rr; break;
01417     case X86::CMOVNO16rr: Opc = X86::CMOVO16rr; break;
01418     case X86::CMOVNO32rr: Opc = X86::CMOVO32rr; break;
01419     case X86::CMOVNO64rr: Opc = X86::CMOVO64rr; break;
01420     }
01421     if (NewMI) {
01422       MachineFunction &MF = *MI->getParent()->getParent();
01423       MI = MF.CloneMachineInstr(MI);
01424       NewMI = false;
01425     }
01426     MI->setDesc(get(Opc));
01427     // Fallthrough intended.
01428   }
01429   default:
01430     return TargetInstrInfoImpl::commuteInstruction(MI, NewMI);
01431   }
01432 }
01433 
01434 static X86::CondCode GetCondFromBranchOpc(unsigned BrOpc) {
01435   switch (BrOpc) {
01436   default: return X86::COND_INVALID;
01437   case X86::JE:  return X86::COND_E;
01438   case X86::JNE: return X86::COND_NE;
01439   case X86::JL:  return X86::COND_L;
01440   case X86::JLE: return X86::COND_LE;
01441   case X86::JG:  return X86::COND_G;
01442   case X86::JGE: return X86::COND_GE;
01443   case X86::JB:  return X86::COND_B;
01444   case X86::JBE: return X86::COND_BE;
01445   case X86::JA:  return X86::COND_A;
01446   case X86::JAE: return X86::COND_AE;
01447   case X86::JS:  return X86::COND_S;
01448   case X86::JNS: return X86::COND_NS;
01449   case X86::JP:  return X86::COND_P;
01450   case X86::JNP: return X86::COND_NP;
01451   case X86::JO:  return X86::COND_O;
01452   case X86::JNO: return X86::COND_NO;
01453   }
01454 }
01455 
01456 unsigned X86::GetCondBranchFromCond(X86::CondCode CC) {
01457   switch (CC) {
01458   default: llvm_unreachable("Illegal condition code!");
01459   case X86::COND_E:  return X86::JE;
01460   case X86::COND_NE: return X86::JNE;
01461   case X86::COND_L:  return X86::JL;
01462   case X86::COND_LE: return X86::JLE;
01463   case X86::COND_G:  return X86::JG;
01464   case X86::COND_GE: return X86::JGE;
01465   case X86::COND_B:  return X86::JB;
01466   case X86::COND_BE: return X86::JBE;
01467   case X86::COND_A:  return X86::JA;
01468   case X86::COND_AE: return X86::JAE;
01469   case X86::COND_S:  return X86::JS;
01470   case X86::COND_NS: return X86::JNS;
01471   case X86::COND_P:  return X86::JP;
01472   case X86::COND_NP: return X86::JNP;
01473   case X86::COND_O:  return X86::JO;
01474   case X86::COND_NO: return X86::JNO;
01475   }
01476 }
01477 
01478 /// GetOppositeBranchCondition - Return the inverse of the specified condition,
01479 /// e.g. turning COND_E to COND_NE.
01480 X86::CondCode X86::GetOppositeBranchCondition(X86::CondCode CC) {
01481   switch (CC) {
01482   default: llvm_unreachable("Illegal condition code!");
01483   case X86::COND_E:  return X86::COND_NE;
01484   case X86::COND_NE: return X86::COND_E;
01485   case X86::COND_L:  return X86::COND_GE;
01486   case X86::COND_LE: return X86::COND_G;
01487   case X86::COND_G:  return X86::COND_LE;
01488   case X86::COND_GE: return X86::COND_L;
01489   case X86::COND_B:  return X86::COND_AE;
01490   case X86::COND_BE: return X86::COND_A;
01491   case X86::COND_A:  return X86::COND_BE;
01492   case X86::COND_AE: return X86::COND_B;
01493   case X86::COND_S:  return X86::COND_NS;
01494   case X86::COND_NS: return X86::COND_S;
01495   case X86::COND_P:  return X86::COND_NP;
01496   case X86::COND_NP: return X86::COND_P;
01497   case X86::COND_O:  return X86::COND_NO;
01498   case X86::COND_NO: return X86::COND_O;
01499   }
01500 }
01501 
01502 bool X86InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
01503   const TargetInstrDesc &TID = MI->getDesc();
01504   if (!TID.isTerminator()) return false;
01505   
01506   // Conditional branch is a special case.
01507   if (TID.isBranch() && !TID.isBarrier())
01508     return true;
01509   if (!TID.isPredicable())
01510     return true;
01511   return !isPredicated(MI);
01512 }
01513 
01514 // For purposes of branch analysis do not count FP_REG_KILL as a terminator.
01515 static bool isBrAnalysisUnpredicatedTerminator(const MachineInstr *MI,
01516                                                const X86InstrInfo &TII) {
01517   if (MI->getOpcode() == X86::FP_REG_KILL)
01518     return false;
01519   return TII.isUnpredicatedTerminator(MI);
01520 }
01521 
01522 bool X86InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, 
01523                                  MachineBasicBlock *&TBB,
01524                                  MachineBasicBlock *&FBB,
01525                                  SmallVectorImpl<MachineOperand> &Cond,
01526                                  bool AllowModify) const {
01527   // Start from the bottom of the block and work up, examining the
01528   // terminator instructions.
01529   MachineBasicBlock::iterator I = MBB.end();
01530   while (I != MBB.begin()) {
01531     --I;
01532     // Working from the bottom, when we see a non-terminator
01533     // instruction, we're done.
01534     if (!isBrAnalysisUnpredicatedTerminator(I, *this))
01535       break;
01536     // A terminator that isn't a branch can't easily be handled
01537     // by this analysis.
01538     if (!I->getDesc().isBranch())
01539       return true;
01540     // Handle unconditional branches.
01541     if (I->getOpcode() == X86::JMP) {
01542       if (!AllowModify) {
01543         TBB = I->getOperand(0).getMBB();
01544         continue;
01545       }
01546 
01547       // If the block has any instructions after a JMP, delete them.
01548       while (next(I) != MBB.end())
01549         next(I)->eraseFromParent();
01550       Cond.clear();
01551       FBB = 0;
01552       // Delete the JMP if it's equivalent to a fall-through.
01553       if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
01554         TBB = 0;
01555         I->eraseFromParent();
01556         I = MBB.end();
01557         continue;
01558       }
01559       // TBB is used to indicate the unconditinal destination.
01560       TBB = I->getOperand(0).getMBB();
01561       continue;
01562     }
01563     // Handle conditional branches.
01564     X86::CondCode BranchCode = GetCondFromBranchOpc(I->getOpcode());
01565     if (BranchCode == X86::COND_INVALID)
01566       return true;  // Can't handle indirect branch.
01567     // Working from the bottom, handle the first conditional branch.
01568     if (Cond.empty()) {
01569       FBB = TBB;
01570       TBB = I->getOperand(0).getMBB();
01571       Cond.push_back(MachineOperand::CreateImm(BranchCode));
01572       continue;
01573     }
01574     // Handle subsequent conditional branches. Only handle the case
01575     // where all conditional branches branch to the same destination
01576     // and their condition opcodes fit one of the special
01577     // multi-branch idioms.
01578     assert(Cond.size() == 1);
01579     assert(TBB);
01580     // Only handle the case where all conditional branches branch to
01581     // the same destination.
01582     if (TBB != I->getOperand(0).getMBB())
01583       return true;
01584     X86::CondCode OldBranchCode = (X86::CondCode)Cond[0].getImm();
01585     // If the conditions are the same, we can leave them alone.
01586     if (OldBranchCode == BranchCode)
01587       continue;
01588     // If they differ, see if they fit one of the known patterns.
01589     // Theoretically we could handle more patterns here, but
01590     // we shouldn't expect to see them if instruction selection
01591     // has done a reasonable job.
01592     if ((OldBranchCode == X86::COND_NP &&
01593          BranchCode == X86::COND_E) ||
01594         (OldBranchCode == X86::COND_E &&
01595          BranchCode == X86::COND_NP))
01596       BranchCode = X86::COND_NP_OR_E;
01597     else if ((OldBranchCode == X86::COND_P &&
01598               BranchCode == X86::COND_NE) ||
01599              (OldBranchCode == X86::COND_NE &&
01600               BranchCode == X86::COND_P))
01601       BranchCode = X86::COND_NE_OR_P;
01602     else
01603       return true;
01604     // Update the MachineOperand.
01605     Cond[0].setImm(BranchCode);
01606   }
01607 
01608   return false;
01609 }
01610 
01611 unsigned X86InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
01612   MachineBasicBlock::iterator I = MBB.end();
01613   unsigned Count = 0;
01614 
01615   while (I != MBB.begin()) {
01616     --I;
01617     if (I->getOpcode() != X86::JMP &&
01618         GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
01619       break;
01620     // Remove the branch.
01621     I->eraseFromParent();
01622     I = MBB.end();
01623     ++Count;
01624   }
01625   
01626   return Count;
01627 }
01628 
01629 unsigned
01630 X86InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
01631                            MachineBasicBlock *FBB,
01632                            const SmallVectorImpl<MachineOperand> &Cond) const {
01633   // FIXME this should probably have a DebugLoc operand
01634   DebugLoc dl = DebugLoc::getUnknownLoc();
01635   // Shouldn't be a fall through.
01636   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
01637   assert((Cond.size() == 1 || Cond.size() == 0) &&
01638          "X86 branch conditions have one component!");
01639 
01640   if (Cond.empty()) {
01641     // Unconditional branch?
01642     assert(!FBB && "Unconditional branch with multiple successors!");
01643     BuildMI(&MBB, dl, get(X86::JMP)).addMBB(TBB);
01644     return 1;
01645   }
01646 
01647   // Conditional branch.
01648   unsigned Count = 0;
01649   X86::CondCode CC = (X86::CondCode)Cond[0].getImm();
01650   switch (CC) {
01651   case X86::COND_NP_OR_E:
01652     // Synthesize NP_OR_E with two branches.
01653     BuildMI(&MBB, dl, get(X86::JNP)).addMBB(TBB);
01654     ++Count;
01655     BuildMI(&MBB, dl, get(X86::JE)).addMBB(TBB);
01656     ++Count;
01657     break;
01658   case X86::COND_NE_OR_P:
01659     // Synthesize NE_OR_P with two branches.
01660     BuildMI(&MBB, dl, get(X86::JNE)).addMBB(TBB);
01661     ++Count;
01662     BuildMI(&MBB, dl, get(X86::JP)).addMBB(TBB);
01663     ++Count;
01664     break;
01665   default: {
01666     unsigned Opc = GetCondBranchFromCond(CC);
01667     BuildMI(&MBB, dl, get(Opc)).addMBB(TBB);
01668     ++Count;
01669   }
01670   }
01671   if (FBB) {
01672     // Two-way Conditional branch. Insert the second branch.
01673     BuildMI(&MBB, dl, get(X86::JMP)).addMBB(FBB);
01674     ++Count;
01675   }
01676   return Count;
01677 }
01678 
01679 /// isHReg - Test if the given register is a physical h register.
01680 static bool isHReg(unsigned Reg) {
01681   return X86::GR8_ABCD_HRegClass.contains(Reg);
01682 }
01683 
01684 bool X86InstrInfo::copyRegToReg(MachineBasicBlock &MBB,
01685                                 MachineBasicBlock::iterator MI,
01686                                 unsigned DestReg, unsigned SrcReg,
01687                                 const TargetRegisterClass *DestRC,
01688                                 const TargetRegisterClass *SrcRC) const {
01689   DebugLoc DL = DebugLoc::getUnknownLoc();
01690   if (MI != MBB.end()) DL = MI->getDebugLoc();
01691 
01692   // Determine if DstRC and SrcRC have a common superclass in common.
01693   const TargetRegisterClass *CommonRC = DestRC;
01694   if (DestRC == SrcRC)
01695     /* Source and destination have the same register class. */;
01696   else if (CommonRC->hasSuperClass(SrcRC))
01697     CommonRC = SrcRC;
01698   else if (!DestRC->hasSubClass(SrcRC)) {
01699     // Neither of GR64_NOREX or GR64_NOSP is a superclass of the other,
01700     // but we want to copy then as GR64. Similarly, for GR32_NOREX and
01701     // GR32_NOSP, copy as GR32.
01702     if (SrcRC->hasSuperClass(&X86::GR64RegClass) &&
01703         DestRC->hasSuperClass(&X86::GR64RegClass))
01704       CommonRC = &X86::GR64RegClass;
01705     else if (SrcRC->hasSuperClass(&X86::GR32RegClass) &&
01706              DestRC->hasSuperClass(&X86::GR32RegClass))
01707       CommonRC = &X86::GR32RegClass;
01708     else
01709       CommonRC = 0;
01710   }
01711 
01712   if (CommonRC) {
01713     unsigned Opc;
01714     if (CommonRC == &X86::GR64RegClass || CommonRC == &X86::GR64_NOSPRegClass) {
01715       Opc = X86::MOV64rr;
01716     } else if (CommonRC == &X86::GR32RegClass ||
01717                CommonRC == &X86::GR32_NOSPRegClass) {
01718       Opc = X86::MOV32rr;
01719     } else if (CommonRC == &X86::GR16RegClass) {
01720       Opc = X86::MOV16rr;
01721     } else if (CommonRC == &X86::GR8RegClass) {
01722       // Copying to or from a physical H register on x86-64 requires a NOREX
01723       // move.  Otherwise use a normal move.
01724       if ((isHReg(DestReg) || isHReg(SrcReg)) &&
01725           TM.getSubtarget<X86Subtarget>().is64Bit())
01726         Opc = X86::MOV8rr_NOREX;
01727       else
01728         Opc = X86::MOV8rr;
01729     } else if (CommonRC == &X86::GR64_ABCDRegClass) {
01730       Opc = X86::MOV64rr;
01731     } else if (CommonRC == &X86::GR32_ABCDRegClass) {
01732       Opc = X86::MOV32rr;
01733     } else if (CommonRC == &X86::GR16_ABCDRegClass) {
01734       Opc = X86::MOV16rr;
01735     } else if (CommonRC == &X86::GR8_ABCD_LRegClass) {
01736       Opc = X86::MOV8rr;
01737     } else if (CommonRC == &X86::GR8_ABCD_HRegClass) {
01738       if (TM.getSubtarget<X86Subtarget>().is64Bit())
01739         Opc = X86::MOV8rr_NOREX;
01740       else
01741         Opc = X86::MOV8rr;
01742     } else if (CommonRC == &X86::GR64_NOREXRegClass ||
01743                CommonRC == &X86::GR64_NOREX_NOSPRegClass) {
01744       Opc = X86::MOV64rr;
01745     } else if (CommonRC == &X86::GR32_NOREXRegClass) {
01746       Opc = X86::MOV32rr;
01747     } else if (CommonRC == &X86::GR16_NOREXRegClass) {
01748       Opc = X86::MOV16rr;
01749     } else if (CommonRC == &X86::GR8_NOREXRegClass) {
01750       Opc = X86::MOV8rr;
01751     } else if (CommonRC == &X86::RFP32RegClass) {
01752       Opc = X86::MOV_Fp3232;
01753     } else if (CommonRC == &X86::RFP64RegClass || CommonRC == &X86::RSTRegClass) {
01754       Opc = X86::MOV_Fp6464;
01755     } else if (CommonRC == &X86::RFP80RegClass) {
01756       Opc = X86::MOV_Fp8080;
01757     } else if (CommonRC == &X86::FR32RegClass) {
01758       Opc = X86::FsMOVAPSrr;
01759     } else if (CommonRC == &X86::FR64RegClass) {
01760       Opc = X86::FsMOVAPDrr;
01761     } else if (CommonRC == &X86::VR128RegClass) {
01762       Opc = X86::MOVAPSrr;
01763     } else if (CommonRC == &X86::VR64RegClass) {
01764       Opc = X86::MMX_MOVQ64rr;
01765     } else {
01766       return false;
01767     }
01768     BuildMI(MBB, MI, DL, get(Opc), DestReg).addReg(SrcReg);
01769     return true;
01770   }
01771 
01772   // Moving EFLAGS to / from another register requires a push and a pop.
01773   if (SrcRC == &X86::CCRRegClass) {
01774     if (SrcReg != X86::EFLAGS)
01775       return false;
01776     if (DestRC == &X86::GR64RegClass || DestRC == &X86::GR64_NOSPRegClass) {
01777       BuildMI(MBB, MI, DL, get(X86::PUSHFQ));
01778       BuildMI(MBB, MI, DL, get(X86::POP64r), DestReg);
01779       return true;
01780     } else if (DestRC == &X86::GR32RegClass ||
01781                DestRC == &X86::GR32_NOSPRegClass) {
01782       BuildMI(MBB, MI, DL, get(X86::PUSHFD));
01783       BuildMI(MBB, MI, DL, get(X86::POP32r), DestReg);
01784       return true;
01785     }
01786   } else if (DestRC == &X86::CCRRegClass) {
01787     if (DestReg != X86::EFLAGS)
01788       return false;
01789     if (SrcRC == &X86::GR64RegClass || DestRC == &X86::GR64_NOSPRegClass) {
01790       BuildMI(MBB, MI, DL, get(X86::PUSH64r)).addReg(SrcReg);
01791       BuildMI(MBB, MI, DL, get(X86::POPFQ));
01792       return true;
01793     } else if (SrcRC == &X86::GR32RegClass ||
01794                DestRC == &X86::GR32_NOSPRegClass) {
01795       BuildMI(MBB, MI, DL, get(X86::PUSH32r)).addReg(SrcReg);
01796       BuildMI(MBB, MI, DL, get(X86::POPFD));
01797       return true;
01798     }
01799   }
01800 
01801   // Moving from ST(0) turns into FpGET_ST0_32 etc.
01802   if (SrcRC == &X86::RSTRegClass) {
01803     // Copying from ST(0)/ST(1).
01804     if (SrcReg != X86::ST0 && SrcReg != X86::ST1)
01805       // Can only copy from ST(0)/ST(1) right now
01806       return false;
01807     bool isST0 = SrcReg == X86::ST0;
01808     unsigned Opc;
01809     if (DestRC == &X86::RFP32RegClass)
01810       Opc = isST0 ? X86::FpGET_ST0_32 : X86::FpGET_ST1_32;
01811     else if (DestRC == &X86::RFP64RegClass)
01812       Opc = isST0 ? X86::FpGET_ST0_64 : X86::FpGET_ST1_64;
01813     else {
01814       if (DestRC != &X86::RFP80RegClass)
01815         return false;
01816       Opc = isST0 ? X86::FpGET_ST0_80 : X86::FpGET_ST1_80;
01817     }
01818     BuildMI(MBB, MI, DL, get(Opc), DestReg);
01819     return true;
01820   }
01821 
01822   // Moving to ST(0) turns into FpSET_ST0_32 etc.
01823   if (DestRC == &X86::RSTRegClass) {
01824     // Copying to ST(0) / ST(1).
01825     if (DestReg != X86::ST0 && DestReg != X86::ST1)
01826       // Can only copy to TOS right now
01827       return false;
01828     bool isST0 = DestReg == X86::ST0;
01829     unsigned Opc;
01830     if (SrcRC == &X86::RFP32RegClass)
01831       Opc = isST0 ? X86::FpSET_ST0_32 : X86::FpSET_ST1_32;
01832     else if (SrcRC == &X86::RFP64RegClass)
01833       Opc = isST0 ? X86::FpSET_ST0_64 : X86::FpSET_ST1_64;
01834     else {
01835       if (SrcRC != &X86::RFP80RegClass)
01836         return false;
01837       Opc = isST0 ? X86::FpSET_ST0_80 : X86::FpSET_ST1_80;
01838     }
01839     BuildMI(MBB, MI, DL, get(Opc)).addReg(SrcReg);
01840     return true;
01841   }
01842   
01843   // Not yet supported!
01844   return false;
01845 }
01846 
01847 static unsigned getStoreRegOpcode(unsigned SrcReg,
01848                                   const TargetRegisterClass *RC,
01849                                   bool isStackAligned,
01850                                   TargetMachine &TM) {
01851   unsigned Opc = 0;
01852   if (RC == &X86::GR64RegClass || RC == &X86::GR64_NOSPRegClass) {
01853     Opc = X86::MOV64mr;
01854   } else if (RC == &X86::GR32RegClass || RC == &X86::GR32_NOSPRegClass) {
01855     Opc = X86::MOV32mr;
01856   } else if (RC == &X86::GR16RegClass) {
01857     Opc = X86::MOV16mr;
01858   } else if (RC == &X86::GR8RegClass) {
01859     // Copying to or from a physical H register on x86-64 requires a NOREX
01860     // move.  Otherwise use a normal move.
01861     if (isHReg(SrcReg) &&
01862         TM.getSubtarget<X86Subtarget>().is64Bit())
01863       Opc = X86::MOV8mr_NOREX;
01864     else
01865       Opc = X86::MOV8mr;
01866   } else if (RC == &X86::GR64_ABCDRegClass) {
01867     Opc = X86::MOV64mr;
01868   } else if (RC == &X86::GR32_ABCDRegClass) {
01869     Opc = X86::MOV32mr;
01870   } else if (RC == &X86::GR16_ABCDRegClass) {
01871     Opc = X86::MOV16mr;
01872   } else if (RC == &X86::GR8_ABCD_LRegClass) {
01873     Opc = X86::MOV8mr;
01874   } else if (RC == &X86::GR8_ABCD_HRegClass) {
01875     if (TM.getSubtarget<X86Subtarget>().is64Bit())
01876       Opc = X86::MOV8mr_NOREX;
01877     else
01878       Opc = X86::MOV8mr;
01879   } else if (RC == &X86::GR64_NOREXRegClass ||
01880              RC == &X86::GR64_NOREX_NOSPRegClass) {
01881     Opc = X86::MOV64mr;
01882   } else if (RC == &X86::GR32_NOREXRegClass) {
01883     Opc = X86::MOV32mr;
01884   } else if (RC == &X86::GR16_NOREXRegClass) {
01885     Opc = X86::MOV16mr;
01886   } else if (RC == &X86::GR8_NOREXRegClass) {
01887     Opc = X86::MOV8mr;
01888   } else if (RC == &X86::RFP80RegClass) {
01889     Opc = X86::ST_FpP80m;   // pops
01890   } else if (RC == &X86::RFP64RegClass) {
01891     Opc = X86::ST_Fp64m;
01892   } else if (RC == &X86::RFP32RegClass) {
01893     Opc = X86::ST_Fp32m;
01894   } else if (RC == &X86::FR32RegClass) {
01895     Opc = X86::MOVSSmr;
01896   } else if (RC == &X86::FR64RegClass) {
01897     Opc = X86::MOVSDmr;
01898   } else if (RC == &X86::VR128RegClass) {
01899     // If stack is realigned we can use aligned stores.
01900     Opc = isStackAligned ? X86::MOVAPSmr : X86::MOVUPSmr;
01901   } else if (RC == &X86::VR64RegClass) {
01902     Opc = X86::MMX_MOVQ64mr;
01903   } else {
01904     llvm_unreachable("Unknown regclass");
01905   }
01906 
01907   return Opc;
01908 }
01909 
01910 void X86InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
01911                                        MachineBasicBlock::iterator MI,
01912                                        unsigned SrcReg, bool isKill, int FrameIdx,
01913                                        const TargetRegisterClass *RC) const {
01914   const MachineFunction &MF = *MBB.getParent();
01915   bool isAligned = (RI.getStackAlignment() >= 16) ||
01916     RI.needsStackRealignment(MF);
01917   unsigned Opc = getStoreRegOpcode(SrcReg, RC, isAligned, TM);
01918   DebugLoc DL = DebugLoc::getUnknownLoc();
01919   if (MI != MBB.end()) DL = MI->getDebugLoc();
01920   addFrameReference(BuildMI(MBB, MI, DL, get(Opc)), FrameIdx)
01921     .addReg(SrcReg, getKillRegState(isKill));
01922 }
01923 
01924 void X86InstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
01925                                   bool isKill,
01926                                   SmallVectorImpl<MachineOperand> &Addr,
01927                                   const TargetRegisterClass *RC,
01928                                   SmallVectorImpl<MachineInstr*> &NewMIs) const {
01929   bool isAligned = (RI.getStackAlignment() >= 16) ||
01930     RI.needsStackRealignment(MF);
01931   unsigned Opc = getStoreRegOpcode(SrcReg, RC, isAligned, TM);
01932   DebugLoc DL = DebugLoc::getUnknownLoc();
01933   MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc));
01934   for (unsigned i = 0, e = Addr.size(); i != e; ++i)
01935     MIB.addOperand(Addr[i]);
01936   MIB.addReg(SrcReg, getKillRegState(isKill));
01937   NewMIs.push_back(MIB);
01938 }
01939 
01940 static unsigned getLoadRegOpcode(unsigned DestReg,
01941                                  const TargetRegisterClass *RC,
01942                                  bool isStackAligned,
01943                                  const TargetMachine &TM) {
01944   unsigned Opc = 0;
01945   if (RC == &X86::GR64RegClass || RC == &X86::GR64_NOSPRegClass) {
01946     Opc = X86::MOV64rm;
01947   } else if (RC == &X86::GR32RegClass || RC == &X86::GR32_NOSPRegClass) {
01948     Opc = X86::MOV32rm;
01949   } else if (RC == &X86::GR16RegClass) {
01950     Opc = X86::MOV16rm;
01951   } else if (RC == &X86::GR8RegClass) {
01952     // Copying to or from a physical H register on x86-64 requires a NOREX
01953     // move.  Otherwise use a normal move.
01954     if (isHReg(DestReg) &&
01955         TM.getSubtarget<X86Subtarget>().is64Bit())
01956       Opc = X86::MOV8rm_NOREX;
01957     else
01958       Opc = X86::MOV8rm;
01959   } else if (RC == &X86::GR64_ABCDRegClass) {
01960     Opc = X86::MOV64rm;
01961   } else if (RC == &X86::GR32_ABCDRegClass) {
01962     Opc = X86::MOV32rm;
01963   } else if (RC == &X86::GR16_ABCDRegClass) {
01964     Opc = X86::MOV16rm;
01965   } else if (RC == &X86::GR8_ABCD_LRegClass) {
01966     Opc = X86::MOV8rm;
01967   } else if (RC == &X86::GR8_ABCD_HRegClass) {
01968     if (TM.getSubtarget<X86Subtarget>().is64Bit())
01969       Opc = X86::MOV8rm_NOREX;
01970     else
01971       Opc = X86::MOV8rm;
01972   } else if (RC == &X86::GR64_NOREXRegClass ||
01973              RC == &X86::GR64_NOREX_NOSPRegClass) {
01974     Opc = X86::MOV64rm;
01975   } else if (RC == &X86::GR32_NOREXRegClass) {
01976     Opc = X86::MOV32rm;
01977   } else if (RC == &X86::GR16_NOREXRegClass) {
01978     Opc = X86::MOV16rm;
01979   } else if (RC == &X86::GR8_NOREXRegClass) {
01980     Opc = X86::MOV8rm;
01981   } else if (RC == &X86::RFP80RegClass) {
01982     Opc = X86::LD_Fp80m;
01983   } else if (RC == &X86::RFP64RegClass) {
01984     Opc = X86::LD_Fp64m;
01985   } else if (RC == &X86::RFP32RegClass) {
01986     Opc = X86::LD_Fp32m;
01987   } else if (RC == &X86::FR32RegClass) {
01988     Opc = X86::MOVSSrm;
01989   } else if (RC == &X86::FR64RegClass) {
01990     Opc = X86::MOVSDrm;
01991   } else if (RC == &X86::VR128RegClass) {
01992     // If stack is realigned we can use aligned loads.
01993     Opc = isStackAligned ? X86::MOVAPSrm : X86::MOVUPSrm;
01994   } else if (RC == &X86::VR64RegClass) {
01995     Opc = X86::MMX_MOVQ64rm;
01996   } else {
01997     llvm_unreachable("Unknown regclass");
01998   }
01999 
02000   return Opc;
02001 }
02002 
02003 void X86InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
02004                                         MachineBasicBlock::iterator MI,
02005                                         unsigned DestReg, int FrameIdx,
02006                                         const TargetRegisterClass *RC) const{
02007   const MachineFunction &MF = *MBB.getParent();
02008   bool isAligned = (RI.getStackAlignment() >= 16) ||
02009     RI.needsStackRealignment(MF);
02010   unsigned Opc = getLoadRegOpcode(DestReg, RC, isAligned, TM);
02011   DebugLoc DL = DebugLoc::getUnknownLoc();
02012   if (MI != MBB.end()) DL = MI->getDebugLoc();
02013   addFrameReference(BuildMI(MBB, MI, DL, get(Opc), DestReg), FrameIdx);
02014 }
02015 
02016 void X86InstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
02017                                  SmallVectorImpl<MachineOperand> &Addr,
02018                                  const TargetRegisterClass *RC,
02019                                  SmallVectorImpl<MachineInstr*> &NewMIs) const {
02020   bool isAligned = (RI.getStackAlignment() >= 16) ||
02021     RI.needsStackRealignment(MF);
02022   unsigned Opc = getLoadRegOpcode(DestReg, RC, isAligned, TM);
02023   DebugLoc DL = DebugLoc::getUnknownLoc();
02024   MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc), DestReg);
02025   for (unsigned i = 0, e = Addr.size(); i != e; ++i)
02026     MIB.addOperand(Addr[i]);
02027   NewMIs.push_back(MIB);
02028 }
02029 
02030 bool X86InstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
02031                                              MachineBasicBlock::iterator MI,
02032                                 const std::vector<CalleeSavedInfo> &CSI) const {
02033   if (CSI.empty())
02034     return false;
02035 
02036   DebugLoc DL = DebugLoc::getUnknownLoc();
02037   if (MI != MBB.end()) DL = MI->getDebugLoc();
02038 
02039   bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
02040   bool isWin64 = TM.getSubtarget<X86Subtarget>().isTargetWin64();
02041   unsigned SlotSize = is64Bit ? 8 : 4;
02042 
02043   MachineFunction &MF = *MBB.getParent();
02044   unsigned FPReg = RI.getFrameRegister(MF);
02045   X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
02046   unsigned CalleeFrameSize = 0;
02047   
02048   unsigned Opc = is64Bit ? X86::PUSH64r : X86::PUSH32r;
02049   for (unsigned i = CSI.size(); i != 0; --i) {
02050     unsigned Reg = CSI[i-1].getReg();
02051     const TargetRegisterClass *RegClass = CSI[i-1].getRegClass();
02052     // Add the callee-saved register as live-in. It's killed at the spill.
02053     MBB.addLiveIn(Reg);
02054     if (Reg == FPReg)
02055       // X86RegisterInfo::emitPrologue will handle spilling of frame register.
02056       continue;
02057     if (RegClass != &X86::VR128RegClass && !isWin64) {
02058       CalleeFrameSize += SlotSize;
02059       BuildMI(MBB, MI, DL, get(Opc)).addReg(Reg, RegState::Kill);
02060     } else {
02061       storeRegToStackSlot(MBB, MI, Reg, true, CSI[i-1].getFrameIdx(), RegClass);
02062     }
02063   }
02064 
02065   X86FI->setCalleeSavedFrameSize(CalleeFrameSize);
02066   return true;
02067 }
02068 
02069 bool X86InstrInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
02070                                                MachineBasicBlock::iterator MI,
02071                                 const std::vector<CalleeSavedInfo> &CSI) const {
02072   if (CSI.empty())
02073     return false;
02074 
02075   DebugLoc DL = DebugLoc::getUnknownLoc();
02076   if (MI != MBB.end()) DL = MI->getDebugLoc();
02077 
02078   MachineFunction &MF = *MBB.getParent();
02079   unsigned FPReg = RI.getFrameRegister(MF);
02080   bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
02081   bool isWin64 = TM.getSubtarget<X86Subtarget>().isTargetWin64();
02082   unsigned Opc = is64Bit ? X86::POP64r : X86::POP32r;
02083   for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
02084     unsigned Reg = CSI[i].getReg();
02085     if (Reg == FPReg)
02086       // X86RegisterInfo::emitEpilogue will handle restoring of frame register.
02087       continue;
02088     const TargetRegisterClass *RegClass = CSI[i].getRegClass();
02089     if (RegClass != &X86::VR128RegClass && !isWin64) {
02090       BuildMI(MBB, MI, DL, get(Opc), Reg);
02091     } else {
02092       loadRegFromStackSlot(MBB, MI, Reg, CSI[i].getFrameIdx(), RegClass);
02093     }
02094   }
02095   return true;
02096 }
02097 
02098 static MachineInstr *FuseTwoAddrInst(MachineFunction &MF, unsigned Opcode,
02099                                      const SmallVectorImpl<MachineOperand> &MOs,
02100                                      MachineInstr *MI,
02101                                      const TargetInstrInfo &TII) {
02102   // Create the base instruction with the memory operand as the first part.
02103   MachineInstr *NewMI = MF.CreateMachineInstr(TII.get(Opcode),
02104                                               MI->getDebugLoc(), true);
02105   MachineInstrBuilder MIB(NewMI);
02106   unsigned NumAddrOps = MOs.size();
02107   for (unsigned i = 0; i != NumAddrOps; ++i)
02108     MIB.addOperand(MOs[i]);
02109   if (NumAddrOps < 4)  // FrameIndex only
02110     addOffset(MIB, 0);
02111   
02112   // Loop over the rest of the ri operands, converting them over.
02113   unsigned NumOps = MI->getDesc().getNumOperands()-2;
02114   for (unsigned i = 0; i != NumOps; ++i) {
02115     MachineOperand &MO = MI->getOperand(i+2);
02116     MIB.addOperand(MO);
02117   }
02118   for (unsigned i = NumOps+2, e = MI->getNumOperands(); i != e; ++i) {
02119     MachineOperand &MO = MI->getOperand(i);
02120     MIB.addOperand(MO);
02121   }
02122   return MIB;
02123 }
02124 
02125 static MachineInstr *FuseInst(MachineFunction &MF,
02126                               unsigned Opcode, unsigned OpNo,
02127                               const SmallVectorImpl<MachineOperand> &MOs,
02128                               MachineInstr *MI, const TargetInstrInfo &TII) {
02129   MachineInstr *NewMI = MF.CreateMachineInstr(TII.get(Opcode),
02130                                               MI->getDebugLoc(), true);
02131   MachineInstrBuilder MIB(NewMI);
02132   
02133   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
02134     MachineOperand &MO = MI->getOperand(i);
02135     if (i == OpNo) {
02136       assert(MO.isReg() && "Expected to fold into reg operand!");
02137       unsigned NumAddrOps = MOs.size();
02138       for (unsigned i = 0; i != NumAddrOps; ++i)
02139         MIB.addOperand(MOs[i]);
02140       if (NumAddrOps < 4)  // FrameIndex only
02141         addOffset(MIB, 0);
02142     } else {
02143       MIB.addOperand(MO);
02144     }
02145   }
02146   return MIB;
02147 }
02148 
02149 static MachineInstr *MakeM0Inst(const TargetInstrInfo &TII, unsigned Opcode,
02150                                 const SmallVectorImpl<MachineOperand> &MOs,
02151                                 MachineInstr *MI) {
02152   MachineFunction &MF = *MI->getParent()->getParent();
02153   MachineInstrBuilder MIB = BuildMI(MF, MI->getDebugLoc(), TII.get(Opcode));
02154 
02155   unsigned NumAddrOps = MOs.size();
02156   for (unsigned i = 0; i != NumAddrOps; ++i)
02157     MIB.addOperand(MOs[i]);
02158   if (NumAddrOps < 4)  // FrameIndex only
02159     addOffset(MIB, 0);
02160   return MIB.addImm(0);
02161 }
02162 
02163 MachineInstr*
02164 X86InstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
02165                                     MachineInstr *MI, unsigned i,
02166                                     const SmallVectorImpl<MachineOperand> &MOs,
02167                                     unsigned Align) const {
02168   const DenseMap<unsigned*, std::pair<unsigned,unsigned> > *OpcodeTablePtr=NULL;
02169   bool isTwoAddrFold = false;
02170   unsigned NumOps = MI->getDesc().getNumOperands();
02171   bool isTwoAddr = NumOps > 1 &&
02172     MI->getDesc().getOperandConstraint(1, TOI::TIED_TO) != -1;
02173 
02174   MachineInstr *NewMI = NULL;
02175   // Folding a memory location into the two-address part of a two-address
02176   // instruction is different than folding it other places.  It requires
02177   // replacing the *two* registers with the memory location.
02178   if (isTwoAddr && NumOps >= 2 && i < 2 &&
02179       MI->getOperand(0).isReg() &&
02180       MI->getOperand(1).isReg() &&
02181       MI->getOperand(0).getReg() == MI->getOperand(1).getReg()) { 
02182     OpcodeTablePtr = &RegOp2MemOpTable2Addr;
02183     isTwoAddrFold = true;
02184   } else if (i == 0) { // If operand 0
02185     if (MI->getOpcode() == X86::MOV16r0)
02186       NewMI = MakeM0Inst(*this, X86::MOV16mi, MOs, MI);
02187     else if (MI->getOpcode() == X86::MOV32r0)
02188       NewMI = MakeM0Inst(*this, X86::MOV32mi, MOs, MI);
02189     else if (MI->getOpcode() == X86::MOV8r0)
02190       NewMI = MakeM0Inst(*this, X86::MOV8mi, MOs, MI);
02191     if (NewMI)
02192       return NewMI;
02193     
02194     OpcodeTablePtr = &RegOp2MemOpTable0;
02195   } else if (i == 1) {
02196     OpcodeTablePtr = &RegOp2MemOpTable1;
02197   } else if (i == 2) {
02198     OpcodeTablePtr = &RegOp2MemOpTable2;
02199   }
02200   
02201   // If table selected...
02202   if (OpcodeTablePtr) {
02203     // Find the Opcode to fuse
02204     DenseMap<unsigned*, std::pair<unsigned,unsigned> >::iterator I =
02205       OpcodeTablePtr->find((unsigned*)MI->getOpcode());
02206     if (I != OpcodeTablePtr->end()) {
02207       unsigned MinAlign = I->second.second;
02208       if (Align < MinAlign)
02209         return NULL;
02210       if (isTwoAddrFold)
02211         NewMI = FuseTwoAddrInst(MF, I->second.first, MOs, MI, *this);
02212       else
02213         NewMI = FuseInst(MF, I->second.first, i, MOs, MI, *this);
02214       return NewMI;
02215     }
02216   }
02217   
02218   // No fusion 
02219   if (PrintFailedFusing)
02220     cerr << "We failed to fuse operand " << i << " in " << *MI;
02221   return NULL;
02222 }
02223 
02224 
02225 MachineInstr* X86InstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
02226                                                   MachineInstr *MI,
02227                                            const SmallVectorImpl<unsigned> &Ops,
02228                                                   int FrameIndex) const {
02229   // Check switch flag 
02230   if (NoFusing) return NULL;
02231 
02232   const MachineFrameInfo *MFI = MF.getFrameInfo();
02233   unsigned Alignment = MFI->getObjectAlignment(FrameIndex);
02234   if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
02235     unsigned NewOpc = 0;
02236     switch (MI->getOpcode()) {
02237     default: return NULL;
02238     case X86::TEST8rr:  NewOpc = X86::CMP8ri; break;
02239     case X86::TEST16rr: NewOpc = X86::CMP16ri; break;
02240     case X86::TEST32rr: NewOpc = X86::CMP32ri; break;
02241     case X86::TEST64rr: NewOpc = X86::CMP64ri32; break;
02242     }
02243     // Change to CMPXXri r, 0 first.
02244     MI->setDesc(get(NewOpc));
02245     MI->getOperand(1).ChangeToImmediate(0);
02246   } else if (Ops.size() != 1)
02247     return NULL;
02248 
02249   SmallVector<MachineOperand,4> MOs;
02250   MOs.push_back(MachineOperand::CreateFI(FrameIndex));
02251   return foldMemoryOperandImpl(MF, MI, Ops[0], MOs, Alignment);
02252 }
02253 
02254 MachineInstr* X86InstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
02255                                                   MachineInstr *MI,
02256                                            const SmallVectorImpl<unsigned> &Ops,
02257                                                   MachineInstr *LoadMI) const {
02258   // Check switch flag 
02259   if (NoFusing) return NULL;
02260 
02261   // Determine the alignment of the load.
02262   unsigned Alignment = 0;
02263   if (LoadMI->hasOneMemOperand())
02264     Alignment = LoadMI->memoperands_begin()->getAlignment();
02265   else if (LoadMI->getOpcode() == X86::V_SET0 ||
02266            LoadMI->getOpcode() == X86::V_SETALLONES)
02267     Alignment = 16;
02268   if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
02269     unsigned NewOpc = 0;
02270     switch (MI->getOpcode()) {
02271     default: return NULL;
02272     case X86::TEST8rr:  NewOpc = X86::CMP8ri; break;
02273     case X86::TEST16rr: NewOpc = X86::CMP16ri; break;
02274     case X86::TEST32rr: NewOpc = X86::CMP32ri; break;
02275     case X86::TEST64rr: NewOpc = X86::CMP64ri32; break;
02276     }
02277     // Change to CMPXXri r, 0 first.
02278     MI->setDesc(get(NewOpc));
02279     MI->getOperand(1).ChangeToImmediate(0);
02280   } else if (Ops.size() != 1)
02281     return NULL;
02282 
02283   SmallVector<MachineOperand,X86AddrNumOperands> MOs;
02284   if (LoadMI->getOpcode() == X86::V_SET0 ||
02285       LoadMI->getOpcode() == X86::V_SETALLONES) {
02286     // Folding a V_SET0 or V_SETALLONES as a load, to ease register pressure.
02287     // Create a constant-pool entry and operands to load from it.
02288 
02289     // x86-32 PIC requires a PIC base register for constant pools.
02290     unsigned PICBase = 0;
02291     if (TM.getRelocationModel() == Reloc::PIC_) {
02292       if (TM.getSubtarget<X86Subtarget>().is64Bit())
02293         PICBase = X86::RIP;
02294       else
02295         // FIXME: PICBase = TM.getInstrInfo()->getGlobalBaseReg(&MF);
02296         // This doesn't work for several reasons.
02297         // 1. GlobalBaseReg may have been spilled.
02298         // 2. It may not be live at MI.
02299         return false;
02300     }
02301 
02302     // Create a v4i32 constant-pool entry.
02303     MachineConstantPool &MCP = *MF.getConstantPool();
02304     const VectorType *Ty =
02305           VectorType::get(Type::getInt32Ty(MF.getFunction()->getContext()), 4);
02306     Constant *C = LoadMI->getOpcode() == X86::V_SET0 ?
02307                     Constant::getNullValue(Ty) :
02308                     Constant::getAllOnesValue(Ty);
02309     unsigned CPI = MCP.getConstantPoolIndex(C, 16);
02310 
02311     // Create operands to load from the constant pool entry.
02312     MOs.push_back(MachineOperand::CreateReg(PICBase, false));
02313     MOs.push_back(MachineOperand::CreateImm(1));
02314     MOs.push_back(MachineOperand::CreateReg(0, false));
02315     MOs.push_back(MachineOperand::CreateCPI(CPI, 0));
02316     MOs.push_back(MachineOperand::CreateReg(0, false));
02317   } else {
02318     // Folding a normal load. Just copy the load's address operands.
02319     unsigned NumOps = LoadMI->getDesc().getNumOperands();
02320     for (unsigned i = NumOps - X86AddrNumOperands; i != NumOps; ++i)
02321       MOs.push_back(LoadMI->getOperand(i));
02322   }
02323   return foldMemoryOperandImpl(MF, MI, Ops[0], MOs, Alignment);
02324 }
02325 
02326 
02327 bool X86InstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
02328                                   const SmallVectorImpl<unsigned> &Ops) const {
02329   // Check switch flag 
02330   if (NoFusing) return 0;
02331 
02332   if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
02333     switch (MI->getOpcode()) {
02334     default: return false;
02335     case X86::TEST8rr: 
02336     case X86::TEST16rr:
02337     case X86::TEST32rr:
02338     case X86::TEST64rr:
02339       return true;
02340     }
02341   }
02342 
02343   if (Ops.size() != 1)
02344     return false;
02345 
02346   unsigned OpNum = Ops[0];
02347   unsigned Opc = MI->getOpcode();
02348   unsigned NumOps = MI->getDesc().getNumOperands();
02349   bool isTwoAddr = NumOps > 1 &&
02350     MI->getDesc().getOperandConstraint(1, TOI::TIED_TO) != -1;
02351 
02352   // Folding a memory location into the two-address part of a two-address
02353   // instruction is different than folding it other places.  It requires
02354   // replacing the *two* registers with the memory location.
02355   const DenseMap<unsigned*, std::pair<unsigned,unsigned> > *OpcodeTablePtr=NULL;
02356   if (isTwoAddr && NumOps >= 2 && OpNum < 2) { 
02357     OpcodeTablePtr = &RegOp2MemOpTable2Addr;
02358   } else if (OpNum == 0) { // If operand 0
02359     switch (Opc) {
02360     case X86::MOV8r0:
02361     case X86::MOV16r0:
02362     case X86::MOV32r0:
02363       return true;
02364     default: break;
02365     }
02366     OpcodeTablePtr = &RegOp2MemOpTable0;
02367   } else if (OpNum == 1) {
02368     OpcodeTablePtr = &RegOp2MemOpTable1;
02369   } else if (OpNum == 2) {
02370     OpcodeTablePtr = &RegOp2MemOpTable2;
02371   }
02372   
02373   if (OpcodeTablePtr) {
02374     // Find the Opcode to fuse
02375     DenseMap<unsigned*, std::pair<unsigned,unsigned> >::iterator I =
02376       OpcodeTablePtr->find((unsigned*)Opc);
02377     if (I != OpcodeTablePtr->end())
02378       return true;
02379   }
02380   return false;
02381 }
02382 
02383 bool X86InstrInfo::unfoldMemoryOperand(MachineFunction &MF, MachineInstr *MI,
02384                                 unsigned Reg, bool UnfoldLoad, bool UnfoldStore,
02385                                 SmallVectorImpl<MachineInstr*> &NewMIs) const {
02386   DenseMap<unsigned*, std::pair<unsigned,unsigned> >::iterator I =
02387     MemOp2RegOpTable.find((unsigned*)MI->getOpcode());
02388   if (I == MemOp2RegOpTable.end())
02389     return false;
02390   DebugLoc dl = MI->getDebugLoc();
02391   unsigned Opc = I->second.first;
02392   unsigned Index = I->second.second & 0xf;
02393   bool FoldedLoad = I->second.second & (1 << 4);
02394   bool FoldedStore = I->second.second & (1 << 5);
02395   if (UnfoldLoad && !FoldedLoad)
02396     return false;
02397   UnfoldLoad &= FoldedLoad;
02398   if (UnfoldStore && !FoldedStore)
02399     return false;
02400   UnfoldStore &= FoldedStore;
02401 
02402   const TargetInstrDesc &TID = get(Opc);
02403   const TargetOperandInfo &TOI = TID.OpInfo[Index];
02404   const TargetRegisterClass *RC = TOI.getRegClass(&RI);
02405   SmallVector<MachineOperand, X86AddrNumOperands> AddrOps;
02406   SmallVector<MachineOperand,2> BeforeOps;
02407   SmallVector<MachineOperand,2> AfterOps;
02408   SmallVector<MachineOperand,4> ImpOps;
02409   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
02410     MachineOperand &Op = MI->getOperand(i);
02411     if (i >= Index && i < Index + X86AddrNumOperands)
02412       AddrOps.push_back(Op);
02413     else if (Op.isReg() && Op.isImplicit())
02414       ImpOps.push_back(Op);
02415     else if (i < Index)
02416       BeforeOps.push_back(Op);
02417     else if (i > Index)
02418       AfterOps.push_back(Op);
02419   }
02420 
02421   // Emit the load instruction.
02422   if (UnfoldLoad) {
02423     loadRegFromAddr(MF, Reg, AddrOps, RC, NewMIs);
02424     if (UnfoldStore) {
02425       // Address operands cannot be marked isKill.
02426       for (unsigned i = 1; i != 1 + X86AddrNumOperands; ++i) {
02427         MachineOperand &MO = NewMIs[0]->getOperand(i);
02428         if (MO.isReg())
02429           MO.setIsKill(false);
02430       }
02431     }
02432   }
02433 
02434   // Emit the data processing instruction.
02435   MachineInstr *DataMI = MF.CreateMachineInstr(TID, MI->getDebugLoc(), true);
02436   MachineInstrBuilder MIB(DataMI);
02437   
02438   if (FoldedStore)
02439     MIB.addReg(Reg, RegState::Define);
02440   for (unsigned i = 0, e = BeforeOps.size(); i != e; ++i)
02441     MIB.addOperand(BeforeOps[i]);
02442   if (FoldedLoad)
02443     MIB.addReg(Reg);
02444   for (unsigned i = 0, e = AfterOps.size(); i != e; ++i)
02445     MIB.addOperand(AfterOps[i]);
02446   for (unsigned i = 0, e = ImpOps.size(); i != e; ++i) {
02447     MachineOperand &MO = ImpOps[i];
02448     MIB.addReg(MO.getReg(),
02449                getDefRegState(MO.isDef()) |
02450                RegState::Implicit |
02451                getKillRegState(MO.isKill()) |
02452                getDeadRegState(MO.isDead()) |
02453                getUndefRegState(MO.isUndef()));
02454   }
02455   // Change CMP32ri r, 0 back to TEST32rr r, r, etc.
02456   unsigned NewOpc = 0;
02457   switch (DataMI->getOpcode()) {
02458   default: break;
02459   case X86::CMP64ri32:
02460   case X86::CMP32ri:
02461   case X86::CMP16ri:
02462   case X86::CMP8ri: {
02463     MachineOperand &MO0 = DataMI->getOperand(0);
02464     MachineOperand &MO1 = DataMI->getOperand(1);
02465     if (MO1.getImm() == 0) {
02466       switch (DataMI->getOpcode()) {
02467       default: break;
02468       case X86::CMP64ri32: NewOpc = X86::TEST64rr; break;
02469       case X86::CMP32ri:   NewOpc = X86::TEST32rr; break;
02470       case X86::CMP16ri:   NewOpc = X86::TEST16rr; break;
02471       case X86::CMP8ri:    NewOpc = X86::TEST8rr; break;
02472       }
02473       DataMI->setDesc(get(NewOpc));
02474       MO1.ChangeToRegister(MO0.getReg(), false);
02475     }
02476   }
02477   }
02478   NewMIs.push_back(DataMI);
02479 
02480   // Emit the store instruction.
02481   if (UnfoldStore) {
02482     const TargetRegisterClass *DstRC = TID.OpInfo[0].getRegClass(&RI);
02483     storeRegToAddr(MF, Reg, true, AddrOps, DstRC, NewMIs);
02484   }
02485 
02486   return true;
02487 }
02488 
02489 bool
02490 X86InstrInfo::unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
02491                                   SmallVectorImpl<SDNode*> &NewNodes) const {
02492   if (!N->isMachineOpcode())
02493     return false;
02494 
02495   DenseMap<unsigned*, std::pair<unsigned,unsigned> >::iterator I =
02496     MemOp2RegOpTable.find((unsigned*)N->getMachineOpcode());
02497   if (I == MemOp2RegOpTable.end())
02498     return false;
02499   unsigned Opc = I->second.first;
02500   unsigned Index = I->second.second & 0xf;
02501   bool FoldedLoad = I->second.second & (1 << 4);
02502   bool FoldedStore = I->second.second & (1 << 5);
02503   const TargetInstrDesc &TID = get(Opc);
02504   const TargetRegisterClass *RC = TID.OpInfo[Index].getRegClass(&RI);
02505   unsigned NumDefs = TID.NumDefs;
02506   std::vector<SDValue> AddrOps;
02507   std::vector<SDValue> BeforeOps;
02508   std::vector<SDValue> AfterOps;
02509   DebugLoc dl = N->getDebugLoc();
02510   unsigned NumOps = N->getNumOperands();
02511   for (unsigned i = 0; i != NumOps-1; ++i) {
02512     SDValue Op = N->getOperand(i);
02513     if (i >= Index-NumDefs && i < Index-NumDefs + X86AddrNumOperands)
02514       AddrOps.push_back(Op);
02515     else if (i < Index-NumDefs)
02516       BeforeOps.push_back(Op);
02517     else if (i > Index-NumDefs)
02518       AfterOps.push_back(Op);
02519   }
02520   SDValue Chain = N->getOperand(NumOps-1);
02521   AddrOps.push_back(Chain);
02522 
02523   // Emit the load instruction.
02524   SDNode *Load = 0;
02525   const MachineFunction &MF = DAG.getMachineFunction();
02526   if (FoldedLoad) {
02527     EVT VT = *RC->vt_begin();
02528     bool isAligned = (RI.getStackAlignment() >= 16) ||
02529       RI.needsStackRealignment(MF);
02530     Load = DAG.getTargetNode(getLoadRegOpcode(0, RC, isAligned, TM), dl,
02531                              VT, MVT::Other, &AddrOps[0], AddrOps.size());
02532     NewNodes.push_back(Load);
02533   }
02534 
02535   // Emit the data processing instruction.
02536   std::vector<EVT> VTs;
02537   const TargetRegisterClass *DstRC = 0;
02538   if (TID.getNumDefs() > 0) {
02539     DstRC = TID.OpInfo[0].getRegClass(&RI);
02540     VTs.push_back(*DstRC->vt_begin());
02541   }
02542   for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
02543     EVT VT = N->getValueType(i);
02544     if (VT != MVT::Other && i >= (unsigned)TID.getNumDefs())
02545       VTs.push_back(VT);
02546   }
02547   if (Load)
02548     BeforeOps.push_back(SDValue(Load, 0));
02549   std::copy(AfterOps.begin(), AfterOps.end(), std::back_inserter(BeforeOps));
02550   SDNode *NewNode= DAG.getTargetNode(Opc, dl, VTs, &BeforeOps[0],
02551                                      BeforeOps.size());
02552   NewNodes.push_back(NewNode);
02553 
02554   // Emit the store instruction.
02555   if (FoldedStore) {
02556     AddrOps.pop_back();
02557     AddrOps.push_back(SDValue(NewNode, 0));
02558     AddrOps.push_back(Chain);
02559     bool isAligned = (RI.getStackAlignment() >= 16) ||
02560       RI.needsStackRealignment(MF);
02561     SDNode *Store = DAG.getTargetNode(getStoreRegOpcode(0, DstRC,
02562                                                         isAligned, TM),
02563                                       dl, MVT::Other,
02564                                       &AddrOps[0], AddrOps.size());
02565     NewNodes.push_back(Store);
02566   }
02567 
02568   return true;
02569 }
02570 
02571 unsigned X86InstrInfo::getOpcodeAfterMemoryUnfold(unsigned Opc,
02572                                       bool UnfoldLoad, bool UnfoldStore) const {
02573   DenseMap<unsigned*, std::pair<unsigned,unsigned> >::iterator I =
02574     MemOp2RegOpTable.find((unsigned*)Opc);
02575   if (I == MemOp2RegOpTable.end())
02576     return 0;
02577   bool FoldedLoad = I->second.second & (1 << 4);
02578   bool FoldedStore = I->second.second & (1 << 5);
02579   if (UnfoldLoad && !FoldedLoad)
02580     return 0;
02581   if (UnfoldStore && !FoldedStore)
02582     return 0;
02583   return I->second.first;
02584 }
02585 
02586 bool X86InstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
02587   if (MBB.empty()) return false;
02588   
02589   switch (MBB.back().getOpcode()) {
02590   case X86::TCRETURNri:
02591   case X86::TCRETURNdi:
02592   case X86::RET:     // Return.
02593   case X86::RETI:
02594   case X86::TAILJMPd:
02595   case X86::TAILJMPr:
02596   case X86::TAILJMPm:
02597   case X86::JMP:     // Uncond branch.
02598   case X86::JMP32r:  // Indirect branch.
02599   case X86::JMP64r:  // Indirect branch (64-bit).
02600   case X86::JMP32m:  // Indirect branch through mem.
02601   case X86::JMP64m:  // Indirect branch through mem (64-bit).
02602     return true;
02603   default: return false;
02604   }
02605 }
02606 
02607 bool X86InstrInfo::
02608 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
02609   assert(Cond.size() == 1 && "Invalid X86 branch condition!");
02610   X86::CondCode CC = static_cast<X86::CondCode>(Cond[0].getImm());
02611   if (CC == X86::COND_NE_OR_P || CC == X86::COND_NP_OR_E)
02612     return true;
02613   Cond[0].setImm(GetOppositeBranchCondition(CC));
02614   return false;
02615 }
02616 
02617 bool X86InstrInfo::
02618 isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const {
02619   // FIXME: Return false for x87 stack register classes for now. We can't
02620   // allow any loads of these registers before FpGet_ST0_80.
02621   return !(RC == &X86::CCRRegClass || RC == &X86::RFP32RegClass ||
02622            RC == &X86::RFP64RegClass || RC == &X86::RFP80RegClass);
02623 }
02624 
02625 unsigned X86InstrInfo::sizeOfImm(const TargetInstrDesc *Desc) {
02626   switch (Desc->TSFlags & X86II::ImmMask) {
02627   case X86II::Imm8:   return 1;
02628   case X86II::Imm16:  return 2;
02629   case X86II::Imm32:  return 4;
02630   case X86II::Imm64:  return 8;
02631   default: llvm_unreachable("Immediate size not set!");
02632     return 0;
02633   }
02634 }
02635 
02636 /// isX86_64ExtendedReg - Is the MachineOperand a x86-64 extended register?
02637 /// e.g. r8, xmm8, etc.
02638 bool X86InstrInfo::isX86_64ExtendedReg(const MachineOperand &MO) {
02639   if (!MO.isReg()) return false;
02640   switch (MO.getReg()) {
02641   default: break;
02642   case X86::R8:    case X86::R9:    case X86::R10:   case X86::R11:
02643   case X86::R12:   case X86::R13:   case X86::R14:   case X86::R15:
02644   case X86::R8D:   case X86::R9D:   case X86::R10D:  case X86::R11D:
02645   case X86::R12D:  case X86::R13D:  case X86::R14D:  case X86::R15D:
02646   case X86::R8W:   case X86::R9W:   case X86::R10W:  case X86::R11W:
02647   case X86::R12W:  case X86::R13W:  case X86::R14W:  case X86::R15W:
02648   case X86::R8B:   case X86::R9B:   case X86::R10B:  case X86::R11B:
02649   case X86::R12B:  case X86::R13B:  case X86::R14B:  case X86::R15B:
02650   case X86::XMM8:  case X86::XMM9:  case X86::XMM10: case X86::XMM11:
02651   case X86::XMM12: case X86::XMM13: case X86::XMM14: case X86::XMM15:
02652     return true;
02653   }
02654   return false;
02655 }
02656 
02657 
02658 /// determineREX - Determine if the MachineInstr has to be encoded with a X86-64
02659 /// REX prefix which specifies 1) 64-bit instructions, 2) non-default operand
02660 /// size, and 3) use of X86-64 extended registers.
02661 unsigned X86InstrInfo::determineREX(const MachineInstr &MI) {
02662   unsigned REX = 0;
02663   const TargetInstrDesc &Desc = MI.getDesc();
02664 
02665   // Pseudo instructions do not need REX prefix byte.
02666   if ((Desc.TSFlags & X86II::FormMask) == X86II::Pseudo)
02667     return 0;
02668   if (Desc.TSFlags & X86II::REX_W)
02669     REX |= 1 << 3;
02670 
02671   unsigned NumOps = Desc.getNumOperands();
02672   if (NumOps) {
02673     bool isTwoAddr = NumOps > 1 &&
02674       Desc.getOperandConstraint(1, TOI::TIED_TO) != -1;
02675 
02676     // If it accesses SPL, BPL, SIL, or DIL, then it requires a 0x40 REX prefix.
02677     unsigned i = isTwoAddr ? 1 : 0;
02678     for (unsigned e = NumOps; i != e; ++i) {
02679       const MachineOperand& MO = MI.getOperand(i);
02680       if (MO.isReg()) {
02681         unsigned Reg = MO.getReg();
02682         if (isX86_64NonExtLowByteReg(Reg))
02683           REX |= 0x40;
02684       }
02685     }
02686 
02687     switch (Desc.TSFlags & X86II::FormMask) {
02688     case X86II::MRMInitReg:
02689       if (isX86_64ExtendedReg(MI.getOperand(0)))
02690         REX |= (1 << 0) | (1 << 2);
02691       break;
02692     case X86II::MRMSrcReg: {
02693       if (isX86_64ExtendedReg(MI.getOperand(0)))
02694         REX |= 1 << 2;
02695       i = isTwoAddr ? 2 : 1;
02696       for (unsigned e = NumOps; i != e; ++i) {
02697         const MachineOperand& MO = MI.getOperand(i);
02698         if (isX86_64ExtendedReg(MO))
02699           REX |= 1 << 0;
02700       }
02701       break;
02702     }
02703     case X86II::MRMSrcMem: {
02704       if (isX86_64ExtendedReg(MI.getOperand(0)))
02705         REX |= 1 << 2;
02706       unsigned Bit = 0;
02707       i = isTwoAddr ? 2 : 1;
02708       for (; i != NumOps; ++i) {
02709         const MachineOperand& MO = MI.getOperand(i);
02710         if (MO.isReg()) {
02711           if (isX86_64ExtendedReg(MO))
02712             REX |= 1 << Bit;
02713           Bit++;
02714         }
02715       }
02716       break;
02717     }
02718     case X86II::MRM0m: case X86II::MRM1m:
02719     case X86II::MRM2m: case X86II::MRM3m:
02720     case X86II::MRM4m: case X86II::MRM5m:
02721     case X86II::MRM6m: case X86II::MRM7m:
02722     case X86II::MRMDestMem: {
02723       unsigned e = (isTwoAddr ? X86AddrNumOperands+1 : X86AddrNumOperands);
02724       i = isTwoAddr ? 1 : 0;
02725       if (NumOps > e && isX86_64ExtendedReg(MI.getOperand(e)))
02726         REX |= 1 << 2;
02727       unsigned Bit = 0;
02728       for (; i != e; ++i) {
02729         const MachineOperand& MO = MI.getOperand(i);
02730         if (MO.isReg()) {
02731           if (isX86_64ExtendedReg(MO))
02732             REX |= 1 << Bit;
02733           Bit++;
02734         }
02735       }
02736       break;
02737     }
02738     default: {
02739       if (isX86_64ExtendedReg(MI.getOperand(0)))
02740         REX |= 1 << 0;
02741       i = isTwoAddr ? 2 : 1;
02742       for (unsigned e = NumOps; i != e; ++i) {
02743         const MachineOperand& MO = MI.getOperand(i);
02744         if (isX86_64ExtendedReg(MO))
02745           REX |= 1 << 2;
02746       }
02747       break;
02748     }
02749     }
02750   }
02751   return REX;
02752 }
02753 
02754 /// sizePCRelativeBlockAddress - This method returns the size of a PC
02755 /// relative block address instruction
02756 ///
02757 static unsigned sizePCRelativeBlockAddress() {
02758   return 4;
02759 }
02760 
02761 /// sizeGlobalAddress - Give the size of the emission of this global address
02762 ///
02763 static unsigned sizeGlobalAddress(bool dword) {
02764   return dword ? 8 : 4;
02765 }
02766 
02767 /// sizeConstPoolAddress - Give the size of the emission of this constant
02768 /// pool address
02769 ///
02770 static unsigned sizeConstPoolAddress(bool dword) {
02771   return dword ? 8 : 4;
02772 }
02773 
02774 /// sizeExternalSymbolAddress - Give the size of the emission of this external
02775 /// symbol
02776 ///
02777 static unsigned sizeExternalSymbolAddress(bool dword) {
02778   return dword ? 8 : 4;
02779 }
02780 
02781 /// sizeJumpTableAddress - Give the size of the emission of this jump
02782 /// table address
02783 ///
02784 static unsigned sizeJumpTableAddress(bool dword) {
02785   return dword ? 8 : 4;
02786 }
02787 
02788 static unsigned sizeConstant(unsigned Size) {
02789   return Size;
02790 }
02791 
02792 static unsigned sizeRegModRMByte(){
02793   return 1;
02794 }
02795 
02796 static unsigned sizeSIBByte(){
02797   return 1;
02798 }
02799 
02800 static unsigned getDisplacementFieldSize(const MachineOperand *RelocOp) {
02801   unsigned FinalSize = 0;
02802   // If this is a simple integer displacement that doesn't require a relocation.
02803   if (!RelocOp) {
02804     FinalSize += sizeConstant(4);
02805     return FinalSize;
02806   }
02807   
02808   // Otherwise, this is something that requires a relocation.
02809   if (RelocOp->isGlobal()) {
02810     FinalSize += sizeGlobalAddress(false);
02811   } else if (RelocOp->isCPI()) {
02812     FinalSize += sizeConstPoolAddress(false);
02813   } else if (RelocOp->isJTI()) {
02814     FinalSize += sizeJumpTableAddress(false);
02815   } else {
02816     llvm_unreachable("Unknown value to relocate!");
02817   }
02818   return FinalSize;
02819 }
02820 
02821 static unsigned getMemModRMByteSize(const MachineInstr &MI, unsigned Op,
02822                                     bool IsPIC, bool Is64BitMode) {
02823   const MachineOperand &Op3 = MI.getOperand(Op+3);
02824   int DispVal = 0;
02825   const MachineOperand *DispForReloc = 0;
02826   unsigned FinalSize = 0;
02827   
02828   // Figure out what sort of displacement we have to handle here.
02829   if (Op3.isGlobal()) {
02830     DispForReloc = &Op3;
02831   } else if (Op3.isCPI()) {
02832     if (Is64BitMode || IsPIC) {
02833       DispForReloc = &Op3;
02834     } else {
02835       DispVal = 1;
02836     }
02837   } else if (Op3.isJTI()) {
02838     if (Is64BitMode || IsPIC) {
02839       DispForReloc = &Op3;
02840     } else {
02841       DispVal = 1; 
02842     }
02843   } else {
02844     DispVal = 1;
02845   }
02846 
02847   const MachineOperand &Base     = MI.getOperand(Op);
02848   const MachineOperand &IndexReg = MI.getOperand(Op+2);
02849 
02850   unsigned BaseReg = Base.getReg();
02851 
02852   // Is a SIB byte needed?
02853   if ((!Is64BitMode || DispForReloc || BaseReg != 0) &&
02854       IndexReg.getReg() == 0 &&
02855       (BaseReg == 0 || X86RegisterInfo::getX86RegNum(BaseReg) != N86::ESP)) {      
02856     if (BaseReg == 0) {  // Just a displacement?
02857       // Emit special case [disp32] encoding
02858       ++FinalSize; 
02859       FinalSize += getDisplacementFieldSize(DispForReloc);
02860     } else {
02861       unsigned BaseRegNo = X86RegisterInfo::getX86RegNum(BaseReg);
02862       if (!DispForReloc && DispVal == 0 && BaseRegNo != N86::EBP) {
02863         // Emit simple indirect register encoding... [EAX] f.e.
02864         ++FinalSize;
02865       // Be pessimistic and assume it's a disp32, not a disp8
02866       } else {
02867         // Emit the most general non-SIB encoding: [REG+disp32]
02868         ++FinalSize;
02869         FinalSize += getDisplacementFieldSize(DispForReloc);
02870       }
02871     }
02872 
02873   } else {  // We need a SIB byte, so start by outputting the ModR/M byte first
02874     assert(IndexReg.getReg() != X86::ESP &&
02875            IndexReg.getReg() != X86::RSP && "Cannot use ESP as index reg!");
02876 
02877     bool ForceDisp32 = false;
02878     if (BaseReg == 0 || DispForReloc) {
02879       // Emit the normal disp32 encoding.
02880       ++FinalSize;
02881       ForceDisp32 = true;
02882     } else {
02883       ++FinalSize;
02884     }
02885 
02886     FinalSize += sizeSIBByte();
02887 
02888     // Do we need to output a displacement?
02889     if (DispVal != 0 || ForceDisp32) {
02890       FinalSize += getDisplacementFieldSize(DispForReloc);
02891     }
02892   }
02893   return FinalSize;
02894 }
02895 
02896 
02897 static unsigned GetInstSizeWithDesc(const MachineInstr &MI,
02898                                     const TargetInstrDesc *Desc,
02899                                     bool IsPIC, bool Is64BitMode) {
02900   
02901   unsigned Opcode = Desc->Opcode;
02902   unsigned FinalSize = 0;
02903 
02904   // Emit the lock opcode prefix as needed.
02905   if (Desc->TSFlags & X86II::LOCK) ++FinalSize;
02906 
02907   // Emit segment override opcode prefix as needed.
02908   switch (Desc->TSFlags & X86II::SegOvrMask) {
02909   case X86II::FS:
02910   case X86II::GS:
02911    ++FinalSize;
02912    break;
02913   default: llvm_unreachable("Invalid segment!");
02914   case 0: break;  // No segment override!
02915   }
02916 
02917   // Emit the repeat opcode prefix as needed.
02918   if ((Desc->TSFlags & X86II::Op0Mask) == X86II::REP) ++FinalSize;
02919 
02920   // Emit the operand size opcode prefix as needed.
02921   if (Desc->TSFlags & X86II::OpSize) ++FinalSize;
02922 
02923   // Emit the address size opcode prefix as needed.
02924   if (Desc->TSFlags & X86II::AdSize) ++FinalSize;
02925 
02926   bool Need0FPrefix = false;
02927   switch (Desc->TSFlags & X86II::Op0Mask) {
02928   case X86II::TB:  // Two-byte opcode prefix
02929   case X86II::T8:  // 0F 38
02930   case X86II::TA:  // 0F 3A
02931     Need0FPrefix = true;
02932     break;
02933   case X86II::TF: // F2 0F 38
02934     ++FinalSize;
02935     Need0FPrefix = true;
02936     break;
02937   case X86II::REP: break; // already handled.
02938   case X86II::XS:   // F3 0F
02939     ++FinalSize;
02940     Need0FPrefix = true;
02941     break;
02942   case X86II::XD:   // F2 0F
02943     ++FinalSize;
02944     Need0FPrefix = true;
02945     break;
02946   case X86II::D8: case X86II::D9: case X86II::DA: case X86II::DB:
02947   case X86II::DC: case X86II::DD: case X86II::DE: case X86II::DF:
02948     ++FinalSize;
02949     break; // Two-byte opcode prefix
02950   default: llvm_unreachable("Invalid prefix!");
02951   case 0: break;  // No prefix!
02952   }
02953 
02954   if (Is64BitMode) {
02955     // REX prefix
02956     unsigned REX = X86InstrInfo::determineREX(MI);
02957     if (REX)
02958       ++FinalSize;
02959   }
02960 
02961   // 0x0F escape code must be emitted just before the opcode.
02962   if (Need0FPrefix)
02963     ++FinalSize;
02964 
02965   switch (Desc->TSFlags & X86II::Op0Mask) {
02966   case X86II::T8:  // 0F 38
02967     ++FinalSize;
02968     break;
02969   case X86II::TA:  // 0F 3A
02970     ++FinalSize;
02971     break;
02972   case X86II::TF: // F2 0F 38
02973     ++FinalSize;
02974     break;
02975   }
02976 
02977   // If this is a two-address instruction, skip one of the register operands.
02978   unsigned NumOps = Desc->getNumOperands();
02979   unsigned CurOp = 0;
02980   if (NumOps > 1 && Desc->getOperandConstraint(1, TOI::TIED_TO) != -1)
02981     CurOp++;
02982   else if (NumOps > 2 && Desc->getOperandConstraint(NumOps-1, TOI::TIED_TO)== 0)
02983     // Skip the last source operand that is tied_to the dest reg. e.g. LXADD32
02984     --NumOps;
02985 
02986   switch (Desc->TSFlags & X86II::FormMask) {
02987   default: llvm_unreachable("Unknown FormMask value in X86 MachineCodeEmitter!");
02988   case X86II::Pseudo:
02989     // Remember the current PC offset, this is the PIC relocation
02990     // base address.
02991     switch (Opcode) {
02992     default: 
02993       break;
02994     case TargetInstrInfo::INLINEASM: {
02995       const MachineFunction *MF = MI.getParent()->getParent();
02996       const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
02997       FinalSize += TII.getInlineAsmLength(MI.getOperand(0).getSymbolName(),
02998                                           *MF->getTarget().getTargetAsmInfo());
02999       break;
03000     }
03001     case TargetInstrInfo::DBG_LABEL:
03002     case TargetInstrInfo::EH_LABEL:
03003       break;
03004     case TargetInstrInfo::IMPLICIT_DEF:
03005     case TargetInstrInfo::DECLARE:
03006     case X86::DWARF_LOC:
03007     case X86::FP_REG_KILL:
03008       break;
03009     case X86::MOVPC32r: {
03010       // This emits the "call" portion of this pseudo instruction.
03011       ++FinalSize;
03012       FinalSize += sizeConstant(X86InstrInfo::sizeOfImm(Desc));
03013       break;
03014     }
03015     }
03016     CurOp = NumOps;
03017     break;
03018   case X86II::RawFrm:
03019     ++FinalSize;
03020 
03021     if (CurOp != NumOps) {
03022       const MachineOperand &MO = MI.getOperand(CurOp++);
03023       if (MO.isMBB()) {
03024         FinalSize += sizePCRelativeBlockAddress();
03025       } else if (MO.isGlobal()) {
03026         FinalSize += sizeGlobalAddress(false);
03027       } else if (MO.isSymbol()) {
03028         FinalSize += sizeExternalSymbolAddress(false);
03029       } else if (MO.isImm()) {
03030         FinalSize += sizeConstant(X86InstrInfo::sizeOfImm(Desc));
03031       } else {
03032         llvm_unreachable("Unknown RawFrm operand!");
03033       }
03034     }
03035     break;
03036 
03037   case X86II::AddRegFrm:
03038     ++FinalSize;
03039     ++CurOp;
03040     
03041     if (CurOp != NumOps) {
03042       const MachineOperand &MO1 = MI.getOperand(CurOp++);
03043       unsigned Size = X86InstrInfo::sizeOfImm(Desc);
03044       if (MO1.isImm())
03045         FinalSize += sizeConstant(Size);
03046       else {
03047         bool dword = false;
03048         if (Opcode == X86::MOV64ri)
03049           dword = true; 
03050         if (MO1.isGlobal()) {
03051           FinalSize += sizeGlobalAddress(dword);
03052         } else if (MO1.isSymbol())
03053           FinalSize += sizeExternalSymbolAddress(dword);
03054         else if (MO1.isCPI())
03055           FinalSize += sizeConstPoolAddress(dword);
03056         else if (MO1.isJTI())
03057           FinalSize += sizeJumpTableAddress(dword);
03058       }
03059     }
03060     break;
03061 
03062   case X86II::MRMDestReg: {
03063     ++FinalSize; 
03064     FinalSize += sizeRegModRMByte();
03065     CurOp += 2;
03066     if (CurOp != NumOps) {
03067       ++CurOp;
03068       FinalSize += sizeConstant(X86InstrInfo::sizeOfImm(Desc));
03069     }
03070     break;
03071   }
03072   case X86II::MRMDestMem: {
03073     ++FinalSize;
03074     FinalSize += getMemModRMByteSize(MI, CurOp, IsPIC, Is64BitMode);
03075     CurOp +=  X86AddrNumOperands + 1;
03076     if (CurOp != NumOps) {
03077       ++CurOp;
03078       FinalSize += sizeConstant(X86InstrInfo::sizeOfImm(Desc));
03079     }
03080     break;
03081   }
03082 
03083   case X86II::MRMSrcReg:
03084     ++FinalSize;
03085     FinalSize += sizeRegModRMByte();
03086     CurOp += 2;
03087     if (CurOp != NumOps) {
03088       ++CurOp;
03089       FinalSize += sizeConstant(X86InstrInfo::sizeOfImm(Desc));
03090     }
03091     break;
03092 
03093   case X86II::MRMSrcMem: {
03094     int AddrOperands;
03095     if (Opcode == X86::LEA64r || Opcode == X86::LEA64_32r ||
03096         Opcode == X86::LEA16r || Opcode == X86::LEA32r)
03097       AddrOperands = X86AddrNumOperands - 1; // No segment register
03098     else
03099       AddrOperands = X86AddrNumOperands;
03100 
03101     ++FinalSize;
03102     FinalSize += getMemModRMByteSize(MI, CurOp+1, IsPIC, Is64BitMode);
03103     CurOp += AddrOperands + 1;
03104     if (CurOp != NumOps) {
03105       ++CurOp;
03106       FinalSize += sizeConstant(X86InstrInfo::sizeOfImm(Desc));
03107     }
03108     break;
03109   }
03110 
03111   case X86II::MRM0r: case X86II::MRM1r:
03112   case X86II::MRM2r: case X86II::MRM3r:
03113   case X86II::MRM4r: case X86II::MRM5r:
03114   case X86II::MRM6r: case X86II::MRM7r:
03115     ++FinalSize;
03116     if (Desc->getOpcode() == X86::LFENCE ||
03117         Desc->getOpcode() == X86::MFENCE) {
03118       // Special handling of lfence and mfence;
03119       FinalSize += sizeRegModRMByte();
03120     } else if (Desc->getOpcode() == X86::MONITOR ||
03121                Desc->getOpcode() == X86::MWAIT) {
03122       // Special handling of monitor and mwait.
03123       FinalSize += sizeRegModRMByte() + 1; // +1 for the opcode.
03124     } else {
03125       ++CurOp;
03126       FinalSize += sizeRegModRMByte();
03127     }
03128 
03129     if (CurOp != NumOps) {
03130       const MachineOperand &MO1 = MI.getOperand(CurOp++);
03131       unsigned Size = X86InstrInfo::sizeOfImm(Desc);
03132       if (MO1.isImm())
03133         FinalSize += sizeConstant(Size);
03134       else {
03135         bool dword = false;
03136         if (Opcode == X86::MOV64ri32)
03137           dword = true;
03138         if (MO1.isGlobal()) {
03139           FinalSize += sizeGlobalAddress(dword);
03140         } else if (MO1.isSymbol())
03141           FinalSize += sizeExternalSymbolAddress(dword);
03142         else if (MO1.isCPI())
03143           FinalSize += sizeConstPoolAddress(dword);
03144         else if (MO1.isJTI())
03145           FinalSize += sizeJumpTableAddress(dword);
03146       }
03147     }
03148     break;
03149 
03150   case X86II::MRM0m: case X86II::MRM1m:
03151   case X86II::MRM2m: case X86II::MRM3m:
03152   case X86II::MRM4m: case X86II::MRM5m:
03153   case X86II::MRM6m: case X86II::MRM7m: {
03154     
03155     ++FinalSize;
03156     FinalSize += getMemModRMByteSize(MI, CurOp, IsPIC, Is64BitMode);
03157     CurOp += X86AddrNumOperands;
03158 
03159     if (CurOp != NumOps) {
03160       const MachineOperand &MO = MI.getOperand(CurOp++);
03161       unsigned Size = X86InstrInfo::sizeOfImm(Desc);
03162       if (MO.isImm())
03163         FinalSize += sizeConstant(Size);
03164       else {
03165         bool dword = false;
03166         if (Opcode == X86::MOV64mi32)
03167           dword = true;
03168         if (MO.isGlobal()) {
03169           FinalSize += sizeGlobalAddress(dword);
03170         } else if (MO.isSymbol())
03171           FinalSize += sizeExternalSymbolAddress(dword);
03172         else if (MO.isCPI())
03173           FinalSize += sizeConstPoolAddress(dword);
03174         else if (MO.isJTI())
03175           FinalSize += sizeJumpTableAddress(dword);
03176       }
03177     }
03178     break;
03179   }
03180 
03181   case X86II::MRMInitReg:
03182     ++FinalSize;
03183     // Duplicate register, used by things like MOV8r0 (aka xor reg,reg).
03184     FinalSize += sizeRegModRMByte();
03185     ++CurOp;
03186     break;
03187   }
03188 
03189   if (!Desc->isVariadic() && CurOp != NumOps) {
03190     std::string msg;
03191     raw_string_ostream Msg(msg);
03192     Msg << "Cannot determine size: " << MI;
03193     llvm_report_error(Msg.str());
03194   }
03195   
03196 
03197   return FinalSize;
03198 }
03199 
03200 
03201 unsigned X86InstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
03202   const TargetInstrDesc &Desc = MI->getDesc();
03203   bool IsPIC = TM.getRelocationModel() == Reloc::PIC_;
03204   bool Is64BitMode = TM.getSubtargetImpl()->is64Bit();
03205   unsigned Size = GetInstSizeWithDesc(*MI, &Desc, IsPIC, Is64BitMode);
03206   if (Desc.getOpcode() == X86::MOVPC32r)
03207     Size += GetInstSizeWithDesc(*MI, &get(X86::POP32r), IsPIC, Is64BitMode);
03208   return Size;
03209 }
03210 
03211 /// getGlobalBaseReg - Return a virtual register initialized with the
03212 /// the global base register value. Output instructions required to
03213 /// initialize the register in the function entry block, if necessary.
03214 ///
03215 unsigned X86InstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
03216   assert(!TM.getSubtarget<X86Subtarget>().is64Bit() &&
03217          "X86-64 PIC uses RIP relative addressing");
03218 
03219   X86MachineFunctionInfo *X86FI = MF->getInfo<X86MachineFunctionInfo>();
03220   unsigned GlobalBaseReg = X86FI->getGlobalBaseReg();
03221   if (GlobalBaseReg != 0)
03222     return GlobalBaseReg;
03223 
03224   // Insert the set of GlobalBaseReg into the first MBB of the function
03225   MachineBasicBlock &FirstMBB = MF->front();
03226   MachineBasicBlock::iterator MBBI = FirstMBB.begin();
03227   DebugLoc DL = DebugLoc::getUnknownLoc();
03228   if (MBBI != FirstMBB.end()) DL = MBBI->getDebugLoc();
03229   MachineRegisterInfo &RegInfo = MF->getRegInfo();
03230   unsigned PC = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
03231   
03232   const TargetInstrInfo *TII = TM.getInstrInfo();
03233   // Operand of MovePCtoStack is completely ignored by asm printer. It's
03234   // only used in JIT code emission as displacement to pc.
03235   BuildMI(FirstMBB, MBBI, DL, TII->get(X86::MOVPC32r), PC).addImm(0);
03236   
03237   // If we're using vanilla 'GOT' PIC style, we should use relative addressing
03238   // not to pc, but to _GLOBAL_OFFSET_TABLE_ external.
03239   if (TM.getSubtarget<X86Subtarget>().isPICStyleGOT()) {
03240     GlobalBaseReg = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
03241     // Generate addl $__GLOBAL_OFFSET_TABLE_ + [.-piclabel], %some_register
03242     BuildMI(FirstMBB, MBBI, DL, TII->get(X86::ADD32ri), GlobalBaseReg)
03243       .addReg(PC).addExternalSymbol("_GLOBAL_OFFSET_TABLE_", 0,
03244                                     X86II::MO_GOT_ABSOLUTE_ADDRESS);
03245   } else {
03246     GlobalBaseReg = PC;
03247   }
03248 
03249   X86FI->setGlobalBaseReg(GlobalBaseReg);
03250   return GlobalBaseReg;
03251 }