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