Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 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" |
Owen Anderson | 44eb65c | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineInstr.h" |
Evan Cheng | 58dcb0e | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | |
| 21 | // commuteInstruction - The default implementation of this method just exchanges |
| 22 | // operand 1 and 2. |
Evan Cheng | 58dcb0e | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 23 | MachineInstr *TargetInstrInfoImpl::commuteInstruction(MachineInstr *MI, |
| 24 | bool NewMI) const { |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 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(); |
Evan Cheng | 58dcb0e | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 31 | bool ChangeReg0 = false; |
Evan Cheng | 9cec00e | 2008-02-13 09:13:21 +0000 | [diff] [blame] | 32 | if (MI->getOperand(0).getReg() == Reg1) { |
Evan Cheng | a4d16a1 | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 33 | // Must be two address instruction! |
| 34 | assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) && |
| 35 | "Expecting a two-address instruction!"); |
| 36 | Reg2IsKill = false; |
Evan Cheng | 58dcb0e | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 37 | ChangeReg0 = true; |
Evan Cheng | a4d16a1 | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 38 | } |
Evan Cheng | 58dcb0e | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 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(); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 44 | MachineFunction &MF = *MI->getParent()->getParent(); |
| 45 | return BuildMI(MF, MI->getDesc()) |
| 46 | .addReg(Reg0, true, false, false, Reg0IsDead) |
Evan Cheng | 58dcb0e | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 47 | .addReg(Reg2, false, false, Reg2IsKill) |
| 48 | .addReg(Reg1, false, false, Reg1IsKill); |
| 49 | } |
| 50 | |
| 51 | if (ChangeReg0) |
| 52 | MI->getOperand(0).setReg(Reg2); |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 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 | |
Evan Cheng | f20db15 | 2008-02-15 18:21:33 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 80 | bool TargetInstrInfoImpl::PredicateInstruction(MachineInstr *MI, |
Owen Anderson | 44eb65c | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 81 | const SmallVectorImpl<MachineOperand> &Pred) const { |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 82 | bool MadeChange = false; |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 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; |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 99 | } |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 100 | ++j; |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | return MadeChange; |
| 104 | } |
Evan Cheng | ca1267c | 2008-03-31 20:40:39 +0000 | [diff] [blame] | 105 | |
| 106 | void TargetInstrInfoImpl::reMaterialize(MachineBasicBlock &MBB, |
| 107 | MachineBasicBlock::iterator I, |
| 108 | unsigned DestReg, |
| 109 | const MachineInstr *Orig) const { |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 110 | MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig); |
Evan Cheng | ca1267c | 2008-03-31 20:40:39 +0000 | [diff] [blame] | 111 | MI->getOperand(0).setReg(DestReg); |
| 112 | MBB.insert(I, MI); |
| 113 | } |
| 114 | |
Nicolas Geoffray | 52e724a | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 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 | } |