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