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" |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 16 | #include "RegisterPressure.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" |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineMemOperand.h" |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Andrew Trick | ed395c8 | 2012-03-07 23:01:06 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/ScheduleDAGInstrs.h" |
Evan Cheng | ab8be96 | 2011-06-29 01:14:12 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCInstrItineraries.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetMachine.h" |
| 28 | #include "llvm/Target/TargetInstrInfo.h" |
| 29 | #include "llvm/Target/TargetRegisterInfo.h" |
Evan Cheng | 5b1b4489 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetSubtargetInfo.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Debug.h" |
| 32 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallSet.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 34 | using namespace llvm; |
| 35 | |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 36 | ScheduleDAGInstrs::ScheduleDAGInstrs(MachineFunction &mf, |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 37 | const MachineLoopInfo &mli, |
Andrew Trick | 5e920d7 | 2012-01-14 02:17:12 +0000 | [diff] [blame] | 38 | const MachineDominatorTree &mdt, |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 39 | bool IsPostRAFlag, |
| 40 | LiveIntervals *lis) |
Evan Cheng | 3ef1c87 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 41 | : ScheduleDAG(mf), MLI(mli), MDT(mdt), MFI(mf.getFrameInfo()), |
Andrew Trick | d790cad | 2012-03-07 23:00:59 +0000 | [diff] [blame] | 42 | InstrItins(mf.getTarget().getInstrItineraryData()), LIS(lis), |
Andrew Trick | 0070792 | 2012-04-13 23:29:54 +0000 | [diff] [blame] | 43 | IsPostRA(IsPostRAFlag), UnitLatencies(false), CanHandleTerminators(false), |
| 44 | LoopRegs(MLI, MDT), FirstDbgValue(0) { |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 45 | assert((IsPostRA || LIS) && "PreRA scheduling requires LiveIntervals"); |
Devang Patel | cf4cc84 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 46 | DbgValues.clear(); |
Andrew Trick | cc77b54 | 2012-02-22 06:08:13 +0000 | [diff] [blame] | 47 | assert(!(IsPostRA && MRI.getNumVirtRegs()) && |
Andrew Trick | 19273ae | 2012-02-21 04:51:23 +0000 | [diff] [blame] | 48 | "Virtual registers must be removed prior to PostRA scheduling"); |
Evan Cheng | 38bdfc6 | 2009-10-18 19:58:47 +0000 | [diff] [blame] | 49 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 50 | |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 51 | /// getUnderlyingObjectFromInt - This is the function that does the work of |
| 52 | /// looking through basic ptrtoint+arithmetic+inttoptr sequences. |
| 53 | static const Value *getUnderlyingObjectFromInt(const Value *V) { |
| 54 | do { |
Dan Gohman | 8906f95 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 55 | if (const Operator *U = dyn_cast<Operator>(V)) { |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 56 | // If we find a ptrtoint, we can transfer control back to the |
| 57 | // regular getUnderlyingObjectFromInt. |
Dan Gohman | 8906f95 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 58 | if (U->getOpcode() == Instruction::PtrToInt) |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 59 | return U->getOperand(0); |
| 60 | // If we find an add of a constant or a multiplied value, it's |
| 61 | // likely that the other operand will lead us to the base |
| 62 | // object. We don't have to worry about the case where the |
Dan Gohman | 748f98f | 2009-08-07 01:26:06 +0000 | [diff] [blame] | 63 | // object address is somehow being computed by the multiply, |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 64 | // because our callers only care when the result is an |
| 65 | // identifibale object. |
Dan Gohman | 8906f95 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 66 | if (U->getOpcode() != Instruction::Add || |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 67 | (!isa<ConstantInt>(U->getOperand(1)) && |
Dan Gohman | 8906f95 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 68 | Operator::getOpcode(U->getOperand(1)) != Instruction::Mul)) |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 69 | return V; |
| 70 | V = U->getOperand(0); |
| 71 | } else { |
| 72 | return V; |
| 73 | } |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 74 | assert(V->getType()->isIntegerTy() && "Unexpected operand type!"); |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 75 | } while (1); |
| 76 | } |
| 77 | |
Dan Gohman | 5034dd3 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 78 | /// getUnderlyingObject - This is a wrapper around GetUnderlyingObject |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 79 | /// and adds support for basic ptrtoint+arithmetic+inttoptr sequences. |
| 80 | static const Value *getUnderlyingObject(const Value *V) { |
| 81 | // First just call Value::getUnderlyingObject to let it do what it does. |
| 82 | do { |
Dan Gohman | 5034dd3 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 83 | V = GetUnderlyingObject(V); |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 84 | // If it found an inttoptr, use special code to continue climing. |
Dan Gohman | 8906f95 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 85 | if (Operator::getOpcode(V) != Instruction::IntToPtr) |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 86 | break; |
| 87 | const Value *O = getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0)); |
| 88 | // If that succeeded in finding a pointer, continue the search. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 89 | if (!O->getType()->isPointerTy()) |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 90 | break; |
| 91 | V = O; |
| 92 | } while (1); |
| 93 | return V; |
| 94 | } |
| 95 | |
| 96 | /// getUnderlyingObjectForInstr - If this machine instr has memory reference |
| 97 | /// information and it can be tracked to a normal reference to a known |
| 98 | /// object, return the Value for that object. Otherwise return null. |
Evan Cheng | 38bdfc6 | 2009-10-18 19:58:47 +0000 | [diff] [blame] | 99 | static const Value *getUnderlyingObjectForInstr(const MachineInstr *MI, |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 100 | const MachineFrameInfo *MFI, |
| 101 | bool &MayAlias) { |
| 102 | MayAlias = true; |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 103 | if (!MI->hasOneMemOperand() || |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 104 | !(*MI->memoperands_begin())->getValue() || |
| 105 | (*MI->memoperands_begin())->isVolatile()) |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 106 | return 0; |
| 107 | |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 108 | const Value *V = (*MI->memoperands_begin())->getValue(); |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 109 | if (!V) |
| 110 | return 0; |
| 111 | |
| 112 | V = getUnderlyingObject(V); |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 113 | if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) { |
| 114 | // For now, ignore PseudoSourceValues which may alias LLVM IR values |
| 115 | // because the code that uses this function has no way to cope with |
| 116 | // such aliases. |
Evan Cheng | 38bdfc6 | 2009-10-18 19:58:47 +0000 | [diff] [blame] | 117 | if (PSV->isAliased(MFI)) |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 118 | return 0; |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 119 | |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 120 | MayAlias = PSV->mayAlias(MFI); |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 121 | return V; |
| 122 | } |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 123 | |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 124 | if (isIdentifiedObject(V)) |
| 125 | return V; |
| 126 | |
| 127 | return 0; |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Andrew Trick | 918f38a | 2012-04-20 20:05:21 +0000 | [diff] [blame] | 130 | void ScheduleDAGInstrs::startBlock(MachineBasicBlock *bb) { |
| 131 | BB = bb; |
Andrew Trick | e8deca8 | 2011-10-07 06:33:09 +0000 | [diff] [blame] | 132 | LoopRegs.Deps.clear(); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 133 | if (MachineLoop *ML = MLI.getLoopFor(BB)) |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 134 | if (BB == ML->getLoopLatch()) |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 135 | LoopRegs.VisitLoop(ML); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 138 | void ScheduleDAGInstrs::finishBlock() { |
Andrew Trick | a30444a | 2012-04-20 20:24:33 +0000 | [diff] [blame] | 139 | // Subclasses should no longer refer to the old block. |
Andrew Trick | 918f38a | 2012-04-20 20:05:21 +0000 | [diff] [blame] | 140 | BB = 0; |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 143 | /// Initialize the map with the number of registers. |
Andrew Trick | 035ec40 | 2012-03-07 23:00:57 +0000 | [diff] [blame] | 144 | void Reg2SUnitsMap::setRegLimit(unsigned Limit) { |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 145 | PhysRegSet.setUniverse(Limit); |
| 146 | SUnits.resize(Limit); |
| 147 | } |
| 148 | |
| 149 | /// Clear the map without deallocating storage. |
Andrew Trick | 035ec40 | 2012-03-07 23:00:57 +0000 | [diff] [blame] | 150 | void Reg2SUnitsMap::clear() { |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 151 | for (const_iterator I = reg_begin(), E = reg_end(); I != E; ++I) { |
| 152 | SUnits[*I].clear(); |
| 153 | } |
| 154 | PhysRegSet.clear(); |
| 155 | } |
| 156 | |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 157 | /// Initialize the DAG and common scheduler state for the current scheduling |
| 158 | /// region. This does not actually create the DAG, only clears it. The |
| 159 | /// scheduling driver may call BuildSchedGraph multiple times per scheduling |
| 160 | /// region. |
| 161 | void ScheduleDAGInstrs::enterRegion(MachineBasicBlock *bb, |
| 162 | MachineBasicBlock::iterator begin, |
| 163 | MachineBasicBlock::iterator end, |
| 164 | unsigned endcount) { |
Andrew Trick | 918f38a | 2012-04-20 20:05:21 +0000 | [diff] [blame] | 165 | assert(bb == BB && "startBlock should set BB"); |
Andrew Trick | 68675c6 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 166 | RegionBegin = begin; |
| 167 | RegionEnd = end; |
Andrew Trick | cf46b5a | 2012-03-07 23:00:52 +0000 | [diff] [blame] | 168 | EndIndex = endcount; |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 169 | MISUnitMap.clear(); |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 170 | |
| 171 | // Check to see if the scheduler cares about latencies. |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 172 | UnitLatencies = forceUnitLatencies(); |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 173 | |
| 174 | ScheduleDAG::clearDAG(); |
| 175 | } |
| 176 | |
| 177 | /// Close the current scheduling region. Don't clear any state in case the |
| 178 | /// driver wants to refer to the previous scheduling region. |
| 179 | void ScheduleDAGInstrs::exitRegion() { |
| 180 | // Nothing to do. |
| 181 | } |
| 182 | |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 183 | /// addSchedBarrierDeps - Add dependencies from instructions in the current |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 184 | /// list of instructions being scheduled to scheduling barrier by adding |
| 185 | /// the exit SU to the register defs and use list. This is because we want to |
| 186 | /// make sure instructions which define registers that are either used by |
| 187 | /// the terminator or are live-out are properly scheduled. This is |
| 188 | /// especially important when the definition latency of the return value(s) |
| 189 | /// are too high to be hidden by the branch or when the liveout registers |
| 190 | /// used by instructions in the fallthrough block. |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 191 | void ScheduleDAGInstrs::addSchedBarrierDeps() { |
Andrew Trick | 68675c6 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 192 | MachineInstr *ExitMI = RegionEnd != BB->end() ? &*RegionEnd : 0; |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 193 | ExitSU.setInstr(ExitMI); |
| 194 | bool AllDepKnown = ExitMI && |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 195 | (ExitMI->isCall() || ExitMI->isBarrier()); |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 196 | if (ExitMI && AllDepKnown) { |
| 197 | // If it's a call or a barrier, add dependencies on the defs and uses of |
| 198 | // instruction. |
| 199 | for (unsigned i = 0, e = ExitMI->getNumOperands(); i != e; ++i) { |
| 200 | const MachineOperand &MO = ExitMI->getOperand(i); |
| 201 | if (!MO.isReg() || MO.isDef()) continue; |
| 202 | unsigned Reg = MO.getReg(); |
| 203 | if (Reg == 0) continue; |
| 204 | |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 205 | if (TRI->isPhysicalRegister(Reg)) |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 206 | Uses[Reg].push_back(&ExitSU); |
Andrew Trick | d3a7486 | 2012-03-16 05:04:25 +0000 | [diff] [blame] | 207 | else { |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 208 | assert(!IsPostRA && "Virtual register encountered after regalloc."); |
Andrew Trick | d3a7486 | 2012-03-16 05:04:25 +0000 | [diff] [blame] | 209 | addVRegUseDeps(&ExitSU, i); |
| 210 | } |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 211 | } |
| 212 | } else { |
| 213 | // For others, e.g. fallthrough, conditional branch, assume the exit |
Evan Cheng | de5fa93 | 2010-10-27 23:17:17 +0000 | [diff] [blame] | 214 | // uses all the registers that are livein to the successor blocks. |
Benjamin Kramer | a82d526 | 2012-03-16 17:38:19 +0000 | [diff] [blame] | 215 | assert(Uses.empty() && "Uses in set before adding deps?"); |
Evan Cheng | de5fa93 | 2010-10-27 23:17:17 +0000 | [diff] [blame] | 216 | for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(), |
| 217 | SE = BB->succ_end(); SI != SE; ++SI) |
| 218 | for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(), |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 219 | E = (*SI)->livein_end(); I != E; ++I) { |
Evan Cheng | de5fa93 | 2010-10-27 23:17:17 +0000 | [diff] [blame] | 220 | unsigned Reg = *I; |
Benjamin Kramer | a82d526 | 2012-03-16 17:38:19 +0000 | [diff] [blame] | 221 | if (!Uses.contains(Reg)) |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 222 | Uses[Reg].push_back(&ExitSU); |
Evan Cheng | de5fa93 | 2010-10-27 23:17:17 +0000 | [diff] [blame] | 223 | } |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 227 | /// MO is an operand of SU's instruction that defines a physical register. Add |
| 228 | /// data dependencies from SU to any uses of the physical register. |
| 229 | void ScheduleDAGInstrs::addPhysRegDataDeps(SUnit *SU, |
| 230 | const MachineOperand &MO) { |
| 231 | assert(MO.isDef() && "expect physreg def"); |
| 232 | |
| 233 | // Ask the target if address-backscheduling is desirable, and if so how much. |
| 234 | const TargetSubtargetInfo &ST = TM.getSubtarget<TargetSubtargetInfo>(); |
| 235 | unsigned SpecialAddressLatency = ST.getSpecialAddressLatency(); |
| 236 | unsigned DataLatency = SU->Latency; |
| 237 | |
Craig Topper | e4fd907 | 2012-03-04 10:43:23 +0000 | [diff] [blame] | 238 | for (const uint16_t *Alias = TRI->getOverlaps(MO.getReg()); *Alias; ++Alias) { |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 239 | if (!Uses.contains(*Alias)) |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 240 | continue; |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 241 | std::vector<SUnit*> &UseList = Uses[*Alias]; |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 242 | for (unsigned i = 0, e = UseList.size(); i != e; ++i) { |
| 243 | SUnit *UseSU = UseList[i]; |
| 244 | if (UseSU == SU) |
| 245 | continue; |
| 246 | unsigned LDataLatency = DataLatency; |
| 247 | // Optionally add in a special extra latency for nodes that |
| 248 | // feed addresses. |
| 249 | // TODO: Perhaps we should get rid of |
| 250 | // SpecialAddressLatency and just move this into |
| 251 | // adjustSchedDependency for the targets that care about it. |
| 252 | if (SpecialAddressLatency != 0 && !UnitLatencies && |
| 253 | UseSU != &ExitSU) { |
| 254 | MachineInstr *UseMI = UseSU->getInstr(); |
| 255 | const MCInstrDesc &UseMCID = UseMI->getDesc(); |
| 256 | int RegUseIndex = UseMI->findRegisterUseOperandIdx(*Alias); |
| 257 | assert(RegUseIndex >= 0 && "UseMI doesn't use register!"); |
| 258 | if (RegUseIndex >= 0 && |
| 259 | (UseMI->mayLoad() || UseMI->mayStore()) && |
| 260 | (unsigned)RegUseIndex < UseMCID.getNumOperands() && |
| 261 | UseMCID.OpInfo[RegUseIndex].isLookupPtrRegClass()) |
| 262 | LDataLatency += SpecialAddressLatency; |
| 263 | } |
| 264 | // Adjust the dependence latency using operand def/use |
| 265 | // information (if any), and then allow the target to |
| 266 | // perform its own adjustments. |
| 267 | const SDep& dep = SDep(SU, SDep::Data, LDataLatency, *Alias); |
| 268 | if (!UnitLatencies) { |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 269 | computeOperandLatency(SU, UseSU, const_cast<SDep &>(dep)); |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 270 | ST.adjustSchedDependency(SU, UseSU, const_cast<SDep &>(dep)); |
| 271 | } |
| 272 | UseSU->addPred(dep); |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 277 | /// addPhysRegDeps - Add register dependencies (data, anti, and output) from |
| 278 | /// this SUnit to following instructions in the same scheduling region that |
| 279 | /// depend the physical register referenced at OperIdx. |
| 280 | void ScheduleDAGInstrs::addPhysRegDeps(SUnit *SU, unsigned OperIdx) { |
| 281 | const MachineInstr *MI = SU->getInstr(); |
| 282 | const MachineOperand &MO = MI->getOperand(OperIdx); |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 283 | |
| 284 | // Optionally add output and anti dependencies. For anti |
| 285 | // dependencies we use a latency of 0 because for a multi-issue |
| 286 | // target we want to allow the defining instruction to issue |
| 287 | // in the same cycle as the using instruction. |
| 288 | // TODO: Using a latency of 1 here for output dependencies assumes |
| 289 | // there's no cost for reusing registers. |
| 290 | SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output; |
Craig Topper | e4fd907 | 2012-03-04 10:43:23 +0000 | [diff] [blame] | 291 | for (const uint16_t *Alias = TRI->getOverlaps(MO.getReg()); *Alias; ++Alias) { |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 292 | if (!Defs.contains(*Alias)) |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 293 | continue; |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 294 | std::vector<SUnit *> &DefList = Defs[*Alias]; |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 295 | for (unsigned i = 0, e = DefList.size(); i != e; ++i) { |
| 296 | SUnit *DefSU = DefList[i]; |
| 297 | if (DefSU == &ExitSU) |
| 298 | continue; |
| 299 | if (DefSU != SU && |
| 300 | (Kind != SDep::Output || !MO.isDead() || |
| 301 | !DefSU->getInstr()->registerDefIsDead(*Alias))) { |
| 302 | if (Kind == SDep::Anti) |
| 303 | DefSU->addPred(SDep(SU, Kind, 0, /*Reg=*/*Alias)); |
| 304 | else { |
| 305 | unsigned AOLat = TII->getOutputLatency(InstrItins, MI, OperIdx, |
| 306 | DefSU->getInstr()); |
| 307 | DefSU->addPred(SDep(SU, Kind, AOLat, /*Reg=*/*Alias)); |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 313 | if (!MO.isDef()) { |
| 314 | // Either insert a new Reg2SUnits entry with an empty SUnits list, or |
| 315 | // retrieve the existing SUnits list for this register's uses. |
| 316 | // Push this SUnit on the use list. |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 317 | Uses[MO.getReg()].push_back(SU); |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 318 | } |
| 319 | else { |
| 320 | addPhysRegDataDeps(SU, MO); |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 321 | |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 322 | // Either insert a new Reg2SUnits entry with an empty SUnits list, or |
| 323 | // retrieve the existing SUnits list for this register's defs. |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 324 | std::vector<SUnit *> &DefList = Defs[MO.getReg()]; |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 325 | |
| 326 | // If a def is going to wrap back around to the top of the loop, |
| 327 | // backschedule it. |
| 328 | if (!UnitLatencies && DefList.empty()) { |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 329 | LoopDependencies::LoopDeps::iterator I = LoopRegs.Deps.find(MO.getReg()); |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 330 | if (I != LoopRegs.Deps.end()) { |
| 331 | const MachineOperand *UseMO = I->second.first; |
| 332 | unsigned Count = I->second.second; |
| 333 | const MachineInstr *UseMI = UseMO->getParent(); |
| 334 | unsigned UseMOIdx = UseMO - &UseMI->getOperand(0); |
| 335 | const MCInstrDesc &UseMCID = UseMI->getDesc(); |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 336 | const TargetSubtargetInfo &ST = |
| 337 | TM.getSubtarget<TargetSubtargetInfo>(); |
| 338 | unsigned SpecialAddressLatency = ST.getSpecialAddressLatency(); |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 339 | // TODO: If we knew the total depth of the region here, we could |
| 340 | // handle the case where the whole loop is inside the region but |
| 341 | // is large enough that the isScheduleHigh trick isn't needed. |
| 342 | if (UseMOIdx < UseMCID.getNumOperands()) { |
| 343 | // Currently, we only support scheduling regions consisting of |
| 344 | // single basic blocks. Check to see if the instruction is in |
| 345 | // the same region by checking to see if it has the same parent. |
| 346 | if (UseMI->getParent() != MI->getParent()) { |
| 347 | unsigned Latency = SU->Latency; |
| 348 | if (UseMCID.OpInfo[UseMOIdx].isLookupPtrRegClass()) |
| 349 | Latency += SpecialAddressLatency; |
| 350 | // This is a wild guess as to the portion of the latency which |
| 351 | // will be overlapped by work done outside the current |
| 352 | // scheduling region. |
| 353 | Latency -= std::min(Latency, Count); |
| 354 | // Add the artificial edge. |
| 355 | ExitSU.addPred(SDep(SU, SDep::Order, Latency, |
| 356 | /*Reg=*/0, /*isNormalMemory=*/false, |
| 357 | /*isMustAlias=*/false, |
| 358 | /*isArtificial=*/true)); |
| 359 | } else if (SpecialAddressLatency > 0 && |
| 360 | UseMCID.OpInfo[UseMOIdx].isLookupPtrRegClass()) { |
| 361 | // The entire loop body is within the current scheduling region |
| 362 | // and the latency of this operation is assumed to be greater |
| 363 | // than the latency of the loop. |
| 364 | // TODO: Recursively mark data-edge predecessors as |
| 365 | // isScheduleHigh too. |
| 366 | SU->isScheduleHigh = true; |
| 367 | } |
| 368 | } |
| 369 | LoopRegs.Deps.erase(I); |
| 370 | } |
| 371 | } |
| 372 | |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 373 | // clear this register's use list |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 374 | if (Uses.contains(MO.getReg())) |
| 375 | Uses[MO.getReg()].clear(); |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 376 | |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 377 | if (!MO.isDead()) |
| 378 | DefList.clear(); |
| 379 | |
| 380 | // Calls will not be reordered because of chain dependencies (see |
| 381 | // below). Since call operands are dead, calls may continue to be added |
| 382 | // to the DefList making dependence checking quadratic in the size of |
| 383 | // the block. Instead, we leave only one call at the back of the |
| 384 | // DefList. |
| 385 | if (SU->isCall) { |
| 386 | while (!DefList.empty() && DefList.back()->isCall) |
| 387 | DefList.pop_back(); |
| 388 | } |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 389 | // Defs are pushed in the order they are visited and never reordered. |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 390 | DefList.push_back(SU); |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 394 | /// addVRegDefDeps - Add register output and data dependencies from this SUnit |
| 395 | /// to instructions that occur later in the same scheduling region if they read |
| 396 | /// from or write to the virtual register defined at OperIdx. |
| 397 | /// |
| 398 | /// TODO: Hoist loop induction variable increments. This has to be |
| 399 | /// reevaluated. Generally, IV scheduling should be done before coalescing. |
| 400 | void ScheduleDAGInstrs::addVRegDefDeps(SUnit *SU, unsigned OperIdx) { |
| 401 | const MachineInstr *MI = SU->getInstr(); |
| 402 | unsigned Reg = MI->getOperand(OperIdx).getReg(); |
| 403 | |
Andrew Trick | cc77b54 | 2012-02-22 06:08:13 +0000 | [diff] [blame] | 404 | // SSA defs do not have output/anti dependencies. |
Andrew Trick | 2fc0977 | 2012-02-22 18:34:49 +0000 | [diff] [blame] | 405 | // The current operand is a def, so we have at least one. |
Andrew Trick | cc77b54 | 2012-02-22 06:08:13 +0000 | [diff] [blame] | 406 | if (llvm::next(MRI.def_begin(Reg)) == MRI.def_end()) |
| 407 | return; |
| 408 | |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 409 | // Add output dependence to the next nearest def of this vreg. |
| 410 | // |
| 411 | // Unless this definition is dead, the output dependence should be |
| 412 | // transitively redundant with antidependencies from this definition's |
| 413 | // uses. We're conservative for now until we have a way to guarantee the uses |
| 414 | // are not eliminated sometime during scheduling. The output dependence edge |
| 415 | // is also useful if output latency exceeds def-use latency. |
Andrew Trick | c0ccb8b | 2012-04-20 20:05:28 +0000 | [diff] [blame] | 416 | VReg2SUnitMap::iterator DefI = VRegDefs.find(Reg); |
Andrew Trick | 8ae3ac7 | 2012-02-22 21:59:00 +0000 | [diff] [blame] | 417 | if (DefI == VRegDefs.end()) |
| 418 | VRegDefs.insert(VReg2SUnit(Reg, SU)); |
| 419 | else { |
| 420 | SUnit *DefSU = DefI->SU; |
| 421 | if (DefSU != SU && DefSU != &ExitSU) { |
| 422 | unsigned OutLatency = TII->getOutputLatency(InstrItins, MI, OperIdx, |
| 423 | DefSU->getInstr()); |
| 424 | DefSU->addPred(SDep(SU, SDep::Output, OutLatency, Reg)); |
| 425 | } |
| 426 | DefI->SU = SU; |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 427 | } |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 430 | /// addVRegUseDeps - Add a register data dependency if the instruction that |
| 431 | /// defines the virtual register used at OperIdx is mapped to an SUnit. Add a |
| 432 | /// register antidependency from this SUnit to instructions that occur later in |
| 433 | /// the same scheduling region if they write the virtual register. |
| 434 | /// |
| 435 | /// TODO: Handle ExitSU "uses" properly. |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 436 | void ScheduleDAGInstrs::addVRegUseDeps(SUnit *SU, unsigned OperIdx) { |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 437 | MachineInstr *MI = SU->getInstr(); |
| 438 | unsigned Reg = MI->getOperand(OperIdx).getReg(); |
| 439 | |
| 440 | // Lookup this operand's reaching definition. |
| 441 | assert(LIS && "vreg dependencies requires LiveIntervals"); |
Andrew Trick | 63d578b | 2012-02-23 03:16:24 +0000 | [diff] [blame] | 442 | SlotIndex UseIdx = LIS->getInstructionIndex(MI).getRegSlot(); |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 443 | LiveInterval *LI = &LIS->getInterval(Reg); |
Andrew Trick | 63d578b | 2012-02-23 03:16:24 +0000 | [diff] [blame] | 444 | VNInfo *VNI = LI->getVNInfoBefore(UseIdx); |
| 445 | // VNI will be valid because MachineOperand::readsReg() is checked by caller. |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 446 | MachineInstr *Def = LIS->getInstructionFromIndex(VNI->def); |
Andrew Trick | 63d578b | 2012-02-23 03:16:24 +0000 | [diff] [blame] | 447 | // Phis and other noninstructions (after coalescing) have a NULL Def. |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 448 | if (Def) { |
| 449 | SUnit *DefSU = getSUnit(Def); |
| 450 | if (DefSU) { |
| 451 | // The reaching Def lives within this scheduling region. |
| 452 | // Create a data dependence. |
| 453 | // |
| 454 | // TODO: Handle "special" address latencies cleanly. |
| 455 | const SDep &dep = SDep(DefSU, SDep::Data, DefSU->Latency, Reg); |
| 456 | if (!UnitLatencies) { |
| 457 | // Adjust the dependence latency using operand def/use information, then |
| 458 | // allow the target to perform its own adjustments. |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 459 | computeOperandLatency(DefSU, SU, const_cast<SDep &>(dep)); |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 460 | const TargetSubtargetInfo &ST = TM.getSubtarget<TargetSubtargetInfo>(); |
| 461 | ST.adjustSchedDependency(DefSU, SU, const_cast<SDep &>(dep)); |
| 462 | } |
| 463 | SU->addPred(dep); |
| 464 | } |
| 465 | } |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 466 | |
| 467 | // Add antidependence to the following def of the vreg it uses. |
Andrew Trick | c0ccb8b | 2012-04-20 20:05:28 +0000 | [diff] [blame] | 468 | VReg2SUnitMap::iterator DefI = VRegDefs.find(Reg); |
Andrew Trick | 8ae3ac7 | 2012-02-22 21:59:00 +0000 | [diff] [blame] | 469 | if (DefI != VRegDefs.end() && DefI->SU != SU) |
| 470 | DefI->SU->addPred(SDep(SU, SDep::Anti, 0, Reg)); |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 471 | } |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 472 | |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 473 | /// Create an SUnit for each real instruction, numbered in top-down toplological |
| 474 | /// order. The instruction order A < B, implies that no edge exists from B to A. |
| 475 | /// |
| 476 | /// Map each real instruction to its SUnit. |
| 477 | /// |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 478 | /// After initSUnits, the SUnits vector cannot be resized and the scheduler may |
| 479 | /// hang onto SUnit pointers. We may relax this in the future by using SUnit IDs |
| 480 | /// instead of pointers. |
| 481 | /// |
| 482 | /// MachineScheduler relies on initSUnits numbering the nodes by their order in |
| 483 | /// the original instruction list. |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 484 | void ScheduleDAGInstrs::initSUnits() { |
| 485 | // We'll be allocating one SUnit for each real instruction in the region, |
| 486 | // which is contained within a basic block. |
| 487 | SUnits.reserve(BB->size()); |
| 488 | |
Andrew Trick | 68675c6 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 489 | for (MachineBasicBlock::iterator I = RegionBegin; I != RegionEnd; ++I) { |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 490 | MachineInstr *MI = I; |
| 491 | if (MI->isDebugValue()) |
| 492 | continue; |
| 493 | |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 494 | SUnit *SU = newSUnit(MI); |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 495 | MISUnitMap[MI] = SU; |
| 496 | |
| 497 | SU->isCall = MI->isCall(); |
| 498 | SU->isCommutable = MI->isCommutable(); |
| 499 | |
| 500 | // Assign the Latency field of SU using target-provided information. |
| 501 | if (UnitLatencies) |
| 502 | SU->Latency = 1; |
| 503 | else |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 504 | computeLatency(SU); |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 505 | } |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 508 | /// If RegPressure is non null, compute register pressure as a side effect. The |
| 509 | /// DAG builder is an efficient place to do it because it already visits |
| 510 | /// operands. |
| 511 | void ScheduleDAGInstrs::buildSchedGraph(AliasAnalysis *AA, |
| 512 | RegPressureTracker *RPTracker) { |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 513 | // Create an SUnit for each real instruction. |
| 514 | initSUnits(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 515 | |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 516 | // We build scheduling units by walking a block's instruction list from bottom |
| 517 | // to top. |
| 518 | |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 519 | // Remember where a generic side-effecting instruction is as we procede. |
| 520 | SUnit *BarrierChain = 0, *AliasChain = 0; |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 521 | |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 522 | // Memory references to specific known memory locations are tracked |
| 523 | // so that they can be given more precise dependencies. We track |
| 524 | // separately the known memory locations that may alias and those |
| 525 | // that are known not to alias |
| 526 | std::map<const Value *, SUnit *> AliasMemDefs, NonAliasMemDefs; |
| 527 | std::map<const Value *, std::vector<SUnit *> > AliasMemUses, NonAliasMemUses; |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 528 | |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 529 | // Remove any stale debug info; sometimes BuildSchedGraph is called again |
| 530 | // without emitting the info from the previous call. |
Devang Patel | cf4cc84 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 531 | DbgValues.clear(); |
| 532 | FirstDbgValue = NULL; |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 533 | |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 534 | assert(Defs.empty() && Uses.empty() && |
| 535 | "Only BuildGraph should update Defs/Uses"); |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 536 | Defs.setRegLimit(TRI->getNumRegs()); |
| 537 | Uses.setRegLimit(TRI->getNumRegs()); |
Andrew Trick | 9b66853 | 2011-05-06 21:52:52 +0000 | [diff] [blame] | 538 | |
Andrew Trick | 8ae3ac7 | 2012-02-22 21:59:00 +0000 | [diff] [blame] | 539 | assert(VRegDefs.empty() && "Only BuildSchedGraph may access VRegDefs"); |
| 540 | // FIXME: Allow SparseSet to reserve space for the creation of virtual |
| 541 | // registers during scheduling. Don't artificially inflate the Universe |
| 542 | // because we want to assert that vregs are not created during DAG building. |
| 543 | VRegDefs.setUniverse(MRI.getNumVirtRegs()); |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 544 | |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 545 | // Model data dependencies between instructions being scheduled and the |
| 546 | // ExitSU. |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 547 | addSchedBarrierDeps(); |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 548 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 549 | // Walk the list of instructions, from bottom moving up. |
Devang Patel | cf4cc84 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 550 | MachineInstr *PrevMI = NULL; |
Andrew Trick | 68675c6 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 551 | for (MachineBasicBlock::iterator MII = RegionEnd, MIE = RegionBegin; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 552 | MII != MIE; --MII) { |
| 553 | MachineInstr *MI = prior(MII); |
Devang Patel | cf4cc84 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 554 | if (MI && PrevMI) { |
| 555 | DbgValues.push_back(std::make_pair(PrevMI, MI)); |
| 556 | PrevMI = NULL; |
| 557 | } |
| 558 | |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 559 | if (MI->isDebugValue()) { |
Devang Patel | cf4cc84 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 560 | PrevMI = MI; |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 561 | continue; |
| 562 | } |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 563 | if (RPTracker) { |
| 564 | RPTracker->recede(); |
| 565 | assert(RPTracker->getPos() == prior(MII) && "RPTracker can't find MI"); |
| 566 | } |
Devang Patel | cf4cc84 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 567 | |
Andrew Trick | 0070792 | 2012-04-13 23:29:54 +0000 | [diff] [blame] | 568 | assert((!MI->isTerminator() || CanHandleTerminators) && !MI->isLabel() && |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 569 | "Cannot schedule terminators or labels!"); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 570 | |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 571 | SUnit *SU = MISUnitMap[MI]; |
| 572 | assert(SU && "No SUnit mapped to this MI"); |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 573 | |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 574 | // Add register-based dependencies (data, anti, and output). |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 575 | for (unsigned j = 0, n = MI->getNumOperands(); j != n; ++j) { |
| 576 | const MachineOperand &MO = MI->getOperand(j); |
| 577 | if (!MO.isReg()) continue; |
| 578 | unsigned Reg = MO.getReg(); |
| 579 | if (Reg == 0) continue; |
| 580 | |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 581 | if (TRI->isPhysicalRegister(Reg)) |
| 582 | addPhysRegDeps(SU, j); |
| 583 | else { |
| 584 | assert(!IsPostRA && "Virtual register encountered!"); |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 585 | if (MO.isDef()) |
| 586 | addVRegDefDeps(SU, j); |
Andrew Trick | 63d578b | 2012-02-23 03:16:24 +0000 | [diff] [blame] | 587 | else if (MO.readsReg()) // ignore undef operands |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 588 | addVRegUseDeps(SU, j); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 589 | } |
| 590 | } |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 591 | |
| 592 | // Add chain dependencies. |
David Goodwin | 7c9b1ac | 2009-11-02 17:06:28 +0000 | [diff] [blame] | 593 | // Chain dependencies used to enforce memory order should have |
| 594 | // latency of 0 (except for true dependency of Store followed by |
| 595 | // aliased Load... we estimate that with a single cycle of latency |
| 596 | // assuming the hardware will bypass) |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 597 | // Note that isStoreToStackSlot and isLoadFromStackSLot are not usable |
| 598 | // after stack slots are lowered to actual addresses. |
| 599 | // TODO: Use an AliasAnalysis and do real alias-analysis queries, and |
| 600 | // produce more precise dependence information. |
David Goodwin | 7c9b1ac | 2009-11-02 17:06:28 +0000 | [diff] [blame] | 601 | #define STORE_LOAD_LATENCY 1 |
| 602 | unsigned TrueMemOrderLatency = 0; |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 603 | if (MI->isCall() || MI->hasUnmodeledSideEffects() || |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 604 | (MI->hasVolatileMemoryRef() && |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 605 | (!MI->mayLoad() || !MI->isInvariantLoad(AA)))) { |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 606 | // Be conservative with these and add dependencies on all memory |
| 607 | // references, even those that are known to not alias. |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 608 | for (std::map<const Value *, SUnit *>::iterator I = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 609 | NonAliasMemDefs.begin(), E = NonAliasMemDefs.end(); I != E; ++I) { |
David Goodwin | 7c9b1ac | 2009-11-02 17:06:28 +0000 | [diff] [blame] | 610 | I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 611 | } |
| 612 | for (std::map<const Value *, std::vector<SUnit *> >::iterator I = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 613 | NonAliasMemUses.begin(), E = NonAliasMemUses.end(); I != E; ++I) { |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 614 | for (unsigned i = 0, e = I->second.size(); i != e; ++i) |
David Goodwin | 7c9b1ac | 2009-11-02 17:06:28 +0000 | [diff] [blame] | 615 | I->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 616 | } |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 617 | NonAliasMemDefs.clear(); |
| 618 | NonAliasMemUses.clear(); |
| 619 | // Add SU to the barrier chain. |
| 620 | if (BarrierChain) |
| 621 | BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0)); |
| 622 | BarrierChain = SU; |
| 623 | |
| 624 | // fall-through |
| 625 | new_alias_chain: |
| 626 | // Chain all possibly aliasing memory references though SU. |
| 627 | if (AliasChain) |
| 628 | AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0)); |
| 629 | AliasChain = SU; |
| 630 | for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k) |
| 631 | PendingLoads[k]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency)); |
| 632 | for (std::map<const Value *, SUnit *>::iterator I = AliasMemDefs.begin(), |
| 633 | E = AliasMemDefs.end(); I != E; ++I) { |
| 634 | I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0)); |
| 635 | } |
| 636 | for (std::map<const Value *, std::vector<SUnit *> >::iterator I = |
| 637 | AliasMemUses.begin(), E = AliasMemUses.end(); I != E; ++I) { |
| 638 | for (unsigned i = 0, e = I->second.size(); i != e; ++i) |
| 639 | I->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency)); |
| 640 | } |
| 641 | PendingLoads.clear(); |
| 642 | AliasMemDefs.clear(); |
| 643 | AliasMemUses.clear(); |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 644 | } else if (MI->mayStore()) { |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 645 | bool MayAlias = true; |
David Goodwin | 7c9b1ac | 2009-11-02 17:06:28 +0000 | [diff] [blame] | 646 | TrueMemOrderLatency = STORE_LOAD_LATENCY; |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 647 | if (const Value *V = getUnderlyingObjectForInstr(MI, MFI, MayAlias)) { |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 648 | // A store to a specific PseudoSourceValue. Add precise dependencies. |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 649 | // Record the def in MemDefs, first adding a dep if there is |
| 650 | // an existing def. |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 651 | std::map<const Value *, SUnit *>::iterator I = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 652 | ((MayAlias) ? AliasMemDefs.find(V) : NonAliasMemDefs.find(V)); |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 653 | std::map<const Value *, SUnit *>::iterator IE = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 654 | ((MayAlias) ? AliasMemDefs.end() : NonAliasMemDefs.end()); |
| 655 | if (I != IE) { |
David Goodwin | 7c9b1ac | 2009-11-02 17:06:28 +0000 | [diff] [blame] | 656 | I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0, /*Reg=*/0, |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 657 | /*isNormalMemory=*/true)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 658 | I->second = SU; |
| 659 | } else { |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 660 | if (MayAlias) |
| 661 | AliasMemDefs[V] = SU; |
| 662 | else |
| 663 | NonAliasMemDefs[V] = SU; |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 664 | } |
| 665 | // Handle the uses in MemUses, if there are any. |
Dan Gohman | a629b48 | 2008-12-08 17:50:35 +0000 | [diff] [blame] | 666 | std::map<const Value *, std::vector<SUnit *> >::iterator J = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 667 | ((MayAlias) ? AliasMemUses.find(V) : NonAliasMemUses.find(V)); |
| 668 | std::map<const Value *, std::vector<SUnit *> >::iterator JE = |
| 669 | ((MayAlias) ? AliasMemUses.end() : NonAliasMemUses.end()); |
| 670 | if (J != JE) { |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 671 | for (unsigned i = 0, e = J->second.size(); i != e; ++i) |
David Goodwin | 7c9b1ac | 2009-11-02 17:06:28 +0000 | [diff] [blame] | 672 | J->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency, |
| 673 | /*Reg=*/0, /*isNormalMemory=*/true)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 674 | J->second.clear(); |
| 675 | } |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 676 | if (MayAlias) { |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 677 | // Add dependencies from all the PendingLoads, i.e. loads |
| 678 | // with no underlying object. |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 679 | for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k) |
| 680 | PendingLoads[k]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency)); |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 681 | // Add dependence on alias chain, if needed. |
| 682 | if (AliasChain) |
| 683 | AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0)); |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 684 | } |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 685 | // Add dependence on barrier chain, if needed. |
| 686 | if (BarrierChain) |
| 687 | BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0)); |
David Goodwin | 5be870a | 2009-11-05 00:16:44 +0000 | [diff] [blame] | 688 | } else { |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 689 | // Treat all other stores conservatively. |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 690 | goto new_alias_chain; |
David Goodwin | 7c9b1ac | 2009-11-02 17:06:28 +0000 | [diff] [blame] | 691 | } |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 692 | |
| 693 | if (!ExitSU.isPred(SU)) |
| 694 | // Push store's up a bit to avoid them getting in between cmp |
| 695 | // and branches. |
| 696 | ExitSU.addPred(SDep(SU, SDep::Order, 0, |
| 697 | /*Reg=*/0, /*isNormalMemory=*/false, |
| 698 | /*isMustAlias=*/false, |
| 699 | /*isArtificial=*/true)); |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 700 | } else if (MI->mayLoad()) { |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 701 | bool MayAlias = true; |
David Goodwin | 7c9b1ac | 2009-11-02 17:06:28 +0000 | [diff] [blame] | 702 | TrueMemOrderLatency = 0; |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 703 | if (MI->isInvariantLoad(AA)) { |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 704 | // Invariant load, no chain dependencies needed! |
David Goodwin | 5be870a | 2009-11-05 00:16:44 +0000 | [diff] [blame] | 705 | } else { |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 706 | if (const Value *V = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 707 | getUnderlyingObjectForInstr(MI, MFI, MayAlias)) { |
| 708 | // A load from a specific PseudoSourceValue. Add precise dependencies. |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 709 | std::map<const Value *, SUnit *>::iterator I = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 710 | ((MayAlias) ? AliasMemDefs.find(V) : NonAliasMemDefs.find(V)); |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 711 | std::map<const Value *, SUnit *>::iterator IE = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 712 | ((MayAlias) ? AliasMemDefs.end() : NonAliasMemDefs.end()); |
| 713 | if (I != IE) |
| 714 | I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0, /*Reg=*/0, |
| 715 | /*isNormalMemory=*/true)); |
| 716 | if (MayAlias) |
| 717 | AliasMemUses[V].push_back(SU); |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 718 | else |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 719 | NonAliasMemUses[V].push_back(SU); |
| 720 | } else { |
| 721 | // A load with no underlying object. Depend on all |
| 722 | // potentially aliasing stores. |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 723 | for (std::map<const Value *, SUnit *>::iterator I = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 724 | AliasMemDefs.begin(), E = AliasMemDefs.end(); I != E; ++I) |
| 725 | I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0)); |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 726 | |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 727 | PendingLoads.push_back(SU); |
| 728 | MayAlias = true; |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 729 | } |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 730 | |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 731 | // Add dependencies on alias and barrier chains, if needed. |
| 732 | if (MayAlias && AliasChain) |
| 733 | AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0)); |
| 734 | if (BarrierChain) |
| 735 | BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0)); |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 736 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 737 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 738 | } |
Devang Patel | cf4cc84 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 739 | if (PrevMI) |
| 740 | FirstDbgValue = PrevMI; |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 741 | |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 742 | Defs.clear(); |
| 743 | Uses.clear(); |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 744 | VRegDefs.clear(); |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 745 | PendingLoads.clear(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 748 | void ScheduleDAGInstrs::computeLatency(SUnit *SU) { |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 749 | // Compute the latency for the node. |
Evan Cheng | 3ef1c87 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 750 | if (!InstrItins || InstrItins->isEmpty()) { |
| 751 | SU->Latency = 1; |
Dan Gohman | 4ea8e85 | 2008-12-16 02:38:22 +0000 | [diff] [blame] | 752 | |
Evan Cheng | 3ef1c87 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 753 | // Simplistic target-independent heuristic: assume that loads take |
| 754 | // extra time. |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 755 | if (SU->getInstr()->mayLoad()) |
Dan Gohman | 4ea8e85 | 2008-12-16 02:38:22 +0000 | [diff] [blame] | 756 | SU->Latency += 2; |
Evan Cheng | 8239daf | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 757 | } else { |
| 758 | SU->Latency = TII->getInstrLatency(InstrItins, SU->getInstr()); |
| 759 | } |
Dan Gohman | c8c2827 | 2008-11-21 00:12:10 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 762 | void ScheduleDAGInstrs::computeOperandLatency(SUnit *Def, SUnit *Use, |
David Goodwin | dc4bdcd | 2009-08-19 16:08:58 +0000 | [diff] [blame] | 763 | SDep& dep) const { |
Evan Cheng | 3ef1c87 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 764 | if (!InstrItins || InstrItins->isEmpty()) |
David Goodwin | dc4bdcd | 2009-08-19 16:08:58 +0000 | [diff] [blame] | 765 | return; |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 766 | |
David Goodwin | dc4bdcd | 2009-08-19 16:08:58 +0000 | [diff] [blame] | 767 | // For a data dependency with a known register... |
| 768 | if ((dep.getKind() != SDep::Data) || (dep.getReg() == 0)) |
| 769 | return; |
| 770 | |
| 771 | const unsigned Reg = dep.getReg(); |
| 772 | |
| 773 | // ... find the definition of the register in the defining |
| 774 | // instruction |
| 775 | MachineInstr *DefMI = Def->getInstr(); |
| 776 | int DefIdx = DefMI->findRegisterDefOperandIdx(Reg); |
| 777 | if (DefIdx != -1) { |
Evan Cheng | 1aca5bc | 2010-10-08 18:42:25 +0000 | [diff] [blame] | 778 | const MachineOperand &MO = DefMI->getOperand(DefIdx); |
| 779 | if (MO.isReg() && MO.isImplicit() && |
Evan Cheng | d82de83 | 2010-10-08 23:01:57 +0000 | [diff] [blame] | 780 | DefIdx >= (int)DefMI->getDesc().getNumOperands()) { |
Evan Cheng | 1aca5bc | 2010-10-08 18:42:25 +0000 | [diff] [blame] | 781 | // This is an implicit def, getOperandLatency() won't return the correct |
| 782 | // latency. e.g. |
| 783 | // %D6<def>, %D7<def> = VLD1q16 %R2<kill>, 0, ..., %Q3<imp-def> |
| 784 | // %Q1<def> = VMULv8i16 %Q1<kill>, %Q3<kill>, ... |
| 785 | // What we want is to compute latency between def of %D6/%D7 and use of |
| 786 | // %Q3 instead. |
Jakob Stoklund Olesen | 02634be | 2012-02-22 22:52:52 +0000 | [diff] [blame] | 787 | unsigned Op2 = DefMI->findRegisterDefOperandIdx(Reg, false, true, TRI); |
| 788 | if (DefMI->getOperand(Op2).isReg()) |
| 789 | DefIdx = Op2; |
Evan Cheng | 1aca5bc | 2010-10-08 18:42:25 +0000 | [diff] [blame] | 790 | } |
Evan Cheng | a0792de | 2010-10-06 06:27:31 +0000 | [diff] [blame] | 791 | MachineInstr *UseMI = Use->getInstr(); |
Evan Cheng | 3881cb7 | 2010-09-29 22:42:35 +0000 | [diff] [blame] | 792 | // For all uses of the register, calculate the maxmimum latency |
| 793 | int Latency = -1; |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 794 | if (UseMI) { |
| 795 | for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) { |
| 796 | const MachineOperand &MO = UseMI->getOperand(i); |
| 797 | if (!MO.isReg() || !MO.isUse()) |
| 798 | continue; |
| 799 | unsigned MOReg = MO.getReg(); |
| 800 | if (MOReg != Reg) |
| 801 | continue; |
David Goodwin | dc4bdcd | 2009-08-19 16:08:58 +0000 | [diff] [blame] | 802 | |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 803 | int UseCycle = TII->getOperandLatency(InstrItins, DefMI, DefIdx, |
| 804 | UseMI, i); |
| 805 | Latency = std::max(Latency, UseCycle); |
| 806 | } |
| 807 | } else { |
| 808 | // UseMI is null, then it must be a scheduling barrier. |
| 809 | if (!InstrItins || InstrItins->isEmpty()) |
| 810 | return; |
| 811 | unsigned DefClass = DefMI->getDesc().getSchedClass(); |
| 812 | Latency = InstrItins->getOperandCycle(DefClass, DefIdx); |
David Goodwin | dc4bdcd | 2009-08-19 16:08:58 +0000 | [diff] [blame] | 813 | } |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 814 | |
| 815 | // If we found a latency, then replace the existing dependence latency. |
| 816 | if (Latency >= 0) |
| 817 | dep.setLatency(Latency); |
David Goodwin | dc4bdcd | 2009-08-19 16:08:58 +0000 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 821 | void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const { |
| 822 | SU->getInstr()->dump(); |
| 823 | } |
| 824 | |
| 825 | std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const { |
| 826 | std::string s; |
| 827 | raw_string_ostream oss(s); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 828 | if (SU == &EntrySU) |
| 829 | oss << "<entry>"; |
| 830 | else if (SU == &ExitSU) |
| 831 | oss << "<exit>"; |
| 832 | else |
| 833 | SU->getInstr()->print(oss); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 834 | return oss.str(); |
| 835 | } |
| 836 | |
Andrew Trick | 56b94c5 | 2012-03-07 00:18:22 +0000 | [diff] [blame] | 837 | /// Return the basic block label. It is not necessarilly unique because a block |
| 838 | /// contains multiple scheduling regions. But it is fine for visualization. |
| 839 | std::string ScheduleDAGInstrs::getDAGName() const { |
| 840 | return "dag." + BB->getFullName(); |
| 841 | } |