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