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 | |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 15 | #define DEBUG_TYPE "misched" |
Dan Gohman | 8906f95 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 16 | #include "llvm/Operator.h" |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/AliasAnalysis.h" |
Dan Gohman | 5034dd3 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/ValueTracking.h" |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/LiveIntervalAnalysis.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" |
Andrew Trick | afc2657 | 2012-06-06 19:47:35 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/RegisterPressure.h" |
Andrew Trick | 53e98a2 | 2012-11-28 05:13:24 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/ScheduleDFS.h" |
Andrew Trick | ed395c8 | 2012-03-07 23:01:06 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/ScheduleDAGInstrs.h" |
Evan Cheng | ab8be96 | 2011-06-29 01:14:12 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCInstrItineraries.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetMachine.h" |
| 29 | #include "llvm/Target/TargetInstrInfo.h" |
| 30 | #include "llvm/Target/TargetRegisterInfo.h" |
Evan Cheng | 5b1b4489 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetSubtargetInfo.h" |
Andrew Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CommandLine.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Debug.h" |
Andrew Trick | 1e94e98 | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Format.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 35 | #include "llvm/Support/raw_ostream.h" |
Sergei Larin | 009cf9e | 2012-11-15 17:45:50 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/MapVector.h" |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/SmallSet.h" |
Andrew Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/SmallPtrSet.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 39 | using namespace llvm; |
| 40 | |
Andrew Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 41 | static cl::opt<bool> EnableAASchedMI("enable-aa-sched-mi", cl::Hidden, |
| 42 | cl::ZeroOrMore, cl::init(false), |
| 43 | cl::desc("Enable use of AA during MI GAD construction")); |
| 44 | |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 45 | ScheduleDAGInstrs::ScheduleDAGInstrs(MachineFunction &mf, |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 46 | const MachineLoopInfo &mli, |
Andrew Trick | 5e920d7 | 2012-01-14 02:17:12 +0000 | [diff] [blame] | 47 | const MachineDominatorTree &mdt, |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 48 | bool IsPostRAFlag, |
| 49 | LiveIntervals *lis) |
Andrew Trick | 412cd2f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 50 | : ScheduleDAG(mf), MLI(mli), MDT(mdt), MFI(mf.getFrameInfo()), LIS(lis), |
Andrew Trick | 714973e | 2012-10-09 23:44:23 +0000 | [diff] [blame] | 51 | IsPostRA(IsPostRAFlag), CanHandleTerminators(false), FirstDbgValue(0) { |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 52 | assert((IsPostRA || LIS) && "PreRA scheduling requires LiveIntervals"); |
Devang Patel | cf4cc84 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 53 | DbgValues.clear(); |
Andrew Trick | cc77b54 | 2012-02-22 06:08:13 +0000 | [diff] [blame] | 54 | assert(!(IsPostRA && MRI.getNumVirtRegs()) && |
Andrew Trick | 19273ae | 2012-02-21 04:51:23 +0000 | [diff] [blame] | 55 | "Virtual registers must be removed prior to PostRA scheduling"); |
Andrew Trick | 781ab47 | 2012-09-18 18:20:00 +0000 | [diff] [blame] | 56 | |
| 57 | const TargetSubtargetInfo &ST = TM.getSubtarget<TargetSubtargetInfo>(); |
| 58 | SchedModel.init(*ST.getSchedModel(), &ST, TII); |
Evan Cheng | 38bdfc6 | 2009-10-18 19:58:47 +0000 | [diff] [blame] | 59 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 60 | |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 61 | /// getUnderlyingObjectFromInt - This is the function that does the work of |
| 62 | /// looking through basic ptrtoint+arithmetic+inttoptr sequences. |
| 63 | static const Value *getUnderlyingObjectFromInt(const Value *V) { |
| 64 | do { |
Dan Gohman | 8906f95 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 65 | if (const Operator *U = dyn_cast<Operator>(V)) { |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 66 | // If we find a ptrtoint, we can transfer control back to the |
| 67 | // regular getUnderlyingObjectFromInt. |
Dan Gohman | 8906f95 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 68 | if (U->getOpcode() == Instruction::PtrToInt) |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 69 | return U->getOperand(0); |
Andrew Trick | 8f82a08 | 2012-11-28 03:42:49 +0000 | [diff] [blame] | 70 | // If we find an add of a constant, a multiplied value, or a phi, it's |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 71 | // likely that the other operand will lead us to the base |
| 72 | // object. We don't have to worry about the case where the |
Dan Gohman | 748f98f | 2009-08-07 01:26:06 +0000 | [diff] [blame] | 73 | // object address is somehow being computed by the multiply, |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 74 | // because our callers only care when the result is an |
Nick Lewycky | 6b0db5f | 2012-10-26 04:27:49 +0000 | [diff] [blame] | 75 | // identifiable object. |
Dan Gohman | 8906f95 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 76 | if (U->getOpcode() != Instruction::Add || |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 77 | (!isa<ConstantInt>(U->getOperand(1)) && |
Andrew Trick | 8f82a08 | 2012-11-28 03:42:49 +0000 | [diff] [blame] | 78 | Operator::getOpcode(U->getOperand(1)) != Instruction::Mul && |
| 79 | !isa<PHINode>(U->getOperand(1)))) |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 80 | return V; |
| 81 | V = U->getOperand(0); |
| 82 | } else { |
| 83 | return V; |
| 84 | } |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 85 | assert(V->getType()->isIntegerTy() && "Unexpected operand type!"); |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 86 | } while (1); |
| 87 | } |
| 88 | |
Dan Gohman | 5034dd3 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 89 | /// getUnderlyingObject - This is a wrapper around GetUnderlyingObject |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 90 | /// and adds support for basic ptrtoint+arithmetic+inttoptr sequences. |
| 91 | static const Value *getUnderlyingObject(const Value *V) { |
| 92 | // First just call Value::getUnderlyingObject to let it do what it does. |
| 93 | do { |
Dan Gohman | 5034dd3 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 94 | V = GetUnderlyingObject(V); |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 95 | // If it found an inttoptr, use special code to continue climing. |
Dan Gohman | 8906f95 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 96 | if (Operator::getOpcode(V) != Instruction::IntToPtr) |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 97 | break; |
| 98 | const Value *O = getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0)); |
| 99 | // If that succeeded in finding a pointer, continue the search. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 100 | if (!O->getType()->isPointerTy()) |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 101 | break; |
| 102 | V = O; |
| 103 | } while (1); |
| 104 | return V; |
| 105 | } |
| 106 | |
| 107 | /// getUnderlyingObjectForInstr - If this machine instr has memory reference |
| 108 | /// information and it can be tracked to a normal reference to a known |
| 109 | /// object, return the Value for that object. Otherwise return null. |
Evan Cheng | 38bdfc6 | 2009-10-18 19:58:47 +0000 | [diff] [blame] | 110 | static const Value *getUnderlyingObjectForInstr(const MachineInstr *MI, |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 111 | const MachineFrameInfo *MFI, |
| 112 | bool &MayAlias) { |
| 113 | MayAlias = true; |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 114 | if (!MI->hasOneMemOperand() || |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 115 | !(*MI->memoperands_begin())->getValue() || |
| 116 | (*MI->memoperands_begin())->isVolatile()) |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 117 | return 0; |
| 118 | |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 119 | const Value *V = (*MI->memoperands_begin())->getValue(); |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 120 | if (!V) |
| 121 | return 0; |
| 122 | |
| 123 | V = getUnderlyingObject(V); |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 124 | if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) { |
| 125 | // For now, ignore PseudoSourceValues which may alias LLVM IR values |
| 126 | // because the code that uses this function has no way to cope with |
| 127 | // such aliases. |
Evan Cheng | 38bdfc6 | 2009-10-18 19:58:47 +0000 | [diff] [blame] | 128 | if (PSV->isAliased(MFI)) |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 129 | return 0; |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 130 | |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 131 | MayAlias = PSV->mayAlias(MFI); |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 132 | return V; |
| 133 | } |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 134 | |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 135 | if (isIdentifiedObject(V)) |
| 136 | return V; |
| 137 | |
| 138 | return 0; |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Andrew Trick | 918f38a | 2012-04-20 20:05:21 +0000 | [diff] [blame] | 141 | void ScheduleDAGInstrs::startBlock(MachineBasicBlock *bb) { |
| 142 | BB = bb; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 145 | void ScheduleDAGInstrs::finishBlock() { |
Andrew Trick | a30444a | 2012-04-20 20:24:33 +0000 | [diff] [blame] | 146 | // Subclasses should no longer refer to the old block. |
Andrew Trick | 918f38a | 2012-04-20 20:05:21 +0000 | [diff] [blame] | 147 | BB = 0; |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 150 | /// Initialize the map with the number of registers. |
Andrew Trick | 035ec40 | 2012-03-07 23:00:57 +0000 | [diff] [blame] | 151 | void Reg2SUnitsMap::setRegLimit(unsigned Limit) { |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 152 | PhysRegSet.setUniverse(Limit); |
| 153 | SUnits.resize(Limit); |
| 154 | } |
| 155 | |
| 156 | /// Clear the map without deallocating storage. |
Andrew Trick | 035ec40 | 2012-03-07 23:00:57 +0000 | [diff] [blame] | 157 | void Reg2SUnitsMap::clear() { |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 158 | for (const_iterator I = reg_begin(), E = reg_end(); I != E; ++I) { |
| 159 | SUnits[*I].clear(); |
| 160 | } |
| 161 | PhysRegSet.clear(); |
| 162 | } |
| 163 | |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 164 | /// Initialize the DAG and common scheduler state for the current scheduling |
| 165 | /// region. This does not actually create the DAG, only clears it. The |
| 166 | /// scheduling driver may call BuildSchedGraph multiple times per scheduling |
| 167 | /// region. |
| 168 | void ScheduleDAGInstrs::enterRegion(MachineBasicBlock *bb, |
| 169 | MachineBasicBlock::iterator begin, |
| 170 | MachineBasicBlock::iterator end, |
| 171 | unsigned endcount) { |
Andrew Trick | 918f38a | 2012-04-20 20:05:21 +0000 | [diff] [blame] | 172 | assert(bb == BB && "startBlock should set BB"); |
Andrew Trick | 68675c6 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 173 | RegionBegin = begin; |
| 174 | RegionEnd = end; |
Andrew Trick | cf46b5a | 2012-03-07 23:00:52 +0000 | [diff] [blame] | 175 | EndIndex = endcount; |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 176 | MISUnitMap.clear(); |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 177 | |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 178 | ScheduleDAG::clearDAG(); |
| 179 | } |
| 180 | |
| 181 | /// Close the current scheduling region. Don't clear any state in case the |
| 182 | /// driver wants to refer to the previous scheduling region. |
| 183 | void ScheduleDAGInstrs::exitRegion() { |
| 184 | // Nothing to do. |
| 185 | } |
| 186 | |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 187 | /// addSchedBarrierDeps - Add dependencies from instructions in the current |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 188 | /// list of instructions being scheduled to scheduling barrier by adding |
| 189 | /// the exit SU to the register defs and use list. This is because we want to |
| 190 | /// make sure instructions which define registers that are either used by |
| 191 | /// the terminator or are live-out are properly scheduled. This is |
| 192 | /// especially important when the definition latency of the return value(s) |
| 193 | /// are too high to be hidden by the branch or when the liveout registers |
| 194 | /// used by instructions in the fallthrough block. |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 195 | void ScheduleDAGInstrs::addSchedBarrierDeps() { |
Andrew Trick | 68675c6 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 196 | MachineInstr *ExitMI = RegionEnd != BB->end() ? &*RegionEnd : 0; |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 197 | ExitSU.setInstr(ExitMI); |
| 198 | bool AllDepKnown = ExitMI && |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 199 | (ExitMI->isCall() || ExitMI->isBarrier()); |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 200 | if (ExitMI && AllDepKnown) { |
| 201 | // If it's a call or a barrier, add dependencies on the defs and uses of |
| 202 | // instruction. |
| 203 | for (unsigned i = 0, e = ExitMI->getNumOperands(); i != e; ++i) { |
| 204 | const MachineOperand &MO = ExitMI->getOperand(i); |
| 205 | if (!MO.isReg() || MO.isDef()) continue; |
| 206 | unsigned Reg = MO.getReg(); |
| 207 | if (Reg == 0) continue; |
| 208 | |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 209 | if (TRI->isPhysicalRegister(Reg)) |
Andrew Trick | ffd2526 | 2012-08-23 00:39:43 +0000 | [diff] [blame] | 210 | Uses[Reg].push_back(PhysRegSUOper(&ExitSU, -1)); |
Andrew Trick | d3a7486 | 2012-03-16 05:04:25 +0000 | [diff] [blame] | 211 | else { |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 212 | assert(!IsPostRA && "Virtual register encountered after regalloc."); |
Andrew Trick | 177d87a | 2012-12-01 01:22:44 +0000 | [diff] [blame^] | 213 | if (MO.readsReg()) // ignore undef operands |
| 214 | addVRegUseDeps(&ExitSU, i); |
Andrew Trick | d3a7486 | 2012-03-16 05:04:25 +0000 | [diff] [blame] | 215 | } |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 216 | } |
| 217 | } else { |
| 218 | // For others, e.g. fallthrough, conditional branch, assume the exit |
Evan Cheng | de5fa93 | 2010-10-27 23:17:17 +0000 | [diff] [blame] | 219 | // uses all the registers that are livein to the successor blocks. |
Benjamin Kramer | a82d526 | 2012-03-16 17:38:19 +0000 | [diff] [blame] | 220 | assert(Uses.empty() && "Uses in set before adding deps?"); |
Evan Cheng | de5fa93 | 2010-10-27 23:17:17 +0000 | [diff] [blame] | 221 | for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(), |
| 222 | SE = BB->succ_end(); SI != SE; ++SI) |
| 223 | for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(), |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 224 | E = (*SI)->livein_end(); I != E; ++I) { |
Evan Cheng | de5fa93 | 2010-10-27 23:17:17 +0000 | [diff] [blame] | 225 | unsigned Reg = *I; |
Benjamin Kramer | a82d526 | 2012-03-16 17:38:19 +0000 | [diff] [blame] | 226 | if (!Uses.contains(Reg)) |
Andrew Trick | ffd2526 | 2012-08-23 00:39:43 +0000 | [diff] [blame] | 227 | Uses[Reg].push_back(PhysRegSUOper(&ExitSU, -1)); |
Evan Cheng | de5fa93 | 2010-10-27 23:17:17 +0000 | [diff] [blame] | 228 | } |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 232 | /// MO is an operand of SU's instruction that defines a physical register. Add |
| 233 | /// data dependencies from SU to any uses of the physical register. |
Andrew Trick | ffd2526 | 2012-08-23 00:39:43 +0000 | [diff] [blame] | 234 | void ScheduleDAGInstrs::addPhysRegDataDeps(SUnit *SU, unsigned OperIdx) { |
| 235 | const MachineOperand &MO = SU->getInstr()->getOperand(OperIdx); |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 236 | assert(MO.isDef() && "expect physreg def"); |
| 237 | |
| 238 | // Ask the target if address-backscheduling is desirable, and if so how much. |
| 239 | const TargetSubtargetInfo &ST = TM.getSubtarget<TargetSubtargetInfo>(); |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 240 | |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 241 | for (MCRegAliasIterator Alias(MO.getReg(), TRI, true); |
| 242 | Alias.isValid(); ++Alias) { |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 243 | if (!Uses.contains(*Alias)) |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 244 | continue; |
Andrew Trick | ffd2526 | 2012-08-23 00:39:43 +0000 | [diff] [blame] | 245 | std::vector<PhysRegSUOper> &UseList = Uses[*Alias]; |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 246 | for (unsigned i = 0, e = UseList.size(); i != e; ++i) { |
Andrew Trick | ffd2526 | 2012-08-23 00:39:43 +0000 | [diff] [blame] | 247 | SUnit *UseSU = UseList[i].SU; |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 248 | if (UseSU == SU) |
| 249 | continue; |
Andrew Trick | 39817f9 | 2012-10-08 18:54:00 +0000 | [diff] [blame] | 250 | |
Andrew Trick | 39817f9 | 2012-10-08 18:54:00 +0000 | [diff] [blame] | 251 | // Adjust the dependence latency using operand def/use information, |
| 252 | // then allow the target to perform its own adjustments. |
Andrew Trick | ffd2526 | 2012-08-23 00:39:43 +0000 | [diff] [blame] | 253 | int UseOp = UseList[i].OpIdx; |
Andrew Trick | ae692f2 | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 254 | MachineInstr *RegUse = 0; |
| 255 | SDep Dep; |
| 256 | if (UseOp < 0) |
| 257 | Dep = SDep(SU, SDep::Artificial); |
| 258 | else { |
| 259 | Dep = SDep(SU, SDep::Data, *Alias); |
| 260 | RegUse = UseSU->getInstr(); |
| 261 | Dep.setMinLatency( |
| 262 | SchedModel.computeOperandLatency(SU->getInstr(), OperIdx, |
| 263 | RegUse, UseOp, /*FindMin=*/true)); |
| 264 | } |
| 265 | Dep.setLatency( |
Andrew Trick | a98f600 | 2012-10-08 18:53:57 +0000 | [diff] [blame] | 266 | SchedModel.computeOperandLatency(SU->getInstr(), OperIdx, |
| 267 | RegUse, UseOp, /*FindMin=*/false)); |
Andrew Trick | b7e0289 | 2012-06-05 21:11:27 +0000 | [diff] [blame] | 268 | |
Andrew Trick | ae692f2 | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 269 | ST.adjustSchedDependency(SU, UseSU, Dep); |
| 270 | UseSU->addPred(Dep); |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 275 | /// addPhysRegDeps - Add register dependencies (data, anti, and output) from |
| 276 | /// this SUnit to following instructions in the same scheduling region that |
| 277 | /// depend the physical register referenced at OperIdx. |
| 278 | void ScheduleDAGInstrs::addPhysRegDeps(SUnit *SU, unsigned OperIdx) { |
| 279 | const MachineInstr *MI = SU->getInstr(); |
| 280 | const MachineOperand &MO = MI->getOperand(OperIdx); |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 281 | |
| 282 | // Optionally add output and anti dependencies. For anti |
| 283 | // dependencies we use a latency of 0 because for a multi-issue |
| 284 | // target we want to allow the defining instruction to issue |
| 285 | // in the same cycle as the using instruction. |
| 286 | // TODO: Using a latency of 1 here for output dependencies assumes |
| 287 | // there's no cost for reusing registers. |
| 288 | SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output; |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 289 | for (MCRegAliasIterator Alias(MO.getReg(), TRI, true); |
| 290 | Alias.isValid(); ++Alias) { |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 291 | if (!Defs.contains(*Alias)) |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 292 | continue; |
Andrew Trick | ffd2526 | 2012-08-23 00:39:43 +0000 | [diff] [blame] | 293 | std::vector<PhysRegSUOper> &DefList = Defs[*Alias]; |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 294 | for (unsigned i = 0, e = DefList.size(); i != e; ++i) { |
Andrew Trick | ffd2526 | 2012-08-23 00:39:43 +0000 | [diff] [blame] | 295 | SUnit *DefSU = DefList[i].SU; |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 296 | if (DefSU == &ExitSU) |
| 297 | continue; |
| 298 | if (DefSU != SU && |
| 299 | (Kind != SDep::Output || !MO.isDead() || |
| 300 | !DefSU->getInstr()->registerDefIsDead(*Alias))) { |
| 301 | if (Kind == SDep::Anti) |
Andrew Trick | a78d322 | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 302 | DefSU->addPred(SDep(SU, Kind, /*Reg=*/*Alias)); |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 303 | else { |
Andrew Trick | a78d322 | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 304 | SDep Dep(SU, Kind, /*Reg=*/*Alias); |
| 305 | unsigned OutLatency = |
Andrew Trick | 412cd2f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 306 | SchedModel.computeOutputLatency(MI, OperIdx, DefSU->getInstr()); |
Andrew Trick | a78d322 | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 307 | Dep.setMinLatency(OutLatency); |
| 308 | Dep.setLatency(OutLatency); |
| 309 | DefSU->addPred(Dep); |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 315 | if (!MO.isDef()) { |
| 316 | // Either insert a new Reg2SUnits entry with an empty SUnits list, or |
| 317 | // retrieve the existing SUnits list for this register's uses. |
| 318 | // Push this SUnit on the use list. |
Andrew Trick | ffd2526 | 2012-08-23 00:39:43 +0000 | [diff] [blame] | 319 | Uses[MO.getReg()].push_back(PhysRegSUOper(SU, OperIdx)); |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 320 | } |
| 321 | else { |
Andrew Trick | ffd2526 | 2012-08-23 00:39:43 +0000 | [diff] [blame] | 322 | addPhysRegDataDeps(SU, OperIdx); |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 323 | |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 324 | // Either insert a new Reg2SUnits entry with an empty SUnits list, or |
| 325 | // retrieve the existing SUnits list for this register's defs. |
Andrew Trick | ffd2526 | 2012-08-23 00:39:43 +0000 | [diff] [blame] | 326 | std::vector<PhysRegSUOper> &DefList = Defs[MO.getReg()]; |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 327 | |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 328 | // clear this register's use list |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 329 | if (Uses.contains(MO.getReg())) |
| 330 | Uses[MO.getReg()].clear(); |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 331 | |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 332 | if (!MO.isDead()) |
| 333 | DefList.clear(); |
| 334 | |
| 335 | // Calls will not be reordered because of chain dependencies (see |
| 336 | // below). Since call operands are dead, calls may continue to be added |
| 337 | // to the DefList making dependence checking quadratic in the size of |
| 338 | // the block. Instead, we leave only one call at the back of the |
| 339 | // DefList. |
| 340 | if (SU->isCall) { |
Andrew Trick | ffd2526 | 2012-08-23 00:39:43 +0000 | [diff] [blame] | 341 | while (!DefList.empty() && DefList.back().SU->isCall) |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 342 | DefList.pop_back(); |
| 343 | } |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 344 | // Defs are pushed in the order they are visited and never reordered. |
Andrew Trick | ffd2526 | 2012-08-23 00:39:43 +0000 | [diff] [blame] | 345 | DefList.push_back(PhysRegSUOper(SU, OperIdx)); |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 349 | /// addVRegDefDeps - Add register output and data dependencies from this SUnit |
| 350 | /// to instructions that occur later in the same scheduling region if they read |
| 351 | /// from or write to the virtual register defined at OperIdx. |
| 352 | /// |
| 353 | /// TODO: Hoist loop induction variable increments. This has to be |
| 354 | /// reevaluated. Generally, IV scheduling should be done before coalescing. |
| 355 | void ScheduleDAGInstrs::addVRegDefDeps(SUnit *SU, unsigned OperIdx) { |
| 356 | const MachineInstr *MI = SU->getInstr(); |
| 357 | unsigned Reg = MI->getOperand(OperIdx).getReg(); |
| 358 | |
Andrew Trick | 4b72ada | 2012-07-28 01:48:15 +0000 | [diff] [blame] | 359 | // Singly defined vregs do not have output/anti dependencies. |
Andrew Trick | 2fc0977 | 2012-02-22 18:34:49 +0000 | [diff] [blame] | 360 | // The current operand is a def, so we have at least one. |
Andrew Trick | 4b72ada | 2012-07-28 01:48:15 +0000 | [diff] [blame] | 361 | // Check here if there are any others... |
Andrew Trick | 8b5704f | 2012-07-30 23:48:17 +0000 | [diff] [blame] | 362 | if (MRI.hasOneDef(Reg)) |
Andrew Trick | 4b72ada | 2012-07-28 01:48:15 +0000 | [diff] [blame] | 363 | return; |
Andrew Trick | cc77b54 | 2012-02-22 06:08:13 +0000 | [diff] [blame] | 364 | |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 365 | // Add output dependence to the next nearest def of this vreg. |
| 366 | // |
| 367 | // Unless this definition is dead, the output dependence should be |
| 368 | // transitively redundant with antidependencies from this definition's |
| 369 | // uses. We're conservative for now until we have a way to guarantee the uses |
| 370 | // are not eliminated sometime during scheduling. The output dependence edge |
| 371 | // is also useful if output latency exceeds def-use latency. |
Andrew Trick | c0ccb8b | 2012-04-20 20:05:28 +0000 | [diff] [blame] | 372 | VReg2SUnitMap::iterator DefI = VRegDefs.find(Reg); |
Andrew Trick | 8ae3ac7 | 2012-02-22 21:59:00 +0000 | [diff] [blame] | 373 | if (DefI == VRegDefs.end()) |
| 374 | VRegDefs.insert(VReg2SUnit(Reg, SU)); |
| 375 | else { |
| 376 | SUnit *DefSU = DefI->SU; |
| 377 | if (DefSU != SU && DefSU != &ExitSU) { |
Andrew Trick | a78d322 | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 378 | SDep Dep(SU, SDep::Output, Reg); |
Andrew Trick | 412cd2f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 379 | unsigned OutLatency = |
| 380 | SchedModel.computeOutputLatency(MI, OperIdx, DefSU->getInstr()); |
Andrew Trick | a78d322 | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 381 | Dep.setMinLatency(OutLatency); |
| 382 | Dep.setLatency(OutLatency); |
| 383 | DefSU->addPred(Dep); |
Andrew Trick | 8ae3ac7 | 2012-02-22 21:59:00 +0000 | [diff] [blame] | 384 | } |
| 385 | DefI->SU = SU; |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 386 | } |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 389 | /// addVRegUseDeps - Add a register data dependency if the instruction that |
| 390 | /// defines the virtual register used at OperIdx is mapped to an SUnit. Add a |
| 391 | /// register antidependency from this SUnit to instructions that occur later in |
| 392 | /// the same scheduling region if they write the virtual register. |
| 393 | /// |
| 394 | /// TODO: Handle ExitSU "uses" properly. |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 395 | void ScheduleDAGInstrs::addVRegUseDeps(SUnit *SU, unsigned OperIdx) { |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 396 | MachineInstr *MI = SU->getInstr(); |
| 397 | unsigned Reg = MI->getOperand(OperIdx).getReg(); |
| 398 | |
| 399 | // Lookup this operand's reaching definition. |
| 400 | assert(LIS && "vreg dependencies requires LiveIntervals"); |
Jakob Stoklund Olesen | 93e29ce | 2012-05-20 02:44:38 +0000 | [diff] [blame] | 401 | LiveRangeQuery LRQ(LIS->getInterval(Reg), LIS->getInstructionIndex(MI)); |
| 402 | VNInfo *VNI = LRQ.valueIn(); |
Andrew Trick | c3ad885 | 2012-04-24 18:04:41 +0000 | [diff] [blame] | 403 | |
Andrew Trick | 63d578b | 2012-02-23 03:16:24 +0000 | [diff] [blame] | 404 | // VNI will be valid because MachineOperand::readsReg() is checked by caller. |
Jakob Stoklund Olesen | 93e29ce | 2012-05-20 02:44:38 +0000 | [diff] [blame] | 405 | assert(VNI && "No value to read by operand"); |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 406 | MachineInstr *Def = LIS->getInstructionFromIndex(VNI->def); |
Andrew Trick | 63d578b | 2012-02-23 03:16:24 +0000 | [diff] [blame] | 407 | // Phis and other noninstructions (after coalescing) have a NULL Def. |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 408 | if (Def) { |
| 409 | SUnit *DefSU = getSUnit(Def); |
| 410 | if (DefSU) { |
| 411 | // The reaching Def lives within this scheduling region. |
| 412 | // Create a data dependence. |
Andrew Trick | a78d322 | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 413 | SDep dep(DefSU, SDep::Data, Reg); |
Andrew Trick | a98f600 | 2012-10-08 18:53:57 +0000 | [diff] [blame] | 414 | // Adjust the dependence latency using operand def/use information, then |
| 415 | // allow the target to perform its own adjustments. |
| 416 | int DefOp = Def->findRegisterDefOperandIdx(Reg); |
| 417 | dep.setLatency( |
| 418 | SchedModel.computeOperandLatency(Def, DefOp, MI, OperIdx, false)); |
| 419 | dep.setMinLatency( |
| 420 | SchedModel.computeOperandLatency(Def, DefOp, MI, OperIdx, true)); |
Andrew Trick | b7e0289 | 2012-06-05 21:11:27 +0000 | [diff] [blame] | 421 | |
Andrew Trick | a98f600 | 2012-10-08 18:53:57 +0000 | [diff] [blame] | 422 | const TargetSubtargetInfo &ST = TM.getSubtarget<TargetSubtargetInfo>(); |
| 423 | ST.adjustSchedDependency(DefSU, SU, const_cast<SDep &>(dep)); |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 424 | SU->addPred(dep); |
| 425 | } |
| 426 | } |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 427 | |
| 428 | // Add antidependence to the following def of the vreg it uses. |
Andrew Trick | c0ccb8b | 2012-04-20 20:05:28 +0000 | [diff] [blame] | 429 | VReg2SUnitMap::iterator DefI = VRegDefs.find(Reg); |
Andrew Trick | 8ae3ac7 | 2012-02-22 21:59:00 +0000 | [diff] [blame] | 430 | if (DefI != VRegDefs.end() && DefI->SU != SU) |
Andrew Trick | a78d322 | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 431 | DefI->SU->addPred(SDep(SU, SDep::Anti, Reg)); |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 432 | } |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 433 | |
Andrew Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 434 | /// Return true if MI is an instruction we are unable to reason about |
| 435 | /// (like a call or something with unmodeled side effects). |
| 436 | static inline bool isGlobalMemoryObject(AliasAnalysis *AA, MachineInstr *MI) { |
| 437 | if (MI->isCall() || MI->hasUnmodeledSideEffects() || |
Jakob Stoklund Olesen | f036f7a | 2012-08-29 21:19:21 +0000 | [diff] [blame] | 438 | (MI->hasOrderedMemoryRef() && |
Andrew Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 439 | (!MI->mayLoad() || !MI->isInvariantLoad(AA)))) |
| 440 | return true; |
| 441 | return false; |
| 442 | } |
| 443 | |
| 444 | // This MI might have either incomplete info, or known to be unsafe |
| 445 | // to deal with (i.e. volatile object). |
| 446 | static inline bool isUnsafeMemoryObject(MachineInstr *MI, |
| 447 | const MachineFrameInfo *MFI) { |
| 448 | if (!MI || MI->memoperands_empty()) |
| 449 | return true; |
| 450 | // We purposefully do no check for hasOneMemOperand() here |
| 451 | // in hope to trigger an assert downstream in order to |
| 452 | // finish implementation. |
| 453 | if ((*MI->memoperands_begin())->isVolatile() || |
| 454 | MI->hasUnmodeledSideEffects()) |
| 455 | return true; |
| 456 | |
| 457 | const Value *V = (*MI->memoperands_begin())->getValue(); |
| 458 | if (!V) |
| 459 | return true; |
| 460 | |
| 461 | V = getUnderlyingObject(V); |
| 462 | if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) { |
| 463 | // Similarly to getUnderlyingObjectForInstr: |
| 464 | // For now, ignore PseudoSourceValues which may alias LLVM IR values |
| 465 | // because the code that uses this function has no way to cope with |
| 466 | // such aliases. |
| 467 | if (PSV->isAliased(MFI)) |
| 468 | return true; |
| 469 | } |
| 470 | // Does this pointer refer to a distinct and identifiable object? |
| 471 | if (!isIdentifiedObject(V)) |
| 472 | return true; |
| 473 | |
| 474 | return false; |
| 475 | } |
| 476 | |
| 477 | /// This returns true if the two MIs need a chain edge betwee them. |
| 478 | /// If these are not even memory operations, we still may need |
| 479 | /// chain deps between them. The question really is - could |
| 480 | /// these two MIs be reordered during scheduling from memory dependency |
| 481 | /// point of view. |
| 482 | static bool MIsNeedChainEdge(AliasAnalysis *AA, const MachineFrameInfo *MFI, |
| 483 | MachineInstr *MIa, |
| 484 | MachineInstr *MIb) { |
| 485 | // Cover a trivial case - no edge is need to itself. |
| 486 | if (MIa == MIb) |
| 487 | return false; |
| 488 | |
| 489 | if (isUnsafeMemoryObject(MIa, MFI) || isUnsafeMemoryObject(MIb, MFI)) |
| 490 | return true; |
| 491 | |
| 492 | // If we are dealing with two "normal" loads, we do not need an edge |
| 493 | // between them - they could be reordered. |
| 494 | if (!MIa->mayStore() && !MIb->mayStore()) |
| 495 | return false; |
| 496 | |
| 497 | // To this point analysis is generic. From here on we do need AA. |
| 498 | if (!AA) |
| 499 | return true; |
| 500 | |
| 501 | MachineMemOperand *MMOa = *MIa->memoperands_begin(); |
| 502 | MachineMemOperand *MMOb = *MIb->memoperands_begin(); |
| 503 | |
| 504 | // FIXME: Need to handle multiple memory operands to support all targets. |
| 505 | if (!MIa->hasOneMemOperand() || !MIb->hasOneMemOperand()) |
| 506 | llvm_unreachable("Multiple memory operands."); |
| 507 | |
| 508 | // The following interface to AA is fashioned after DAGCombiner::isAlias |
| 509 | // and operates with MachineMemOperand offset with some important |
| 510 | // assumptions: |
| 511 | // - LLVM fundamentally assumes flat address spaces. |
| 512 | // - MachineOperand offset can *only* result from legalization and |
| 513 | // cannot affect queries other than the trivial case of overlap |
| 514 | // checking. |
| 515 | // - These offsets never wrap and never step outside |
| 516 | // of allocated objects. |
| 517 | // - There should never be any negative offsets here. |
| 518 | // |
| 519 | // FIXME: Modify API to hide this math from "user" |
| 520 | // FIXME: Even before we go to AA we can reason locally about some |
| 521 | // memory objects. It can save compile time, and possibly catch some |
| 522 | // corner cases not currently covered. |
| 523 | |
| 524 | assert ((MMOa->getOffset() >= 0) && "Negative MachineMemOperand offset"); |
| 525 | assert ((MMOb->getOffset() >= 0) && "Negative MachineMemOperand offset"); |
| 526 | |
| 527 | int64_t MinOffset = std::min(MMOa->getOffset(), MMOb->getOffset()); |
| 528 | int64_t Overlapa = MMOa->getSize() + MMOa->getOffset() - MinOffset; |
| 529 | int64_t Overlapb = MMOb->getSize() + MMOb->getOffset() - MinOffset; |
| 530 | |
| 531 | AliasAnalysis::AliasResult AAResult = AA->alias( |
| 532 | AliasAnalysis::Location(MMOa->getValue(), Overlapa, |
| 533 | MMOa->getTBAAInfo()), |
| 534 | AliasAnalysis::Location(MMOb->getValue(), Overlapb, |
| 535 | MMOb->getTBAAInfo())); |
| 536 | |
| 537 | return (AAResult != AliasAnalysis::NoAlias); |
| 538 | } |
| 539 | |
| 540 | /// This recursive function iterates over chain deps of SUb looking for |
| 541 | /// "latest" node that needs a chain edge to SUa. |
| 542 | static unsigned |
| 543 | iterateChainSucc(AliasAnalysis *AA, const MachineFrameInfo *MFI, |
| 544 | SUnit *SUa, SUnit *SUb, SUnit *ExitSU, unsigned *Depth, |
| 545 | SmallPtrSet<const SUnit*, 16> &Visited) { |
| 546 | if (!SUa || !SUb || SUb == ExitSU) |
| 547 | return *Depth; |
| 548 | |
| 549 | // Remember visited nodes. |
| 550 | if (!Visited.insert(SUb)) |
| 551 | return *Depth; |
| 552 | // If there is _some_ dependency already in place, do not |
| 553 | // descend any further. |
| 554 | // TODO: Need to make sure that if that dependency got eliminated or ignored |
| 555 | // for any reason in the future, we would not violate DAG topology. |
| 556 | // Currently it does not happen, but makes an implicit assumption about |
| 557 | // future implementation. |
| 558 | // |
| 559 | // Independently, if we encounter node that is some sort of global |
| 560 | // object (like a call) we already have full set of dependencies to it |
| 561 | // and we can stop descending. |
| 562 | if (SUa->isSucc(SUb) || |
| 563 | isGlobalMemoryObject(AA, SUb->getInstr())) |
| 564 | return *Depth; |
| 565 | |
| 566 | // If we do need an edge, or we have exceeded depth budget, |
| 567 | // add that edge to the predecessors chain of SUb, |
| 568 | // and stop descending. |
| 569 | if (*Depth > 200 || |
| 570 | MIsNeedChainEdge(AA, MFI, SUa->getInstr(), SUb->getInstr())) { |
Andrew Trick | a78d322 | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 571 | SUb->addPred(SDep(SUa, SDep::MayAliasMem)); |
Andrew Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 572 | return *Depth; |
| 573 | } |
| 574 | // Track current depth. |
| 575 | (*Depth)++; |
| 576 | // Iterate over chain dependencies only. |
| 577 | for (SUnit::const_succ_iterator I = SUb->Succs.begin(), E = SUb->Succs.end(); |
| 578 | I != E; ++I) |
| 579 | if (I->isCtrl()) |
| 580 | iterateChainSucc (AA, MFI, SUa, I->getSUnit(), ExitSU, Depth, Visited); |
| 581 | return *Depth; |
| 582 | } |
| 583 | |
| 584 | /// This function assumes that "downward" from SU there exist |
| 585 | /// tail/leaf of already constructed DAG. It iterates downward and |
| 586 | /// checks whether SU can be aliasing any node dominated |
| 587 | /// by it. |
| 588 | static void adjustChainDeps(AliasAnalysis *AA, const MachineFrameInfo *MFI, |
Andrew Trick | 1c2d3c5 | 2012-06-13 02:39:03 +0000 | [diff] [blame] | 589 | SUnit *SU, SUnit *ExitSU, std::set<SUnit *> &CheckList, |
| 590 | unsigned LatencyToLoad) { |
Andrew Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 591 | if (!SU) |
| 592 | return; |
| 593 | |
| 594 | SmallPtrSet<const SUnit*, 16> Visited; |
| 595 | unsigned Depth = 0; |
| 596 | |
| 597 | for (std::set<SUnit *>::iterator I = CheckList.begin(), IE = CheckList.end(); |
| 598 | I != IE; ++I) { |
| 599 | if (SU == *I) |
| 600 | continue; |
Andrew Trick | 1c2d3c5 | 2012-06-13 02:39:03 +0000 | [diff] [blame] | 601 | if (MIsNeedChainEdge(AA, MFI, SU->getInstr(), (*I)->getInstr())) { |
Andrew Trick | a78d322 | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 602 | SDep Dep(SU, SDep::MayAliasMem); |
| 603 | Dep.setLatency(((*I)->getInstr()->mayLoad()) ? LatencyToLoad : 0); |
| 604 | (*I)->addPred(Dep); |
Andrew Trick | 1c2d3c5 | 2012-06-13 02:39:03 +0000 | [diff] [blame] | 605 | } |
Andrew Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 606 | // Now go through all the chain successors and iterate from them. |
| 607 | // Keep track of visited nodes. |
| 608 | for (SUnit::const_succ_iterator J = (*I)->Succs.begin(), |
| 609 | JE = (*I)->Succs.end(); J != JE; ++J) |
| 610 | if (J->isCtrl()) |
| 611 | iterateChainSucc (AA, MFI, SU, J->getSUnit(), |
| 612 | ExitSU, &Depth, Visited); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | /// Check whether two objects need a chain edge, if so, add it |
| 617 | /// otherwise remember the rejected SU. |
| 618 | static inline |
| 619 | void addChainDependency (AliasAnalysis *AA, const MachineFrameInfo *MFI, |
| 620 | SUnit *SUa, SUnit *SUb, |
| 621 | std::set<SUnit *> &RejectList, |
| 622 | unsigned TrueMemOrderLatency = 0, |
| 623 | bool isNormalMemory = false) { |
| 624 | // If this is a false dependency, |
| 625 | // do not add the edge, but rememeber the rejected node. |
| 626 | if (!EnableAASchedMI || |
Andrew Trick | a78d322 | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 627 | MIsNeedChainEdge(AA, MFI, SUa->getInstr(), SUb->getInstr())) { |
| 628 | SDep Dep(SUa, isNormalMemory ? SDep::MayAliasMem : SDep::Barrier); |
| 629 | Dep.setLatency(TrueMemOrderLatency); |
| 630 | SUb->addPred(Dep); |
| 631 | } |
Andrew Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 632 | else { |
| 633 | // Duplicate entries should be ignored. |
| 634 | RejectList.insert(SUb); |
| 635 | DEBUG(dbgs() << "\tReject chain dep between SU(" |
| 636 | << SUa->NodeNum << ") and SU(" |
| 637 | << SUb->NodeNum << ")\n"); |
| 638 | } |
| 639 | } |
| 640 | |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 641 | /// Create an SUnit for each real instruction, numbered in top-down toplological |
| 642 | /// order. The instruction order A < B, implies that no edge exists from B to A. |
| 643 | /// |
| 644 | /// Map each real instruction to its SUnit. |
| 645 | /// |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 646 | /// After initSUnits, the SUnits vector cannot be resized and the scheduler may |
| 647 | /// hang onto SUnit pointers. We may relax this in the future by using SUnit IDs |
| 648 | /// instead of pointers. |
| 649 | /// |
| 650 | /// MachineScheduler relies on initSUnits numbering the nodes by their order in |
| 651 | /// the original instruction list. |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 652 | void ScheduleDAGInstrs::initSUnits() { |
| 653 | // We'll be allocating one SUnit for each real instruction in the region, |
| 654 | // which is contained within a basic block. |
| 655 | SUnits.reserve(BB->size()); |
| 656 | |
Andrew Trick | 68675c6 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 657 | for (MachineBasicBlock::iterator I = RegionBegin; I != RegionEnd; ++I) { |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 658 | MachineInstr *MI = I; |
| 659 | if (MI->isDebugValue()) |
| 660 | continue; |
| 661 | |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 662 | SUnit *SU = newSUnit(MI); |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 663 | MISUnitMap[MI] = SU; |
| 664 | |
| 665 | SU->isCall = MI->isCall(); |
| 666 | SU->isCommutable = MI->isCommutable(); |
| 667 | |
| 668 | // Assign the Latency field of SU using target-provided information. |
Andrew Trick | 412cd2f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 669 | SU->Latency = SchedModel.computeInstrLatency(SU->getInstr()); |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 670 | } |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 673 | /// If RegPressure is non null, compute register pressure as a side effect. The |
| 674 | /// DAG builder is an efficient place to do it because it already visits |
| 675 | /// operands. |
| 676 | void ScheduleDAGInstrs::buildSchedGraph(AliasAnalysis *AA, |
| 677 | RegPressureTracker *RPTracker) { |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 678 | // Create an SUnit for each real instruction. |
| 679 | initSUnits(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 680 | |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 681 | // We build scheduling units by walking a block's instruction list from bottom |
| 682 | // to top. |
| 683 | |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 684 | // Remember where a generic side-effecting instruction is as we procede. |
| 685 | SUnit *BarrierChain = 0, *AliasChain = 0; |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 686 | |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 687 | // Memory references to specific known memory locations are tracked |
| 688 | // so that they can be given more precise dependencies. We track |
| 689 | // separately the known memory locations that may alias and those |
| 690 | // that are known not to alias |
Sergei Larin | 009cf9e | 2012-11-15 17:45:50 +0000 | [diff] [blame] | 691 | MapVector<const Value *, SUnit *> AliasMemDefs, NonAliasMemDefs; |
| 692 | MapVector<const Value *, std::vector<SUnit *> > AliasMemUses, NonAliasMemUses; |
Andrew Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 693 | std::set<SUnit*> RejectMemNodes; |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 694 | |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 695 | // Remove any stale debug info; sometimes BuildSchedGraph is called again |
| 696 | // without emitting the info from the previous call. |
Devang Patel | cf4cc84 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 697 | DbgValues.clear(); |
| 698 | FirstDbgValue = NULL; |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 699 | |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 700 | assert(Defs.empty() && Uses.empty() && |
| 701 | "Only BuildGraph should update Defs/Uses"); |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 702 | Defs.setRegLimit(TRI->getNumRegs()); |
| 703 | Uses.setRegLimit(TRI->getNumRegs()); |
Andrew Trick | 9b66853 | 2011-05-06 21:52:52 +0000 | [diff] [blame] | 704 | |
Andrew Trick | 8ae3ac7 | 2012-02-22 21:59:00 +0000 | [diff] [blame] | 705 | assert(VRegDefs.empty() && "Only BuildSchedGraph may access VRegDefs"); |
| 706 | // FIXME: Allow SparseSet to reserve space for the creation of virtual |
| 707 | // registers during scheduling. Don't artificially inflate the Universe |
| 708 | // because we want to assert that vregs are not created during DAG building. |
| 709 | VRegDefs.setUniverse(MRI.getNumVirtRegs()); |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 710 | |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 711 | // Model data dependencies between instructions being scheduled and the |
| 712 | // ExitSU. |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 713 | addSchedBarrierDeps(); |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 714 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 715 | // Walk the list of instructions, from bottom moving up. |
Devang Patel | cf4cc84 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 716 | MachineInstr *PrevMI = NULL; |
Andrew Trick | 68675c6 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 717 | for (MachineBasicBlock::iterator MII = RegionEnd, MIE = RegionBegin; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 718 | MII != MIE; --MII) { |
| 719 | MachineInstr *MI = prior(MII); |
Devang Patel | cf4cc84 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 720 | if (MI && PrevMI) { |
| 721 | DbgValues.push_back(std::make_pair(PrevMI, MI)); |
| 722 | PrevMI = NULL; |
| 723 | } |
| 724 | |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 725 | if (MI->isDebugValue()) { |
Devang Patel | cf4cc84 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 726 | PrevMI = MI; |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 727 | continue; |
| 728 | } |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 729 | if (RPTracker) { |
| 730 | RPTracker->recede(); |
| 731 | assert(RPTracker->getPos() == prior(MII) && "RPTracker can't find MI"); |
| 732 | } |
Devang Patel | cf4cc84 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 733 | |
Andrew Trick | 0070792 | 2012-04-13 23:29:54 +0000 | [diff] [blame] | 734 | assert((!MI->isTerminator() || CanHandleTerminators) && !MI->isLabel() && |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 735 | "Cannot schedule terminators or labels!"); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 736 | |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 737 | SUnit *SU = MISUnitMap[MI]; |
| 738 | assert(SU && "No SUnit mapped to this MI"); |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 739 | |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 740 | // Add register-based dependencies (data, anti, and output). |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 741 | for (unsigned j = 0, n = MI->getNumOperands(); j != n; ++j) { |
| 742 | const MachineOperand &MO = MI->getOperand(j); |
| 743 | if (!MO.isReg()) continue; |
| 744 | unsigned Reg = MO.getReg(); |
| 745 | if (Reg == 0) continue; |
| 746 | |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 747 | if (TRI->isPhysicalRegister(Reg)) |
| 748 | addPhysRegDeps(SU, j); |
| 749 | else { |
| 750 | assert(!IsPostRA && "Virtual register encountered!"); |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 751 | if (MO.isDef()) |
| 752 | addVRegDefDeps(SU, j); |
Andrew Trick | 63d578b | 2012-02-23 03:16:24 +0000 | [diff] [blame] | 753 | else if (MO.readsReg()) // ignore undef operands |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 754 | addVRegUseDeps(SU, j); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 755 | } |
| 756 | } |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 757 | |
| 758 | // Add chain dependencies. |
David Goodwin | 7c9b1ac | 2009-11-02 17:06:28 +0000 | [diff] [blame] | 759 | // Chain dependencies used to enforce memory order should have |
| 760 | // latency of 0 (except for true dependency of Store followed by |
| 761 | // aliased Load... we estimate that with a single cycle of latency |
| 762 | // assuming the hardware will bypass) |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 763 | // Note that isStoreToStackSlot and isLoadFromStackSLot are not usable |
| 764 | // after stack slots are lowered to actual addresses. |
| 765 | // TODO: Use an AliasAnalysis and do real alias-analysis queries, and |
| 766 | // produce more precise dependence information. |
Andrew Trick | 1c2d3c5 | 2012-06-13 02:39:03 +0000 | [diff] [blame] | 767 | unsigned TrueMemOrderLatency = MI->mayStore() ? 1 : 0; |
Andrew Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 768 | if (isGlobalMemoryObject(AA, MI)) { |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 769 | // Be conservative with these and add dependencies on all memory |
| 770 | // references, even those that are known to not alias. |
Sergei Larin | 009cf9e | 2012-11-15 17:45:50 +0000 | [diff] [blame] | 771 | for (MapVector<const Value *, SUnit *>::iterator I = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 772 | NonAliasMemDefs.begin(), E = NonAliasMemDefs.end(); I != E; ++I) { |
Andrew Trick | a78d322 | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 773 | I->second->addPred(SDep(SU, SDep::Barrier)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 774 | } |
Sergei Larin | 009cf9e | 2012-11-15 17:45:50 +0000 | [diff] [blame] | 775 | for (MapVector<const Value *, std::vector<SUnit *> >::iterator I = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 776 | NonAliasMemUses.begin(), E = NonAliasMemUses.end(); I != E; ++I) { |
Andrew Trick | a78d322 | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 777 | for (unsigned i = 0, e = I->second.size(); i != e; ++i) { |
| 778 | SDep Dep(SU, SDep::Barrier); |
| 779 | Dep.setLatency(TrueMemOrderLatency); |
| 780 | I->second[i]->addPred(Dep); |
| 781 | } |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 782 | } |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 783 | // Add SU to the barrier chain. |
| 784 | if (BarrierChain) |
Andrew Trick | a78d322 | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 785 | BarrierChain->addPred(SDep(SU, SDep::Barrier)); |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 786 | BarrierChain = SU; |
Andrew Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 787 | // This is a barrier event that acts as a pivotal node in the DAG, |
| 788 | // so it is safe to clear list of exposed nodes. |
Andrew Trick | 1c2d3c5 | 2012-06-13 02:39:03 +0000 | [diff] [blame] | 789 | adjustChainDeps(AA, MFI, SU, &ExitSU, RejectMemNodes, |
| 790 | TrueMemOrderLatency); |
Andrew Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 791 | RejectMemNodes.clear(); |
| 792 | NonAliasMemDefs.clear(); |
| 793 | NonAliasMemUses.clear(); |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 794 | |
| 795 | // fall-through |
| 796 | new_alias_chain: |
| 797 | // Chain all possibly aliasing memory references though SU. |
Andrew Trick | 1c2d3c5 | 2012-06-13 02:39:03 +0000 | [diff] [blame] | 798 | if (AliasChain) { |
| 799 | unsigned ChainLatency = 0; |
| 800 | if (AliasChain->getInstr()->mayLoad()) |
| 801 | ChainLatency = TrueMemOrderLatency; |
| 802 | addChainDependency(AA, MFI, SU, AliasChain, RejectMemNodes, |
| 803 | ChainLatency); |
| 804 | } |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 805 | AliasChain = SU; |
| 806 | for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k) |
Andrew Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 807 | addChainDependency(AA, MFI, SU, PendingLoads[k], RejectMemNodes, |
| 808 | TrueMemOrderLatency); |
Sergei Larin | 009cf9e | 2012-11-15 17:45:50 +0000 | [diff] [blame] | 809 | for (MapVector<const Value *, SUnit *>::iterator I = AliasMemDefs.begin(), |
Andrew Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 810 | E = AliasMemDefs.end(); I != E; ++I) |
| 811 | addChainDependency(AA, MFI, SU, I->second, RejectMemNodes); |
Sergei Larin | 009cf9e | 2012-11-15 17:45:50 +0000 | [diff] [blame] | 812 | for (MapVector<const Value *, std::vector<SUnit *> >::iterator I = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 813 | AliasMemUses.begin(), E = AliasMemUses.end(); I != E; ++I) { |
| 814 | for (unsigned i = 0, e = I->second.size(); i != e; ++i) |
Andrew Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 815 | addChainDependency(AA, MFI, SU, I->second[i], RejectMemNodes, |
| 816 | TrueMemOrderLatency); |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 817 | } |
Andrew Trick | 1c2d3c5 | 2012-06-13 02:39:03 +0000 | [diff] [blame] | 818 | adjustChainDeps(AA, MFI, SU, &ExitSU, RejectMemNodes, |
| 819 | TrueMemOrderLatency); |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 820 | PendingLoads.clear(); |
| 821 | AliasMemDefs.clear(); |
| 822 | AliasMemUses.clear(); |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 823 | } else if (MI->mayStore()) { |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 824 | bool MayAlias = true; |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 825 | if (const Value *V = getUnderlyingObjectForInstr(MI, MFI, MayAlias)) { |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 826 | // A store to a specific PseudoSourceValue. Add precise dependencies. |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 827 | // Record the def in MemDefs, first adding a dep if there is |
| 828 | // an existing def. |
Sergei Larin | 009cf9e | 2012-11-15 17:45:50 +0000 | [diff] [blame] | 829 | MapVector<const Value *, SUnit *>::iterator I = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 830 | ((MayAlias) ? AliasMemDefs.find(V) : NonAliasMemDefs.find(V)); |
Sergei Larin | 009cf9e | 2012-11-15 17:45:50 +0000 | [diff] [blame] | 831 | MapVector<const Value *, SUnit *>::iterator IE = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 832 | ((MayAlias) ? AliasMemDefs.end() : NonAliasMemDefs.end()); |
| 833 | if (I != IE) { |
Sergei Larin | 009cf9e | 2012-11-15 17:45:50 +0000 | [diff] [blame] | 834 | addChainDependency(AA, MFI, SU, I->second, RejectMemNodes, 0, true); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 835 | I->second = SU; |
| 836 | } else { |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 837 | if (MayAlias) |
| 838 | AliasMemDefs[V] = SU; |
| 839 | else |
| 840 | NonAliasMemDefs[V] = SU; |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 841 | } |
| 842 | // Handle the uses in MemUses, if there are any. |
Sergei Larin | 009cf9e | 2012-11-15 17:45:50 +0000 | [diff] [blame] | 843 | MapVector<const Value *, std::vector<SUnit *> >::iterator J = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 844 | ((MayAlias) ? AliasMemUses.find(V) : NonAliasMemUses.find(V)); |
Sergei Larin | 009cf9e | 2012-11-15 17:45:50 +0000 | [diff] [blame] | 845 | MapVector<const Value *, std::vector<SUnit *> >::iterator JE = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 846 | ((MayAlias) ? AliasMemUses.end() : NonAliasMemUses.end()); |
| 847 | if (J != JE) { |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 848 | for (unsigned i = 0, e = J->second.size(); i != e; ++i) |
Andrew Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 849 | addChainDependency(AA, MFI, SU, J->second[i], RejectMemNodes, |
| 850 | TrueMemOrderLatency, true); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 851 | J->second.clear(); |
| 852 | } |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 853 | if (MayAlias) { |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 854 | // Add dependencies from all the PendingLoads, i.e. loads |
| 855 | // with no underlying object. |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 856 | for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k) |
Andrew Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 857 | addChainDependency(AA, MFI, SU, PendingLoads[k], RejectMemNodes, |
| 858 | TrueMemOrderLatency); |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 859 | // Add dependence on alias chain, if needed. |
| 860 | if (AliasChain) |
Andrew Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 861 | addChainDependency(AA, MFI, SU, AliasChain, RejectMemNodes); |
| 862 | // But we also should check dependent instructions for the |
| 863 | // SU in question. |
Andrew Trick | 1c2d3c5 | 2012-06-13 02:39:03 +0000 | [diff] [blame] | 864 | adjustChainDeps(AA, MFI, SU, &ExitSU, RejectMemNodes, |
| 865 | TrueMemOrderLatency); |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 866 | } |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 867 | // Add dependence on barrier chain, if needed. |
Andrew Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 868 | // There is no point to check aliasing on barrier event. Even if |
| 869 | // SU and barrier _could_ be reordered, they should not. In addition, |
| 870 | // we have lost all RejectMemNodes below barrier. |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 871 | if (BarrierChain) |
Andrew Trick | a78d322 | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 872 | BarrierChain->addPred(SDep(SU, SDep::Barrier)); |
David Goodwin | 5be870a | 2009-11-05 00:16:44 +0000 | [diff] [blame] | 873 | } else { |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 874 | // Treat all other stores conservatively. |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 875 | goto new_alias_chain; |
David Goodwin | 7c9b1ac | 2009-11-02 17:06:28 +0000 | [diff] [blame] | 876 | } |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 877 | |
| 878 | if (!ExitSU.isPred(SU)) |
| 879 | // Push store's up a bit to avoid them getting in between cmp |
| 880 | // and branches. |
Andrew Trick | a78d322 | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 881 | ExitSU.addPred(SDep(SU, SDep::Artificial)); |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 882 | } else if (MI->mayLoad()) { |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 883 | bool MayAlias = true; |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 884 | if (MI->isInvariantLoad(AA)) { |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 885 | // Invariant load, no chain dependencies needed! |
David Goodwin | 5be870a | 2009-11-05 00:16:44 +0000 | [diff] [blame] | 886 | } else { |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 887 | if (const Value *V = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 888 | getUnderlyingObjectForInstr(MI, MFI, MayAlias)) { |
| 889 | // A load from a specific PseudoSourceValue. Add precise dependencies. |
Sergei Larin | 009cf9e | 2012-11-15 17:45:50 +0000 | [diff] [blame] | 890 | MapVector<const Value *, SUnit *>::iterator I = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 891 | ((MayAlias) ? AliasMemDefs.find(V) : NonAliasMemDefs.find(V)); |
Sergei Larin | 009cf9e | 2012-11-15 17:45:50 +0000 | [diff] [blame] | 892 | MapVector<const Value *, SUnit *>::iterator IE = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 893 | ((MayAlias) ? AliasMemDefs.end() : NonAliasMemDefs.end()); |
| 894 | if (I != IE) |
Andrew Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 895 | addChainDependency(AA, MFI, SU, I->second, RejectMemNodes, 0, true); |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 896 | if (MayAlias) |
| 897 | AliasMemUses[V].push_back(SU); |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 898 | else |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 899 | NonAliasMemUses[V].push_back(SU); |
| 900 | } else { |
| 901 | // A load with no underlying object. Depend on all |
| 902 | // potentially aliasing stores. |
Sergei Larin | 009cf9e | 2012-11-15 17:45:50 +0000 | [diff] [blame] | 903 | for (MapVector<const Value *, SUnit *>::iterator I = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 904 | AliasMemDefs.begin(), E = AliasMemDefs.end(); I != E; ++I) |
Andrew Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 905 | addChainDependency(AA, MFI, SU, I->second, RejectMemNodes); |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 906 | |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 907 | PendingLoads.push_back(SU); |
| 908 | MayAlias = true; |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 909 | } |
Andrew Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 910 | if (MayAlias) |
Andrew Trick | 1c2d3c5 | 2012-06-13 02:39:03 +0000 | [diff] [blame] | 911 | adjustChainDeps(AA, MFI, SU, &ExitSU, RejectMemNodes, /*Latency=*/0); |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 912 | // Add dependencies on alias and barrier chains, if needed. |
| 913 | if (MayAlias && AliasChain) |
Andrew Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 914 | addChainDependency(AA, MFI, SU, AliasChain, RejectMemNodes); |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 915 | if (BarrierChain) |
Andrew Trick | a78d322 | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 916 | BarrierChain->addPred(SDep(SU, SDep::Barrier)); |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 917 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 918 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 919 | } |
Devang Patel | cf4cc84 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 920 | if (PrevMI) |
| 921 | FirstDbgValue = PrevMI; |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 922 | |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 923 | Defs.clear(); |
| 924 | Uses.clear(); |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 925 | VRegDefs.clear(); |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 926 | PendingLoads.clear(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 927 | } |
| 928 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 929 | void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const { |
Manman Ren | b720be6 | 2012-09-11 22:23:19 +0000 | [diff] [blame] | 930 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 931 | SU->getInstr()->dump(); |
Manman Ren | 77e300e | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 932 | #endif |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const { |
| 936 | std::string s; |
| 937 | raw_string_ostream oss(s); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 938 | if (SU == &EntrySU) |
| 939 | oss << "<entry>"; |
| 940 | else if (SU == &ExitSU) |
| 941 | oss << "<exit>"; |
| 942 | else |
| 943 | SU->getInstr()->print(oss); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 944 | return oss.str(); |
| 945 | } |
| 946 | |
Andrew Trick | 56b94c5 | 2012-03-07 00:18:22 +0000 | [diff] [blame] | 947 | /// Return the basic block label. It is not necessarilly unique because a block |
| 948 | /// contains multiple scheduling regions. But it is fine for visualization. |
| 949 | std::string ScheduleDAGInstrs::getDAGName() const { |
| 950 | return "dag." + BB->getFullName(); |
| 951 | } |
Andrew Trick | 1e94e98 | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 952 | |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 953 | //===----------------------------------------------------------------------===// |
| 954 | // SchedDFSResult Implementation |
| 955 | //===----------------------------------------------------------------------===// |
| 956 | |
| 957 | namespace llvm { |
| 958 | /// \brief Internal state used to compute SchedDFSResult. |
| 959 | class SchedDFSImpl { |
| 960 | SchedDFSResult &R; |
| 961 | |
| 962 | /// Join DAG nodes into equivalence classes by their subtree. |
| 963 | IntEqClasses SubtreeClasses; |
| 964 | /// List PredSU, SuccSU pairs that represent data edges between subtrees. |
| 965 | std::vector<std::pair<const SUnit*, const SUnit*> > ConnectionPairs; |
| 966 | |
| 967 | public: |
| 968 | SchedDFSImpl(SchedDFSResult &r): R(r), SubtreeClasses(R.DFSData.size()) {} |
| 969 | |
| 970 | /// SubtreID is initialized to zero, set to itself to flag the root of a |
| 971 | /// subtree, set to the parent to indicate an interior node, |
| 972 | /// then set to a representative subtree ID during finalization. |
| 973 | bool isVisited(const SUnit *SU) const { |
| 974 | return R.DFSData[SU->NodeNum].SubtreeID; |
| 975 | } |
| 976 | |
| 977 | /// Initialize this node's instruction count. We don't need to flag the node |
| 978 | /// visited until visitPostorder because the DAG cannot have cycles. |
| 979 | void visitPreorder(const SUnit *SU) { |
| 980 | R.DFSData[SU->NodeNum].InstrCount = SU->getInstr()->isTransient() ? 0 : 1; |
| 981 | } |
| 982 | |
| 983 | /// Mark this node as either the root of a subtree or an interior |
| 984 | /// node. Increment the parent node's instruction count. |
| 985 | void visitPostorder(const SUnit *SU, const SDep *PredDep, const SUnit *Parent) { |
| 986 | R.DFSData[SU->NodeNum].SubtreeID = SU->NodeNum; |
| 987 | |
| 988 | // Join the child to its parent if they are connected via data dependence |
| 989 | // and do not exceed the limit. |
| 990 | if (!Parent || PredDep->getKind() != SDep::Data) |
| 991 | return; |
| 992 | |
| 993 | unsigned PredCnt = R.DFSData[SU->NodeNum].InstrCount; |
| 994 | if (PredCnt > R.SubtreeLimit) |
| 995 | return; |
| 996 | |
| 997 | R.DFSData[SU->NodeNum].SubtreeID = Parent->NodeNum; |
| 998 | |
| 999 | // Add the recently finished predecessor's bottom-up descendent count. |
| 1000 | R.DFSData[Parent->NodeNum].InstrCount += PredCnt; |
| 1001 | SubtreeClasses.join(Parent->NodeNum, SU->NodeNum); |
| 1002 | } |
| 1003 | |
| 1004 | /// Determine whether the DFS cross edge should be considered a subtree edge |
| 1005 | /// or a connection between subtrees. |
| 1006 | void visitCross(const SDep &PredDep, const SUnit *Succ) { |
| 1007 | if (PredDep.getKind() == SDep::Data) { |
| 1008 | // If this is a cross edge to a root, join the subtrees. This happens when |
| 1009 | // the root was first reached by a non-data dependence. |
| 1010 | unsigned NodeNum = PredDep.getSUnit()->NodeNum; |
| 1011 | unsigned PredCnt = R.DFSData[NodeNum].InstrCount; |
| 1012 | if (R.DFSData[NodeNum].SubtreeID == NodeNum && PredCnt < R.SubtreeLimit) { |
| 1013 | R.DFSData[NodeNum].SubtreeID = Succ->NodeNum; |
| 1014 | R.DFSData[Succ->NodeNum].InstrCount += PredCnt; |
| 1015 | SubtreeClasses.join(Succ->NodeNum, NodeNum); |
| 1016 | return; |
| 1017 | } |
| 1018 | } |
| 1019 | ConnectionPairs.push_back(std::make_pair(PredDep.getSUnit(), Succ)); |
| 1020 | } |
| 1021 | |
| 1022 | /// Set each node's subtree ID to the representative ID and record connections |
| 1023 | /// between trees. |
| 1024 | void finalize() { |
| 1025 | SubtreeClasses.compress(); |
| 1026 | R.SubtreeConnections.resize(SubtreeClasses.getNumClasses()); |
| 1027 | R.SubtreeConnectLevels.resize(SubtreeClasses.getNumClasses()); |
| 1028 | DEBUG(dbgs() << R.getNumSubtrees() << " subtrees:\n"); |
| 1029 | for (unsigned Idx = 0, End = R.DFSData.size(); Idx != End; ++Idx) { |
| 1030 | R.DFSData[Idx].SubtreeID = SubtreeClasses[Idx]; |
| 1031 | DEBUG(dbgs() << " SU(" << Idx << ") in tree " |
| 1032 | << R.DFSData[Idx].SubtreeID << '\n'); |
| 1033 | } |
| 1034 | for (std::vector<std::pair<const SUnit*, const SUnit*> >::const_iterator |
| 1035 | I = ConnectionPairs.begin(), E = ConnectionPairs.end(); |
| 1036 | I != E; ++I) { |
| 1037 | unsigned PredTree = SubtreeClasses[I->first->NodeNum]; |
| 1038 | unsigned SuccTree = SubtreeClasses[I->second->NodeNum]; |
| 1039 | if (PredTree == SuccTree) |
| 1040 | continue; |
| 1041 | unsigned Depth = I->first->getDepth(); |
| 1042 | addConnection(PredTree, SuccTree, Depth); |
| 1043 | addConnection(SuccTree, PredTree, Depth); |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | protected: |
| 1048 | /// Called by finalize() to record a connection between trees. |
| 1049 | void addConnection(unsigned FromTree, unsigned ToTree, unsigned Depth) { |
| 1050 | if (!Depth) |
| 1051 | return; |
| 1052 | |
| 1053 | SmallVectorImpl<SchedDFSResult::Connection> &Connections = |
| 1054 | R.SubtreeConnections[FromTree]; |
| 1055 | for (SmallVectorImpl<SchedDFSResult::Connection>::iterator |
| 1056 | I = Connections.begin(), E = Connections.end(); I != E; ++I) { |
| 1057 | if (I->TreeID == ToTree) { |
| 1058 | I->Level = std::max(I->Level, Depth); |
| 1059 | return; |
| 1060 | } |
| 1061 | } |
| 1062 | Connections.push_back(SchedDFSResult::Connection(ToTree, Depth)); |
| 1063 | } |
| 1064 | }; |
| 1065 | } // namespace llvm |
| 1066 | |
Andrew Trick | 1e94e98 | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1067 | namespace { |
| 1068 | /// \brief Manage the stack used by a reverse depth-first search over the DAG. |
| 1069 | class SchedDAGReverseDFS { |
| 1070 | std::vector<std::pair<const SUnit*, SUnit::const_pred_iterator> > DFSStack; |
| 1071 | public: |
| 1072 | bool isComplete() const { return DFSStack.empty(); } |
| 1073 | |
| 1074 | void follow(const SUnit *SU) { |
| 1075 | DFSStack.push_back(std::make_pair(SU, SU->Preds.begin())); |
| 1076 | } |
| 1077 | void advance() { ++DFSStack.back().second; } |
| 1078 | |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1079 | const SDep *backtrack() { |
| 1080 | DFSStack.pop_back(); |
| 1081 | return DFSStack.empty() ? 0 : llvm::prior(DFSStack.back().second); |
| 1082 | } |
Andrew Trick | 1e94e98 | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1083 | |
| 1084 | const SUnit *getCurr() const { return DFSStack.back().first; } |
| 1085 | |
| 1086 | SUnit::const_pred_iterator getPred() const { return DFSStack.back().second; } |
| 1087 | |
| 1088 | SUnit::const_pred_iterator getPredEnd() const { |
| 1089 | return getCurr()->Preds.end(); |
| 1090 | } |
| 1091 | }; |
| 1092 | } // anonymous |
| 1093 | |
Andrew Trick | 1e94e98 | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1094 | /// Compute an ILP metric for all nodes in the subDAG reachable via depth-first |
| 1095 | /// search from this root. |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1096 | void SchedDFSResult::compute(ArrayRef<SUnit *> Roots) { |
Andrew Trick | 1e94e98 | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1097 | if (!IsBottomUp) |
| 1098 | llvm_unreachable("Top-down ILP metric is unimplemnted"); |
| 1099 | |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1100 | SchedDFSImpl Impl(*this); |
| 1101 | for (ArrayRef<const SUnit*>::const_iterator |
| 1102 | RootI = Roots.begin(), RootE = Roots.end(); RootI != RootE; ++RootI) { |
| 1103 | SchedDAGReverseDFS DFS; |
| 1104 | Impl.visitPreorder(*RootI); |
| 1105 | DFS.follow(*RootI); |
| 1106 | for (;;) { |
| 1107 | // Traverse the leftmost path as far as possible. |
| 1108 | while (DFS.getPred() != DFS.getPredEnd()) { |
| 1109 | const SDep &PredDep = *DFS.getPred(); |
| 1110 | DFS.advance(); |
| 1111 | // If the pred is already valid, skip it. We may preorder visit a node |
| 1112 | // with InstrCount==0 more than once, but it won't affect heuristics |
| 1113 | // because we don't care about cross edges to leaf copies. |
| 1114 | if (Impl.isVisited(PredDep.getSUnit())) { |
| 1115 | Impl.visitCross(PredDep, DFS.getCurr()); |
| 1116 | continue; |
| 1117 | } |
| 1118 | Impl.visitPreorder(PredDep.getSUnit()); |
| 1119 | DFS.follow(PredDep.getSUnit()); |
| 1120 | } |
| 1121 | // Visit the top of the stack in postorder and backtrack. |
| 1122 | const SUnit *Child = DFS.getCurr(); |
| 1123 | const SDep *PredDep = DFS.backtrack(); |
| 1124 | Impl.visitPostorder(Child, PredDep, PredDep ? DFS.getCurr() : 0); |
| 1125 | if (DFS.isComplete()) |
| 1126 | break; |
Andrew Trick | 1e94e98 | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1127 | } |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1128 | } |
| 1129 | Impl.finalize(); |
| 1130 | } |
| 1131 | |
| 1132 | /// The root of the given SubtreeID was just scheduled. For all subtrees |
| 1133 | /// connected to this tree, record the depth of the connection so that the |
| 1134 | /// nearest connected subtrees can be prioritized. |
| 1135 | void SchedDFSResult::scheduleTree(unsigned SubtreeID) { |
| 1136 | for (SmallVectorImpl<Connection>::const_iterator |
| 1137 | I = SubtreeConnections[SubtreeID].begin(), |
| 1138 | E = SubtreeConnections[SubtreeID].end(); I != E; ++I) { |
| 1139 | SubtreeConnectLevels[I->TreeID] = |
| 1140 | std::max(SubtreeConnectLevels[I->TreeID], I->Level); |
| 1141 | DEBUG(dbgs() << " Tree: " << I->TreeID |
| 1142 | << " @" << SubtreeConnectLevels[I->TreeID] << '\n'); |
Andrew Trick | 1e94e98 | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 1147 | void ILPValue::print(raw_ostream &OS) const { |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1148 | OS << InstrCount << " / " << Length << " = "; |
| 1149 | if (!Length) |
Andrew Trick | 1e94e98 | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1150 | OS << "BADILP"; |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1151 | else |
| 1152 | OS << format("%g", ((double)InstrCount / Length)); |
Andrew Trick | 1e94e98 | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | void ILPValue::dump() const { |
| 1156 | dbgs() << *this << '\n'; |
| 1157 | } |
| 1158 | |
| 1159 | namespace llvm { |
| 1160 | |
| 1161 | raw_ostream &operator<<(raw_ostream &OS, const ILPValue &Val) { |
| 1162 | Val.print(OS); |
| 1163 | return OS; |
| 1164 | } |
| 1165 | |
| 1166 | } // namespace llvm |
| 1167 | #endif // !NDEBUG || LLVM_ENABLE_DUMP |