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