Convert uses of std::vector in TargetInstrInfo to SmallVector. This change had to...
[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/ADT/SmallVector.h"
17 #include "llvm/CodeGen/MachineInstr.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 using namespace llvm;
20
21 // commuteInstruction - The default implementation of this method just exchanges
22 // operand 1 and 2.
23 MachineInstr *TargetInstrInfoImpl::commuteInstruction(MachineInstr *MI,
24                                                       bool NewMI) const {
25   assert(MI->getOperand(1).isRegister() && MI->getOperand(2).isRegister() &&
26          "This only knows how to commute register operands so far");
27   unsigned Reg1 = MI->getOperand(1).getReg();
28   unsigned Reg2 = MI->getOperand(2).getReg();
29   bool Reg1IsKill = MI->getOperand(1).isKill();
30   bool Reg2IsKill = MI->getOperand(2).isKill();
31   bool ChangeReg0 = false;
32   if (MI->getOperand(0).getReg() == Reg1) {
33     // Must be two address instruction!
34     assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) &&
35            "Expecting a two-address instruction!");
36     Reg2IsKill = false;
37     ChangeReg0 = true;
38   }
39
40   if (NewMI) {
41     // Create a new instruction.
42     unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
43     bool Reg0IsDead = MI->getOperand(0).isDead();
44     MachineFunction &MF = *MI->getParent()->getParent();
45     return BuildMI(MF, MI->getDesc())
46       .addReg(Reg0, true, false, false, Reg0IsDead)
47       .addReg(Reg2, false, false, Reg2IsKill)
48       .addReg(Reg1, false, false, Reg1IsKill);
49   }
50
51   if (ChangeReg0)
52     MI->getOperand(0).setReg(Reg2);
53   MI->getOperand(2).setReg(Reg1);
54   MI->getOperand(1).setReg(Reg2);
55   MI->getOperand(2).setIsKill(Reg1IsKill);
56   MI->getOperand(1).setIsKill(Reg2IsKill);
57   return MI;
58 }
59
60 /// CommuteChangesDestination - Return true if commuting the specified
61 /// instruction will also changes the destination operand. Also return the
62 /// current operand index of the would be new destination register by
63 /// reference. This can happen when the commutable instruction is also a
64 /// two-address instruction.
65 bool TargetInstrInfoImpl::CommuteChangesDestination(MachineInstr *MI,
66                                                     unsigned &OpIdx) const{
67   assert(MI->getOperand(1).isRegister() && MI->getOperand(2).isRegister() &&
68          "This only knows how to commute register operands so far");
69   if (MI->getOperand(0).getReg() == MI->getOperand(1).getReg()) {
70     // Must be two address instruction!
71     assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) &&
72            "Expecting a two-address instruction!");
73     OpIdx = 2;
74     return true;
75   }
76   return false;
77 }
78
79
80 bool TargetInstrInfoImpl::PredicateInstruction(MachineInstr *MI,
81                             const SmallVectorImpl<MachineOperand> &Pred) const {
82   bool MadeChange = false;
83   const TargetInstrDesc &TID = MI->getDesc();
84   if (!TID.isPredicable())
85     return false;
86   
87   for (unsigned j = 0, i = 0, e = MI->getNumOperands(); i != e; ++i) {
88     if (TID.OpInfo[i].isPredicate()) {
89       MachineOperand &MO = MI->getOperand(i);
90       if (MO.isReg()) {
91         MO.setReg(Pred[j].getReg());
92         MadeChange = true;
93       } else if (MO.isImm()) {
94         MO.setImm(Pred[j].getImm());
95         MadeChange = true;
96       } else if (MO.isMBB()) {
97         MO.setMBB(Pred[j].getMBB());
98         MadeChange = true;
99       }
100       ++j;
101     }
102   }
103   return MadeChange;
104 }
105
106 void TargetInstrInfoImpl::reMaterialize(MachineBasicBlock &MBB,
107                                         MachineBasicBlock::iterator I,
108                                         unsigned DestReg,
109                                         const MachineInstr *Orig) const {
110   MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
111   MI->getOperand(0).setReg(DestReg);
112   MBB.insert(I, MI);
113 }
114
115 unsigned
116 TargetInstrInfoImpl::GetFunctionSizeInBytes(const MachineFunction &MF) const {
117   unsigned FnSize = 0;
118   for (MachineFunction::const_iterator MBBI = MF.begin(), E = MF.end();
119        MBBI != E; ++MBBI) {
120     const MachineBasicBlock &MBB = *MBBI;
121     for (MachineBasicBlock::const_iterator I = MBB.begin(),E = MBB.end(); I != E; ++I)
122       FnSize += GetInstSizeInBytes(I);
123   }
124   return FnSize;
125 }