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" |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame] | 16 | #include "llvm/Target/TargetLowering.h" |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 17 | #include "llvm/Target/TargetMachine.h" |
| 18 | #include "llvm/Target/TargetRegisterInfo.h" |
Owen Anderson | 44eb65c | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallVector.h" |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineInstr.h" |
Evan Cheng | 58dcb0e | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineMemOperand.h" |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Andrew Trick | 6b12072 | 2010-12-08 20:04:29 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/ScoreboardHazardRecognizer.h" |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Nick Lewycky | 028700f | 2011-12-15 22:58:58 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCInstrItineraries.h" |
Andrew Trick | c8bfd1d | 2011-01-21 05:51:33 +0000 | [diff] [blame] | 28 | #include "llvm/Support/CommandLine.h" |
Jakob Stoklund Olesen | 9fac415 | 2010-07-13 00:23:30 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Debug.h" |
Evan Cheng | 34c7509 | 2009-07-10 23:26:12 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ErrorHandling.h" |
| 31 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 32 | using namespace llvm; |
| 33 | |
Andrew Trick | c8bfd1d | 2011-01-21 05:51:33 +0000 | [diff] [blame] | 34 | static cl::opt<bool> DisableHazardRecognizer( |
| 35 | "disable-sched-hazard", cl::Hidden, cl::init(false), |
| 36 | cl::desc("Disable hazard detection during preRA scheduling")); |
| 37 | |
Evan Cheng | 4d54e5b | 2010-06-22 01:18:16 +0000 | [diff] [blame] | 38 | /// ReplaceTailWithBranchTo - Delete the instruction OldInst and everything |
| 39 | /// after it, replacing it with an unconditional branch to NewDest. |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame] | 40 | void |
| 41 | TargetInstrInfoImpl::ReplaceTailWithBranchTo(MachineBasicBlock::iterator Tail, |
| 42 | MachineBasicBlock *NewDest) const { |
| 43 | MachineBasicBlock *MBB = Tail->getParent(); |
| 44 | |
| 45 | // Remove all the old successors of MBB from the CFG. |
| 46 | while (!MBB->succ_empty()) |
| 47 | MBB->removeSuccessor(MBB->succ_begin()); |
| 48 | |
| 49 | // Remove all the dead instructions from the end of MBB. |
| 50 | MBB->erase(Tail, MBB->end()); |
| 51 | |
| 52 | // If MBB isn't immediately before MBB, insert a branch to it. |
| 53 | if (++MachineFunction::iterator(MBB) != MachineFunction::iterator(NewDest)) |
| 54 | InsertBranch(*MBB, NewDest, 0, SmallVector<MachineOperand, 0>(), |
| 55 | Tail->getDebugLoc()); |
| 56 | MBB->addSuccessor(NewDest); |
| 57 | } |
| 58 | |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 59 | // commuteInstruction - The default implementation of this method just exchanges |
Evan Cheng | 34c7509 | 2009-07-10 23:26:12 +0000 | [diff] [blame] | 60 | // the two operands returned by findCommutedOpIndices. |
Evan Cheng | 58dcb0e | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 61 | MachineInstr *TargetInstrInfoImpl::commuteInstruction(MachineInstr *MI, |
| 62 | bool NewMI) const { |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 63 | const MCInstrDesc &MCID = MI->getDesc(); |
| 64 | bool HasDef = MCID.getNumDefs(); |
Evan Cheng | 34c7509 | 2009-07-10 23:26:12 +0000 | [diff] [blame] | 65 | if (HasDef && !MI->getOperand(0).isReg()) |
| 66 | // No idea how to commute this instruction. Target should implement its own. |
| 67 | return 0; |
| 68 | unsigned Idx1, Idx2; |
| 69 | if (!findCommutedOpIndices(MI, Idx1, Idx2)) { |
| 70 | std::string msg; |
| 71 | raw_string_ostream Msg(msg); |
| 72 | Msg << "Don't know how to commute: " << *MI; |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 73 | report_fatal_error(Msg.str()); |
Evan Cheng | 34c7509 | 2009-07-10 23:26:12 +0000 | [diff] [blame] | 74 | } |
Evan Cheng | 498c290 | 2009-07-01 08:29:08 +0000 | [diff] [blame] | 75 | |
| 76 | assert(MI->getOperand(Idx1).isReg() && MI->getOperand(Idx2).isReg() && |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 77 | "This only knows how to commute register operands so far"); |
Evan Cheng | cb08f18 | 2011-08-22 23:04:56 +0000 | [diff] [blame] | 78 | unsigned Reg0 = HasDef ? MI->getOperand(0).getReg() : 0; |
Evan Cheng | 498c290 | 2009-07-01 08:29:08 +0000 | [diff] [blame] | 79 | unsigned Reg1 = MI->getOperand(Idx1).getReg(); |
| 80 | unsigned Reg2 = MI->getOperand(Idx2).getReg(); |
Pete Cooper | 442ee9c | 2012-03-28 17:02:22 +0000 | [diff] [blame] | 81 | unsigned SubReg0 = HasDef ? MI->getOperand(0).getSubReg() : 0; |
| 82 | unsigned SubReg1 = MI->getOperand(Idx1).getSubReg(); |
| 83 | unsigned SubReg2 = MI->getOperand(Idx2).getSubReg(); |
Evan Cheng | 498c290 | 2009-07-01 08:29:08 +0000 | [diff] [blame] | 84 | bool Reg1IsKill = MI->getOperand(Idx1).isKill(); |
| 85 | bool Reg2IsKill = MI->getOperand(Idx2).isKill(); |
Evan Cheng | cb08f18 | 2011-08-22 23:04:56 +0000 | [diff] [blame] | 86 | // If destination is tied to either of the commuted source register, then |
| 87 | // it must be updated. |
| 88 | if (HasDef && Reg0 == Reg1 && |
| 89 | MI->getDesc().getOperandConstraint(Idx1, MCOI::TIED_TO) == 0) { |
Evan Cheng | a4d16a1 | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 90 | Reg2IsKill = false; |
Evan Cheng | cb08f18 | 2011-08-22 23:04:56 +0000 | [diff] [blame] | 91 | Reg0 = Reg2; |
Pete Cooper | 442ee9c | 2012-03-28 17:02:22 +0000 | [diff] [blame] | 92 | SubReg0 = SubReg2; |
Evan Cheng | cb08f18 | 2011-08-22 23:04:56 +0000 | [diff] [blame] | 93 | } else if (HasDef && Reg0 == Reg2 && |
| 94 | MI->getDesc().getOperandConstraint(Idx2, MCOI::TIED_TO) == 0) { |
| 95 | Reg1IsKill = false; |
| 96 | Reg0 = Reg1; |
Pete Cooper | 442ee9c | 2012-03-28 17:02:22 +0000 | [diff] [blame] | 97 | SubReg0 = SubReg1; |
Evan Cheng | a4d16a1 | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 98 | } |
Evan Cheng | 58dcb0e | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 99 | |
| 100 | if (NewMI) { |
| 101 | // Create a new instruction. |
Evan Cheng | 498c290 | 2009-07-01 08:29:08 +0000 | [diff] [blame] | 102 | bool Reg0IsDead = HasDef ? MI->getOperand(0).isDead() : false; |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 103 | MachineFunction &MF = *MI->getParent()->getParent(); |
Evan Cheng | 498c290 | 2009-07-01 08:29:08 +0000 | [diff] [blame] | 104 | if (HasDef) |
| 105 | return BuildMI(MF, MI->getDebugLoc(), MI->getDesc()) |
Pete Cooper | 442ee9c | 2012-03-28 17:02:22 +0000 | [diff] [blame] | 106 | .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead), SubReg0) |
| 107 | .addReg(Reg2, getKillRegState(Reg2IsKill), SubReg2) |
| 108 | .addReg(Reg1, getKillRegState(Reg1IsKill), SubReg1); |
Evan Cheng | 498c290 | 2009-07-01 08:29:08 +0000 | [diff] [blame] | 109 | else |
| 110 | return BuildMI(MF, MI->getDebugLoc(), MI->getDesc()) |
Pete Cooper | 442ee9c | 2012-03-28 17:02:22 +0000 | [diff] [blame] | 111 | .addReg(Reg2, getKillRegState(Reg2IsKill), SubReg2) |
| 112 | .addReg(Reg1, getKillRegState(Reg1IsKill), SubReg1); |
Evan Cheng | 58dcb0e | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Pete Cooper | 442ee9c | 2012-03-28 17:02:22 +0000 | [diff] [blame] | 115 | if (HasDef) { |
Evan Cheng | cb08f18 | 2011-08-22 23:04:56 +0000 | [diff] [blame] | 116 | MI->getOperand(0).setReg(Reg0); |
Pete Cooper | 442ee9c | 2012-03-28 17:02:22 +0000 | [diff] [blame] | 117 | MI->getOperand(0).setSubReg(SubReg0); |
| 118 | } |
Evan Cheng | 498c290 | 2009-07-01 08:29:08 +0000 | [diff] [blame] | 119 | MI->getOperand(Idx2).setReg(Reg1); |
| 120 | MI->getOperand(Idx1).setReg(Reg2); |
Pete Cooper | 442ee9c | 2012-03-28 17:02:22 +0000 | [diff] [blame] | 121 | MI->getOperand(Idx2).setSubReg(SubReg1); |
| 122 | MI->getOperand(Idx1).setSubReg(SubReg2); |
Evan Cheng | 498c290 | 2009-07-01 08:29:08 +0000 | [diff] [blame] | 123 | MI->getOperand(Idx2).setIsKill(Reg1IsKill); |
| 124 | MI->getOperand(Idx1).setIsKill(Reg2IsKill); |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 125 | return MI; |
| 126 | } |
| 127 | |
Evan Cheng | 261ce1d | 2009-07-10 19:15:51 +0000 | [diff] [blame] | 128 | /// findCommutedOpIndices - If specified MI is commutable, return the two |
| 129 | /// operand indices that would swap value. Return true if the instruction |
| 130 | /// is not in a form which this routine understands. |
| 131 | bool TargetInstrInfoImpl::findCommutedOpIndices(MachineInstr *MI, |
| 132 | unsigned &SrcOpIdx1, |
| 133 | unsigned &SrcOpIdx2) const { |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 134 | assert(!MI->isBundle() && |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 135 | "TargetInstrInfoImpl::findCommutedOpIndices() can't handle bundles"); |
| 136 | |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 137 | const MCInstrDesc &MCID = MI->getDesc(); |
| 138 | if (!MCID.isCommutable()) |
Evan Cheng | 498c290 | 2009-07-01 08:29:08 +0000 | [diff] [blame] | 139 | return false; |
Evan Cheng | 261ce1d | 2009-07-10 19:15:51 +0000 | [diff] [blame] | 140 | // This assumes v0 = op v1, v2 and commuting would swap v1 and v2. If this |
| 141 | // is not true, then the target must implement this. |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 142 | SrcOpIdx1 = MCID.getNumDefs(); |
Evan Cheng | 261ce1d | 2009-07-10 19:15:51 +0000 | [diff] [blame] | 143 | SrcOpIdx2 = SrcOpIdx1 + 1; |
| 144 | if (!MI->getOperand(SrcOpIdx1).isReg() || |
| 145 | !MI->getOperand(SrcOpIdx2).isReg()) |
| 146 | // No idea. |
| 147 | return false; |
| 148 | return true; |
Evan Cheng | f20db15 | 2008-02-15 18:21:33 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | |
Evan Cheng | 32f9763 | 2011-12-09 06:41:08 +0000 | [diff] [blame] | 152 | bool |
| 153 | TargetInstrInfoImpl::isUnpredicatedTerminator(const MachineInstr *MI) const { |
| 154 | if (!MI->isTerminator()) return false; |
| 155 | |
| 156 | // Conditional branch is a special case. |
| 157 | if (MI->isBranch() && !MI->isBarrier()) |
| 158 | return true; |
| 159 | if (!MI->isPredicable()) |
| 160 | return true; |
| 161 | return !isPredicated(MI); |
| 162 | } |
| 163 | |
| 164 | |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 165 | bool TargetInstrInfoImpl::PredicateInstruction(MachineInstr *MI, |
Owen Anderson | 44eb65c | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 166 | const SmallVectorImpl<MachineOperand> &Pred) const { |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 167 | bool MadeChange = false; |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 168 | |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 169 | assert(!MI->isBundle() && |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 170 | "TargetInstrInfoImpl::PredicateInstruction() can't handle bundles"); |
| 171 | |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 172 | const MCInstrDesc &MCID = MI->getDesc(); |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 173 | if (!MI->isPredicable()) |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 174 | return false; |
Andrew Trick | 6b12072 | 2010-12-08 20:04:29 +0000 | [diff] [blame] | 175 | |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 176 | for (unsigned j = 0, i = 0, e = MI->getNumOperands(); i != e; ++i) { |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 177 | if (MCID.OpInfo[i].isPredicate()) { |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 178 | MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 179 | if (MO.isReg()) { |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 180 | MO.setReg(Pred[j].getReg()); |
| 181 | MadeChange = true; |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 182 | } else if (MO.isImm()) { |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 183 | MO.setImm(Pred[j].getImm()); |
| 184 | MadeChange = true; |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 185 | } else if (MO.isMBB()) { |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 186 | MO.setMBB(Pred[j].getMBB()); |
| 187 | MadeChange = true; |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 188 | } |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 189 | ++j; |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | return MadeChange; |
| 193 | } |
Evan Cheng | ca1267c | 2008-03-31 20:40:39 +0000 | [diff] [blame] | 194 | |
Jakob Stoklund Olesen | 2df3f58 | 2011-08-08 20:53:24 +0000 | [diff] [blame] | 195 | bool TargetInstrInfoImpl::hasLoadFromStackSlot(const MachineInstr *MI, |
| 196 | const MachineMemOperand *&MMO, |
| 197 | int &FrameIndex) const { |
| 198 | for (MachineInstr::mmo_iterator o = MI->memoperands_begin(), |
| 199 | oe = MI->memoperands_end(); |
| 200 | o != oe; |
| 201 | ++o) { |
| 202 | if ((*o)->isLoad() && (*o)->getValue()) |
| 203 | if (const FixedStackPseudoSourceValue *Value = |
| 204 | dyn_cast<const FixedStackPseudoSourceValue>((*o)->getValue())) { |
| 205 | FrameIndex = Value->getFrameIndex(); |
| 206 | MMO = *o; |
| 207 | return true; |
| 208 | } |
| 209 | } |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | bool TargetInstrInfoImpl::hasStoreToStackSlot(const MachineInstr *MI, |
| 214 | const MachineMemOperand *&MMO, |
| 215 | int &FrameIndex) const { |
| 216 | for (MachineInstr::mmo_iterator o = MI->memoperands_begin(), |
| 217 | oe = MI->memoperands_end(); |
| 218 | o != oe; |
| 219 | ++o) { |
| 220 | if ((*o)->isStore() && (*o)->getValue()) |
| 221 | if (const FixedStackPseudoSourceValue *Value = |
| 222 | dyn_cast<const FixedStackPseudoSourceValue>((*o)->getValue())) { |
| 223 | FrameIndex = Value->getFrameIndex(); |
| 224 | MMO = *o; |
| 225 | return true; |
| 226 | } |
| 227 | } |
| 228 | return false; |
| 229 | } |
| 230 | |
Evan Cheng | ca1267c | 2008-03-31 20:40:39 +0000 | [diff] [blame] | 231 | void TargetInstrInfoImpl::reMaterialize(MachineBasicBlock &MBB, |
| 232 | MachineBasicBlock::iterator I, |
| 233 | unsigned DestReg, |
Evan Cheng | 3784453 | 2009-07-16 09:20:10 +0000 | [diff] [blame] | 234 | unsigned SubIdx, |
Evan Cheng | d57cdd5 | 2009-11-14 02:55:43 +0000 | [diff] [blame] | 235 | const MachineInstr *Orig, |
Jakob Stoklund Olesen | 9edf7de | 2010-06-02 22:47:25 +0000 | [diff] [blame] | 236 | const TargetRegisterInfo &TRI) const { |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 237 | MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig); |
Jakob Stoklund Olesen | 9edf7de | 2010-06-02 22:47:25 +0000 | [diff] [blame] | 238 | MI->substituteRegister(MI->getOperand(0).getReg(), DestReg, SubIdx, TRI); |
Evan Cheng | ca1267c | 2008-03-31 20:40:39 +0000 | [diff] [blame] | 239 | MBB.insert(I, MI); |
| 240 | } |
| 241 | |
Evan Cheng | 9fe2009 | 2011-01-20 08:34:58 +0000 | [diff] [blame] | 242 | bool |
| 243 | TargetInstrInfoImpl::produceSameValue(const MachineInstr *MI0, |
| 244 | const MachineInstr *MI1, |
| 245 | const MachineRegisterInfo *MRI) const { |
Evan Cheng | 506049f | 2010-03-03 01:44:33 +0000 | [diff] [blame] | 246 | return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs); |
| 247 | } |
| 248 | |
Jakob Stoklund Olesen | 30ac046 | 2010-01-06 23:47:07 +0000 | [diff] [blame] | 249 | MachineInstr *TargetInstrInfoImpl::duplicate(MachineInstr *Orig, |
| 250 | MachineFunction &MF) const { |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 251 | assert(!Orig->isNotDuplicable() && |
Jakob Stoklund Olesen | 30ac046 | 2010-01-06 23:47:07 +0000 | [diff] [blame] | 252 | "Instruction cannot be duplicated"); |
| 253 | return MF.CloneMachineInstr(Orig); |
| 254 | } |
| 255 | |
Jakob Stoklund Olesen | 1f32340 | 2010-07-09 20:43:13 +0000 | [diff] [blame] | 256 | // If the COPY instruction in MI can be folded to a stack operation, return |
| 257 | // the register class to use. |
| 258 | static const TargetRegisterClass *canFoldCopy(const MachineInstr *MI, |
| 259 | unsigned FoldIdx) { |
| 260 | assert(MI->isCopy() && "MI must be a COPY instruction"); |
| 261 | if (MI->getNumOperands() != 2) |
| 262 | return 0; |
| 263 | assert(FoldIdx<2 && "FoldIdx refers no nonexistent operand"); |
| 264 | |
| 265 | const MachineOperand &FoldOp = MI->getOperand(FoldIdx); |
| 266 | const MachineOperand &LiveOp = MI->getOperand(1-FoldIdx); |
| 267 | |
| 268 | if (FoldOp.getSubReg() || LiveOp.getSubReg()) |
| 269 | return 0; |
| 270 | |
| 271 | unsigned FoldReg = FoldOp.getReg(); |
| 272 | unsigned LiveReg = LiveOp.getReg(); |
| 273 | |
| 274 | assert(TargetRegisterInfo::isVirtualRegister(FoldReg) && |
| 275 | "Cannot fold physregs"); |
| 276 | |
| 277 | const MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo(); |
| 278 | const TargetRegisterClass *RC = MRI.getRegClass(FoldReg); |
| 279 | |
| 280 | if (TargetRegisterInfo::isPhysicalRegister(LiveOp.getReg())) |
| 281 | return RC->contains(LiveOp.getReg()) ? RC : 0; |
| 282 | |
Jakob Stoklund Olesen | fa226bc | 2011-06-02 05:43:46 +0000 | [diff] [blame] | 283 | if (RC->hasSubClassEq(MRI.getRegClass(LiveReg))) |
Jakob Stoklund Olesen | 1f32340 | 2010-07-09 20:43:13 +0000 | [diff] [blame] | 284 | return RC; |
| 285 | |
| 286 | // FIXME: Allow folding when register classes are memory compatible. |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | bool TargetInstrInfoImpl:: |
| 291 | canFoldMemoryOperand(const MachineInstr *MI, |
| 292 | const SmallVectorImpl<unsigned> &Ops) const { |
| 293 | return MI->isCopy() && Ops.size() == 1 && canFoldCopy(MI, Ops[0]); |
| 294 | } |
| 295 | |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 296 | /// foldMemoryOperand - Attempt to fold a load or store of the specified stack |
| 297 | /// slot into the specified machine instruction for the specified operand(s). |
| 298 | /// If this is possible, a new instruction is returned with the specified |
| 299 | /// operand folded, otherwise NULL is returned. The client is responsible for |
| 300 | /// removing the old instruction and adding the new one in the instruction |
| 301 | /// stream. |
| 302 | MachineInstr* |
Jakob Stoklund Olesen | e05442d | 2010-07-09 17:29:08 +0000 | [diff] [blame] | 303 | TargetInstrInfo::foldMemoryOperand(MachineBasicBlock::iterator MI, |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 304 | const SmallVectorImpl<unsigned> &Ops, |
Jakob Stoklund Olesen | 1f32340 | 2010-07-09 20:43:13 +0000 | [diff] [blame] | 305 | int FI) const { |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 306 | unsigned Flags = 0; |
| 307 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 308 | if (MI->getOperand(Ops[i]).isDef()) |
| 309 | Flags |= MachineMemOperand::MOStore; |
| 310 | else |
| 311 | Flags |= MachineMemOperand::MOLoad; |
| 312 | |
Jakob Stoklund Olesen | 1f32340 | 2010-07-09 20:43:13 +0000 | [diff] [blame] | 313 | MachineBasicBlock *MBB = MI->getParent(); |
| 314 | assert(MBB && "foldMemoryOperand needs an inserted instruction"); |
| 315 | MachineFunction &MF = *MBB->getParent(); |
Jakob Stoklund Olesen | e05442d | 2010-07-09 17:29:08 +0000 | [diff] [blame] | 316 | |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 317 | // Ask the target to do the actual folding. |
Jakob Stoklund Olesen | 9fac415 | 2010-07-13 00:23:30 +0000 | [diff] [blame] | 318 | if (MachineInstr *NewMI = foldMemoryOperandImpl(MF, MI, Ops, FI)) { |
| 319 | // Add a memory operand, foldMemoryOperandImpl doesn't do that. |
| 320 | assert((!(Flags & MachineMemOperand::MOStore) || |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 321 | NewMI->mayStore()) && |
Jakob Stoklund Olesen | 9fac415 | 2010-07-13 00:23:30 +0000 | [diff] [blame] | 322 | "Folded a def to a non-store!"); |
| 323 | assert((!(Flags & MachineMemOperand::MOLoad) || |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 324 | NewMI->mayLoad()) && |
Jakob Stoklund Olesen | 9fac415 | 2010-07-13 00:23:30 +0000 | [diff] [blame] | 325 | "Folded a use to a non-load!"); |
| 326 | const MachineFrameInfo &MFI = *MF.getFrameInfo(); |
| 327 | assert(MFI.getObjectOffset(FI) != -1); |
| 328 | MachineMemOperand *MMO = |
Jay Foad | f4a5084 | 2011-11-15 07:51:13 +0000 | [diff] [blame] | 329 | MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI), |
Chris Lattner | 93a95ae | 2010-09-21 04:46:39 +0000 | [diff] [blame] | 330 | Flags, MFI.getObjectSize(FI), |
Jakob Stoklund Olesen | 9fac415 | 2010-07-13 00:23:30 +0000 | [diff] [blame] | 331 | MFI.getObjectAlignment(FI)); |
| 332 | NewMI->addMemOperand(MF, MMO); |
Jakob Stoklund Olesen | 1f32340 | 2010-07-09 20:43:13 +0000 | [diff] [blame] | 333 | |
Jakob Stoklund Olesen | 1f32340 | 2010-07-09 20:43:13 +0000 | [diff] [blame] | 334 | // FIXME: change foldMemoryOperandImpl semantics to also insert NewMI. |
Jakob Stoklund Olesen | 9fac415 | 2010-07-13 00:23:30 +0000 | [diff] [blame] | 335 | return MBB->insert(MI, NewMI); |
Jakob Stoklund Olesen | 1f32340 | 2010-07-09 20:43:13 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Jakob Stoklund Olesen | 9fac415 | 2010-07-13 00:23:30 +0000 | [diff] [blame] | 338 | // Straight COPY may fold as load/store. |
| 339 | if (!MI->isCopy() || Ops.size() != 1) |
| 340 | return 0; |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 341 | |
Jakob Stoklund Olesen | 9fac415 | 2010-07-13 00:23:30 +0000 | [diff] [blame] | 342 | const TargetRegisterClass *RC = canFoldCopy(MI, Ops[0]); |
| 343 | if (!RC) |
| 344 | return 0; |
Jakob Stoklund Olesen | e05442d | 2010-07-09 17:29:08 +0000 | [diff] [blame] | 345 | |
Jakob Stoklund Olesen | 9fac415 | 2010-07-13 00:23:30 +0000 | [diff] [blame] | 346 | const MachineOperand &MO = MI->getOperand(1-Ops[0]); |
| 347 | MachineBasicBlock::iterator Pos = MI; |
| 348 | const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo(); |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 349 | |
Jakob Stoklund Olesen | 9fac415 | 2010-07-13 00:23:30 +0000 | [diff] [blame] | 350 | if (Flags == MachineMemOperand::MOStore) |
| 351 | storeRegToStackSlot(*MBB, Pos, MO.getReg(), MO.isKill(), FI, RC, TRI); |
| 352 | else |
| 353 | loadRegFromStackSlot(*MBB, Pos, MO.getReg(), FI, RC, TRI); |
| 354 | return --Pos; |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | /// foldMemoryOperand - Same as the previous version except it allows folding |
| 358 | /// of any load and store from / to any address, not just from a specific |
| 359 | /// stack slot. |
| 360 | MachineInstr* |
Jakob Stoklund Olesen | e05442d | 2010-07-09 17:29:08 +0000 | [diff] [blame] | 361 | TargetInstrInfo::foldMemoryOperand(MachineBasicBlock::iterator MI, |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 362 | const SmallVectorImpl<unsigned> &Ops, |
| 363 | MachineInstr* LoadMI) const { |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 364 | assert(LoadMI->canFoldAsLoad() && "LoadMI isn't foldable!"); |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 365 | #ifndef NDEBUG |
| 366 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 367 | assert(MI->getOperand(Ops[i]).isUse() && "Folding load into def!"); |
| 368 | #endif |
Jakob Stoklund Olesen | e05442d | 2010-07-09 17:29:08 +0000 | [diff] [blame] | 369 | MachineBasicBlock &MBB = *MI->getParent(); |
| 370 | MachineFunction &MF = *MBB.getParent(); |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 371 | |
| 372 | // Ask the target to do the actual folding. |
| 373 | MachineInstr *NewMI = foldMemoryOperandImpl(MF, MI, Ops, LoadMI); |
| 374 | if (!NewMI) return 0; |
| 375 | |
Jakob Stoklund Olesen | e05442d | 2010-07-09 17:29:08 +0000 | [diff] [blame] | 376 | NewMI = MBB.insert(MI, NewMI); |
| 377 | |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 378 | // Copy the memoperands from the load to the folded instruction. |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 379 | NewMI->setMemRefs(LoadMI->memoperands_begin(), |
| 380 | LoadMI->memoperands_end()); |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 381 | |
| 382 | return NewMI; |
| 383 | } |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 384 | |
Evan Cheng | 44acc24 | 2010-06-12 00:11:53 +0000 | [diff] [blame] | 385 | bool TargetInstrInfo:: |
| 386 | isReallyTriviallyReMaterializableGeneric(const MachineInstr *MI, |
| 387 | AliasAnalysis *AA) const { |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 388 | const MachineFunction &MF = *MI->getParent()->getParent(); |
| 389 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 390 | const TargetMachine &TM = MF.getTarget(); |
| 391 | const TargetInstrInfo &TII = *TM.getInstrInfo(); |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 392 | |
Jakob Stoklund Olesen | 4a0a18a | 2011-09-01 18:27:51 +0000 | [diff] [blame] | 393 | // Remat clients assume operand 0 is the defined register. |
| 394 | if (!MI->getNumOperands() || !MI->getOperand(0).isReg()) |
| 395 | return false; |
| 396 | unsigned DefReg = MI->getOperand(0).getReg(); |
| 397 | |
Jakob Stoklund Olesen | 9d548d0 | 2011-09-01 17:18:50 +0000 | [diff] [blame] | 398 | // A sub-register definition can only be rematerialized if the instruction |
| 399 | // doesn't read the other parts of the register. Otherwise it is really a |
| 400 | // read-modify-write operation on the full virtual register which cannot be |
| 401 | // moved safely. |
Jakob Stoklund Olesen | 4a0a18a | 2011-09-01 18:27:51 +0000 | [diff] [blame] | 402 | if (TargetRegisterInfo::isVirtualRegister(DefReg) && |
| 403 | MI->getOperand(0).getSubReg() && MI->readsVirtualRegister(DefReg)) |
Jakob Stoklund Olesen | 9d548d0 | 2011-09-01 17:18:50 +0000 | [diff] [blame] | 404 | return false; |
| 405 | |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 406 | // A load from a fixed stack slot can be rematerialized. This may be |
| 407 | // redundant with subsequent checks, but it's target-independent, |
| 408 | // simple, and a common case. |
| 409 | int FrameIdx = 0; |
| 410 | if (TII.isLoadFromStackSlot(MI, FrameIdx) && |
| 411 | MF.getFrameInfo()->isImmutableObjectIndex(FrameIdx)) |
| 412 | return true; |
| 413 | |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 414 | // Avoid instructions obviously unsafe for remat. |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 415 | if (MI->isNotDuplicable() || MI->mayStore() || |
Evan Cheng | c36b706 | 2011-01-07 23:50:32 +0000 | [diff] [blame] | 416 | MI->hasUnmodeledSideEffects()) |
| 417 | return false; |
| 418 | |
| 419 | // Don't remat inline asm. We have no idea how expensive it is |
| 420 | // even if it's side effect free. |
| 421 | if (MI->isInlineAsm()) |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 422 | return false; |
| 423 | |
| 424 | // Avoid instructions which load from potentially varying memory. |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 425 | if (MI->mayLoad() && !MI->isInvariantLoad(AA)) |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 426 | return false; |
| 427 | |
| 428 | // If any of the registers accessed are non-constant, conservatively assume |
| 429 | // the instruction is not rematerializable. |
| 430 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 431 | const MachineOperand &MO = MI->getOperand(i); |
| 432 | if (!MO.isReg()) continue; |
| 433 | unsigned Reg = MO.getReg(); |
| 434 | if (Reg == 0) |
| 435 | continue; |
| 436 | |
| 437 | // Check for a well-behaved physical register. |
| 438 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
| 439 | if (MO.isUse()) { |
| 440 | // If the physreg has no defs anywhere, it's just an ambient register |
| 441 | // and we can freely move its uses. Alternatively, if it's allocatable, |
| 442 | // it could get allocated to something with a def during allocation. |
Jakob Stoklund Olesen | c035c94 | 2012-01-16 22:34:08 +0000 | [diff] [blame] | 443 | if (!MRI.isConstantPhysReg(Reg, MF)) |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 444 | return false; |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 445 | } else { |
| 446 | // A physreg def. We can't remat it. |
| 447 | return false; |
| 448 | } |
| 449 | continue; |
| 450 | } |
| 451 | |
Jakob Stoklund Olesen | 4a0a18a | 2011-09-01 18:27:51 +0000 | [diff] [blame] | 452 | // Only allow one virtual-register def. There may be multiple defs of the |
| 453 | // same virtual register, though. |
| 454 | if (MO.isDef() && Reg != DefReg) |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 455 | return false; |
| 456 | |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 457 | // Don't allow any virtual-register uses. Rematting an instruction with |
| 458 | // virtual register uses would length the live ranges of the uses, which |
| 459 | // is not necessarily a good idea, certainly not "trivial". |
| 460 | if (MO.isUse()) |
| 461 | return false; |
| 462 | } |
| 463 | |
| 464 | // Everything checked out. |
| 465 | return true; |
| 466 | } |
Evan Cheng | 774bc88 | 2010-06-14 21:06:53 +0000 | [diff] [blame] | 467 | |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame] | 468 | /// isSchedulingBoundary - Test if the given instruction should be |
| 469 | /// considered a scheduling boundary. This primarily includes labels |
| 470 | /// and terminators. |
| 471 | bool TargetInstrInfoImpl::isSchedulingBoundary(const MachineInstr *MI, |
| 472 | const MachineBasicBlock *MBB, |
| 473 | const MachineFunction &MF) const{ |
| 474 | // Terminators and labels can't be scheduled around. |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 475 | if (MI->isTerminator() || MI->isLabel()) |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame] | 476 | return true; |
| 477 | |
| 478 | // Don't attempt to schedule around any instruction that defines |
| 479 | // a stack-oriented pointer, as it's unlikely to be profitable. This |
| 480 | // saves compile time, because it doesn't require every single |
| 481 | // stack slot reference to depend on the instruction that does the |
| 482 | // modification. |
| 483 | const TargetLowering &TLI = *MF.getTarget().getTargetLowering(); |
| 484 | if (MI->definesRegister(TLI.getStackPointerRegisterToSaveRestore())) |
| 485 | return true; |
| 486 | |
| 487 | return false; |
| 488 | } |
| 489 | |
Andrew Trick | c8bfd1d | 2011-01-21 05:51:33 +0000 | [diff] [blame] | 490 | // Provide a global flag for disabling the PreRA hazard recognizer that targets |
| 491 | // may choose to honor. |
| 492 | bool TargetInstrInfoImpl::usePreRAHazardRecognizer() const { |
| 493 | return !DisableHazardRecognizer; |
| 494 | } |
| 495 | |
| 496 | // Default implementation of CreateTargetRAHazardRecognizer. |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 497 | ScheduleHazardRecognizer *TargetInstrInfoImpl:: |
| 498 | CreateTargetHazardRecognizer(const TargetMachine *TM, |
| 499 | const ScheduleDAG *DAG) const { |
| 500 | // Dummy hazard recognizer allows all instructions to issue. |
| 501 | return new ScheduleHazardRecognizer(); |
| 502 | } |
| 503 | |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 504 | // Default implementation of CreateTargetMIHazardRecognizer. |
| 505 | ScheduleHazardRecognizer *TargetInstrInfoImpl:: |
| 506 | CreateTargetMIHazardRecognizer(const InstrItineraryData *II, |
| 507 | const ScheduleDAG *DAG) const { |
| 508 | return (ScheduleHazardRecognizer *) |
| 509 | new ScoreboardHazardRecognizer(II, DAG, "misched"); |
| 510 | } |
| 511 | |
Evan Cheng | 774bc88 | 2010-06-14 21:06:53 +0000 | [diff] [blame] | 512 | // Default implementation of CreateTargetPostRAHazardRecognizer. |
| 513 | ScheduleHazardRecognizer *TargetInstrInfoImpl:: |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 514 | CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II, |
| 515 | const ScheduleDAG *DAG) const { |
| 516 | return (ScheduleHazardRecognizer *) |
| 517 | new ScoreboardHazardRecognizer(II, DAG, "post-RA-sched"); |
Evan Cheng | 774bc88 | 2010-06-14 21:06:53 +0000 | [diff] [blame] | 518 | } |
Nick Lewycky | 028700f | 2011-12-15 22:58:58 +0000 | [diff] [blame] | 519 | |
Andrew Trick | c36d033 | 2012-06-08 17:23:27 +0000 | [diff] [blame] | 520 | //===----------------------------------------------------------------------===// |
| 521 | // SelectionDAG latency interface. |
| 522 | //===----------------------------------------------------------------------===// |
| 523 | |
Nick Lewycky | 028700f | 2011-12-15 22:58:58 +0000 | [diff] [blame] | 524 | int |
Eli Friedman | 1e2ec6a | 2011-12-19 20:06:03 +0000 | [diff] [blame] | 525 | TargetInstrInfoImpl::getOperandLatency(const InstrItineraryData *ItinData, |
| 526 | SDNode *DefNode, unsigned DefIdx, |
| 527 | SDNode *UseNode, unsigned UseIdx) const { |
Nick Lewycky | 028700f | 2011-12-15 22:58:58 +0000 | [diff] [blame] | 528 | if (!ItinData || ItinData->isEmpty()) |
| 529 | return -1; |
| 530 | |
| 531 | if (!DefNode->isMachineOpcode()) |
| 532 | return -1; |
| 533 | |
| 534 | unsigned DefClass = get(DefNode->getMachineOpcode()).getSchedClass(); |
| 535 | if (!UseNode->isMachineOpcode()) |
| 536 | return ItinData->getOperandCycle(DefClass, DefIdx); |
| 537 | unsigned UseClass = get(UseNode->getMachineOpcode()).getSchedClass(); |
| 538 | return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx); |
| 539 | } |
| 540 | |
Eli Friedman | 1e2ec6a | 2011-12-19 20:06:03 +0000 | [diff] [blame] | 541 | int TargetInstrInfoImpl::getInstrLatency(const InstrItineraryData *ItinData, |
| 542 | SDNode *N) const { |
Nick Lewycky | 028700f | 2011-12-15 22:58:58 +0000 | [diff] [blame] | 543 | if (!ItinData || ItinData->isEmpty()) |
| 544 | return 1; |
| 545 | |
| 546 | if (!N->isMachineOpcode()) |
| 547 | return 1; |
| 548 | |
| 549 | return ItinData->getStageLatency(get(N->getMachineOpcode()).getSchedClass()); |
| 550 | } |
| 551 | |
Andrew Trick | c36d033 | 2012-06-08 17:23:27 +0000 | [diff] [blame] | 552 | //===----------------------------------------------------------------------===// |
| 553 | // MachineInstr latency interface. |
| 554 | //===----------------------------------------------------------------------===// |
| 555 | |
| 556 | unsigned |
Andrew Trick | eb81df7 | 2012-06-08 21:52:38 +0000 | [diff] [blame^] | 557 | TargetInstrInfoImpl::getNumMicroOps(const InstrItineraryData *ItinData, |
| 558 | const MachineInstr *MI) const { |
Andrew Trick | c36d033 | 2012-06-08 17:23:27 +0000 | [diff] [blame] | 559 | if (!ItinData || ItinData->isEmpty()) |
| 560 | return 1; |
| 561 | |
| 562 | unsigned Class = MI->getDesc().getSchedClass(); |
| 563 | unsigned UOps = ItinData->Itineraries[Class].NumMicroOps; |
| 564 | if (UOps) |
| 565 | return UOps; |
| 566 | |
| 567 | // The # of u-ops is dynamically determined. The specific target should |
| 568 | // override this function to return the right number. |
| 569 | return 1; |
| 570 | } |
| 571 | |
| 572 | /// Return the default expected latency for a def based on it's opcode. |
| 573 | unsigned TargetInstrInfo::defaultDefLatency(const InstrItineraryData *ItinData, |
| 574 | const MachineInstr *DefMI) const { |
| 575 | if (DefMI->mayLoad()) |
| 576 | return ItinData->Props.LoadLatency; |
| 577 | if (isHighLatencyDef(DefMI->getOpcode())) |
| 578 | return ItinData->Props.HighLatency; |
| 579 | return 1; |
| 580 | } |
| 581 | |
Andrew Trick | eb81df7 | 2012-06-08 21:52:38 +0000 | [diff] [blame^] | 582 | unsigned TargetInstrInfoImpl:: |
| 583 | getInstrLatency(const InstrItineraryData *ItinData, |
| 584 | const MachineInstr *MI, |
| 585 | unsigned *PredCost) const { |
Andrew Trick | c36d033 | 2012-06-08 17:23:27 +0000 | [diff] [blame] | 586 | // Default to one cycle for no itinerary. However, an "empty" itinerary may |
| 587 | // still have a MinLatency property, which getStageLatency checks. |
| 588 | if (!ItinData) |
| 589 | return MI->mayLoad() ? 2 : 1; |
| 590 | |
| 591 | return ItinData->getStageLatency(MI->getDesc().getSchedClass()); |
| 592 | } |
| 593 | |
Andrew Trick | eb81df7 | 2012-06-08 21:52:38 +0000 | [diff] [blame^] | 594 | bool TargetInstrInfoImpl::hasLowDefLatency(const InstrItineraryData *ItinData, |
| 595 | const MachineInstr *DefMI, |
| 596 | unsigned DefIdx) const { |
Andrew Trick | c36d033 | 2012-06-08 17:23:27 +0000 | [diff] [blame] | 597 | if (!ItinData || ItinData->isEmpty()) |
| 598 | return false; |
| 599 | |
| 600 | unsigned DefClass = DefMI->getDesc().getSchedClass(); |
| 601 | int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx); |
| 602 | return (DefCycle != -1 && DefCycle <= 1); |
| 603 | } |
| 604 | |
| 605 | /// Both DefMI and UseMI must be valid. By default, call directly to the |
| 606 | /// itinerary. This may be overriden by the target. |
Andrew Trick | eb81df7 | 2012-06-08 21:52:38 +0000 | [diff] [blame^] | 607 | int TargetInstrInfoImpl:: |
| 608 | getOperandLatency(const InstrItineraryData *ItinData, |
| 609 | const MachineInstr *DefMI, unsigned DefIdx, |
| 610 | const MachineInstr *UseMI, unsigned UseIdx) const { |
Andrew Trick | c36d033 | 2012-06-08 17:23:27 +0000 | [diff] [blame] | 611 | unsigned DefClass = DefMI->getDesc().getSchedClass(); |
| 612 | unsigned UseClass = UseMI->getDesc().getSchedClass(); |
| 613 | return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx); |
| 614 | } |
| 615 | |
| 616 | /// If we can determine the operand latency from the def only, without itinerary |
| 617 | /// lookup, do so. Otherwise return -1. |
| 618 | static int computeDefOperandLatency( |
| 619 | const TargetInstrInfo *TII, const InstrItineraryData *ItinData, |
| 620 | const MachineInstr *DefMI, bool FindMin) { |
| 621 | |
| 622 | // Let the target hook getInstrLatency handle missing itineraries. |
| 623 | if (!ItinData) |
| 624 | return TII->getInstrLatency(ItinData, DefMI); |
| 625 | |
| 626 | // Return a latency based on the itinerary properties and defining instruction |
| 627 | // if possible. Some common subtargets don't require per-operand latency, |
| 628 | // especially for minimum latencies. |
| 629 | if (FindMin) { |
| 630 | // If MinLatency is valid, call getInstrLatency. This uses Stage latency if |
| 631 | // it exists before defaulting to MinLatency. |
| 632 | if (ItinData->Props.MinLatency >= 0) |
| 633 | return TII->getInstrLatency(ItinData, DefMI); |
| 634 | |
| 635 | // If MinLatency is invalid, OperandLatency is interpreted as MinLatency. |
| 636 | // For empty itineraries, short-cirtuit the check and default to one cycle. |
| 637 | if (ItinData->isEmpty()) |
| 638 | return 1; |
| 639 | } |
| 640 | else if(ItinData->isEmpty()) |
| 641 | return TII->defaultDefLatency(ItinData, DefMI); |
| 642 | |
| 643 | // ...operand lookup required |
| 644 | return -1; |
| 645 | } |
| 646 | |
| 647 | /// computeOperandLatency - Compute and return the latency of the given data |
| 648 | /// dependent def and use when the operand indices are already known. |
| 649 | /// |
| 650 | /// FindMin may be set to get the minimum vs. expected latency. |
| 651 | unsigned TargetInstrInfo:: |
| 652 | computeOperandLatency(const InstrItineraryData *ItinData, |
| 653 | const MachineInstr *DefMI, unsigned DefIdx, |
| 654 | const MachineInstr *UseMI, unsigned UseIdx, |
| 655 | bool FindMin) const { |
| 656 | |
| 657 | int DefLatency = computeDefOperandLatency(this, ItinData, DefMI, FindMin); |
| 658 | if (DefLatency >= 0) |
| 659 | return DefLatency; |
| 660 | |
| 661 | assert(ItinData && !ItinData->isEmpty() && "computeDefOperandLatency fail"); |
| 662 | |
| 663 | int OperLatency = getOperandLatency(ItinData, DefMI, DefIdx, UseMI, UseIdx); |
| 664 | if (OperLatency >= 0) |
| 665 | return OperLatency; |
| 666 | |
| 667 | // No operand latency was found. |
| 668 | unsigned InstrLatency = getInstrLatency(ItinData, DefMI); |
| 669 | |
| 670 | // Expected latency is the max of the stage latency and itinerary props. |
| 671 | if (!FindMin) |
| 672 | InstrLatency = std::max(InstrLatency, defaultDefLatency(ItinData, DefMI)); |
| 673 | return InstrLatency; |
| 674 | } |
| 675 | |
| 676 | /// computeOperandLatency - Compute and return the latency of the given data |
| 677 | /// dependent def and use. DefMI must be a valid def. UseMI may be NULL for an |
| 678 | /// unknown use. Depending on the subtarget's itinerary properties, this may or |
| 679 | /// may not need to call getOperandLatency(). |
| 680 | /// |
| 681 | /// FindMin may be set to get the minimum vs. expected latency. Minimum |
| 682 | /// latency is used for scheduling groups, while expected latency is for |
| 683 | /// instruction cost and critical path. |
| 684 | /// |
| 685 | /// For most subtargets, we don't need DefIdx or UseIdx to compute min latency. |
| 686 | /// DefMI must be a valid definition, but UseMI may be NULL for an unknown use. |
| 687 | unsigned TargetInstrInfo:: |
| 688 | computeOperandLatency(const InstrItineraryData *ItinData, |
| 689 | const TargetRegisterInfo *TRI, |
| 690 | const MachineInstr *DefMI, const MachineInstr *UseMI, |
| 691 | unsigned Reg, bool FindMin) const { |
| 692 | |
| 693 | int DefLatency = computeDefOperandLatency(this, ItinData, DefMI, FindMin); |
| 694 | if (DefLatency >= 0) |
| 695 | return DefLatency; |
| 696 | |
| 697 | assert(ItinData && !ItinData->isEmpty() && "computeDefOperandLatency fail"); |
| 698 | |
| 699 | // Find the definition of the register in the defining instruction. |
| 700 | int DefIdx = DefMI->findRegisterDefOperandIdx(Reg); |
| 701 | if (DefIdx != -1) { |
| 702 | const MachineOperand &MO = DefMI->getOperand(DefIdx); |
| 703 | if (MO.isReg() && MO.isImplicit() && |
| 704 | DefIdx >= (int)DefMI->getDesc().getNumOperands()) { |
| 705 | // This is an implicit def, getOperandLatency() won't return the correct |
| 706 | // latency. e.g. |
| 707 | // %D6<def>, %D7<def> = VLD1q16 %R2<kill>, 0, ..., %Q3<imp-def> |
| 708 | // %Q1<def> = VMULv8i16 %Q1<kill>, %Q3<kill>, ... |
| 709 | // What we want is to compute latency between def of %D6/%D7 and use of |
| 710 | // %Q3 instead. |
| 711 | unsigned Op2 = DefMI->findRegisterDefOperandIdx(Reg, false, true, TRI); |
| 712 | if (DefMI->getOperand(Op2).isReg()) |
| 713 | DefIdx = Op2; |
| 714 | } |
| 715 | // For all uses of the register, calculate the maxmimum latency |
| 716 | int OperLatency = -1; |
| 717 | |
| 718 | // UseMI is null, then it must be a scheduling barrier. |
| 719 | if (!UseMI) { |
| 720 | unsigned DefClass = DefMI->getDesc().getSchedClass(); |
| 721 | OperLatency = ItinData->getOperandCycle(DefClass, DefIdx); |
| 722 | } |
| 723 | else { |
| 724 | for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) { |
| 725 | const MachineOperand &MO = UseMI->getOperand(i); |
| 726 | if (!MO.isReg() || !MO.isUse()) |
| 727 | continue; |
| 728 | unsigned MOReg = MO.getReg(); |
| 729 | if (MOReg != Reg) |
| 730 | continue; |
| 731 | |
| 732 | int UseCycle = getOperandLatency(ItinData, DefMI, DefIdx, UseMI, i); |
| 733 | OperLatency = std::max(OperLatency, UseCycle); |
| 734 | } |
| 735 | } |
| 736 | // If we found an operand latency, we're done. |
| 737 | if (OperLatency >= 0) |
| 738 | return OperLatency; |
| 739 | } |
| 740 | // No operand latency was found. |
| 741 | unsigned InstrLatency = getInstrLatency(ItinData, DefMI); |
| 742 | |
| 743 | // Expected latency is the max of the stage latency and itinerary props. |
| 744 | if (!FindMin) |
| 745 | InstrLatency = std::max(InstrLatency, defaultDefLatency(ItinData, DefMI)); |
| 746 | return InstrLatency; |
| 747 | } |