4498c984e2641233c6d10af8256432e2e3f13aa4
[oota-llvm.git] / lib / CodeGen / TargetInstrInfoImpl.cpp
1 //===-- TargetInstrInfoImpl.cpp - Target Instruction Information ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the TargetInstrInfoImpl class, it just provides default
11 // implementations of various methods.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Target/TargetInstrInfo.h"
16 #include "llvm/CodeGen/MachineInstr.h"
17 using namespace llvm;
18
19 // commuteInstruction - The default implementation of this method just exchanges
20 // operand 1 and 2.
21 MachineInstr *TargetInstrInfoImpl::commuteInstruction(MachineInstr *MI) const {
22   assert(MI->getOperand(1).isRegister() && MI->getOperand(2).isRegister() &&
23          "This only knows how to commute register operands so far");
24   unsigned Reg1 = MI->getOperand(1).getReg();
25   unsigned Reg2 = MI->getOperand(2).getReg();
26   bool Reg1IsKill = MI->getOperand(1).isKill();
27   bool Reg2IsKill = MI->getOperand(2).isKill();
28   MI->getOperand(2).setReg(Reg1);
29   MI->getOperand(1).setReg(Reg2);
30   MI->getOperand(2).setIsKill(Reg1IsKill);
31   MI->getOperand(1).setIsKill(Reg2IsKill);
32   return MI;
33 }
34
35 bool TargetInstrInfoImpl::PredicateInstruction(MachineInstr *MI,
36                                                const std::vector<MachineOperand> &Pred) const {
37   bool MadeChange = false;
38   const TargetInstrDescriptor *TID = MI->getDesc();
39   if (TID->isPredicable()) {
40     for (unsigned j = 0, i = 0, e = MI->getNumOperands(); i != e; ++i) {
41       if (TID->OpInfo[i].isPredicate()) {
42         MachineOperand &MO = MI->getOperand(i);
43         if (MO.isReg()) {
44           MO.setReg(Pred[j].getReg());
45           MadeChange = true;
46         } else if (MO.isImm()) {
47           MO.setImm(Pred[j].getImm());
48           MadeChange = true;
49         } else if (MO.isMBB()) {
50           MO.setMBB(Pred[j].getMBB());
51           MadeChange = true;
52         }
53         ++j;
54       }
55     }
56   }
57   return MadeChange;
58 }