caffe62867b1d1c8d0de3fe86e1ce4155c635c78
[oota-llvm.git] / lib / Target / X86 / X86InstrInfo.cpp
1 //===- X86InstrInfo.cpp - X86 Instruction Information -----------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the X86 implementation of the TargetInstrInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "X86InstrInfo.h"
15 #include "X86.h"
16 #include "llvm/CodeGen/MachineInstrBuilder.h"
17
18 #include "X86GenInstrInfo.inc"
19
20 using namespace llvm;
21
22 X86InstrInfo::X86InstrInfo()
23   : TargetInstrInfo(X86Insts, sizeof(X86Insts)/sizeof(X86Insts[0]), 0) {
24 }
25
26
27 // createNOPinstr - returns the target's implementation of NOP, which is
28 // usually a pseudo-instruction, implemented by a degenerate version of
29 // another instruction, e.g. X86: `xchg ax, ax'; SparcV9: `sethi r0, r0, r0'
30 //
31 MachineInstr* X86InstrInfo::createNOPinstr() const {
32   return BuildMI(X86::XCHGrr16, 2).addReg(X86::AX, MachineOperand::UseAndDef)
33                                   .addReg(X86::AX, MachineOperand::UseAndDef);
34 }
35
36
37 /// isNOPinstr - not having a special NOP opcode, we need to know if a given
38 /// instruction is interpreted as an `official' NOP instr, i.e., there may be
39 /// more than one way to `do nothing' but only one canonical way to slack off.
40 //
41 bool X86InstrInfo::isNOPinstr(const MachineInstr &MI) const {
42   // Make sure the instruction is EXACTLY `xchg ax, ax'
43   if (MI.getOpcode() == X86::XCHGrr16) {
44     const MachineOperand &op0 = MI.getOperand(0), &op1 = MI.getOperand(1);
45     if (op0.isRegister() && op0.getReg() == X86::AX &&
46         op1.isRegister() && op1.getReg() == X86::AX) {
47       return true;
48     }
49   }
50   // FIXME: there are several NOOP instructions, we should check for them here.
51   return false;
52 }
53
54 bool X86InstrInfo::isMoveInstr(const MachineInstr& MI,
55                                unsigned& sourceReg,
56                                unsigned& destReg) const {
57   MachineOpCode oc = MI.getOpcode();
58   if (oc == X86::MOVrr8 || oc == X86::MOVrr16 || oc == X86::MOVrr32 ||
59       oc == X86::FpMOV) {
60       assert(MI.getNumOperands() == 2 &&
61              MI.getOperand(0).isRegister() &&
62              MI.getOperand(1).isRegister() &&
63              "invalid register-register move instruction");
64       sourceReg = MI.getOperand(1).getReg();
65       destReg = MI.getOperand(0).getReg();
66       return true;
67   }
68   return false;
69 }