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