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