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