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