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" |
| 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(); |
Evan Cheng | 9cec00e | 2008-02-13 09:13:21 +0000 | [diff] [blame] | 28 | if (MI->getOperand(0).getReg() == Reg1) { |
Evan Cheng | a4d16a1 | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 29 | // Must be two address instruction! |
| 30 | assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) && |
| 31 | "Expecting a two-address instruction!"); |
| 32 | Reg2IsKill = false; |
| 33 | MI->getOperand(0).setReg(Reg2); |
| 34 | } |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 35 | MI->getOperand(2).setReg(Reg1); |
| 36 | MI->getOperand(1).setReg(Reg2); |
| 37 | MI->getOperand(2).setIsKill(Reg1IsKill); |
| 38 | MI->getOperand(1).setIsKill(Reg2IsKill); |
| 39 | return MI; |
| 40 | } |
| 41 | |
Evan Cheng | f20db15 | 2008-02-15 18:21:33 +0000 | [diff] [blame] | 42 | /// CommuteChangesDestination - Return true if commuting the specified |
| 43 | /// instruction will also changes the destination operand. Also return the |
| 44 | /// current operand index of the would be new destination register by |
| 45 | /// reference. This can happen when the commutable instruction is also a |
| 46 | /// two-address instruction. |
| 47 | bool TargetInstrInfoImpl::CommuteChangesDestination(MachineInstr *MI, |
| 48 | unsigned &OpIdx) const{ |
| 49 | assert(MI->getOperand(1).isRegister() && MI->getOperand(2).isRegister() && |
| 50 | "This only knows how to commute register operands so far"); |
| 51 | if (MI->getOperand(0).getReg() == MI->getOperand(1).getReg()) { |
| 52 | // Must be two address instruction! |
| 53 | assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) && |
| 54 | "Expecting a two-address instruction!"); |
| 55 | OpIdx = 2; |
| 56 | return true; |
| 57 | } |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 62 | bool TargetInstrInfoImpl::PredicateInstruction(MachineInstr *MI, |
Evan Cheng | f20db15 | 2008-02-15 18:21:33 +0000 | [diff] [blame] | 63 | const std::vector<MachineOperand> &Pred) const { |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 64 | bool MadeChange = false; |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 65 | const TargetInstrDesc &TID = MI->getDesc(); |
| 66 | if (!TID.isPredicable()) |
| 67 | return false; |
| 68 | |
| 69 | for (unsigned j = 0, i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 70 | if (TID.OpInfo[i].isPredicate()) { |
| 71 | MachineOperand &MO = MI->getOperand(i); |
| 72 | if (MO.isReg()) { |
| 73 | MO.setReg(Pred[j].getReg()); |
| 74 | MadeChange = true; |
| 75 | } else if (MO.isImm()) { |
| 76 | MO.setImm(Pred[j].getImm()); |
| 77 | MadeChange = true; |
| 78 | } else if (MO.isMBB()) { |
| 79 | MO.setMBB(Pred[j].getMBB()); |
| 80 | MadeChange = true; |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 81 | } |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 82 | ++j; |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | return MadeChange; |
| 86 | } |
Evan Cheng | ca1267c | 2008-03-31 20:40:39 +0000 | [diff] [blame] | 87 | |
| 88 | void TargetInstrInfoImpl::reMaterialize(MachineBasicBlock &MBB, |
| 89 | MachineBasicBlock::iterator I, |
| 90 | unsigned DestReg, |
| 91 | const MachineInstr *Orig) const { |
| 92 | MachineInstr *MI = Orig->clone(); |
| 93 | MI->getOperand(0).setReg(DestReg); |
| 94 | MBB.insert(I, MI); |
| 95 | } |
| 96 | |
Nicolas Geoffray | 52e724a | 2008-04-16 20:10:13 +0000 | [diff] [blame^] | 97 | unsigned |
| 98 | TargetInstrInfoImpl::GetFunctionSizeInBytes(const MachineFunction &MF) const { |
| 99 | unsigned FnSize = 0; |
| 100 | for (MachineFunction::const_iterator MBBI = MF.begin(), E = MF.end(); |
| 101 | MBBI != E; ++MBBI) { |
| 102 | const MachineBasicBlock &MBB = *MBBI; |
| 103 | for (MachineBasicBlock::const_iterator I = MBB.begin(),E = MBB.end(); I != E; ++I) |
| 104 | FnSize += GetInstSizeInBytes(I); |
| 105 | } |
| 106 | return FnSize; |
| 107 | } |