Implement insertGoto and reverseBranchCondition for the X86.
[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 #include "X86GenInstrInfo.inc"
18 using namespace llvm;
19
20 X86InstrInfo::X86InstrInfo()
21   : TargetInstrInfo(X86Insts, sizeof(X86Insts)/sizeof(X86Insts[0])) {
22 }
23
24
25 bool X86InstrInfo::isMoveInstr(const MachineInstr& MI,
26                                unsigned& sourceReg,
27                                unsigned& destReg) const {
28   MachineOpCode oc = MI.getOpcode();
29   if (oc == X86::MOV8rr || oc == X86::MOV16rr || oc == X86::MOV32rr ||
30       oc == X86::FpMOV) {
31       assert(MI.getNumOperands() == 2 &&
32              MI.getOperand(0).isRegister() &&
33              MI.getOperand(1).isRegister() &&
34              "invalid register-register move instruction");
35       sourceReg = MI.getOperand(1).getReg();
36       destReg = MI.getOperand(0).getReg();
37       return true;
38   }
39   return false;
40 }
41
42 void X86InstrInfo::insertGoto(MachineBasicBlock& MBB,
43                               MachineBasicBlock& TMBB) const {
44   BuildMI(MBB, MBB.end(), X86::JMP, 1).addMBB(&TMBB);
45 }
46
47 MachineBasicBlock::iterator
48 X86InstrInfo::reverseBranchCondition(MachineBasicBlock::iterator MI) const {
49   unsigned Opcode = MI->getOpcode();
50   assert(isBranch(Opcode) && "MachineInstr must be a branch");
51   unsigned ROpcode;
52   switch (Opcode) {
53   case X86::JB:  ROpcode = X86::JAE;
54   case X86::JAE: ROpcode = X86::JB;
55   case X86::JE:  ROpcode = X86::JNE;
56   case X86::JNE: ROpcode = X86::JE;
57   case X86::JBE: ROpcode = X86::JA;
58   case X86::JA:  ROpcode = X86::JBE;
59   case X86::JS:  ROpcode = X86::JNS;
60   case X86::JNS: ROpcode = X86::JS;
61   case X86::JL:  ROpcode = X86::JGE;
62   case X86::JGE: ROpcode = X86::JL;
63   case X86::JLE: ROpcode = X86::JG;
64   case X86::JG:  ROpcode = X86::JLE;
65   default:
66     assert(0 && "Cannot reverse uncodnitional branches!");
67   }
68   MachineBasicBlock* MBB = MI->getParent();
69   MachineBasicBlock* TMBB = MI->getOperand(0).getMachineBasicBlock();
70   MachineInstrBuilder IB = BuildMI(*MBB, MBB->erase(MI), ROpcode, 1);
71   IB.addMBB(TMBB);
72   return IB;
73 }