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