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(); |
| 81 | bool Reg1IsKill = MI->getOperand(Idx1).isKill(); |
| 82 | bool Reg2IsKill = MI->getOperand(Idx2).isKill(); |
Evan Cheng | cb08f18 | 2011-08-22 23:04:56 +0000 | [diff] [blame] | 83 | // If destination is tied to either of the commuted source register, then |
| 84 | // it must be updated. |
| 85 | if (HasDef && Reg0 == Reg1 && |
| 86 | MI->getDesc().getOperandConstraint(Idx1, MCOI::TIED_TO) == 0) { |
Evan Cheng | a4d16a1 | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 87 | Reg2IsKill = false; |
Evan Cheng | cb08f18 | 2011-08-22 23:04:56 +0000 | [diff] [blame] | 88 | Reg0 = Reg2; |
| 89 | } else if (HasDef && Reg0 == Reg2 && |
| 90 | MI->getDesc().getOperandConstraint(Idx2, MCOI::TIED_TO) == 0) { |
| 91 | Reg1IsKill = false; |
| 92 | Reg0 = Reg1; |
Evan Cheng | a4d16a1 | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 93 | } |
Evan Cheng | 58dcb0e | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 94 | |
| 95 | if (NewMI) { |
| 96 | // Create a new instruction. |
Evan Cheng | 498c290 | 2009-07-01 08:29:08 +0000 | [diff] [blame] | 97 | bool Reg0IsDead = HasDef ? MI->getOperand(0).isDead() : false; |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 98 | MachineFunction &MF = *MI->getParent()->getParent(); |
Evan Cheng | 498c290 | 2009-07-01 08:29:08 +0000 | [diff] [blame] | 99 | if (HasDef) |
| 100 | return BuildMI(MF, MI->getDebugLoc(), MI->getDesc()) |
| 101 | .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead)) |
| 102 | .addReg(Reg2, getKillRegState(Reg2IsKill)) |
| 103 | .addReg(Reg1, getKillRegState(Reg2IsKill)); |
| 104 | else |
| 105 | return BuildMI(MF, MI->getDebugLoc(), MI->getDesc()) |
| 106 | .addReg(Reg2, getKillRegState(Reg2IsKill)) |
| 107 | .addReg(Reg1, getKillRegState(Reg2IsKill)); |
Evan Cheng | 58dcb0e | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Evan Cheng | cb08f18 | 2011-08-22 23:04:56 +0000 | [diff] [blame] | 110 | if (HasDef) |
| 111 | MI->getOperand(0).setReg(Reg0); |
Evan Cheng | 498c290 | 2009-07-01 08:29:08 +0000 | [diff] [blame] | 112 | MI->getOperand(Idx2).setReg(Reg1); |
| 113 | MI->getOperand(Idx1).setReg(Reg2); |
| 114 | MI->getOperand(Idx2).setIsKill(Reg1IsKill); |
| 115 | MI->getOperand(Idx1).setIsKill(Reg2IsKill); |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 116 | return MI; |
| 117 | } |
| 118 | |
Evan Cheng | 261ce1d | 2009-07-10 19:15:51 +0000 | [diff] [blame] | 119 | /// findCommutedOpIndices - If specified MI is commutable, return the two |
| 120 | /// operand indices that would swap value. Return true if the instruction |
| 121 | /// is not in a form which this routine understands. |
| 122 | bool TargetInstrInfoImpl::findCommutedOpIndices(MachineInstr *MI, |
| 123 | unsigned &SrcOpIdx1, |
| 124 | unsigned &SrcOpIdx2) const { |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 125 | assert(!MI->isBundle() && |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 126 | "TargetInstrInfoImpl::findCommutedOpIndices() can't handle bundles"); |
| 127 | |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 128 | const MCInstrDesc &MCID = MI->getDesc(); |
| 129 | if (!MCID.isCommutable()) |
Evan Cheng | 498c290 | 2009-07-01 08:29:08 +0000 | [diff] [blame] | 130 | return false; |
Evan Cheng | 261ce1d | 2009-07-10 19:15:51 +0000 | [diff] [blame] | 131 | // This assumes v0 = op v1, v2 and commuting would swap v1 and v2. If this |
| 132 | // is not true, then the target must implement this. |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 133 | SrcOpIdx1 = MCID.getNumDefs(); |
Evan Cheng | 261ce1d | 2009-07-10 19:15:51 +0000 | [diff] [blame] | 134 | SrcOpIdx2 = SrcOpIdx1 + 1; |
| 135 | if (!MI->getOperand(SrcOpIdx1).isReg() || |
| 136 | !MI->getOperand(SrcOpIdx2).isReg()) |
| 137 | // No idea. |
| 138 | return false; |
| 139 | return true; |
Evan Cheng | f20db15 | 2008-02-15 18:21:33 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | |
Evan Cheng | 32f9763 | 2011-12-09 06:41:08 +0000 | [diff] [blame] | 143 | bool |
| 144 | TargetInstrInfoImpl::isUnpredicatedTerminator(const MachineInstr *MI) const { |
| 145 | if (!MI->isTerminator()) return false; |
| 146 | |
| 147 | // Conditional branch is a special case. |
| 148 | if (MI->isBranch() && !MI->isBarrier()) |
| 149 | return true; |
| 150 | if (!MI->isPredicable()) |
| 151 | return true; |
| 152 | return !isPredicated(MI); |
| 153 | } |
| 154 | |
| 155 | |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 156 | bool TargetInstrInfoImpl::PredicateInstruction(MachineInstr *MI, |
Owen Anderson | 44eb65c | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 157 | const SmallVectorImpl<MachineOperand> &Pred) const { |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 158 | bool MadeChange = false; |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 159 | |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 160 | assert(!MI->isBundle() && |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 161 | "TargetInstrInfoImpl::PredicateInstruction() can't handle bundles"); |
| 162 | |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 163 | const MCInstrDesc &MCID = MI->getDesc(); |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 164 | if (!MI->isPredicable()) |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 165 | return false; |
Andrew Trick | 6b12072 | 2010-12-08 20:04:29 +0000 | [diff] [blame] | 166 | |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 167 | for (unsigned j = 0, i = 0, e = MI->getNumOperands(); i != e; ++i) { |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 168 | if (MCID.OpInfo[i].isPredicate()) { |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 169 | MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 170 | if (MO.isReg()) { |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 171 | MO.setReg(Pred[j].getReg()); |
| 172 | MadeChange = true; |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 173 | } else if (MO.isImm()) { |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 174 | MO.setImm(Pred[j].getImm()); |
| 175 | MadeChange = true; |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 176 | } else if (MO.isMBB()) { |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 177 | MO.setMBB(Pred[j].getMBB()); |
| 178 | MadeChange = true; |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 179 | } |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 180 | ++j; |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | return MadeChange; |
| 184 | } |
Evan Cheng | ca1267c | 2008-03-31 20:40:39 +0000 | [diff] [blame] | 185 | |
Jakob Stoklund Olesen | 2df3f58 | 2011-08-08 20:53:24 +0000 | [diff] [blame] | 186 | bool TargetInstrInfoImpl::hasLoadFromStackSlot(const MachineInstr *MI, |
| 187 | const MachineMemOperand *&MMO, |
| 188 | int &FrameIndex) const { |
| 189 | for (MachineInstr::mmo_iterator o = MI->memoperands_begin(), |
| 190 | oe = MI->memoperands_end(); |
| 191 | o != oe; |
| 192 | ++o) { |
| 193 | if ((*o)->isLoad() && (*o)->getValue()) |
| 194 | if (const FixedStackPseudoSourceValue *Value = |
| 195 | dyn_cast<const FixedStackPseudoSourceValue>((*o)->getValue())) { |
| 196 | FrameIndex = Value->getFrameIndex(); |
| 197 | MMO = *o; |
| 198 | return true; |
| 199 | } |
| 200 | } |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | bool TargetInstrInfoImpl::hasStoreToStackSlot(const MachineInstr *MI, |
| 205 | const MachineMemOperand *&MMO, |
| 206 | int &FrameIndex) const { |
| 207 | for (MachineInstr::mmo_iterator o = MI->memoperands_begin(), |
| 208 | oe = MI->memoperands_end(); |
| 209 | o != oe; |
| 210 | ++o) { |
| 211 | if ((*o)->isStore() && (*o)->getValue()) |
| 212 | if (const FixedStackPseudoSourceValue *Value = |
| 213 | dyn_cast<const FixedStackPseudoSourceValue>((*o)->getValue())) { |
| 214 | FrameIndex = Value->getFrameIndex(); |
| 215 | MMO = *o; |
| 216 | return true; |
| 217 | } |
| 218 | } |
| 219 | return false; |
| 220 | } |
| 221 | |
Evan Cheng | ca1267c | 2008-03-31 20:40:39 +0000 | [diff] [blame] | 222 | void TargetInstrInfoImpl::reMaterialize(MachineBasicBlock &MBB, |
| 223 | MachineBasicBlock::iterator I, |
| 224 | unsigned DestReg, |
Evan Cheng | 3784453 | 2009-07-16 09:20:10 +0000 | [diff] [blame] | 225 | unsigned SubIdx, |
Evan Cheng | d57cdd5 | 2009-11-14 02:55:43 +0000 | [diff] [blame] | 226 | const MachineInstr *Orig, |
Jakob Stoklund Olesen | 9edf7de | 2010-06-02 22:47:25 +0000 | [diff] [blame] | 227 | const TargetRegisterInfo &TRI) const { |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 228 | MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig); |
Jakob Stoklund Olesen | 9edf7de | 2010-06-02 22:47:25 +0000 | [diff] [blame] | 229 | MI->substituteRegister(MI->getOperand(0).getReg(), DestReg, SubIdx, TRI); |
Evan Cheng | ca1267c | 2008-03-31 20:40:39 +0000 | [diff] [blame] | 230 | MBB.insert(I, MI); |
| 231 | } |
| 232 | |
Evan Cheng | 9fe2009 | 2011-01-20 08:34:58 +0000 | [diff] [blame] | 233 | bool |
| 234 | TargetInstrInfoImpl::produceSameValue(const MachineInstr *MI0, |
| 235 | const MachineInstr *MI1, |
| 236 | const MachineRegisterInfo *MRI) const { |
Evan Cheng | 506049f | 2010-03-03 01:44:33 +0000 | [diff] [blame] | 237 | return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs); |
| 238 | } |
| 239 | |
Jakob Stoklund Olesen | 30ac046 | 2010-01-06 23:47:07 +0000 | [diff] [blame] | 240 | MachineInstr *TargetInstrInfoImpl::duplicate(MachineInstr *Orig, |
| 241 | MachineFunction &MF) const { |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 242 | assert(!Orig->isNotDuplicable() && |
Jakob Stoklund Olesen | 30ac046 | 2010-01-06 23:47:07 +0000 | [diff] [blame] | 243 | "Instruction cannot be duplicated"); |
| 244 | return MF.CloneMachineInstr(Orig); |
| 245 | } |
| 246 | |
Jakob Stoklund Olesen | 1f32340 | 2010-07-09 20:43:13 +0000 | [diff] [blame] | 247 | // If the COPY instruction in MI can be folded to a stack operation, return |
| 248 | // the register class to use. |
| 249 | static const TargetRegisterClass *canFoldCopy(const MachineInstr *MI, |
| 250 | unsigned FoldIdx) { |
| 251 | assert(MI->isCopy() && "MI must be a COPY instruction"); |
| 252 | if (MI->getNumOperands() != 2) |
| 253 | return 0; |
| 254 | assert(FoldIdx<2 && "FoldIdx refers no nonexistent operand"); |
| 255 | |
| 256 | const MachineOperand &FoldOp = MI->getOperand(FoldIdx); |
| 257 | const MachineOperand &LiveOp = MI->getOperand(1-FoldIdx); |
| 258 | |
| 259 | if (FoldOp.getSubReg() || LiveOp.getSubReg()) |
| 260 | return 0; |
| 261 | |
| 262 | unsigned FoldReg = FoldOp.getReg(); |
| 263 | unsigned LiveReg = LiveOp.getReg(); |
| 264 | |
| 265 | assert(TargetRegisterInfo::isVirtualRegister(FoldReg) && |
| 266 | "Cannot fold physregs"); |
| 267 | |
| 268 | const MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo(); |
| 269 | const TargetRegisterClass *RC = MRI.getRegClass(FoldReg); |
| 270 | |
| 271 | if (TargetRegisterInfo::isPhysicalRegister(LiveOp.getReg())) |
| 272 | return RC->contains(LiveOp.getReg()) ? RC : 0; |
| 273 | |
Jakob Stoklund Olesen | fa226bc | 2011-06-02 05:43:46 +0000 | [diff] [blame] | 274 | if (RC->hasSubClassEq(MRI.getRegClass(LiveReg))) |
Jakob Stoklund Olesen | 1f32340 | 2010-07-09 20:43:13 +0000 | [diff] [blame] | 275 | return RC; |
| 276 | |
| 277 | // FIXME: Allow folding when register classes are memory compatible. |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | bool TargetInstrInfoImpl:: |
| 282 | canFoldMemoryOperand(const MachineInstr *MI, |
| 283 | const SmallVectorImpl<unsigned> &Ops) const { |
| 284 | return MI->isCopy() && Ops.size() == 1 && canFoldCopy(MI, Ops[0]); |
| 285 | } |
| 286 | |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 287 | /// foldMemoryOperand - Attempt to fold a load or store of the specified stack |
| 288 | /// slot into the specified machine instruction for the specified operand(s). |
| 289 | /// If this is possible, a new instruction is returned with the specified |
| 290 | /// operand folded, otherwise NULL is returned. The client is responsible for |
| 291 | /// removing the old instruction and adding the new one in the instruction |
| 292 | /// stream. |
| 293 | MachineInstr* |
Jakob Stoklund Olesen | e05442d | 2010-07-09 17:29:08 +0000 | [diff] [blame] | 294 | TargetInstrInfo::foldMemoryOperand(MachineBasicBlock::iterator MI, |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 295 | const SmallVectorImpl<unsigned> &Ops, |
Jakob Stoklund Olesen | 1f32340 | 2010-07-09 20:43:13 +0000 | [diff] [blame] | 296 | int FI) const { |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 297 | unsigned Flags = 0; |
| 298 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 299 | if (MI->getOperand(Ops[i]).isDef()) |
| 300 | Flags |= MachineMemOperand::MOStore; |
| 301 | else |
| 302 | Flags |= MachineMemOperand::MOLoad; |
| 303 | |
Jakob Stoklund Olesen | 1f32340 | 2010-07-09 20:43:13 +0000 | [diff] [blame] | 304 | MachineBasicBlock *MBB = MI->getParent(); |
| 305 | assert(MBB && "foldMemoryOperand needs an inserted instruction"); |
| 306 | MachineFunction &MF = *MBB->getParent(); |
Jakob Stoklund Olesen | e05442d | 2010-07-09 17:29:08 +0000 | [diff] [blame] | 307 | |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 308 | // Ask the target to do the actual folding. |
Jakob Stoklund Olesen | 9fac415 | 2010-07-13 00:23:30 +0000 | [diff] [blame] | 309 | if (MachineInstr *NewMI = foldMemoryOperandImpl(MF, MI, Ops, FI)) { |
| 310 | // Add a memory operand, foldMemoryOperandImpl doesn't do that. |
| 311 | assert((!(Flags & MachineMemOperand::MOStore) || |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 312 | NewMI->mayStore()) && |
Jakob Stoklund Olesen | 9fac415 | 2010-07-13 00:23:30 +0000 | [diff] [blame] | 313 | "Folded a def to a non-store!"); |
| 314 | assert((!(Flags & MachineMemOperand::MOLoad) || |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 315 | NewMI->mayLoad()) && |
Jakob Stoklund Olesen | 9fac415 | 2010-07-13 00:23:30 +0000 | [diff] [blame] | 316 | "Folded a use to a non-load!"); |
| 317 | const MachineFrameInfo &MFI = *MF.getFrameInfo(); |
| 318 | assert(MFI.getObjectOffset(FI) != -1); |
| 319 | MachineMemOperand *MMO = |
Jay Foad | f4a5084 | 2011-11-15 07:51:13 +0000 | [diff] [blame] | 320 | MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI), |
Chris Lattner | 93a95ae | 2010-09-21 04:46:39 +0000 | [diff] [blame] | 321 | Flags, MFI.getObjectSize(FI), |
Jakob Stoklund Olesen | 9fac415 | 2010-07-13 00:23:30 +0000 | [diff] [blame] | 322 | MFI.getObjectAlignment(FI)); |
| 323 | NewMI->addMemOperand(MF, MMO); |
Jakob Stoklund Olesen | 1f32340 | 2010-07-09 20:43:13 +0000 | [diff] [blame] | 324 | |
Jakob Stoklund Olesen | 1f32340 | 2010-07-09 20:43:13 +0000 | [diff] [blame] | 325 | // FIXME: change foldMemoryOperandImpl semantics to also insert NewMI. |
Jakob Stoklund Olesen | 9fac415 | 2010-07-13 00:23:30 +0000 | [diff] [blame] | 326 | return MBB->insert(MI, NewMI); |
Jakob Stoklund Olesen | 1f32340 | 2010-07-09 20:43:13 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Jakob Stoklund Olesen | 9fac415 | 2010-07-13 00:23:30 +0000 | [diff] [blame] | 329 | // Straight COPY may fold as load/store. |
| 330 | if (!MI->isCopy() || Ops.size() != 1) |
| 331 | return 0; |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 332 | |
Jakob Stoklund Olesen | 9fac415 | 2010-07-13 00:23:30 +0000 | [diff] [blame] | 333 | const TargetRegisterClass *RC = canFoldCopy(MI, Ops[0]); |
| 334 | if (!RC) |
| 335 | return 0; |
Jakob Stoklund Olesen | e05442d | 2010-07-09 17:29:08 +0000 | [diff] [blame] | 336 | |
Jakob Stoklund Olesen | 9fac415 | 2010-07-13 00:23:30 +0000 | [diff] [blame] | 337 | const MachineOperand &MO = MI->getOperand(1-Ops[0]); |
| 338 | MachineBasicBlock::iterator Pos = MI; |
| 339 | const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo(); |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 340 | |
Jakob Stoklund Olesen | 9fac415 | 2010-07-13 00:23:30 +0000 | [diff] [blame] | 341 | if (Flags == MachineMemOperand::MOStore) |
| 342 | storeRegToStackSlot(*MBB, Pos, MO.getReg(), MO.isKill(), FI, RC, TRI); |
| 343 | else |
| 344 | loadRegFromStackSlot(*MBB, Pos, MO.getReg(), FI, RC, TRI); |
| 345 | return --Pos; |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /// foldMemoryOperand - Same as the previous version except it allows folding |
| 349 | /// of any load and store from / to any address, not just from a specific |
| 350 | /// stack slot. |
| 351 | MachineInstr* |
Jakob Stoklund Olesen | e05442d | 2010-07-09 17:29:08 +0000 | [diff] [blame] | 352 | TargetInstrInfo::foldMemoryOperand(MachineBasicBlock::iterator MI, |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 353 | const SmallVectorImpl<unsigned> &Ops, |
| 354 | MachineInstr* LoadMI) const { |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 355 | assert(LoadMI->canFoldAsLoad() && "LoadMI isn't foldable!"); |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 356 | #ifndef NDEBUG |
| 357 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 358 | assert(MI->getOperand(Ops[i]).isUse() && "Folding load into def!"); |
| 359 | #endif |
Jakob Stoklund Olesen | e05442d | 2010-07-09 17:29:08 +0000 | [diff] [blame] | 360 | MachineBasicBlock &MBB = *MI->getParent(); |
| 361 | MachineFunction &MF = *MBB.getParent(); |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 362 | |
| 363 | // Ask the target to do the actual folding. |
| 364 | MachineInstr *NewMI = foldMemoryOperandImpl(MF, MI, Ops, LoadMI); |
| 365 | if (!NewMI) return 0; |
| 366 | |
Jakob Stoklund Olesen | e05442d | 2010-07-09 17:29:08 +0000 | [diff] [blame] | 367 | NewMI = MBB.insert(MI, NewMI); |
| 368 | |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 369 | // Copy the memoperands from the load to the folded instruction. |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 370 | NewMI->setMemRefs(LoadMI->memoperands_begin(), |
| 371 | LoadMI->memoperands_end()); |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 372 | |
| 373 | return NewMI; |
| 374 | } |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 375 | |
Evan Cheng | 44acc24 | 2010-06-12 00:11:53 +0000 | [diff] [blame] | 376 | bool TargetInstrInfo:: |
| 377 | isReallyTriviallyReMaterializableGeneric(const MachineInstr *MI, |
| 378 | AliasAnalysis *AA) const { |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 379 | const MachineFunction &MF = *MI->getParent()->getParent(); |
| 380 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 381 | const TargetMachine &TM = MF.getTarget(); |
| 382 | const TargetInstrInfo &TII = *TM.getInstrInfo(); |
| 383 | const TargetRegisterInfo &TRI = *TM.getRegisterInfo(); |
| 384 | |
Jakob Stoklund Olesen | 4a0a18a | 2011-09-01 18:27:51 +0000 | [diff] [blame] | 385 | // Remat clients assume operand 0 is the defined register. |
| 386 | if (!MI->getNumOperands() || !MI->getOperand(0).isReg()) |
| 387 | return false; |
| 388 | unsigned DefReg = MI->getOperand(0).getReg(); |
| 389 | |
Jakob Stoklund Olesen | 9d548d0 | 2011-09-01 17:18:50 +0000 | [diff] [blame] | 390 | // A sub-register definition can only be rematerialized if the instruction |
| 391 | // doesn't read the other parts of the register. Otherwise it is really a |
| 392 | // read-modify-write operation on the full virtual register which cannot be |
| 393 | // moved safely. |
Jakob Stoklund Olesen | 4a0a18a | 2011-09-01 18:27:51 +0000 | [diff] [blame] | 394 | if (TargetRegisterInfo::isVirtualRegister(DefReg) && |
| 395 | MI->getOperand(0).getSubReg() && MI->readsVirtualRegister(DefReg)) |
Jakob Stoklund Olesen | 9d548d0 | 2011-09-01 17:18:50 +0000 | [diff] [blame] | 396 | return false; |
| 397 | |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 398 | // A load from a fixed stack slot can be rematerialized. This may be |
| 399 | // redundant with subsequent checks, but it's target-independent, |
| 400 | // simple, and a common case. |
| 401 | int FrameIdx = 0; |
| 402 | if (TII.isLoadFromStackSlot(MI, FrameIdx) && |
| 403 | MF.getFrameInfo()->isImmutableObjectIndex(FrameIdx)) |
| 404 | return true; |
| 405 | |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 406 | // Avoid instructions obviously unsafe for remat. |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 407 | if (MI->isNotDuplicable() || MI->mayStore() || |
Evan Cheng | c36b706 | 2011-01-07 23:50:32 +0000 | [diff] [blame] | 408 | MI->hasUnmodeledSideEffects()) |
| 409 | return false; |
| 410 | |
| 411 | // Don't remat inline asm. We have no idea how expensive it is |
| 412 | // even if it's side effect free. |
| 413 | if (MI->isInlineAsm()) |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 414 | return false; |
| 415 | |
| 416 | // Avoid instructions which load from potentially varying memory. |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 417 | if (MI->mayLoad() && !MI->isInvariantLoad(AA)) |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 418 | return false; |
| 419 | |
| 420 | // If any of the registers accessed are non-constant, conservatively assume |
| 421 | // the instruction is not rematerializable. |
| 422 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 423 | const MachineOperand &MO = MI->getOperand(i); |
| 424 | if (!MO.isReg()) continue; |
| 425 | unsigned Reg = MO.getReg(); |
| 426 | if (Reg == 0) |
| 427 | continue; |
| 428 | |
| 429 | // Check for a well-behaved physical register. |
| 430 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
| 431 | if (MO.isUse()) { |
| 432 | // If the physreg has no defs anywhere, it's just an ambient register |
| 433 | // and we can freely move its uses. Alternatively, if it's allocatable, |
| 434 | // it could get allocated to something with a def during allocation. |
| 435 | if (!MRI.def_empty(Reg)) |
| 436 | return false; |
| 437 | BitVector AllocatableRegs = TRI.getAllocatableSet(MF, 0); |
| 438 | if (AllocatableRegs.test(Reg)) |
| 439 | return false; |
| 440 | // Check for a def among the register's aliases too. |
| 441 | for (const unsigned *Alias = TRI.getAliasSet(Reg); *Alias; ++Alias) { |
| 442 | unsigned AliasReg = *Alias; |
| 443 | if (!MRI.def_empty(AliasReg)) |
| 444 | return false; |
| 445 | if (AllocatableRegs.test(AliasReg)) |
| 446 | return false; |
| 447 | } |
| 448 | } else { |
| 449 | // A physreg def. We can't remat it. |
| 450 | return false; |
| 451 | } |
| 452 | continue; |
| 453 | } |
| 454 | |
Jakob Stoklund Olesen | 4a0a18a | 2011-09-01 18:27:51 +0000 | [diff] [blame] | 455 | // Only allow one virtual-register def. There may be multiple defs of the |
| 456 | // same virtual register, though. |
| 457 | if (MO.isDef() && Reg != DefReg) |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 458 | return false; |
| 459 | |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 460 | // Don't allow any virtual-register uses. Rematting an instruction with |
| 461 | // virtual register uses would length the live ranges of the uses, which |
| 462 | // is not necessarily a good idea, certainly not "trivial". |
| 463 | if (MO.isUse()) |
| 464 | return false; |
| 465 | } |
| 466 | |
| 467 | // Everything checked out. |
| 468 | return true; |
| 469 | } |
Evan Cheng | 774bc88 | 2010-06-14 21:06:53 +0000 | [diff] [blame] | 470 | |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame] | 471 | /// isSchedulingBoundary - Test if the given instruction should be |
| 472 | /// considered a scheduling boundary. This primarily includes labels |
| 473 | /// and terminators. |
| 474 | bool TargetInstrInfoImpl::isSchedulingBoundary(const MachineInstr *MI, |
| 475 | const MachineBasicBlock *MBB, |
| 476 | const MachineFunction &MF) const{ |
| 477 | // Terminators and labels can't be scheduled around. |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 478 | if (MI->isTerminator() || MI->isLabel()) |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame] | 479 | return true; |
| 480 | |
| 481 | // Don't attempt to schedule around any instruction that defines |
| 482 | // a stack-oriented pointer, as it's unlikely to be profitable. This |
| 483 | // saves compile time, because it doesn't require every single |
| 484 | // stack slot reference to depend on the instruction that does the |
| 485 | // modification. |
| 486 | const TargetLowering &TLI = *MF.getTarget().getTargetLowering(); |
| 487 | if (MI->definesRegister(TLI.getStackPointerRegisterToSaveRestore())) |
| 488 | return true; |
| 489 | |
| 490 | return false; |
| 491 | } |
| 492 | |
Andrew Trick | c8bfd1d | 2011-01-21 05:51:33 +0000 | [diff] [blame] | 493 | // Provide a global flag for disabling the PreRA hazard recognizer that targets |
| 494 | // may choose to honor. |
| 495 | bool TargetInstrInfoImpl::usePreRAHazardRecognizer() const { |
| 496 | return !DisableHazardRecognizer; |
| 497 | } |
| 498 | |
| 499 | // Default implementation of CreateTargetRAHazardRecognizer. |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 500 | ScheduleHazardRecognizer *TargetInstrInfoImpl:: |
| 501 | CreateTargetHazardRecognizer(const TargetMachine *TM, |
| 502 | const ScheduleDAG *DAG) const { |
| 503 | // Dummy hazard recognizer allows all instructions to issue. |
| 504 | return new ScheduleHazardRecognizer(); |
| 505 | } |
| 506 | |
Evan Cheng | 774bc88 | 2010-06-14 21:06:53 +0000 | [diff] [blame] | 507 | // Default implementation of CreateTargetPostRAHazardRecognizer. |
| 508 | ScheduleHazardRecognizer *TargetInstrInfoImpl:: |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 509 | CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II, |
| 510 | const ScheduleDAG *DAG) const { |
| 511 | return (ScheduleHazardRecognizer *) |
| 512 | new ScoreboardHazardRecognizer(II, DAG, "post-RA-sched"); |
Evan Cheng | 774bc88 | 2010-06-14 21:06:53 +0000 | [diff] [blame] | 513 | } |
Nick Lewycky | 028700f | 2011-12-15 22:58:58 +0000 | [diff] [blame^] | 514 | |
| 515 | int |
| 516 | TargetInstrInfo::getOperandLatency(const InstrItineraryData *ItinData, |
| 517 | SDNode *DefNode, unsigned DefIdx, |
| 518 | SDNode *UseNode, unsigned UseIdx) const { |
| 519 | if (!ItinData || ItinData->isEmpty()) |
| 520 | return -1; |
| 521 | |
| 522 | if (!DefNode->isMachineOpcode()) |
| 523 | return -1; |
| 524 | |
| 525 | unsigned DefClass = get(DefNode->getMachineOpcode()).getSchedClass(); |
| 526 | if (!UseNode->isMachineOpcode()) |
| 527 | return ItinData->getOperandCycle(DefClass, DefIdx); |
| 528 | unsigned UseClass = get(UseNode->getMachineOpcode()).getSchedClass(); |
| 529 | return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx); |
| 530 | } |
| 531 | |
| 532 | int TargetInstrInfo::getInstrLatency(const InstrItineraryData *ItinData, |
| 533 | SDNode *N) const { |
| 534 | if (!ItinData || ItinData->isEmpty()) |
| 535 | return 1; |
| 536 | |
| 537 | if (!N->isMachineOpcode()) |
| 538 | return 1; |
| 539 | |
| 540 | return ItinData->getStageLatency(get(N->getMachineOpcode()).getSchedClass()); |
| 541 | } |
| 542 | |