Dan Gohman | a629b48 | 2008-12-08 17:50:35 +0000 | [diff] [blame] | 1 | //===---- ScheduleDAGInstrs.cpp - MachineInstr Rescheduling ---------------===// |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 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 | // |
Dan Gohman | a629b48 | 2008-12-08 17:50:35 +0000 | [diff] [blame] | 10 | // This implements the ScheduleDAGInstrs class, which implements re-scheduling |
| 11 | // of MachineInstrs. |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "sched-instrs" |
Dan Gohman | 6dc75fe | 2009-02-06 17:12:10 +0000 | [diff] [blame] | 16 | #include "ScheduleDAGInstrs.h" |
Dan Gohman | 8906f95 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 17 | #include "llvm/Operator.h" |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/AliasAnalysis.h" |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineMemOperand.h" |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetMachine.h" |
| 24 | #include "llvm/Target/TargetInstrInfo.h" |
| 25 | #include "llvm/Target/TargetRegisterInfo.h" |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetSubtarget.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" |
| 28 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallSet.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 32 | ScheduleDAGInstrs::ScheduleDAGInstrs(MachineFunction &mf, |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 33 | const MachineLoopInfo &mli, |
| 34 | const MachineDominatorTree &mdt) |
Evan Cheng | 3ef1c87 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 35 | : ScheduleDAG(mf), MLI(mli), MDT(mdt), MFI(mf.getFrameInfo()), |
| 36 | InstrItins(mf.getTarget().getInstrItineraryData()), |
| 37 | Defs(TRI->getNumRegs()), Uses(TRI->getNumRegs()), LoopRegs(MLI, MDT) { |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 38 | DbgValueVec.clear(); |
Evan Cheng | 38bdfc6 | 2009-10-18 19:58:47 +0000 | [diff] [blame] | 39 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 40 | |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 41 | /// Run - perform scheduling. |
| 42 | /// |
| 43 | void ScheduleDAGInstrs::Run(MachineBasicBlock *bb, |
| 44 | MachineBasicBlock::iterator begin, |
| 45 | MachineBasicBlock::iterator end, |
| 46 | unsigned endcount) { |
| 47 | BB = bb; |
| 48 | Begin = begin; |
| 49 | InsertPosIndex = endcount; |
| 50 | |
| 51 | ScheduleDAG::Run(bb, end); |
| 52 | } |
| 53 | |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 54 | /// getUnderlyingObjectFromInt - This is the function that does the work of |
| 55 | /// looking through basic ptrtoint+arithmetic+inttoptr sequences. |
| 56 | static const Value *getUnderlyingObjectFromInt(const Value *V) { |
| 57 | do { |
Dan Gohman | 8906f95 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 58 | if (const Operator *U = dyn_cast<Operator>(V)) { |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 59 | // If we find a ptrtoint, we can transfer control back to the |
| 60 | // regular getUnderlyingObjectFromInt. |
Dan Gohman | 8906f95 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 61 | if (U->getOpcode() == Instruction::PtrToInt) |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 62 | return U->getOperand(0); |
| 63 | // If we find an add of a constant or a multiplied value, it's |
| 64 | // likely that the other operand will lead us to the base |
| 65 | // object. We don't have to worry about the case where the |
Dan Gohman | 748f98f | 2009-08-07 01:26:06 +0000 | [diff] [blame] | 66 | // object address is somehow being computed by the multiply, |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 67 | // because our callers only care when the result is an |
| 68 | // identifibale object. |
Dan Gohman | 8906f95 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 69 | if (U->getOpcode() != Instruction::Add || |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 70 | (!isa<ConstantInt>(U->getOperand(1)) && |
Dan Gohman | 8906f95 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 71 | Operator::getOpcode(U->getOperand(1)) != Instruction::Mul)) |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 72 | return V; |
| 73 | V = U->getOperand(0); |
| 74 | } else { |
| 75 | return V; |
| 76 | } |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 77 | assert(V->getType()->isIntegerTy() && "Unexpected operand type!"); |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 78 | } while (1); |
| 79 | } |
| 80 | |
| 81 | /// getUnderlyingObject - This is a wrapper around Value::getUnderlyingObject |
| 82 | /// and adds support for basic ptrtoint+arithmetic+inttoptr sequences. |
| 83 | static const Value *getUnderlyingObject(const Value *V) { |
| 84 | // First just call Value::getUnderlyingObject to let it do what it does. |
| 85 | do { |
| 86 | V = V->getUnderlyingObject(); |
| 87 | // If it found an inttoptr, use special code to continue climing. |
Dan Gohman | 8906f95 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 88 | if (Operator::getOpcode(V) != Instruction::IntToPtr) |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 89 | break; |
| 90 | const Value *O = getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0)); |
| 91 | // If that succeeded in finding a pointer, continue the search. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 92 | if (!O->getType()->isPointerTy()) |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 93 | break; |
| 94 | V = O; |
| 95 | } while (1); |
| 96 | return V; |
| 97 | } |
| 98 | |
| 99 | /// getUnderlyingObjectForInstr - If this machine instr has memory reference |
| 100 | /// information and it can be tracked to a normal reference to a known |
| 101 | /// object, return the Value for that object. Otherwise return null. |
Evan Cheng | 38bdfc6 | 2009-10-18 19:58:47 +0000 | [diff] [blame] | 102 | static const Value *getUnderlyingObjectForInstr(const MachineInstr *MI, |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 103 | const MachineFrameInfo *MFI, |
| 104 | bool &MayAlias) { |
| 105 | MayAlias = true; |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 106 | if (!MI->hasOneMemOperand() || |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 107 | !(*MI->memoperands_begin())->getValue() || |
| 108 | (*MI->memoperands_begin())->isVolatile()) |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 109 | return 0; |
| 110 | |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 111 | const Value *V = (*MI->memoperands_begin())->getValue(); |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 112 | if (!V) |
| 113 | return 0; |
| 114 | |
| 115 | V = getUnderlyingObject(V); |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 116 | if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) { |
| 117 | // For now, ignore PseudoSourceValues which may alias LLVM IR values |
| 118 | // because the code that uses this function has no way to cope with |
| 119 | // such aliases. |
Evan Cheng | 38bdfc6 | 2009-10-18 19:58:47 +0000 | [diff] [blame] | 120 | if (PSV->isAliased(MFI)) |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 121 | return 0; |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 122 | |
| 123 | MayAlias = PSV->mayAlias(MFI); |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 124 | return V; |
| 125 | } |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 126 | |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 127 | if (isIdentifiedObject(V)) |
| 128 | return V; |
| 129 | |
| 130 | return 0; |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 133 | void ScheduleDAGInstrs::StartBlock(MachineBasicBlock *BB) { |
| 134 | if (MachineLoop *ML = MLI.getLoopFor(BB)) |
| 135 | if (BB == ML->getLoopLatch()) { |
| 136 | MachineBasicBlock *Header = ML->getHeader(); |
| 137 | for (MachineBasicBlock::livein_iterator I = Header->livein_begin(), |
| 138 | E = Header->livein_end(); I != E; ++I) |
| 139 | LoopLiveInRegs.insert(*I); |
| 140 | LoopRegs.VisitLoop(ML); |
| 141 | } |
| 142 | } |
| 143 | |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame^] | 144 | /// AddSchedBarrierDeps - Add dependencies from instructions in the current |
| 145 | /// list of instructions being scheduled to scheduling barrier by adding |
| 146 | /// the exit SU to the register defs and use list. This is because we want to |
| 147 | /// make sure instructions which define registers that are either used by |
| 148 | /// the terminator or are live-out are properly scheduled. This is |
| 149 | /// especially important when the definition latency of the return value(s) |
| 150 | /// are too high to be hidden by the branch or when the liveout registers |
| 151 | /// used by instructions in the fallthrough block. |
| 152 | void ScheduleDAGInstrs::AddSchedBarrierDeps() { |
| 153 | MachineInstr *ExitMI = InsertPos != BB->end() ? &*InsertPos : 0; |
| 154 | ExitSU.setInstr(ExitMI); |
| 155 | bool AllDepKnown = ExitMI && |
| 156 | (ExitMI->getDesc().isCall() || ExitMI->getDesc().isBarrier()); |
| 157 | if (ExitMI && AllDepKnown) { |
| 158 | // If it's a call or a barrier, add dependencies on the defs and uses of |
| 159 | // instruction. |
| 160 | for (unsigned i = 0, e = ExitMI->getNumOperands(); i != e; ++i) { |
| 161 | const MachineOperand &MO = ExitMI->getOperand(i); |
| 162 | if (!MO.isReg() || MO.isDef()) continue; |
| 163 | unsigned Reg = MO.getReg(); |
| 164 | if (Reg == 0) continue; |
| 165 | |
| 166 | assert(TRI->isPhysicalRegister(Reg) && "Virtual register encountered!"); |
| 167 | Uses[Reg].push_back(&ExitSU); |
| 168 | } |
| 169 | } else { |
| 170 | // For others, e.g. fallthrough, conditional branch, assume the exit |
| 171 | // uses all the registers. |
| 172 | for (int i = 0, e = TRI->getNumRegs(); i != e; ++i) |
| 173 | Uses[i].push_back(&ExitSU); |
| 174 | } |
| 175 | } |
| 176 | |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 177 | void ScheduleDAGInstrs::BuildSchedGraph(AliasAnalysis *AA) { |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 178 | // We'll be allocating one SUnit for each instruction, plus one for |
| 179 | // the region exit node. |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 180 | SUnits.reserve(BB->size()); |
| 181 | |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 182 | // We build scheduling units by walking a block's instruction list from bottom |
| 183 | // to top. |
| 184 | |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 185 | // Remember where a generic side-effecting instruction is as we procede. |
| 186 | SUnit *BarrierChain = 0, *AliasChain = 0; |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 187 | |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 188 | // Memory references to specific known memory locations are tracked |
| 189 | // so that they can be given more precise dependencies. We track |
| 190 | // separately the known memory locations that may alias and those |
| 191 | // that are known not to alias |
| 192 | std::map<const Value *, SUnit *> AliasMemDefs, NonAliasMemDefs; |
| 193 | std::map<const Value *, std::vector<SUnit *> > AliasMemUses, NonAliasMemUses; |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 194 | |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 195 | // Keep track of dangling debug references to registers. |
Bill Wendling | 87ea294 | 2010-07-15 20:04:36 +0000 | [diff] [blame] | 196 | std::vector<std::pair<MachineInstr*, unsigned> > |
| 197 | DanglingDebugValue(TRI->getNumRegs(), |
| 198 | std::make_pair(static_cast<MachineInstr*>(0), 0)); |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 199 | |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 200 | // Check to see if the scheduler cares about latencies. |
| 201 | bool UnitLatencies = ForceUnitLatencies(); |
| 202 | |
Dan Gohman | 8749b61 | 2008-12-16 03:35:01 +0000 | [diff] [blame] | 203 | // Ask the target if address-backscheduling is desirable, and if so how much. |
David Goodwin | 7104616 | 2009-08-13 16:05:04 +0000 | [diff] [blame] | 204 | const TargetSubtarget &ST = TM.getSubtarget<TargetSubtarget>(); |
| 205 | unsigned SpecialAddressLatency = ST.getSpecialAddressLatency(); |
Dan Gohman | 8749b61 | 2008-12-16 03:35:01 +0000 | [diff] [blame] | 206 | |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 207 | // Remove any stale debug info; sometimes BuildSchedGraph is called again |
| 208 | // without emitting the info from the previous call. |
| 209 | DbgValueVec.clear(); |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 210 | |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame^] | 211 | // Model data dependencies between instructions being scheduled and the |
| 212 | // ExitSU. |
| 213 | AddSchedBarrierDeps(); |
| 214 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 215 | // Walk the list of instructions, from bottom moving up. |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 216 | for (MachineBasicBlock::iterator MII = InsertPos, MIE = Begin; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 217 | MII != MIE; --MII) { |
| 218 | MachineInstr *MI = prior(MII); |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 219 | // DBG_VALUE does not have SUnit's built, so just remember these for later |
| 220 | // reinsertion. |
| 221 | if (MI->isDebugValue()) { |
| 222 | if (MI->getNumOperands()==3 && MI->getOperand(0).isReg() && |
| 223 | MI->getOperand(0).getReg()) |
| 224 | DanglingDebugValue[MI->getOperand(0).getReg()] = |
| 225 | std::make_pair(MI, DbgValueVec.size()); |
| 226 | DbgValueVec.push_back(MI); |
| 227 | continue; |
| 228 | } |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 229 | const TargetInstrDesc &TID = MI->getDesc(); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 230 | assert(!TID.isTerminator() && !MI->isLabel() && |
| 231 | "Cannot schedule terminators or labels!"); |
| 232 | // Create the SUnit for this MI. |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 233 | SUnit *SU = NewSUnit(MI); |
| 234 | |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 235 | // Assign the Latency field of SU using target-provided information. |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 236 | if (UnitLatencies) |
| 237 | SU->Latency = 1; |
| 238 | else |
| 239 | ComputeLatency(SU); |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 240 | |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 241 | // Add register-based dependencies (data, anti, and output). |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 242 | for (unsigned j = 0, n = MI->getNumOperands(); j != n; ++j) { |
| 243 | const MachineOperand &MO = MI->getOperand(j); |
| 244 | if (!MO.isReg()) continue; |
| 245 | unsigned Reg = MO.getReg(); |
| 246 | if (Reg == 0) continue; |
| 247 | |
| 248 | assert(TRI->isPhysicalRegister(Reg) && "Virtual register encountered!"); |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 249 | |
| 250 | if (MO.isDef() && DanglingDebugValue[Reg].first!=0) { |
Jim Grosbach | 309d20c | 2010-05-19 22:57:06 +0000 | [diff] [blame] | 251 | SU->DbgInstrList.push_back(DanglingDebugValue[Reg].first); |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 252 | DbgValueVec[DanglingDebugValue[Reg].second] = 0; |
| 253 | DanglingDebugValue[Reg] = std::make_pair((MachineInstr*)0, 0); |
| 254 | } |
| 255 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 256 | std::vector<SUnit *> &UseList = Uses[Reg]; |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 257 | std::vector<SUnit *> &DefList = Defs[Reg]; |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 258 | // Optionally add output and anti dependencies. For anti |
| 259 | // dependencies we use a latency of 0 because for a multi-issue |
| 260 | // target we want to allow the defining instruction to issue |
| 261 | // in the same cycle as the using instruction. |
| 262 | // TODO: Using a latency of 1 here for output dependencies assumes |
| 263 | // there's no cost for reusing registers. |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 264 | SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output; |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 265 | unsigned AOLatency = (Kind == SDep::Anti) ? 0 : 1; |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 266 | for (unsigned i = 0, e = DefList.size(); i != e; ++i) { |
| 267 | SUnit *DefSU = DefList[i]; |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame^] | 268 | if (DefSU == &ExitSU) |
| 269 | continue; |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 270 | if (DefSU != SU && |
| 271 | (Kind != SDep::Output || !MO.isDead() || |
| 272 | !DefSU->getInstr()->registerDefIsDead(Reg))) |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 273 | DefSU->addPred(SDep(SU, Kind, AOLatency, /*Reg=*/Reg)); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 274 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 275 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 276 | std::vector<SUnit *> &DefList = Defs[*Alias]; |
| 277 | for (unsigned i = 0, e = DefList.size(); i != e; ++i) { |
| 278 | SUnit *DefSU = DefList[i]; |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame^] | 279 | if (DefSU == &ExitSU) |
| 280 | continue; |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 281 | if (DefSU != SU && |
| 282 | (Kind != SDep::Output || !MO.isDead() || |
Dan Gohman | 91203cf | 2009-10-26 18:26:18 +0000 | [diff] [blame] | 283 | !DefSU->getInstr()->registerDefIsDead(*Alias))) |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 284 | DefSU->addPred(SDep(SU, Kind, AOLatency, /*Reg=*/ *Alias)); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 285 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | if (MO.isDef()) { |
| 289 | // Add any data dependencies. |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 290 | unsigned DataLatency = SU->Latency; |
| 291 | for (unsigned i = 0, e = UseList.size(); i != e; ++i) { |
| 292 | SUnit *UseSU = UseList[i]; |
Evan Cheng | a69ec09 | 2010-03-22 21:24:33 +0000 | [diff] [blame] | 293 | if (UseSU == SU) |
| 294 | continue; |
| 295 | unsigned LDataLatency = DataLatency; |
| 296 | // Optionally add in a special extra latency for nodes that |
| 297 | // feed addresses. |
| 298 | // TODO: Do this for register aliases too. |
| 299 | // TODO: Perhaps we should get rid of |
| 300 | // SpecialAddressLatency and just move this into |
| 301 | // adjustSchedDependency for the targets that care about it. |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame^] | 302 | if (SpecialAddressLatency != 0 && !UnitLatencies && |
| 303 | UseSU != &ExitSU) { |
Evan Cheng | a69ec09 | 2010-03-22 21:24:33 +0000 | [diff] [blame] | 304 | MachineInstr *UseMI = UseSU->getInstr(); |
| 305 | const TargetInstrDesc &UseTID = UseMI->getDesc(); |
| 306 | int RegUseIndex = UseMI->findRegisterUseOperandIdx(Reg); |
| 307 | assert(RegUseIndex >= 0 && "UseMI doesn's use register!"); |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame^] | 308 | if (RegUseIndex >= 0 && |
| 309 | (UseTID.mayLoad() || UseTID.mayStore()) && |
Evan Cheng | a69ec09 | 2010-03-22 21:24:33 +0000 | [diff] [blame] | 310 | (unsigned)RegUseIndex < UseTID.getNumOperands() && |
| 311 | UseTID.OpInfo[RegUseIndex].isLookupPtrRegClass()) |
| 312 | LDataLatency += SpecialAddressLatency; |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 313 | } |
Evan Cheng | a69ec09 | 2010-03-22 21:24:33 +0000 | [diff] [blame] | 314 | // Adjust the dependence latency using operand def/use |
| 315 | // information (if any), and then allow the target to |
| 316 | // perform its own adjustments. |
| 317 | const SDep& dep = SDep(SU, SDep::Data, LDataLatency, Reg); |
| 318 | if (!UnitLatencies) { |
Dan Gohman | 3fb150a | 2010-04-17 17:42:52 +0000 | [diff] [blame] | 319 | ComputeOperandLatency(SU, UseSU, const_cast<SDep &>(dep)); |
| 320 | ST.adjustSchedDependency(SU, UseSU, const_cast<SDep &>(dep)); |
Evan Cheng | a69ec09 | 2010-03-22 21:24:33 +0000 | [diff] [blame] | 321 | } |
| 322 | UseSU->addPred(dep); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 323 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 324 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 325 | std::vector<SUnit *> &UseList = Uses[*Alias]; |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 326 | for (unsigned i = 0, e = UseList.size(); i != e; ++i) { |
| 327 | SUnit *UseSU = UseList[i]; |
Evan Cheng | a69ec09 | 2010-03-22 21:24:33 +0000 | [diff] [blame] | 328 | if (UseSU == SU) |
| 329 | continue; |
| 330 | const SDep& dep = SDep(SU, SDep::Data, DataLatency, *Alias); |
| 331 | if (!UnitLatencies) { |
Dan Gohman | 3fb150a | 2010-04-17 17:42:52 +0000 | [diff] [blame] | 332 | ComputeOperandLatency(SU, UseSU, const_cast<SDep &>(dep)); |
| 333 | ST.adjustSchedDependency(SU, UseSU, const_cast<SDep &>(dep)); |
David Goodwin | 7104616 | 2009-08-13 16:05:04 +0000 | [diff] [blame] | 334 | } |
Evan Cheng | a69ec09 | 2010-03-22 21:24:33 +0000 | [diff] [blame] | 335 | UseSU->addPred(dep); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 336 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Dan Gohman | 8749b61 | 2008-12-16 03:35:01 +0000 | [diff] [blame] | 339 | // If a def is going to wrap back around to the top of the loop, |
| 340 | // backschedule it. |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 341 | if (!UnitLatencies && DefList.empty()) { |
Dan Gohman | 8749b61 | 2008-12-16 03:35:01 +0000 | [diff] [blame] | 342 | LoopDependencies::LoopDeps::iterator I = LoopRegs.Deps.find(Reg); |
| 343 | if (I != LoopRegs.Deps.end()) { |
| 344 | const MachineOperand *UseMO = I->second.first; |
| 345 | unsigned Count = I->second.second; |
| 346 | const MachineInstr *UseMI = UseMO->getParent(); |
| 347 | unsigned UseMOIdx = UseMO - &UseMI->getOperand(0); |
| 348 | const TargetInstrDesc &UseTID = UseMI->getDesc(); |
| 349 | // TODO: If we knew the total depth of the region here, we could |
| 350 | // handle the case where the whole loop is inside the region but |
| 351 | // is large enough that the isScheduleHigh trick isn't needed. |
| 352 | if (UseMOIdx < UseTID.getNumOperands()) { |
| 353 | // Currently, we only support scheduling regions consisting of |
| 354 | // single basic blocks. Check to see if the instruction is in |
| 355 | // the same region by checking to see if it has the same parent. |
| 356 | if (UseMI->getParent() != MI->getParent()) { |
| 357 | unsigned Latency = SU->Latency; |
| 358 | if (UseTID.OpInfo[UseMOIdx].isLookupPtrRegClass()) |
| 359 | Latency += SpecialAddressLatency; |
| 360 | // This is a wild guess as to the portion of the latency which |
| 361 | // will be overlapped by work done outside the current |
| 362 | // scheduling region. |
| 363 | Latency -= std::min(Latency, Count); |
| 364 | // Add the artifical edge. |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 365 | ExitSU.addPred(SDep(SU, SDep::Order, Latency, |
| 366 | /*Reg=*/0, /*isNormalMemory=*/false, |
| 367 | /*isMustAlias=*/false, |
| 368 | /*isArtificial=*/true)); |
Dan Gohman | 8749b61 | 2008-12-16 03:35:01 +0000 | [diff] [blame] | 369 | } else if (SpecialAddressLatency > 0 && |
| 370 | UseTID.OpInfo[UseMOIdx].isLookupPtrRegClass()) { |
| 371 | // The entire loop body is within the current scheduling region |
| 372 | // and the latency of this operation is assumed to be greater |
| 373 | // than the latency of the loop. |
| 374 | // TODO: Recursively mark data-edge predecessors as |
| 375 | // isScheduleHigh too. |
| 376 | SU->isScheduleHigh = true; |
| 377 | } |
| 378 | } |
| 379 | LoopRegs.Deps.erase(I); |
| 380 | } |
| 381 | } |
| 382 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 383 | UseList.clear(); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 384 | if (!MO.isDead()) |
| 385 | DefList.clear(); |
| 386 | DefList.push_back(SU); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 387 | } else { |
| 388 | UseList.push_back(SU); |
| 389 | } |
| 390 | } |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 391 | |
| 392 | // Add chain dependencies. |
David Goodwin | 7c9b1ac | 2009-11-02 17:06:28 +0000 | [diff] [blame] | 393 | // Chain dependencies used to enforce memory order should have |
| 394 | // latency of 0 (except for true dependency of Store followed by |
| 395 | // aliased Load... we estimate that with a single cycle of latency |
| 396 | // assuming the hardware will bypass) |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 397 | // Note that isStoreToStackSlot and isLoadFromStackSLot are not usable |
| 398 | // after stack slots are lowered to actual addresses. |
| 399 | // TODO: Use an AliasAnalysis and do real alias-analysis queries, and |
| 400 | // produce more precise dependence information. |
David Goodwin | 7c9b1ac | 2009-11-02 17:06:28 +0000 | [diff] [blame] | 401 | #define STORE_LOAD_LATENCY 1 |
| 402 | unsigned TrueMemOrderLatency = 0; |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 403 | if (TID.isCall() || TID.hasUnmodeledSideEffects() || |
| 404 | (MI->hasVolatileMemoryRef() && |
| 405 | (!TID.mayLoad() || !MI->isInvariantLoad(AA)))) { |
| 406 | // Be conservative with these and add dependencies on all memory |
| 407 | // references, even those that are known to not alias. |
| 408 | for (std::map<const Value *, SUnit *>::iterator I = |
| 409 | NonAliasMemDefs.begin(), E = NonAliasMemDefs.end(); I != E; ++I) { |
David Goodwin | 7c9b1ac | 2009-11-02 17:06:28 +0000 | [diff] [blame] | 410 | I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 411 | } |
| 412 | for (std::map<const Value *, std::vector<SUnit *> >::iterator I = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 413 | NonAliasMemUses.begin(), E = NonAliasMemUses.end(); I != E; ++I) { |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 414 | for (unsigned i = 0, e = I->second.size(); i != e; ++i) |
David Goodwin | 7c9b1ac | 2009-11-02 17:06:28 +0000 | [diff] [blame] | 415 | I->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 416 | } |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 417 | NonAliasMemDefs.clear(); |
| 418 | NonAliasMemUses.clear(); |
| 419 | // Add SU to the barrier chain. |
| 420 | if (BarrierChain) |
| 421 | BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0)); |
| 422 | BarrierChain = SU; |
| 423 | |
| 424 | // fall-through |
| 425 | new_alias_chain: |
| 426 | // Chain all possibly aliasing memory references though SU. |
| 427 | if (AliasChain) |
| 428 | AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0)); |
| 429 | AliasChain = SU; |
| 430 | for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k) |
| 431 | PendingLoads[k]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency)); |
| 432 | for (std::map<const Value *, SUnit *>::iterator I = AliasMemDefs.begin(), |
| 433 | E = AliasMemDefs.end(); I != E; ++I) { |
| 434 | I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0)); |
| 435 | } |
| 436 | for (std::map<const Value *, std::vector<SUnit *> >::iterator I = |
| 437 | AliasMemUses.begin(), E = AliasMemUses.end(); I != E; ++I) { |
| 438 | for (unsigned i = 0, e = I->second.size(); i != e; ++i) |
| 439 | I->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency)); |
| 440 | } |
| 441 | PendingLoads.clear(); |
| 442 | AliasMemDefs.clear(); |
| 443 | AliasMemUses.clear(); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 444 | } else if (TID.mayStore()) { |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 445 | bool MayAlias = true; |
David Goodwin | 7c9b1ac | 2009-11-02 17:06:28 +0000 | [diff] [blame] | 446 | TrueMemOrderLatency = STORE_LOAD_LATENCY; |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 447 | if (const Value *V = getUnderlyingObjectForInstr(MI, MFI, MayAlias)) { |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 448 | // A store to a specific PseudoSourceValue. Add precise dependencies. |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 449 | // Record the def in MemDefs, first adding a dep if there is |
| 450 | // an existing def. |
| 451 | std::map<const Value *, SUnit *>::iterator I = |
| 452 | ((MayAlias) ? AliasMemDefs.find(V) : NonAliasMemDefs.find(V)); |
| 453 | std::map<const Value *, SUnit *>::iterator IE = |
| 454 | ((MayAlias) ? AliasMemDefs.end() : NonAliasMemDefs.end()); |
| 455 | if (I != IE) { |
David Goodwin | 7c9b1ac | 2009-11-02 17:06:28 +0000 | [diff] [blame] | 456 | I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0, /*Reg=*/0, |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 457 | /*isNormalMemory=*/true)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 458 | I->second = SU; |
| 459 | } else { |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 460 | if (MayAlias) |
| 461 | AliasMemDefs[V] = SU; |
| 462 | else |
| 463 | NonAliasMemDefs[V] = SU; |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 464 | } |
| 465 | // Handle the uses in MemUses, if there are any. |
Dan Gohman | a629b48 | 2008-12-08 17:50:35 +0000 | [diff] [blame] | 466 | std::map<const Value *, std::vector<SUnit *> >::iterator J = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 467 | ((MayAlias) ? AliasMemUses.find(V) : NonAliasMemUses.find(V)); |
| 468 | std::map<const Value *, std::vector<SUnit *> >::iterator JE = |
| 469 | ((MayAlias) ? AliasMemUses.end() : NonAliasMemUses.end()); |
| 470 | if (J != JE) { |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 471 | for (unsigned i = 0, e = J->second.size(); i != e; ++i) |
David Goodwin | 7c9b1ac | 2009-11-02 17:06:28 +0000 | [diff] [blame] | 472 | J->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency, |
| 473 | /*Reg=*/0, /*isNormalMemory=*/true)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 474 | J->second.clear(); |
| 475 | } |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 476 | if (MayAlias) { |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 477 | // Add dependencies from all the PendingLoads, i.e. loads |
| 478 | // with no underlying object. |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 479 | for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k) |
| 480 | PendingLoads[k]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency)); |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 481 | // Add dependence on alias chain, if needed. |
| 482 | if (AliasChain) |
| 483 | AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0)); |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 484 | } |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 485 | // Add dependence on barrier chain, if needed. |
| 486 | if (BarrierChain) |
| 487 | BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0)); |
David Goodwin | 5be870a | 2009-11-05 00:16:44 +0000 | [diff] [blame] | 488 | } else { |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 489 | // Treat all other stores conservatively. |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 490 | goto new_alias_chain; |
David Goodwin | 7c9b1ac | 2009-11-02 17:06:28 +0000 | [diff] [blame] | 491 | } |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame^] | 492 | |
| 493 | if (!ExitSU.isPred(SU)) |
| 494 | // Push store's up a bit to avoid them getting in between cmp |
| 495 | // and branches. |
| 496 | ExitSU.addPred(SDep(SU, SDep::Order, 0, |
| 497 | /*Reg=*/0, /*isNormalMemory=*/false, |
| 498 | /*isMustAlias=*/false, |
| 499 | /*isArtificial=*/true)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 500 | } else if (TID.mayLoad()) { |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 501 | bool MayAlias = true; |
David Goodwin | 7c9b1ac | 2009-11-02 17:06:28 +0000 | [diff] [blame] | 502 | TrueMemOrderLatency = 0; |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 503 | if (MI->isInvariantLoad(AA)) { |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 504 | // Invariant load, no chain dependencies needed! |
David Goodwin | 5be870a | 2009-11-05 00:16:44 +0000 | [diff] [blame] | 505 | } else { |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 506 | if (const Value *V = |
| 507 | getUnderlyingObjectForInstr(MI, MFI, MayAlias)) { |
| 508 | // A load from a specific PseudoSourceValue. Add precise dependencies. |
| 509 | std::map<const Value *, SUnit *>::iterator I = |
| 510 | ((MayAlias) ? AliasMemDefs.find(V) : NonAliasMemDefs.find(V)); |
| 511 | std::map<const Value *, SUnit *>::iterator IE = |
| 512 | ((MayAlias) ? AliasMemDefs.end() : NonAliasMemDefs.end()); |
| 513 | if (I != IE) |
| 514 | I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0, /*Reg=*/0, |
| 515 | /*isNormalMemory=*/true)); |
| 516 | if (MayAlias) |
| 517 | AliasMemUses[V].push_back(SU); |
| 518 | else |
| 519 | NonAliasMemUses[V].push_back(SU); |
| 520 | } else { |
| 521 | // A load with no underlying object. Depend on all |
| 522 | // potentially aliasing stores. |
| 523 | for (std::map<const Value *, SUnit *>::iterator I = |
| 524 | AliasMemDefs.begin(), E = AliasMemDefs.end(); I != E; ++I) |
| 525 | I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0)); |
| 526 | |
| 527 | PendingLoads.push_back(SU); |
| 528 | MayAlias = true; |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 529 | } |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 530 | |
| 531 | // Add dependencies on alias and barrier chains, if needed. |
| 532 | if (MayAlias && AliasChain) |
| 533 | AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0)); |
| 534 | if (BarrierChain) |
| 535 | BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0)); |
| 536 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 537 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 538 | } |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 539 | |
| 540 | for (int i = 0, e = TRI->getNumRegs(); i != e; ++i) { |
| 541 | Defs[i].clear(); |
| 542 | Uses[i].clear(); |
| 543 | } |
| 544 | PendingLoads.clear(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 547 | void ScheduleDAGInstrs::FinishBlock() { |
| 548 | // Nothing to do. |
| 549 | } |
| 550 | |
Dan Gohman | c8c2827 | 2008-11-21 00:12:10 +0000 | [diff] [blame] | 551 | void ScheduleDAGInstrs::ComputeLatency(SUnit *SU) { |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 552 | // Compute the latency for the node. |
Evan Cheng | 3ef1c87 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 553 | if (!InstrItins || InstrItins->isEmpty()) { |
| 554 | SU->Latency = 1; |
Dan Gohman | 4ea8e85 | 2008-12-16 02:38:22 +0000 | [diff] [blame] | 555 | |
Evan Cheng | 3ef1c87 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 556 | // Simplistic target-independent heuristic: assume that loads take |
| 557 | // extra time. |
Dan Gohman | 4ea8e85 | 2008-12-16 02:38:22 +0000 | [diff] [blame] | 558 | if (SU->getInstr()->getDesc().mayLoad()) |
| 559 | SU->Latency += 2; |
Evan Cheng | 3ef1c87 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 560 | } else |
| 561 | SU->Latency = |
| 562 | InstrItins->getStageLatency(SU->getInstr()->getDesc().getSchedClass()); |
Dan Gohman | c8c2827 | 2008-11-21 00:12:10 +0000 | [diff] [blame] | 563 | } |
| 564 | |
David Goodwin | dc4bdcd | 2009-08-19 16:08:58 +0000 | [diff] [blame] | 565 | void ScheduleDAGInstrs::ComputeOperandLatency(SUnit *Def, SUnit *Use, |
| 566 | SDep& dep) const { |
Evan Cheng | 3ef1c87 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 567 | if (!InstrItins || InstrItins->isEmpty()) |
David Goodwin | dc4bdcd | 2009-08-19 16:08:58 +0000 | [diff] [blame] | 568 | return; |
| 569 | |
| 570 | // For a data dependency with a known register... |
| 571 | if ((dep.getKind() != SDep::Data) || (dep.getReg() == 0)) |
| 572 | return; |
| 573 | |
| 574 | const unsigned Reg = dep.getReg(); |
| 575 | |
| 576 | // ... find the definition of the register in the defining |
| 577 | // instruction |
| 578 | MachineInstr *DefMI = Def->getInstr(); |
| 579 | int DefIdx = DefMI->findRegisterDefOperandIdx(Reg); |
| 580 | if (DefIdx != -1) { |
Evan Cheng | 1aca5bc | 2010-10-08 18:42:25 +0000 | [diff] [blame] | 581 | const MachineOperand &MO = DefMI->getOperand(DefIdx); |
| 582 | if (MO.isReg() && MO.isImplicit() && |
Evan Cheng | d82de83 | 2010-10-08 23:01:57 +0000 | [diff] [blame] | 583 | DefIdx >= (int)DefMI->getDesc().getNumOperands()) { |
Evan Cheng | 1aca5bc | 2010-10-08 18:42:25 +0000 | [diff] [blame] | 584 | // This is an implicit def, getOperandLatency() won't return the correct |
| 585 | // latency. e.g. |
| 586 | // %D6<def>, %D7<def> = VLD1q16 %R2<kill>, 0, ..., %Q3<imp-def> |
| 587 | // %Q1<def> = VMULv8i16 %Q1<kill>, %Q3<kill>, ... |
| 588 | // What we want is to compute latency between def of %D6/%D7 and use of |
| 589 | // %Q3 instead. |
| 590 | DefIdx = DefMI->findRegisterDefOperandIdx(Reg, false, true, TRI); |
| 591 | } |
Evan Cheng | a0792de | 2010-10-06 06:27:31 +0000 | [diff] [blame] | 592 | MachineInstr *UseMI = Use->getInstr(); |
Evan Cheng | 3881cb7 | 2010-09-29 22:42:35 +0000 | [diff] [blame] | 593 | // For all uses of the register, calculate the maxmimum latency |
| 594 | int Latency = -1; |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame^] | 595 | if (UseMI) { |
| 596 | for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) { |
| 597 | const MachineOperand &MO = UseMI->getOperand(i); |
| 598 | if (!MO.isReg() || !MO.isUse()) |
| 599 | continue; |
| 600 | unsigned MOReg = MO.getReg(); |
| 601 | if (MOReg != Reg) |
| 602 | continue; |
David Goodwin | dc4bdcd | 2009-08-19 16:08:58 +0000 | [diff] [blame] | 603 | |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame^] | 604 | int UseCycle = TII->getOperandLatency(InstrItins, DefMI, DefIdx, |
| 605 | UseMI, i); |
| 606 | Latency = std::max(Latency, UseCycle); |
| 607 | } |
| 608 | } else { |
| 609 | // UseMI is null, then it must be a scheduling barrier. |
| 610 | if (!InstrItins || InstrItins->isEmpty()) |
| 611 | return; |
| 612 | unsigned DefClass = DefMI->getDesc().getSchedClass(); |
| 613 | Latency = InstrItins->getOperandCycle(DefClass, DefIdx); |
David Goodwin | dc4bdcd | 2009-08-19 16:08:58 +0000 | [diff] [blame] | 614 | } |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame^] | 615 | |
| 616 | // If we found a latency, then replace the existing dependence latency. |
| 617 | if (Latency >= 0) |
| 618 | dep.setLatency(Latency); |
David Goodwin | dc4bdcd | 2009-08-19 16:08:58 +0000 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 622 | void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const { |
| 623 | SU->getInstr()->dump(); |
| 624 | } |
| 625 | |
| 626 | std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const { |
| 627 | std::string s; |
| 628 | raw_string_ostream oss(s); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 629 | if (SU == &EntrySU) |
| 630 | oss << "<entry>"; |
| 631 | else if (SU == &ExitSU) |
| 632 | oss << "<exit>"; |
| 633 | else |
| 634 | SU->getInstr()->print(oss); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 635 | return oss.str(); |
| 636 | } |
| 637 | |
| 638 | // EmitSchedule - Emit the machine code in scheduled order. |
Dan Gohman | af1d8ca | 2010-05-01 00:01:06 +0000 | [diff] [blame] | 639 | MachineBasicBlock *ScheduleDAGInstrs::EmitSchedule() { |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 640 | // For MachineInstr-based scheduling, we're rescheduling the instructions in |
| 641 | // the block, so start by removing them from the block. |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 642 | while (Begin != InsertPos) { |
Dan Gohman | f711939 | 2009-01-16 22:10:20 +0000 | [diff] [blame] | 643 | MachineBasicBlock::iterator I = Begin; |
| 644 | ++Begin; |
| 645 | BB->remove(I); |
| 646 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 647 | |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 648 | // First reinsert any remaining debug_values; these are either constants, |
| 649 | // or refer to live-in registers. The beginning of the block is the right |
| 650 | // place for the latter. The former might reasonably be placed elsewhere |
| 651 | // using some kind of ordering algorithm, but right now it doesn't matter. |
| 652 | for (int i = DbgValueVec.size()-1; i>=0; --i) |
| 653 | if (DbgValueVec[i]) |
| 654 | BB->insert(InsertPos, DbgValueVec[i]); |
| 655 | |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 656 | // Then re-insert them according to the given schedule. |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 657 | for (unsigned i = 0, e = Sequence.size(); i != e; i++) { |
| 658 | SUnit *SU = Sequence[i]; |
| 659 | if (!SU) { |
| 660 | // Null SUnit* is a noop. |
| 661 | EmitNoop(); |
| 662 | continue; |
| 663 | } |
| 664 | |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 665 | BB->insert(InsertPos, SU->getInstr()); |
Jim Grosbach | 309d20c | 2010-05-19 22:57:06 +0000 | [diff] [blame] | 666 | for (unsigned i = 0, e = SU->DbgInstrList.size() ; i < e ; ++i) |
| 667 | BB->insert(InsertPos, SU->DbgInstrList[i]); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 670 | // Update the Begin iterator, as the first instruction in the block |
| 671 | // may have been scheduled later. |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 672 | if (!DbgValueVec.empty()) { |
| 673 | for (int i = DbgValueVec.size()-1; i>=0; --i) |
| 674 | if (DbgValueVec[i]!=0) { |
| 675 | Begin = DbgValueVec[DbgValueVec.size()-1]; |
| 676 | break; |
| 677 | } |
| 678 | } else if (!Sequence.empty()) |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 679 | Begin = Sequence[0]->getInstr(); |
| 680 | |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 681 | DbgValueVec.clear(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 682 | return BB; |
| 683 | } |