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" |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineInstr.h" |
Evan Cheng | 58dcb0e | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
| 23 | // commuteInstruction - The default implementation of this method just exchanges |
| 24 | // operand 1 and 2. |
Evan Cheng | 58dcb0e | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 25 | MachineInstr *TargetInstrInfoImpl::commuteInstruction(MachineInstr *MI, |
| 26 | bool NewMI) const { |
Evan Cheng | 498c290 | 2009-07-01 08:29:08 +0000 | [diff] [blame] | 27 | const TargetInstrDesc &TID = MI->getDesc(); |
| 28 | bool HasDef = TID.getNumDefs(); |
| 29 | unsigned Idx1 = HasDef ? 1 : 0; |
| 30 | unsigned Idx2 = HasDef ? 2 : 1; |
| 31 | |
| 32 | assert(MI->getOperand(Idx1).isReg() && MI->getOperand(Idx2).isReg() && |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 33 | "This only knows how to commute register operands so far"); |
Evan Cheng | 498c290 | 2009-07-01 08:29:08 +0000 | [diff] [blame] | 34 | unsigned Reg1 = MI->getOperand(Idx1).getReg(); |
| 35 | unsigned Reg2 = MI->getOperand(Idx2).getReg(); |
| 36 | bool Reg1IsKill = MI->getOperand(Idx1).isKill(); |
| 37 | bool Reg2IsKill = MI->getOperand(Idx2).isKill(); |
Evan Cheng | 58dcb0e | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 38 | bool ChangeReg0 = false; |
Evan Cheng | 498c290 | 2009-07-01 08:29:08 +0000 | [diff] [blame] | 39 | if (HasDef && MI->getOperand(0).getReg() == Reg1) { |
Evan Cheng | a4d16a1 | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 40 | // Must be two address instruction! |
| 41 | assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) && |
| 42 | "Expecting a two-address instruction!"); |
| 43 | Reg2IsKill = false; |
Evan Cheng | 58dcb0e | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 44 | ChangeReg0 = true; |
Evan Cheng | a4d16a1 | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 45 | } |
Evan Cheng | 58dcb0e | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 46 | |
| 47 | if (NewMI) { |
| 48 | // Create a new instruction. |
Evan Cheng | 498c290 | 2009-07-01 08:29:08 +0000 | [diff] [blame] | 49 | unsigned Reg0 = HasDef |
| 50 | ? (ChangeReg0 ? Reg2 : MI->getOperand(0).getReg()) : 0; |
| 51 | bool Reg0IsDead = HasDef ? MI->getOperand(0).isDead() : false; |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 52 | MachineFunction &MF = *MI->getParent()->getParent(); |
Evan Cheng | 498c290 | 2009-07-01 08:29:08 +0000 | [diff] [blame] | 53 | if (HasDef) |
| 54 | return BuildMI(MF, MI->getDebugLoc(), MI->getDesc()) |
| 55 | .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead)) |
| 56 | .addReg(Reg2, getKillRegState(Reg2IsKill)) |
| 57 | .addReg(Reg1, getKillRegState(Reg2IsKill)); |
| 58 | else |
| 59 | return BuildMI(MF, MI->getDebugLoc(), MI->getDesc()) |
| 60 | .addReg(Reg2, getKillRegState(Reg2IsKill)) |
| 61 | .addReg(Reg1, getKillRegState(Reg2IsKill)); |
Evan Cheng | 58dcb0e | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | if (ChangeReg0) |
| 65 | MI->getOperand(0).setReg(Reg2); |
Evan Cheng | 498c290 | 2009-07-01 08:29:08 +0000 | [diff] [blame] | 66 | MI->getOperand(Idx2).setReg(Reg1); |
| 67 | MI->getOperand(Idx1).setReg(Reg2); |
| 68 | MI->getOperand(Idx2).setIsKill(Reg1IsKill); |
| 69 | MI->getOperand(Idx1).setIsKill(Reg2IsKill); |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 70 | return MI; |
| 71 | } |
| 72 | |
Evan Cheng | 261ce1d | 2009-07-10 19:15:51 +0000 | [diff] [blame] | 73 | /// findCommutedOpIndices - If specified MI is commutable, return the two |
| 74 | /// operand indices that would swap value. Return true if the instruction |
| 75 | /// is not in a form which this routine understands. |
| 76 | bool TargetInstrInfoImpl::findCommutedOpIndices(MachineInstr *MI, |
| 77 | unsigned &SrcOpIdx1, |
| 78 | unsigned &SrcOpIdx2) const { |
Evan Cheng | 498c290 | 2009-07-01 08:29:08 +0000 | [diff] [blame] | 79 | const TargetInstrDesc &TID = MI->getDesc(); |
Evan Cheng | 261ce1d | 2009-07-10 19:15:51 +0000 | [diff] [blame] | 80 | if (!TID.isCommutable()) |
Evan Cheng | 498c290 | 2009-07-01 08:29:08 +0000 | [diff] [blame] | 81 | return false; |
Evan Cheng | 261ce1d | 2009-07-10 19:15:51 +0000 | [diff] [blame] | 82 | // This assumes v0 = op v1, v2 and commuting would swap v1 and v2. If this |
| 83 | // is not true, then the target must implement this. |
| 84 | SrcOpIdx1 = TID.getNumDefs(); |
| 85 | SrcOpIdx2 = SrcOpIdx1 + 1; |
| 86 | if (!MI->getOperand(SrcOpIdx1).isReg() || |
| 87 | !MI->getOperand(SrcOpIdx2).isReg()) |
| 88 | // No idea. |
| 89 | return false; |
| 90 | return true; |
Evan Cheng | f20db15 | 2008-02-15 18:21:33 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 94 | bool TargetInstrInfoImpl::PredicateInstruction(MachineInstr *MI, |
Owen Anderson | 44eb65c | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 95 | const SmallVectorImpl<MachineOperand> &Pred) const { |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 96 | bool MadeChange = false; |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 97 | const TargetInstrDesc &TID = MI->getDesc(); |
| 98 | if (!TID.isPredicable()) |
| 99 | return false; |
| 100 | |
| 101 | for (unsigned j = 0, i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 102 | if (TID.OpInfo[i].isPredicate()) { |
| 103 | MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 104 | if (MO.isReg()) { |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 105 | MO.setReg(Pred[j].getReg()); |
| 106 | MadeChange = true; |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 107 | } else if (MO.isImm()) { |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 108 | MO.setImm(Pred[j].getImm()); |
| 109 | MadeChange = true; |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 110 | } else if (MO.isMBB()) { |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 111 | MO.setMBB(Pred[j].getMBB()); |
| 112 | MadeChange = true; |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 113 | } |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 114 | ++j; |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | return MadeChange; |
| 118 | } |
Evan Cheng | ca1267c | 2008-03-31 20:40:39 +0000 | [diff] [blame] | 119 | |
| 120 | void TargetInstrInfoImpl::reMaterialize(MachineBasicBlock &MBB, |
| 121 | MachineBasicBlock::iterator I, |
| 122 | unsigned DestReg, |
| 123 | const MachineInstr *Orig) const { |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 124 | MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig); |
Evan Cheng | ca1267c | 2008-03-31 20:40:39 +0000 | [diff] [blame] | 125 | MI->getOperand(0).setReg(DestReg); |
| 126 | MBB.insert(I, MI); |
| 127 | } |
| 128 | |
Nicolas Geoffray | 52e724a | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 129 | unsigned |
| 130 | TargetInstrInfoImpl::GetFunctionSizeInBytes(const MachineFunction &MF) const { |
| 131 | unsigned FnSize = 0; |
| 132 | for (MachineFunction::const_iterator MBBI = MF.begin(), E = MF.end(); |
| 133 | MBBI != E; ++MBBI) { |
| 134 | const MachineBasicBlock &MBB = *MBBI; |
Evan Cheng | 3885578 | 2008-09-11 05:58:06 +0000 | [diff] [blame] | 135 | for (MachineBasicBlock::const_iterator I = MBB.begin(),E = MBB.end(); |
| 136 | I != E; ++I) |
Nicolas Geoffray | 52e724a | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 137 | FnSize += GetInstSizeInBytes(I); |
| 138 | } |
| 139 | return FnSize; |
| 140 | } |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 141 | |
| 142 | /// foldMemoryOperand - Attempt to fold a load or store of the specified stack |
| 143 | /// slot into the specified machine instruction for the specified operand(s). |
| 144 | /// If this is possible, a new instruction is returned with the specified |
| 145 | /// operand folded, otherwise NULL is returned. The client is responsible for |
| 146 | /// removing the old instruction and adding the new one in the instruction |
| 147 | /// stream. |
| 148 | MachineInstr* |
| 149 | TargetInstrInfo::foldMemoryOperand(MachineFunction &MF, |
| 150 | MachineInstr* MI, |
| 151 | const SmallVectorImpl<unsigned> &Ops, |
| 152 | int FrameIndex) const { |
| 153 | unsigned Flags = 0; |
| 154 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 155 | if (MI->getOperand(Ops[i]).isDef()) |
| 156 | Flags |= MachineMemOperand::MOStore; |
| 157 | else |
| 158 | Flags |= MachineMemOperand::MOLoad; |
| 159 | |
| 160 | // Ask the target to do the actual folding. |
| 161 | MachineInstr *NewMI = foldMemoryOperandImpl(MF, MI, Ops, FrameIndex); |
| 162 | if (!NewMI) return 0; |
| 163 | |
| 164 | assert((!(Flags & MachineMemOperand::MOStore) || |
| 165 | NewMI->getDesc().mayStore()) && |
| 166 | "Folded a def to a non-store!"); |
| 167 | assert((!(Flags & MachineMemOperand::MOLoad) || |
| 168 | NewMI->getDesc().mayLoad()) && |
| 169 | "Folded a use to a non-load!"); |
| 170 | const MachineFrameInfo &MFI = *MF.getFrameInfo(); |
| 171 | assert(MFI.getObjectOffset(FrameIndex) != -1); |
| 172 | MachineMemOperand MMO(PseudoSourceValue::getFixedStack(FrameIndex), |
| 173 | Flags, |
| 174 | MFI.getObjectOffset(FrameIndex), |
| 175 | MFI.getObjectSize(FrameIndex), |
| 176 | MFI.getObjectAlignment(FrameIndex)); |
| 177 | NewMI->addMemOperand(MF, MMO); |
| 178 | |
| 179 | return NewMI; |
| 180 | } |
| 181 | |
| 182 | /// foldMemoryOperand - Same as the previous version except it allows folding |
| 183 | /// of any load and store from / to any address, not just from a specific |
| 184 | /// stack slot. |
| 185 | MachineInstr* |
| 186 | TargetInstrInfo::foldMemoryOperand(MachineFunction &MF, |
| 187 | MachineInstr* MI, |
| 188 | const SmallVectorImpl<unsigned> &Ops, |
| 189 | MachineInstr* LoadMI) const { |
| 190 | assert(LoadMI->getDesc().canFoldAsLoad() && "LoadMI isn't foldable!"); |
| 191 | #ifndef NDEBUG |
| 192 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 193 | assert(MI->getOperand(Ops[i]).isUse() && "Folding load into def!"); |
| 194 | #endif |
| 195 | |
| 196 | // Ask the target to do the actual folding. |
| 197 | MachineInstr *NewMI = foldMemoryOperandImpl(MF, MI, Ops, LoadMI); |
| 198 | if (!NewMI) return 0; |
| 199 | |
| 200 | // Copy the memoperands from the load to the folded instruction. |
| 201 | for (std::list<MachineMemOperand>::iterator I = LoadMI->memoperands_begin(), |
| 202 | E = LoadMI->memoperands_end(); I != E; ++I) |
| 203 | NewMI->addMemOperand(MF, *I); |
| 204 | |
| 205 | return NewMI; |
| 206 | } |