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