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