blob: ccfe4358911b3fc3f9176dd7b9c00176e282601f [file] [log] [blame]
Chris Lattner64105522008-01-01 01:03:04 +00001//===-- 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"
Owen Anderson44eb65c2008-08-14 22:49:33 +000016#include "llvm/ADT/SmallVector.h"
Chris Lattner64105522008-01-01 01:03:04 +000017#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng58dcb0e2008-06-16 07:33:11 +000018#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner64105522008-01-01 01:03:04 +000019using namespace llvm;
20
21// commuteInstruction - The default implementation of this method just exchanges
22// operand 1 and 2.
Evan Cheng58dcb0e2008-06-16 07:33:11 +000023MachineInstr *TargetInstrInfoImpl::commuteInstruction(MachineInstr *MI,
24 bool NewMI) const {
Chris Lattner64105522008-01-01 01:03:04 +000025 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();
Evan Cheng58dcb0e2008-06-16 07:33:11 +000031 bool ChangeReg0 = false;
Evan Cheng9cec00e2008-02-13 09:13:21 +000032 if (MI->getOperand(0).getReg() == Reg1) {
Evan Chenga4d16a12008-02-13 02:46:49 +000033 // Must be two address instruction!
34 assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) &&
35 "Expecting a two-address instruction!");
36 Reg2IsKill = false;
Evan Cheng58dcb0e2008-06-16 07:33:11 +000037 ChangeReg0 = true;
Evan Chenga4d16a12008-02-13 02:46:49 +000038 }
Evan Cheng58dcb0e2008-06-16 07:33:11 +000039
40 if (NewMI) {
41 // Create a new instruction.
42 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
43 bool Reg0IsDead = MI->getOperand(0).isDead();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000044 MachineFunction &MF = *MI->getParent()->getParent();
45 return BuildMI(MF, MI->getDesc())
46 .addReg(Reg0, true, false, false, Reg0IsDead)
Evan Cheng58dcb0e2008-06-16 07:33:11 +000047 .addReg(Reg2, false, false, Reg2IsKill)
48 .addReg(Reg1, false, false, Reg1IsKill);
49 }
50
51 if (ChangeReg0)
52 MI->getOperand(0).setReg(Reg2);
Chris Lattner64105522008-01-01 01:03:04 +000053 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
Evan Chengf20db152008-02-15 18:21:33 +000060/// 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.
65bool 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
Chris Lattner64105522008-01-01 01:03:04 +000080bool TargetInstrInfoImpl::PredicateInstruction(MachineInstr *MI,
Owen Anderson44eb65c2008-08-14 22:49:33 +000081 const SmallVectorImpl<MachineOperand> &Pred) const {
Chris Lattner64105522008-01-01 01:03:04 +000082 bool MadeChange = false;
Chris Lattner749c6f62008-01-07 07:27:27 +000083 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);
Dan Gohman014278e2008-09-13 17:58:21 +000090 if (MO.isRegister()) {
Chris Lattner749c6f62008-01-07 07:27:27 +000091 MO.setReg(Pred[j].getReg());
92 MadeChange = true;
Dan Gohman014278e2008-09-13 17:58:21 +000093 } else if (MO.isImmediate()) {
Chris Lattner749c6f62008-01-07 07:27:27 +000094 MO.setImm(Pred[j].getImm());
95 MadeChange = true;
Dan Gohman014278e2008-09-13 17:58:21 +000096 } else if (MO.isMachineBasicBlock()) {
Chris Lattner749c6f62008-01-07 07:27:27 +000097 MO.setMBB(Pred[j].getMBB());
98 MadeChange = true;
Chris Lattner64105522008-01-01 01:03:04 +000099 }
Chris Lattner749c6f62008-01-07 07:27:27 +0000100 ++j;
Chris Lattner64105522008-01-01 01:03:04 +0000101 }
102 }
103 return MadeChange;
104}
Evan Chengca1267c2008-03-31 20:40:39 +0000105
106void TargetInstrInfoImpl::reMaterialize(MachineBasicBlock &MBB,
107 MachineBasicBlock::iterator I,
108 unsigned DestReg,
109 const MachineInstr *Orig) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000110 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
Evan Chengca1267c2008-03-31 20:40:39 +0000111 MI->getOperand(0).setReg(DestReg);
112 MBB.insert(I, MI);
113}
114
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000115unsigned
116TargetInstrInfoImpl::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;
Evan Cheng38855782008-09-11 05:58:06 +0000121 for (MachineBasicBlock::const_iterator I = MBB.begin(),E = MBB.end();
122 I != E; ++I)
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000123 FnSize += GetInstSizeInBytes(I);
124 }
125 return FnSize;
126}